Skip to content

Commit f10e8f1

Browse files
committed
AArch64: Add compile-time guard for Neon
Previously, we assumed that every AArch64 system has Neon, and that the implementation has access to it. With this commit, use of Neon is explicitly guarded by __ARM_NEON, similar to how all use of AVX2 is guarded by __AVX2__. Signed-off-by: Hanno Becker <beckphan@amazon.co.uk>
1 parent 5dbdf15 commit f10e8f1

36 files changed

Lines changed: 138 additions & 50 deletions

.github/workflows/ci.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ jobs:
3434
arch: aarch64
3535
mode: native
3636
nix_shell: ci
37+
- runner: ubuntu-24.04-arm
38+
name: 'aarch64, no Neon'
39+
arch: aarch64
40+
mode: native
41+
nix_shell: ci
42+
cflags: '-march=armv8-a+nosimd'
43+
extra_args: '--no-auto'
3744
- runner: ubuntu-24.04-arm
3845
name: 'x86_64, cross'
3946
arch: x86_64
@@ -112,6 +119,7 @@ jobs:
112119
nix-cache: ${{ matrix.target.mode == 'native' && 'false' || 'true' }}
113120
gh_token: ${{ secrets.GITHUB_TOKEN }}
114121
compile_mode: ${{ matrix.target.mode }}
122+
cflags: ${{ matrix.target.cflags || '' }}
115123
extra_args: ${{ matrix.target.extra_args || '' }}
116124
exec_wrapper: ${{ matrix.target.exec_wrapper || '' }}
117125
opt: 'no_opt'
@@ -122,8 +130,9 @@ jobs:
122130
nix-shell: ${{ matrix.target.nix_shell }}
123131
gh_token: ${{ secrets.GITHUB_TOKEN }}
124132
compile_mode: native
125-
cflags: "-DMLDSA_DEBUG -fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all"
133+
cflags: "${{ matrix.target.cflags || '' }} -DMLDSA_DEBUG -fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all"
126134
ldflags: "-fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all"
135+
extra_args: ${{ matrix.target.extra_args || '' }}
127136
check_namespace: 'false'
128137
- name: build + test (cross, opt)
129138
uses: ./.github/actions/multi-functest
@@ -134,6 +143,7 @@ jobs:
134143
nix-cache: ${{ matrix.target.mode == 'native' && 'false' || 'true' }}
135144
gh_token: ${{ secrets.GITHUB_TOKEN }}
136145
compile_mode: ${{ matrix.target.mode }}
146+
cflags: ${{ matrix.target.cflags || '' }}
137147
extra_args: ${{ matrix.target.extra_args || '' }}
138148
exec_wrapper: ${{ matrix.target.exec_wrapper || '' }}
139149
opt: 'opt'
@@ -148,7 +158,7 @@ jobs:
148158
compile_mode: ${{ matrix.target.mode }}
149159
extra_args: ${{ matrix.target.extra_args || '' }}
150160
exec_wrapper: ${{ matrix.target.exec_wrapper || '' }}
151-
cflags: "-DMLDSA_DEBUG"
161+
cflags: "${{ matrix.target.cflags || '' }} -DMLDSA_DEBUG"
152162
opt: 'opt'
153163
backend_tests:
154164
name: AArch64 FIPS202 backends (${{ matrix.backend }})

dev/fips202/aarch64/auto.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
#if (!defined(MLD_CONFIG_NO_KEYPAIR_API) || \
4343
!defined(MLD_CONFIG_NO_SIGN_API) || !defined(MLD_CONFIG_REDUCE_RAM)) && \
4444
!defined(MLD_CONFIG_SERIAL_FIPS202_ONLY)
45+
/* Batched, SIMD-based Keccak-f1600 implementations. */
46+
#if defined(MLD_SYS_AARCH64_NEON)
47+
4548
/*
4649
* Keccak-f1600x2/x4
4750
*
@@ -74,6 +77,8 @@
7477

7578
#endif /* !__ARM_FEATURE_SHA3 */
7679

80+
#endif /* MLD_SYS_AARCH64_NEON */
81+
7782
#endif /* (!MLD_CONFIG_NO_KEYPAIR_API || !MLD_CONFIG_NO_SIGN_API || \
7883
!MLD_CONFIG_REDUCE_RAM) && !MLD_CONFIG_SERIAL_FIPS202_ONLY */
7984

mldsa/mldsa_native.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@
525525
#undef MLD_SYSV_ABI_SUPPORTED
526526
#undef MLD_SYS_AARCH64
527527
#undef MLD_SYS_AARCH64_EB
528+
#undef MLD_SYS_AARCH64_NEON
528529
#undef MLD_SYS_APPLE
529530
#undef MLD_SYS_ARMV81M_MVE
530531
#undef MLD_SYS_BIG_ENDIAN

