Source linked

Zig Overhauls @bitCast et Fixes LLVM Backend Réduction intégrale

La nouvelle sémantique @bitCast de Zig passe de la réinterprétation de la mémoire à la mise en page de bits logiques, permettant des optimisations de backend LLVM plus robustes.

zigmatthew luggllvm backendbitcastcompilerpacked struct

On June 25, 2026, Zig's devlog dropped a substantial change: @bitCast is no longer a memory reinterpretation. The builtin now operates on the 'logical bits' of a type, and the LLVM backend finally stops misusing LLVM's bit-int types for memory storage.

Why the LLVM Backend Needed Fixing Zig historically lowered arbitrary-width integer types (u4, i13, u40) directly to LLVM IR's bit-int types (i4, i13, i40). Problem: LLVM's documented semantics for those types in memory are unnecessarily restrictive, and because Clang never emits them, the code paths are poorly tested. Matthew Lugg observed 'many instances of trivial optimizations being missed and even straight-up miscompilations.' The fix: use bit-int types only for SSA values, and zero- or sign-extend to ABI-sized types (i8, i16, i32) when storing to memory. That matches how Clang lowers C's _BitInt(N) - a path LLVM actually optimizes.

The New @bitCast Semantics in Detail Changing memory layout broke the old @bitCast, which was defined as pointer-cast-and-load. Lugg opted to implement proposal #19755 from Jacob Young (accepted in 2024), which redefines @bitCast in terms of a type's 'logical bit layout'. Every compatible type has an ordered sequence of bits: u5 has 5 bits (LSB to MSB); u5 has 10 bits (first element's bits, then second's). @bitCast reinterprets those bits directly, without any memory indirection. This definition was already used by the self-hosted x86_64 backend, so the PR extended it to the LLVM and C backends, plus comptime execution. The compiler's Legalize pass now handles complex @bitCast operations, decomposing them into simpler ones that backends can lower reliably. With this land, Zig closes several open issues and eliminates crash-inducing Illegal Behavior in the test suite. The new semantics also make @bitCast between integers and packed structs/unions consistent by construction. Expect fewer optimizer surprises and a clearer contract for anyone using @bitCast in production Zig code.


Source: Zig's New BitCast Semantics and LLVM Back End Improvements
Domain: ziglang.org

Read original source ->

External source stays available while the OJO article and comment thread stay local.

Comments load interactively on the live page.