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: {}};
18
19static std::array<uint8_t, 8> EmptyHash = {
20 ._M_elems: {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
21
22GloballyHashedType DenseMapInfo<GloballyHashedType>::Empty{EmptyHash};
23
24LocallyHashedType LocallyHashedType::hashType(ArrayRef<uint8_t> RecordData) {
25 return {.Hash: llvm::hash_value(S: RecordData), .RecordData: RecordData};
26}
27
28GloballyHashedType
29GloballyHashedType::hashType(ArrayRef<uint8_t> RecordData,
30 ArrayRef<GloballyHashedType> PreviousTypes,
31 ArrayRef<GloballyHashedType> PreviousIds) {
32 SmallVector<TiReference, 4> Refs;
33 discoverTypeIndices(RecordData, Refs);
34 TruncatedBLAKE3<8> S;
35 S.init();
36 uint32_t Off = 0;
37 S.update(Data: RecordData.take_front(N: sizeof(RecordPrefix)));
38 RecordData = RecordData.drop_front(N: 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(N: Off, M: PreLen);
43 S.update(Data: PreData);
44 auto Prev = (Ref.Kind == TiRefKind::IndexRef) ? PreviousIds : PreviousTypes;
45
46 auto RefData = RecordData.slice(N: Ref.Offset, M: 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(Data: 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(N: Off);
73 S.update(Data: TrailingBytes);
74
75 return {S.final()};
76}
77