StableHashing.h 3.9 KB

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