StableHashing.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //===- llvm/CodeGen/StableHashing.h - Utilities for stable hashing * 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. // This file provides types and functions for computing and combining stable
  10. // hashes. Stable hashes can be useful for hashing across different modules,
  11. // processes, or compiler runs.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_CODEGEN_STABLEHASHING_H
  15. #define LLVM_CODEGEN_STABLEHASHING_H
  16. #include "llvm/ADT/StringRef.h"
  17. namespace llvm {
  18. /// An opaque object representing a stable hash code. It can be serialized,
  19. /// deserialized, and is stable across processes and executions.
  20. using stable_hash = uint64_t;
  21. // Implementation details
  22. namespace hashing {
  23. namespace detail {
  24. // Stable hashes are based on the 64-bit FNV-1 hash:
  25. // https://en.wikipedia.org/wiki/Fowler-Noll-Vo_hash_function
  26. const uint64_t FNV_PRIME_64 = 1099511628211u;
  27. const uint64_t FNV_OFFSET_64 = 14695981039346656037u;
  28. inline void stable_hash_append(stable_hash &Hash, const char Value) {
  29. Hash = Hash ^ (Value & 0xFF);
  30. Hash = Hash * FNV_PRIME_64;
  31. }
  32. inline void stable_hash_append(stable_hash &Hash, stable_hash Value) {
  33. for (unsigned I = 0; I < 8; ++I) {
  34. stable_hash_append(Hash, static_cast<char>(Value));
  35. Value >>= 8;
  36. }
  37. }
  38. } // namespace detail
  39. } // namespace hashing
  40. inline stable_hash stable_hash_combine(stable_hash A, stable_hash B) {
  41. stable_hash Hash = hashing::detail::FNV_OFFSET_64;
  42. hashing::detail::stable_hash_append(Hash, A);
  43. hashing::detail::stable_hash_append(Hash, B);
  44. return Hash;
  45. }
  46. inline stable_hash stable_hash_combine(stable_hash A, stable_hash B,
  47. stable_hash C) {
  48. stable_hash Hash = hashing::detail::FNV_OFFSET_64;
  49. hashing::detail::stable_hash_append(Hash, A);
  50. hashing::detail::stable_hash_append(Hash, B);
  51. hashing::detail::stable_hash_append(Hash, C);
  52. return Hash;
  53. }
  54. inline stable_hash stable_hash_combine(stable_hash A, stable_hash B,
  55. stable_hash C, stable_hash D) {
  56. stable_hash Hash = hashing::detail::FNV_OFFSET_64;
  57. hashing::detail::stable_hash_append(Hash, A);
  58. hashing::detail::stable_hash_append(Hash, B);
  59. hashing::detail::stable_hash_append(Hash, C);
  60. hashing::detail::stable_hash_append(Hash, D);
  61. return Hash;
  62. }
  63. /// Compute a stable_hash for a sequence of values.
  64. ///
  65. /// This hashes a sequence of values. It produces the same stable_hash as
  66. /// 'stable_hash_combine(a, b, c, ...)', but can run over arbitrary sized
  67. /// sequences and is significantly faster given pointers and types which
  68. /// can be hashed as a sequence of bytes.
  69. template <typename InputIteratorT>
  70. stable_hash stable_hash_combine_range(InputIteratorT First,
  71. InputIteratorT Last) {
  72. stable_hash Hash = hashing::detail::FNV_OFFSET_64;
  73. for (auto I = First; I != Last; ++I)
  74. hashing::detail::stable_hash_append(Hash, *I);
  75. return Hash;
  76. }
  77. inline stable_hash stable_hash_combine_array(const stable_hash *P, size_t C) {
  78. stable_hash Hash = hashing::detail::FNV_OFFSET_64;
  79. for (size_t I = 0; I < C; ++I)
  80. hashing::detail::stable_hash_append(Hash, P[I]);
  81. return Hash;
  82. }
  83. inline stable_hash stable_hash_combine_string(const StringRef &S) {
  84. return stable_hash_combine_range(S.begin(), S.end());
  85. }
  86. inline stable_hash stable_hash_combine_string(const char *C) {
  87. stable_hash Hash = hashing::detail::FNV_OFFSET_64;
  88. while (*C)
  89. hashing::detail::stable_hash_append(Hash, *(C++));
  90. return Hash;
  91. }
  92. } // namespace llvm
  93. #endif