Skip to content

Commit bf60496

Browse files
authored
fix: use explicit zig arch targets instead of native (#21682)
## Summary Supersedes #21685. Fixes SIGILL crashes on ARM64 Mac devcontainers caused by zig's `-target native` detecting host CPU features (SVE on Graviton) and emitting instructions Apple Silicon can't execute. #21611 tried to fix this with `-march=generic` from cmake, but that's not a valid AArch64 flag — zig silently ignored it. #21685 fixed ARM only; this PR also fixes x86 and simplifies the architecture targeting: - **zig wrappers**: Replace `-target native` with explicit `aarch64`/`x86_64` targets on both architectures, preventing host-specific CPU feature detection (SVE, AVX-512, etc.). Error on unsupported architectures. - **arch.cmake**: Hardcode `-march=skylake` on x86, skip on ARM. Remove `TARGET_ARCH` variable and auto-detection logic entirely. - **CMakePresets.json**: Remove all `TARGET_ARCH` entries from cross-compile presets (invalid `generic` on arm64-linux, redundant `skylake` on amd64-linux/windows). ## Note on TARGET_ARCH removal `TARGET_ARCH` was originally introduced in #2660 (Oct 2023) so the `publish-bb.yml` GitHub Actions workflow could target `westmere` for broader x86 compatibility when shipping standalone bb binaries. That workflow was deleted in #12347 (Feb 2025), so there are no remaining consumers of configurable arch targeting. If bb binaries are ever distributed to end users on pre-skylake x86 hardware again, this would need to be reintroduced (e.g. via `-DCMAKE_CXX_FLAGS=-march=westmere` or a new variable). For now, all consumers are assumed to be on modern hardware where skylake is the minimum. ## Test plan - [ ] CI barretenberg build passes on ARM and x86 - [ ] M3 devcontainer: `NO_CACHE=1` rebuild produces working binaries (no SIGILL)
2 parents 848af53 + 22ba61d commit bf60496

4 files changed

Lines changed: 17 additions & 25 deletions

File tree

barretenberg/cpp/CMakePresets.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,6 @@
489489
},
490490
"cacheVariables": {
491491
"CMAKE_SYSTEM_NAME": "Linux",
492-
"TARGET_ARCH": "skylake",
493492
"AVM_TRANSPILER_LIB": "${sourceDir}/../../avm-transpiler/target/x86_64-apple-darwin/release/libavm_transpiler.a"
494493
}
495494
},
@@ -503,7 +502,6 @@
503502
},
504503
"cacheVariables": {
505504
"CMAKE_SYSTEM_NAME": "Linux",
506-
"TARGET_ARCH": "generic",
507505
"AVM_TRANSPILER_LIB": "${sourceDir}/../../avm-transpiler/target/aarch64-unknown-linux-gnu/release/libavm_transpiler.a"
508506
}
509507
},
@@ -554,7 +552,6 @@
554552
"cacheVariables": {
555553
"CMAKE_SYSTEM_NAME": "Windows",
556554
"CMAKE_SYSTEM_PROCESSOR": "x86_64",
557-
"TARGET_ARCH": "skylake",
558555
"BB_LITE": "ON",
559556
"AVM_TRANSPILER_LIB": ""
560557
}

barretenberg/cpp/cmake/arch.cmake

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,9 @@ if(WASM)
55
add_compile_options(-fno-exceptions -fno-slp-vectorize)
66
endif()
77

8-
# Auto-detect TARGET_ARCH on x86_64 if not explicitly set (native builds only).
9-
# On ARM, we skip -march entirely — the zig wrappers use an explicit aarch64 target
10-
# to produce generic ARM64 code without CPU-specific extensions (e.g. SVE).
11-
# Skip auto-detection when cross-compiling — the toolchain (e.g. Zig -mcpu) handles
12-
# architecture targeting, and injecting -march here conflicts with it.
13-
if(NOT WASM AND NOT TARGET_ARCH AND NOT ARM AND NOT CMAKE_CROSSCOMPILING)
14-
set(TARGET_ARCH "skylake")
15-
endif()
16-
17-
if(NOT WASM AND TARGET_ARCH)
18-
message(STATUS "Target architecture: ${TARGET_ARCH}")
19-
add_compile_options(-march=${TARGET_ARCH})
8+
# Target skylake on x86 for AVX2 etc. ARM is handled by the zig wrapper scripts
9+
# which use explicit aarch64 targets to produce generic ARM64 code without
10+
# CPU-specific extensions (e.g. SVE on Graviton) that would break on Apple Silicon.
11+
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
12+
add_compile_options(-march=skylake)
2013
endif()

barretenberg/cpp/scripts/zig-c++.sh

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#!/bin/bash
22
# Wrapper for zig c++ that pins glibc 2.35 on Linux (Ubuntu 22.04+ compat)
33
# and uses native target on macOS.
4-
# On ARM64 Linux, use an explicit aarch64 target instead of 'native' to produce
5-
# generic ARM64 code. This prevents CPU-specific instructions (e.g. SVE on Graviton)
6-
# from being emitted, ensuring binaries work across all ARM64 machines including
7-
# Apple Silicon in devcontainers.
4+
# Use explicit architecture targets instead of 'native' to prevent zig from
5+
# detecting host-specific CPU features (e.g. SVE on Graviton, AVX-512 on
6+
# Sapphire Rapids) that would produce binaries incompatible with other machines.
7+
# cmake's arch.cmake handles -march=skylake for x86; ARM gets baseline aarch64 (no SVE).
88
if [[ "$(uname -s)" == "Linux" ]]; then
99
case "$(uname -m)" in
1010
aarch64|arm64) exec zig c++ -target aarch64-linux-gnu.2.35 "$@" ;;
11-
*) exec zig c++ -target native-linux-gnu.2.35 "$@" ;;
11+
x86_64|amd64) exec zig c++ -target x86_64-linux-gnu.2.35 "$@" ;;
12+
*) echo "Error: unsupported architecture '$(uname -m)'" >&2; exit 1 ;;
1213
esac
1314
else
1415
exec zig c++ "$@"

barretenberg/cpp/scripts/zig-cc.sh

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#!/bin/bash
22
# Wrapper for zig cc that pins glibc 2.35 on Linux (Ubuntu 22.04+ compat)
33
# and uses native target on macOS.
4-
# On ARM64 Linux, use an explicit aarch64 target instead of 'native' to produce
5-
# generic ARM64 code. This prevents CPU-specific instructions (e.g. SVE on Graviton)
6-
# from being emitted, ensuring binaries work across all ARM64 machines including
7-
# Apple Silicon in devcontainers.
4+
# Use explicit architecture targets instead of 'native' to prevent zig from
5+
# detecting host-specific CPU features (e.g. SVE on Graviton, AVX-512 on
6+
# Sapphire Rapids) that would produce binaries incompatible with other machines.
7+
# cmake's arch.cmake handles -march=skylake for x86; ARM gets baseline aarch64 (no SVE).
88
if [[ "$(uname -s)" == "Linux" ]]; then
99
case "$(uname -m)" in
1010
aarch64|arm64) exec zig cc -target aarch64-linux-gnu.2.35 "$@" ;;
11-
*) exec zig cc -target native-linux-gnu.2.35 "$@" ;;
11+
x86_64|amd64) exec zig cc -target x86_64-linux-gnu.2.35 "$@" ;;
12+
*) echo "Error: unsupported architecture '$(uname -m)'" >&2; exit 1 ;;
1213
esac
1314
else
1415
exec zig cc "$@"

0 commit comments

Comments
 (0)