Skip to content

Commit

Permalink
[AMDGPU][NFC] Add True16 operand definitions.
Browse files Browse the repository at this point in the history
Reviewed By: Joe_Nash

Differential Revision: https://reviews.llvm.org/D156103
  • Loading branch information
kosarev committed Sep 25, 2023
1 parent dae7e2d commit 9310baa
Show file tree
Hide file tree
Showing 11 changed files with 257 additions and 29 deletions.
36 changes: 36 additions & 0 deletions llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ class AMDGPUOperand : public MCParsedAsmOperand {
return isRegOrImmWithInputMods(AMDGPU::VS_32RegClassID, MVT::i16);
}

bool isRegOrImmWithIntT16InputMods() const {
return isRegOrImmWithInputMods(AMDGPU::VS_16RegClassID, MVT::i16);
}

bool isRegOrImmWithInt32InputMods() const {
return isRegOrImmWithInputMods(AMDGPU::VS_32RegClassID, MVT::i32);
}
Expand All @@ -293,6 +297,10 @@ class AMDGPUOperand : public MCParsedAsmOperand {
return isRegOrImmWithInputMods(AMDGPU::VS_32RegClassID, MVT::f16);
}

bool isRegOrImmWithFPT16InputMods() const {
return isRegOrImmWithInputMods(AMDGPU::VS_16RegClassID, MVT::f16);
}

bool isRegOrImmWithFP32InputMods() const {
return isRegOrImmWithInputMods(AMDGPU::VS_32RegClassID, MVT::f32);
}
Expand Down Expand Up @@ -512,7 +520,15 @@ class AMDGPUOperand : public MCParsedAsmOperand {
return isRegOrInlineNoMods(AMDGPU::VS_64RegClassID, MVT::i64);
}

bool isVCSrcTB16() const {
return isRegOrInlineNoMods(AMDGPU::VS_16RegClassID, MVT::i16);
}

bool isVCSrcTB16_Lo128() const {
return isRegOrInlineNoMods(AMDGPU::VS_16_Lo128RegClassID, MVT::i16);
}

bool isVCSrcFake16B16_Lo128() const {
return isRegOrInlineNoMods(AMDGPU::VS_32_Lo128RegClassID, MVT::i16);
}

Expand All @@ -532,7 +548,15 @@ class AMDGPUOperand : public MCParsedAsmOperand {
return isRegOrInlineNoMods(AMDGPU::VS_64RegClassID, MVT::f64);
}

bool isVCSrcTF16() const {
return isRegOrInlineNoMods(AMDGPU::VS_16RegClassID, MVT::f16);
}

bool isVCSrcTF16_Lo128() const {
return isRegOrInlineNoMods(AMDGPU::VS_16_Lo128RegClassID, MVT::f16);
}

bool isVCSrcFake16F16_Lo128() const {
return isRegOrInlineNoMods(AMDGPU::VS_32_Lo128RegClassID, MVT::f16);
}

Expand All @@ -552,10 +576,16 @@ class AMDGPUOperand : public MCParsedAsmOperand {
return isVCSrcF64() || isLiteralImm(MVT::i64);
}

bool isVSrcTB16() const { return isVCSrcTB16() || isLiteralImm(MVT::i16); }

bool isVSrcTB16_Lo128() const {
return isVCSrcTB16_Lo128() || isLiteralImm(MVT::i16);
}

bool isVSrcFake16B16_Lo128() const {
return isVCSrcFake16B16_Lo128() || isLiteralImm(MVT::i16);
}

bool isVSrcB16() const {
return isVCSrcB16() || isLiteralImm(MVT::i16);
}
Expand Down Expand Up @@ -588,10 +618,16 @@ class AMDGPUOperand : public MCParsedAsmOperand {
return isVCSrcF64() || isLiteralImm(MVT::f64);
}

bool isVSrcTF16() const { return isVCSrcTF16() || isLiteralImm(MVT::f16); }

bool isVSrcTF16_Lo128() const {
return isVCSrcTF16_Lo128() || isLiteralImm(MVT::f16);
}

bool isVSrcFake16F16_Lo128() const {
return isVCSrcFake16F16_Lo128() || isLiteralImm(MVT::f16);
}

bool isVSrcF16() const {
return isVCSrcF16() || isLiteralImm(MVT::f16);
}
Expand Down
74 changes: 74 additions & 0 deletions llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,61 @@ DECODE_OPERAND_SRC_REG_OR_IMM_DEFERRED_9(VS_32, OPW16, 16)
DECODE_OPERAND_SRC_REG_OR_IMM_DEFERRED_9(VS_32, OPW32, 32)
DECODE_OPERAND_SRC_REG_OR_IMM_DEFERRED_9(SReg_32, OPW32, 32)

static DecodeStatus DecodeVGPR_16RegisterClass(MCInst &Inst, unsigned Imm,
uint64_t /*Addr*/,
const MCDisassembler *Decoder) {
assert(isUInt<10>(Imm) && "10-bit encoding expected");
assert((Imm & (1 << 8)) == 0 && "Imm{8} should not be used");

bool IsHi = Imm & (1 << 9);
unsigned RegIdx = Imm & 0xff;
auto DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
return addOperand(Inst, DAsm->createVGPR16Operand(RegIdx, IsHi));
}

