[llvm] Set hidden visibility for weak prefetch target symbols to avoid relocation error in shared libraries. (PR #200242)
Rahman Lavaee via llvm-commits
llvm-commits at lists.llvm.org
Thu May 28 11:56:08 PDT 2026
https://github.com/rlavaee updated https://github.com/llvm/llvm-project/pull/200242
>From d253691db64392399928de2ec66acfccab06c273 Mon Sep 17 00:00:00 2001
From: Rahman Lavaee <rahmanl at google.com>
Date: Thu, 28 May 2026 18:33:07 +0000
Subject: [PATCH 1/2] Set hidden visibility for weak prefetch target symbols to
avoid relocation errors in shared libraries
---
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 8 +++++++-
llvm/lib/CodeGen/InsertCodePrefetch.cpp | 1 +
.../CodeGen/X86/basic-block-sections-code-prefetch.ll | 2 ++
3 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 17fd80e81a673..71ae34c881ed3 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -2016,9 +2016,15 @@ void AsmPrinter::emitPrefetchTargetSymbol(const UniqueBBID &BBID,
getPrefetchTargetSymbolName(FunctionName, BBID, CallsiteIndex));
// If the function is weak-linkage it may be replaced by a strong
// version, in which case the prefetch targets should also be replaced.
+ bool IsWeak = MF->getFunction().isWeakForLinker();
OutStreamer->emitSymbolAttribute(
PrefetchTargetSymbol,
- MF->getFunction().isWeakForLinker() ? MCSA_Weak : MCSA_Global);
+ IsWeak ? MCSA_Weak : MCSA_Global);
+ if (IsWeak) {
+ // Weak prefetch target symbols must have hidden visibility to avoid
+ // relocation error in shared libraries.
+ OutStreamer->emitSymbolAttribute(PrefetchTargetSymbol, MCSA_Hidden);
+ }
OutStreamer->emitLabel(PrefetchTargetSymbol);
}
diff --git a/llvm/lib/CodeGen/InsertCodePrefetch.cpp b/llvm/lib/CodeGen/InsertCodePrefetch.cpp
index d6db34aed9b42..272ea83351882 100644
--- a/llvm/lib/CodeGen/InsertCodePrefetch.cpp
+++ b/llvm/lib/CodeGen/InsertCodePrefetch.cpp
@@ -163,6 +163,7 @@ insertPrefetchHints(MachineFunction &MF,
// available.
if (!WeakFallbackSym->isBindingSet()) {
WeakFallbackSym->setBinding(ELF::STB_WEAK);
+ WeakFallbackSym->setVisibility(ELF::STV_HIDDEN);
PrefetchInstr->setPostInstrSymbol(MF, WeakFallbackSym);
}
}
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 ce0d5a694b06d..da3d9a24cf294 100644
--- a/llvm/test/CodeGen/X86/basic-block-sections-code-prefetch.ll
+++ b/llvm/test/CodeGen/X86/basic-block-sections-code-prefetch.ll
@@ -67,8 +67,10 @@ define weak i32 @bar() nounwind {
ret i32 %call
; CHECK: bar:
; CHECK-NEXT: .weak __llvm_prefetch_target_bar_21_1
+; CHECK-NEXT: .hidden __llvm_prefetch_target_bar_21_1
; CHECK-NEXT: __llvm_prefetch_target_bar_21_1:
; CHECK-NEXT: .weak __llvm_prefetch_target_bar_0_0
+; CHECK-NEXT: .hidden __llvm_prefetch_target_bar_0_0
; CHECK-NEXT: __llvm_prefetch_target_bar_0_0:
; CHECK: callq baz at PLT
; CHECK-NEXT: prefetchit1 __llvm_prefetch_target_foo_0_0(%rip)
>From e5266cc672fedc9ed266de4522ea4ddd9fbce29d Mon Sep 17 00:00:00 2001
From: Rahman Lavaee <rahmanl at google.com>
Date: Thu, 28 May 2026 18:55:53 +0000
Subject: [PATCH 2/2] clang-format.
---
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 71ae34c881ed3..4cbd0e26e38d2 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -2017,9 +2017,8 @@ void AsmPrinter::emitPrefetchTargetSymbol(const UniqueBBID &BBID,
// If the function is weak-linkage it may be replaced by a strong
// version, in which case the prefetch targets should also be replaced.
bool IsWeak = MF->getFunction().isWeakForLinker();
- OutStreamer->emitSymbolAttribute(
- PrefetchTargetSymbol,
- IsWeak ? MCSA_Weak : MCSA_Global);
+ OutStreamer->emitSymbolAttribute(PrefetchTargetSymbol,
+ IsWeak ? MCSA_Weak : MCSA_Global);
if (IsWeak) {
// Weak prefetch target symbols must have hidden visibility to avoid
// relocation error in shared libraries.
More information about the llvm-commits
mailing list