[llvm] [CodeGen] Port FreeMachineFunction to new pass manager (PR #79421)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Jan 25 00:52:11 PST 2024
https://github.com/paperchalice updated https://github.com/llvm/llvm-project/pull/79421
>From 583f7fa158d5500453a18d3b24d3896c493a353c Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Sun, 21 Jan 2024 18:21:50 +0800
Subject: [PATCH 1/2] [CodeGen] Port FreeMachineFunction to new pass manager
---
.../llvm/CodeGen/FreeMachineFunction.h | 25 +++++++++++++++++++
llvm/include/llvm/Passes/CodeGenPassBuilder.h | 1 +
.../llvm/Passes/MachinePassRegistry.def | 4 +--
llvm/lib/CodeGen/CMakeLists.txt | 1 +
llvm/lib/CodeGen/FreeMachineFunction.cpp | 22 ++++++++++++++++
llvm/lib/CodeGen/MachinePassManager.cpp | 10 ++++++--
llvm/lib/Passes/PassBuilder.cpp | 1 +
llvm/tools/llc/NewPMDriver.cpp | 1 +
8 files changed, 60 insertions(+), 5 deletions(-)
create mode 100644 llvm/include/llvm/CodeGen/FreeMachineFunction.h
create mode 100644 llvm/lib/CodeGen/FreeMachineFunction.cpp
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..edc2e64432e4914 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("free-machine-function", FreeMachineFunctionPass, ())
// MACHINE_FUNCTION_PASS("mir-printer", PrintMIRPass, ())
-// MACHINE_FUNCTION_PASS("free-machine-function", FreeMachineFunctionPass, ())
#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"
>From 6c9c0ed480706013a9d2ce7b6f03daa9f4a5b483 Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Thu, 25 Jan 2024 16:50:31 +0800
Subject: [PATCH 2/2] Don't expose FreeMachineFunctionPass
---
llvm/include/llvm/Passes/MachinePassRegistry.def | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/include/llvm/Passes/MachinePassRegistry.def b/llvm/include/llvm/Passes/MachinePassRegistry.def
index edc2e64432e4914..ffea0ffd3045498 100644
--- a/llvm/include/llvm/Passes/MachinePassRegistry.def
+++ b/llvm/include/llvm/Passes/MachinePassRegistry.def
@@ -125,7 +125,7 @@ MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis,
#ifndef MACHINE_FUNCTION_PASS
#define MACHINE_FUNCTION_PASS(NAME, PASS_NAME, CONSTRUCTOR)
#endif
-MACHINE_FUNCTION_PASS("free-machine-function", FreeMachineFunctionPass, ())
+// MACHINE_FUNCTION_PASS("free-machine-function", FreeMachineFunctionPass, ())
// MACHINE_FUNCTION_PASS("mir-printer", PrintMIRPass, ())
#undef MACHINE_FUNCTION_PASS
More information about the llvm-commits
mailing list