Skip to content

Commit 3e6212c

Browse files
committed
X86: Add prefetch insertion based on Propeller profile
This commit introduces a new pass for prefetch insertion on X86 targets. The pass utilizes Propeller profiles to guide prefetch placement, optimizing memory access patterns. The new file llvm/lib/Target/X86/PrefetchInsertion.cpp implements this functionality. This commit also includes necessary modifications to related CodeGen and X86 target files to integrate the new pass. A build issue where PrefetchInsertion.cpp was not included in the CMakeLists.txt was also resolved.
1 parent d0a7411 commit 3e6212c

File tree

13 files changed

+794
-399
lines changed

13 files changed

+794
-399
lines changed

llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h

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

45+
struct BBPosition {
46+
UniqueBBID BBID;
47+
unsigned BBOffset;
48+
};
49+
50+
struct PrefetchHint {
51+
BBPosition SitePosition;
52+
StringRef TargetFunctionName;
53+
BBPosition TargetPosition;
54+
};
55+
4556
// This represents the raw input profile for one function.
4657
struct FunctionPathAndClusterInfo {
4758
// BB Cluster information specified by `UniqueBBID`s.
@@ -50,19 +61,42 @@ struct FunctionPathAndClusterInfo {
5061
// the edge a -> b (a is not cloned). The index of the path in this vector
5162
// determines the `UniqueBBID::CloneID` of the cloned blocks in that path.
5263
SmallVector<SmallVector<unsigned>> ClonePaths;
64+
SmallVector<PrefetchHint> PrefetchHints;
65+
DenseSet<BBPosition> PrefetchTargets;
5366
// Node counts for each basic block.
5467
DenseMap<UniqueBBID, uint64_t> NodeCounts;
55-
// Edge counts for each edge, stored as a nested map.
68+
// Edge counts for each edge.
5669
DenseMap<UniqueBBID, DenseMap<UniqueBBID, uint64_t>> EdgeCounts;
5770
};
5871

72+
// Provides DenseMapInfo BBPosition.
73+
template <> struct DenseMapInfo<BBPosition> {
74+
static inline BBPosition getEmptyKey() {
75+
return {DenseMapInfo<UniqueBBID>::getEmptyKey(),
76+
DenseMapInfo<unsigned>::getEmptyKey()};
77+
}
78+
static inline BBPosition getTombstoneKey() {
79+
return BBPosition{DenseMapInfo<UniqueBBID>::getTombstoneKey(),
80+
DenseMapInfo<unsigned>::getTombstoneKey()};
81+
}
82+
static unsigned getHashValue(const BBPosition &Val) {
83+
std::pair<unsigned, unsigned> PairVal = std::make_pair(
84+
DenseMapInfo<UniqueBBID>::getHashValue(Val.BBID), Val.BBOffset);
85+
return DenseMapInfo<std::pair<unsigned, unsigned>>::getHashValue(PairVal);
86+
}
87+
static bool isEqual(const BBPosition &LHS, const BBPosition &RHS) {
88+
return DenseMapInfo<UniqueBBID>::isEqual(LHS.BBID, RHS.BBID) &&
89+
DenseMapInfo<unsigned>::isEqual(LHS.BBOffset, RHS.BBOffset);
90+
}
91+
};
92+
5993
class BasicBlockSectionsProfileReader {
6094
public:
6195
friend class BasicBlockSectionsProfileReaderWrapperPass;
6296
BasicBlockSectionsProfileReader(const MemoryBuffer *Buf)
63-
: MBuf(Buf), LineIt(*Buf, /*SkipBlanks=*/true, /*CommentMarker=*/'#'){};
97+
: MBuf(Buf), LineIt(*Buf, /*SkipBlanks=*/true, /*CommentMarker=*/'#') {};
6498

65-
BasicBlockSectionsProfileReader(){};
99+
BasicBlockSectionsProfileReader() {};
66100

67101
// Returns true if basic block sections profile exist for function \p
68102
// FuncName.
@@ -86,6 +120,11 @@ class BasicBlockSectionsProfileReader {
86120
uint64_t getEdgeCount(StringRef FuncName, const UniqueBBID &SrcBBID,
87121
const UniqueBBID &SinkBBID) const;
88122

123+
SmallVector<PrefetchHint>
124+
getPrefetchHintsForFunction(StringRef FuncName) const;
125+
126+
DenseSet<BBPosition> getPrefetchTargetsForFunction(StringRef FuncName) const;
127+
89128
private:
90129
StringRef getAliasName(StringRef FuncName) const {
91130
auto R = FuncAliasMap.find(FuncName);
@@ -194,6 +233,10 @@ class BasicBlockSectionsProfileReaderWrapperPass : public ImmutablePass {
194233

195234
uint64_t getEdgeCount(StringRef FuncName, const UniqueBBID &SrcBBID,
196235
const UniqueBBID &DestBBID) const;
236+
SmallVector<PrefetchHint>
237+
getPrefetchHintsForFunction(StringRef FuncName) const;
238+
239+
DenseSet<BBPosition> getPrefetchTargetsForFunction(StringRef FuncName) const;
197240

198241
// Initializes the FunctionNameToDIFilename map for the current module and
199242
// then reads the profile for the matching functions.

0 commit comments

Comments
 (0)