Skip to content

[CodeGen] Avoid MachineModuleInfo in MachineModuleSlotTracker #140530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions llvm/include/llvm/CodeGen/MIRPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ void printMIR(raw_ostream &OS, const Module &M);

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

/// Determine a possible list of successors of a basic block based on the
/// basic block machine operand being used inside the block. This should give
Expand Down
8 changes: 5 additions & 3 deletions llvm/include/llvm/CodeGen/MachineModuleSlotTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ class MachineModuleInfo;
class MachineFunction;
class Module;

using MFGetterFnT = std::function<MachineFunction *(const Function &)>;

class MachineModuleSlotTracker : public ModuleSlotTracker {
const Function &TheFunction;
const MachineModuleInfo &TheMMI;
MFGetterFnT MachineFunctionGetterFn;

unsigned MDNStartSlot = 0, MDNEndSlot = 0;

void processMachineFunctionMetadata(AbstractSlotTrackerStorage *AST,
Expand All @@ -33,8 +36,7 @@ class MachineModuleSlotTracker : public ModuleSlotTracker {
bool ShouldInitializeAllMetadata);

public:
MachineModuleSlotTracker(const MachineModuleInfo &MMI,
const MachineFunction *MF,
MachineModuleSlotTracker(MFGetterFnT Fn, const MachineFunction *MF,
bool ShouldInitializeAllMetadata = true);
~MachineModuleSlotTracker();

Expand Down
29 changes: 22 additions & 7 deletions llvm/lib/CodeGen/MIRPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
#include "llvm/CodeGen/MachineConstantPool.h"
#include "llvm/CodeGen/MachineFrameInfo.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/MachineJumpTableInfo.h"
#include "llvm/CodeGen/MachineMemOperand.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/MachineModuleSlotTracker.h"
#include "llvm/CodeGen/MachineOperand.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
Expand Down Expand Up @@ -103,8 +105,8 @@ struct MFPrintState {
/// Synchronization scope names registered with LLVMContext.
SmallVector<StringRef, 8> SSNs;

MFPrintState(const MachineModuleInfo &MMI, const MachineFunction &MF)
: MST(MMI, &MF) {}
MFPrintState(MFGetterFnT Fn, const MachineFunction &MF)
: MST(std::move(Fn), &MF) {}
};

} // end anonymous namespace
Expand Down Expand Up @@ -168,9 +170,10 @@ static void convertCalledGlobals(yaml::MachineFunction &YMF,
const MachineFunction &MF,
MachineModuleSlotTracker &MST);

static void printMF(raw_ostream &OS, const MachineModuleInfo &MMI,
static void printMF(raw_ostream &OS, MFGetterFnT Fn,
const MachineFunction &MF) {
MFPrintState State(MMI, MF);
MFPrintState State(std::move(Fn), MF);

State.RegisterMaskIds = initRegisterMaskIds(MF);

yaml::MachineFunction YamlMF;
Expand Down Expand Up @@ -983,11 +986,23 @@ void llvm::printMIR(raw_ostream &OS, const Module &M) {
Out << const_cast<Module &>(M);
}

void llvm::printMIR(raw_ostream &OS, const MachineModuleInfo &MMI,
const MachineFunction &MF) {
void llvm::printMIR(raw_ostream &OS, MachineModuleInfo *MMI,
FunctionAnalysisManager *FAM, const MachineFunction &MF) {
// RemoveDIs: as there's no textual form for DbgRecords yet, print debug-info
// in dbg.value format.
ScopedDbgInfoFormatSetter FormatSetter(
const_cast<Function &>(MF.getFunction()), UseNewDbgInfoFormat);
printMF(OS, MMI, MF);
if (MMI) {
printMF(
OS, [&](const Function &F) { return MMI->getMachineFunction(F); }, MF);
} else {
printMF(
OS,
[&](const Function &F) {
return &FAM->getResult<MachineFunctionAnalysis>(
const_cast<Function &>(F))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't need const_cast

.getMF();
},
MF);
}
}
14 changes: 6 additions & 8 deletions llvm/lib/CodeGen/MIRPrintingPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ PreservedAnalyses PrintMIRPreparePass::run(Module &M, ModuleAnalysisManager &) {

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

printMIR(OS, MMI, MF);
printMIR(OS, nullptr, &FAM, MF);
return PreservedAnalyses::all();
}

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

const MachineModuleInfo &MMI =
getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
MachineModuleInfo *MMI =
&getAnalysis<MachineModuleInfoWrapperPass>().getMMI();

printMIR(StrOS, MMI, MF);
printMIR(StrOS, MMI, nullptr, MF);
MachineFunctions.append(Str);
return false;
}
Expand Down
9 changes: 4 additions & 5 deletions llvm/lib/CodeGen/MachineModuleSlotTracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void MachineModuleSlotTracker::processMachineModule(
if (&F != &TheFunction)
continue;
MDNStartSlot = AST->getNextMetadataSlot();
if (auto *MF = TheMMI.getMachineFunction(F))
if (auto *MF = MachineFunctionGetterFn(F))
processMachineFunctionMetadata(AST, *MF);
MDNEndSlot = AST->getNextMetadataSlot();
break;
Expand All @@ -52,7 +52,7 @@ void MachineModuleSlotTracker::processMachineFunction(
bool ShouldInitializeAllMetadata) {
if (!ShouldInitializeAllMetadata && F == &TheFunction) {
MDNStartSlot = AST->getNextMetadataSlot();
if (auto *MF = TheMMI.getMachineFunction(*F))
if (auto *MF = MachineFunctionGetterFn(*F))
processMachineFunctionMetadata(AST, *MF);
MDNEndSlot = AST->getNextMetadataSlot();
}
Expand All @@ -64,11 +64,10 @@ void MachineModuleSlotTracker::collectMachineMDNodes(
}

MachineModuleSlotTracker::MachineModuleSlotTracker(
const MachineModuleInfo &MMI, const MachineFunction *MF,
bool ShouldInitializeAllMetadata)
MFGetterFnT Fn, const MachineFunction *MF, bool ShouldInitializeAllMetadata)
: ModuleSlotTracker(MF->getFunction().getParent(),
ShouldInitializeAllMetadata),
TheFunction(MF->getFunction()), TheMMI(MMI) {
TheFunction(MF->getFunction()), MachineFunctionGetterFn(Fn) {
setProcessHook([this](AbstractSlotTrackerStorage *AST, const Module *M,
bool ShouldInitializeAllMetadata) {
this->processMachineModule(AST, M, ShouldInitializeAllMetadata);
Expand Down
Loading