zig/lib/std
mlugg d0e74ffe52
compiler: rework comptime pointer representation and access
We've got a big one here! This commit reworks how we represent pointers
in the InternPool, and rewrites the logic for loading and storing from
them at comptime.

Firstly, the pointer representation. Previously, pointers were
represented in a highly structured manner: pointers to fields, array
elements, etc, were explicitly represented. This works well for simple
cases, but is quite difficult to handle in the cases of unusual
reinterpretations, pointer casts, offsets, etc. Therefore, pointers are
now represented in a more "flat" manner. For types without well-defined
layouts -- such as comptime-only types, automatic-layout aggregates, and
so on -- we still use this "hierarchical" structure. However, for types
with well-defined layouts, we use a byte offset associated with the
pointer. This allows the comptime pointer access logic to deal with
reinterpreted pointers far more gracefully, because the "base address"
of a pointer -- for instance a `field` -- is a single value which
pointer accesses cannot exceed since the parent has undefined layout.
This strategy is also more useful to most backends -- see the updated
logic in `codegen.zig` and `codegen/llvm.zig`. For backends which do
prefer a chain of field and elements accesses for lowering pointer
values, such as SPIR-V, there is a helpful function in `Value` which
creates a strategy to derive a pointer value using ideally only field
and element accesses. This is actually more correct than the previous
logic, since it correctly handles pointer casts which, after the dust
has settled, end up referring exactly to an aggregate field or array
element.

In terms of the pointer access code, it has been rewritten from the
ground up. The old logic had become rather a mess of special cases being
added whenever bugs were hit, and was still riddled with bugs. The new
logic was written to handle the "difficult" cases correctly, the most
notable of which is restructuring of a comptime-only array (for
instance, converting a `[3][2]comptime_int` to a `[2][3]comptime_int`.
Currently, the logic for loading and storing work somewhat differently,
but a future change will likely improve the loading logic to bring it
more in line with the store strategy. As far as I can tell, the rewrite
has fixed all bugs exposed by #19414.

As a part of this, the comptime bitcast logic has also been rewritten.
Previously, bitcasts simply worked by serializing the entire value into
an in-memory buffer, then deserializing it. This strategy has two key
weaknesses: pointers, and undefined values. Representations of these
values at comptime cannot be easily serialized/deserialized whilst
preserving data, which means many bitcasts would become runtime-known if
pointers were involved, or would turn `undefined` values into `0xAA`.
The new logic works by "flattening" the datastructure to be cast into a
sequence of bit-packed atomic values, and then "unflattening" it; using
serialization when necessary, but with special handling for `undefined`
values and for pointers which align in virtual memory. The resulting
code is definitely slower -- more on this later -- but it is correct.

The pointer access and bitcast logic required some helper functions and
types which are not generally useful elsewhere, so I opted to split them
into separate files `Sema/comptime_ptr_access.zig` and
`Sema/bitcast.zig`, with simple re-exports in `Sema.zig` for their small
public APIs.

Whilst working on this branch, I caught various unrelated bugs with
transitive Sema errors, and with the handling of `undefined` values.
These bugs have been fixed, and corresponding behavior test added.

In terms of performance, I do anticipate that this commit will regress
performance somewhat, because the new pointer access and bitcast logic
is necessarily more complex. I have not yet taken performance
measurements, but will do shortly, and post the results in this PR. If
the performance regression is severe, I will do work to to optimize the
new logic before merge.

Resolves: #19452
Resolves: #19460
2024-04-17 13:41:25 +01:00
..
Build lib/std/Build/Step/CheckObject: dump section as string 2024-04-15 18:06:44 +02:00
c haiku: fix directory iteration 2024-04-08 13:20:14 -04:00
compress compress.xz: remove unnecessary variable 2024-03-13 18:43:36 +01:00
crypto std.crypto.pcurves.*: simpler, smaller, faster u64 addition with carry (#19644) 2024-04-14 01:13:22 +02:00
dwarf extract std.posix from std.os 2024-03-19 11:45:09 -07:00
fmt implement ryu 64-bit backend 2024-03-29 22:15:17 -07:00
fs fix namespacing of std.fs.Dir.Walker.Entry 2024-04-14 18:08:42 -07:00
hash std.hash.crc: get rid of usingnamespace 2024-04-11 14:42:04 -07:00
heap WasmAllocator: fix safety panic during OOM 2024-03-23 11:32:37 +01:00
http Revert "std.http.Client: always omit port when it matches default" 2024-04-12 22:37:07 -07:00
io std.io: remove BufferedTee 2024-03-20 21:05:35 -07:00
json json.WriteStream.objectFieldRaw() (#19553) 2024-04-06 03:52:20 -04:00
math std: fix big int llshr to respect aliasing (#19612) 2024-04-13 03:06:23 -07:00
mem
meta std: promote tests to doctests 2024-03-21 14:11:46 -07:00
net std.net, std.http: simplify 2024-02-23 02:37:11 -07:00
os io_uring: simplify copy_cqe logic 2024-03-31 23:57:16 +02:00
posix cbe: fix optional codegen 2024-04-13 01:35:20 -04:00
Random extract std.posix from std.os 2024-03-19 11:45:09 -07:00
sort lib: correct unnecessary uses of 'var' 2023-11-19 09:55:07 +00:00
tar std.tar: implement executable bit only 2024-04-02 14:02:43 -07:00
Target Target: cleanup 2024-04-14 15:33:46 -04:00
testing
Thread Update uses of @fieldParentPtr to use RLS 2024-03-30 20:50:48 -04:00
time std: promote tests to doctests 2024-03-21 14:11:46 -07:00
tz
unicode
valgrind valgrind client request wrappers take const pointers (#19237) 2024-03-26 14:00:13 +02:00
zig Target: cleanup 2024-04-14 15:33:46 -04:00
array_hash_map.zig std.array_hash_map: remove meta context verification 2024-03-21 19:40:56 -07:00
array_list.zig Refactor ArrayList replaceRange tests 2024-04-02 13:46:51 -07:00
ascii.zig std: promote tests to doctests 2024-03-21 14:11:46 -07:00
atomic.zig bsd: debitrot AtomicOrder renames 2024-03-15 02:28:50 -04:00
base64.zig improve documentation in std 2024-03-10 18:13:30 -07:00
bit_set.zig std: promote tests to doctests 2024-03-21 14:11:46 -07:00
BitStack.zig
bounded_array.zig std: promote tests to doctests 2024-03-21 14:11:46 -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: revert the breaking changes only 2024-04-11 14:09:58 -07:00
builtin.zig extract std.posix from std.os 2024-03-19 11:45:09 -07:00
c.zig haiku: debitrot 2024-03-23 18:11:32 +01:00
child_process.zig std: promote tests to doctests 2024-03-21 14:11:46 -07:00
coff.zig lib/std/coff: add ImportNameType.NAME_EXPORTAS variant 2024-03-29 20:34:28 +01:00
compress.zig improve documentation in std 2024-03-10 18:13:30 -07:00
comptime_string_map.zig Remove redundant test name prefixes now that test names are fully qualified 2024-02-26 15:18:31 -08:00
crypto.zig Merge pull request #19239 from jedisct1/ml-kem 2024-03-11 18:48:08 -07:00
debug.zig std.debug.panic: pass the args 2024-04-11 23:33:38 -07:00
dwarf.zig extract std.posix from std.os 2024-03-19 11:45:09 -07:00
dynamic_library.zig extract std.posix from std.os 2024-03-19 11:45:09 -07:00
elf.zig improve documentation in std 2024-03-10 18:13:30 -07:00
enums.zig compiler: implement analysis-local comptime-mutable memory 2024-03-25 14:49:41 +00:00
fifo.zig std: promote tests to doctests 2024-03-21 14:11:46 -07:00
fmt.zig compiler: implement analysis-local comptime-mutable memory 2024-03-25 14:49:41 +00:00
fs.zig fix compilation failures found by CI 2024-03-19 16:18:18 -07:00
gpu.zig std: add gpu namespace 2024-02-05 11:55:14 +03:30
hash.zig XXH3 Implementation for Zig STD (#17530) 2023-10-17 13:55:01 -05:00
hash_map.zig std.hash_map: fix pointer lock safety false positive 2024-03-20 17:14:52 -07:00
heap.zig extract std.posix from std.os 2024-03-19 11:45:09 -07:00
http.zig std.http.Server: expose arbitrary HTTP headers 2024-02-23 02:58:02 -07:00
Ini.zig
io.zig Uri: propagate per-component encoding 2024-04-10 02:11:54 -07:00
json.zig x86_64: pass more tests 2023-10-25 04:28:30 -04:00
leb128.zig Dwarf: improve x86_64 backend debug info 2023-11-12 03:21:52 -05:00
linked_list.zig Revert "std.SinglyLinkedList: add sort function" 2023-11-24 22:33:50 -07:00
log.zig Buffer the logging function 2024-02-09 14:02:57 -08:00
macho.zig lib/std/macho: update PLATFORM enum with VISIONOS tags 2024-04-06 22:21:57 +02:00
math.zig std.math: rework modf 2024-03-29 09:33:57 +00:00
mem.zig std.mem.indexOfSentinel: valgrind integration 2024-03-21 15:01:57 -07:00
meta.zig compiler: implement analysis-local comptime-mutable memory 2024-03-25 14:49:41 +00:00
multi_array_list.zig std.builtin: make container layout fields lowercase 2024-03-11 07:09:07 -07:00
net.zig compiler: rework comptime pointer representation and access 2024-04-17 13:41:25 +01:00
once.zig std: improve std.once tests 2024-04-15 15:24:30 -07:00
os.zig fix compilation failures found by CI 2024-03-19 16:18:18 -07:00
packed_int_array.zig compiler: rework comptime pointer representation and access 2024-04-17 13:41:25 +01:00
pdb.zig extract std.posix from std.os 2024-03-19 11:45:09 -07:00
posix.zig haiku: debitrot 2024-03-23 18:11:32 +01:00
priority_dequeue.zig Remove redundant test name prefixes now that test names are fully qualified 2024-02-26 15:18:31 -08:00
priority_queue.zig Remove redundant test name prefixes now that test names are fully qualified 2024-02-26 15:18:31 -08:00
process.zig ArgIteratorWindows: Match post-2008 C runtime rather than CommandLineToArgvW 2024-04-15 02:09:48 -07:00
Progress.zig std.builtin: make atomic order fields lowercase 2024-03-11 07:09:10 -07:00
Random.zig Fixup filename casing 2024-02-08 15:39:28 +01:00
RingBuffer.zig std.RingBuffer: use sliceAt/sliceFirst in read*AssumeLength 2024-03-10 18:17:23 +11:00
segmented_list.zig Remove redundant test name prefixes now that test names are fully qualified 2024-02-26 15:18:31 -08:00
SemanticVersion.zig Change many test blocks to doctests/decltests 2024-02-26 15:18:31 -08:00
simd.zig improve documentation in std 2024-03-10 18:13:30 -07:00
sort.zig std: promote tests to doctests 2024-03-21 14:11:46 -07:00
start.zig cbe: rewrite CType 2024-03-30 20:50:48 -04:00
start_windows_tls.zig
std.zig extract std.posix from std.os 2024-03-19 11:45:09 -07:00
tar.zig fetch: use empty string instead of null for root_dir 2024-04-09 15:00:22 +02:00
Target.zig wasi: change default os version to 0.1.0 2024-04-14 18:43:52 -04:00
testing.zig std: promote tests to doctests 2024-03-21 14:11:46 -07:00
Thread.zig Merge pull request #19348 from jedisct1/wasi-threads-compfix 2024-03-21 15:13:15 -07:00
time.zig std: promote tests to doctests 2024-03-21 14:11:46 -07:00
treap.zig Remove redundant test name prefixes now that test names are fully qualified 2024-02-26 15:18:31 -08:00
tz.zig std.builtin.Endian: make the tags lower case 2023-10-31 21:37:35 -04:00
unicode.zig compiler: implement analysis-local comptime-mutable memory 2024-03-25 14:49:41 +00:00
Uri.zig std.http.Client: always omit port when it matches default 2024-04-12 22:37:07 -07:00
valgrind.zig Fix simple doc mistakes. (#17624) 2023-10-21 21:24:55 +00:00
wasm.zig Remove redundant test name prefixes now that test names are fully qualified 2024-02-26 15:18:31 -08:00
zig.zig std.zig.fmtId: conditionally escape primitives/_ 2024-04-07 14:47:10 +02:00