[llvm] MIPS/Asm/O32: Don't add another $ to PrivateGlobal symbol (PR #80644)

YunQiang Su via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 5 17:19:13 PST 2024


https://github.com/wzssyqa updated https://github.com/llvm/llvm-project/pull/80644

>From 1c6dccc8be1c2713bf1583eec035a2912a862efc Mon Sep 17 00:00:00 2001
From: YunQiang Su <syq at gcc.gnu.org>
Date: Sun, 4 Feb 2024 14:46:10 +0800
Subject: [PATCH] MIPS/Asm/O32: Don't add another $ to PrivateGlobal symbol

For asm code like:
xx:
	la	$2,$yy
$yy:
	nop

MIPS O32 use `$` as PrivateGlobalPrefix, while another
`$` is added for getOrCreateSymbol, thus:
  error: Undefined temporary symbol $$yy

We also set symbols that starts with `.L` as local for O32.

See: #65020.
---
 .../Target/Mips/AsmParser/MipsAsmParser.cpp   |  7 +++++-
 llvm/test/CodeGen/Mips/hf1_body.ll            |  4 ++--
 llvm/test/MC/Mips/macro-la-local.s            | 23 +++++++++++++++++++
 3 files changed, 31 insertions(+), 3 deletions(-)
 create mode 100644 llvm/test/MC/Mips/macro-la-local.s

diff --git a/llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp b/llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp
index 3c673ae938fde..2ddd8a6f31756 100644
--- a/llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp
+++ b/llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp
@@ -2920,6 +2920,11 @@ bool MipsAsmParser::loadAndAddSymbolAddress(const MCExpr *SymExpr,
         (Res.getSymA()->getSymbol().isELF() &&
          cast<MCSymbolELF>(Res.getSymA()->getSymbol()).getBinding() ==
              ELF::STB_LOCAL);
+    if (!IsLocalSym && ABI.IsO32()) {
+      // PrivateGlobalPrefix for O32 is '$', while we support '.L' anyway.
+      if (Res.getSymA()->getSymbol().getName().starts_with(".L"))
+        IsLocalSym = true;
+    }
     bool UseXGOT = STI->hasFeature(Mips::FeatureXGOT) && !IsLocalSym;
 
     // The case where the result register is $25 is somewhat special. If the
@@ -6359,7 +6364,7 @@ bool MipsAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
       return true;
 
     SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
-    MCSymbol *Sym = getContext().getOrCreateSymbol("$" + Identifier);
+    MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
     // Otherwise create a symbol reference.
     const MCExpr *SymRef =
         MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
diff --git a/llvm/test/CodeGen/Mips/hf1_body.ll b/llvm/test/CodeGen/Mips/hf1_body.ll
index 184ea31bddc9d..c3dea67896210 100644
--- a/llvm/test/CodeGen/Mips/hf1_body.ll
+++ b/llvm/test/CodeGen/Mips/hf1_body.ll
@@ -23,8 +23,8 @@ entry:
 ; ALL:       .set reorder
 ; ALL:       .reloc 0, R_MIPS_NONE, v_sf
 ; GAS:       la $25, $__fn_local_v_sf
-; IAS:       lw $25, %got($$__fn_local_v_sf)($gp)
-; IAS:       addiu $25, $25, %lo($$__fn_local_v_sf)
+; IAS:       lw $25, %got($__fn_local_v_sf)($gp)
+; IAS:       addiu $25, $25, %lo($__fn_local_v_sf)
 ; ALL:       mfc1 $4, $f12
 ; ALL:       jr $25
 ; ALL:       .end __fn_stub_v_sf
diff --git a/llvm/test/MC/Mips/macro-la-local.s b/llvm/test/MC/Mips/macro-la-local.s
new file mode 100644
index 0000000000000..14c8e1b689ac9
--- /dev/null
+++ b/llvm/test/MC/Mips/macro-la-local.s
@@ -0,0 +1,23 @@
+# RUN: llvm-mc %s -triple=mips-unknown-linux -show-encoding -mcpu=mips32r2 | \
+# RUN:   FileCheck %s --check-prefixes=CHECK
+# RUN: llvm-mc %s -triple=mips-unknown-linux -show-encoding -mcpu=mips32r6 | \
+# RUN:   FileCheck %s --check-prefixes=CHECK
+
+	.text
+	.abicalls
+	.option	pic2
+xx:
+	la	$2,.Lhello #CHECK: lw      $2, %got(.Lhello)($gp)          # encoding: [0x8f,0x82,A,A]
+                           #CHECK: #   fixup A - offset: 0, value: %got(.Lhello), kind: fixup_Mips_GOT
+                           #CHECK: addiu   $2, $2, %lo(.Lhello)            # encoding: [0x24,0x42,A,A]
+                           #CHECK: #   fixup A - offset: 0, value: %lo(.Lhello), kind: fixup_Mips_LO16
+
+	la	$2,$hello2 #CHECK: lw      $2, %got($hello2)($gp)          # encoding: [0x8f,0x82,A,A]
+                           #CHECK: #   fixup A - offset: 0, value: %got($hello2), kind: fixup_Mips_GOT
+                           #CHECK: addiu   $2, $2, %lo($hello2)            # encoding: [0x24,0x42,A,A]
+                           #CHECK: #   fixup A - offset: 0, value: %lo($hello2), kind: fixup_Mips_LO16
+	.rdata
+.Lhello:
+	.asciz "Hello world\n"
+$hello2:
+	.asciz "Hello world\n"



More information about the llvm-commits mailing list