Skip to content

Commit a265dbc

Browse files
committed
feat(AsmPrinter): Add support for emitting prefetch target symbols
1 parent 500b536 commit a265dbc

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
@@ -50,6 +50,12 @@ struct CallsiteID {
5050
unsigned CallsiteIndex;
5151
};
5252

53+
struct PrefetchHint {
54+
SubblockID SitePosition;
55+
StringRef TargetFunctionName;
56+
osition TargetPosition;
57+
};
58+
5359
// This represents the raw input profile for one function.
5460
struct FunctionPathAndClusterInfo {
5561
// BB Cluster information specified by `UniqueBBID`s.
@@ -61,6 +67,7 @@ struct FunctionPathAndClusterInfo {
6167
// Code prefetch targets, specified by the callsite ID immediately after
6268
// which beginning must be targetted for prefetching.
6369
SmallVector<CallsiteID> PrefetchTargets;
70+
SmallVector<PrefetchHint> PrefetchHints;
6471
// Node counts for each basic block.
6572
DenseMap<UniqueBBID, uint64_t> NodeCounts;
6673
// Edge counts for each edge.
@@ -71,6 +78,27 @@ struct FunctionPathAndClusterInfo {
7178
DenseMap<unsigned, uint64_t> BBHashes;
7279
};
7380

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+
74102
class BasicBlockSectionsProfileReader {
75103
public:
76104
friend class BasicBlockSectionsProfileReaderWrapperPass;
@@ -102,6 +130,9 @@ class BasicBlockSectionsProfileReader {
102130
SmallVector<CallsiteID>
103131
getPrefetchTargetsForFunction(StringRef FuncName) const;
104132

133+
SmallVector<PrefetchHint>
134+
getPrefetchHintsForFunction(StringRef FuncName) const;
135+
105136
private:
106137
StringRef getAliasName(StringRef FuncName) const {
107138
auto R = FuncAliasMap.find(FuncName);
@@ -210,6 +241,10 @@ class BasicBlockSectionsProfileReaderWrapperPass : public ImmutablePass {
210241

211242
uint64_t getEdgeCount(StringRef FuncName, const UniqueBBID &SrcBBID,
212243
const UniqueBBID &DestBBID) const;
244+
SmallVector<PrefetchHint>
245+
getPrefetchHintsForFunction(StringRef FuncName) const;
246+
247+
DenseSet<SubblockID> getPrefetchTargetsForFunction(StringRef FuncName) const;
213248

214249
SmallVector<CallsiteID>
215250
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

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

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

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>
@@ -578,6 +586,12 @@ BasicBlockSectionsProfileReaderWrapperPass::getPrefetchTargetsForFunction(
578586
return BBSPR.getPrefetchTargetsForFunction(FuncName);
579587
}
580588

589+
SmallVector<PrefetchHint>
590+
BasicBlockSectionsProfileReaderWrapperPass::getPrefetchHintsForFunction(
591+
StringRef FuncName) const {
592+
return BBSPR.getPrefetchHintsForFunction(FuncName);
593+
}
594+
581595
BasicBlockSectionsProfileReader &
582596
BasicBlockSectionsProfileReaderWrapperPass::getBBSPR() {
583597
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)