[llvm] [PassManager] Support MachineFunctionProperties (PR #83668)

via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 6 20:37:44 PST 2024


https://github.com/paperchalice updated https://github.com/llvm/llvm-project/pull/83668

>From 22aa716e952a0a47ad30d453f744dfc61f61c9f9 Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Thu, 7 Mar 2024 09:07:03 +0800
Subject: [PATCH] [PassManager] Support MachineFunctionProperties

---
 llvm/include/llvm/IR/PassManager.h         | 13 +++++++++----
 llvm/include/llvm/IR/PassManagerInternal.h |  3 +++
 llvm/lib/CodeGen/MachinePassManager.cpp    | 16 ++++++++++++++++
 3 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/llvm/include/llvm/IR/PassManager.h b/llvm/include/llvm/IR/PassManager.h
index c03d49c3b7b978..ad7a42223549e5 100644
--- a/llvm/include/llvm/IR/PassManager.h
+++ b/llvm/include/llvm/IR/PassManager.h
@@ -64,6 +64,8 @@ extern llvm::cl::opt<bool> UseNewDbgInfoFormat;
 
 namespace llvm {
 
+class MachineFunction;
+
 // RemoveDIs: Provide facilities for converting debug-info from one form to
 // another, which are no-ops for everything but modules.
 template <class IRUnitT> inline bool shouldConvertDbgInfo(IRUnitT &IR) {
@@ -271,8 +273,10 @@ class PassManager : public PassInfoMixin<
   LLVM_ATTRIBUTE_MINSIZE
       std::enable_if_t<!std::is_same<PassT, PassManager>::value>
       addPass(PassT &&Pass) {
-    using PassModelT =
-        detail::PassModel<IRUnitT, PassT, AnalysisManagerT, ExtraArgTs...>;
+    using PassModelT = std::conditional_t<
+        std::is_same_v<IRUnitT, MachineFunction>,
+        detail::MachinePassModel<PassT>,
+        detail::PassModel<IRUnitT, PassT, AnalysisManagerT, ExtraArgTs...>>;
     // Do not use make_unique or emplace_back, they cause too many template
     // instantiations, causing terrible compile times.
     Passes.push_back(std::unique_ptr<PassConceptT>(
@@ -298,8 +302,9 @@ class PassManager : public PassInfoMixin<
   static bool isRequired() { return true; }
 
 protected:
-  using PassConceptT =
-      detail::PassConcept<IRUnitT, AnalysisManagerT, ExtraArgTs...>;
+  using PassConceptT = std::conditional_t<
+      std::is_same_v<IRUnitT, MachineFunction>, detail::MachinePassConcept,
+      detail::PassConcept<IRUnitT, AnalysisManagerT, ExtraArgTs...>>;
 
   std::vector<std::unique_ptr<PassConceptT>> Passes;
 };
diff --git a/llvm/include/llvm/IR/PassManagerInternal.h b/llvm/include/llvm/IR/PassManagerInternal.h
index 4ada6ee5dd6831..06b941fe947fdb 100644
--- a/llvm/include/llvm/IR/PassManagerInternal.h
+++ b/llvm/include/llvm/IR/PassManagerInternal.h
@@ -328,6 +328,9 @@ struct AnalysisPassModel
   PassT Pass;
 };
 
+struct MachinePassConcept;
+template <typename PassT> struct MachinePassModel;
+
 } // end namespace detail
 
 } // end namespace llvm
diff --git a/llvm/lib/CodeGen/MachinePassManager.cpp b/llvm/lib/CodeGen/MachinePassManager.cpp
index 9a750b5bed4339..83c90ada93cde7 100644
--- a/llvm/lib/CodeGen/MachinePassManager.cpp
+++ b/llvm/lib/CodeGen/MachinePassManager.cpp
@@ -121,11 +121,27 @@ PassManager<MachineFunction>::run(MachineFunction &MF,
   for (auto &Pass : Passes) {
     if (!PI.runBeforePass<MachineFunction>(*Pass, MF))
       continue;
+    auto &MFProps = MF.getProperties();
+#ifndef NDEBUG
+    auto RequiredProperties = Pass->getRequiredProperties();
+    if (!MFProps.verifyRequiredProperties(RequiredProperties)) {
+      errs() << "MachineFunctionProperties required by " << Pass->name()
+             << " pass are not met by function " << F.getName() << ".\n"
+             << "Required properties: ";
+      RequiredProperties.print(errs());
+      errs() << "\nCurrent properties: ";
+      MFProps.print(errs());
+      errs() << '\n';
+      report_fatal_error("MachineFunctionProperties check failed");
+    }
+#endif
 
     PreservedAnalyses PassPA = Pass->run(MF, MFAM);
     if (MMI.getMachineFunction(F)) {
       MFAM.invalidate(MF, PassPA);
       PI.runAfterPass(*Pass, MF, PassPA);
+      MFProps.set(Pass->getSetProperties());
+      MFProps.reset(Pass->getClearedProperties());
     } else {
       MFAM.clear(MF, F.getName());
       PI.runAfterPassInvalidated<MachineFunction>(*Pass, PassPA);



More information about the llvm-commits mailing list