Skip to content

Commit 69f45fc

Browse files
committed
Fixes to be upstreamed - 12
see "[CodeGen] Avoid MachineModuleInfo in MachineModuleSlotTracker (PR 140530)"
1 parent f51f53e commit 69f45fc

File tree

7 files changed

+45
-32
lines changed

7 files changed

+45
-32
lines changed

llvm/include/llvm/CodeGen/MIRPrinter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ LLVM_ABI void printMIR(raw_ostream &OS, const Module &M);
5050

5151
/// Print a machine function using the MIR serialization format to the given
5252
/// output stream.
53-
LLVM_ABI void printMIR(raw_ostream &OS, const MachineModuleInfo &MMI,
54-
const MachineFunction &MF);
53+
LLVM_ABI void printMIR(raw_ostream &OS, MachineModuleInfo *MMI,
54+
FunctionAnalysisManager *MFA, const MachineFunction &MF);
5555

5656
/// Determine a possible list of successors of a basic block based on the
5757
/// basic block machine operand being used inside the block. This should give

llvm/include/llvm/CodeGen/MachineModuleSlotTracker.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ class MachineModuleInfo;
2020
class MachineFunction;
2121
class Module;
2222

23+
using MFGetterFnT = std::function<MachineFunction *(const Function &)>;
24+
2325
class LLVM_ABI MachineModuleSlotTracker : public ModuleSlotTracker {
2426
const Function &TheFunction;
25-
const MachineModuleInfo &TheMMI;
27+
MFGetterFnT MachineFunctionGetterFn;
2628
unsigned MDNStartSlot = 0, MDNEndSlot = 0;
2729

2830
void processMachineFunctionMetadata(AbstractSlotTrackerStorage *AST,
@@ -34,8 +36,7 @@ class LLVM_ABI MachineModuleSlotTracker : public ModuleSlotTracker {
3436
bool ShouldInitializeAllMetadata);
3537

3638
public:
37-
MachineModuleSlotTracker(const MachineModuleInfo &MMI,
38-
const MachineFunction *MF,
39+
MachineModuleSlotTracker(MFGetterFnT Fn, const MachineFunction *MF,
3940
bool ShouldInitializeAllMetadata = true);
4041
~MachineModuleSlotTracker();
4142

llvm/lib/CodeGen/MIRPrinter.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
#include "llvm/CodeGen/MachineConstantPool.h"
2525
#include "llvm/CodeGen/MachineFrameInfo.h"
2626
#include "llvm/CodeGen/MachineFunction.h"
27+
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
2728
#include "llvm/CodeGen/MachineInstr.h"
2829
#include "llvm/CodeGen/MachineJumpTableInfo.h"
2930
#include "llvm/CodeGen/MachineMemOperand.h"
31+
#include "llvm/CodeGen/MachineModuleInfo.h"
3032
#include "llvm/CodeGen/MachineModuleSlotTracker.h"
3133
#include "llvm/CodeGen/MachineOperand.h"
3234
#include "llvm/CodeGen/MachineRegisterInfo.h"
@@ -101,8 +103,8 @@ struct MFPrintState {
101103
/// Synchronization scope names registered with LLVMContext.
102104
SmallVector<StringRef, 8> SSNs;
103105

104-
MFPrintState(const MachineModuleInfo &MMI, const MachineFunction &MF)
105-
: MST(MMI, &MF) {}
106+
MFPrintState(MFGetterFnT Fn, const MachineFunction &MF)
107+
: MST(std::move(Fn), &MF) {}
106108
};
107109

108110
} // end anonymous namespace
@@ -170,9 +172,10 @@ static void convertCalledGlobals(yaml::MachineFunction &YMF,
170172
const MachineFunction &MF,
171173
MachineModuleSlotTracker &MST);
172174

173-
static void printMF(raw_ostream &OS, const MachineModuleInfo &MMI,
175+
static void printMF(raw_ostream &OS, MFGetterFnT Fn,
174176
const MachineFunction &MF) {
175-
MFPrintState State(MMI, MF);
177+
MFPrintState State(std::move(Fn), MF);
178+
176179
State.RegisterMaskIds = initRegisterMaskIds(MF);
177180

178181
yaml::MachineFunction YamlMF;
@@ -995,7 +998,19 @@ void llvm::printMIR(raw_ostream &OS, const Module &M) {
995998
Out << const_cast<Module &>(M);
996999
}
9971000

998-
void llvm::printMIR(raw_ostream &OS, const MachineModuleInfo &MMI,
999-
const MachineFunction &MF) {
1000-
printMF(OS, MMI, MF);
1001+
void llvm::printMIR(raw_ostream &OS, MachineModuleInfo *MMI,
1002+
FunctionAnalysisManager *FAM, const MachineFunction &MF) {
1003+
if (MMI) {
1004+
printMF(
1005+
OS, [&](const Function &F) { return MMI->getMachineFunction(F); }, MF);
1006+
} else {
1007+
printMF(
1008+
OS,
1009+
[&](const Function &F) {
1010+
return &FAM->getResult<MachineFunctionAnalysis>(
1011+
const_cast<Function &>(F))
1012+
.getMF();
1013+
},
1014+
MF);
1015+
}
10011016
}

llvm/lib/CodeGen/MIRPrintingPass.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,10 @@ PreservedAnalyses PrintMIRPreparePass::run(Module &M, ModuleAnalysisManager &) {
2727

2828
PreservedAnalyses PrintMIRPass::run(MachineFunction &MF,
2929
MachineFunctionAnalysisManager &MFAM) {
30-
auto &MAMP = MFAM.getResult<ModuleAnalysisManagerMachineFunctionProxy>(MF);
31-
Module *M = MF.getFunction().getParent();
32-
const MachineModuleInfo &MMI =
33-
MAMP.getCachedResult<MachineModuleAnalysis>(*M)->getMMI();
30+
auto &FAM = MFAM.getResult<FunctionAnalysisManagerMachineFunctionProxy>(MF)
31+
.getManager();
3432

35-
printMIR(OS, MMI, MF);
33+
printMIR(OS, nullptr, &FAM, MF);
3634
return PreservedAnalyses::all();
3735
}
3836

@@ -59,10 +57,10 @@ struct MIRPrintingPass : public MachineFunctionPass {
5957
std::string Str;
6058
raw_string_ostream StrOS(Str);
6159

62-
const MachineModuleInfo &MMI =
63-
getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
60+
MachineModuleInfo *MMI =
61+
&getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
6462

65-
printMIR(StrOS, MMI, MF);
63+
printMIR(StrOS, MMI, nullptr, MF);
6664
MachineFunctions.append(Str);
6765
return false;
6866
}

llvm/lib/CodeGen/MachineModuleSlotTracker.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void MachineModuleSlotTracker::processMachineModule(
3939
if (&F != &TheFunction)
4040
continue;
4141
MDNStartSlot = AST->getNextMetadataSlot();
42-
if (auto *MF = TheMMI.getMachineFunction(F))
42+
if (auto *MF = MachineFunctionGetterFn(F))
4343
processMachineFunctionMetadata(AST, *MF);
4444
MDNEndSlot = AST->getNextMetadataSlot();
4545
break;
@@ -52,7 +52,7 @@ void MachineModuleSlotTracker::processMachineFunction(
5252
bool ShouldInitializeAllMetadata) {
5353
if (!ShouldInitializeAllMetadata && F == &TheFunction) {
5454
MDNStartSlot = AST->getNextMetadataSlot();
55-
if (auto *MF = TheMMI.getMachineFunction(*F))
55+
if (auto *MF = MachineFunctionGetterFn(*F))
5656
processMachineFunctionMetadata(AST, *MF);
5757
MDNEndSlot = AST->getNextMetadataSlot();
5858
}
@@ -64,11 +64,10 @@ void MachineModuleSlotTracker::collectMachineMDNodes(
6464
}
6565

6666
MachineModuleSlotTracker::MachineModuleSlotTracker(
67-
const MachineModuleInfo &MMI, const MachineFunction *MF,
68-
bool ShouldInitializeAllMetadata)
67+
MFGetterFnT Fn, const MachineFunction *MF, bool ShouldInitializeAllMetadata)
6968
: ModuleSlotTracker(MF->getFunction().getParent(),
7069
ShouldInitializeAllMetadata),
71-
TheFunction(MF->getFunction()), TheMMI(MMI) {
70+
TheFunction(MF->getFunction()), MachineFunctionGetterFn(Fn) {
7271
setProcessHook([this](AbstractSlotTrackerStorage *AST, const Module *M,
7372
bool ShouldInitializeAllMetadata) {
7473
this->processMachineModule(AST, M, ShouldInitializeAllMetadata);

llvm/tools/llvm-reduce/ReducerWorkItem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ void ReducerWorkItem::print(raw_ostream &ROS, void *p) const {
450450
printMIR(ROS, *M);
451451
for (Function &F : *M) {
452452
if (auto *MF = MMI->getMachineFunction(F))
453-
printMIR(ROS, *MMI, *MF);
453+
printMIR(ROS, MMI.get(), nullptr, *MF);
454454
}
455455
} else {
456456
M->print(ROS, /*AssemblyAnnotationWriter=*/nullptr,

llvm/unittests/MIR/MachineMetadata.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ body: |
250250
auto *NewMMO = MF->getMachineMemOperand(OldMMO, AAInfo);
251251
MI.setMemRefs(*MF, NewMMO);
252252

253-
MachineModuleSlotTracker MST(MMI, MF);
253+
MachineModuleSlotTracker MST([&](const Function& F) { return MMI.getMachineFunction(F); }, MF);
254254
// Print that MI with new machine metadata, which slot numbers should be
255255
// assigned.
256256
EXPECT_EQ("%1:gpr32 = LDRWui %0, 0 :: (load (s32) from %ir.p, "
@@ -274,7 +274,7 @@ body: |
274274
EXPECT_EQ(Collected, Generated);
275275

276276
// FileCheck the output from MIR printer.
277-
std::string Output = print([&](raw_ostream &OS) { printMIR(OS, MMI, *MF); });
277+
std::string Output = print([&](raw_ostream &OS) { printMIR(OS, &MMI, nullptr, *MF); });
278278
std::string CheckString = R"(
279279
CHECK: machineMetadataNodes:
280280
CHECK-DAG: ![[MMDOMAIN:[0-9]+]] = distinct !{!{{[0-9]+}}, !"domain"}
@@ -400,7 +400,7 @@ body: |
400400
auto *NewMMO = MF->getMachineMemOperand(OldMMO, AAInfo);
401401
MI.setMemRefs(*MF, NewMMO);
402402

403-
MachineModuleSlotTracker MST(MMI, MF);
403+
MachineModuleSlotTracker MST([&](const Function& F) { return MMI.getMachineFunction(F); }, MF);
404404
// Print that MI with new machine metadata, which slot numbers should be
405405
// assigned.
406406
EXPECT_EQ("%1:gr32 = MOV32rm %0, 1, $noreg, 0, $noreg :: (load (s32) from %ir.p, "
@@ -424,7 +424,7 @@ body: |
424424
EXPECT_EQ(Collected, Generated);
425425

426426
// FileCheck the output from MIR printer.
427-
std::string Output = print([&](raw_ostream &OS) { printMIR(OS, MMI, *MF); });
427+
std::string Output = print([&](raw_ostream &OS) { printMIR(OS, &MMI, nullptr, *MF); });
428428
std::string CheckString = R"(
429429
CHECK: machineMetadataNodes:
430430
CHECK-DAG: ![[MMDOMAIN:[0-9]+]] = distinct !{!{{[0-9]+}}, !"domain"}
@@ -498,7 +498,7 @@ body: |
498498
auto *NewMMO = MF->getMachineMemOperand(OldMMO, AAInfo);
499499
MI.setMemRefs(*MF, NewMMO);
500500

501-
MachineModuleSlotTracker MST(MMI, MF);
501+
MachineModuleSlotTracker MST([&](const Function& F) { return MMI.getMachineFunction(F); }, MF);
502502
// Print that MI with new machine metadata, which slot numbers should be
503503
// assigned.
504504
EXPECT_EQ(
@@ -523,7 +523,7 @@ body: |
523523
EXPECT_EQ(Collected, Generated);
524524

525525
// FileCheck the output from MIR printer.
526-
std::string Output = print([&](raw_ostream &OS) { printMIR(OS, MMI, *MF); });
526+
std::string Output = print([&](raw_ostream &OS) { printMIR(OS, &MMI, nullptr, *MF); });
527527
std::string CheckString = R"(
528528
CHECK: machineMetadataNodes:
529529
CHECK-DAG: ![[MMDOMAIN:[0-9]+]] = distinct !{!{{[0-9]+}}, !"domain"}

0 commit comments

Comments
 (0)