10b57cec5SDimitry Andric //===- TypeHashing.cpp -------------------------------------------*- C++-*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/TypeHashing.h"
100b57cec5SDimitry Andric 
110b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h"
12bdd1243dSDimitry Andric #include "llvm/Support/BLAKE3.h"
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric using namespace llvm;
150b57cec5SDimitry Andric using namespace llvm::codeview;
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric LocallyHashedType DenseMapInfo<LocallyHashedType>::Empty{0, {}};
180b57cec5SDimitry Andric LocallyHashedType DenseMapInfo<LocallyHashedType>::Tombstone{hash_code(-1), {}};
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric static std::array<uint8_t, 8> EmptyHash = {
210b57cec5SDimitry Andric     {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
220b57cec5SDimitry Andric static std::array<uint8_t, 8> TombstoneHash = {
230b57cec5SDimitry Andric     {0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric GloballyHashedType DenseMapInfo<GloballyHashedType>::Empty{EmptyHash};
260b57cec5SDimitry Andric GloballyHashedType DenseMapInfo<GloballyHashedType>::Tombstone{TombstoneHash};
270b57cec5SDimitry Andric 
hashType(ArrayRef<uint8_t> RecordData)280b57cec5SDimitry Andric LocallyHashedType LocallyHashedType::hashType(ArrayRef<uint8_t> RecordData) {
290b57cec5SDimitry Andric   return {llvm::hash_value(RecordData), RecordData};
300b57cec5SDimitry Andric }
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric GloballyHashedType
hashType(ArrayRef<uint8_t> RecordData,ArrayRef<GloballyHashedType> PreviousTypes,ArrayRef<GloballyHashedType> PreviousIds)330b57cec5SDimitry Andric GloballyHashedType::hashType(ArrayRef<uint8_t> RecordData,
340b57cec5SDimitry Andric                              ArrayRef<GloballyHashedType> PreviousTypes,
350b57cec5SDimitry Andric                              ArrayRef<GloballyHashedType> PreviousIds) {
360b57cec5SDimitry Andric   SmallVector<TiReference, 4> Refs;
370b57cec5SDimitry Andric   discoverTypeIndices(RecordData, Refs);
38bdd1243dSDimitry Andric   TruncatedBLAKE3<8> S;
390b57cec5SDimitry Andric   S.init();
400b57cec5SDimitry Andric   uint32_t Off = 0;
410b57cec5SDimitry Andric   S.update(RecordData.take_front(sizeof(RecordPrefix)));
420b57cec5SDimitry Andric   RecordData = RecordData.drop_front(sizeof(RecordPrefix));
430b57cec5SDimitry Andric   for (const auto &Ref : Refs) {
440b57cec5SDimitry Andric     // Hash any data that comes before this TiRef.
450b57cec5SDimitry Andric     uint32_t PreLen = Ref.Offset - Off;
460b57cec5SDimitry Andric     ArrayRef<uint8_t> PreData = RecordData.slice(Off, PreLen);
470b57cec5SDimitry Andric     S.update(PreData);
480b57cec5SDimitry Andric     auto Prev = (Ref.Kind == TiRefKind::IndexRef) ? PreviousIds : PreviousTypes;
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric     auto RefData = RecordData.slice(Ref.Offset, Ref.Count * sizeof(TypeIndex));
510b57cec5SDimitry Andric     // For each type index referenced, add in the previously computed hash
520b57cec5SDimitry Andric     // value of that type.
530b57cec5SDimitry Andric     ArrayRef<TypeIndex> Indices(
540b57cec5SDimitry Andric         reinterpret_cast<const TypeIndex *>(RefData.data()), Ref.Count);
550b57cec5SDimitry Andric     for (TypeIndex TI : Indices) {
560b57cec5SDimitry Andric       ArrayRef<uint8_t> BytesToHash;
570b57cec5SDimitry Andric       if (TI.isSimple() || TI.isNoneType()) {
580b57cec5SDimitry Andric         const uint8_t *IndexBytes = reinterpret_cast<const uint8_t *>(&TI);
59bdd1243dSDimitry Andric         BytesToHash = ArrayRef(IndexBytes, sizeof(TypeIndex));
600b57cec5SDimitry Andric       } else {
610b57cec5SDimitry Andric         if (TI.toArrayIndex() >= Prev.size() ||
620b57cec5SDimitry Andric             Prev[TI.toArrayIndex()].empty()) {
630b57cec5SDimitry Andric           // There are references to yet-unhashed records. Suspend hashing for
640b57cec5SDimitry Andric           // this record until all the other records are processed.
650b57cec5SDimitry Andric           return {};
660b57cec5SDimitry Andric         }
670b57cec5SDimitry Andric         BytesToHash = Prev[TI.toArrayIndex()].Hash;
680b57cec5SDimitry Andric       }
690b57cec5SDimitry Andric       S.update(BytesToHash);
700b57cec5SDimitry Andric     }
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric     Off = Ref.Offset + Ref.Count * sizeof(TypeIndex);
730b57cec5SDimitry Andric   }
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric   // Don't forget to add in any trailing bytes.
760b57cec5SDimitry Andric   auto TrailingBytes = RecordData.drop_front(Off);
770b57cec5SDimitry Andric   S.update(TrailingBytes);
780b57cec5SDimitry Andric 
79bdd1243dSDimitry Andric   return {S.final()};
800b57cec5SDimitry Andric }
81