Skip to content

Commit de3d0ee

Browse files
committed
Revert "Revert "[MIR] Target specific MIR formating and parsing""
There was an unguarded dereference of MF in a function that permitted nullptr. Fixed This reverts commit 71d64f7.
1 parent 71d64f7 commit de3d0ee

27 files changed

+562
-315
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//===-- llvm/CodeGen/MIRFormatter.h -----------------------------*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// This file contains the declaration of the MIRFormatter class.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_CODEGEN_MIRFORMATTER_H
15+
#define LLVM_CODEGEN_MIRFORMATTER_H
16+
17+
#include "llvm/CodeGen/MachineInstr.h"
18+
#include "llvm/CodeGen/PseudoSourceValue.h"
19+
20+
namespace llvm {
21+
22+
struct PerFunctionMIParsingState;
23+
struct SlotMapping;
24+
25+
/// MIRFormater - Interface to format MIR operand based on target
26+
class MIRFormatter {
27+
public:
28+
typedef function_ref<bool(StringRef::iterator Loc, const Twine &)>
29+
ErrorCallbackType;
30+
31+
MIRFormatter() {}
32+
virtual ~MIRFormatter() = default;
33+
34+
/// Implement target specific printing for machine operand immediate value, so
35+
/// that we can have more meaningful mnemonic than a 64-bit integer. Passing
36+
/// None to OpIdx means the index is unknown.
37+
virtual void printImm(raw_ostream &OS, const MachineInstr &MI,
38+
Optional<unsigned> OpIdx, int64_t Imm) const {
39+
OS << Imm;
40+
}
41+
42+
/// Implement target specific parsing of immediate mnemonics. The mnemonic is
43+
/// dot seperated strings.
44+
virtual bool parseImmMnemonic(const unsigned OpCode, const unsigned OpIdx,
45+
StringRef Src, int64_t &Imm,
46+
ErrorCallbackType ErrorCallback) const {
47+
llvm_unreachable("target did not implement parsing MIR immediate mnemonic");
48+
}
49+
50+
/// Implement target specific printing of target custom pseudo source value.
51+
/// Default implementation is not necessarily the correct MIR serialization
52+
/// format.
53+
virtual void
54+
printCustomPseudoSourceValue(raw_ostream &OS, ModuleSlotTracker &MST,
55+
const PseudoSourceValue &PSV) const {
56+
PSV.printCustom(OS);
57+
}
58+
59+
/// Implement target specific parsing of target custom pseudo source value.
60+
virtual bool parseCustomPseudoSourceValue(
61+
StringRef Src, MachineFunction &MF, PerFunctionMIParsingState &PFS,
62+
const PseudoSourceValue *&PSV, ErrorCallbackType ErrorCallback) const {
63+
llvm_unreachable(
64+
"target did not implement parsing MIR custom pseudo source value");
65+
}
66+
67+
/// Helper functions to print IR value as MIR serialization format which will
68+
/// be useful for target specific printer, e.g. for printing IR value in
69+
/// custom pseudo source value.
70+
static void printIRValue(raw_ostream &OS, const Value &V,
71+
ModuleSlotTracker &MST);
72+
73+
/// Helper functions to parse IR value from MIR serialization format which
74+
/// will be useful for target specific parser, e.g. for parsing IR value for
75+
/// custom pseudo source value.
76+
static bool parseIRValue(StringRef Src, MachineFunction &MF,
77+
PerFunctionMIParsingState &PFS, const Value *&V,
78+
ErrorCallbackType ErrorCallback);
79+
};
80+
81+
} // end namespace llvm
82+
83+
#endif

llvm/include/llvm/CodeGen/MIRParser/MIParser.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,16 @@ struct PerFunctionMIParsingState {
171171
DenseMap<unsigned, unsigned> ConstantPoolSlots;
172172
DenseMap<unsigned, unsigned> JumpTableSlots;
173173

174+
/// Maps from slot numbers to function's unnamed values.
175+
DenseMap<unsigned, const Value *> Slots2Values;
176+
174177
PerFunctionMIParsingState(MachineFunction &MF, SourceMgr &SM,
175178
const SlotMapping &IRSlots,
176179
PerTargetMIParsingState &Target);
177180

178181
VRegInfo &getVRegInfo(unsigned Num);
179182
VRegInfo &getVRegInfoNamed(StringRef RegName);
183+
const Value *getIRValue(unsigned Slot);
180184
};
181185

182186
/// Parse the machine basic block definitions, and skip the machine

