LLVM 24.0.0git
AMDGPUDisassembler.cpp
Go to the documentation of this file.
1//===- AMDGPUDisassembler.cpp - Disassembler for AMDGPU ISA ---------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9//===----------------------------------------------------------------------===//
10//
11/// \file
12///
13/// This file contains definition for AMDGPU ISA disassembler
14//
15//===----------------------------------------------------------------------===//
16
17// ToDo: What to do with instruction suffixes (v_mov_b32 vs v_mov_b32_e32)?
18
22#include "SIDefines.h"
23#include "SIRegisterInfo.h"
29#include "llvm/MC/MCAsmInfo.h"
30#include "llvm/MC/MCContext.h"
31#include "llvm/MC/MCDecoder.h"
33#include "llvm/MC/MCExpr.h"
34#include "llvm/MC/MCInstrDesc.h"
40
41using namespace llvm;
42using namespace llvm::MCD;
43
44#define DEBUG_TYPE "amdgpu-disassembler"
45
46#define SGPR_MAX \
47 (isGFX10Plus() ? AMDGPU::EncValues::SGPR_MAX_GFX10 \
48 : AMDGPU::EncValues::SGPR_MAX_SI)
49
51
52static int64_t getInlineImmValF16(unsigned Imm);
53static int64_t getInlineImmValBF16(unsigned Imm);
54static int64_t getInlineImmVal32(unsigned Imm);
55static int64_t getInlineImmVal64(unsigned Imm);
56
58 MCContext &Ctx, MCInstrInfo const *MCII)
59 : MCDisassembler(STI, Ctx), MCII(MCII), MRI(*Ctx.getRegisterInfo()),
60 MAI(Ctx.getAsmInfo()),
61 HwModeRegClass(STI.getHwMode(MCSubtargetInfo::HwMode_RegInfo)),
62 TargetMaxInstBytes(MAI.getMaxInstLength(&STI)),
63 TargetID(AMDGPU::createAMDGPUTargetID(STI, "")),
64 CodeObjectVersion(AMDGPU::getDefaultAMDHSACodeObjectVersion()) {
65 // ToDo: AMDGPUDisassembler supports only VI ISA.
66 if (!STI.hasFeature(AMDGPU::FeatureGCN3Encoding) && !isGFX10Plus())
67 reportFatalUsageError("disassembly not yet supported for subtarget");
68
69 for (auto [Symbol, Code] : AMDGPU::UCVersion::getGFXVersions())
70 createConstantSymbolExpr(Symbol, Code);
71
72 UCVersionW64Expr = createConstantSymbolExpr("UC_VERSION_W64_BIT", 0x2000);
73 UCVersionW32Expr = createConstantSymbolExpr("UC_VERSION_W32_BIT", 0x4000);
74 UCVersionMDPExpr = createConstantSymbolExpr("UC_VERSION_MDP_BIT", 0x8000);
75}
76
80
82 unsigned EFlags) const {
83 OS << "\t.amdgcn_target \""
84 << STI.getTargetTriple().normalize(Triple::CanonicalForm::FOUR_IDENT)
85 << '-';
86
87 // Get CPU name from ELF e_flags MACH field
88 unsigned MACH = EFlags & ELF::EF_AMDGPU_MACH;
89
90#define X(NUM, ENUM, NAME) \
91 case ELF::ENUM: \
92 OS << NAME; \
93 break;
94 switch (MACH) {
96 default:
97 OS << "unknown";
98 break;
99 }
100#undef X
101
102 // Add xnack and sramecc from ELF flags (v4 format)
103 if (CodeObjectVersion >= AMDGPU::AMDHSA_COV4) {
104 // Hardwired-on features are not selectable target-ID modifiers.
105 bool SramEccHardwiredOn = TargetID.isSramEccSupported() &&
106 !STI.hasFeature(AMDGPU::FeatureSRAMECCOnOffModes);
107 unsigned SrameccSetting = EFlags & ELF::EF_AMDGPU_FEATURE_SRAMECC_V4;
108 switch (SrameccSetting) {
110 break;
112 TargetID.setSramEccSetting(AMDGPU::TargetIDSetting::Any);
113 break;
115 TargetID.setSramEccSetting(AMDGPU::TargetIDSetting::Off);
116 if (!SramEccHardwiredOn)
117 OS << ":sramecc-";
118 break;
120 TargetID.setSramEccSetting(AMDGPU::TargetIDSetting::On);
121 if (!SramEccHardwiredOn)
122 OS << ":sramecc+";
123 break;
124 }
125
126 // Targets that hardwire xnack on (e.g. gfx1250) don't expose it as a
127 // selectable modifier, so don't print it.
128 bool XnackHardwiredOn = TargetID.isXnackSupported() &&
129 !STI.hasFeature(AMDGPU::FeatureXNACKOnOffModes);
130 unsigned XnackSetting = EFlags & ELF::EF_AMDGPU_FEATURE_XNACK_V4;
131 switch (XnackSetting) {
133 break;
135 TargetID.setXnackSetting(AMDGPU::TargetIDSetting::Any);
136 break;
138 TargetID.setXnackSetting(AMDGPU::TargetIDSetting::Off);
139 if (!XnackHardwiredOn)
140 OS << ":xnack-";
141 break;
143 TargetID.setXnackSetting(AMDGPU::TargetIDSetting::On);
144 if (!XnackHardwiredOn)
145 OS << ":xnack+";
146 break;
147 }
148 }
149
150 OS << "\"\n";
151}
152
154addOperand(MCInst &Inst, const MCOperand& Opnd) {
155 Inst.addOperand(Opnd);
156 return Opnd.isValid() ?
159}
160
162 AMDGPU::OpName Name) {
163 int OpIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), Name);
164 if (OpIdx != -1) {
165 auto *I = MI.begin();
166 std::advance(I, OpIdx);
167 MI.insert(I, Op);
168 }
169 return OpIdx;
170}
171
173 uint64_t Addr,
174 const MCDisassembler *Decoder) {
175 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
176
177 // Our branches take a simm16.
178 int64_t Offset = SignExtend64<16>(Imm) * 4 + 4 + Addr;
179
180 if (DAsm->tryAddingSymbolicOperand(Inst, Offset, Addr, true, 2, 2, 0))
182 return addOperand(Inst, MCOperand::createImm(Imm));
183}
184
186 const MCDisassembler *Decoder) {
187 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
188 int64_t Offset;
189 if (DAsm->isGFX12Plus()) { // GFX12 supports 24-bit signed offsets.
191 } else if (DAsm->isVI()) { // VI supports 20-bit unsigned offsets.
192 Offset = Imm & 0xFFFFF;
193 } else { // GFX9+ supports 21-bit signed offsets.
195 }
197}
198
200 uint64_t Addr,
201 const MCDisassembler *Decoder) {
202 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
203
204 // Signed 24-bit byte offset from the next instruction. The encoding is 8
205 // bytes, so the target address is PC + 8 + offset.
206 int64_t Target = SignExtend64<24>(Imm) + 8 + Addr;
207 if (DAsm->tryAddingSymbolicOperand(Inst, Target, Addr, /*IsBranch=*/true,
208 /*Offset=*/4, /*OpSize=*/3,
209 /*InstSize=*/8))
212}
213
215 const MCDisassembler *Decoder) {
216 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
217 return addOperand(Inst,
218 DAsm->decodeBoolReg(Inst, static_cast<unsigned>(Val)));
219}
220
222 uint64_t Addr,
223 const MCDisassembler *Decoder) {
224 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
225 return addOperand(Inst,
226 DAsm->decodeSplitBarrier(Inst, static_cast<unsigned>(Val)));
227}
228
230 const MCDisassembler *Decoder) {
231 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
232 return addOperand(Inst, DAsm->decodeDpp8FI(static_cast<unsigned>(Val)));
233}
234
235#define DECODE_OPERAND(StaticDecoderName, DecoderName) \
236 static DecodeStatus StaticDecoderName(MCInst &Inst, uint64_t Imm, \
237 uint64_t /*Addr*/, \
238 const MCDisassembler *Decoder) { \
239 auto DAsm = static_cast<const AMDGPUDisassembler *>(Decoder); \
240 return addOperand(Inst, DAsm->DecoderName(static_cast<unsigned>(Imm))); \
241 }
242
243// Decoder for registers, decode directly using RegClassID. Imm(8-bit) is
244// number of register. Used by VGPR only and AGPR only operands.
245#define DECODE_OPERAND_REG_8(RegClass) \
246 static DecodeStatus Decode##RegClass##RegisterClass( \
247 MCInst &Inst, uint64_t Imm, uint64_t /*Addr*/, \
248 const MCDisassembler *Decoder) { \
249 assert(Imm < (1 << 8) && "8-bit encoding"); \
250 auto DAsm = static_cast<const AMDGPUDisassembler *>(Decoder); \
251 return addOperand(Inst, \
252 DAsm->createRegOperand(AMDGPU::RegClass##RegClassID, \
253 static_cast<unsigned>(Imm))); \
254 }
255
256#define DECODE_SrcOp(Name, EncSize, OpWidth, EncImm) \
257 static DecodeStatus Name(MCInst &Inst, uint64_t Imm, uint64_t /*Addr*/, \
258 const MCDisassembler *Decoder) { \
259 if (!isUInt<EncSize>(Imm)) \
260 return MCDisassembler::Fail; \
261 auto DAsm = static_cast<const AMDGPUDisassembler *>(Decoder); \
262 return addOperand(Inst, DAsm->decodeSrcOp(Inst, OpWidth, \
263 static_cast<unsigned>(EncImm))); \
264 }
265
266static DecodeStatus decodeSrcOp(MCInst &Inst, unsigned EncSize,
267 unsigned OpWidth, uint64_t Imm, uint64_t EncImm,
268 const MCDisassembler *Decoder) {
269 assert(Imm < (1ULL << EncSize) && "Operand doesn't fit encoding!");
270 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
271 return addOperand(
272 Inst, DAsm->decodeSrcOp(Inst, OpWidth, static_cast<unsigned>(EncImm)));
273}
274
275// Decode an indexed-resource (rsrcidx) 9-bit srsrc field into a 32-bit index
276// register. SGPRs are encoded as 128-251, VGPRs have bit 8 set.
277static DecodeStatus decodeRsrcRegOp(MCInst &Inst, unsigned Imm,
278 uint64_t /* Addr */,
279 const MCDisassembler *Decoder,
280 unsigned OpWidth) {
281 // Uniform-indexed resource. SGPR[0..123] encoded as 128-251.
282 if (Imm >= 128 && Imm < 256)
283 Imm -= 128;
284 return decodeSrcOp(Inst, 9, OpWidth, Imm, Imm, Decoder);
285}
286
288 uint64_t /* Addr */,
289 const MCDisassembler *Decoder) {
290 unsigned OpWidth = 32;
291 // 0-127: Uniform-direct resource in SGPRs (SReg_128).
292 if (Imm < 128)
293 OpWidth = 128;
294 return decodeRsrcRegOp(Inst, Imm, 0, Decoder, OpWidth);
295}
296
298 uint64_t /* Addr */,
299 const MCDisassembler *Decoder) {
300 unsigned OpWidth = 32;
301 // 0-127: Uniform-direct resource in SGPRs (SReg_256).
302 if (Imm < 128)
303 OpWidth = 256;
304 return decodeRsrcRegOp(Inst, Imm, 0, Decoder, OpWidth);
305}
306
307// Decoder for registers. Imm(7-bit) is number of register, uses decodeSrcOp to
308// get register class. Used by SGPR only operands.
309#define DECODE_OPERAND_SREG_7(RegClass, OpWidth) \
310 DECODE_SrcOp(Decode##RegClass##RegisterClass, 7, OpWidth, Imm)
311
312#define DECODE_OPERAND_SREG_8(RegClass, OpWidth) \
313 DECODE_SrcOp(Decode##RegClass##RegisterClass, 8, OpWidth, Imm)
314
315#define DECODE_OPERAND_SREG_9(RegClass, OpWidth) \
316 DECODE_SrcOp(Decode##RegClass##RegisterClass, 9, OpWidth, Imm)
317
318// Decoder for registers. Imm(10-bit): Imm{7-0} is number of register,
319// Imm{9} is acc(agpr or vgpr) Imm{8} should be 0 (see VOP3Pe_SMFMAC).
320// Set Imm{8} to 1 (IS_VGPR) to decode using 'enum10' from decodeSrcOp.
321// Used by AV_ register classes (AGPR or VGPR only register operands).
322template <unsigned OpWidth>
324 const MCDisassembler *Decoder) {
325 return decodeSrcOp(Inst, 10, OpWidth, Imm, Imm | AMDGPU::EncValues::IS_VGPR,
326 Decoder);
327}
328
329// Decoder for Src(9-bit encoding) registers only.
330template <unsigned OpWidth>
332 uint64_t /* Addr */,
333 const MCDisassembler *Decoder) {
334 return decodeSrcOp(Inst, 9, OpWidth, Imm, Imm, Decoder);
335}
336
337// Decoder for Src(9-bit encoding) AGPR, register number encoded in 9bits, set
338// Imm{9} to 1 (set acc) and decode using 'enum10' from decodeSrcOp, registers
339// only.
340template <unsigned OpWidth>
342 const MCDisassembler *Decoder) {
343 // A clear Imm{8} names an SGPR or an inline constant, which this
344 // register-only operand cannot hold.
347 return decodeSrcOp(Inst, 9, OpWidth, Imm, Imm | 512, Decoder);
348}
349
350// Decoder for 'enum10' from decodeSrcOp, Imm{0-8} is 9-bit Src encoding
351// Imm{9} is acc, registers only.
352template <unsigned OpWidth>
354 uint64_t /* Addr */,
355 const MCDisassembler *Decoder) {
356 // A clear Imm{8} names an SGPR or an inline constant, which this
357 // register-only operand cannot hold.
360 return decodeSrcOp(Inst, 10, OpWidth, Imm, Imm, Decoder);
361}
362
363// Decoder for RegisterOperands using 9-bit Src encoding. Operand can be
364// register from RegClass or immediate. Registers that don't belong to RegClass
365// will be decoded and InstPrinter will report warning. Immediate will be
366// decoded into constant matching the OperandType (important for floating point
367// types).
368template <unsigned OpWidth>
370 uint64_t /* Addr */,
371 const MCDisassembler *Decoder) {
372 return decodeSrcOp(Inst, 9, OpWidth, Imm, Imm, Decoder);
373}
374
375// Decoder for Src(9-bit encoding) AGPR or immediate. Set Imm{9} to 1 (set acc)
376// and decode using 'enum10' from decodeSrcOp.
377template <unsigned OpWidth>
379 uint64_t /* Addr */,
380 const MCDisassembler *Decoder) {
381 return decodeSrcOp(Inst, 9, OpWidth, Imm, Imm | 512, Decoder);
382}
383
384// Default decoders generated by tablegen: 'Decode<RegClass>RegisterClass'
385// when RegisterClass is used as an operand. Most often used for destination
386// operands.
387
389DECODE_OPERAND_REG_8(VGPR_32_Lo128)
392DECODE_OPERAND_REG_8(VReg_128)
393DECODE_OPERAND_REG_8(VReg_192)
394DECODE_OPERAND_REG_8(VReg_256)
395DECODE_OPERAND_REG_8(VReg_288)
396DECODE_OPERAND_REG_8(VReg_320)
397DECODE_OPERAND_REG_8(VReg_352)
398DECODE_OPERAND_REG_8(VReg_384)
399DECODE_OPERAND_REG_8(VReg_512)
400DECODE_OPERAND_REG_8(VReg_1024)
401
402DECODE_OPERAND_SREG_7(SReg_32, 32)
403DECODE_OPERAND_SREG_7(SReg_32_XM0, 32)
404DECODE_OPERAND_SREG_7(SReg_32_XEXEC, 32)
405DECODE_OPERAND_SREG_7(SReg_32_XM0_XEXEC, 32)
406DECODE_OPERAND_SREG_7(SReg_32_XEXEC_HI, 32)
407DECODE_OPERAND_SREG_7(SReg_64_XEXEC, 64)
408DECODE_OPERAND_SREG_7(SReg_64_XEXEC_XNULL, 64)
409DECODE_OPERAND_SREG_7(SReg_96, 96)
410DECODE_OPERAND_SREG_7(SReg_128, 128)
411DECODE_OPERAND_SREG_7(SReg_256, 256)
412DECODE_OPERAND_SREG_7(SReg_256_XNULL, 256)
413DECODE_OPERAND_SREG_7(SReg_512, 512)
414
415DECODE_OPERAND_SREG_8(SReg_64, 64)
416
417// GFX13 VBUFFER instructions use a 9-bit srsrc field. For the non-indexed form
418// the two extra MSBs are always 0, so the value still decodes to an SReg_128.
419DECODE_OPERAND_SREG_9(SReg_128_XNULL, 128)
420
423DECODE_OPERAND_REG_8(AReg_128)
424DECODE_OPERAND_REG_8(AReg_256)
425DECODE_OPERAND_REG_8(AReg_512)
426DECODE_OPERAND_REG_8(AReg_1024)
427
429 uint64_t /*Addr*/,
431 assert(isUInt<10>(Imm) && "10-bit encoding expected");
432 assert((Imm & (1 << 8)) == 0 && "Imm{8} should not be used");
433
434 bool IsHi = Imm & (1 << 9);
435 unsigned RegIdx = static_cast<unsigned>(Imm & 0xff);
436 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
437 return addOperand(Inst, DAsm->createVGPR16Operand(RegIdx, IsHi));
438}
439
440static DecodeStatus
442 const MCDisassembler *Decoder) {
443 assert(isUInt<8>(Imm) && "8-bit encoding expected");
444
445 bool IsHi = Imm & (1 << 7);
446 unsigned RegIdx = static_cast<unsigned>(Imm & 0x7f);
447 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
448 return addOperand(Inst, DAsm->createVGPR16Operand(RegIdx, IsHi));
449}
450
451template <unsigned OpWidth>
453 uint64_t /*Addr*/,
454 const MCDisassembler *Decoder) {
455 assert(isUInt<9>(Imm) && "9-bit encoding expected");
456
457 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
459 bool IsHi = Imm & (1 << 7);
460 unsigned RegIdx = static_cast<unsigned>(Imm & 0x7f);
461 return addOperand(Inst, DAsm->createVGPR16Operand(RegIdx, IsHi));
462 }
463 return addOperand(
464 Inst, DAsm->decodeNonVGPRSrcOp(Inst, OpWidth,
465 static_cast<unsigned>(Imm & 0xFF)));
466}
467
468template <unsigned OpWidth>
470 uint64_t /*Addr*/,
471 const MCDisassembler *Decoder) {
472 assert(isUInt<10>(Imm) && "10-bit encoding expected");
473
474 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
476 bool IsHi = Imm & (1 << 9);
477 unsigned RegIdx = static_cast<unsigned>(Imm & 0xff);
478 return addOperand(Inst, DAsm->createVGPR16Operand(RegIdx, IsHi));
479 }
480 return addOperand(
481 Inst, DAsm->decodeNonVGPRSrcOp(Inst, OpWidth,
482 static_cast<unsigned>(Imm & 0xFF)));
483}
484
486 uint64_t /*Addr*/,
487 const MCDisassembler *Decoder) {
488 assert(isUInt<10>(Imm) && "10-bit encoding expected");
491
492 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
493
494 bool IsHi = Imm & (1 << 9);
495 unsigned RegIdx = static_cast<unsigned>(Imm & 0xff);
496 return addOperand(Inst, DAsm->createVGPR16Operand(RegIdx, IsHi));
497}
498
500 uint64_t Addr,
501 const MCDisassembler *Decoder) {
502 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
503 return addOperand(
504 Inst, DAsm->decodeMandatoryLiteralConstant(static_cast<unsigned>(Imm)));
505}
506
508 uint64_t Addr,
509 const MCDisassembler *Decoder) {
510 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
511 return addOperand(Inst, DAsm->decodeMandatoryLiteral64Constant(Imm));
512}
513
515 uint64_t Addr, const void *Decoder) {
516 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
517 return addOperand(Inst,
518 DAsm->decodeVOPDDstYOp(Inst, static_cast<unsigned>(Val)));
519}
520
521static DecodeStatus decodeAVLdSt(MCInst &Inst, uint64_t Imm, unsigned Opw,
522 const MCDisassembler *Decoder) {
523 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
524 return addOperand(
525 Inst, DAsm->decodeSrcOp(Inst, Opw, static_cast<unsigned>(Imm | 256)));
526}
527
528template <unsigned Opw>
530 uint64_t /* Addr */,
531 const MCDisassembler *Decoder) {
532 return decodeAVLdSt(Inst, Imm, Opw, Decoder);
533}
534
536 uint64_t Addr,
537 const MCDisassembler *Decoder) {
538 assert(Imm < (1 << 9) && "9-bit encoding");
539 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
540 return addOperand(Inst,
541 DAsm->decodeSrcOp(Inst, 64, static_cast<unsigned>(Imm)));
542}
543
544#define DECODE_SDWA(DecName) \
545DECODE_OPERAND(decodeSDWA##DecName, decodeSDWA##DecName)
546
547DECODE_SDWA(Src32)
548DECODE_SDWA(Src16)
549DECODE_SDWA(VopcDst)
550
551#define DECODE_SDWA_IMM_FIELD(Name, MaxImm) \
552 static DecodeStatus Name(MCInst &Inst, uint64_t Imm, uint64_t /* Addr */, \
553 const MCDisassembler * /* Decoder */) { \
554 if (Imm > (MaxImm)) \
555 return MCDisassembler::Fail; \
556 return addOperand(Inst, MCOperand::createImm(Imm)); \
557 }
558
559// The 3-bit SDWA sel fields only define values up to DWORD; 7 is reserved.
561// The 2-bit SDWA dst_unused field only defines values up to UNUSED_PRESERVE;
562// 3 is reserved.
563DECODE_SDWA_IMM_FIELD(decodeSDWADstUnused,
564 AMDGPU::SDWA::DstUnused::UNUSED_PRESERVE)
565#undef DECODE_SDWA_IMM_FIELD
566
567static DecodeStatus decodeVersionImm(MCInst &Inst, uint64_t Imm,
568 uint64_t /* Addr */,
570 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
571 return addOperand(Inst, DAsm->decodeVersionImm(static_cast<unsigned>(Imm)));
572}
573
574#include "AMDGPUGenDisassemblerTables.inc"
575
576namespace {
577// Define bitwidths for various types used to instantiate the decoder.
578template <> constexpr uint32_t InsnBitWidth<uint32_t> = 32;
579template <> constexpr uint32_t InsnBitWidth<uint64_t> = 64;
580template <> constexpr uint32_t InsnBitWidth<std::bitset<96>> = 96;
581template <> constexpr uint32_t InsnBitWidth<std::bitset<128>> = 128;
582} // namespace
583
584//===----------------------------------------------------------------------===//
585//
586//===----------------------------------------------------------------------===//
587
588template <typename InsnType>
590 InsnType Inst, uint64_t Address,
591 raw_ostream &Comments) const {
592 assert(MI.getOpcode() == 0);
593 assert(MI.getNumOperands() == 0);
594 MCInst TmpInst;
595 HasLiteral = false;
596 const auto SavedBytes = Bytes;
597
598 SmallString<64> LocalComments;
599 raw_svector_ostream LocalCommentStream(LocalComments);
600 CommentStream = &LocalCommentStream;
601
602 DecodeStatus Res =
603 decodeInstruction(Table, TmpInst, Inst, Address, this, STI);
604 if (Res != MCDisassembler::Fail && !decodeImmOperands(TmpInst, *MCII))
606
607 CommentStream = nullptr;
608
609 if (Res != MCDisassembler::Fail) {
610 MI = TmpInst;
611 Comments << LocalComments;
613 }
614 Bytes = SavedBytes;
616}
617
618template <typename InsnType>
621 MCInst &MI, InsnType Inst, uint64_t Address,
622 raw_ostream &Comments) const {
623 for (const uint8_t *T : {Table1, Table2}) {
624 if (DecodeStatus Res = tryDecodeInst(T, MI, Inst, Address, Comments))
625 return Res;
626 }
628}
629
630template <typename T> static inline T eatBytes(ArrayRef<uint8_t>& Bytes) {
631 assert(Bytes.size() >= sizeof(T));
632 const auto Res =
634 Bytes = Bytes.slice(sizeof(T));
635 return Res;
636}
637
638static inline std::bitset<96> eat12Bytes(ArrayRef<uint8_t> &Bytes) {
639 using namespace llvm::support::endian;
640 assert(Bytes.size() >= 12);
641 std::bitset<96> Lo(read<uint64_t, endianness::little>(Bytes.data()));
642 Bytes = Bytes.slice(8);
643 std::bitset<96> Hi(read<uint32_t, endianness::little>(Bytes.data()));
644 Bytes = Bytes.slice(4);
645 return (Hi << 64) | Lo;
646}
647
648static inline std::bitset<128> eat16Bytes(ArrayRef<uint8_t> &Bytes) {
649 using namespace llvm::support::endian;
650 assert(Bytes.size() >= 16);
651 std::bitset<128> Lo(read<uint64_t, endianness::little>(Bytes.data()));
652 Bytes = Bytes.slice(8);
653 std::bitset<128> Hi(read<uint64_t, endianness::little>(Bytes.data()));
654 Bytes = Bytes.slice(8);
655 return (Hi << 64) | Lo;
656}
657
658bool AMDGPUDisassembler::decodeImmOperands(MCInst &MI,
659 const MCInstrInfo &MCII) const {
660 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
661 for (auto [OpNo, OpDesc] : enumerate(Desc.operands())) {
662 if (OpNo >= MI.getNumOperands())
663 continue;
664
665 // TODO: Fix V_DUAL_FMAMK_F32_X_FMAAK_F32_gfx12 vsrc operands,
666 // defined to take VGPR_32, but in reality allowing inline constants.
667 bool IsSrc = AMDGPU::OPERAND_SRC_FIRST <= OpDesc.OperandType &&
668 OpDesc.OperandType <= AMDGPU::OPERAND_SRC_LAST;
669 if (!IsSrc && OpDesc.OperandType != MCOI::OPERAND_REGISTER)
670 continue;
671
672 MCOperand &Op = MI.getOperand(OpNo);
673 if (!Op.isImm())
674 continue;
675 int64_t Imm = Op.getImm();
679 continue;
680 }
681
683 Op = decodeLiteralConstant(Desc, OpDesc);
684 if (!Op.isValid())
685 return false;
686 continue;
687 }
688
691 switch (OpDesc.OperandType) {
694 // Inline constant encodings are not allowed for NOINLINE operand types.
695 // Keep the raw encoding value.
696 continue;
702 break;
706 break;
710 break;
712 // V_PK_FMAC_F16 on GFX11+ duplicates the f16 inline constant to both
713 // halves, so we need to produce the duplicated value for correct
714 // round-trip.
715 if (isGFX11Plus()) {
716 int64_t F16Val = getInlineImmValF16(Imm);
717 Imm = (F16Val << 16) | (F16Val & 0xFFFF);
718 } else {
720 }
721 break;
722 }
731 break;
732 default:
734 }
735 Op.setImm(Imm);
736 }
737 }
738 return true;
739}
740
742 ArrayRef<uint8_t> Bytes_,
743 uint64_t Address,
744 raw_ostream &CS) const {
745 unsigned MaxInstBytesNum = std::min((size_t)TargetMaxInstBytes, Bytes_.size());
746 Bytes = Bytes_.slice(0, MaxInstBytesNum);
747
748 // In case the opcode is not recognized we'll assume a Size of 4 bytes (unless
749 // there are fewer bytes left). This will be overridden on success.
750 Size = std::min((size_t)4, Bytes_.size());
751
752 do {
753 // ToDo: better to switch encoding length using some bit predicate
754 // but it is unknown yet, so try all we can
755
756 // Try to decode DPP and SDWA first to solve conflict with VOP1 and VOP2
757 // encodings
758 if (isGFX1250Plus() && Bytes.size() >= 16) {
759 std::bitset<128> DecW = eat16Bytes(Bytes);
760 if (tryDecodeInst(DecoderTableGFX1250128, MI, DecW, Address, CS))
761 break;
762 Bytes = Bytes_.slice(0, MaxInstBytesNum);
763 }
764
765 if (isGFX11Plus() && Bytes.size() >= 12) {
766 std::bitset<96> DecW = eat12Bytes(Bytes);
767
768 if (isGFX1170() &&
769 tryDecodeInst(DecoderTableGFX117096, DecoderTableGFX1170_FAKE1696, MI,
770 DecW, Address, CS))
771 break;
772
773 if (isGFX11() &&
774 tryDecodeInst(DecoderTableGFX1196, DecoderTableGFX11_FAKE1696, MI,
775 DecW, Address, CS))
776 break;
777
778 if (isGFX1250() &&
779 tryDecodeInst(DecoderTableGFX125096, DecoderTableGFX1250_FAKE1696, MI,
780 DecW, Address, CS))
781 break;
782
783 if (isGFX12() &&
784 tryDecodeInst(DecoderTableGFX1296, DecoderTableGFX12_FAKE1696, MI,
785 DecW, Address, CS))
786 break;
787
788 if (isGFX12() &&
789 tryDecodeInst(DecoderTableGFX12W6496, MI, DecW, Address, CS))
790 break;
791
792 if (isGFX13() &&
793 tryDecodeInst(DecoderTableGFX1396, DecoderTableGFX13_FAKE1696, MI,
794 DecW, Address, CS))
795 break;
796
797 if (STI.hasFeature(AMDGPU::Feature64BitLiterals)) {
798 // Return 8 bytes for a potential literal.
799 Bytes = Bytes_.slice(4, MaxInstBytesNum - 4);
800
801 if (isGFX1250() &&
802 tryDecodeInst(DecoderTableGFX125096, MI, DecW, Address, CS))
803 break;
804 }
805
806 // Reinitialize Bytes
807 Bytes = Bytes_.slice(0, MaxInstBytesNum);
808
809 } else if (Bytes.size() >= 16 &&
810 STI.hasFeature(AMDGPU::FeatureGFX950Insts)) {
811 std::bitset<128> DecW = eat16Bytes(Bytes);
812 if (tryDecodeInst(DecoderTableGFX940128, MI, DecW, Address, CS))
813 break;
814
815 // Reinitialize Bytes
816 Bytes = Bytes_.slice(0, MaxInstBytesNum);
817 }
818
819 if (Bytes.size() >= 8) {
820 const uint64_t QW = eatBytes<uint64_t>(Bytes);
821
822 if (STI.hasFeature(AMDGPU::FeatureGFX10_BEncoding) &&
823 tryDecodeInst(DecoderTableGFX10_B64, MI, QW, Address, CS))
824 break;
825
826 if (STI.hasFeature(AMDGPU::FeatureUnpackedD16VMem) &&
827 tryDecodeInst(DecoderTableGFX80_UNPACKED64, MI, QW, Address, CS))
828 break;
829
830 if (STI.hasFeature(AMDGPU::FeatureGFX950Insts) &&
831 tryDecodeInst(DecoderTableGFX95064, MI, QW, Address, CS))
832 break;
833
834 // Some GFX9 subtargets repurposed the v_mad_mix_f32, v_mad_mixlo_f16 and
835 // v_mad_mixhi_f16 for FMA variants. Try to decode using this special
836 // table first so we print the correct name.
837 if (STI.hasFeature(AMDGPU::FeatureFmaMixInsts) &&
838 tryDecodeInst(DecoderTableGFX9_DL64, MI, QW, Address, CS))
839 break;
840
841 if (STI.hasFeature(AMDGPU::FeatureGFX940Insts) &&
842 tryDecodeInst(DecoderTableGFX94064, MI, QW, Address, CS))
843 break;
844
845 if (STI.hasFeature(AMDGPU::FeatureGFX90AInsts) &&
846 tryDecodeInst(DecoderTableGFX90A64, MI, QW, Address, CS))
847 break;
848
849 if ((isVI() || isGFX9()) &&
850 tryDecodeInst(DecoderTableGFX864, MI, QW, Address, CS))
851 break;
852
853 if (isGFX9() && tryDecodeInst(DecoderTableGFX964, MI, QW, Address, CS))
854 break;
855
856 if (isGFX10() && tryDecodeInst(DecoderTableGFX1064, MI, QW, Address, CS))
857 break;
858
859 if (isGFX1250() &&
860 tryDecodeInst(DecoderTableGFX125064, DecoderTableGFX1250_FAKE1664, MI,
861 QW, Address, CS))
862 break;
863
864 if (isGFX12() &&
865 tryDecodeInst(DecoderTableGFX1264, DecoderTableGFX12_FAKE1664, MI, QW,
866 Address, CS))
867 break;
868
869 if (isGFX1170() &&
870 tryDecodeInst(DecoderTableGFX117064, DecoderTableGFX1170_FAKE1664, MI,
871 QW, Address, CS))
872 break;
873
874 if (isGFX11() &&
875 tryDecodeInst(DecoderTableGFX1164, DecoderTableGFX11_FAKE1664, MI, QW,
876 Address, CS))
877 break;
878
879 if (isGFX1170() &&
880 tryDecodeInst(DecoderTableGFX1170W6464, MI, QW, Address, CS))
881 break;
882
883 if (isGFX11() &&
884 tryDecodeInst(DecoderTableGFX11W6464, MI, QW, Address, CS))
885 break;
886
887 if (isGFX12() &&
888 tryDecodeInst(DecoderTableGFX12W6464, MI, QW, Address, CS))
889 break;
890
891 if (isGFX13() &&
892 tryDecodeInst(DecoderTableGFX1364, DecoderTableGFX13_FAKE1664, MI, QW,
893 Address, CS))
894 break;
895
896 // Reinitialize Bytes
897 Bytes = Bytes_.slice(0, MaxInstBytesNum);
898 }
899
900 // Try decode 32-bit instruction
901 if (Bytes.size() >= 4) {
902 const uint32_t DW = eatBytes<uint32_t>(Bytes);
903
904 if ((isVI() || isGFX9()) &&
905 tryDecodeInst(DecoderTableGFX832, MI, DW, Address, CS))
906 break;
907
908 if (tryDecodeInst(DecoderTableAMDGPU32, MI, DW, Address, CS))
909 break;
910
911 if (isGFX9() && tryDecodeInst(DecoderTableGFX932, MI, DW, Address, CS))
912 break;
913
914 if (STI.hasFeature(AMDGPU::FeatureGFX950Insts) &&
915 tryDecodeInst(DecoderTableGFX95032, MI, DW, Address, CS))
916 break;
917
918 if (STI.hasFeature(AMDGPU::FeatureGFX90AInsts) &&
919 tryDecodeInst(DecoderTableGFX90A32, MI, DW, Address, CS))
920 break;
921
922 if (STI.hasFeature(AMDGPU::FeatureGFX10_BEncoding) &&
923 tryDecodeInst(DecoderTableGFX10_B32, MI, DW, Address, CS))
924 break;
925
926 if (isGFX10() && tryDecodeInst(DecoderTableGFX1032, MI, DW, Address, CS))
927 break;
928
929 if (isGFX1170() &&
930 tryDecodeInst(DecoderTableGFX117032, DecoderTableGFX1170_FAKE1632, MI,
931 DW, Address, CS))
932 break;
933
934 if (isGFX11() &&
935 tryDecodeInst(DecoderTableGFX1132, DecoderTableGFX11_FAKE1632, MI, DW,
936 Address, CS))
937 break;
938
939 if (isGFX1250() &&
940 tryDecodeInst(DecoderTableGFX125032, DecoderTableGFX1250_FAKE1632, MI,
941 DW, Address, CS))
942 break;
943
944 if (isGFX12() &&
945 tryDecodeInst(DecoderTableGFX1232, DecoderTableGFX12_FAKE1632, MI, DW,
946 Address, CS))
947 break;
948
949 if (isGFX13() &&
950 tryDecodeInst(DecoderTableGFX1332, DecoderTableGFX13_FAKE1632, MI, DW,
951 Address, CS))
952 break;
953 }
954
956 } while (false);
957
959
960 if (SIInstrFlags::isDPP(*MCII, MI)) {
961 if (isMacDPP(MI))
963
964 if (SIInstrFlags::isVOP3P(*MCII, MI))
966 else if (SIInstrFlags::isVOPC(*MCII, MI))
967 convertVOPCDPPInst(MI); // Special VOP3 case
968 else if (AMDGPU::isVOPC64DPP(MI.getOpcode()))
969 convertVOPC64DPPInst(MI); // Special VOP3 case
970 else if (AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::dpp8) !=
971 -1)
973 else if (SIInstrFlags::isVOP3(*MCII, MI))
974 convertVOP3DPPInst(MI); // Regular VOP3 case
975 }
976
978
979 if (AMDGPU::isMAC(MI.getOpcode())) {
980 // Insert dummy unused src2_modifiers.
982 AMDGPU::OpName::src2_modifiers);
983 }
984
985 if (MI.getOpcode() == AMDGPU::V_CVT_SR_BF8_F32_e64_dpp ||
986 MI.getOpcode() == AMDGPU::V_CVT_SR_FP8_F32_e64_dpp) {
987 // Insert dummy unused src2_modifiers.
989 AMDGPU::OpName::src2_modifiers);
990 }
991
992 if (SIInstrFlags::isDS(*MCII, MI) && !AMDGPU::hasGDS(STI)) {
993 insertNamedMCOperand(MI, MCOperand::createImm(0), AMDGPU::OpName::gds);
994 }
995
996 if (SIInstrFlags::isMUBUF(*MCII, MI) || SIInstrFlags::isFLAT(*MCII, MI) ||
997 SIInstrFlags::isSMRD(*MCII, MI)) {
998 int CPolPos = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
999 AMDGPU::OpName::cpol);
1000 if (CPolPos != -1) {
1001 unsigned CPol =
1003 if (MI.getNumOperands() <= (unsigned)CPolPos) {
1005 AMDGPU::OpName::cpol);
1006 } else if (CPol) {
1007 MI.getOperand(CPolPos).setImm(MI.getOperand(CPolPos).getImm() | CPol);
1008 }
1009 }
1010 }
1011
1012 if (SIInstrFlags::isBuffer(*MCII, MI) &&
1013 (STI.hasFeature(AMDGPU::FeatureGFX90AInsts))) {
1014 // GFX90A lost TFE, its place is occupied by ACC.
1015 int TFEOpIdx =
1016 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::tfe);
1017 if (TFEOpIdx != -1) {
1018 auto *TFEIter = MI.begin();
1019 std::advance(TFEIter, TFEOpIdx);
1020 MI.insert(TFEIter, MCOperand::createImm(0));
1021 }
1022 }
1023
1024 // Validate buffer instruction offsets for GFX12+ - must not be a negative.
1026 int OffsetIdx =
1027 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::offset);
1028 if (OffsetIdx != -1) {
1029 uint32_t Imm = MI.getOperand(OffsetIdx).getImm();
1030 int64_t SignedOffset = SignExtend64<24>(Imm);
1031 if (SignedOffset < 0)
1032 return MCDisassembler::Fail;
1033 }
1034 }
1035
1036 if (SIInstrFlags::isBuffer(*MCII, MI)) {
1037 int SWZOpIdx =
1038 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::swz);
1039 if (SWZOpIdx != -1) {
1040 auto *SWZIter = MI.begin();
1041 std::advance(SWZIter, SWZOpIdx);
1042 MI.insert(SWZIter, MCOperand::createImm(0));
1043 }
1044 }
1045
1046 const MCInstrDesc &Desc = MCII->get(MI.getOpcode());
1048 int VAddr0Idx =
1049 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vaddr0);
1050 int RsrcIdx =
1051 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::srsrc);
1052 unsigned NSAArgs = RsrcIdx - VAddr0Idx - 1;
1053 if (VAddr0Idx >= 0 && NSAArgs > 0) {
1054 unsigned NSAWords = (NSAArgs + 3) / 4;
1055 if (Bytes.size() < 4 * NSAWords)
1056 return MCDisassembler::Fail;
1057 for (unsigned i = 0; i < NSAArgs; ++i) {
1058 const unsigned VAddrIdx = VAddr0Idx + 1 + i;
1059 auto VAddrRCID =
1060 MCII->getOpRegClassID(Desc.operands()[VAddrIdx], HwModeRegClass);
1061 MI.insert(MI.begin() + VAddrIdx, createRegOperand(VAddrRCID, Bytes[i]));
1062 }
1063 Bytes = Bytes.slice(4 * NSAWords);
1064 }
1065
1067 }
1068
1069 if (SIInstrFlags::isVIMAGE(*MCII, MI) || SIInstrFlags::isVSAMPLE(*MCII, MI))
1071
1072 if (SIInstrFlags::isEXP(*MCII, MI))
1074
1075 if (SIInstrFlags::isVINTERP(*MCII, MI))
1077
1078 if (SIInstrFlags::isSDWA(*MCII, MI))
1080
1081 if (SIInstrFlags::isMAI(*MCII, MI) && !convertMAIInst(MI))
1082 return MCDisassembler::Fail;
1083
1084 if (SIInstrFlags::isWMMA(*MCII, MI) && !convertWMMAInst(MI))
1085 return MCDisassembler::Fail;
1086
1087 int VDstIn_Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
1088 AMDGPU::OpName::vdst_in);
1089 if (VDstIn_Idx != -1) {
1090 int Tied = MCII->get(MI.getOpcode()).getOperandConstraint(VDstIn_Idx,
1092 if (Tied != -1 && (MI.getNumOperands() <= (unsigned)VDstIn_Idx ||
1093 !MI.getOperand(VDstIn_Idx).isReg() ||
1094 MI.getOperand(VDstIn_Idx).getReg() != MI.getOperand(Tied).getReg())) {
1095 if (MI.getNumOperands() > (unsigned)VDstIn_Idx)
1096 MI.erase(&MI.getOperand(VDstIn_Idx));
1098 MCOperand::createReg(MI.getOperand(Tied).getReg()),
1099 AMDGPU::OpName::vdst_in);
1100 }
1101 }
1102
1103 bool IsSOPK = SIInstrFlags::isSOPK(*MCII, MI);
1104 if (AMDGPU::hasNamedOperand(MI.getOpcode(), AMDGPU::OpName::imm) && !IsSOPK)
1106
1107 // Some VOPC instructions, e.g., v_cmpx_f_f64, use VOP3 encoding and
1108 // have EXEC as implicit destination. Issue a warning if encoding for
1109 // vdst is not EXEC.
1110 if (SIInstrFlags::isVOP3(*MCII, MI) &&
1111 MCII->get(MI.getOpcode()).getNumDefs() == 0 &&
1112 MCII->get(MI.getOpcode()).hasImplicitDefOfPhysReg(AMDGPU::EXEC)) {
1113 auto ExecEncoding = MRI.getEncodingValue(AMDGPU::EXEC_LO);
1114 if (Bytes_[0] != ExecEncoding)
1116 }
1117
1118 Size = MaxInstBytesNum - Bytes.size();
1119 return Status;
1120}
1121
1123 if (STI.hasFeature(AMDGPU::FeatureGFX11Insts)) {
1124 // The MCInst still has these fields even though they are no longer encoded
1125 // in the GFX11 instruction.
1126 insertNamedMCOperand(MI, MCOperand::createImm(0), AMDGPU::OpName::vm);
1127 insertNamedMCOperand(MI, MCOperand::createImm(0), AMDGPU::OpName::compr);
1128 }
1129}
1130
1133 if (MI.getOpcode() == AMDGPU::V_INTERP_P10_F16_F32_inreg_t16_gfx11 ||
1134 MI.getOpcode() == AMDGPU::V_INTERP_P10_F16_F32_inreg_fake16_gfx11 ||
1135 MI.getOpcode() == AMDGPU::V_INTERP_P10_F16_F32_inreg_t16_gfx12 ||
1136 MI.getOpcode() == AMDGPU::V_INTERP_P10_F16_F32_inreg_fake16_gfx12 ||
1137 MI.getOpcode() == AMDGPU::V_INTERP_P10_F16_F32_inreg_t16_gfx13 ||
1138 MI.getOpcode() == AMDGPU::V_INTERP_P10_F16_F32_inreg_fake16_gfx13 ||
1139 MI.getOpcode() == AMDGPU::V_INTERP_P10_RTZ_F16_F32_inreg_t16_gfx11 ||
1140 MI.getOpcode() == AMDGPU::V_INTERP_P10_RTZ_F16_F32_inreg_fake16_gfx11 ||
1141 MI.getOpcode() == AMDGPU::V_INTERP_P10_RTZ_F16_F32_inreg_t16_gfx12 ||
1142 MI.getOpcode() == AMDGPU::V_INTERP_P10_RTZ_F16_F32_inreg_fake16_gfx12 ||
1143 MI.getOpcode() == AMDGPU::V_INTERP_P10_RTZ_F16_F32_inreg_t16_gfx13 ||
1144 MI.getOpcode() == AMDGPU::V_INTERP_P10_RTZ_F16_F32_inreg_fake16_gfx13 ||
1145 MI.getOpcode() == AMDGPU::V_INTERP_P2_F16_F32_inreg_t16_gfx11 ||
1146 MI.getOpcode() == AMDGPU::V_INTERP_P2_F16_F32_inreg_fake16_gfx11 ||
1147 MI.getOpcode() == AMDGPU::V_INTERP_P2_F16_F32_inreg_t16_gfx12 ||
1148 MI.getOpcode() == AMDGPU::V_INTERP_P2_F16_F32_inreg_fake16_gfx12 ||
1149 MI.getOpcode() == AMDGPU::V_INTERP_P2_F16_F32_inreg_t16_gfx13 ||
1150 MI.getOpcode() == AMDGPU::V_INTERP_P2_F16_F32_inreg_fake16_gfx13 ||
1151 MI.getOpcode() == AMDGPU::V_INTERP_P2_RTZ_F16_F32_inreg_t16_gfx11 ||
1152 MI.getOpcode() == AMDGPU::V_INTERP_P2_RTZ_F16_F32_inreg_fake16_gfx11 ||
1153 MI.getOpcode() == AMDGPU::V_INTERP_P2_RTZ_F16_F32_inreg_t16_gfx12 ||
1154 MI.getOpcode() == AMDGPU::V_INTERP_P2_RTZ_F16_F32_inreg_fake16_gfx12 ||
1155 MI.getOpcode() == AMDGPU::V_INTERP_P2_RTZ_F16_F32_inreg_t16_gfx13 ||
1156 MI.getOpcode() == AMDGPU::V_INTERP_P2_RTZ_F16_F32_inreg_fake16_gfx13) {
1157 // The MCInst has this field that is not directly encoded in the
1158 // instruction.
1159 insertNamedMCOperand(MI, MCOperand::createImm(0), AMDGPU::OpName::op_sel);
1160 }
1161}
1162
1164 if (STI.hasFeature(AMDGPU::FeatureGFX9) ||
1165 STI.hasFeature(AMDGPU::FeatureGFX10)) {
1166 if (AMDGPU::hasNamedOperand(MI.getOpcode(), AMDGPU::OpName::sdst))
1167 // VOPC - insert clamp
1168 insertNamedMCOperand(MI, MCOperand::createImm(0), AMDGPU::OpName::clamp);
1169 } else if (STI.hasFeature(AMDGPU::FeatureVolcanicIslands)) {
1170 int SDst = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::sdst);
1171 if (SDst != -1) {
1172 // VOPC - insert VCC register as sdst
1174 AMDGPU::OpName::sdst);
1175 } else {
1176 // VOP1/2 - insert omod if present in instruction
1177 insertNamedMCOperand(MI, MCOperand::createImm(0), AMDGPU::OpName::omod);
1178 }
1179 }
1180}
1181
1182/// Adjust the register values used by V_MFMA_F8F6F4_f8_f8 instructions to the
1183/// appropriate subregister for the used format width.
1184///
1185/// \returns false if the operand cannot be narrowed down to \p NumRegs, which
1186/// means the encoding is malformed.
1188 MCOperand &MO, uint8_t NumRegs) {
1189 // A malformed encoding can select an operand that is not a register at all.
1190 if (!MO.isReg())
1191 return false;
1192
1193 MCRegister NewReg;
1194 switch (NumRegs) {
1195 case 4:
1196 NewReg = MRI.getSubReg(MO.getReg(), AMDGPU::sub0_sub1_sub2_sub3);
1197 break;
1198 case 6:
1199 NewReg = MRI.getSubReg(MO.getReg(), AMDGPU::sub0_sub1_sub2_sub3_sub4_sub5);
1200 break;
1201 case 8:
1202 NewReg = MRI.getSubReg(MO.getReg(),
1203 AMDGPU::sub0_sub1_sub2_sub3_sub4_sub5_sub6_sub7);
1204 // For mfma f8/f8 is the widest format, so the operand already has the
1205 // requested width and there is no subregister to select.
1206 if (!NewReg)
1207 return true;
1208 break;
1209 case 12:
1210 // There is no 384-bit subreg index defined.
1211 if (MCRegister BaseReg = MRI.getSubReg(MO.getReg(), AMDGPU::sub0)) {
1212 NewReg = MRI.getMatchingSuperReg(
1213 BaseReg, AMDGPU::sub0, &MRI.getRegClass(AMDGPU::VReg_384RegClassID));
1214 }
1215 break;
1216 case 16:
1217 // No-op in cases where one operand is still f8/bf8.
1218 return true;
1219 default:
1220 llvm_unreachable("Unexpected size for mfma/wmma f8f6f4 operand");
1221 }
1222
1223 if (!NewReg)
1224 return false;
1225
1226 MO.setReg(NewReg);
1227 return true;
1228}
1229
1230/// f8f6f4 instructions have different pseudos depending on the used formats. In
1231/// the disassembler table, we only have the variants with the largest register
1232/// classes which assume using an fp8/bf8 format for both operands. The actual
1233/// register class depends on the format in blgp and cbsz operands. Adjust the
1234/// register classes depending on the used format.
1236 int BlgpIdx =
1237 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::blgp);
1238 if (BlgpIdx == -1)
1239 return true;
1240
1241 int CbszIdx =
1242 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::cbsz);
1243
1244 unsigned CBSZ = MI.getOperand(CbszIdx).getImm();
1245 unsigned BLGP = MI.getOperand(BlgpIdx).getImm();
1246
1247 const AMDGPU::MFMA_F8F6F4_Info *AdjustedRegClassOpcode =
1248 AMDGPU::getMFMA_F8F6F4_WithFormatArgs(CBSZ, BLGP, MI.getOpcode());
1249 if (!AdjustedRegClassOpcode ||
1250 AdjustedRegClassOpcode->Opcode == MI.getOpcode())
1251 return true;
1252
1253 MI.setOpcode(AdjustedRegClassOpcode->Opcode);
1254 int Src0Idx =
1255 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src0);
1256 int Src1Idx =
1257 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src1);
1258 return adjustMFMA_F8F6F4OpRegClass(MRI, MI.getOperand(Src0Idx),
1259 AdjustedRegClassOpcode->NumRegsSrcA) &&
1260 adjustMFMA_F8F6F4OpRegClass(MRI, MI.getOperand(Src1Idx),
1261 AdjustedRegClassOpcode->NumRegsSrcB);
1262}
1263
1265 int FmtAIdx =
1266 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::matrix_a_fmt);
1267 if (FmtAIdx == -1)
1268 return true;
1269
1270 int FmtBIdx =
1271 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::matrix_b_fmt);
1272
1273 unsigned FmtA = MI.getOperand(FmtAIdx).getImm();
1274 unsigned FmtB = MI.getOperand(FmtBIdx).getImm();
1275
1276 const AMDGPU::MFMA_F8F6F4_Info *AdjustedRegClassOpcode =
1277 AMDGPU::getWMMA_F8F6F4_WithFormatArgs(FmtA, FmtB, MI.getOpcode());
1278 if (!AdjustedRegClassOpcode ||
1279 AdjustedRegClassOpcode->Opcode == MI.getOpcode())
1280 return true;
1281
1282 MI.setOpcode(AdjustedRegClassOpcode->Opcode);
1283 int Src0Idx =
1284 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src0);
1285 int Src1Idx =
1286 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src1);
1287 return adjustMFMA_F8F6F4OpRegClass(MRI, MI.getOperand(Src0Idx),
1288 AdjustedRegClassOpcode->NumRegsSrcA) &&
1289 adjustMFMA_F8F6F4OpRegClass(MRI, MI.getOperand(Src1Idx),
1290 AdjustedRegClassOpcode->NumRegsSrcB);
1291}
1292
1294 unsigned OpSel = 0;
1295 unsigned OpSelHi = 0;
1296 unsigned NegLo = 0;
1297 unsigned NegHi = 0;
1298};
1299
1300// Reconstruct values of VOP3/VOP3P operands such as op_sel.
1301// Note that these values do not affect disassembler output,
1302// so this is only necessary for consistency with src_modifiers.
1304 bool IsVOP3P = false) {
1305 VOPModifiers Modifiers;
1306 unsigned Opc = MI.getOpcode();
1307 const AMDGPU::OpName ModOps[] = {AMDGPU::OpName::src0_modifiers,
1308 AMDGPU::OpName::src1_modifiers,
1309 AMDGPU::OpName::src2_modifiers};
1310 for (int J = 0; J < 3; ++J) {
1311 int OpIdx = AMDGPU::getNamedOperandIdx(Opc, ModOps[J]);
1312 if (OpIdx == -1)
1313 continue;
1314
1315 unsigned Val = MI.getOperand(OpIdx).getImm();
1316
1317 Modifiers.OpSel |= !!(Val & SISrcMods::OP_SEL_0) << J;
1318 if (IsVOP3P) {
1319 Modifiers.OpSelHi |= !!(Val & SISrcMods::OP_SEL_1) << J;
1320 Modifiers.NegLo |= !!(Val & SISrcMods::NEG) << J;
1321 Modifiers.NegHi |= !!(Val & SISrcMods::NEG_HI) << J;
1322 } else if (J == 0) {
1323 Modifiers.OpSel |= !!(Val & SISrcMods::DST_OP_SEL) << 3;
1324 }
1325 }
1326
1327 return Modifiers;
1328}
1329
1330// Instructions decode the op_sel/suffix bits into the src_modifier
1331// operands. Copy those bits into the src operands for true16 VGPRs.
1333 const unsigned Opc = MI.getOpcode();
1334 const MCRegisterClass &ConversionRC =
1335 MRI.getRegClass(AMDGPU::VGPR_16RegClassID);
1336 constexpr std::array<std::tuple<AMDGPU::OpName, AMDGPU::OpName, unsigned>, 4>
1337 OpAndOpMods = {{{AMDGPU::OpName::src0, AMDGPU::OpName::src0_modifiers,
1339 {AMDGPU::OpName::src1, AMDGPU::OpName::src1_modifiers,
1341 {AMDGPU::OpName::src2, AMDGPU::OpName::src2_modifiers,
1343 {AMDGPU::OpName::vdst, AMDGPU::OpName::src0_modifiers,
1345 for (const auto &[OpName, OpModsName, OpSelMask] : OpAndOpMods) {
1346 int OpIdx = AMDGPU::getNamedOperandIdx(Opc, OpName);
1347 int OpModsIdx = AMDGPU::getNamedOperandIdx(Opc, OpModsName);
1348 if (OpIdx == -1 || OpModsIdx == -1)
1349 continue;
1350 MCOperand &Op = MI.getOperand(OpIdx);
1351 if (!Op.isReg())
1352 continue;
1353 if (!ConversionRC.contains(Op.getReg()))
1354 continue;
1355 unsigned OpEnc = MRI.getEncodingValue(Op.getReg());
1356 const MCOperand &OpMods = MI.getOperand(OpModsIdx);
1357 unsigned ModVal = OpMods.getImm();
1358 if (ModVal & OpSelMask) { // isHi
1359 unsigned RegIdx = OpEnc & AMDGPU::HWEncoding::REG_IDX_MASK;
1360 Op.setReg(ConversionRC.getRegister(RegIdx * 2 + 1));
1361 }
1362 }
1363}
1364
1365// MAC opcodes have special old and src2 operands.
1366// src2 is tied to dst, while old is not tied (but assumed to be).
1368 constexpr int DST_IDX = 0;
1369 auto Opcode = MI.getOpcode();
1370 const auto &Desc = MCII->get(Opcode);
1371 auto OldIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::old);
1372
1373 if (OldIdx != -1 && Desc.getOperandConstraint(
1374 OldIdx, MCOI::OperandConstraint::TIED_TO) == -1) {
1375 assert(AMDGPU::hasNamedOperand(Opcode, AMDGPU::OpName::src2));
1376 assert(Desc.getOperandConstraint(
1377 AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src2),
1379 (void)DST_IDX;
1380 return true;
1381 }
1382
1383 return false;
1384}
1385
1386// Create dummy old operand and insert dummy unused src2_modifiers
1388 assert(MI.getNumOperands() + 1 < MCII->get(MI.getOpcode()).getNumOperands());
1389 insertNamedMCOperand(MI, MCOperand::createReg(0), AMDGPU::OpName::old);
1391 AMDGPU::OpName::src2_modifiers);
1392}
1393
1395 unsigned Opc = MI.getOpcode();
1396
1397 int VDstInIdx =
1398 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vdst_in);
1399 if (VDstInIdx != -1)
1400 insertNamedMCOperand(MI, MI.getOperand(0), AMDGPU::OpName::vdst_in);
1401
1402 unsigned DescNumOps = MCII->get(Opc).getNumOperands();
1403 if (MI.getNumOperands() < DescNumOps &&
1404 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::op_sel)) {
1406 auto Mods = collectVOPModifiers(MI);
1408 AMDGPU::OpName::op_sel);
1409 } else {
1410 // Insert dummy unused src modifiers.
1411 if (MI.getNumOperands() < DescNumOps &&
1412 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::src0_modifiers))
1414 AMDGPU::OpName::src0_modifiers);
1415
1416 if (MI.getNumOperands() < DescNumOps &&
1417 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::src1_modifiers))
1419 AMDGPU::OpName::src1_modifiers);
1420 }
1421}
1422
1425
1426 int VDstInIdx =
1427 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vdst_in);
1428 if (VDstInIdx != -1)
1429 insertNamedMCOperand(MI, MI.getOperand(0), AMDGPU::OpName::vdst_in);
1430
1431 unsigned Opc = MI.getOpcode();
1432 unsigned DescNumOps = MCII->get(Opc).getNumOperands();
1433 if (MI.getNumOperands() < DescNumOps &&
1434 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::op_sel)) {
1435 auto Mods = collectVOPModifiers(MI);
1437 AMDGPU::OpName::op_sel);
1438 }
1439}
1440
1441// Given a wide tuple \p Reg check if it will overflow 256 registers.
1442// \returns \p Reg on success or NoRegister otherwise.
1444 const MCRegisterInfo &MRI) {
1445 unsigned NumRegs = RC.getSizeInBits() / 32;
1446 MCRegister Sub0 = MRI.getSubReg(Reg, AMDGPU::sub0);
1447 if (!Sub0)
1448 return Reg;
1449
1450 MCRegister BaseReg;
1451 if (MRI.getRegClass(AMDGPU::VGPR_32RegClassID).contains(Sub0))
1452 BaseReg = AMDGPU::VGPR0;
1453 else if (MRI.getRegClass(AMDGPU::AGPR_32RegClassID).contains(Sub0))
1454 BaseReg = AMDGPU::AGPR0;
1455
1456 assert(BaseReg && "Only vector registers expected");
1457
1458 return (Sub0 - BaseReg + NumRegs <= 256) ? Reg : MCRegister();
1459}
1460
1461// Note that before gfx10, the MIMG encoding provided no information about
1462// VADDR size. Consequently, decoded instructions always show address as if it
1463// has 1 dword, which could be not really so.
1465 int VDstIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
1466 AMDGPU::OpName::vdst);
1467
1468 int VDataIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
1469 AMDGPU::OpName::vdata);
1470 int VAddr0Idx =
1471 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vaddr0);
1472 AMDGPU::OpName RsrcOpName = SIInstrFlags::isMIMG(*MCII, MI)
1473 ? AMDGPU::OpName::srsrc
1474 : AMDGPU::OpName::rsrc;
1475 int RsrcIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), RsrcOpName);
1476 int DMaskIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
1477 AMDGPU::OpName::dmask);
1478
1479 int TFEIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
1480 AMDGPU::OpName::tfe);
1481 int D16Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
1482 AMDGPU::OpName::d16);
1483
1484 const AMDGPU::MIMGInfo *Info = AMDGPU::getMIMGInfo(MI.getOpcode());
1485 const AMDGPU::MIMGBaseOpcodeInfo *BaseOpcode =
1486 AMDGPU::getMIMGBaseOpcodeInfo(Info->BaseOpcode);
1487
1488 assert(VDataIdx != -1);
1489 if (BaseOpcode->BVH) {
1490 // Add A16 operand for intersect_ray instructions
1491 addOperand(MI, MCOperand::createImm(BaseOpcode->A16));
1492 return;
1493 }
1494
1495 bool IsAtomic = (VDstIdx != -1);
1496 bool IsGather4 = SIInstrFlags::isGather4(*MCII, MI);
1497 bool IsVSample = SIInstrFlags::isVSAMPLE(*MCII, MI);
1498 bool IsNSA = false;
1499 bool IsPartialNSA = false;
1500 unsigned AddrSize = Info->VAddrDwords;
1501
1502 if (isGFX10Plus()) {
1503 unsigned DimIdx =
1504 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::dim);
1505 int A16Idx =
1506 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::a16);
1507 const AMDGPU::MIMGDimInfo *Dim =
1508 AMDGPU::getMIMGDimInfoByEncoding(MI.getOperand(DimIdx).getImm());
1509 const bool IsA16 = (A16Idx != -1 && MI.getOperand(A16Idx).getImm());
1510
1511 AddrSize =
1512 AMDGPU::getAddrSizeMIMGOp(BaseOpcode, Dim, IsA16, AMDGPU::hasG16(STI));
1513
1514 // VSAMPLE insts that do not use vaddr3 behave the same as NSA forms.
1515 // VIMAGE insts other than BVH never use vaddr4.
1516 IsNSA = Info->MIMGEncoding == AMDGPU::MIMGEncGfx10NSA ||
1517 Info->MIMGEncoding == AMDGPU::MIMGEncGfx11NSA ||
1518 Info->MIMGEncoding == AMDGPU::MIMGEncGfx12 ||
1519 Info->MIMGEncoding == AMDGPU::MIMGEncGfx13;
1520 if (!IsNSA) {
1521 if (!IsVSample && AddrSize > 12)
1522 AddrSize = 16;
1523 } else {
1524 if (AddrSize > Info->VAddrDwords) {
1525 if (!STI.hasFeature(AMDGPU::FeaturePartialNSAEncoding)) {
1526 // The NSA encoding does not contain enough operands for the
1527 // combination of base opcode / dimension. Should this be an error?
1528 return;
1529 }
1530 IsPartialNSA = true;
1531 }
1532 }
1533 }
1534
1535 unsigned DMask = MI.getOperand(DMaskIdx).getImm() & 0xf;
1536 unsigned DstSize = IsGather4 ? 4 : std::max(llvm::popcount(DMask), 1);
1537
1538 bool D16 = D16Idx >= 0 && MI.getOperand(D16Idx).getImm();
1539 if (D16 && AMDGPU::hasPackedD16(STI)) {
1540 DstSize = (DstSize + 1) / 2;
1541 }
1542
1543 if (TFEIdx != -1 && MI.getOperand(TFEIdx).getImm())
1544 DstSize += 1;
1545
1546 if (DstSize == Info->VDataDwords && AddrSize == Info->VAddrDwords)
1547 return;
1548
1549 int NewOpcode =
1550 AMDGPU::getMIMGOpcode(Info->BaseOpcode, Info->MIMGEncoding, DstSize,
1551 AddrSize, Info->IndexedRsrc, Info->IndexedSamp);
1552 if (NewOpcode == -1)
1553 return;
1554
1555 // Widen the register to the correct number of enabled channels.
1556 MCRegister NewVdata;
1557 if (DstSize != Info->VDataDwords) {
1558 auto DataRCID = MCII->getOpRegClassID(
1559 MCII->get(NewOpcode).operands()[VDataIdx], HwModeRegClass);
1560
1561 // Get first subregister of VData
1562 MCRegister Vdata0 = MI.getOperand(VDataIdx).getReg();
1563 MCRegister VdataSub0 = MRI.getSubReg(Vdata0, AMDGPU::sub0);
1564 Vdata0 = (VdataSub0 != 0)? VdataSub0 : Vdata0;
1565
1566 const MCRegisterClass &NewRC = MRI.getRegClass(DataRCID);
1567 NewVdata = MRI.getMatchingSuperReg(Vdata0, AMDGPU::sub0, &NewRC);
1568 NewVdata = CheckVGPROverflow(NewVdata, NewRC, MRI);
1569 if (!NewVdata) {
1570 // It's possible to encode this such that the low register + enabled
1571 // components exceeds the register count.
1572 return;
1573 }
1574 }
1575
1576 // If not using NSA on GFX10+, widen vaddr0 address register to correct size.
1577 // If using partial NSA on GFX11+ widen last address register.
1578 int VAddrSAIdx = IsPartialNSA ? (RsrcIdx - 1) : VAddr0Idx;
1579 MCRegister NewVAddrSA;
1580 if (STI.hasFeature(AMDGPU::FeatureNSAEncoding) && (!IsNSA || IsPartialNSA) &&
1581 AddrSize != Info->VAddrDwords) {
1582 MCRegister VAddrSA = MI.getOperand(VAddrSAIdx).getReg();
1583 MCRegister VAddrSubSA = MRI.getSubReg(VAddrSA, AMDGPU::sub0);
1584 VAddrSA = VAddrSubSA ? VAddrSubSA : VAddrSA;
1585
1586 auto AddrRCID = MCII->getOpRegClassID(
1587 MCII->get(NewOpcode).operands()[VAddrSAIdx], HwModeRegClass);
1588
1589 const MCRegisterClass &NewRC = MRI.getRegClass(AddrRCID);
1590 NewVAddrSA = MRI.getMatchingSuperReg(VAddrSA, AMDGPU::sub0, &NewRC);
1591 NewVAddrSA = CheckVGPROverflow(NewVAddrSA, NewRC, MRI);
1592 if (!NewVAddrSA)
1593 return;
1594 }
1595
1596 MI.setOpcode(NewOpcode);
1597
1598 if (NewVdata != AMDGPU::NoRegister) {
1599 MI.getOperand(VDataIdx) = MCOperand::createReg(NewVdata);
1600
1601 if (IsAtomic) {
1602 // Atomic operations have an additional operand (a copy of data)
1603 MI.getOperand(VDstIdx) = MCOperand::createReg(NewVdata);
1604 }
1605 }
1606
1607 if (NewVAddrSA) {
1608 MI.getOperand(VAddrSAIdx) = MCOperand::createReg(NewVAddrSA);
1609 } else if (IsNSA) {
1610 assert(AddrSize <= Info->VAddrDwords);
1611 MI.erase(MI.begin() + VAddr0Idx + AddrSize,
1612 MI.begin() + VAddr0Idx + Info->VAddrDwords);
1613 }
1614}
1615
1616// Opsel and neg bits are used in src_modifiers and standalone operands. Autogen
1617// decoder only adds to src_modifiers, so manually add the bits to the other
1618// operands.
1620 unsigned Opc = MI.getOpcode();
1621 unsigned DescNumOps = MCII->get(Opc).getNumOperands();
1622 auto Mods = collectVOPModifiers(MI, true);
1623
1624 if (MI.getNumOperands() < DescNumOps &&
1625 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::vdst_in))
1626 insertNamedMCOperand(MI, MCOperand::createImm(0), AMDGPU::OpName::vdst_in);
1627
1628 if (MI.getNumOperands() < DescNumOps &&
1629 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::op_sel))
1631 AMDGPU::OpName::op_sel);
1632 if (MI.getNumOperands() < DescNumOps &&
1633 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::op_sel_hi))
1635 AMDGPU::OpName::op_sel_hi);
1636 if (MI.getNumOperands() < DescNumOps &&
1637 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::neg_lo))
1639 AMDGPU::OpName::neg_lo);
1640 if (MI.getNumOperands() < DescNumOps &&
1641 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::neg_hi))
1643 AMDGPU::OpName::neg_hi);
1644}
1645
1646// Create dummy old operand and insert optional operands
1648 unsigned Opc = MI.getOpcode();
1649 unsigned DescNumOps = MCII->get(Opc).getNumOperands();
1650
1651 if (MI.getNumOperands() < DescNumOps &&
1652 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::old))
1653 insertNamedMCOperand(MI, MCOperand::createReg(0), AMDGPU::OpName::old);
1654
1655 if (MI.getNumOperands() < DescNumOps &&
1656 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::src0_modifiers))
1658 AMDGPU::OpName::src0_modifiers);
1659
1660 if (MI.getNumOperands() < DescNumOps &&
1661 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::src1_modifiers))
1663 AMDGPU::OpName::src1_modifiers);
1664}
1665
1667 unsigned Opc = MI.getOpcode();
1668 unsigned DescNumOps = MCII->get(Opc).getNumOperands();
1669
1671
1672 if (MI.getNumOperands() < DescNumOps &&
1673 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::op_sel)) {
1676 AMDGPU::OpName::op_sel);
1677 }
1678}
1679
1681 assert(HasLiteral && "Should have decoded a literal");
1682 insertNamedMCOperand(MI, MCOperand::createImm(Literal), AMDGPU::OpName::immX);
1683}
1684
1685const char* AMDGPUDisassembler::getRegClassName(unsigned RegClassID) const {
1687 &getAMDGPUMCRegisterClass(RegClassID));
1688}
1689
1690inline
1692 const Twine& ErrMsg) const {
1693 *CommentStream << "Error: " + ErrMsg;
1694
1695 // ToDo: add support for error operands to MCInst.h
1696 // return MCOperand::createError(V);
1697 return MCOperand();
1698}
1699
1703
1704inline
1706 unsigned Val) const {
1707 const auto &RegCl = getAMDGPUMCRegisterClass(RegClassID);
1708 if (Val >= RegCl.getNumRegs())
1709 return errOperand(Val, Twine(getRegClassName(RegClassID)) +
1710 ": unknown register " + Twine(Val));
1711 return createRegOperand(RegCl.getRegister(Val));
1712}
1713
1714inline
1716 unsigned Val) const {
1717 // ToDo: SI/CI have 104 SGPRs, VI - 102
1718 // Valery: here we accepting as much as we can, let assembler sort it out
1719 int shift = 0;
1720 switch (SRegClassID) {
1721 case AMDGPU::SGPR_32RegClassID:
1722 case AMDGPU::TTMP_32RegClassID:
1723 break;
1724 case AMDGPU::SGPR_64RegClassID:
1725 case AMDGPU::TTMP_64RegClassID:
1726 shift = 1;
1727 break;
1728 case AMDGPU::SGPR_96RegClassID:
1729 case AMDGPU::TTMP_96RegClassID:
1730 case AMDGPU::SGPR_128RegClassID:
1731 case AMDGPU::TTMP_128RegClassID:
1732 // ToDo: unclear if s[100:104] is available on VI. Can we use VCC as SGPR in
1733 // this bundle?
1734 case AMDGPU::SGPR_256RegClassID:
1735 case AMDGPU::TTMP_256RegClassID:
1736 // ToDo: unclear if s[96:104] is available on VI. Can we use VCC as SGPR in
1737 // this bundle?
1738 case AMDGPU::SGPR_288RegClassID:
1739 case AMDGPU::TTMP_288RegClassID:
1740 case AMDGPU::SGPR_320RegClassID:
1741 case AMDGPU::TTMP_320RegClassID:
1742 case AMDGPU::SGPR_352RegClassID:
1743 case AMDGPU::TTMP_352RegClassID:
1744 case AMDGPU::SGPR_384RegClassID:
1745 case AMDGPU::TTMP_384RegClassID:
1746 case AMDGPU::SGPR_512RegClassID:
1747 case AMDGPU::TTMP_512RegClassID:
1748 shift = 2;
1749 break;
1750 // ToDo: unclear if s[88:104] is available on VI. Can we use VCC as SGPR in
1751 // this bundle?
1752 default:
1753 llvm_unreachable("unhandled register class");
1754 }
1755
1756 if (Val % (1 << shift)) {
1757 *CommentStream << "Warning: " << getRegClassName(SRegClassID)
1758 << ": scalar reg isn't aligned " << Val;
1759 }
1760
1761 return createRegOperand(SRegClassID, Val >> shift);
1762}
1763
1765 bool IsHi) const {
1766 unsigned RegIdxInVGPR16 = RegIdx * 2 + (IsHi ? 1 : 0);
1767 return createRegOperand(AMDGPU::VGPR_16RegClassID, RegIdxInVGPR16);
1768}
1769
1770// Decode Literals for insts which always have a literal in the encoding
1773 if (HasLiteral) {
1774 assert(
1776 "Should only decode multiple kimm with VOPD, check VSrc operand types");
1777 if (Literal != Val)
1778 return errOperand(Val, "More than one unique literal is illegal");
1779 }
1780 HasLiteral = true;
1781 Literal = Val;
1782 return MCOperand::createImm(Literal);
1783}
1784
1787 if (HasLiteral) {
1788 if (Literal != Val)
1789 return errOperand(Val, "More than one unique literal is illegal");
1790 }
1791 HasLiteral = true;
1792 Literal = Val;
1793
1794 bool UseLit64 = Hi_32(Literal) == 0;
1796 LitModifier::Lit64, Literal, getContext()))
1797 : MCOperand::createImm(Literal);
1798}
1799
1802 const MCOperandInfo &OpDesc) const {
1803 // For now all literal constants are supposed to be unsigned integer
1804 // ToDo: deal with signed/unsigned 64-bit integer constants
1805 // ToDo: deal with float/double constants
1806 if (!HasLiteral) {
1807 if (Bytes.size() < 4) {
1808 return errOperand(0, "cannot read literal, inst bytes left " +
1809 Twine(Bytes.size()));
1810 }
1811 HasLiteral = true;
1812 Literal = eatBytes<uint32_t>(Bytes);
1813 }
1814
1815 // For disassembling always assume all inline constants are available.
1816 bool HasInv2Pi = true;
1817
1818 // Invalid instruction codes may contain literals for inline-only
1819 // operands, so we support them here as well.
1820 int64_t Val = Literal;
1821 bool UseLit = false;
1822 switch (OpDesc.OperandType) {
1823 default:
1824 llvm_unreachable("Unexpected operand type!");
1828 UseLit = AMDGPU::isInlinableLiteralBF16(Val, HasInv2Pi);
1829 break;
1832 break;
1836 UseLit = AMDGPU::isInlinableLiteralFP16(Val, HasInv2Pi);
1837 break;
1839 UseLit = AMDGPU::isInlinableLiteralV2F16(Val);
1840 break;
1843 break;
1846 break;
1850 UseLit = AMDGPU::isInlinableLiteralI16(Val, HasInv2Pi);
1851 break;
1853 UseLit = AMDGPU::isInlinableLiteralV2I16(Val);
1854 break;
1864 UseLit = AMDGPU::isInlinableLiteral32(Val, HasInv2Pi);
1865 break;
1870 UseLit = AMDGPU::isInlinableLiteral64(Val << 32, HasInv2Pi);
1871 if (!UseLit)
1872 Val <<= 32;
1873 break;
1877 UseLit = AMDGPU::isInlinableLiteral64(Val, HasInv2Pi);
1878 break;
1880 // TODO: Disassembling V_DUAL_FMAMK_F32_X_FMAMK_F32_gfx11 hits
1881 // decoding a literal in a position of a register operand. Give
1882 // it special handling in the caller, decodeImmOperands(), instead
1883 // of quietly allowing it here.
1884 break;
1885 }
1886
1889 : MCOperand::createImm(Val);
1890}
1891
1893 assert(STI.hasFeature(AMDGPU::Feature64BitLiterals));
1894
1895 if (!HasLiteral) {
1896 if (Bytes.size() < 8) {
1897 return errOperand(0, "cannot read literal64, inst bytes left " +
1898 Twine(Bytes.size()));
1899 }
1900 HasLiteral = true;
1901 Literal = eatBytes<uint64_t>(Bytes);
1902 }
1903
1904 bool UseLit64 = Hi_32(Literal) == 0;
1905
1906 UseLit64 |= AMDGPU::isInlinableLiteral64(
1907 Literal, STI.hasFeature(AMDGPU::FeatureInv2PiInlineImm));
1908
1910 LitModifier::Lit64, Literal, getContext()))
1911 : MCOperand::createImm(Literal);
1912}
1913
1915 using namespace AMDGPU::EncValues;
1916
1917 assert(Imm >= INLINE_INTEGER_C_MIN && Imm <= INLINE_INTEGER_C_MAX);
1918 return MCOperand::createImm((Imm <= INLINE_INTEGER_C_POSITIVE_MAX) ?
1919 (static_cast<int64_t>(Imm) - INLINE_INTEGER_C_MIN) :
1920 (INLINE_INTEGER_C_POSITIVE_MAX - static_cast<int64_t>(Imm)));
1921 // Cast prevents negative overflow.
1922}
1923
1924static int64_t getInlineImmVal32(unsigned Imm) {
1925 switch (Imm) {
1926 case 240:
1927 return llvm::bit_cast<uint32_t>(0.5f);
1928 case 241:
1929 return llvm::bit_cast<uint32_t>(-0.5f);
1930 case 242:
1931 return llvm::bit_cast<uint32_t>(1.0f);
1932 case 243:
1933 return llvm::bit_cast<uint32_t>(-1.0f);
1934 case 244:
1935 return llvm::bit_cast<uint32_t>(2.0f);
1936 case 245:
1937 return llvm::bit_cast<uint32_t>(-2.0f);
1938 case 246:
1939 return llvm::bit_cast<uint32_t>(4.0f);
1940 case 247:
1941 return llvm::bit_cast<uint32_t>(-4.0f);
1942 case 248: // 1 / (2 * PI)
1943 return 0x3e22f983;
1944 default:
1945 llvm_unreachable("invalid fp inline imm");
1946 }
1947}
1948
1949static int64_t getInlineImmVal64(unsigned Imm) {
1950 switch (Imm) {
1951 case 240:
1952 return llvm::bit_cast<uint64_t>(0.5);
1953 case 241:
1954 return llvm::bit_cast<uint64_t>(-0.5);
1955 case 242:
1956 return llvm::bit_cast<uint64_t>(1.0);
1957 case 243:
1958 return llvm::bit_cast<uint64_t>(-1.0);
1959 case 244:
1960 return llvm::bit_cast<uint64_t>(2.0);
1961 case 245:
1962 return llvm::bit_cast<uint64_t>(-2.0);
1963 case 246:
1964 return llvm::bit_cast<uint64_t>(4.0);
1965 case 247:
1966 return llvm::bit_cast<uint64_t>(-4.0);
1967 case 248: // 1 / (2 * PI)
1968 return 0x3fc45f306dc9c882;
1969 default:
1970 llvm_unreachable("invalid fp inline imm");
1971 }
1972}
1973
1974static int64_t getInlineImmValF16(unsigned Imm) {
1975 switch (Imm) {
1976 case 240:
1977 return 0x3800;
1978 case 241:
1979 return 0xB800;
1980 case 242:
1981 return 0x3C00;
1982 case 243:
1983 return 0xBC00;
1984 case 244:
1985 return 0x4000;
1986 case 245:
1987 return 0xC000;
1988 case 246:
1989 return 0x4400;
1990 case 247:
1991 return 0xC400;
1992 case 248: // 1 / (2 * PI)
1993 return 0x3118;
1994 default:
1995 llvm_unreachable("invalid fp inline imm");
1996 }
1997}
1998
1999static int64_t getInlineImmValBF16(unsigned Imm) {
2000 switch (Imm) {
2001 case 240:
2002 return 0x3F00;
2003 case 241:
2004 return 0xBF00;
2005 case 242:
2006 return 0x3F80;
2007 case 243:
2008 return 0xBF80;
2009 case 244:
2010 return 0x4000;
2011 case 245:
2012 return 0xC000;
2013 case 246:
2014 return 0x4080;
2015 case 247:
2016 return 0xC080;
2017 case 248: // 1 / (2 * PI)
2018 return 0x3E22;
2019 default:
2020 llvm_unreachable("invalid fp inline imm");
2021 }
2022}
2023
2024unsigned AMDGPUDisassembler::getVgprClassId(unsigned Width) const {
2025 using namespace AMDGPU;
2026
2027 switch (Width) {
2028 case 16:
2029 case 32:
2030 return VGPR_32RegClassID;
2031 case 64:
2032 return VReg_64RegClassID;
2033 case 96:
2034 return VReg_96RegClassID;
2035 case 128:
2036 return VReg_128RegClassID;
2037 case 160:
2038 return VReg_160RegClassID;
2039 case 192:
2040 return VReg_192RegClassID;
2041 case 256:
2042 return VReg_256RegClassID;
2043 case 288:
2044 return VReg_288RegClassID;
2045 case 320:
2046 return VReg_320RegClassID;
2047 case 352:
2048 return VReg_352RegClassID;
2049 case 384:
2050 return VReg_384RegClassID;
2051 case 512:
2052 return VReg_512RegClassID;
2053 case 1024:
2054 return VReg_1024RegClassID;
2055 }
2056 llvm_unreachable("Invalid register width!");
2057}
2058
2059unsigned AMDGPUDisassembler::getAgprClassId(unsigned Width) const {
2060 using namespace AMDGPU;
2061
2062 switch (Width) {
2063 case 16:
2064 case 32:
2065 return AGPR_32RegClassID;
2066 case 64:
2067 return AReg_64RegClassID;
2068 case 96:
2069 return AReg_96RegClassID;
2070 case 128:
2071 return AReg_128RegClassID;
2072 case 160:
2073 return AReg_160RegClassID;
2074 case 256:
2075 return AReg_256RegClassID;
2076 case 288:
2077 return AReg_288RegClassID;
2078 case 320:
2079 return AReg_320RegClassID;
2080 case 352:
2081 return AReg_352RegClassID;
2082 case 384:
2083 return AReg_384RegClassID;
2084 case 512:
2085 return AReg_512RegClassID;
2086 case 1024:
2087 return AReg_1024RegClassID;
2088 }
2089 llvm_unreachable("Invalid register width!");
2090}
2091
2092std::optional<unsigned>
2094 using namespace AMDGPU;
2095
2096 switch (Width) {
2097 case 16:
2098 case 32:
2099 return SGPR_32RegClassID;
2100 case 64:
2101 return SGPR_64RegClassID;
2102 case 96:
2103 return SGPR_96RegClassID;
2104 case 128:
2105 return SGPR_128RegClassID;
2106 case 160:
2107 return SGPR_160RegClassID;
2108 case 256:
2109 return SGPR_256RegClassID;
2110 case 288:
2111 return SGPR_288RegClassID;
2112 case 320:
2113 return SGPR_320RegClassID;
2114 case 352:
2115 return SGPR_352RegClassID;
2116 case 384:
2117 return SGPR_384RegClassID;
2118 case 512:
2119 return SGPR_512RegClassID;
2120 }
2121 return std::nullopt;
2122}
2123
2124std::optional<unsigned>
2126 using namespace AMDGPU;
2127
2128 switch (Width) {
2129 case 16:
2130 case 32:
2131 return TTMP_32RegClassID;
2132 case 64:
2133 return TTMP_64RegClassID;
2134 case 128:
2135 return TTMP_128RegClassID;
2136 case 256:
2137 return TTMP_256RegClassID;
2138 case 288:
2139 return TTMP_288RegClassID;
2140 case 320:
2141 return TTMP_320RegClassID;
2142 case 352:
2143 return TTMP_352RegClassID;
2144 case 384:
2145 return TTMP_384RegClassID;
2146 case 512:
2147 return TTMP_512RegClassID;
2148 }
2149 return std::nullopt;
2150}
2151
2152int AMDGPUDisassembler::getTTmpIdx(unsigned Val) const {
2153 using namespace AMDGPU::EncValues;
2154
2155 unsigned TTmpMin = isGFX9Plus() ? TTMP_GFX9PLUS_MIN : TTMP_VI_MIN;
2156 unsigned TTmpMax = isGFX9Plus() ? TTMP_GFX9PLUS_MAX : TTMP_VI_MAX;
2157
2158 return (TTmpMin <= Val && Val <= TTmpMax)? Val - TTmpMin : -1;
2159}
2160
2162 unsigned Val) const {
2163 using namespace AMDGPU::EncValues;
2164
2165 assert(Val < 1024); // enum10
2166
2167 bool IsAGPR = Val & 512;
2168 Val &= 511;
2169
2170 if (VGPR_MIN <= Val && Val <= VGPR_MAX) {
2171 return createRegOperand(IsAGPR ? getAgprClassId(Width)
2172 : getVgprClassId(Width), Val - VGPR_MIN);
2173 }
2174 return decodeNonVGPRSrcOp(Inst, Width, Val & 0xFF);
2175}
2176
2178 unsigned Width,
2179 unsigned Val) const {
2180 // Cases when Val{8} is 1 (vgpr, agpr or true 16 vgpr) should have been
2181 // decoded earlier.
2182 assert(Val < (1 << 8) && "9-bit Src encoding when Val{8} is 0");
2183 using namespace AMDGPU::EncValues;
2184
2185 // Not every operand width has a supported non-VGPR source encoding.
2186 // Selecting an unsupported SGPR, ttmp, or special register is malformed.
2187 auto UnsupportedWidth = [&]() {
2188 return errOperand(Val, "unsupported " + Twine(Width) +
2189 "-bit non-VGPR operand encoding " + Twine(Val));
2190 };
2191
2192 if (Val <= SGPR_MAX) {
2193 // "SGPR_MIN <= Val" is always true and causes compilation warning.
2194 static_assert(SGPR_MIN == 0);
2195 std::optional<unsigned> ClassId = getSgprClassId(Width);
2196 if (!ClassId)
2197 return UnsupportedWidth();
2198 return createSRegOperand(*ClassId, Val - SGPR_MIN);
2199 }
2200
2201 int TTmpIdx = getTTmpIdx(Val);
2202 if (TTmpIdx >= 0) {
2203 std::optional<unsigned> ClassId = getTtmpClassId(Width);
2204 if (!ClassId)
2205 return UnsupportedWidth();
2206 return createSRegOperand(*ClassId, TTmpIdx);
2207 }
2208
2209 if ((INLINE_INTEGER_C_MIN <= Val && Val <= INLINE_INTEGER_C_MAX) ||
2210 (INLINE_FLOATING_C_MIN <= Val && Val <= INLINE_FLOATING_C_MAX) ||
2211 Val == LITERAL_CONST)
2212 return MCOperand::createImm(Val);
2213
2214 if (Val == LITERAL64_CONST && STI.hasFeature(AMDGPU::Feature64BitLiterals)) {
2215 // Only VOP1, VOP2, VOPC, SOP1, SOP2 and SOPC may encode a 64-bit literal.
2216 // VOP3, VOP3P and VOPD have to use a 32-bit one.
2217 if (SIInstrFlags::isVOP3Like(*MCII, Inst) ||
2218 AMDGPU::isVOPD(Inst.getOpcode())) {
2219 return errOperand(Val,
2220 "64-bit literal is not supported by this instruction");
2221 }
2222 return decodeLiteral64Constant();
2223 }
2224
2225 switch (Width) {
2226 case 32:
2227 case 16:
2228 return decodeSpecialReg32(Val);
2229 case 64:
2230 return decodeSpecialReg64(Val);
2231 case 96:
2232 case 128:
2233 case 256:
2234 case 512:
2235 return decodeSpecialReg96Plus(Val);
2236 default:
2237 return UnsupportedWidth();
2238 }
2239}
2240
2241// Bit 0 of DstY isn't stored in the instruction, because it's always the
2242// opposite of bit 0 of DstX.
2244 unsigned Val) const {
2245 int VDstXInd =
2246 AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::vdstX);
2247 assert(VDstXInd != -1);
2248 assert(Inst.getOperand(VDstXInd).isReg());
2249 unsigned XDstReg = MRI.getEncodingValue(Inst.getOperand(VDstXInd).getReg());
2250 Val |= ~XDstReg & 1;
2251 return createRegOperand(getVgprClassId(32), Val);
2252}
2253
2255 using namespace AMDGPU;
2256
2257 switch (Val) {
2258 // clang-format off
2259 case 102: return createRegOperand(FLAT_SCR_LO);
2260 case 103: return createRegOperand(FLAT_SCR_HI);
2261 case 104: return createRegOperand(XNACK_MASK_LO);
2262 case 105: return createRegOperand(XNACK_MASK_HI);
2263 case 106: return createRegOperand(VCC_LO);
2264 case 107: return createRegOperand(VCC_HI);
2265 case 108: return createRegOperand(TBA_LO);
2266 case 109: return createRegOperand(TBA_HI);
2267 case 110: return createRegOperand(TMA_LO);
2268 case 111: return createRegOperand(TMA_HI);
2269 case 124:
2270 return isGFX11Plus() ? createRegOperand(SGPR_NULL) : createRegOperand(M0);
2271 case 125:
2272 return isGFX11Plus() ? createRegOperand(M0) : createRegOperand(SGPR_NULL);
2273 case 126: return createRegOperand(EXEC_LO);
2274 case 127: return createRegOperand(EXEC_HI);
2275 case 230: return createRegOperand(SRC_FLAT_SCRATCH_BASE_LO);
2276 case 231: return createRegOperand(SRC_FLAT_SCRATCH_BASE_HI);
2277 case 235: return createRegOperand(SRC_SHARED_BASE_LO);
2278 case 236: return createRegOperand(SRC_SHARED_LIMIT_LO);
2279 case 237:
2281 return createRegOperand(SRC_PRIVATE_BASE_LO);
2282 break;
2283 case 238:
2285 return createRegOperand(SRC_PRIVATE_LIMIT_LO);
2286 break;
2287 case 239:
2289 return createRegOperand(SRC_POPS_EXITING_WAVE_ID);
2290 break;
2291 case 251:
2292 if (!isGFX11Plus())
2293 return createRegOperand(SRC_VCCZ);
2294 break;
2295 case 252:
2296 if (!isGFX11Plus())
2297 return createRegOperand(SRC_EXECZ);
2298 break;
2299 case 253: return createRegOperand(SRC_SCC);
2300 case 254: return createRegOperand(LDS_DIRECT);
2301 default: break;
2302 // clang-format on
2303 }
2304 return errOperand(Val, "unknown operand encoding " + Twine(Val));
2305}
2306
2308 using namespace AMDGPU;
2309
2310 switch (Val) {
2311 case 102: return createRegOperand(FLAT_SCR);
2312 case 104: return createRegOperand(XNACK_MASK);
2313 case 106: return createRegOperand(VCC);
2314 case 108: return createRegOperand(TBA);
2315 case 110: return createRegOperand(TMA);
2316 case 124:
2317 if (isGFX11Plus())
2318 return createRegOperand(SGPR_NULL);
2319 break;
2320 case 125:
2321 if (!isGFX11Plus())
2322 return createRegOperand(SGPR_NULL);
2323 break;
2324 case 126: return createRegOperand(EXEC);
2325 case 230: return createRegOperand(SRC_FLAT_SCRATCH_BASE_LO);
2326 case 235: return createRegOperand(SRC_SHARED_BASE);
2327 case 236: return createRegOperand(SRC_SHARED_LIMIT);
2328 case 237:
2330 return createRegOperand(SRC_PRIVATE_BASE);
2331 break;
2332 case 238:
2334 return createRegOperand(SRC_PRIVATE_LIMIT);
2335 break;
2336 case 239:
2338 return createRegOperand(SRC_POPS_EXITING_WAVE_ID);
2339 break;
2340 case 251:
2341 if (!isGFX11Plus())
2342 return createRegOperand(SRC_VCCZ);
2343 break;
2344 case 252:
2345 if (!isGFX11Plus())
2346 return createRegOperand(SRC_EXECZ);
2347 break;
2348 case 253: return createRegOperand(SRC_SCC);
2349 default: break;
2350 }
2351 return errOperand(Val, "unknown operand encoding " + Twine(Val));
2352}
2353
2355 using namespace AMDGPU;
2356
2357 switch (Val) {
2358 case 124:
2359 if (isGFX11Plus())
2360 return createRegOperand(SGPR_NULL);
2361 break;
2362 case 125:
2363 if (!isGFX11Plus())
2364 return createRegOperand(SGPR_NULL);
2365 break;
2366 default:
2367 break;
2368 }
2369 return errOperand(Val, "unknown operand encoding " + Twine(Val));
2370}
2371
2373 const unsigned Val) const {
2374 using namespace AMDGPU::SDWA;
2375 using namespace AMDGPU::EncValues;
2376
2377 if (STI.hasFeature(AMDGPU::FeatureGFX9) ||
2378 STI.hasFeature(AMDGPU::FeatureGFX10)) {
2379 // XXX: cast to int is needed to avoid stupid warning:
2380 // compare with unsigned is always true
2381 if (int(SDWA9EncValues::SRC_VGPR_MIN) <= int(Val) &&
2382 Val <= SDWA9EncValues::SRC_VGPR_MAX) {
2383 return createRegOperand(getVgprClassId(Width),
2384 Val - SDWA9EncValues::SRC_VGPR_MIN);
2385 }
2386 if (SDWA9EncValues::SRC_SGPR_MIN <= Val &&
2387 Val <= (isGFX10Plus() ? SDWA9EncValues::SRC_SGPR_MAX_GFX10
2388 : SDWA9EncValues::SRC_SGPR_MAX_SI)) {
2389 return createSRegOperand(*getSgprClassId(Width),
2390 Val - SDWA9EncValues::SRC_SGPR_MIN);
2391 }
2392 if (SDWA9EncValues::SRC_TTMP_MIN <= Val &&
2393 Val <= SDWA9EncValues::SRC_TTMP_MAX) {
2394 return createSRegOperand(*getTtmpClassId(Width),
2395 Val - SDWA9EncValues::SRC_TTMP_MIN);
2396 }
2397
2398 const unsigned SVal = Val - SDWA9EncValues::SRC_SGPR_MIN;
2399
2400 if ((INLINE_INTEGER_C_MIN <= SVal && SVal <= INLINE_INTEGER_C_MAX) ||
2401 (INLINE_FLOATING_C_MIN <= SVal && SVal <= INLINE_FLOATING_C_MAX))
2402 return MCOperand::createImm(SVal);
2403
2404 return decodeSpecialReg32(SVal);
2405 }
2406 if (STI.hasFeature(AMDGPU::FeatureVolcanicIslands))
2407 return createRegOperand(getVgprClassId(Width), Val);
2408 llvm_unreachable("unsupported target");
2409}
2410
2412 return decodeSDWASrc(16, Val);
2413}
2414
2416 return decodeSDWASrc(32, Val);
2417}
2418
2420 using namespace AMDGPU::SDWA;
2421
2422 assert((STI.hasFeature(AMDGPU::FeatureGFX9) ||
2423 STI.hasFeature(AMDGPU::FeatureGFX10)) &&
2424 "SDWAVopcDst should be present only on GFX9+");
2425
2426 bool IsWave32 = STI.hasFeature(AMDGPU::FeatureWavefrontSize32);
2427
2428 if (Val & SDWA9EncValues::VOPC_DST_VCC_MASK) {
2429 Val &= SDWA9EncValues::VOPC_DST_SGPR_MASK;
2430
2431 int TTmpIdx = getTTmpIdx(Val);
2432 if (TTmpIdx >= 0)
2433 return createSRegOperand(*getTtmpClassId(IsWave32 ? 32 : 64), TTmpIdx);
2434 if (Val > SGPR_MAX) {
2435 return IsWave32 ? decodeSpecialReg32(Val) : decodeSpecialReg64(Val);
2436 }
2437 return createSRegOperand(*getSgprClassId(IsWave32 ? 32 : 64), Val);
2438 }
2439 return createRegOperand(IsWave32 ? AMDGPU::VCC_LO : AMDGPU::VCC);
2440}
2441
2443 unsigned Val) const {
2444 return STI.hasFeature(AMDGPU::FeatureWavefrontSize32)
2445 ? decodeSrcOp(Inst, 32, Val)
2446 : decodeSrcOp(Inst, 64, Val);
2447}
2448
2450 unsigned Val) const {
2451 using namespace AMDGPU::EncValues;
2452 constexpr unsigned M0Encoding = 125;
2453 bool IsValidBarrier =
2454 Val == M0Encoding ||
2455 (INLINE_INTEGER_C_MIN <= Val && Val < INLINE_INTEGER_C_MIN + 32) ||
2456 (INLINE_INTEGER_C_POSITIVE_MAX < Val &&
2457 Val <= INLINE_INTEGER_C_POSITIVE_MAX + 4);
2458 if (!IsValidBarrier)
2459 return MCOperand();
2460 return decodeSrcOp(Inst, 32, Val);
2461}
2462
2465 return MCOperand();
2466 return MCOperand::createImm(Val);
2467}
2468
2470 using VersionField = AMDGPU::EncodingField<7, 0>;
2471 using W64Bit = AMDGPU::EncodingBit<13>;
2472 using W32Bit = AMDGPU::EncodingBit<14>;
2473 using MDPBit = AMDGPU::EncodingBit<15>;
2475
2476 auto [Version, W64, W32, MDP] = Encoding::decode(Imm);
2477
2478 // Decode into a plain immediate if any unused bits are raised.
2479 if (Encoding::encode(Version, W64, W32, MDP) != Imm)
2480 return MCOperand::createImm(Imm);
2481
2482 const auto &Versions = AMDGPU::UCVersion::getGFXVersions();
2483 const auto *I = find_if(
2484 Versions, [Version = Version](const AMDGPU::UCVersion::GFXVersion &V) {
2485 return V.Code == Version;
2486 });
2487 MCContext &Ctx = getContext();
2488 const MCExpr *E;
2489 if (I == Versions.end())
2491 else
2492 E = MCSymbolRefExpr::create(Ctx.getOrCreateSymbol(I->Symbol), Ctx);
2493
2494 if (W64)
2495 E = MCBinaryExpr::createOr(E, UCVersionW64Expr, Ctx);
2496 if (W32)
2497 E = MCBinaryExpr::createOr(E, UCVersionW32Expr, Ctx);
2498 if (MDP)
2499 E = MCBinaryExpr::createOr(E, UCVersionMDPExpr, Ctx);
2500
2501 return MCOperand::createExpr(E);
2502}
2503
2505 return STI.hasFeature(AMDGPU::FeatureVolcanicIslands);
2506}
2507
2509
2511 return STI.hasFeature(AMDGPU::FeatureGFX90AInsts);
2512}
2513
2515
2517
2521
2523 return STI.hasFeature(AMDGPU::FeatureGFX11);
2524}
2525
2529
2531 return STI.hasFeature(AMDGPU::FeatureGFX11_7Insts);
2532}
2533
2535 return STI.hasFeature(AMDGPU::FeatureGFX12);
2536}
2537
2541
2543
2547
2549
2553
2555 return STI.hasFeature(AMDGPU::FeatureArchitectedFlatScratch);
2556}
2557
2561//===----------------------------------------------------------------------===//
2562// AMDGPU specific symbol handling
2563//===----------------------------------------------------------------------===//
2564
2565/// Print a string describing the reserved bit range specified by Mask with
2566/// offset BaseBytes for use in error comments. Mask is a single continuous
2567/// range of 1s surrounded by zeros. The format here is meant to align with the
2568/// tables that describe these bits in llvm.org/docs/AMDGPUUsage.html.
2569static SmallString<32> getBitRangeFromMask(uint32_t Mask, unsigned BaseBytes) {
2570 SmallString<32> Result;
2571 raw_svector_ostream S(Result);
2572
2573 int TrailingZeros = llvm::countr_zero(Mask);
2574 int PopCount = llvm::popcount(Mask);
2575
2576 if (PopCount == 1) {
2577 S << "bit (" << (TrailingZeros + BaseBytes * CHAR_BIT) << ')';
2578 } else {
2579 S << "bits in range ("
2580 << (TrailingZeros + PopCount - 1 + BaseBytes * CHAR_BIT) << ':'
2581 << (TrailingZeros + BaseBytes * CHAR_BIT) << ')';
2582 }
2583
2584 return Result;
2585}
2586
2587#define GET_FIELD(MASK) (AMDHSA_BITS_GET(FourByteBuffer, MASK))
2588#define PRINT_DIRECTIVE(DIRECTIVE, MASK) \
2589 do { \
2590 KdStream << Indent << DIRECTIVE " " << GET_FIELD(MASK) << '\n'; \
2591 } while (0)
2592#define PRINT_PSEUDO_DIRECTIVE_COMMENT(DIRECTIVE, MASK) \
2593 do { \
2594 KdStream << Indent << MAI.getCommentString() << ' ' << DIRECTIVE " " \
2595 << GET_FIELD(MASK) << '\n'; \
2596 } while (0)
2597
2598#define CHECK_RESERVED_BITS_IMPL(MASK, DESC, MSG) \
2599 do { \
2600 if (FourByteBuffer & (MASK)) { \
2601 return createStringError(std::errc::invalid_argument, \
2602 "kernel descriptor " DESC \
2603 " reserved %s set" MSG, \
2604 getBitRangeFromMask((MASK), 0).c_str()); \
2605 } \
2606 } while (0)
2607
2608#define CHECK_RESERVED_BITS(MASK) CHECK_RESERVED_BITS_IMPL(MASK, #MASK, "")
2609#define CHECK_RESERVED_BITS_MSG(MASK, MSG) \
2610 CHECK_RESERVED_BITS_IMPL(MASK, #MASK, ", " MSG)
2611#define CHECK_RESERVED_BITS_DESC(MASK, DESC) \
2612 CHECK_RESERVED_BITS_IMPL(MASK, DESC, "")
2613#define CHECK_RESERVED_BITS_DESC_MSG(MASK, DESC, MSG) \
2614 CHECK_RESERVED_BITS_IMPL(MASK, DESC, ", " MSG)
2615
2616// NOLINTNEXTLINE(readability-identifier-naming)
2618 uint32_t FourByteBuffer, raw_string_ostream &KdStream) const {
2619 using namespace amdhsa;
2620 StringRef Indent = "\t";
2621
2622 // We cannot accurately backward compute #VGPRs used from
2623 // GRANULATED_WORKITEM_VGPR_COUNT. But we are concerned with getting the same
2624 // value of GRANULATED_WORKITEM_VGPR_COUNT in the reassembled binary. So we
2625 // simply calculate the inverse of what the assembler does.
2626
2627 uint32_t GranulatedWorkitemVGPRCount =
2628 GET_FIELD(COMPUTE_PGM_RSRC1_GRANULATED_WORKITEM_VGPR_COUNT);
2629
2630 uint32_t NextFreeVGPR =
2631 (GranulatedWorkitemVGPRCount + 1) *
2632 AMDGPU::IsaInfo::getVGPREncodingGranule(STI, EnableWavefrontSize32);
2633
2634 KdStream << Indent << ".amdhsa_next_free_vgpr " << NextFreeVGPR << '\n';
2635
2636 // We cannot backward compute values used to calculate
2637 // GRANULATED_WAVEFRONT_SGPR_COUNT. Hence the original values for following
2638 // directives can't be computed:
2639 // .amdhsa_reserve_vcc
2640 // .amdhsa_reserve_flat_scratch
2641 // .amdhsa_reserve_xnack_mask
2642 // They take their respective default values if not specified in the assembly.
2643 //
2644 // GRANULATED_WAVEFRONT_SGPR_COUNT
2645 // = f(NEXT_FREE_SGPR + VCC + FLAT_SCRATCH + XNACK_MASK)
2646 //
2647 // We compute the inverse as though all directives apart from NEXT_FREE_SGPR
2648 // are set to 0. So while disassembling we consider that:
2649 //
2650 // GRANULATED_WAVEFRONT_SGPR_COUNT
2651 // = f(NEXT_FREE_SGPR + 0 + 0 + 0)
2652 //
2653 // The disassembler cannot recover the original values of those 3 directives.
2654
2655 uint32_t GranulatedWavefrontSGPRCount =
2656 GET_FIELD(COMPUTE_PGM_RSRC1_GRANULATED_WAVEFRONT_SGPR_COUNT);
2657
2658 if (isGFX10Plus())
2659 CHECK_RESERVED_BITS_MSG(COMPUTE_PGM_RSRC1_GRANULATED_WAVEFRONT_SGPR_COUNT,
2660 "must be zero on gfx10+");
2661
2662 uint32_t NextFreeSGPR = (GranulatedWavefrontSGPRCount + 1) *
2664
2665 KdStream << Indent << ".amdhsa_reserve_vcc " << 0 << '\n';
2667 KdStream << Indent << ".amdhsa_reserve_flat_scratch " << 0 << '\n';
2668 // Only print the directive on xnack-supporting targets (matching the
2669 // asmprinter), unless the binary erronously set xnack on an unsupported
2670 // target
2671 bool ReservedXnackMask = TargetID.isXnackOnOrAny();
2672 if (STI.hasFeature(AMDGPU::FeatureSupportsXNACK) || ReservedXnackMask) {
2673 KdStream << Indent << ".amdhsa_reserve_xnack_mask " << ReservedXnackMask
2674 << '\n';
2675 }
2676 KdStream << Indent << ".amdhsa_next_free_sgpr " << NextFreeSGPR << "\n";
2677
2678 CHECK_RESERVED_BITS(COMPUTE_PGM_RSRC1_PRIORITY);
2679
2680 PRINT_DIRECTIVE(".amdhsa_float_round_mode_32",
2681 COMPUTE_PGM_RSRC1_FLOAT_ROUND_MODE_32);
2682 PRINT_DIRECTIVE(".amdhsa_float_round_mode_16_64",
2683 COMPUTE_PGM_RSRC1_FLOAT_ROUND_MODE_16_64);
2684 PRINT_DIRECTIVE(".amdhsa_float_denorm_mode_32",
2685 COMPUTE_PGM_RSRC1_FLOAT_DENORM_MODE_32);
2686 PRINT_DIRECTIVE(".amdhsa_float_denorm_mode_16_64",
2687 COMPUTE_PGM_RSRC1_FLOAT_DENORM_MODE_16_64);
2688
2689 CHECK_RESERVED_BITS(COMPUTE_PGM_RSRC1_PRIV);
2690
2691 if (STI.hasFeature(AMDGPU::FeatureDX10ClampAndIEEEMode))
2692 PRINT_DIRECTIVE(".amdhsa_dx10_clamp",
2693 COMPUTE_PGM_RSRC1_GFX6_GFX11_ENABLE_DX10_CLAMP);
2694
2695 CHECK_RESERVED_BITS(COMPUTE_PGM_RSRC1_DEBUG_MODE);
2696
2697 if (STI.hasFeature(AMDGPU::FeatureDX10ClampAndIEEEMode))
2698 PRINT_DIRECTIVE(".amdhsa_ieee_mode",
2699 COMPUTE_PGM_RSRC1_GFX6_GFX11_ENABLE_IEEE_MODE);
2700
2701 CHECK_RESERVED_BITS(COMPUTE_PGM_RSRC1_BULKY);
2702 CHECK_RESERVED_BITS(COMPUTE_PGM_RSRC1_CDBG_USER);
2703
2704 // Bits [26].
2705 if (isGFX9Plus()) {
2706 PRINT_DIRECTIVE(".amdhsa_fp16_overflow", COMPUTE_PGM_RSRC1_GFX9_PLUS_FP16_OVFL);
2707 } else {
2708 CHECK_RESERVED_BITS_DESC_MSG(COMPUTE_PGM_RSRC1_GFX6_GFX8_RESERVED0,
2709 "COMPUTE_PGM_RSRC1", "must be zero pre-gfx9");
2710 }
2711
2712 // Bits [27].
2713 if (isGFX1250Plus()) {
2714 PRINT_PSEUDO_DIRECTIVE_COMMENT("FLAT_SCRATCH_IS_NV",
2715 COMPUTE_PGM_RSRC1_GFX125_FLAT_SCRATCH_IS_NV);
2716 } else {
2717 CHECK_RESERVED_BITS_DESC(COMPUTE_PGM_RSRC1_GFX6_GFX120_RESERVED1,
2718 "COMPUTE_PGM_RSRC1");
2719 }
2720
2721 // Bits [28].
2722 CHECK_RESERVED_BITS_DESC(COMPUTE_PGM_RSRC1_RESERVED2, "COMPUTE_PGM_RSRC1");
2723
2724 // Bits [29-31].
2725 if (isGFX10Plus()) {
2726 // WGP_MODE is not available on GFX1250.
2727 if (!isGFX1250Plus()) {
2728 PRINT_DIRECTIVE(".amdhsa_workgroup_processor_mode",
2729 COMPUTE_PGM_RSRC1_GFX10_PLUS_WGP_MODE);
2730 }
2731 PRINT_DIRECTIVE(".amdhsa_memory_ordered", COMPUTE_PGM_RSRC1_GFX10_PLUS_MEM_ORDERED);
2732 PRINT_DIRECTIVE(".amdhsa_forward_progress", COMPUTE_PGM_RSRC1_GFX10_PLUS_FWD_PROGRESS);
2733 } else {
2734 CHECK_RESERVED_BITS_DESC(COMPUTE_PGM_RSRC1_GFX6_GFX9_RESERVED3,
2735 "COMPUTE_PGM_RSRC1");
2736 }
2737
2738 if (isGFX12Plus())
2739 PRINT_DIRECTIVE(".amdhsa_round_robin_scheduling",
2740 COMPUTE_PGM_RSRC1_GFX12_PLUS_ENABLE_WG_RR_EN);
2741
2742 return true;
2743}
2744
2745// NOLINTNEXTLINE(readability-identifier-naming)
2747 uint32_t FourByteBuffer, raw_string_ostream &KdStream) const {
2748 using namespace amdhsa;
2749 StringRef Indent = "\t";
2751 PRINT_DIRECTIVE(".amdhsa_enable_private_segment",
2752 COMPUTE_PGM_RSRC2_ENABLE_PRIVATE_SEGMENT);
2753 else
2754 PRINT_DIRECTIVE(".amdhsa_system_sgpr_private_segment_wavefront_offset",
2755 COMPUTE_PGM_RSRC2_ENABLE_PRIVATE_SEGMENT);
2756 PRINT_DIRECTIVE(".amdhsa_system_sgpr_workgroup_id_x",
2757 COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_ID_X);
2758 PRINT_DIRECTIVE(".amdhsa_system_sgpr_workgroup_id_y",
2759 COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_ID_Y);
2760 PRINT_DIRECTIVE(".amdhsa_system_sgpr_workgroup_id_z",
2761 COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_ID_Z);
2762 PRINT_DIRECTIVE(".amdhsa_system_sgpr_workgroup_info",
2763 COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_INFO);
2764 PRINT_DIRECTIVE(".amdhsa_system_vgpr_workitem_id",
2765 COMPUTE_PGM_RSRC2_ENABLE_VGPR_WORKITEM_ID);
2766
2767 CHECK_RESERVED_BITS(COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_ADDRESS_WATCH);
2768 CHECK_RESERVED_BITS(COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_MEMORY);
2769 CHECK_RESERVED_BITS(COMPUTE_PGM_RSRC2_GRANULATED_LDS_SIZE);
2770
2772 ".amdhsa_exception_fp_ieee_invalid_op",
2773 COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_INVALID_OPERATION);
2774 PRINT_DIRECTIVE(".amdhsa_exception_fp_denorm_src",
2775 COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_FP_DENORMAL_SOURCE);
2777 ".amdhsa_exception_fp_ieee_div_zero",
2778 COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_DIVISION_BY_ZERO);
2779 PRINT_DIRECTIVE(".amdhsa_exception_fp_ieee_overflow",
2780 COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_OVERFLOW);
2781 PRINT_DIRECTIVE(".amdhsa_exception_fp_ieee_underflow",
2782 COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_UNDERFLOW);
2783 PRINT_DIRECTIVE(".amdhsa_exception_fp_ieee_inexact",
2784 COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_INEXACT);
2785 PRINT_DIRECTIVE(".amdhsa_exception_int_div_zero",
2786 COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_INT_DIVIDE_BY_ZERO);
2787
2788 CHECK_RESERVED_BITS_DESC(COMPUTE_PGM_RSRC2_RESERVED0, "COMPUTE_PGM_RSRC2");
2789
2790 return true;
2791}
2792
2793// NOLINTNEXTLINE(readability-identifier-naming)
2795 uint32_t FourByteBuffer, raw_string_ostream &KdStream) const {
2796 using namespace amdhsa;
2797 StringRef Indent = "\t";
2798 if (isGFX90A()) {
2799 KdStream << Indent << ".amdhsa_accum_offset "
2800 << (GET_FIELD(COMPUTE_PGM_RSRC3_GFX90A_ACCUM_OFFSET) + 1) * 4
2801 << '\n';
2802
2803 PRINT_DIRECTIVE(".amdhsa_tg_split", COMPUTE_PGM_RSRC3_GFX90A_TG_SPLIT);
2804
2805 CHECK_RESERVED_BITS_DESC_MSG(COMPUTE_PGM_RSRC3_GFX90A_RESERVED0,
2806 "COMPUTE_PGM_RSRC3", "must be zero on gfx90a");
2807 CHECK_RESERVED_BITS_DESC_MSG(COMPUTE_PGM_RSRC3_GFX90A_RESERVED1,
2808 "COMPUTE_PGM_RSRC3", "must be zero on gfx90a");
2809 } else if (isGFX10Plus()) {
2810 // Bits [0-3].
2811 if (!isGFX12Plus()) {
2812 if (!EnableWavefrontSize32 || !*EnableWavefrontSize32) {
2813 PRINT_DIRECTIVE(".amdhsa_shared_vgpr_count",
2814 COMPUTE_PGM_RSRC3_GFX10_GFX11_SHARED_VGPR_COUNT);
2815 } else {
2817 "SHARED_VGPR_COUNT",
2818 COMPUTE_PGM_RSRC3_GFX10_GFX11_SHARED_VGPR_COUNT);
2819 }
2820 } else {
2821 CHECK_RESERVED_BITS_DESC_MSG(COMPUTE_PGM_RSRC3_GFX12_PLUS_RESERVED0,
2822 "COMPUTE_PGM_RSRC3",
2823 "must be zero on gfx12+");
2824 }
2825
2826 // Bits [4-11].
2827 if (isGFX11()) {
2828 PRINT_DIRECTIVE(".amdhsa_inst_pref_size",
2829 COMPUTE_PGM_RSRC3_GFX11_INST_PREF_SIZE);
2830 PRINT_PSEUDO_DIRECTIVE_COMMENT("TRAP_ON_START",
2831 COMPUTE_PGM_RSRC3_GFX11_TRAP_ON_START);
2832 PRINT_PSEUDO_DIRECTIVE_COMMENT("TRAP_ON_END",
2833 COMPUTE_PGM_RSRC3_GFX11_TRAP_ON_END);
2834 } else if (isGFX12Plus()) {
2835 PRINT_DIRECTIVE(".amdhsa_inst_pref_size",
2836 COMPUTE_PGM_RSRC3_GFX12_PLUS_INST_PREF_SIZE);
2837 } else {
2838 CHECK_RESERVED_BITS_DESC_MSG(COMPUTE_PGM_RSRC3_GFX10_RESERVED1,
2839 "COMPUTE_PGM_RSRC3",
2840 "must be zero on gfx10");
2841 }
2842
2843 // Bits [12].
2844 CHECK_RESERVED_BITS_DESC_MSG(COMPUTE_PGM_RSRC3_GFX10_PLUS_RESERVED2,
2845 "COMPUTE_PGM_RSRC3", "must be zero on gfx10+");
2846
2847 // Bits [13].
2848 if (isGFX12Plus()) {
2850 COMPUTE_PGM_RSRC3_GFX12_PLUS_GLG_EN);
2851 } else {
2852 CHECK_RESERVED_BITS_DESC_MSG(COMPUTE_PGM_RSRC3_GFX10_GFX11_RESERVED3,
2853 "COMPUTE_PGM_RSRC3",
2854 "must be zero on gfx10 or gfx11");
2855 }
2856
2857 // Bits [14-21].
2858 if (isGFX1250Plus()) {
2859 PRINT_DIRECTIVE(".amdhsa_named_barrier_count",
2860 COMPUTE_PGM_RSRC3_GFX125_NAMED_BAR_CNT);
2862 "ENABLE_DYNAMIC_VGPR", COMPUTE_PGM_RSRC3_GFX125_ENABLE_DYNAMIC_VGPR);
2864 COMPUTE_PGM_RSRC3_GFX125_TCP_SPLIT);
2866 "ENABLE_DIDT_THROTTLE",
2867 COMPUTE_PGM_RSRC3_GFX125_ENABLE_DIDT_THROTTLE);
2868 } else {
2869 CHECK_RESERVED_BITS_DESC_MSG(COMPUTE_PGM_RSRC3_GFX10_GFX120_RESERVED4,
2870 "COMPUTE_PGM_RSRC3",
2871 "must be zero on gfx10+");
2872 }
2873
2874 // Bits [22-30].
2875 CHECK_RESERVED_BITS_DESC_MSG(COMPUTE_PGM_RSRC3_GFX10_PLUS_RESERVED5,
2876 "COMPUTE_PGM_RSRC3", "must be zero on gfx10+");
2877
2878 // Bits [31].
2879 if (isGFX11Plus()) {
2881 COMPUTE_PGM_RSRC3_GFX11_PLUS_IMAGE_OP);
2882 } else {
2883 CHECK_RESERVED_BITS_DESC_MSG(COMPUTE_PGM_RSRC3_GFX10_RESERVED6,
2884 "COMPUTE_PGM_RSRC3",
2885 "must be zero on gfx10");
2886 }
2887 } else if (FourByteBuffer) {
2888 return createStringError(
2889 std::errc::invalid_argument,
2890 "kernel descriptor COMPUTE_PGM_RSRC3 must be all zero before gfx9");
2891 }
2892 return true;
2893}
2894#undef PRINT_PSEUDO_DIRECTIVE_COMMENT
2895#undef PRINT_DIRECTIVE
2896#undef GET_FIELD
2897#undef CHECK_RESERVED_BITS_IMPL
2898#undef CHECK_RESERVED_BITS
2899#undef CHECK_RESERVED_BITS_MSG
2900#undef CHECK_RESERVED_BITS_DESC
2901#undef CHECK_RESERVED_BITS_DESC_MSG
2902
2903/// Create an error object to return from onSymbolStart for reserved kernel
2904/// descriptor bits being set.
2905static Error createReservedKDBitsError(uint32_t Mask, unsigned BaseBytes,
2906 const char *Msg = "") {
2907 return createStringError(
2908 std::errc::invalid_argument, "kernel descriptor reserved %s set%s%s",
2909 getBitRangeFromMask(Mask, BaseBytes).c_str(), *Msg ? ", " : "", Msg);
2910}
2911
2912/// Create an error object to return from onSymbolStart for reserved kernel
2913/// descriptor bytes being set.
2914static Error createReservedKDBytesError(unsigned BaseInBytes,
2915 unsigned WidthInBytes) {
2916 // Create an error comment in the same format as the "Kernel Descriptor"
2917 // table here: https://llvm.org/docs/AMDGPUUsage.html#kernel-descriptor .
2918 return createStringError(
2919 std::errc::invalid_argument,
2920 "kernel descriptor reserved bits in range (%u:%u) set",
2921 (BaseInBytes + WidthInBytes) * CHAR_BIT - 1, BaseInBytes * CHAR_BIT);
2922}
2923
2926 raw_string_ostream &KdStream) const {
2927#define PRINT_DIRECTIVE(DIRECTIVE, MASK) \
2928 do { \
2929 KdStream << Indent << DIRECTIVE " " \
2930 << ((TwoByteBuffer & MASK) >> (MASK##_SHIFT)) << '\n'; \
2931 } while (0)
2932
2933 uint16_t TwoByteBuffer = 0;
2934 uint32_t FourByteBuffer = 0;
2935
2936 StringRef ReservedBytes;
2937 StringRef Indent = "\t";
2938
2939 assert(Bytes.size() == 64);
2940 DataExtractor DE(Bytes, /*IsLittleEndian=*/true);
2941
2942 switch (Cursor.tell()) {
2944 FourByteBuffer = DE.getU32(Cursor);
2945 KdStream << Indent << ".amdhsa_group_segment_fixed_size " << FourByteBuffer
2946 << '\n';
2947 return true;
2948
2950 FourByteBuffer = DE.getU32(Cursor);
2951 KdStream << Indent << ".amdhsa_private_segment_fixed_size "
2952 << FourByteBuffer << '\n';
2953 return true;
2954
2956 FourByteBuffer = DE.getU32(Cursor);
2957 KdStream << Indent << ".amdhsa_kernarg_size "
2958 << FourByteBuffer << '\n';
2959 return true;
2960
2962 // 4 reserved bytes, must be 0.
2963 ReservedBytes = DE.getBytes(Cursor, 4);
2964 for (char B : ReservedBytes) {
2965 if (B != 0)
2967 }
2968 return true;
2969
2971 // KERNEL_CODE_ENTRY_BYTE_OFFSET
2972 // So far no directive controls this for Code Object V3, so simply skip for
2973 // disassembly.
2974 DE.skip(Cursor, 8);
2975 return true;
2976
2978 // 20 reserved bytes, must be 0.
2979 ReservedBytes = DE.getBytes(Cursor, 20);
2980 for (char B : ReservedBytes) {
2981 if (B != 0)
2983 }
2984 return true;
2985
2987 FourByteBuffer = DE.getU32(Cursor);
2988 return decodeCOMPUTE_PGM_RSRC3(FourByteBuffer, KdStream);
2989
2991 FourByteBuffer = DE.getU32(Cursor);
2992 return decodeCOMPUTE_PGM_RSRC1(FourByteBuffer, KdStream);
2993
2995 FourByteBuffer = DE.getU32(Cursor);
2996 return decodeCOMPUTE_PGM_RSRC2(FourByteBuffer, KdStream);
2997
2999 using namespace amdhsa;
3000 TwoByteBuffer = DE.getU16(Cursor);
3001
3003 PRINT_DIRECTIVE(".amdhsa_user_sgpr_private_segment_buffer",
3004 KERNEL_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER);
3005 PRINT_DIRECTIVE(".amdhsa_user_sgpr_dispatch_ptr",
3006 KERNEL_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR);
3007 PRINT_DIRECTIVE(".amdhsa_user_sgpr_queue_ptr",
3008 KERNEL_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR);
3009 PRINT_DIRECTIVE(".amdhsa_user_sgpr_kernarg_segment_ptr",
3010 KERNEL_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR);
3011 PRINT_DIRECTIVE(".amdhsa_user_sgpr_dispatch_id",
3012 KERNEL_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID);
3014 PRINT_DIRECTIVE(".amdhsa_user_sgpr_flat_scratch_init",
3015 KERNEL_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT);
3016 PRINT_DIRECTIVE(".amdhsa_user_sgpr_private_segment_size",
3017 KERNEL_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_SIZE);
3018
3019 if (TwoByteBuffer & KERNEL_CODE_PROPERTY_RESERVED0)
3020 return createReservedKDBitsError(KERNEL_CODE_PROPERTY_RESERVED0,
3022
3023 // Reserved for GFX9
3024 if (isGFX9() &&
3025 (TwoByteBuffer & KERNEL_CODE_PROPERTY_ENABLE_WAVEFRONT_SIZE32)) {
3027 KERNEL_CODE_PROPERTY_ENABLE_WAVEFRONT_SIZE32,
3028 amdhsa::KERNEL_CODE_PROPERTIES_OFFSET, "must be zero on gfx9");
3029 }
3030 if (isGFX10Plus()) {
3031 PRINT_DIRECTIVE(".amdhsa_wavefront_size32",
3032 KERNEL_CODE_PROPERTY_ENABLE_WAVEFRONT_SIZE32);
3033 }
3034
3035 if (CodeObjectVersion >= AMDGPU::AMDHSA_COV5)
3036 PRINT_DIRECTIVE(".amdhsa_uses_dynamic_stack",
3037 KERNEL_CODE_PROPERTY_USES_DYNAMIC_STACK);
3038
3039 if (TwoByteBuffer & KERNEL_CODE_PROPERTY_RESERVED1) {
3040 return createReservedKDBitsError(KERNEL_CODE_PROPERTY_RESERVED1,
3042 }
3043
3044 return true;
3045
3047 using namespace amdhsa;
3048 TwoByteBuffer = DE.getU16(Cursor);
3049 if (TwoByteBuffer & KERNARG_PRELOAD_SPEC_LENGTH) {
3050 PRINT_DIRECTIVE(".amdhsa_user_sgpr_kernarg_preload_length",
3051 KERNARG_PRELOAD_SPEC_LENGTH);
3052 }
3053
3054 if (TwoByteBuffer & KERNARG_PRELOAD_SPEC_OFFSET) {
3055 PRINT_DIRECTIVE(".amdhsa_user_sgpr_kernarg_preload_offset",
3056 KERNARG_PRELOAD_SPEC_OFFSET);
3057 }
3058 return true;
3059
3061 // 4 bytes from here are reserved, must be 0.
3062 ReservedBytes = DE.getBytes(Cursor, 4);
3063 for (char B : ReservedBytes) {
3064 if (B != 0)
3066 }
3067 return true;
3068
3069 default:
3070 llvm_unreachable("Unhandled index. Case statements cover everything.");
3071 return true;
3072 }
3073#undef PRINT_DIRECTIVE
3074}
3075
3077 StringRef KdName, ArrayRef<uint8_t> Bytes, uint64_t KdAddress) const {
3078
3079 // CP microcode requires the kernel descriptor to be 64 aligned.
3080 if (Bytes.size() != 64 || KdAddress % 64 != 0)
3081 return createStringError(std::errc::invalid_argument,
3082 "kernel descriptor must be 64-byte aligned");
3083
3084 // FIXME: We can't actually decode "in order" as is done below, as e.g. GFX10
3085 // requires us to know the setting of .amdhsa_wavefront_size32 in order to
3086 // accurately produce .amdhsa_next_free_vgpr, and they appear in the wrong
3087 // order. Workaround this by first looking up .amdhsa_wavefront_size32 here
3088 // when required.
3089 if (isGFX10Plus()) {
3090 uint16_t KernelCodeProperties =
3093 EnableWavefrontSize32 =
3094 AMDHSA_BITS_GET(KernelCodeProperties,
3095 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_WAVEFRONT_SIZE32);
3096 }
3097
3098 std::string Kd;
3099 raw_string_ostream KdStream(Kd);
3100 KdStream << ".amdhsa_kernel " << KdName << '\n';
3101
3103 while (C && C.tell() < Bytes.size()) {
3104 Expected<bool> Res = decodeKernelDescriptorDirective(C, Bytes, KdStream);
3105
3106 cantFail(C.takeError());
3107
3108 if (!Res)
3109 return Res;
3110 }
3111 KdStream << ".end_amdhsa_kernel\n";
3112 outs() << KdStream.str();
3113 return true;
3114}
3115
3117 uint64_t &Size,
3118 ArrayRef<uint8_t> Bytes,
3119 uint64_t Address) const {
3120 // Right now only kernel descriptor needs to be handled.
3121 // We ignore all other symbols for target specific handling.
3122 // TODO:
3123 // Fix the spurious symbol issue for AMDGPU kernels. Exists for both Code
3124 // Object V2 and V3 when symbols are marked protected.
3125
3126 // amd_kernel_code_t for Code Object V2.
3127 if (Symbol.Type == ELF::STT_AMDGPU_HSA_KERNEL) {
3128 Size = 256;
3129 return createStringError(std::errc::invalid_argument,
3130 "code object v2 is not supported");
3131 }
3132
3133 // Code Object V3 kernel descriptors.
3134 StringRef Name = Symbol.Name;
3135 if (Symbol.Type == ELF::STT_OBJECT && Name.ends_with(StringRef(".kd"))) {
3136 Size = 64; // Size = 64 regardless of success or failure.
3137 return decodeKernelDescriptor(Name.drop_back(3), Bytes, Address);
3138 }
3139
3140 return false;
3141}
3142
3143const MCExpr *AMDGPUDisassembler::createConstantSymbolExpr(StringRef Id,
3144 int64_t Val) {
3145 MCContext &Ctx = getContext();
3146 MCSymbol *Sym = Ctx.getOrCreateSymbol(Id);
3147 // Note: only set value to Val on a new symbol in case an dissassembler
3148 // has already been initialized in this context.
3149 if (!Sym->isVariable()) {
3151 } else {
3152 int64_t Res = ~Val;
3153 bool Valid = Sym->getVariableValue()->evaluateAsAbsolute(Res);
3154 if (!Valid || Res != Val)
3155 Ctx.reportWarning(SMLoc(), "unsupported redefinition of " + Id);
3156 }
3157 return MCSymbolRefExpr::create(Sym, Ctx);
3158}
3159
3161 // Check for MUBUF and MTBUF instructions
3162 if (SIInstrFlags::isBuffer(*MCII, MI))
3163 return true;
3164
3165 // Check for SMEM buffer instructions (S_BUFFER_* instructions)
3166 if (SIInstrFlags::isSMRD(*MCII, MI) &&
3167 AMDGPU::getSMEMIsBuffer(MI.getOpcode()))
3168 return true;
3169
3170 return false;
3171}
3172
3173//===----------------------------------------------------------------------===//
3174// AMDGPUSymbolizer
3175//===----------------------------------------------------------------------===//
3176
3177// Try to find symbol name for specified label
3179 MCInst &Inst, raw_ostream & /*cStream*/, int64_t Value,
3180 uint64_t /*Address*/, bool IsBranch, uint64_t /*Offset*/,
3181 uint64_t /*OpSize*/, uint64_t /*InstSize*/) {
3182
3183 if (!IsBranch) {
3184 return false;
3185 }
3186
3187 auto *Symbols = static_cast<SectionSymbolsTy *>(DisInfo);
3188 if (!Symbols)
3189 return false;
3190
3191 auto Result = llvm::find_if(*Symbols, [Value](const SymbolInfoTy &Val) {
3192 return Val.Addr == static_cast<uint64_t>(Value) &&
3193 Val.Type == ELF::STT_NOTYPE;
3194 });
3195 if (Result != Symbols->end()) {
3196 auto *Sym = Ctx.getOrCreateSymbol(Result->Name);
3197 const auto *Add = MCSymbolRefExpr::create(Sym, Ctx);
3199 return true;
3200 }
3201 // Add to list of referenced addresses, so caller can synthesize a label.
3202 ReferencedAddresses.push_back(static_cast<uint64_t>(Value));
3203 return false;
3204}
3205
3207 int64_t Value,
3208 uint64_t Address) {
3209 llvm_unreachable("unimplemented");
3210}
3211
3212//===----------------------------------------------------------------------===//
3213// Initialization
3214//===----------------------------------------------------------------------===//
3215
3217 LLVMOpInfoCallback /*GetOpInfo*/,
3218 LLVMSymbolLookupCallback /*SymbolLookUp*/,
3219 void *DisInfo,
3220 MCContext *Ctx,
3221 std::unique_ptr<MCRelocationInfo> &&RelInfo) {
3222 return new AMDGPUSymbolizer(*Ctx, std::move(RelInfo), DisInfo);
3223}
3224
3226 const MCSubtargetInfo &STI,
3227 MCContext &Ctx) {
3228 return new AMDGPUDisassembler(STI, Ctx, T.createMCInstrInfo());
3229}
3230
3231extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void
MCDisassembler::DecodeStatus DecodeStatus
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
#define CHECK_RESERVED_BITS_DESC(MASK, DESC)
static DecodeStatus decodeRsrcRegOp(MCInst &Inst, unsigned Imm, uint64_t, const MCDisassembler *Decoder, unsigned OpWidth)
static DecodeStatus DecodeVGPR_16_Lo128RegisterClass(MCInst &Inst, uint64_t Imm, uint64_t, const MCDisassembler *Decoder)
static VOPModifiers collectVOPModifiers(const MCInst &MI, bool IsVOP3P=false)
static DecodeStatus decodeSrcRegOrImmA9(MCInst &Inst, uint64_t Imm, uint64_t, const MCDisassembler *Decoder)
uint64_t uint64_t
static int insertNamedMCOperand(MCInst &MI, const MCOperand &Op, AMDGPU::OpName Name)
#define DECODE_OPERAND_SREG_9(RegClass, OpWidth)
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAMDGPUDisassembler()
static DecodeStatus decodeSrcOp(MCInst &Inst, unsigned EncSize, unsigned OpWidth, uint64_t Imm, uint64_t EncImm, const MCDisassembler *Decoder)
static DecodeStatus decodeOperand_KImmFP64(MCInst &Inst, uint64_t Imm, uint64_t Addr, const MCDisassembler *Decoder)
static SmallString< 32 > getBitRangeFromMask(uint32_t Mask, unsigned BaseBytes)
Print a string describing the reserved bit range specified by Mask with offset BaseBytes for use in e...
static DecodeStatus decodeDpp8FI(MCInst &Inst, uint64_t Val, uint64_t Addr, const MCDisassembler *Decoder)
static DecodeStatus decodeSrcReg9(MCInst &Inst, uint64_t Imm, uint64_t, const MCDisassembler *Decoder)
#define DECODE_OPERAND_SREG_8(RegClass, OpWidth)
static DecodeStatus decodeSrcA9(MCInst &Inst, uint64_t Imm, uint64_t, const MCDisassembler *Decoder)
static DecodeStatus decodeOperandVOPDDstY(MCInst &Inst, uint64_t Val, uint64_t Addr, const void *Decoder)
static std::bitset< 128 > eat16Bytes(ArrayRef< uint8_t > &Bytes)
#define DECODE_OPERAND_SREG_7(RegClass, OpWidth)
static DecodeStatus decodeSMEMOffsetPCRel(MCInst &Inst, uint64_t Imm, uint64_t Addr, const MCDisassembler *Decoder)
#define PRINT_PSEUDO_DIRECTIVE_COMMENT(DIRECTIVE, MASK)
uint64_t Imm
static DecodeStatus decodeSMEMOffset(MCInst &Inst, uint64_t Imm, uint64_t Addr, const MCDisassembler *Decoder)
static DecodeStatus decodeOperand_VSrcT16(MCInst &Inst, uint64_t Imm, uint64_t, const MCDisassembler *Decoder)
static DecodeStatus decodeOperand_VSrcT16_Lo128(MCInst &Inst, uint64_t Imm, uint64_t, const MCDisassembler *Decoder)
static MCRegister CheckVGPROverflow(MCRegister Reg, const MCRegisterClass &RC, const MCRegisterInfo &MRI)
static int64_t getInlineImmValBF16(unsigned Imm)
#define DECODE_SDWA(DecName)
#define DECODE_OPERAND_REG_8(RegClass)
static DecodeStatus decodeSrcAV10(MCInst &Inst, uint64_t Imm, uint64_t, const MCDisassembler *Decoder)
#define PRINT_DIRECTIVE(DIRECTIVE, MASK)
static DecodeStatus decodeBoolReg(MCInst &Inst, uint64_t Val, uint64_t Addr, const MCDisassembler *Decoder)
static DecodeStatus decodeOperand_KImmFP(MCInst &Inst, uint64_t Imm, uint64_t Addr, const MCDisassembler *Decoder)
static DecodeStatus decodeRsrcReg256(MCInst &Inst, unsigned Imm, uint64_t, const MCDisassembler *Decoder)
static int64_t getInlineImmVal32(unsigned Imm)
static MCDisassembler::DecodeStatus addOperand(MCInst &Inst, const MCOperand &Opnd)
static DecodeStatus decodeOperand_VSrc_f64(MCInst &Inst, uint64_t Imm, uint64_t Addr, const MCDisassembler *Decoder)
#define CHECK_RESERVED_BITS(MASK)
#define SGPR_MAX
static int64_t getInlineImmVal64(unsigned Imm)
static DecodeStatus decodeSrcRegOrImm9(MCInst &Inst, uint64_t Imm, uint64_t, const MCDisassembler *Decoder)
static T eatBytes(ArrayRef< uint8_t > &Bytes)
static DecodeStatus decodeSplitBarrier(MCInst &Inst, uint64_t Val, uint64_t Addr, const MCDisassembler *Decoder)
#define DECODE_SDWA_IMM_FIELD(Name, MaxImm)
static MCDisassembler * createAMDGPUDisassembler(const Target &T, const MCSubtargetInfo &STI, MCContext &Ctx)
static DecodeStatus decodeSOPPBrTarget(MCInst &Inst, uint64_t Imm, uint64_t Addr, const MCDisassembler *Decoder)
#define CHECK_RESERVED_BITS_MSG(MASK, MSG)
static DecodeStatus decodeAV10(MCInst &Inst, uint64_t Imm, uint64_t, const MCDisassembler *Decoder)
uint64_t const MCDisassembler * Decoder
static MCSymbolizer * createAMDGPUSymbolizer(const Triple &, LLVMOpInfoCallback, LLVMSymbolLookupCallback, void *DisInfo, MCContext *Ctx, std::unique_ptr< MCRelocationInfo > &&RelInfo)
static int64_t getInlineImmValF16(unsigned Imm)
#define GET_FIELD(MASK)
static std::bitset< 96 > eat12Bytes(ArrayRef< uint8_t > &Bytes)
static DecodeStatus decodeAVLdSt(MCInst &Inst, uint64_t Imm, unsigned Opw, const MCDisassembler *Decoder)
static DecodeStatus decodeRsrcReg128(MCInst &Inst, unsigned Imm, uint64_t, const MCDisassembler *Decoder)
static DecodeStatus DecodeVGPR_16RegisterClass(MCInst &Inst, uint64_t Imm, uint64_t, const MCDisassembler *Decoder)
static Error createReservedKDBytesError(unsigned BaseInBytes, unsigned WidthInBytes)
Create an error object to return from onSymbolStart for reserved kernel descriptor bytes being set.
static bool adjustMFMA_F8F6F4OpRegClass(const MCRegisterInfo &MRI, MCOperand &MO, uint8_t NumRegs)
Adjust the register values used by V_MFMA_F8F6F4_f8_f8 instructions to the appropriate subregister fo...
static DecodeStatus decodeOperand_VGPR_16(MCInst &Inst, uint64_t Imm, uint64_t, const MCDisassembler *Decoder)
#define CHECK_RESERVED_BITS_DESC_MSG(MASK, DESC, MSG)
static Error createReservedKDBitsError(uint32_t Mask, unsigned BaseBytes, const char *Msg="")
Create an error object to return from onSymbolStart for reserved kernel descriptor bits being set.
This file contains declaration for AMDGPU ISA disassembler.
Provides AMDGPU specific target descriptions.
AMDHSA kernel descriptor definitions.
#define AMDHSA_BITS_GET(SRC, MSK)
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
#define AMDGPU_MACH_LIST(X)
Definition ELF.h:576
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition Compiler.h:211
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
#define T
MachineRegisterInfo & MRI
MachineInstr *& Lo
Interface definition for SIRegisterInfo.
const char * Msg
std::optional< unsigned > getSgprClassId(unsigned Width) const
Return the SGPR/TTMP register class accepted by source decoding for Width, or std::nullopt if that wi...
MCOperand decodeNonVGPRSrcOp(const MCInst &Inst, unsigned Width, unsigned Val) const
MCOperand decodeLiteral64Constant() const
void convertVOPC64DPPInst(MCInst &MI) const
bool isBufferInstruction(const MCInst &MI) const
Check if the instruction is a buffer operation (MUBUF, MTBUF, or S_BUFFER)
void convertEXPInst(MCInst &MI) const
MCOperand decodeSpecialReg64(unsigned Val) const
const char * getRegClassName(unsigned RegClassID) const
Expected< bool > decodeCOMPUTE_PGM_RSRC1(uint32_t FourByteBuffer, raw_string_ostream &KdStream) const
Decode as directives that handle COMPUTE_PGM_RSRC1.
MCOperand decodeSplitBarrier(const MCInst &Inst, unsigned Val) const
Expected< bool > decodeKernelDescriptorDirective(DataExtractor::Cursor &Cursor, ArrayRef< uint8_t > Bytes, raw_string_ostream &KdStream) const
void convertVOPCDPPInst(MCInst &MI) const
MCOperand decodeSpecialReg96Plus(unsigned Val) const
MCOperand decodeSDWASrc32(unsigned Val) const
void setABIVersion(unsigned Version) override
ELF-specific, set the ABI version from the object header.
Expected< bool > decodeCOMPUTE_PGM_RSRC2(uint32_t FourByteBuffer, raw_string_ostream &KdStream) const
Decode as directives that handle COMPUTE_PGM_RSRC2.
unsigned getAgprClassId(unsigned Width) const
MCOperand decodeDpp8FI(unsigned Val) const
MCOperand decodeSDWASrc(unsigned Width, unsigned Val) const
void convertFMAanyK(MCInst &MI) const
DecodeStatus tryDecodeInst(const uint8_t *Table, MCInst &MI, InsnType Inst, uint64_t Address, raw_ostream &Comments) const
void convertMacDPPInst(MCInst &MI) const
MCOperand decodeVOPDDstYOp(MCInst &Inst, unsigned Val) const
void convertDPP8Inst(MCInst &MI) const
MCOperand createVGPR16Operand(unsigned RegIdx, bool IsHi) const
MCOperand errOperand(unsigned V, const Twine &ErrMsg) const
MCOperand decodeVersionImm(unsigned Imm) const
Expected< bool > decodeKernelDescriptor(StringRef KdName, ArrayRef< uint8_t > Bytes, uint64_t KdAddress) const
void convertVOP3DPPInst(MCInst &MI) const
void convertTrue16OpSel(MCInst &MI) const
MCOperand decodeSrcOp(const MCInst &Inst, unsigned Width, unsigned Val) const
bool convertMAIInst(MCInst &MI) const
f8f6f4 instructions have different pseudos depending on the used formats.
MCOperand decodeMandatoryLiteralConstant(unsigned Imm) const
MCOperand decodeLiteralConstant(const MCInstrDesc &Desc, const MCOperandInfo &OpDesc) const
Expected< bool > decodeCOMPUTE_PGM_RSRC3(uint32_t FourByteBuffer, raw_string_ostream &KdStream) const
Decode as directives that handle COMPUTE_PGM_RSRC3.
AMDGPUDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx, MCInstrInfo const *MCII)
MCOperand decodeSpecialReg32(unsigned Val) const
MCOperand createRegOperand(MCRegister Reg) const
MCOperand decodeSDWAVopcDst(unsigned Val) const
void convertVINTERPInst(MCInst &MI) const
void convertSDWAInst(MCInst &MI) const
static MCOperand decodeIntImmed(unsigned Imm)
MCOperand decodeBoolReg(const MCInst &Inst, unsigned Val) const
void emitTargetIDIfSupported(raw_ostream &OS, unsigned EFlags) const override
Emit something based on ELF's e_flags if the target needs to.
unsigned getVgprClassId(unsigned Width) const
DecodeStatus getInstruction(MCInst &MI, uint64_t &Size, ArrayRef< uint8_t > Bytes, uint64_t Address, raw_ostream &CS) const override
Returns the disassembly of a single instruction.
std::optional< unsigned > getTtmpClassId(unsigned Width) const
MCOperand decodeMandatoryLiteral64Constant(uint64_t Imm) const
void convertMIMGInst(MCInst &MI) const
bool isMacDPP(MCInst &MI) const
int getTTmpIdx(unsigned Val) const
void convertVOP3PDPPInst(MCInst &MI) const
bool convertWMMAInst(MCInst &MI) const
MCOperand createSRegOperand(unsigned SRegClassID, unsigned Val) const
MCOperand decodeSDWASrc16(unsigned Val) const
Expected< bool > onSymbolStart(SymbolInfoTy &Symbol, uint64_t &Size, ArrayRef< uint8_t > Bytes, uint64_t Address) const override
Used to perform separate target specific disassembly for a particular symbol.
static const AMDGPUMCExpr * createLit(LitModifier Lit, int64_t Value, MCContext &Ctx)
bool tryAddingSymbolicOperand(MCInst &Inst, raw_ostream &cStream, int64_t Value, uint64_t Address, bool IsBranch, uint64_t Offset, uint64_t OpSize, uint64_t InstSize) override
Try to add a symbolic operand instead of Value to the MCInst.
void tryAddingPcLoadReferenceComment(raw_ostream &cStream, int64_t Value, uint64_t Address) override
Try to add a comment on the PC-relative load.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
const T * data() const
Definition ArrayRef.h:138
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition ArrayRef.h:185
A class representing a position in a DataExtractor, as well as any error encountered during extractio...
LLVM_ABI uint32_t getU32(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint32_t value from *offset_ptr.
LLVM_ABI uint16_t getU16(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint16_t value from *offset_ptr.
LLVM_ABI void skip(Cursor &C, uint64_t Length) const
Advance the Cursor position by the given number of bytes.
LLVM_ABI StringRef getBytes(uint64_t *OffsetPtr, uint64_t Length, Error *Err=nullptr) const
Extract a fixed number of bytes from the specified offset.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
static const MCBinaryExpr * createOr(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition MCExpr.h:407
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
Context object for machine code objects.
Definition MCContext.h:84
const MCRegisterInfo * getRegisterInfo() const
Definition MCContext.h:411
Superclass for all disassemblers.
MCDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
MCContext & getContext() const
const MCSubtargetInfo & STI
raw_ostream * CommentStream
DecodeStatus
Ternary decode status.
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
unsigned getOpcode() const
Definition MCInst.h:202
void addOperand(const MCOperand Op)
Definition MCInst.h:215
const MCOperand & getOperand(unsigned i) const
Definition MCInst.h:210
Describe properties that are true of each instruction in the target description file.
Interface to description of machine instruction set.
Definition MCInstrInfo.h:27
This holds information about one operand of a machine instruction, indicating the register class for ...
Definition MCInstrDesc.h:88
uint8_t OperandType
Information about the type of the operand.
Instances of this class represent operands of the MCInst class.
Definition MCInst.h:40
static MCOperand createExpr(const MCExpr *Val)
Definition MCInst.h:166
int64_t getImm() const
Definition MCInst.h:84
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:138
static MCOperand createImm(int64_t Val)
Definition MCInst.h:145
void setReg(MCRegister Reg)
Set the register number.
Definition MCInst.h:79
bool isReg() const
Definition MCInst.h:65
MCRegister getReg() const
Returns the register number.
Definition MCInst.h:73
bool isValid() const
Definition MCInst.h:64
MCRegisterClass - Base class of TargetRegisterClass.
MCRegister getRegister(unsigned i) const
getRegister - Return the specified register in the class.
unsigned getSizeInBits() const
Return the size of the physical register in bits if we are able to determine it.
bool contains(MCRegister Reg) const
contains - Return true if the specified register is included in this register class.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
const char * getRegClassName(const MCRegisterClass *Class) const
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
Generic base class for all target subtargets.
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:213
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
bool isVariable() const
isVariable - Check if this is a variable symbol.
Definition MCSymbol.h:267
LLVM_ABI void setVariableValue(const MCExpr *Value)
Definition MCSymbol.cpp:51
const MCExpr * getVariableValue() const
Get the expression of the variable symbol.
Definition MCSymbol.h:270
Symbolize and annotate disassembled instructions.
Represents a location in source code.
Definition SMLoc.h:22
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:28
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an std::string.
std::string & str()
Returns the string's reference.
A raw_ostream that writes to an SmallVector or SmallString.
const char *(* LLVMSymbolLookupCallback)(void *DisInfo, uint64_t ReferenceValue, uint64_t *ReferenceType, uint64_t ReferencePC, const char **ReferenceName)
The type for the symbol lookup function.
int(* LLVMOpInfoCallback)(void *DisInfo, uint64_t PC, uint64_t Offset, uint64_t OpSize, uint64_t InstSize, int TagType, void *TagBuf)
The type for the operand information call back function.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned getVGPREncodingGranule(const MCSubtargetInfo &STI, std::optional< bool > EnableWavefrontSize32)
unsigned getSGPREncodingGranule(const MCSubtargetInfo &STI)
ArrayRef< GFXVersion > getGFXVersions()
EncodingField< Bit, Bit, D > EncodingBit
bool isPKFMACF16InlineConstant(uint32_t Literal, bool IsGFX11Plus)
LLVM_READONLY const MIMGInfo * getMIMGInfo(unsigned Opc)
bool isInlinableLiteralFP16(int16_t Literal, bool HasInv2Pi)
MCRegister getMCReg(MCRegister Reg, const MCSubtargetInfo &STI)
If Reg is a pseudo reg, return the correct hardware register given STI otherwise return Reg.
bool isInlinableLiteralV2I16(uint32_t Literal)
bool isGFX10(const MCSubtargetInfo &STI)
bool isInlinableLiteralV2BF16(uint32_t Literal)
bool isGFX12Plus(const MCSubtargetInfo &STI)
bool hasPackedD16(const MCSubtargetInfo &STI)
bool isInlinableLiteralV2F16(uint32_t Literal)
bool getSMEMIsBuffer(unsigned Opc)
bool isGFX13(const MCSubtargetInfo &STI)
bool isVOPC64DPP(unsigned Opc)
bool hasPrivateApertureRegs(const MCSubtargetInfo &STI)
unsigned getAMDHSACodeObjectVersion(const Module &M)
bool isGFX9(const MCSubtargetInfo &STI)
LLVM_READONLY bool hasNamedOperand(uint32_t Opcode, OpName NamedIdx)
int getMIMGOpcode(unsigned BaseOpcode, unsigned MIMGEncoding, unsigned VDataDwords, unsigned VAddrDwords, bool IndexedRsrc, bool IndexedSamp)
LLVM_READONLY const MIMGDimInfo * getMIMGDimInfoByEncoding(uint8_t DimEnc)
bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi)
const MFMA_F8F6F4_Info * getWMMA_F8F6F4_WithFormatArgs(unsigned FmtA, unsigned FmtB, unsigned F8F8Opcode)
bool isInlinableLiteralBF16(int16_t Literal, bool HasInv2Pi, bool *IsBF16)
bool hasG16(const MCSubtargetInfo &STI)
unsigned getAddrSizeMIMGOp(const MIMGBaseOpcodeInfo *BaseOpcode, const MIMGDimInfo *Dim, bool IsA16, bool IsG16Supported)
bool isGFX13Plus(const MCSubtargetInfo &STI)
bool isGFX11Plus(const MCSubtargetInfo &STI)
bool isGFX10Plus(const MCSubtargetInfo &STI)
@ OPERAND_REG_IMM_V2FP64
Definition SIDefines.h:447
@ OPERAND_KIMM32
Operand with 32-bit immediate that uses the constant bus.
Definition SIDefines.h:465
@ OPERAND_REG_IMM_INT64
Definition SIDefines.h:432
@ OPERAND_REG_IMM_V2FP16
Definition SIDefines.h:440
@ OPERAND_REG_INLINE_C_FP64
Definition SIDefines.h:456
@ OPERAND_REG_IMM_NOINLINE_FP16
Definition SIDefines.h:438
@ OPERAND_REG_INLINE_C_BF16
Definition SIDefines.h:453
@ OPERAND_REG_INLINE_C_V2BF16
Definition SIDefines.h:458
@ OPERAND_REG_IMM_V2INT64
Definition SIDefines.h:443
@ OPERAND_REG_IMM_V2INT16
Definition SIDefines.h:442
@ OPERAND_REG_IMM_BF16
Definition SIDefines.h:436
@ OPERAND_REG_IMM_INT32
Operands with register, 32-bit, or 64-bit immediate.
Definition SIDefines.h:431
@ OPERAND_REG_IMM_V2BF16
Definition SIDefines.h:439
@ OPERAND_REG_IMM_FP16
Definition SIDefines.h:437
@ OPERAND_REG_IMM_V2FP16_SPLAT
Definition SIDefines.h:441
@ OPERAND_REG_INLINE_C_INT64
Definition SIDefines.h:452
@ OPERAND_REG_INLINE_C_INT16
Operands with register or inline constant.
Definition SIDefines.h:450
@ OPERAND_REG_IMM_NOINLINE_V2FP16
Definition SIDefines.h:444
@ OPERAND_REG_IMM_FP64
Definition SIDefines.h:435
@ OPERAND_REG_INLINE_C_V2FP16
Definition SIDefines.h:459
@ OPERAND_REG_INLINE_AC_INT32
Operands with an AccVGPR register or inline constant.
Definition SIDefines.h:470
@ OPERAND_REG_INLINE_AC_FP32
Definition SIDefines.h:471
@ OPERAND_REG_IMM_V2INT32
Definition SIDefines.h:445
@ OPERAND_REG_IMM_FP32
Definition SIDefines.h:434
@ OPERAND_REG_INLINE_C_FP32
Definition SIDefines.h:455
@ OPERAND_REG_INLINE_C_INT32
Definition SIDefines.h:451
@ OPERAND_REG_INLINE_C_V2INT16
Definition SIDefines.h:457
@ OPERAND_REG_IMM_V2FP32
Definition SIDefines.h:446
@ OPERAND_REG_INLINE_AC_FP64
Definition SIDefines.h:472
@ OPERAND_REG_INLINE_C_FP16
Definition SIDefines.h:454
@ OPERAND_REG_IMM_INT16
Definition SIDefines.h:433
bool hasGDS(const MCSubtargetInfo &STI)
bool isGFX9Plus(const MCSubtargetInfo &STI)
bool isVOPD(unsigned Opc)
bool isGFX1250(const MCSubtargetInfo &STI)
unsigned hasKernargPreload(const MCSubtargetInfo &STI)
bool isMAC(unsigned Opc)
LLVM_READONLY const MIMGBaseOpcodeInfo * getMIMGBaseOpcodeInfo(unsigned BaseOpcode)
bool isGFX1250Plus(const MCSubtargetInfo &STI)
bool hasPopsExitingWaveID(const MCSubtargetInfo &STI)
bool isInlinableLiteralI16(int32_t Literal, bool HasInv2Pi)
bool hasVOPD(const MCSubtargetInfo &STI)
bool isInlinableLiteral64(int64_t Literal, bool HasInv2Pi)
Is this literal inlinable.
const MFMA_F8F6F4_Info * getMFMA_F8F6F4_WithFormatArgs(unsigned CBSZ, unsigned BLGP, unsigned F8F8Opcode)
@ STT_NOTYPE
Definition ELF.h:1272
@ STT_AMDGPU_HSA_KERNEL
Definition ELF.h:1286
@ STT_OBJECT
Definition ELF.h:1273
@ EF_AMDGPU_FEATURE_XNACK_ANY_V4
Definition ELF.h:718
@ EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4
Definition ELF.h:729
@ EF_AMDGPU_FEATURE_SRAMECC_OFF_V4
Definition ELF.h:733
@ EF_AMDGPU_FEATURE_XNACK_UNSUPPORTED_V4
Definition ELF.h:716
@ EF_AMDGPU_FEATURE_XNACK_OFF_V4
Definition ELF.h:720
@ EF_AMDGPU_FEATURE_XNACK_V4
Definition ELF.h:714
@ EF_AMDGPU_FEATURE_SRAMECC_V4
Definition ELF.h:727
@ EF_AMDGPU_FEATURE_XNACK_ON_V4
Definition ELF.h:722
@ EF_AMDGPU_MACH
Definition ELF.h:660
@ EF_AMDGPU_FEATURE_SRAMECC_ANY_V4
Definition ELF.h:731
@ EF_AMDGPU_FEATURE_SRAMECC_ON_V4
Definition ELF.h:735
constexpr bool isAtomicRet(const T &...O)
Definition SIDefines.h:367
constexpr bool isVOPC(const T &...O)
Definition SIDefines.h:236
constexpr bool isVOP3(const T &...O)
Definition SIDefines.h:239
constexpr bool isMAI(const T &...O)
Definition SIDefines.h:355
constexpr bool isFLAT(const T &...O)
Definition SIDefines.h:286
constexpr bool isVOP3P(const T &...O)
Definition SIDefines.h:242
constexpr bool isBuffer(const T &...O)
Definition SIDefines.h:267
constexpr bool isVIMAGE(const T &...O)
Definition SIDefines.h:277
constexpr bool isSMRD(const T &...O)
Definition SIDefines.h:271
constexpr bool isVOP3Like(const T &...O)
Definition SIDefines.h:245
constexpr bool isMIMG(const T &...O)
Definition SIDefines.h:274
constexpr bool isWMMA(const T &...O)
Definition SIDefines.h:370
constexpr bool isMUBUF(const T &...O)
Definition SIDefines.h:261
constexpr bool isSDWA(const T &...O)
Definition SIDefines.h:252
constexpr bool isEXP(const T &...O)
Definition SIDefines.h:283
constexpr bool isSOPK(const T &...O)
Definition SIDefines.h:224
constexpr bool isVINTERP(const T &...O)
Definition SIDefines.h:298
constexpr bool isVSAMPLE(const T &...O)
Definition SIDefines.h:280
constexpr bool isDS(const T &...O)
Definition SIDefines.h:289
constexpr bool isGather4(const T &...O)
Definition SIDefines.h:307
constexpr bool isDPP(const T &...O)
Definition SIDefines.h:255
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
Definition Endian.h:53
uint16_t read16(const void *P, endianness E)
Definition Endian.h:389
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2575
LLVM_ABI raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
Op::Description Desc
constexpr int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition bit.h:156
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:204
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
constexpr uint32_t Hi_32(uint64_t Value)
Return the high 32 bits of a 64 bit value.
Definition MathExtras.h:151
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition Error.h:769
Target & getTheGCNTarget()
The target for GCN GPUs.
To bit_cast(const From &from) noexcept
Definition bit.h:90
@ Add
Sum of integers.
DWARFExpression::Operation Op
unsigned M0(unsigned Val)
Definition VE.h:393
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1788
Target & getTheGCNLegacyTarget()
The target for GCN GPUs, registered under the legacy "amdgcn" architecture name for use with -march.
std::vector< SymbolInfoTy > SectionSymbolsTy
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:572
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
static void RegisterMCSymbolizer(Target &T, Target::MCSymbolizerCtorTy Fn)
RegisterMCSymbolizer - Register an MCSymbolizer implementation for the given target.
static void RegisterMCDisassembler(Target &T, Target::MCDisassemblerCtorTy Fn)
RegisterMCDisassembler - Register a MCDisassembler implementation for the given target.