[llvm] [Codegen, X86] Add prefetch insertion based on Propeller profile (PR #166324)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 18 00:47:21 PDT 2026


================
@@ -81,7 +96,89 @@ bool InsertCodePrefetch::runOnMachineFunction(MachineFunction &MF) {
     V.erase(llvm::unique(V), V.end());
   }
   MF.setPrefetchTargets(PrefetchTargetsByBBID);
-  return false;
+}
+
+static void
+insertPrefetchHints(MachineFunction &MF,
+                    const SmallVector<PrefetchHint> &PrefetchHints) {
+  bool IsELF = MF.getTarget().getTargetTriple().isOSBinFormatELF();
+  const Module *M = MF.getFunction().getParent();
+  DenseMap<UniqueBBID, SmallVector<PrefetchHint>> PrefetchHintsBySiteBBID;
+  for (const auto &H : PrefetchHints)
+    PrefetchHintsBySiteBBID[H.SiteID.BBID].push_back(H);
+  // Sort prefetch hints by their callsite index so we can insert them by one
+  // pass over the block's instructions.
+  for (auto &[SiteBBID, Hints] : PrefetchHintsBySiteBBID) {
+    llvm::sort(Hints, [](const PrefetchHint &H1, const PrefetchHint &H2) {
+      return H1.SiteID.CallsiteIndex < H2.SiteID.CallsiteIndex;
+    });
+  }
+  auto PtrTy =
+      PointerType::getUnqual(MF.getFunction().getParent()->getContext());
+  const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
+  for (auto &BB : MF) {
+    auto It = PrefetchHintsBySiteBBID.find(*BB.getBBID());
+    if (It == PrefetchHintsBySiteBBID.end())
+      continue;
+    const auto &BBHints = It->second;
+    unsigned NumCallsInBB = 0;
+    auto InstrIt = BB.begin();
+    for (auto HintIt = BBHints.begin(); HintIt != BBHints.end();) {
+      bool TargetFunctionDefined = false;
+      if (Function *TargetFunction = M->getFunction(HintIt->TargetFunction))
+        TargetFunctionDefined = !TargetFunction->isDeclaration();
+      auto NextInstrIt = InstrIt == BB.end() ? BB.end() : std::next(InstrIt);
+      // Insert all the prefetch hints which must be placed after this call (or
+      // at the beginning of the block if `NumCallsInBB` is zero.
+      while (HintIt != BBHints.end() &&
+             NumCallsInBB >= HintIt->SiteID.CallsiteIndex) {
+        auto TargetSymbolName = getPrefetchTargetSymbolName(
+            HintIt->TargetFunction, HintIt->TargetID.BBID,
+            HintIt->TargetID.CallsiteIndex);
+        auto *GV = MF.getFunction().getParent()->getOrInsertGlobal(
+            TargetSymbolName, PtrTy);
+        MachineInstr *PrefetchInstr =
+            TII->insertCodePrefetchInstr(BB, InstrIt, GV);
+        if (!TargetFunctionDefined && IsELF) {
+          // If the target function is not defined in this module, we guard
+          // against undefined prefetch target symbol by emitting a fallback
+          // symbol with weak linkage right after the prefetch instruction. If
+          // there is no strong symbol, the fallback will be used and we
+          // prefetch the next address:
+          //
+          // prefetchit1 __llvm_prefetch_target_foo_x_y(%rip)
+          // .weak __llvm_prefetch_target_foo_x_y
+          // __llvm_prefetch_target_foo_x_y:
+          MCSymbolELF *WeakFallbackSym = static_cast<MCSymbolELF *>(
+              MF.getContext().getOrCreateSymbol(TargetSymbolName));
+          WeakFallbackSym->setIsWeakref();
----------------
MaskRay wrote:

`setIsWeakref` is for `.weakref`. 

> [.weakref alias, target](https://sourceware.org/pipermail/binutils/2005-October/044471.html) enables the creation of weak aliases without directly modifying the target symbol's binding. This allows a header file in library A to optionally depend on symbols from library B. All relocations using alias are redirected to target.

This is a misuse of the ELF symbol API. Instead, set the binding directly via setBinding(ELF::STB_WEAK)

https://github.com/llvm/llvm-project/pull/166324


More information about the llvm-commits mailing list