Skip to content

Commit 6d8bdb1

Browse files
committed
Refine and polish.
1 parent cc4e333 commit 6d8bdb1

File tree

6 files changed

+84
-57
lines changed

6 files changed

+84
-57
lines changed

llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,11 @@ struct BBClusterInfo {
4242
unsigned PositionInCluster;
4343
};
4444

45-
// Assuming a block is split into subblocks across its callsites, this struct
46-
// uniquely identifies the subblock in block `BBID` which starts from right
47-
// after call number `SubblockIndex` (or the beginning of the block if
48-
// `SubblockIndex` is zero) to the call number `SubblockIndex+1` (or the end of
49-
// the block if `SubblockIndex` is the last call in the block).
50-
struct SubblockID {
45+
// The prefetch symbol is emitted immediately after the call of the given index
46+
// in block `BBID` (or at the beginning of the block if CallsiteIndex is -1).
47+
struct CallsiteID {
5148
UniqueBBID BBID;
52-
unsigned SubblockIndex;
49+
int CallsiteIndex;
5350
};
5451

5552
// This represents the raw input profile for one function.
@@ -60,9 +57,9 @@ struct FunctionPathAndClusterInfo {
6057
// the edge a -> b (a is not cloned). The index of the path in this vector
6158
// determines the `UniqueBBID::CloneID` of the cloned blocks in that path.
6259
SmallVector<SmallVector<unsigned>> ClonePaths;
63-
// Code prefetch targets, specified by the subblock ID of which beginning must
64-
// be targetted for prefetching.
65-
SmallVector<SubblockID> PrefetchTargets;
60+
// Code prefetch targets, specified by the callsite ID immediately after
61+
// which beginning must be targetted for prefetching.
62+
SmallVector<CallsiteID> PrefetchTargets;
6663
// Node counts for each basic block.
6764
DenseMap<UniqueBBID, uint64_t> NodeCounts;
6865
// Edge counts for each edge.
@@ -99,9 +96,9 @@ class BasicBlockSectionsProfileReader {
9996
uint64_t getEdgeCount(StringRef FuncName, const UniqueBBID &SrcBBID,
10097
const UniqueBBID &SinkBBID) const;
10198

102-
// Returns the prefetch targets (identified by their containing subblocks) for
103-
// function `FuncName`.
104-
SmallVector<SubblockID>
99+
// Returns the prefetch targets (identified by their containing callsite IDs)
100+
// for function `FuncName`.
101+
SmallVector<CallsiteID>
105102
getPrefetchTargetsForFunction(StringRef FuncName) const;
106103

107104
private:
@@ -213,7 +210,7 @@ class BasicBlockSectionsProfileReaderWrapperPass : public ImmutablePass {
213210
uint64_t getEdgeCount(StringRef FuncName, const UniqueBBID &SrcBBID,
214211
const UniqueBBID &DestBBID) const;
215212

216-
SmallVector<SubblockID>
213+
SmallVector<CallsiteID>
217214
getPrefetchTargetsForFunction(StringRef FuncName) const;
218215

219216
// Initializes the FunctionNameToDIFilename map for the current module and

llvm/include/llvm/CodeGen/MachineBasicBlock.h

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

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

239237
/// Cached MCSymbol for this block (used if IsEHContTarget).
240238
mutable MCSymbol *CachedEHContMCSymbol = nullptr;
@@ -717,12 +715,12 @@ class MachineBasicBlock
717715

718716
std::optional<UniqueBBID> getBBID() const { return BBID; }
719717

720-
const SmallVector<unsigned> &getPrefetchTargetSubblockIndexes() const {
721-
return PrefetchTargetSubblockIndexes;
718+
const SmallVector<int> &getPrefetchTargetCallsiteIndexes() const {
719+
return PrefetchTargetCallsiteIndexes;
722720
}
723721

724-
void setPrefetchTargetSubblockIndexes(const SmallVector<unsigned> &V) {
725-
PrefetchTargetSubblockIndexes = V;
722+
void setPrefetchTargetCallsiteIndexes(const SmallVector<int> &V) {
723+
PrefetchTargetCallsiteIndexes = V;
726724
}
727725

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

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

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

1989-
SmallVector<unsigned> PrefetchTargets =
1990-
MBB.getPrefetchTargetSubblockIndexes();
1989+
SmallVector<int> PrefetchTargets =
1990+
MBB.getPrefetchTargetCallsiteIndexes();
19911991
auto PrefetchTargetIt = PrefetchTargets.begin();
1992-
unsigned NumCalls = 0;
1992+
int CurrentCallsiteIndex = -1;
19931993
// Helper to emit a symbol for the prefetch target and proceed to the next
19941994
// one.
19951995
auto EmitPrefetchTargetSymbolIfNeeded = [&]() {
1996-
if (PrefetchTargetIt == PrefetchTargets.end())
1997-
return;
1998-
if (NumCalls < *PrefetchTargetIt)
1999-
return;
2000-
MCSymbol *PrefetchTargetSymbol = OutContext.getOrCreateSymbol(
2001-
Twine("__llvm_prefetch_target_") + MF->getName() + Twine("_") +
2002-
utostr(MBB.getBBID()->BaseID) + Twine("_") +
2003-
utostr(*PrefetchTargetIt));
2004-
// If the function is weak-linkage it may be replaced by a strong version,
2005-
// in which case the prefetch targets should also be replaced.
2006-
OutStreamer->emitSymbolAttribute(
2007-
PrefetchTargetSymbol,
2008-
MF->getFunction().isWeakForLinker() ? MCSA_Weak : MCSA_Global);
2009-
OutStreamer->emitLabel(PrefetchTargetSymbol);
2010-
++PrefetchTargetIt;
1996+
if (PrefetchTargetIt != PrefetchTargets.end() &&
1997+
*PrefetchTargetIt == CurrentCallsiteIndex) {
1998+
MCSymbol *PrefetchTargetSymbol = OutContext.getOrCreateSymbol(
1999+
Twine("__llvm_prefetch_target_") + MF->getName() + Twine("_") +
2000+
utostr(MBB.getBBID()->BaseID) + Twine("_") + utostr(static_cast<unsigned>(*PrefetchTargetIt + 1)));
2001+
// If the function is weak-linkage it may be replaced by a strong
2002+
// version, in which case the prefetch targets should also be replaced.
2003+
OutStreamer->emitSymbolAttribute(
2004+
PrefetchTargetSymbol,
2005+
MF->getFunction().isWeakForLinker() ? MCSA_Weak : MCSA_Global);
2006+
OutStreamer->emitLabel(PrefetchTargetSymbol);
2007+
++PrefetchTargetIt;
2008+
}
20112009
};
20122010

20132011
for (auto &MI : MBB) {
20142012
EmitPrefetchTargetSymbolIfNeeded();
2013+
20152014
// Print the assembly for the instruction.
20162015
if (!MI.isPosition() && !MI.isImplicitDef() && !MI.isKill() &&
20172016
!MI.isDebugInstr()) {
@@ -2152,7 +2151,7 @@ void AsmPrinter::emitFunctionBody() {
21522151
if (MI.isCall()) {
21532152
if (MF->getTarget().Options.BBAddrMap)
21542153
OutStreamer->emitLabel(createCallsiteEndSymbol(MBB));
2155-
++NumCalls;
2154+
CurrentCallsiteIndex++;
21562155
}
21572156

21582157
if (TM.Options.EmitCallGraphSection && MI.isCall())
@@ -2165,8 +2164,7 @@ void AsmPrinter::emitFunctionBody() {
21652164
for (auto &Handler : Handlers)
21662165
Handler->endInstruction();
21672166
}
2168-
// If the block ends with a call, we may need to emit a prefetch target
2169-
// at the end.
2167+
// Emit the last prefetch target in case the last instruction was a call.
21702168
EmitPrefetchTargetSymbolIfNeeded();
21712169

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

llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ uint64_t BasicBlockSectionsProfileReader::getEdgeCount(
9393
return EdgeIt->second;
9494
}
9595

96-
SmallVector<SubblockID>
96+
SmallVector<CallsiteID>
9797
BasicBlockSectionsProfileReader::getPrefetchTargetsForFunction(
9898
StringRef FuncName) const {
9999
return ProgramPathAndClusterInfo.lookup(getAliasName(FuncName))
@@ -155,6 +155,35 @@ BasicBlockSectionsProfileReader::getPrefetchTargetsForFunction(
155155
// +-->: 5 :
156156
// ....
157157
// ****************************************************************************
158+
// This profile can also specify prefetch targets (starting with 't') which
159+
// instruct the compiler to emit a prefetch symbol for the given target.
160+
// A prefetch target is specified by a pair "<bbid>,<subblock_index>" where
161+
// bbid specifies the target basic block and subblock_index is a zero-based
162+
// index. Subblock 0 refers to the region at the beginning of the block up to
163+
// the first callsite. Subblock `i > 0` refers to the region immediately after
164+
// the `i`-th callsite up to the `i+1`-th callsite (or the end of the block).
165+
// The prefetch target is always emitted at the beginning of the subblock.
166+
// This is the beginning of the basic block for `i = 0` and immediately after
167+
// the `i`-th call for every `i > 0`.
168+
//
169+
// Example: A basic block in function "foo" with BBID 10 and two call instructions (call_A, call_B).
170+
// This block is conceptually split into subblocks, with the prefetch target
171+
// symbol emitted at the beginning of each subblock.
172+
//
173+
// +----------------------------------+
174+
// | __llvm_prefetch_target_foo_10_0: | <- Subblock 0 (before call_A)
175+
// | Instruction 1 |
176+
// | Instruction 2 |
177+
// | call_A (Callsite 0) |
178+
// | __llvm_prefetch_target_foo_10_1: | <--- Subblock 1 (after call_A,
179+
// | | before call_B)
180+
// | Instruction 3 |
181+
// | call_B (Callsite 1) |
182+
// | __llvm_prefetch_target_foo_10_2: | <--- Subblock 2 (after call_B,
183+
// | | before call_C)
184+
// | Instruction 4 |
185+
// +----------------------------------+
186+
//
158187
Error BasicBlockSectionsProfileReader::ReadV1Profile() {
159188
auto FI = ProgramPathAndClusterInfo.end();
160189

@@ -315,7 +344,7 @@ Error BasicBlockSectionsProfileReader::ReadV1Profile() {
315344
}
316345
continue;
317346
}
318-
case 't': { // Prefetch target specifier.
347+
case 't': { // Callsite target specifier.
319348
// Skip the profile when we the profile iterator (FI) refers to the
320349
// past-the-end element.
321350
if (FI == ProgramPathAndClusterInfo.end())
@@ -324,16 +353,16 @@ Error BasicBlockSectionsProfileReader::ReadV1Profile() {
324353
Values[0].split(PrefetchTargetStr, ',');
325354
if (PrefetchTargetStr.size() != 2)
326355
return createProfileParseError(
327-
Twine("Prefetch target target expected: ") + Values[0]);
356+
Twine("Callsite target expected: ") + Values[0]);
328357
auto TargetBBID = parseUniqueBBID(PrefetchTargetStr[0]);
329358
if (!TargetBBID)
330359
return TargetBBID.takeError();
331-
unsigned long long TargetCallsiteIndex;
332-
if (getAsUnsignedInteger(PrefetchTargetStr[1], 10, TargetCallsiteIndex))
333-
return createProfileParseError(Twine("unsigned integer expected: '") +
360+
long long CallsiteIndex;
361+
if (getAsSignedInteger(PrefetchTargetStr[1], 10, CallsiteIndex))
362+
return createProfileParseError(Twine("signed integer expected: '") +
334363
PrefetchTargetStr[1]);
335364
FI->second.PrefetchTargets.push_back(
336-
SubblockID{*TargetBBID, static_cast<unsigned>(TargetCallsiteIndex)});
365+
CallsiteID{*TargetBBID, static_cast<int>(CallsiteIndex - 1)});
337366
continue;
338367
}
339368
default:
@@ -542,7 +571,7 @@ uint64_t BasicBlockSectionsProfileReaderWrapperPass::getEdgeCount(
542571
return BBSPR.getEdgeCount(FuncName, SrcBBID, SinkBBID);
543572
}
544573

545-
SmallVector<SubblockID>
574+
SmallVector<CallsiteID>
546575
BasicBlockSectionsProfileReaderWrapperPass::getPrefetchTargetsForFunction(
547576
StringRef FuncName) const {
548577
return BBSPR.getPrefetchTargetsForFunction(FuncName);

llvm/lib/CodeGen/InsertCodePrefetch.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,17 @@ bool InsertCodePrefetch::runOnMachineFunction(MachineFunction &MF) {
7070
return false;
7171
// Set each block's prefetch targets so AsmPrinter can emit a special symbol
7272
// there.
73-
SmallVector<SubblockID> PrefetchTargets =
73+
SmallVector<CallsiteID> PrefetchTargets =
7474
getAnalysis<BasicBlockSectionsProfileReaderWrapperPass>()
7575
.getPrefetchTargetsForFunction(MF.getName());
76-
DenseMap<UniqueBBID, SmallVector<unsigned>> PrefetchTargetsByBBID;
76+
DenseMap<UniqueBBID, SmallVector<int>> PrefetchTargetsByBBID;
7777
for (const auto &Target : PrefetchTargets)
78-
PrefetchTargetsByBBID[Target.BBID].push_back(Target.SubblockIndex);
78+
PrefetchTargetsByBBID[Target.BBID].push_back(Target.CallsiteIndex);
7979
for (auto &MBB : MF) {
8080
auto R = PrefetchTargetsByBBID.find(*MBB.getBBID());
8181
if (R == PrefetchTargetsByBBID.end())
8282
continue;
83-
MBB.setPrefetchTargetSubblockIndexes(R->second);
83+
MBB.setPrefetchTargetCallsiteIndexes(R->second);
8484
}
8585
return false;
8686
}

llvm/test/CodeGen/X86/basic-block-sections-code-prefetch.ll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
; RUN: echo 't 1,0' >> %t
88
; RUN: echo 't 1,1' >> %t
99
; RUN: echo 't 2,1' >> %t
10+
; RUN: echo 't 4,0' >> %t
1011
; RUN: echo 'f _Z3barv' >> %t
1112
; RUN: echo 't 0,0' >> %t
1213
; RUN: echo 't 21,1' >> %t
@@ -48,6 +49,10 @@ define i32 @_Z3foob(i1 zeroext %0) nounwind {
4849
13: ; preds = %11, %9
4950
%14 = load i32, ptr %2, align 4
5051
ret i32 %14
52+
; CHECK: .LBB0_3:
53+
; CHECK-NEXT: .globl __llvm_prefetch_target__Z3foob_4_0
54+
; CHECK-NEXT: __llvm_prefetch_target__Z3foob_4_0:
55+
5156
}
5257

5358
define weak i32 @_Z3barv() nounwind {

0 commit comments

Comments
 (0)