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/MCSymbolELF.h"
32 #include "llvm/MC/MCTargetAsmParser.h"
33 #include "llvm/Support/ELF.h"
34 #include "llvm/Support/SourceMgr.h"
35 #include "llvm/Support/TargetRegistry.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Support/Debug.h"
43 struct OptionalOperand;
45 class AMDGPUOperand : public MCParsedAsmOperand {
53 SMLoc StartLoc, EndLoc;
56 AMDGPUOperand(enum KindTy K) : MCParsedAsmOperand(), Kind(K) {}
87 const MCRegisterInfo *TRI;
98 void addImmOperands(MCInst &Inst, unsigned N) const {
99 Inst.addOperand(MCOperand::createImm(getImm()));
102 StringRef getToken() const {
103 return StringRef(Tok.Data, Tok.Length);
106 void addRegOperands(MCInst &Inst, unsigned N) const {
107 Inst.addOperand(MCOperand::createReg(getReg()));
110 void addRegOrImmOperands(MCInst &Inst, unsigned N) const {
112 addRegOperands(Inst, N);
114 addImmOperands(Inst, N);
117 void addRegWithInputModsOperands(MCInst &Inst, unsigned N) const {
118 Inst.addOperand(MCOperand::createImm(
119 Reg.Modifiers == -1 ? 0 : Reg.Modifiers));
120 addRegOperands(Inst, N);
123 void addSoppBrTargetOperands(MCInst &Inst, unsigned N) const {
125 addImmOperands(Inst, N);
128 Inst.addOperand(MCOperand::createExpr(Expr));
132 bool defaultTokenHasSuffix() const {
133 StringRef Token(Tok.Data, Tok.Length);
135 return Token.endswith("_e32") || Token.endswith("_e64");
138 bool isToken() const override {
139 return Kind == Token;
142 bool isImm() const override {
143 return Kind == Immediate;
146 bool isInlineImm() const {
147 float F = BitsToFloat(Imm.Val);
148 // TODO: Add 0.5pi for VI
149 return isImm() && ((Imm.Val <= 64 && Imm.Val >= -16) ||
150 (F == 0.0 || F == 0.5 || F == -0.5 || F == 1.0 || F == -1.0 ||
151 F == 2.0 || F == -2.0 || F == 4.0 || F == -4.0));
154 bool isDSOffset0() const {
156 return Imm.Type == ImmTyDSOffset0;
159 bool isDSOffset1() const {
161 return Imm.Type == ImmTyDSOffset1;
164 int64_t getImm() const {
168 enum ImmTy getImmTy() const {
173 bool isRegKind() const {
174 return Kind == Register;
177 bool isReg() const override {
178 return Kind == Register && Reg.Modifiers == -1;
181 bool isRegWithInputMods() const {
182 return Kind == Register && (Reg.IsForcedVOP3 || Reg.Modifiers != -1);
185 void setModifiers(unsigned Mods) {
187 Reg.Modifiers = Mods;
190 bool hasModifiers() const {
192 return Reg.Modifiers != -1;
195 unsigned getReg() const override {
199 bool isRegOrImm() const {
200 return isReg() || isImm();
203 bool isRegClass(unsigned RCID) const {
204 return Reg.TRI->getRegClass(RCID).contains(getReg());
207 bool isSCSrc32() const {
208 return isInlineImm() || (isReg() && isRegClass(AMDGPU::SReg_32RegClassID));
211 bool isSSrc32() const {
212 return isImm() || (isReg() && isRegClass(AMDGPU::SReg_32RegClassID));
215 bool isSSrc64() const {
216 return isImm() || isInlineImm() ||
217 (isReg() && isRegClass(AMDGPU::SReg_64RegClassID));
220 bool isSCSrc64() const {
221 return (isReg() && isRegClass(AMDGPU::SReg_64RegClassID)) || isInlineImm();
224 bool isVCSrc32() const {
225 return isInlineImm() || (isReg() && isRegClass(AMDGPU::VS_32RegClassID));
228 bool isVCSrc64() const {
229 return isInlineImm() || (isReg() && isRegClass(AMDGPU::VS_64RegClassID));
232 bool isVSrc32() const {
233 return isImm() || (isReg() && isRegClass(AMDGPU::VS_32RegClassID));
236 bool isVSrc64() const {
237 return isImm() || (isReg() && isRegClass(AMDGPU::VS_64RegClassID));
240 bool isMem() const override {
244 bool isExpr() const {
245 return Kind == Expression;
248 bool isSoppBrTarget() const {
249 return isExpr() || isImm();
252 SMLoc getStartLoc() const override {
256 SMLoc getEndLoc() const override {
260 void print(raw_ostream &OS) const override {
263 OS << "<register " << getReg() << " mods: " << Reg.Modifiers << '>';
269 OS << '\'' << getToken() << '\'';
272 OS << "<expr " << *Expr << '>';
277 static std::unique_ptr<AMDGPUOperand> CreateImm(int64_t Val, SMLoc Loc,
278 enum ImmTy Type = ImmTyNone,
279 bool IsFPImm = false) {
280 auto Op = llvm::make_unique<AMDGPUOperand>(Immediate);
282 Op->Imm.IsFPImm = IsFPImm;
289 static std::unique_ptr<AMDGPUOperand> CreateToken(StringRef Str, SMLoc Loc,
290 bool HasExplicitEncodingSize = true) {
291 auto Res = llvm::make_unique<AMDGPUOperand>(Token);
292 Res->Tok.Data = Str.data();
293 Res->Tok.Length = Str.size();
299 static std::unique_ptr<AMDGPUOperand> CreateReg(unsigned RegNo, SMLoc S,
301 const MCRegisterInfo *TRI,
303 auto Op = llvm::make_unique<AMDGPUOperand>(Register);
304 Op->Reg.RegNo = RegNo;
306 Op->Reg.Modifiers = -1;
307 Op->Reg.IsForcedVOP3 = ForceVOP3;
313 static std::unique_ptr<AMDGPUOperand> CreateExpr(const class MCExpr *Expr, SMLoc S) {
314 auto Op = llvm::make_unique<AMDGPUOperand>(Expression);
321 bool isDSOffset() const;
322 bool isDSOffset01() const;
323 bool isSWaitCnt() const;
324 bool isMubufOffset() const;
325 bool isSMRDOffset() const;
326 bool isSMRDLiteralOffset() const;
329 class AMDGPUAsmParser : public MCTargetAsmParser {
330 const MCInstrInfo &MII;
333 unsigned ForcedEncodingSize;
336 return getSTI().getFeatureBits()[AMDGPU::FeatureVolcanicIslands];
339 bool hasSGPR102_SGPR103() const {
343 /// @name Auto-generated Match Functions
346 #define GET_ASSEMBLER_HEADER
347 #include "AMDGPUGenAsmMatcher.inc"
352 bool ParseDirectiveMajorMinor(uint32_t &Major, uint32_t &Minor);
353 bool ParseDirectiveHSACodeObjectVersion();
354 bool ParseDirectiveHSACodeObjectISA();
355 bool ParseAMDKernelCodeTValue(StringRef ID, amd_kernel_code_t &Header);
356 bool ParseDirectiveAMDKernelCodeT();
357 bool ParseSectionDirectiveHSAText();
358 bool subtargetHasRegister(const MCRegisterInfo &MRI, unsigned RegNo) const;
359 bool ParseDirectiveAMDGPUHsaKernel();
363 enum AMDGPUMatchResultTy {
364 Match_PreferE32 = FIRST_TARGET_MATCH_RESULT_TY
367 AMDGPUAsmParser(MCSubtargetInfo &STI, MCAsmParser &_Parser,
368 const MCInstrInfo &MII,
369 const MCTargetOptions &Options)
370 : MCTargetAsmParser(Options, STI), MII(MII), Parser(_Parser),
371 ForcedEncodingSize(0) {
372 if (getSTI().getFeatureBits().none()) {
373 // Set default features.
374 STI.ToggleFeature("SOUTHERN_ISLANDS");
377 setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));
380 AMDGPUTargetStreamer &getTargetStreamer() {
381 MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
382 return static_cast<AMDGPUTargetStreamer &>(TS);
385 unsigned getForcedEncodingSize() const {
386 return ForcedEncodingSize;
389 void setForcedEncodingSize(unsigned Size) {
390 ForcedEncodingSize = Size;
393 bool isForcedVOP3() const {
394 return ForcedEncodingSize == 64;
397 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
398 unsigned checkTargetMatchPredicate(MCInst &Inst) override;
399 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
400 OperandVector &Operands, MCStreamer &Out,
402 bool MatchingInlineAsm) override;
403 bool ParseDirective(AsmToken DirectiveID) override;
404 OperandMatchResultTy parseOperand(OperandVector &Operands, StringRef Mnemonic);
405 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
406 SMLoc NameLoc, OperandVector &Operands) override;
408 OperandMatchResultTy parseIntWithPrefix(const char *Prefix, int64_t &Int,
409 int64_t Default = 0);
410 OperandMatchResultTy parseIntWithPrefix(const char *Prefix,
411 OperandVector &Operands,
412 enum AMDGPUOperand::ImmTy ImmTy =
413 AMDGPUOperand::ImmTyNone);
414 OperandMatchResultTy parseNamedBit(const char *Name, OperandVector &Operands,
415 enum AMDGPUOperand::ImmTy ImmTy =
416 AMDGPUOperand::ImmTyNone);
417 OperandMatchResultTy parseOptionalOps(
418 const ArrayRef<OptionalOperand> &OptionalOps,
419 OperandVector &Operands);
422 void cvtDSOffset01(MCInst &Inst, const OperandVector &Operands);
423 void cvtDS(MCInst &Inst, const OperandVector &Operands);
424 OperandMatchResultTy parseDSOptionalOps(OperandVector &Operands);
425 OperandMatchResultTy parseDSOff01OptionalOps(OperandVector &Operands);
426 OperandMatchResultTy parseDSOffsetOptional(OperandVector &Operands);
428 bool parseCnt(int64_t &IntVal);
429 OperandMatchResultTy parseSWaitCntOps(OperandVector &Operands);
430 OperandMatchResultTy parseSOppBrTarget(OperandVector &Operands);
432 OperandMatchResultTy parseFlatOptionalOps(OperandVector &Operands);
433 OperandMatchResultTy parseFlatAtomicOptionalOps(OperandVector &Operands);
434 void cvtFlat(MCInst &Inst, const OperandVector &Operands);
436 void cvtMubuf(MCInst &Inst, const OperandVector &Operands);
437 OperandMatchResultTy parseOffset(OperandVector &Operands);
438 OperandMatchResultTy parseMubufOptionalOps(OperandVector &Operands);
439 OperandMatchResultTy parseGLC(OperandVector &Operands);
440 OperandMatchResultTy parseSLC(OperandVector &Operands);
441 OperandMatchResultTy parseTFE(OperandVector &Operands);
443 OperandMatchResultTy parseDMask(OperandVector &Operands);
444 OperandMatchResultTy parseUNorm(OperandVector &Operands);
445 OperandMatchResultTy parseR128(OperandVector &Operands);
447 void cvtVOP3(MCInst &Inst, const OperandVector &Operands);
448 OperandMatchResultTy parseVOP3OptionalOps(OperandVector &Operands);
451 struct OptionalOperand {
453 AMDGPUOperand::ImmTy Type;
456 bool (*ConvertResult)(int64_t&);
461 static int getRegClass(bool IsVgpr, unsigned RegWidth) {
465 case 1: return AMDGPU::VGPR_32RegClassID;
466 case 2: return AMDGPU::VReg_64RegClassID;
467 case 3: return AMDGPU::VReg_96RegClassID;
468 case 4: return AMDGPU::VReg_128RegClassID;
469 case 8: return AMDGPU::VReg_256RegClassID;
470 case 16: return AMDGPU::VReg_512RegClassID;
476 case 1: return AMDGPU::SGPR_32RegClassID;
477 case 2: return AMDGPU::SGPR_64RegClassID;
478 case 4: return AMDGPU::SReg_128RegClassID;
479 case 8: return AMDGPU::SReg_256RegClassID;
480 case 16: return AMDGPU::SReg_512RegClassID;
484 static unsigned getRegForName(StringRef RegName) {
486 return StringSwitch<unsigned>(RegName)
487 .Case("exec", AMDGPU::EXEC)
488 .Case("vcc", AMDGPU::VCC)
489 .Case("flat_scratch", AMDGPU::FLAT_SCR)
490 .Case("m0", AMDGPU::M0)
491 .Case("scc", AMDGPU::SCC)
492 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO)
493 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI)
494 .Case("vcc_lo", AMDGPU::VCC_LO)
495 .Case("vcc_hi", AMDGPU::VCC_HI)
496 .Case("exec_lo", AMDGPU::EXEC_LO)
497 .Case("exec_hi", AMDGPU::EXEC_HI)
501 bool AMDGPUAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) {
502 const AsmToken Tok = Parser.getTok();
503 StartLoc = Tok.getLoc();
504 EndLoc = Tok.getEndLoc();
505 StringRef RegName = Tok.getString();
506 RegNo = getRegForName(RegName);
513 // Match vgprs and sgprs
514 if (RegName[0] != 's' && RegName[0] != 'v')
517 bool IsVgpr = RegName[0] == 'v';
519 unsigned RegIndexInClass;
520 if (RegName.size() > 1) {
521 // We have a 32-bit register
523 if (RegName.substr(1).getAsInteger(10, RegIndexInClass))
527 // We have a register greater than 32-bits.
529 int64_t RegLo, RegHi;
531 if (getLexer().isNot(AsmToken::LBrac))
535 if (getParser().parseAbsoluteExpression(RegLo))
538 if (getLexer().isNot(AsmToken::Colon))
542 if (getParser().parseAbsoluteExpression(RegHi))
545 if (getLexer().isNot(AsmToken::RBrac))
549 RegWidth = (RegHi - RegLo) + 1;
551 // VGPR registers aren't aligned.
552 RegIndexInClass = RegLo;
554 // SGPR registers are aligned. Max alignment is 4 dwords.
555 unsigned Size = std::min(RegWidth, 4u);
556 if (RegLo % Size != 0)
559 RegIndexInClass = RegLo / Size;
563 const MCRegisterInfo *TRI = getContext().getRegisterInfo();
564 int RCID = getRegClass(IsVgpr, RegWidth);
568 const MCRegisterClass RC = TRI->getRegClass(RCID);
569 if (RegIndexInClass >= RC.getNumRegs())
572 RegNo = RC.getRegister(RegIndexInClass);
573 return !subtargetHasRegister(*TRI, RegNo);
576 unsigned AMDGPUAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
578 uint64_t TSFlags = MII.get(Inst.getOpcode()).TSFlags;
580 if ((getForcedEncodingSize() == 32 && (TSFlags & SIInstrFlags::VOP3)) ||
581 (getForcedEncodingSize() == 64 && !(TSFlags & SIInstrFlags::VOP3)))
582 return Match_InvalidOperand;
584 if ((TSFlags & SIInstrFlags::VOP3) &&
585 (TSFlags & SIInstrFlags::VOPAsmPrefer32Bit) &&
586 getForcedEncodingSize() != 64)
587 return Match_PreferE32;
589 return Match_Success;
593 bool AMDGPUAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
594 OperandVector &Operands,
597 bool MatchingInlineAsm) {
600 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm)) {
604 Out.EmitInstruction(Inst, getSTI());
606 case Match_MissingFeature:
607 return Error(IDLoc, "instruction not supported on this GPU");
609 case Match_MnemonicFail:
610 return Error(IDLoc, "unrecognized instruction mnemonic");
612 case Match_InvalidOperand: {
613 SMLoc ErrorLoc = IDLoc;
614 if (ErrorInfo != ~0ULL) {
615 if (ErrorInfo >= Operands.size()) {
616 if (isForcedVOP3()) {
617 // If 64-bit encoding has been forced we can end up with no
618 // clamp or omod operands if none of the registers have modifiers,
619 // so we need to add these to the operand list.
620 AMDGPUOperand &LastOp =
621 ((AMDGPUOperand &)*Operands[Operands.size() - 1]);
622 if (LastOp.isRegKind() ||
624 LastOp.getImmTy() != AMDGPUOperand::ImmTyNone)) {
625 SMLoc S = Parser.getTok().getLoc();
626 Operands.push_back(AMDGPUOperand::CreateImm(0, S,
627 AMDGPUOperand::ImmTyClamp));
628 Operands.push_back(AMDGPUOperand::CreateImm(0, S,
629 AMDGPUOperand::ImmTyOMod));
630 bool Res = MatchAndEmitInstruction(IDLoc, Opcode, Operands,
638 return Error(IDLoc, "too few operands for instruction");
641 ErrorLoc = ((AMDGPUOperand &)*Operands[ErrorInfo]).getStartLoc();
642 if (ErrorLoc == SMLoc())
645 return Error(ErrorLoc, "invalid operand for instruction");
647 case Match_PreferE32:
648 return Error(IDLoc, "internal error: instruction without _e64 suffix "
649 "should be encoded as e32");
651 llvm_unreachable("Implement any new match types added!");
654 bool AMDGPUAsmParser::ParseDirectiveMajorMinor(uint32_t &Major,
656 if (getLexer().isNot(AsmToken::Integer))
657 return TokError("invalid major version");
659 Major = getLexer().getTok().getIntVal();
662 if (getLexer().isNot(AsmToken::Comma))
663 return TokError("minor version number required, comma expected");
666 if (getLexer().isNot(AsmToken::Integer))
667 return TokError("invalid minor version");
669 Minor = getLexer().getTok().getIntVal();
675 bool AMDGPUAsmParser::ParseDirectiveHSACodeObjectVersion() {
680 if (ParseDirectiveMajorMinor(Major, Minor))
683 getTargetStreamer().EmitDirectiveHSACodeObjectVersion(Major, Minor);
687 bool AMDGPUAsmParser::ParseDirectiveHSACodeObjectISA() {
692 StringRef VendorName;
695 // If this directive has no arguments, then use the ISA version for the
697 if (getLexer().is(AsmToken::EndOfStatement)) {
698 AMDGPU::IsaVersion Isa = AMDGPU::getIsaVersion(getSTI().getFeatureBits());
699 getTargetStreamer().EmitDirectiveHSACodeObjectISA(Isa.Major, Isa.Minor,
706 if (ParseDirectiveMajorMinor(Major, Minor))
709 if (getLexer().isNot(AsmToken::Comma))
710 return TokError("stepping version number required, comma expected");
713 if (getLexer().isNot(AsmToken::Integer))
714 return TokError("invalid stepping version");
716 Stepping = getLexer().getTok().getIntVal();
719 if (getLexer().isNot(AsmToken::Comma))
720 return TokError("vendor name required, comma expected");
723 if (getLexer().isNot(AsmToken::String))
724 return TokError("invalid vendor name");
726 VendorName = getLexer().getTok().getStringContents();
729 if (getLexer().isNot(AsmToken::Comma))
730 return TokError("arch name required, comma expected");
733 if (getLexer().isNot(AsmToken::String))
734 return TokError("invalid arch name");
736 ArchName = getLexer().getTok().getStringContents();
739 getTargetStreamer().EmitDirectiveHSACodeObjectISA(Major, Minor, Stepping,
740 VendorName, ArchName);
744 bool AMDGPUAsmParser::ParseAMDKernelCodeTValue(StringRef ID,
745 amd_kernel_code_t &Header) {
747 if (getLexer().isNot(AsmToken::Equal))
748 return TokError("expected '='");
751 if (getLexer().isNot(AsmToken::Integer))
752 return TokError("amd_kernel_code_t values must be integers");
754 uint64_t Value = getLexer().getTok().getIntVal();
757 if (ID == "kernel_code_version_major")
758 Header.amd_kernel_code_version_major = Value;
759 else if (ID == "kernel_code_version_minor")
760 Header.amd_kernel_code_version_minor = Value;
761 else if (ID == "machine_kind")
762 Header.amd_machine_kind = Value;
763 else if (ID == "machine_version_major")
764 Header.amd_machine_version_major = Value;
765 else if (ID == "machine_version_minor")
766 Header.amd_machine_version_minor = Value;
767 else if (ID == "machine_version_stepping")
768 Header.amd_machine_version_stepping = Value;
769 else if (ID == "kernel_code_entry_byte_offset")
770 Header.kernel_code_entry_byte_offset = Value;
771 else if (ID == "kernel_code_prefetch_byte_size")
772 Header.kernel_code_prefetch_byte_size = Value;
773 else if (ID == "max_scratch_backing_memory_byte_size")
774 Header.max_scratch_backing_memory_byte_size = Value;
775 else if (ID == "compute_pgm_rsrc1_vgprs")
776 Header.compute_pgm_resource_registers |= S_00B848_VGPRS(Value);
777 else if (ID == "compute_pgm_rsrc1_sgprs")
778 Header.compute_pgm_resource_registers |= S_00B848_SGPRS(Value);
779 else if (ID == "compute_pgm_rsrc1_priority")
780 Header.compute_pgm_resource_registers |= S_00B848_PRIORITY(Value);
781 else if (ID == "compute_pgm_rsrc1_float_mode")
782 Header.compute_pgm_resource_registers |= S_00B848_FLOAT_MODE(Value);
783 else if (ID == "compute_pgm_rsrc1_priv")
784 Header.compute_pgm_resource_registers |= S_00B848_PRIV(Value);
785 else if (ID == "compute_pgm_rsrc1_dx10_clamp")
786 Header.compute_pgm_resource_registers |= S_00B848_DX10_CLAMP(Value);
787 else if (ID == "compute_pgm_rsrc1_debug_mode")
788 Header.compute_pgm_resource_registers |= S_00B848_DEBUG_MODE(Value);
789 else if (ID == "compute_pgm_rsrc1_ieee_mode")
790 Header.compute_pgm_resource_registers |= S_00B848_IEEE_MODE(Value);
791 else if (ID == "compute_pgm_rsrc2_scratch_en")
792 Header.compute_pgm_resource_registers |= (S_00B84C_SCRATCH_EN(Value) << 32);
793 else if (ID == "compute_pgm_rsrc2_user_sgpr")
794 Header.compute_pgm_resource_registers |= (S_00B84C_USER_SGPR(Value) << 32);
795 else if (ID == "compute_pgm_rsrc2_tgid_x_en")
796 Header.compute_pgm_resource_registers |= (S_00B84C_TGID_X_EN(Value) << 32);
797 else if (ID == "compute_pgm_rsrc2_tgid_y_en")
798 Header.compute_pgm_resource_registers |= (S_00B84C_TGID_Y_EN(Value) << 32);
799 else if (ID == "compute_pgm_rsrc2_tgid_z_en")
800 Header.compute_pgm_resource_registers |= (S_00B84C_TGID_Z_EN(Value) << 32);
801 else if (ID == "compute_pgm_rsrc2_tg_size_en")
802 Header.compute_pgm_resource_registers |= (S_00B84C_TG_SIZE_EN(Value) << 32);
803 else if (ID == "compute_pgm_rsrc2_tidig_comp_cnt")
804 Header.compute_pgm_resource_registers |=
805 (S_00B84C_TIDIG_COMP_CNT(Value) << 32);
806 else if (ID == "compute_pgm_rsrc2_excp_en_msb")
807 Header.compute_pgm_resource_registers |=
808 (S_00B84C_EXCP_EN_MSB(Value) << 32);
809 else if (ID == "compute_pgm_rsrc2_lds_size")
810 Header.compute_pgm_resource_registers |= (S_00B84C_LDS_SIZE(Value) << 32);
811 else if (ID == "compute_pgm_rsrc2_excp_en")
812 Header.compute_pgm_resource_registers |= (S_00B84C_EXCP_EN(Value) << 32);
813 else if (ID == "compute_pgm_resource_registers")
814 Header.compute_pgm_resource_registers = Value;
815 else if (ID == "enable_sgpr_private_segment_buffer")
816 Header.code_properties |=
817 (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER_SHIFT);
818 else if (ID == "enable_sgpr_dispatch_ptr")
819 Header.code_properties |=
820 (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR_SHIFT);
821 else if (ID == "enable_sgpr_queue_ptr")
822 Header.code_properties |=
823 (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR_SHIFT);
824 else if (ID == "enable_sgpr_kernarg_segment_ptr")
825 Header.code_properties |=
826 (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR_SHIFT);
827 else if (ID == "enable_sgpr_dispatch_id")
828 Header.code_properties |=
829 (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID_SHIFT);
830 else if (ID == "enable_sgpr_flat_scratch_init")
831 Header.code_properties |=
832 (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT_SHIFT);
833 else if (ID == "enable_sgpr_private_segment_size")
834 Header.code_properties |=
835 (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_SIZE_SHIFT);
836 else if (ID == "enable_sgpr_grid_workgroup_count_x")
837 Header.code_properties |=
838 (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_X_SHIFT);
839 else if (ID == "enable_sgpr_grid_workgroup_count_y")
840 Header.code_properties |=
841 (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Y_SHIFT);
842 else if (ID == "enable_sgpr_grid_workgroup_count_z")
843 Header.code_properties |=
844 (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Z_SHIFT);
845 else if (ID == "enable_ordered_append_gds")
846 Header.code_properties |=
847 (Value << AMD_CODE_PROPERTY_ENABLE_ORDERED_APPEND_GDS_SHIFT);
848 else if (ID == "private_element_size")
849 Header.code_properties |=
850 (Value << AMD_CODE_PROPERTY_PRIVATE_ELEMENT_SIZE_SHIFT);
851 else if (ID == "is_ptr64")
852 Header.code_properties |=
853 (Value << AMD_CODE_PROPERTY_IS_PTR64_SHIFT);
854 else if (ID == "is_dynamic_callstack")
855 Header.code_properties |=
856 (Value << AMD_CODE_PROPERTY_IS_DYNAMIC_CALLSTACK_SHIFT);
857 else if (ID == "is_debug_enabled")
858 Header.code_properties |=
859 (Value << AMD_CODE_PROPERTY_IS_DEBUG_SUPPORTED_SHIFT);
860 else if (ID == "is_xnack_enabled")
861 Header.code_properties |=
862 (Value << AMD_CODE_PROPERTY_IS_XNACK_SUPPORTED_SHIFT);
863 else if (ID == "workitem_private_segment_byte_size")
864 Header.workitem_private_segment_byte_size = Value;
865 else if (ID == "workgroup_group_segment_byte_size")
866 Header.workgroup_group_segment_byte_size = Value;
867 else if (ID == "gds_segment_byte_size")
868 Header.gds_segment_byte_size = Value;
869 else if (ID == "kernarg_segment_byte_size")
870 Header.kernarg_segment_byte_size = Value;
871 else if (ID == "workgroup_fbarrier_count")
872 Header.workgroup_fbarrier_count = Value;
873 else if (ID == "wavefront_sgpr_count")
874 Header.wavefront_sgpr_count = Value;
875 else if (ID == "workitem_vgpr_count")
876 Header.workitem_vgpr_count = Value;
877 else if (ID == "reserved_vgpr_first")
878 Header.reserved_vgpr_first = Value;
879 else if (ID == "reserved_vgpr_count")
880 Header.reserved_vgpr_count = Value;
881 else if (ID == "reserved_sgpr_first")
882 Header.reserved_sgpr_first = Value;
883 else if (ID == "reserved_sgpr_count")
884 Header.reserved_sgpr_count = Value;
885 else if (ID == "debug_wavefront_private_segment_offset_sgpr")
886 Header.debug_wavefront_private_segment_offset_sgpr = Value;
887 else if (ID == "debug_private_segment_buffer_sgpr")
888 Header.debug_private_segment_buffer_sgpr = Value;
889 else if (ID == "kernarg_segment_alignment")
890 Header.kernarg_segment_alignment = Value;
891 else if (ID == "group_segment_alignment")
892 Header.group_segment_alignment = Value;
893 else if (ID == "private_segment_alignment")
894 Header.private_segment_alignment = Value;
895 else if (ID == "wavefront_size")
896 Header.wavefront_size = Value;
897 else if (ID == "call_convention")
898 Header.call_convention = Value;
899 else if (ID == "runtime_loader_kernel_symbol")
900 Header.runtime_loader_kernel_symbol = Value;
902 return TokError("amd_kernel_code_t value not recognized.");
907 bool AMDGPUAsmParser::ParseDirectiveAMDKernelCodeT() {
909 amd_kernel_code_t Header;
910 AMDGPU::initDefaultAMDKernelCodeT(Header, getSTI().getFeatureBits());
914 if (getLexer().isNot(AsmToken::EndOfStatement))
915 return TokError("amd_kernel_code_t values must begin on a new line");
917 // Lex EndOfStatement. This is in a while loop, because lexing a comment
918 // will set the current token to EndOfStatement.
919 while(getLexer().is(AsmToken::EndOfStatement))
922 if (getLexer().isNot(AsmToken::Identifier))
923 return TokError("expected value identifier or .end_amd_kernel_code_t");
925 StringRef ID = getLexer().getTok().getIdentifier();
928 if (ID == ".end_amd_kernel_code_t")
931 if (ParseAMDKernelCodeTValue(ID, Header))
935 getTargetStreamer().EmitAMDKernelCodeT(Header);
940 bool AMDGPUAsmParser::ParseSectionDirectiveHSAText() {
941 getParser().getStreamer().SwitchSection(
942 AMDGPU::getHSATextSection(getContext()));
946 bool AMDGPUAsmParser::ParseDirectiveAMDGPUHsaKernel() {
947 if (getLexer().isNot(AsmToken::Identifier))
948 return TokError("expected symbol name");
950 StringRef KernelName = Parser.getTok().getString();
952 getTargetStreamer().EmitAMDGPUSymbolType(KernelName,
953 ELF::STT_AMDGPU_HSA_KERNEL);
958 bool AMDGPUAsmParser::ParseDirective(AsmToken DirectiveID) {
959 StringRef IDVal = DirectiveID.getString();
961 if (IDVal == ".hsa_code_object_version")
962 return ParseDirectiveHSACodeObjectVersion();
964 if (IDVal == ".hsa_code_object_isa")
965 return ParseDirectiveHSACodeObjectISA();
967 if (IDVal == ".amd_kernel_code_t")
968 return ParseDirectiveAMDKernelCodeT();
970 if (IDVal == ".hsatext" || IDVal == ".text")
971 return ParseSectionDirectiveHSAText();
973 if (IDVal == ".amdgpu_hsa_kernel")
974 return ParseDirectiveAMDGPUHsaKernel();
979 bool AMDGPUAsmParser::subtargetHasRegister(const MCRegisterInfo &MRI,
980 unsigned RegNo) const {
984 // VI only has 102 SGPRs, so make sure we aren't trying to use the 2 more that
986 for (MCRegAliasIterator R(AMDGPU::SGPR102_SGPR103, &MRI, true);
995 static bool operandsHaveModifiers(const OperandVector &Operands) {
997 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
998 const AMDGPUOperand &Op = ((AMDGPUOperand&)*Operands[i]);
999 if (Op.isRegKind() && Op.hasModifiers())
1001 if (Op.isImm() && (Op.getImmTy() == AMDGPUOperand::ImmTyOMod ||
1002 Op.getImmTy() == AMDGPUOperand::ImmTyClamp))
1008 AMDGPUAsmParser::OperandMatchResultTy
1009 AMDGPUAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
1011 // Try to parse with a custom parser
1012 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1014 // If we successfully parsed the operand or if there as an error parsing,
1017 // If we are parsing after we reach EndOfStatement then this means we
1018 // are appending default values to the Operands list. This is only done
1019 // by custom parser, so we shouldn't continue on to the generic parsing.
1020 if (ResTy == MatchOperand_Success || ResTy == MatchOperand_ParseFail ||
1021 getLexer().is(AsmToken::EndOfStatement))
1024 bool Negate = false, Abs = false;
1025 if (getLexer().getKind()== AsmToken::Minus) {
1030 if (getLexer().getKind() == AsmToken::Pipe) {
1035 switch(getLexer().getKind()) {
1036 case AsmToken::Integer: {
1037 SMLoc S = Parser.getTok().getLoc();
1039 if (getParser().parseAbsoluteExpression(IntVal))
1040 return MatchOperand_ParseFail;
1041 if (!isInt<32>(IntVal) && !isUInt<32>(IntVal)) {
1042 Error(S, "invalid immediate: only 32-bit values are legal");
1043 return MatchOperand_ParseFail;
1048 Operands.push_back(AMDGPUOperand::CreateImm(IntVal, S));
1049 return MatchOperand_Success;
1051 case AsmToken::Real: {
1052 // FIXME: We should emit an error if a double precisions floating-point
1053 // value is used. I'm not sure the best way to detect this.
1054 SMLoc S = Parser.getTok().getLoc();
1056 if (getParser().parseAbsoluteExpression(IntVal))
1057 return MatchOperand_ParseFail;
1059 APFloat F((float)BitsToDouble(IntVal));
1063 AMDGPUOperand::CreateImm(F.bitcastToAPInt().getZExtValue(), S));
1064 return MatchOperand_Success;
1066 case AsmToken::Identifier: {
1069 if (!ParseRegister(RegNo, S, E)) {
1071 bool HasModifiers = operandsHaveModifiers(Operands);
1072 unsigned Modifiers = 0;
1078 if (getLexer().getKind() != AsmToken::Pipe)
1079 return MatchOperand_ParseFail;
1084 if (Modifiers && !HasModifiers) {
1085 // We are adding a modifier to src1 or src2 and previous sources
1086 // don't have modifiers, so we need to go back and empty modifers
1087 // for each previous source.
1088 for (unsigned PrevRegIdx = Operands.size() - 1; PrevRegIdx > 1;
1091 AMDGPUOperand &RegOp = ((AMDGPUOperand&)*Operands[PrevRegIdx]);
1092 RegOp.setModifiers(0);
1097 Operands.push_back(AMDGPUOperand::CreateReg(
1098 RegNo, S, E, getContext().getRegisterInfo(),
1101 if (HasModifiers || Modifiers) {
1102 AMDGPUOperand &RegOp = ((AMDGPUOperand&)*Operands[Operands.size() - 1]);
1103 RegOp.setModifiers(Modifiers);
1107 Operands.push_back(AMDGPUOperand::CreateToken(Parser.getTok().getString(),
1111 return MatchOperand_Success;
1114 return MatchOperand_NoMatch;
1118 bool AMDGPUAsmParser::ParseInstruction(ParseInstructionInfo &Info,
1120 SMLoc NameLoc, OperandVector &Operands) {
1122 // Clear any forced encodings from the previous instruction.
1123 setForcedEncodingSize(0);
1125 if (Name.endswith("_e64"))
1126 setForcedEncodingSize(64);
1127 else if (Name.endswith("_e32"))
1128 setForcedEncodingSize(32);
1130 // Add the instruction mnemonic
1131 Operands.push_back(AMDGPUOperand::CreateToken(Name, NameLoc));
1133 while (!getLexer().is(AsmToken::EndOfStatement)) {
1134 AMDGPUAsmParser::OperandMatchResultTy Res = parseOperand(Operands, Name);
1136 // Eat the comma or space if there is one.
1137 if (getLexer().is(AsmToken::Comma))
1141 case MatchOperand_Success: break;
1142 case MatchOperand_ParseFail: return Error(getLexer().getLoc(),
1143 "failed parsing operand.");
1144 case MatchOperand_NoMatch: return Error(getLexer().getLoc(),
1145 "not a valid operand.");
1149 // Once we reach end of statement, continue parsing so we can add default
1150 // values for optional arguments.
1151 AMDGPUAsmParser::OperandMatchResultTy Res;
1152 while ((Res = parseOperand(Operands, Name)) != MatchOperand_NoMatch) {
1153 if (Res != MatchOperand_Success)
1154 return Error(getLexer().getLoc(), "failed parsing operand.");
1159 //===----------------------------------------------------------------------===//
1160 // Utility functions
1161 //===----------------------------------------------------------------------===//
1163 AMDGPUAsmParser::OperandMatchResultTy
1164 AMDGPUAsmParser::parseIntWithPrefix(const char *Prefix, int64_t &Int,
1167 // We are at the end of the statement, and this is a default argument, so
1168 // use a default value.
1169 if (getLexer().is(AsmToken::EndOfStatement)) {
1171 return MatchOperand_Success;
1174 switch(getLexer().getKind()) {
1175 default: return MatchOperand_NoMatch;
1176 case AsmToken::Identifier: {
1177 StringRef OffsetName = Parser.getTok().getString();
1178 if (!OffsetName.equals(Prefix))
1179 return MatchOperand_NoMatch;
1182 if (getLexer().isNot(AsmToken::Colon))
1183 return MatchOperand_ParseFail;
1186 if (getLexer().isNot(AsmToken::Integer))
1187 return MatchOperand_ParseFail;
1189 if (getParser().parseAbsoluteExpression(Int))
1190 return MatchOperand_ParseFail;
1194 return MatchOperand_Success;
1197 AMDGPUAsmParser::OperandMatchResultTy
1198 AMDGPUAsmParser::parseIntWithPrefix(const char *Prefix, OperandVector &Operands,
1199 enum AMDGPUOperand::ImmTy ImmTy) {
1201 SMLoc S = Parser.getTok().getLoc();
1204 AMDGPUAsmParser::OperandMatchResultTy Res = parseIntWithPrefix(Prefix, Offset);
1205 if (Res != MatchOperand_Success)
1208 Operands.push_back(AMDGPUOperand::CreateImm(Offset, S, ImmTy));
1209 return MatchOperand_Success;
1212 AMDGPUAsmParser::OperandMatchResultTy
1213 AMDGPUAsmParser::parseNamedBit(const char *Name, OperandVector &Operands,
1214 enum AMDGPUOperand::ImmTy ImmTy) {
1216 SMLoc S = Parser.getTok().getLoc();
1218 // We are at the end of the statement, and this is a default argument, so
1219 // use a default value.
1220 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1221 switch(getLexer().getKind()) {
1222 case AsmToken::Identifier: {
1223 StringRef Tok = Parser.getTok().getString();
1227 } else if (Tok.startswith("no") && Tok.endswith(Name)) {
1231 return MatchOperand_NoMatch;
1236 return MatchOperand_NoMatch;
1240 Operands.push_back(AMDGPUOperand::CreateImm(Bit, S, ImmTy));
1241 return MatchOperand_Success;
1244 static bool operandsHasOptionalOp(const OperandVector &Operands,
1245 const OptionalOperand &OOp) {
1246 for (unsigned i = 0; i < Operands.size(); i++) {
1247 const AMDGPUOperand &ParsedOp = ((const AMDGPUOperand &)*Operands[i]);
1248 if ((ParsedOp.isImm() && ParsedOp.getImmTy() == OOp.Type) ||
1249 (ParsedOp.isToken() && ParsedOp.getToken() == OOp.Name))
1256 AMDGPUAsmParser::OperandMatchResultTy
1257 AMDGPUAsmParser::parseOptionalOps(const ArrayRef<OptionalOperand> &OptionalOps,
1258 OperandVector &Operands) {
1259 SMLoc S = Parser.getTok().getLoc();
1260 for (const OptionalOperand &Op : OptionalOps) {
1261 if (operandsHasOptionalOp(Operands, Op))
1263 AMDGPUAsmParser::OperandMatchResultTy Res;
1266 Res = parseNamedBit(Op.Name, Operands, Op.Type);
1267 if (Res == MatchOperand_NoMatch)
1272 Res = parseIntWithPrefix(Op.Name, Value, Op.Default);
1274 if (Res == MatchOperand_NoMatch)
1277 if (Res != MatchOperand_Success)
1280 if (Op.ConvertResult && !Op.ConvertResult(Value)) {
1281 return MatchOperand_ParseFail;
1284 Operands.push_back(AMDGPUOperand::CreateImm(Value, S, Op.Type));
1285 return MatchOperand_Success;
1287 return MatchOperand_NoMatch;
1290 //===----------------------------------------------------------------------===//
1292 //===----------------------------------------------------------------------===//
1294 static const OptionalOperand DSOptionalOps [] = {
1295 {"offset", AMDGPUOperand::ImmTyOffset, false, 0, nullptr},
1296 {"gds", AMDGPUOperand::ImmTyGDS, true, 0, nullptr}
1299 static const OptionalOperand DSOptionalOpsOff01 [] = {
1300 {"offset0", AMDGPUOperand::ImmTyDSOffset0, false, 0, nullptr},
1301 {"offset1", AMDGPUOperand::ImmTyDSOffset1, false, 0, nullptr},
1302 {"gds", AMDGPUOperand::ImmTyGDS, true, 0, nullptr}
1305 AMDGPUAsmParser::OperandMatchResultTy
1306 AMDGPUAsmParser::parseDSOptionalOps(OperandVector &Operands) {
1307 return parseOptionalOps(DSOptionalOps, Operands);
1309 AMDGPUAsmParser::OperandMatchResultTy
1310 AMDGPUAsmParser::parseDSOff01OptionalOps(OperandVector &Operands) {
1311 return parseOptionalOps(DSOptionalOpsOff01, Operands);
1314 AMDGPUAsmParser::OperandMatchResultTy
1315 AMDGPUAsmParser::parseDSOffsetOptional(OperandVector &Operands) {
1316 SMLoc S = Parser.getTok().getLoc();
1317 AMDGPUAsmParser::OperandMatchResultTy Res =
1318 parseIntWithPrefix("offset", Operands, AMDGPUOperand::ImmTyOffset);
1319 if (Res == MatchOperand_NoMatch) {
1320 Operands.push_back(AMDGPUOperand::CreateImm(0, S,
1321 AMDGPUOperand::ImmTyOffset));
1322 Res = MatchOperand_Success;
1327 bool AMDGPUOperand::isDSOffset() const {
1328 return isImm() && isUInt<16>(getImm());
1331 bool AMDGPUOperand::isDSOffset01() const {
1332 return isImm() && isUInt<8>(getImm());
1335 void AMDGPUAsmParser::cvtDSOffset01(MCInst &Inst,
1336 const OperandVector &Operands) {
1338 std::map<enum AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1340 for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
1341 AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1343 // Add the register arguments
1345 Op.addRegOperands(Inst, 1);
1349 // Handle optional arguments
1350 OptionalIdx[Op.getImmTy()] = i;
1353 unsigned Offset0Idx = OptionalIdx[AMDGPUOperand::ImmTyDSOffset0];
1354 unsigned Offset1Idx = OptionalIdx[AMDGPUOperand::ImmTyDSOffset1];
1355 unsigned GDSIdx = OptionalIdx[AMDGPUOperand::ImmTyGDS];
1357 ((AMDGPUOperand &)*Operands[Offset0Idx]).addImmOperands(Inst, 1); // offset0
1358 ((AMDGPUOperand &)*Operands[Offset1Idx]).addImmOperands(Inst, 1); // offset1
1359 ((AMDGPUOperand &)*Operands[GDSIdx]).addImmOperands(Inst, 1); // gds
1360 Inst.addOperand(MCOperand::createReg(AMDGPU::M0)); // m0
1363 void AMDGPUAsmParser::cvtDS(MCInst &Inst, const OperandVector &Operands) {
1365 std::map<enum AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1366 bool GDSOnly = false;
1368 for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
1369 AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1371 // Add the register arguments
1373 Op.addRegOperands(Inst, 1);
1377 if (Op.isToken() && Op.getToken() == "gds") {
1382 // Handle optional arguments
1383 OptionalIdx[Op.getImmTy()] = i;
1386 unsigned OffsetIdx = OptionalIdx[AMDGPUOperand::ImmTyOffset];
1387 ((AMDGPUOperand &)*Operands[OffsetIdx]).addImmOperands(Inst, 1); // offset
1390 unsigned GDSIdx = OptionalIdx[AMDGPUOperand::ImmTyGDS];
1391 ((AMDGPUOperand &)*Operands[GDSIdx]).addImmOperands(Inst, 1); // gds
1393 Inst.addOperand(MCOperand::createReg(AMDGPU::M0)); // m0
1397 //===----------------------------------------------------------------------===//
1399 //===----------------------------------------------------------------------===//
1401 bool AMDGPUAsmParser::parseCnt(int64_t &IntVal) {
1402 StringRef CntName = Parser.getTok().getString();
1406 if (getLexer().isNot(AsmToken::LParen))
1410 if (getLexer().isNot(AsmToken::Integer))
1413 if (getParser().parseAbsoluteExpression(CntVal))
1416 if (getLexer().isNot(AsmToken::RParen))
1420 if (getLexer().is(AsmToken::Amp) || getLexer().is(AsmToken::Comma))
1426 if (CntName == "vmcnt") {
1429 } else if (CntName == "expcnt") {
1432 } else if (CntName == "lgkmcnt") {
1439 IntVal &= ~(CntMask << CntShift);
1440 IntVal |= (CntVal << CntShift);
1444 AMDGPUAsmParser::OperandMatchResultTy
1445 AMDGPUAsmParser::parseSWaitCntOps(OperandVector &Operands) {
1446 // Disable all counters by default.
1450 int64_t CntVal = 0x77f;
1451 SMLoc S = Parser.getTok().getLoc();
1453 switch(getLexer().getKind()) {
1454 default: return MatchOperand_ParseFail;
1455 case AsmToken::Integer:
1456 // The operand can be an integer value.
1457 if (getParser().parseAbsoluteExpression(CntVal))
1458 return MatchOperand_ParseFail;
1461 case AsmToken::Identifier:
1463 if (parseCnt(CntVal))
1464 return MatchOperand_ParseFail;
1465 } while(getLexer().isNot(AsmToken::EndOfStatement));
1468 Operands.push_back(AMDGPUOperand::CreateImm(CntVal, S));
1469 return MatchOperand_Success;
1472 bool AMDGPUOperand::isSWaitCnt() const {
1476 //===----------------------------------------------------------------------===//
1477 // sopp branch targets
1478 //===----------------------------------------------------------------------===//
1480 AMDGPUAsmParser::OperandMatchResultTy
1481 AMDGPUAsmParser::parseSOppBrTarget(OperandVector &Operands) {
1482 SMLoc S = Parser.getTok().getLoc();
1484 switch (getLexer().getKind()) {
1485 default: return MatchOperand_ParseFail;
1486 case AsmToken::Integer: {
1488 if (getParser().parseAbsoluteExpression(Imm))
1489 return MatchOperand_ParseFail;
1490 Operands.push_back(AMDGPUOperand::CreateImm(Imm, S));
1491 return MatchOperand_Success;
1494 case AsmToken::Identifier:
1495 Operands.push_back(AMDGPUOperand::CreateExpr(
1496 MCSymbolRefExpr::create(getContext().getOrCreateSymbol(
1497 Parser.getTok().getString()), getContext()), S));
1499 return MatchOperand_Success;
1503 //===----------------------------------------------------------------------===//
1505 //===----------------------------------------------------------------------===//
1507 static const OptionalOperand FlatOptionalOps [] = {
1508 {"glc", AMDGPUOperand::ImmTyGLC, true, 0, nullptr},
1509 {"slc", AMDGPUOperand::ImmTySLC, true, 0, nullptr},
1510 {"tfe", AMDGPUOperand::ImmTyTFE, true, 0, nullptr}
1513 static const OptionalOperand FlatAtomicOptionalOps [] = {
1514 {"slc", AMDGPUOperand::ImmTySLC, true, 0, nullptr},
1515 {"tfe", AMDGPUOperand::ImmTyTFE, true, 0, nullptr}
1518 AMDGPUAsmParser::OperandMatchResultTy
1519 AMDGPUAsmParser::parseFlatOptionalOps(OperandVector &Operands) {
1520 return parseOptionalOps(FlatOptionalOps, Operands);
1523 AMDGPUAsmParser::OperandMatchResultTy
1524 AMDGPUAsmParser::parseFlatAtomicOptionalOps(OperandVector &Operands) {
1525 return parseOptionalOps(FlatAtomicOptionalOps, Operands);
1528 void AMDGPUAsmParser::cvtFlat(MCInst &Inst,
1529 const OperandVector &Operands) {
1530 std::map<AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1532 for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
1533 AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1535 // Add the register arguments
1537 Op.addRegOperands(Inst, 1);
1541 // Handle 'glc' token which is sometimes hard-coded into the
1542 // asm string. There are no MCInst operands for these.
1546 // Handle optional arguments
1547 OptionalIdx[Op.getImmTy()] = i;
1551 // flat atomic instructions don't have a glc argument.
1552 if (OptionalIdx.count(AMDGPUOperand::ImmTyGLC)) {
1553 unsigned GLCIdx = OptionalIdx[AMDGPUOperand::ImmTyGLC];
1554 ((AMDGPUOperand &)*Operands[GLCIdx]).addImmOperands(Inst, 1);
1557 unsigned SLCIdx = OptionalIdx[AMDGPUOperand::ImmTySLC];
1558 unsigned TFEIdx = OptionalIdx[AMDGPUOperand::ImmTyTFE];
1560 ((AMDGPUOperand &)*Operands[SLCIdx]).addImmOperands(Inst, 1);
1561 ((AMDGPUOperand &)*Operands[TFEIdx]).addImmOperands(Inst, 1);
1564 //===----------------------------------------------------------------------===//
1566 //===----------------------------------------------------------------------===//
1568 static const OptionalOperand MubufOptionalOps [] = {
1569 {"offset", AMDGPUOperand::ImmTyOffset, false, 0, nullptr},
1570 {"glc", AMDGPUOperand::ImmTyGLC, true, 0, nullptr},
1571 {"slc", AMDGPUOperand::ImmTySLC, true, 0, nullptr},
1572 {"tfe", AMDGPUOperand::ImmTyTFE, true, 0, nullptr}
1575 AMDGPUAsmParser::OperandMatchResultTy
1576 AMDGPUAsmParser::parseMubufOptionalOps(OperandVector &Operands) {
1577 return parseOptionalOps(MubufOptionalOps, Operands);
1580 AMDGPUAsmParser::OperandMatchResultTy
1581 AMDGPUAsmParser::parseOffset(OperandVector &Operands) {
1582 return parseIntWithPrefix("offset", Operands);
1585 AMDGPUAsmParser::OperandMatchResultTy
1586 AMDGPUAsmParser::parseGLC(OperandVector &Operands) {
1587 return parseNamedBit("glc", Operands);
1590 AMDGPUAsmParser::OperandMatchResultTy
1591 AMDGPUAsmParser::parseSLC(OperandVector &Operands) {
1592 return parseNamedBit("slc", Operands);
1595 AMDGPUAsmParser::OperandMatchResultTy
1596 AMDGPUAsmParser::parseTFE(OperandVector &Operands) {
1597 return parseNamedBit("tfe", Operands);
1600 bool AMDGPUOperand::isMubufOffset() const {
1601 return isImm() && isUInt<12>(getImm());
1604 void AMDGPUAsmParser::cvtMubuf(MCInst &Inst,
1605 const OperandVector &Operands) {
1606 std::map<enum AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1608 for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
1609 AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1611 // Add the register arguments
1613 Op.addRegOperands(Inst, 1);
1617 // Handle the case where soffset is an immediate
1618 if (Op.isImm() && Op.getImmTy() == AMDGPUOperand::ImmTyNone) {
1619 Op.addImmOperands(Inst, 1);
1623 // Handle tokens like 'offen' which are sometimes hard-coded into the
1624 // asm string. There are no MCInst operands for these.
1630 // Handle optional arguments
1631 OptionalIdx[Op.getImmTy()] = i;
1634 assert(OptionalIdx.size() == 4);
1636 unsigned OffsetIdx = OptionalIdx[AMDGPUOperand::ImmTyOffset];
1637 unsigned GLCIdx = OptionalIdx[AMDGPUOperand::ImmTyGLC];
1638 unsigned SLCIdx = OptionalIdx[AMDGPUOperand::ImmTySLC];
1639 unsigned TFEIdx = OptionalIdx[AMDGPUOperand::ImmTyTFE];
1641 ((AMDGPUOperand &)*Operands[OffsetIdx]).addImmOperands(Inst, 1);
1642 ((AMDGPUOperand &)*Operands[GLCIdx]).addImmOperands(Inst, 1);
1643 ((AMDGPUOperand &)*Operands[SLCIdx]).addImmOperands(Inst, 1);
1644 ((AMDGPUOperand &)*Operands[TFEIdx]).addImmOperands(Inst, 1);
1647 //===----------------------------------------------------------------------===//
1649 //===----------------------------------------------------------------------===//
1651 AMDGPUAsmParser::OperandMatchResultTy
1652 AMDGPUAsmParser::parseDMask(OperandVector &Operands) {
1653 return parseIntWithPrefix("dmask", Operands);
1656 AMDGPUAsmParser::OperandMatchResultTy
1657 AMDGPUAsmParser::parseUNorm(OperandVector &Operands) {
1658 return parseNamedBit("unorm", Operands);
1661 AMDGPUAsmParser::OperandMatchResultTy
1662 AMDGPUAsmParser::parseR128(OperandVector &Operands) {
1663 return parseNamedBit("r128", Operands);
1666 //===----------------------------------------------------------------------===//
1668 //===----------------------------------------------------------------------===//
1670 bool AMDGPUOperand::isSMRDOffset() const {
1672 // FIXME: Support 20-bit offsets on VI. We need to to pass subtarget
1673 // information here.
1674 return isImm() && isUInt<8>(getImm());
1677 bool AMDGPUOperand::isSMRDLiteralOffset() const {
1678 // 32-bit literals are only supported on CI and we only want to use them
1679 // when the offset is > 8-bits.
1680 return isImm() && !isUInt<8>(getImm()) && isUInt<32>(getImm());
1683 //===----------------------------------------------------------------------===//
1685 //===----------------------------------------------------------------------===//
1687 static bool ConvertOmodMul(int64_t &Mul) {
1688 if (Mul != 1 && Mul != 2 && Mul != 4)
1695 static bool ConvertOmodDiv(int64_t &Div) {
1709 static const OptionalOperand VOP3OptionalOps [] = {
1710 {"clamp", AMDGPUOperand::ImmTyClamp, true, 0, nullptr},
1711 {"mul", AMDGPUOperand::ImmTyOMod, false, 1, ConvertOmodMul},
1712 {"div", AMDGPUOperand::ImmTyOMod, false, 1, ConvertOmodDiv},
1715 static bool isVOP3(OperandVector &Operands) {
1716 if (operandsHaveModifiers(Operands))
1719 AMDGPUOperand &DstOp = ((AMDGPUOperand&)*Operands[1]);
1721 if (DstOp.isReg() && DstOp.isRegClass(AMDGPU::SGPR_64RegClassID))
1724 if (Operands.size() >= 5)
1727 if (Operands.size() > 3) {
1728 AMDGPUOperand &Src1Op = ((AMDGPUOperand&)*Operands[3]);
1729 if (Src1Op.getReg() && (Src1Op.isRegClass(AMDGPU::SReg_32RegClassID) ||
1730 Src1Op.isRegClass(AMDGPU::SReg_64RegClassID)))
1736 AMDGPUAsmParser::OperandMatchResultTy
1737 AMDGPUAsmParser::parseVOP3OptionalOps(OperandVector &Operands) {
1739 // The value returned by this function may change after parsing
1740 // an operand so store the original value here.
1741 bool HasModifiers = operandsHaveModifiers(Operands);
1743 bool IsVOP3 = isVOP3(Operands);
1744 if (HasModifiers || IsVOP3 ||
1745 getLexer().isNot(AsmToken::EndOfStatement) ||
1746 getForcedEncodingSize() == 64) {
1748 AMDGPUAsmParser::OperandMatchResultTy Res =
1749 parseOptionalOps(VOP3OptionalOps, Operands);
1751 if (!HasModifiers && Res == MatchOperand_Success) {
1752 // We have added a modifier operation, so we need to make sure all
1753 // previous register operands have modifiers
1754 for (unsigned i = 2, e = Operands.size(); i != e; ++i) {
1755 AMDGPUOperand &Op = ((AMDGPUOperand&)*Operands[i]);
1762 return MatchOperand_NoMatch;
1765 void AMDGPUAsmParser::cvtVOP3(MCInst &Inst, const OperandVector &Operands) {
1768 const MCInstrDesc &Desc = MII.get(Inst.getOpcode());
1769 if (Desc.getNumDefs() > 0) {
1770 ((AMDGPUOperand &)*Operands[i++]).addRegOperands(Inst, 1);
1773 std::map<enum AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1775 if (operandsHaveModifiers(Operands)) {
1776 for (unsigned e = Operands.size(); i != e; ++i) {
1777 AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1779 if (Op.isRegWithInputMods()) {
1780 ((AMDGPUOperand &)*Operands[i]).addRegWithInputModsOperands(Inst, 2);
1783 OptionalIdx[Op.getImmTy()] = i;
1786 unsigned ClampIdx = OptionalIdx[AMDGPUOperand::ImmTyClamp];
1787 unsigned OModIdx = OptionalIdx[AMDGPUOperand::ImmTyOMod];
1789 ((AMDGPUOperand &)*Operands[ClampIdx]).addImmOperands(Inst, 1);
1790 ((AMDGPUOperand &)*Operands[OModIdx]).addImmOperands(Inst, 1);
1792 for (unsigned e = Operands.size(); i != e; ++i)
1793 ((AMDGPUOperand &)*Operands[i]).addRegOrImmOperands(Inst, 1);
1797 /// Force static initialization.
1798 extern "C" void LLVMInitializeAMDGPUAsmParser() {
1799 RegisterMCAsmParser<AMDGPUAsmParser> A(TheAMDGPUTarget);
1800 RegisterMCAsmParser<AMDGPUAsmParser> B(TheGCNTarget);
1803 #define GET_REGISTER_MATCHER
1804 #define GET_MATCHER_IMPLEMENTATION
1805 #include "AMDGPUGenAsmMatcher.inc"