Skip to content

[PassInstrumentation] Pass Any as const &. (NFC) - #215120

Open
fhahn wants to merge 1 commit into
llvm:mainfrom
fhahn:pm-pass-any-as-const
Open

[PassInstrumentation] Pass Any as const &. (NFC)#215120
fhahn wants to merge 1 commit into
llvm:mainfrom
fhahn:pm-pass-any-as-const

Conversation

@fhahn

@fhahn fhahn commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

llvm::Any has a heap allocation on every copy and pass instrumentations take the IR unit to instrument as llvm::Any. In the default pipeline without any additional options, OptNoneInstrumentation is registered and runs on each executed pass.

Update to take llvm::Any as const &, to avoid unnecessary heap allocations and improves compile-time:

  • stage1-O3: -0.25%
  • stage1-ReleaseThinLTO: -0.26%
  • stage1-ReleaseLTO-g: -0.20%
  • stage1-aarch64-O3: -0.22%
  • stage2-O3: -0.26%
  • clang: -0.46%

https://llvm-compile-time-tracker.com/compare.php?from=5371bcaa368e59a99a03f6a726949ee52e07fb65&to=a4544f9d91d9f806791f2ba74cd93b0685fc3f75&stat=instructions:u

I also have a follow-up that makes llvm:Any store pointer values inline, which gives some more gains, but it would require changing the layout of llvm::Any.

llvm::Any has a heap allocation on every copy and pass instrumentations
take the IR unit to instrument as llvm::Any. In the default pipeline
without any additional options, OptNoneInstrumentation is registered and
runs on each executed pass.

Update to take llvm::Any as const &, to avoid unnecessary copies. This
improves compile-time:

* stage1-O3: -0.25%
* stage1-ReleaseThinLTO: -0.26%
* stage1-ReleaseLTO-g: -0.20%
* stage1-aarch64-O3: -0.22%
* stage2-O3: -0.26%
* clang: -0.46%

https://llvm-compile-time-tracker.com/compare.php?from=5371bcaa368e59a99a03f6a726949ee52e07fb65&to=a4544f9d91d9f806791f2ba74cd93b0685fc3f75&stat=instructions:u

I also have a follow-up that makes llvm:Any store pointer values inline,
which gives some more gains, but it would require changing the layout of
llvm::Any.
@llvmorg-github-actions

llvmorg-github-actions Bot commented Aug 9, 2026

Copy link
Copy Markdown

@llvm/pr-subscribers-pgo
@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-llvm-ir

Author: Florian Hahn (fhahn)

Changes

llvm::Any has a heap allocation on every copy and pass instrumentations take the IR unit to instrument as llvm::Any. In the default pipeline without any additional options, OptNoneInstrumentation is registered and runs on each executed pass.

Update to take llvm::Any as const &, to avoid unnecessary heap allocations and improves compile-time:

  • stage1-O3: -0.25%
  • stage1-ReleaseThinLTO: -0.26%
  • stage1-ReleaseLTO-g: -0.20%
  • stage1-aarch64-O3: -0.22%
  • stage2-O3: -0.26%
  • clang: -0.46%

https://llvm-compile-time-tracker.com/compare.php?from=5371bcaa368e59a99a03f6a726949ee52e07fb65&to=a4544f9d91d9f806791f2ba74cd93b0685fc3f75&stat=instructions:u

I also have a follow-up that makes llvm:Any store pointer values inline, which gives some more gains, but it would require changing the layout of llvm::Any.


Patch is 43.73 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/215120.diff

13 Files Affected:

  • (modified) llvm/include/llvm/IR/DroppedVariableStatsIR.h (+3-3)
  • (modified) llvm/include/llvm/IR/PassInstrumentation.h (+7-7)
  • (modified) llvm/include/llvm/Passes/StandardInstrumentations.h (+23-22)
  • (modified) llvm/include/llvm/Transforms/IPO/SampleProfileProbe.h (+1-1)
  • (modified) llvm/lib/CodeGen/TargetPassConfig.cpp (+1-1)
  • (modified) llvm/lib/IR/DroppedVariableStatsIR.cpp (+6-6)
  • (modified) llvm/lib/IR/PassTimingInfo.cpp (+4-4)
  • (modified) llvm/lib/Passes/StandardInstrumentations.cpp (+84-75)
  • (modified) llvm/lib/Transforms/IPO/SampleProfileProbe.cpp (+7-6)
  • (modified) llvm/lib/Transforms/Scalar/LoopPassManager.cpp (+3-2)
  • (modified) llvm/lib/Transforms/Utils/Debugify.cpp (+9-6)
  • (modified) llvm/tools/llvm-reduce/deltas/RunIRPasses.cpp (+1-1)
  • (modified) llvm/unittests/IR/PassBuilderCallbacksTest.cpp (+19-15)
diff --git a/llvm/include/llvm/IR/DroppedVariableStatsIR.h b/llvm/include/llvm/IR/DroppedVariableStatsIR.h
index 9fc231999eb4f..8c1a3e87b2606 100644
--- a/llvm/include/llvm/IR/DroppedVariableStatsIR.h
+++ b/llvm/include/llvm/IR/DroppedVariableStatsIR.h
@@ -34,9 +34,9 @@ class LLVM_ABI DroppedVariableStatsIR : public DroppedVariableStats {
   DroppedVariableStatsIR(bool DroppedVarStatsEnabled)
       : llvm::DroppedVariableStats(DroppedVarStatsEnabled) {}
 
-  void runBeforePass(StringRef P, Any IR);
+  void runBeforePass(StringRef P, const Any &IR);
 
-  void runAfterPass(StringRef P, Any IR);
+  void runAfterPass(StringRef P, const Any &IR);
 
   void registerCallbacks(PassInstrumentationCallbacks &PIC);
 
@@ -81,7 +81,7 @@ class LLVM_ABI DroppedVariableStatsIR : public DroppedVariableStats {
       DenseMap<StringRef, DenseMap<VarID, DILocation *>> &InlinedAtsMap,
       StringRef FuncName, bool Before) override;
 
-  template <typename IRUnitT> static const IRUnitT *unwrapIR(Any IR);
+  template <typename IRUnitT> static const IRUnitT *unwrapIR(const Any &IR);
 };
 
 } // namespace llvm