mldsa/mldsa_native_asm.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@
552552
#undef MLD_SYSV_ABI_SUPPORTED
553553
#undef MLD_SYS_AARCH64
554554
#undef MLD_SYS_AARCH64_EB
555+
#undef MLD_SYS_AARCH64_NEON
555556
#undef MLD_SYS_APPLE
556557
#undef MLD_SYS_ARMV81M_MVE
557558
#undef MLD_SYS_BIG_ENDIAN

mldsa/src/fips202/native/aarch64/auto.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
#if (!defined(MLD_CONFIG_NO_KEYPAIR_API) || \
4343
!defined(MLD_CONFIG_NO_SIGN_API) || !defined(MLD_CONFIG_REDUCE_RAM)) && \
4444
!defined(MLD_CONFIG_SERIAL_FIPS202_ONLY)
45+
/* Batched, SIMD-based Keccak-f1600 implementations. */
46+
#if defined(MLD_SYS_AARCH64_NEON)
47+
4548
/*
4649
* Keccak-f1600x2/x4
4750
*
@@ -74,6 +77,8 @@
7477

7578
#endif /* !__ARM_FEATURE_SHA3 */
7679

80+
#endif /* MLD_SYS_AARCH64_NEON */
81+
7782
#endif /* (!MLD_CONFIG_NO_KEYPAIR_API || !MLD_CONFIG_NO_SIGN_API || \
7883
!MLD_CONFIG_REDUCE_RAM) && !MLD_CONFIG_SERIAL_FIPS202_ONLY */
7984

mldsa/src/native/meta.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
#include "../sys.h"
1414

15-
#ifdef MLD_SYS_AARCH64
15+
#ifdef MLD_SYS_AARCH64_NEON
1616
#include "aarch64/meta.h"
1717
#endif
1818

mldsa/src/sys.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@
3939
#define MLD_SYS_AARCH64
4040
#endif
4141

42+
/* Check if the AArch64 compilation target supports NEON (Advanced SIMD).
43+
*
44+
* Some compilers also define __ARM_NEON__, but __ARM_NEON is the most reliable
45+
* signal. Specifically, clang on Apple appears to keep __ARM_NEON__ set even if
46+
* -march=armv8-a+nosimd is set.
47+
*
48+
* gcc 4.8 -- the first gcc version introducing Neon support -- sets neither
49+
* __ARM_NEON nor __ARM_NEON__; in fact, there is no preprocessor signal that
50+
* Neon is enabled. If you use gcc 4.8 and need Neon, you should set
51+
* MLD_SYS_AARCH64_NEON manually. gcc 4.9 onwards do set __ARM_NEON.
52+
*/
53+
#if defined(MLD_SYS_AARCH64) && defined(__ARM_NEON)
54+
#define MLD_SYS_AARCH64_NEON
55+
#endif
56+
4257
/* Check if we're running on an AArch64 big endian system. */
4358
#if defined(__AARCH64EB__)
4459
#define MLD_SYS_AARCH64_EB

scripts/autogen

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4257,10 +4257,10 @@ ABI_CAPS = {
42574257
},
42584258
"NEON": {
42594259
"asm_subdir": "aarch64",
4260-
"guard": None,
4260+
"guard": "MLD_SYS_AARCH64_NEON",
42614261
"syscap": "MLD_SYS_CAP_AARCH64_NEON",
42624262
"description": "AArch64 NEON",
4263-
"cflags": [],
4263+
"cflags": ["-march=armv8-a+simd"],
42644264
"aux_files": [],
42654265
},
42664266
"SHA3": {

test/abicheck/aarch64/abicheck_aarch64.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ int check_aarch64_aapcs_compliance(struct aarch64_register_state *before,
4545
}
4646
}
4747

48-
/* The call stub leaves vector output untouched when NEON is unavailable. */
48+
/* The call stub leaves vector output untouched when NEON was not compiled
49+
* in or is unavailable at runtime. */
50+
#if defined(MLD_SYS_AARCH64_NEON)
4951
if (mld_sys_check_capability(MLD_SYS_CAP_AARCH64_NEON))
5052
{
5153
/* Check callee-saved NEON registers (d8-d15, lower 64 bits only). */
@@ -58,6 +60,7 @@ int check_aarch64_aapcs_compliance(struct aarch64_register_state *before,
5860
}
5961
}
6062
}
63+
#endif /* MLD_SYS_AARCH64_NEON */
6164

6265
return violations;
6366
}

test/abicheck/aarch64/abicheck_aarch64.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ static MLD_INLINE void call_stub_aarch64(struct aarch64_register_state *input,
3333
struct aarch64_register_state *output,
3434
void (*function_ptr)(void))
3535
{
36+
#if defined(MLD_SYS_AARCH64_NEON)
3637
int use_neon = mld_sys_check_capability(MLD_SYS_CAP_AARCH64_NEON) != 0;
38+
#else
39+
int use_neon = 0;
40+
#endif
3741
asm_call_stub_aarch64(input, output, function_ptr, use_neon);
3842
}
3943

0 commit comments

Comments
 (0)