Skip to content

fix: dispatch hashtree at runtime on every x86_64 target - #13

Merged
wemeetagain merged 1 commit into
mainfrom
cayman/x86-runtime-dispatch
Sep 30, 2026
Merged

wemeetagain merged 1 commit into
mainfrom
cayman/x86-runtime-dispatch

Conversation

@wemeetagain

Copy link
Copy Markdown
Member

Motivation

On x86_64, isSupported() only calls hashtree_hash when the compile-time CPU has avx512f+avx512vl, avx2+bmi2 or sha+avx. Baseline builds, including the published lodestar-z packages (built with only -Dtarget), therefore never link hashtree and hash every pair with std SHA-256, even though hashtree_hash selects its backend at runtime. Measured with lodestar-z on a Threadripper 1950X (SHA-NI), ReleaseSafe:

                            baseline (std)   baseline (this PR)   znver1
hash(), ns per pair         ~535             ~42                  ~42
mainnet BeaconState root    ~11.9 s          ~1.3 s               ~1.4 s

Separately, x86_64-macos with -Dcpu=haswell fails to link today (undefined symbol: _hashtree_sha256_avx2_x8, ...) because the x86 .S files are not assembled for Mach-O.

Description

Select hashtree on x86_64 by target OS instead of compile-time CPU features.

  • isSupported() returns true on x86_64 except macOS and Windows MSVC, matching the targets where build.zig assembles the x86 backends
  • macOS and Windows MSVC keep the std fallback regardless of -Dcpu, which also fixes the macOS link failure

Tested: zig build test natively, with -Dcpu=baseline (Debug and ReleaseSafe), the baseline test binary under qemu-x86_64 qemu64, Nehalem-v2 and EPYC-v1, and aarch64-linux-musl via qemu; x86_64-macos (with and without -Dcpu=haswell), x86_64-windows-gnu and x86_64-linux-musl build. x86_64-windows-msvc fails the same way on main (no MSVC headers here).

AI Assistance Disclosure

Primarily AI-authored with Claude Code, including the measurements.

Select hashtree on x86_64 by target OS instead of compile-time CPU features, since `hashtree_hash` already picks its backend at runtime.

- `isSupported()` returns true on x86_64 except macOS and Windows MSVC, the targets where `build.zig` does not assemble the x86 backends
- baseline builds (for example published packages built with only `-Dtarget`) now use SHA-NI, AVX-512, AVX2, AVX or SSE instead of std SHA-256
- macOS keeps the std fallback whatever `-Dcpu` is, which fixes the undefined `_hashtree_sha256_*` link errors with `-Dcpu=haswell`
@wemeetagain
wemeetagain merged commit 9b0fdad into main Sep 30, 2026
3 checks passed
@wemeetagain
wemeetagain deleted the cayman/x86-runtime-dispatch branch September 30, 2026 14:05
spiral-ladder pushed a commit to ChainSafe/lodestar-z that referenced this pull request Oct 1, 2026
**Motivation**

The current hashtree-z pin sends baseline x86_64 builds through Zig's
SHA-256 fallback even though hashtree selects its backend at runtime.
[ChainSafe/hashtree-z#13](ChainSafe/hashtree-z#13)
enables that dispatch on supported targets and fixes x86_64 macOS
linking with explicit CPU features.

**Description**

Update `hashtree` in `build.zig.zon` from `f5c1b81` to `9b0fdad`,
including the package hash generated by `zig fetch`.

**AI Assistance Disclosure**

Implementation was primarily AI-authored with Codex.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant