[llvm] 12a8bc0 - [CodeGen] Port FreeMachineFunction to new pass manager (#79421)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 25 01:24:09 PST 2024


Author: paperchalice
Date: 2024-01-25T17:24:05+08:00
New Revision: 12a8bc09ca4fa975d3e79a05e1fb14f03b23e3d5

URL: https://github.com/llvm/llvm-project/commit/12a8bc09ca4fa975d3e79a05e1fb14f03b23e3d5
DIFF: https://github.com/llvm/llvm-project/commit/12a8bc09ca4fa975d3e79a05e1fb14f03b23e3d5.diff

LOG: [CodeGen] Port FreeMachineFunction to new pass manager (#79421)

This pass should be the last machine function pass in pipeline, also
ignore `PI.runAfterPass(*P, MF, PassPA);` to avoid accessing a dangling
reference.

Added: 
    llvm/include/llvm/CodeGen/FreeMachineFunction.h
    llvm/lib/CodeGen/FreeMachineFunction.cpp

Modified: 
    llvm/include/llvm/Passes/CodeGenPassBuilder.h
    llvm/include/llvm/Passes/MachinePassRegistry.def
    llvm/lib/CodeGen/CMakeLists.txt
    llvm/lib/CodeGen/MachinePassManager.cpp
    llvm/lib/Passes/PassBuilder.cpp
    llvm/tools/llc/NewPMDriver.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/CodeGen/FreeMachineFunction.h b/llvm/include/llvm/CodeGen/FreeMachineFunction.h
new file mode 100644
index 000000000000000..77b76c591201a7d
--- /dev/null
+++ b/llvm/include/llvm/CodeGen/FreeMachineFunction.h
@@ -0,0 +1,25 @@
+//===- llvm/CodeGen/FreeMachineFunction.h -----------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CODEGEN_FREEMACHINEFUNCTION_H
+#define LLVM_CODEGEN_FREEMACHINEFUNCTION_H
+
+#include "llvm/CodeGen/MachinePassManager.h"
+
+namespace llvm {
+
+class FreeMachineFunctionPass
+    : public MachinePassInfoMixin<FreeMachineFunctionPass> {
+public:
+  PreservedAnalyses run(MachineFunction &MF,
+                        MachineFunctionAnalysisManager &MFAM);
+};
+
+} // namespace llvm
+
+#endif // LLVM_CODEGEN_FREEMACHINEFUNCTION_H

diff  --git a/llvm/include/llvm/Passes/CodeGenPassBuilder.h b/llvm/include/llvm/Passes/CodeGenPassBuilder.h
index c5c53a5bccae73b..2c8073ad551b741 100644
--- a/llvm/include/llvm/Passes/CodeGenPassBuilder.h
+++ b/llvm/include/llvm/Passes/CodeGenPassBuilder.h
@@ -29,6 +29,7 @@
 #include "llvm/CodeGen/DwarfEHPrepare.h"
 #include "llvm/CodeGen/ExpandMemCmp.h"
 #include "llvm/CodeGen/ExpandReductions.h"
+#include "llvm/CodeGen/FreeMachineFunction.h"
 #include "llvm/CodeGen/GCMetadata.h"
 #include "llvm/CodeGen/GlobalMerge.h"
 #include "llvm/CodeGen/IndirectBrExpand.h"

diff  --git a/llvm/include/llvm/Passes/MachinePassRegistry.def b/llvm/include/llvm/Passes/MachinePassRegistry.def
index 5278c0d1ed7e691..ffea0ffd3045498 100644
--- a/llvm/include/llvm/Passes/MachinePassRegistry.def
+++ b/llvm/include/llvm/Passes/MachinePassRegistry.def
@@ -125,8 +125,8 @@ MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis,
 #ifndef MACHINE_FUNCTION_PASS
 #define MACHINE_FUNCTION_PASS(NAME, PASS_NAME, CONSTRUCTOR)
 #endif
-// MACHINE_FUNCTION_PASS("mir-printer", PrintMIRPass, ())
 // MACHINE_FUNCTION_PASS("free-machine-function", FreeMachineFunctionPass, ())
+// MACHINE_FUNCTION_PASS("mir-printer", PrintMIRPass, ())
 #undef MACHINE_FUNCTION_PASS
 
 // After a pass is converted to new pass manager, its entry should be moved from
@@ -175,8 +175,6 @@ DUMMY_MACHINE_FUNCTION_PASS("fentry-insert", FEntryInserterPass, ())
 DUMMY_MACHINE_FUNCTION_PASS("finalize-isel", FinalizeISelPass, ())
 DUMMY_MACHINE_FUNCTION_PASS("fixup-statepoint-caller-saved",
                             FixupStatepointCallerSavedPass, ())
-DUMMY_MACHINE_FUNCTION_PASS("free-machine-function", FreeMachineFunctionPass,
-                            ())
 DUMMY_MACHINE_FUNCTION_PASS("fs-profile-loader", MIRProfileLoaderNewPass,
                             (File, ProfileFile, P, FS))
 DUMMY_MACHINE_FUNCTION_PASS("funclet-layout", FuncletLayoutPass, ())

diff  --git a/llvm/lib/CodeGen/CMakeLists.txt b/llvm/lib/CodeGen/CMakeLists.txt
index 28c32116b2ba50d..8d1c22d327df660 100644
--- a/llvm/lib/CodeGen/CMakeLists.txt
+++ b/llvm/lib/CodeGen/CMakeLists.txt
@@ -78,6 +78,7 @@ add_llvm_component_library(LLVMCodeGen
   FEntryInserter.cpp
   FinalizeISel.cpp
   FixupStatepointCallerSaved.cpp
+  FreeMachineFunction.cpp
   FuncletLayout.cpp
   GCMetadata.cpp
   GCMetadataPrinter.cpp

diff  --git a/llvm/lib/CodeGen/FreeMachineFunction.cpp b/llvm/lib/CodeGen/FreeMachineFunction.cpp
new file mode 100644
index 000000000000000..f38f3e63a3f37a9
--- /dev/null
+++ b/llvm/lib/CodeGen/FreeMachineFunction.cpp
@@ -0,0 +1,22 @@
+//===- FreeMachineFunction.cpp --------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/CodeGen/FreeMachineFunction.h"
+#include "llvm/CodeGen/MachineFunction.h"
+#include "llvm/CodeGen/MachineModuleInfo.h"
+
+using namespace llvm;
+
+PreservedAnalyses
+FreeMachineFunctionPass::run(MachineFunction &MF,
+                             MachineFunctionAnalysisManager &MFAM) {
+  auto &MMI = MF.getMMI();
+  MFAM.invalidate(MF, PreservedAnalyses::none());
+  MMI.deleteMachineFunctionFor(MF.getFunction()); // MF is dangling now.
+  return PreservedAnalyses::none();
+}

diff  --git a/llvm/lib/CodeGen/MachinePassManager.cpp b/llvm/lib/CodeGen/MachinePassManager.cpp
index 914e6b19fde9ac8..0770eba660b45de 100644
--- a/llvm/lib/CodeGen/MachinePassManager.cpp
+++ b/llvm/lib/CodeGen/MachinePassManager.cpp
@@ -11,6 +11,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/CodeGen/MachinePassManager.h"
+#include "llvm/CodeGen/FreeMachineFunction.h"
 #include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/CodeGen/MachineModuleInfo.h"
 #include "llvm/IR/PassManagerImpl.h"
@@ -94,8 +95,13 @@ Error MachineFunctionPassManager::run(Module &M,
 
         // TODO: EmitSizeRemarks
         PreservedAnalyses PassPA = P->run(MF, MFAM);
-        MFAM.invalidate(MF, PassPA);
-        PI.runAfterPass(*P, MF, PassPA);
+
+        // MF is dangling after FreeMachineFunctionPass
+        if (P->name() != FreeMachineFunctionPass::name()) {
+          MFAM.invalidate(MF, PassPA);
+
+          PI.runAfterPass(*P, MF, PassPA);
+        }
       }
     }
   } while (true);

diff  --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 2fe167bd439d487..c934ec42f6eb156 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -80,6 +80,7 @@
 #include "llvm/CodeGen/ExpandLargeDivRem.h"
 #include "llvm/CodeGen/ExpandLargeFpConvert.h"
 #include "llvm/CodeGen/ExpandMemCmp.h"
+#include "llvm/CodeGen/FreeMachineFunction.h"
 #include "llvm/CodeGen/GCMetadata.h"
 #include "llvm/CodeGen/GlobalMerge.h"
 #include "llvm/CodeGen/HardwareLoops.h"

diff  --git a/llvm/tools/llc/NewPMDriver.cpp b/llvm/tools/llc/NewPMDriver.cpp
index 2f979f3081f795f..8b1c263391b9ee5 100644
--- a/llvm/tools/llc/NewPMDriver.cpp
+++ b/llvm/tools/llc/NewPMDriver.cpp
@@ -16,6 +16,7 @@
 #include "llvm/Analysis/CGSCCPassManager.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
 #include "llvm/CodeGen/CommandFlags.h"
+#include "llvm/CodeGen/FreeMachineFunction.h"
 #include "llvm/CodeGen/MIRParser/MIRParser.h"
 #include "llvm/CodeGen/MIRPrinter.h"
 #include "llvm/CodeGen/MachineModuleInfo.h"


        


More information about the llvm-commits mailing list