LLVM 23.0.0git
TypeHashing.cpp
Go to the documentation of this file.
1//===- TypeHashing.cpp -------------------------------------------*- 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
10
12#include "llvm/Support/BLAKE3.h"
13
14using namespace llvm;
15using namespace llvm::codeview;
16
18
19static std::array<uint8_t, 8> EmptyHash = {
20 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
21
23
27
30 ArrayRef<GloballyHashedType> PreviousTypes,
31 ArrayRef<GloballyHashedType> PreviousIds) {
33 discoverTypeIndices(RecordData, Refs);
35 S.init();
36 uint32_t Off = 0;
37 S.update(RecordData.take_front(sizeof(RecordPrefix)));
38 RecordData = RecordData.drop_front(sizeof(RecordPrefix));
39 for (const auto &Ref : Refs) {
40 // Hash any data that comes before this TiRef.
41 uint32_t PreLen = Ref.Offset - Off;
42 ArrayRef<uint8_t> PreData = RecordData.slice(Off, PreLen);
43 S.update(PreData);
44 auto Prev = (Ref.Kind == TiRefKind::IndexRef) ? PreviousIds : PreviousTypes;
45
46 auto RefData = RecordData.slice(Ref.Offset, Ref.Count * sizeof(TypeIndex));
47 // For each type index referenced, add in the previously computed hash
48 // value of that type.
49 ArrayRef<TypeIndex> Indices(
50 reinterpret_cast<const TypeIndex *>(RefData.data()), Ref.Count);
51 for (TypeIndex TI : Indices) {
52 ArrayRef<uint8_t> BytesToHash;
53 if (TI.isSimple() || TI.isNoneType()) {
54 const uint8_t *IndexBytes = reinterpret_cast<const uint8_t *>(&TI);
55 BytesToHash = ArrayRef(IndexBytes, sizeof(TypeIndex));
56 } else {
57 if (TI.toArrayIndex() >= Prev.size() ||
58 Prev[TI.toArrayIndex()].empty()) {
59 // There are references to yet-unhashed records. Suspend hashing for
60 // this record until all the other records are processed.
61 return {};
62 }
63 BytesToHash = Prev[TI.toArrayIndex()].Hash;
64 }
65 S.update(BytesToHash);
66 }
67
68 Off = Ref.Offset + Ref.Count * sizeof(TypeIndex);
69 }
70
71 // Don't forget to add in any trailing bytes.
72 auto TrailingBytes = RecordData.drop_front(Off);
73 S.update(TrailingBytes);
74
75 return {S.final()};
76}
static std::array< uint8_t, 8 > EmptyHash
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
ArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
Definition ArrayRef.h:218
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition ArrayRef.h:194
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition ArrayRef.h:185
void init()
Reinitialize the internal state.
Definition BLAKE3.h:43
void update(ArrayRef< uint8_t > Data)
Digest more data.
Definition BLAKE3.h:52
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Like BLAKE3 but using a class-level template parameter for specifying the hash size of the final() an...
Definition BLAKE3.h:107
void final(BLAKE3Result< NumBytes > &Result)
Finalize the hasher and put the result in Result.
Definition BLAKE3.h:112
A 32-bit type reference.
Definition TypeIndex.h:97
LLVM_ABI void discoverTypeIndices(ArrayRef< uint8_t > RecordData, SmallVectorImpl< TiReference > &Refs)
This is an optimization pass for GlobalISel generic memory operations.
hash_code hash_value(const FixedPointSemantics &Val)
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
ArrayRef(const T &OneElt) -> ArrayRef< T >
An information struct used to provide DenseMap with the various necessary components for a given valu...
A globally hashed type represents a hash value that is sufficient to uniquely identify a record acros...
Definition TypeHashing.h:81
static LLVM_ABI GloballyHashedType hashType(ArrayRef< uint8_t > RecordData, ArrayRef< GloballyHashedType > PreviousTypes, ArrayRef< GloballyHashedType > PreviousIds)
Given a sequence of bytes representing a record, compute a global hash for this record.
A locally hashed type represents a straightforward hash code of a serialized record.
Definition TypeHashing.h:35
static LLVM_ABI LocallyHashedType hashType(ArrayRef< uint8_t > RecordData)
Given a type, compute its local hash.
ArrayRef< uint8_t > RecordData
Definition TypeHashing.h:37