[SPIR-V] Emit builtin variable OpVariable into entry block (#189958)

This commit is contained in:
Arseniy Obolenskiy 2026-04-03 13:18:48 +02:00 committed by GitHub
parent c2ec012098
commit 03b9c7278e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 92 additions and 5 deletions

View File

@ -834,7 +834,14 @@ Register SPIRVGlobalRegistry::buildGlobalVariable(
return ResVReg;
}
auto MIB = MIRBuilder.buildInstr(SPIRV::OpVariable)
// Emit the OpVariable into the entry block to ensure the def dominates
// all uses across all MBBs.
MachineBasicBlock &EntryBB = MIRBuilder.getMF().front();
MachineIRBuilder GVBuilder(MIRBuilder.getState());
if (&GVBuilder.getMBB() != &EntryBB)
GVBuilder.setInsertPt(EntryBB, EntryBB.getFirstTerminator());
auto MIB = GVBuilder.buildInstr(SPIRV::OpVariable)
.addDef(ResVReg)
.addUse(getSPIRVTypeID(BaseType))
.addImm(static_cast<uint32_t>(Storage));

View File

@ -0,0 +1,48 @@
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv-vulkan-unknown %s -o - | FileCheck %s
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-vulkan-unknown %s -o - -filetype=obj | spirv-val %}
;; Verify correct translation when the same builtin is called from multiple
;; non-entry blocks. The fix is validated by -verify-machineinstrs that fails
;; if the OpVariable's VReg definition does not dominate all its uses in MIR.
; CHECK-DAG: OpDecorate %[[#VarID:]] BuiltIn LocalInvocationId
; CHECK-DAG: %[[#VarID]] = OpVariable %[[#]] Input
define internal spir_func void @main_inner(<3 x i32> noundef %ID) {
entry:
ret void
}
define void @main.1() #0 {
entry:
%cmp = icmp sgt i32 1, 0
br i1 %cmp, label %then, label %else
then:
%0 = call i32 @llvm.spv.thread.id.in.group.i32(i32 0)
%1 = insertelement <3 x i32> poison, i32 %0, i64 0
%2 = call i32 @llvm.spv.thread.id.in.group.i32(i32 1)
%3 = insertelement <3 x i32> %1, i32 %2, i64 1
%4 = call i32 @llvm.spv.thread.id.in.group.i32(i32 2)
%5 = insertelement <3 x i32> %3, i32 %4, i64 2
call void @main_inner(<3 x i32> %5)
br label %exit
else:
%6 = call i32 @llvm.spv.thread.id.in.group.i32(i32 0)
%7 = insertelement <3 x i32> poison, i32 %6, i64 0
%8 = call i32 @llvm.spv.thread.id.in.group.i32(i32 1)
%9 = insertelement <3 x i32> %7, i32 %8, i64 1
%10 = call i32 @llvm.spv.thread.id.in.group.i32(i32 2)
%11 = insertelement <3 x i32> %9, i32 %10, i64 2
call void @main_inner(<3 x i32> %11)
br label %exit
exit:
ret void
}
declare i32 @llvm.spv.thread.id.in.group.i32(i32) #1
attributes #0 = { norecurse "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }
attributes #1 = { nounwind willreturn memory(none) }

View File

@ -1,7 +1,6 @@
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s --check-prefix=CHECK-SPIRV
; TODO: This test currently fails with LLVM_ENABLE_EXPENSIVE_CHECKS enabled
; XFAIL: expensive_checks
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s --check-prefix=CHECK-SPIRV
; TODO: enable spirv-val: OpGroupAsyncCopy event arg needs OpTypeEvent, not OpTypePointer
; RUNx: %if spirv-tools %{ llc -O0 -mtriple=spirv32-unknown-unknown %s -o - -filetype=obj | spirv-val %}
; CHECK-SPIRV-DAG: %[[#]] = OpGroupAsyncCopy %[[#]] %[[#Scope:]]
; CHECK-SPIRV-DAG: %[[#Scope]] = OpConstant %[[#]]

View File

@ -0,0 +1,33 @@
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv32-unknown-unknown %s -o - -filetype=obj | spirv-val %}
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}
;; Verify correct translation when the same builtin is called from multiple
;; non-entry blocks. The fix is validated by -verify-machineinstrs that fails
;; if the OpVariable's VReg definition does not dominate all its uses in MIR.
; CHECK-DAG: OpDecorate %[[#VarID:]] BuiltIn LocalInvocationId
; CHECK-DAG: %[[#VarID]] = OpVariable %[[#]] Input
define spir_kernel void @test(ptr addrspace(1) %out, i32 %n) {
entry:
%cmp = icmp sgt i32 %n, 0
br i1 %cmp, label %then, label %else
then:
%id0 = call spir_func i32 @_Z12get_local_idj(i32 0)
store i32 %id0, ptr addrspace(1) %out, align 4
br label %exit
else:
%id1 = call spir_func i32 @_Z12get_local_idj(i32 0)
%neg = sub i32 0, %id1
store i32 %neg, ptr addrspace(1) %out, align 4
br label %exit
exit:
ret void
}
declare spir_func i32 @_Z12get_local_idj(i32)