Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NFC][InstrProf] Refactor InstrProfiling lowering pass #74970

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/BackendUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
getInstrProfOptions(CodeGenOpts, LangOpts))
PB.registerPipelineStartEPCallback(
[Options](ModulePassManager &MPM, OptimizationLevel Level) {
MPM.addPass(InstrProfiling(*Options, false));
MPM.addPass(InstrProfilingLoweringPass(*Options, false));
});

// TODO: Consider passing the MemoryProfileOutput to the pass builder via
Expand Down
4 changes: 2 additions & 2 deletions clang/test/CodeGen/pgo-instrumentation.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
// Ensure Pass PGOInstrumentationGenPass is invoked.
// 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
// CHECK-PGOGENPASS-INVOKED-INSTR-GEN: Running pass: PGOInstrumentationGen on
// CHECK-INSTRPROF: Running pass: InstrProfiling on
// CHECK-INSTRPROF: Running pass: InstrProfilingLoweringPass on
//
// Ensure Pass PGOInstrumentationGenPass is not invoked.
// 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
// CHECK-PGOGENPASS-INVOKED-INSTR-GEN-CLANG-NOT: Running pass: PGOInstrumentationGen on

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

// Ensure Pass PGOInstrumentationUsePass is invoked.
// RUN: llvm-profdata merge -o %t.profdata %S/Inputs/pgotestir.profraw
Expand Down
3 changes: 2 additions & 1 deletion llvm/include/llvm/Transforms/Instrumentation.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ Comdat *getOrCreateFunctionComdat(Function &F, Triple &T);
// Place global in a large section for x86-64 ELF binaries to mitigate
// relocation overflow pressure. This can be be used for metadata globals that
// aren't directly accessed by code, which has no performance impact.
void setGlobalVariableLargeSection(Triple &TargetTriple, GlobalVariable &GV);
void setGlobalVariableLargeSection(const Triple &TargetTriple,
GlobalVariable &GV);

// Insert GCOV profiling instrumentation
struct GCOVOptions {
Expand Down
46 changes: 29 additions & 17 deletions llvm/include/llvm/Transforms/Instrumentation/InstrProfiling.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,46 @@ using LoadStorePair = std::pair<Instruction *, Instruction *>;
/// Instrumentation based profiling lowering pass. This pass lowers
/// the profile instrumented code generated by FE or the IR based
/// instrumentation pass.
class InstrProfiling : public PassInfoMixin<InstrProfiling> {
class InstrProfilingLoweringPass
: public PassInfoMixin<InstrProfilingLoweringPass> {
const InstrProfOptions Options;
// Is this lowering for the context-sensitive instrumentation.
const bool IsCS = false;

public:
InstrProfiling() : IsCS(false) {}
InstrProfiling(const InstrProfOptions &Options, bool IsCS = false)
InstrProfilingLoweringPass() = default;
InstrProfilingLoweringPass(const InstrProfOptions &Options, bool IsCS = false)
: Options(Options), IsCS(IsCS) {}

PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
bool run(Module &M,
std::function<const TargetLibraryInfo &(Function &F)> GetTLI);
};

class InstrProfiling final {
mtrofin marked this conversation as resolved.
Show resolved Hide resolved
public:
InstrProfiling(Module &M, const InstrProfOptions &Options,
std::function<const TargetLibraryInfo &(Function &F)> GetTLI,
bool IsCS)
: M(M), Options(Options), TT(Triple(M.getTargetTriple())), IsCS(IsCS),
GetTLI(GetTLI) {}

bool lower();

private:
InstrProfOptions Options;
Module *M;
Triple TT;
Module &M;
const InstrProfOptions Options;
const Triple TT;
// Is this lowering for the context-sensitive instrumentation.
const bool IsCS;

std::function<const TargetLibraryInfo &(Function &F)> GetTLI;
struct PerFunctionProfileData {
uint32_t NumValueSites[IPVK_Last + 1];
uint32_t NumValueSites[IPVK_Last + 1] = {};
GlobalVariable *RegionCounters = nullptr;
GlobalVariable *DataVar = nullptr;
GlobalVariable *RegionBitmaps = nullptr;
uint32_t NumBitmapBytes = 0;

PerFunctionProfileData() {
memset(NumValueSites, 0, sizeof(uint32_t) * (IPVK_Last + 1));
}
PerFunctionProfileData() = default;
};
DenseMap<GlobalVariable *, PerFunctionProfileData> ProfileDataMap;
/// If runtime relocation is enabled, this maps functions to the load
Expand All @@ -64,11 +79,8 @@ class InstrProfiling : public PassInfoMixin<InstrProfiling> {
std::vector<GlobalValue *> CompilerUsedVars;
std::vector<GlobalValue *> UsedVars;
std::vector<GlobalVariable *> ReferencedNames;
GlobalVariable *NamesVar;
size_t NamesSize;

// Is this lowering for the context-sensitive instrumentation.
bool IsCS;
GlobalVariable *NamesVar = nullptr;
size_t NamesSize = 0;

// vector of counter load/store pairs to be register promoted.
std::vector<LoadStorePair> PromotionCandidates;
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Passes/PassBuilderPipelines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM,
Options.DoCounterPromotion = true;
Options.UseBFIInPromotion = IsCS;
Options.Atomic = AtomicCounterUpdate;
MPM.addPass(InstrProfiling(Options, IsCS));
MPM.addPass(InstrProfilingLoweringPass(Options, IsCS));
}

void PassBuilder::addPGOInstrPassesForO0(
Expand All @@ -837,7 +837,7 @@ void PassBuilder::addPGOInstrPassesForO0(
Options.DoCounterPromotion = false;
Options.UseBFIInPromotion = IsCS;
Options.Atomic = AtomicCounterUpdate;
MPM.addPass(InstrProfiling(Options, IsCS));
MPM.addPass(InstrProfilingLoweringPass(Options, IsCS));
}

static InlineParams getInlineParamsFromOptLevel(OptimizationLevel Level) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Passes/PassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ MODULE_PASS("inliner-wrapper-no-mandatory-first",
ModuleInlinerWrapperPass(getInlineParams(), false))
MODULE_PASS("insert-gcov-profiling", GCOVProfilerPass())
MODULE_PASS("instrorderfile", InstrOrderFilePass())
MODULE_PASS("instrprof", InstrProfiling())
MODULE_PASS("instrprof", InstrProfilingLoweringPass())
MODULE_PASS("internalize", InternalizePass())
MODULE_PASS("invalidate<all>", InvalidateAllAnalysesPass())
MODULE_PASS("iroutliner", IROutlinerPass())
Expand Down
Loading