Skip to content

C/C++ front-ends: declare GCC's AArch64 builtins - #9581

Draft
tautschnig wants to merge 10 commits into
diffblue:developfrom
tautschnig:aarch64-gcc-builtins
Draft

tautschnig wants to merge 10 commits into
diffblue:developfrom
tautschnig:aarch64-gcc-builtins

Conversation

@tautschnig

Copy link
Copy Markdown
Collaborator

Depends-on: #9039

GCC's <arm_neon.h>, <arm_acle.h>, <arm_fp16.h> and <arm_bf16.h> implement
most intrinsics by calling builtin_aarch64* builtins, and as of GCC 13
GCC registers some intrinsics (e.g., vreinterpret
*, and the FP8 ones of
GCC 15) itself instead of defining them in the header. CBMC type-checks
the bodies of all functions these headers define, and gave each
undeclared builtin the type int(void), so every program including
<arm_neon.h> failed with conversion errors.

GCC builds the types of these builtins from mode and qualifier tables, so
there is no list to parse. The new gcc_aarch64_builtins.py instead asks
GCC: it calls each candidate with arguments of a struct type, for which
GCC reports the expected parameter types, and assigns a well-typed call to
a struct, for which GCC reports the return type. Candidates are the
_builtin_aarch64* names in the headers (before and after preprocessing)
and those strings of cc1 that GCC treats as builtins when redeclared. Run
with GCC 11 to 16, this yields 3611 builtins.

GCC 12 and later declare the NEON tuple types (int8x8x2_t etc.) and the
builtins taking or returning them when processing
'#pragma GCC aarch64 "arm_neon.h"', and __arm_data512_t at
'#pragma GCC aarch64 "arm_acle.h"'. The scanner now injects these
declarations (gcc_builtin_headers_aarch64_neon.h) at the pragma, for
--arch arm64, without changing source locations. All other builtins are
declared on demand from gcc_builtin_headers_aarch64.h. GCC 11 defines the
tuple types in <arm_neon.h> and passes tuples to the same builtins as
opaque values; as such a builtin is only looked up on demand when no
pragma declared it, gcc_builtin_headers_aarch64.h keeps its GCC 11
signature. Types are spelt so that the declarations parse whether or not
the __fp16 and __bf16 keywords are enabled.

With this, a program using intrinsics from <arm_neon.h> and <arm_acle.h>
of each of GCC 11 to 16 type-checks and verifies in C (GCC 15 and 16
default to C23, which needs --c23). The builtins have no bodies, so their
results are nondeterministic.

Co-authored-by: Kiro kiro-agent@users.noreply.github.com

  • Each commit message has a non-empty body, explaining why the change was made.
  • Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • n/a The feature or user visible behaviour I have added or modified has been documented in the User Guide in doc/cprover-manual/
  • Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • n/a My commit message includes data points confirming performance improvements (if claimed).
  • My PR is restricted to a single feature or bugfix.
  • n/a White-space or formatting changes outside the feature-related changed lines are in commits of their own.

tautschnig and others added 7 commits November 27, 2026 10:55
The ia32 GCC builtin declarations were split across nine headers
(gcc_builtin_headers_ia32.h plus -2..-9) solely to keep each embedded string
literal under the 65536-character limit. Now that file_converter emits a
character-array initialiser with no such limit, fold them back into a single
gcc_builtin_headers_ia32.h and drop the now-redundant per-file symbols,
find_pattern lookups, and build rules.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
The --inc mode only understood the ..._BUILTIN_INFOS form that recent
clang-tblgen versions emit. LLVM 20 and earlier emit
BUILTIN(name, "types", "attrs") and
TARGET_BUILTIN(name, "types", "attrs", "features") lines instead, and LLVM
packages ship these pre-generated (e.g.,
/usr/lib/llvm-20/include/clang/Basic/arm_neon.inc and arm_fp16.inc). Parse
that form as well, so that the NEON declarations can be checked against an
installed LLVM without building clang-tblgen. Those lines spell the full
name, so PREFIX is only prepended to names that lack it.

