[llvm] MipsAsmParser/O32: Don't add redundant $ to $-prefixed symbol in the la macro (PR #80644)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 13 22:10:00 PST 2024


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

>From 43d59dee0930bf922773f7d009b2db7297db7972 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 1/4] 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"

>From c8f05b19b3a3191e303cfe2c899ef0300014feee Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Tue, 13 Feb 2024 00:34:59 -0800
Subject: [PATCH 2/4] Update MipsAsmParser.cpp

---
 llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp b/llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp
index 2ddd8a6f31756..b001b3782b853 100644
--- a/llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp
+++ b/llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp
@@ -1,4 +1,4 @@
-//===-- MipsAsmParser.cpp - Parse Mips assembly to MCInst instructions ----===//
+///===-- MipsAsmParser.cpp - Parse Mips assembly to MCInst instructions ----===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -2920,11 +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;
-    }
+    // For O32, "$"-prefixed symbols are recognized as temporary while
+    // .L-prefixed symbols are not (PrivateGlobalPrefix is "$"). Recognize ".L"
+    // manually.
+    if (ABI.IsO32() && 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

>From a760ac793b9915163fed1b275424b7830ba73758 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Tue, 13 Feb 2024 00:36:00 -0800
Subject: [PATCH 3/4] Update MipsAsmParser.cpp

---
 llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp b/llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp
index b001b3782b853..36aab383da68d 100644
--- a/llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp
+++ b/llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp
@@ -1,4 +1,4 @@
-///===-- MipsAsmParser.cpp - Parse Mips assembly to MCInst instructions ----===//
+//===-- MipsAsmParser.cpp - Parse Mips assembly to MCInst instructions ----===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.

>From 800d8be4eaed0838e68ff81b2f4fbe34085ab879 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Tue, 13 Feb 2024 22:03:43 -0800
Subject: [PATCH 4/4] Fold tests into macro-la-pic.s

---
 llvm/test/MC/Mips/macro-la-local.s | 23 -----------------------
 llvm/test/MC/Mips/macro-la-pic.s   | 22 ++++++++++++++++++++++
 2 files changed, 22 insertions(+), 23 deletions(-)
 delete mode 100644 llvm/test/MC/Mips/macro-la-local.s

diff --git a/llvm/test/MC/Mips/macro-la-local.s b/llvm/test/MC/Mips/macro-la-local.s
deleted file mode 100644
index 14c8e1b689ac9..0000000000000
--- a/llvm/test/MC/Mips/macro-la-local.s
+++ /dev/null
@@ -1,23 +0,0 @@
-# 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"
diff --git a/llvm/test/MC/Mips/macro-la-pic.s b/llvm/test/MC/Mips/macro-la-pic.s
index 2303f34c35bcf..1875952d80c4e 100644
--- a/llvm/test/MC/Mips/macro-la-pic.s
+++ b/llvm/test/MC/Mips/macro-la-pic.s
@@ -255,3 +255,25 @@ la $25, 2f
 # XN32: lw $25, %got_disp(.Ltmp1)($gp)  # encoding: [0x8f,0x99,A,A]
 # XN32:                                 #   fixup A - offset: 0, value: %got_disp(.Ltmp1), kind: fixup_Mips_GOT_DISP
 2:
+
+la $2,.Lstr
+# O32:      lw      $2, %got(.Lstr)($gp)          # encoding: [0x8f,0x82,A,A]
+# O32-NEXT:                           #   fixup A - offset: 0, value: %got(.Lstr), kind: fixup_Mips_GOT
+# O32-NEXT: addiu   $2, $2, %lo(.Lstr)            # encoding: [0x24,0x42,A,A]
+# O32-NEXT:                           #   fixup A - offset: 0, value: %lo(.Lstr), kind: fixup_Mips_LO16
+
+# N32:      lw      $2, %got_disp(.Lstr)($gp)     # encoding: [0x8f,0x82,A,A]
+# N32-NEXT:                           #   fixup A - offset: 0, value: %got_disp(.Lstr), kind: fixup_Mips_GOT_DISP
+
+la $2,$str2
+# O32:      lw      $2, %got($str2)($gp)          # encoding: [0x8f,0x82,A,A]
+# O32-NEXT: #   fixup A - offset: 0, value: %got($str2), kind: fixup_Mips_GOT
+# O32-NEXT: addiu   $2, $2, %lo($str2)            # encoding: [0x24,0x42,A,A]
+# O32-NEXT: #   fixup A - offset: 0, value: %lo($str2), kind: fixup_Mips_LO16
+
+# N32:      lw      $2, %got_disp($str2)($gp)     # encoding: [0x8f,0x82,A,A]
+# N32-NEXT:                           #   fixup A - offset: 0, value: %got_disp($str2), kind: fixup_Mips_GOT_DISP
+
+.rodata
+.Lstr: .4byte 0
+$str2: .4byte 0



More information about the llvm-commits mailing list