Skip to content

[TableGen][DecoderEmitter] Add option to emit type-specialized decodeToMCInst #146593

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions llvm/include/llvm/TableGen/Record.h
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,8 @@ class ListInit final : public TypedInit,
size_t size() const { return NumElements; }
bool empty() const { return NumElements == 0; }

std::vector<int64_t> getAsListOfInts() const;

const Init *getBit(unsigned Bit) const override {
llvm_unreachable("Illegal bit reference off list");
}
Expand Down
20 changes: 20 additions & 0 deletions llvm/include/llvm/Target/Target.td
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,15 @@ class OptionalDefOperand<ValueType ty, dag OpTypes, dag defaultops>
let MIOperandInfo = OpTypes;
}

// InstrDecoderOption - This class is used to provide some options to the
// TableGen DecoderEmitter backend.
class InstrDecoderOption<string ty, list<int> bws> {
string CPPType = ty; // C++ type for generating non-templated code.
list<int> Bitwidths = bws; // List of bitwidths supported by the above type.

assert !not(!empty(CPPType)), "CPP type cannot be empty";
assert !not(!empty(Bitwidths)), "Bitwidths cannot be empty";
}

// InstrInfo - This class should only be instantiated once to provide parameters
// which are global to the target machine.
Expand All @@ -1158,6 +1167,17 @@ class InstrInfo {
//
// This option is a temporary migration help. It will go away.
bit guessInstructionProperties = true;

// This is a list of instruction decoder options for this target. When non
// empty, it should list all the C++ types and associated bitwidths that this
// target intends to use to call the TableGen generated `decodeInstruction`
// function. If this list is empty, the decoder emitter will generate
// templated code. However, if a target intends to call this function with
// more than one `InsnType`, it may be beneficial to provide these decoder
// options to generate non-templated form of `decodeInstruction` and
// associated helper functions and avoid some code duplication in the
// `decodeToMCInst` function.
list<InstrDecoderOption> DecoderOptions = [];
}

// Standard Pseudo Instructions.
Expand Down
43 changes: 23 additions & 20 deletions llvm/lib/TableGen/Record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,15 @@ std::string ListInit::getAsString() const {
return Result + "]";
}

std::vector<int64_t> ListInit::getAsListOfInts() const {
if (!isa<IntRecTy>(getElementType()))
PrintFatalError("List does not contain integer values");
std::vector<int64_t> Ints;
for (const Init *I : getElements())
Ints.push_back(cast<IntInit>(I)->getValue());
return Ints;
}