static DecodeStatus
DecodeVGPR_16_Lo128RegisterClass(MCInst &Inst, unsigned Imm, uint64_t /*Addr*/,
const MCDisassembler *Decoder) {
assert(isUInt<8>(Imm) && "8-bit encoding expected");

bool IsHi = Imm & (1 << 7);
unsigned RegIdx = Imm & 0x7f;
auto DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
return addOperand(Inst, DAsm->createVGPR16Operand(RegIdx, IsHi));
}

static DecodeStatus decodeOperand_VSrcT16_Lo128(MCInst &Inst, unsigned Imm,
uint64_t /*Addr*/,
const MCDisassembler *Decoder) {
assert(isUInt<9>(Imm) && "9-bit encoding expected");

const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
bool IsVGPR = Imm & (1 << 8);
if (IsVGPR) {
bool IsHi = Imm & (1 << 7);
unsigned RegIdx = Imm & 0x7f;
return addOperand(Inst, DAsm->createVGPR16Operand(RegIdx, IsHi));
}
return addOperand(Inst, DAsm->decodeNonVGPRSrcOp(AMDGPUDisassembler::OPW16,
Imm & 0xFF, false, 16));
}

static DecodeStatus decodeOperand_VSrcT16(MCInst &Inst, unsigned Imm,
uint64_t /*Addr*/,
const MCDisassembler *Decoder) {
assert(isUInt<10>(Imm) && "10-bit encoding expected");

const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
bool IsVGPR = Imm & (1 << 8);
if (IsVGPR) {
bool IsHi = Imm & (1 << 9);
unsigned RegIdx = Imm & 0xff;
return addOperand(Inst, DAsm->createVGPR16Operand(RegIdx, IsHi));
}
return addOperand(Inst, DAsm->decodeNonVGPRSrcOp(AMDGPUDisassembler::OPW16,
Imm & 0xFF, false, 16));
}

