LLVM 22.0.0git
MachineBlockHashInfo.h
Go to the documentation of this file.
1//===- llvm/CodeGen/MachineBlockHashInfo.h ----------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Compute the hashes of basic blocks.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CODEGEN_MACHINEBLOCKHASHINFO_H
14#define LLVM_CODEGEN_MACHINEBLOCKHASHINFO_H
15
17
18namespace llvm {
19
20/// An object wrapping several components of a basic block hash. The combined
21/// (blended) hash is represented and stored as one uint64_t, while individual
22/// components are of smaller size (e.g., uint16_t or uint8_t).
24public:
25 explicit BlendedBlockHash(uint16_t Offset, uint16_t OpcodeHash,
26 uint16_t InstrHash, uint16_t NeighborHash)
27 : Offset(Offset), OpcodeHash(OpcodeHash), InstrHash(InstrHash),
28 NeighborHash(NeighborHash) {}
29
30 explicit BlendedBlockHash(uint64_t CombinedHash) {
31 Offset = CombinedHash & 0xffff;
32 CombinedHash >>= 16;
33 OpcodeHash = CombinedHash & 0xffff;
34 CombinedHash >>= 16;
35 InstrHash = CombinedHash & 0xffff;
36 CombinedHash >>= 16;
37 NeighborHash = CombinedHash & 0xffff;
38 }
39
40 /// Combine the blended hash into uint64_t.
41 uint64_t combine() const {
42 uint64_t Hash = 0;
43 Hash |= uint64_t(NeighborHash);
44 Hash <<= 16;
45 Hash |= uint64_t(InstrHash);
46 Hash <<= 16;
47 Hash |= uint64_t(OpcodeHash);
48 Hash <<= 16;
49 Hash |= uint64_t(Offset);
50 return Hash;
51 }
52
53 /// Compute a distance between two given blended hashes. The smaller the
54 /// distance, the more similar two blocks are. For identical basic blocks,
55 /// the distance is zero.
56 /// Since OpcodeHash is highly stable, we consider a match good only if
57 /// the OpcodeHashes are identical. Mismatched OpcodeHashes lead to low
58 /// matching accuracy, and poor matches undermine the quality of final
59 /// inference. Notably, during inference, we also consider the matching
60 /// ratio of basic blocks. For MachineFunctions with a low matching
61 /// ratio, we directly skip optimization to reduce the impact of
62 /// mismatches. This ensures even very poor profiles won’t cause negative
63 /// optimization.
64 /// In the context of matching, we consider NeighborHash to be more
65 /// important. This is especially true when accounting for inlining
66 /// scenarios, where the position of a basic block in the control
67 /// flow graph is more critical.
69 assert(OpcodeHash == BBH.OpcodeHash &&
70 "incorrect blended hash distance computation");
71 uint64_t Dist = 0;
72 // Account for NeighborHash
73 Dist += NeighborHash == BBH.NeighborHash ? 0 : 1;
74 Dist <<= 16;
75 // Account for InstrHash
76 Dist += InstrHash == BBH.InstrHash ? 0 : 1;
77 Dist <<= 16;
78 // Account for Offset
79 Dist += (Offset >= BBH.Offset ? Offset - BBH.Offset : BBH.Offset - Offset);
80 return Dist;
81 }
82
83private:
84 /// The offset of the basic block from the function start.
86 /// Hash of the basic block instructions, excluding operands.
87 uint16_t OpcodeHash{0};
88 /// Hash of the basic block instructions, including opcodes and
89 /// operands.
90 uint16_t InstrHash{0};
91 /// OpcodeHash of the basic block together with OpcodeHashes of its
92 /// successors and predecessors.
93 uint16_t NeighborHash{0};
94};
95
98
99public:
100 static char ID;
102
103 StringRef getPassName() const override { return "Basic Block Hash Compute"; }
104
105 void getAnalysisUsage(AnalysisUsage &AU) const override;
106
107 bool runOnMachineFunction(MachineFunction &F) override;
108
110};
111
112} // end namespace llvm
113
114#endif // LLVM_CODEGEN_MACHINEBLOCKHASHINFO_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
#define F(x, y, z)
Definition MD5.cpp:55
Represent the analysis usage information of a pass.
bool runOnMachineFunction(MachineFunction &F) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
uint64_t getMBBHash(const MachineBasicBlock &MBB)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
uint64_t distance(const BlendedBlockHash &BBH) const
Compute a distance between two given blended hashes.
BlendedBlockHash(uint16_t Offset, uint16_t OpcodeHash, uint16_t InstrHash, uint16_t NeighborHash)
uint64_t combine() const
Combine the blended hash into uint64_t.
BlendedBlockHash(uint64_t CombinedHash)