[llvm] [Analysis] Avoid running transform passes that have just been run (PR #112092)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 12 04:05:04 PDT 2024


https://github.com/dtcxzyw created https://github.com/llvm/llvm-project/pull/112092

RFC link: https://discourse.llvm.org/t/rfc-pipeline-avoid-running-transform-passes-that-have-just-been-run/82467


>From 84cdd4b6fe5a4639371bb9e8255f3f0048b10638 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sat, 12 Oct 2024 10:14:24 +0800
Subject: [PATCH] [Analysis] Avoid running transform passes that have just been
 run

---
 .../llvm/Analysis/LastRunTrackingAnalysis.h   | 91 +++++++++++++++++++
 .../llvm/Transforms/InstCombine/InstCombine.h |  7 ++
 llvm/lib/Analysis/CMakeLists.txt              |  1 +
 llvm/lib/Analysis/LastRunTrackingAnalysis.cpp | 41 +++++++++
 llvm/lib/Passes/PassBuilder.cpp               |  1 +
 llvm/lib/Passes/PassRegistry.def              |  2 +
 .../InstCombine/InstructionCombining.cpp      | 14 ++-
 7 files changed, 156 insertions(+), 1 deletion(-)
 create mode 100644 llvm/include/llvm/Analysis/LastRunTrackingAnalysis.h
 create mode 100644 llvm/lib/Analysis/LastRunTrackingAnalysis.cpp

diff --git a/llvm/include/llvm/Analysis/LastRunTrackingAnalysis.h b/llvm/include/llvm/Analysis/LastRunTrackingAnalysis.h
new file mode 100644
index 00000000000000..39a7f618310aa5
--- /dev/null
+++ b/llvm/include/llvm/Analysis/LastRunTrackingAnalysis.h
@@ -0,0 +1,91 @@
+//===- LastRunTrackingAnalysis.h - Avoid running redundant pass -*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This is an analysis pass to track a set of passes that have been run, so that
+// we can avoid running a pass again if there is no change since the last run of
+// the pass.
+//
+// In this pass we track a set of passes S for each function with the following
+// transition rules:
+//   1. If pass P make changes, set S = {P}.
+//   2. If pass P doesn't make changes, set S = S + {P}.
+//
+// Before running a pass P which satisfies P(P(x)) == P(x), we check if P is in
+// S. If so, we skip this pass since we know that there will be no change.
+//
+// Notes:
+//   1. Some transform passes have parameters that may vary in the optimization
+//   pipeline. We should check if parameters in current run is compatible with
+//   that in the last run.
+//   2. Module passes are specially handled. If a module pass make changes, we
+//   clear all sets.
+//   3. This pass only tracks at the function level. Loop passes are not
+//   supported for now.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_ANALYSIS_LASTRUNTRACKINGANALYSIS_H
+#define LLVM_ANALYSIS_LASTRUNTRACKINGANALYSIS_H
+
+#include "llvm/ADT/DenseMap.h"
+#include "llvm/IR/PassManager.h"
+#include <functional>
+
+namespace llvm {
+
+/// TODO: class desc/PassID/OptionT::isCompatibleWith
+class LastRunTrackingInfo {
+public:
+  using PassID = const void *;
+  using OptionPtr = const void *;
+  // CompatibilityCheckFn is a closure that stores the parameters of last run.
+  using CompatibilityCheckFn = std::function<bool(OptionPtr)>;
+
+  /// TODO:
+  template <typename OptionT>
+  bool shouldSkip(PassID ID, const OptionT &Opt) const {
+    return shouldSkipImpl(ID, &Opt);
+  }
+  bool shouldSkip(PassID ID) const { return shouldSkipImpl(ID, nullptr); }
+
+  /// TODO:
+  template <typename OptionT>
+  void update(PassID ID, bool Changed, const OptionT &Opt) {
+    updateImpl(ID, Changed, [Opt](OptionPtr Ptr) {
+      return static_cast<const OptionT *>(Ptr)->isCompatibleWith(Opt);
+    });
+  }
+  void update(PassID ID, bool Changed) {
+    updateImpl(ID, Changed, CompatibilityCheckFn{});
+  }
+
+private:
+  bool shouldSkipImpl(PassID ID, OptionPtr Ptr) const;
+  void updateImpl(PassID ID, bool Changed, CompatibilityCheckFn CheckFn);
+
+  DenseMap<PassID, CompatibilityCheckFn> TrackedPasses;
+};
+
+class LastRunTrackingAnalysis final
+    : public AnalysisInfoMixin<LastRunTrackingAnalysis> {
+  friend AnalysisInfoMixin<LastRunTrackingAnalysis>;
+  static AnalysisKey Key;
+
+public:
+  using Result = LastRunTrackingInfo;
+  LastRunTrackingInfo run(Function &F, FunctionAnalysisManager &) {
+    return LastRunTrackingInfo();
+  }
+  LastRunTrackingInfo run(Module &M, ModuleAnalysisManager &) {
+    return LastRunTrackingInfo();
+  }
+};
+
+} // namespace llvm
+
+#endif // LLVM_ANALYSIS_LASTRUNTRACKINGANALYSIS_H
diff --git a/llvm/include/llvm/Transforms/InstCombine/InstCombine.h b/llvm/include/llvm/Transforms/InstCombine/InstCombine.h
index b4f0166239520a..21307b978734b0 100644
--- a/llvm/include/llvm/Transforms/InstCombine/InstCombine.h
+++ b/llvm/include/llvm/Transforms/InstCombine/InstCombine.h
@@ -43,12 +43,19 @@ struct InstCombineOptions {
     MaxIterations = Value;
     return *this;
   }
+
+  /// Only enable skipping in standard optimization pipeline.
+  bool isCompatibleWith(const InstCombineOptions &LastOption) const {
+    return !VerifyFixpoint && !LastOption.VerifyFixpoint &&
+           MaxIterations == 1 && LastOption.MaxIterations == 1;
+  }
 };
 
 class InstCombinePass : public PassInfoMixin<InstCombinePass> {
 private:
   InstructionWorklist Worklist;
   InstCombineOptions Options;
+  static char ID;
 
 public:
   explicit InstCombinePass(InstCombineOptions Opts = {});
diff --git a/llvm/lib/Analysis/CMakeLists.txt b/llvm/lib/Analysis/CMakeLists.txt
index 393803fad89383..0db5b80f336cb5 100644
--- a/llvm/lib/Analysis/CMakeLists.txt
+++ b/llvm/lib/Analysis/CMakeLists.txt
@@ -79,6 +79,7 @@ add_llvm_component_library(LLVMAnalysis
   InstructionPrecedenceTracking.cpp
   InstructionSimplify.cpp
   InteractiveModelRunner.cpp
+  LastRunTrackingAnalysis.cpp
   LazyBranchProbabilityInfo.cpp
   LazyBlockFrequencyInfo.cpp
   LazyCallGraph.cpp
diff --git a/llvm/lib/Analysis/LastRunTrackingAnalysis.cpp b/llvm/lib/Analysis/LastRunTrackingAnalysis.cpp
new file mode 100644
index 00000000000000..598a6cec9a06d0
--- /dev/null
+++ b/llvm/lib/Analysis/LastRunTrackingAnalysis.cpp
@@ -0,0 +1,41 @@
+//===- LastRunTrackingAnalysis.cpp - Avoid running redundant pass -*- C++ -*-=//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This is an analysis pass to track a set of passes that have been run, so that
+// we can avoid running a pass again if there is no change since the last run of
+// the pass.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Analysis/LastRunTrackingAnalysis.h"
+#include "llvm/Support/CommandLine.h"
+
+using namespace llvm;
+
+static cl::opt<bool>
+    DisableLastRunTracking("disable-last-run-tracking", cl::Hidden,
+                           cl::desc("Disable last run tracking"),
+                           cl::init(false));
+
+bool LastRunTrackingInfo::shouldSkipImpl(PassID ID, OptionPtr Ptr) const {
+  if (DisableLastRunTracking)
+    return false;
+  auto Iter = TrackedPasses.find(ID);
+  if (Iter == TrackedPasses.end())
+    return false;
+  return !Iter->second || Iter->second(Ptr);
+}
+
+void LastRunTrackingInfo::updateImpl(PassID ID, bool Changed,
+                                     CompatibilityCheckFn CheckFn) {
+  if (Changed)
+    TrackedPasses.clear();
+  TrackedPasses[ID] = std::move(CheckFn);
+}
+
+AnalysisKey LastRunTrackingAnalysis::Key;
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index f47f8ca35d7737..e515a86d1bf310 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -46,6 +46,7 @@
 #include "llvm/Analysis/InlineAdvisor.h"
 #include "llvm/Analysis/InlineSizeEstimatorAnalysis.h"
 #include "llvm/Analysis/InstCount.h"
+#include "llvm/Analysis/LastRunTrackingAnalysis.h"
 #include "llvm/Analysis/LazyCallGraph.h"
 #include "llvm/Analysis/LazyValueInfo.h"
 #include "llvm/Analysis/Lint.h"
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 90859c18c4f499..55fb8135ddd41c 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -25,6 +25,7 @@ MODULE_ANALYSIS("dxil-metadata", DXILMetadataAnalysis())
 MODULE_ANALYSIS("dxil-resource", DXILResourceAnalysis())
 MODULE_ANALYSIS("inline-advisor", InlineAdvisorAnalysis())
 MODULE_ANALYSIS("ir-similarity", IRSimilarityAnalysis())
+MODULE_ANALYSIS("last-run-tracking", LastRunTrackingAnalysis())
 MODULE_ANALYSIS("lcg", LazyCallGraphAnalysis())
 MODULE_ANALYSIS("module-summary", ModuleSummaryIndexAnalysis())
 MODULE_ANALYSIS("no-op-module", NoOpModuleAnalysis())
@@ -285,6 +286,7 @@ FUNCTION_ANALYSIS(
     MachineFunctionAnalysis(static_cast<const LLVMTargetMachine *>(TM)))
 FUNCTION_ANALYSIS("gc-function", GCFunctionAnalysis())
 FUNCTION_ANALYSIS("inliner-size-estimator", InlineSizeEstimatorAnalysis())
+FUNCTION_ANALYSIS("last-run-tracking", LastRunTrackingAnalysis())
 FUNCTION_ANALYSIS("lazy-value-info", LazyValueAnalysis())
 FUNCTION_ANALYSIS("loops", LoopAnalysis())
 FUNCTION_ANALYSIS("memdep", MemoryDependenceAnalysis())
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 954c4cf19c2077..1a512cae7bf778 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -47,6 +47,7 @@
 #include "llvm/Analysis/ConstantFolding.h"
 #include "llvm/Analysis/GlobalsModRef.h"
 #include "llvm/Analysis/InstructionSimplify.h"
+#include "llvm/Analysis/LastRunTrackingAnalysis.h"
 #include "llvm/Analysis/LazyBlockFrequencyInfo.h"
 #include "llvm/Analysis/MemoryBuiltins.h"
 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
@@ -5538,8 +5539,15 @@ void InstCombinePass::printPipeline(
   OS << '>';
 }
 
+char InstCombinePass::ID = 0;
+
 PreservedAnalyses InstCombinePass::run(Function &F,
                                        FunctionAnalysisManager &AM) {
+  auto &LRT = AM.getResult<LastRunTrackingAnalysis>(F);
+  // No changes since last InstCombine pass, exit early.
+  if (LRT.shouldSkip(&ID, Options))
+    return PreservedAnalyses::all();
+
   auto &AC = AM.getResult<AssumptionAnalysis>(F);
   auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
   auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
@@ -5555,12 +5563,16 @@ PreservedAnalyses InstCombinePass::run(Function &F,
   auto *BPI = AM.getCachedResult<BranchProbabilityAnalysis>(F);
 
   if (!combineInstructionsOverFunction(F, Worklist, AA, AC, TLI, TTI, DT, ORE,
-                                       BFI, BPI, PSI, Options))
+                                       BFI, BPI, PSI, Options)) {
     // No changes, all analyses are preserved.
+    LRT.update(&ID, /*Changed=*/false, Options);
     return PreservedAnalyses::all();
+  }
 
   // Mark all the analyses that instcombine updates as preserved.
   PreservedAnalyses PA;
+  LRT.update(&ID, /*Changed=*/true, Options);
+  PA.preserve<LastRunTrackingAnalysis>();
   PA.preserveSet<CFGAnalyses>();
   return PA;
 }



More information about the llvm-commits mailing list