Minimal repro:
```Zig
fn foo(slice: []const u8, arr: *const [34]u8) void {
_ = slice;
_ = arr;
}
export fn main() void {
const data: []const u8 = "dummy";
const arr: [34]u8 = @splat(1);
foo(data, &arr);
}
```
With `zig build-obj -target spirv64-vulkan repro.zig` on a debug build causes a nasty linker error:
```
thread 1389148 panic: index out of bounds: index 3021285136, len 81
zig/src/link/Spirv/Flush.zig:78:28: 0x2368081 in copy (main.zig)
if (input.marks[word] != input.mark) {
...
```
Seems like it's coming from call instruction emitting. `params` is reserved `id_scratch` up front, but the slice that's returned can be invalidated by (at least) a path in `cg.resolve()`, as it can also use `id_scratch`. This causes a dangling pointer, which writes some garbage data IDs, and badaboom.
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/37560
Reviewed-by: Ali Cheraghi <alichraghi@noreply.codeberg.org>
The `bitcast` function in SPIR-V's codegen seems to only handle int-to-pointer conversions, even though both int-to-pointer and int-_from_-ptr instructions route to it.
Because of this, `@intFromPtr` emits the `if (target_util.addrSpaceIsLogical(target, .local)) { ... }` fallback branch which doesn't validate, rather than `OpConvertPtrToU`. This also affects things like safety checks generated from `@alignCast`.
Minimal reproduction:
```Zig
extern fn getPtr() *addrspace(.physical_storage_buffer) u8;
export fn main() void {
const testing: usize = @intFromPtr(getPtr());
_ = testing;
}
```
Impl note: There is an `intFromPtr()` function in codegen that emits the instruction, but it's hardcoded to `usize` when `OpConvertPtrToU` as an instruction isn't, so I decided to not use it just in case.
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/37468
Reviewed-by: Ali Cheraghi <alichraghi@noreply.codeberg.org>
Min repro:
```Zig
export fn main() callconv(.kernel) void {
@extern(*const fn() callconv(.spirv_device) *u32, .{ .name = "extern_fn" })().* = 1;
}
```
On my machine, this code causes the `OpStore` after the `OpFunctionCall` to have a pointer ID of 0. I think I've tracked it down to what looks like UB in codegen.
I believe the sema code the `unreachable` comment next to my addition is referring to is this:
```Zig
if (func_val == null and !func_is_extern and !block.is_typeof and zcu.getTarget().cpu.arch.isSpirv()) {
return sema.fail(block, func_src, "SPIR-V does not support calling function pointers", .{});
}
```
which special-cases extern functions, causing undefined behavior in codegen with `@extern(...)`s, where it doesn't account for that. Regular `extern fn`s work fine since they don't go through a function pointer.
I'd ideally like to make this UB fail to compile, or at least try to catch it in a test somehow, but this is my first PR here, and I'm not familiar with this codebase yet! Please let me know if the patch could be better.
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/37456
Reviewed-by: Ali Cheraghi <alichraghi@noreply.codeberg.org>
I'm writing a small numeric library whose kernels run on the CPU, on CUDA and on Vulkan through the self-hosted SPIR-V backend, and I was making them generic over the float type. `f64` needs its own `exp` and `log` on the GPU, so I wrote them as polynomial series, and the `f64` Vulkan module stopped validating. This fixes the three backend bugs underneath that, and a crash I ran into after merging master.
The series take floats apart with `@bitCast` on vectors. Between vectors of the same length this reaches the Vulkan/OpenGL fallback in `bitCast`, which stores the source into a function variable of the destination type:
```zig
export fn main() callconv(.{ .spirv_kernel = .{ .x = 1, .y = 1, .z = 1 } }) void {
var x: @Vector(2, f32) = .{ 1, 2 };
_ = &x;
const bits: @Vector(2, i32) = @bitCast(x);
_ = bits;
}
```
```
$ zig build-exe -fno-llvm -target spirv64-vulkan -mcpu vulkan_v1_2 bitcast.zig -femit-bin=bitcast.spv
$ spirv-val --target-env vulkan1.2 bitcast.spv
error: line 45: OpStore Pointer <id> '36[%36]'s type does not match Object <id> '29[%29]'s type.
```
An elementwise vector bitcast is now a scalar `bitCast` per lane, the way `buildConvert` and friends already unroll vectors. Going through the scalar path also normalizes lanes of strange integers: a single `OpBitcast` on the whole vector validates for `@Vector(n, u3)` to `@Vector(n, i3)`, but leaves lanes that don't compare equal to the negative values they should hold.
The series also convert between integer and float lanes. A vector of one lane, or of more than four without `vector16`, is an array in SPIR-V, and `airFloatFromInt` and `airIntFromFloat` emitted a single `OpConvert*` with that array as its result type:
```zig
export fn main() callconv(.{ .spirv_kernel = .{ .x = 1, .y = 1, .z = 1 } }) void {
var x: @Vector(1, i32) = .{1};
_ = &x;
const y: @Vector(1, f32) = @floatFromInt(x);
_ = y;
}
```
```
$ spirv-val --target-env vulkan1.2 convert.spv
error: line 40: Expected float scalar or vector type as Result Type: ConvertSToF
%y = OpConvertSToF %_arr_f32_u32_1 %26
```
`@floatCast` and `@intCast` already go through `buildConvert`, which unrolls the vector; these two now do as well, with `buildConvert` picking the int/float conversion opcodes.
The coefficients sit in a comptime array that the series take as a comptime slice, and that alone fails to compile, although nothing reads the slice at runtime:
```zig
const weights = [_]f32{ 1, 2, 3 };
inline fn first(comptime w: []const f32) f32 {
return w[0];
}
export fn main() callconv(.{ .spirv_kernel = .{ .x = 1, .y = 1, .z = 1 } }) void {
var x: f32 = first(&weights);
_ = &x;
}
```
```
$ zig build-exe -fno-llvm -target spirv64-vulkan -mcpu vulkan_v1_2 dbgvar.zig -femit-bin=dbgvar.spv
dbgvar.zig:7:8: error: TODO (SPIR-V): pointer to constant 'dbgvar.weights'
```
It appears `airDbgVar` lowers the operand only to attach an `OpName` to it, and a pointer to a non-extern constant is still a TODO. A comptime-known value has no runtime variable of its own to name, so `airDbgVar` now leaves interned operands alone. The same thing held back 58 of the behavior tests skipped on SPIR-V, and the test commit un-skips them.
After merging master, four of those tests crashed the compiler in `structFieldPtr`, which has `unreachable` for packed containers since ebb8683ef1 ([#36996](https://codeberg.org/ziglang/zig/pulls/36996)):
```zig
const Flags = packed struct { a: bool, b: bool };
export fn main() callconv(.{ .spirv_kernel = .{ .x = 1, .y = 1, .z = 1 } }) void {
var flags: Flags = .{ .a = true, .b = false };
const b = &flags.b;
b.* = true;
}
```
```
thread 3961624 panic: reached unreachable code
src/codegen/spirv/CodeGen.zig:6906:27: 0x4767157 in structFieldPtr (main.zig)
.@"packed" => unreachable,
```
My reading is that the `unreachable` holds for the pointer derivations it was written for, since `constantPtr` now turns packed pointers into pointers to their host integer first, but `airStructFieldPtr` and `airStructFieldPtrIndex` still pass packed structs through. A pointer to a packed field is a bit-pointer into the struct's own host integer, so it is the struct pointer: that is what the old code computed, and what the C backend now does with `.begin`. Packed unions get back the TODO error they had before. I may be missing a plan for packed field pointers here, and I'm happy to change it if so. The commit also un-skips three packed tests that crash on master the same way. I suppose nothing caught this because those tests are skipped on SPIR-V and the `spirv64-vulkan` behavior target is commented out in `test/tests.zig`.
I ran the behavior tests through `tools/unskip_spirv_tests.zig` and [zig-spirv-test-executor](https://github.com/Snektron/zig-spirv-test-executor) on a Radeon 680M, and the new and un-skipped tests on an RTX 4060 as well. Every fix has tests that fail on master and pass with it; the vector ones compare lane by lane, since `expectEqual` on vectors doesn't compile for SPIR-V yet. Running each behavior file whole, nothing that passes on master fails. Five more skips already pass on master without these changes, so I left them out, but I can add them. The new vector tests copy the aarch64, arm, sparc64 and riscv64 skips of the tests around them, which I couldn't check.
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/37040
Reviewed-by: Ali Cheraghi <alichraghi@noreply.codeberg.org>
This change at long last replaces the `Zcu.PerThread` parameters in
`Type.print` and `Value.print` with `*Zcu` parameters. This required
various tweaks to avoid the need to intern values when printing them.
The most significant change is to `Value.pointerDerivation`, whose API
and implementation has been changed quite a bit. This API is used by the
C and SPIR-V backends, which I therefore also needed to update---but it
seems to have been surprisingly painless overall!
Most of this diff comes from replacing `fmt(pt)` with `fmt(zcu)` (plus a
few more similar patterns), then following compile errors for a while to
change parameters/constants/fields from `pt` to `zcu` where necessary.
Actual logic changes should be limited to the following source files:
* `src/print_value.zig`
* `src/Type.zig`
* `src/Value.zig`
* `src/codegen/c.zig`
* `src/codegen/spirv/CodeGen.zig`
Previously, the SPIR-V linker would merge every function's MIR into one module, then parsed
it in `BinaryModule`, and run three passes over it:
- `prune_unused` walked the module from its entry points to drop dead declarations
- `dedup_types` deduplicated types and constants
- `lower_invocation_globals` rewrote function signatures to pass a pointer to global
variables as function parameter
Now codegen emits instructions in the order it resolves them (not the spec specified order)
into one section per function. then the linker sorts them into the correct order while also
interning types, constants, strings, etc in the flush step.
Navs the frontend no longer references are now skipped using `Zcu.resolveReferences` instead
of a pass like `prune_unused`. Extern variables are also only loaded when an MIR references them.
`lower_invocation_globals` is gone too. non-`threadlocal` globals on SPIR-V targets are disallowed now.
Currently only vulkan supports them via the newly added `private` address space. See #36715
The new linker also supports incremental compilation now!
ArrayList:
- `getLastOrNull` has been deprecated and renamed to `last`
- `getLast` has been removed in favor of `last` combined with `.?`
- `lastPtr` has been added which returns `?*T`
Upgrade guide:
```zig
if (list.getLastOrNull()) |foo| {
// ...
}
const foo = list.getLast();
```
⬇️
```zig
if (list.last()) |foo| {
// ...
}
const foo = list.last().?;
```
Co-authored-by: Ryan Liptak <squeek502@hotmail.com>
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/36318
Reviewed-by: Ryan Liptak <squeek502@noreply.codeberg.org>
This commit implements the `@backingInt` and `@fromBackingInt` builtins.
These builtins are meant to replace the now-deprecated `@intFromEnum` and
`@enumFromInt` builtins.
`@backingInt` works with all enums and with bitpacks with *explicit* backing
integer types only. It also works with tagged unions, returning the backing
integer of the active tag value.
An `undefined` enum or bitpack yields an `undefined` backing integer.
The builtin is lowered to `bit_cast`.
`@fromBackingInt` infers its result type, which may be any enum or a bitpack
with an *explicit* backing integer type. It takes a parameter of exactly
that backing integer type.
For enums, passing a backing integer that is either `undefined` or would
yield an invalid tag value results in safety-checked Illegal Behavior.
For bitpacks, passing an `undefined` backing integer yields an `undefined`
bitpack.
The builtin is lowered to `bit_cast` or `bit_cast_safe` (more details below).
`@bitCast` now also performs a safety check for invalid tag values if its
destination type is an `enum`.
This commit also introduces a new AIR instruction, `bit_cast_safe`.
Similarly to `int_cast_safe`, it includes a safety check for invalid enum
tag values. When `@enumFromInt` is eventually removed, `int_cast_safe` won't
need to perform this safety check anymore.
Legalize only implements `expand_bit_cast_safe`, and no additional insts
to scalarize `bit_cast_safe` with vector/array operands. That is because
it can just reuse the existing scalarizations for regular `bit_cast`.
The safety check mandated by `bit_cast_safe` is only desired if the result
type is a scalar enum tag value, so the scalarization process is exactly
equivalent. The only difference is that we have to perform a safety check
on the result of the scalarization.
This means that there's no reason for a backend to want scalarization of
regular `bit_cast`, but not of `bit_cast_safe` (or the other way round).
If a backend is having trouble with the `bit_cast_safe` safety check it
should just use `expand_bit_cast_safe` instead.
Also adds a `std.meta.BackingInt()` function to conveniently get the result
type of `@backingInt` and for use in the langref.
This fixes functions that return non-primitive optional values. E.g.
fn someFnWithOptional(x: f32) ?[2]f32 {
if (x > 5) return null;
return .{ x, x };
}
Pieced together referencing airIsNull, and airUnwrapOptionalPtr. Tested
that shaders in an external project are outputting the expected values