[lld] Resolve undefined prefetch targets to zero to effectively prefetch the next instruction. (PR #174448)

Rahman Lavaee via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 5 09:27:33 PST 2026


https://github.com/rlavaee created https://github.com/llvm/llvm-project/pull/174448

This PR handles the undefined symbol resolution described in **https://discourse.llvm.org/t/rfc-code-prefetch-insertion/88668**.

Prefetch directives are specified via a post-link profile based on the symbol names and SHT_LLVM_BB_ADDR_MAP from a previous build. Prefetch targets are defined as global to allow prefetching across different modules. So we must guard against undefined symbols. This PR handles it by resolving undefined prefetch targets as zero. The prefetch instruction will then prefetch the next instruction which is less intrusive than prefetching a random address.

>From 95b5f54784cdabccea6cf353be80a3588be311c9 Mon Sep 17 00:00:00 2001
From: Rahman Lavaee <rahmanl at google.com>
Date: Sat, 27 Dec 2025 08:20:06 +0000
Subject: [PATCH 1/4] Handle __llvm_prefetch_target symbols in LLD.

---
 lld/ELF/Relocations.cpp                  | 16 ++++++++++++++++
 lld/test/ELF/prefetch-undefined-symbol.s | 19 +++++++++++++++++++
 2 files changed, 35 insertions(+)
 create mode 100644 lld/test/ELF/prefetch-undefined-symbol.s

diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index 59aa43036ce01..27d78054125fe 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -50,6 +50,8 @@ using namespace llvm::support::endian;
 using namespace lld;
 using namespace lld::elf;
 
+constexpr const char prefetchSymbolPrefix[] = "__llvm_prefetch_target_";
+
 static void printDefinedLocation(ELFSyncStream &s, const Symbol &sym) {
   s << "\n>>> defined in " << sym.file;
 }
@@ -670,6 +672,12 @@ bool RelocScan::maybeReportUndefined(Undefined &sym, uint64_t offset) {
   if (ctx.arg.unresolvedSymbols == UnresolvedPolicy::Ignore && canBeExternal)
     return false;
 
+  // Prefetch target symbols are specified through profiling which can be
+  // inaccurate.
+  if (sym.getName().starts_with(prefetchSymbolPrefix)) {
+    return false;
+  }
+
   // clang (as of 2019-06-12) / gcc (as of 8.2.1) PPC64 may emit a .rela.toc
   // which references a switch table in a discarded .rodata/.text section. The
   // .toc and the .rela.toc are incorrectly not placed in the comdat. The ELF
@@ -1131,6 +1139,14 @@ void RelocScan::process(RelExpr expr, RelType type, uint64_t offset,
     }
   }
 
+  if (sym.getName().starts_with(prefetchSymbolPrefix)) {
+    dbgs() << "Sym: " << sym.getName() << " " << offset  << " " << addend << "\n";
+    // Prefetch target symbols may be undefined due to inaccurate profiles.
+    // The unresolved relocation will result in 0x0, effectively prefetching the
+    // next instruction.
+    return;
+  }
+
   auto diag = Err(ctx);
   diag << "relocation " << type << " cannot be used against ";
   if (sym.getName().empty())
diff --git a/lld/test/ELF/prefetch-undefined-symbol.s b/lld/test/ELF/prefetch-undefined-symbol.s
new file mode 100644
index 0000000000000..56e5da22d6697
--- /dev/null
+++ b/lld/test/ELF/prefetch-undefined-symbol.s
@@ -0,0 +1,19 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+# RUN: ld.lld %t -o %tout
+# RUN: llvm-nm -D %tout
+
+	.global foo
+	.local bar
+	.global __llvm_prefetch_target_bar
+
+	.section .text.foo,"ax",%progbits
+foo:
+	prefetchit1 __llvm_prefetch_target_bar(%rip)
+	prefetchit1 __llvm_prefetch_target_baz(%rip)
+
+	.section .text.bar,"ax",%progbits
+bar:
+__llvm_prefetch_target_bar:
+        nop
+

>From 00bcf163cd2b3247cc10b04622dee8dffdbb705e Mon Sep 17 00:00:00 2001
From: Rahman Lavaee <rahmanl at google.com>
Date: Fri, 2 Jan 2026 07:40:32 +0000
Subject: [PATCH 2/4] Properly handle undefined prefetch target symbols.

---
 lld/ELF/InputSection.cpp                 |  3 +++
 lld/ELF/Relocations.cpp                  | 21 +++++++++++----------
 lld/ELF/Relocations.h                    |  3 +++
 lld/test/ELF/prefetch-undefined-symbol.s |  4 +++-
 4 files changed, 20 insertions(+), 11 deletions(-)

diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index ff7ef2dce5c79..8c9e087e12f11 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -772,6 +772,9 @@ static int64_t getTlsTpOffset(Ctx &ctx, const Symbol &s) {
 
 uint64_t InputSectionBase::getRelocTargetVA(Ctx &ctx, const Relocation &r,
                                             uint64_t p) const {
+  if (r.sym->isUndefined() && r.sym->getName().starts_with(prefetchSymbolPrefix)) {
+    return 0;
+  }
   int64_t a = r.addend;
   switch (r.expr) {
   case R_ABS:
diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index 27d78054125fe..66aa88879b236 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -50,8 +50,6 @@ using namespace llvm::support::endian;
 using namespace lld;
 using namespace lld::elf;
 
-constexpr const char prefetchSymbolPrefix[] = "__llvm_prefetch_target_";
-
 static void printDefinedLocation(ELFSyncStream &s, const Symbol &sym) {
   s << "\n>>> defined in " << sym.file;
 }
@@ -959,6 +957,17 @@ void RelocScan::process(RelExpr expr, RelType type, uint64_t offset,
     return;
   }
 
+  if (sym.getName().starts_with(prefetchSymbolPrefix)) {
+    sec->addReloc({expr, type, offset, addend, &sym});
+    dbgs() << "MY Sym: " << sym.getName() << " " << offset  << " " << addend << "\n";
+    // Prefetch target symbols may be undefined due to inaccurate profiles.
+    // The unresolved relocation will result in 0x0, effectively prefetching the
+    // next instruction.
+    return;
+  }
+
+
+
   if (needsGot(expr)) {
     if (ctx.arg.emachine == EM_MIPS) {
       // MIPS ABI has special rules to process GOT entries and doesn't
@@ -1139,14 +1148,6 @@ void RelocScan::process(RelExpr expr, RelType type, uint64_t offset,
     }
   }
 
-  if (sym.getName().starts_with(prefetchSymbolPrefix)) {
-    dbgs() << "Sym: " << sym.getName() << " " << offset  << " " << addend << "\n";
-    // Prefetch target symbols may be undefined due to inaccurate profiles.
-    // The unresolved relocation will result in 0x0, effectively prefetching the
-    // next instruction.
-    return;
-  }
-
   auto diag = Err(ctx);
   diag << "relocation " << type << " cannot be used against ";
   if (sym.getName().empty())
diff --git a/lld/ELF/Relocations.h b/lld/ELF/Relocations.h
index 86ca298cd7a56..6f17f4e182ffc 100644
--- a/lld/ELF/Relocations.h
+++ b/lld/ELF/Relocations.h
@@ -16,6 +16,9 @@
 #include <vector>
 
 namespace lld::elf {
+
+constexpr const char prefetchSymbolPrefix[] = "__llvm_prefetch_target_";
+
 struct Ctx;
 struct ELFSyncStream;
 class Defined;
diff --git a/lld/test/ELF/prefetch-undefined-symbol.s b/lld/test/ELF/prefetch-undefined-symbol.s
index 56e5da22d6697..c7872dbd989c7 100644
--- a/lld/test/ELF/prefetch-undefined-symbol.s
+++ b/lld/test/ELF/prefetch-undefined-symbol.s
@@ -1,7 +1,7 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 # RUN: ld.lld %t -o %tout
-# RUN: llvm-nm -D %tout
+# RUN: llvm-objdump -d %tout | FileCheck %s
 
 	.global foo
 	.local bar
@@ -9,7 +9,9 @@
 
 	.section .text.foo,"ax",%progbits
 foo:
+# CHECK:      prefetchit1 0x7(%rip)
 	prefetchit1 __llvm_prefetch_target_bar(%rip)
+# CHECK-NEXT: prefetchit1 (%rip)
 	prefetchit1 __llvm_prefetch_target_baz(%rip)
 
 	.section .text.bar,"ax",%progbits

>From 9deb6072da140e69318af44e5f79462679e78938 Mon Sep 17 00:00:00 2001
From: Rahman Lavaee <rahmanl at google.com>
Date: Fri, 2 Jan 2026 07:40:50 +0000
Subject: [PATCH 3/4] clang-format.

---
 lld/ELF/InputSection.cpp | 3 ++-
 lld/ELF/Relocations.cpp  | 5 ++---
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index 8c9e087e12f11..4162f6125bb1e 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -772,7 +772,8 @@ static int64_t getTlsTpOffset(Ctx &ctx, const Symbol &s) {
 
 uint64_t InputSectionBase::getRelocTargetVA(Ctx &ctx, const Relocation &r,
                                             uint64_t p) const {
-  if (r.sym->isUndefined() && r.sym->getName().starts_with(prefetchSymbolPrefix)) {
+  if (r.sym->isUndefined() &&
+      r.sym->getName().starts_with(prefetchSymbolPrefix)) {
     return 0;
   }
   int64_t a = r.addend;
diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index 66aa88879b236..01a0d9bfbb347 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -959,15 +959,14 @@ void RelocScan::process(RelExpr expr, RelType type, uint64_t offset,
 
   if (sym.getName().starts_with(prefetchSymbolPrefix)) {
     sec->addReloc({expr, type, offset, addend, &sym});
-    dbgs() << "MY Sym: " << sym.getName() << " " << offset  << " " << addend << "\n";
+    dbgs() << "MY Sym: " << sym.getName() << " " << offset << " " << addend
+           << "\n";
     // Prefetch target symbols may be undefined due to inaccurate profiles.
     // The unresolved relocation will result in 0x0, effectively prefetching the
     // next instruction.
     return;
   }
 
-
-
   if (needsGot(expr)) {
     if (ctx.arg.emachine == EM_MIPS) {
       // MIPS ABI has special rules to process GOT entries and doesn't

>From c877ddcef6598918e58723ad13178e4148763b2a Mon Sep 17 00:00:00 2001
From: Rahman Lavaee <rahmanl at google.com>
Date: Mon, 5 Jan 2026 17:15:32 +0000
Subject: [PATCH 4/4] Add comment.

---
 lld/ELF/InputSection.cpp | 2 ++
 lld/ELF/Relocations.cpp  | 2 --
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp
index 4162f6125bb1e..316e4f2efc820 100644
--- a/lld/ELF/InputSection.cpp
+++ b/lld/ELF/InputSection.cpp
@@ -772,6 +772,8 @@ static int64_t getTlsTpOffset(Ctx &ctx, const Symbol &s) {
 
 uint64_t InputSectionBase::getRelocTargetVA(Ctx &ctx, const Relocation &r,
                                             uint64_t p) const {
+  // Resolve undefined prefetch targets to zero, to effectively prefetch the
+  // next instruction.
   if (r.sym->isUndefined() &&
       r.sym->getName().starts_with(prefetchSymbolPrefix)) {
     return 0;
diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index 01a0d9bfbb347..ce82c5f15a408 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -959,8 +959,6 @@ void RelocScan::process(RelExpr expr, RelType type, uint64_t offset,
 
   if (sym.getName().starts_with(prefetchSymbolPrefix)) {
     sec->addReloc({expr, type, offset, addend, &sym});
-    dbgs() << "MY Sym: " << sym.getName() << " " << offset << " " << addend
-           << "\n";
     // Prefetch target symbols may be undefined due to inaccurate profiles.
     // The unresolved relocation will result in 0x0, effectively prefetching the
     // next instruction.



More information about the llvm-commits mailing list