Skip to content

Commit 1d608fc

Browse files
authored
[NFC][InstrProf] Refactor InstrProfiling lowering pass (#74970)
Akin other passes - refactored the name to `InstrProfilingLoweringPass` to better communicate what it does, and split the pass part and the transformation part to avoid needing to initialize object state during `::run`. A subsequent PR will move `InstrLowering` to the .cpp file and rename it to `InstrLowerer`.
1 parent 9bd32d7 commit 1d608fc

File tree

8 files changed

+97
-92
lines changed

8 files changed

+97
-92
lines changed

clang/lib/CodeGen/BackendUtil.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
982982
getInstrProfOptions(CodeGenOpts, LangOpts))
983983
PB.registerPipelineStartEPCallback(
984984
[Options](ModulePassManager &MPM, OptimizationLevel Level) {
985-
MPM.addPass(InstrProfiling(*Options, false));
985+
MPM.addPass(InstrProfilingLoweringPass(*Options, false));
986986
});
987987

988988
// TODO: Consider passing the MemoryProfileOutput to the pass builder via

clang/test/CodeGen/pgo-instrumentation.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
// Ensure Pass PGOInstrumentationGenPass is invoked.
44
// RUN: %clang_cc1 -O2 -fprofile-instrument=llvm %s -fdebug-pass-manager -emit-llvm -o - 2>&1 | FileCheck %s -check-prefix=CHECK-PGOGENPASS-INVOKED-INSTR-GEN --check-prefix=CHECK-INSTRPROF
55
// CHECK-PGOGENPASS-INVOKED-INSTR-GEN: Running pass: PGOInstrumentationGen on
6-
// CHECK-INSTRPROF: Running pass: InstrProfiling on
6+
// CHECK-INSTRPROF: Running pass: InstrProfilingLoweringPass on
77
//
88
// Ensure Pass PGOInstrumentationGenPass is not invoked.
99
// RUN: %clang_cc1 -O2 -fprofile-instrument=clang %s -fdebug-pass-manager -emit-llvm -o - 2>&1 | FileCheck %s -check-prefix=CHECK-PGOGENPASS-INVOKED-INSTR-GEN-CLANG
1010
// CHECK-PGOGENPASS-INVOKED-INSTR-GEN-CLANG-NOT: Running pass: PGOInstrumentationGen on
1111

1212
// RUN: %clang_cc1 -O2 -fprofile-instrument=clang %s -fdebug-pass-manager -emit-llvm -o - 2>&1 | FileCheck %s --check-prefix=CHECK-CLANG-INSTRPROF
1313
// RUN: %clang_cc1 -O0 -fprofile-instrument=clang %s -fdebug-pass-manager -emit-llvm -o - 2>&1 | FileCheck %s --check-prefix=CHECK-CLANG-INSTRPROF
14-
// CHECK-CLANG-INSTRPROF: Running pass: InstrProfiling on
14+
// CHECK-CLANG-INSTRPROF: Running pass: InstrProfilingLoweringPass on
1515

1616
// Ensure Pass PGOInstrumentationUsePass is invoked.
1717
// RUN: llvm-profdata merge -o %t.profdata %S/Inputs/pgotestir.profraw

llvm/include/llvm/Transforms/Instrumentation.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ Comdat *getOrCreateFunctionComdat(Function &F, Triple &T);
5252
// Place global in a large section for x86-64 ELF binaries to mitigate
5353
// relocation overflow pressure. This can be be used for metadata globals that
5454
// aren't directly accessed by code, which has no performance impact.
55-
void setGlobalVariableLargeSection(Triple &TargetTriple, GlobalVariable &GV);
55+
void setGlobalVariableLargeSection(const Triple &TargetTriple,
56+
GlobalVariable &GV);
5657

