|
| 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 | +#include "llvm/Support/raw_ostream.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 StaticDataAnnotator : public ModulePass { |
| 47 | +public: |
| 48 | + static char ID; |
| 49 | + |
| 50 | + StaticDataProfileInfo *SDPI = nullptr; |
| 51 | + const ProfileSummaryInfo *PSI = nullptr; |
| 52 | + |
| 53 | + StaticDataAnnotator() : ModulePass(ID) { |
| 54 | + initializeStaticDataAnnotatorPass(*PassRegistry::getPassRegistry()); |
| 55 | + } |
| 56 | + |
| 57 | + void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 58 | + AU.addRequired<StaticDataProfileInfoWrapperPass>(); |
| 59 | + AU.addRequired<ProfileSummaryInfoWrapperPass>(); |
| 60 | + AU.setPreservesAll(); |
| 61 | + ModulePass::getAnalysisUsage(AU); |
| 62 | + } |
| 63 | + |
| 64 | + StringRef getPassName() const override { return "Static Data Annotator"; } |
| 65 | + |
| 66 | + bool runOnModule(Module &M) override; |
| 67 | +}; |
| 68 | + |
| 69 | +bool StaticDataAnnotator::runOnModule(Module &M) { |
| 70 | + SDPI = &getAnalysis<StaticDataProfileInfoWrapperPass>() |
| 71 | + .getStaticDataProfileInfo(); |
| 72 | + PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); |
| 73 | + |
| 74 | + if (!PSI->hasProfileSummary()) |
| 75 | + return false; |
| 76 | + |
| 77 | + bool Changed = false; |
| 78 | + for (auto &GV : M.globals()) { |
| 79 | + if (GV.isDeclarationForLinker()) |
| 80 | + continue; |
| 81 | + |
| 82 | + // The implementation below assumes prior passes don't set section prefixes, |
| 83 | + // and specifically do 'assign' rather than 'update'. So report error if a |
| 84 | + // section prefix is already set. |
| 85 | + if (auto maybeSectionPrefix = GV.getSectionPrefix(); |
| 86 | + maybeSectionPrefix && !maybeSectionPrefix->empty()) |
| 87 | + llvm::report_fatal_error("Global variable " + GV.getName() + |
| 88 | + " already has a section prefix " + |
| 89 | + *maybeSectionPrefix); |
| 90 | + |
| 91 | + StringRef SectionPrefix = SDPI->getConstantSectionPrefix(&GV, PSI); |
| 92 | + if (SectionPrefix.empty()) |
| 93 | + continue; |
| 94 | + |
| 95 | + GV.setSectionPrefix(SectionPrefix); |
| 96 | + Changed = true; |
| 97 | + } |
| 98 | + |
| 99 | + return Changed; |
| 100 | +} |
| 101 | + |
| 102 | +char StaticDataAnnotator::ID = 0; |
| 103 | + |
| 104 | +INITIALIZE_PASS(StaticDataAnnotator, DEBUG_TYPE, "Static Data Annotator", false, |
| 105 | + false) |
| 106 | + |
| 107 | +ModulePass *llvm::createStaticDataAnnotatorPass() { |
| 108 | + return new StaticDataAnnotator(); |
| 109 | +} |
0 commit comments