Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:09

0001 //===- llvm/ADT/StableHashing.h - Utilities for stable hashing * C++ *-----===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file provides types and functions for computing and combining stable
0010 // hashes. Stable hashes can be useful for hashing across different modules,
0011 // processes, machines, or compiler runs for a specific compiler version. It
0012 // currently employs the xxh3_64bits hashing algorithm. Be aware that this
0013 // implementation may be adjusted or updated as improvements to the compiler are
0014 // made.
0015 //
0016 //===----------------------------------------------------------------------===//
0017 
0018 #ifndef LLVM_ADT_STABLEHASHING_H
0019 #define LLVM_ADT_STABLEHASHING_H
0020 
0021 #include "llvm/ADT/StringRef.h"
0022 #include "llvm/Support/xxhash.h"
0023 
0024 namespace llvm {
0025 
0026 /// An opaque object representing a stable hash code. It can be serialized,
0027 /// deserialized, and is stable across processes and executions.
0028 using stable_hash = uint64_t;
0029 
0030 inline stable_hash stable_hash_combine(ArrayRef<stable_hash> Buffer) {
0031   const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(Buffer.data());
0032   size_t Size = Buffer.size() * sizeof(stable_hash);
0033   return xxh3_64bits(ArrayRef<uint8_t>(Ptr, Size));
0034 }
0035 
0036 inline stable_hash stable_hash_combine(stable_hash A, stable_hash B) {
0037   stable_hash Hashes[2] = {A, B};
0038   return stable_hash_combine(Hashes);
0039 }
0040 
0041 inline stable_hash stable_hash_combine(stable_hash A, stable_hash B,
0042                                        stable_hash C) {
0043   stable_hash Hashes[3] = {A, B, C};
0044   return stable_hash_combine(Hashes);
0045 }
0046 
0047 inline stable_hash stable_hash_combine(stable_hash A, stable_hash B,
0048                                        stable_hash C, stable_hash D) {
0049   stable_hash Hashes[4] = {A, B, C, D};
0050   return stable_hash_combine(Hashes);
0051 }
0052 
0053 // Removes suffixes introduced by LLVM from the name to enhance stability and
0054 // maintain closeness to the original name across different builds.
0055 inline StringRef get_stable_name(StringRef Name) {
0056   // Return the part after ".content." that represents contents.
0057   auto [P0, S0] = Name.rsplit(".content.");
0058   if (!S0.empty())
0059     return S0;
0060 
0061   // Ignore these suffixes.
0062   auto [P1, S1] = Name.rsplit(".llvm.");
0063   auto [P2, S2] = P1.rsplit(".__uniq.");
0064   return P2;
0065 }
0066 
0067 // Generates a consistent hash value for a given input name across different
0068 // program executions and environments. This function first converts the input
0069 // name into a stable form using the `get_stable_name` function, and then
0070 // computes a hash of this stable name. For instance, `foo.llvm.1234` would have
0071 // the same hash as `foo.llvm.5678.
0072 inline stable_hash stable_hash_name(StringRef Name) {
0073   return xxh3_64bits(get_stable_name(Name));
0074 }
0075 
0076 } // namespace llvm
0077 
0078 #endif