1 //===-- AMDGPUAsmParser.cpp - Parse SI asm to MCInst instructions ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
11 #include "MCTargetDesc/AMDGPUTargetStreamer.h"
12 #include "Utils/AMDGPUBaseInfo.h"
13 #include "AMDKernelCodeT.h"
14 #include "SIDefines.h"
15 #include "llvm/ADT/APFloat.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/StringSwitch.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/MC/MCInstrInfo.h"
25 #include "llvm/MC/MCParser/MCAsmLexer.h"
26 #include "llvm/MC/MCParser/MCAsmParser.h"
27 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
28 #include "llvm/MC/MCRegisterInfo.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCSubtargetInfo.h"
31 #include "llvm/MC/MCTargetAsmParser.h"
32 #include "llvm/Support/SourceMgr.h"
33 #include "llvm/Support/TargetRegistry.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Support/Debug.h"
41 struct OptionalOperand;
43 class AMDGPUOperand : public MCParsedAsmOperand {
51 SMLoc StartLoc, EndLoc;
54 AMDGPUOperand(enum KindTy K) : MCParsedAsmOperand(), Kind(K) {}
85 const MCRegisterInfo *TRI;
96 void addImmOperands(MCInst &Inst, unsigned N) const {
97 Inst.addOperand(MCOperand::createImm(getImm()));
100 StringRef getToken() const {
101 return StringRef(Tok.Data, Tok.Length);
104 void addRegOperands(MCInst &Inst, unsigned N) const {
105 Inst.addOperand(MCOperand::createReg(getReg()));
108 void addRegOrImmOperands(MCInst &Inst, unsigned N) const {
110 addRegOperands(Inst, N);
112 addImmOperands(Inst, N);
115 void addRegWithInputModsOperands(MCInst &Inst, unsigned N) const {
116 Inst.addOperand(MCOperand::createImm(
117 Reg.Modifiers == -1 ? 0 : Reg.Modifiers));
118 addRegOperands(Inst, N);
121 void addSoppBrTargetOperands(MCInst &Inst, unsigned N) const {
123 addImmOperands(Inst, N);
126 Inst.addOperand(MCOperand::createExpr(Expr));
130 bool defaultTokenHasSuffix() const {
131 StringRef Token(Tok.Data, Tok.Length);
133 return Token.endswith("_e32") || Token.endswith("_e64");
136 bool isToken() const override {
137 return Kind == Token;
140 bool isImm() const override {
141 return Kind == Immediate;
144 bool isInlineImm() const {
145 float F = BitsToFloat(Imm.Val);
146 // TODO: Add 0.5pi for VI
147 return isImm() && ((Imm.Val <= 64 && Imm.Val >= -16) ||
148 (F == 0.0 || F == 0.5 || F == -0.5 || F == 1.0 || F == -1.0 ||
149 F == 2.0 || F == -2.0 || F == 4.0 || F == -4.0));
152 bool isDSOffset0() const {
154 return Imm.Type == ImmTyDSOffset0;
157 bool isDSOffset1() const {
159 return Imm.Type == ImmTyDSOffset1;
162 int64_t getImm() const {
166 enum ImmTy getImmTy() const {
171 bool isRegKind() const {
172 return Kind == Register;
175 bool isReg() const override {
176 return Kind == Register && Reg.Modifiers == -1;
179 bool isRegWithInputMods() const {
180 return Kind == Register && (Reg.IsForcedVOP3 || Reg.Modifiers != -1);
183 void setModifiers(unsigned Mods) {
185 Reg.Modifiers = Mods;
188 bool hasModifiers() const {
190 return Reg.Modifiers != -1;
193 unsigned getReg() const override {
197 bool isRegOrImm() const {
198 return isReg() || isImm();
201 bool isRegClass(unsigned RCID) const {
202 return Reg.TRI->getRegClass(RCID).contains(getReg());
205 bool isSCSrc32() const {
206 return isInlineImm() || (isReg() && isRegClass(AMDGPU::SReg_32RegClassID));
209 bool isSSrc32() const {
210 return isImm() || (isReg() && isRegClass(AMDGPU::SReg_32RegClassID));
213 bool isSSrc64() const {
214 return isImm() || isInlineImm() ||
215 (isReg() && isRegClass(AMDGPU::SReg_64RegClassID));
218 bool isSCSrc64() const {
219 return (isReg() && isRegClass(AMDGPU::SReg_64RegClassID)) || isInlineImm();
222 bool isVCSrc32() const {
223 return isInlineImm() || (isReg() && isRegClass(AMDGPU::VS_32RegClassID));
226 bool isVCSrc64() const {
227 return isInlineImm() || (isReg() && isRegClass(AMDGPU::VS_64RegClassID));
230 bool isVSrc32() const {
231 return isImm() || (isReg() && isRegClass(AMDGPU::VS_32RegClassID));
234 bool isVSrc64() const {
235 return isImm() || (isReg() && isRegClass(AMDGPU::VS_64RegClassID));
238 bool isMem() const override {
242 bool isExpr() const {
243 return Kind == Expression;
246 bool isSoppBrTarget() const {
247 return isExpr() || isImm();
250 SMLoc getStartLoc() const override {
254 SMLoc getEndLoc() const override {
258 void print(raw_ostream &OS) const override {
261 OS << "<register " << getReg() << " mods: " << Reg.Modifiers << '>';
267 OS << '\'' << getToken() << '\'';
270 OS << "<expr " << *Expr << '>';
275 static std::unique_ptr<AMDGPUOperand> CreateImm(int64_t Val, SMLoc Loc,
276 enum ImmTy Type = ImmTyNone,
277 bool IsFPImm = false) {
278 auto Op = llvm::make_unique<AMDGPUOperand>(Immediate);
280 Op->Imm.IsFPImm = IsFPImm;
287 static std::unique_ptr<AMDGPUOperand> CreateToken(StringRef Str, SMLoc Loc,
288 bool HasExplicitEncodingSize = true) {
289 auto Res = llvm::make_unique<AMDGPUOperand>(Token);
290 Res->Tok.Data = Str.data();
291 Res->Tok.Length = Str.size();
297 static std::unique_ptr<AMDGPUOperand> CreateReg(unsigned RegNo, SMLoc S,
299 const MCRegisterInfo *TRI,
301 auto Op = llvm::make_unique<AMDGPUOperand>(Register);
302 Op->Reg.RegNo = RegNo;
304 Op->Reg.Modifiers = -1;
305 Op->Reg.IsForcedVOP3 = ForceVOP3;
311 static std::unique_ptr<AMDGPUOperand> CreateExpr(const class MCExpr *Expr, SMLoc S) {
312 auto Op = llvm::make_unique<AMDGPUOperand>(Expression);
319 bool isDSOffset() const;
320 bool isDSOffset01() const;
321 bool isSWaitCnt() const;
322 bool isMubufOffset() const;
323 bool isSMRDOffset() const;
324 bool isSMRDLiteralOffset() const;
327 class AMDGPUAsmParser : public MCTargetAsmParser {
328 MCSubtargetInfo &STI;
329 const MCInstrInfo &MII;
332 unsigned ForcedEncodingSize;
335 return STI.getFeatureBits()[AMDGPU::FeatureVolcanicIslands];
338 bool hasSGPR102_SGPR103() const {
342 /// @name Auto-generated Match Functions
345 #define GET_ASSEMBLER_HEADER
346 #include "AMDGPUGenAsmMatcher.inc"
351 bool ParseDirectiveMajorMinor(uint32_t &Major, uint32_t &Minor);
352 bool ParseDirectiveHSACodeObjectVersion();
353 bool ParseDirectiveHSACodeObjectISA();
354 bool ParseAMDKernelCodeTValue(StringRef ID, amd_kernel_code_t &Header);
355 bool ParseDirectiveAMDKernelCodeT();
356 bool ParseSectionDirectiveHSAText();
357 bool subtargetHasRegister(const MCRegisterInfo &MRI, unsigned RegNo) const;
361 enum AMDGPUMatchResultTy {
362 Match_PreferE32 = FIRST_TARGET_MATCH_RESULT_TY
365 AMDGPUAsmParser(MCSubtargetInfo &STI, MCAsmParser &_Parser,
366 const MCInstrInfo &MII,
367 const MCTargetOptions &Options)
368 : MCTargetAsmParser(Options), STI(STI), MII(MII), Parser(_Parser),
369 ForcedEncodingSize(0) {
371 if (STI.getFeatureBits().none()) {
372 // Set default features.
373 STI.ToggleFeature("SOUTHERN_ISLANDS");
376 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
379 AMDGPUTargetStreamer &getTargetStreamer() {
380 MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
381 return static_cast<AMDGPUTargetStreamer &>(TS);
384 unsigned getForcedEncodingSize() const {
385 return ForcedEncodingSize;
388 void setForcedEncodingSize(unsigned Size) {
389 ForcedEncodingSize = Size;
392 bool isForcedVOP3() const {
393 return ForcedEncodingSize == 64;
396 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
397 unsigned checkTargetMatchPredicate(MCInst &Inst) override;
398 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
399 OperandVector &Operands, MCStreamer &Out,
401 bool MatchingInlineAsm) override;
402 bool ParseDirective(AsmToken DirectiveID) override;
403 OperandMatchResultTy parseOperand(OperandVector &Operands, StringRef Mnemonic);
404 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
405 SMLoc NameLoc, OperandVector &Operands) override;
407 OperandMatchResultTy parseIntWithPrefix(const char *Prefix, int64_t &Int,
408 int64_t Default = 0);
409 OperandMatchResultTy parseIntWithPrefix(const char *Prefix,
410 OperandVector &Operands,
411 enum AMDGPUOperand::ImmTy ImmTy =
412 AMDGPUOperand::ImmTyNone);
413 OperandMatchResultTy parseNamedBit(const char *Name, OperandVector &Operands,
414 enum AMDGPUOperand::ImmTy ImmTy =
415 AMDGPUOperand::ImmTyNone);
416 OperandMatchResultTy parseOptionalOps(
417 const ArrayRef<OptionalOperand> &OptionalOps,
418 OperandVector &Operands);
421 void cvtDSOffset01(MCInst &Inst, const OperandVector &Operands);
422 void cvtDS(MCInst &Inst, const OperandVector &Operands);
423 OperandMatchResultTy parseDSOptionalOps(OperandVector &Operands);
424 OperandMatchResultTy parseDSOff01OptionalOps(OperandVector &Operands);
425 OperandMatchResultTy parseDSOffsetOptional(OperandVector &Operands);
427 bool parseCnt(int64_t &IntVal);
428 OperandMatchResultTy parseSWaitCntOps(OperandVector &Operands);
429 OperandMatchResultTy parseSOppBrTarget(OperandVector &Operands);
431 OperandMatchResultTy parseFlatOptionalOps(OperandVector &Operands);
432 OperandMatchResultTy parseFlatAtomicOptionalOps(OperandVector &Operands);
433 void cvtFlat(MCInst &Inst, const OperandVector &Operands);
435 void cvtMubuf(MCInst &Inst, const OperandVector &Operands);
436 OperandMatchResultTy parseOffset(OperandVector &Operands);
437 OperandMatchResultTy parseMubufOptionalOps(OperandVector &Operands);
438 OperandMatchResultTy parseGLC(OperandVector &Operands);
439 OperandMatchResultTy parseSLC(OperandVector &Operands);
440 OperandMatchResultTy parseTFE(OperandVector &Operands);
442 OperandMatchResultTy parseDMask(OperandVector &Operands);
443 OperandMatchResultTy parseUNorm(OperandVector &Operands);
444 OperandMatchResultTy parseR128(OperandVector &Operands);
446 void cvtVOP3(MCInst &Inst, const OperandVector &Operands);
447 OperandMatchResultTy parseVOP3OptionalOps(OperandVector &Operands);
450 struct OptionalOperand {
452 AMDGPUOperand::ImmTy Type;
455 bool (*ConvertResult)(int64_t&);
460 static int getRegClass(bool IsVgpr, unsigned RegWidth) {
464 case 1: return AMDGPU::VGPR_32RegClassID;
465 case 2: return AMDGPU::VReg_64RegClassID;
466 case 3: return AMDGPU::VReg_96RegClassID;
467 case 4: return AMDGPU::VReg_128RegClassID;
468 case 8: return AMDGPU::VReg_256RegClassID;
469 case 16: return AMDGPU::VReg_512RegClassID;
475 case 1: return AMDGPU::SGPR_32RegClassID;
476 case 2: return AMDGPU::SGPR_64RegClassID;
477 case 4: return AMDGPU::SReg_128RegClassID;
478 case 8: return AMDGPU::SReg_256RegClassID;
479 case 16: return AMDGPU::SReg_512RegClassID;
483 static unsigned getRegForName(StringRef RegName) {
485 return StringSwitch<unsigned>(RegName)
486 .Case("exec", AMDGPU::EXEC)
487 .Case("vcc", AMDGPU::VCC)
488 .Case("flat_scratch", AMDGPU::FLAT_SCR)
489 .Case("m0", AMDGPU::M0)
490 .Case("scc", AMDGPU::SCC)
491 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO)
492 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI)
493 .Case("vcc_lo", AMDGPU::VCC_LO)
494 .Case("vcc_hi", AMDGPU::VCC_HI)
495 .Case("exec_lo", AMDGPU::EXEC_LO)
496 .Case("exec_hi", AMDGPU::EXEC_HI)
500 bool AMDGPUAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) {
501 const AsmToken Tok = Parser.getTok();
502 StartLoc = Tok.getLoc();
503 EndLoc = Tok.getEndLoc();
504 StringRef RegName = Tok.getString();
505 RegNo = getRegForName(RegName);
512 // Match vgprs and sgprs
513 if (RegName[0] != 's' && RegName[0] != 'v')
516 bool IsVgpr = RegName[0] == 'v';
518 unsigned RegIndexInClass;
519 if (RegName.size() > 1) {
520 // We have a 32-bit register
522 if (RegName.substr(1).getAsInteger(10, RegIndexInClass))
526 // We have a register greater than 32-bits.
528 int64_t RegLo, RegHi;
530 if (getLexer().isNot(AsmToken::LBrac))
534 if (getParser().parseAbsoluteExpression(RegLo))
537 if (getLexer().isNot(AsmToken::Colon))
541 if (getParser().parseAbsoluteExpression(RegHi))
544 if (getLexer().isNot(AsmToken::RBrac))
548 RegWidth = (RegHi - RegLo) + 1;
550 // VGPR registers aren't aligned.
551 RegIndexInClass = RegLo;
553 // SGPR registers are aligned. Max alignment is 4 dwords.
554 unsigned Size = std::min(RegWidth, 4u);
555 if (RegLo % Size != 0)
558 RegIndexInClass = RegLo / Size;
562 const MCRegisterInfo *TRI = getContext().getRegisterInfo();
563 int RCID = getRegClass(IsVgpr, RegWidth);
567 const MCRegisterClass RC = TRI->getRegClass(RCID);
568 if (RegIndexInClass >= RC.getNumRegs())
571 RegNo = RC.getRegister(RegIndexInClass);
572 return !subtargetHasRegister(*TRI, RegNo);
575 unsigned AMDGPUAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
577 uint64_t TSFlags = MII.get(Inst.getOpcode()).TSFlags;
579 if ((getForcedEncodingSize() == 32 && (TSFlags & SIInstrFlags::VOP3)) ||
580 (getForcedEncodingSize() == 64 && !(TSFlags & SIInstrFlags::VOP3)))
581 return Match_InvalidOperand;
583 if ((TSFlags & SIInstrFlags::VOP3) &&
584 (TSFlags & SIInstrFlags::VOPAsmPrefer32Bit) &&
585 getForcedEncodingSize() != 64)
586 return Match_PreferE32;
588 return Match_Success;
592 bool AMDGPUAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
593 OperandVector &Operands,
596 bool MatchingInlineAsm) {
599 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm)) {
603 Out.EmitInstruction(Inst, STI);
605 case Match_MissingFeature:
606 return Error(IDLoc, "instruction not supported on this GPU");
608 case Match_MnemonicFail:
609 return Error(IDLoc, "unrecognized instruction mnemonic");
611 case Match_InvalidOperand: {
612 SMLoc ErrorLoc = IDLoc;
613 if (ErrorInfo != ~0ULL) {
614 if (ErrorInfo >= Operands.size()) {
615 if (isForcedVOP3()) {
616 // If 64-bit encoding has been forced we can end up with no
617 // clamp or omod operands if none of the registers have modifiers,
618 // so we need to add these to the operand list.
619 AMDGPUOperand &LastOp =
620 ((AMDGPUOperand &)*Operands[Operands.size() - 1]);
621 if (LastOp.isRegKind() ||
623 LastOp.getImmTy() != AMDGPUOperand::ImmTyNone)) {
624 SMLoc S = Parser.getTok().getLoc();
625 Operands.push_back(AMDGPUOperand::CreateImm(0, S,
626 AMDGPUOperand::ImmTyClamp));
627 Operands.push_back(AMDGPUOperand::CreateImm(0, S,
628 AMDGPUOperand::ImmTyOMod));
629 bool Res = MatchAndEmitInstruction(IDLoc, Opcode, Operands,
637 return Error(IDLoc, "too few operands for instruction");
640 ErrorLoc = ((AMDGPUOperand &)*Operands[ErrorInfo]).getStartLoc();
641 if (ErrorLoc == SMLoc())
644 return Error(ErrorLoc, "invalid operand for instruction");
646 case Match_PreferE32:
647 return Error(IDLoc, "internal error: instruction without _e64 suffix "
648 "should be encoded as e32");
650 llvm_unreachable("Implement any new match types added!");
653 bool AMDGPUAsmParser::ParseDirectiveMajorMinor(uint32_t &Major,
655 if (getLexer().isNot(AsmToken::Integer))
656 return TokError("invalid major version");
658 Major = getLexer().getTok().getIntVal();
661 if (getLexer().isNot(AsmToken::Comma))
662 return TokError("minor version number required, comma expected");
665 if (getLexer().isNot(AsmToken::Integer))
666 return TokError("invalid minor version");
668 Minor = getLexer().getTok().getIntVal();
674 bool AMDGPUAsmParser::ParseDirectiveHSACodeObjectVersion() {
679 if (ParseDirectiveMajorMinor(Major, Minor))
682 getTargetStreamer().EmitDirectiveHSACodeObjectVersion(Major, Minor);
686 bool AMDGPUAsmParser::ParseDirectiveHSACodeObjectISA() {
691 StringRef VendorName;
694 // If this directive has no arguments, then use the ISA version for the
696 if (getLexer().is(AsmToken::EndOfStatement)) {
697 AMDGPU::IsaVersion Isa = AMDGPU::getIsaVersion(STI.getFeatureBits());
698 getTargetStreamer().EmitDirectiveHSACodeObjectISA(Isa.Major, Isa.Minor,
705 if (ParseDirectiveMajorMinor(Major, Minor))
708 if (getLexer().isNot(AsmToken::Comma))
709 return TokError("stepping version number required, comma expected");
712 if (getLexer().isNot(AsmToken::Integer))
713 return TokError("invalid stepping version");
715 Stepping = getLexer().getTok().getIntVal();
718 if (getLexer().isNot(AsmToken::Comma))
719 return TokError("vendor name required, comma expected");
722 if (getLexer().isNot(AsmToken::String))
723 return TokError("invalid vendor name");
725 VendorName = getLexer().getTok().getStringContents();
728 if (getLexer().isNot(AsmToken::Comma))
729 return TokError("arch name required, comma expected");
732 if (getLexer().isNot(AsmToken::String))
733 return TokError("invalid arch name");
735 ArchName = getLexer().getTok().getStringContents();
738 getTargetStreamer().EmitDirectiveHSACodeObjectISA(Major, Minor, Stepping,
739 VendorName, ArchName);
743 bool AMDGPUAsmParser::ParseAMDKernelCodeTValue(StringRef ID,
744 amd_kernel_code_t &Header) {
746 if (getLexer().isNot(AsmToken::Equal))
747 return TokError("expected '='");
750 if (getLexer().isNot(AsmToken::Integer))
751 return TokError("amd_kernel_code_t values must be integers");
753 uint64_t Value = getLexer().getTok().getIntVal();
756 if (ID == "kernel_code_version_major")
757 Header.amd_kernel_code_version_major = Value;
758 else if (ID == "kernel_code_version_minor")
759 Header.amd_kernel_code_version_minor = Value;
760 else if (ID == "machine_kind")
761 Header.amd_machine_kind = Value;
762 else if (ID == "machine_version_major")
763 Header.amd_machine_version_major = Value;
764 else if (ID == "machine_version_minor")
765 Header.amd_machine_version_minor = Value;
766 else if (ID == "machine_version_stepping")
767 Header.amd_machine_version_stepping = Value;
768 else if (ID == "kernel_code_entry_byte_offset")
769 Header.kernel_code_entry_byte_offset = Value;
770 else if (ID == "kernel_code_prefetch_byte_size")
771 Header.kernel_code_prefetch_byte_size = Value;
772 else if (ID == "max_scratch_backing_memory_byte_size")
773 Header.max_scratch_backing_memory_byte_size = Value;
774 else if (ID == "compute_pgm_rsrc1_vgprs")
775 Header.compute_pgm_resource_registers |= S_00B848_VGPRS(Value);
776 else if (ID == "compute_pgm_rsrc1_sgprs")
777 Header.compute_pgm_resource_registers |= S_00B848_SGPRS(Value);
778 else if (ID == "compute_pgm_rsrc1_priority")
779 Header.compute_pgm_resource_registers |= S_00B848_PRIORITY(Value);
780 else if (ID == "compute_pgm_rsrc1_float_mode")
781 Header.compute_pgm_resource_registers |= S_00B848_FLOAT_MODE(Value);
782 else if (ID == "compute_pgm_rsrc1_priv")
783 Header.compute_pgm_resource_registers |= S_00B848_PRIV(Value);
784 else if (ID == "compute_pgm_rsrc1_dx10_clamp")
785 Header.compute_pgm_resource_registers |= S_00B848_DX10_CLAMP(Value);
786 else if (ID == "compute_pgm_rsrc1_debug_mode")
787 Header.compute_pgm_resource_registers |= S_00B848_DEBUG_MODE(Value);
788 else if (ID == "compute_pgm_rsrc1_ieee_mode")
789 Header.compute_pgm_resource_registers |= S_00B848_IEEE_MODE(Value);
790 else if (ID == "compute_pgm_rsrc2_scratch_en")
791 Header.compute_pgm_resource_registers |= (S_00B84C_SCRATCH_EN(Value) << 32);
792 else if (ID == "compute_pgm_rsrc2_user_sgpr")
793 Header.compute_pgm_resource_registers |= (S_00B84C_USER_SGPR(Value) << 32);
794 else if (ID == "compute_pgm_rsrc2_tgid_x_en")
795 Header.compute_pgm_resource_registers |= (S_00B84C_TGID_X_EN(Value) << 32);
796 else if (ID == "compute_pgm_rsrc2_tgid_y_en")
797 Header.compute_pgm_resource_registers |= (S_00B84C_TGID_Y_EN(Value) << 32);
798 else if (ID == "compute_pgm_rsrc2_tgid_z_en")
799 Header.compute_pgm_resource_registers |= (S_00B84C_TGID_Z_EN(Value) << 32);
800 else if (ID == "compute_pgm_rsrc2_tg_size_en")
801 Header.compute_pgm_resource_registers |= (S_00B84C_TG_SIZE_EN(Value) << 32);
802 else if (ID == "compute_pgm_rsrc2_tidig_comp_cnt")
803 Header.compute_pgm_resource_registers |=
804 (S_00B84C_TIDIG_COMP_CNT(Value) << 32);
805 else if (ID == "compute_pgm_rsrc2_excp_en_msb")
806 Header.compute_pgm_resource_registers |=
807 (S_00B84C_EXCP_EN_MSB(Value) << 32);
808 else if (ID == "compute_pgm_rsrc2_lds_size")
809 Header.compute_pgm_resource_registers |= (S_00B84C_LDS_SIZE(Value) << 32);
810 else if (ID == "compute_pgm_rsrc2_excp_en")
811 Header.compute_pgm_resource_registers |= (S_00B84C_EXCP_EN(Value) << 32);
812 else if (ID == "compute_pgm_resource_registers")
813 Header.compute_pgm_resource_registers = Value;
814 else if (ID == "enable_sgpr_private_segment_buffer")
815 Header.code_properties |=
816 (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER_SHIFT);
817 else if (ID == "enable_sgpr_dispatch_ptr")
818 Header.code_properties |=
819 (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR_SHIFT);
820 else if (ID == "enable_sgpr_queue_ptr")
821 Header.code_properties |=
822 (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR_SHIFT);
823 else if (ID == "enable_sgpr_kernarg_segment_ptr")
824 Header.code_properties |=
825 (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR_SHIFT);
826 else if (ID == "enable_sgpr_dispatch_id")
827 Header.code_properties |=
828 (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID_SHIFT);
829 else if (ID == "enable_sgpr_flat_scratch_init")
830 Header.code_properties |=
831 (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT_SHIFT);
832 else if (ID == "enable_sgpr_private_segment_size")
833 Header.code_properties |=
834 (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_SIZE_SHIFT);
835 else if (ID == "enable_sgpr_grid_workgroup_count_x")
836 Header.code_properties |=
837 (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_X_SHIFT);
838 else if (ID == "enable_sgpr_grid_workgroup_count_y")
839 Header.code_properties |=
840 (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Y_SHIFT);
841 else if (ID == "enable_sgpr_grid_workgroup_count_z")
842 Header.code_properties |=
843 (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Z_SHIFT);
844 else if (ID == "enable_ordered_append_gds")
845 Header.code_properties |=
846 (Value << AMD_CODE_PROPERTY_ENABLE_ORDERED_APPEND_GDS_SHIFT);
847 else if (ID == "private_element_size")
848 Header.code_properties |=
849 (Value << AMD_CODE_PROPERTY_PRIVATE_ELEMENT_SIZE_SHIFT);
850 else if (ID == "is_ptr64")
851 Header.code_properties |=
852 (Value << AMD_CODE_PROPERTY_IS_PTR64_SHIFT);
853 else if (ID == "is_dynamic_callstack")
854 Header.code_properties |=
855 (Value << AMD_CODE_PROPERTY_IS_DYNAMIC_CALLSTACK_SHIFT);
856 else if (ID == "is_debug_enabled")
857 Header.code_properties |=
858 (Value << AMD_CODE_PROPERTY_IS_DEBUG_SUPPORTED_SHIFT);
859 else if (ID == "is_xnack_enabled")
860 Header.code_properties |=
861 (Value << AMD_CODE_PROPERTY_IS_XNACK_SUPPORTED_SHIFT);
862 else if (ID == "workitem_private_segment_byte_size")
863 Header.workitem_private_segment_byte_size = Value;
864 else if (ID == "workgroup_group_segment_byte_size")
865 Header.workgroup_group_segment_byte_size = Value;
866 else if (ID == "gds_segment_byte_size")
867 Header.gds_segment_byte_size = Value;
868 else if (ID == "kernarg_segment_byte_size")
869 Header.kernarg_segment_byte_size = Value;
870 else if (ID == "workgroup_fbarrier_count")
871 Header.workgroup_fbarrier_count = Value;
872 else if (ID == "wavefront_sgpr_count")
873 Header.wavefront_sgpr_count = Value;
874 else if (ID == "workitem_vgpr_count")
875 Header.workitem_vgpr_count = Value;
876 else if (ID == "reserved_vgpr_first")
877 Header.reserved_vgpr_first = Value;
878 else if (ID == "reserved_vgpr_count")
879 Header.reserved_vgpr_count = Value;
880 else if (ID == "reserved_sgpr_first")
881 Header.reserved_sgpr_first = Value;
882 else if (ID == "reserved_sgpr_count")
883 Header.reserved_sgpr_count = Value;
884 else if (ID == "debug_wavefront_private_segment_offset_sgpr")
885 Header.debug_wavefront_private_segment_offset_sgpr = Value;
886 else if (ID == "debug_private_segment_buffer_sgpr")
887 Header.debug_private_segment_buffer_sgpr = Value;
888 else if (ID == "kernarg_segment_alignment")
889 Header.kernarg_segment_alignment = Value;
890 else if (ID == "group_segment_alignment")
891 Header.group_segment_alignment = Value;
892 else if (ID == "private_segment_alignment")
893 Header.private_segment_alignment = Value;
894 else if (ID == "wavefront_size")
895 Header.wavefront_size = Value;
896 else if (ID == "call_convention")
897 Header.call_convention = Value;
898 else if (ID == "runtime_loader_kernel_symbol")
899 Header.runtime_loader_kernel_symbol = Value;
901 return TokError("amd_kernel_code_t value not recognized.");
906 bool AMDGPUAsmParser::ParseDirectiveAMDKernelCodeT() {
908 amd_kernel_code_t Header;
909 AMDGPU::initDefaultAMDKernelCodeT(Header, STI.getFeatureBits());
913 if (getLexer().isNot(AsmToken::EndOfStatement))
914 return TokError("amd_kernel_code_t values must begin on a new line");
916 // Lex EndOfStatement. This is in a while loop, because lexing a comment
917 // will set the current token to EndOfStatement.
918 while(getLexer().is(AsmToken::EndOfStatement))
921 if (getLexer().isNot(AsmToken::Identifier))
922 return TokError("expected value identifier or .end_amd_kernel_code_t");
924 StringRef ID = getLexer().getTok().getIdentifier();
927 if (ID == ".end_amd_kernel_code_t")
930 if (ParseAMDKernelCodeTValue(ID, Header))
934 getTargetStreamer().EmitAMDKernelCodeT(Header);
939 bool AMDGPUAsmParser::ParseSectionDirectiveHSAText() {
940 getParser().getStreamer().SwitchSection(
941 AMDGPU::getHSATextSection(getContext()));
945 bool AMDGPUAsmParser::ParseDirective(AsmToken DirectiveID) {
946 StringRef IDVal = DirectiveID.getString();
948 if (IDVal == ".hsa_code_object_version")
949 return ParseDirectiveHSACodeObjectVersion();
951 if (IDVal == ".hsa_code_object_isa")
952 return ParseDirectiveHSACodeObjectISA();
954 if (IDVal == ".amd_kernel_code_t")
955 return ParseDirectiveAMDKernelCodeT();
957 if (IDVal == ".hsatext" || IDVal == ".text")
958 return ParseSectionDirectiveHSAText();
963 bool AMDGPUAsmParser::subtargetHasRegister(const MCRegisterInfo &MRI,
964 unsigned RegNo) const {
968 // VI only has 102 SGPRs, so make sure we aren't trying to use the 2 more that
970 for (MCRegAliasIterator R(AMDGPU::SGPR102_SGPR103, &MRI, true);
979 static bool operandsHaveModifiers(const OperandVector &Operands) {
981 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
982 const AMDGPUOperand &Op = ((AMDGPUOperand&)*Operands[i]);
983 if (Op.isRegKind() && Op.hasModifiers())
985 if (Op.isImm() && (Op.getImmTy() == AMDGPUOperand::ImmTyOMod ||
986 Op.getImmTy() == AMDGPUOperand::ImmTyClamp))
992 AMDGPUAsmParser::OperandMatchResultTy
993 AMDGPUAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
995 // Try to parse with a custom parser
996 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
998 // If we successfully parsed the operand or if there as an error parsing,
1001 // If we are parsing after we reach EndOfStatement then this means we
1002 // are appending default values to the Operands list. This is only done
1003 // by custom parser, so we shouldn't continue on to the generic parsing.
1004 if (ResTy == MatchOperand_Success || ResTy == MatchOperand_ParseFail ||
1005 getLexer().is(AsmToken::EndOfStatement))
1008 bool Negate = false, Abs = false;
1009 if (getLexer().getKind()== AsmToken::Minus) {
1014 if (getLexer().getKind() == AsmToken::Pipe) {
1019 switch(getLexer().getKind()) {
1020 case AsmToken::Integer: {
1021 SMLoc S = Parser.getTok().getLoc();
1023 if (getParser().parseAbsoluteExpression(IntVal))
1024 return MatchOperand_ParseFail;
1025 if (!isInt<32>(IntVal) && !isUInt<32>(IntVal)) {
1026 Error(S, "invalid immediate: only 32-bit values are legal");
1027 return MatchOperand_ParseFail;
1032 Operands.push_back(AMDGPUOperand::CreateImm(IntVal, S));
1033 return MatchOperand_Success;
1035 case AsmToken::Real: {
1036 // FIXME: We should emit an error if a double precisions floating-point
1037 // value is used. I'm not sure the best way to detect this.
1038 SMLoc S = Parser.getTok().getLoc();
1040 if (getParser().parseAbsoluteExpression(IntVal))
1041 return MatchOperand_ParseFail;
1043 APFloat F((float)BitsToDouble(IntVal));
1047 AMDGPUOperand::CreateImm(F.bitcastToAPInt().getZExtValue(), S));
1048 return MatchOperand_Success;
1050 case AsmToken::Identifier: {
1053 if (!ParseRegister(RegNo, S, E)) {
1055 bool HasModifiers = operandsHaveModifiers(Operands);
1056 unsigned Modifiers = 0;
1062 if (getLexer().getKind() != AsmToken::Pipe)
1063 return MatchOperand_ParseFail;
1068 if (Modifiers && !HasModifiers) {
1069 // We are adding a modifier to src1 or src2 and previous sources
1070 // don't have modifiers, so we need to go back and empty modifers
1071 // for each previous source.
1072 for (unsigned PrevRegIdx = Operands.size() - 1; PrevRegIdx > 1;
1075 AMDGPUOperand &RegOp = ((AMDGPUOperand&)*Operands[PrevRegIdx]);
1076 RegOp.setModifiers(0);
1081 Operands.push_back(AMDGPUOperand::CreateReg(
1082 RegNo, S, E, getContext().getRegisterInfo(),
1085 if (HasModifiers || Modifiers) {
1086 AMDGPUOperand &RegOp = ((AMDGPUOperand&)*Operands[Operands.size() - 1]);
1087 RegOp.setModifiers(Modifiers);
1091 Operands.push_back(AMDGPUOperand::CreateToken(Parser.getTok().getString(),
1095 return MatchOperand_Success;
1098 return MatchOperand_NoMatch;
1102 bool AMDGPUAsmParser::ParseInstruction(ParseInstructionInfo &Info,
1104 SMLoc NameLoc, OperandVector &Operands) {
1106 // Clear any forced encodings from the previous instruction.
1107 setForcedEncodingSize(0);
1109 if (Name.endswith("_e64"))
1110 setForcedEncodingSize(64);
1111 else if (Name.endswith("_e32"))
1112 setForcedEncodingSize(32);
1114 // Add the instruction mnemonic
1115 Operands.push_back(AMDGPUOperand::CreateToken(Name, NameLoc));
1117 while (!getLexer().is(AsmToken::EndOfStatement)) {
1118 AMDGPUAsmParser::OperandMatchResultTy Res = parseOperand(Operands, Name);
1120 // Eat the comma or space if there is one.
1121 if (getLexer().is(AsmToken::Comma))
1125 case MatchOperand_Success: break;
1126 case MatchOperand_ParseFail: return Error(getLexer().getLoc(),
1127 "failed parsing operand.");
1128 case MatchOperand_NoMatch: return Error(getLexer().getLoc(),
1129 "not a valid operand.");
1133 // Once we reach end of statement, continue parsing so we can add default
1134 // values for optional arguments.
1135 AMDGPUAsmParser::OperandMatchResultTy Res;
1136 while ((Res = parseOperand(Operands, Name)) != MatchOperand_NoMatch) {
1137 if (Res != MatchOperand_Success)
1138 return Error(getLexer().getLoc(), "failed parsing operand.");
1143 //===----------------------------------------------------------------------===//
1144 // Utility functions
1145 //===----------------------------------------------------------------------===//
1147 AMDGPUAsmParser::OperandMatchResultTy
1148 AMDGPUAsmParser::parseIntWithPrefix(const char *Prefix, int64_t &Int,
1151 // We are at the end of the statement, and this is a default argument, so
1152 // use a default value.
1153 if (getLexer().is(AsmToken::EndOfStatement)) {
1155 return MatchOperand_Success;
1158 switch(getLexer().getKind()) {
1159 default: return MatchOperand_NoMatch;
1160 case AsmToken::Identifier: {
1161 StringRef OffsetName = Parser.getTok().getString();
1162 if (!OffsetName.equals(Prefix))
1163 return MatchOperand_NoMatch;
1166 if (getLexer().isNot(AsmToken::Colon))
1167 return MatchOperand_ParseFail;
1170 if (getLexer().isNot(AsmToken::Integer))
1171 return MatchOperand_ParseFail;
1173 if (getParser().parseAbsoluteExpression(Int))
1174 return MatchOperand_ParseFail;
1178 return MatchOperand_Success;
1181 AMDGPUAsmParser::OperandMatchResultTy
1182 AMDGPUAsmParser::parseIntWithPrefix(const char *Prefix, OperandVector &Operands,
1183 enum AMDGPUOperand::ImmTy ImmTy) {
1185 SMLoc S = Parser.getTok().getLoc();
1188 AMDGPUAsmParser::OperandMatchResultTy Res = parseIntWithPrefix(Prefix, Offset);
1189 if (Res != MatchOperand_Success)
1192 Operands.push_back(AMDGPUOperand::CreateImm(Offset, S, ImmTy));
1193 return MatchOperand_Success;
1196 AMDGPUAsmParser::OperandMatchResultTy
1197 AMDGPUAsmParser::parseNamedBit(const char *Name, OperandVector &Operands,
1198 enum AMDGPUOperand::ImmTy ImmTy) {
1200 SMLoc S = Parser.getTok().getLoc();
1202 // We are at the end of the statement, and this is a default argument, so
1203 // use a default value.
1204 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1205 switch(getLexer().getKind()) {
1206 case AsmToken::Identifier: {
1207 StringRef Tok = Parser.getTok().getString();
1211 } else if (Tok.startswith("no") && Tok.endswith(Name)) {
1215 return MatchOperand_NoMatch;
1220 return MatchOperand_NoMatch;
1224 Operands.push_back(AMDGPUOperand::CreateImm(Bit, S, ImmTy));
1225 return MatchOperand_Success;
1228 static bool operandsHasOptionalOp(const OperandVector &Operands,
1229 const OptionalOperand &OOp) {
1230 for (unsigned i = 0; i < Operands.size(); i++) {
1231 const AMDGPUOperand &ParsedOp = ((const AMDGPUOperand &)*Operands[i]);
1232 if ((ParsedOp.isImm() && ParsedOp.getImmTy() == OOp.Type) ||
1233 (ParsedOp.isToken() && ParsedOp.getToken() == OOp.Name))
1240 AMDGPUAsmParser::OperandMatchResultTy
1241 AMDGPUAsmParser::parseOptionalOps(const ArrayRef<OptionalOperand> &OptionalOps,
1242 OperandVector &Operands) {
1243 SMLoc S = Parser.getTok().getLoc();
1244 for (const OptionalOperand &Op : OptionalOps) {
1245 if (operandsHasOptionalOp(Operands, Op))
1247 AMDGPUAsmParser::OperandMatchResultTy Res;
1250 Res = parseNamedBit(Op.Name, Operands, Op.Type);
1251 if (Res == MatchOperand_NoMatch)
1256 Res = parseIntWithPrefix(Op.Name, Value, Op.Default);
1258 if (Res == MatchOperand_NoMatch)
1261 if (Res != MatchOperand_Success)
1264 if (Op.ConvertResult && !Op.ConvertResult(Value)) {
1265 return MatchOperand_ParseFail;
1268 Operands.push_back(AMDGPUOperand::CreateImm(Value, S, Op.Type));
1269 return MatchOperand_Success;
1271 return MatchOperand_NoMatch;
1274 //===----------------------------------------------------------------------===//
1276 //===----------------------------------------------------------------------===//
1278 static const OptionalOperand DSOptionalOps [] = {
1279 {"offset", AMDGPUOperand::ImmTyOffset, false, 0, nullptr},
1280 {"gds", AMDGPUOperand::ImmTyGDS, true, 0, nullptr}
1283 static const OptionalOperand DSOptionalOpsOff01 [] = {
1284 {"offset0", AMDGPUOperand::ImmTyDSOffset0, false, 0, nullptr},
1285 {"offset1", AMDGPUOperand::ImmTyDSOffset1, false, 0, nullptr},
1286 {"gds", AMDGPUOperand::ImmTyGDS, true, 0, nullptr}
1289 AMDGPUAsmParser::OperandMatchResultTy
1290 AMDGPUAsmParser::parseDSOptionalOps(OperandVector &Operands) {
1291 return parseOptionalOps(DSOptionalOps, Operands);
1293 AMDGPUAsmParser::OperandMatchResultTy
1294 AMDGPUAsmParser::parseDSOff01OptionalOps(OperandVector &Operands) {
1295 return parseOptionalOps(DSOptionalOpsOff01, Operands);
1298 AMDGPUAsmParser::OperandMatchResultTy
1299 AMDGPUAsmParser::parseDSOffsetOptional(OperandVector &Operands) {
1300 SMLoc S = Parser.getTok().getLoc();
1301 AMDGPUAsmParser::OperandMatchResultTy Res =
1302 parseIntWithPrefix("offset", Operands, AMDGPUOperand::ImmTyOffset);
1303 if (Res == MatchOperand_NoMatch) {
1304 Operands.push_back(AMDGPUOperand::CreateImm(0, S,
1305 AMDGPUOperand::ImmTyOffset));
1306 Res = MatchOperand_Success;
1311 bool AMDGPUOperand::isDSOffset() const {
1312 return isImm() && isUInt<16>(getImm());
1315 bool AMDGPUOperand::isDSOffset01() const {
1316 return isImm() && isUInt<8>(getImm());
1319 void AMDGPUAsmParser::cvtDSOffset01(MCInst &Inst,
1320 const OperandVector &Operands) {
1322 std::map<enum AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1324 for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
1325 AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1327 // Add the register arguments
1329 Op.addRegOperands(Inst, 1);
1333 // Handle optional arguments
1334 OptionalIdx[Op.getImmTy()] = i;
1337 unsigned Offset0Idx = OptionalIdx[AMDGPUOperand::ImmTyDSOffset0];
1338 unsigned Offset1Idx = OptionalIdx[AMDGPUOperand::ImmTyDSOffset1];
1339 unsigned GDSIdx = OptionalIdx[AMDGPUOperand::ImmTyGDS];
1341 ((AMDGPUOperand &)*Operands[Offset0Idx]).addImmOperands(Inst, 1); // offset0
1342 ((AMDGPUOperand &)*Operands[Offset1Idx]).addImmOperands(Inst, 1); // offset1
1343 ((AMDGPUOperand &)*Operands[GDSIdx]).addImmOperands(Inst, 1); // gds
1344 Inst.addOperand(MCOperand::createReg(AMDGPU::M0)); // m0
1347 void AMDGPUAsmParser::cvtDS(MCInst &Inst, const OperandVector &Operands) {
1349 std::map<enum AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1350 bool GDSOnly = false;
1352 for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
1353 AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1355 // Add the register arguments
1357 Op.addRegOperands(Inst, 1);
1361 if (Op.isToken() && Op.getToken() == "gds") {
1366 // Handle optional arguments
1367 OptionalIdx[Op.getImmTy()] = i;
1370 unsigned OffsetIdx = OptionalIdx[AMDGPUOperand::ImmTyOffset];
1371 ((AMDGPUOperand &)*Operands[OffsetIdx]).addImmOperands(Inst, 1); // offset
1374 unsigned GDSIdx = OptionalIdx[AMDGPUOperand::ImmTyGDS];
1375 ((AMDGPUOperand &)*Operands[GDSIdx]).addImmOperands(Inst, 1); // gds
1377 Inst.addOperand(MCOperand::createReg(AMDGPU::M0)); // m0
1381 //===----------------------------------------------------------------------===//
1383 //===----------------------------------------------------------------------===//
1385 bool AMDGPUAsmParser::parseCnt(int64_t &IntVal) {
1386 StringRef CntName = Parser.getTok().getString();
1390 if (getLexer().isNot(AsmToken::LParen))
1394 if (getLexer().isNot(AsmToken::Integer))
1397 if (getParser().parseAbsoluteExpression(CntVal))
1400 if (getLexer().isNot(AsmToken::RParen))
1404 if (getLexer().is(AsmToken::Amp) || getLexer().is(AsmToken::Comma))
1410 if (CntName == "vmcnt") {
1413 } else if (CntName == "expcnt") {
1416 } else if (CntName == "lgkmcnt") {
1423 IntVal &= ~(CntMask << CntShift);
1424 IntVal |= (CntVal << CntShift);
1428 AMDGPUAsmParser::OperandMatchResultTy
1429 AMDGPUAsmParser::parseSWaitCntOps(OperandVector &Operands) {
1430 // Disable all counters by default.
1434 int64_t CntVal = 0x77f;
1435 SMLoc S = Parser.getTok().getLoc();
1437 switch(getLexer().getKind()) {
1438 default: return MatchOperand_ParseFail;
1439 case AsmToken::Integer:
1440 // The operand can be an integer value.
1441 if (getParser().parseAbsoluteExpression(CntVal))
1442 return MatchOperand_ParseFail;
1445 case AsmToken::Identifier:
1447 if (parseCnt(CntVal))
1448 return MatchOperand_ParseFail;
1449 } while(getLexer().isNot(AsmToken::EndOfStatement));
1452 Operands.push_back(AMDGPUOperand::CreateImm(CntVal, S));
1453 return MatchOperand_Success;
1456 bool AMDGPUOperand::isSWaitCnt() const {
1460 //===----------------------------------------------------------------------===//
1461 // sopp branch targets
1462 //===----------------------------------------------------------------------===//
1464 AMDGPUAsmParser::OperandMatchResultTy
1465 AMDGPUAsmParser::parseSOppBrTarget(OperandVector &Operands) {
1466 SMLoc S = Parser.getTok().getLoc();
1468 switch (getLexer().getKind()) {
1469 default: return MatchOperand_ParseFail;
1470 case AsmToken::Integer: {
1472 if (getParser().parseAbsoluteExpression(Imm))
1473 return MatchOperand_ParseFail;
1474 Operands.push_back(AMDGPUOperand::CreateImm(Imm, S));
1475 return MatchOperand_Success;
1478 case AsmToken::Identifier:
1479 Operands.push_back(AMDGPUOperand::CreateExpr(
1480 MCSymbolRefExpr::create(getContext().getOrCreateSymbol(
1481 Parser.getTok().getString()), getContext()), S));
1483 return MatchOperand_Success;
1487 //===----------------------------------------------------------------------===//
1489 //===----------------------------------------------------------------------===//
1491 static const OptionalOperand FlatOptionalOps [] = {
1492 {"glc", AMDGPUOperand::ImmTyGLC, true, 0, nullptr},
1493 {"slc", AMDGPUOperand::ImmTySLC, true, 0, nullptr},
1494 {"tfe", AMDGPUOperand::ImmTyTFE, true, 0, nullptr}
1497 static const OptionalOperand FlatAtomicOptionalOps [] = {
1498 {"slc", AMDGPUOperand::ImmTySLC, true, 0, nullptr},
1499 {"tfe", AMDGPUOperand::ImmTyTFE, true, 0, nullptr}
1502 AMDGPUAsmParser::OperandMatchResultTy
1503 AMDGPUAsmParser::parseFlatOptionalOps(OperandVector &Operands) {
1504 return parseOptionalOps(FlatOptionalOps, Operands);
1507 AMDGPUAsmParser::OperandMatchResultTy
1508 AMDGPUAsmParser::parseFlatAtomicOptionalOps(OperandVector &Operands) {
1509 return parseOptionalOps(FlatAtomicOptionalOps, Operands);
1512 void AMDGPUAsmParser::cvtFlat(MCInst &Inst,
1513 const OperandVector &Operands) {
1514 std::map<AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1516 for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
1517 AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1519 // Add the register arguments
1521 Op.addRegOperands(Inst, 1);
1525 // Handle 'glc' token which is sometimes hard-coded into the
1526 // asm string. There are no MCInst operands for these.
1530 // Handle optional arguments
1531 OptionalIdx[Op.getImmTy()] = i;
1535 // flat atomic instructions don't have a glc argument.
1536 if (OptionalIdx.count(AMDGPUOperand::ImmTyGLC)) {
1537 unsigned GLCIdx = OptionalIdx[AMDGPUOperand::ImmTyGLC];
1538 ((AMDGPUOperand &)*Operands[GLCIdx]).addImmOperands(Inst, 1);
1541 unsigned SLCIdx = OptionalIdx[AMDGPUOperand::ImmTySLC];
1542 unsigned TFEIdx = OptionalIdx[AMDGPUOperand::ImmTyTFE];
1544 ((AMDGPUOperand &)*Operands[SLCIdx]).addImmOperands(Inst, 1);
1545 ((AMDGPUOperand &)*Operands[TFEIdx]).addImmOperands(Inst, 1);
1548 //===----------------------------------------------------------------------===//
1550 //===----------------------------------------------------------------------===//
1552 static const OptionalOperand MubufOptionalOps [] = {
1553 {"offset", AMDGPUOperand::ImmTyOffset, false, 0, nullptr},
1554 {"glc", AMDGPUOperand::ImmTyGLC, true, 0, nullptr},
1555 {"slc", AMDGPUOperand::ImmTySLC, true, 0, nullptr},
1556 {"tfe", AMDGPUOperand::ImmTyTFE, true, 0, nullptr}
1559 AMDGPUAsmParser::OperandMatchResultTy
1560 AMDGPUAsmParser::parseMubufOptionalOps(OperandVector &Operands) {
1561 return parseOptionalOps(MubufOptionalOps, Operands);
1564 AMDGPUAsmParser::OperandMatchResultTy
1565 AMDGPUAsmParser::parseOffset(OperandVector &Operands) {
1566 return parseIntWithPrefix("offset", Operands);
1569 AMDGPUAsmParser::OperandMatchResultTy
1570 AMDGPUAsmParser::parseGLC(OperandVector &Operands) {
1571 return parseNamedBit("glc", Operands);
1574 AMDGPUAsmParser::OperandMatchResultTy
1575 AMDGPUAsmParser::parseSLC(OperandVector &Operands) {
1576 return parseNamedBit("slc", Operands);
1579 AMDGPUAsmParser::OperandMatchResultTy
1580 AMDGPUAsmParser::parseTFE(OperandVector &Operands) {
1581 return parseNamedBit("tfe", Operands);
1584 bool AMDGPUOperand::isMubufOffset() const {
1585 return isImm() && isUInt<12>(getImm());
1588 void AMDGPUAsmParser::cvtMubuf(MCInst &Inst,
1589 const OperandVector &Operands) {
1590 std::map<enum AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1592 for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
1593 AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1595 // Add the register arguments
1597 Op.addRegOperands(Inst, 1);
1601 // Handle the case where soffset is an immediate
1602 if (Op.isImm() && Op.getImmTy() == AMDGPUOperand::ImmTyNone) {
1603 Op.addImmOperands(Inst, 1);
1607 // Handle tokens like 'offen' which are sometimes hard-coded into the
1608 // asm string. There are no MCInst operands for these.
1614 // Handle optional arguments
1615 OptionalIdx[Op.getImmTy()] = i;
1618 assert(OptionalIdx.size() == 4);
1620 unsigned OffsetIdx = OptionalIdx[AMDGPUOperand::ImmTyOffset];
1621 unsigned GLCIdx = OptionalIdx[AMDGPUOperand::ImmTyGLC];
1622 unsigned SLCIdx = OptionalIdx[AMDGPUOperand::ImmTySLC];
1623 unsigned TFEIdx = OptionalIdx[AMDGPUOperand::ImmTyTFE];
1625 ((AMDGPUOperand &)*Operands[OffsetIdx]).addImmOperands(Inst, 1);
1626 ((AMDGPUOperand &)*Operands[GLCIdx]).addImmOperands(Inst, 1);
1627 ((AMDGPUOperand &)*Operands[SLCIdx]).addImmOperands(Inst, 1);
1628 ((AMDGPUOperand &)*Operands[TFEIdx]).addImmOperands(Inst, 1);
1631 //===----------------------------------------------------------------------===//
1633 //===----------------------------------------------------------------------===//
1635 AMDGPUAsmParser::OperandMatchResultTy
1636 AMDGPUAsmParser::parseDMask(OperandVector &Operands) {
1637 return parseIntWithPrefix("dmask", Operands);
1640 AMDGPUAsmParser::OperandMatchResultTy
1641 AMDGPUAsmParser::parseUNorm(OperandVector &Operands) {
1642 return parseNamedBit("unorm", Operands);
1645 AMDGPUAsmParser::OperandMatchResultTy
1646 AMDGPUAsmParser::parseR128(OperandVector &Operands) {
1647 return parseNamedBit("r128", Operands);
1650 //===----------------------------------------------------------------------===//
1652 //===----------------------------------------------------------------------===//
1654 bool AMDGPUOperand::isSMRDOffset() const {
1656 // FIXME: Support 20-bit offsets on VI. We need to to pass subtarget
1657 // information here.
1658 return isImm() && isUInt<8>(getImm());
1661 bool AMDGPUOperand::isSMRDLiteralOffset() const {
1662 // 32-bit literals are only supported on CI and we only want to use them
1663 // when the offset is > 8-bits.
1664 return isImm() && !isUInt<8>(getImm()) && isUInt<32>(getImm());
1667 //===----------------------------------------------------------------------===//
1669 //===----------------------------------------------------------------------===//
1671 static bool ConvertOmodMul(int64_t &Mul) {
1672 if (Mul != 1 && Mul != 2 && Mul != 4)
1679 static bool ConvertOmodDiv(int64_t &Div) {
1693 static const OptionalOperand VOP3OptionalOps [] = {
1694 {"clamp", AMDGPUOperand::ImmTyClamp, true, 0, nullptr},
1695 {"mul", AMDGPUOperand::ImmTyOMod, false, 1, ConvertOmodMul},
1696 {"div", AMDGPUOperand::ImmTyOMod, false, 1, ConvertOmodDiv},
1699 static bool isVOP3(OperandVector &Operands) {
1700 if (operandsHaveModifiers(Operands))
1703 AMDGPUOperand &DstOp = ((AMDGPUOperand&)*Operands[1]);
1705 if (DstOp.isReg() && DstOp.isRegClass(AMDGPU::SGPR_64RegClassID))
1708 if (Operands.size() >= 5)
1711 if (Operands.size() > 3) {
1712 AMDGPUOperand &Src1Op = ((AMDGPUOperand&)*Operands[3]);
1713 if (Src1Op.getReg() && (Src1Op.isRegClass(AMDGPU::SReg_32RegClassID) ||
1714 Src1Op.isRegClass(AMDGPU::SReg_64RegClassID)))
1720 AMDGPUAsmParser::OperandMatchResultTy
1721 AMDGPUAsmParser::parseVOP3OptionalOps(OperandVector &Operands) {
1723 // The value returned by this function may change after parsing
1724 // an operand so store the original value here.
1725 bool HasModifiers = operandsHaveModifiers(Operands);
1727 bool IsVOP3 = isVOP3(Operands);
1728 if (HasModifiers || IsVOP3 ||
1729 getLexer().isNot(AsmToken::EndOfStatement) ||
1730 getForcedEncodingSize() == 64) {
1732 AMDGPUAsmParser::OperandMatchResultTy Res =
1733 parseOptionalOps(VOP3OptionalOps, Operands);
1735 if (!HasModifiers && Res == MatchOperand_Success) {
1736 // We have added a modifier operation, so we need to make sure all
1737 // previous register operands have modifiers
1738 for (unsigned i = 2, e = Operands.size(); i != e; ++i) {
1739 AMDGPUOperand &Op = ((AMDGPUOperand&)*Operands[i]);
1746 return MatchOperand_NoMatch;
1749 void AMDGPUAsmParser::cvtVOP3(MCInst &Inst, const OperandVector &Operands) {
1752 const MCInstrDesc &Desc = MII.get(Inst.getOpcode());
1753 if (Desc.getNumDefs() > 0) {
1754 ((AMDGPUOperand &)*Operands[i++]).addRegOperands(Inst, 1);
1757 std::map<enum AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1759 if (operandsHaveModifiers(Operands)) {
1760 for (unsigned e = Operands.size(); i != e; ++i) {
1761 AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1763 if (Op.isRegWithInputMods()) {
1764 ((AMDGPUOperand &)*Operands[i]).addRegWithInputModsOperands(Inst, 2);
1767 OptionalIdx[Op.getImmTy()] = i;
1770 unsigned ClampIdx = OptionalIdx[AMDGPUOperand::ImmTyClamp];
1771 unsigned OModIdx = OptionalIdx[AMDGPUOperand::ImmTyOMod];
1773 ((AMDGPUOperand &)*Operands[ClampIdx]).addImmOperands(Inst, 1);
1774 ((AMDGPUOperand &)*Operands[OModIdx]).addImmOperands(Inst, 1);
1776 for (unsigned e = Operands.size(); i != e; ++i)
1777 ((AMDGPUOperand &)*Operands[i]).addRegOrImmOperands(Inst, 1);
1781 /// Force static initialization.
1782 extern "C" void LLVMInitializeAMDGPUAsmParser() {
1783 RegisterMCAsmParser<AMDGPUAsmParser> A(TheAMDGPUTarget);
1784 RegisterMCAsmParser<AMDGPUAsmParser> B(TheGCNTarget);
1787 #define GET_REGISTER_MATCHER
1788 #define GET_MATCHER_IMPLEMENTATION
1789 #include "AMDGPUGenAsmMatcher.inc"