Commit Graph

172 Commits

Author SHA1 Message Date
Ali Cheraghi 88d2961df4 spirv: codegen and linker fixes for logical-addressing
A handful of changes to get the regressed behavior tests running again.

- Replace `decorateBlockOffsets` with a recursive function `decorateLayout`
  that walks arrays, vectors, structs, unions, optionals, and error unions,
  emitting `ArrayStride` and member `Offset` decorations at every
  level. Previously we weren't handling nested types.
- Restrict `Block` decoration to struct types with
  `uniform`, `push_constant`, `storage_buffer` storage classes.
  Previously we decorated through every pointer, contaminating the cached struct
  type so the same shape used as a stack local also picked up `Block`.
- Lower `ptr_slice_ptr_ptr` and `ptr_slice_len_ptr`
- No longer emit a redundant `**T` typed `OpVariable` for function parameters.
  Logical addressing also forbids such variables.
- Eliminate dead code from invocation globals unreachable from any entry point.
  Reverting the workaround in `lib/std/start.zig`.
2026-06-14 09:11:59 +02:00
Ali Cheraghi e2d11ff76b spirv: set execution mode via cc info
Execution modes (e.g. `LocalSize`, `OriginUpperLeft`) were previously set
via `gpu.executionMode()`, which used inline assembly to emit `OpExecutionMode`.
The SPIR-V assembler now rejects this instruction and retrieves execution mode information
from function cc, deleting `gpu.executionMode()` entirely.

Two new `spirv_task` and `spirv_mesh` calling conventions are also added
and `PackedCallingConvention.unpack()` now takes a trailing data slice.
2026-06-14 09:11:59 +02:00
David Senoner c91727108e Replace usages of ArrayHashMapUnmanaged with array_hash_map.Custom 2026-06-10 16:14:58 +02:00
David Senoner 37651eea89 Replace usages of AutoArrayHashMapUnmanaged with array_hash_map.Auto 2026-06-10 16:14:58 +02:00
David Senoner d23e22c416 Replace usages of StringArrayHashMapUnmanaged with array_hash_map.String 2026-06-10 16:14:53 +02:00
Krzysztof Wolicki abd649a50c Fix TODO in Type.eql 2026-06-07 05:44:13 +02:00
Ali Cheraghi d7d131c050 add @SpirvType builtin
Closes #35240
Fixes #35238
Fixes #35259

Supported types are:
- `OpTypeSampler`
- `OpTypeImage`
- `OpTypeSampledImage`
- `OpTypeRuntimeArray` with indexing and `.len` field

The SPIR-V backend is bit-rotted so behavior tests no longer pass (compiler crashes).
However I've verified the new added tests are passing.
2026-06-07 04:58:53 +02:00
Matthew Lugg 8fe1ec0cc9 compiler: refactor backend error handling
All public codegen and linker APIs now use the following error set:

    Allocator.Error || Io.Cancelable || error{AlreadyReported}

This is defined as `link.Error` and aliased as `codegen.Error`.

The compiler "backend" (including both codegen and linker) has a fairly
limited set of failure modes. Most of them are as follows:
* Bad inline assembly (codegen)
* Output file I/O error (link)
* Symbol with no definition or multiple definitions (link)
* Relocation error, e.g. overflow (link)
* Unimplemented/unsupported feature (codegen/link)
* `error.OutOfMemory` (codegen/link)
* `error.Canceled` (codegen/link)

The last two cases are special, because they are possible across most of
the compiler codebase and have fixed code paths for handling---e.g.
`error.OutOfMemory` almost always calls `Compilation.setAllocFailure`,
and `error.Canceled` should typically propagate all the way up the call
stack.

However, all of the other cases should follow the same general pattern:
the codegen/linker implementation should mark an error on `Compilation`
somehow (either through `link_diags` or `failed_codegen`), and return
`error.AlreadyReported`. This error code indicates that an operation
failed, but that the caller does not need to take action to recover,
because the failure has already been recorded in a way which will be
expressed to the user. For instance, the codegen error set used to
contain `error.Overflow`, but this case should have already been
reported to the user, because implementation-agnostic code cannot know
how best to express the failure to the user.

The `error.AlreadyReported` error code encompasses what was previously
represented by multiple errors, including `error.CodegenFail`,
`error.LinkFailure`, and `error.AnalysisFail`. It is not necessary to
differentiate these cases. (Not to be confused with the usage of
`error.AnalysisFail` in the compiler *frontend*, i.e. `Sema`, which is
unchanged by this diff.)

Because the backend error set is now very small, it is easy to
exhaustively `switch` on, and also many functions can be given concrete
error sets. There are no longer different error sets for different
operations. (As an exception, I have not tackled `link.File.open` in
this diff, which has a big inferred error set where I think most errors
are reported to the user with an `else => |e|` case.)

I have also changed how `link.MappedFile`, our abstraction for handling
the awkward "moving things around" aspect of incremental linking, does
error handling. The public API of this abstraction now uses the
following small error set:

    Allocator.Error || Io.Cancelable || error{MappedFileIo}

The first two cases are self-explanatory. Then, `error.MappedFileIo` is
a catch-all error code encompassing that an error was encountered when
accessing the underlying `Io.File`. This error may have occurred when
attempting to flush the file to disk, or to change its size, etc. In
this case, the *specific* error---which was actually returned from the
`Io.File` API---is available in the `MappedFile.io_err.?` field. Linker
implementations which use `MappedFile` (currently `Elf2` and `Coff`)
should eventually handle `error.MappedFileIo` by reporting an error in
`Compilation.link_diags`, including that specific I/O error in the error
message.

The rationale here is essentially that error codes returned from
functions exist for control flow purposes, and a linker implementation
should not care about the distinction between file I/O error codes (e.g.
`error.DiskQuota` vs `error.InputOutput`). The only reason it needs the
concrete error is to expose it to the user. Therefore, by wrapping these
failure modes under `error.MappedFileIo`, we keep the error set actually
used by the linker implementation as small as possible, which encourages
avoiding patterns like `else => |e| diags.fail(...)` (which is
potentially dangerous since it may prevent `error.OutOfMemory` or
`error.Canceled` from propagating correctly). It additionally helps
linker implementations maintain more precise error messages---for
instance, if `Elf2` encounters `error.NoSpaceLeft` writing to the output
file, the error will now read "failed to write output file: NoSpaceLeft"
instead of something more generic like "prelink failed: NoSpaceLeft".

Finally, while working on these refactors, I was able to eliminate those
pesky `src_loc: Zcu.LazySrcLoc` parameters from the codegen and link
logic. These parameters did not make sense, because at this point in the
compiler pipeline, we do not have precise source location
information---so these parameters were always passed as just the
location of the function declaration, or as some placeholder
(`.unneeded` or the top of `lib/std/std.zig`) if there wasn't an
appropriate function definition. The only logic which actually *uses*
these source locations is codegen implementations' `fail` functions. Per
my earlier list of failure modes, those functions are called in two main
cases:

* Bad inline assembly
* Unimplemented/unsupported feature

The first case should be moved to the compiler frontend (tracked by
https://github.com/ziglang/zig/issues/10761), while the second case is
essentially a compiler TODO so it is permissible for it to have
imprecise error reporting. Therefore, codegen implementations' `fail`
functions now just call `navSrcLoc` themselves when necessary, which
(once we make improvements to inline assembly) will only happen when
there is a deficiency in the codegen implementation.

I was careful to make `Elf2` and `Coff` error reporting work well in
this diff, by using no `else` case other than `else => |e| return e`
when `switch`ing on errors and by always handling `error.MappedFileIo`
by unwrapping `MappedFile.io_err.?`. I also added concrete error sets to
almost every function in `Elf2`. (That was, um, actually why I started
this diff, because it was annoying me that I wouldn't get as many
compile errors at once...)
2026-06-01 10:26:01 +01:00
Krzysztof Wolicki 9e80795623 all: update to use new std.lang.Type definitions 2026-05-27 10:03:51 +01:00
Andrew Kelley cd23f7a814 Merge pull request 'std.meta: Remove Int/Tuple in favor of @Int/@Tuple' (#35188) from linus/zig:deprecated-std-meta into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/35188
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-05-06 19:35:40 +02:00
Linus Groh 991f56fd6b std.meta: Remove Int in favor of @Int 2026-05-03 21:42:06 +01:00
Matthew Lugg 4c330e053b compiler: use 'std.lang' instead of 'std.builtin' 2026-05-03 12:23:30 +01:00
Matthew Lugg e133f793ee compiler: depend on 'std.lang' instead of 'std.builtin' 2026-05-03 12:23:29 +01:00
Saurabh Mishra d02d0b879c std:ArrayList: Merge getLastOrNull into getLast (#32008)
This PR merges the functionality of the `getLastOrNull` method into `getLast`, which improves consistency as its
based on methods like `front`, `back`, and `peek` in the `Deque` and `PriorityQueue` containers.

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/32008
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-05-02 02:26:15 +02:00
Matthew Lugg 57634b7809 compiler: remove i0 from the language
Resolves: https://github.com/ziglang/zig/issues/1593
2026-04-30 08:57:51 +01:00
GasInfinity 1deb029a66 std: rename bit_set variants and deprecate the managed one.
* aliases and deprecates the previous names.
* also update callsites to use the non-deprecated declarations.
2026-04-27 16:46:26 +02:00
Pavel Verigo d840583458 remove AIR .bool_or/.bool_and 2026-04-19 21:49:51 +02:00
Matthew Lugg c0f3a23831 llvm: get rid of a bunch of PerThread usages
Also, notably, remove `Air.value`! The `onePossibleValue` check was
actually dead code, because it is a bug if Sema ever emits code which
considers a value of OPV type to be runtime-known---and at that point
`Air.value` is just a thin wrapper around `Air.Ref.toInterned`.
2026-03-28 16:46:59 +00:00
Matthew Lugg 5d215838a7 InternPool.Nav: fix race, refactor
I've realised that the cause of at least some of our weird CI flakiness
was a bug in how `Nav` values were resolved. Consider this scenario: the
frontend resolves the type of a `Nav`, and then sends a function to the
backend, which requires the backend to lower a pointer to that `Nav`.
The backend calls `InternPool.getNav` to determine the `Nav`'s type.
However, this races with the frontend resolving the *value* of that
`Nav`. This involves writing separately to two fields, `bits` and
`type_or_value`. If only one of these changes is observed, then the
backend will incorrectly interpret the type as the value or vice versa,
leading to a crash or even a miscompilation. (Of course, there's also
the straightforward issue that the racing loads were non-atomic, making
them illegal).

The only good solution to this was to make `Nav` 4 bytes bigger, giving
it separate `type` and `value` fields. In theory that's a quite small
change, but it ended up having a bunch of nice consequences which led to
this diff being a bit bulkier than expected:

* `Nav.Repr.Bits` was simplified, because it no longer has to track
  "resolution status": we can use `.none` for that. This frees up some
  bits to make things more consistent between the "type resolved" and
  "fully resolved" states.

* This consistency allowed the `Nav.status` union to be replaced with a
  simpler field `Nav.resolved`, which is a bit nicer to work with.

* Most of the "getter" functions were able to be removed from `Nav`
  because the state they were fetching had been moved to simple fields
  on `Nav.resolved`.

* There were still a handful of free bits in `Nav.Repr.Bits`, which
  could be used to represent the "const" and "threadlocal" flags rather
  than these being stored on `Key.Extern` and `Key.Variable`. This is a
  bit more convenient for linkers.

* With those bits gone, `Key.Variable` is a trivial wrapper around a
  type and an initial value, and the fact that a declaration is mutable
  can be represented solely through the "const" flag. Therefore,
  `Key.Variable` no longer served a purpose, and could be eliminated
  entirely in favour of storing the variable's initial value directly in
  the "value" field of the `Nav`.

So, I'm quite pleased with this refactor! But anyway, regarding the bug
fix which actually motivated this: if I've done my job correctly, this
should solve some crashes, such as these (which were what tipped me off
to this bug in the first place):

https://codeberg.org/ziglang/zig/actions/runs/2306/jobs/7/attempt/1
https://codeberg.org/ziglang/zig/actions/runs/2173/jobs/6/attempt/1

...and, who knows, perhaps even the random SIGSEGVs we've seen on some
targets! Probably not, but one can hope.
2026-03-15 11:47:14 +00:00
Matthew Lugg ea7e34224a compiler: don't call getUnionLayout on packed unions 2026-03-10 10:26:15 +00:00
Matthew Lugg d462794e20 get the compiler building
The change in codegen/x86_64/CodeGen.zig was not strictly necessary (the
Sema change I did solves the error I was getting there), I just think
it's better style anyway.
2026-03-10 10:26:13 +00:00
Matthew Lugg b27c56fe50 compiler: get everything building
Several backends are crashing right now. I'll need to fix at least the C
backend before this branch is ready to PR.
2026-03-10 10:26:12 +00:00
Matthew Lugg b8997f871f Sema: clean up and fix alignment handling 2026-03-10 10:26:09 +00:00
Matthew Lugg 187fef209f compiler: rework OPV and noreturn-like types 2026-03-10 10:26:08 +00:00
Matthew Lugg b19074d252 compiler: represent bitpacks as their backing integer
Now that https://github.com/ziglang/zig/issues/24657 has been
implemented, the compiler can simplify its internal representation of
comptime-known `packed struct` and `packed union` values. Instead of
storing them field-wise, we can simply store their backing integer
value. This simplifies many operations and improves efficiency in some
cases.
2026-03-10 10:26:08 +00:00
Matthew Lugg 3086c7977b type resolution progress 2026-03-10 10:26:07 +00:00
Matthew Lugg 510ea6f61f type resolution progress 2026-03-10 10:26:07 +00:00
rpkak 17dd2f0a85 spirv: implement AIR tag struct_field_ptr 2026-02-15 23:09:24 +01:00
Mathieu Suen 36b65ab59e Air: add "unwrap" functions for loading extra data 2026-02-06 13:06:49 +00:00
Andrew Kelley 5e002910df Merge pull request '@extern: add support for SPIR-V locations and descriptors' (#30570) from ashpil/zig:extern-bindings-locations into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30570
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-01-06 23:44:10 +01:00
ashpil 24e7500c33 @extern: add support for spir-v locations and descriptors 2025-12-22 10:00:35 -08:00
Matthew Lugg 18bc7e802f compiler: replace thread pool with std.Io
Eliminate the `std.Thread.Pool` used in the compiler for concurrency and
asynchrony, in favour of the new `std.Io.async` and `std.Io.concurrent`
primitives.

This removes the last usage of `std.Thread.Pool` in the Zig repository.
2025-12-22 12:55:16 +00:00
Benjamin Jurk 4b5351bc0d update deprecated ArrayListUnmanaged usage (#25958) 2025-11-20 14:46:23 -08:00
Prokop Randáček 94e98bfe80 Dedupe types when printing error messages 2025-11-16 16:20:45 +02:00
traxar 868413007f src/codegen/spirv/CodeGen.zig: add missing unary Ops 2025-11-15 16:39:39 +09:00
Matthew Lugg c091e27aac compiler: spring cleaning
I started this diff trying to remove a little dead code from the C
backend, but ended up finding a bunch of dead code sprinkled all over
the place:

* `packed` handling in the C backend which was made dead by `Legalize`
* Representation of pointers to runtime-known vector indices
* Handling for the `vector_store_elem` AIR instruction (now removed)
* Old tuple handling from when they used the InternPool repr of structs
* Straightforward unused functions
* TODOs in the LLVM backend for features which Zig just does not support
2025-11-12 16:00:15 +00:00
kbz_8 c603d27f90 Fixing SPIR-V header generator magic + Adding Zig compiler version to SPIR-V OpSource (#25435)
* fixing Zig generator magic in SPIR-V header; adding zig compiler version to SPIR-V OpSource

* Update src/codegen/spirv/Module.zig

Co-authored-by: rpkak <67059904+rpkak@users.noreply.github.com>

---------

Co-authored-by: rpkak <67059904+rpkak@users.noreply.github.com>
2025-11-04 04:40:29 +00:00
Cooksey99 2cbd0d3f08 spirv: fix airWorkGroupSize to use workgroup_size builtin 2025-10-28 10:23:32 +01:00
GasInfinity 104c272ae5 feat: init x86_16 arch via CBE 2025-10-27 11:19:51 +01:00
Andrew Kelley 79f267f6b9 std.Io: delete GenericReader
and delete deprecated alias std.io
2025-08-29 17:14:26 -07:00
Ali Cheraghi 2f422372b5 spirv: do not decorate nav alignment
they seem to be always `null` even when accessed through extern key so we have no way to tell whether they have natural alignment or not to decorate. And the reason we don't always decorate them is because some environments might be too dumb and crash for this.
2025-08-18 11:24:57 -07:00
Justus Klausecker 8f5ec2a1fb Sema: replace all remaining aggregate interns related to @typeInfo 2025-08-12 16:33:58 +02:00
Andrew Kelley 749f10af49 std.ArrayList: make unmanaged the default 2025-08-11 15:52:49 -07:00
Ali Cheraghi bed99e1ecd spirv: remove prune_unused ISel 2025-08-09 13:27:04 +03:30
Ali Cheraghi cd4b03c5ed spirv: define and use extended instruction set opcodes 2025-08-04 07:05:00 +03:30
Ali Cheraghi 246e1de554 Watch: do not fail when file is removed
before this we would get a crash
2025-08-03 13:16:49 +03:30
Ali Cheraghi 31de2c873f spirv: refactor 2025-08-02 04:16:01 +03:30
Ali Cheraghi f43f89a705 spirv: snake-case the spec 2025-07-14 15:16:17 +02:00
Andrew Kelley 5378fdb153 std.fmt: fully remove format string from format methods
Introduces `std.fmt.alt` which is a helper for calling alternate format
methods besides one named "format".
2025-07-07 22:43:53 -07:00
Andrew Kelley 30c2921eb8 compiler: update a bunch of format strings 2025-07-07 22:43:52 -07:00