static DecodeStatus decodeOperand_KImmFP(MCInst &Inst, unsigned Imm,
uint64_t Addr,
const MCDisassembler *Decoder) {
Expand Down Expand Up @@ -1141,6 +1196,13 @@ MCOperand AMDGPUDisassembler::createSRegOperand(unsigned SRegClassID,
return createRegOperand(SRegClassID, Val >> shift);
}

MCOperand AMDGPUDisassembler::createVGPR16Operand(unsigned RegIdx,
bool IsHi) const {
unsigned RCID =
IsHi ? AMDGPU::VGPR_HI16RegClassID : AMDGPU::VGPR_LO16RegClassID;
return createRegOperand(RCID, RegIdx);
}

// Decode Literals for insts which always have a literal in the encoding
MCOperand
AMDGPUDisassembler::decodeMandatoryLiteralConstant(unsigned Val) const {
Expand Down Expand Up @@ -1397,6 +1459,18 @@ MCOperand AMDGPUDisassembler::decodeSrcOp(const OpWidthTy Width, unsigned Val,
return createRegOperand(IsAGPR ? getAgprClassId(Width)
: getVgprClassId(Width), Val - VGPR_MIN);
}
return decodeNonVGPRSrcOp(Width, Val & 0xFF, MandatoryLiteral, ImmWidth);
}

MCOperand AMDGPUDisassembler::decodeNonVGPRSrcOp(const OpWidthTy Width,
unsigned Val,
bool MandatoryLiteral,
unsigned ImmWidth) const {
// Cases when Val{8} is 1 (vgpr, agpr or true 16 vgpr) should have been
// decoded earlier.
assert(Val < (1 << 8) && "9-bit Src encoding when Val{8} is 0");
using namespace AMDGPU::EncValues;

if (Val <= SGPR_MAX) {
// "SGPR_MIN <= Val" is always true and causes compilation warning.
static_assert(SGPR_MIN == 0);
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class AMDGPUDisassembler : public MCDisassembler {
MCOperand createRegOperand(unsigned int RegId) const;
MCOperand createRegOperand(unsigned RegClassID, unsigned Val) const;
MCOperand createSRegOperand(unsigned SRegClassID, unsigned Val) const;
MCOperand createVGPR16Operand(unsigned RegIdx, bool IsHi) const;

MCOperand errOperand(unsigned V, const Twine& ErrMsg) const;

Expand Down Expand Up @@ -234,6 +235,10 @@ class AMDGPUDisassembler : public MCDisassembler {
bool MandatoryLiteral = false,
unsigned ImmWidth = 0) const;

MCOperand decodeNonVGPRSrcOp(const OpWidthTy Width, unsigned Val,
bool MandatoryLiteral = false,
unsigned ImmWidth = 0) const;

MCOperand decodeVOPDDstYOp(MCInst &Inst, unsigned Val) const;
MCOperand decodeSpecialReg32(unsigned Val) const;
MCOperand decodeSpecialReg64(unsigned Val) const;
Expand Down
30 changes: 30 additions & 0 deletions llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUMCCodeEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ class AMDGPUMCCodeEmitter : public MCCodeEmitter {
SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI) const;

void getMachineOpValueT16(const MCInst &MI, unsigned OpNo, APInt &Op,
SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI) const;

void getMachineOpValueT16Lo128(const MCInst &MI, unsigned OpNo, APInt &Op,
SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI) const;

/// Use a fixup to encode the simm16 field for SOPP branch
/// instructions.
void getSOPPBrEncoding(const MCInst &MI, unsigned OpNo, APInt &Op,
Expand Down Expand Up @@ -547,6 +555,28 @@ void AMDGPUMCCodeEmitter::getMachineOpValue(const MCInst &MI,
getMachineOpValueCommon(MI, MO, OpNo, Op, Fixups, STI);
}

void AMDGPUMCCodeEmitter::getMachineOpValueT16(
const MCInst &MI, unsigned OpNo, APInt &Op,
SmallVectorImpl<MCFixup> &Fixups, const MCSubtargetInfo &STI) const {
llvm_unreachable("TODO: Implement getMachineOpValueT16().");
}

void AMDGPUMCCodeEmitter::getMachineOpValueT16Lo128(
const MCInst &MI, unsigned OpNo, APInt &Op,
SmallVectorImpl<MCFixup> &Fixups, const MCSubtargetInfo &STI) const {
const MCOperand &MO = MI.getOperand(OpNo);
if (MO.isReg()) {
uint16_t Encoding = MRI.getEncodingValue(MO.getReg());
unsigned RegIdx = Encoding & AMDGPU::EncValues::REG_IDX_MASK;
bool IsHi = Encoding & AMDGPU::EncValues::IS_HI;
bool IsVGPR = Encoding & AMDGPU::EncValues::IS_VGPR;
assert((!IsVGPR || isUInt<7>(RegIdx)) && "VGPR0-VGPR127 expected!");
Op = (IsVGPR ? 0x100 : 0) | (IsHi ? 0x80 : 0) | RegIdx;
return;
}
getMachineOpValueCommon(MI, MO, OpNo, Op, Fixups, STI);
}

void AMDGPUMCCodeEmitter::getMachineOpValueCommon(
const MCInst &MI, const MCOperand &MO, unsigned OpNo, APInt &Op,
SmallVectorImpl<MCFixup> &Fixups, const MCSubtargetInfo &STI) const {
Expand Down
4 changes: 3 additions & 1 deletion llvm/lib/Target/AMDGPU/SIDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ namespace AMDGPU {
namespace EncValues { // Encoding values of enum9/8/7 operands

enum : unsigned {
REG_IDX_MASK = 255,
SGPR_MIN = 0,
SGPR_MAX_SI = 101,
SGPR_MAX_GFX10 = 105,
Expand All @@ -329,7 +330,8 @@ enum : unsigned {
LITERAL_CONST = 255,
VGPR_MIN = 256,
VGPR_MAX = 511,
IS_VGPR = 256 // Indicates VGPR or AGPR
IS_VGPR = 256, // Indicates VGPR or AGPR
IS_HI = 512, // High 16-bit register.
};

} // namespace EncValues
Expand Down
10 changes: 10 additions & 0 deletions llvm/lib/Target/AMDGPU/SIInstrFormats.td
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,16 @@ def CPolBit {

class VOPDstOperand <RegisterClass rc> : RegisterOperand <rc, "printVOPDst">;

def VOPDstOperand_t16 : VOPDstOperand <VGPR_16> {
let EncoderMethod = "getMachineOpValueT16";
let DecoderMethod = "DecodeVGPR_16RegisterClass";
}

def VOPDstOperand_t16Lo128 : VOPDstOperand <VGPR_16_Lo128> {
let EncoderMethod = "getMachineOpValueT16Lo128";
let DecoderMethod = "DecodeVGPR_16_Lo128RegisterClass";
}

class VINTRPe <bits<2> op> : Enc32 {
bits<8> vdst;
bits<8> vsrc;
Expand Down
Loading

4 comments on commit 9310baa

@kazutakahirata
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This patch breaks the -Werror build with clang-14.0.6 because the functions you are adding to llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.cpp, such as DecodeVGPR_16RegisterClass and DecodeVGPR_16_Lo128RegisterClass, are not used anywhere.

I could remove them, but I am wondering if you are planning to use them in near future. I'm happy to add [[maybe_unused]] if you are planning to use them in near future. Alternatively, I could remove them. Please let me know. Thanks!

@kosarev
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kazutakahirata Hi, I'm going to submit more patches today that will make them be used, but if it's very unrgent, [[maybe_unused]] would do -- thanks!

@kosarev
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kazutakahirata It seems submitting https://reviews.llvm.org/D156104 fixed this.

@kazutakahirata
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked in a patch to add [[maybe_used]] but already reverted it. Thanks!

Please sign in to comment.