LLVM 21.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
21#include "SIDefines.h"
22#include "SIRegisterInfo.h"
28#include "llvm/MC/MCAsmInfo.h"
29#include "llvm/MC/MCContext.h"
31#include "llvm/MC/MCExpr.h"
32#include "llvm/MC/MCInstrDesc.h"
37
38using namespace llvm;
39
40#define DEBUG_TYPE "amdgpu-disassembler"
41
42#define SGPR_MAX \
43 (isGFX10Plus() ? AMDGPU::EncValues::SGPR_MAX_GFX10 \
44 : AMDGPU::EncValues::SGPR_MAX_SI)
45
47
49 MCContext &Ctx, MCInstrInfo const *MCII)
50 : MCDisassembler(STI, Ctx), MCII(MCII), MRI(*Ctx.getRegisterInfo()),
51 MAI(*Ctx.getAsmInfo()), TargetMaxInstBytes(MAI.getMaxInstLength(&STI)),
52 CodeObjectVersion(AMDGPU::getDefaultAMDHSACodeObjectVersion()) {
53 // ToDo: AMDGPUDisassembler supports only VI ISA.
54 if (!STI.hasFeature(AMDGPU::FeatureGCN3Encoding) && !isGFX10Plus())
55 report_fatal_error("Disassembly not yet supported for subtarget");
56
57 for (auto [Symbol, Code] : AMDGPU::UCVersion::getGFXVersions())
58 createConstantSymbolExpr(Symbol, Code);
59
60 UCVersionW64Expr = createConstantSymbolExpr("UC_VERSION_W64_BIT", 0x2000);
61 UCVersionW32Expr = createConstantSymbolExpr("UC_VERSION_W32_BIT", 0x4000);
62 UCVersionMDPExpr = createConstantSymbolExpr("UC_VERSION_MDP_BIT", 0x8000);
63}
64
67}
68
70addOperand(MCInst &Inst, const MCOperand& Opnd) {
71 Inst.addOperand(Opnd);
72 return Opnd.isValid() ?
75}
76
78 AMDGPU::OpName Name) {
79 int OpIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), Name);
80 if (OpIdx != -1) {
81 auto *I = MI.begin();
82 std::advance(I, OpIdx);
83 MI.insert(I, Op);
84 }
85 return OpIdx;
86}
87
88static DecodeStatus decodeSOPPBrTarget(MCInst &Inst, unsigned Imm,
90 const MCDisassembler *Decoder) {
91 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
92
93 // Our branches take a simm16.
94 int64_t Offset = SignExtend64<16>(Imm) * 4 + 4 + Addr;
95
96 if (DAsm->tryAddingSymbolicOperand(Inst, Offset, Addr, true, 2, 2, 0))
98 return addOperand(Inst, MCOperand::createImm(Imm));
99}
100
102 const MCDisassembler *Decoder) {
103 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
104 int64_t Offset;
105 if (DAsm->isGFX12Plus()) { // GFX12 supports 24-bit signed offsets.
106 Offset = SignExtend64<24>(Imm);
107 } else if (DAsm->isVI()) { // VI supports 20-bit unsigned offsets.
108 Offset = Imm & 0xFFFFF;
109 } else { // GFX9+ supports 21-bit signed offsets.
110 Offset = SignExtend64<21>(Imm);
111 }
113}
114
115static DecodeStatus decodeBoolReg(MCInst &Inst, unsigned Val, uint64_t Addr,
116 const MCDisassembler *Decoder) {
117 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
118 return addOperand(Inst, DAsm->decodeBoolReg(Val));
119}
120
121static DecodeStatus decodeSplitBarrier(MCInst &Inst, unsigned Val,
123 const MCDisassembler *Decoder) {
124 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
125 return addOperand(Inst, DAsm->decodeSplitBarrier(Val));
126}
127
128static DecodeStatus decodeDpp8FI(MCInst &Inst, unsigned Val, uint64_t Addr,
129 const MCDisassembler *Decoder) {
130 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
131 return addOperand(Inst, DAsm->decodeDpp8FI(Val));
132}
133
134#define DECODE_OPERAND(StaticDecoderName, DecoderName) \
135 static DecodeStatus StaticDecoderName(MCInst &Inst, unsigned Imm, \
136 uint64_t /*Addr*/, \
137 const MCDisassembler *Decoder) { \
138 auto DAsm = static_cast<const AMDGPUDisassembler *>(Decoder); \
139 return addOperand(Inst, DAsm->DecoderName(Imm)); \
140 }
141
142// Decoder for registers, decode directly using RegClassID. Imm(8-bit) is
143// number of register. Used by VGPR only and AGPR only operands.
144#define DECODE_OPERAND_REG_8(RegClass) \
145 static DecodeStatus Decode##RegClass##RegisterClass( \
146 MCInst &Inst, unsigned Imm, uint64_t /*Addr*/, \
147 const MCDisassembler *Decoder) { \
148 assert(Imm < (1 << 8) && "8-bit encoding"); \
149 auto DAsm = static_cast<const AMDGPUDisassembler *>(Decoder); \
150 return addOperand( \
151 Inst, DAsm->createRegOperand(AMDGPU::RegClass##RegClassID, Imm)); \
152 }
153
154#define DECODE_SrcOp(Name, EncSize, OpWidth, EncImm, MandatoryLiteral, \
155 ImmWidth) \
156 static DecodeStatus Name(MCInst &Inst, unsigned Imm, uint64_t /*Addr*/, \
157 const MCDisassembler *Decoder) { \
158 assert(Imm < (1 << EncSize) && #EncSize "-bit encoding"); \
159 auto DAsm = static_cast<const AMDGPUDisassembler *>(Decoder); \
160 return addOperand(Inst, \
161 DAsm->decodeSrcOp(AMDGPUDisassembler::OpWidth, EncImm, \
162 MandatoryLiteral, ImmWidth)); \
163 }
164
165static DecodeStatus decodeSrcOp(MCInst &Inst, unsigned EncSize,
167 unsigned Imm, unsigned EncImm,
168 bool MandatoryLiteral, unsigned ImmWidth,
170 const MCDisassembler *Decoder) {
171 assert(Imm < (1U << EncSize) && "Operand doesn't fit encoding!");
172 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
173 return addOperand(Inst, DAsm->decodeSrcOp(OpWidth, EncImm, MandatoryLiteral,
174 ImmWidth, Sema));
175}
176
177// Decoder for registers. Imm(7-bit) is number of register, uses decodeSrcOp to
178// get register class. Used by SGPR only operands.
179#define DECODE_OPERAND_REG_7(RegClass, OpWidth) \
180 DECODE_SrcOp(Decode##RegClass##RegisterClass, 7, OpWidth, Imm, false, 0)
181
182// Decoder for registers. Imm(10-bit): Imm{7-0} is number of register,
183// Imm{9} is acc(agpr or vgpr) Imm{8} should be 0 (see VOP3Pe_SMFMAC).
184// Set Imm{8} to 1 (IS_VGPR) to decode using 'enum10' from decodeSrcOp.
185// Used by AV_ register classes (AGPR or VGPR only register operands).
186template <AMDGPUDisassembler::OpWidthTy OpWidth>
187static DecodeStatus decodeAV10(MCInst &Inst, unsigned Imm, uint64_t /* Addr */,
188 const MCDisassembler *Decoder) {
189 return decodeSrcOp(Inst, 10, OpWidth, Imm, Imm | AMDGPU::EncValues::IS_VGPR,
190 false, 0, AMDGPU::OperandSemantics::INT, Decoder);
191}
192
193// Decoder for Src(9-bit encoding) registers only.
194template <AMDGPUDisassembler::OpWidthTy OpWidth>
195static DecodeStatus decodeSrcReg9(MCInst &Inst, unsigned Imm,
196 uint64_t /* Addr */,
197 const MCDisassembler *Decoder) {
198 return decodeSrcOp(Inst, 9, OpWidth, Imm, Imm, false, 0,
200}
201
202// Decoder for Src(9-bit encoding) AGPR, register number encoded in 9bits, set
203// Imm{9} to 1 (set acc) and decode using 'enum10' from decodeSrcOp, registers
204// only.
205template <AMDGPUDisassembler::OpWidthTy OpWidth>
206static DecodeStatus decodeSrcA9(MCInst &Inst, unsigned Imm, uint64_t /* Addr */,
207 const MCDisassembler *Decoder) {
208 return decodeSrcOp(Inst, 9, OpWidth, Imm, Imm | 512, false, 0,
210}
211
212// Decoder for 'enum10' from decodeSrcOp, Imm{0-8} is 9-bit Src encoding
213// Imm{9} is acc, registers only.
214template <AMDGPUDisassembler::OpWidthTy OpWidth>
215static DecodeStatus decodeSrcAV10(MCInst &Inst, unsigned Imm,
216 uint64_t /* Addr */,
217 const MCDisassembler *Decoder) {
218 return decodeSrcOp(Inst, 10, OpWidth, Imm, Imm, false, 0,
220}
221
222// Decoder for RegisterOperands using 9-bit Src encoding. Operand can be
223// register from RegClass or immediate. Registers that don't belong to RegClass
224// will be decoded and InstPrinter will report warning. Immediate will be
225// decoded into constant of size ImmWidth, should match width of immediate used
226// by OperandType (important for floating point types).
227template <AMDGPUDisassembler::OpWidthTy OpWidth, unsigned ImmWidth,
228 unsigned OperandSemantics>
229static DecodeStatus decodeSrcRegOrImm9(MCInst &Inst, unsigned Imm,
230 uint64_t /* Addr */,
231 const MCDisassembler *Decoder) {
232 return decodeSrcOp(Inst, 9, OpWidth, Imm, Imm, false, ImmWidth,
233 (AMDGPU::OperandSemantics)OperandSemantics, Decoder);
234}
235
236// Decoder for Src(9-bit encoding) AGPR or immediate. Set Imm{9} to 1 (set acc)
237// and decode using 'enum10' from decodeSrcOp.
238template <AMDGPUDisassembler::OpWidthTy OpWidth, unsigned ImmWidth,
239 unsigned OperandSemantics>
240static DecodeStatus decodeSrcRegOrImmA9(MCInst &Inst, unsigned Imm,
241 uint64_t /* Addr */,
242 const MCDisassembler *Decoder) {
243 return decodeSrcOp(Inst, 9, OpWidth, Imm, Imm | 512, false, ImmWidth,
244 (AMDGPU::OperandSemantics)OperandSemantics, Decoder);
245}
246
247template <AMDGPUDisassembler::OpWidthTy OpWidth, unsigned ImmWidth,
248 unsigned OperandSemantics>
250 uint64_t /* Addr */,
251 const MCDisassembler *Decoder) {
252 return decodeSrcOp(Inst, 9, OpWidth, Imm, Imm, true, ImmWidth,
253 (AMDGPU::OperandSemantics)OperandSemantics, Decoder);
254}
255
256// Default decoders generated by tablegen: 'Decode<RegClass>RegisterClass'
257// when RegisterClass is used as an operand. Most often used for destination
258// operands.
259
261DECODE_OPERAND_REG_8(VGPR_32_Lo128)
264DECODE_OPERAND_REG_8(VReg_128)
265DECODE_OPERAND_REG_8(VReg_192)
266DECODE_OPERAND_REG_8(VReg_256)
267DECODE_OPERAND_REG_8(VReg_288)
268DECODE_OPERAND_REG_8(VReg_352)
269DECODE_OPERAND_REG_8(VReg_384)
270DECODE_OPERAND_REG_8(VReg_512)
271DECODE_OPERAND_REG_8(VReg_1024)
272
273DECODE_OPERAND_REG_7(SReg_32, OPW32)
274DECODE_OPERAND_REG_7(SReg_32_XEXEC, OPW32)
275DECODE_OPERAND_REG_7(SReg_32_XM0_XEXEC, OPW32)
276DECODE_OPERAND_REG_7(SReg_32_XEXEC_HI, OPW32)
277DECODE_OPERAND_REG_7(SReg_64, OPW64)
278DECODE_OPERAND_REG_7(SReg_64_XEXEC, OPW64)
279DECODE_OPERAND_REG_7(SReg_64_XEXEC_XNULL, OPW64)
280DECODE_OPERAND_REG_7(SReg_96, OPW96)
281DECODE_OPERAND_REG_7(SReg_128, OPW128)
282DECODE_OPERAND_REG_7(SReg_128_XNULL, OPW128)
283DECODE_OPERAND_REG_7(SReg_256, OPW256)
284DECODE_OPERAND_REG_7(SReg_256_XNULL, OPW256)
285DECODE_OPERAND_REG_7(SReg_512, OPW512)
286
289DECODE_OPERAND_REG_8(AReg_128)
290DECODE_OPERAND_REG_8(AReg_256)
291DECODE_OPERAND_REG_8(AReg_512)
292DECODE_OPERAND_REG_8(AReg_1024)
293
295 uint64_t /*Addr*/,
296 const MCDisassembler *Decoder) {
297 assert(isUInt<10>(Imm) && "10-bit encoding expected");
298 assert((Imm & (1 << 8)) == 0 && "Imm{8} should not be used");
299
300 bool IsHi = Imm & (1 << 9);
301 unsigned RegIdx = Imm & 0xff;
302 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
303 return addOperand(Inst, DAsm->createVGPR16Operand(RegIdx, IsHi));
304}
305
306static DecodeStatus
308 const MCDisassembler *Decoder) {
309 assert(isUInt<8>(Imm) && "8-bit encoding expected");
310
311 bool IsHi = Imm & (1 << 7);
312 unsigned RegIdx = Imm & 0x7f;
313 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
314 return addOperand(Inst, DAsm->createVGPR16Operand(RegIdx, IsHi));
315}
316
317template <AMDGPUDisassembler::OpWidthTy OpWidth, unsigned ImmWidth,
318 unsigned OperandSemantics>
320 uint64_t /*Addr*/,
321 const MCDisassembler *Decoder) {
322 assert(isUInt<9>(Imm) && "9-bit encoding expected");
323
324 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
325 if (Imm & AMDGPU::EncValues::IS_VGPR) {
326 bool IsHi = Imm & (1 << 7);
327 unsigned RegIdx = Imm & 0x7f;
328 return addOperand(Inst, DAsm->createVGPR16Operand(RegIdx, IsHi));
329 }
330 return addOperand(Inst, DAsm->decodeNonVGPRSrcOp(
331 OpWidth, Imm & 0xFF, false, ImmWidth,
332 (AMDGPU::OperandSemantics)OperandSemantics));
333}
334
335template <AMDGPUDisassembler::OpWidthTy OpWidth, unsigned ImmWidth,
336 unsigned OperandSemantics>
337static DecodeStatus
339 uint64_t /*Addr*/,
340 const MCDisassembler *Decoder) {
341 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
342 assert(isUInt<9>(Imm) && "9-bit encoding expected");
343
344 if (Imm & AMDGPU::EncValues::IS_VGPR) {
345 bool IsHi = Imm & (1 << 7);
346 unsigned RegIdx = Imm & 0x7f;
347 return addOperand(Inst, DAsm->createVGPR16Operand(RegIdx, IsHi));
348 }
349 return addOperand(Inst, DAsm->decodeNonVGPRSrcOp(
350 OpWidth, Imm & 0xFF, true, ImmWidth,
351 (AMDGPU::OperandSemantics)OperandSemantics));
352}
353
354template <AMDGPUDisassembler::OpWidthTy OpWidth, unsigned ImmWidth,
355 unsigned OperandSemantics>
356static DecodeStatus decodeOperand_VSrcT16(MCInst &Inst, unsigned Imm,
357 uint64_t /*Addr*/,
358 const MCDisassembler *Decoder) {
359 assert(isUInt<10>(Imm) && "10-bit encoding expected");
360
361 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
362 if (Imm & AMDGPU::EncValues::IS_VGPR) {
363 bool IsHi = Imm & (1 << 9);
364 unsigned RegIdx = Imm & 0xff;
365 return addOperand(Inst, DAsm->createVGPR16Operand(RegIdx, IsHi));
366 }
367 return addOperand(Inst, DAsm->decodeNonVGPRSrcOp(
368 OpWidth, Imm & 0xFF, false, ImmWidth,
369 (AMDGPU::OperandSemantics)OperandSemantics));
370}
371
372static DecodeStatus decodeOperand_VGPR_16(MCInst &Inst, unsigned Imm,
373 uint64_t /*Addr*/,
374 const MCDisassembler *Decoder) {
375 assert(isUInt<10>(Imm) && "10-bit encoding expected");
376 assert(Imm & AMDGPU::EncValues::IS_VGPR && "VGPR expected");
377
378 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
379
380 bool IsHi = Imm & (1 << 9);
381 unsigned RegIdx = Imm & 0xff;
382 return addOperand(Inst, DAsm->createVGPR16Operand(RegIdx, IsHi));
383}
384
385static DecodeStatus decodeOperand_KImmFP(MCInst &Inst, unsigned Imm,
387 const MCDisassembler *Decoder) {
388 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
389 return addOperand(Inst, DAsm->decodeMandatoryLiteralConstant(Imm));
390}
391
392static DecodeStatus decodeOperandVOPDDstY(MCInst &Inst, unsigned Val,
393 uint64_t Addr, const void *Decoder) {
394 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
395 return addOperand(Inst, DAsm->decodeVOPDDstYOp(Inst, Val));
396}
397
398static bool IsAGPROperand(const MCInst &Inst, int OpIdx,
399 const MCRegisterInfo *MRI) {
400 if (OpIdx < 0)
401 return false;
402
403 const MCOperand &Op = Inst.getOperand(OpIdx);
404 if (!Op.isReg())
405 return false;
406
407 MCRegister Sub = MRI->getSubReg(Op.getReg(), AMDGPU::sub0);
408 auto Reg = Sub ? Sub : Op.getReg();
409 return Reg >= AMDGPU::AGPR0 && Reg <= AMDGPU::AGPR255;
410}
411
412static DecodeStatus decodeAVLdSt(MCInst &Inst, unsigned Imm,
414 const MCDisassembler *Decoder) {
415 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
416 if (!DAsm->isGFX90A()) {
417 Imm &= 511;
418 } else {
419 // If atomic has both vdata and vdst their register classes are tied.
420 // The bit is decoded along with the vdst, first operand. We need to
421 // change register class to AGPR if vdst was AGPR.
422 // If a DS instruction has both data0 and data1 their register classes
423 // are also tied.
424 unsigned Opc = Inst.getOpcode();
425 uint64_t TSFlags = DAsm->getMCII()->get(Opc).TSFlags;
426 AMDGPU::OpName DataName = (TSFlags & SIInstrFlags::DS)
427 ? AMDGPU::OpName::data0
428 : AMDGPU::OpName::vdata;
429 const MCRegisterInfo *MRI = DAsm->getContext().getRegisterInfo();
430 int DataIdx = AMDGPU::getNamedOperandIdx(Opc, DataName);
431 if ((int)Inst.getNumOperands() == DataIdx) {
432 int DstIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst);
433 if (IsAGPROperand(Inst, DstIdx, MRI))
434 Imm |= 512;
435 }
436
437 if (TSFlags & SIInstrFlags::DS) {
438 int Data2Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::data1);
439 if ((int)Inst.getNumOperands() == Data2Idx &&
440 IsAGPROperand(Inst, DataIdx, MRI))
441 Imm |= 512;
442 }
443 }
444 return addOperand(Inst, DAsm->decodeSrcOp(Opw, Imm | 256));
445}
446
447template <AMDGPUDisassembler::OpWidthTy Opw>
448static DecodeStatus decodeAVLdSt(MCInst &Inst, unsigned Imm,
449 uint64_t /* Addr */,
450 const MCDisassembler *Decoder) {
451 return decodeAVLdSt(Inst, Imm, Opw, Decoder);
452}
453
454static DecodeStatus decodeOperand_VSrc_f64(MCInst &Inst, unsigned Imm,
456 const MCDisassembler *Decoder) {
457 assert(Imm < (1 << 9) && "9-bit encoding");
458 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
459 return addOperand(Inst,
460 DAsm->decodeSrcOp(AMDGPUDisassembler::OPW64, Imm, false, 64,
462}
463
464#define DECODE_SDWA(DecName) \
465DECODE_OPERAND(decodeSDWA##DecName, decodeSDWA##DecName)
466
467DECODE_SDWA(Src32)
468DECODE_SDWA(Src16)
469DECODE_SDWA(VopcDst)
470
471static DecodeStatus decodeVersionImm(MCInst &Inst, unsigned Imm,
472 uint64_t /* Addr */,
473 const MCDisassembler *Decoder) {
474 const auto *DAsm = static_cast<const AMDGPUDisassembler *>(Decoder);
475 return addOperand(Inst, DAsm->decodeVersionImm(Imm));
476}
477
478#include "AMDGPUGenDisassemblerTables.inc"
479
480//===----------------------------------------------------------------------===//
481//
482//===----------------------------------------------------------------------===//
483
484template <typename T> static inline T eatBytes(ArrayRef<uint8_t>& Bytes) {
485 assert(Bytes.size() >= sizeof(T));
486 const auto Res =
487 support::endian::read<T, llvm::endianness::little>(Bytes.data());
488 Bytes = Bytes.slice(sizeof(T));
489 return Res;
490}
491
493 assert(Bytes.size() >= 12);
494 uint64_t Lo =
495 support::endian::read<uint64_t, llvm::endianness::little>(Bytes.data());
496 Bytes = Bytes.slice(8);
497 uint64_t Hi =
498 support::endian::read<uint32_t, llvm::endianness::little>(Bytes.data());
499 Bytes = Bytes.slice(4);
500 return DecoderUInt128(Lo, Hi);
501}
502
504 assert(Bytes.size() >= 16);
505 uint64_t Lo =
506 support::endian::read<uint64_t, llvm::endianness::little>(Bytes.data());
507 Bytes = Bytes.slice(8);
508 uint64_t Hi =
509 support::endian::read<uint64_t, llvm::endianness::little>(Bytes.data());
510 Bytes = Bytes.slice(8);
511 return DecoderUInt128(Lo, Hi);
512}
513
515 ArrayRef<uint8_t> Bytes_,
517 raw_ostream &CS) const {
518 unsigned MaxInstBytesNum = std::min((size_t)TargetMaxInstBytes, Bytes_.size());
519 Bytes = Bytes_.slice(0, MaxInstBytesNum);
520
521 // In case the opcode is not recognized we'll assume a Size of 4 bytes (unless
522 // there are fewer bytes left). This will be overridden on success.
523 Size = std::min((size_t)4, Bytes_.size());
524
525 do {
526 // ToDo: better to switch encoding length using some bit predicate
527 // but it is unknown yet, so try all we can
528
529 // Try to decode DPP and SDWA first to solve conflict with VOP1 and VOP2
530 // encodings
531 if (isGFX11Plus() && Bytes.size() >= 12 ) {
532 DecoderUInt128 DecW = eat12Bytes(Bytes);
533
534 if (isGFX11() &&
535 tryDecodeInst(DecoderTableGFX1196, DecoderTableGFX11_FAKE1696, MI,
536 DecW, Address, CS))
537 break;
538
539 if (isGFX12() &&
540 tryDecodeInst(DecoderTableGFX1296, DecoderTableGFX12_FAKE1696, MI,
541 DecW, Address, CS))
542 break;
543
544 if (isGFX12() &&
545 tryDecodeInst(DecoderTableGFX12W6496, MI, DecW, Address, CS))
546 break;
547
548 // Reinitialize Bytes
549 Bytes = Bytes_.slice(0, MaxInstBytesNum);
550
551 } else if (Bytes.size() >= 16 &&
552 STI.hasFeature(AMDGPU::FeatureGFX950Insts)) {
553 DecoderUInt128 DecW = eat16Bytes(Bytes);
554 if (tryDecodeInst(DecoderTableGFX940128, MI, DecW, Address, CS))
555 break;
556
557 // Reinitialize Bytes
558 Bytes = Bytes_.slice(0, MaxInstBytesNum);
559 }
560
561 if (Bytes.size() >= 8) {
562 const uint64_t QW = eatBytes<uint64_t>(Bytes);
563
564 if (STI.hasFeature(AMDGPU::FeatureGFX10_BEncoding) &&
565 tryDecodeInst(DecoderTableGFX10_B64, MI, QW, Address, CS))
566 break;
567
568 if (STI.hasFeature(AMDGPU::FeatureUnpackedD16VMem) &&
569 tryDecodeInst(DecoderTableGFX80_UNPACKED64, MI, QW, Address, CS))
570 break;
571
572 if (STI.hasFeature(AMDGPU::FeatureGFX950Insts) &&
573 tryDecodeInst(DecoderTableGFX95064, MI, QW, Address, CS))
574 break;
575
576 // Some GFX9 subtargets repurposed the v_mad_mix_f32, v_mad_mixlo_f16 and
577 // v_mad_mixhi_f16 for FMA variants. Try to decode using this special
578 // table first so we print the correct name.
579 if (STI.hasFeature(AMDGPU::FeatureFmaMixInsts) &&
580 tryDecodeInst(DecoderTableGFX9_DL64, MI, QW, Address, CS))
581 break;
582
583 if (STI.hasFeature(AMDGPU::FeatureGFX940Insts) &&
584 tryDecodeInst(DecoderTableGFX94064, MI, QW, Address, CS))
585 break;
586
587 if (STI.hasFeature(AMDGPU::FeatureGFX90AInsts) &&
588 tryDecodeInst(DecoderTableGFX90A64, MI, QW, Address, CS))
589 break;
590
591 if ((isVI() || isGFX9()) &&
592 tryDecodeInst(DecoderTableGFX864, MI, QW, Address, CS))
593 break;
594
595 if (isGFX9() && tryDecodeInst(DecoderTableGFX964, MI, QW, Address, CS))
596 break;
597
598 if (isGFX10() && tryDecodeInst(DecoderTableGFX1064, MI, QW, Address, CS))
599 break;
600
601 if (isGFX12() &&
602 tryDecodeInst(DecoderTableGFX1264, DecoderTableGFX12_FAKE1664, MI, QW,
603 Address, CS))
604 break;
605
606 if (isGFX11() &&
607 tryDecodeInst(DecoderTableGFX1164, DecoderTableGFX11_FAKE1664, MI, QW,
608 Address, CS))
609 break;
610
611 if (isGFX11() &&
612 tryDecodeInst(DecoderTableGFX11W6464, MI, QW, Address, CS))
613 break;
614
615 if (isGFX12() &&
616 tryDecodeInst(DecoderTableGFX12W6464, MI, QW, Address, CS))
617 break;
618
619 // Reinitialize Bytes
620 Bytes = Bytes_.slice(0, MaxInstBytesNum);
621 }
622
623 // Try decode 32-bit instruction
624 if (Bytes.size() >= 4) {
625 const uint32_t DW = eatBytes<uint32_t>(Bytes);
626
627 if ((isVI() || isGFX9()) &&
628 tryDecodeInst(DecoderTableGFX832, MI, DW, Address, CS))
629 break;
630
631 if (tryDecodeInst(DecoderTableAMDGPU32, MI, DW, Address, CS))
632 break;
633
634 if (isGFX9() && tryDecodeInst(DecoderTableGFX932, MI, DW, Address, CS))
635 break;
636
637 if (STI.hasFeature(AMDGPU::FeatureGFX950Insts) &&
638 tryDecodeInst(DecoderTableGFX95032, MI, DW, Address, CS))
639 break;
640
641 if (STI.hasFeature(AMDGPU::FeatureGFX90AInsts) &&
642 tryDecodeInst(DecoderTableGFX90A32, MI, DW, Address, CS))
643 break;
644
645 if (STI.hasFeature(AMDGPU::FeatureGFX10_BEncoding) &&
646 tryDecodeInst(DecoderTableGFX10_B32, MI, DW, Address, CS))
647 break;
648
649 if (isGFX10() && tryDecodeInst(DecoderTableGFX1032, MI, DW, Address, CS))
650 break;
651
652 if (isGFX11() &&
653 tryDecodeInst(DecoderTableGFX1132, DecoderTableGFX11_FAKE1632, MI, DW,
654 Address, CS))
655 break;
656
657 if (isGFX12() &&
658 tryDecodeInst(DecoderTableGFX1232, DecoderTableGFX12_FAKE1632, MI, DW,
659 Address, CS))
660 break;
661 }
662
664 } while (false);
665
666 if (MCII->get(MI.getOpcode()).TSFlags & SIInstrFlags::DPP) {
667 if (isMacDPP(MI))
669
670 if (MCII->get(MI.getOpcode()).TSFlags & SIInstrFlags::VOP3P)
672 else if (MCII->get(MI.getOpcode()).TSFlags & SIInstrFlags::VOPC)
673 convertVOPCDPPInst(MI); // Special VOP3 case
674 else if (AMDGPU::isVOPC64DPP(MI.getOpcode()))
675 convertVOPC64DPPInst(MI); // Special VOP3 case
676 else if (AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::dpp8) !=
677 -1)
679 else if (MCII->get(MI.getOpcode()).TSFlags & SIInstrFlags::VOP3)
680 convertVOP3DPPInst(MI); // Regular VOP3 case
681 }
682
684
685 if (AMDGPU::isMAC(MI.getOpcode())) {
686 // Insert dummy unused src2_modifiers.
688 AMDGPU::OpName::src2_modifiers);
689 }
690
691 if (MI.getOpcode() == AMDGPU::V_CVT_SR_BF8_F32_e64_dpp ||
692 MI.getOpcode() == AMDGPU::V_CVT_SR_FP8_F32_e64_dpp) {
693 // Insert dummy unused src2_modifiers.
695 AMDGPU::OpName::src2_modifiers);
696 }
697
698 if ((MCII->get(MI.getOpcode()).TSFlags & SIInstrFlags::DS) &&
700 insertNamedMCOperand(MI, MCOperand::createImm(0), AMDGPU::OpName::gds);
701 }
702
703 if (MCII->get(MI.getOpcode()).TSFlags &
705 int CPolPos = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
706 AMDGPU::OpName::cpol);
707 if (CPolPos != -1) {
708 unsigned CPol =
709 (MCII->get(MI.getOpcode()).TSFlags & SIInstrFlags::IsAtomicRet) ?
711 if (MI.getNumOperands() <= (unsigned)CPolPos) {
713 AMDGPU::OpName::cpol);
714 } else if (CPol) {
715 MI.getOperand(CPolPos).setImm(MI.getOperand(CPolPos).getImm() | CPol);
716 }
717 }
718 }
719
720 if ((MCII->get(MI.getOpcode()).TSFlags &
722 (STI.hasFeature(AMDGPU::FeatureGFX90AInsts))) {
723 // GFX90A lost TFE, its place is occupied by ACC.
724 int TFEOpIdx =
725 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::tfe);
726 if (TFEOpIdx != -1) {
727 auto *TFEIter = MI.begin();
728 std::advance(TFEIter, TFEOpIdx);
729 MI.insert(TFEIter, MCOperand::createImm(0));
730 }
731 }
732
733 if (MCII->get(MI.getOpcode()).TSFlags &
735 int SWZOpIdx =
736 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::swz);
737 if (SWZOpIdx != -1) {
738 auto *SWZIter = MI.begin();
739 std::advance(SWZIter, SWZOpIdx);
740 MI.insert(SWZIter, MCOperand::createImm(0));
741 }
742 }
743
744 if (MCII->get(MI.getOpcode()).TSFlags & SIInstrFlags::MIMG) {
745 int VAddr0Idx =
746 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vaddr0);
747 int RsrcIdx =
748 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::srsrc);
749 unsigned NSAArgs = RsrcIdx - VAddr0Idx - 1;
750 if (VAddr0Idx >= 0 && NSAArgs > 0) {
751 unsigned NSAWords = (NSAArgs + 3) / 4;
752 if (Bytes.size() < 4 * NSAWords)
754 for (unsigned i = 0; i < NSAArgs; ++i) {
755 const unsigned VAddrIdx = VAddr0Idx + 1 + i;
756 auto VAddrRCID =
757 MCII->get(MI.getOpcode()).operands()[VAddrIdx].RegClass;
758 MI.insert(MI.begin() + VAddrIdx, createRegOperand(VAddrRCID, Bytes[i]));
759 }
760 Bytes = Bytes.slice(4 * NSAWords);
761 }
762
764 }
765
766 if (MCII->get(MI.getOpcode()).TSFlags &
769
770 if (MCII->get(MI.getOpcode()).TSFlags & SIInstrFlags::EXP)
772
773 if (MCII->get(MI.getOpcode()).TSFlags & SIInstrFlags::VINTERP)
775
776 if (MCII->get(MI.getOpcode()).TSFlags & SIInstrFlags::SDWA)
778
779 if (MCII->get(MI.getOpcode()).TSFlags & SIInstrFlags::IsMAI)
781
782 int VDstIn_Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
783 AMDGPU::OpName::vdst_in);
784 if (VDstIn_Idx != -1) {
785 int Tied = MCII->get(MI.getOpcode()).getOperandConstraint(VDstIn_Idx,
787 if (Tied != -1 && (MI.getNumOperands() <= (unsigned)VDstIn_Idx ||
788 !MI.getOperand(VDstIn_Idx).isReg() ||
789 MI.getOperand(VDstIn_Idx).getReg() != MI.getOperand(Tied).getReg())) {
790 if (MI.getNumOperands() > (unsigned)VDstIn_Idx)
791 MI.erase(&MI.getOperand(VDstIn_Idx));
793 MCOperand::createReg(MI.getOperand(Tied).getReg()),
794 AMDGPU::OpName::vdst_in);
795 }
796 }
797
798 int ImmLitIdx =
799 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::imm);
800 bool IsSOPK = MCII->get(MI.getOpcode()).TSFlags & SIInstrFlags::SOPK;
801 if (ImmLitIdx != -1 && !IsSOPK)
802 convertFMAanyK(MI, ImmLitIdx);
803
804 Size = MaxInstBytesNum - Bytes.size();
806}
807
809 if (STI.hasFeature(AMDGPU::FeatureGFX11Insts)) {
810 // The MCInst still has these fields even though they are no longer encoded
811 // in the GFX11 instruction.
812 insertNamedMCOperand(MI, MCOperand::createImm(0), AMDGPU::OpName::vm);
813 insertNamedMCOperand(MI, MCOperand::createImm(0), AMDGPU::OpName::compr);
814 }
815}
816
819 if (MI.getOpcode() == AMDGPU::V_INTERP_P10_F16_F32_inreg_t16_gfx11 ||
820 MI.getOpcode() == AMDGPU::V_INTERP_P10_F16_F32_inreg_fake16_gfx11 ||
821 MI.getOpcode() == AMDGPU::V_INTERP_P10_F16_F32_inreg_t16_gfx12 ||
822 MI.getOpcode() == AMDGPU::V_INTERP_P10_F16_F32_inreg_fake16_gfx12 ||
823 MI.getOpcode() == AMDGPU::V_INTERP_P10_RTZ_F16_F32_inreg_t16_gfx11 ||
824 MI.getOpcode() == AMDGPU::V_INTERP_P10_RTZ_F16_F32_inreg_fake16_gfx11 ||
825 MI.getOpcode() == AMDGPU::V_INTERP_P10_RTZ_F16_F32_inreg_t16_gfx12 ||
826 MI.getOpcode() == AMDGPU::V_INTERP_P10_RTZ_F16_F32_inreg_fake16_gfx12 ||
827 MI.getOpcode() == AMDGPU::V_INTERP_P2_F16_F32_inreg_t16_gfx11 ||
828 MI.getOpcode() == AMDGPU::V_INTERP_P2_F16_F32_inreg_fake16_gfx11 ||
829 MI.getOpcode() == AMDGPU::V_INTERP_P2_F16_F32_inreg_t16_gfx12 ||
830 MI.getOpcode() == AMDGPU::V_INTERP_P2_F16_F32_inreg_fake16_gfx12 ||
831 MI.getOpcode() == AMDGPU::V_INTERP_P2_RTZ_F16_F32_inreg_t16_gfx11 ||
832 MI.getOpcode() == AMDGPU::V_INTERP_P2_RTZ_F16_F32_inreg_fake16_gfx11 ||
833 MI.getOpcode() == AMDGPU::V_INTERP_P2_RTZ_F16_F32_inreg_t16_gfx12 ||
834 MI.getOpcode() == AMDGPU::V_INTERP_P2_RTZ_F16_F32_inreg_fake16_gfx12) {
835 // The MCInst has this field that is not directly encoded in the
836 // instruction.
837 insertNamedMCOperand(MI, MCOperand::createImm(0), AMDGPU::OpName::op_sel);
838 }
839}
840
842 if (STI.hasFeature(AMDGPU::FeatureGFX9) ||
843 STI.hasFeature(AMDGPU::FeatureGFX10)) {
844 if (AMDGPU::hasNamedOperand(MI.getOpcode(), AMDGPU::OpName::sdst))
845 // VOPC - insert clamp
846 insertNamedMCOperand(MI, MCOperand::createImm(0), AMDGPU::OpName::clamp);
847 } else if (STI.hasFeature(AMDGPU::FeatureVolcanicIslands)) {
848 int SDst = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::sdst);
849 if (SDst != -1) {
850 // VOPC - insert VCC register as sdst
852 AMDGPU::OpName::sdst);
853 } else {
854 // VOP1/2 - insert omod if present in instruction
855 insertNamedMCOperand(MI, MCOperand::createImm(0), AMDGPU::OpName::omod);
856 }
857 }
858}
859
860/// Adjust the register values used by V_MFMA_F8F6F4_f8_f8 instructions to the
861/// appropriate subregister for the used format width.
863 MCOperand &MO, uint8_t NumRegs) {
864 switch (NumRegs) {
865 case 4:
866 return MO.setReg(MRI.getSubReg(MO.getReg(), AMDGPU::sub0_sub1_sub2_sub3));
867 case 6:
868 return MO.setReg(
869 MRI.getSubReg(MO.getReg(), AMDGPU::sub0_sub1_sub2_sub3_sub4_sub5));
870 case 8:
871 // No-op in cases where one operand is still f8/bf8.
872 return;
873 default:
874 llvm_unreachable("Unexpected size for mfma f8f6f4 operand");
875 }
876}
877
878/// f8f6f4 instructions have different pseudos depending on the used formats. In
879/// the disassembler table, we only have the variants with the largest register
880/// classes which assume using an fp8/bf8 format for both operands. The actual
881/// register class depends on the format in blgp and cbsz operands. Adjust the
882/// register classes depending on the used format.
884 int BlgpIdx =
885 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::blgp);
886 if (BlgpIdx == -1)
887 return;
888
889 int CbszIdx =
890 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::cbsz);
891
892 unsigned CBSZ = MI.getOperand(CbszIdx).getImm();
893 unsigned BLGP = MI.getOperand(BlgpIdx).getImm();
894
895 const AMDGPU::MFMA_F8F6F4_Info *AdjustedRegClassOpcode =
896 AMDGPU::getMFMA_F8F6F4_WithFormatArgs(CBSZ, BLGP, MI.getOpcode());
897 if (!AdjustedRegClassOpcode ||
898 AdjustedRegClassOpcode->Opcode == MI.getOpcode())
899 return;
900
901 MI.setOpcode(AdjustedRegClassOpcode->Opcode);
902 int Src0Idx =
903 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src0);
904 int Src1Idx =
905 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src1);
906 adjustMFMA_F8F6F4OpRegClass(MRI, MI.getOperand(Src0Idx),
907 AdjustedRegClassOpcode->NumRegsSrcA);
908 adjustMFMA_F8F6F4OpRegClass(MRI, MI.getOperand(Src1Idx),
909 AdjustedRegClassOpcode->NumRegsSrcB);
910}
911
913 unsigned OpSel = 0;
914 unsigned OpSelHi = 0;
915 unsigned NegLo = 0;
916 unsigned NegHi = 0;
917};
918
919// Reconstruct values of VOP3/VOP3P operands such as op_sel.
920// Note that these values do not affect disassembler output,
921// so this is only necessary for consistency with src_modifiers.
923 bool IsVOP3P = false) {
924 VOPModifiers Modifiers;
925 unsigned Opc = MI.getOpcode();
926 const AMDGPU::OpName ModOps[] = {AMDGPU::OpName::src0_modifiers,
927 AMDGPU::OpName::src1_modifiers,
928 AMDGPU::OpName::src2_modifiers};
929 for (int J = 0; J < 3; ++J) {
930 int OpIdx = AMDGPU::getNamedOperandIdx(Opc, ModOps[J]);
931 if (OpIdx == -1)
932 continue;
933
934 unsigned Val = MI.getOperand(OpIdx).getImm();
935
936 Modifiers.OpSel |= !!(Val & SISrcMods::OP_SEL_0) << J;
937 if (IsVOP3P) {
938 Modifiers.OpSelHi |= !!(Val & SISrcMods::OP_SEL_1) << J;
939 Modifiers.NegLo |= !!(Val & SISrcMods::NEG) << J;
940 Modifiers.NegHi |= !!(Val & SISrcMods::NEG_HI) << J;
941 } else if (J == 0) {
942 Modifiers.OpSel |= !!(Val & SISrcMods::DST_OP_SEL) << 3;
943 }
944 }
945
946 return Modifiers;
947}
948
949// Instructions decode the op_sel/suffix bits into the src_modifier
950// operands. Copy those bits into the src operands for true16 VGPRs.
952 const unsigned Opc = MI.getOpcode();
953 const MCRegisterClass &ConversionRC =
954 MRI.getRegClass(AMDGPU::VGPR_16RegClassID);
955 constexpr std::array<std::tuple<AMDGPU::OpName, AMDGPU::OpName, unsigned>, 4>
956 OpAndOpMods = {{{AMDGPU::OpName::src0, AMDGPU::OpName::src0_modifiers,
958 {AMDGPU::OpName::src1, AMDGPU::OpName::src1_modifiers,
960 {AMDGPU::OpName::src2, AMDGPU::OpName::src2_modifiers,
962 {AMDGPU::OpName::vdst, AMDGPU::OpName::src0_modifiers,
964 for (const auto &[OpName, OpModsName, OpSelMask] : OpAndOpMods) {
965 int OpIdx = AMDGPU::getNamedOperandIdx(Opc, OpName);
966 int OpModsIdx = AMDGPU::getNamedOperandIdx(Opc, OpModsName);
967 if (OpIdx == -1 || OpModsIdx == -1)
968 continue;
969 MCOperand &Op = MI.getOperand(OpIdx);
970 if (!Op.isReg())
971 continue;
972 if (!ConversionRC.contains(Op.getReg()))
973 continue;
974 unsigned OpEnc = MRI.getEncodingValue(Op.getReg());
975 const MCOperand &OpMods = MI.getOperand(OpModsIdx);
976 unsigned ModVal = OpMods.getImm();
977 if (ModVal & OpSelMask) { // isHi
978 unsigned RegIdx = OpEnc & AMDGPU::HWEncoding::REG_IDX_MASK;
979 Op.setReg(ConversionRC.getRegister(RegIdx * 2 + 1));
980 }
981 }
982}
983
984// MAC opcodes have special old and src2 operands.
985// src2 is tied to dst, while old is not tied (but assumed to be).
987 constexpr int DST_IDX = 0;
988 auto Opcode = MI.getOpcode();
989 const auto &Desc = MCII->get(Opcode);
990 auto OldIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::old);
991
992 if (OldIdx != -1 && Desc.getOperandConstraint(
993 OldIdx, MCOI::OperandConstraint::TIED_TO) == -1) {
994 assert(AMDGPU::hasNamedOperand(Opcode, AMDGPU::OpName::src2));
995 assert(Desc.getOperandConstraint(
996 AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src2),
998 (void)DST_IDX;
999 return true;
1000 }
1001
1002 return false;
1003}
1004
1005// Create dummy old operand and insert dummy unused src2_modifiers
1007 assert(MI.getNumOperands() + 1 < MCII->get(MI.getOpcode()).getNumOperands());
1008 insertNamedMCOperand(MI, MCOperand::createReg(0), AMDGPU::OpName::old);
1010 AMDGPU::OpName::src2_modifiers);
1011}
1012
1014 unsigned Opc = MI.getOpcode();
1015
1016 int VDstInIdx =
1017 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vdst_in);
1018 if (VDstInIdx != -1)
1019 insertNamedMCOperand(MI, MI.getOperand(0), AMDGPU::OpName::vdst_in);
1020
1021 unsigned DescNumOps = MCII->get(Opc).getNumOperands();
1022 if (MI.getNumOperands() < DescNumOps &&
1023 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::op_sel)) {
1025 auto Mods = collectVOPModifiers(MI);
1027 AMDGPU::OpName::op_sel);
1028 } else {
1029 // Insert dummy unused src modifiers.
1030 if (MI.getNumOperands() < DescNumOps &&
1031 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::src0_modifiers))
1033 AMDGPU::OpName::src0_modifiers);
1034
1035 if (MI.getNumOperands() < DescNumOps &&
1036 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::src1_modifiers))
1038 AMDGPU::OpName::src1_modifiers);
1039 }
1040}
1041
1044
1045 int VDstInIdx =
1046 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vdst_in);
1047 if (VDstInIdx != -1)
1048 insertNamedMCOperand(MI, MI.getOperand(0), AMDGPU::OpName::vdst_in);
1049
1050 unsigned Opc = MI.getOpcode();
1051 unsigned DescNumOps = MCII->get(Opc).getNumOperands();
1052 if (MI.getNumOperands() < DescNumOps &&
1053 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::op_sel)) {
1054 auto Mods = collectVOPModifiers(MI);
1056 AMDGPU::OpName::op_sel);
1057 }
1058}
1059
1060// Note that before gfx10, the MIMG encoding provided no information about
1061// VADDR size. Consequently, decoded instructions always show address as if it
1062// has 1 dword, which could be not really so.
1064 auto TSFlags = MCII->get(MI.getOpcode()).TSFlags;
1065
1066 int VDstIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
1067 AMDGPU::OpName::vdst);
1068
1069 int VDataIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
1070 AMDGPU::OpName::vdata);
1071 int VAddr0Idx =
1072 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vaddr0);
1073 AMDGPU::OpName RsrcOpName = (TSFlags & SIInstrFlags::MIMG)
1074 ? AMDGPU::OpName::srsrc
1075 : AMDGPU::OpName::rsrc;
1076 int RsrcIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), RsrcOpName);
1077 int DMaskIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
1078 AMDGPU::OpName::dmask);
1079
1080 int TFEIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
1081 AMDGPU::OpName::tfe);
1082 int D16Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
1083 AMDGPU::OpName::d16);
1084
1085 const AMDGPU::MIMGInfo *Info = AMDGPU::getMIMGInfo(MI.getOpcode());
1086 const AMDGPU::MIMGBaseOpcodeInfo *BaseOpcode =
1088
1089 assert(VDataIdx != -1);
1090 if (BaseOpcode->BVH) {
1091 // Add A16 operand for intersect_ray instructions
1092 addOperand(MI, MCOperand::createImm(BaseOpcode->A16));
1093 return;
1094 }
1095
1096 bool IsAtomic = (VDstIdx != -1);
1097 bool IsGather4 = TSFlags & SIInstrFlags::Gather4;
1098 bool IsVSample = TSFlags & SIInstrFlags::VSAMPLE;
1099 bool IsNSA = false;
1100 bool IsPartialNSA = false;
1101 unsigned AddrSize = Info->VAddrDwords;
1102
1103 if (isGFX10Plus()) {
1104 unsigned DimIdx =
1105 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::dim);
1106 int A16Idx =
1107 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::a16);
1108 const AMDGPU::MIMGDimInfo *Dim =
1109 AMDGPU::getMIMGDimInfoByEncoding(MI.getOperand(DimIdx).getImm());
1110 const bool IsA16 = (A16Idx != -1 && MI.getOperand(A16Idx).getImm());
1111
1112 AddrSize =
1113 AMDGPU::getAddrSizeMIMGOp(BaseOpcode, Dim, IsA16, AMDGPU::hasG16(STI));
1114
1115 // VSAMPLE insts that do not use vaddr3 behave the same as NSA forms.
1116 // VIMAGE insts other than BVH never use vaddr4.
1117 IsNSA = Info->MIMGEncoding == AMDGPU::MIMGEncGfx10NSA ||
1118 Info->MIMGEncoding == AMDGPU::MIMGEncGfx11NSA ||
1119 Info->MIMGEncoding == AMDGPU::MIMGEncGfx12;
1120 if (!IsNSA) {
1121 if (!IsVSample && AddrSize > 12)
1122 AddrSize = 16;
1123 } else {
1124 if (AddrSize > Info->VAddrDwords) {
1125 if (!STI.hasFeature(AMDGPU::FeaturePartialNSAEncoding)) {
1126 // The NSA encoding does not contain enough operands for the
1127 // combination of base opcode / dimension. Should this be an error?
1128 return;
1129 }
1130 IsPartialNSA = true;
1131 }
1132 }
1133 }
1134
1135 unsigned DMask = MI.getOperand(DMaskIdx).getImm() & 0xf;
1136 unsigned DstSize = IsGather4 ? 4 : std::max(llvm::popcount(DMask), 1);
1137
1138 bool D16 = D16Idx >= 0 && MI.getOperand(D16Idx).getImm();
1139 if (D16 && AMDGPU::hasPackedD16(STI)) {
1140 DstSize = (DstSize + 1) / 2;
1141 }
1142
1143 if (TFEIdx != -1 && MI.getOperand(TFEIdx).getImm())
1144 DstSize += 1;
1145
1146 if (DstSize == Info->VDataDwords && AddrSize == Info->VAddrDwords)
1147 return;
1148
1149 int NewOpcode =
1150 AMDGPU::getMIMGOpcode(Info->BaseOpcode, Info->MIMGEncoding, DstSize, AddrSize);
1151 if (NewOpcode == -1)
1152 return;
1153
1154 // Widen the register to the correct number of enabled channels.
1155 MCRegister NewVdata;
1156 if (DstSize != Info->VDataDwords) {
1157 auto DataRCID = MCII->get(NewOpcode).operands()[VDataIdx].RegClass;
1158
1159 // Get first subregister of VData
1160 MCRegister Vdata0 = MI.getOperand(VDataIdx).getReg();
1161 MCRegister VdataSub0 = MRI.getSubReg(Vdata0, AMDGPU::sub0);
1162 Vdata0 = (VdataSub0 != 0)? VdataSub0 : Vdata0;
1163
1164 NewVdata = MRI.getMatchingSuperReg(Vdata0, AMDGPU::sub0,
1165 &MRI.getRegClass(DataRCID));
1166 if (!NewVdata) {
1167 // It's possible to encode this such that the low register + enabled
1168 // components exceeds the register count.
1169 return;
1170 }
1171 }
1172
1173 // If not using NSA on GFX10+, widen vaddr0 address register to correct size.
1174 // If using partial NSA on GFX11+ widen last address register.
1175 int VAddrSAIdx = IsPartialNSA ? (RsrcIdx - 1) : VAddr0Idx;
1176 MCRegister NewVAddrSA;
1177 if (STI.hasFeature(AMDGPU::FeatureNSAEncoding) && (!IsNSA || IsPartialNSA) &&
1178 AddrSize != Info->VAddrDwords) {
1179 MCRegister VAddrSA = MI.getOperand(VAddrSAIdx).getReg();
1180 MCRegister VAddrSubSA = MRI.getSubReg(VAddrSA, AMDGPU::sub0);
1181 VAddrSA = VAddrSubSA ? VAddrSubSA : VAddrSA;
1182
1183 auto AddrRCID = MCII->get(NewOpcode).operands()[VAddrSAIdx].RegClass;
1184 NewVAddrSA = MRI.getMatchingSuperReg(VAddrSA, AMDGPU::sub0,
1185 &MRI.getRegClass(AddrRCID));
1186 if (!NewVAddrSA)
1187 return;
1188 }
1189
1190 MI.setOpcode(NewOpcode);
1191
1192 if (NewVdata != AMDGPU::NoRegister) {
1193 MI.getOperand(VDataIdx) = MCOperand::createReg(NewVdata);
1194
1195 if (IsAtomic) {
1196 // Atomic operations have an additional operand (a copy of data)
1197 MI.getOperand(VDstIdx) = MCOperand::createReg(NewVdata);
1198 }
1199 }
1200
1201 if (NewVAddrSA) {
1202 MI.getOperand(VAddrSAIdx) = MCOperand::createReg(NewVAddrSA);
1203 } else if (IsNSA) {
1204 assert(AddrSize <= Info->VAddrDwords);
1205 MI.erase(MI.begin() + VAddr0Idx + AddrSize,
1206 MI.begin() + VAddr0Idx + Info->VAddrDwords);
1207 }
1208}
1209
1210// Opsel and neg bits are used in src_modifiers and standalone operands. Autogen
1211// decoder only adds to src_modifiers, so manually add the bits to the other
1212// operands.
1214 unsigned Opc = MI.getOpcode();
1215 unsigned DescNumOps = MCII->get(Opc).getNumOperands();
1216 auto Mods = collectVOPModifiers(MI, true);
1217
1218 if (MI.getNumOperands() < DescNumOps &&
1219 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::vdst_in))
1220 insertNamedMCOperand(MI, MCOperand::createImm(0), AMDGPU::OpName::vdst_in);
1221
1222 if (MI.getNumOperands() < DescNumOps &&
1223 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::op_sel))
1225 AMDGPU::OpName::op_sel);
1226 if (MI.getNumOperands() < DescNumOps &&
1227 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::op_sel_hi))
1229 AMDGPU::OpName::op_sel_hi);
1230 if (MI.getNumOperands() < DescNumOps &&
1231 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::neg_lo))
1233 AMDGPU::OpName::neg_lo);
1234 if (MI.getNumOperands() < DescNumOps &&
1235 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::neg_hi))
1237 AMDGPU::OpName::neg_hi);
1238}
1239
1240// Create dummy old operand and insert optional operands
1242 unsigned Opc = MI.getOpcode();
1243 unsigned DescNumOps = MCII->get(Opc).getNumOperands();
1244
1245 if (MI.getNumOperands() < DescNumOps &&
1246 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::old))
1247 insertNamedMCOperand(MI, MCOperand::createReg(0), AMDGPU::OpName::old);
1248
1249 if (MI.getNumOperands() < DescNumOps &&
1250 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::src0_modifiers))
1252 AMDGPU::OpName::src0_modifiers);
1253
1254 if (MI.getNumOperands() < DescNumOps &&
1255 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::src1_modifiers))
1257 AMDGPU::OpName::src1_modifiers);
1258}
1259
1261 unsigned Opc = MI.getOpcode();
1262 unsigned DescNumOps = MCII->get(Opc).getNumOperands();
1263
1265
1266 if (MI.getNumOperands() < DescNumOps &&
1267 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::op_sel)) {
1270 AMDGPU::OpName::op_sel);
1271 }
1272}
1273
1274void AMDGPUDisassembler::convertFMAanyK(MCInst &MI, int ImmLitIdx) const {
1275 assert(HasLiteral && "Should have decoded a literal");
1276 const MCInstrDesc &Desc = MCII->get(MI.getOpcode());
1277 unsigned DescNumOps = Desc.getNumOperands();
1279 AMDGPU::OpName::immDeferred);
1280 assert(DescNumOps == MI.getNumOperands());
1281 for (unsigned I = 0; I < DescNumOps; ++I) {
1282 auto &Op = MI.getOperand(I);
1283 auto OpType = Desc.operands()[I].OperandType;
1284 bool IsDeferredOp = (OpType == AMDGPU::OPERAND_REG_IMM_FP32_DEFERRED ||
1286 if (Op.isImm() && Op.getImm() == AMDGPU::EncValues::LITERAL_CONST &&
1287 IsDeferredOp)
1288 Op.setImm(Literal);
1289 }
1290}
1291
1292const char* AMDGPUDisassembler::getRegClassName(unsigned RegClassID) const {
1293 return getContext().getRegisterInfo()->
1294 getRegClassName(&AMDGPUMCRegisterClasses[RegClassID]);
1295}
1296
1297inline
1299 const Twine& ErrMsg) const {
1300 *CommentStream << "Error: " + ErrMsg;
1301
1302 // ToDo: add support for error operands to MCInst.h
1303 // return MCOperand::createError(V);
1304 return MCOperand();
1305}
1306
1307inline
1310}
1311
1312inline
1314 unsigned Val) const {
1315 const auto& RegCl = AMDGPUMCRegisterClasses[RegClassID];
1316 if (Val >= RegCl.getNumRegs())
1317 return errOperand(Val, Twine(getRegClassName(RegClassID)) +
1318 ": unknown register " + Twine(Val));
1319 return createRegOperand(RegCl.getRegister(Val));
1320}
1321
1322inline
1324 unsigned Val) const {
1325 // ToDo: SI/CI have 104 SGPRs, VI - 102
1326 // Valery: here we accepting as much as we can, let assembler sort it out
1327 int shift = 0;
1328 switch (SRegClassID) {
1329 case AMDGPU::SGPR_32RegClassID:
1330 case AMDGPU::TTMP_32RegClassID:
1331 break;
1332 case AMDGPU::SGPR_64RegClassID:
1333 case AMDGPU::TTMP_64RegClassID:
1334 shift = 1;
1335 break;
1336 case AMDGPU::SGPR_96RegClassID:
1337 case AMDGPU::TTMP_96RegClassID:
1338 case AMDGPU::SGPR_128RegClassID:
1339 case AMDGPU::TTMP_128RegClassID:
1340 // ToDo: unclear if s[100:104] is available on VI. Can we use VCC as SGPR in
1341 // this bundle?
1342 case AMDGPU::SGPR_256RegClassID:
1343 case AMDGPU::TTMP_256RegClassID:
1344 // ToDo: unclear if s[96:104] is available on VI. Can we use VCC as SGPR in
1345 // this bundle?
1346 case AMDGPU::SGPR_288RegClassID:
1347 case AMDGPU::TTMP_288RegClassID:
1348 case AMDGPU::SGPR_320RegClassID:
1349 case AMDGPU::TTMP_320RegClassID:
1350 case AMDGPU::SGPR_352RegClassID:
1351 case AMDGPU::TTMP_352RegClassID:
1352 case AMDGPU::SGPR_384RegClassID:
1353 case AMDGPU::TTMP_384RegClassID:
1354 case AMDGPU::SGPR_512RegClassID:
1355 case AMDGPU::TTMP_512RegClassID:
1356 shift = 2;
1357 break;
1358 // ToDo: unclear if s[88:104] is available on VI. Can we use VCC as SGPR in
1359 // this bundle?
1360 default:
1361 llvm_unreachable("unhandled register class");
1362 }
1363
1364 if (Val % (1 << shift)) {
1365 *CommentStream << "Warning: " << getRegClassName(SRegClassID)
1366 << ": scalar reg isn't aligned " << Val;
1367 }
1368
1369 return createRegOperand(SRegClassID, Val >> shift);
1370}
1371
1373 bool IsHi) const {
1374 unsigned RegIdxInVGPR16 = RegIdx * 2 + (IsHi ? 1 : 0);
1375 return createRegOperand(AMDGPU::VGPR_16RegClassID, RegIdxInVGPR16);
1376}
1377
1378// Decode Literals for insts which always have a literal in the encoding
1381 if (HasLiteral) {
1382 assert(
1384 "Should only decode multiple kimm with VOPD, check VSrc operand types");
1385 if (Literal != Val)
1386 return errOperand(Val, "More than one unique literal is illegal");
1387 }
1388 HasLiteral = true;
1389 Literal = Val;
1390 return MCOperand::createImm(Literal);
1391}
1392
1394 // For now all literal constants are supposed to be unsigned integer
1395 // ToDo: deal with signed/unsigned 64-bit integer constants
1396 // ToDo: deal with float/double constants
1397 if (!HasLiteral) {
1398 if (Bytes.size() < 4) {
1399 return errOperand(0, "cannot read literal, inst bytes left " +
1400 Twine(Bytes.size()));
1401 }
1402 HasLiteral = true;
1403 Literal = Literal64 = eatBytes<uint32_t>(Bytes);
1404 if (ExtendFP64)
1405 Literal64 <<= 32;
1406 }
1407 return MCOperand::createImm(ExtendFP64 ? Literal64 : Literal);
1408}
1409
1411 using namespace AMDGPU::EncValues;
1412
1413 assert(Imm >= INLINE_INTEGER_C_MIN && Imm <= INLINE_INTEGER_C_MAX);
1414 return MCOperand::createImm((Imm <= INLINE_INTEGER_C_POSITIVE_MAX) ?
1415 (static_cast<int64_t>(Imm) - INLINE_INTEGER_C_MIN) :
1416 (INLINE_INTEGER_C_POSITIVE_MAX - static_cast<int64_t>(Imm)));
1417 // Cast prevents negative overflow.
1418}
1419
1420static int64_t getInlineImmVal32(unsigned Imm) {
1421 switch (Imm) {
1422 case 240:
1423 return llvm::bit_cast<uint32_t>(0.5f);
1424 case 241:
1425 return llvm::bit_cast<uint32_t>(-0.5f);
1426 case 242:
1427 return llvm::bit_cast<uint32_t>(1.0f);
1428 case 243:
1429 return llvm::bit_cast<uint32_t>(-1.0f);
1430 case 244:
1431 return llvm::bit_cast<uint32_t>(2.0f);
1432 case 245:
1433 return llvm::bit_cast<uint32_t>(-2.0f);
1434 case 246:
1435 return llvm::bit_cast<uint32_t>(4.0f);
1436 case 247:
1437 return llvm::bit_cast<uint32_t>(-4.0f);
1438 case 248: // 1 / (2 * PI)
1439 return 0x3e22f983;
1440 default:
1441 llvm_unreachable("invalid fp inline imm");
1442 }
1443}
1444
1445static int64_t getInlineImmVal64(unsigned Imm) {
1446 switch (Imm) {
1447 case 240:
1448 return llvm::bit_cast<uint64_t>(0.5);
1449 case 241:
1450 return llvm::bit_cast<uint64_t>(-0.5);
1451 case 242:
1452 return llvm::bit_cast<uint64_t>(1.0);
1453 case 243:
1454 return llvm::bit_cast<uint64_t>(-1.0);
1455 case 244:
1456 return llvm::bit_cast<uint64_t>(2.0);
1457 case 245:
1458 return llvm::bit_cast<uint64_t>(-2.0);
1459 case 246:
1460 return llvm::bit_cast<uint64_t>(4.0);
1461 case 247:
1462 return llvm::bit_cast<uint64_t>(-4.0);
1463 case 248: // 1 / (2 * PI)
1464 return 0x3fc45f306dc9c882;
1465 default:
1466 llvm_unreachable("invalid fp inline imm");
1467 }
1468}
1469
1470static int64_t getInlineImmValF16(unsigned Imm) {
1471 switch (Imm) {
1472 case 240:
1473 return 0x3800;
1474 case 241:
1475 return 0xB800;
1476 case 242:
1477 return 0x3C00;
1478 case 243:
1479 return 0xBC00;
1480 case 244:
1481 return 0x4000;
1482 case 245:
1483 return 0xC000;
1484 case 246:
1485 return 0x4400;
1486 case 247:
1487 return 0xC400;
1488 case 248: // 1 / (2 * PI)
1489 return 0x3118;
1490 default:
1491 llvm_unreachable("invalid fp inline imm");
1492 }
1493}
1494
1495static int64_t getInlineImmValBF16(unsigned Imm) {
1496 switch (Imm) {
1497 case 240:
1498 return 0x3F00;
1499 case 241:
1500 return 0xBF00;
1501 case 242:
1502 return 0x3F80;
1503 case 243:
1504 return 0xBF80;
1505 case 244:
1506 return 0x4000;
1507 case 245:
1508 return 0xC000;
1509 case 246:
1510 return 0x4080;
1511 case 247:
1512 return 0xC080;
1513 case 248: // 1 / (2 * PI)
1514 return 0x3E22;
1515 default:
1516 llvm_unreachable("invalid fp inline imm");
1517 }
1518}
1519
1520static int64_t getInlineImmVal16(unsigned Imm, AMDGPU::OperandSemantics Sema) {
1522 : getInlineImmValF16(Imm);
1523}
1524
1525MCOperand AMDGPUDisassembler::decodeFPImmed(unsigned ImmWidth, unsigned Imm,
1529
1530 // ToDo: case 248: 1/(2*PI) - is allowed only on VI
1531 // ImmWidth 0 is a default case where operand should not allow immediates.
1532 // Imm value is still decoded into 32 bit immediate operand, inst printer will
1533 // use it to print verbose error message.
1534 switch (ImmWidth) {
1535 case 0:
1536 case 32:
1538 case 64:
1540 case 16:
1541 return MCOperand::createImm(getInlineImmVal16(Imm, Sema));
1542 default:
1543 llvm_unreachable("implement me");
1544 }
1545}
1546
1548 using namespace AMDGPU;
1549
1550 assert(OPW_FIRST_ <= Width && Width < OPW_LAST_);
1551 switch (Width) {
1552 default: // fall
1553 case OPW32:
1554 case OPW16:
1555 case OPWV216:
1556 return VGPR_32RegClassID;
1557 case OPW64:
1558 case OPWV232: return VReg_64RegClassID;
1559 case OPW96: return VReg_96RegClassID;
1560 case OPW128: return VReg_128RegClassID;
1561 case OPW192: return VReg_192RegClassID;
1562 case OPW160: return VReg_160RegClassID;
1563 case OPW256: return VReg_256RegClassID;
1564 case OPW288: return VReg_288RegClassID;
1565 case OPW320: return VReg_320RegClassID;
1566 case OPW352: return VReg_352RegClassID;
1567 case OPW384: return VReg_384RegClassID;
1568 case OPW512: return VReg_512RegClassID;
1569 case OPW1024: return VReg_1024RegClassID;
1570 }
1571}
1572
1574 using namespace AMDGPU;
1575
1576 assert(OPW_FIRST_ <= Width && Width < OPW_LAST_);
1577 switch (Width) {
1578 default: // fall
1579 case OPW32:
1580 case OPW16:
1581 case OPWV216:
1582 return AGPR_32RegClassID;
1583 case OPW64:
1584 case OPWV232: return AReg_64RegClassID;
1585 case OPW96: return AReg_96RegClassID;
1586 case OPW128: return AReg_128RegClassID;
1587 case OPW160: return AReg_160RegClassID;
1588 case OPW256: return AReg_256RegClassID;
1589 case OPW288: return AReg_288RegClassID;
1590 case OPW320: return AReg_320RegClassID;
1591 case OPW352: return AReg_352RegClassID;
1592 case OPW384: return AReg_384RegClassID;
1593 case OPW512: return AReg_512RegClassID;
1594 case OPW1024: return AReg_1024RegClassID;
1595 }
1596}
1597
1598
1600 using namespace AMDGPU;
1601
1602 assert(OPW_FIRST_ <= Width && Width < OPW_LAST_);
1603 switch (Width) {
1604 default: // fall
1605 case OPW32:
1606 case OPW16:
1607 case OPWV216:
1608 return SGPR_32RegClassID;
1609 case OPW64:
1610 case OPWV232: return SGPR_64RegClassID;
1611 case OPW96: return SGPR_96RegClassID;
1612 case OPW128: return SGPR_128RegClassID;
1613 case OPW160: return SGPR_160RegClassID;
1614 case OPW256: return SGPR_256RegClassID;
1615 case OPW288: return SGPR_288RegClassID;
1616 case OPW320: return SGPR_320RegClassID;
1617 case OPW352: return SGPR_352RegClassID;
1618 case OPW384: return SGPR_384RegClassID;
1619 case OPW512: return SGPR_512RegClassID;
1620 }
1621}
1622
1624 using namespace AMDGPU;
1625
1626 assert(OPW_FIRST_ <= Width && Width < OPW_LAST_);
1627 switch (Width) {
1628 default: // fall
1629 case OPW32:
1630 case OPW16:
1631 case OPWV216:
1632 return TTMP_32RegClassID;
1633 case OPW64:
1634 case OPWV232: return TTMP_64RegClassID;
1635 case OPW128: return TTMP_128RegClassID;
1636 case OPW256: return TTMP_256RegClassID;
1637 case OPW288: return TTMP_288RegClassID;
1638 case OPW320: return TTMP_320RegClassID;
1639 case OPW352: return TTMP_352RegClassID;
1640 case OPW384: return TTMP_384RegClassID;
1641 case OPW512: return TTMP_512RegClassID;
1642 }
1643}
1644
1645int AMDGPUDisassembler::getTTmpIdx(unsigned Val) const {
1646 using namespace AMDGPU::EncValues;
1647
1648 unsigned TTmpMin = isGFX9Plus() ? TTMP_GFX9PLUS_MIN : TTMP_VI_MIN;
1649 unsigned TTmpMax = isGFX9Plus() ? TTMP_GFX9PLUS_MAX : TTMP_VI_MAX;
1650
1651 return (TTmpMin <= Val && Val <= TTmpMax)? Val - TTmpMin : -1;
1652}
1653
1655 bool MandatoryLiteral,
1656 unsigned ImmWidth,
1657 AMDGPU::OperandSemantics Sema) const {
1658 using namespace AMDGPU::EncValues;
1659
1660 assert(Val < 1024); // enum10
1661
1662 bool IsAGPR = Val & 512;
1663 Val &= 511;
1664
1665 if (VGPR_MIN <= Val && Val <= VGPR_MAX) {
1666 return createRegOperand(IsAGPR ? getAgprClassId(Width)
1667 : getVgprClassId(Width), Val - VGPR_MIN);
1668 }
1669 return decodeNonVGPRSrcOp(Width, Val & 0xFF, MandatoryLiteral, ImmWidth,
1670 Sema);
1671}
1672
1675 bool MandatoryLiteral, unsigned ImmWidth,
1676 AMDGPU::OperandSemantics Sema) const {
1677 // Cases when Val{8} is 1 (vgpr, agpr or true 16 vgpr) should have been
1678 // decoded earlier.
1679 assert(Val < (1 << 8) && "9-bit Src encoding when Val{8} is 0");
1680 using namespace AMDGPU::EncValues;
1681
1682 if (Val <= SGPR_MAX) {
1683 // "SGPR_MIN <= Val" is always true and causes compilation warning.
1684 static_assert(SGPR_MIN == 0);
1685 return createSRegOperand(getSgprClassId(Width), Val - SGPR_MIN);
1686 }
1687
1688 int TTmpIdx = getTTmpIdx(Val);
1689 if (TTmpIdx >= 0) {
1690 return createSRegOperand(getTtmpClassId(Width), TTmpIdx);
1691 }
1692
1693 if (INLINE_INTEGER_C_MIN <= Val && Val <= INLINE_INTEGER_C_MAX)
1694 return decodeIntImmed(Val);
1695
1696 if (INLINE_FLOATING_C_MIN <= Val && Val <= INLINE_FLOATING_C_MAX)
1697 return decodeFPImmed(ImmWidth, Val, Sema);
1698
1699 if (Val == LITERAL_CONST) {
1700 if (MandatoryLiteral)
1701 // Keep a sentinel value for deferred setting
1702 return MCOperand::createImm(LITERAL_CONST);
1704 }
1705
1706 switch (Width) {
1707 case OPW32:
1708 case OPW16:
1709 case OPWV216:
1710 return decodeSpecialReg32(Val);
1711 case OPW64:
1712 case OPWV232:
1713 return decodeSpecialReg64(Val);
1714 case OPW96:
1715 case OPW128:
1716 case OPW256:
1717 case OPW512:
1718 return decodeSpecialReg96Plus(Val);
1719 default:
1720 llvm_unreachable("unexpected immediate type");
1721 }
1722}
1723
1724// Bit 0 of DstY isn't stored in the instruction, because it's always the
1725// opposite of bit 0 of DstX.
1727 unsigned Val) const {
1728 int VDstXInd =
1729 AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::vdstX);
1730 assert(VDstXInd != -1);
1731 assert(Inst.getOperand(VDstXInd).isReg());
1732 unsigned XDstReg = MRI.getEncodingValue(Inst.getOperand(VDstXInd).getReg());
1733 Val |= ~XDstReg & 1;
1735 return createRegOperand(getVgprClassId(Width), Val);
1736}
1737
1739 using namespace AMDGPU;
1740
1741 switch (Val) {
1742 // clang-format off
1743 case 102: return createRegOperand(FLAT_SCR_LO);
1744 case 103: return createRegOperand(FLAT_SCR_HI);
1745 case 104: return createRegOperand(XNACK_MASK_LO);
1746 case 105: return createRegOperand(XNACK_MASK_HI);
1747 case 106: return createRegOperand(VCC_LO);
1748 case 107: return createRegOperand(VCC_HI);
1749 case 108: return createRegOperand(TBA_LO);
1750 case 109: return createRegOperand(TBA_HI);
1751 case 110: return createRegOperand(TMA_LO);
1752 case 111: return createRegOperand(TMA_HI);
1753 case 124:
1754 return isGFX11Plus() ? createRegOperand(SGPR_NULL) : createRegOperand(M0);
1755 case 125:
1756 return isGFX11Plus() ? createRegOperand(M0) : createRegOperand(SGPR_NULL);
1757 case 126: return createRegOperand(EXEC_LO);
1758 case 127: return createRegOperand(EXEC_HI);
1759 case 235: return createRegOperand(SRC_SHARED_BASE_LO);
1760 case 236: return createRegOperand(SRC_SHARED_LIMIT_LO);
1761 case 237: return createRegOperand(SRC_PRIVATE_BASE_LO);
1762 case 238: return createRegOperand(SRC_PRIVATE_LIMIT_LO);
1763 case 239: return createRegOperand(SRC_POPS_EXITING_WAVE_ID);
1764 case 251: return createRegOperand(SRC_VCCZ);
1765 case 252: return createRegOperand(SRC_EXECZ);
1766 case 253: return createRegOperand(SRC_SCC);
1767 case 254: return createRegOperand(LDS_DIRECT);
1768 default: break;
1769 // clang-format on
1770 }
1771 return errOperand(Val, "unknown operand encoding " + Twine(Val));
1772}
1773
1775 using namespace AMDGPU;
1776
1777 switch (Val) {
1778 case 102: return createRegOperand(FLAT_SCR);
1779 case 104: return createRegOperand(XNACK_MASK);
1780 case 106: return createRegOperand(VCC);
1781 case 108: return createRegOperand(TBA);
1782 case 110: return createRegOperand(TMA);
1783 case 124:
1784 if (isGFX11Plus())
1785 return createRegOperand(SGPR_NULL);
1786 break;
1787 case 125:
1788 if (!isGFX11Plus())
1789 return createRegOperand(SGPR_NULL);
1790 break;
1791 case 126: return createRegOperand(EXEC);
1792 case 235: return createRegOperand(SRC_SHARED_BASE);
1793 case 236: return createRegOperand(SRC_SHARED_LIMIT);
1794 case 237: return createRegOperand(SRC_PRIVATE_BASE);
1795 case 238: return createRegOperand(SRC_PRIVATE_LIMIT);
1796 case 239: return createRegOperand(SRC_POPS_EXITING_WAVE_ID);
1797 case 251: return createRegOperand(SRC_VCCZ);
1798 case 252: return createRegOperand(SRC_EXECZ);
1799 case 253: return createRegOperand(SRC_SCC);
1800 default: break;
1801 }
1802 return errOperand(Val, "unknown operand encoding " + Twine(Val));
1803}
1804
1806 using namespace AMDGPU;
1807
1808 switch (Val) {
1809 case 124:
1810 if (isGFX11Plus())
1811 return createRegOperand(SGPR_NULL);
1812 break;
1813 case 125:
1814 if (!isGFX11Plus())
1815 return createRegOperand(SGPR_NULL);
1816 break;
1817 default:
1818 break;
1819 }
1820 return errOperand(Val, "unknown operand encoding " + Twine(Val));
1821}
1822
1824AMDGPUDisassembler::decodeSDWASrc(const OpWidthTy Width, const unsigned Val,
1825 unsigned ImmWidth,
1826 AMDGPU::OperandSemantics Sema) const {
1827 using namespace AMDGPU::SDWA;
1828 using namespace AMDGPU::EncValues;
1829
1830 if (STI.hasFeature(AMDGPU::FeatureGFX9) ||
1831 STI.hasFeature(AMDGPU::FeatureGFX10)) {
1832 // XXX: cast to int is needed to avoid stupid warning:
1833 // compare with unsigned is always true
1834 if (int(SDWA9EncValues::SRC_VGPR_MIN) <= int(Val) &&
1835 Val <= SDWA9EncValues::SRC_VGPR_MAX) {
1836 return createRegOperand(getVgprClassId(Width),
1837 Val - SDWA9EncValues::SRC_VGPR_MIN);
1838 }
1839 if (SDWA9EncValues::SRC_SGPR_MIN <= Val &&
1840 Val <= (isGFX10Plus() ? SDWA9EncValues::SRC_SGPR_MAX_GFX10
1841 : SDWA9EncValues::SRC_SGPR_MAX_SI)) {
1842 return createSRegOperand(getSgprClassId(Width),
1843 Val - SDWA9EncValues::SRC_SGPR_MIN);
1844 }
1845 if (SDWA9EncValues::SRC_TTMP_MIN <= Val &&
1846 Val <= SDWA9EncValues::SRC_TTMP_MAX) {
1847 return createSRegOperand(getTtmpClassId(Width),
1848 Val - SDWA9EncValues::SRC_TTMP_MIN);
1849 }
1850
1851 const unsigned SVal = Val - SDWA9EncValues::SRC_SGPR_MIN;
1852
1853 if (INLINE_INTEGER_C_MIN <= SVal && SVal <= INLINE_INTEGER_C_MAX)
1854 return decodeIntImmed(SVal);
1855
1856 if (INLINE_FLOATING_C_MIN <= SVal && SVal <= INLINE_FLOATING_C_MAX)
1857 return decodeFPImmed(ImmWidth, SVal, Sema);
1858
1859 return decodeSpecialReg32(SVal);
1860 }
1861 if (STI.hasFeature(AMDGPU::FeatureVolcanicIslands))
1862 return createRegOperand(getVgprClassId(Width), Val);
1863 llvm_unreachable("unsupported target");
1864}
1865
1868}
1869
1872}
1873
1875 using namespace AMDGPU::SDWA;
1876
1877 assert((STI.hasFeature(AMDGPU::FeatureGFX9) ||
1878 STI.hasFeature(AMDGPU::FeatureGFX10)) &&
1879 "SDWAVopcDst should be present only on GFX9+");
1880
1881 bool IsWave32 = STI.hasFeature(AMDGPU::FeatureWavefrontSize32);
1882
1883 if (Val & SDWA9EncValues::VOPC_DST_VCC_MASK) {
1884 Val &= SDWA9EncValues::VOPC_DST_SGPR_MASK;
1885
1886 int TTmpIdx = getTTmpIdx(Val);
1887 if (TTmpIdx >= 0) {
1888 auto TTmpClsId = getTtmpClassId(IsWave32 ? OPW32 : OPW64);
1889 return createSRegOperand(TTmpClsId, TTmpIdx);
1890 }
1891 if (Val > SGPR_MAX) {
1892 return IsWave32 ? decodeSpecialReg32(Val) : decodeSpecialReg64(Val);
1893 }
1894 return createSRegOperand(getSgprClassId(IsWave32 ? OPW32 : OPW64), Val);
1895 }
1896 return createRegOperand(IsWave32 ? AMDGPU::VCC_LO : AMDGPU::VCC);
1897}
1898
1900 return STI.hasFeature(AMDGPU::FeatureWavefrontSize32)
1901 ? decodeSrcOp(OPW32, Val)
1902 : decodeSrcOp(OPW64, Val);
1903}
1904
1906 return decodeSrcOp(OPW32, Val);
1907}
1908
1911 return MCOperand();
1912 return MCOperand::createImm(Val);
1913}
1914
1916 using VersionField = AMDGPU::EncodingField<7, 0>;
1917 using W64Bit = AMDGPU::EncodingBit<13>;
1918 using W32Bit = AMDGPU::EncodingBit<14>;
1919 using MDPBit = AMDGPU::EncodingBit<15>;
1921
1922 auto [Version, W64, W32, MDP] = Encoding::decode(Imm);
1923
1924 // Decode into a plain immediate if any unused bits are raised.
1925 if (Encoding::encode(Version, W64, W32, MDP) != Imm)
1926 return MCOperand::createImm(Imm);
1927
1928 const auto &Versions = AMDGPU::UCVersion::getGFXVersions();
1929 const auto *I = find_if(
1930 Versions, [Version = Version](const AMDGPU::UCVersion::GFXVersion &V) {
1931 return V.Code == Version;
1932 });
1933 MCContext &Ctx = getContext();
1934 const MCExpr *E;
1935 if (I == Versions.end())
1937 else
1938 E = MCSymbolRefExpr::create(Ctx.getOrCreateSymbol(I->Symbol), Ctx);
1939
1940 if (W64)
1941 E = MCBinaryExpr::createOr(E, UCVersionW64Expr, Ctx);
1942 if (W32)
1943 E = MCBinaryExpr::createOr(E, UCVersionW32Expr, Ctx);
1944 if (MDP)
1945 E = MCBinaryExpr::createOr(E, UCVersionMDPExpr, Ctx);
1946
1947 return MCOperand::createExpr(E);
1948}
1949
1951 return STI.hasFeature(AMDGPU::FeatureVolcanicIslands);
1952}
1953
1955
1957 return STI.hasFeature(AMDGPU::FeatureGFX90AInsts);
1958}
1959
1961
1963
1965 return AMDGPU::isGFX10Plus(STI);
1966}
1967
1969 return STI.hasFeature(AMDGPU::FeatureGFX11);
1970}
1971
1973 return AMDGPU::isGFX11Plus(STI);
1974}
1975
1977 return STI.hasFeature(AMDGPU::FeatureGFX12);
1978}
1979
1981 return AMDGPU::isGFX12Plus(STI);
1982}
1983
1985 return STI.hasFeature(AMDGPU::FeatureArchitectedFlatScratch);
1986}
1987
1990}
1991
1992//===----------------------------------------------------------------------===//
1993// AMDGPU specific symbol handling
1994//===----------------------------------------------------------------------===//
1995
1996/// Print a string describing the reserved bit range specified by Mask with
1997/// offset BaseBytes for use in error comments. Mask is a single continuous
1998/// range of 1s surrounded by zeros. The format here is meant to align with the
1999/// tables that describe these bits in llvm.org/docs/AMDGPUUsage.html.
2000static SmallString<32> getBitRangeFromMask(uint32_t Mask, unsigned BaseBytes) {
2001 SmallString<32> Result;
2002 raw_svector_ostream S(Result);
2003
2004 int TrailingZeros = llvm::countr_zero(Mask);
2005 int PopCount = llvm::popcount(Mask);
2006
2007 if (PopCount == 1) {
2008 S << "bit (" << (TrailingZeros + BaseBytes * CHAR_BIT) << ')';
2009 } else {
2010 S << "bits in range ("
2011 << (TrailingZeros + PopCount - 1 + BaseBytes * CHAR_BIT) << ':'
2012 << (TrailingZeros + BaseBytes * CHAR_BIT) << ')';
2013 }
2014
2015 return Result;
2016}
2017
2018#define GET_FIELD(MASK) (AMDHSA_BITS_GET(FourByteBuffer, MASK))
2019#define PRINT_DIRECTIVE(DIRECTIVE, MASK) \
2020 do { \
2021 KdStream << Indent << DIRECTIVE " " << GET_FIELD(MASK) << '\n'; \
2022 } while (0)
2023#define PRINT_PSEUDO_DIRECTIVE_COMMENT(DIRECTIVE, MASK) \
2024 do { \
2025 KdStream << Indent << MAI.getCommentString() << ' ' << DIRECTIVE " " \
2026 << GET_FIELD(MASK) << '\n'; \
2027 } while (0)
2028
2029#define CHECK_RESERVED_BITS_IMPL(MASK, DESC, MSG) \
2030 do { \
2031 if (FourByteBuffer & (MASK)) { \
2032 return createStringError(std::errc::invalid_argument, \
2033 "kernel descriptor " DESC \
2034 " reserved %s set" MSG, \
2035 getBitRangeFromMask((MASK), 0).c_str()); \
2036 } \
2037 } while (0)
2038
2039#define CHECK_RESERVED_BITS(MASK) CHECK_RESERVED_BITS_IMPL(MASK, #MASK, "")
2040#define CHECK_RESERVED_BITS_MSG(MASK, MSG) \
2041 CHECK_RESERVED_BITS_IMPL(MASK, #MASK, ", " MSG)
2042#define CHECK_RESERVED_BITS_DESC(MASK, DESC) \
2043 CHECK_RESERVED_BITS_IMPL(MASK, DESC, "")
2044#define CHECK_RESERVED_BITS_DESC_MSG(MASK, DESC, MSG) \
2045 CHECK_RESERVED_BITS_IMPL(MASK, DESC, ", " MSG)
2046
2047// NOLINTNEXTLINE(readability-identifier-naming)
2049 uint32_t FourByteBuffer, raw_string_ostream &KdStream) const {
2050 using namespace amdhsa;
2051 StringRef Indent = "\t";
2052
2053 // We cannot accurately backward compute #VGPRs used from
2054 // GRANULATED_WORKITEM_VGPR_COUNT. But we are concerned with getting the same
2055 // value of GRANULATED_WORKITEM_VGPR_COUNT in the reassembled binary. So we
2056 // simply calculate the inverse of what the assembler does.
2057
2058 uint32_t GranulatedWorkitemVGPRCount =
2059 GET_FIELD(COMPUTE_PGM_RSRC1_GRANULATED_WORKITEM_VGPR_COUNT);
2060
2061 uint32_t NextFreeVGPR =
2062 (GranulatedWorkitemVGPRCount + 1) *
2063 AMDGPU::IsaInfo::getVGPREncodingGranule(&STI, EnableWavefrontSize32);
2064
2065 KdStream << Indent << ".amdhsa_next_free_vgpr " << NextFreeVGPR << '\n';
2066
2067 // We cannot backward compute values used to calculate
2068 // GRANULATED_WAVEFRONT_SGPR_COUNT. Hence the original values for following
2069 // directives can't be computed:
2070 // .amdhsa_reserve_vcc
2071 // .amdhsa_reserve_flat_scratch
2072 // .amdhsa_reserve_xnack_mask
2073 // They take their respective default values if not specified in the assembly.
2074 //
2075 // GRANULATED_WAVEFRONT_SGPR_COUNT
2076 // = f(NEXT_FREE_SGPR + VCC + FLAT_SCRATCH + XNACK_MASK)
2077 //
2078 // We compute the inverse as though all directives apart from NEXT_FREE_SGPR
2079 // are set to 0. So while disassembling we consider that:
2080 //
2081 // GRANULATED_WAVEFRONT_SGPR_COUNT
2082 // = f(NEXT_FREE_SGPR + 0 + 0 + 0)
2083 //
2084 // The disassembler cannot recover the original values of those 3 directives.
2085
2086 uint32_t GranulatedWavefrontSGPRCount =
2087 GET_FIELD(COMPUTE_PGM_RSRC1_GRANULATED_WAVEFRONT_SGPR_COUNT);
2088
2089 if (isGFX10Plus())
2090 CHECK_RESERVED_BITS_MSG(COMPUTE_PGM_RSRC1_GRANULATED_WAVEFRONT_SGPR_COUNT,
2091 "must be zero on gfx10+");
2092
2093 uint32_t NextFreeSGPR = (GranulatedWavefrontSGPRCount + 1) *
2095
2096 KdStream << Indent << ".amdhsa_reserve_vcc " << 0 << '\n';
2098 KdStream << Indent << ".amdhsa_reserve_flat_scratch " << 0 << '\n';
2099 KdStream << Indent << ".amdhsa_reserve_xnack_mask " << 0 << '\n';
2100 KdStream << Indent << ".amdhsa_next_free_sgpr " << NextFreeSGPR << "\n";
2101
2102 CHECK_RESERVED_BITS(COMPUTE_PGM_RSRC1_PRIORITY);
2103
2104 PRINT_DIRECTIVE(".amdhsa_float_round_mode_32",
2105 COMPUTE_PGM_RSRC1_FLOAT_ROUND_MODE_32);
2106 PRINT_DIRECTIVE(".amdhsa_float_round_mode_16_64",
2107 COMPUTE_PGM_RSRC1_FLOAT_ROUND_MODE_16_64);
2108 PRINT_DIRECTIVE(".amdhsa_float_denorm_mode_32",
2109 COMPUTE_PGM_RSRC1_FLOAT_DENORM_MODE_32);
2110 PRINT_DIRECTIVE(".amdhsa_float_denorm_mode_16_64",
2111 COMPUTE_PGM_RSRC1_FLOAT_DENORM_MODE_16_64);
2112
2113 CHECK_RESERVED_BITS(COMPUTE_PGM_RSRC1_PRIV);
2114
2115 if (!isGFX12Plus())
2116 PRINT_DIRECTIVE(".amdhsa_dx10_clamp",
2117 COMPUTE_PGM_RSRC1_GFX6_GFX11_ENABLE_DX10_CLAMP);
2118
2119 CHECK_RESERVED_BITS(COMPUTE_PGM_RSRC1_DEBUG_MODE);
2120
2121 if (!isGFX12Plus())
2122 PRINT_DIRECTIVE(".amdhsa_ieee_mode",
2123 COMPUTE_PGM_RSRC1_GFX6_GFX11_ENABLE_IEEE_MODE);
2124
2125 CHECK_RESERVED_BITS(COMPUTE_PGM_RSRC1_BULKY);
2126 CHECK_RESERVED_BITS(COMPUTE_PGM_RSRC1_CDBG_USER);
2127
2128 if (isGFX9Plus())
2129 PRINT_DIRECTIVE(".amdhsa_fp16_overflow", COMPUTE_PGM_RSRC1_GFX9_PLUS_FP16_OVFL);
2130
2131 if (!isGFX9Plus())
2132 CHECK_RESERVED_BITS_DESC_MSG(COMPUTE_PGM_RSRC1_GFX6_GFX8_RESERVED0,
2133 "COMPUTE_PGM_RSRC1", "must be zero pre-gfx9");
2134
2135 CHECK_RESERVED_BITS_DESC(COMPUTE_PGM_RSRC1_RESERVED1, "COMPUTE_PGM_RSRC1");
2136
2137 if (!isGFX10Plus())
2138 CHECK_RESERVED_BITS_DESC_MSG(COMPUTE_PGM_RSRC1_GFX6_GFX9_RESERVED2,
2139 "COMPUTE_PGM_RSRC1", "must be zero pre-gfx10");
2140
2141 if (isGFX10Plus()) {
2142 PRINT_DIRECTIVE(".amdhsa_workgroup_processor_mode",
2143 COMPUTE_PGM_RSRC1_GFX10_PLUS_WGP_MODE);
2144 PRINT_DIRECTIVE(".amdhsa_memory_ordered", COMPUTE_PGM_RSRC1_GFX10_PLUS_MEM_ORDERED);
2145 PRINT_DIRECTIVE(".amdhsa_forward_progress", COMPUTE_PGM_RSRC1_GFX10_PLUS_FWD_PROGRESS);
2146 }
2147
2148 if (isGFX12Plus())
2149 PRINT_DIRECTIVE(".amdhsa_round_robin_scheduling",
2150 COMPUTE_PGM_RSRC1_GFX12_PLUS_ENABLE_WG_RR_EN);
2151
2152 return true;
2153}
2154
2155// NOLINTNEXTLINE(readability-identifier-naming)
2157 uint32_t FourByteBuffer, raw_string_ostream &KdStream) const {
2158 using namespace amdhsa;
2159 StringRef Indent = "\t";
2161 PRINT_DIRECTIVE(".amdhsa_enable_private_segment",
2162 COMPUTE_PGM_RSRC2_ENABLE_PRIVATE_SEGMENT);
2163 else
2164 PRINT_DIRECTIVE(".amdhsa_system_sgpr_private_segment_wavefront_offset",
2165 COMPUTE_PGM_RSRC2_ENABLE_PRIVATE_SEGMENT);
2166 PRINT_DIRECTIVE(".amdhsa_system_sgpr_workgroup_id_x",
2167 COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_ID_X);
2168 PRINT_DIRECTIVE(".amdhsa_system_sgpr_workgroup_id_y",
2169 COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_ID_Y);
2170 PRINT_DIRECTIVE(".amdhsa_system_sgpr_workgroup_id_z",
2171 COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_ID_Z);
2172 PRINT_DIRECTIVE(".amdhsa_system_sgpr_workgroup_info",
2173 COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_INFO);
2174 PRINT_DIRECTIVE(".amdhsa_system_vgpr_workitem_id",
2175 COMPUTE_PGM_RSRC2_ENABLE_VGPR_WORKITEM_ID);
2176
2177 CHECK_RESERVED_BITS(COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_ADDRESS_WATCH);
2178 CHECK_RESERVED_BITS(COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_MEMORY);
2179 CHECK_RESERVED_BITS(COMPUTE_PGM_RSRC2_GRANULATED_LDS_SIZE);
2180
2182 ".amdhsa_exception_fp_ieee_invalid_op",
2183 COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_INVALID_OPERATION);
2184 PRINT_DIRECTIVE(".amdhsa_exception_fp_denorm_src",
2185 COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_FP_DENORMAL_SOURCE);
2187 ".amdhsa_exception_fp_ieee_div_zero",
2188 COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_DIVISION_BY_ZERO);
2189 PRINT_DIRECTIVE(".amdhsa_exception_fp_ieee_overflow",
2190 COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_OVERFLOW);
2191 PRINT_DIRECTIVE(".amdhsa_exception_fp_ieee_underflow",
2192 COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_UNDERFLOW);
2193 PRINT_DIRECTIVE(".amdhsa_exception_fp_ieee_inexact",
2194 COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_INEXACT);
2195 PRINT_DIRECTIVE(".amdhsa_exception_int_div_zero",
2196 COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_INT_DIVIDE_BY_ZERO);
2197
2198 CHECK_RESERVED_BITS_DESC(COMPUTE_PGM_RSRC2_RESERVED0, "COMPUTE_PGM_RSRC2");
2199
2200 return true;
2201}
2202
2203// NOLINTNEXTLINE(readability-identifier-naming)
2205 uint32_t FourByteBuffer, raw_string_ostream &KdStream) const {
2206 using namespace amdhsa;
2207 StringRef Indent = "\t";
2208 if (isGFX90A()) {
2209 KdStream << Indent << ".amdhsa_accum_offset "
2210 << (GET_FIELD(COMPUTE_PGM_RSRC3_GFX90A_ACCUM_OFFSET) + 1) * 4
2211 << '\n';
2212
2213 PRINT_DIRECTIVE(".amdhsa_tg_split", COMPUTE_PGM_RSRC3_GFX90A_TG_SPLIT);
2214
2215 CHECK_RESERVED_BITS_DESC_MSG(COMPUTE_PGM_RSRC3_GFX90A_RESERVED0,
2216 "COMPUTE_PGM_RSRC3", "must be zero on gfx90a");
2217 CHECK_RESERVED_BITS_DESC_MSG(COMPUTE_PGM_RSRC3_GFX90A_RESERVED1,
2218 "COMPUTE_PGM_RSRC3", "must be zero on gfx90a");
2219 } else if (isGFX10Plus()) {
2220 // Bits [0-3].
2221 if (!isGFX12Plus()) {
2222 if (!EnableWavefrontSize32 || !*EnableWavefrontSize32) {
2223 PRINT_DIRECTIVE(".amdhsa_shared_vgpr_count",
2224 COMPUTE_PGM_RSRC3_GFX10_GFX11_SHARED_VGPR_COUNT);
2225 } else {
2227 "SHARED_VGPR_COUNT",
2228 COMPUTE_PGM_RSRC3_GFX10_GFX11_SHARED_VGPR_COUNT);
2229 }
2230 } else {
2231 CHECK_RESERVED_BITS_DESC_MSG(COMPUTE_PGM_RSRC3_GFX12_PLUS_RESERVED0,
2232 "COMPUTE_PGM_RSRC3",
2233 "must be zero on gfx12+");
2234 }
2235
2236 // Bits [4-11].
2237 if (isGFX11()) {
2238 PRINT_DIRECTIVE(".amdhsa_inst_pref_size",
2239 COMPUTE_PGM_RSRC3_GFX11_INST_PREF_SIZE);
2240 PRINT_PSEUDO_DIRECTIVE_COMMENT("TRAP_ON_START",
2241 COMPUTE_PGM_RSRC3_GFX11_TRAP_ON_START);
2242 PRINT_PSEUDO_DIRECTIVE_COMMENT("TRAP_ON_END",
2243 COMPUTE_PGM_RSRC3_GFX11_TRAP_ON_END);
2244 } else if (isGFX12Plus()) {
2245 PRINT_DIRECTIVE(".amdhsa_inst_pref_size",
2246 COMPUTE_PGM_RSRC3_GFX12_PLUS_INST_PREF_SIZE);
2247 } else {
2248 CHECK_RESERVED_BITS_DESC_MSG(COMPUTE_PGM_RSRC3_GFX10_RESERVED1,
2249 "COMPUTE_PGM_RSRC3",
2250 "must be zero on gfx10");
2251 }
2252
2253 // Bits [12].
2254 CHECK_RESERVED_BITS_DESC_MSG(COMPUTE_PGM_RSRC3_GFX10_PLUS_RESERVED2,
2255 "COMPUTE_PGM_RSRC3", "must be zero on gfx10+");
2256
2257 // Bits [13].
2258 if (isGFX12Plus()) {
2260 COMPUTE_PGM_RSRC3_GFX12_PLUS_GLG_EN);
2261 } else {
2262 CHECK_RESERVED_BITS_DESC_MSG(COMPUTE_PGM_RSRC3_GFX10_GFX11_RESERVED3,
2263 "COMPUTE_PGM_RSRC3",
2264 "must be zero on gfx10 or gfx11");
2265 }
2266
2267 // Bits [14-30].
2268 CHECK_RESERVED_BITS_DESC_MSG(COMPUTE_PGM_RSRC3_GFX10_PLUS_RESERVED4,
2269 "COMPUTE_PGM_RSRC3", "must be zero on gfx10+");
2270
2271 // Bits [31].
2272 if (isGFX11Plus()) {
2274 COMPUTE_PGM_RSRC3_GFX11_PLUS_IMAGE_OP);
2275 } else {
2276 CHECK_RESERVED_BITS_DESC_MSG(COMPUTE_PGM_RSRC3_GFX10_RESERVED5,
2277 "COMPUTE_PGM_RSRC3",
2278 "must be zero on gfx10");
2279 }
2280 } else if (FourByteBuffer) {
2281 return createStringError(
2282 std::errc::invalid_argument,
2283 "kernel descriptor COMPUTE_PGM_RSRC3 must be all zero before gfx9");
2284 }
2285 return true;
2286}
2287#undef PRINT_PSEUDO_DIRECTIVE_COMMENT
2288#undef PRINT_DIRECTIVE
2289#undef GET_FIELD
2290#undef CHECK_RESERVED_BITS_IMPL
2291#undef CHECK_RESERVED_BITS
2292#undef CHECK_RESERVED_BITS_MSG
2293#undef CHECK_RESERVED_BITS_DESC
2294#undef CHECK_RESERVED_BITS_DESC_MSG
2295
2296/// Create an error object to return from onSymbolStart for reserved kernel
2297/// descriptor bits being set.
2298static Error createReservedKDBitsError(uint32_t Mask, unsigned BaseBytes,
2299 const char *Msg = "") {
2300 return createStringError(
2301 std::errc::invalid_argument, "kernel descriptor reserved %s set%s%s",
2302 getBitRangeFromMask(Mask, BaseBytes).c_str(), *Msg ? ", " : "", Msg);
2303}
2304
2305/// Create an error object to return from onSymbolStart for reserved kernel
2306/// descriptor bytes being set.
2307static Error createReservedKDBytesError(unsigned BaseInBytes,
2308 unsigned WidthInBytes) {
2309 // Create an error comment in the same format as the "Kernel Descriptor"
2310 // table here: https://llvm.org/docs/AMDGPUUsage.html#kernel-descriptor .
2311 return createStringError(
2312 std::errc::invalid_argument,
2313 "kernel descriptor reserved bits in range (%u:%u) set",
2314 (BaseInBytes + WidthInBytes) * CHAR_BIT - 1, BaseInBytes * CHAR_BIT);
2315}
2316
2319 raw_string_ostream &KdStream) const {
2320#define PRINT_DIRECTIVE(DIRECTIVE, MASK) \
2321 do { \
2322 KdStream << Indent << DIRECTIVE " " \
2323 << ((TwoByteBuffer & MASK) >> (MASK##_SHIFT)) << '\n'; \
2324 } while (0)
2325
2326 uint16_t TwoByteBuffer = 0;
2327 uint32_t FourByteBuffer = 0;
2328
2329 StringRef ReservedBytes;
2330 StringRef Indent = "\t";
2331
2332 assert(Bytes.size() == 64);
2333 DataExtractor DE(Bytes, /*IsLittleEndian=*/true, /*AddressSize=*/8);
2334
2335 switch (Cursor.tell()) {
2337 FourByteBuffer = DE.getU32(Cursor);
2338 KdStream << Indent << ".amdhsa_group_segment_fixed_size " << FourByteBuffer
2339 << '\n';
2340 return true;
2341
2343 FourByteBuffer = DE.getU32(Cursor);
2344 KdStream << Indent << ".amdhsa_private_segment_fixed_size "
2345 << FourByteBuffer << '\n';
2346 return true;
2347
2349 FourByteBuffer = DE.getU32(Cursor);
2350 KdStream << Indent << ".amdhsa_kernarg_size "
2351 << FourByteBuffer << '\n';
2352 return true;
2353
2355 // 4 reserved bytes, must be 0.
2356 ReservedBytes = DE.getBytes(Cursor, 4);
2357 for (int I = 0; I < 4; ++I) {
2358 if (ReservedBytes[I] != 0)
2360 }
2361 return true;
2362
2364 // KERNEL_CODE_ENTRY_BYTE_OFFSET
2365 // So far no directive controls this for Code Object V3, so simply skip for
2366 // disassembly.
2367 DE.skip(Cursor, 8);
2368 return true;
2369
2371 // 20 reserved bytes, must be 0.
2372 ReservedBytes = DE.getBytes(Cursor, 20);
2373 for (int I = 0; I < 20; ++I) {
2374 if (ReservedBytes[I] != 0)
2376 }
2377 return true;
2378
2380 FourByteBuffer = DE.getU32(Cursor);
2381 return decodeCOMPUTE_PGM_RSRC3(FourByteBuffer, KdStream);
2382
2384 FourByteBuffer = DE.getU32(Cursor);
2385 return decodeCOMPUTE_PGM_RSRC1(FourByteBuffer, KdStream);
2386
2388 FourByteBuffer = DE.getU32(Cursor);
2389 return decodeCOMPUTE_PGM_RSRC2(FourByteBuffer, KdStream);
2390
2392 using namespace amdhsa;
2393 TwoByteBuffer = DE.getU16(Cursor);
2394
2396 PRINT_DIRECTIVE(".amdhsa_user_sgpr_private_segment_buffer",
2397 KERNEL_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER);
2398 PRINT_DIRECTIVE(".amdhsa_user_sgpr_dispatch_ptr",
2399 KERNEL_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR);
2400 PRINT_DIRECTIVE(".amdhsa_user_sgpr_queue_ptr",
2401 KERNEL_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR);
2402 PRINT_DIRECTIVE(".amdhsa_user_sgpr_kernarg_segment_ptr",
2403 KERNEL_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR);
2404 PRINT_DIRECTIVE(".amdhsa_user_sgpr_dispatch_id",
2405 KERNEL_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID);
2407 PRINT_DIRECTIVE(".amdhsa_user_sgpr_flat_scratch_init",
2408 KERNEL_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT);
2409 PRINT_DIRECTIVE(".amdhsa_user_sgpr_private_segment_size",
2410 KERNEL_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_SIZE);
2411
2412 if (TwoByteBuffer & KERNEL_CODE_PROPERTY_RESERVED0)
2413 return createReservedKDBitsError(KERNEL_CODE_PROPERTY_RESERVED0,
2415
2416 // Reserved for GFX9
2417 if (isGFX9() &&
2418 (TwoByteBuffer & KERNEL_CODE_PROPERTY_ENABLE_WAVEFRONT_SIZE32)) {
2420 KERNEL_CODE_PROPERTY_ENABLE_WAVEFRONT_SIZE32,
2421 amdhsa::KERNEL_CODE_PROPERTIES_OFFSET, "must be zero on gfx9");
2422 }
2423 if (isGFX10Plus()) {
2424 PRINT_DIRECTIVE(".amdhsa_wavefront_size32",
2425 KERNEL_CODE_PROPERTY_ENABLE_WAVEFRONT_SIZE32);
2426 }
2427
2428 if (CodeObjectVersion >= AMDGPU::AMDHSA_COV5)
2429 PRINT_DIRECTIVE(".amdhsa_uses_dynamic_stack",
2430 KERNEL_CODE_PROPERTY_USES_DYNAMIC_STACK);
2431
2432 if (TwoByteBuffer & KERNEL_CODE_PROPERTY_RESERVED1) {
2433 return createReservedKDBitsError(KERNEL_CODE_PROPERTY_RESERVED1,
2435 }
2436
2437 return true;
2438
2440 using namespace amdhsa;
2441 TwoByteBuffer = DE.getU16(Cursor);
2442 if (TwoByteBuffer & KERNARG_PRELOAD_SPEC_LENGTH) {
2443 PRINT_DIRECTIVE(".amdhsa_user_sgpr_kernarg_preload_length",
2444 KERNARG_PRELOAD_SPEC_LENGTH);
2445 }
2446
2447 if (TwoByteBuffer & KERNARG_PRELOAD_SPEC_OFFSET) {
2448 PRINT_DIRECTIVE(".amdhsa_user_sgpr_kernarg_preload_offset",
2449 KERNARG_PRELOAD_SPEC_OFFSET);
2450 }
2451 return true;
2452
2454 // 4 bytes from here are reserved, must be 0.
2455 ReservedBytes = DE.getBytes(Cursor, 4);
2456 for (int I = 0; I < 4; ++I) {
2457 if (ReservedBytes[I] != 0)
2459 }
2460 return true;
2461
2462 default:
2463 llvm_unreachable("Unhandled index. Case statements cover everything.");
2464 return true;
2465 }
2466#undef PRINT_DIRECTIVE
2467}
2468
2470 StringRef KdName, ArrayRef<uint8_t> Bytes, uint64_t KdAddress) const {
2471
2472 // CP microcode requires the kernel descriptor to be 64 aligned.
2473 if (Bytes.size() != 64 || KdAddress % 64 != 0)
2474 return createStringError(std::errc::invalid_argument,
2475 "kernel descriptor must be 64-byte aligned");
2476
2477 // FIXME: We can't actually decode "in order" as is done below, as e.g. GFX10
2478 // requires us to know the setting of .amdhsa_wavefront_size32 in order to
2479 // accurately produce .amdhsa_next_free_vgpr, and they appear in the wrong
2480 // order. Workaround this by first looking up .amdhsa_wavefront_size32 here
2481 // when required.
2482 if (isGFX10Plus()) {
2483 uint16_t KernelCodeProperties =
2486 EnableWavefrontSize32 =
2487 AMDHSA_BITS_GET(KernelCodeProperties,
2488 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_WAVEFRONT_SIZE32);
2489 }
2490
2491 std::string Kd;
2492 raw_string_ostream KdStream(Kd);
2493 KdStream << ".amdhsa_kernel " << KdName << '\n';
2494
2496 while (C && C.tell() < Bytes.size()) {
2497 Expected<bool> Res = decodeKernelDescriptorDirective(C, Bytes, KdStream);
2498
2499 cantFail(C.takeError());
2500
2501 if (!Res)
2502 return Res;
2503 }
2504 KdStream << ".end_amdhsa_kernel\n";
2505 outs() << KdStream.str();
2506 return true;
2507}
2508
2510 uint64_t &Size,
2511 ArrayRef<uint8_t> Bytes,
2512 uint64_t Address) const {
2513 // Right now only kernel descriptor needs to be handled.
2514 // We ignore all other symbols for target specific handling.
2515 // TODO:
2516 // Fix the spurious symbol issue for AMDGPU kernels. Exists for both Code
2517 // Object V2 and V3 when symbols are marked protected.
2518
2519 // amd_kernel_code_t for Code Object V2.
2520 if (Symbol.Type == ELF::STT_AMDGPU_HSA_KERNEL) {
2521 Size = 256;
2522 return createStringError(std::errc::invalid_argument,
2523 "code object v2 is not supported");
2524 }
2525
2526 // Code Object V3 kernel descriptors.
2527 StringRef Name = Symbol.Name;
2528 if (Symbol.Type == ELF::STT_OBJECT && Name.ends_with(StringRef(".kd"))) {
2529 Size = 64; // Size = 64 regardless of success or failure.
2530 return decodeKernelDescriptor(Name.drop_back(3), Bytes, Address);
2531 }
2532
2533 return false;
2534}
2535
2536const MCExpr *AMDGPUDisassembler::createConstantSymbolExpr(StringRef Id,
2537 int64_t Val) {
2538 MCContext &Ctx = getContext();
2539 MCSymbol *Sym = Ctx.getOrCreateSymbol(Id);
2540 // Note: only set value to Val on a new symbol in case an dissassembler
2541 // has already been initialized in this context.
2542 if (!Sym->isVariable()) {
2543 Sym->setVariableValue(MCConstantExpr::create(Val, Ctx));
2544 } else {
2545 int64_t Res = ~Val;
2546 bool Valid = Sym->getVariableValue()->evaluateAsAbsolute(Res);
2547 if (!Valid || Res != Val)
2548 Ctx.reportWarning(SMLoc(), "unsupported redefinition of " + Id);
2549 }
2550 return MCSymbolRefExpr::create(Sym, Ctx);
2551}
2552
2553//===----------------------------------------------------------------------===//
2554// AMDGPUSymbolizer
2555//===----------------------------------------------------------------------===//
2556
2557// Try to find symbol name for specified label
2559 MCInst &Inst, raw_ostream & /*cStream*/, int64_t Value,
2560 uint64_t /*Address*/, bool IsBranch, uint64_t /*Offset*/,
2561 uint64_t /*OpSize*/, uint64_t /*InstSize*/) {
2562
2563 if (!IsBranch) {
2564 return false;
2565 }
2566
2567 auto *Symbols = static_cast<SectionSymbolsTy *>(DisInfo);
2568 if (!Symbols)
2569 return false;
2570
2571 auto Result = llvm::find_if(*Symbols, [Value](const SymbolInfoTy &Val) {
2572 return Val.Addr == static_cast<uint64_t>(Value) &&
2573 Val.Type == ELF::STT_NOTYPE;
2574 });
2575 if (Result != Symbols->end()) {
2576 auto *Sym = Ctx.getOrCreateSymbol(Result->Name);
2577 const auto *Add = MCSymbolRefExpr::create(Sym, Ctx);
2579 return true;
2580 }
2581 // Add to list of referenced addresses, so caller can synthesize a label.
2582 ReferencedAddresses.push_back(static_cast<uint64_t>(Value));
2583 return false;
2584}
2585
2587 int64_t Value,
2588 uint64_t Address) {
2589 llvm_unreachable("unimplemented");
2590}
2591
2592//===----------------------------------------------------------------------===//
2593// Initialization
2594//===----------------------------------------------------------------------===//
2595
2597 LLVMOpInfoCallback /*GetOpInfo*/,
2598 LLVMSymbolLookupCallback /*SymbolLookUp*/,
2599 void *DisInfo,
2600 MCContext *Ctx,
2601 std::unique_ptr<MCRelocationInfo> &&RelInfo) {
2602 return new AMDGPUSymbolizer(*Ctx, std::move(RelInfo), DisInfo);
2603}
2604
2606 const MCSubtargetInfo &STI,
2607 MCContext &Ctx) {
2608 return new AMDGPUDisassembler(STI, Ctx, T.createMCInstrInfo());
2609}
2610
2616}
unsigned const MachineRegisterInfo * MRI
aarch64 promote const
static int IsAGPROperand(const MCInst &Inst, AMDGPU::OpName Name, const MCRegisterInfo *MRI)
#define CHECK_RESERVED_BITS_DESC(MASK, DESC)
static VOPModifiers collectVOPModifiers(const MCInst &MI, bool IsVOP3P=false)
static DecodeStatus decodeSrcRegOrImm9(MCInst &Inst, unsigned Imm, uint64_t, const MCDisassembler *Decoder)
static int insertNamedMCOperand(MCInst &MI, const MCOperand &Op, AMDGPU::OpName Name)
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 decodeSMEMOffset(MCInst &Inst, unsigned Imm, uint64_t Addr, const MCDisassembler *Decoder)
static DecodeStatus decodeVersionImm(MCInst &Inst, unsigned Imm, uint64_t, const MCDisassembler *Decoder)
static DecodeStatus decodeSrcA9(MCInst &Inst, unsigned Imm, uint64_t, const MCDisassembler *Decoder)
static DecodeStatus decodeOperand_VGPR_16(MCInst &Inst, unsigned Imm, uint64_t, const MCDisassembler *Decoder)
static DecoderUInt128 eat12Bytes(ArrayRef< uint8_t > &Bytes)
#define PRINT_PSEUDO_DIRECTIVE_COMMENT(DIRECTIVE, MASK)
static DecodeStatus decodeSrcRegOrImmDeferred9(MCInst &Inst, unsigned Imm, uint64_t, const MCDisassembler *Decoder)
static DecodeStatus decodeDpp8FI(MCInst &Inst, unsigned Val, uint64_t Addr, const MCDisassembler *Decoder)
static DecodeStatus decodeOperand_VSrcT16_Lo128(MCInst &Inst, unsigned Imm, uint64_t, const MCDisassembler *Decoder)
static DecodeStatus decodeOperand_VSrc_f64(MCInst &Inst, unsigned Imm, uint64_t Addr, const MCDisassembler *Decoder)
static int64_t getInlineImmVal16(unsigned Imm, AMDGPU::OperandSemantics Sema)
static int64_t getInlineImmValBF16(unsigned Imm)
#define DECODE_SDWA(DecName)
static DecodeStatus decodeSOPPBrTarget(MCInst &Inst, unsigned Imm, uint64_t Addr, const MCDisassembler *Decoder)
static DecodeStatus decodeSrcRegOrImmA9(MCInst &Inst, unsigned Imm, uint64_t, const MCDisassembler *Decoder)
#define DECODE_OPERAND_REG_8(RegClass)
static DecodeStatus decodeSrcOp(MCInst &Inst, unsigned EncSize, AMDGPUDisassembler::OpWidthTy OpWidth, unsigned Imm, unsigned EncImm, bool MandatoryLiteral, unsigned ImmWidth, AMDGPU::OperandSemantics Sema, const MCDisassembler *Decoder)
#define PRINT_DIRECTIVE(DIRECTIVE, MASK)
static DecoderUInt128 eat16Bytes(ArrayRef< uint8_t > &Bytes)
static DecodeStatus decodeOperand_VSrcT16_Lo128_Deferred(MCInst &Inst, unsigned Imm, uint64_t, const MCDisassembler *Decoder)
static DecodeStatus DecodeVGPR_16RegisterClass(MCInst &Inst, unsigned Imm, uint64_t, const MCDisassembler *Decoder)
static DecodeStatus decodeSrcReg9(MCInst &Inst, unsigned Imm, uint64_t, const MCDisassembler *Decoder)
static int64_t getInlineImmVal32(unsigned Imm)
#define DECODE_OPERAND_REG_7(RegClass, OpWidth)
static MCDisassembler::DecodeStatus addOperand(MCInst &Inst, const MCOperand &Opnd)
#define CHECK_RESERVED_BITS(MASK)
static DecodeStatus decodeSrcAV10(MCInst &Inst, unsigned Imm, uint64_t, const MCDisassembler *Decoder)
#define SGPR_MAX
static int64_t getInlineImmVal64(unsigned Imm)
static T eatBytes(ArrayRef< uint8_t > &Bytes)
static DecodeStatus decodeOperand_KImmFP(MCInst &Inst, unsigned Imm, uint64_t Addr, const MCDisassembler *Decoder)
static DecodeStatus decodeOperand_VSrcT16(MCInst &Inst, unsigned Imm, uint64_t, const MCDisassembler *Decoder)
static MCDisassembler * createAMDGPUDisassembler(const Target &T, const MCSubtargetInfo &STI, MCContext &Ctx)
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAMDGPUDisassembler()
static DecodeStatus decodeAVLdSt(MCInst &Inst, unsigned Imm, AMDGPUDisassembler::OpWidthTy Opw, const MCDisassembler *Decoder)
static DecodeStatus DecodeVGPR_16_Lo128RegisterClass(MCInst &Inst, unsigned Imm, uint64_t, const MCDisassembler *Decoder)
#define CHECK_RESERVED_BITS_MSG(MASK, MSG)
static DecodeStatus decodeOperandVOPDDstY(MCInst &Inst, unsigned Val, uint64_t Addr, const void *Decoder)
static MCSymbolizer * createAMDGPUSymbolizer(const Triple &, LLVMOpInfoCallback, LLVMSymbolLookupCallback, void *DisInfo, MCContext *Ctx, std::unique_ptr< MCRelocationInfo > &&RelInfo)
static DecodeStatus decodeBoolReg(MCInst &Inst, unsigned Val, uint64_t Addr, const MCDisassembler *Decoder)
static int64_t getInlineImmValF16(unsigned Imm)
#define GET_FIELD(MASK)
static Error createReservedKDBytesError(unsigned BaseInBytes, unsigned WidthInBytes)
Create an error object to return from onSymbolStart for reserved kernel descriptor bytes being set.
static DecodeStatus decodeSplitBarrier(MCInst &Inst, unsigned Val, uint64_t Addr, const MCDisassembler *Decoder)
static DecodeStatus decodeAV10(MCInst &Inst, unsigned 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.
static void 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...
This file contains declaration for AMDGPU ISA disassembler.
Provides AMDGPU specific target descriptions.
AMDHSA kernel descriptor definitions.
#define AMDHSA_BITS_GET(SRC, MSK)
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:128
uint64_t Addr
std::string Name
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Interface definition for SIRegisterInfo.
void convertVOPC64DPPInst(MCInst &MI) const
void convertEXPInst(MCInst &MI) const
MCOperand createRegOperand(unsigned int RegId) 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.
Expected< bool > decodeKernelDescriptorDirective(DataExtractor::Cursor &Cursor, ArrayRef< uint8_t > Bytes, raw_string_ostream &KdStream) const
void convertVOPCDPPInst(MCInst &MI) const
unsigned getVgprClassId(const OpWidthTy Width) const
unsigned getAgprClassId(const OpWidthTy Width) 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.
MCOperand decodeDpp8FI(unsigned Val) const
void convertMacDPPInst(MCInst &MI) const
MCOperand decodeVOPDDstYOp(MCInst &Inst, unsigned Val) const
MCOperand decodeBoolReg(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
MCOperand decodeSDWASrc(const OpWidthTy Width, unsigned Val, unsigned ImmWidth, AMDGPU::OperandSemantics Sema) const
Expected< bool > decodeKernelDescriptor(StringRef KdName, ArrayRef< uint8_t > Bytes, uint64_t KdAddress) const
MCOperand decodeSplitBarrier(unsigned Val) const
void convertVOP3DPPInst(MCInst &MI) const
void convertTrue16OpSel(MCInst &MI) const
void convertFMAanyK(MCInst &MI, int ImmLitIdx) const
MCOperand decodeMandatoryLiteralConstant(unsigned Imm) const
MCOperand decodeNonVGPRSrcOp(const OpWidthTy Width, unsigned Val, bool MandatoryLiteral=false, unsigned ImmWidth=0, AMDGPU::OperandSemantics Sema=AMDGPU::OperandSemantics::INT) const
Expected< bool > decodeCOMPUTE_PGM_RSRC3(uint32_t FourByteBuffer, raw_string_ostream &KdStream) const
Decode as directives that handle COMPUTE_PGM_RSRC3.
static MCOperand decodeFPImmed(unsigned ImmWidth, unsigned Imm, AMDGPU::OperandSemantics Sema)
MCOperand decodeSrcOp(const OpWidthTy Width, unsigned Val, bool MandatoryLiteral=false, unsigned ImmWidth=0, AMDGPU::OperandSemantics Sema=AMDGPU::OperandSemantics::INT) const
AMDGPUDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx, MCInstrInfo const *MCII)
MCOperand decodeSpecialReg32(unsigned Val) const
MCOperand decodeLiteralConstant(bool ExtendFP64) const
MCOperand decodeSDWAVopcDst(unsigned Val) const
void convertVINTERPInst(MCInst &MI) const
void convertSDWAInst(MCInst &MI) const
DecodeStatus tryDecodeInst(const uint8_t *Table, MCInst &MI, InsnType Inst, uint64_t Address, raw_ostream &Comments) const
unsigned getSgprClassId(const OpWidthTy Width) const
static MCOperand decodeIntImmed(unsigned Imm)
void convertMAIInst(MCInst &MI) const
f8f6f4 instructions have different pseudos depending on the used formats.
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.
unsigned getTtmpClassId(const OpWidthTy Width) const
void convertMIMGInst(MCInst &MI) const
bool isMacDPP(MCInst &MI) const
int getTTmpIdx(unsigned Val) const
void convertVOP3PDPPInst(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.
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.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:168
const T * data() const
Definition: ArrayRef.h:165
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:198
This class represents an Operation in the Expression.
A class representing a position in a DataExtractor, as well as any error encountered during extractio...
Definition: DataExtractor.h:54
uint64_t tell() const
Return the current position of this Cursor.
Definition: DataExtractor.h:71
uint32_t getU32(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint32_t value from *offset_ptr.
uint16_t getU16(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint16_t value from *offset_ptr.
void skip(Cursor &C, uint64_t Length) const
Advance the Cursor position by the given number of bytes.
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:160
Tagged union holding either a T or a Error.
Definition: Error.h:481
static const MCBinaryExpr * createOr(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:602
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:222
Context object for machine code objects.
Definition: MCContext.h:83
const MCRegisterInfo * getRegisterInfo() const
Definition: MCContext.h:414
void reportWarning(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:1080
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:212
Superclass for all disassemblers.
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:185
unsigned getNumOperands() const
Definition: MCInst.h:209
unsigned getOpcode() const
Definition: MCInst.h:199
void addOperand(const MCOperand Op)
Definition: MCInst.h:211
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:207
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:198
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:26
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:37
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:163
int64_t getImm() const
Definition: MCInst.h:81
static MCOperand createReg(MCRegister Reg)
Definition: MCInst.h:135
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:142
void setReg(MCRegister Reg)
Set the register number.
Definition: MCInst.h:76
bool isReg() const
Definition: MCInst.h:62
MCRegister getReg() const
Returns the register number.
Definition: MCInst.h:70
bool isValid() const
Definition: MCInst.h:61
MCRegisterClass - Base class of TargetRegisterClass.
unsigned getRegister(unsigned i) const
getRegister - Return the specified register in the class.
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...
MCRegister getMatchingSuperReg(MCRegister Reg, unsigned SubIdx, const MCRegisterClass *RC) const
Return a super-register of the specified register Reg so its sub-register of index SubIdx is Reg.
uint16_t getEncodingValue(MCRegister Reg) const
Returns the encoding for Reg.
const MCRegisterClass & getRegClass(unsigned i) const
Returns the register class associated with the enumeration value.
MCRegister getSubReg(MCRegister Reg, unsigned Idx) const
Returns the physical register number of sub-register "Index" for physical register RegNo.
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:33
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:398
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
Symbolize and annotate disassembled instructions.
Definition: MCSymbolizer.h:39
MCContext & Ctx
Definition: MCSymbolizer.h:41
Represents a location in source code.
Definition: SMLoc.h:23
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
LLVM Value Representation.
Definition: Value.h:74
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
std::string & str()
Returns the string's reference.
Definition: raw_ostream.h:679
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:691
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()
LLVM_READONLY const MIMGInfo * getMIMGInfo(unsigned Opc)
MCRegister getMCReg(MCRegister Reg, const MCSubtargetInfo &STI)
If Reg is a pseudo reg, return the correct hardware register given STI otherwise return Reg.
int getMIMGOpcode(unsigned BaseOpcode, unsigned MIMGEncoding, unsigned VDataDwords, unsigned VAddrDwords)
bool isGFX10(const MCSubtargetInfo &STI)
bool isGFX12Plus(const MCSubtargetInfo &STI)
bool hasPackedD16(const MCSubtargetInfo &STI)
bool isVOPC64DPP(unsigned Opc)
unsigned getAMDHSACodeObjectVersion(const Module &M)
LLVM_READONLY bool hasNamedOperand(uint64_t Opcode, OpName NamedIdx)
bool isGFX9(const MCSubtargetInfo &STI)
LLVM_READONLY const MIMGDimInfo * getMIMGDimInfoByEncoding(uint8_t DimEnc)
bool hasG16(const MCSubtargetInfo &STI)
unsigned getAddrSizeMIMGOp(const MIMGBaseOpcodeInfo *BaseOpcode, const MIMGDimInfo *Dim, bool IsA16, bool IsG16Supported)
bool isGFX11Plus(const MCSubtargetInfo &STI)
bool isGFX10Plus(const MCSubtargetInfo &STI)
@ OPERAND_REG_IMM_FP32_DEFERRED
Definition: SIDefines.h:209
@ OPERAND_REG_IMM_FP16_DEFERRED
Definition: SIDefines.h:208
bool hasGDS(const MCSubtargetInfo &STI)
bool isGFX9Plus(const MCSubtargetInfo &STI)
unsigned hasKernargPreload(const MCSubtargetInfo &STI)
bool isMAC(unsigned Opc)
LLVM_READONLY const MIMGBaseOpcodeInfo * getMIMGBaseOpcodeInfo(unsigned BaseOpcode)
bool hasVOPD(const MCSubtargetInfo &STI)
const MFMA_F8F6F4_Info * getMFMA_F8F6F4_WithFormatArgs(unsigned CBSZ, unsigned BLGP, unsigned F8F8Opcode)
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ STT_NOTYPE
Definition: ELF.h:1373
@ STT_AMDGPU_HSA_KERNEL
Definition: ELF.h:1387
@ STT_OBJECT
Definition: ELF.h:1374
uint16_t read16(const void *P, endianness E)
Definition: Endian.h:402
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition: bit.h:385
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:1291
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:215
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:756
Target & getTheGCNTarget()
The target for GCN GPUs.
@ Add
Sum of integers.
std::vector< SymbolInfoTy > SectionSymbolsTy
unsigned M0(unsigned Val)
Definition: VE.h:375
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:1766
Description of the encoding of one expression Op.
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.