0
Fork 0
mirror of https://codeberg.org/ziglang/zig synced 2026-11-18 07:05:05 +00:00
zig/lib/std
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 Adds support for making run step argument paths absolute 2026-06-24 02:41:05 +02:00
c std.Io.Dir: support renamePreserve on Darwin systems (#35358) 2026-06-12 23:30:44 +02:00
compress flate: Correct math when calculating number of available bits 2026-06-17 09:15:17 +02:00
crypto std.crypto: argon2/scrypt: cleanup VerifyOptions 2026-06-11 01:36:18 +02:00
debug std.debug.Dwarf.expression: fix a couple of failing tests on x32/n32 2026-06-18 03:59:15 +02:00
dwarf fix compiler ftbfs from std.macho and std.dwarf changes 2025-09-30 13:44:51 +01:00
fmt workaround surfaced backend bugs 2026-05-25 18:38:01 -07:00
fs std.Io.Dir: support renamePreserve on Darwin systems (#35358) 2026-06-12 23:30:44 +02:00
hash add @SpirvType builtin 2026-06-07 04:58:53 +02:00
heap BrkAllocator: Fix big_size_class_count holding the max index rather than count 2026-06-03 02:24:15 +02:00
http all: update to use new std.lang.Type definitions 2026-05-27 10:03:51 +01:00
Io Writer.writeBytePreserve: preserve only 'preserve -| 1' already buffered bytes 2026-06-18 02:31:42 +02:00
json Replace usages of StringArrayHashMapUnmanaged with array_hash_map.String 2026-06-10 16:14:53 +02:00
lang std: add basic target information for m88k-openbsd 2026-05-23 11:02:55 +02:00
math libzigc: log1p, log1pf 2026-05-31 09:21:02 +02:00
mem all: update to use new std.lang.Type definitions 2026-05-27 10:03:51 +01:00
meta all: update to use new std.lang.Type definitions 2026-05-27 10:03:51 +01:00
os Revert "std.os.linux: fix kernel_timespec.nsec to be isize" 2026-06-20 11:12:45 +02:00
posix all: update to use new std.lang.Type definitions 2026-05-27 10:03:51 +01:00
process Replace usages of ArrayHashMapUnmanaged with array_hash_map.Custom 2026-06-10 16:14:58 +02:00
Random remove uses of array multiplication 2026-04-30 08:57:51 +01:00
sort std.sort.pdq: fix stack overflow on 32-bit targets 2026-06-11 05:40:50 +02:00
tar remove uses of array multiplication 2026-04-30 08:57:51 +01:00
Target spirv: drive capability and extension emission from target features 2026-06-20 09:38:20 +02:00
testing all: update to use new std.lang.Type definitions 2026-05-27 10:03:51 +01:00
time removed reduntant @as() from switch in getDaysInMonth 2026-02-05 21:58:35 +01:00
tz Support legacy TZ format, expose header struct to a potential writer 2022-01-01 12:47:08 +00:00
unicode remove uses of array multiplication 2026-04-30 08:57:51 +01:00
valgrind Remove numerous things deprecated during the 0.14 release cycle 2025-07-11 08:17:43 +02:00
zig llvm: only load/store ABI-sized integers to/from memory 2026-06-24 10:20:52 +01:00
zip/testdata Add zip.extractTo test cases (#35748) 2026-06-17 23:22:55 +02:00
zon std.zon.parse: fix large int parse 2026-06-07 06:11:08 +02:00
array_hash_map.zig std: Update incorrect docs about ArrayHashMap being managed 2026-05-29 20:41:11 +02:00
array_list.zig std.array_list: add last method, deprecated getLast 2026-05-25 18:54:36 -07:00
ascii.zig std.ascii: Remove indexOfIgnoreCase*() in favor of findIgnoreCase*() 2026-05-08 21:55:08 +01:00
atomic.zig std.atomic: add m88k to cacheLineForCpu() 2026-05-23 11:02:56 +02:00
base64.zig remove uses of array multiplication 2026-04-30 08:57:51 +01:00
bit_set.zig re-enable some tests on RISC-V that no longer fail 2026-05-21 20:37:11 +02:00
BitStack.zig std.ArrayList: make unmanaged the default 2025-08-11 15:52:49 -07:00
buf_map.zig lib: correct unnecessary uses of 'var' 2023-11-19 09:55:07 +00:00
buf_set.zig Change many test blocks to doctests/decltests 2024-02-26 15:18:31 -08:00
Build.zig std.Build.Step.Fmt: dupe lazy path lists 2026-06-11 16:48:13 +02:00
c.zig std.Io.Dir: support renamePreserve on Darwin systems (#35358) 2026-06-12 23:30:44 +02:00
coff.zig all: update to use new std.lang.Type definitions 2026-05-27 10:03:51 +01:00
compress.zig std.compress: rework flate to new I/O API 2025-07-31 22:10:11 -07:00
crypto.zig remove uses of array multiplication 2026-04-30 08:57:51 +01:00
debug.zig std.Target: add PSX 2026-06-19 04:22:55 +02:00
deque.zig std:ArrayList: Merge getLastOrNull into getLast (#32008) 2026-05-02 02:26:15 +02:00
DoublyLinkedList.zig std.DoublyLinkedList: rename pop to popLast (#35692) 2026-06-11 05:43:57 +02:00
dwarf.zig Dwarf: cleanup emitted debug info 2024-08-22 08:44:08 +02:00
dynamic_library.zig DynLib: fix elf loading 2026-06-16 22:48:18 +02:00
elf.zig all: update to use new std.lang.Type definitions 2026-05-27 10:03:51 +01:00
enums.zig all: update to use new std.lang.Type definitions 2026-05-27 10:03:51 +01:00
fmt.zig Merge pull request 'std.meta: Remove Int/Tuple in favor of @Int/@Tuple' (#35188) from linus/zig:deprecated-std-meta into master 2026-05-06 19:35:40 +02:00
fs.zig std: find a better home for the "preopens" concept 2026-01-08 05:06:31 +01:00
hash.zig all: replace all @Type usages 2025-11-22 22:42:38 +00:00
hash_map.zig lldb: update for upcoming hash map rename 2026-05-26 19:32:01 +02:00
heap.zig add SafeAllocator 2026-05-25 18:32:36 -07:00
http.zig remove uses of array multiplication 2026-04-30 08:57:51 +01:00
Io.zig all: update to use new std.lang.Type definitions 2026-05-27 10:03:51 +01:00
json.zig rename std.Io.GenericReader to std.Io.Reader in outdated doc comment 2026-05-17 19:48:12 +03:00
lang.zig @extern: add flat decoration 2026-06-18 13:38:58 +02:00
leb128.zig std.meta: Remove Int in favor of @Int 2026-05-03 21:42:06 +01:00
log.zig std.Threaded: replace console kernel32 functions with ntdll 2026-02-05 07:41:25 -05:00
macho.zig std: update for language changes 2026-03-10 10:38:50 +00:00
math.zig disable failing tests revealed by this branch 2026-05-29 15:32:27 -07:00
mem.zig add @SpirvType builtin 2026-06-07 04:58:53 +02:00
meta.zig std.meta: remove stringToEnum slow-path 2026-06-06 20:41:09 -07:00
multi_array_list.zig all: update to use new std.lang.Type definitions 2026-05-27 10:03:51 +01:00
os.zig std.os: no longer require libc for sh(eb)-linux 2026-06-04 17:05:00 +02:00
pdb.zig Removes dead code, updates some tests, fixes typos in comments, formats 2026-04-12 04:01:30 -07:00
pie.zig std.Target: add abin32 and x32 Abi tags 2026-06-18 02:37:52 +02:00
posix.zig std.elf: add Ident struct 2026-05-17 18:08:57 +01:00
priority_dequeue.zig priority queue and dequeue: use *const Self in read-only methods and fix deinit (#31712) 2026-04-03 22:20:46 +02:00
priority_queue.zig priority queue and dequeue: use *const Self in read-only methods and fix deinit (#31712) 2026-04-03 22:20:46 +02:00
process.zig Build: fix tryFindProgram on Windows and reintroduce ref in test block 2026-06-07 23:27:01 -07:00
Progress.zig all: update to use new std.lang.Type definitions 2026-05-27 10:03:51 +01:00
Random.zig std.meta: Remove Int in favor of @Int 2026-05-03 21:42:06 +01:00
SemanticVersion.zig Prefer <err> => |e| return e over <err> => return <err> 2026-04-20 18:03:14 -07:00
simd.zig std: re-enable a bunch of tests that now pass 2026-04-25 21:54:48 +02:00
SinglyLinkedList.zig SinglyLinkedList.remove docs: Assumes -> asserts 2025-10-25 21:28:54 -07:00
sort.zig std.sort: clarify description of equalRange 2026-04-16 19:42:00 +02:00
spirv.zig std.spirv: add Scope, MemorySemantics, and barrier primitives 2026-06-18 13:38:58 +02:00
start.zig std.Target: add PSX 2026-06-19 04:22:55 +02:00
static_string_map.zig StaticStringMap: reinstate pub getIndex with clarifying docs and test 2026-06-06 23:37:09 +02:00
std.zig std: rename gpu.zig to spirv.zig 2026-06-14 09:11:59 +02:00
tar.zig remove uses of array multiplication 2026-04-30 08:57:51 +01:00
Target.zig std.Target: add PSX 2026-06-19 04:22:55 +02:00
testing.zig add @SpirvType builtin 2026-06-07 04:58:53 +02:00
Thread.zig std.Target: add abin32 and x32 Abi tags 2026-06-18 02:37:52 +02:00
time.zig std: finish moving time to Io interface 2026-02-02 23:02:31 -08:00
treap.zig std.ArrayList: make unmanaged the default 2025-08-11 15:52:49 -07:00
tz.zig std.tz: fix redundant endian handling 2025-08-28 18:30:57 -07:00
unicode.zig remove uses of array multiplication 2026-04-30 08:57:51 +01:00
Uri.zig std.Uri: reject invalid URI schemes according to RFC3986 2026-03-23 05:22:46 +01:00
valgrind.zig remove condition codes 2025-07-16 10:27:39 -07:00
wasm.zig wasm linker: implement @tagName for sparse enums 2025-01-15 15:11:36 -08:00
zig.zig rename an internal environment variable 2026-06-22 16:53:16 -07:00
zip.zig Add zip.extractTo test cases (#35748) 2026-06-17 23:22:55 +02:00
zon.zig zon: Add anonymous struct literal in the example 2025-08-15 23:35:16 +02:00