mirror of
https://codeberg.org/ziglang/zig
synced 2026-11-18 07:05:05 +00:00
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
|
|
|---|---|---|
| .. | ||
| Build | ||
| c | ||
| compress | ||
| crypto | ||
| debug | ||
| dwarf | ||
| fmt | ||
| fs | ||
| hash | ||
| heap | ||
| http | ||
| Io | ||
| json | ||
| lang | ||
| math | ||
| mem | ||
| meta | ||
| os | ||
| posix | ||
| process | ||
| Random | ||
| sort | ||
| tar | ||
| Target | ||
| testing | ||
| time | ||
| tz | ||
| unicode | ||
| valgrind | ||
| zig | ||
| zip/testdata | ||
| zon | ||
| array_hash_map.zig | ||
| array_list.zig | ||
| ascii.zig | ||
| atomic.zig | ||
| base64.zig | ||
| bit_set.zig | ||
| BitStack.zig | ||
| buf_map.zig | ||
| buf_set.zig | ||
| Build.zig | ||
| c.zig | ||
| coff.zig | ||
| compress.zig | ||
| crypto.zig | ||
| debug.zig | ||
| deque.zig | ||
| DoublyLinkedList.zig | ||
| dwarf.zig | ||
| dynamic_library.zig | ||
| elf.zig | ||
| enums.zig | ||
| fmt.zig | ||
| fs.zig | ||
| hash.zig | ||
| hash_map.zig | ||
| heap.zig | ||
| http.zig | ||
| Io.zig | ||
| json.zig | ||
| lang.zig | ||
| leb128.zig | ||
| log.zig | ||
| macho.zig | ||
| math.zig | ||
| mem.zig | ||
| meta.zig | ||
| multi_array_list.zig | ||
| os.zig | ||
| pdb.zig | ||
| pie.zig | ||
| posix.zig | ||
| priority_dequeue.zig | ||
| priority_queue.zig | ||
| process.zig | ||
| Progress.zig | ||
| Random.zig | ||
| SemanticVersion.zig | ||
| simd.zig | ||
| SinglyLinkedList.zig | ||
| sort.zig | ||
| spirv.zig | ||
| start.zig | ||
| static_string_map.zig | ||
| std.zig | ||
| tar.zig | ||
| Target.zig | ||
| testing.zig | ||
| Thread.zig | ||
| time.zig | ||
| treap.zig | ||
| tz.zig | ||
| unicode.zig | ||
| Uri.zig | ||
| valgrind.zig | ||
| wasm.zig | ||
| zig.zig | ||
| zip.zig | ||
| zon.zig | ||