[llvm] [Pass] Add `RequiredPassMixin` for non skippable passes (PR #115692)

via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 10 23:29:55 PST 2024


https://github.com/paperchalice created https://github.com/llvm/llvm-project/pull/115692

Follow the idea in #115471, add a new kind of mix-in. This PR demonstrates how it works:
```c++
// A required pass
struct SomePass : PassInfoMixin<SomePass>, RequiredPassMixin {
    PreservedAnalysis run(...);
};
```
Rather than provide `isRequired` for each pass, it seems that checking the sub-class relationship is enough.

>From 3f4b3d8b31d17cd1e83c9a7d2ed6b980ff6a915e Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Mon, 11 Nov 2024 15:19:53 +0800
Subject: [PATCH] [Pass] Add `RequiredPassMixin` for non skippable pass

---
 llvm/include/llvm/IR/PassManager.h         | 11 +++++++++++
 llvm/include/llvm/IR/PassManagerInternal.h |  6 +++++-
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/IR/PassManager.h b/llvm/include/llvm/IR/PassManager.h
index d269221fac0701..dd4bbb7da165ce 100644
--- a/llvm/include/llvm/IR/PassManager.h
+++ b/llvm/include/llvm/IR/PassManager.h
@@ -112,6 +112,17 @@ struct AnalysisInfoMixin : PassInfoMixin<DerivedT> {
   }
 };
 
+/// A mix-in indicates that the pass cannot be skipped
+/// in pass instrumentation.
+struct RequiredPassMixin {};
+
+/// A convenient mix-in to indicate the pass cannot be skipped
+/// in pass instrumentation.
+///
+/// It automatically mixes in \c PassInfoMixin and \c RequiredPassMixin.
+template <typename DerivedT>
+struct RequiredPassInfoMixin : PassInfoMixin<DerivedT>, RequiredPassMixin {};
+
 namespace detail {
 
 /// Actual unpacker of extra arguments in getAnalysisResult,
diff --git a/llvm/include/llvm/IR/PassManagerInternal.h b/llvm/include/llvm/IR/PassManagerInternal.h
index 4ada6ee5dd6831..3fdc584c091afe 100644
--- a/llvm/include/llvm/IR/PassManagerInternal.h
+++ b/llvm/include/llvm/IR/PassManagerInternal.h
@@ -29,6 +29,7 @@ namespace llvm {
 template <typename IRUnitT> class AllAnalysesOn;
 template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager;
 class PreservedAnalyses;
+struct RequiredPassMixin;
 
 // Implementation details of the pass manager interfaces.
 namespace detail {
@@ -112,7 +113,10 @@ struct PassModel : PassConcept<IRUnitT, AnalysisManagerT, ExtraArgTs...> {
     return false;
   }
 
-  bool isRequired() const override { return passIsRequiredImpl<PassT>(); }
+  bool isRequired() const override {
+    return passIsRequiredImpl<PassT>() ||
+           std::is_base_of_v<RequiredPassMixin, PassT>;
+  }
 
   PassT Pass;
 };



More information about the llvm-commits mailing list