ziglang/zig
261
5.9k
Fork
You've already forked zig
763

SPIRV Types Not Considered Equal #35915

Closed
opened 2026年06月24日 08:40:35 +02:00 by MasonRemaley · 3 comments

Zig Version

0.17.0-dev.957+53c670f46

Steps to Reproduce, Observed Behavior, and Expected Behavior

I expected this test to pass:

constSampler=@SpirvType(.sampler);constSampler2=@SpirvType(.sampler);comptimeassert(Sampler==Sampler2);

However, SPIRV types are currently considered to be unique. This issue is partially resolved by #35914, but that fix isn't enough to get the test case passing.

I believe that this is because each type is given a unique name in zirReifySpirvType:

Lines 20603 to 20610 in f26cdb2
constname=tryip.getOrPutStringFmt(
gpa,
io,
pt.tid,
"{f}__SpirvType_{d}",
.{block.type_name_ctx.fmt(ip),@intFromEnum(inst)},
.no_embedded_nulls,
);

If you log the types with @compileLog, you can see the unique names:

@as(type,spirv.Sampler__SpirvType_12)@as(type,spirv.Sampler2__SpirvType_17)

I decided not to PR a fix since it's possible I'm misunderstanding the situation and this is intentional, I haven't dug into the SPIRV backend much yet.

Assuming this isn't desirable, I think we should rework zirReifySpirvType to look more like zirVectorType--ie it should only store its input parameters on the resulting type--and then add some coverage for type equality to test/behavior/spirv.zig.

CC @alichraghi

### Zig Version 0.17.0-dev.957+53c670f46 ### Steps to Reproduce, Observed Behavior, and Expected Behavior I expected this test to pass: ```zig const Sampler = @SpirvType(.sampler); const Sampler2 = @SpirvType(.sampler); comptime assert(Sampler == Sampler2); ``` However, SPIRV types are currently considered to be unique. This issue is partially resolved by #35914, but that fix isn't enough to get the test case passing. I believe that this is because each type is given a unique name in `zirReifySpirvType`: https://codeberg.org/ziglang/zig/src/commit/f26cdb2771a4bb4d5f1d5acc446ec51c3e177f75/src/Sema.zig#L20603-L20610 If you log the types with `@compileLog`, you can see the unique names: ```zig @as(type, spirv.Sampler__SpirvType_12) @as(type, spirv.Sampler2__SpirvType_17) ``` I decided not to PR a fix since it's possible I'm misunderstanding the situation and this is intentional, I haven't dug into the SPIRV backend much yet. Assuming this isn't desirable, I think we should rework `zirReifySpirvType` to look more like `zirVectorType`--ie it should only store its input parameters on the resulting type--and then add some coverage for type equality to `test/behavior/spirv.zig`. CC @alichraghi

Yes that's intentional. @SpirvType(.runtime_array) types can be decorated via inline assembly so discerning between them was necessary since otherwise this would result a miscompilation:

constArr1=@SpirvType(.{.runtime_array=u32});constArr2=@SpirvType(.{.runtime_array=u32});asmvolatile("OpDecorate %arr ArrayStride 5",[arr]"t"(Arr1));

However, I think you're right because:

  • My above case reasoning is now irrelevant since types are no longer cached in CodeGen and we have a deduplication ISel which prevents the final module from having two exact types
  • There are no documented decorations for other types in @SpirvType (sampler/image/sampled_image) and using inline asm to override the default array stride is inherently wrong because we can literally just use a larger or smaller element type instead.
Yes that's intentional. `@SpirvType(.runtime_array)` types can be decorated via inline assembly so discerning between them was necessary since otherwise this would result a miscompilation: ```zig const Arr1 = @SpirvType(.{ .runtime_array = u32 }); const Arr2 = @SpirvType(.{ .runtime_array = u32 }); asm volatile ("OpDecorate %arr ArrayStride 5", [arr] "t" (Arr1)); ``` However, I think you're right because: - My above case reasoning is now irrelevant since types are no longer cached in CodeGen and we have a deduplication ISel which prevents the final module from having two exact types - There are no documented decorations for other types in `@SpirvType` (`sampler`/`image`/`sampled_image`) and using inline asm to override the default array stride is inherently wrong because we can literally just use a larger or smaller element type instead.
Owner
Copy link

FWIW, the frontend logic here is incorrect regardless of what the SPIR-V backend needs. Sema cannot blindly return distinct types every time a type-creation instruction is analyzed, because:

  • Incremental compilation relies on analysis being idempotent for reasonable performance
  • It makes comptime function call memoization have observable side effects, which the language specification intentionally avoids

