Skip to content

[Pass] Add RequiredPassMixin for non skippable passes #115692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

paperchalice
Copy link
Contributor

Follow the idea in #115471, add a new kind of mix-in. This PR demonstrates how it works:

// 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.

@llvmbot
Copy link
Member

llvmbot commented Nov 11, 2024

@llvm/pr-subscribers-llvm-ir

Author: None (paperchalice)

Changes

Follow the idea in #115471, add a new kind of mix-in. This PR demonstrates how it works:

// A required pass
struct SomePass : PassInfoMixin&lt;SomePass&gt;, RequiredPassMixin {
    PreservedAnalysis run(...);
};

Rather than provide isRequired for each pass, it seems that checking the sub-class relationship is enough.


Full diff: https://github.com/llvm/llvm-project/pull/115692.diff

2 Files Affected:

  • (modified) llvm/include/llvm/IR/PassManager.h (+11)
  • (modified) llvm/include/llvm/IR/PassManagerInternal.h (+5-1)
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;
 };

Copy link
Contributor

@aeubanks aeubanks left a comment

Choose a reason for hiding this comment

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

I actually meant that we'd have

struct RequiredPassInfoMixin : PassInfoMixin {
static constexpr bool isRequired = true;
};
struct OptionalPassInfoMixin: PassInfoMixin {
static constexpr bool isRequired = false;
};

and we'd get rid of the default isRequired = false here. passes must inherit from one of them or get a compilation error. this forces people to make the decision on whether or not the pass is a required or optional one (maybe we should rename to unconditional/conditional, or mandatory, or something else), as opposed to getting a default "optional" pass which is often times incorrect

if we do this, we should probably have an RFC since it affects a lot of people. there are also things we can do to make the transition from static bool isRequired() to this new method easier for people

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants