Skip to content

Commit 7cd5fbe

Browse files
Do not sign return addresses in functions built for ARMv8.3 and later
A target of ARMv8.3 or later has `FEAT_PAuth`, so under `-mbranch-protection=pac-ret` the compiler folds `autiasp; ret` into `retaa`. Unlike `paciasp` and `autiasp`, `retaa` is not in the `NOP` space and raises SIGILL on cores without `FEAT_PAuth` (Graviton2, Ampere Altra). One of these functions is the CPU probe `_simsimd_capabilities_arm` (`arch=armv8.5-a+sve`), which runs on every core; the others are the bf16 kernels (`arch=armv8.6-a+...`). Add `branch-protection=none` to these targets only. The code for ARMv8.2 and older keeps the signing: a callee whose branch protection differs from the caller's is not inlined, and with ThinLTO that broke `vectorize(enable)` loops calling `simsimd_bf16_to_f32`. See ClickHouse/ClickHouse#131748
1 parent e81e1c2 commit 7cd5fbe

6 files changed

Lines changed: 17 additions & 12 deletions

File tree

‎include/simsimd/curved.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,8 @@ SIMSIMD_PUBLIC void simsimd_bilinear_f16c_neon(simsimd_f16c_t const *a, simsimd_
501501

502502
#if SIMSIMD_TARGET_NEON_BF16
503503
#pragma GCC push_options
504-
#pragma GCC target("arch=armv8.6-a+simd+bf16")
505-
#pragma clang attribute push(__attribute__((target("arch=armv8.6-a+simd+bf16"))), apply_to = function)
504+
#pragma GCC target("arch=armv8.6-a+simd+bf16,branch-protection=none")
505+
#pragma clang attribute push(__attribute__((target("arch=armv8.6-a+simd+bf16,branch-protection=none"))), apply_to = function)
506506

507507
SIMSIMD_PUBLIC void simsimd_bilinear_bf16_neon(simsimd_bf16_t const *a, simsimd_bf16_t const *b,
508508
simsimd_bf16_t const *c, simsimd_size_t n, simsimd_distance_t *result) {

‎include/simsimd/dot.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,8 @@ SIMSIMD_PUBLIC void simsimd_vdot_f16c_neon(simsimd_f16c_t const *a_pairs, simsim
525525

526526
#if SIMSIMD_TARGET_NEON_BF16
527527
#pragma GCC push_options
528-
#pragma GCC target("arch=armv8.6-a+simd+bf16")
529-
#pragma clang attribute push(__attribute__((target("arch=armv8.6-a+simd+bf16"))), apply_to = function)
528+
#pragma GCC target("arch=armv8.6-a+simd+bf16,branch-protection=none")
529+
#pragma clang attribute push(__attribute__((target("arch=armv8.6-a+simd+bf16,branch-protection=none"))), apply_to = function)
530530

531531
SIMSIMD_INTERNAL bfloat16x8_t _simsimd_partial_load_bf16x8_neon(simsimd_bf16_t const *x, simsimd_size_t n) {
532532
union {

‎include/simsimd/elementwise.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,8 +2053,8 @@ SIMSIMD_PUBLIC void simsimd_fma_f32_neon( //
20532053

20542054
#if SIMSIMD_TARGET_NEON_BF16
20552055
#pragma GCC push_options
2056-
#pragma GCC target("arch=armv8.6-a+simd+bf16")
2057-
#pragma clang attribute push(__attribute__((target("arch=armv8.6-a+simd+bf16"))), apply_to = function)
2056+
#pragma GCC target("arch=armv8.6-a+simd+bf16,branch-protection=none")
2057+
#pragma clang attribute push(__attribute__((target("arch=armv8.6-a+simd+bf16,branch-protection=none"))), apply_to = function)
20582058

20592059
SIMSIMD_PUBLIC void simsimd_sum_bf16_neon(simsimd_bf16_t const *a, simsimd_bf16_t const *b, simsimd_size_t n,
20602060
simsimd_bf16_t *result) {

‎include/simsimd/simsimd.h‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,15 @@ SIMSIMD_PUBLIC simsimd_capability_t _simsimd_capabilities_x86(void) {
470470
/* Compiling the next section one may get: selected processor does not support system register name 'id_aa64zfr0_el1'.
471471
* Suppressing assembler errors is very complicated, so when dealing with older ARM CPUs it's simpler to compile this
472472
* function targeting newer ones.
473+
*
474+
* `branch-protection=none` here and on every other target of ARMv8.3 or later: such a target has `FEAT_PAuth`, so
475+
* under `-mbranch-protection=pac-ret` the compiler folds `autiasp; ret` into `retaa`, which, unlike `autiasp`, is not
476+
* a `NOP` on older cores and raises SIGILL there - in this probe, before anything is dispatched. Code for ARMv8.2 and
477+
* older keeps the signing, and its attributes must match the callers' for (Thin)LTO to inline it.
473478
*/
474479
#pragma GCC push_options
475-
#pragma GCC target("arch=armv8.5-a+sve")
476-
#pragma clang attribute push(__attribute__((target("arch=armv8.5-a+sve"))), apply_to = function)
480+
#pragma GCC target("arch=armv8.5-a+sve,branch-protection=none")
481+
#pragma clang attribute push(__attribute__((target("arch=armv8.5-a+sve,branch-protection=none"))), apply_to = function)
477482

478483
#if _SIMSIMD_HAS_POSIX_EXTENSIONS
479484
/** @brief SIGILL handler for `mrs` instruction testing on Linux ARM */

‎include/simsimd/sparse.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,8 +1292,8 @@ SIMSIMD_PUBLIC void simsimd_spdot_counts_u16_sve2( //
12921292

12931293
#if SIMSIMD_TARGET_SVE2 && SIMSIMD_TARGET_SVE_BF16
12941294
#pragma GCC push_options
1295-
#pragma GCC target("arch=armv8.6-a+sve+sve2+bf16")
1296-
#pragma clang attribute push(__attribute__((target("arch=armv8.6-a+sve+sve2+bf16"))), apply_to = function)
1295+
#pragma GCC target("arch=armv8.6-a+sve+sve2+bf16,branch-protection=none")
1296+
#pragma clang attribute push(__attribute__((target("arch=armv8.6-a+sve+sve2+bf16,branch-protection=none"))), apply_to = function)
12971297

12981298
SIMSIMD_PUBLIC void simsimd_spdot_weights_u16_sve2( //
12991299
simsimd_u16_t const *a, simsimd_u16_t const *b, //

‎include/simsimd/spatial.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,8 @@ SIMSIMD_PUBLIC void simsimd_cos_f16_neon(simsimd_f16_t const *a, simsimd_f16_t c
469469

470470
#if SIMSIMD_TARGET_NEON_BF16
471471
#pragma GCC push_options
472-
#pragma GCC target("arch=armv8.6-a+simd+bf16")
473-
#pragma clang attribute push(__attribute__((target("arch=armv8.6-a+simd+bf16"))), apply_to = function)
472+
#pragma GCC target("arch=armv8.6-a+simd+bf16,branch-protection=none")
473+
#pragma clang attribute push(__attribute__((target("arch=armv8.6-a+simd+bf16,branch-protection=none"))), apply_to = function)
474474

475475
SIMSIMD_PUBLIC void simsimd_cos_bf16_neon(simsimd_bf16_t const *a, simsimd_bf16_t const *b, simsimd_size_t n,
476476
simsimd_distance_t *result) {

0 commit comments

Comments
 (0)