diff options
author | Nikita Popov <npopov@redhat.com> | 2024-06-24 15:00:11 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-24 15:00:11 +0200 |
commit | 5cd0ba30f53d11835dbfd05ad4071d397387fb04 (patch) | |
tree | f82e4b26861f63fed771a50c5075c75a7e1cc4ad | |
parent | 0cf1e662b13fb20d3897c5b9055c60b0de50beca (diff) | |
download | llvm-5cd0ba30f53d11835dbfd05ad4071d397387fb04.zip llvm-5cd0ba30f53d11835dbfd05ad4071d397387fb04.tar.gz llvm-5cd0ba30f53d11835dbfd05ad4071d397387fb04.tar.bz2 |
Reapply [IR] Lazily initialize the class to pass name mapping (NFC) (#96321) (#96462)
On MSVC the `this` uses inside `decltype` require a lambda capture. On
clang they result in an unused capture warning instead. Add the capture
and suppress the warning with `(void)this`.
-----
Initializing this map is somewhat expensive (especially for O0), so we
currently only do it if certain flags are used. I would like to make use
of it for crash dumps (#96078), where we don't know in advance whether
it will be needed or not.
This patch changes the initialization to a lazy approach, where a
callback is registered that does the actual initialization. The
callbacks will be run the first time the pass name is requested.
This way there is no compile-time impact if the mapping is not used.
19 files changed, 40 insertions, 48 deletions
diff --git a/llvm/include/llvm/IR/PassInstrumentation.h b/llvm/include/llvm/IR/PassInstrumentation.h index ff5e544..f2eb8a9 100644 --- a/llvm/include/llvm/IR/PassInstrumentation.h +++ b/llvm/include/llvm/IR/PassInstrumentation.h @@ -149,6 +149,11 @@ public: AnalysesClearedCallbacks.emplace_back(std::move(C)); } + template <typename CallableT> + void registerClassToPassNameCallback(CallableT C) { + ClassToPassNameCallbacks.emplace_back(std::move(C)); + } + /// Add a class name to pass name mapping for use by pass instrumentation. void addClassToPassName(StringRef ClassName, StringRef PassName); /// Get the pass name for a given pass class name. @@ -185,6 +190,7 @@ private: SmallVector<llvm::unique_function<AnalysesClearedFunc>, 4> AnalysesClearedCallbacks; + SmallVector<llvm::unique_function<void ()>, 4> ClassToPassNameCallbacks; DenseMap<StringRef, std::string> ClassToPassName; }; diff --git a/llvm/include/llvm/Passes/TargetPassRegistry.inc b/llvm/include/llvm/Passes/TargetPassRegistry.inc index b618331..521913c 100644 --- a/llvm/include/llvm/Passes/TargetPassRegistry.inc +++ b/llvm/include/llvm/Passes/TargetPassRegistry.inc @@ -21,9 +21,12 @@ #error "must provide <Target>PassRegistry.def" #endif -if (PopulateClassToPassNames) { - auto *PIC = PB.getPassInstrumentationCallbacks(); - +auto *PIC = PB.getPassInstrumentationCallbacks(); +if (PIC) { + PIC->registerClassToPassNameCallback([this, PIC]() { + // MSVC requires this to be captured if it's used inside decltype. + // Other compilers consider it an unused lambda capture. + (void)this; #define ADD_CLASS_PASS_TO_PASS_NAME(NAME, CREATE_PASS) \ PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); #define ADD_CLASS_PASS_TO_PASS_NAME_WITH_PARAMS(NAME, CLASS) \ @@ -69,6 +72,7 @@ if (PopulateClassToPassNames) { #undef MACHINE_FUNCTION_PASS_WITH_PARAMS #undef ADD_CLASS_PASS_TO_PASS_NAME #undef ADD_CLASS_PASS_TO_PASS_NAME_WITH_PARAMS + }); } #define ADD_PASS(NAME, CREATE_PASS) \ diff --git a/llvm/include/llvm/Target/TargetMachine.h b/llvm/include/llvm/Target/TargetMachine.h index f09c3b2..b8e56c7 100644 --- a/llvm/include/llvm/Target/TargetMachine.h +++ b/llvm/include/llvm/Target/TargetMachine.h @@ -369,8 +369,7 @@ public: /// Allow the target to modify the pass pipeline. // TODO: Populate all pass names by using <Target>PassRegistry.def. - virtual void registerPassBuilderCallbacks(PassBuilder &, - bool PopulateClassToPassNames) {} + virtual void registerPassBuilderCallbacks(PassBuilder &) {} /// Allow the target to register alias analyses with the AAManager for use /// with the new pass manager. Only affects the "default" AAManager. diff --git a/llvm/lib/IR/PassInstrumentation.cpp b/llvm/lib/IR/PassInstrumentation.cpp index 86c82a1..0c4e769 100644 --- a/llvm/lib/IR/PassInstrumentation.cpp +++ b/llvm/lib/IR/PassInstrumentation.cpp @@ -24,6 +24,11 @@ void PassInstrumentationCallbacks::addClassToPassName(StringRef ClassName, StringRef PassInstrumentationCallbacks::getPassNameForClassName(StringRef ClassName) { + if (!ClassToPassNameCallbacks.empty()) { + for (auto &Fn : ClassToPassNameCallbacks) + Fn(); + ClassToPassNameCallbacks.clear(); + } return ClassToPassName[ClassName]; } diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp index f819345..c19d5a7 100644 --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/llvm/lib/Passes/PassBuilder.cpp @@ -325,18 +325,6 @@ AnalysisKey NoOpLoopAnalysis::Key; namespace { -/// Whether or not we should populate a PassInstrumentationCallbacks's class to -/// pass name map. -/// -/// This is for optimization purposes so we don't populate it if we never use -/// it. This should be updated if new pass instrumentation wants to use the map. -/// We currently only use this for --print-before/after. -bool shouldPopulateClassToPassNames() { - return PrintPipelinePasses || !printBeforePasses().empty() || - !printAfterPasses().empty() || !isFilterPassesEmpty() || - TargetPassConfig::hasLimitedCodeGenPipeline(); -} - // A pass for testing -print-on-crash. // DO NOT USE THIS EXCEPT FOR TESTING! class TriggerCrashPass : public PassInfoMixin<TriggerCrashPass> { @@ -416,10 +404,13 @@ PassBuilder::PassBuilder(TargetMachine *TM, PipelineTuningOptions PTO, std::optional<PGOOptions> PGOOpt, PassInstrumentationCallbacks *PIC) : TM(TM), PTO(PTO), PGOOpt(PGOOpt), PIC(PIC) { - bool ShouldPopulateClassToPassNames = PIC && shouldPopulateClassToPassNames(); if (TM) - TM->registerPassBuilderCallbacks(*this, ShouldPopulateClassToPassNames); - if (ShouldPopulateClassToPassNames) { + TM->registerPassBuilderCallbacks(*this); + if (PIC) { + PIC->registerClassToPassNameCallback([this, PIC]() { + // MSVC requires this to be captured if it's used inside decltype. + // Other compilers consider it an unused lambda capture. + (void)this; #define MODULE_PASS(NAME, CREATE_PASS) \ PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); #define MODULE_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \ @@ -453,6 +444,7 @@ PassBuilder::PassBuilder(TargetMachine *TM, PipelineTuningOptions PTO, #define MACHINE_FUNCTION_PASS(NAME, CREATE_PASS) \ PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); #include "llvm/Passes/MachinePassRegistry.def" + }); } } diff --git a/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp b/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp index 8c924e7..37ce07d 100644 --- a/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp +++ b/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp @@ -554,8 +554,7 @@ public: } // end anonymous namespace -void AArch64TargetMachine::registerPassBuilderCallbacks( - PassBuilder &PB, bool PopulateClassToPassNames) { +void AArch64TargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) { PB.registerLateLoopOptimizationsEPCallback( [=](LoopPassManager &LPM, OptimizationLevel Level) { diff --git a/llvm/lib/Target/AArch64/AArch64TargetMachine.h b/llvm/lib/Target/AArch64/AArch64TargetMachine.h index e396d92..1a470ca 100644 --- a/llvm/lib/Target/AArch64/AArch64TargetMachine.h +++ b/llvm/lib/Target/AArch64/AArch64TargetMachine.h @@ -43,8 +43,7 @@ public: // Pass Pipeline Configuration TargetPassConfig *createPassConfig(PassManagerBase &PM) override; - void registerPassBuilderCallbacks(PassBuilder &PB, - bool PopulateClassToPassNames) override; + void registerPassBuilderCallbacks(PassBuilder &PB) override; TargetTransformInfo getTargetTransformInfo(const Function &F) const override; diff --git a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp index 3e21d8e..9162e11 100644 --- a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp +++ b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp @@ -658,8 +658,7 @@ Error AMDGPUTargetMachine::buildCodeGenPipeline( return CGPB.buildPipeline(MPM, Out, DwoOut, FileType); } -void AMDGPUTargetMachine::registerPassBuilderCallbacks( - PassBuilder &PB, bool PopulateClassToPassNames) { +void AMDGPUTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) { #define GET_PASS_REGISTRY "AMDGPUPassRegistry.def" #include "llvm/Passes/TargetPassRegistry.inc" diff --git a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.h b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.h index 98b0bc0..0f74fbc 100644 --- a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.h +++ b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.h @@ -58,8 +58,7 @@ public: const CGPassBuilderOption &Opts, PassInstrumentationCallbacks *PIC) override; - void registerPassBuilderCallbacks(PassBuilder &PB, - bool PopulateClassToPassNames) override; + void registerPassBuilderCallbacks(PassBuilder &PB) override; void registerDefaultAliasAnalyses(AAManager &) override; /// Get the integer value of a null pointer in the given address space. diff --git a/llvm/lib/Target/BPF/BPFTargetMachine.cpp b/llvm/lib/Target/BPF/BPFTargetMachine.cpp index 7b73c9f..7d91fa8 100644 --- a/llvm/lib/Target/BPF/BPFTargetMachine.cpp +++ b/llvm/lib/Target/BPF/BPFTargetMachine.cpp @@ -113,8 +113,7 @@ static Expected<bool> parseBPFPreserveStaticOffsetOptions(StringRef Params) { "BPFPreserveStaticOffsetPass"); } -void BPFTargetMachine::registerPassBuilderCallbacks( - PassBuilder &PB, bool PopulateClassToPassNames) { +void BPFTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) { #define GET_PASS_REGISTRY "BPFPassRegistry.def" #include "llvm/Passes/TargetPassRegistry.inc" diff --git a/llvm/lib/Target/BPF/BPFTargetMachine.h b/llvm/lib/Target/BPF/BPFTargetMachine.h index 0a28394..4e6adc7 100644 --- a/llvm/lib/Target/BPF/BPFTargetMachine.h +++ b/llvm/lib/Target/BPF/BPFTargetMachine.h @@ -42,8 +42,7 @@ public: return TLOF.get(); } - void registerPassBuilderCallbacks(PassBuilder &PB, - bool PopulateClassToPassNames) override; + void registerPassBuilderCallbacks(PassBuilder &PB) override; }; } diff --git a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp index c853393..e6dbb25 100644 --- a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp +++ b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp @@ -102,8 +102,7 @@ DirectXTargetMachine::DirectXTargetMachine(const Target &T, const Triple &TT, DirectXTargetMachine::~DirectXTargetMachine() {} -void DirectXTargetMachine::registerPassBuilderCallbacks( - PassBuilder &PB, bool PopulateClassToPassNames) { +void DirectXTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) { #define GET_PASS_REGISTRY "DirectXPassRegistry.def" #include "llvm/Passes/TargetPassRegistry.inc" } diff --git a/llvm/lib/Target/DirectX/DirectXTargetMachine.h b/llvm/lib/Target/DirectX/DirectXTargetMachine.h index 428beaf..d04c375 100644 --- a/llvm/lib/Target/DirectX/DirectXTargetMachine.h +++ b/llvm/lib/Target/DirectX/DirectXTargetMachine.h @@ -47,8 +47,7 @@ public: } TargetTransformInfo getTargetTransformInfo(const Function &F) const override; - void registerPassBuilderCallbacks(PassBuilder &PB, - bool PopulateClassToPassNames) override; + void registerPassBuilderCallbacks(PassBuilder &PB) override; }; } // namespace llvm diff --git a/llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp b/llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp index e488650..b362285 100644 --- a/llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp +++ b/llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp @@ -312,8 +312,7 @@ HexagonTargetMachine::getSubtargetImpl(const Function &F) const { return I.get(); } -void HexagonTargetMachine::registerPassBuilderCallbacks( - PassBuilder &PB, bool PopulateClassToPassNames) { +void HexagonTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) { #define GET_PASS_REGISTRY "HexagonPassRegistry.def" #include "llvm/Passes/TargetPassRegistry.inc" diff --git a/llvm/lib/Target/Hexagon/HexagonTargetMachine.h b/llvm/lib/Target/Hexagon/HexagonTargetMachine.h index 34ff45b..6e9a78b 100644 --- a/llvm/lib/Target/Hexagon/HexagonTargetMachine.h +++ b/llvm/lib/Target/Hexagon/HexagonTargetMachine.h @@ -35,8 +35,7 @@ public: ~HexagonTargetMachine() override; const HexagonSubtarget *getSubtargetImpl(const Function &F) const override; - void registerPassBuilderCallbacks(PassBuilder &PB, - bool PopulateClassToPassNames) override; + void registerPassBuilderCallbacks(PassBuilder &PB) override; TargetPassConfig *createPassConfig(PassManagerBase &PM) override; TargetTransformInfo getTargetTransformInfo(const Function &F) const override; diff --git a/llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp b/llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp index b60a1d7..152f200 100644 --- a/llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp +++ b/llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp @@ -224,8 +224,7 @@ void NVPTXTargetMachine::registerDefaultAliasAnalyses(AAManager &AAM) { AAM.registerFunctionAnalysis<NVPTXAA>(); } -void NVPTXTargetMachine::registerPassBuilderCallbacks( - PassBuilder &PB, bool PopulateClassToPassNames) { +void NVPTXTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) { #define GET_PASS_REGISTRY "NVPTXPassRegistry.def" #include "llvm/Passes/TargetPassRegistry.inc" diff --git a/llvm/lib/Target/NVPTX/NVPTXTargetMachine.h b/llvm/lib/Target/NVPTX/NVPTXTargetMachine.h index 870ea20..2b88da6 100644 --- a/llvm/lib/Target/NVPTX/NVPTXTargetMachine.h +++ b/llvm/lib/Target/NVPTX/NVPTXTargetMachine.h @@ -66,8 +66,7 @@ public: void registerDefaultAliasAnalyses(AAManager &AAM) override; - void registerPassBuilderCallbacks(PassBuilder &PB, - bool PopulateClassToPassNames) override; + void registerPassBuilderCallbacks(PassBuilder &PB) override; TargetTransformInfo getTargetTransformInfo(const Function &F) const override; diff --git a/llvm/lib/Target/X86/X86CodeGenPassBuilder.cpp b/llvm/lib/Target/X86/X86CodeGenPassBuilder.cpp index 9819bfd..d979517 100644 --- a/llvm/lib/Target/X86/X86CodeGenPassBuilder.cpp +++ b/llvm/lib/Target/X86/X86CodeGenPassBuilder.cpp @@ -50,8 +50,7 @@ Error X86CodeGenPassBuilder::addInstSelector(AddMachinePass &addPass) const { } // namespace -void X86TargetMachine::registerPassBuilderCallbacks( - PassBuilder &PB, bool PopulateClassToPassNames) { +void X86TargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) { #define GET_PASS_REGISTRY "X86PassRegistry.def" #include "llvm/Passes/TargetPassRegistry.inc" } diff --git a/llvm/lib/Target/X86/X86TargetMachine.h b/llvm/lib/Target/X86/X86TargetMachine.h index 916445c..ec4a93e9 100644 --- a/llvm/lib/Target/X86/X86TargetMachine.h +++ b/llvm/lib/Target/X86/X86TargetMachine.h @@ -66,8 +66,7 @@ public: SMDiagnostic &Error, SMRange &SourceRange) const override; - void registerPassBuilderCallbacks(PassBuilder &PB, - bool PopulateClassToPassNames) override; + void registerPassBuilderCallbacks(PassBuilder &PB) override; Error buildCodeGenPipeline(ModulePassManager &, raw_pwrite_stream &, raw_pwrite_stream *, CodeGenFileType, |