llvm/include/llvm/CodeGen/MachineMemOperand.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace llvm {
2626

2727
class FoldingSetNodeID;
2828
class MDNode;
29+
class MIRFormatter;
2930
class raw_ostream;
3031
class MachineFunction;
3132
class ModuleSlotTracker;
@@ -295,7 +296,8 @@ class MachineMemOperand {
295296
/// @{
296297
void print(raw_ostream &OS, ModuleSlotTracker &MST,
297298
SmallVectorImpl<StringRef> &SSNs, const LLVMContext &Context,
298-
const MachineFrameInfo *MFI, const TargetInstrInfo *TII) const;
299+
const MachineFrameInfo *MFI, const TargetInstrInfo *TII,
300+
const MIRFormatter *MIRF) const;
299301
/// @}
300302

301303
friend bool operator==(const MachineMemOperand &LHS,

llvm/include/llvm/CodeGen/MachineOperand.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ class MachineOperand {
278278
/// More complex way of printing a MachineOperand.
279279
/// \param TypeToPrint specifies the generic type to be printed on uses and
280280
/// defs. It can be determined using MachineInstr::getTypeToPrint.
281+
/// \param OpIdx - specifies the index of the operand in machine instruction.
282+
/// This will be used by target dependent MIR formatter. Could be None if the
283+
/// index is unknown, e.g. called by dump().
281284
/// \param PrintDef - whether we want to print `def` on an operand which
282285
/// isDef. Sometimes, if the operand is printed before '=', we don't print
283286
/// `def`.
@@ -294,8 +297,9 @@ class MachineOperand {
294297
/// information from it's parent.
295298
/// \param IntrinsicInfo - same as \p TRI.
296299
void print(raw_ostream &os, ModuleSlotTracker &MST, LLT TypeToPrint,
297-
bool PrintDef, bool IsStandalone, bool ShouldPrintRegisterTies,
298-
unsigned TiedOperandIdx, const TargetRegisterInfo *TRI,
300+
Optional<unsigned> OpIdx, bool PrintDef, bool IsStandalone,
301+
bool ShouldPrintRegisterTies, unsigned TiedOperandIdx,
302+
const TargetRegisterInfo *TRI,
299303
const TargetIntrinsicInfo *IntrinsicInfo) const;
300304

301305
/// Same as print(os, TRI, IntrinsicInfo), but allows to specify the low-level

llvm/include/llvm/CodeGen/PseudoSourceValue.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace llvm {
2222

2323
class MachineFrameInfo;
2424
class MachineMemOperand;
25+
class MIRFormatter;
2526
class raw_ostream;
2627
class TargetInstrInfo;
2728

@@ -52,6 +53,7 @@ class PseudoSourceValue {
5253
const PseudoSourceValue* PSV);
5354

5455
friend class MachineMemOperand; // For printCustom().
56+
friend class MIRFormatter; // For printCustom().
5557

5658
/// Implement printing for PseudoSourceValue. This is called from
5759
/// Value::print or Value's operator<<.

llvm/include/llvm/CodeGen/TargetInstrInfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/ADT/DenseMapInfo.h"
1919
#include "llvm/ADT/None.h"
2020
#include "llvm/CodeGen/LiveRegUnits.h"
21+
#include "llvm/CodeGen/MIRFormatter.h"
2122
#include "llvm/CodeGen/MachineBasicBlock.h"
2223
#include "llvm/CodeGen/MachineCombinerPattern.h"
2324
#include "llvm/CodeGen/MachineFunction.h"
@@ -1807,6 +1808,7 @@ class TargetInstrInfo : public MCInstrInfo {
18071808
Register Reg) const;
18081809

18091810
private:
1811+
mutable std::unique_ptr<MIRFormatter> Formatter;
18101812
unsigned CallFrameSetupOpcode, CallFrameDestroyOpcode;
18111813
unsigned CatchRetOpcode;
18121814
unsigned ReturnOpcode;

llvm/include/llvm/Target/TargetMachine.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class MCInstrInfo;
3333
class MCRegisterInfo;
3434
class MCSubtargetInfo;
3535
class MCSymbol;
36+
class MIRFormatter;
3637
class raw_pwrite_stream;
3738
class PassManagerBuilder;
3839
struct PerFunctionMIParsingState;
@@ -94,6 +95,7 @@ class TargetMachine {
9495
std::unique_ptr<const MCRegisterInfo> MRI;
9596
std::unique_ptr<const MCInstrInfo> MII;
9697
std::unique_ptr<const MCSubtargetInfo> STI;
98+
std::unique_ptr<const MIRFormatter> MIRF;
9799

98100
unsigned RequireStructuredCFG : 1;
99101
unsigned O0WantsFastISel : 1;
@@ -197,6 +199,10 @@ class TargetMachine {
197199
return nullptr;
198200
}
199201

202+
/// Return MIR formatter to format/parse MIR operands. Target can override
203+
/// this virtual function and return target specific MIR formatter.
204+
virtual const MIRFormatter *getMIRFormatter() const { return MIRF.get(); }
205+
200206
bool requiresStructuredCFG() const { return RequireStructuredCFG; }
201207
void setRequiresStructuredCFG(bool Value) { RequireStructuredCFG = Value; }
202208

llvm/lib/CodeGen/MIRParser/MILexer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ static MIToken::TokenKind getIdentifierKind(StringRef Identifier) {
242242
.Case("jump-table", MIToken::kw_jump_table)
243243
.Case("constant-pool", MIToken::kw_constant_pool)
244244
.Case("call-entry", MIToken::kw_call_entry)
245+
.Case("custom", MIToken::kw_custom)
245246
.Case("liveout", MIToken::kw_liveout)
246247
.Case("address-taken", MIToken::kw_address_taken)
247248
.Case("landing-pad", MIToken::kw_landing_pad)

llvm/lib/CodeGen/MIRParser/MILexer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ struct MIToken {
110110
kw_jump_table,
111111
kw_constant_pool,
112112
kw_call_entry,
113+
kw_custom,
113114
kw_liveout,
114115
kw_address_taken,
115116
kw_landing_pad,

0 commit comments

Comments
 (0)