5758
// Insert GCOV profiling instrumentation
5859
struct GCOVOptions {

llvm/include/llvm/Transforms/Instrumentation/InstrProfiling.h

+29-17
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,46 @@ using LoadStorePair = std::pair<Instruction *, Instruction *>;
3131
/// Instrumentation based profiling lowering pass. This pass lowers
3232
/// the profile instrumented code generated by FE or the IR based
3333
/// instrumentation pass.
34-
class InstrProfiling : public PassInfoMixin<InstrProfiling> {
34+
class InstrProfilingLoweringPass
35+
: public PassInfoMixin<InstrProfilingLoweringPass> {
36+
const InstrProfOptions Options;
37+
// Is this lowering for the context-sensitive instrumentation.
38+
const bool IsCS = false;
39+
3540
public:
36-
InstrProfiling() : IsCS(false) {}
37-
InstrProfiling(const InstrProfOptions &Options, bool IsCS = false)
41+
InstrProfilingLoweringPass() = default;
42+
InstrProfilingLoweringPass(const InstrProfOptions &Options, bool IsCS = false)
3843
: Options(Options), IsCS(IsCS) {}
3944

4045
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
41-
bool run(Module &M,
42-
std::function<const TargetLibraryInfo &(Function &F)> GetTLI);
46+
};
47+
48+
class InstrProfiling final {
49+
public:
50+
InstrProfiling(Module &M, const InstrProfOptions &Options,
51+
std::function<const TargetLibraryInfo &(Function &F)> GetTLI,
52+
bool IsCS)
53+
: M(M), Options(Options), TT(Triple(M.getTargetTriple())), IsCS(IsCS),
54+
GetTLI(GetTLI) {}
55+
56+
bool lower();
4357

4458
private:
45-
InstrProfOptions Options;
46-
Module *M;
47-
Triple TT;
59+
Module &M;
60+
const InstrProfOptions Options;
61+
const Triple TT;
62+
// Is this lowering for the context-sensitive instrumentation.
63+
const bool IsCS;
64+
4865
std::function<const TargetLibraryInfo &(Function &F)> GetTLI;
4966
struct PerFunctionProfileData {
50-
uint32_t NumValueSites[IPVK_Last + 1];
67+
uint32_t NumValueSites[IPVK_Last + 1] = {};
5168
GlobalVariable *RegionCounters = nullptr;
5269
GlobalVariable *DataVar = nullptr;
5370
GlobalVariable *RegionBitmaps = nullptr;
5471
uint32_t NumBitmapBytes = 0;
5572

56-
PerFunctionProfileData() {
57-
memset(NumValueSites, 0, sizeof(uint32_t) * (IPVK_Last + 1));
58-
}
73+
PerFunctionProfileData() = default;
5974
};
6075
DenseMap<GlobalVariable *, PerFunctionProfileData> ProfileDataMap;
6176
/// If runtime relocation is enabled, this maps functions to the load
@@ -64,11 +79,8 @@ class InstrProfiling : public PassInfoMixin<InstrProfiling> {
6479
std::vector<GlobalValue *> CompilerUsedVars;
6580
std::vector<GlobalValue *> UsedVars;
6681
std::vector<GlobalVariable *> ReferencedNames;
67-
GlobalVariable *NamesVar;
68-
size_t NamesSize;
69-
70-
// Is this lowering for the context-sensitive instrumentation.
71-
bool IsCS;
82+
GlobalVariable *NamesVar = nullptr;
83+
size_t NamesSize = 0;
7284

7385
// vector of counter load/store pairs to be register promoted.
7486
std::vector<LoadStorePair> PromotionCandidates;

llvm/lib/Passes/PassBuilderPipelines.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM,
810810
Options.DoCounterPromotion = true;
811811
Options.UseBFIInPromotion = IsCS;
812812
Options.Atomic = AtomicCounterUpdate;
813-
MPM.addPass(InstrProfiling(Options, IsCS));
813+
MPM.addPass(InstrProfilingLoweringPass(Options, IsCS));
814814
}
815815

816816
void PassBuilder::addPGOInstrPassesForO0(
@@ -837,7 +837,7 @@ void PassBuilder::addPGOInstrPassesForO0(
837837
Options.DoCounterPromotion = false;
838838
Options.UseBFIInPromotion = IsCS;
839839
Options.Atomic = AtomicCounterUpdate;
840-
MPM.addPass(InstrProfiling(Options, IsCS));
840+
MPM.addPass(InstrProfilingLoweringPass(Options, IsCS));
841841
}
842842

843843
static InlineParams getInlineParamsFromOptLevel(OptimizationLevel Level) {

llvm/lib/Passes/PassRegistry.def

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ MODULE_PASS("inliner-wrapper-no-mandatory-first",
7777
ModuleInlinerWrapperPass(getInlineParams(), false))
7878
MODULE_PASS("insert-gcov-profiling", GCOVProfilerPass())
7979
MODULE_PASS("instrorderfile", InstrOrderFilePass())
80-
MODULE_PASS("instrprof", InstrProfiling())
80+
MODULE_PASS("instrprof", InstrProfilingLoweringPass())
8181
MODULE_PASS("internalize", InternalizePass())
8282
MODULE_PASS("invalidate<all>", InvalidateAllAnalysesPass())
8383
MODULE_PASS("iroutliner", IROutlinerPass())

0 commit comments

Comments
 (0)