Skip to content

Commit bfb9539

Browse files
authored
TableGen: Support target specialized pseudoinstructions (#159880)
Allow a target to steal the definition of a generic pseudoinstruction and remap the operands. This works by defining a new instruction, which will simply swap out the emitted entry in the InstrInfo table. This is intended to eliminate the C++ half of the implementation of PointerLikeRegClass. With RegClassByHwMode, the remaining usecase for PointerLikeRegClass are the common codegen pseudoinstructions. Every target maintains its own copy of the generic pseudo operand definitions anyway, so we can stub out the register operands with an appropriate class instead of waiting for runtime resolution. In the future we could probably take this a bit further. For example, there is a similar problem for ADJCALLSTACKUP/DOWN since they depend on target register definitions for the stack pointer register.
1 parent 522177c commit bfb9539

File tree

4 files changed

+242
-1
lines changed

4 files changed

+242
-1
lines changed

llvm/include/llvm/Target/Target.td

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,99 @@ def CONVERGENCECTRL_GLUE : StandardPseudoInstruction {
15791579
}
15801580
}
15811581

1582+
/// Allow a target to replace the instruction definition of a
1583+
/// StandardPseudoInstruction. A target should only define one
1584+
/// instance of this per instruction.
1585+
///
1586+
/// This is intended to allow targets to specify the register class
1587+
/// used for pointers. It should not be used to change the fundamental
1588+
/// operand structure (e.g., this should not add or remove operands,
1589+
/// or change the operand types).
1590+
class TargetSpecializedStandardPseudoInstruction<
1591+
StandardPseudoInstruction base_inst> : Instruction {
1592+
1593+
StandardPseudoInstruction Instruction = base_inst;
1594+
let OutOperandList = base_inst.OutOperandList;
1595+
let InOperandList = base_inst.InOperandList;
1596+
1597+
// TODO: Copy everything
1598+
let usesCustomInserter = base_inst.usesCustomInserter;
1599+
let hasSideEffects = base_inst.hasSideEffects;
1600+
let mayLoad = base_inst.mayLoad;
1601+
let mayStore = base_inst.mayStore;
1602+
let isTerminator = base_inst.isTerminator;
1603+
let isBranch = base_inst.isBranch;
1604+
let isIndirectBranch = base_inst.isIndirectBranch;
1605+
let isEHScopeReturn = base_inst.isEHScopeReturn;
1606+
let isReturn = base_inst.isReturn;
1607+
let isCall = base_inst.isCall;
1608+
let hasCtrlDep = base_inst.hasCtrlDep;
1609+
let isReMaterializable = base_inst.isReMaterializable;
1610+
let isMeta = base_inst.isMeta;
1611+
let Size = base_inst.Size;
1612+
let isAsCheapAsAMove = base_inst.isAsCheapAsAMove;
1613+
let isPseudo = true;
1614+
let hasNoSchedulingInfo = true;
1615+
let isNotDuplicable = base_inst.isNotDuplicable;
1616+
let isConvergent = base_inst.isConvergent;
1617+
let hasExtraSrcRegAllocReq = base_inst.hasExtraSrcRegAllocReq;
1618+
let hasExtraDefRegAllocReq = base_inst.hasExtraDefRegAllocReq;
1619+
}
1620+
1621+
// All pseudo instructions which need a pointer register class, which
1622+
// should be specialized by a target.
1623+
defvar PseudosWithPtrOps = [
1624+
LOAD_STACK_GUARD,
1625+
PREALLOCATED_ARG,
1626+
PATCHABLE_EVENT_CALL,
1627+
PATCHABLE_TYPED_EVENT_CALL
1628+
];
1629+
1630+
1631+
/// Replace PointerLikeRegClass operands in OperandList with new_rc.
1632+
class RemapPointerOperandList<dag OperandList, RegisterClassLike new_rc> {
1633+
// Collect the set of names so we can query and rewrite them.
1634+
list<string> op_names = !foreach(i, !range(!size(OperandList)),
1635+
!getdagname(OperandList, i));
1636+
1637+
// Beautiful language. This would be a lot easier if !getdagarg
1638+
// didn't require a specific type. We can't just collect a list of
1639+
// the operand values and reconstruct the dag, since there isn't a
1640+
// common base class for all the field kinds used in
1641+
// pseudoinstruction definitions; therefore everything must be
1642+
// maintained as a dag, so use a foldl. Additionally, ? doesn't
1643+
// evaluate as false so we get even more noise.
1644+
dag ret =
1645+
!foldl(OperandList, op_names, acc, name,
1646+
!cond(
1647+
!initialized(!getdagarg<PointerLikeRegClass>(OperandList, name))
1648+
: !setdagarg(acc, name, new_rc),
1649+
!initialized(!getdagarg<unknown_class>(OperandList, name)) : acc,
1650+
!initialized(!getdagarg<DAGOperand>(OperandList, name)) : acc
1651+
)
1652+
);
1653+
}
1654+
1655+
/// Define an override for a pseudoinstruction which uses a pointer
1656+
/// register class, specialized to the target's pointer type.
1657+
class RemapPointerOperands<StandardPseudoInstruction inst,
1658+
RegisterClassLike new_rc> :
1659+
TargetSpecializedStandardPseudoInstruction<inst> {
1660+
let OutOperandList =
1661+
RemapPointerOperandList<inst.OutOperandList, new_rc>.ret;
1662+
let InOperandList =
1663+
RemapPointerOperandList<inst.InOperandList, new_rc>.ret;
1664+
}
1665+
1666+
/// Helper to replace all pseudoinstructions using pointers to a
1667+
/// target register class. Most targets should use this.
1668+
multiclass RemapAllTargetPseudoPointerOperands<
1669+
RegisterClassLike default_ptr_rc> {
1670+
foreach inst = PseudosWithPtrOps in {
1671+
def : RemapPointerOperands<inst, default_ptr_rc>;
1672+
}
1673+
}
1674+
15821675
// Generic opcodes used in GlobalISel.
15831676
include "llvm/Target/GenericOpcodes.td"
15841677

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// RUN: llvm-tblgen -gen-instr-info -I %p/../../include %s -DONECASE -o - | FileCheck -check-prefixes=CHECK,ONECASE %s
2+
// RUN: llvm-tblgen -gen-instr-info -I %p/../../include %s -DALLCASES -o - | FileCheck -check-prefixes=CHECK,ALLCASES %s
3+
// RUN: not llvm-tblgen -gen-instr-info -I %p/../../include %s -DERROR -o /dev/null 2>&1 | FileCheck -check-prefix=ERROR %s
4+
5+
// CHECK: namespace llvm::MyTarget {
6+
// CHECK: enum {
7+
// CHECK: LOAD_STACK_GUARD = [[LOAD_STACK_GUARD_OPCODE:[0-9]+]],
8+
// CHECK: PREALLOCATED_ARG = [[PREALLOCATED_ARG_OPCODE:[0-9]+]],
9+
// CHECK: PATCHABLE_EVENT_CALL = [[PATCHABLE_EVENT_CALL_OPCODE:[0-9]+]],
10+
// CHECK: PATCHABLE_TYPED_EVENT_CALL = [[PATCHABLE_TYPED_EVENT_CALL_OPCODE:[0-9]+]],
11+
12+
// Make sure no enum entry is emitted for MY_LOAD_STACK_GUARD
13+
// CHECK: G_UBFX = [[G_UBFX_OPCODE:[0-9]+]],
14+
// CHECK-NEXT: MY_MOV = [[MY_MOV_OPCODE:[0-9]+]],
15+
// CHECK-NEXT: INSTRUCTION_LIST_END = [[INSTR_LIST_END_OPCODE:[0-9]+]]
16+
17+
18+
// CHECK: extern const MyTargetInstrTable MyTargetDescs = {
19+
// CHECK-NEXT: {
20+
// CHECK-NEXT: { [[MY_MOV_OPCODE]], 2, 1, 2, 0, 0, 0, {{[0-9]+}}, MyTargetImpOpBase + 0, 0|(1ULL<<MCID::ExtraSrcRegAllocReq)|(1ULL<<MCID::ExtraDefRegAllocReq), 0x0ULL }, // MY_MOV
21+
// CHECK-NEXT: { [[G_UBFX_OPCODE]], 4, 1, 0, 0, 0, 0, {{[0-9]+}}, MyTargetImpOpBase + 0, 0|(1ULL<<MCID::PreISelOpcode)|(1ULL<<MCID::Pseudo)|(1ULL<<MCID::ExtraSrcRegAllocReq)|(1ULL<<MCID::ExtraDefRegAllocReq), 0x0ULL }, // G_UBFX
22+
23+
// ONECASE: { [[LOAD_STACK_GUARD_OPCODE]], 1, 1, 0, 0, 0, 0, [[LOAD_STACK_GUARD_OP_ENTRY:[0-9]+]], MyTargetImpOpBase + 0, 0|(1ULL<<MCID::Pseudo)|(1ULL<<MCID::MayLoad)|(1ULL<<MCID::Rematerializable)|(1ULL<<MCID::ExtraSrcRegAllocReq)|(1ULL<<MCID::ExtraDefRegAllocReq), 0x0ULL }, // MY_LOAD_STACK_GUARD
24+
25+
// ALLCASES: { [[PATCHABLE_TYPED_EVENT_CALL_OPCODE]], 3, 0, 0, 0, 0, 0, [[PATCHABLE_TYPED_EVENT_CALL_OP_ENTRY:[0-9]+]], MyTargetImpOpBase + 0, 0|(1ULL<<MCID::Pseudo)|(1ULL<<MCID::Call)|(1ULL<<MCID::MayLoad)|(1ULL<<MCID::MayStore)|(1ULL<<MCID::UsesCustomInserter)|(1ULL<<MCID::UnmodeledSideEffects)|(1ULL<<MCID::ExtraSrcRegAllocReq)|(1ULL<<MCID::ExtraDefRegAllocReq), 0x0ULL }, // anonymous_
26+
// ALLCASES: { [[PATCHABLE_EVENT_CALL_OPCODE]], 2, 0, 0, 0, 0, 0, [[PATCHABLE_EVENT_CALL_OP_ENTRY:[0-9]+]], MyTargetImpOpBase + 0, 0|(1ULL<<MCID::Pseudo)|(1ULL<<MCID::Call)|(1ULL<<MCID::MayLoad)|(1ULL<<MCID::MayStore)|(1ULL<<MCID::UsesCustomInserter)|(1ULL<<MCID::UnmodeledSideEffects)|(1ULL<<MCID::ExtraSrcRegAllocReq)|(1ULL<<MCID::ExtraDefRegAllocReq), 0x0ULL }, // anonymous_
27+
// ALLCASES: { [[PREALLOCATED_ARG_OPCODE]], 3, 1, 0, 0, 0, 0, [[PREALLOCATED_ARG_OP_ENTRY:[0-9]+]], MyTargetImpOpBase + 0, 0|(1ULL<<MCID::Pseudo)|(1ULL<<MCID::UsesCustomInserter)|(1ULL<<MCID::UnmodeledSideEffects)|(1ULL<<MCID::ExtraSrcRegAllocReq)|(1ULL<<MCID::ExtraDefRegAllocReq), 0x0ULL }, // anonymous_
28+
// ALLCASES: { [[LOAD_STACK_GUARD_OPCODE]], 1, 1, 0, 0, 0, 0, [[LOAD_STACK_GUARD_OP_ENTRY:[0-9]+]], MyTargetImpOpBase + 0, 0|(1ULL<<MCID::Pseudo)|(1ULL<<MCID::MayLoad)|(1ULL<<MCID::Rematerializable)|(1ULL<<MCID::ExtraSrcRegAllocReq)|(1ULL<<MCID::ExtraDefRegAllocReq), 0x0ULL }, // anonymous_
29+
30+
// CHECK: /* 0 */ { -1, 0, MCOI::OPERAND_UNKNOWN, 0 },
31+
32+
// ONECASE: /* [[LOAD_STACK_GUARD_OP_ENTRY]] */ { MyTarget::XRegsRegClassID, 0, MCOI::OPERAND_REGISTER, 0 },
33+
34+
// ALLCASES: /* [[LOAD_STACK_GUARD_OP_ENTRY]] */ { MyTarget::XRegsRegClassID, 0, MCOI::OPERAND_REGISTER, 0 },
35+
// ALLCASES: /* [[PREALLOCATED_ARG_OP_ENTRY]] */ { MyTarget::XRegsRegClassID, 0, MCOI::OPERAND_REGISTER, 0 }, { -1, 0, MCOI::OPERAND_IMMEDIATE, 0 }, { -1, 0, MCOI::OPERAND_IMMEDIATE, 0 },
36+
// ALLCASES: /* [[PATCHABLE_EVENT_CALL_OP_ENTRY]] */ { MyTarget::XRegsRegClassID, 0, MCOI::OPERAND_REGISTER, 0 }, { -1, 0, MCOI::OPERAND_UNKNOWN, 0 },
37+
// ALLCASES: /* [[PATCHABLE_TYPED_EVENT_CALL_OP_ENTRY]] */ { -1, 0, MCOI::OPERAND_UNKNOWN, 0 }, { MyTarget::XRegsRegClassID, 0, MCOI::OPERAND_REGISTER, 0 }, { -1, 0, MCOI::OPERAND_UNKNOWN, 0 },
38+
39+
40+
// CHECK: const char MyTargetInstrNameData[] = {
41+
// CHECK: /* {{[0-9]+}} */ "LOAD_STACK_GUARD\000"
42+
43+
include "llvm/Target/Target.td"
44+
45+
class MyReg<string n>
46+
: Register<n> {
47+
let Namespace = "MyTarget";
48+
}
49+
50+
class MyClass<int size, list<ValueType> types, dag registers>
51+
: RegisterClass<"MyTarget", types, size, registers> {
52+
let Size = size;
53+
}
54+
55+
def X0 : MyReg<"x0">;
56+
def X1 : MyReg<"x1">;
57+
def XRegs : RegisterClass<"MyTarget", [i64], 64, (add X0, X1)>;
58+
59+
60+
class TestInstruction : Instruction {
61+
let Size = 2;
62+
let Namespace = "MyTarget";
63+
let hasSideEffects = false;
64+
}
65+
66+
#ifdef ONECASE
67+
68+
// Example setting the pointer register class manually
69+
def MY_LOAD_STACK_GUARD :
70+
TargetSpecializedStandardPseudoInstruction<LOAD_STACK_GUARD> {
71+
let Namespace = "MyTarget";
72+
let OutOperandList = (outs XRegs:$dst);
73+
}
74+
75+
#endif
76+
77+
#ifdef ALLCASES
78+
79+
defm my_remaps : RemapAllTargetPseudoPointerOperands<XRegs>;
80+
81+
#endif
82+
83+
84+
#ifdef ERROR
85+
86+
def MY_LOAD_STACK_GUARD_0 : TargetSpecializedStandardPseudoInstruction<LOAD_STACK_GUARD>;
87+
88+
// ERROR: :[[@LINE+1]]:5: error: multiple overrides of 'LOAD_STACK_GUARD' defined
89+
def MY_LOAD_STACK_GUARD_1 : TargetSpecializedStandardPseudoInstruction<LOAD_STACK_GUARD>;
90+
91+
#endif
92+
93+
def MY_MOV : TestInstruction {
94+
let OutOperandList = (outs XRegs:$dst);
95+
let InOperandList = (ins XRegs:$src);
96+
let AsmString = "my_mov $dst, $src";
97+
}
98+
99+
100+
def MyTargetISA : InstrInfo;
101+
def MyTarget : Target { let InstructionSet = MyTargetISA; }