Also decode Clang's 'h' type code as __fp16: in Clang's builtin type
encoding 'h' is "half" (__fp16), whereas _Float16 is 'x'. Clang's
<arm_neon.h> and <arm_fp16.h> pass float16_t, which is a typedef of __fp16,
and CBMC accepts __fp16 on ARM even where _Float16 is unavailable (GCC
before version 12).

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
Clang's <arm_neon.h> and <arm_fp16.h> implement the NEON intrinsics as
static inline functions that call __builtin_neon_* builtins. CBMC
type-checks those function bodies whether or not they are used, and gives
an undeclared builtin the type int(void). A program including Clang's
<arm_neon.h> thus either failed with conversion errors or computed with
wrongly typed values.

Add clang_builtin_headers_neon.h with 1190 declarations and wire it into
the front-end like the other per-target builtin headers. The NEON builtins
are polymorphic: <arm_neon.h> casts operands to a representative vector
type (__gcc_v8qi or __gcc_v16qi for 64-bit or 128-bit vectors) and passes a
NeonTypeFlags code, so each has a single concrete signature. The
declarations were generated with clang_builtins.py: those of a recent LLVM
main from clang-tblgen -gen-arm-neon-sema, completed with the pre-generated
arm_neon.inc and arm_fp16.inc of LLVM 18 and 20. Where versions differ,
the newer signature is used (LLVM 18's vqshrun*_n and vqrshrun*_n scalar
builtins return signed types).

Several of these builtins take vectors for which no typedef existed:
unsigned elements, one lane, or four half-precision lanes. Add
__gcc_v8uqi, __gcc_v16uqi, __gcc_v4uhi, __gcc_v8uhi, __gcc_v16uhi,
__gcc_v2usi, __gcc_v1udi, __gcc_v1df and __gcc_v4hf to
gcc_builtin_headers_types.h and to clang_builtins.py's vector_map.
__gcc_v8uhi and __gcc_v16uhi were already named in vector_map (and in
unreachable duplicates in gcc_builtin_headers_ia32.h), but never declared:
a declaration naming an unknown type parses as an old-style parameter list,
which gave __builtin_neon_vaddvq_u16 and others an int parameter. Vectors of
__bf16 use the half-precision typedefs, as CBMC models __bf16 (like __fp16)
as _Float16. Builtins of the FP8 extension remain undeclared as CBMC has no
__mfp8 type.

Look up the builtins for arm64 and for 32-bit ARM, as Clang's 32-bit
<arm_neon.h> uses the same builtins. arm64 no longer looks up the
32-bit-only iWMMXt builtins of gcc_builtin_headers_arm.h. The header is
named after Clang because GCC's <arm_neon.h> uses different builtins.

Together with neon_vector_type support (diffblue#9038) and support for
neon_polyvector_type, LLVM 18's <arm_neon.h> type-checks in full for both
--arch arm64 and --arch arm.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
GCC's vector extension permits a scalar operand in a comparison, as in
arithmetic: the scalar is converted to the vector type, i.e., compared with
each lane. GCC's <arm_neon.h> relies on this (e.g., vtst_p8 computes
(uint8x8_t)(__a & __b) != 0), so every program including it failed with
"vector operator 'notequal' not defined for types 'uint8x8_t' and
'signed int'". Convert the scalar operand to the vector type, as
typecheck_expr_binary_arithmetic does.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
GCC supports _Float16 and __bf16 on AArch64 long before it does on x86:
aarch64-linux-gnu-gcc-8 accepts _Float16 and aarch64-linux-gnu-gcc-10
accepts __bf16 (gcc-8 does not). GCC's <arm_bf16.h>, which <arm_neon.h>
includes, declares "typedef __bf16 bfloat16_t;", so with GCC 10 to 12 as
the system compiler (e.g., GCC 11 on Ubuntu 22.04) CBMC failed to parse
any program including <arm_neon.h> for --arch arm64. Enable both keywords
for these GCC versions on arm64.

A regression test would need GCC 10 to 12 as the system compiler. Checked
by putting a gcc wrapper that reports version 11 (resp. 9) first on PATH:
cbmc --arch arm64 then parses GCC 11's preprocessed <arm_neon.h> (resp.
reports a syntax error at "typedef __bf16 bfloat16_t;" in arm_bf16.h).

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
GCC's <arm_neon.h> (and, as of GCC 16, arm_private_neon_types.h and
arm_private_fp8.h) give user-visible names to vector and scalar types that
GCC itself provides on AArch64, e.g., "typedef __Int8x8_t int8x8_t;" and
"typedef __mfp8 mfloat8_t;", and convert pointers to GCC's internal
__builtin_aarch64_simd_* types. CBMC knew none of these, so no program
including <arm_neon.h> could be parsed, in C or C++.

