LLVM 24.0.0git
AMDGPUMCCodeEmitter.cpp
Go to the documentation of this file.
1//===-- AMDGPUMCCodeEmitter.cpp - AMDGPU Code Emitter ---------------------===//
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/// \file
10/// The AMDGPU code emitter produces machine code that can be executed
11/// directly on the GPU device.
12//
13//===----------------------------------------------------------------------===//
14
18#include "SIDefines.h"
20#include "llvm/ADT/APInt.h"
22#include "llvm/MC/MCContext.h"
23#include "llvm/MC/MCExpr.h"
24#include "llvm/MC/MCInstrInfo.h"
29#include <optional>
30
31using namespace llvm;
32
33namespace {
34
35class AMDGPUMCCodeEmitter : public MCCodeEmitter {
36 const MCRegisterInfo &MRI;
37 const MCInstrInfo &MCII;
38
39public:
40 AMDGPUMCCodeEmitter(const MCInstrInfo &MCII, const MCRegisterInfo &MRI)
41 : MRI(MRI), MCII(MCII) {}
42
43 /// Encode the instruction and write it to the OS.
44 void encodeInstruction(const MCInst &MI, SmallVectorImpl<char> &CB,
45 SmallVectorImpl<MCFixup> &Fixups,
46 const MCSubtargetInfo &STI) const override;
47
48 void getMachineOpValue(const MCInst &MI, const MCOperand &MO, APInt &Op,
49 SmallVectorImpl<MCFixup> &Fixups,
50 const MCSubtargetInfo &STI) const;
51
52 void getMachineOpValueT16(const MCInst &MI, unsigned OpNo, APInt &Op,
53 SmallVectorImpl<MCFixup> &Fixups,
54 const MCSubtargetInfo &STI) const;
55
56 void getMachineOpValueT16Lo128(const MCInst &MI, unsigned OpNo, APInt &Op,
57 SmallVectorImpl<MCFixup> &Fixups,
58 const MCSubtargetInfo &STI) const;
59
60 void getMachineOpValueRsrcRegOp(const MCInst &MI, unsigned OpNo, APInt &Op,
61 SmallVectorImpl<MCFixup> &Fixups,
62 const MCSubtargetInfo &STI) const;
63
64 /// Use a fixup to encode the simm16 field for SOPP branch
65 /// instructions.
66 void getSOPPBrEncoding(const MCInst &MI, unsigned OpNo, APInt &Op,
67 SmallVectorImpl<MCFixup> &Fixups,
68 const MCSubtargetInfo &STI) const;
69
70 void getSMEMOffsetEncoding(const MCInst &MI, unsigned OpNo, APInt &Op,
71 SmallVectorImpl<MCFixup> &Fixups,
72 const MCSubtargetInfo &STI) const;
73
74 /// 24-bit PC-relative byte offset for s_prefetch_inst_pc_rel.
75 void getSMEMOffsetPCRelEncoding(const MCInst &MI, unsigned OpNo, APInt &Op,
76 SmallVectorImpl<MCFixup> &Fixups,
77 const MCSubtargetInfo &STI) const;
78
79 void getSDWASrcEncoding(const MCInst &MI, unsigned OpNo, APInt &Op,
80 SmallVectorImpl<MCFixup> &Fixups,
81 const MCSubtargetInfo &STI) const;
82
83 void getSDWAVopcDstEncoding(const MCInst &MI, unsigned OpNo, APInt &Op,
84 SmallVectorImpl<MCFixup> &Fixups,
85 const MCSubtargetInfo &STI) const;
86
87 void getAVOperandEncoding(const MCInst &MI, unsigned OpNo, APInt &Op,
88 SmallVectorImpl<MCFixup> &Fixups,
89 const MCSubtargetInfo &STI) const;
90
91private:
92 uint64_t getImplicitOpSelHiEncoding(int Opcode) const;
93 void getMachineOpValueCommon(const MCInst &MI, const MCOperand &MO,
94 unsigned OpNo, APInt &Op,
95 SmallVectorImpl<MCFixup> &Fixups,
96 const MCSubtargetInfo &STI) const;
97
98 /// Encode an fp or int literal.
99 std::optional<uint64_t>
100 getLitEncoding(const MCInstrDesc &Desc, const MCOperand &MO, unsigned OpNo,
101 const MCSubtargetInfo &STI,
102 bool HasMandatoryLiteral = false) const;
103
104 void getBinaryCodeForInstr(const MCInst &MI, SmallVectorImpl<MCFixup> &Fixups,
105 APInt &Inst, APInt &Scratch,
106 const MCSubtargetInfo &STI) const;
107
108 template <bool HasSrc0, bool HasSrc1, bool HasSrc2>
109 APInt postEncodeVOP3(const MCInst &MI, APInt EncodedValue,
110 const MCSubtargetInfo &STI) const;
111
112 APInt postEncodeVOPCX(const MCInst &MI, APInt EncodedValue,
113 const MCSubtargetInfo &STI) const;
114};
115
116} // end anonymous namespace
117
119 MCContext &Ctx) {
120 return new AMDGPUMCCodeEmitter(MCII, *Ctx.getRegisterInfo());
121}
122
124 const MCExpr *Value, uint16_t Kind, bool PCRel = false) {
125 Fixups.push_back(MCFixup::create(Offset, Value, Kind, PCRel));
126}
127
128// Returns the encoding value to use if the given integer is an integer inline
129// immediate value, or 0 if it is not.
130template <typename IntTy>
132 if (Imm >= 0 && Imm <= 64)
133 return 128 + Imm;
134
135 if (Imm >= -16 && Imm <= -1)
136 return 192 + std::abs(Imm);
137
138 return 0;
139}
140
142 uint16_t IntImm = getIntInlineImmEncoding(static_cast<int16_t>(Val));
143 if (IntImm != 0)
144 return IntImm;
145
146 if (Val == 0x3800) // 0.5
147 return 240;
148
149 if (Val == 0xB800) // -0.5
150 return 241;
151
152 if (Val == 0x3C00) // 1.0
153 return 242;
154
155 if (Val == 0xBC00) // -1.0
156 return 243;
157
158 if (Val == 0x4000) // 2.0
159 return 244;
160
161 if (Val == 0xC000) // -2.0
162 return 245;
163
164 if (Val == 0x4400) // 4.0
165 return 246;
166
167 if (Val == 0xC400) // -4.0
168 return 247;
169
170 if (Val == 0x3118 && // 1.0 / (2.0 * pi)
171 STI.hasFeature(AMDGPU::FeatureInv2PiInlineImm))
172 return 248;
173
174 return 255;
175}
176
178 uint16_t IntImm = getIntInlineImmEncoding(static_cast<int16_t>(Val));
179 if (IntImm != 0)
180 return IntImm;
181
182 // clang-format off
183 switch (Val) {
184 case 0x3F00: return 240; // 0.5
185 case 0xBF00: return 241; // -0.5
186 case 0x3F80: return 242; // 1.0
187 case 0xBF80: return 243; // -1.0
188 case 0x4000: return 244; // 2.0
189 case 0xC000: return 245; // -2.0
190 case 0x4080: return 246; // 4.0
191 case 0xC080: return 247; // -4.0
192 case 0x3E22: return 248; // 1.0 / (2.0 * pi)
193 default: return 255;
194 }
195 // clang-format on
196}
197
199 uint32_t IntImm = getIntInlineImmEncoding(static_cast<int32_t>(Val));
200 if (IntImm != 0)
201 return IntImm;
202
203 if (Val == llvm::bit_cast<uint32_t>(0.5f))
204 return 240;
205
206 if (Val == llvm::bit_cast<uint32_t>(-0.5f))
207 return 241;
208
209 if (Val == llvm::bit_cast<uint32_t>(1.0f))
210 return 242;
211
212 if (Val == llvm::bit_cast<uint32_t>(-1.0f))
213 return 243;
214
215 if (Val == llvm::bit_cast<uint32_t>(2.0f))
216 return 244;
217
218 if (Val == llvm::bit_cast<uint32_t>(-2.0f))
219 return 245;
220
221 if (Val == llvm::bit_cast<uint32_t>(4.0f))
222 return 246;
223
224 if (Val == llvm::bit_cast<uint32_t>(-4.0f))
225 return 247;
226
227 if (Val == 0x3e22f983 && // 1.0 / (2.0 * pi)
228 STI.hasFeature(AMDGPU::FeatureInv2PiInlineImm))
229 return 248;
230
231 return 255;
232}
233
235 return getLit32Encoding(Val, STI);
236}
237
239 const MCSubtargetInfo &STI, bool IsFP) {
240 uint32_t IntImm = getIntInlineImmEncoding(static_cast<int64_t>(Val));
241 if (IntImm != 0)
242 return IntImm;
243
244 if (Val == llvm::bit_cast<uint64_t>(0.5))
245 return 240;
246
247 if (Val == llvm::bit_cast<uint64_t>(-0.5))
248 return 241;
249
250 if (Val == llvm::bit_cast<uint64_t>(1.0))
251 return 242;
252
253 if (Val == llvm::bit_cast<uint64_t>(-1.0))
254 return 243;
255
256 if (Val == llvm::bit_cast<uint64_t>(2.0))
257 return 244;
258
259 if (Val == llvm::bit_cast<uint64_t>(-2.0))
260 return 245;
261
262 if (Val == llvm::bit_cast<uint64_t>(4.0))
263 return 246;
264
265 if (Val == llvm::bit_cast<uint64_t>(-4.0))
266 return 247;
267
268 if (Val == 0x3fc45f306dc9c882 && // 1.0 / (2.0 * pi)
269 STI.hasFeature(AMDGPU::FeatureInv2PiInlineImm))
270 return 248;
271
272 // The rest part needs to align with AMDGPUInstPrinter::printLiteral64.
273
274 bool CanUse64BitLiterals = STI.hasFeature(AMDGPU::Feature64BitLiterals) &&
276 if (IsFP) {
277 return CanUse64BitLiterals && Lo_32(Val) ? 254 : 255;
278 }
279
280 return CanUse64BitLiterals && (!isInt<32>(Val) || !isUInt<32>(Val)) ? 254
281 : 255;
282}
283
284std::optional<uint64_t> AMDGPUMCCodeEmitter::getLitEncoding(
285 const MCInstrDesc &Desc, const MCOperand &MO, unsigned OpNo,
286 const MCSubtargetInfo &STI, bool HasMandatoryLiteral) const {
287 const MCOperandInfo &OpInfo = Desc.operands()[OpNo];
288 int64_t Imm = 0;
289 if (MO.isExpr()) {
290 if (!MO.getExpr()->evaluateAsAbsolute(Imm) ||
292 if (OpInfo.OperandType == AMDGPU::OPERAND_KIMM16 ||
295 return Imm;
296 if (STI.hasFeature(AMDGPU::Feature64BitLiterals) &&
297 AMDGPU::getOperandSize(OpInfo) == 8 &&
299 return 254;
300 return 255;
301 }
302 } else {
303 assert(!MO.isDFPImm());
304
305 if (!MO.isImm())
306 return {};
307
308 Imm = MO.getImm();
309 }
310
311 switch (OpInfo.OperandType) {
321 return getLit32Encoding(static_cast<uint32_t>(Imm), STI);
322
326 return getLit64Encoding(Desc, static_cast<uint64_t>(Imm), STI, false);
327
330 return getLit64Encoding(Desc, static_cast<uint64_t>(Imm), STI, true);
331
334 auto Enc = getLit64Encoding(Desc, static_cast<uint64_t>(Imm), STI, true);
335 return (HasMandatoryLiteral && Enc == 255) ? 254 : Enc;
336 }
337
340 return getLit16IntEncoding(static_cast<uint32_t>(Imm), STI);
341
344 // FIXME Is this correct? What do inline immediates do on SI for f16 src
345 // which does not have f16 support?
346 return getLit16Encoding(static_cast<uint16_t>(Imm), STI);
347
349 return 255;
350
353 // We don't actually need to check Inv2Pi here because BF16 instructions can
354 // only be emitted for targets that already support the feature.
355 return getLitBF16Encoding(static_cast<uint16_t>(Imm));
356
359 return AMDGPU::getInlineEncodingV2I16(static_cast<uint32_t>(Imm))
360 .value_or(255);
361
364 return AMDGPU::getInlineEncodingV2F16(static_cast<uint32_t>(Imm))
365 .value_or(255);
366
368 // V_PK_FMAC_F16 has different inline constant behavior on pre-GFX11 vs
369 // GFX11+: pre-GFX11 produces (f16, 0), GFX11+ duplicates f16 to both
370 // halves.
371 return AMDGPU::getPKFMACF16InlineEncoding(static_cast<uint32_t>(Imm),
373 .value_or(255);
374
377 return AMDGPU::getInlineEncodingV2BF16(static_cast<uint32_t>(Imm))
378 .value_or(255);
379
381 return 255;
382
386 return Imm;
387 default:
388 llvm_unreachable("invalid operand size");
389 }
390}
391
392uint64_t AMDGPUMCCodeEmitter::getImplicitOpSelHiEncoding(int Opcode) const {
393 using namespace AMDGPU::VOP3PEncoding;
394
395 if (AMDGPU::hasNamedOperand(Opcode, AMDGPU::OpName::op_sel_hi)) {
396 if (AMDGPU::hasNamedOperand(Opcode, AMDGPU::OpName::src2))
397 return 0;
398 if (AMDGPU::hasNamedOperand(Opcode, AMDGPU::OpName::src1))
399 return OP_SEL_HI_2;
400 if (AMDGPU::hasNamedOperand(Opcode, AMDGPU::OpName::src0))
401 return OP_SEL_HI_1 | OP_SEL_HI_2;
402 }
404}
405
406void AMDGPUMCCodeEmitter::encodeInstruction(const MCInst &MI,
407 SmallVectorImpl<char> &CB,
408 SmallVectorImpl<MCFixup> &Fixups,
409 const MCSubtargetInfo &STI) const {
410 int Opcode = MI.getOpcode();
411 APInt Encoding, Scratch;
412 getBinaryCodeForInstr(MI, Fixups, Encoding, Scratch, STI);
413 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
414 unsigned bytes = Desc.getSize();
415
416 // Set unused op_sel_hi bits to 1 for VOP3P and MAI instructions.
417 // Note that accvgpr_read/write are MAI, have src0, but do not use op_sel.
418 if ((SIInstrFlags::isVOP3P(Desc) || Opcode == AMDGPU::V_ACCVGPR_READ_B32_vi ||
419 Opcode == AMDGPU::V_ACCVGPR_WRITE_B32_vi) &&
420 // Matrix B format operand reuses op_sel_hi.
421 !AMDGPU::hasNamedOperand(Opcode, AMDGPU::OpName::matrix_b_fmt) &&
422 // Matrix B scale operand reuses op_sel_hi.
423 !AMDGPU::hasNamedOperand(Opcode, AMDGPU::OpName::matrix_b_scale) &&
424 // Matrix B reuse operand reuses op_sel_hi.
425 !AMDGPU::hasNamedOperand(Opcode, AMDGPU::OpName::matrix_b_reuse) &&
426 // Some VOP3P instructions have no op_sel_hi field, and SP3 encodes
427 // those bits as zero.
428 !AMDGPU::hasNoOpSelHi(Opcode)) {
429 Encoding |= getImplicitOpSelHiEncoding(Opcode);
430 }
431
432 for (unsigned i = 0; i < bytes; i++) {
433 CB.push_back((uint8_t)Encoding.extractBitsAsZExtValue(8, 8 * i));
434 }
435
436 // NSA encoding.
438 int vaddr0 = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
439 AMDGPU::OpName::vaddr0);
440 int srsrc = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
441 AMDGPU::OpName::srsrc);
442 assert(vaddr0 >= 0 && srsrc > vaddr0);
443 unsigned NumExtraAddrs = srsrc - vaddr0 - 1;
444 unsigned NumPadding = ~(NumExtraAddrs - 1u) & 3;
445
446 for (unsigned i = 0; i < NumExtraAddrs; ++i) {
447 getMachineOpValue(MI, MI.getOperand(vaddr0 + 1 + i), Encoding, Fixups,
448 STI);
449 CB.push_back((uint8_t)Encoding.getLimitedValue());
450 }
451 CB.append(NumPadding, 0);
452 }
453
454 if ((bytes > 8 && STI.hasFeature(AMDGPU::FeatureVOP3Literal)) ||
455 (bytes > 4 && !STI.hasFeature(AMDGPU::FeatureVOP3Literal)))
456 return;
457
458 // Do not print literals from SISrc Operands for insts with mandatory literals
459 if (AMDGPU::hasNamedOperand(MI.getOpcode(), AMDGPU::OpName::imm))
460 return;
461
462 // Check for additional literals
463 for (unsigned i = 0, e = Desc.getNumOperands(); i < e; ++i) {
464
465 // Check if this operand should be encoded as [SV]Src
467 continue;
468
469 // Is this operand a literal immediate?
470 const MCOperand &Op = MI.getOperand(i);
471 auto Enc = getLitEncoding(Desc, Op, i, STI);
472 if (!Enc || (*Enc != 255 && *Enc != 254))
473 continue;
474
475 // Yes! Encode it
476 int64_t Imm = 0;
477
478 bool IsLit = false;
479 if (Op.isImm())
480 Imm = Op.getImm();
481 else if (Op.isExpr()) {
482 if (const auto *C = dyn_cast<MCConstantExpr>(Op.getExpr())) {
483 Imm = C->getValue();
484 } else if (AMDGPU::isLitExpr(Op.getExpr())) {
485 IsLit = true;
486 Imm = AMDGPU::getLitValue(Op.getExpr());
487 }
488 } else // Exprs will be replaced with a fixup value.
489 llvm_unreachable("Must be immediate or expr");
490
491 if (*Enc == 254) {
492 assert(STI.hasFeature(AMDGPU::Feature64BitLiterals));
494 } else {
495 auto OpType =
496 static_cast<AMDGPU::OperandType>(Desc.operands()[i].OperandType);
497 Imm = AMDGPU::encode32BitLiteral(Imm, OpType, IsLit);
499 }
500
501 // Only one literal value allowed
502 break;
503 }
504}
505
506void AMDGPUMCCodeEmitter::getSOPPBrEncoding(const MCInst &MI, unsigned OpNo,
507 APInt &Op,
508 SmallVectorImpl<MCFixup> &Fixups,
509 const MCSubtargetInfo &STI) const {
510 const MCOperand &MO = MI.getOperand(OpNo);
511
512 if (MO.isExpr()) {
513 const MCExpr *Expr = MO.getExpr();
514 addFixup(Fixups, 0, Expr, AMDGPU::fixup_si_sopp_br, true);
515 Op = APInt::getZero(96);
516 } else {
517 getMachineOpValue(MI, MO, Op, Fixups, STI);
518 }
519}
520
521void AMDGPUMCCodeEmitter::getSMEMOffsetEncoding(
522 const MCInst &MI, unsigned OpNo, APInt &Op,
523 SmallVectorImpl<MCFixup> &Fixups, const MCSubtargetInfo &STI) const {
524 auto Offset = MI.getOperand(OpNo).getImm();
525 // VI only supports 20-bit unsigned offsets.
527 Op = Offset;
528}
529
530void AMDGPUMCCodeEmitter::getSMEMOffsetPCRelEncoding(
531 const MCInst &MI, unsigned OpNo, APInt &Op,
532 SmallVectorImpl<MCFixup> &Fixups, const MCSubtargetInfo &STI) const {
533 const MCOperand &MO = MI.getOperand(OpNo);
534
535 if (MO.isExpr()) {
536 // Field is Inst[55:32], so the fixup starts at byte 4. PC-relative
537 // evaluation then yields label - (PC + 4); adjustFixupValue subtracts the
538 // remaining 4 bytes to reach the next instruction.
539 addFixup(Fixups, 4, MO.getExpr(), AMDGPU::fixup_si_smem_pcrel, true);
540 Op = APInt::getZero(96);
541 return;
542 }
543
544 getSMEMOffsetEncoding(MI, OpNo, Op, Fixups, STI);
545}
546
547void AMDGPUMCCodeEmitter::getSDWASrcEncoding(const MCInst &MI, unsigned OpNo,
548 APInt &Op,
549 SmallVectorImpl<MCFixup> &Fixups,
550 const MCSubtargetInfo &STI) const {
551 using namespace AMDGPU::SDWA;
552
553 uint64_t RegEnc = 0;
554
555 const MCOperand &MO = MI.getOperand(OpNo);
556
557 if (MO.isReg()) {
558 MCRegister Reg = MO.getReg();
559 RegEnc |= MRI.getEncodingValue(Reg);
560 RegEnc &= SDWA9EncValues::SRC_VGPR_MASK;
562 RegEnc |= SDWA9EncValues::SRC_SGPR_MASK;
563 }
564 Op = RegEnc;
565 return;
566 } else {
567 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
568 auto Enc = getLitEncoding(Desc, MO, OpNo, STI);
569 if (Enc && *Enc != 255) {
570 Op = *Enc | SDWA9EncValues::SRC_SGPR_MASK;
571 return;
572 }
573 }
574
575 llvm_unreachable("Unsupported operand kind");
576}
577
578void AMDGPUMCCodeEmitter::getSDWAVopcDstEncoding(
579 const MCInst &MI, unsigned OpNo, APInt &Op,
580 SmallVectorImpl<MCFixup> &Fixups, const MCSubtargetInfo &STI) const {
581 using namespace AMDGPU::SDWA;
582
583 uint64_t RegEnc = 0;
584
585 const MCOperand &MO = MI.getOperand(OpNo);
586
587 MCRegister Reg = MO.getReg();
588 if (Reg != AMDGPU::VCC && Reg != AMDGPU::VCC_LO) {
589 RegEnc |= MRI.getEncodingValue(Reg);
590 RegEnc &= SDWA9EncValues::VOPC_DST_SGPR_MASK;
591 RegEnc |= SDWA9EncValues::VOPC_DST_VCC_MASK;
592 }
593 Op = RegEnc;
594}
595
596void AMDGPUMCCodeEmitter::getAVOperandEncoding(
597 const MCInst &MI, unsigned OpNo, APInt &Op,
598 SmallVectorImpl<MCFixup> &Fixups, const MCSubtargetInfo &STI) const {
599 MCRegister Reg = MI.getOperand(OpNo).getReg();
600 unsigned Enc = MRI.getEncodingValue(Reg);
602 bool IsVGPROrAGPR =
604
605 // VGPR and AGPR have the same encoding, but SrcA and SrcB operands of mfma
606 // instructions use acc[0:1] modifier bits to distinguish. These bits are
607 // encoded as a virtual 9th bit of the register for these operands.
608 bool IsAGPR = Enc & AMDGPU::HWEncoding::IS_AGPR;
609
610 Op = Idx | (IsVGPROrAGPR << 8) | (IsAGPR << 9);
611}
612
613static bool needsPCRel(const MCExpr *Expr) {
614 switch (Expr->getKind()) {
615 case MCExpr::SymbolRef: {
616 auto *SE = cast<MCSymbolRefExpr>(Expr);
617 auto Spec = AMDGPU::getSpecifier(SE);
618 return Spec != AMDGPUMCExpr::S_ABS32_LO &&
620 }
621 case MCExpr::Binary: {
622 auto *BE = cast<MCBinaryExpr>(Expr);
623 if (BE->getOpcode() == MCBinaryExpr::Sub)
624 return false;
625 return needsPCRel(BE->getLHS()) || needsPCRel(BE->getRHS());
626 }
627 case MCExpr::Unary:
628 return needsPCRel(cast<MCUnaryExpr>(Expr)->getSubExpr());
630 case MCExpr::Target:
631 case MCExpr::Constant:
632 return false;
633 }
634 llvm_unreachable("invalid kind");
635}
636
637void AMDGPUMCCodeEmitter::getMachineOpValue(const MCInst &MI,
638 const MCOperand &MO, APInt &Op,
639 SmallVectorImpl<MCFixup> &Fixups,
640 const MCSubtargetInfo &STI) const {
641 if (MO.isReg()){
642 unsigned Enc = MRI.getEncodingValue(MO.getReg());
644 bool IsVGPROrAGPR =
646 Op = Idx | (IsVGPROrAGPR << 8);
647 return;
648 }
649 unsigned OpNo = &MO - MI.begin();
650 getMachineOpValueCommon(MI, MO, OpNo, Op, Fixups, STI);
651}
652
653void AMDGPUMCCodeEmitter::getMachineOpValueT16(
654 const MCInst &MI, unsigned OpNo, APInt &Op,
655 SmallVectorImpl<MCFixup> &Fixups, const MCSubtargetInfo &STI) const {
656 const MCOperand &MO = MI.getOperand(OpNo);
657 if (MO.isReg()) {
658 unsigned Enc = MRI.getEncodingValue(MO.getReg());
659 unsigned Idx = Enc & AMDGPU::HWEncoding::REG_IDX_MASK;
660 bool IsVGPR = Enc & AMDGPU::HWEncoding::IS_VGPR;
661 Op = Idx | (IsVGPR << 8);
662 return;
663 }
664 getMachineOpValueCommon(MI, MO, OpNo, Op, Fixups, STI);
665 // VGPRs include the suffix/op_sel bit in the register encoding, but
666 // immediates and SGPRs include it in src_modifiers. Therefore, copy the
667 // op_sel bit from the src operands into src_modifier operands if Op is
668 // src_modifiers and the corresponding src is a VGPR
669 int SrcMOIdx = -1;
670 assert(OpNo < INT_MAX);
671 if ((int)OpNo == AMDGPU::getNamedOperandIdx(MI.getOpcode(),
672 AMDGPU::OpName::src0_modifiers)) {
673 SrcMOIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src0);
674 int VDstMOIdx =
675 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vdst);
676 if (VDstMOIdx != -1) {
677 auto DstReg = MI.getOperand(VDstMOIdx).getReg();
678 if (AMDGPU::isHi16Reg(DstReg, MRI))
680 }
681 } else if ((int)OpNo == AMDGPU::getNamedOperandIdx(
682 MI.getOpcode(), AMDGPU::OpName::src1_modifiers))
683 SrcMOIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src1);
684 else if ((int)OpNo == AMDGPU::getNamedOperandIdx(
685 MI.getOpcode(), AMDGPU::OpName::src2_modifiers))
686 SrcMOIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src2);
687 if (SrcMOIdx == -1)
688 return;
689
690 const MCOperand &SrcMO = MI.getOperand(SrcMOIdx);
691 if (!SrcMO.isReg())
692 return;
693 auto SrcReg = SrcMO.getReg();
694 if (AMDGPU::isSGPR(SrcReg, &MRI))
695 return;
696 if (AMDGPU::isHi16Reg(SrcReg, MRI))
698}
699
700void AMDGPUMCCodeEmitter::getMachineOpValueT16Lo128(
701 const MCInst &MI, unsigned OpNo, APInt &Op,
702 SmallVectorImpl<MCFixup> &Fixups, const MCSubtargetInfo &STI) const {
703 const MCOperand &MO = MI.getOperand(OpNo);
704 if (MO.isReg()) {
705 uint16_t Encoding = MRI.getEncodingValue(MO.getReg());
706 unsigned RegIdx = Encoding & AMDGPU::HWEncoding::LO256_REG_IDX_MASK;
707 bool IsHi = Encoding & AMDGPU::HWEncoding::IS_HI16;
708 bool IsVGPR = Encoding & AMDGPU::HWEncoding::IS_VGPR;
709 assert((!IsVGPR || isUInt<7>(RegIdx)) && "VGPR0-VGPR127 expected!");
710 Op = (IsVGPR ? 0x100 : 0) | (IsHi ? 0x80 : 0) | RegIdx;
711 return;
712 }
713 getMachineOpValueCommon(MI, MO, OpNo, Op, Fixups, STI);
714}
715
716// Encode an indexed-resource (rsrcidx) operand into the 9-bit srsrc field:
717// bit 8 selects a VGPR (or AGPR) index register, bit 7 selects a 32-bit SGPR
718// index register, and bits 6-0 hold the register index.
719void AMDGPUMCCodeEmitter::getMachineOpValueRsrcRegOp(
720 const MCInst &MI, unsigned OpNo, APInt &Op,
721 SmallVectorImpl<MCFixup> &Fixups, const MCSubtargetInfo &STI) const {
722 const MCOperand &MO = MI.getOperand(OpNo);
723 bool IsSReg32 =
724 MRI.getRegClass(AMDGPU::SReg_32RegClassID).contains(MO.getReg());
725 unsigned Enc = MRI.getEncodingValue(MO.getReg());
727 bool IsVGPROrAGPR =
729 Op = Idx | IsVGPROrAGPR << 8 | IsSReg32 << 7;
730}
731
732void AMDGPUMCCodeEmitter::getMachineOpValueCommon(
733 const MCInst &MI, const MCOperand &MO, unsigned OpNo, APInt &Op,
734 SmallVectorImpl<MCFixup> &Fixups, const MCSubtargetInfo &STI) const {
735 bool isLikeImm = false;
736 int64_t Val;
737
738 if (MO.isImm()) {
739 Val = MO.getImm();
740 isLikeImm = true;
741 } else if (MO.isExpr() && MO.getExpr()->evaluateAsAbsolute(Val)) {
742 isLikeImm = true;
743 } else if (MO.isExpr()) {
744 // FIXME: If this is expression is PCRel or not should not depend on what
745 // the expression looks like. Given that this is just a general expression,
746 // it should probably be FK_Data_4 and whatever is producing
747 //
748 // s_add_u32 s2, s2, (extern_const_addrspace+16
749 //
750 // And expecting a PCRel should instead produce
751 //
752 // .Ltmp1:
753 // s_add_u32 s2, s2, (extern_const_addrspace+16)-.Ltmp1
754 bool PCRel = needsPCRel(MO.getExpr());
755 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
756 uint32_t Offset = Desc.getSize();
757 assert(Offset == 4 || Offset == 8);
758 unsigned Size = AMDGPU::getOperandSize(Desc, OpNo);
760 addFixup(Fixups, Offset, MO.getExpr(), Kind, PCRel);
761 }
762
763 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
764 if (AMDGPU::isSISrcOperand(Desc, OpNo)) {
765 bool HasMandatoryLiteral =
766 AMDGPU::hasNamedOperand(MI.getOpcode(), AMDGPU::OpName::imm);
767 if (auto Enc = getLitEncoding(Desc, MO, OpNo, STI, HasMandatoryLiteral)) {
768 Op = *Enc;
769 return;
770 }
771
772 llvm_unreachable("Operand not supported for SISrc");
773 }
774
775 if (isLikeImm) {
776 Op = Val;
777 return;
778 }
779
780 llvm_unreachable("Encoding of this operand type is not supported yet.");
781}
782
783template <bool HasSrc0, bool HasSrc1, bool HasSrc2>
784APInt AMDGPUMCCodeEmitter::postEncodeVOP3(const MCInst &MI, APInt EncodedValue,
785 const MCSubtargetInfo &STI) const {
786 if (!AMDGPU::isGFX10Plus(STI))
787 return EncodedValue;
788 // Set unused source fields in VOP3 encodings to inline immediate 0 to avoid
789 // hardware conservatively assuming the instruction reads SGPRs.
790 constexpr uint64_t InlineImmediate0 = 0x80;
791 if (!HasSrc0)
792 EncodedValue |= InlineImmediate0 << 32;
793 if (!HasSrc1)
794 EncodedValue |= InlineImmediate0 << 41;
795 if (!HasSrc2)
796 EncodedValue |= InlineImmediate0 << 50;
797 return EncodedValue;
798}
799
800APInt AMDGPUMCCodeEmitter::postEncodeVOPCX(const MCInst &MI, APInt EncodedValue,
801 const MCSubtargetInfo &STI) const {
802 // GFX10+ v_cmpx opcodes promoted to VOP3 have implied dst=EXEC.
803 // Documentation requires dst to be encoded as EXEC (0x7E),
804 // but it looks like the actual value encoded for dst operand
805 // is ignored by HW. It was decided to define dst as "do not care"
806 // in td files to allow disassembler accept any dst value.
807 // However, dst is encoded as EXEC for compatibility with SP3.
809 MCII.get(MI.getOpcode()).hasImplicitDefOfPhysReg(AMDGPU::EXEC));
810 EncodedValue |= MRI.getEncodingValue(AMDGPU::EXEC_LO) &
812 return postEncodeVOP3<true, true, false>(MI, EncodedValue, STI);
813}
814
815#include "AMDGPUGenMCCodeEmitter.inc"
static void addFixup(SmallVectorImpl< MCFixup > &Fixups, uint32_t Offset, const MCExpr *Value, uint16_t Kind, bool PCRel=false)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
uint64_t Imm
static uint32_t getLit64Encoding(const MCInstrDesc &Desc, uint64_t Val, const MCSubtargetInfo &STI, bool IsFP)
static uint32_t getLit16IntEncoding(uint32_t Val, const MCSubtargetInfo &STI)
static void addFixup(SmallVectorImpl< MCFixup > &Fixups, uint32_t Offset, const MCExpr *Value, uint16_t Kind, bool PCRel=false)
static uint32_t getLitBF16Encoding(uint16_t Val)
static uint32_t getLit16Encoding(uint16_t Val, const MCSubtargetInfo &STI)
static uint32_t getIntInlineImmEncoding(IntTy Imm)
static bool needsPCRel(const MCExpr *Expr)
static uint32_t getLit32Encoding(uint32_t Val, const MCSubtargetInfo &STI)
Provides AMDGPU specific target descriptions.
This file implements a class to represent arbitrary precision integral constant values and operations...
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
IRTranslator LLVM IR MI
Register Reg
MachineRegisterInfo & MRI
LLVM_ABI uint64_t extractBitsAsZExtValue(unsigned numBits, unsigned bitPosition) const
Definition APInt.cpp:517
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value.
Definition APInt.h:471
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:196
@ Sub
Subtraction.
Definition MCExpr.h:323
MCCodeEmitter - Generic instruction encoding interface.
Context object for machine code objects.
Definition MCContext.h:84
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
@ Unary
Unary expressions.
Definition MCExpr.h:44
@ Constant
Constant expressions.
Definition MCExpr.h:42
@ SymbolRef
References to labels and assigned expressions.
Definition MCExpr.h:43
@ Target
Target specific expression.
Definition MCExpr.h:46
@ Specifier
Expression with a relocation specifier.
Definition MCExpr.h:45
@ Binary
Binary expressions.
Definition MCExpr.h:41
ExprKind getKind() const
Definition MCExpr.h:85
static MCFixupKind getDataKindForSize(unsigned Size)
Return the generic fixup kind for a value with the given size.
Definition MCFixup.h:114
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, bool PCRel=false)
Consider bit fields if we need more flags.
Definition MCFixup.h:90
Describe properties that are true of each instruction in the target description file.
LLVM_ABI bool hasImplicitDefOfPhysReg(MCRegister Reg, const MCRegisterInfo *MRI=nullptr) const
Return true if this instruction implicitly defines the specified physical register.
Interface to description of machine instruction set.
Definition MCInstrInfo.h:27
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode.
Definition MCInstrInfo.h:89
uint8_t OperandType
Information about the type of the operand.
Instances of this class represent operands of the MCInst class.
Definition MCInst.h:40
int64_t getImm() const
Definition MCInst.h:84
bool isImm() const
Definition MCInst.h:66
bool isReg() const
Definition MCInst.h:65
MCRegister getReg() const
Returns the register number.
Definition MCInst.h:73
bool isDFPImm() const
Definition MCInst.h:68
const MCExpr * getExpr() const
Definition MCInst.h:118
bool isExpr() const
Definition MCInst.h:69
bool contains(MCRegister Reg) const
contains - Return true if the specified register is included in this register class.
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
LLVM Value Representation.
Definition Value.h:75
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool isSGPR(MCRegister Reg, const MCRegisterInfo *TRI)
Is Reg - scalar register.
bool isHi16Reg(MCRegister Reg, const MCRegisterInfo &MRI)
static AMDGPUMCExpr::Specifier getSpecifier(const MCSymbolRefExpr *SRE)
LLVM_READONLY bool isLitExpr(const MCExpr *Expr)
@ fixup_si_sopp_br
16-bit PC relative fixup for SOPP branch instructions.
@ fixup_si_smem_pcrel
24-bit PC-relative byte offset for s_prefetch_inst_pc_rel.
LLVM_READONLY bool hasNamedOperand(uint32_t Opcode, OpName NamedIdx)
bool hasNoOpSelHi(unsigned Opc)
Returns true if this VOP3P opcode has no op_sel_hi field, in which case those bits are encoded as zer...
constexpr bool isSISrcOperand(const MCOperandInfo &OpInfo)
Is this an AMDGPU specific source operand?
LLVM_READONLY AMDGPUMCExpr::VariantKind getExprKind(const MCExpr *Expr)
LLVM_READONLY int64_t getLitValue(const MCExpr *Expr)
bool isGFX11Plus(const MCSubtargetInfo &STI)
std::optional< unsigned > getInlineEncodingV2F16(uint32_t Literal)
bool isGFX10Plus(const MCSubtargetInfo &STI)
int64_t encode32BitLiteral(int64_t Imm, OperandType Type, bool IsLit)
@ OPERAND_REG_IMM_V2FP64
Definition SIDefines.h:447
@ OPERAND_KIMM32
Operand with 32-bit immediate that uses the constant bus.
Definition SIDefines.h:465
@ OPERAND_REG_IMM_INT64
Definition SIDefines.h:432
@ OPERAND_REG_IMM_V2FP16
Definition SIDefines.h:440
@ OPERAND_REG_INLINE_C_FP64
Definition SIDefines.h:456
@ OPERAND_REG_IMM_NOINLINE_FP16
Definition SIDefines.h:438
@ OPERAND_REG_INLINE_C_BF16
Definition SIDefines.h:453
@ OPERAND_REG_INLINE_C_V2BF16
Definition SIDefines.h:458
@ OPERAND_REG_IMM_V2INT64
Definition SIDefines.h:443
@ OPERAND_REG_IMM_V2INT16
Definition SIDefines.h:442
@ OPERAND_REG_IMM_BF16
Definition SIDefines.h:436
@ OPERAND_REG_IMM_INT32
Operands with register, 32-bit, or 64-bit immediate.
Definition SIDefines.h:431
@ OPERAND_REG_IMM_V2BF16
Definition SIDefines.h:439
@ OPERAND_REG_IMM_FP16
Definition SIDefines.h:437
@ OPERAND_REG_IMM_V2FP16_SPLAT
Definition SIDefines.h:441
@ OPERAND_REG_INLINE_C_INT64
Definition SIDefines.h:452
@ OPERAND_REG_INLINE_C_INT16
Operands with register or inline constant.
Definition SIDefines.h:450
@ OPERAND_REG_IMM_NOINLINE_V2FP16
Definition SIDefines.h:444
@ OPERAND_REG_IMM_FP64
Definition SIDefines.h:435
@ OPERAND_REG_INLINE_C_V2FP16
Definition SIDefines.h:459
@ OPERAND_REG_INLINE_AC_INT32
Operands with an AccVGPR register or inline constant.
Definition SIDefines.h:470
@ OPERAND_REG_INLINE_AC_FP32
Definition SIDefines.h:471
@ OPERAND_REG_IMM_V2INT32
Definition SIDefines.h:445
@ OPERAND_REG_IMM_FP32
Definition SIDefines.h:434
@ OPERAND_REG_INLINE_C_FP32
Definition SIDefines.h:455
@ OPERAND_REG_INLINE_C_INT32
Definition SIDefines.h:451
@ OPERAND_REG_INLINE_C_V2INT16
Definition SIDefines.h:457
@ OPERAND_REG_IMM_V2FP32
Definition SIDefines.h:446
@ OPERAND_REG_INLINE_AC_FP64
Definition SIDefines.h:472
@ OPERAND_REG_INLINE_C_FP16
Definition SIDefines.h:454
@ OPERAND_REG_IMM_INT16
Definition SIDefines.h:433
@ OPERAND_INLINE_SPLIT_BARRIER_INT32
Definition SIDefines.h:462
std::optional< unsigned > getPKFMACF16InlineEncoding(uint32_t Literal, bool IsGFX11Plus)
std::optional< unsigned > getInlineEncodingV2I16(uint32_t Literal)
bool isVI(const MCSubtargetInfo &STI)
MCRegister mc2PseudoReg(MCRegister Reg)
Convert hardware register Reg to a pseudo register.
std::optional< unsigned > getInlineEncodingV2BF16(uint32_t Literal)
LLVM_READNONE unsigned getOperandSize(const MCOperandInfo &OpInfo)
constexpr bool isVOP3(const T &...O)
Definition SIDefines.h:239
constexpr bool isVOP3P(const T &...O)
Definition SIDefines.h:242
constexpr bool isVOP3Like(const T &...O)
Definition SIDefines.h:245
constexpr bool isMIMG(const T &...O)
Definition SIDefines.h:274
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition Endian.h:82
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
Op::Description Desc
uint16_t MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition MCFixup.h:22
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
Definition MathExtras.h:156
To bit_cast(const From &from) noexcept
Definition bit.h:90
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
MCCodeEmitter * createAMDGPUMCCodeEmitter(const MCInstrInfo &MCII, MCContext &Ctx)