[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 19:58:30 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;
----------------
HaohaiWen wrote:
```
// Returns the prefetch hints to be injected in function `FuncName`.
SmallVector<PrefetchHint>
getPrefetchHintsForFunction(StringRef FuncName) const;
```
>From the comments, it is easily to believe that the place to insert those BBHints is in this MF, but the target functions may be different.
If the target functions are absolutely same, I think it's better to add assert or comments.
https://github.com/llvm/llvm-project/pull/166324
More information about the llvm-commits
mailing list