Skip to content

Commit 7affc5b

Browse files
committed
[DecoderEmitter] Add support for DecodeOrder and resolve-conflicts-try-all
1 parent e07716d commit 7affc5b

21 files changed

+304
-163
lines changed

llvm/include/llvm/Target/Target.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,10 @@ class InstructionEncoding {
551551
// where multiple ISA namespaces exist).
552552
string DecoderNamespace = "";
553553

554+
// Within a namespace, DecodeOrder is used to order instructions when we need
555+
// to attempt multiple encoding.
556+
int DecodeOrder = 0;
557+
554558
// List of predicates which will be turned into isel matching code.
555559
list<Predicate> Predicates = [];
556560

llvm/lib/Target/AArch64/AArch64InstrFormats.td

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,7 +1957,7 @@ class MRSI : RtSystemI<1, (outs GPR64:$Rt), (ins mrs_sysreg_op:$systemreg),
19571957
"mrs", "\t$Rt, $systemreg"> {
19581958
bits<16> systemreg;
19591959
let Inst{20-5} = systemreg;
1960-
let DecoderNamespace = "Fallback";
1960+
let DecodeOrder = 1;
19611961
// The MRS is set as a NZCV setting instruction. Not all MRS instructions
19621962
// require doing this. The alternative was to explicitly model each one, but
19631963
// it feels like it is unnecessary because it seems there are no negative
@@ -1972,7 +1972,7 @@ class MSRI : RtSystemI<0, (outs), (ins msr_sysreg_op:$systemreg, GPR64:$Rt),
19721972
"msr", "\t$systemreg, $Rt"> {
19731973
bits<16> systemreg;
19741974
let Inst{20-5} = systemreg;
1975-
let DecoderNamespace = "Fallback";
1975+
let DecodeOrder = 1;
19761976
}
19771977

19781978
def SystemPStateFieldWithImm0_15Operand : AsmOperandClass {
@@ -2045,7 +2045,8 @@ class MSRpstateImm0_1
20452045
// MSRpstateI aliases with MSRI. When the MSRpstateI decoder method returns
20462046
// Fail the decoder should attempt to decode the instruction as MSRI.
20472047
let hasCompleteDecoder = false;
2048-
let DecoderNamespace = "Fallback";
2048+
let DecodeOrder = 1;
2049+
20492050
}
20502051

20512052
// SYS and SYSL generic system instructions.

llvm/lib/Target/AArch64/AArch64InstrInfo.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10712,8 +10712,8 @@ def RPRFM:
1071210712
let hasSideEffects = 1;
1071310713
// RPRFM overlaps with PRFM (reg), when the decoder method of PRFM returns
1071410714
// Fail, the decoder should attempt to decode RPRFM. This requires setting
10715-
// the decoder namespace to "Fallback".
10716-
let DecoderNamespace = "Fallback";
10715+
// the decode order for RPRFM to be 1 ( > decode order for PRFM).
10716+
let DecodeOrder = 1;
1071710717
}
1071810718

1071910719
//===----------------------------------------------------------------------===//

llvm/lib/Target/AArch64/Disassembler/AArch64Disassembler.cpp

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,43 +1578,37 @@ DecodeStatus AArch64Disassembler::getInstruction(MCInst &MI, uint64_t &Size,
15781578
uint32_t Insn =
15791579
(Bytes[3] << 24) | (Bytes[2] << 16) | (Bytes[1] << 8) | (Bytes[0] << 0);
15801580

1581-
const uint8_t *Tables[] = {DecoderTable32, DecoderTableFallback32};
1582-
1583-
for (const auto *Table : Tables) {
1584-
DecodeStatus Result =
1585-
decodeInstruction(Table, MI, Insn, Address, this, STI);
1586-
1587-
const MCInstrDesc &Desc = MCII->get(MI.getOpcode());
1588-
1589-
// For Scalable Matrix Extension (SME) instructions that have an implicit
1590-
// operand for the accumulator (ZA) or implicit immediate zero which isn't
1591-
// encoded, manually insert operand.
1592-
for (unsigned i = 0; i < Desc.getNumOperands(); i++) {
1593-
if (Desc.operands()[i].OperandType == MCOI::OPERAND_REGISTER) {
1594-
switch (Desc.operands()[i].RegClass) {
1595-
default:
1596-
break;
1597-
case AArch64::MPRRegClassID:
1598-
MI.insert(MI.begin() + i, MCOperand::createReg(AArch64::ZA));
1599-
break;
1600-
case AArch64::MPR8RegClassID:
1601-
MI.insert(MI.begin() + i, MCOperand::createReg(AArch64::ZAB0));
1602-
break;
1603-
case AArch64::ZTRRegClassID:
1604-
MI.insert(MI.begin() + i, MCOperand::createReg(AArch64::ZT0));
1605-
break;
1606-
}
1607-
} else if (Desc.operands()[i].OperandType ==
1608-
AArch64::OPERAND_IMPLICIT_IMM_0) {
1609-
MI.insert(MI.begin() + i, MCOperand::createImm(0));
1581+
DecodeStatus Result =
1582+
decodeInstruction(DecoderTable32, MI, Insn, Address, this, STI);
1583+
if (Result != Success)
1584+
return Result;
1585+
1586+
const MCInstrDesc &Desc = MCII->get(MI.getOpcode());
1587+
1588+
// For Scalable Matrix Extension (SME) instructions that have an implicit
1589+
// operand for the accumulator (ZA) or implicit immediate zero which isn't
1590+
// encoded, manually insert operand.
1591+
for (unsigned i = 0; i < Desc.getNumOperands(); i++) {
1592+
if (Desc.operands()[i].OperandType == MCOI::OPERAND_REGISTER) {
1593+
switch (Desc.operands()[i].RegClass) {
1594+
default:
1595+
break;
1596+
case AArch64::MPRRegClassID:
1597+
MI.insert(MI.begin() + i, MCOperand::createReg(AArch64::ZA));
1598+
break;
1599+
case AArch64::MPR8RegClassID:
1600+
MI.insert(MI.begin() + i, MCOperand::createReg(AArch64::ZAB0));
1601+
break;
1602+
case AArch64::ZTRRegClassID:
1603+
MI.insert(MI.begin() + i, MCOperand::createReg(AArch64::ZT0));
1604+
break;
16101605
}
1606+
} else if (Desc.operands()[i].OperandType ==
1607+
AArch64::OPERAND_IMPLICIT_IMM_0) {
1608+
MI.insert(MI.begin() + i, MCOperand::createImm(0));
16111609
}
1612-
1613-
if (Result != MCDisassembler::Fail)
1614-
return Result;
16151610
}
1616-
1617-
return MCDisassembler::Fail;
1611+
return Success;
16181612
}
16191613

16201614
uint64_t AArch64Disassembler::suggestBytesToSkip(ArrayRef<uint8_t> Bytes,

llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.cpp

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -487,18 +487,6 @@ DecodeStatus AMDGPUDisassembler::tryDecodeInst(const uint8_t *Table, MCInst &MI,
487487
return MCDisassembler::Fail;
488488
}
489489

490-
template <typename InsnType>
491-
DecodeStatus
492-
AMDGPUDisassembler::tryDecodeInst(const uint8_t *Table1, const uint8_t *Table2,
493-
MCInst &MI, InsnType Inst, uint64_t Address,
494-
raw_ostream &Comments) const {
495-
for (const uint8_t *T : {Table1, Table2}) {
496-
if (DecodeStatus Res = tryDecodeInst(T, MI, Inst, Address, Comments))
497-
return Res;
498-
}
499-
return MCDisassembler::Fail;
500-
}
501-
502490
template <typename T> static inline T eatBytes(ArrayRef<uint8_t>& Bytes) {
503491
assert(Bytes.size() >= sizeof(T));
504492
const auto Res =
@@ -617,18 +605,15 @@ DecodeStatus AMDGPUDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
617605
std::bitset<96> DecW = eat12Bytes(Bytes);
618606

619607
if (isGFX11() &&
620-
tryDecodeInst(DecoderTableGFX1196, DecoderTableGFX11_FAKE1696, MI,
621-
DecW, Address, CS))
608+
tryDecodeInst(DecoderTableGFX1196, MI, DecW, Address, CS))
622609
break;
623610

624611
if (isGFX1250() &&
625-
tryDecodeInst(DecoderTableGFX125096, DecoderTableGFX1250_FAKE1696, MI,
626-
DecW, Address, CS))
612+
tryDecodeInst(DecoderTableGFX125096, MI, DecW, Address, CS))
627613
break;
628614

629615
if (isGFX12() &&
630-
tryDecodeInst(DecoderTableGFX1296, DecoderTableGFX12_FAKE1696, MI,
631-
DecW, Address, CS))
616+
tryDecodeInst(DecoderTableGFX1296, MI, DecW, Address, CS))
632617
break;
633618

634619
if (isGFX12() &&
@@ -698,18 +683,13 @@ DecodeStatus AMDGPUDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
698683
break;
699684

700685
if (isGFX1250() &&
701-
tryDecodeInst(DecoderTableGFX125064, DecoderTableGFX1250_FAKE1664, MI,
702-
QW, Address, CS))
686+
tryDecodeInst(DecoderTableGFX125064, MI, QW, Address, CS))
703687
break;
704688

705-
if (isGFX12() &&
706-
tryDecodeInst(DecoderTableGFX1264, DecoderTableGFX12_FAKE1664, MI, QW,
707-
Address, CS))
689+
if (isGFX12() && tryDecodeInst(DecoderTableGFX1264, MI, QW, Address, CS))
708690
break;
709691

710-
if (isGFX11() &&
711-
tryDecodeInst(DecoderTableGFX1164, DecoderTableGFX11_FAKE1664, MI, QW,
712-
Address, CS))
692+
if (isGFX11() && tryDecodeInst(DecoderTableGFX1164, MI, QW, Address, CS))
713693
break;
714694

715695
if (isGFX11() &&
@@ -753,19 +733,14 @@ DecodeStatus AMDGPUDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
753733
if (isGFX10() && tryDecodeInst(DecoderTableGFX1032, MI, DW, Address, CS))
754734
break;
755735

756-
if (isGFX11() &&
757-
tryDecodeInst(DecoderTableGFX1132, DecoderTableGFX11_FAKE1632, MI, DW,
758-
Address, CS))
736+
if (isGFX11() && tryDecodeInst(DecoderTableGFX1132, MI, DW, Address, CS))
759737
break;
760738

761739
if (isGFX1250() &&
762-
tryDecodeInst(DecoderTableGFX125032, DecoderTableGFX1250_FAKE1632, MI,
763-
DW, Address, CS))
740+
tryDecodeInst(DecoderTableGFX125032, MI, DW, Address, CS))
764741
break;
765742

766-
if (isGFX12() &&
767-
tryDecodeInst(DecoderTableGFX1232, DecoderTableGFX12_FAKE1632, MI, DW,
768-
Address, CS))
743+
if (isGFX12() && tryDecodeInst(DecoderTableGFX1232, MI, DW, Address, CS))
769744
break;
770745
}
771746

llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,6 @@ class AMDGPUDisassembler : public MCDisassembler {
7979
template <typename InsnType>
8080
DecodeStatus tryDecodeInst(const uint8_t *Table, MCInst &MI, InsnType Inst,
8181
uint64_t Address, raw_ostream &Comments) const;
82-
template <typename InsnType>
83-
DecodeStatus tryDecodeInst(const uint8_t *Table1, const uint8_t *Table2,
84-
MCInst &MI, InsnType Inst, uint64_t Address,
85-
raw_ostream &Comments) const;
8682

8783
Expected<bool> onSymbolStart(SymbolInfoTy &Symbol, uint64_t &Size,
8884
ArrayRef<uint8_t> Bytes,

llvm/lib/Target/AMDGPU/VINTERPInstructions.td

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,9 @@ defm : VInterpF16Pat<int_amdgcn_interp_p2_rtz_f16,
239239

240240
multiclass VINTERP_Real_gfx11 <bits<7> op, string asmName> {
241241
defvar ps = !cast<VOP3_Pseudo>(NAME);
242-
let AssemblerPredicate = isGFX11Only, DecoderNamespace = "GFX11" #
243-
!if(ps.Pfl.IsRealTrue16, "", "_FAKE16") in {
242+
let AssemblerPredicate = isGFX11Only, DecoderNamespace = "GFX11",
243+
// When decoding, attempt decoding IsRealTrue16 first, then the fake one.
244+
DecodeOrder = !if(ps.Pfl.IsRealTrue16, 0, 1) in {
244245
def _gfx11 :
245246
VINTERP_Real<ps, SIEncodingFamily.GFX11, asmName>,
246247
VINTERPe_gfx11<op, ps.Pfl>;
@@ -249,8 +250,9 @@ multiclass VINTERP_Real_gfx11 <bits<7> op, string asmName> {
249250

250251
multiclass VINTERP_Real_gfx12 <bits<7> op, string asmName> {
251252
defvar ps = !cast<VOP3_Pseudo>(NAME);
252-
let AssemblerPredicate = isGFX12Only, DecoderNamespace = "GFX12" #
253-
!if(ps.Pfl.IsRealTrue16, "", "_FAKE16") in {
253+
let AssemblerPredicate = isGFX12Only, DecoderNamespace = "GFX12",
254+
// When decoding, attempt decoding IsRealTrue16 first, then the fake one.
255+
DecodeOrder = !if(ps.Pfl.IsRealTrue16, 0, 1) in {
254256
def _gfx12 :
255257
VINTERP_Real<ps, SIEncodingFamily.GFX12, asmName>,
256258
VINTERPe_gfx12<op, ps.Pfl>;

llvm/lib/Target/AMDGPU/VOP1Instructions.td

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -940,8 +940,8 @@ multiclass VOP1_Real_e32_with_name<GFXGen Gen, bits<9> op, string opName,
940940
string asmName> {
941941
defvar ps = !cast<VOP1_Pseudo>(opName#"_e32");
942942
let AsmString = asmName # ps.AsmOperands,
943-
DecoderNamespace = Gen.DecoderNamespace #
944-
!if(ps.Pfl.IsRealTrue16, "", "_FAKE16") in {
943+
DecoderNamespace = Gen.DecoderNamespace,
944+
DecodeOrder = !if(ps.Pfl.IsRealTrue16, 0, 1) in {
945945
defm NAME : VOP1_Real_e32<Gen, op, opName>;
946946
}
947947
}
@@ -961,8 +961,8 @@ multiclass VOP1_Real_dpp_with_name<GFXGen Gen, bits<9> op, string opName,
961961
string asmName> {
962962
defvar ps = !cast<VOP1_Pseudo>(opName#"_e32");
963963
let AsmString = asmName # ps.Pfl.AsmDPP16,
964-
DecoderNamespace = Gen.DecoderNamespace #
965-
!if(ps.Pfl.IsRealTrue16, "", "_FAKE16") in {
964+
DecoderNamespace = Gen.DecoderNamespace,
965+
DecodeOrder = !if(ps.Pfl.IsRealTrue16, 0, 1) in {
966966
defm NAME : VOP1_Real_dpp<Gen, op, opName>;
967967
}
968968
}
@@ -977,8 +977,8 @@ multiclass VOP1_Real_dpp8_with_name<GFXGen Gen, bits<9> op, string opName,
977977
string asmName> {
978978
defvar ps = !cast<VOP1_Pseudo>(opName#"_e32");
979979
let AsmString = asmName # ps.Pfl.AsmDPP8,
980-
DecoderNamespace = Gen.DecoderNamespace #
981-
!if(ps.Pfl.IsRealTrue16, "", "_FAKE16") in {
980+
DecoderNamespace = Gen.DecoderNamespace,
981+
DecodeOrder = !if(ps.Pfl.IsRealTrue16, 0, 1) in {
982982
if !not(ps.Pfl.HasExt64BitDPP) then
983983
defm NAME : VOP1_Real_dpp8<Gen, op, opName>;
984984
}

llvm/lib/Target/AMDGPU/VOP2Instructions.td

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ class VOP2_Real_Gen <VOP2_Pseudo ps, GFXGen Gen, string real_name = ps.Mnemonic>
127127
VOP2_Real <ps, Gen.Subtarget, real_name> {
128128
let AssemblerPredicate = Gen.AssemblerPredicate;
129129
let True16Predicate = !if(ps.Pfl.IsRealTrue16, UseRealTrue16Insts, NoTrue16Predicate);
130-
let DecoderNamespace = Gen.DecoderNamespace#
131-
!if(ps.Pfl.IsRealTrue16, "", "_FAKE16");
130+
let DecoderNamespace = Gen.DecoderNamespace;
131+
let DecodeOrder = !if(ps.Pfl.IsRealTrue16, 0, 1);
132132
}
133133

134134
class VOP2_SDWA_Pseudo <string OpName, VOPProfile P, list<dag> pattern=[]> :
@@ -1517,8 +1517,8 @@ class VOP2_DPP16_Gen<bits<6> op, VOP2_DPP_Pseudo ps, GFXGen Gen,
15171517
VOP2_DPP16<op, ps, Gen.Subtarget, opName, p> {
15181518
let AssemblerPredicate = Gen.AssemblerPredicate;
15191519
let True16Predicate = !if(ps.Pfl.IsRealTrue16, UseRealTrue16Insts, NoTrue16Predicate);
1520-
let DecoderNamespace = Gen.DecoderNamespace#
1521-
!if(ps.Pfl.IsRealTrue16, "", "_FAKE16");
1520+
let DecoderNamespace = Gen.DecoderNamespace;
1521+
let DecodeOrder = !if(ps.Pfl.IsRealTrue16, 0, 1);
15221522
}
15231523

15241524
class VOP2_DPP8<bits<6> op, VOP2_Pseudo ps,
@@ -1547,8 +1547,8 @@ class VOP2_DPP8_Gen<bits<6> op, VOP2_Pseudo ps, GFXGen Gen,
15471547
VOP2_DPP8<op, ps, p> {
15481548
let AssemblerPredicate = Gen.AssemblerPredicate;
15491549
let True16Predicate = !if(ps.Pfl.IsRealTrue16, UseRealTrue16Insts, NoTrue16Predicate);
1550-
let DecoderNamespace = Gen.DecoderNamespace#
1551-
!if(ps.Pfl.IsRealTrue16, "", "_FAKE16");
1550+
let DecoderNamespace = Gen.DecoderNamespace;
1551+
let DecodeOrder = !if(ps.Pfl.IsRealTrue16, 0, 1);
15521552
}
15531553

15541554
//===----------------------------------------------------------------------===//

llvm/lib/Target/AMDGPU/VOPCInstructions.td

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ class VOPCInstAlias <VOP3_Pseudo ps, Instruction inst,
310310
let SubtargetPredicate = AssemblerPredicate;
311311

312312
string DecoderNamespace; // dummy
313+
int DecodeOrder; // dummy
313314
}
314315

315316
multiclass VOPCInstAliases <string old_name, string Arch, string real_name = old_name, string mnemonic_from = real_name> {
@@ -1677,7 +1678,8 @@ multiclass VOPC_Real_with_name<GFXGen Gen, bits<9> op, string OpName,
16771678
pseudo_mnemonic),
16781679
asm_name, ps64.AsmVariantName>;
16791680

1680-
let DecoderNamespace = Gen.DecoderNamespace # !if(ps32.Pfl.IsRealTrue16, "", "_FAKE16") in {
1681+
let DecoderNamespace = Gen.DecoderNamespace,
1682+
DecodeOrder = !if(ps32.Pfl.IsRealTrue16, 0, 1) in {
16811683
def _e32#Gen.Suffix :
16821684
// 32 and 64 bit forms of the instruction have _e32 and _e64
16831685
// respectively appended to their assembly mnemonic.
@@ -1753,7 +1755,7 @@ multiclass VOPC_Real_with_name<GFXGen Gen, bits<9> op, string OpName,
17531755
def _e64_dpp8#Gen.Suffix : VOPC64_DPP8_Dst<{0, op}, ps64, asm_name>;
17541756
}
17551757
} // end if ps64.Pfl.HasExtVOP3DPP
1756-
} // End DecoderNamespace
1758+
} // End DecoderOrder
17571759
} // End AssemblerPredicate
17581760
}
17591761

@@ -1824,7 +1826,8 @@ multiclass VOPCX_Real_with_name<GFXGen Gen, bits<9> op, string OpName,
18241826
pseudo_mnemonic),
18251827
asm_name, ps64.AsmVariantName>;
18261828

1827-
let DecoderNamespace = Gen.DecoderNamespace # !if(ps32.Pfl.IsRealTrue16, "", "_FAKE16") in {
1829+
let DecoderNamespace = Gen.DecoderNamespace,
1830+
DecodeOrder = !if(ps32.Pfl.IsRealTrue16, 0, 1) in {
18281831
def _e32#Gen.Suffix
18291832
: VOPC_Real<ps32, Gen.Subtarget, asm_name>,
18301833
VOPCe<op{7-0}> {
@@ -1880,7 +1883,7 @@ multiclass VOPCX_Real_with_name<GFXGen Gen, bits<9> op, string OpName,
18801883
}
18811884
}
18821885
} // End if ps64.Pfl.HasExtVOP3DPP
1883-
} // End DecoderNamespace
1886+
} // End DecodeOrder
18841887
} // End AssemblerPredicate
18851888
}
18861889

0 commit comments

Comments
 (0)