[llvm] Insert symbols for prefetch targets read from basic blocks section profile. (PR #168439)
Sriraman Tallam via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 19 15:46:42 PST 2025
================
@@ -1985,7 +1985,33 @@ void AsmPrinter::emitFunctionBody() {
// Print a label for the basic block.
emitBasicBlockStart(MBB);
DenseMap<StringRef, unsigned> MnemonicCounts;
+
+ SmallVector<unsigned> PrefetchTargets =
+ MBB.getPrefetchTargetCallsiteIndexes();
+ auto PrefetchTargetIt = PrefetchTargets.begin();
+ unsigned LastCallsiteIndex = 0;
+ // Helper to emit a symbol for the prefetch target and proceed to the next
+ // one.
+ auto EmitPrefetchTargetSymbolIfNeeded = [&]() {
+ if (PrefetchTargetIt != PrefetchTargets.end() &&
+ *PrefetchTargetIt == LastCallsiteIndex) {
+ MCSymbol *PrefetchTargetSymbol = OutContext.getOrCreateSymbol(
+ Twine("__llvm_prefetch_target_") + MF->getName() + Twine("_") +
+ utostr(MBB.getBBID()->BaseID) + Twine("_") +
+ utostr(static_cast<unsigned>(*PrefetchTargetIt)));
+ // If the function is weak-linkage it may be replaced by a strong
+ // version, in which case the prefetch targets should also be replaced.
+ OutStreamer->emitSymbolAttribute(
+ PrefetchTargetSymbol,
+ MF->getFunction().isWeakForLinker() ? MCSA_Weak : MCSA_Global);
+ OutStreamer->emitLabel(PrefetchTargetSymbol);
+ ++PrefetchTargetIt;
+ }
+ };
+
for (auto &MI : MBB) {
+ EmitPrefetchTargetSymbolIfNeeded();
----------------
tmsri wrote:
I would have EmitPrefetchTargetIt take a parameter which is the unsigned callsiteindex. I would remove the updates to the PrefetchTargets from the lambda and do it within the loop and replace this line with:
```C
if (PrefetchTargetIt != PrefetchTargets.end() &&
*PrefetchTargetIt == LastCallSiteIndex) {
EmitPrefetchTargetSymbolIfNeeded(*PrefetchTargetIt);
++PrefetchTargetIt;
}
```
https://github.com/llvm/llvm-project/pull/168439
More information about the llvm-commits
mailing list