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
9#include "llvm/DebugInfo/CodeView/TypeHashing.h"
10
11#include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h"
12#include "llvm/Support/BLAKE3.h"
13
14using namespace llvm;
15using namespace llvm::codeview;
16
17LocallyHashedType DenseMapInfo<LocallyHashedType>::Empty{.Hash: 0, .RecordData: {}};
18LocallyHashedType DenseMapInfo<LocallyHashedType>::Tombstone{.Hash: hash_code(-1), .RecordData: {}};
19
20static std::array<uint8_t, 8> EmptyHash = {
21 ._M_elems: {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
22static std::array<uint8_t, 8> TombstoneHash = {
23 ._M_elems: {0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
24
25GloballyHashedType DenseMapInfo<GloballyHashedType>::Empty{EmptyHash};
26GloballyHashedType DenseMapInfo<GloballyHashedType>::Tombstone{TombstoneHash};
27
28LocallyHashedType LocallyHashedType::hashType(ArrayRef<uint8_t> RecordData) {
29 return {.Hash: llvm::hash_value(S: RecordData), .RecordData: RecordData};
30}
31
32GloballyHashedType
33GloballyHashedType::hashType(ArrayRef<uint8_t> RecordData,
34 ArrayRef<GloballyHashedType> PreviousTypes,
35 ArrayRef<GloballyHashedType> PreviousIds) {
36 SmallVector<TiReference, 4> Refs;
37 discoverTypeIndices(RecordData, Refs);
38 TruncatedBLAKE3<8> S;
39 S.init();
40 uint32_t Off = 0;
41 S.update(Data: RecordData.take_front(N: sizeof(RecordPrefix)));
42 RecordData = RecordData.drop_front(N: sizeof(RecordPrefix));
43 for (const auto &Ref : Refs) {
44 // Hash any data that comes before this TiRef.
45 uint32_t PreLen = Ref.Offset - Off;
46 ArrayRef<uint8_t> PreData = RecordData.slice(N: Off, M: PreLen);
47 S.update(Data: PreData);
48 auto Prev = (Ref.Kind == TiRefKind::IndexRef) ? PreviousIds : PreviousTypes;
49
50 auto RefData = RecordData.slice(N: Ref.Offset, M: Ref.Count * sizeof(TypeIndex));
51 // For each type index referenced, add in the previously computed hash
52 // value of that type.
53 ArrayRef<TypeIndex> Indices(
54 reinterpret_cast<const TypeIndex *>(RefData.data()), Ref.Count);
55 for (TypeIndex TI : Indices) {
56 ArrayRef<uint8_t> BytesToHash;
57 if (TI.isSimple() || TI.isNoneType()) {
58 const uint8_t *IndexBytes = reinterpret_cast<const uint8_t *>(&TI);
59 BytesToHash = ArrayRef(IndexBytes, sizeof(TypeIndex));
60 } else {
61 if (TI.toArrayIndex() >= Prev.size() ||
62 Prev[TI.toArrayIndex()].empty()) {
63 // There are references to yet-unhashed records. Suspend hashing for
64 // this record until all the other records are processed.
65 return {};
66 }
67 BytesToHash = Prev[TI.toArrayIndex()].Hash;
68 }
69 S.update(Data: BytesToHash);
70 }
71
72 Off = Ref.Offset + Ref.Count * sizeof(TypeIndex);
73 }
74
75 // Don't forget to add in any trailing bytes.
76 auto TrailingBytes = RecordData.drop_front(N: Off);
77 S.update(Data: TrailingBytes);
78
79 return {S.final()};
80}
81

source code of llvm/lib/DebugInfo/CodeView/TypeHashing.cpp