|
| 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 the StaticDataProfileInfo. |
| 15 | +// The StaticDataAnnotator pass is a module pass. It iterates global variables |
| 16 | +// in the module, looks up counters from StaticDataProfileInfo and sets the |
| 17 | +// section prefix based on profiles. |
| 18 | +// |
| 19 | +// The three-pass structure is implemented for practical reasons, to work around |
| 20 | +// the limitation that a module pass based on legacy pass manager cannot make |
| 21 | +// use of MachineBlockFrequencyInfo analysis. In the future, we can consider |
| 22 | +// porting the StaticDataSplitter pass to a module-pass using the new pass |
| 23 | +// manager framework. That way, analysis are lazily computed as opposed to |
| 24 | +// eagerly scheduled, and a module pass can use MachineBlockFrequencyInfo. |
| 25 | +//===----------------------------------------------------------------------===// |
| 26 | + |
| 27 | +#include "llvm/Analysis/ProfileSummaryInfo.h" |
| 28 | +#include "llvm/Analysis/StaticDataProfileInfo.h" |
| 29 | +#include "llvm/CodeGen/Passes.h" |
| 30 | +#include "llvm/IR/Analysis.h" |
| 31 | +#include "llvm/IR/Module.h" |
| 32 | +#include "llvm/IR/PassManager.h" |
| 33 | +#include "llvm/InitializePasses.h" |
| 34 | +#include "llvm/Pass.h" |
| 35 | +#include "llvm/Support/raw_ostream.h" |
| 36 | + |
| 37 | +#define DEBUG_TYPE "static-data-annotator" |
| 38 | + |
| 39 | +using namespace llvm; |
| 40 | + |
| 41 | +class StaticDataAnnotator : public ModulePass { |
| 42 | +public: |
| 43 | + static char ID; |
| 44 | + |
| 45 | + StaticDataProfileInfo *SDPI = nullptr; |
| 46 | + const ProfileSummaryInfo *PSI = nullptr; |
| 47 | + |
| 48 | + StaticDataAnnotator() : ModulePass(ID) { |
| 49 | + initializeStaticDataAnnotatorPass(*PassRegistry::getPassRegistry()); |
| 50 | + } |
| 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 | +// Returns true if the global variable already has a section prefix that is the |
| 65 | +// same as `Prefix`. |
| 66 | +static bool alreadyHasSectionPrefix(const GlobalVariable &GV, |
| 67 | + StringRef Prefix) { |
| 68 | + std::optional<StringRef> SectionPrefix = GV.getSectionPrefix(); |
| 69 | + return SectionPrefix && (*SectionPrefix == Prefix); |
| 70 | +} |
| 71 | + |
| 72 | +bool StaticDataAnnotator::runOnModule(Module &M) { |
| 73 | + SDPI = &getAnalysis<StaticDataProfileInfoWrapperPass>() |
| 74 | + .getStaticDataProfileInfo(); |
| 75 | + PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); |
| 76 | + |
| 77 | + if (!PSI->hasProfileSummary()) |
| 78 | + return false; |
| 79 | + |
| 80 | + bool Changed = false; |
| 81 | + for (auto &GV : M.globals()) { |
| 82 | + if (GV.isDeclarationForLinker()) |
| 83 | + continue; |
| 84 | + |
| 85 | + // Skip global variables without profile counts. The module may not be |
| 86 | + // profiled or instrumented. |
| 87 | + auto Count = SDPI->getConstantProfileCount(&GV); |
| 88 | + if (!Count) |
| 89 | + continue; |
| 90 | + |
| 91 | + if (PSI->isHotCount(*Count) && !alreadyHasSectionPrefix(GV, "hot")) { |
| 92 | + // The variable counter is hot, set 'hot' section prefix if the section |
| 93 | + // prefix isn't hot already. |
| 94 | + GV.setSectionPrefix("hot"); |
| 95 | + Changed = true; |
| 96 | + } else if (PSI->isColdCount(*Count) && !SDPI->hasUnknownCount(&GV) && |
| 97 | + !alreadyHasSectionPrefix(GV, "unlikely")) { |
| 98 | + // The variable counter is cold, set 'unlikely' section prefix when |
| 99 | + // 1) the section prefix isn't unlikely already, and |
| 100 | + // 2) the variable is not seen without profile counts. The reason is that |
| 101 | + // a variable without profile counts doesn't have all its uses profiled, |
| 102 | + // for example when a function is not instrumented, or not sampled (new |
| 103 | + // code paths). |
| 104 | + GV.setSectionPrefix("unlikely"); |
| 105 | + Changed = true; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return Changed; |
| 110 | +} |
| 111 | + |
| 112 | +char StaticDataAnnotator::ID = 0; |
| 113 | + |
| 114 | +INITIALIZE_PASS(StaticDataAnnotator, DEBUG_TYPE, "Static Data Annotator", false, |
| 115 | + false) |
| 116 | + |
| 117 | +ModulePass *llvm::createStaticDataAnnotatorPass() { |
| 118 | + return new StaticDataAnnotator(); |
| 119 | +} |
0 commit comments