Add gcc_builtin_headers_aarch64_types.h, following GCC's
aarch64-simd-builtin-types.def and aarch64-builtins.cc, and include it in
the built-in declarations of both front-ends for --arch arm64. CBMC models
__fp16 and __bf16 as _Float16 already; the vector types use
__CPROVER_Float16 so that they do not depend on the _Float16 and __bf16
keywords being enabled, and are spelt with the vector_size attribute
first: the C++ front-end takes "typedef char v __attribute__((vector_size
(8)));" for a vector of two ints. __mfp8, an opaque 8-bit floating-point
type, is modelled as an unsigned 8-bit integer. GCC 11 and earlier pass
tuples of vectors to the structure load and store builtins as opaque
integers of 256, 384 and 512 bits, which are vectors here.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
GCC's <arm_neon.h>, <arm_acle.h>, <arm_fp16.h> and <arm_bf16.h> implement
most intrinsics by calling __builtin_aarch64_* builtins, and as of GCC 13
GCC registers some intrinsics (e.g., vreinterpret_*, and the FP8 ones of
GCC 15) itself instead of defining them in the header. CBMC type-checks
the bodies of all functions these headers define, and gave each
undeclared builtin the type int(void), so every program including
<arm_neon.h> failed with conversion errors.

GCC builds the types of these builtins from mode and qualifier tables, so
there is no list to parse. The new gcc_aarch64_builtins.py instead asks
GCC: it calls each candidate with arguments of a struct type, for which
GCC reports the expected parameter types, and assigns a well-typed call to
a struct, for which GCC reports the return type. Candidates are the
__builtin_aarch64_* names in the headers (before and after preprocessing)
and those strings of cc1 that GCC treats as builtins when redeclared. Run
with GCC 11 to 16, this yields 3611 builtins.

GCC 12 and later declare the NEON tuple types (int8x8x2_t etc.) and the
builtins taking or returning them when processing
'#pragma GCC aarch64 "arm_neon.h"', and __arm_data512_t at
'#pragma GCC aarch64 "arm_acle.h"'. The scanner now injects these
declarations (gcc_builtin_headers_aarch64_neon.h) at the pragma, for
--arch arm64, without changing source locations. All other builtins are
declared on demand from gcc_builtin_headers_aarch64.h. GCC 11 defines the
tuple types in <arm_neon.h> and passes tuples to the same builtins as
opaque values; as such a builtin is only looked up on demand when no
pragma declared it, gcc_builtin_headers_aarch64.h keeps its GCC 11
signature. Types are spelt so that the declarations parse whether or not
the __fp16 and __bf16 keywords are enabled.

With this, a program using intrinsics from <arm_neon.h> and <arm_acle.h>
of each of GCC 11 to 16 type-checks and verifies in C (GCC 15 and 16
default to C23, which needs --c23). The builtins have no bodies, so their
results are nondeterministic.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
tautschnig and others added 3 commits December 1, 2026 10:53
With GCC's arm_neon.h, intrinsics that only rearrange the bits of vectors
have no body in goto-cc, so their results were nondeterministic:
vcombine_* calls __builtin_aarch64_combine*, GCC 12 to 14 implement
vget_low_*/vget_high_* with __builtin_aarch64_get_low*/get_high* (and
__builtin_aarch64_vget_lo_half*/vget_hi_half* for bfloat16), and GCC 13
and later register vreinterpret* (GCC 15 and later also vget_low_*,
vget_high_* and vcombine_mf8) themselves when processing
`#pragma GCC aarch64 "arm_neon.h"`. builtin_factory declares all of these
from gcc_builtin_headers_aarch64.h, without a body. Programs use these
intrinsics to convert between vector types and widths, and arm_neon.h
itself uses vcombine_* in the table lookup intrinsics (vtbl*).

