[llvm] b54d214 - [llvm][RISCV][MC] Fix arithmetic error in pseudo parsing (#200217)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 2 08:18:36 PDT 2026


Author: Paul Kirth
Date: 2026-06-02T08:18:12-07:00
New Revision: b54d214b77aac2bab9b949bb4bdd3cc511fbe944

URL: https://github.com/llvm/llvm-project/commit/b54d214b77aac2bab9b949bb4bdd3cc511fbe944
DIFF: https://github.com/llvm/llvm-project/commit/b54d214b77aac2bab9b949bb4bdd3cc511fbe944.diff

LOG: [llvm][RISCV][MC] Fix arithmetic error in pseudo parsing (#200217)

When arithmetic was used in RISCV pseudo instructions, like lla, the
custom parsing breaks the associativity of the operators by parsing the
entire remainder of an expression. This would lead to scenarios where we
would parse `top - 0x100 -0x10` as `top - (0x100 -0x10)` and yield the
wrong offset (`0xF0`(wrong) vs. `0x110`(correct)).

Instead, don't advance the parser and just examine the token to
determine if it's an identifier and if we should handle this case or
bail out. In cases we do handle, just use `parseExpression()`, which
will handle this correctly.

Added: 
    

Modified: 
    llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
    llvm/test/MC/RISCV/rvi-pseudos.s

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp b/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
index 66ed507cf36f4..93fb1fa5a889f 100644
--- a/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
+++ b/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
@@ -2235,45 +2235,19 @@ ParseStatus RISCVAsmParser::parseBareSymbol(OperandVector &Operands) {
   if (getLexer().getKind() != AsmToken::Identifier)
     return ParseStatus::NoMatch;
 
-  StringRef Identifier;
-  AsmToken Tok = getLexer().getTok();
-
-  if (getParser().parseIdentifier(Identifier))
-    return ParseStatus::Failure;
-
-  SMLoc E = SMLoc::getFromPointer(S.getPointer() + Identifier.size());
-
+  StringRef Identifier = getTok().getIdentifier();
   MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
 
   if (Sym->isVariable()) {
     const MCExpr *V = Sym->getVariableValue();
-    if (!isa<MCSymbolRefExpr>(V)) {
-      getLexer().UnLex(Tok); // Put back if it's not a bare symbol.
+    if (!isa<MCSymbolRefExpr>(V))
       return ParseStatus::NoMatch;
-    }
-    Res = V;
-  } else
-    Res = MCSymbolRefExpr::create(Sym, getContext());
-
-  MCBinaryExpr::Opcode Opcode;
-  switch (getLexer().getKind()) {
-  default:
-    Operands.push_back(RISCVOperand::createExpr(Res, S, E, isRV64()));
-    return ParseStatus::Success;
-  case AsmToken::Plus:
-    Opcode = MCBinaryExpr::Add;
-    getLexer().Lex();
-    break;
-  case AsmToken::Minus:
-    Opcode = MCBinaryExpr::Sub;
-    getLexer().Lex();
-    break;
   }
 
-  const MCExpr *Expr;
-  if (getParser().parseExpression(Expr, E))
+  SMLoc E;
+  if (getParser().parseExpression(Res, E))
     return ParseStatus::Failure;
-  Res = MCBinaryExpr::create(Opcode, Res, Expr, getContext());
+
   Operands.push_back(RISCVOperand::createExpr(Res, S, E, isRV64()));
   return ParseStatus::Success;
 }

diff  --git a/llvm/test/MC/RISCV/rvi-pseudos.s b/llvm/test/MC/RISCV/rvi-pseudos.s
index 6c5307ca418ee..d128dcf084b69 100644
--- a/llvm/test/MC/RISCV/rvi-pseudos.s
+++ b/llvm/test/MC/RISCV/rvi-pseudos.s
@@ -229,3 +229,9 @@ lla a5, (0xFF + a_symbol) - 4
 # CHECK: auipc a5, %pcrel_hi(a_symbol-4)
 # CHECK: addi  a5, a5, %pcrel_lo(.Lpcrel_hi37)
 lla a5, a_symbol - 4
+
+## Check that subtraction associativity is respected
+# CHECK-LABEL: .Lpcrel_hi38:
+# CHECK-NEXT: auipc sp, %pcrel_hi(top-256-16)
+# CHECK-NEXT: addi  sp, sp, %pcrel_lo(.Lpcrel_hi38)
+lla sp, top - 0x100 - 0x10


        


More information about the llvm-commits mailing list