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

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 18 15:56:37 PST 2024


================
@@ -0,0 +1,51 @@
+//===- 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/ADT/Statistic.h"
+#include "llvm/Support/CommandLine.h"
+
+using namespace llvm;
+
+#define DEBUG_TYPE "last-run-tracking"
+STATISTIC(NumSkippedPasses, "Number of skipped passes");
+STATISTIC(NumLRTQueries, "Number of LastRunTracking queries");
+
+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;
+  ++NumLRTQueries;
+  auto Iter = TrackedPasses.find(ID);
+  if (Iter == TrackedPasses.end())
+    return false;
+  if (!Iter->second || Iter->second(Ptr)) {
----------------
dtcxzyw wrote:

It is expected behavior that we avoid run the same pass regardless of whether the last run of this pass modifies IR.
See also the test https://github.com/llvm/llvm-project/blob/3a3517c5e9d45a1d1aae5320887478b228b0f8be/llvm/unittests/Analysis/LastRunTrackingAnalysisTest.cpp#L96-L97.

For InstCombine, we expect that it reaches a fixpoint in the first run. Thus it is safe to avoid the second run. See also the discussion in https://discourse.llvm.org/t/rfc-pipeline-avoid-running-transform-passes-that-have-just-been-run/82467/8. If not, InstCombine should report something like `Instruction Combining on XXX did not reach a fixpoint...`.



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


More information about the llvm-commits mailing list