Skip to content

Commit cea8aa4

Browse files
committed
feat(AsmPrinter): Add support for emitting prefetch target symbols
1 parent 8744c4d commit cea8aa4

File tree

5 files changed

+84
-1
lines changed

5 files changed

+84
-1
lines changed

llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h

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

55+
struct PrefetchHint {
56+
SubblockID SitePosition;
57+
StringRef TargetFunctionName;
58+
osition TargetPosition;
59+
};
60+
5561
// This represents the raw input profile for one function.
5662
struct FunctionPathAndClusterInfo {
5763
// BB Cluster information specified by `UniqueBBID`s.
@@ -63,6 +69,7 @@ struct FunctionPathAndClusterInfo {
6369
// Code prefetch targets, specified by the subblock ID of which beginning must
6470
// be targetted for prefetching.
6571
SmallVector<SubblockID> PrefetchTargets;
72+
SmallVector<PrefetchHint> PrefetchHints;
6673
// Node counts for each basic block.
6774
DenseMap<UniqueBBID, uint64_t> NodeCounts;
6875
// Edge counts for each edge.
@@ -73,6 +80,27 @@ struct FunctionPathAndClusterInfo {
7380
DenseMap<unsigned, uint64_t> BBHashes;
7481
};
7582

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+
76104
class BasicBlockSectionsProfileReader {
77105
public:
78106
friend class BasicBlockSectionsProfileReaderWrapperPass;
@@ -104,6 +132,9 @@ class BasicBlockSectionsProfileReader {
104132
SmallVector<SubblockID>
105133
getPrefetchTargetsForFunction(StringRef FuncName) const;
106134

135+
SmallVector<PrefetchHint>
136+
getPrefetchHintsForFunction(StringRef FuncName) const;
137+
107138
private:
108139
StringRef getAliasName(StringRef FuncName) const {
109140
auto R = FuncAliasMap.find(FuncName);
@@ -212,6 +243,10 @@ class BasicBlockSectionsProfileReaderWrapperPass : public ImmutablePass {
212243

213244
uint64_t getEdgeCount(StringRef FuncName, const UniqueBBID &SrcBBID,
214245
const UniqueBBID &DestBBID) const;
246+
SmallVector<PrefetchHint>
247+
getPrefetchHintsForFunction(StringRef FuncName) const;
248+
249+
DenseSet<SubblockID> getPrefetchTargetsForFunction(StringRef FuncName) const;
215250

216251
SmallVector<SubblockID>
217252
getPrefetchTargetsForFunction(StringRef FuncName) const;

llvm/include/llvm/CodeGen/MachineBasicBlock.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ template <> struct DenseMapInfo<MBBSectionID> {
100100
}
101101
};
102102

103+
struct PrefetchTarget {
104+
StringRef TargetFunction;
105+
UniqueBBID TargetBBID;
106+
unsigned TargetBBOffset;
107+
};
108+
103109
template <> struct ilist_traits<MachineInstr> {
104110
private:
105111
friend class MachineBasicBlock; // Set by the owning MachineBasicBlock.
@@ -213,6 +219,8 @@ class MachineBasicBlock
213219
/// basic block sections and basic block labels.
214220
std::optional<UniqueBBID> BBID;
215221

222+
SmallVector<unsigned> PrefetchTargets;
223+
216224
/// With basic block sections, this stores the Section ID of the basic block.
217225
MBBSectionID SectionID{0};
218226

@@ -1290,6 +1298,12 @@ class MachineBasicBlock
12901298
/// Return the MCSymbol for this basic block.
12911299
LLVM_ABI MCSymbol *getSymbol() const;
12921300

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

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "WasmException.h"
1919
#include "WinCFGuard.h"
2020
#include "WinException.h"
21+
#include "llvm/Support/SMLoc.h"
2122
#include "llvm/ADT/APFloat.h"
2223
#include "llvm/ADT/APInt.h"
2324
#include "llvm/ADT/BitmaskEnum.h"
@@ -178,6 +179,11 @@ static cl::opt<bool> EmitJumpTableSizesSection(
178179
cl::desc("Emit a section containing jump table addresses and sizes"),
179180
cl::Hidden, cl::init(false));
180181

182+
static cl::opt<bool> InsertNoopsForPrefetch(
183+
"insert-noops-for-prefetch",
184+
cl::desc("Whether to insert noops instead of prefetches."), cl::init(false),
185+
cl::Hidden);
186+
181187
// This isn't turned on by default, since several of the scheduling models are
182188
// not completely accurate, and we don't want to be misleading.
183189
static cl::opt<bool> PrintLatency(
@@ -1982,6 +1988,7 @@ void AsmPrinter::emitFunctionBody() {
19821988
FunctionCallGraphInfo FuncCGInfo;
19831989
const auto &CallSitesInfoMap = MF->getCallSitesInfo();
19841990
for (auto &MBB : *MF) {
1991+
int NextPrefetchTargetIndex = MBB.getPrefetchTargets().empty() ? -1 : 0;
19851992
// Print a label for the basic block.
19861993
emitBasicBlockStart(MBB);
19871994
DenseMap<StringRef, unsigned> MnemonicCounts;
@@ -2125,7 +2132,7 @@ void AsmPrinter::emitFunctionBody() {
21252132
break;
21262133
}
21272134
default:
2128-
emitInstruction(&MI);
2135+
emitInstruction(&MI);
21292136

21302137
auto CountInstruction = [&](const MachineInstr &MI) {
21312138
// Skip Meta instructions inside bundles.

llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ BasicBlockSectionsProfileReader::getPrefetchTargetsForFunction(
100100
.PrefetchTargets;
101101
}
102102

103+
SmallVector<PrefetchHint>
104+
BasicBlockSectionsProfileReader::getPrefetchHintsForFunction(
105+
StringRef FuncName) const {
106+
return ProgramPathAndClusterInfo.lookup(getAliasName(FuncName)).PrefetchHints;
107+
}
108+
109+
110+
103111
// Reads the version 1 basic block sections profile. Profile for each function
104112
// is encoded as follows:
105113
// m <module_name>
@@ -547,6 +555,12 @@ BasicBlockSectionsProfileReaderWrapperPass::getPrefetchTargetsForFunction(
547555
return BBSPR.getPrefetchTargetsForFunction(FuncName);
548556
}
549557

558+
SmallVector<PrefetchHint>
559+
BasicBlockSectionsProfileReaderWrapperPass::getPrefetchHintsForFunction(
560+
StringRef FuncName) const {
561+
return BBSPR.getPrefetchHintsForFunction(FuncName);
562+
}
563+
550564
BasicBlockSectionsProfileReader &
551565
BasicBlockSectionsProfileReaderWrapperPass::getBBSPR() {
552566
return BBSPR;

llvm/lib/CodeGen/MachineBasicBlock.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,19 @@ 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+
93106
MCSymbol *MachineBasicBlock::getEHContSymbol() const {
94107
if (!CachedEHContMCSymbol) {
95108
const MachineFunction *MF = getParent();

0 commit comments

Comments
 (0)