Skip to content

Commit 12a8bc0

Browse files
authored
[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.
1 parent 50cc07f commit 12a8bc0

File tree

8 files changed

+60
-5
lines changed

8 files changed

+60
-5
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===- llvm/CodeGen/FreeMachineFunction.h -----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CODEGEN_FREEMACHINEFUNCTION_H
10+
#define LLVM_CODEGEN_FREEMACHINEFUNCTION_H
11+
12+
#include "llvm/CodeGen/MachinePassManager.h"
13+
14+
namespace llvm {
15+
16+
class FreeMachineFunctionPass
17+
: public MachinePassInfoMixin<FreeMachineFunctionPass> {
18+
public:
19+
PreservedAnalyses run(MachineFunction &MF,
20+
MachineFunctionAnalysisManager &MFAM);
21+
};
22+
23+
} // namespace llvm
24+
25+
#endif // LLVM_CODEGEN_FREEMACHINEFUNCTION_H

llvm/include/llvm/Passes/CodeGenPassBuilder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "llvm/CodeGen/DwarfEHPrepare.h"
3030
#include "llvm/CodeGen/ExpandMemCmp.h"
3131
#include "llvm/CodeGen/ExpandReductions.h"
32+
#include "llvm/CodeGen/FreeMachineFunction.h"
3233
#include "llvm/CodeGen/GCMetadata.h"
3334
#include "llvm/CodeGen/GlobalMerge.h"
3435
#include "llvm/CodeGen/IndirectBrExpand.h"

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis,
125125
#ifndef MACHINE_FUNCTION_PASS
126126
#define MACHINE_FUNCTION_PASS(NAME, PASS_NAME, CONSTRUCTOR)
127127
#endif
128-
// MACHINE_FUNCTION_PASS("mir-printer", PrintMIRPass, ())
129128
// MACHINE_FUNCTION_PASS("free-machine-function", FreeMachineFunctionPass, ())
129+
// MACHINE_FUNCTION_PASS("mir-printer", PrintMIRPass, ())
130130
#undef MACHINE_FUNCTION_PASS
131131

132132
// 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, ())
175175
DUMMY_MACHINE_FUNCTION_PASS("finalize-isel", FinalizeISelPass, ())
176176
DUMMY_MACHINE_FUNCTION_PASS("fixup-statepoint-caller-saved",
177177
FixupStatepointCallerSavedPass, ())
178-
DUMMY_MACHINE_FUNCTION_PASS("free-machine-function", FreeMachineFunctionPass,
179-
())
180178
DUMMY_MACHINE_FUNCTION_PASS("fs-profile-loader", MIRProfileLoaderNewPass,
181179
(File, ProfileFile, P, FS))
182180
DUMMY_MACHINE_FUNCTION_PASS("funclet-layout", FuncletLayoutPass, ())

llvm/lib/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ add_llvm_component_library(LLVMCodeGen
7878
FEntryInserter.cpp
7979
FinalizeISel.cpp
8080
FixupStatepointCallerSaved.cpp
81+
FreeMachineFunction.cpp
8182
FuncletLayout.cpp
8283
GCMetadata.cpp
8384
GCMetadataPrinter.cpp
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===- FreeMachineFunction.cpp --------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/CodeGen/FreeMachineFunction.h"
10+
#include "llvm/CodeGen/MachineFunction.h"
11+
#include "llvm/CodeGen/MachineModuleInfo.h"
12+
13+
using namespace llvm;
14+
15+
PreservedAnalyses
16+
FreeMachineFunctionPass::run(MachineFunction &MF,
17+
MachineFunctionAnalysisManager &MFAM) {
18+
auto &MMI = MF.getMMI();
19+
MFAM.invalidate(MF, PreservedAnalyses::none());
20+
MMI.deleteMachineFunctionFor(MF.getFunction()); // MF is dangling now.
21+
return PreservedAnalyses::none();
22+
}

llvm/lib/CodeGen/MachinePassManager.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "llvm/CodeGen/MachinePassManager.h"
14+
#include "llvm/CodeGen/FreeMachineFunction.h"
1415
#include "llvm/CodeGen/MachineFunction.h"
1516
#include "llvm/CodeGen/MachineModuleInfo.h"
1617
#include "llvm/IR/PassManagerImpl.h"
@@ -94,8 +95,13 @@ Error MachineFunctionPassManager::run(Module &M,
9495

9596
// TODO: EmitSizeRemarks
9697
PreservedAnalyses PassPA = P->run(MF, MFAM);
97-
MFAM.invalidate(MF, PassPA);
98-
PI.runAfterPass(*P, MF, PassPA);
98+
99+
// MF is dangling after FreeMachineFunctionPass
100+
if (P->name() != FreeMachineFunctionPass::name()) {
101+
MFAM.invalidate(MF, PassPA);
102+
103+
PI.runAfterPass(*P, MF, PassPA);
104+
}
99105
}
100106
}
101107
} while (true);

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
#include "llvm/CodeGen/ExpandLargeDivRem.h"
8181
#include "llvm/CodeGen/ExpandLargeFpConvert.h"
8282
#include "llvm/CodeGen/ExpandMemCmp.h"
83+
#include "llvm/CodeGen/FreeMachineFunction.h"
8384
#include "llvm/CodeGen/GCMetadata.h"
8485
#include "llvm/CodeGen/GlobalMerge.h"
8586
#include "llvm/CodeGen/HardwareLoops.h"

llvm/tools/llc/NewPMDriver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "llvm/Analysis/CGSCCPassManager.h"
1717
#include "llvm/Analysis/TargetLibraryInfo.h"
1818
#include "llvm/CodeGen/CommandFlags.h"
19+
#include "llvm/CodeGen/FreeMachineFunction.h"
1920
#include "llvm/CodeGen/MIRParser/MIRParser.h"
2021
#include "llvm/CodeGen/MIRPrinter.h"
2122
#include "llvm/CodeGen/MachineModuleInfo.h"

0 commit comments

Comments
 (0)