Mark Abseil 64-bit large-input hash as preserve_most and noinline. Hashing inputs longer than 32 bytes forces callers (such as hash table lookups and growth) to spill live registers across the out-of-line call, while ThinLTO and FDO inlining of the entry point bloats hot callers with duplicated dispatch and vector hashing code. - Apply the preserve_most calling convention to the 64-bit contiguous hash helpers for inputs over 32 bytes. - Mark the entry point as noinline so ThinLTO and FDO keep the preserve_most slow path out-of-line rather than duplicating it into callers. - Forward between internal helpers with musttail and disable loop unrolling in the >64-byte loop to minimize register usage inside the callees. Generated code diff in the hash implementations themselves (x86-64 with AES-NI): - 33-64 byte path: identical instructions with zero prologue/epilogue register saves (touches only rax and vector registers) and direct tail-jumps to the larger paths. - 65-1024 byte path: disabling 2x loop unrolling keeps the loop within rax, r11, and vector registers, so it also emits zero prologue/epilogue saves and shrinks by ~45%. - Inputs over 1024 bytes: the piecewise chunking loop saves 7 registers instead of 2 in its prologue/epilogue, amortized over multi-kilobyte inputs. PiperOrigin-RevId: 1021820919
diff --git a/absl/base/attributes.h b/absl/base/attributes.h index 32af1dd..3c1770f 100644 --- a/absl/base/attributes.h +++ b/absl/base/attributes.h
@@ -203,6 +203,50 @@ #define ABSL_INTERNAL_MUSTTAIL #endif +// ABSL_INTERNAL_PRESERVE_MOST +// +// Tells the compiler to use the `preserve_most` calling convention for a +// function, which preserves all general-purpose registers (except R11 on +// x86-64) across the call. This shifts the burden of saving and restoring +// registers from the caller to the callee, reducing register spills and code +// size at call sites that invoke cold out-of-line slow paths. +// +// MANDATORY USAGE REQUIREMENTS: +// * Out-of-line slow paths: Apply to cold or rarely executed out-of-line +// functions called from hot inline fast paths, where shrinking caller +// register-spill code outweighs any extra callee save/restore instructions. +// * Hot functions: May also be used on small, self-contained functions +// that use minimal registers, benefiting callers without degrading +// performance of the function itself. +// * Benchmark justification: The performance or code-size impact of using +// `ABSL_INTERNAL_PRESERVE_MOST` MUST be clearly demonstrated by benchmarks. +// * This macro is strictly for internal Abseil use and must not be exposed as a +// public Abseil API. +// +// See: https://clang.llvm.org/docs/AttributeReference.html#preserve-most +#ifdef ABSL_INTERNAL_HAVE_PRESERVE_MOST +#error "ABSL_INTERNAL_HAVE_PRESERVE_MOST cannot be directly set." +#elif defined(ABSL_INTERNAL_PRESERVE_MOST) +#error "ABSL_INTERNAL_PRESERVE_MOST cannot be directly set." +#elif ABSL_HAVE_CPP_ATTRIBUTE(clang::preserve_most) && \ + /* Clang <= 16 clobbered return registers in epilogues (D143425). */ \ + defined(__clang__) && __clang_major__ >= 17 && \ + /* AArch64 lazy PLT resolver clobbers X10-X15. */ \ + defined(__x86_64__) && \ + /* Unsupported on Windows PE/COFF and across DLL boundaries. */ \ + !defined(_WIN32) && !defined(ABSL_BUILD_DLL) && \ + !defined(ABSL_CONSUME_DLL) && \ + /* Unpatched x86-64 glibc lazy PLT resolver clobbers R10 (pre-GRTEv5). */ \ + (!defined(__GLIBC__) || \ + (defined(__GOOGLE_GRTE_VERSION__) && \ + __GOOGLE_GRTE_VERSION__ >= 20180301L)) +#define ABSL_INTERNAL_HAVE_PRESERVE_MOST 1 +#define ABSL_INTERNAL_PRESERVE_MOST [[clang::preserve_most]] +#else +#define ABSL_INTERNAL_HAVE_PRESERVE_MOST 0 +#define ABSL_INTERNAL_PRESERVE_MOST +#endif + // ABSL_ATTRIBUTE_WEAK // // Tags a function as weak for the purposes of compilation and linking.
diff --git a/absl/base/attributes_test.cc b/absl/base/attributes_test.cc index deabd5d..1489fb2 100644 --- a/absl/base/attributes_test.cc +++ b/absl/base/attributes_test.cc
@@ -105,4 +105,21 @@ EXPECT_EQ(NoSanitizeCfiCall([](int x) { return x + 1; }, 41), 42); } +ABSL_INTERNAL_PRESERVE_MOST ABSL_ATTRIBUTE_NOINLINE int PreserveMostCallee( + int a, int b) { + return a + b; +} + +TEST(Attributes, PreserveMost) { +#if defined(__GLIBC__) && \ + (!defined(__GOOGLE_GRTE_VERSION__) || __GOOGLE_GRTE_VERSION__ < 20180301L) + static_assert(!ABSL_INTERNAL_HAVE_PRESERVE_MOST); +#elif ABSL_HAVE_CPP_ATTRIBUTE(clang::preserve_most) && defined(__clang__) && \ + __clang_major__ >= 17 && defined(__x86_64__) && !defined(_WIN32) && \ + !defined(ABSL_BUILD_DLL) && !defined(ABSL_CONSUME_DLL) + static_assert(ABSL_INTERNAL_HAVE_PRESERVE_MOST); +#endif + EXPECT_EQ(PreserveMostCallee(20, 22), 42); +} + } // namespace
diff --git a/absl/hash/internal/hash.cc b/absl/hash/internal/hash.cc index df75c25..cbbb390 100644 --- a/absl/hash/internal/hash.cc +++ b/absl/hash/internal/hash.cc
@@ -215,8 +215,8 @@ return Mix4x16Vectors(na, nb, nc, nd); } -[[maybe_unused]] ABSL_ATTRIBUTE_NOINLINE uint64_t -LowLevelHashLenGt64(uint64_t seed, const void* data, size_t len) { +[[maybe_unused]] ABSL_INTERNAL_PRESERVE_MOST ABSL_ATTRIBUTE_NOINLINE uint64_t +LowLevelHashLenGt64(uint64_t seed, const unsigned char* data, size_t len) { assert(len > 64); const uint8_t* ptr = static_cast<const uint8_t*>(data); const uint8_t* last_32_ptr = ptr + len - 32; @@ -257,6 +257,9 @@ } }; MixerCD mix_cd = {state2, state3}; +#if defined(__clang__) +#pragma clang loop unroll(disable) +#endif do { PrefetchFutureDataToLocalCache(ptr); mix_ab(ptr); @@ -294,8 +297,8 @@ return Mix32Bytes(last_32_ptr, Mix32Bytes(ptr, current_state)); } -[[maybe_unused]] ABSL_ATTRIBUTE_NOINLINE uint64_t -LowLevelHashLenGt64(uint64_t seed, const void* data, size_t len) { +[[maybe_unused]] ABSL_INTERNAL_PRESERVE_MOST ABSL_ATTRIBUTE_NOINLINE uint64_t +LowLevelHashLenGt64(uint64_t seed, const unsigned char* data, size_t len) { assert(len > 64); const uint8_t* ptr = static_cast<const uint8_t*>(data); uint64_t current_state = seed ^ kStaticRandomData[0] ^ len; @@ -344,13 +347,13 @@ } #endif // ABSL_AES_INTERNAL_HAVE_X86_SIMD -[[maybe_unused]] uint64_t LowLevelHashLenGt32(uint64_t seed, const void* data, - size_t len) { +[[maybe_unused]] ABSL_INTERNAL_PRESERVE_MOST uint64_t +LowLevelHashLenGt32(uint64_t seed, const unsigned char* data, size_t len) { assert(len > 32); if (ABSL_PREDICT_FALSE(len > 64)) { - return LowLevelHashLenGt64(seed, data, len); + ABSL_INTERNAL_MUSTTAIL return LowLevelHashLenGt64(seed, data, len); } - return LowLevelHash33To64(seed, static_cast<const uint8_t*>(data), len); + return LowLevelHash33To64(seed, data, len); } ABSL_ATTRIBUTE_ALWAYS_INLINE inline uint64_t HashBlockOn32Bit( @@ -379,17 +382,17 @@ std::integral_constant<int, 4>{}); } -ABSL_ATTRIBUTE_ALWAYS_INLINE inline uint64_t HashBlockOn64Bit( - uint64_t state, const unsigned char* data, size_t len) { +ABSL_INTERNAL_PRESERVE_MOST ABSL_ATTRIBUTE_ALWAYS_INLINE inline uint64_t +HashBlockOn64Bit(uint64_t state, const unsigned char* data, size_t len) { #ifdef ABSL_HAVE_INTRINSIC_INT128 - return LowLevelHashLenGt32(state, data, len); + ABSL_INTERNAL_MUSTTAIL return LowLevelHashLenGt32(state, data, len); #else return hash_internal::CityHash64WithSeed(reinterpret_cast<const char*>(data), len, state); #endif } -ABSL_ATTRIBUTE_NOINLINE uint64_t +ABSL_INTERNAL_PRESERVE_MOST ABSL_ATTRIBUTE_NOINLINE uint64_t SplitAndCombineOn64Bit(uint64_t state, const unsigned char* first, size_t len) { while (len >= PiecewiseChunkSize()) { state = HashBlockOn64Bit(state, first, PiecewiseChunkSize()); @@ -419,15 +422,20 @@ return SplitAndCombineOn32Bit(state, first, len); } -uint64_t CombineLargeContiguousImplOn64BitLengthGt32(uint64_t state, - const unsigned char* first, - size_t len) { +ABSL_INTERNAL_PRESERVE_MOST ABSL_ATTRIBUTE_NOINLINE uint64_t +CombineLargeContiguousImplOn64BitLengthGt32(uint64_t state, + const unsigned char* first, + size_t len) { assert(len > 32); assert(sizeof(size_t) == 8); // NOLINT(misc-static-assert) if (ABSL_PREDICT_TRUE(len <= PiecewiseChunkSize())) { +#ifdef ABSL_HAVE_INTRINSIC_INT128 + ABSL_INTERNAL_MUSTTAIL return HashBlockOn64Bit(state, first, len); +#else return HashBlockOn64Bit(state, first, len); +#endif } - return SplitAndCombineOn64Bit(state, first, len); + ABSL_INTERNAL_MUSTTAIL return SplitAndCombineOn64Bit(state, first, len); } ABSL_CONST_INIT const void* const MixingHashState::kSeed = &kSeed;
diff --git a/absl/hash/internal/hash.h b/absl/hash/internal/hash.h index 8aef023..9d7b3e4 100644 --- a/absl/hash/internal/hash.h +++ b/absl/hash/internal/hash.h
@@ -1168,10 +1168,11 @@ CombineLargeContiguousImplOn32BitLengthGt8(uint64_t state, const unsigned char* first, size_t len); -ABSL_ATTRIBUTE_PURE_FUNCTION uint64_t -CombineLargeContiguousImplOn64BitLengthGt32(uint64_t state, - const unsigned char* first, - size_t len); +ABSL_INTERNAL_PRESERVE_MOST ABSL_ATTRIBUTE_PURE_FUNCTION ABSL_ATTRIBUTE_NOINLINE + uint64_t + CombineLargeContiguousImplOn64BitLengthGt32(uint64_t state, + const unsigned char* first, + size_t len); ABSL_ATTRIBUTE_ALWAYS_INLINE inline uint64_t CombineSmallContiguousImpl( uint64_t state, const unsigned char* first, size_t len) {