[llvm] [InsertCodePrefetch,X86] Prevent duplicate prefetch fallback symbols (PR #195348)

Rahman Lavaee via llvm-commits llvm-commits at lists.llvm.org
Thu May 7 10:46:37 PDT 2026


https://github.com/rlavaee updated https://github.com/llvm/llvm-project/pull/195348

>From 4553cee4e57a792afd0856581db6d0cae301c89b Mon Sep 17 00:00:00 2001
From: Rahman Lavaee <rahmanl at google.com>
Date: Fri, 1 May 2026 19:39:43 +0000
Subject: [PATCH] [InsertCodePrefetch] Prevent duplicate prefetch fallback
 symbols

When multiple prefetch hints target the same external symbol, only the
first one should provide the weak fallback definition. This avoids
multiple definitions of the same symbol in the generated assembly.
---
 llvm/lib/CodeGen/InsertCodePrefetch.cpp               | 11 +++++++++--
 .../CodeGen/X86/basic-block-sections-code-prefetch.ll |  2 ++
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/CodeGen/InsertCodePrefetch.cpp b/llvm/lib/CodeGen/InsertCodePrefetch.cpp
index 83a40605b5846..d6db34aed9b42 100644
--- a/llvm/lib/CodeGen/InsertCodePrefetch.cpp
+++ b/llvm/lib/CodeGen/InsertCodePrefetch.cpp
@@ -156,8 +156,15 @@ insertPrefetchHints(MachineFunction &MF,
           // __llvm_prefetch_target_foo_x_y:
           MCSymbolELF *WeakFallbackSym = static_cast<MCSymbolELF *>(
               MF.getContext().getOrCreateSymbol(TargetSymbolName));
-          WeakFallbackSym->setBinding(ELF::STB_WEAK);
-          PrefetchInstr->setPostInstrSymbol(MF, WeakFallbackSym);
+          // The fallback symbol may have been defined via another prefetch
+          // instruction in the same module, in which case we should not emit it
+          // here. Ideally, getOrCreateSymbol should tell us if the symbol
+          // existed, but we use `isBindingSet()` since that API is not
+          // available.
+          if (!WeakFallbackSym->isBindingSet()) {
+            WeakFallbackSym->setBinding(ELF::STB_WEAK);
+            PrefetchInstr->setPostInstrSymbol(MF, WeakFallbackSym);
+          }
         }
         PrefetchInserted = true;
         ++HintIt;
diff --git a/llvm/test/CodeGen/X86/basic-block-sections-code-prefetch.ll b/llvm/test/CodeGen/X86/basic-block-sections-code-prefetch.ll
index dec7557181090..ce0d5a694b06d 100644
--- a/llvm/test/CodeGen/X86/basic-block-sections-code-prefetch.ll
+++ b/llvm/test/CodeGen/X86/basic-block-sections-code-prefetch.ll
@@ -12,6 +12,7 @@
 ; RUN: echo 'i 3,0 bar,0,0' >> %t
 ; RUN: echo 'i 2,1 foo,1,0' >> %t
 ; RUN: echo 'i 1,1 extfunc,5,5' >> %t
+; RUN: echo 'i 1,1 extfunc,5,5' >> %t
 ; RUN: echo 'i 1,1 bar,0,0' >> %t
 ; RUN: echo 'f bar' >> %t
 ; RUN: echo 't 0,0' >> %t
@@ -40,6 +41,7 @@ cond.true:                                           ; preds = %1
 ; CHECK-NEXT:   prefetchit1     __llvm_prefetch_target_extfunc_5_5(%rip)
 ; CHECK-NEXT:   .weak __llvm_prefetch_target_extfunc_5_5
 ; CHECK-NEXT: __llvm_prefetch_target_extfunc_5_5:
+; CHECK-NEXT:   prefetchit1     __llvm_prefetch_target_extfunc_5_5(%rip)
 ; CHECK-NEXT:   prefetchit1	__llvm_prefetch_target_bar_0_0(%rip)
 
 cond.false:                                          ; preds = %1



More information about the llvm-commits mailing list