|
7 | 7 | #include "llvm/ADT/SmallVector.h"
|
8 | 8 | #include "llvm/IR/GlobalValue.h"
|
9 | 9 | #include "llvm/ProfileData/MemProfData.inc"
|
| 10 | +#include "llvm/Support/BLAKE3.h" |
10 | 11 | #include "llvm/Support/Endian.h"
|
11 | 12 | #include "llvm/Support/EndianStream.h"
|
| 13 | +#include "llvm/Support/HashBuilder.h" |
12 | 14 | #include "llvm/Support/raw_ostream.h"
|
13 | 15 |
|
14 | 16 | #include <bitset>
|
@@ -308,24 +310,19 @@ struct Frame {
|
308 | 310 | << " Inline: " << IsInlineFrame << "\n";
|
309 | 311 | }
|
310 | 312 |
|
311 |
| - // Return a hash value based on the contents of the frame. Here we don't use |
312 |
| - // hashing from llvm ADT since we are going to persist the hash id, the hash |
313 |
| - // combine algorithm in ADT uses a new randomized seed each time. |
| 313 | + // Return a hash value based on the contents of the frame. Here we use a |
| 314 | + // cryptographic hash function to minimize the chance of hash collisions. We |
| 315 | + // do persist FrameIds as part of memprof formats up to Version 2, inclusive. |
| 316 | + // However, the deserializer never calls this function; it uses FrameIds |
| 317 | + // merely as keys to look up Frames proper. |
314 | 318 | inline FrameId hash() const {
|
315 |
| - auto HashCombine = [](auto Value, size_t Seed) { |
316 |
| - std::hash<decltype(Value)> Hasher; |
317 |
| - // The constant used below is the 64 bit representation of the fractional |
318 |
| - // part of the golden ratio. Used here for the randomness in their bit |
319 |
| - // pattern. |
320 |
| - return Hasher(Value) + 0x9e3779b97f4a7c15 + (Seed << 6) + (Seed >> 2); |
321 |
| - }; |
322 |
| - |
323 |
| - size_t Result = 0; |
324 |
| - Result ^= HashCombine(Function, Result); |
325 |
| - Result ^= HashCombine(LineOffset, Result); |
326 |
| - Result ^= HashCombine(Column, Result); |
327 |
| - Result ^= HashCombine(IsInlineFrame, Result); |
328 |
| - return static_cast<FrameId>(Result); |
| 319 | + llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little> |
| 320 | + HashBuilder; |
| 321 | + HashBuilder.add(Function, LineOffset, Column, IsInlineFrame); |
| 322 | + llvm::BLAKE3Result<8> Hash = HashBuilder.final(); |
| 323 | + FrameId Id; |
| 324 | + std::memcpy(&Id, Hash.data(), sizeof(Hash)); |
| 325 | + return Id; |
329 | 326 | }
|
330 | 327 | };
|
331 | 328 |
|
|
0 commit comments