[llvm] [X86Backend] Use GetExternalSymbolSymbol for MO_ExternalSymbol. (PR #133352)

weiwei chen via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 27 20:00:10 PDT 2025


https://github.com/weiweichen updated https://github.com/llvm/llvm-project/pull/133352

>From 859ed81810432bf419ab661e31cc3256b3e3564f Mon Sep 17 00:00:00 2001
From: Weiwei Chen <weiwei.chen at modular.com>
Date: Fri, 28 Mar 2025 02:11:05 +0000
Subject: [PATCH 1/3] Use GetExternalSymbolSymbol for MO_ExternalSymbol.

---
 llvm/lib/Target/X86/X86MCInstLower.cpp | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Target/X86/X86MCInstLower.cpp b/llvm/lib/Target/X86/X86MCInstLower.cpp
index 07f99dbba6a36..599861bb6fb45 100644
--- a/llvm/lib/Target/X86/X86MCInstLower.cpp
+++ b/llvm/lib/Target/X86/X86MCInstLower.cpp
@@ -192,8 +192,15 @@ MCSymbol *X86MCInstLower::GetSymbolFromOperand(const MachineOperand &MO) const {
   }
 
   Name += Suffix;
-  if (!Sym)
-    Sym = Ctx.getOrCreateSymbol(Name);
+  if (!Sym) {
+    MCSymbol* S = Ctx.lookupSymbol(Name);
+    // If new MCSymbol needs to be created for
+    // MachineOperand::MO_ExternalSymbol, create is as an external symbol.
+    if(!S && MO.getType() == MachineOperand::MO_ExternalSymbol)
+      Sym = AsmPrinter.GetExternalSymbolSymbol(Name);
+    else
+      Sym = Ctx.getOrCreateSymbol(Name);
+  }
 
   // If the target flags on the operand changes the name of the symbol, do that
   // before we return the symbol.
@@ -349,12 +356,8 @@ MCOperand X86MCInstLower::LowerMachineOperand(const MachineInstr *MI,
     return MCOperand::createImm(MO.getImm());
   case MachineOperand::MO_MachineBasicBlock:
   case MachineOperand::MO_GlobalAddress:
-    return LowerSymbolOperand(MO, GetSymbolFromOperand(MO));
   case MachineOperand::MO_ExternalSymbol: {
-    MCSymbol *Sym = GetSymbolFromOperand(MO);
-    Sym->setExternal(true);
-    return LowerSymbolOperand(MO, Sym);
-  }
+    return LowerSymbolOperand(MO, GetSymbolFromOperand(MO));
   case MachineOperand::MO_MCSymbol:
     return LowerSymbolOperand(MO, MO.getMCSymbol());
   case MachineOperand::MO_JumpTableIndex:

>From ec3e34e3161b68c4f8e5a1a728d536e4f903de93 Mon Sep 17 00:00:00 2001
From: Weiwei Chen <weiwei.chen at modular.com>
Date: Fri, 28 Mar 2025 02:31:26 +0000
Subject: [PATCH 2/3] Fix formatting.

---
 llvm/lib/Target/X86/X86MCInstLower.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Target/X86/X86MCInstLower.cpp b/llvm/lib/Target/X86/X86MCInstLower.cpp
index e536b0d411b8b..b3383a63fb519 100644
--- a/llvm/lib/Target/X86/X86MCInstLower.cpp
+++ b/llvm/lib/Target/X86/X86MCInstLower.cpp
@@ -193,10 +193,10 @@ MCSymbol *X86MCInstLower::GetSymbolFromOperand(const MachineOperand &MO) const {
 
   Name += Suffix;
   if (!Sym) {
-    MCSymbol* S = Ctx.lookupSymbol(Name);
+    MCSymbol *S = Ctx.lookupSymbol(Name);
     // If new MCSymbol needs to be created for
     // MachineOperand::MO_ExternalSymbol, create is as an external symbol.
-    if(!S && MO.getType() == MachineOperand::MO_ExternalSymbol)
+    if (!S && MO.getType() == MachineOperand::MO_ExternalSymbol)
       Sym = AsmPrinter.GetExternalSymbolSymbol(Name);
     else
       Sym = Ctx.getOrCreateSymbol(Name);

>From cf82df020926ac0dc0b7d7bfa3fcd88ed447884c Mon Sep 17 00:00:00 2001
From: Weiwei Chen <weiwei.chen at modular.com>
Date: Fri, 28 Mar 2025 02:59:45 +0000
Subject: [PATCH 3/3] Make change more explicit.

---
 llvm/lib/Target/X86/X86MCInstLower.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Target/X86/X86MCInstLower.cpp b/llvm/lib/Target/X86/X86MCInstLower.cpp
index b3383a63fb519..927cda9cb3eba 100644
--- a/llvm/lib/Target/X86/X86MCInstLower.cpp
+++ b/llvm/lib/Target/X86/X86MCInstLower.cpp
@@ -193,11 +193,11 @@ MCSymbol *X86MCInstLower::GetSymbolFromOperand(const MachineOperand &MO) const {
 
   Name += Suffix;
   if (!Sym) {
-    MCSymbol *S = Ctx.lookupSymbol(Name);
     // If new MCSymbol needs to be created for
-    // MachineOperand::MO_ExternalSymbol, create is as an external symbol.
-    if (!S && MO.getType() == MachineOperand::MO_ExternalSymbol)
-      Sym = AsmPrinter.GetExternalSymbolSymbol(Name);
+    // MachineOperand::MO_ExternalSymbol, create it as an symbol
+    // in AsmPrinter's OutContext.
+    if (MO.isSymbol())
+      Sym = AsmPrinter.OutContext.getOrCreateSymbol(Name);
     else
       Sym = Ctx.getOrCreateSymbol(Name);
   }



More information about the llvm-commits mailing list