If distinct types are necessary, the type still needs to be keyed on something reproducible---for instance the AST node.

(That said, it sounds like you've determined that the distinct types aren't necessary anyway in this case?)

FWIW, the frontend logic here is incorrect regardless of what the SPIR-V backend needs. Sema cannot blindly return distinct types every time a type-creation instruction is analyzed, because: * Incremental compilation relies on analysis being idempotent for reasonable performance * It makes comptime function call memoization have observable side effects, which the language specification intentionally avoids If distinct types are necessary, the type still needs to be keyed on *something* reproducible---for instance the AST node. (That said, it sounds like you've determined that the distinct types aren't necessary anyway in this case?)

distinct types aren't necessary anyway in this case?

Correct

> distinct types aren't necessary anyway in this case? Correct
mlugg added this to the Urgent milestone 2026年06月24日 14:17:56 +02:00
andrewrk modified the milestone from Urgent to 0.17.0 2026年07月01日 10:19:43 +02:00
Sign in to join this conversation.
No Branch/Tag specified
master
elfv2
spork8
restricted
0.16.x
panic-rewrite
parking-futex-lockfree
windows-Io-cleanup
Io-watch
ParseCommandLineOptions
windows-async-files
poll-ring
debug-file-leaks-differently
debug-file-leaks
ProcessPrng
elfv2-dyn
jobserver
threadtheft
io-threaded-no-queue
0.15.x
Io.net
comptime-allocator
restricted-function-pointers
cli
wasm-linker-writer
wrangle-writer-buffering
sha1-stream
async-await-demo
fixes
0.14.x
ast-node-methods
macos-debug-info
make-vs-configure
fuzz-macos
sans-aro
ArrayList-reserve
incr-bug
llvm-ir-nosanitize-metadata
ci-tarballs
ci-scripts
threadpool
0.12.x
new-pkg-hash
json-diagnostics
more-doctests
rework-comptime-mutation
0.11.x
ci-perf-comment
stage2-async
0.10.x
autofix
0.9.x
aro
hcs
0.8.x
0.7.x
0.16.0
0.15.2
0.15.1
0.15.0
0.14.1
0.14.0
0.12.1
0.13.0
0.12.0
0.11.0
0.10.1
0.10.0
0.9.1
0.9.0
0.8.1
0.8.0
0.7.1
0.7.0
0.6.0
0.5.0
0.4.0
0.3.0
0.2.0
0.1.1
0.1.0
Labels
Clear labels
abi/f32
abi/ilp32
abi/sf
accepted
This proposal is planned.
arch/21k
arch/6502
arch/aarch64
arch/alpha
arch/amdgcn
arch/arc
arch/arc32
arch/arc64
arch/arm
arch/avr
arch/bfin
arch/bpf
arch/colossus
arch/cris
arch/csky
arch/dlx
arch/epiphany
arch/fr30
arch/frv
arch/hexagon
arch/hppa
arch/hppa64
arch/ia64
arch/kalimba
arch/kvx
arch/lanai
arch/lm32
arch/loongarch32
arch/loongarch64
arch/m32r
arch/m68k
arch/m88k
arch/mcore
arch/microblaze
arch/mips
arch/mips64
arch/mmix
arch/moxie
arch/mrisc32
arch/msp430
arch/nds32
arch/ns32k
arch/nvptx
arch/or1k
arch/powerpc
arch/powerpc64
arch/propeller
arch/riscv32
arch/riscv64
arch/rl78
arch/rx
arch/s390x
arch/sh
arch/sparc
arch/sparc64
arch/spirv
arch/spu
arch/tricore
arch/v850
arch/vax
arch/vc4
arch/ve
arch/wasm
arch/x86
arch/x86_64
arch/xcore
arch/xtensa
autodoc
The web application for interactive documentation and generation of its assets.
backend/c
The C backend outputs C source code.
backend/llvm
The LLVM backend outputs an LLVM bitcode module.
backend/self-hosted
The self-hosted backends produce machine code directly.
binutils
Zig's included binary utilities: zig ar, zig dlltool, zig lib, zig ranlib, zig objcopy, and zig rc.
breaking
Implementing this issue could cause existing code to no longer compile or have different behavior.
build system
The Zig build system - zig build, std.Build, the build runner, and package management.
debug info
An issue related to debug information (e.g. DWARF) produced by the Zig compiler.
docs
An issue with documentation, e.g. the language reference or standard library doc comments.
error message
This issue points out an error message that is unhelpful and should be improved.
frontend
Tokenization, parsing, AstGen, ZonGen, Sema, Legalize, and Liveness.
fuzzing
An issue related to Zig's integrated fuzz testing.
incremental
Reuse of internal compiler state for faster compilation.
lib/c
This issue relates to Zig's libc implementation and/or vendored libcs.
lib/compiler-rt
This issue relates to Zig's compiler-rt library.
lib/cxx
This issue relates to Zig's vendored libc++ and/or libc++abi.
lib/std
This issue relates to Zig's standard library.
lib/tsan
This issue relates to Zig's vendored libtsan.
lib/ubsan-rt
This issue relates to Zig's ubsan-rt library.
lib/unwind
This issue relates to Zig's vendored libunwind.
linking
Zig's integrated object file and incremental linker.
miscompilation
The compiler reports success but produces semantically incorrect code.
os/android
os/contiki
os/dragonfly
os/driverkit
os/emscripten
os/freebsd
os/fuchsia
os/haiku
os/hermit
os/hurd
os/illumos
os/ios
os/linux
os/maccatalyst
os/macos
os/managarm
os/netbsd
os/ohos
os/openbsd
os/plan9
os/redox
os/rtems
os/serenity
os/tvos
os/uefi
os/visionos
os/wasi
os/watchos
os/windows
proposal
This issue suggests language modifications. If it also has the "accepted" label then it is planned.
release notes
This issue or pull request should be mentioned in the release notes.
testing
This issue is related to testing the compiler, standard library, or other parts of Zig.
zig cc
Zig as a drop-in C-family compiler.
zig fmt
The Zig source code formatter.
zig reduce
The Zig source code reduction tool.
bounty
https://ziglang.org/news/announcing-donor-bounties
bug
Observed behavior contradicts documented or intended behavior.
contributor-friendly
This issue is limited in scope and/or knowledge of project internals.
downstream
An issue with a third-party project that uses this project.
enhancement
Solving this issue will likely involve adding new logic or components to the codebase.
infra
An issue related to project infrastructure, e.g. continuous integration.
optimization
A task to improve performance and/or resource usage.
question
No questions on the issue tracker; use a community space instead.
regression
Something that used to work in a previous version stopped working
upstream
An issue with a third-party project that this project uses.
use case
Describes a real use case that is difficult or impossible, but does not propose a solution.
No labels
abi/f32
abi/ilp32
abi/sf
accepted
arch/21k
arch/6502
arch/aarch64
arch/alpha
arch/amdgcn
arch/arc
arch/arc32
arch/arc64
arch/arm
arch/avr
arch/bfin
arch/bpf
arch/colossus
arch/cris
arch/csky
arch/dlx
arch/epiphany
arch/fr30
arch/frv
arch/hexagon
arch/hppa
arch/hppa64
arch/ia64
arch/kalimba
arch/kvx
arch/lanai
arch/lm32
arch/loongarch32
arch/loongarch64
arch/m32r
arch/m68k
arch/m88k
arch/mcore
arch/microblaze
arch/mips
arch/mips64
arch/mmix
arch/moxie
arch/mrisc32
arch/msp430
arch/nds32
arch/ns32k
arch/nvptx
arch/or1k
arch/powerpc
arch/powerpc64
arch/propeller
arch/riscv32
arch/riscv64
arch/rl78
arch/rx
arch/s390x
arch/sh
arch/sparc
arch/sparc64
arch/spirv
arch/spu
arch/tricore
arch/v850
arch/vax
arch/vc4
arch/ve
arch/wasm
arch/x86
arch/x86_64
arch/xcore
arch/xtensa
autodoc
backend/c
backend/llvm
backend/self-hosted
binutils
breaking
build system
debug info
docs
error message
frontend
fuzzing
incremental
lib/c
lib/compiler-rt
lib/cxx
lib/std
lib/tsan
lib/ubsan-rt
lib/unwind
linking
miscompilation
os/android
os/contiki
os/dragonfly
os/driverkit
os/emscripten
os/freebsd
os/fuchsia
os/haiku
os/hermit
os/hurd
os/illumos
os/ios
os/linux
os/maccatalyst
os/macos
os/managarm
os/netbsd
os/ohos
os/openbsd
os/plan9
os/redox
os/rtems
os/serenity
os/tvos
os/uefi
os/visionos
os/wasi
os/watchos
os/windows
proposal
release notes
testing
zig cc
zig fmt
zig reduce
bounty
bug
contributor-friendly
downstream
enhancement
infra
optimization
question
regression
upstream
use case
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ziglang/zig#35915
Reference in a new issue
ziglang/zig
No description provided.
Delete branch "%!s()"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?