Skip to content

Commit d6d4aad

Browse files
author
wudexin
committed
Adding Matching and Inference Functionality to Propeller
1 parent f6fc5e8 commit d6d4aad

15 files changed

+716
-6
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#ifndef LLVM_CODEGEN_BASIC_BLOCK_AND_INFERENCE_H
2+
#define LLVM_CODEGEN_BASIC_BLOCK_AND_INFERENCE_H
3+
4+
#include "llvm/CodeGen/BasicBlockSectionsProfileReader.h"
5+
#include "llvm/CodeGen/MachineFunctionPass.h"
6+
#include "llvm/Transforms/Utils/SampleProfileInference.h"
7+
8+
namespace llvm {
9+
10+
class BasicBlockMatchingAndInference : public MachineFunctionPass {
11+
private:
12+
using Edge = std::pair<const MachineBasicBlock *, const MachineBasicBlock *>;
13+
using BlockWeightMap = DenseMap<const MachineBasicBlock *, uint64_t>;
14+
using EdgeWeightMap = DenseMap<Edge, uint64_t>;
15+
using BlockEdgeMap = DenseMap<const MachineBasicBlock *,
16+
SmallVector<const MachineBasicBlock *, 8>>;
17+
18+
struct WeightInfo {
19+
// Weight of basic blocks.
20+
BlockWeightMap BlockWeights;
21+
// Weight of edges.
22+
EdgeWeightMap EdgeWeights;
23+
};
24+
25+
public:
26+
static char ID;
27+
BasicBlockMatchingAndInference();
28+
29+
StringRef getPassName() const override {
30+
return "Basic Block Matching and Inference";
31+
}
32+
33+
void getAnalysisUsage(AnalysisUsage &AU) const override;
34+
35+
bool runOnMachineFunction(MachineFunction &F) override;
36+
37+
std::optional<WeightInfo> getWeightInfo(StringRef FuncName) const;
38+
39+
private:
40+
StringMap<WeightInfo> ProgramWeightInfo;
41+
42+
WeightInfo initWeightInfoByMatching(MachineFunction &MF);
43+
44+
void generateWeightInfoByInference(MachineFunction &MF,
45+
WeightInfo &MatchWeight);
46+
};
47+
48+
} // end namespace llvm
49+
50+
#endif // LLVM_CODEGEN_BASIC_BLOCK_AND_INFERENCE_H

llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ struct FunctionPathAndClusterInfo {
5454
DenseMap<UniqueBBID, uint64_t> NodeCounts;
5555
// Edge counts for each edge, stored as a nested map.
5656
DenseMap<UniqueBBID, DenseMap<UniqueBBID, uint64_t>> EdgeCounts;
57+
// Hash for each basic block.
58+
DenseMap<unsigned, uint64_t> BBHashes;
5759
};
5860

