Files
carbon-lang/common/hashing.cpp
T
Chandler Carruth 34cf614a26 Improve register allocation across HashSizedBytesLarge calls (#8119)
Because this function was a non-static member function, calls passed
`this` across the call boundary, and the caller assumed it might clobber
the memory of `Hasher`.

This PR changes it to a `[[gnu::pure]]` and `static` function which
takes and returns a `uint64_t` by value like other static helpers.
Between knowing this does not write to memory and the state being passed
in and out in registers, this allows `Hasher` to stay in a single
register across all size classes, reducing stack memory accesses and
code size.

On an AMD Ryzen 9 9950X (Zen 5), hashtable and lexer benchmarks vs
`trunk` (10 runs; negative means faster):

| Hashtable benchmark     |         x86-64-v3 |         x86-64-v2 |
| :---------------------- | ----------------: | ----------------: |
| BM_SetContainsHitPtr    |  -2.1% (-4..+1%)  |  -1.1% (-3..+5%)  |
| BM_SetLookupHitPtr      |  -2.8% (-4..-1%)  |  -2.7% (-7..-2%)  |
| BM_SetEraseInsertHitPtr |  +1.0% (-1..+6%)  |  +1.8% (-0..+5%)  |
| BM_SetInsertSeq         |  -2.7% (-3..-2%)  |  -2.6% (-3..-2%)  |
| BM_MapContainsHit       |  -2.7% (-4..-0%)  |  -3.7% (-8..-1%)  |
| BM_MapUpdateHit         |  -6.2% (-9..-2%)  | -5.6% (-10..+2%)  |
| BM_MapLookupHit         |  -2.4% (-9..-1%)  |  -1.1% (-3..+2%)  |
| BM_MapInsertSeq         |  -2.4% (-4..-1%)  |  -0.5% (-3..+1%)  |
| 4-Hit-Family Mean       |            -3.47% |            -3.28% |

| Lexer benchmark                  |       x86-64-v3 |       x86-64-v2 |
| :------------------------------- | --------------: | --------------: |
| BM_ValidIdentifiers              | +0.1% (-1..+1%) | -0.8% (-3..+1%) |
| BM_ValidKeywords                 |           -1.3% |           +0.9% |
| BM_ValidKeywordsAsRawIdentifiers |           +0.0% |           +1.8% |
| BM_RawIdentifierFocus            |           +0.7% |           -0.3% |
| BM_RandomSource                  |           +0.8% |           -0.0% |

Zen 5 x86-64-v3 per-lookup hardware counters (`BM_SetContainsHitPtr`):

| Size/Keys | Variant |       Cycles |       Instrs |          Ops |
| :-------- | :------ | -----------: | -----------: | -----------: |
| 4096/256  | trunk   |        18.20 |        98.54 |        89.27 |
| 4096/256  | after   | 17.51 (-0.7) | 95.92 (-2.6) | 87.63 (-1.6) |
| 4096/2048 | trunk   |        19.63 |        98.39 |        89.11 |
| 4096/2048 | after   | 18.99 (-0.6) | 95.90 (-2.5) | 87.62 (-1.5) |

Assisted-by: Antigravity with various models
2026-11-20 21:57:55 +00:00

91 lines
3.3 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "common/hashing.h"
#include <cstddef>
#include "llvm/Support/FormatVariadic.h"
namespace Carbon {
auto HashCode::Print(llvm::raw_ostream& out) const -> void {
out << llvm::formatv("{0:x16}", value_);
}
auto Hasher::HashSizedBytesLarge(llvm::ArrayRef<std::byte> bytes,
uint64_t buffer) -> uint64_t {
const std::byte* data_ptr = bytes.data();
const ssize_t size = bytes.size();
CARBON_DCHECK(size > 32);
// If we have 64 bytes or more, we're going to handle two 32-byte chunks at a
// time using a simplified version of the main algorithm. This is based
// heavily on the 64-byte and larger processing approach used by Abseil. The
// goal is to mix the input data using as few multiplies (or other operations)
// as we can and with as much [ILP][1] as we can. The ILP comes largely from
// creating parallel structures to the operations.
//
// [1]: https://en.wikipedia.org/wiki/Instruction-level_parallelism
auto mix32 = [](const std::byte* data_ptr, uint64_t buffer, uint64_t random0,
uint64_t random1) {
uint64_t a = Read8(data_ptr);
uint64_t b = Read8(data_ptr + 8);
uint64_t c = Read8(data_ptr + 16);
uint64_t d = Read8(data_ptr + 24);
uint64_t m0 = Mix(a ^ random0, b ^ buffer);
uint64_t m1 = Mix(c ^ random1, d ^ buffer);
return (m0 ^ m1);
};
// Prefetch the first bytes into cache.
__builtin_prefetch(data_ptr, 0 /* read */, 0 /* discard after next use */);
uint64_t buffer0 = buffer ^ StaticRandomData[0];
uint64_t buffer1 = buffer ^ StaticRandomData[2];
const std::byte* tail_32b_ptr = data_ptr + (size - 32);
const std::byte* tail_16b_ptr = data_ptr + (size - 16);
const std::byte* end_ptr = data_ptr + (size - 64);
while (data_ptr < end_ptr) {
// Prefetch the next 64-bytes while we process the current 64-bytes.
__builtin_prefetch(data_ptr + 64, 0 /* read */,
0 /* discard after next use */);
buffer0 =
mix32(data_ptr, buffer0, StaticRandomData[4], StaticRandomData[5]);
buffer1 =
mix32(data_ptr + 32, buffer1, StaticRandomData[6], StaticRandomData[7]);
data_ptr += 64;
}
// If we haven't reached our 32-byte tail pointer, consume another 32-bytes
// directly.
if (data_ptr < tail_32b_ptr) {
buffer0 =
mix32(data_ptr, buffer0, StaticRandomData[4], StaticRandomData[5]);
data_ptr += 32;
}
if (data_ptr < tail_16b_ptr) {
// We have more than 16-bytes in the tail so use a full 32-byte mix from the
// 32-byte tail pointer.
buffer1 =
mix32(tail_32b_ptr, buffer1, StaticRandomData[6], StaticRandomData[7]);
} else {
// 16-bytes or less in the tail, do something more minimal instead of a full
// 32-byte mix. As this only involves a single multiply, we don't decompose
// further even when the tail is (much) shorter.
buffer1 = Mix(Read8(tail_16b_ptr) ^ StaticRandomData[6],
Read8(tail_16b_ptr + 8) ^ buffer1);
}
Hasher hasher;
hasher.buffer = buffer0 ^ buffer1;
hasher.HashDense(size);
return hasher.buffer;
}
} // namespace Carbon