llvm/utils/TableGen/Common/CodeGenTarget.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,15 +283,25 @@ void CodeGenTarget::ComputeInstrsByEnum() const {
283283
assert(EndOfPredefines == getNumFixedInstructions() &&
284284
"Missing generic opcode");
285285

286+
unsigned SkippedInsts = 0;
287+
286288
for (const auto &[_, CGIUp] : InstMap) {
287289
const CodeGenInstruction *CGI = CGIUp.get();
288290
if (CGI->Namespace != "TargetOpcode") {
291+
292+
if (CGI->TheDef->isSubClassOf(
293+
"TargetSpecializedStandardPseudoInstruction")) {
294+
++SkippedInsts;
295+
continue;
296+
}
297+
289298
InstrsByEnum.push_back(CGI);
290299
NumPseudoInstructions += CGI->TheDef->getValueAsBit("isPseudo");
291300
}
292301
}
293302

294-
assert(InstrsByEnum.size() == InstMap.size() && "Missing predefined instr");
303+
assert(InstrsByEnum.size() + SkippedInsts == InstMap.size() &&
304+
"Missing predefined instr");
295305

296306
// All of the instructions are now in random order based on the map iteration.
297307
llvm::sort(

llvm/utils/TableGen/InstrInfoEmitter.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ class InstrInfoEmitter {
7272
using OperandInfoListTy = std::vector<OperandInfoTy>;
7373
using OperandInfoMapTy = std::map<OperandInfoTy, unsigned>;
7474

75+
DenseMap<const CodeGenInstruction *, const CodeGenInstruction *>
76+
TargetSpecializedPseudoInsts;
77+
78+
/// Compute mapping of opcodes which should have their definitions overridden
79+
/// by a target version.
80+
void buildTargetSpecializedPseudoInstsMap();
81+
7582
/// Generate member functions in the target-specific GenInstrInfo class.
7683
///
7784
/// This method is used to custom expand TIIPredicate definitions.
@@ -216,6 +223,10 @@ InstrInfoEmitter::CollectOperandInfo(OperandInfoListTy &OperandInfoList,
216223
const CodeGenTarget &Target = CDP.getTargetInfo();
217224
unsigned Offset = 0;
218225
for (const CodeGenInstruction *Inst : Target.getInstructions()) {
226+
auto OverrideEntry = TargetSpecializedPseudoInsts.find(Inst);
227+
if (OverrideEntry != TargetSpecializedPseudoInsts.end())
228+
Inst = OverrideEntry->second;
229+
219230
OperandInfoTy OperandInfo = GetOperandInfo(*Inst);
220231
if (OperandInfoMap.try_emplace(OperandInfo, Offset).second) {
221232
OperandInfoList.push_back(OperandInfo);
@@ -859,6 +870,25 @@ void InstrInfoEmitter::emitTIIHelperMethods(raw_ostream &OS,
859870
}
860871
}
861872

873+
void InstrInfoEmitter::buildTargetSpecializedPseudoInstsMap() {
874+
ArrayRef<const Record *> SpecializedInsts = Records.getAllDerivedDefinitions(
875+
"TargetSpecializedStandardPseudoInstruction");
876+
const CodeGenTarget &Target = CDP.getTargetInfo();
877+
878+
for (const Record *SpecializedRec : SpecializedInsts) {
879+
const CodeGenInstruction &SpecializedInst =
880+
Target.getInstruction(SpecializedRec);
881+
const Record *BaseInstRec = SpecializedRec->getValueAsDef("Instruction");
882+
883+
const CodeGenInstruction &BaseInst = Target.getInstruction(BaseInstRec);
884+
885+
if (!TargetSpecializedPseudoInsts.insert({&BaseInst, &SpecializedInst})
886+
.second)
887+
PrintFatalError(SpecializedRec, "multiple overrides of '" +
888+
BaseInst.getName() + "' defined");
889+
}
890+
}
891+
862892
//===----------------------------------------------------------------------===//
863893
// Main Output.
864894
//===----------------------------------------------------------------------===//
@@ -881,6 +911,8 @@ void InstrInfoEmitter::run(raw_ostream &OS) {
881911

882912
// Collect all of the operand info records.
883913
Timer.startTimer("Collect operand info");
914+
buildTargetSpecializedPseudoInstsMap();
915+
884916
OperandInfoListTy OperandInfoList;
885917
OperandInfoMapTy OperandInfoMap;
886918
unsigned OperandInfoSize =
@@ -963,6 +995,11 @@ void InstrInfoEmitter::run(raw_ostream &OS) {
963995
for (const CodeGenInstruction *Inst : reverse(NumberedInstructions)) {
964996
// Keep a list of the instruction names.
965997
InstrNames.add(Inst->getName());
998+
999+
auto OverrideEntry = TargetSpecializedPseudoInsts.find(Inst);
1000+
if (OverrideEntry != TargetSpecializedPseudoInsts.end())
1001+
Inst = OverrideEntry->second;
1002+
9661003
// Emit the record into the table.
9671004
emitRecord(*Inst, --Num, InstrInfo, EmittedLists, OperandInfoMap, OS);
9681005
}

0 commit comments

Comments
 (0)