[llvm] cfd8498 - [MIR] Fix cyclic dependency of MIR formatter
Benjamin Kramer via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 10 02:24:14 PST 2020
Author: Peng Guo
Date: 2020-01-10T11:18:12+01:00
New Revision: cfd849840134c4632c2f4fa498dfb93c47825b24
URL: https://github.com/llvm/llvm-project/commit/cfd849840134c4632c2f4fa498dfb93c47825b24
DIFF: https://github.com/llvm/llvm-project/commit/cfd849840134c4632c2f4fa498dfb93c47825b24.diff
LOG: [MIR] Fix cyclic dependency of MIR formatter
Summary:
Move MIR formatter pointer from TargetMachine to TargetInstrInfo to
avoid cyclic dependency between target & codegen.
Reviewers: dsanders, bkramer, arsenm
Subscribers: wdng, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72485
Added:
Modified:
llvm/include/llvm/CodeGen/MachineMemOperand.h
llvm/include/llvm/CodeGen/TargetInstrInfo.h
llvm/include/llvm/Target/TargetMachine.h
llvm/lib/CodeGen/MIRParser/MIParser.cpp
llvm/lib/CodeGen/MIRPrinter.cpp
llvm/lib/CodeGen/MachineInstr.cpp
llvm/lib/CodeGen/MachineOperand.cpp
llvm/lib/CodeGen/SelectionDAG/SelectionDAGDumper.cpp
llvm/lib/Target/TargetMachine.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/CodeGen/MachineMemOperand.h b/llvm/include/llvm/CodeGen/MachineMemOperand.h
index b0243646b06c..7ee700c62a25 100644
--- a/llvm/include/llvm/CodeGen/MachineMemOperand.h
+++ b/llvm/include/llvm/CodeGen/MachineMemOperand.h
@@ -26,7 +26,6 @@ namespace llvm {
class FoldingSetNodeID;
class MDNode;
-class MIRFormatter;
class raw_ostream;
class MachineFunction;
class ModuleSlotTracker;
@@ -296,8 +295,7 @@ class MachineMemOperand {
/// @{
void print(raw_ostream &OS, ModuleSlotTracker &MST,
SmallVectorImpl<StringRef> &SSNs, const LLVMContext &Context,
- const MachineFrameInfo *MFI, const TargetInstrInfo *TII,
- const MIRFormatter *MIRF) const;
+ const MachineFrameInfo *MFI, const TargetInstrInfo *TII) const;
/// @}
friend bool operator==(const MachineMemOperand &LHS,
diff --git a/llvm/include/llvm/CodeGen/TargetInstrInfo.h b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
index e410d1c4806d..5ca6ec0a926a 100644
--- a/llvm/include/llvm/CodeGen/TargetInstrInfo.h
+++ b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
@@ -1807,6 +1807,14 @@ class TargetInstrInfo : public MCInstrInfo {
virtual Optional<ParamLoadedValue> describeLoadedValue(const MachineInstr &MI,
Register Reg) const;
+ /// Return MIR formatter to format/parse MIR operands. Target can override
+ /// this virtual function and return target specific MIR formatter.
+ virtual const MIRFormatter *getMIRFormatter() const {
+ if (!Formatter.get())
+ Formatter = std::make_unique<MIRFormatter>();
+ return Formatter.get();
+ }
+
private:
mutable std::unique_ptr<MIRFormatter> Formatter;
unsigned CallFrameSetupOpcode, CallFrameDestroyOpcode;
diff --git a/llvm/include/llvm/Target/TargetMachine.h b/llvm/include/llvm/Target/TargetMachine.h
index 39422ac3bf8c..176ae39b17a7 100644
--- a/llvm/include/llvm/Target/TargetMachine.h
+++ b/llvm/include/llvm/Target/TargetMachine.h
@@ -33,7 +33,6 @@ class MCInstrInfo;
class MCRegisterInfo;
class MCSubtargetInfo;
class MCSymbol;
-class MIRFormatter;
class raw_pwrite_stream;
class PassManagerBuilder;
struct PerFunctionMIParsingState;
@@ -95,7 +94,6 @@ class TargetMachine {
std::unique_ptr<const MCRegisterInfo> MRI;
std::unique_ptr<const MCInstrInfo> MII;
std::unique_ptr<const MCSubtargetInfo> STI;
- std::unique_ptr<const MIRFormatter> MIRF;
unsigned RequireStructuredCFG : 1;
unsigned O0WantsFastISel : 1;
@@ -199,10 +197,6 @@ class TargetMachine {
return nullptr;
}
- /// Return MIR formatter to format/parse MIR operands. Target can override
- /// this virtual function and return target specific MIR formatter.
- virtual const MIRFormatter *getMIRFormatter() const { return MIRF.get(); }
-
bool requiresStructuredCFG() const { return RequireStructuredCFG; }
void setRequiresStructuredCFG(bool Value) { RequireStructuredCFG = Value; }
diff --git a/llvm/lib/CodeGen/MIRParser/MIParser.cpp b/llvm/lib/CodeGen/MIRParser/MIParser.cpp
index 0f2648e2bfac..11cab488e22b 100644
--- a/llvm/lib/CodeGen/MIRParser/MIParser.cpp
+++ b/llvm/lib/CodeGen/MIRParser/MIParser.cpp
@@ -2619,7 +2619,8 @@ bool MIParser::parseMachineOperand(const unsigned OpCode, const unsigned OpIdx,
} else
return parseTypedImmediateOperand(Dest);
case MIToken::dot: {
- if (const auto *Formatter = MF.getTarget().getMIRFormatter()) {
+ const auto *TII = MF.getSubtarget().getInstrInfo();
+ if (const auto *Formatter = TII->getMIRFormatter()) {
return parseTargetImmMnemonic(OpCode, OpIdx, Dest, *Formatter);
}
LLVM_FALLTHROUGH;
@@ -2879,7 +2880,8 @@ bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
break;
case MIToken::kw_custom: {
lex();
- if (const auto *Formatter = MF.getTarget().getMIRFormatter()) {
+ const auto *TII = MF.getSubtarget().getInstrInfo();
+ if (const auto *Formatter = TII->getMIRFormatter()) {
if (Formatter->parseCustomPseudoSourceValue(
Token.stringValue(), MF, PFS, PSV,
[this](StringRef::iterator Loc, const Twine &Msg) -> bool {
diff --git a/llvm/lib/CodeGen/MIRPrinter.cpp b/llvm/lib/CodeGen/MIRPrinter.cpp
index 9d9c12a95918..1d61fdbba4df 100644
--- a/llvm/lib/CodeGen/MIRPrinter.cpp
+++ b/llvm/lib/CodeGen/MIRPrinter.cpp
@@ -709,7 +709,6 @@ void MIPrinter::print(const MachineInstr &MI) {
const auto *TRI = SubTarget.getRegisterInfo();
assert(TRI && "Expected target register info");
const auto *TII = SubTarget.getInstrInfo();
- const auto *MIRF = MF->getTarget().getMIRFormatter();
assert(TII && "Expected target instruction info");
if (MI.isCFIInstruction())
assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
@@ -808,7 +807,7 @@ void MIPrinter::print(const MachineInstr &MI) {
for (const auto *Op : MI.memoperands()) {
if (NeedComma)
OS << ", ";
- Op->print(OS, MST, SSNs, Context, &MFI, TII, MIRF);
+ Op->print(OS, MST, SSNs, Context, &MFI, TII);
NeedComma = true;
}
}
diff --git a/llvm/lib/CodeGen/MachineInstr.cpp b/llvm/lib/CodeGen/MachineInstr.cpp
index 177fef80e2e6..d3803b31f5c4 100644
--- a/llvm/lib/CodeGen/MachineInstr.cpp
+++ b/llvm/lib/CodeGen/MachineInstr.cpp
@@ -89,15 +89,13 @@ static void tryToGetTargetInfo(const MachineInstr &MI,
const TargetRegisterInfo *&TRI,
const MachineRegisterInfo *&MRI,
const TargetIntrinsicInfo *&IntrinsicInfo,
- const TargetInstrInfo *&TII,
- const MIRFormatter *&MIRF) {
+ const TargetInstrInfo *&TII) {
if (const MachineFunction *MF = getMFIfAvailable(MI)) {
TRI = MF->getSubtarget().getRegisterInfo();
MRI = &MF->getRegInfo();
IntrinsicInfo = MF->getTarget().getIntrinsicInfo();
TII = MF->getSubtarget().getInstrInfo();
- MIRF = MF->getTarget().getMIRFormatter();
}
}
@@ -1479,8 +1477,7 @@ void MachineInstr::print(raw_ostream &OS, ModuleSlotTracker &MST,
const TargetRegisterInfo *TRI = nullptr;
const MachineRegisterInfo *MRI = nullptr;
const TargetIntrinsicInfo *IntrinsicInfo = nullptr;
- const MIRFormatter *MIRF = nullptr;
- tryToGetTargetInfo(*this, TRI, MRI, IntrinsicInfo, TII, MIRF);
+ tryToGetTargetInfo(*this, TRI, MRI, IntrinsicInfo, TII);
if (isCFIInstruction())
assert(getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
@@ -1740,7 +1737,7 @@ void MachineInstr::print(raw_ostream &OS, ModuleSlotTracker &MST,
for (const MachineMemOperand *Op : memoperands()) {
if (NeedComma)
OS << ", ";
- Op->print(OS, MST, SSNs, *Context, MFI, TII, MIRF);
+ Op->print(OS, MST, SSNs, *Context, MFI, TII);
NeedComma = true;
}
}
diff --git a/llvm/lib/CodeGen/MachineOperand.cpp b/llvm/lib/CodeGen/MachineOperand.cpp
index 5dd98467ba66..0ea495bf0c09 100644
--- a/llvm/lib/CodeGen/MachineOperand.cpp
+++ b/llvm/lib/CodeGen/MachineOperand.cpp
@@ -784,8 +784,11 @@ void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
}
case MachineOperand::MO_Immediate: {
const MIRFormatter *Formatter = nullptr;
- if (const MachineFunction *MF = getMFIfAvailable(*this))
- Formatter = MF->getTarget().getMIRFormatter();
+ if (const MachineFunction *MF = getMFIfAvailable(*this)) {
+ const auto *TII = MF->getSubtarget().getInstrInfo();
+ assert(TII && "expected instruction info");
+ Formatter = TII->getMIRFormatter();
+ }
if (Formatter)
Formatter->printImm(OS, *getParent(), OpIdx, getImm());
else
@@ -1057,8 +1060,7 @@ void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
SmallVectorImpl<StringRef> &SSNs,
const LLVMContext &Context,
const MachineFrameInfo *MFI,
- const TargetInstrInfo *TII,
- const MIRFormatter* MIRF) const {
+ const TargetInstrInfo *TII) const {
OS << '(';
if (isVolatile())
OS << "volatile ";
@@ -1133,15 +1135,13 @@ void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol());
break;
default: {
+ const MIRFormatter *Formatter = TII->getMIRFormatter();
// FIXME: This is not necessarily the correct MIR serialization format for
// a custom pseudo source value, but at least it allows
// -print-machineinstrs to work on a target with custom pseudo source
// values.
OS << "custom \"";
- if (MIRF)
- MIRF->printCustomPseudoSourceValue(OS, MST, *PVal);
- else
- PVal->printCustom(OS);
+ Formatter->printCustomPseudoSourceValue(OS, MST, *PVal);
OS << '\"';
break;
}
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGDumper.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGDumper.cpp
index cbdcb93e60c9..6fd71393bf38 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGDumper.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGDumper.cpp
@@ -480,8 +480,7 @@ static void printMemOperand(raw_ostream &OS, const MachineMemOperand &MMO,
if (MF)
MST.incorporateFunction(MF->getFunction());
SmallVector<StringRef, 0> SSNs;
- MMO.print(OS, MST, SSNs, Ctx, MFI, TII,
- MF ? MF->getTarget().getMIRFormatter() : nullptr);
+ MMO.print(OS, MST, SSNs, Ctx, MFI, TII);
}
static void printMemOperand(raw_ostream &OS, const MachineMemOperand &MMO,
diff --git a/llvm/lib/Target/TargetMachine.cpp b/llvm/lib/Target/TargetMachine.cpp
index f070b143d5b4..97a1eb2f190a 100644
--- a/llvm/lib/Target/TargetMachine.cpp
+++ b/llvm/lib/Target/TargetMachine.cpp
@@ -12,7 +12,6 @@
#include "llvm/Target/TargetMachine.h"
#include "llvm/Analysis/TargetTransformInfo.h"
-#include "llvm/CodeGen/MIRFormatter.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalAlias.h"
#include "llvm/IR/GlobalValue.h"
@@ -38,9 +37,7 @@ TargetMachine::TargetMachine(const Target &T, StringRef DataLayoutString,
: TheTarget(T), DL(DataLayoutString), TargetTriple(TT), TargetCPU(CPU),
TargetFS(FS), AsmInfo(nullptr), MRI(nullptr), MII(nullptr), STI(nullptr),
RequireStructuredCFG(false), O0WantsFastISel(false),
- DefaultOptions(Options), Options(Options) {
- MIRF = std::make_unique<MIRFormatter>();
-}
+ DefaultOptions(Options), Options(Options) {}
TargetMachine::~TargetMachine() = default;
More information about the llvm-commits
mailing list