[llvm] [CodeGen] Hackt solution to X86 CodeGen extern symbol duplication issue (PR #106737)

weiwei chen via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 30 07:19:09 PDT 2024


https://github.com/weiweichen created https://github.com/llvm/llvm-project/pull/106737

None

>From 3214f1aa08d2a4cf254993f672101c97e3c9efdc Mon Sep 17 00:00:00 2001
From: Weiwei Chen <weiwei.chen at modular.com>
Date: Fri, 30 Aug 2024 04:25:59 +0000
Subject: [PATCH] Hacky fix.

---
 .../llvm/ExecutionEngine/JITLink/JITLink.h        |  3 +++
 llvm/lib/Target/X86/X86MCInstLower.cpp            | 15 ++++++++++-----
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h b/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
index 8fe53760a852d7..8d4daa8a77b736 100644
--- a/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
+++ b/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
@@ -1212,7 +1212,10 @@ class LinkGraph {
   /// of 0.
   Symbol &addExternalSymbol(StringRef Name, orc::ExecutorAddrDiff Size,
                             bool IsWeaklyReferenced) {
+    if(ExternalSymbols.contains(Name))
+       llvm::dbgs() << "Duplicate external symbol: " << Name << "\n";
     assert(!ExternalSymbols.contains(Name) && "Duplicate external symbol");
+
     auto &Sym = Symbol::constructExternal(
         Allocator, createAddressable(orc::ExecutorAddr(), false), Name, Size,
         Linkage::Strong, IsWeaklyReferenced);
diff --git a/llvm/lib/Target/X86/X86MCInstLower.cpp b/llvm/lib/Target/X86/X86MCInstLower.cpp
index 77ddd2366e629e..d01085fd5a532c 100644
--- a/llvm/lib/Target/X86/X86MCInstLower.cpp
+++ b/llvm/lib/Target/X86/X86MCInstLower.cpp
@@ -80,7 +80,7 @@ class X86MCInstLower {
                                 const MachineOperand &MO) const;
   void Lower(const MachineInstr *MI, MCInst &OutMI) const;
 
-  MCSymbol *GetSymbolFromOperand(const MachineOperand &MO) const;
+  MCSymbol *GetSymbolFromOperand(const MachineOperand &MO, MCContext* ExternalContext=nullptr) const;
   MCOperand LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const;
 
 private:
@@ -152,7 +152,7 @@ MachineModuleInfoMachO &X86MCInstLower::getMachOMMI() const {
 
 /// GetSymbolFromOperand - Lower an MO_GlobalAddress or MO_ExternalSymbol
 /// operand to an MCSymbol.
-MCSymbol *X86MCInstLower::GetSymbolFromOperand(const MachineOperand &MO) const {
+MCSymbol *X86MCInstLower::GetSymbolFromOperand(const MachineOperand &MO, MCContext* ExternalContext) const {
   const Triple &TT = TM.getTargetTriple();
   if (MO.isGlobal() && TT.isOSBinFormatELF())
     return AsmPrinter.getSymbolPreferLocal(*MO.getGlobal());
@@ -193,8 +193,13 @@ MCSymbol *X86MCInstLower::GetSymbolFromOperand(const MachineOperand &MO) const {
   }
 
   Name += Suffix;
-  if (!Sym)
-    Sym = Ctx.getOrCreateSymbol(Name);
+  if (!Sym) {
+    if (ExternalContext) {
+      Sym = ExternalContext->getOrCreateSymbol(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.
@@ -351,7 +356,7 @@ MCOperand X86MCInstLower::LowerMachineOperand(const MachineInstr *MI,
   case MachineOperand::MO_MachineBasicBlock:
   case MachineOperand::MO_GlobalAddress:
   case MachineOperand::MO_ExternalSymbol:
-    return LowerSymbolOperand(MO, GetSymbolFromOperand(MO));
+    return LowerSymbolOperand(MO, GetSymbolFromOperand(MO, &AsmPrinter.OutContext));
   case MachineOperand::MO_MCSymbol:
     return LowerSymbolOperand(MO, MO.getMCSymbol());
   case MachineOperand::MO_JumpTableIndex:



More information about the llvm-commits mailing list