diff --git a/llvm/include/llvm/IR/PassInstrumentation.h b/llvm/include/llvm/IR/PassInstrumentation.h
index a7ecd3aa5e2a4..52eba45542390 100644
--- a/llvm/include/llvm/IR/PassInstrumentation.h
+++ b/llvm/include/llvm/IR/PassInstrumentation.h
@@ -83,14 +83,14 @@ class PassInstrumentationCallbacks {
   // already invalidated IRUnit is unsafe. There are ways to handle invalidated
   // IRUnits in a safe way, and we might pursue that as soon as there is a
   // useful instrumentation that needs it.
-  using BeforePassFunc = bool(StringRef, Any);
-  using BeforeSkippedPassFunc = void(StringRef, Any);
-  using BeforeNonSkippedPassFunc = void(StringRef, Any);
-  using AfterPassFunc = void(StringRef, Any, const PreservedAnalyses &);
+  using BeforePassFunc = bool(StringRef, const Any &);
+  using BeforeSkippedPassFunc = void(StringRef, const Any &);
+  using BeforeNonSkippedPassFunc = void(StringRef, const Any &);
+  using AfterPassFunc = void(StringRef, const Any &, const PreservedAnalyses &);
   using AfterPassInvalidatedFunc = void(StringRef, const PreservedAnalyses &);
-  using BeforeAnalysisFunc = void(StringRef, Any);
-  using AfterAnalysisFunc = void(StringRef, Any);
-  using AnalysisInvalidatedFunc = void(StringRef, Any);
+  using BeforeAnalysisFunc = void(StringRef, const Any &);
+  using AfterAnalysisFunc = void(StringRef, const Any &);
+  using AnalysisInvalidatedFunc = void(StringRef, const Any &);
   using AnalysesClearedFunc = void(StringRef);
 
 public:
diff --git a/llvm/include/llvm/Passes/StandardInstrumentations.h b/llvm/include/llvm/Passes/StandardInstrumentations.h
index 4ee5ab2554868..513b71d93f0ce 100644
--- a/llvm/include/llvm/Passes/StandardInstrumentations.h
+++ b/llvm/include/llvm/Passes/StandardInstrumentations.h
@@ -66,8 +66,8 @@ class PrintIRInstrumentation {
           IRName{IRName}, PassID(PassID) {}
   };
 
-  void printBeforePass(StringRef PassID, Any IR);
-  void printAfterPass(StringRef PassID, Any IR);
+  void printBeforePass(StringRef PassID, const Any &IR);
+  void printAfterPass(StringRef PassID, const Any &IR);
   void printAfterPassInvalidated(StringRef PassID);
 
   bool shouldPrintBeforePass(StringRef PassID);
@@ -78,7 +78,8 @@ class PrintIRInstrumentation {
   bool shouldPrintBeforeSomePassNumber();
   bool shouldPrintAfterSomePassNumber();
 
-  void pushPassRunDescriptor(StringRef PassID, Any IR, unsigned PassNumber);
+  void pushPassRunDescriptor(StringRef PassID, const Any &IR,
+                             unsigned PassNumber);
   PassRunDescriptor popPassRunDescriptor(StringRef PassID);
 
   enum class IRDumpFileSuffixType {
@@ -109,7 +110,7 @@ class OptNoneInstrumentation {
 
 private:
   bool DebugLogging;
-  bool shouldRun(StringRef PassID, Any IR);
+  bool shouldRun(StringRef PassID, const Any &IR);
 };
 
 class OptPassGateInstrumentation {
@@ -117,7 +118,7 @@ class OptPassGateInstrumentation {
   bool HasWrittenIR = false;
 public:
   OptPassGateInstrumentation(LLVMContext &Context) : Context(Context) {}
-  LLVM_ABI bool shouldRun(StringRef PassName, Any IR);
+  LLVM_ABI bool shouldRun(StringRef PassName, const Any &IR);
   LLVM_ABI void registerCallbacks(PassInstrumentationCallbacks &PIC);
 };
 
@@ -218,9 +219,9 @@ template <typename IRUnitT> class LLVM_ABI ChangeReporter {
 
   // Determine if this pass/IR is interesting and if so, save the IR
   // otherwise it is left on the stack without data.
-  void saveIRBeforePass(Any IR, StringRef PassID, StringRef PassName);
+  void saveIRBeforePass(const Any &IR, StringRef PassID, StringRef PassName);
   // Compare the IR from before the pass after the pass.
-  void handleIRAfterPass(Any IR, StringRef PassID, StringRef PassName);
+  void handleIRAfterPass(const Any &IR, StringRef PassID, StringRef PassName);
   // Handle the situation where a pass is invalidated.
   void handleInvalidatedPass(StringRef PassID);
 
@@ -229,16 +230,16 @@ template <typename IRUnitT> class LLVM_ABI ChangeReporter {
   void registerRequiredCallbacks(PassInstrumentationCallbacks &PIC);
 
   // Called on the first IR processed.
-  virtual void handleInitialIR(Any IR) = 0;
+  virtual void handleInitialIR(const Any &IR) = 0;
   // Called before and after a pass to get the representation of the IR.
-  virtual void generateIRRepresentation(Any IR, StringRef PassID,
+  virtual void generateIRRepresentation(const Any &IR, StringRef PassID,
                                         IRUnitT &Output) = 0;
   // Called when the pass is not iteresting.
   virtual void omitAfter(StringRef PassID, std::string &Name) = 0;
   // Called when an interesting IR has changed.
   virtual void handleAfter(StringRef PassID, std::string &Name,
                            const IRUnitT &Before, const IRUnitT &After,
-                           Any) = 0;
+                           const Any &) = 0;
   // Called when an interesting pass is invalidated.
   virtual void handleInvalidated(StringRef PassID) = 0;
   // Called when the IR or pass is not interesting.
@@ -263,7 +264,7 @@ class LLVM_ABI TextChangeReporter : public ChangeReporter<IRUnitT> {
   TextChangeReporter(bool Verbose);
 
   // Print a module dump of the first IR that is changed.
-  void handleInitialIR(Any IR) override;
+  void handleInitialIR(const Any &IR) override;
   // Report that the IR was omitted because it did not change.
   void omitAfter(StringRef PassID, std::string &Name) override;
   // Report that the pass was invalidated.
@@ -291,12 +292,12 @@ class LLVM_ABI IRChangedPrinter : public TextChangeReporter<std::string> {
 
 protected:
   // Called before and after a pass to get the representation of the IR.
-  void generateIRRepresentation(Any IR, StringRef PassID,
+  void generateIRRepresentation(const Any &IR, StringRef PassID,
                                 std::string &Output) override;
   // Called when an interesting IR has changed.
   void handleAfter(StringRef PassID, std::string &Name,
                    const std::string &Before, const std::string &After,
-                   Any) override;
+                   const Any &) override;
 };
 
 class LLVM_ABI IRChangedTester : public IRChangedPrinter {
@@ -309,7 +310,7 @@ class LLVM_ABI IRChangedTester : public IRChangedPrinter {
   void handleIR(const std::string &IR, StringRef PassID);
 
   // Check initial IR
-  void handleInitialIR(Any IR) override;
+  void handleInitialIR(const Any &IR) override;
   // Do nothing.
   void omitAfter(StringRef PassID, std::string &Name) override;
   // Do nothing.
@@ -322,7 +323,7 @@ class LLVM_ABI IRChangedTester : public IRChangedPrinter {
   // Call test as interesting IR has changed.
   void handleAfter(StringRef PassID, std::string &Name,
                    const std::string &Before, const std::string &After,
-                   Any) override;
+                   const Any &) override;
 };
 
 // Information that needs to be saved for a basic block in order to compare
@@ -428,7 +429,7 @@ template <typename T> class IRComparer {
           CompareFunc);
 
   // Analyze \p IR and build the IR representation in \p Data.
-  static void analyzeIR(Any IR, IRDataT<T> &Data);
+  static void analyzeIR(const Any &IR, IRDataT<T> &Data);
 
 protected:
   // Generate the data for \p F into \p Data.
@@ -456,13 +457,13 @@ class LLVM_ABI InLineChangePrinter
 
 protected:
   // Create a representation of the IR.
-  void generateIRRepresentation(Any IR, StringRef PassID,
+  void generateIRRepresentation(const Any &IR, StringRef PassID,
                                 IRDataT<EmptyData> &Output) override;
 
   // Called when an interesting IR has changed.
   void handleAfter(StringRef PassID, std::string &Name,
                    const IRDataT<EmptyData> &Before,
-                   const IRDataT<EmptyData> &After, Any) override;
+                   const IRDataT<EmptyData> &After, const Any &) override;
 
   void handleFunctionCompare(StringRef Name, StringRef Prefix, StringRef PassID,
                              StringRef Divider, bool InModule, unsigned Minor,
@@ -495,7 +496,7 @@ class TimeProfilingPassesHandler {
 
 private:
   // Implementation of pass instrumentation callbacks.
-  void runBeforePass(StringRef PassID, Any IR);
+  void runBeforePass(StringRef PassID, const Any &IR);
   void runAfterPass();
 };
 
@@ -544,16 +545,16 @@ class LLVM_ABI DotCfgChangeReporter : public ChangeReporter<IRDataT<DCData>> {
   bool initializeHTML();
 
   // Called on the first IR processed.
-  void handleInitialIR(Any IR) override;
+  void handleInitialIR(const Any &IR) override;
   // Called before and after a pass to get the representation of the IR.
-  void generateIRRepresentation(Any IR, StringRef PassID,
+  void generateIRRepresentation(const Any &IR, StringRef PassID,
                                 IRDataT<DCData> &Output) override;
   // Called when the pass is not iteresting.
   void omitAfter(StringRef PassID, std::string &Name) override;
   // Called when an interesting IR has changed.
   void handleAfter(StringRef PassID, std::string &Name,
                    const IRDataT<DCData> &Before, const IRDataT<DCData> &After,
-                   Any) override;
+                   const Any &) override;
   // Called when an interesting pass is invalidated.
   void handleInvalidated(StringRef PassID) override;
   // Called when the IR or pass is not interesting.
diff --git a/llvm/include/llvm/Transforms/IPO/SampleProfileProbe.h b/llvm/include/llvm/Transforms/IPO/SampleProfileProbe.h
index 1f6234f6778b0..cf62f8fddb38c 100644
--- a/llvm/include/llvm/Transforms/IPO/SampleProfileProbe.h
+++ b/llvm/include/llvm/Transforms/IPO/SampleProfileProbe.h
@@ -49,7 +49,7 @@ class PseudoProbeVerifier {
   LLVM_ABI void registerCallbacks(PassInstrumentationCallbacks &PIC);
 
   // Implementation of pass instrumentation callbacks for new pass manager.
-  LLVM_ABI void runAfterPass(StringRef PassID, Any IR);
+  LLVM_ABI void runAfterPass(StringRef PassID, const Any &IR);
 
 private:
   // Allow a little bias due the rounding to integral factors.
diff --git a/llvm/lib/CodeGen/TargetPassConfig.cpp b/llvm/lib/CodeGen/TargetPassConfig.cpp
index 4996f5517fc0a..ae0b2e0e86814 100644
--- a/llvm/lib/CodeGen/TargetPassConfig.cpp
+++ b/llvm/lib/CodeGen/TargetPassConfig.cpp
@@ -543,7 +543,7 @@ void llvm::registerCodeGenCallback(PassInstrumentationCallbacks &PIC,
                                    TargetMachine &TM) {
 
   // Register a callback for disabling passes.
-  PIC.registerShouldRunOptionalPassCallback([](StringRef P, Any) {
+  PIC.registerShouldRunOptionalPassCallback([](StringRef P, const Any &) {
 
 #define DISABLE_PASS(Option, Name)                                             \
   if (Option && P.contains(#Name))                                             \
diff --git a/llvm/lib/IR/DroppedVariableStatsIR.cpp b/llvm/lib/IR/DroppedVariableStatsIR.cpp
index 382a024c3ee15..38d5eb05e70c9 100644
--- a/llvm/lib/IR/DroppedVariableStatsIR.cpp
+++ b/llvm/lib/IR/DroppedVariableStatsIR.cpp
@@ -20,12 +20,12 @@
 using namespace llvm;
 
 template <typename IRUnitT>
-const IRUnitT *DroppedVariableStatsIR::unwrapIR(Any IR) {
-  const IRUnitT **IRPtr = llvm::any_cast<const IRUnitT *>(&IR);
+const IRUnitT *DroppedVariableStatsIR::unwrapIR(const Any &IR) {
+  const IRUnitT *const *IRPtr = llvm::any_cast<const IRUnitT *>(&IR);
   return IRPtr ? *IRPtr : nullptr;
 }
 
-void DroppedVariableStatsIR::runBeforePass(StringRef P, Any IR) {
+void DroppedVariableStatsIR::runBeforePass(StringRef P, const Any &IR) {
   setup();
   if (const auto *M = unwrapIR<Module>(IR))
     return this->runOnModule(P, M, true);
@@ -33,7 +33,7 @@ void DroppedVariableStatsIR::runBeforePass(StringRef P, Any IR) {
     return this->runOnFunction(P, F, true);
 }
 
-void DroppedVariableStatsIR::runAfterPass(StringRef P, Any IR) {
+void DroppedVariableStatsIR::runAfterPass(StringRef P, const Any &IR) {
   if (const auto *M = unwrapIR<Module>(IR))
     runAfterPassModule(P, M);
   else if (const auto *F = unwrapIR<Function>(IR))
@@ -92,9 +92,9 @@ void DroppedVariableStatsIR::registerCallbacks(
     return;
 
   PIC.registerBeforeNonSkippedPassCallback(
-      [this](StringRef P, Any IR) { return runBeforePass(P, IR); });
+      [this](StringRef P, const Any &IR) { return runBeforePass(P, IR); });
   PIC.registerAfterPassCallback(
-      [this](StringRef P, Any IR, const PreservedAnalyses &PA) {
+      [this](StringRef P, const Any &IR, const PreservedAnalyses &PA) {
         return runAfterPass(P, IR);
       });
   PIC.registerAfterPassInvalidatedCallback(
diff --git a/llvm/lib/IR/PassTimingInfo.cpp b/llvm/lib/IR/PassTimingInfo.cpp
index cb1b91a98b036..c7c3bc530516b 100644
--- a/llvm/lib/IR/PassTimingInfo.cpp
+++ b/llvm/lib/IR/PassTimingInfo.cpp
@@ -301,9 +301,9 @@ void TimePassesHandler::registerCallbacks(PassInstrumentationCallbacks &PIC) {
     return;
 
   PIC.registerBeforeNonSkippedPassCallback(
-      [this](StringRef P, Any) { this->startPassTimer(P); });
+      [this](StringRef P, const Any &) { this->startPassTimer(P); });
   PIC.registerAfterPassCallback(
-      [this](StringRef P, Any, const PreservedAnalyses &) {
+      [this](StringRef P, const Any &, const PreservedAnalyses &) {
         this->stopPassTimer(P);
       });
   PIC.registerAfterPassInvalidatedCallback(
@@ -311,7 +311,7 @@ void TimePassesHandler::registerCallbacks(PassInstrumentationCallbacks &PIC) {
         this->stopPassTimer(P);
       });
   PIC.registerBeforeAnalysisCallback(
-      [this](StringRef P, Any) { this->startAnalysisTimer(P); });
+      [this](StringRef P, const Any &) { this->startAnalysisTimer(P); });
   PIC.registerAfterAnalysisCallback(
-      [this](StringRef P, Any) { this->stopAnalysisTimer(P); });
+      [this](StringRef P, const Any &) { this->stopAnalysisTimer(P); });
 }
diff --git a/llvm/lib/Passes/StandardInstrumentations.cpp b/llvm/lib/Passes/StandardInstrumentations.cpp
index 075cd56402ca9..33e120dc96936 100644
--- a/llvm/lib/Passes/StandardInstrumentations.cpp
+++ b/llvm/lib/Passes/StandardInstrumentations.cpp
@@ -140,8 +140,8 @@ static cl::opt<bool>
                     cl::desc("Dump dropped debug variables stats"),
                     cl::init(false));
 
-template <typename IRUnitT> static const IRUnitT *unwrapIR(Any IR) {
-  const IRUnitT **IRPtr = llvm::any_cast<const IRUnitT *>(&IR);
+template <typename IRUnitT> static const IRUnitT *unwrapIR(const Any &IR) {
+  const IRUnitT *const *IRPtr = llvm::any_cast<const IRUnitT *>(&IR);
   return IRPtr ? *IRPtr : nullptr;
 }
 
@@ -161,7 +161,7 @@ static cl::opt<std::string>
 
 /// Extract Module out of \p IR unit. May return nullptr if \p IR does not match
 /// certain global filters. Will never return nullptr if \p Force is true.
-const Module *unwrapModule(Any IR, bool Force = false) {
+const Module *unwrapModule(const Any &IR, bool Force = false) {
   if (const auto *M = unwrapIR<Module>(IR))
     return M;
 
@@ -237,7 +237,7 @@ void printIR(raw_ostream &OS, const MachineFunction *MF) {
   MF->print(OS);
 }
 
-std::string getIRName(Any IR) {
+std::string getIRName(const Any &IR) {
   if (unwrapIR<Module>(IR))
     return "[module]";
 
@@ -273,7 +273,7 @@ bool sccContainsFilterPrintFunc(const LazyCallGraph::SCC &C) {
          isFunctionInPrintList("*");
 }
 
-bool shouldPrintIR(Any IR) {
+bool shouldPrintIR(const Any &IR) {
   if (const auto *M = unwrapIR<Module>(IR))
     return moduleContainsFilterPrintFunc(*M);
 
@@ -293,7 +293,7 @@ bool shouldPrintIR(Any IR) {
 
 /// Generic IR-printing helper that unpacks a pointer to IRUnit wrapped into
 /// Any and does actual print job.
-void unwrapAndPrint(raw_ostream &OS, Any IR) {
+void unwrapAndPrint(raw_ostream &OS, const Any &IR) {
   if (!shouldPrintIR(IR))
     return;
 
@@ -357,7 +357,7 @@ std::string makeHTMLReady(StringRef SR) {
 }
 
 // Return the module when that is the appropriate level of comparison for \p IR.
-const Module *getModuleForComparison(Any IR) {
+const Module *getModuleForComparison(const Any &IR) {
   if (const auto *M = unwrapIR<Module>(IR))
     return M;
   if (const auto *C = unwrapIR<LazyCallGraph::SCC>(IR))
@@ -371,7 +371,7 @@ bool isInterestingFunction(const Function &F) {
 
 // Return true when this is a pass on IR for which printing
 // of changes is desired.
-bool isInteresting(Any IR, StringRef PassID, StringRef PassName) {
+bool isInteresting(const Any &IR, StringRef PassID, StringRef PassName) {
   if (isIgnored(PassID) || !isPassInPrintList(PassName))
     return false;
   if (const auto *F = unwrapIR<Function>(IR))
@@ -386,7 +386,7 @@ template <typename T> ChangeReporter<T>::~ChangeReporter() {
 }
 
 template <typename T>
-void ChangeReporter<T>::saveIRBeforePass(Any IR, StringRef PassID,
+void ChangeReporter<T>::saveIRBeforePass(const Any &IR, StringRef PassID,
                                          StringRef PassName) {
   // Is this the initial IR?
   if (InitialIR) {
@@ -409,7 +409,7 @@ void ChangeReporter<T>::saveIRBeforePass(Any IR, StringRef PassID,
 }
 
 template <typename T>
-void ChangeReporter<T>::handleIRAfterPass(Any IR, StringRef PassID,
+void ChangeReporter<T>::handleIRAfterPass(const Any &IR, StringRef PassID,
                                           StringRef PassName) {
   assert(!BeforeStack.empty() && "Unexpected empty stack encountered.");
 
@@ -454,12 +454,13 @@ void ChangeReporter<T>::handleInvalidatedPass(StringRef PassID) {
 template <typename T>
 void ChangeReporter<T>::registerRequiredCallbacks(
     PassInstrumentationCallbacks &PIC) {
-  PIC.registerBeforeNonSkippedPassCallback([&PIC, this](StringRef P, Any IR) {
-    saveIRBeforePass(IR, P, PIC.getPassNameForClassName(P));
-  });
+  PIC.registerBeforeNonSkippedPassCallback(
+      [&PIC, this](StringRef P, const Any &IR) {
+        saveIRBeforePass(IR, P, PIC.getPassNameForClassName(P));
+      });
 
   PIC.registerAfterPassCallback(
-      [&PIC, this](StringRef P, Any IR, const PreservedAnalyses &) {
+      [&PIC, this](StringRef P, const Any &IR, const PreservedAnalyses &) {
         handleIRAfterPass(IR, P, PIC.getPassNameForClassName(P));
       });
   PIC.registerAfterPassInvalidatedCallback(
@@ -472,7 +473,8 @@ template <typename T>
 TextChangeReporter<T>::TextChangeReporter(bool Verbose)
     : ChangeReporter<T>(Verbose), Out(dbgs()) {}
 
-template <typename T> void TextChangeReporter<T>::handleInitialIR(Any IR) {
+template <typename T>
+void TextChangeReporter<T>::handleInitialIR(const Any &IR) {
   // Always print the module.
   // Unwrap and print directly to avoid filtering problems in general routines.
   auto *M = unwrapModule(IR, /*Force=*/true);
@@ -513,7 +515,7 @@ void IRChangedPrinter::registerCallbacks(PassInstrumentationCallbacks &PIC) {
     TextChangeReporter<std::string>::registerRequiredCallbacks(PIC);
 }
 
-void IRChangedPrinter::generateIRRepresentation(Any IR, StringRef PassID,
+void IRChangedPrinter::generateIRRepresentation(const Any &IR, StringRef PassID,
                                                 std::string &Output) {
   raw_string_ostream OS(Output);
   unwrapAndPrint(OS, IR);
@@ -522,7 +524,7 @@ void IRChangedPrinter::generateIRRepresentation(Any IR, StringRef PassID,
 
 void IRChangedPrinter::handleAfter(StringRef PassID, std::string &Name,
                                    const std::string &Before,
-                                   const std::string &After, Any) {
+                              ...
[truncated]

@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-debuginfo

Author: Florian Hahn (fhahn)

Changes

llvm::Any has a heap allocation on every copy and pass instrumentations take the IR unit to instrument as llvm::Any. In the default pipeline without any additional options, OptNoneInstrumentation is registered and runs on each executed pass.

Update to take llvm::Any as const &, to avoid unnecessary heap allocations and improves compile-time:

  • stage1-O3: -0.25%
  • stage1-ReleaseThinLTO: -0.26%
  • stage1-ReleaseLTO-g: -0.20%
  • stage1-aarch64-O3: -0.22%
  • stage2-O3: -0.26%
  • clang: -0.46%

https://llvm-compile-time-tracker.com/compare.php?from=5371bcaa368e59a99a03f6a726949ee52e07fb65&amp;to=a4544f9d91d9f806791f2ba74cd93b0685fc3f75&amp;stat=instructions:u

I also have a follow-up that makes llvm:Any store pointer values inline, which gives some more gains, but it would require changing the layout of llvm::Any.


Patch is 43.73 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/215120.diff

13 Files Affected:

  • (modified) llvm/include/llvm/IR/DroppedVariableStatsIR.h (+3-3)
  • (modified) llvm/include/llvm/IR/PassInstrumentation.h (+7-7)
  • (modified) llvm/include/llvm/Passes/StandardInstrumentations.h (+23-22)
  • (modified) llvm/include/llvm/Transforms/IPO/SampleProfileProbe.h (+1-1)
  • (modified) llvm/lib/CodeGen/TargetPassConfig.cpp (+1-1)
  • (modified) llvm/lib/IR/DroppedVariableStatsIR.cpp (+6-6)
  • (modified) llvm/lib/IR/PassTimingInfo.cpp (+4-4)
  • (modified) llvm/lib/Passes/StandardInstrumentations.cpp (+84-75)
  • (modified) llvm/lib/Transforms/IPO/SampleProfileProbe.cpp (+7-6)
  • (modified) llvm/lib/Transforms/Scalar/LoopPassManager.cpp (+3-2)
  • (modified) llvm/lib/Transforms/Utils/Debugify.cpp (+9-6)
  • (modified) llvm/tools/llvm-reduce/deltas/RunIRPasses.cpp (+1-1)
  • (modified) llvm/unittests/IR/PassBuilderCallbacksTest.cpp (+19-15)
diff --git a/llvm/include/llvm/IR/DroppedVariableStatsIR.h b/llvm/include/llvm/IR/DroppedVariableStatsIR.h
index 9fc231999eb4f..8c1a3e87b2606 100644
--- a/llvm/include/llvm/IR/DroppedVariableStatsIR.h
+++ b/llvm/include/llvm/IR/DroppedVariableStatsIR.h
@@ -34,9 +34,9 @@ class LLVM_ABI DroppedVariableStatsIR : public DroppedVariableStats {
   DroppedVariableStatsIR(bool DroppedVarStatsEnabled)
       : llvm::DroppedVariableStats(DroppedVarStatsEnabled) {}
 
-  void runBeforePass(StringRef P, Any IR);
+  void runBeforePass(StringRef P, const Any &IR);
 
-  void runAfterPass(StringRef P, Any IR);
+  void runAfterPass(StringRef P, const Any &IR);
 
   void registerCallbacks(PassInstrumentationCallbacks &PIC);
 
@@ -81,7 +81,7 @@ class LLVM_ABI DroppedVariableStatsIR : public DroppedVariableStats {
       DenseMap<StringRef, DenseMap<VarID, DILocation *>> &InlinedAtsMap,
       StringRef FuncName, bool Before) override;
 
-  template <typename IRUnitT> static const IRUnitT *unwrapIR(Any IR);
+  template <typename IRUnitT> static const IRUnitT *unwrapIR(const Any &IR);
 };
 
 } // namespace llvm
diff --git a/llvm/include/llvm/IR/PassInstrumentation.h b/llvm/include/llvm/IR/PassInstrumentation.h
index a7ecd3aa5e2a4..52eba45542390 100644
--- a/llvm/include/llvm/IR/PassInstrumentation.h
+++ b/llvm/include/llvm/IR/PassInstrumentation.h
@@ -83,14 +83,14 @@ class PassInstrumentationCallbacks {
   // already invalidated IRUnit is unsafe. There are ways to handle invalidated
   // IRUnits in a safe way, and we might pursue that as soon as there is a
   // useful instrumentation that needs it.
-  using BeforePassFunc = bool(StringRef, Any);
-  using BeforeSkippedPassFunc = void(StringRef, Any);
-  using BeforeNonSkippedPassFunc = void(StringRef, Any);
-  using AfterPassFunc = void(StringRef, Any, const PreservedAnalyses &);
+  using BeforePassFunc = bool(StringRef, const Any &);
+  using BeforeSkippedPassFunc = void(StringRef, const Any &);
+  using BeforeNonSkippedPassFunc = void(StringRef, const Any &);
+  using AfterPassFunc = void(StringRef, const Any &, const PreservedAnalyses &);
   using AfterPassInvalidatedFunc = void(StringRef, const PreservedAnalyses &);
-  using BeforeAnalysisFunc = void(StringRef, Any);
-  using AfterAnalysisFunc = void(StringRef, Any);
-  using AnalysisInvalidatedFunc = void(StringRef, Any);
+  using BeforeAnalysisFunc = void(StringRef, const Any &);
+  using AfterAnalysisFunc = void(StringRef, const Any &);
+  using AnalysisInvalidatedFunc = void(StringRef, const Any &);
   using AnalysesClearedFunc = void(StringRef);
 
 public:
diff --git a/llvm/include/llvm/Passes/StandardInstrumentations.h b/llvm/include/llvm/Passes/StandardInstrumentations.h
index 4ee5ab2554868..513b71d93f0ce 100644
--- a/llvm/include/llvm/Passes/StandardInstrumentations.h
+++ b/llvm/include/llvm/Passes/StandardInstrumentations.h
@@ -66,8 +66,8 @@ class PrintIRInstrumentation {
           IRName{IRName}, PassID(PassID) {}
   };
 
-  void printBeforePass(StringRef PassID, Any IR);
-  void printAfterPass(StringRef PassID, Any IR);
+  void printBeforePass(StringRef PassID, const Any &IR);
+  void printAfterPass(StringRef PassID, const Any &IR);
   void printAfterPassInvalidated(StringRef PassID);
 
   bool shouldPrintBeforePass(StringRef PassID);
@@ -78,7 +78,8 @@ class PrintIRInstrumentation {
   bool shouldPrintBeforeSomePassNumber();
   bool shouldPrintAfterSomePassNumber();
 
-  void pushPassRunDescriptor(StringRef PassID, Any IR, unsigned PassNumber);
+  void pushPassRunDescriptor(StringRef PassID, const Any &IR,
+                             unsigned PassNumber);
   PassRunDescriptor popPassRunDescriptor(StringRef PassID);
 
   enum class IRDumpFileSuffixType {
@@ -109,7 +110,7 @@ class OptNoneInstrumentation {
 
 private:
   bool DebugLogging;
-  bool shouldRun(StringRef PassID, Any IR);
+  bool shouldRun(StringRef PassID, const Any &IR);
 };
 
 class OptPassGateInstrumentation {
@@ -117,7 +118,7 @@ class OptPassGateInstrumentation {
   bool HasWrittenIR = false;
 public:
   OptPassGateInstrumentation(LLVMContext &Context) : Context(Context) {}
-  LLVM_ABI bool shouldRun(StringRef PassName, Any IR);
+  LLVM_ABI bool shouldRun(StringRef PassName, const Any &IR);
   LLVM_ABI void registerCallbacks(PassInstrumentationCallbacks &PIC);
 };
 
@@ -218,9 +219,9 @@ template <typename IRUnitT> class LLVM_ABI ChangeReporter {
 
   // Determine if this pass/IR is interesting and if so, save the IR
   // otherwise it is left on the stack without data.
-  void saveIRBeforePass(Any IR, StringRef PassID, StringRef PassName);
+  void saveIRBeforePass(const Any &IR, StringRef PassID, StringRef PassName);
   // Compare the IR from before the pass after the pass.
-  void handleIRAfterPass(Any IR, StringRef PassID, StringRef PassName);
+  void handleIRAfterPass(const Any &IR, StringRef PassID, StringRef PassName);
   // Handle the situation where a pass is invalidated.
   void handleInvalidatedPass(StringRef PassID);
 
@@ -229,16 +230,16 @@ template <typename IRUnitT> class LLVM_ABI ChangeReporter {
   void registerRequiredCallbacks(PassInstrumentationCallbacks &PIC);
 
   // Called on the first IR processed.
-  virtual void handleInitialIR(Any IR) = 0;
+  virtual void handleInitialIR(const Any &IR) = 0;
   // Called before and after a pass to get the representation of the IR.
-  virtual void generateIRRepresentation(Any IR, StringRef PassID,
+  virtual void generateIRRepresentation(const Any &IR, StringRef PassID,
                                         IRUnitT &Output) = 0;
   // Called when the pass is not iteresting.
   virtual void omitAfter(StringRef PassID, std::string &Name) = 0;
   // Called when an interesting IR has changed.
   virtual void handleAfter(StringRef PassID, std::string &Name,
                            const IRUnitT &Before, const IRUnitT &After,
-                           Any) = 0;
+                           const Any &) = 0;
   // Called when an interesting pass is invalidated.
   virtual void handleInvalidated(StringRef PassID) = 0;
   // Called when the IR or pass is not interesting.
@@ -263,7 +264,7 @@ class LLVM_ABI TextChangeReporter : public ChangeReporter<IRUnitT> {
   TextChangeReporter(bool Verbose);
 
   // Print a module dump of the first IR that is changed.
-  void handleInitialIR(Any IR) override;
+  void handleInitialIR(const Any &IR) override;
   // Report that the IR was omitted because it did not change.
   void omitAfter(StringRef PassID, std::string &Name) override;
   // Report that the pass was invalidated.
@@ -291,12 +292,12 @@ class LLVM_ABI IRChangedPrinter : public TextChangeReporter<std::string> {
 
 protected:
   // Called before and after a pass to get the representation of the IR.
-  void generateIRRepresentation(Any IR, StringRef PassID,
+  void generateIRRepresentation(const Any &IR, StringRef PassID,
                                 std::string &Output) override;
   // Called when an interesting IR has changed.
   void handleAfter(StringRef PassID, std::string &Name,
                    const std::string &Before, const std::string &After,
-                   Any) override;
+                   const Any &) override;
 };
 
 class LLVM_ABI IRChangedTester : public IRChangedPrinter {
@@ -309,7 +310,7 @@ class LLVM_ABI IRChangedTester : public IRChangedPrinter {
   void handleIR(const std::string &IR, StringRef PassID);
 
   // Check initial IR
-  void handleInitialIR(Any IR) override;
+  void handleInitialIR(const Any &IR) override;
   // Do nothing.
   void omitAfter(StringRef PassID, std::string &Name) override;
   // Do nothing.
@@ -322,7 +323,7 @@ class LLVM_ABI IRChangedTester : public IRChangedPrinter {
   // Call test as interesting IR has changed.
   void handleAfter(StringRef PassID, std::string &Name,
                    const std::string &Before, const std::string &After,
-                   Any) override;
+                   const Any &) override;
 };
 
 // Information that needs to be saved for a basic block in order to compare
@@ -428,7 +429,7 @@ template <typename T> class IRComparer {
           CompareFunc);
 
   // Analyze \p IR and build the IR representation in \p Data.
-  static void analyzeIR(Any IR, IRDataT<T> &Data);
+  static void analyzeIR(const Any &IR, IRDataT<T> &Data);
 
 protected:
   // Generate the data for \p F into \p Data.
@@ -456,13 +457,13 @@ class LLVM_ABI InLineChangePrinter
 
 protected:
   // Create a representation of the IR.
-  void generateIRRepresentation(Any IR, StringRef PassID,
+  void generateIRRepresentation(const Any &IR, StringRef PassID,
                                 IRDataT<EmptyData> &Output) override;
 
   // Called when an interesting IR has changed.
   void handleAfter(StringRef PassID, std::string &Name,
                    const IRDataT<EmptyData> &Before,
-                   const IRDataT<EmptyData> &After, Any) override;
+                   const IRDataT<EmptyData> &After, const Any &) override;
 
   void handleFunctionCompare(StringRef Name, StringRef Prefix, StringRef PassID,
                              StringRef Divider, bool InModule, unsigned Minor,
@@ -495,7 +496,7 @@ class TimeProfilingPassesHandler {
 
 private:
   // Implementation of pass instrumentation callbacks.
-  void runBeforePass(StringRef PassID, Any IR);
+  void runBeforePass(StringRef PassID, const Any &IR);
   void runAfterPass();
 };
 
@@ -544,16 +545,16 @@ class LLVM_ABI DotCfgChangeReporter : public ChangeReporter<IRDataT<DCData>> {
   bool initializeHTML();
 
   // Called on the first IR processed.
-  void handleInitialIR(Any IR) override;
+  void handleInitialIR(const Any &IR) override;
   // Called before and after a pass to get the representation of the IR.
-  void generateIRRepresentation(Any IR, StringRef PassID,
+  void generateIRRepresentation(const Any &IR, StringRef PassID,
                                 IRDataT<DCData> &Output) override;
   // Called when the pass is not iteresting.
   void omitAfter(StringRef PassID, std::string &Name) override;
   // Called when an interesting IR has changed.
   void handleAfter(StringRef PassID, std::string &Name,
                    const IRDataT<DCData> &Before, const IRDataT<DCData> &After,
-                   Any) override;
+                   const Any &) override;
   // Called when an interesting pass is invalidated.
   void handleInvalidated(StringRef PassID) override;
   // Called when the IR or pass is not interesting.
diff --git a/llvm/include/llvm/Transforms/IPO/SampleProfileProbe.h b/llvm/include/llvm/Transforms/IPO/SampleProfileProbe.h
index 1f6234f6778b0..cf62f8fddb38c 100644
--- a/llvm/include/llvm/Transforms/IPO/SampleProfileProbe.h
+++ b/llvm/include/llvm/Transforms/IPO/SampleProfileProbe.h
@@ -49,7 +49,7 @@ class PseudoProbeVerifier {
   LLVM_ABI void registerCallbacks(PassInstrumentationCallbacks &PIC);
 
   // Implementation of pass instrumentation callbacks for new pass manager.
-  LLVM_ABI void runAfterPass(StringRef PassID, Any IR);
+  LLVM_ABI void runAfterPass(StringRef PassID, const Any &IR);
 
 private:
   // Allow a little bias due the rounding to integral factors.
diff --git a/llvm/lib/CodeGen/TargetPassConfig.cpp b/llvm/lib/CodeGen/TargetPassConfig.cpp
index 4996f5517fc0a..ae0b2e0e86814 100644
--- a/llvm/lib/CodeGen/TargetPassConfig.cpp
+++ b/llvm/lib/CodeGen/TargetPassConfig.cpp
@@ -543,7 +543,7 @@ void llvm::registerCodeGenCallback(PassInstrumentationCallbacks &PIC,
                                    TargetMachine &TM) {
 
   // Register a callback for disabling passes.
-  PIC.registerShouldRunOptionalPassCallback([](StringRef P, Any) {
+  PIC.registerShouldRunOptionalPassCallback([](StringRef P, const Any &) {
 
 #define DISABLE_PASS(Option, Name)                                             \
   if (Option && P.contains(#Name))                                             \
diff --git a/llvm/lib/IR/DroppedVariableStatsIR.cpp b/llvm/lib/IR/DroppedVariableStatsIR.cpp
index 382a024c3ee15..38d5eb05e70c9 100644
--- a/llvm/lib/IR/DroppedVariableStatsIR.cpp
+++ b/llvm/lib/IR/DroppedVariableStatsIR.cpp
@@ -20,12 +20,12 @@
 using namespace llvm;
 
 template <typename IRUnitT>
-const IRUnitT *DroppedVariableStatsIR::unwrapIR(Any IR) {
-  const IRUnitT **IRPtr = llvm::any_cast<const IRUnitT *>(&IR);
+const IRUnitT *DroppedVariableStatsIR::unwrapIR(const Any &IR) {
+  const IRUnitT *const *IRPtr = llvm::any_cast<const IRUnitT *>(&IR);
   return IRPtr ? *IRPtr : nullptr;
 }
 
-void DroppedVariableStatsIR::runBeforePass(StringRef P, Any IR) {
+void DroppedVariableStatsIR::runBeforePass(StringRef P, const Any &IR) {
   setup();
   if (const auto *M = unwrapIR<Module>(IR))
     return this->runOnModule(P, M, true);
@@ -33,7 +33,7 @@ void DroppedVariableStatsIR::runBeforePass(StringRef P, Any IR) {
     return this->runOnFunction(P, F, true);
 }
 
-void DroppedVariableStatsIR::runAfterPass(StringRef P, Any IR) {
+void DroppedVariableStatsIR::runAfterPass(StringRef P, const Any &IR) {
   if (const auto *M = unwrapIR<Module>(IR))
     runAfterPassModule(P, M);
   else if (const auto *F = unwrapIR<Function>(IR))
@@ -92,9 +92,9 @@ void DroppedVariableStatsIR::registerCallbacks(
     return;
 
   PIC.registerBeforeNonSkippedPassCallback(
-      [this](StringRef P, Any IR) { return runBeforePass(P, IR); });
+      [this](StringRef P, const Any &IR) { return runBeforePass(P, IR); });
   PIC.registerAfterPassCallback(
-      [this](StringRef P, Any IR, const PreservedAnalyses &PA) {
+      [this](StringRef P, const Any &IR, const PreservedAnalyses &PA) {
         return runAfterPass(P, IR);
       });
   PIC.registerAfterPassInvalidatedCallback(
diff --git a/llvm/lib/IR/PassTimingInfo.cpp b/llvm/lib/IR/PassTimingInfo.cpp
index cb1b91a98b036..c7c3bc530516b 100644
--- a/llvm/lib/IR/PassTimingInfo.cpp
+++ b/llvm/lib/IR/PassTimingInfo.cpp
@@ -301,9 +301,9 @@ void TimePassesHandler::registerCallbacks(PassInstrumentationCallbacks &PIC) {
     return;
 
   PIC.registerBeforeNonSkippedPassCallback(
-      [this](StringRef P, Any) { this->startPassTimer(P); });
+      [this](StringRef P, const Any &) { this->startPassTimer(P); });
   PIC.registerAfterPassCallback(
-      [this](StringRef P, Any, const PreservedAnalyses &) {
+      [this](StringRef P, const Any &, const PreservedAnalyses &) {
         this->stopPassTimer(P);
       });
   PIC.registerAfterPassInvalidatedCallback(
@@ -311,7 +311,7 @@ void TimePassesHandler::registerCallbacks(PassInstrumentationCallbacks &PIC) {
         this->stopPassTimer(P);
       });
   PIC.registerBeforeAnalysisCallback(
-      [this](StringRef P, Any) { this->startAnalysisTimer(P); });
+      [this](StringRef P, const Any &) { this->startAnalysisTimer(P); });
   PIC.registerAfterAnalysisCallback(
-      [this](StringRef P, Any) { this->stopAnalysisTimer(P); });
+      [this](StringRef P, const Any &) { this->stopAnalysisTimer(P); });
 }
diff --git a/llvm/lib/Passes/StandardInstrumentations.cpp b/llvm/lib/Passes/StandardInstrumentations.cpp
index 075cd56402ca9..33e120dc96936 100644
--- a/llvm/lib/Passes/StandardInstrumentations.cpp
+++ b/llvm/lib/Passes/StandardInstrumentations.cpp
@@ -140,8 +140,8 @@ static cl::opt<bool>
                     cl::desc("Dump dropped debug variables stats"),
                     cl::init(false));
 
-template <typename IRUnitT> static const IRUnitT *unwrapIR(Any IR) {
-  const IRUnitT **IRPtr = llvm::any_cast<const IRUnitT *>(&IR);
+template <typename IRUnitT> static const IRUnitT *unwrapIR(const Any &IR) {
+  const IRUnitT *const *IRPtr = llvm::any_cast<const IRUnitT *>(&IR);
   return IRPtr ? *IRPtr : nullptr;
 }
 
@@ -161,7 +161,7 @@ static cl::opt<std::string>
 
 /// Extract Module out of \p IR unit. May return nullptr if \p IR does not match
 /// certain global filters. Will never return nullptr if \p Force is true.
-const Module *unwrapModule(Any IR, bool Force = false) {
+const Module *unwrapModule(const Any &IR, bool Force = false) {
   if (const auto *M = unwrapIR<Module>(IR))
     return M;
 
@@ -237,7 +237,7 @@ void printIR(raw_ostream &OS, const MachineFunction *MF) {
   MF->print(OS);
 }
 
-std::string getIRName(Any IR) {
+std::string getIRName(const Any &IR) {
   if (unwrapIR<Module>(IR))
     return "[module]";
 
@@ -273,7 +273,7 @@ bool sccContainsFilterPrintFunc(const LazyCallGraph::SCC &C) {
          isFunctionInPrintList("*");
 }
 
-bool shouldPrintIR(Any IR) {
+bool shouldPrintIR(const Any &IR) {
   if (const auto *M = unwrapIR<Module>(IR))
     return moduleContainsFilterPrintFunc(*M);
 
@@ -293,7 +293,7 @@ bool shouldPrintIR(Any IR) {
 
 /// Generic IR-printing helper that unpacks a pointer to IRUnit wrapped into
 /// Any and does actual print job.
-void unwrapAndPrint(raw_ostream &OS, Any IR) {
+void unwrapAndPrint(raw_ostream &OS, const Any &IR) {
   if (!shouldPrintIR(IR))
     return;
 
@@ -357,7 +357,7 @@ std::string makeHTMLReady(StringRef SR) {
 }
 
 // Return the module when that is the appropriate level of comparison for \p IR.
-const Module *getModuleForComparison(Any IR) {
+const Module *getModuleForComparison(const Any &IR) {
   if (const auto *M = unwrapIR<Module>(IR))
     return M;
   if (const auto *C = unwrapIR<LazyCallGraph::SCC>(IR))
@@ -371,7 +371,7 @@ bool isInterestingFunction(const Function &F) {
 
 // Return true when this is a pass on IR for which printing
 // of changes is desired.
-bool isInteresting(Any IR, StringRef PassID, StringRef PassName) {
+bool isInteresting(const Any &IR, StringRef PassID, StringRef PassName) {
   if (isIgnored(PassID) || !isPassInPrintList(PassName))
     return false;
   if (const auto *F = unwrapIR<Function>(IR))
@@ -386,7 +386,7 @@ template <typename T> ChangeReporter<T>::~ChangeReporter() {
 }
 
 template <typename T>
-void ChangeReporter<T>::saveIRBeforePass(Any IR, StringRef PassID,
+void ChangeReporter<T>::saveIRBeforePass(const Any &IR, StringRef PassID,
                                          StringRef PassName) {
   // Is this the initial IR?
   if (InitialIR) {
@@ -409,7 +409,7 @@ void ChangeReporter<T>::saveIRBeforePass(Any IR, StringRef PassID,
 }
 
 template <typename T>
-void ChangeReporter<T>::handleIRAfterPass(Any IR, StringRef PassID,
+void ChangeReporter<T>::handleIRAfterPass(const Any &IR, StringRef PassID,
                                           StringRef PassName) {
   assert(!BeforeStack.empty() && "Unexpected empty stack encountered.");
 
@@ -454,12 +454,13 @@ void ChangeReporter<T>::handleInvalidatedPass(StringRef PassID) {
 template <typename T>
 void ChangeReporter<T>::registerRequiredCallbacks(
     PassInstrumentationCallbacks &PIC) {
-  PIC.registerBeforeNonSkippedPassCallback([&PIC, this](StringRef P, Any IR) {
-    saveIRBeforePass(IR, P, PIC.getPassNameForClassName(P));
-  });
+  PIC.registerBeforeNonSkippedPassCallback(
+      [&PIC, this](StringRef P, const Any &IR) {
+        saveIRBeforePass(IR, P, PIC.getPassNameForClassName(P));
+      });
 
   PIC.registerAfterPassCallback(
-      [&PIC, this](StringRef P, Any IR, const PreservedAnalyses &) {
+      [&PIC, this](StringRef P, const Any &IR, const PreservedAnalyses &) {
         handleIRAfterPass(IR, P, PIC.getPassNameForClassName(P));
       });
   PIC.registerAfterPassInvalidatedCallback(
@@ -472,7 +473,8 @@ template <typename T>
 TextChangeReporter<T>::TextChangeReporter(bool Verbose)
     : ChangeReporter<T>(Verbose), Out(dbgs()) {}
 
-template <typename T> void TextChangeReporter<T>::handleInitialIR(Any IR) {
+template <typename T>
+void TextChangeReporter<T>::handleInitialIR(const Any &IR) {
   // Always print the module.
   // Unwrap and print directly to avoid filtering problems in general routines.
   auto *M = unwrapModule(IR, /*Force=*/true);
@@ -513,7 +515,7 @@ void IRChangedPrinter::registerCallbacks(PassInstrumentationCallbacks &PIC) {
     TextChangeReporter<std::string>::registerRequiredCallbacks(PIC);
 }
 
-void IRChangedPrinter::generateIRRepresentation(Any IR, StringRef PassID,
+void IRChangedPrinter::generateIRRepresentation(const Any &IR, StringRef PassID,
                                                 std::string &Output) {
   raw_string_ostream OS(Output);
   unwrapAndPrint(OS, IR);
@@ -522,7 +524,7 @@ void IRChangedPrinter::generateIRRepresentation(Any IR, StringRef PassID,
 
 void IRChangedPrinter::handleAfter(StringRef PassID, std::string &Name,
                                    const std::string &Before,
-                                   const std::string &After, Any) {
+                              ...
[truncated]

@snehasish snehasish left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement!

@aengelke aengelke left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM. But maybe we can use a PointerUnion instead of Any (or something similar, I think PointerUnion requires complete objects)? I mean, we only ever use pointers and we know all possible pointer types (Module, SCC, Function, Loop, MachineFunction).

@kazutakahirata kazutakahirata left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants