From 5f38a727b0dc15522e1fcde9b05f42f553ec8db2 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Tue, 3 Nov 2026 14:23:48 +0100 Subject: [PATCH] SPIR-V: stop removing freeze instructions Both SPIR-V back-ends lower `freeze` themselves: the Khronos translator (SPIRV-LLVM-Translator#1140) and the LLVM back-end since LLVM 19, each replacing it by its operand (or by a null constant for a frozen undef). With the SPV_KHR_poison_freeze extension, which both support as of version 23, they emit `OpFreezeKHR` instead. Stripping `freeze` beforehand made that extension impossible to use, while adding nothing over the back-ends' own fallback. --- src/spirv.jl | 24 ------------------------ test/spirv.jl | 23 +++++++++++++++++++++++ 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/spirv.jl b/src/spirv.jl index b5156f47..e1557293 100644 --- a/src/spirv.jl +++ b/src/spirv.jl @@ -307,10 +307,6 @@ end # The SPIRV Tools don't handle Julia's debug info, rejecting DW_LANG_Julia... strip_debuginfo!(mod) - # the LLVM to SPIR-V translator does not support the freeze instruction - # (SPIRV-LLVM-Translator#1140) - rm_freeze!(job, mod) - # translate to SPIR-V input = bitcode(mod) dump_input() = let path = tempname(cleanup=false) * ".bc" @@ -905,26 +901,6 @@ end ## LLVM passes -# remove freeze and replace uses by the original value -# (KhronosGroup/SPIRV-LLVM-Translator#1140) -function rm_freeze!(@nospecialize(job::CompilerJob), mod::LLVM.Module) - changed = false - @tracepoint "remove freeze" begin - - for f in mod.functions, bb in f.blocks, inst in bb.instructions - if inst isa LLVM.FreezeInst - orig = first(inst.operands) - replace_uses!(inst, orig) - @compiler_assert isempty(inst.uses) job - erase!(inst) - changed = true - end - end - - end - return changed -end - # flatten `insertvalue`s with multiple indices into single-index ones, extracting and # re-inserting the intermediate aggregates: `insertvalue %agg, %val, 1, 0` becomes # %sub = extractvalue %agg, 1 diff --git a/test/spirv.jl b/test/spirv.jl index 35e09f0e..b2ab9bd6 100644 --- a/test/spirv.jl +++ b/test/spirv.jl @@ -685,6 +685,29 @@ end end end +@testset "freeze" begin + # both back-ends lower `freeze` themselves, so it must reach them + mod = @eval module $(gensym()) + frozen(x::Int32) = Base.llvmcall(""" + %y = freeze i32 %0 + ret i32 %y""", Int32, Tuple{Int32}, x) + function kernel(out::Ptr{Int32}, x::Int32) + unsafe_store!(out, frozen(x)) + return + end + end + + @test @filecheck begin + @check "freeze i32" + SPIRV.code_llvm(mod.kernel, Tuple{Ptr{Int32}, Int32}; backend, kernel=true) + end + + @test @filecheck begin + @check "OpStore" + SPIRV.code_native(mod.kernel, Tuple{Ptr{Int32}, Int32}; backend, kernel=true) + end +end + end @testset "min and max lowering" begin