Skip to content

Commit 82b6033

Browse files
committed
Implement inserting prefetches into the specified positions.
1 parent be3fe4d commit 82b6033

13 files changed

+123
-273
lines changed

llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@ struct SubblockID {
5252
unsigned SubblockIndex;
5353
};
5454

55+
// This represents a prefetch hint to be injected at site `SiteID`, targetting
56+
// `TargetID` in function `TargetFunction`.
5557
struct PrefetchHint {
56-
SubblockID SitePosition;
57-
StringRef TargetFunctionName;
58-
osition TargetPosition;
58+
SubblockID SiteID;
59+
StringRef TargetFunction;
60+
SubblockID TargetID;
5961
};
6062

6163
// This represents the raw input profile for one function.
@@ -80,27 +82,6 @@ struct FunctionPathAndClusterInfo {
8082
DenseMap<unsigned, uint64_t> BBHashes;
8183
};
8284

83-
// Provides DenseMapInfo SubblockID.
84-
template <> struct DenseMapInfo<SubblockID> {
85-
static inline SubblockID getEmptyKey() {
86-
return {DenseMapInfo<UniqueBBID>::getEmptyKey(),
87-
DenseMapInfo<unsigned>::getEmptyKey()};
88-
}
89-
static inline SubblockID getTombstoneKey() {
90-
return SubblockID{DenseMapInfo<UniqueBBID>::getTombstoneKey(),
91-
DenseMapInfo<unsigned>::getTombstoneKey()};
92-
}
93-
static unsigned getHashValue(const SubblockID &Val) {
94-
std::pair<unsigned, unsigned> PairVal = std::make_pair(
95-
DenseMapInfo<UniqueBBID>::getHashValue(Val.BBID), Val.BBOffset);
96-
return DenseMapInfo<std::pair<unsigned, unsigned>>::getHashValue(PairVal);
97-
}
98-
static bool isEqual(const SubblockID &LHS, const SubblockID &RHS) {
99-
return DenseMapInfo<UniqueBBID>::isEqual(LHS.BBID, RHS.BBID) &&
100-
DenseMapInfo<unsigned>::isEqual(LHS.BBOffset, RHS.BBOffset);
101-
}
102-
};
103-
10485
class BasicBlockSectionsProfileReader {
10586
public:
10687
friend class BasicBlockSectionsProfileReaderWrapperPass;
@@ -132,6 +113,7 @@ class BasicBlockSectionsProfileReader {
132113
SmallVector<SubblockID>
133114
getPrefetchTargetsForFunction(StringRef FuncName) const;
134115

116+
// Returns the prefetch hints to be injected in function `FuncName`.
135117
SmallVector<PrefetchHint>
136118
getPrefetchHintsForFunction(StringRef FuncName) const;
137119

@@ -243,11 +225,10 @@ class BasicBlockSectionsProfileReaderWrapperPass : public ImmutablePass {
243225

244226
uint64_t getEdgeCount(StringRef FuncName, const UniqueBBID &SrcBBID,
245227
const UniqueBBID &DestBBID) const;
228+
246229
SmallVector<PrefetchHint>
247230
getPrefetchHintsForFunction(StringRef FuncName) const;
248231

249-
DenseSet<SubblockID> getPrefetchTargetsForFunction(StringRef FuncName) const;
250-
251232
SmallVector<SubblockID>
252233
getPrefetchTargetsForFunction(StringRef FuncName) const;
253234

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===- BasicBlockSectionUtils.h - Utilities for basic block sections --===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CODEGEN_INSERTCODEPREFETCH_H
10+
#define LLVM_CODEGEN_INSERTCODEPREFETCH_H
11+
12+
#include "llvm/ADT/STLExtras.h"
13+
#include "llvm/ADT/SmallString.h"
14+
#include "llvm/Support/UniqueBBID.h"
15+
#include "llvm/Support/CommandLine.h"
16+
17+
namespace llvm {
18+
19+
SmallString<128> getPrefetchTargetSymbolName(StringRef FunctionName, const UniqueBBID &BBID, unsigned SubblockIndex);
20+
21+
} // end namespace llvm
22+
23+
#endif // LLVM_CODEGEN_INSERTCODEPREFETCH_H

llvm/include/llvm/CodeGen/MachineBasicBlock.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,12 +1298,6 @@ class MachineBasicBlock
12981298
/// Return the MCSymbol for this basic block.
12991299
LLVM_ABI MCSymbol *getSymbol() const;
13001300

1301-
MCSymbol *getCallInstSymbol(unsigned CallInstNumber) const;
1302-
1303-
const SmallVector<MCSymbol *, 4> &getCallInstSymbols() const {
1304-
return CallInstSymbols;
1305-
}
1306-
13071301
/// Return the Windows EH Continuation Symbol for this basic block.
13081302
LLVM_ABI MCSymbol *getEHContSymbol() const;
13091303

llvm/include/llvm/CodeGen/TargetInstrInfo.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2370,6 +2370,15 @@ class LLVM_ABI TargetInstrInfo : public MCInstrInfo {
23702370
llvm_unreachable("unknown number of operands necessary");
23712371
}
23722372

2373+
/// Inserts a code prefetch instruction before `InsertBefore` in block `MBB`
2374+
/// targetting `GV`.
2375+
virtual bool insertCodePrefetchInstr(MachineBasicBlock &MBB,
2376+
MachineBasicBlock::iterator InsertBefore,
2377+
const GlobalValue *GV) const {
2378+
return false;
2379+
}
2380+
2381+
23732382
private:
23742383
mutable std::unique_ptr<MIRFormatter> Formatter;
23752384
unsigned CallFrameSetupOpcode, CallFrameDestroyOpcode;

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
#include "llvm/IR/Instruction.h"
8383
#include "llvm/IR/Instructions.h"
8484
#include "llvm/IR/LLVMRemarkStreamer.h"
85+
#include "llvm/CodeGen/InsertCodePrefetch.h"
8586
#include "llvm/IR/Mangler.h"
8687
#include "llvm/IR/Metadata.h"
8788
#include "llvm/IR/Module.h"
@@ -1988,26 +1989,22 @@ void AsmPrinter::emitFunctionBody() {
19881989
FunctionCallGraphInfo FuncCGInfo;
19891990
const auto &CallSitesInfoMap = MF->getCallSitesInfo();
19901991
for (auto &MBB : *MF) {
1991-
int NextPrefetchTargetIndex = MBB.getPrefetchTargets().empty() ? -1 : 0;
19921992
// Print a label for the basic block.
19931993
emitBasicBlockStart(MBB);
19941994
DenseMap<StringRef, unsigned> MnemonicCounts;
19951995

19961996
SmallVector<unsigned> PrefetchTargets =
19971997
MBB.getPrefetchTargetSubblockIndexes();
19981998
auto PrefetchTargetIt = PrefetchTargets.begin();
1999-
unsigned NumCalls = 0;
1999+
unsigned NumCallsInBB = 0;
20002000
// Helper to emit a symbol for the prefetch target and proceed to the next
20012001
// one.
20022002
auto EmitPrefetchTargetSymbolIfNeeded = [&]() {
20032003
if (PrefetchTargetIt == PrefetchTargets.end())
20042004
return;
2005-
if (NumCalls < *PrefetchTargetIt)
2005+
if (NumCallsInBB < *PrefetchTargetIt)
20062006
return;
2007-
MCSymbol *PrefetchTargetSymbol = OutContext.getOrCreateSymbol(
2008-
Twine("__llvm_prefetch_target_") + MF->getName() + Twine("_") +
2009-
utostr(MBB.getBBID()->BaseID) + Twine("_") +
2010-
utostr(*PrefetchTargetIt));
2007+
MCSymbol *PrefetchTargetSymbol = OutContext.getOrCreateSymbol(getPrefetchTargetSymbolName(MF->getName(), *MBB.getBBID(), *PrefetchTargetIt));
20112008
// If the function is weak-linkage it may be replaced by a strong version,
20122009
// in which case the prefetch targets should also be replaced.
20132010
OutStreamer->emitSymbolAttribute(
@@ -2159,7 +2156,7 @@ void AsmPrinter::emitFunctionBody() {
21592156
if (MI.isCall()) {
21602157
if (MF->getTarget().Options.BBAddrMap)
21612158
OutStreamer->emitLabel(createCallsiteEndSymbol(MBB));
2162-
++NumCalls;
2159+
++NumCallsInBB;
21632160
}
21642161

21652162
if (TM.Options.EmitCallGraphSection && MI.isCall())

llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,8 @@ Error BasicBlockSectionsProfileReader::ReadV1Profile() {
335335
auto SiteBBID = parseUniqueBBID(PrefetchSiteStr[0]);
336336
if (!SiteBBID)
337337
return SiteBBID.takeError();
338-
unsigned long long SiteBBOffset;
339-
if (getAsUnsignedInteger(PrefetchSiteStr[1], 10, SiteBBOffset))
338+
unsigned long long SiteSubblockIndex;
339+
if (getAsUnsignedInteger(PrefetchSiteStr[1], 10, SiteSubblockIndex))
340340
return createProfileParseError(Twine("unsigned integer expected: '") +
341341
PrefetchSiteStr[1]);
342342

@@ -346,14 +346,14 @@ Error BasicBlockSectionsProfileReader::ReadV1Profile() {
346346
auto TargetBBID = parseUniqueBBID(PrefetchTargetStr[1]);
347347
if (!TargetBBID)
348348
return TargetBBID.takeError();
349-
unsigned long long TargetBBOffset;
350-
if (getAsUnsignedInteger(PrefetchTargetStr[2], 10, TargetBBOffset))
349+
unsigned long long TargetSubblockIndex;
350+
if (getAsUnsignedInteger(PrefetchTargetStr[2], 10, TargetSubblockIndex))
351351
return createProfileParseError(Twine("unsigned integer expected: '") +
352352
PrefetchTargetStr[2]);
353353
FI->second.PrefetchHints.push_back(
354-
PrefetchHint{{*SiteBBID, static_cast<unsigned>(SiteBBOffset)},
354+
PrefetchHint{SubblockID{*SiteBBID, static_cast<unsigned>(SiteSubblockIndex)},
355355
PrefetchTargetStr[0],
356-
{*TargetBBID, static_cast<unsigned>(TargetBBOffset)}});
356+
SubblockID{*TargetBBID, static_cast<unsigned>(TargetSubblockIndex)}});
357357
continue;
358358
}
359359
case 't': { // Prefetch target specifier.

llvm/lib/CodeGen/InsertCodePrefetch.cpp

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
/// prefetch instruction from any module.
1818
//===----------------------------------------------------------------------===//
1919

20+
#include "llvm/CodeGen/InsertCodePrefetch.h"
21+
2022
#include "llvm/ADT/SmallVector.h"
2123
#include "llvm/ADT/StringExtras.h"
2224
#include "llvm/ADT/StringRef.h"
@@ -26,11 +28,24 @@
2628
#include "llvm/CodeGen/MachineFunction.h"
2729
#include "llvm/CodeGen/MachineFunctionPass.h"
2830
#include "llvm/CodeGen/Passes.h"
31+
#include "llvm/CodeGen/TargetInstrInfo.h"
2932
#include "llvm/InitializePasses.h"
3033

3134
using namespace llvm;
3235
#define DEBUG_TYPE "insert-code-prefetch"
3336

37+
namespace llvm {
38+
SmallString<128> getPrefetchTargetSymbolName(StringRef FunctionName, const UniqueBBID &BBID, unsigned SubblockIndex) {
39+
SmallString<128> R("__llvm_prefetch_target_");
40+
R += FunctionName;
41+
R += "_";
42+
R += utostr(BBID.BaseID);
43+
R += "_";
44+
R += utostr(SubblockIndex);
45+
return R;
46+
}
47+
} // namespace llvm
48+
3449
namespace {
3550
class InsertCodePrefetch : public MachineFunctionPass {
3651
public:
@@ -82,7 +97,40 @@ bool InsertCodePrefetch::runOnMachineFunction(MachineFunction &MF) {
8297
continue;
8398
MBB.setPrefetchTargetSubblockIndexes(R->second);
8499
}
85-
return false;
100+
SmallVector<PrefetchHint> PrefetchHints =
101+
getAnalysis<BasicBlockSectionsProfileReaderWrapperPass>()
102+
.getPrefetchHintsForFunction(MF.getName());
103+
DenseMap<UniqueBBID, SmallVector<PrefetchHint>>
104+
PrefetchHintsBySiteBBID;
105+
for (const auto &H : PrefetchHints)
106+
PrefetchHintsBySiteBBID[H.SiteID.BBID].push_back(H);
107+
for (auto &[SiteBBID, H]: PrefetchHintsBySiteBBID) {
108+
llvm::sort(H, [](const PrefetchHint &H1, const PrefetchHint &H2) {
109+
return H1.SiteID.SubblockIndex < H2.SiteID.SubblockIndex;
110+
});
111+
}
112+
auto PtrTy = PointerType::getUnqual(MF.getFunction().getParent()->getContext());
113+
const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
114+
for (auto &BB : MF) {
115+
auto It = PrefetchHintsBySiteBBID.find(*BB.getBBID());
116+
if (It == PrefetchHintsBySiteBBID.end())
117+
continue;
118+
const auto &PrefetchHints = It->second;
119+
unsigned NumCallsInBB = 0;
120+
auto InstrIt = BB.begin();
121+
for(auto HintIt = PrefetchHints.begin() ; HintIt != PrefetchHints.end();) {
122+
auto NextInstrIt = InstrIt == BB.end() ? BB.end() : std::next(InstrIt);
123+
while (NumCallsInBB >= HintIt->SiteID.SubblockIndex) {
124+
auto *GV = MF.getFunction().getParent()->getOrInsertGlobal(getPrefetchTargetSymbolName(HintIt->TargetFunction, HintIt->TargetID.BBID, HintIt->TargetID.SubblockIndex), PtrTy);
125+
TII->insertCodePrefetchInstr(BB, NextInstrIt, GV);
126+
++HintIt;
127+
}
128+
if (InstrIt == BB.end()) break;
129+
if (InstrIt->isCall()) ++NumCallsInBB;
130+
InstrIt = NextInstrIt;
131+
}
132+
}
133+
return true;
86134
}
87135

88136
void InsertCodePrefetch::getAnalysisUsage(AnalysisUsage &AU) const {

llvm/lib/CodeGen/MachineBasicBlock.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,6 @@ MCSymbol *MachineBasicBlock::getSymbol() const {
9090
return CachedMCSymbol;
9191
}
9292

93-
MCSymbol *MachineBasicBlock::getCallInstSymbol(unsigned CallInstNumber) const {
94-
if (CallInstSymbols.size() <= CallInstNumber) {
95-
const MachineFunction *MF = getParent();
96-
MCContext &Ctx = MF->getContext();
97-
CallInstSymbols.resize(CallInstNumber + 1);
98-
CallInstSymbols[CallInstNumber] = Ctx.createBlockSymbol(
99-
"BB" + Twine(MF->getFunctionNumber()) + "_" + Twine(getNumber()) + "_" +
100-
Twine(CallInstNumber),
101-
/*AlwaysEmit=*/true);
102-
}
103-
return CallInstSymbols[CallInstNumber];
104-
}
105-
10693
MCSymbol *MachineBasicBlock::getEHContSymbol() const {
10794
if (!CachedEHContMCSymbol) {
10895
const MachineFunction *MF = getParent();

llvm/lib/Target/X86/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ set(sources
5858
X86IndirectThunks.cpp
5959
X86InterleavedAccess.cpp
6060
X86InsertPrefetch.cpp
61-
PrefetchInsertion.cpp
6261
X86InstCombineIntrinsic.cpp
6362
X86InstrFMA3Info.cpp
6463
X86InstrFoldTables.cpp

0 commit comments

Comments
 (0)