|
| 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 | + |
| 16 | +#include "llvm/CodeGen/MachineFunctionPass.h" |
| 17 | + |
| 18 | +namespace 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). |
| 23 | +struct BlendedBlockHash { |
| 24 | +public: |
| 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. |
| 68 | + uint64_t distance(const BlendedBlockHash &BBH) const { |
| 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 | + |
| 83 | +private: |
| 84 | + /// The offset of the basic block from the function start. |
| 85 | + uint16_t Offset{0}; |
| 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 | + |
| 96 | +class MachineBlockHashInfo : public MachineFunctionPass { |
| 97 | + DenseMap<const MachineBasicBlock *, uint64_t> MBBHashInfo; |
| 98 | + |
| 99 | +public: |
| 100 | + static char ID; |
| 101 | + MachineBlockHashInfo(); |
| 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 | + |
| 109 | + uint64_t getMBBHash(const MachineBasicBlock &MBB); |
| 110 | +}; |
| 111 | + |
| 112 | +} // end namespace llvm |
| 113 | + |
| 114 | +#endif // LLVM_CODEGEN_MACHINEBLOCKHASHINFO_H |
0 commit comments