Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AMDGPU] Allow lit() on operands which do not accept modifiers #69527

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1577,9 +1577,11 @@ class AMDGPUAsmParser : public MCTargetAsmParser {
bool isNamedOperandModifier(const AsmToken &Token, const AsmToken &NextToken) const;
bool isOpcodeModifierWithVal(const AsmToken &Token, const AsmToken &NextToken) const;
bool parseSP3NegModifier();
ParseStatus parseImm(OperandVector &Operands, bool HasSP3AbsModifier = false);
ParseStatus parseImm(OperandVector &Operands, bool HasSP3AbsModifier = false,
bool HasLit = false);
ParseStatus parseReg(OperandVector &Operands);
ParseStatus parseRegOrImm(OperandVector &Operands, bool HasSP3AbsMod = false);
ParseStatus parseRegOrImm(OperandVector &Operands, bool HasSP3AbsMod = false,
bool HasLit = false);
ParseStatus parseRegOrImmWithFPInputMods(OperandVector &Operands,
bool AllowImm = true);
ParseStatus parseRegOrImmWithIntInputMods(OperandVector &Operands,
Expand Down Expand Up @@ -2904,13 +2906,26 @@ AMDGPUAsmParser::parseRegister(bool RestoreOnFailure) {
}

ParseStatus AMDGPUAsmParser::parseImm(OperandVector &Operands,
bool HasSP3AbsModifier) {
bool HasSP3AbsModifier, bool HasLit) {
// TODO: add syntactic sugar for 1/(2*PI)

if (isRegister())
return ParseStatus::NoMatch;
assert(!isModifier());

if (!HasLit) {
HasLit = trySkipId("lit");
if (HasLit) {
if (!skipToken(AsmToken::LParen, "expected left paren after lit"))
return ParseStatus::Failure;
ParseStatus S = parseImm(Operands, HasSP3AbsModifier, HasLit);
if (S.isSuccess() &&
!skipToken(AsmToken::RParen, "expected closing parentheses"))
return ParseStatus::Failure;
return S;
}
}

const auto& Tok = getToken();
const auto& NextTok = peekToken();
bool IsReal = Tok.is(AsmToken::Real);
Expand All @@ -2923,6 +2938,9 @@ ParseStatus AMDGPUAsmParser::parseImm(OperandVector &Operands,
Negate = true;
}

AMDGPUOperand::Modifiers Mods;
Mods.Lit = HasLit;

if (IsReal) {
// Floating-point expressions are not supported.
// Can only allow floating-point literals with an
Expand All @@ -2941,6 +2959,8 @@ ParseStatus AMDGPUAsmParser::parseImm(OperandVector &Operands,
Operands.push_back(
AMDGPUOperand::CreateImm(this, RealVal.bitcastToAPInt().getZExtValue(), S,
AMDGPUOperand::ImmTyNone, true));
AMDGPUOperand &Op = static_cast<AMDGPUOperand &>(*Operands.back());
Op.setModifiers(Mods);

return ParseStatus::Success;

Expand All @@ -2967,7 +2987,11 @@ ParseStatus AMDGPUAsmParser::parseImm(OperandVector &Operands,

if (Expr->evaluateAsAbsolute(IntVal)) {
Operands.push_back(AMDGPUOperand::CreateImm(this, IntVal, S));
AMDGPUOperand &Op = static_cast<AMDGPUOperand &>(*Operands.back());
Op.setModifiers(Mods);
} else {
if (HasLit)
return ParseStatus::NoMatch;
Operands.push_back(AMDGPUOperand::CreateExpr(this, Expr, S));
}

Expand All @@ -2990,20 +3014,20 @@ ParseStatus AMDGPUAsmParser::parseReg(OperandVector &Operands) {
}

ParseStatus AMDGPUAsmParser::parseRegOrImm(OperandVector &Operands,
bool HasSP3AbsMod) {
bool HasSP3AbsMod, bool HasLit) {
ParseStatus Res = parseReg(Operands);
if (!Res.isNoMatch())
return Res;
if (isModifier())
return ParseStatus::NoMatch;
return parseImm(Operands, HasSP3AbsMod);
return parseImm(Operands, HasSP3AbsMod, HasLit);
}

bool
AMDGPUAsmParser::isNamedOperandModifier(const AsmToken &Token, const AsmToken &NextToken) const {
if (Token.is(AsmToken::Identifier) && NextToken.is(AsmToken::LParen)) {
const auto &str = Token.getString();
return str == "abs" || str == "neg" || str == "sext" || str == "lit";
return str == "abs" || str == "neg" || str == "sext";
}
return false;
}
Expand Down Expand Up @@ -3123,7 +3147,7 @@ AMDGPUAsmParser::parseRegOrImmWithFPInputMods(OperandVector &Operands,

ParseStatus Res;
if (AllowImm) {
Res = parseRegOrImm(Operands, SP3Abs);
Res = parseRegOrImm(Operands, SP3Abs, Lit);
} else {
Res = parseReg(Operands);
}
Expand Down
10 changes: 10 additions & 0 deletions llvm/test/MC/AMDGPU/literals.s
Original file line number Diff line number Diff line change
Expand Up @@ -925,3 +925,13 @@ v_sqrt_f32 v2, lit(123.0
// NOSICI: :[[@LINE+2]]:{{[0-9]+}}: error: expected immediate with lit modifier
// NOGFX89: :[[@LINE+1]]:{{[0-9]+}}: error: expected immediate with lit modifier
v_sqrt_f32 v2, lit(v1)

// Make sure lit() is accepted on operands without modifiers.

// SICI: v_madak_f32 v4, 0x7e8, v8, 0x7e8 ; encoding: [0xff,0x10,0x08,0x42,0xe8,0x07,0x00,0x00]
// GFX89: v_madak_f32 v4, 0x7e8, v8, 0x7e8 ; encoding: [0xff,0x10,0x08,0x30,0xe8,0x07,0x00,0x00]
v_madak_f32 v4, lit(0x7e8), v8, lit(0x7e8)

// NOSICI: :[[@LINE+2]]:{{[0-9]+}}: error: not a valid operand.
// NOGFX89: :[[@LINE+1]]:{{[0-9]+}}: error: not a valid operand.
v_madak_f32 v4, lit(lit(0x7e8)), v8, lit(0x7e8)