| 1 | //===- StaticDataAnnotator - Annotate static data's section prefix --------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // To reason about module-wide data hotness in a module granularity, this file |
| 10 | // implements a module pass StaticDataAnnotator to work coordinately with the |
| 11 | // StaticDataSplitter pass. |
| 12 | // |
| 13 | // The StaticDataSplitter pass is a machine function pass. It analyzes data |
| 14 | // hotness based on code and adds counters in StaticDataProfileInfo via its |
| 15 | // wrapper pass StaticDataProfileInfoWrapper. |
| 16 | // The StaticDataProfileInfoWrapper sits in the middle between the |
| 17 | // StaticDataSplitter and StaticDataAnnotator passes. |
| 18 | // The StaticDataAnnotator pass is a module pass. It iterates global variables |
| 19 | // in the module, looks up counters from StaticDataProfileInfo and sets the |
| 20 | // section prefix based on profiles. |
| 21 | // |
| 22 | // The three-pass structure is implemented for practical reasons, to work around |
| 23 | // the limitation that a module pass based on legacy pass manager cannot make |
| 24 | // use of MachineBlockFrequencyInfo analysis. In the future, we can consider |
| 25 | // porting the StaticDataSplitter pass to a module-pass using the new pass |
| 26 | // manager framework. That way, analysis are lazily computed as opposed to |
| 27 | // eagerly scheduled, and a module pass can use MachineBlockFrequencyInfo. |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
| 30 | #include "llvm/CodeGen/StaticDataAnnotator.h" |
| 31 | #include "llvm/Analysis/ProfileSummaryInfo.h" |
| 32 | #include "llvm/Analysis/StaticDataProfileInfo.h" |
| 33 | #include "llvm/CodeGen/Passes.h" |
| 34 | #include "llvm/IR/Analysis.h" |
| 35 | #include "llvm/IR/Module.h" |
| 36 | #include "llvm/IR/PassManager.h" |
| 37 | #include "llvm/InitializePasses.h" |
| 38 | #include "llvm/Pass.h" |
| 39 | |
| 40 | #define DEBUG_TYPE "static-data-annotator" |
| 41 | |
| 42 | using namespace llvm; |
| 43 | |
| 44 | /// A module pass which iterates global variables in the module and annotates |
| 45 | /// their section prefixes based on profile-driven analysis. |
| 46 | class StaticDataAnnotatorLegacy : public ModulePass { |
| 47 | public: |
| 48 | static char ID; |
| 49 | |
| 50 | StaticDataAnnotatorLegacy() : ModulePass(ID) {} |
| 51 | |
| 52 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 53 | AU.addRequired<StaticDataProfileInfoWrapperPass>(); |
| 54 | AU.addRequired<ProfileSummaryInfoWrapperPass>(); |
| 55 | AU.setPreservesAll(); |
| 56 | ModulePass::getAnalysisUsage(AU); |
| 57 | } |
| 58 | |
| 59 | StringRef getPassName() const override { return "Static Data Annotator" ; } |
| 60 | |
| 61 | bool runOnModule(Module &M) override; |
| 62 | }; |
| 63 | |
| 64 | static bool annotateModule(Module &M, StaticDataProfileInfo *SDPI, |
| 65 | ProfileSummaryInfo *PSI) { |
| 66 | if (!PSI || !PSI->hasProfileSummary()) |
| 67 | return false; |
| 68 | |
| 69 | bool Changed = false; |
| 70 | for (auto &GV : M.globals()) { |
| 71 | if (!llvm::memprof::IsAnnotationOK(GV)) |
| 72 | continue; |
| 73 | |
| 74 | StringRef SectionPrefix = SDPI->getConstantSectionPrefix(C: &GV, PSI); |
| 75 | // setSectionPrefix returns true if the section prefix is updated. |
| 76 | Changed |= GV.setSectionPrefix(SectionPrefix); |
| 77 | } |
| 78 | |
| 79 | return Changed; |
| 80 | } |
| 81 | |
| 82 | bool StaticDataAnnotatorLegacy::runOnModule(Module &M) { |
| 83 | StaticDataProfileInfo *SDPI = &getAnalysis<StaticDataProfileInfoWrapperPass>() |
| 84 | .getStaticDataProfileInfo(); |
| 85 | ProfileSummaryInfo *PSI = |
| 86 | &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); |
| 87 | return annotateModule(M, SDPI, PSI); |
| 88 | } |
| 89 | |
| 90 | char StaticDataAnnotatorLegacy::ID = 0; |
| 91 | |
| 92 | INITIALIZE_PASS(StaticDataAnnotatorLegacy, DEBUG_TYPE, "Static Data Annotator" , |
| 93 | false, false) |
| 94 | |
| 95 | ModulePass *llvm::createStaticDataAnnotatorLegacyPass() { |
| 96 | return new StaticDataAnnotatorLegacy(); |
| 97 | } |
| 98 | |
| 99 | PreservedAnalyses StaticDataAnnoatorPass::run(Module &M, |
| 100 | ModuleAnalysisManager &MAM) { |
| 101 | StaticDataProfileInfo *SDPI = &MAM.getResult<StaticDataProfileInfoAnalysis>(IR&: M) |
| 102 | .getStaticDataProfileInfo(); |
| 103 | ProfileSummaryInfo *PSI = &MAM.getResult<ProfileSummaryAnalysis>(IR&: M); |
| 104 | return annotateModule(M, SDPI, PSI) |
| 105 | ? PreservedAnalyses::none().preserveSet<CFGAnalyses>() |
| 106 | : PreservedAnalyses::all(); |
| 107 | } |
| 108 | |