5961
class BasicBlockSectionsProfileReader {
@@ -86,6 +88,10 @@ class BasicBlockSectionsProfileReader {
8688
uint64_t getEdgeCount(StringRef FuncName, const UniqueBBID &SrcBBID,
8789
const UniqueBBID &SinkBBID) const;
8890

91+
// Return the complete function path and cluster info for the given function.
92+
std::pair<bool, FunctionPathAndClusterInfo>
93+
getFunctionPathAndClusterInfo(StringRef FuncName) const;
94+
8995
private:
9096
StringRef getAliasName(StringRef FuncName) const {
9197
auto R = FuncAliasMap.find(FuncName);
@@ -195,6 +201,9 @@ class BasicBlockSectionsProfileReaderWrapperPass : public ImmutablePass {
195201
uint64_t getEdgeCount(StringRef FuncName, const UniqueBBID &SrcBBID,
196202
const UniqueBBID &DestBBID) const;
197203

204+
std::pair<bool, FunctionPathAndClusterInfo>
205+
getFunctionPathAndClusterInfo(StringRef FuncName) const;
206+
198207
// Initializes the FunctionNameToDIFilename map for the current module and
199208
// then reads the profile for the matching functions.
200209
bool doInitialization(Module &M) override;
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#ifndef LLVM_CODEGEN_MACHINEBLOCKHASHINFO_H
2+
#define LLVM_CODEGEN_MACHINEBLOCKHASHINFO_H
3+
4+
#include "llvm/CodeGen/MachineFunctionPass.h"
5+
6+
namespace llvm {
7+
8+
/// An object wrapping several components of a basic block hash. The combined
9+
/// (blended) hash is represented and stored as one uint64_t, while individual
10+
/// components are of smaller size (e.g., uint16_t or uint8_t).
11+
struct BlendedBlockHash {
12+
private:
13+
static uint64_t combineHashes(uint16_t Hash1, uint16_t Hash2, uint16_t Hash3,
14+
uint16_t Hash4) {
15+
uint64_t Hash = 0;
16+
17+
Hash |= uint64_t(Hash4);
18+
Hash <<= 16;
19+
20+
Hash |= uint64_t(Hash3);
21+
Hash <<= 16;
22+
23+
Hash |= uint64_t(Hash2);
24+
Hash <<= 16;
25+
26+
Hash |= uint64_t(Hash1);
27+
28+
return Hash;
29+
}
30+
31+
static void parseHashes(uint64_t Hash, uint16_t &Hash1, uint16_t &Hash2,
32+
uint16_t &Hash3, uint16_t &Hash4) {
33+
Hash1 = Hash & 0xffff;
34+
Hash >>= 16;
35+
36+
Hash2 = Hash & 0xffff;
37+
Hash >>= 16;
38+
39+
Hash3 = Hash & 0xffff;
40+
Hash >>= 16;
41+
42+
Hash4 = Hash & 0xffff;
43+
Hash >>= 16;
44+
}
45+
46+
public:
47+
explicit BlendedBlockHash() {}
48+
49+
explicit BlendedBlockHash(uint64_t CombinedHash) {
50+
parseHashes(CombinedHash, Offset, OpcodeHash, InstrHash, NeighborHash);
51+
}
52+
53+
/// Combine the blended hash into uint64_t.
54+
uint64_t combine() const {
55+
return combineHashes(Offset, OpcodeHash, InstrHash, NeighborHash);
56+
}
57+
58+
/// Compute a distance between two given blended hashes. The smaller the
59+
/// distance, the more similar two blocks are. For identical basic blocks,
60+
/// the distance is zero.
61+
uint64_t distance(const BlendedBlockHash &BBH) const {
62+
assert(OpcodeHash == BBH.OpcodeHash &&
63+
"incorrect blended hash distance computation");
64+
uint64_t Dist = 0;
65+
// Account for NeighborHash
66+
Dist += NeighborHash == BBH.NeighborHash ? 0 : 1;
67+
Dist <<= 16;
68+
// Account for InstrHash
69+
Dist += InstrHash == BBH.InstrHash ? 0 : 1;
70+
Dist <<= 16;
71+
// Account for Offset
72+
Dist += (Offset >= BBH.Offset ? Offset - BBH.Offset : BBH.Offset - Offset);
73+
return Dist;
74+
}
75+
76+
/// The offset of the basic block from the function start.
77+
uint16_t Offset{0};
78+
/// (Loose) Hash of the basic block instructions, excluding operands.
79+
uint16_t OpcodeHash{0};
80+
/// (Strong) Hash of the basic block instructions, including opcodes and
81+
/// operands.
82+
uint16_t InstrHash{0};
83+
/// Hash of the (loose) basic block together with (loose) hashes of its
84+
/// successors and predecessors.
85+
uint16_t NeighborHash{0};
86+
};
87+
88+
class MachineBlockHashInfo : public MachineFunctionPass {
89+
DenseMap<unsigned, uint64_t> MBBHashInfo;
90+
91+
public:
92+
static char ID;
93+
MachineBlockHashInfo();
94+
95+
StringRef getPassName() const override { return "Basic Block Hash Compute"; }
96+
97+
void getAnalysisUsage(AnalysisUsage &AU) const override;
98+
99+
bool runOnMachineFunction(MachineFunction &F) override;
100+
101+
uint64_t getMBBHash(const MachineBasicBlock &MBB);
102+
};
103+
104+
} // end namespace llvm
105+
106+
#endif // LLVM_CODEGEN_MACHINEBLOCKHASHINFO_H

llvm/include/llvm/CodeGen/Passes.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ LLVM_ABI MachineFunctionPass *createBasicBlockSectionsPass();
6969

7070
LLVM_ABI MachineFunctionPass *createBasicBlockPathCloningPass();
7171

72+
/// createBasicBlockMatchingAndInferencePass - This pass enables matching
73+
/// and inference when using propeller.
74+
MachineFunctionPass *createBasicBlockMatchingAndInferencePass();
75+
76+
/// createMachineBlockHashInfoPass - This pass computes basic block hashes.
77+
MachineFunctionPass *createMachineBlockHashInfoPass();
78+
7279
/// createMachineFunctionSplitterPass - This pass splits machine functions
7380
/// using profile information.
7481
LLVM_ABI MachineFunctionPass *createMachineFunctionSplitterPass();

llvm/include/llvm/InitializePasses.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ LLVM_ABI void initializeAlwaysInlinerLegacyPassPass(PassRegistry &);
5555
LLVM_ABI void initializeAssignmentTrackingAnalysisPass(PassRegistry &);
5656
LLVM_ABI void initializeAssumptionCacheTrackerPass(PassRegistry &);
5757
LLVM_ABI void initializeAtomicExpandLegacyPass(PassRegistry &);
58+
LLVM_ABI void initializeBasicBlockMatchingAndInferencePass(PassRegistry &);
5859
LLVM_ABI void initializeBasicBlockPathCloningPass(PassRegistry &);
5960
LLVM_ABI void
6061
initializeBasicBlockSectionsProfileReaderWrapperPassPass(PassRegistry &);
6162
LLVM_ABI void initializeBasicBlockSectionsPass(PassRegistry &);
6263
LLVM_ABI void initializeBarrierNoopPass(PassRegistry &);
6364
LLVM_ABI void initializeBasicAAWrapperPassPass(PassRegistry &);
6465
LLVM_ABI void initializeBlockFrequencyInfoWrapperPassPass(PassRegistry &);
66+
LLVM_ABI void initializeMachineBlockHashInfoPass(PassRegistry&);
6567
LLVM_ABI void initializeBranchFolderLegacyPass(PassRegistry &);
6668
LLVM_ABI void initializeBranchProbabilityInfoWrapperPassPass(PassRegistry &);
6769
LLVM_ABI void initializeBranchRelaxationLegacyPass(PassRegistry &);

llvm/include/llvm/MC/MCContext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class MCContext {
175175
unsigned GetInstance(unsigned LocalLabelVal);
176176

177177
/// SHT_LLVM_BB_ADDR_MAP version to emit.
178-
uint8_t BBAddrMapVersion = 3;
178+
uint8_t BBAddrMapVersion = 4;
179179

180180
/// The file name of the log file from the environment variable
181181
/// AS_SECURE_LOG_FILE. Which must be set before the .secure_log_unique

llvm/include/llvm/Transforms/Utils/SampleProfileInference.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ template <typename FT> class SampleProfileInference {
130130
SampleProfileInference(FunctionT &F, BlockEdgeMap &Successors,
131131
BlockWeightMap &SampleBlockWeights)
132132
: F(F), Successors(Successors), SampleBlockWeights(SampleBlockWeights) {}
133+
SampleProfileInference(FunctionT &F, BlockEdgeMap &Successors,
134+
BlockWeightMap &SampleBlockWeights,
135+
EdgeWeightMap &SampleEdgeWeights)
136+
: F(F), Successors(Successors), SampleBlockWeights(SampleBlockWeights),
137+
SampleEdgeWeights(SampleEdgeWeights) {}
133138

134139
/// Apply the profile inference algorithm for a given function
135140
void apply(BlockWeightMap &BlockWeights, EdgeWeightMap &EdgeWeights);
@@ -157,6 +162,9 @@ template <typename FT> class SampleProfileInference {
157162

158163
/// Map basic blocks to their sampled weights.
159164
BlockWeightMap &SampleBlockWeights;
165+
166+
/// Map edges to their sampled weights.
167+
EdgeWeightMap SampleEdgeWeights;
160168
};
161169

162170
template <typename BT>
@@ -266,6 +274,14 @@ FlowFunction SampleProfileInference<BT>::createFlowFunction(
266274
FlowJump Jump;
267275
Jump.Source = BlockIndex[BB];
268276
Jump.Target = BlockIndex[Succ];
277+
auto It = SampleEdgeWeights.find(std::make_pair(BB, Succ));
278+
if (It != SampleEdgeWeights.end()) {
279+
Jump.HasUnknownWeight = false;
280+
Jump.Weight = It->second;
281+
} else {
282+
Jump.HasUnknownWeight = true;
283+
Jump.Weight = 0;
284+
}
269285
Func.Jumps.push_back(Jump);
270286
}
271287
}

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "llvm/CodeGen/GCMetadataPrinter.h"
4141
#include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
4242
#include "llvm/CodeGen/MachineBasicBlock.h"
43+
#include "llvm/CodeGen/MachineBlockHashInfo.h"
4344
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
4445
#include "llvm/CodeGen/MachineConstantPool.h"
4546
#include "llvm/CodeGen/MachineDominators.h"
@@ -182,6 +183,8 @@ static cl::opt<bool> PrintLatency(
182183
cl::desc("Print instruction latencies as verbose asm comments"), cl::Hidden,
183184
cl::init(false));
184185

186+
extern cl::opt<bool> EmitBBHash;
187+
185188
STATISTIC(EmittedInsts, "Number of machine instrs printed");
186189

187190
char AsmPrinter::ID = 0;
@@ -461,6 +464,8 @@ void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
461464
AU.addRequired<GCModuleInfo>();
462465
AU.addRequired<LazyMachineBlockFrequencyInfoPass>();
463466
AU.addRequired<MachineBranchProbabilityInfoWrapperPass>();
467+
if (EmitBBHash)
468+
AU.addRequired<MachineBlockHashInfo>();
464469
}
465470

466471
bool AsmPrinter::doInitialization(Module &M) {
@@ -1427,7 +1432,8 @@ getBBAddrMapFeature(const MachineFunction &MF, int NumMBBSectionRanges,
14271432
BrProbEnabled,
14281433
MF.hasBBSections() && NumMBBSectionRanges > 1,
14291434
static_cast<bool>(BBAddrMapSkipEmitBBEntries),
1430-
HasCalls};
1435+
HasCalls,
1436+
static_cast<bool>(EmitBBHash)};
14311437
}
14321438

14331439
void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
@@ -1486,6 +1492,8 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
14861492
PrevMBBEndSymbol = MBBSymbol;
14871493
}
14881494

1495+
auto MBHI = Features.BBHash ? &getAnalysis<MachineBlockHashInfo>() : nullptr;
1496+
14891497
if (!Features.OmitBBEntries) {
14901498
OutStreamer->AddComment("BB id");
14911499
// Emit the BB ID for this basic block.
@@ -1513,6 +1521,10 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
15131521
emitLabelDifferenceAsULEB128(MBB.getEndSymbol(), CurrentLabel);
15141522
// Emit the Metadata.
15151523
OutStreamer->emitULEB128IntValue(getBBAddrMapMetadata(MBB));
1524+
// Emit the Hash.
1525+
if (MBHI) {
1526+
OutStreamer->emitULEB128IntValue(MBHI->getMBBHash(MBB));
1527+
}
15161528
}
15171529
PrevMBBEndSymbol = MBB.getEndSymbol();
15181530
}

0 commit comments

Comments
 (0)