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

Haohai Wen via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 31 01:08:50 PDT 2026


================
@@ -81,7 +97,92 @@ bool InsertCodePrefetch::runOnMachineFunction(MachineFunction &MF) {
     V.erase(llvm::unique(V), V.end());
   }
   MF.setPrefetchTargets(PrefetchTargetsByBBID);
-  return false;
+  return true;
+}
+
+static bool
+insertPrefetchHints(MachineFunction &MF,
+                    const SmallVector<PrefetchHint> &PrefetchHints) {
+  bool PrefetchInserted = false;
+  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) {
----------------
HaohaiWen wrote:

Why not use  `NumCallsInBB == HintIt->SiteID.CallsiteIndexhere`?
Just for curious.

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


More information about the llvm-commits mailing list