0
Fork 0
mirror of https://codeberg.org/ziglang/zig synced 2026-11-18 07:05:05 +00:00
zig/lib
Matthew Lugg fa3a9fcdfa
llvm: only load/store ABI-sized integers to/from memory
In theory, for any integer type containing padding bits (e.g. `i3`),
LLVM *does* support storing and loading that type to and from memory,
and the documented semantics for this in the LLVM langref are a valid
implementation of Zig's integer semantics. However, in practice, this
support is frequently buggy, because Clang never emits such operations
and so they are poorly tested in LLVM. The most recent example of such a
bug is https://codeberg.org/ziglang/zig/issues/35560.

In addition, LLVM's semantics here lead to suboptimal codegen, because
loads/stores are only rounded up to the next byte (rather than the next
ABI integer type) and padding bits are unspecified (or, on LLVM master,
padding bits are all zero, even for a negative signed integer).

We can both mitigate LLVM bugs and get better codegen by employing the
strategy used by the self-hosted backends, wherein we zero- or
sign-extend to an integer of bit size `@sizeOf(WeirdInt) * 8`. This type
has the same size in memory but has no padding bits. When loading, we
load the extended type and then use `trunc nuw` or `trunc nsw`
(depending on the signedness) to convert to the correct value type.

The name of the game here is to minimize occurrences of `wip.load` and
`wip.store` in the LLVM backend, with most accesses instead going
through higher-level functions which handle this extension/truncation
for us. Those higher-level functions are `codegen.llvm.FuncGen.load` and
`codegen.llvm.FuncGen.store`. I changed their signatures slightly to
make them consistent and more generally useful, and changed the majority
of calls to `wip.load`/`wip.store` to use them. They are the only
functions which include the integer extension/truncation logic.

The remaining occurrences of `wip.load` and `wip.store` outside of those
two functions are all in ABI handling. These code paths cannot occur for
non-ABI types due to how we lower `CallingConvention.auto`, therefore
they cannot be reached for non-ABI-sized integer types.

This diff was not purely mechanical---I had to make a handful of
non-trivial logic changes and refactors. The biggest logic change is in
lowering a "select" operation to a manual loop: we now use phi nodes to
track the loop iterator and accumulator instead of an `alloca`. (To be
honest, this wasn't really necessary, but I felt dirty migrating the old
logic when this approach is clearly better.) The most significant
refactor is that the logic for extracting function call arguments has
been moved into `FuncGen`: the `codegen.llvm.Object.updateFunc`
implementation will not initialize the three fields which depend on
function arguments, and a new function `FuncGen.genMainBody` will
populate those fields before calling `genBody` on the main AIR body.
This change was made so that I could use member functions on `FuncGen`
in this logic without jumping through hoops.

The reproduction given in https://codeberg.org/ziglang/zig/issues/35560
is apparently quite delicate, so I haven't added a test case because it
realistically would not be very useful. However, I have manually tested
the reproduction, and can confirm that the bug *does* trigger before
this commit (targeting both baseline and native CPU), and does *not*
trigger after this commit (again, for both baseline and native CPU).

Resolves: https://codeberg.org/ziglang/zig/issues/35560
History 2026-06-24 10:20:52 +01:00
..
build-web webui: fix 'Unique Runs' and 'Coverage' percentage 2026-06-06 23:39:13 +02:00
c libzigc: lround lroundf lroundl (#35600) 2026-06-07 23:08:14 +02:00
compiler Adds support for making run step argument paths absolute 2026-06-24 02:41:05 +02:00
compiler_rt compiler-rt: delete workarounds for __int128 ABI on x86_64-windows 2026-06-19 04:25:42 +02:00
docs Replace usages of AutoArrayHashMapUnmanaged with array_hash_map.Auto 2026-06-10 16:14:58 +02:00
include zig cc: update intrinsic headers to LLVM 22 2026-04-25 21:49:39 +02:00
init rename addCliExtras to addPassthruArgs 2026-05-25 18:54:35 -07:00
libc musl: fix read of stack slot containing ctid in x32 clone() 2026-06-19 08:25:49 +02:00
libcxx llvm-libc: update to LLVM 22 2026-04-26 19:26:16 +02:00
libcxxabi libcxxabi: unregress arm-freebsd and arm-openbsd build 2026-04-26 15:42:05 +02:00
libtsan libtsan: backport llvm/llvm-project#194116 2026-04-25 21:54:47 +02:00
libunwind libunwind: unregress x32 and mips n32 build 2026-04-27 10:16:02 +02:00
lldb InternPool: restore debugger pretty printing 2026-06-06 20:00:55 -07:00
std llvm: only load/store ABI-sized integers to/from memory 2026-06-24 10:20:52 +01:00
c.zig feat(libzigc): add common implementations of pthread_spin_* 2026-04-21 09:03:06 +02:00
compiler_rt.zig compiler-rt: delete workarounds for __int128 ABI on x86_64-windows 2026-06-19 04:25:42 +02:00
fuzzer.zig Replace usages of ArrayHashMapUnmanaged with array_hash_map.Custom 2026-06-10 16:14:58 +02:00
ubsan_rt.zig ubsan_rt: fix wrong prototypes for __ubsan_handle_nonnull_return_v1{_abort} 2026-05-29 05:53:38 +02:00
zig.h main: handle more cli args for zig test -ofmt=c 2026-06-20 21:08:23 -04:00