const Init *OpInit::getBit(unsigned Bit) const {
if (getType() == BitRecTy::get(getRecordKeeper()))
return this;
Expand Down Expand Up @@ -3119,32 +3128,26 @@ int64_t Record::getValueAsInt(StringRef FieldName) const {
std::vector<int64_t>
Record::getValueAsListOfInts(StringRef FieldName) const {
const ListInit *List = getValueAsListInit(FieldName);
std::vector<int64_t> Ints;
for (const Init *I : List->getElements()) {
if (const auto *II = dyn_cast<IntInit>(I))
Ints.push_back(II->getValue());
else
PrintFatalError(getLoc(),
Twine("Record `") + getName() + "', field `" + FieldName +
"' exists but does not have a list of ints value: " +
I->getAsString());
}
return Ints;
if (!isa<IntRecTy>(List->getElementType()))
PrintFatalError(getLoc(),
Twine("Record `") + getName() + "', field `" + FieldName +
"' exists but does not have a list of ints value: " +
List->getAsString());
return List->getAsListOfInts();
}

std::vector<StringRef>
Record::getValueAsListOfStrings(StringRef FieldName) const {
const ListInit *List = getValueAsListInit(FieldName);
if (!isa<StringRecTy>(List->getElementType()))
PrintFatalError(getLoc(),
Twine("Record `") + getName() + "', field `" + FieldName +
"' exists but does not have a list of string value: " +
List->getAsString());

std::vector<StringRef> Strings;
for (const Init *I : List->getElements()) {
if (const auto *SI = dyn_cast<StringInit>(I))
Strings.push_back(SI->getValue());
else
PrintFatalError(getLoc(),
Twine("Record `") + getName() + "', field `" + FieldName +
"' exists but does not have a list of strings value: " +
I->getAsString());
}
for (const Init *I : List->getElements())
Strings.push_back(cast<StringInit>(I)->getValue());
return Strings;
}

Expand Down
7 changes: 7 additions & 0 deletions llvm/lib/Target/AMDGPU/AMDGPU.td
Original file line number Diff line number Diff line change
Expand Up @@ -1973,6 +1973,13 @@ def FeatureISAVersion12_Generic: FeatureSet<

def AMDGPUInstrInfo : InstrInfo {
let guessInstructionProperties = 1;

// Opt-in into non-templated code for instruction decoder.
let DecoderOptions = [
InstrDecoderOption<"uint32_t", [32]>,
InstrDecoderOption<"uint64_t", [64]>,
InstrDecoderOption<"DecoderUInt128", [96, 128]>,
];
}

def AMDGPUAsmParser : AsmParser {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ DecodeStatus AMDGPUDisassembler::getInstruction(MCInst &MI, uint64_t &Size,

// Try to decode DPP and SDWA first to solve conflict with VOP1 and VOP2
// encodings
if (isGFX11Plus() && Bytes.size() >= 12 ) {
if (isGFX11Plus() && Bytes.size() >= 12) {
DecoderUInt128 DecW = eat12Bytes(Bytes);

if (isGFX11() &&
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/M68k/Disassembler/M68kDisassembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ static DecodeStatus DecodeFPCSCRegisterClass(MCInst &Inst, uint64_t RegNo,
}
#define DecodeFPICRegisterClass DecodeFPCSCRegisterClass

static DecodeStatus DecodeCCRCRegisterClass(MCInst &Inst, APInt &Insn,
static DecodeStatus DecodeCCRCRegisterClass(MCInst &Inst, const APInt &Insn,
uint64_t Address,
const void *Decoder) {
llvm_unreachable("unimplemented");
}

static DecodeStatus DecodeSRCRegisterClass(MCInst &Inst, APInt &Insn,
static DecodeStatus DecodeSRCRegisterClass(MCInst &Inst, const APInt &Insn,
uint64_t Address,
const void *Decoder) {
llvm_unreachable("unimplemented");
Expand Down
10 changes: 5 additions & 5 deletions llvm/test/TableGen/DecoderEmitterFnTable.td
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ def Inst3 : TestInstruction {
let AsmString = "Inst3";
}

// CHECK-LABEL: DecodeStatus decodeFn0(DecodeStatus S, InsnType insn, MCInst &MI, uint64_t Address, const MCDisassembler *Decoder, bool &DecodeComplete)
// CHECK-LABEL: DecodeStatus decodeFn1(DecodeStatus S, InsnType insn, MCInst &MI, uint64_t Address, const MCDisassembler *Decoder, bool &DecodeComplete)
// CHECK-LABEL: DecodeStatus decodeFn2(DecodeStatus S, InsnType insn, MCInst &MI, uint64_t Address, const MCDisassembler *Decoder, bool &DecodeComplete)
// CHECK-LABEL: DecodeStatus decodeFn3(DecodeStatus S, InsnType insn, MCInst &MI, uint64_t Address, const MCDisassembler *Decoder, bool &DecodeComplete)
// CHECK-LABEL: decodeToMCInst(unsigned Idx, DecodeStatus S, InsnType insn, MCInst &MI, uint64_t Address, const MCDisassembler *Decoder, bool &DecodeComplete)
// CHECK-LABEL: DecodeStatus decodeFn0(DecodeStatus S, const InsnType &insn, MCInst &MI, uint64_t Address, const MCDisassembler *Decoder, bool &DecodeComplete)
// CHECK-LABEL: DecodeStatus decodeFn1(DecodeStatus S, const InsnType &insn, MCInst &MI, uint64_t Address, const MCDisassembler *Decoder, bool &DecodeComplete)
// CHECK-LABEL: DecodeStatus decodeFn2(DecodeStatus S, const InsnType &insn, MCInst &MI, uint64_t Address, const MCDisassembler *Decoder, bool &DecodeComplete)
// CHECK-LABEL: DecodeStatus decodeFn3(DecodeStatus S, const InsnType &insn, MCInst &MI, uint64_t Address, const MCDisassembler *Decoder, bool &DecodeComplete)
// CHECK-LABEL: decodeToMCInst(unsigned Idx, DecodeStatus S, const InsnType &insn, MCInst &MI, uint64_t Address, const MCDisassembler *Decoder, bool &DecodeComplete)
// CHECK: static constexpr DecodeFnTy decodeFnTable[]
// CHECK-NEXT: decodeFn0,
// CHECK-NEXT: decodeFn1,
Expand Down
13 changes: 8 additions & 5 deletions llvm/test/TableGen/VarLenDecoder.td
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ def FOO32 : MyVarInst<MemOp32> {
);
}

// Instruction length table
// CHECK-LABEL: InstrLenTable
// CHECK: 27,
// CHECK-NEXT: 43,
// CHECK-NEXT: };

// CHECK-SMALL: /* 0 */ MCD::OPC_ExtractField, 3, 5, // Inst{7-3} ...
// CHECK-SMALL-NEXT: /* 3 */ MCD::OPC_FilterValue, 8, 4, 0, // Skip to: 11
// CHECK-SMALL-NEXT: /* 7 */ MCD::OPC_Decode, {{[0-9]+}}, {{[0-9]+}}, 0, // Opcode: FOO16
Expand All @@ -61,11 +67,7 @@ def FOO32 : MyVarInst<MemOp32> {
// CHECK-LARGE-NEXT: /* 14 */ MCD::OPC_Decode, {{[0-9]+}}, {{[0-9]+}}, 1, // Opcode: FOO32
// CHECK-LARGE-NEXT: /* 18 */ MCD::OPC_Fail,

// Instruction length table
// CHECK: 27,
// CHECK-NEXT: 43,
// CHECK-NEXT: };

// CHECK-LABEL: decodeToMCInst
// CHECK: case 0:
// CHECK-NEXT: tmp = fieldFromInstruction(insn, 8, 3);
// CHECK-NEXT: if (!Check(S, DecodeRegClassRegisterClass(MI, tmp, Address, Decoder))) { return MCDisassembler::Fail; }
Expand All @@ -85,6 +87,7 @@ def FOO32 : MyVarInst<MemOp32> {
// CHECK-NEXT: MI.addOperand(MCOperand::createImm(tmp));
// CHECK-NEXT: return S;

// CHECK-LABEL: decodeInstruction
// CHECK-LABEL: case MCD::OPC_ExtractField: {
// CHECK: makeUp(insn, Start + Len);

Expand Down
20 changes: 11 additions & 9 deletions llvm/test/TableGen/trydecode-emission.td
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ def InstB : TestInstruction {
let hasCompleteDecoder = 0;
}

// CHECK-LABEL: decodeNumToSkip
// CHECK-NEXT: unsigned NumToSkip = *Ptr++;
// CHECK-NEXT: NumToSkip |= (*Ptr++) << 8;
// CHECK-NEXT: return NumToSkip;

// CHECK-LARGE-LABEL: decodeNumToSkip
// CHECK-LARGE-NEXT: unsigned NumToSkip = *Ptr++;
// CHECK-LARGE-NEXT: NumToSkip |= (*Ptr++) << 8;
// CHECK-LARGE-NEXT: NumToSkip |= (*Ptr++) << 16;
// CHECK-LARGE-NEXT: return NumToSkip;

// CHECK: /* 0 */ MCD::OPC_ExtractField, 4, 4, // Inst{7-4} ...
// CHECK-NEXT: /* 3 */ MCD::OPC_FilterValueOrFail, 0,
// CHECK-NEXT: /* 5 */ MCD::OPC_CheckField, 2, 2, 0, 6, 0, // Skip to: 17
Expand All @@ -43,10 +54,6 @@ def InstB : TestInstruction {

// CHECK: if (!Check(S, DecodeInstB(MI, insn, Address, Decoder))) { DecodeComplete = false; return MCDisassembler::Fail; }

// CHECK: unsigned NumToSkip = *Ptr++;
// CHECK-NEXT: NumToSkip |= (*Ptr++) << 8;
// CHECK-NEXT: return NumToSkip;

// CHECK-LARGE: /* 0 */ MCD::OPC_ExtractField, 4, 4, // Inst{7-4} ...
// CHECK-LARGE-NEXT: /* 3 */ MCD::OPC_FilterValueOrFail, 0,
// CHECK-LARGE-NEXT: /* 5 */ MCD::OPC_CheckField, 2, 2, 0, 7, 0, 0, // Skip to: 19
Expand All @@ -55,8 +62,3 @@ def InstB : TestInstruction {
// CHECK-LARGE-NEXT: /* 23 */ MCD::OPC_Fail,

// CHECK-LARGE: if (!Check(S, DecodeInstB(MI, insn, Address, Decoder))) { DecodeComplete = false; return MCDisassembler::Fail; }

// CHECK-LARGE: unsigned NumToSkip = *Ptr++;
// CHECK-LARGE-NEXT: NumToSkip |= (*Ptr++) << 8;
// CHECK-LARGE-NEXT: NumToSkip |= (*Ptr++) << 16;
// CHECK-LARGE-NEXT: return NumToSkip;
Loading