In GCC mode on arm64, type checking now replaces a call of such a function
that builtin_factory declared and that has no body by its value, as the
Arm C Language Extensions specify: vreinterpret* is a cast between types
of the same size that keeps the bits (poly128_t is the only operand or
result type that is not a vector), vget_low*/get_low*/vget_lo_half* and
vget_high*/get_high*/vget_hi_half* extract the lower or upper half of the
bytes (lanes are in memory order), and vcombine*/combine* build the vector
from the lanes of their two arguments, where an argument may be a single
lane (as for vcombine_s64 and vcombine_f64). Functions of the program with
such names keep their meaning, also when defined after the call.

builtins.i calls the built-ins and intrinsics as the headers of GCC 12 to
16 do, independent of the host. main.c uses <arm_neon.h> on arm64 hosts
with GCC (checked with GCC 11 to 16, with the SAT and both SMT back ends)
and is trivially successful elsewhere. It checks vreinterpret* only with
GCC 13 and later: GCC 12 and earlier define vreinterpret* as casts between
vector types, which the SMT back ends do not support when the numbers of
lanes differ.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
GCC's arm_neon.h checks the lane argument of the lane intrinsics
(vget_lane_*, vgetq_lane_*, vset_lane_*, vld1_lane_*, vdup_lane_*, the
*_lane_* arithmetic, ...) with

  #define __AARCH64_LANE_CHECK(__vec, __idx) \
    __builtin_aarch64_im_lane_boundsi(sizeof(__vec), sizeof(__vec[0]), __idx)

builtin_factory declares the built-in, but it had no body, so every
call of such an intrinsic added a "no body for callee
__builtin_aarch64_im_lane_boundsi" property that failed, and its
expectations on the lane were lost.

GCC evaluates the check when expanding the call (aarch64-builtins.cc,
AARCH64_SIMD_BUILTIN_LANE_CHECK): it is a compile-time error unless the
lane is a constant in [0, total size / element size) ("lane %d out of
range %d - %d", "lane index must be a constant immediate"). The lane
only becomes a constant once the always_inline intrinsic has been
inlined into its caller, so CBMC's type checker cannot evaluate it:
in the intrinsic's body the lane is a parameter. The model therefore
states the range as a precondition at the call (with a nonzero element
size, which GCC requires as well). Programs that GCC accepts satisfy it,
and symex's constant propagation decides it for constant lanes; a
program that fails it does not compile with GCC. Whether the lane is a
constant is not checked.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
CI fails on four of the new tests in configurations that the tests do
not target:

- gcc_aarch64_builtins/test.desc and gcc11.desc with --cprover-smt2:
  the NEON tuple types (int8x8x2_t and others) are structs with an
  array of vectors. The SMT2 back-end flattens such a struct with
  flatten_array, which concatenated SMT array terms for an array of
  vectors, and CPROVER's SMT2 solver failed on that ("to_bitvector_type"
  in its parser; once the formula is well-typed, its array theory fails
  on let-bound variables in array values). Both are back-end and solver
  bugs that also occur on develop with a struct that has a
  two-dimensional array member; they are fixed separately, and the tag
  broken-cprover-smt-backend can be removed once those fixes are
  merged. Z3 and CVC5 pass (structs are datatypes there).
- gcc_aarch64_builtins/test.desc with the incremental SMT back end,
  which does not support arrays in structs ("Extract can only be
  applied to bit vector terms"), as for other tests tagged no-new-smt.
- clang_builtins/neon.desc and neon-arm.desc with Visual Studio:
  neon.c declares its vector types with __attribute__((vector_size)),
  which the Visual Studio front end does not parse; tag them gcc-only.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
@codecov

codecov Bot commented Dec 1, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.74797% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.37%. Comparing base (3e1f386) to head (8b5f8b8).
⚠️ Report is 36 commits behind head on develop.

Files with missing lines Patch % Lines
src/ansi-c/c_typecheck_expr.cpp 96.05% 3 Missing ⚠️
src/ansi-c/scanner.l 96.77% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #9581      +/-   ##
===========================================
+ Coverage    81.29%   81.37%   +0.07%     
===========================================
  Files         1725     1727       +2     
  Lines       191680   191830     +150     
  Branches        73       76       +3     
===========================================
+ Hits        155835   156110     +275     
+ Misses       35845    35720     -125     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant