Skip to content

Commit 47e051c

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 6ad25c5 commit 47e051c

File tree

13 files changed

+791
-396
lines changed

13 files changed

+791
-396
lines changed

llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h

Lines changed: 45 additions & 2 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,21 +61,44 @@ 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
// Hash for each basic block. The Hashes are stored for every original block
5871
// (not cloned blocks), hence the map key being unsigned instead of
5972
// UniqueBBID.
6073
DenseMap<unsigned, uint64_t> BBHashes;
6174
};
6275

76+
// Provides DenseMapInfo BBPosition.
77+
template <> struct DenseMapInfo<BBPosition> {
78+
static inline BBPosition getEmptyKey() {
79+
return {DenseMapInfo<UniqueBBID>::getEmptyKey(),
80+
DenseMapInfo<unsigned>::getEmptyKey()};
81+
}
82+
static inline BBPosition getTombstoneKey() {
83+
return BBPosition{DenseMapInfo<UniqueBBID>::getTombstoneKey(),
84+
DenseMapInfo<unsigned>::getTombstoneKey()};
85+
}
86+
static unsigned getHashValue(const BBPosition &Val) {
87+
std::pair<unsigned, unsigned> PairVal = std::make_pair(
88+
DenseMapInfo<UniqueBBID>::getHashValue(Val.BBID), Val.BBOffset);
89+
return DenseMapInfo<std::pair<unsigned, unsigned>>::getHashValue(PairVal);
90+
}
91+
static bool isEqual(const BBPosition &LHS, const BBPosition &RHS) {
92+
return DenseMapInfo<UniqueBBID>::isEqual(LHS.BBID, RHS.BBID) &&
93+
DenseMapInfo<unsigned>::isEqual(LHS.BBOffset, RHS.BBOffset);
94+
}
95+
};
96+
6397
class BasicBlockSectionsProfileReader {
6498
public:
6599
friend class BasicBlockSectionsProfileReaderWrapperPass;
66100
BasicBlockSectionsProfileReader(const MemoryBuffer *Buf)
67-
: MBuf(Buf), LineIt(*Buf, /*SkipBlanks=*/true, /*CommentMarker=*/'#'){};
101+
: MBuf(Buf), LineIt(*Buf, /*SkipBlanks=*/true, /*CommentMarker=*/'#') {};
68102

69103
BasicBlockSectionsProfileReader() = default;
70104

@@ -90,6 +124,11 @@ class BasicBlockSectionsProfileReader {
90124
uint64_t getEdgeCount(StringRef FuncName, const UniqueBBID &SrcBBID,
91125
const UniqueBBID &SinkBBID) const;
92126

127+
SmallVector<PrefetchHint>
128+
getPrefetchHintsForFunction(StringRef FuncName) const;
129+
130+
DenseSet<BBPosition> getPrefetchTargetsForFunction(StringRef FuncName) const;
131+
93132
private:
94133
StringRef getAliasName(StringRef FuncName) const {
95134
auto R = FuncAliasMap.find(FuncName);
@@ -198,6 +237,10 @@ class BasicBlockSectionsProfileReaderWrapperPass : public ImmutablePass {
198237

199238
uint64_t getEdgeCount(StringRef FuncName, const UniqueBBID &SrcBBID,
200239
const UniqueBBID &DestBBID) const;
240+
SmallVector<PrefetchHint>
241+
getPrefetchHintsForFunction(StringRef FuncName) const;
242+
243+
DenseSet<BBPosition> getPrefetchTargetsForFunction(StringRef FuncName) const;
201244

202245
// Initializes the FunctionNameToDIFilename map for the current module and
203246
// then reads the profile for the matching functions.

0 commit comments

Comments
 (0)