Skip to content

build: native build flags from #70 (asm, arch baseline, hardening)#71

Merged
dfa1 merged 3 commits into
mainfrom
native-build-flags-70
Jul 17, 2026
Merged

build: native build flags from #70 (asm, arch baseline, hardening)#71
dfa1 merged 3 commits into
mainfrom
native-build-flags-70

Conversation

@dfa1

@dfa1 dfa1 commented Jul 17, 2026

Copy link
Copy Markdown
Owner

Summary

Three flags from #70, each landed and verified independently:

  • Enable x86-64 BMI2 Huffman-decode assembly (3a4c32b): drop -DZSTD_DISABLE_ASM=1, include huf_decompress_amd64.S. Its body is entirely wrapped in #if ZSTD_ENABLE_ASM_X86_64_BMI2 (gated on __x86_64__), so it's a no-op on non-x86_64 targets and activates via zstd's own runtime BMI2 detection where it matters. Confirmed byte-identical object code on this arm64 host; benefits linux-x86_64/osx-x86_64/windows-x86_64.
  • Target ARMv8-A+CRC baseline on aarch64 (6b9e622): -mcpu=generic+crc (zig's syntax for the issue's -march=armv8-a+crc). Measured on this host with a quick JMH smoke run: compress +6.9%, decompress +12-14% for our native path, with unaffected baselines (zstdJni, aircompressor) flat as a sanity check.
  • Full RELRO + immediate binding on Linux (a55266c): -Wl,-z,relro,-z,now, ELF-only. Not a throughput lever, so verified directly with llvm-readelf (GNU_RELRO segment + BIND_NOW) rather than benchmarked.

Not landed: LTO (-flto) — zig's Mach-O backend is a self-hosted linker with no LTO support (error: LTO requires using LLD), confirmed with no local workaround (no ld64.lld available, no Docker to validate the Linux/Windows side where zig's bundled lld does support it). Left for follow-up if a real LLD Mach-O linker becomes available.

Test plan

  • ./mvnw -q validate (checkstyle) clean
  • ./mvnw -q -pl zstd,integration-tests -am test passes on osx-aarch64 (golden-corpus interop included)
  • All six classifiers (osx-aarch64, osx-x86_64, linux-x86_64, linux-aarch64, windows-x86_64, windows-aarch64) cross-compile cleanly with zig cc
  • llvm-readelf confirms GNU_RELRO/BIND_NOW on both Linux classifiers
  • CI native-build + smoke-test matrix (Linux x86_64/aarch64 in particular — untestable locally without Docker)

🤖 Generated with Claude Code

dfa1 added 3 commits July 17, 2026 15:22
Drop -DZSTD_DISABLE_ASM=1 and include huf_decompress_amd64.S in the
build. Its body is entirely wrapped in
`#if ZSTD_ENABLE_ASM_X86_64_BMI2` (gated on __x86_64__), so it
compiles to a no-op on every non-x86_64 classifier and only activates
there via zstd's own runtime BMI2 detection — safe to always include.

Verified it cross-compiles cleanly for all six classifiers. No
measurable effect on this aarch64 host (confirmed byte-identical
object code with/without the flag here), but linux-x86_64,
osx-x86_64, and windows-x86_64 builds gain the hand-tuned asm decode
path, matching what zstd's own Makefile enables by default.

Ref #70
Add -mcpu=generic+crc for aarch64 classifiers (zig's syntax for the
issue's -march=armv8-a+crc; clang's GCC-style -march= isn't accepted
by zig's driver). "generic" keeps every aarch64 CPU supported, unlike
pinning to e.g. apple_m1.

Confirmed real codegen changes (compiled object sizes differ) and
measurable throughput gains on this arm64 host: compress +6.9%,
decompress +12-14% for our native path (zstdJavaBytes/zstdJavaSegment)
in a quick JMH smoke run, with unaffected baselines (zstdJni,
aircompressor) flat as a sanity check. Cross-compiles cleanly for all
six classifiers; local unit + integration tests pass.

Ref #70
Add -Wl,-z,relro,-z,now for linux-* classifiers: the GOT is remapped
read-only once startup relocation completes, closing off the classic
GOT-overwrite exploit primitive. ELF-only — -z is a GNU ld/lld
concept with no Mach-O or PE equivalent, so osx-*/windows-* are
unaffected.

Not a throughput lever (eager symbol binding, not codec hot path), so
verified directly instead of via benchmark: llvm-readelf confirms a
GNU_RELRO program header and BIND_NOW/FLAGS_1 NOW on both
linux-x86_64 and linux-aarch64. Cross-compiles cleanly for all six
classifiers; local unit + integration tests pass.

Ref #70
@dfa1
dfa1 merged commit 34c173a into main Jul 17, 2026
1 check passed
@dfa1
dfa1 deleted the native-build-flags-70 branch July 17, 2026 16:27
dfa1 added a commit that referenced this pull request Jul 17, 2026
Add -flto for linux-* classifiers only. zig's Mach-O backend is a
self-hosted linker with no LTO support ("error: LTO requires using
LLD" even at the -c compile stage) — confirmed no workaround exists
locally (no real ld64.lld available, zig cc doesn't expose a linker
toggle). ELF is different: zig 0.16 still links it through the
bundled real lld by default, and -flto compiles and links cleanly
when cross-compiled to x86_64-linux-gnu/aarch64-linux-gnu from this
host, with the full public API surface (verified via
llvm-readelf --dyn-syms) intact after LTO.

What isn't verified locally: whether the LTO-linked library is
actually correct at runtime — this Mac can't execute Linux ELF
binaries. zstd/pom.xml and integration-tests/pom.xml activate their
native-classifier profile by host OS/arch, so ci.yml's ubuntu-latest
job will natively build and test-run exactly the linux-x86_64
classifier this touches. Marking this experimental pending that
signal, rather than folding it into #71 (asm/arch/hardening), which
only contains changes verified end-to-end on this host.

osx-*/windows-* classifiers are untouched (LTO_FLAG stays empty);
confirmed unaffected locally (rebuilt + zstd/integration-tests suite
passes on osx-aarch64, all six classifiers still cross-compile).

Ref #70
dfa1 added a commit that referenced this pull request Jul 18, 2026
Covers the four changes that landed from #70's investigation (#71,
#73, #75) and notes what was investigated and rejected (LTO,
x86-64-v3) with a pointer to the full benchmark data on #70.
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