Skip to content

Commit e78fae5

Browse files
committed
Add comments and rename functions.
1 parent c6b1fa0 commit e78fae5

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct BBClusterInfo {
4646
// uniquely identifies the subblock in block `BBID` which starts from right
4747
// after call number `SubblockIndex` (or the beginning of the block if
4848
// `SubblockIndex` is zero) to the call number `SubblockIndex+1` (or the end of
49-
// the block if there are are `SubblockIndex` calls in the basic block).
49+
// the block if `SubblockIndex` is the last call in the block).
5050
struct SubblockID {
5151
UniqueBBID BBID;
5252
unsigned SubblockIndex;

llvm/include/llvm/CodeGen/MachineBasicBlock.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,12 @@ class MachineBasicBlock
229229
/// is only computed once and is cached.
230230
mutable MCSymbol *CachedMCSymbol = nullptr;
231231

232-
SmallVector<unsigned> PrefetchTargetIndexes;
232+
/// Contains the subblock indices in this block that are targets of code prefetching.
233+
/// The subblock indexed `i` specifies that region after the `i`th call (or the
234+
/// beginning of the block if `i==0`) to before the`i+1`th callsite (or the
235+
/// end of the block). The prefetch target is always the beginning of the
236+
/// subblock.
237+
SmallVector<unsigned> PrefetchTargetSubblockIndexes;
233238

234239
/// Cached MCSymbol for this block (used if IsEHContTarget).
235240
mutable MCSymbol *CachedEHContMCSymbol = nullptr;
@@ -712,12 +717,12 @@ class MachineBasicBlock
712717

713718
std::optional<UniqueBBID> getBBID() const { return BBID; }
714719

715-
const SmallVector<unsigned> &getPrefetchTargetIndexes() const {
716-
return PrefetchTargetIndexes;
720+
const SmallVector<unsigned> &getPrefetchTargetSubblockIndexes() const {
721+
return PrefetchTargetSubblockIndexes;
717722
}
718723

719-
void setPrefetchTargetIndexes(const SmallVector<unsigned> &V) {
720-
PrefetchTargetIndexes = V;
724+
void setPrefetchTargetSubblockIndexes(const SmallVector<unsigned> &V) {
725+
PrefetchTargetSubblockIndexes = V;
721726
}
722727

723728
/// Returns the section ID of this basic block.

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,17 +1986,20 @@ void AsmPrinter::emitFunctionBody() {
19861986
emitBasicBlockStart(MBB);
19871987
DenseMap<StringRef, unsigned> MnemonicCounts;
19881988

1989-
SmallVector<unsigned> PrefetchTargets = MBB.getPrefetchTargetIndexes();
1989+
SmallVector<unsigned> PrefetchTargets = MBB.getPrefetchTargetSubblockIndexes();
19901990
auto PrefetchTargetIt = PrefetchTargets.begin();
19911991
unsigned NumCalls = 0;
1992+
// Helper to emit a symbol for the prefetch target and proceed to the next
1993+
// one.
19921994
auto EmitPrefetchTargetSymbolIfNeeded = [&]() {
1993-
if (PrefetchTargetIt == PrefetchTargets.end() ||
1994-
NumCalls < *PrefetchTargetIt)
1995-
return;
1995+
if (PrefetchTargetIt == PrefetchTargets.end()) return;
1996+
if (NumCalls < *PrefetchTargetIt) return;
19961997
MCSymbol *PrefetchTargetSymbol = OutContext.getOrCreateSymbol(
19971998
Twine("__llvm_prefetch_target_") + MF->getName() + Twine("_") +
19981999
utostr(MBB.getBBID()->BaseID) + Twine("_") +
19992000
utostr(*PrefetchTargetIt));
2001+
// If the function is weak-linkage it may be replaced by a strong version,
2002+
// in which case the prefetch targets should also be replaced.
20002003
OutStreamer->emitSymbolAttribute(
20012004
PrefetchTargetSymbol,
20022005
MF->getFunction().isWeakForLinker() ? MCSA_Weak : MCSA_Global);
@@ -2159,6 +2162,8 @@ void AsmPrinter::emitFunctionBody() {
21592162
for (auto &Handler : Handlers)
21602163
Handler->endInstruction();
21612164
}
2165+
// If the block ends with a call, we may need to emit a prefetch target
2166+
// at the end.
21622167
EmitPrefetchTargetSymbolIfNeeded();
21632168

21642169
// We must emit temporary symbol for the end of this basic block, if either

llvm/lib/CodeGen/InsertCodePrefetch.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
/// This pass inserts code prefetch instructions according to the prefetch
1313
/// directives in the basic block section profile. The target of a prefetch can
1414
/// be the beginning of any dynamic basic block, that is the beginning of a
15-
/// machine basic block, or immediately after a callsite. A global symbol will
16-
/// be emitted at the position of the target so it can be addressed from the
17-
/// prefetch instruction.
15+
/// machine basic block, or immediately after a callsite. A global symbol is
16+
/// emitted at the position of the target so it can be addressed from the
17+
/// prefetch instruction from any module.
1818
//===----------------------------------------------------------------------===//
1919

2020
#include "llvm/ADT/SmallVector.h"
@@ -68,6 +68,8 @@ bool InsertCodePrefetch::runOnMachineFunction(MachineFunction &MF) {
6868
"BB Sections list not enabled!");
6969
if (hasInstrProfHashMismatch(MF))
7070
return false;
71+
// Set each block's prefetch targets so AsmPrinter can emit a special symbol
72+
// there.
7173
SmallVector<BBPosition> PrefetchTargets =
7274
getAnalysis<BasicBlockSectionsProfileReaderWrapperPass>()
7375
.getPrefetchTargetsForFunction(MF.getName());
@@ -78,9 +80,8 @@ bool InsertCodePrefetch::runOnMachineFunction(MachineFunction &MF) {
7880
auto R = PrefetchTargetsByBBID.find(*MBB.getBBID());
7981
if (R == PrefetchTargetsByBBID.end())
8082
continue;
81-
MBB.setPrefetchTargetIndexes(R->second);
83+
MBB.setPrefetchTargetSubblockIndexes(R->second);
8284
}
83-
8485
return false;
8586
}
8687

0 commit comments

Comments
 (0)