[llvm] [Passes] Generalize ShouldRunExtraVectorPasses to allow re-use (NFCI). (PR #118323)

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 9 12:00:34 PST 2024


================
@@ -0,0 +1,85 @@
+//===- ExtraFunctionPassManager.h - Run Optimizations on Demand -*- 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
+//
+//===----------------------------------------------------------------------===//
+/// \file
+///
+/// This file provides a pass manager that only runs its passes if the
+/// provided marker analysis has been preserved, together with a class to
+/// define such a marker analysis.
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_TRANSFORMS_UTILS_EXTRAPASSMANAGER_H
+#define LLVM_TRANSFORMS_UTILS_EXTRAPASSMANAGER_H
+
+#include "llvm/IR/PassManager.h"
+#include "llvm/Transforms/Scalar/LoopPassManager.h"
+
+namespace llvm {
+
+/// A marker analysis to determine if extra passes should be run on demand.
+/// Passes requesting extra transformations to run need to request and preserve
+/// this analysis.
+template <typename MarkerTy> struct ShouldRunExtraPasses {
+  struct Result {
+    bool invalidate(Function &F, const PreservedAnalyses &PA,
+                    FunctionAnalysisManager::Invalidator &) {
+      // Check whether the analysis has been explicitly invalidated. Otherwise,
+      // it remains preserved.
+      auto PAC = PA.getChecker<MarkerTy>();
+      return !PAC.preservedWhenStateless();
+    }
+
+    bool invalidate(Loop &L, const PreservedAnalyses &PA,
+                    LoopAnalysisManager::Invalidator &) {
+      // Check whether the analysis has been explicitly invalidated. Otherwise,
+      // it remains preserved.
+      auto PAC = PA.getChecker<MarkerTy>();
+      return !PAC.preservedWhenStateless();
+    }
+  };
+
+  Result run(Function &F, FunctionAnalysisManager &FAM) { return Result(); }
+
+  Result run(Loop &L, LoopAnalysisManager &AM,
+             LoopStandardAnalysisResults &AR) {
+    return Result();
+  }
+};
+
+/// A pass manager to run a set of extra function passes if the
+/// ShouldRunExtraPasses marker analysis is present. This allows passes to
+/// request additional transformations on demand. An example is extra
+/// simplifications after loop-vectorization, if runtime checks have been added.
+template <typename MarkerTy>
+struct ExtraFunctionPassManager : public FunctionPassManager {
----------------
aeubanks wrote:

the new pass manager data structures very much do not endorse overriding methods via inheritance. I'd prefer `ExtraFunctionPassManager` to have a `FunctionPassManager` member instead

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


More information about the llvm-commits mailing list