| 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/Analysis/ProfileSummaryInfo.h" |
| 31 | #include "llvm/Analysis/StaticDataProfileInfo.h" |
| 32 | #include "llvm/CodeGen/Passes.h" |
| 33 | #include "llvm/IR/Analysis.h" |
| 34 | #include "llvm/IR/Module.h" |
| 35 | #include "llvm/IR/PassManager.h" |
| 36 | #include "llvm/InitializePasses.h" |
| 37 | #include "llvm/Pass.h" |
| 38 | |
| 39 | #define DEBUG_TYPE "static-data-annotator" |
| 40 | |
| 41 | using namespace llvm; |
| 42 | |
| 43 | /// A module pass which iterates global variables in the module and annotates |
| 44 | /// their section prefixes based on profile-driven analysis. |
| 45 | class StaticDataAnnotator : public ModulePass { |
| 46 | public: |
| 47 | static char ID; |
| 48 | |
| 49 | StaticDataProfileInfo *SDPI = nullptr; |
| 50 | const ProfileSummaryInfo *PSI = nullptr; |
| 51 | |
| 52 | StaticDataAnnotator() : ModulePass(ID) {} |
| 53 | |
| 54 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 55 | AU.addRequired<StaticDataProfileInfoWrapperPass>(); |
| 56 | AU.addRequired<ProfileSummaryInfoWrapperPass>(); |
| 57 | AU.setPreservesAll(); |
| 58 | ModulePass::getAnalysisUsage(AU); |
| 59 | } |
| 60 | |
| 61 | StringRef getPassName() const override { return "Static Data Annotator" ; } |
| 62 | |
| 63 | bool runOnModule(Module &M) override; |
| 64 | }; |
| 65 | |
| 66 | bool StaticDataAnnotator::runOnModule(Module &M) { |
| 67 | SDPI = &getAnalysis<StaticDataProfileInfoWrapperPass>() |
| 68 | .getStaticDataProfileInfo(); |
| 69 | PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); |
| 70 | |
| 71 | if (!PSI->hasProfileSummary()) |
| 72 | return false; |
| 73 | |
| 74 | bool Changed = false; |
| 75 | for (auto &GV : M.globals()) { |
| 76 | if (!llvm::memprof::IsAnnotationOK(GV)) |
| 77 | continue; |
| 78 | |
| 79 | StringRef SectionPrefix = SDPI->getConstantSectionPrefix(C: &GV, PSI); |
| 80 | // setSectionPrefix returns true if the section prefix is updated. |
| 81 | Changed |= GV.setSectionPrefix(SectionPrefix); |
| 82 | } |
| 83 | |
| 84 | return Changed; |
| 85 | } |
| 86 | |
| 87 | char StaticDataAnnotator::ID = 0; |
| 88 | |
| 89 | INITIALIZE_PASS(StaticDataAnnotator, DEBUG_TYPE, "Static Data Annotator" , false, |
| 90 | false) |
| 91 | |
| 92 | ModulePass *llvm::createStaticDataAnnotatorPass() { |
| 93 | return new StaticDataAnnotator(); |
| 94 | } |
| 95 | |