Skip to content

Commit ed5d461

Browse files
committed
Implement inserting prefetches into the specified positions.
1 parent ea967e2 commit ed5d461

File tree

12 files changed

+119
-265
lines changed

12 files changed

+119
-265
lines changed

llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ struct CallsiteID {
5050
unsigned CallsiteIndex;
5151
};
5252

53+
// This represents a prefetch hint to be injected at site `SiteID`, targetting
54+
// `TargetID` in function `TargetFunction`.
5355
struct PrefetchHint {
54-
SubblockID SitePosition;
55-
StringRef TargetFunctionName;
56-
osition TargetPosition;
56+
SubblockID SiteID;
57+
StringRef TargetFunction;
58+
SubblockID TargetID;
5759
};
5860

5961
// This represents the raw input profile for one function.
@@ -78,27 +80,6 @@ struct FunctionPathAndClusterInfo {
7880
DenseMap<unsigned, uint64_t> BBHashes;
7981
};
8082

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

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

@@ -241,11 +223,10 @@ class BasicBlockSectionsProfileReaderWrapperPass : public ImmutablePass {
241223

242224
uint64_t getEdgeCount(StringRef FuncName, const UniqueBBID &SrcBBID,
243225
const UniqueBBID &DestBBID) const;
226+
244227
SmallVector<PrefetchHint>
245228
getPrefetchHintsForFunction(StringRef FuncName) const;
246229

247-
DenseSet<SubblockID> getPrefetchTargetsForFunction(StringRef FuncName) const;
248-
249230
SmallVector<CallsiteID>
250231
getPrefetchTargetsForFunction(StringRef FuncName) const;
251232

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
@@ -1297,12 +1297,6 @@ class MachineBasicBlock
12971297
/// Return the MCSymbol for this basic block.
12981298
LLVM_ABI MCSymbol *getSymbol() const;
12991299

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

llvm/include/llvm/CodeGen/TargetInstrInfo.h

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

2384+
/// Inserts a code prefetch instruction before `InsertBefore` in block `MBB`
2385+
/// targetting `GV`.
2386+
virtual bool insertCodePrefetchInstr(MachineBasicBlock &MBB,
2387+
MachineBasicBlock::iterator InsertBefore,
2388+
const GlobalValue *GV) const {
2389+
return false;
2390+
}
2391+
2392+
23842393
private:
23852394
mutable std::unique_ptr<MIRFormatter> Formatter;
23862395
unsigned CallFrameSetupOpcode, CallFrameDestroyOpcode;

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 1 addition & 1 deletion
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,7 +1989,6 @@ 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;

llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,8 @@ Error BasicBlockSectionsProfileReader::ReadV1Profile() {
365365
auto SiteBBID = parseUniqueBBID(PrefetchSiteStr[0]);
366366
if (!SiteBBID)
367367
return SiteBBID.takeError();
368-
unsigned long long SiteBBOffset;
369-
if (getAsUnsignedInteger(PrefetchSiteStr[1], 10, SiteBBOffset))
368+
unsigned long long SiteSubblockIndex;
369+
if (getAsUnsignedInteger(PrefetchSiteStr[1], 10, SiteSubblockIndex))
370370
return createProfileParseError(Twine("unsigned integer expected: '") +
371371
PrefetchSiteStr[1]);
372372

@@ -376,14 +376,14 @@ Error BasicBlockSectionsProfileReader::ReadV1Profile() {
376376
auto TargetBBID = parseUniqueBBID(PrefetchTargetStr[1]);
377377
if (!TargetBBID)
378378
return TargetBBID.takeError();
379-
unsigned long long TargetBBOffset;
380-
if (getAsUnsignedInteger(PrefetchTargetStr[2], 10, TargetBBOffset))
379+
unsigned long long TargetSubblockIndex;
380+
if (getAsUnsignedInteger(PrefetchTargetStr[2], 10, TargetSubblockIndex))
381381
return createProfileParseError(Twine("unsigned integer expected: '") +
382382
PrefetchTargetStr[2]);
383383
FI->second.PrefetchHints.push_back(
384-
PrefetchHint{{*SiteBBID, static_cast<unsigned>(SiteBBOffset)},
384+
PrefetchHint{SubblockID{*SiteBBID, static_cast<unsigned>(SiteSubblockIndex)},
385385
PrefetchTargetStr[0],
386-
{*TargetBBID, static_cast<unsigned>(TargetBBOffset)}});
386+
SubblockID{*TargetBBID, static_cast<unsigned>(TargetSubblockIndex)}});
387387
continue;
388388
}
389389
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:
@@ -87,7 +102,40 @@ bool InsertCodePrefetch::runOnMachineFunction(MachineFunction &MF) {
87102
continue;
88103
MBB.setPrefetchTargetCallsiteIndexes(R->second);
89104
}
90-
return false;
105+
SmallVector<PrefetchHint> PrefetchHints =
106+
getAnalysis<BasicBlockSectionsProfileReaderWrapperPass>()
107+
.getPrefetchHintsForFunction(MF.getName());
108+
DenseMap<UniqueBBID, SmallVector<PrefetchHint>>
109+
PrefetchHintsBySiteBBID;
110+
for (const auto &H : PrefetchHints)
111+
PrefetchHintsBySiteBBID[H.SiteID.BBID].push_back(H);
112+
for (auto &[SiteBBID, H]: PrefetchHintsBySiteBBID) {
113+
llvm::sort(H, [](const PrefetchHint &H1, const PrefetchHint &H2) {
114+
return H1.SiteID.SubblockIndex < H2.SiteID.SubblockIndex;
115+
});
116+
}
117+
auto PtrTy = PointerType::getUnqual(MF.getFunction().getParent()->getContext());
118+
const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
119+
for (auto &BB : MF) {
120+
auto It = PrefetchHintsBySiteBBID.find(*BB.getBBID());
121+
if (It == PrefetchHintsBySiteBBID.end())
122+
continue;
123+
const auto &PrefetchHints = It->second;
124+
unsigned NumCallsInBB = 0;
125+
auto InstrIt = BB.begin();
126+
for(auto HintIt = PrefetchHints.begin() ; HintIt != PrefetchHints.end();) {
127+
auto NextInstrIt = InstrIt == BB.end() ? BB.end() : std::next(InstrIt);
128+
while (NumCallsInBB >= HintIt->SiteID.SubblockIndex) {
129+
auto *GV = MF.getFunction().getParent()->getOrInsertGlobal(getPrefetchTargetSymbolName(HintIt->TargetFunction, HintIt->TargetID.BBID, HintIt->TargetID.SubblockIndex), PtrTy);
130+
TII->insertCodePrefetchInstr(BB, NextInstrIt, GV);
131+
++HintIt;
132+
}
133+
if (InstrIt == BB.end()) break;
134+
if (InstrIt->isCall()) ++NumCallsInBB;
135+
InstrIt = NextInstrIt;
136+
}
137+
}
138+
return true;
91139
}
92140

93141
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();

0 commit comments

Comments
 (0)