[SPIRV] fix alloca -> OpVariable lowering (#164175)

fixes #163777

Test was written with help from Copilot

---------

Co-authored-by: Juan Manuel Martinez Caamaño <jmartinezcaamao@gmail.com>
This commit is contained in:
Simeon David Schaub 2026-03-11 15:16:22 +01:00 committed by GitHub
parent ea9f8b7746
commit b3f56eb490
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 47 additions and 10 deletions

View File

@ -1911,7 +1911,8 @@ void SPIRVEmitIntrinsics::replacePointerOperandWithPtrCast(
return;
} else if (isTodoType(Pointer)) {
eraseTodoType(Pointer);
if (!isa<CallInst>(Pointer) && !isaGEP(Pointer)) {
if (!isa<CallInst>(Pointer) && !isaGEP(Pointer) &&
!isa<AllocaInst>(Pointer)) {
// If this wouldn't be the first spv_ptrcast but existing type info is
// uncomplete, update spv_assign_ptr_type arguments.
if (CallInst *AssignCI = GR->findAssignPtrTypeInstr(Pointer)) {

View File

@ -0,0 +1,33 @@
; Test that alloca with aggregate type generates correct OpVariable
; with the array type as the pointee, not a pointer-to-pointer type
;
; This test verifies that when we have an alloca of an array containing
; structs with pointers, the OpVariable uses the correct array type
; instead of incorrectly using a pointer-to-pointer type.
; RUN: llc -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}
; CHECK-DAG: %[[#Int8:]] = OpTypeInt 8 0
; CHECK-DAG: %[[#Int64:]] = OpTypeInt 64 0
; CHECK-DAG: %[[#Int32:]] = OpTypeInt 32 0
; CHECK-DAG: %[[#One:]] = OpConstant %[[#Int32]] 1
; CHECK-DAG: %[[#Two:]] = OpConstant %[[#Int32]] 2
; CHECK-DAG: %[[#PtrCross:]] = OpTypePointer CrossWorkgroup %[[#Int8]]
; CHECK-DAG: %[[#Array1:]] = OpTypeArray %[[#Int64]] %[[#One]]
; CHECK-DAG: %[[#Struct1:]] = OpTypeStruct %[[#PtrCross]] %[[#Int64]] %[[#Array1]] %[[#Int64]]
; CHECK-DAG: %[[#Array2:]] = OpTypeArray %[[#Array1]] %[[#Two]]
; CHECK-DAG: %[[#Struct2:]] = OpTypeStruct %[[#Struct1]] %[[#Array2]]
; CHECK-DAG: %[[#Struct3:]] = OpTypeStruct %[[#Struct2]]
; CHECK-DAG: %[[#ArrayStruct:]] = OpTypeArray %[[#Struct3]] %[[#One]]
; CHECK-DAG: %[[#PtrFunc:]] = OpTypePointer Function %[[#ArrayStruct]]
; Verify OpVariable uses the array type, not pointer-to-pointer
; CHECK: %[[#Var:]] = OpVariable %[[#PtrFunc]] Function
define void @test_alloca_aggregate() {
entry:
%y = alloca [1 x { { { ptr addrspace(1), i64, [1 x i64], i64 }, [2 x [1 x i64]] } }]
%ptr = load ptr addrspace(1), ptr %y, align 8
ret void
}

View File

@ -9,21 +9,24 @@
; CHECK-DAG: %[[TyInt64:.*]] = OpTypeInt 64 0
; CHECK-DAG: %[[TyFun:.*]] = OpTypeFunction %[[TyInt64]] %[[TyInt64]]
; CHECK-DAG: %[[TyPtrFunCodeSection:.*]] = OpTypePointer CodeSectionINTEL %[[TyFun]]
; CHECK-DAG: %[[TyPtrPtrFunCodeSection:.*]] = OpTypePointer Function %[[TyPtrFunCodeSection]]
; CHECK-DAG: %[[ConstFunFp:.*]] = OpConstantFunctionPointerINTEL %[[TyPtrFunCodeSection]] %[[DefFunFp:.*]]
; CHECK-DAG: %[[TyPtrFun:.*]] = OpTypePointer Function %[[TyFun]]
; CHECK-DAG: %[[TyPtrPtrFun:.*]] = OpTypePointer Function %[[TyPtrFun]]
; CHECK-DAG: %[[TyInt8:.*]] = OpTypeInt 8 0
; CHECK-DAG: %[[TyPtrInt8:.*]] = OpTypePointer Function %[[TyInt8]]
; CHECK-DAG: %[[TyPtrPtrInt8:.*]] = OpTypePointer Function %[[TyPtrInt8]]
; CHECK-DAG: %[[TyPtrPtrFunCodeSection:.*]] = OpTypePointer Function %[[TyPtrFunCodeSection]]
; CHECK-DAG: %[[ConstFunFp:.*]] = OpConstantFunctionPointerINTEL %[[TyPtrFunCodeSection]] %[[DefFunFp:.*]]
; CHECK: OpFunction
; CHECK: %[[Var:.*]] = OpVariable %[[TyPtrPtrFun]] Function
; CHECK: %[[Cast:.*]] = OpBitcast %[[TyPtrPtrFunCodeSection]] %[[Var]]
; CHECK: OpStore %[[Cast]] %[[ConstFunFp]]
; CHECK: %[[FP:.*]] = OpLoad %[[TyPtrFun]] %[[Var]]
; CHECK: %[[Var:.*]] = OpVariable %[[TyPtrPtrInt8]] Function
; CHECK: %[[Cast1:.*]] = OpBitcast %[[TyPtrPtrFun]] %[[Var]]
; CHECK: %[[Cast2:.*]] = OpBitcast %[[TyPtrPtrFunCodeSection]] %[[Cast1]]
; CHECK: OpStore %[[Cast2]] %[[ConstFunFp]] Aligned 4
; CHECK: %[[Cast3:.*]] = OpBitcast %[[TyPtrPtrFun]] %[[Var]]
; CHECK: %[[FP:.*]] = OpLoad %[[TyPtrFun]] %[[Cast3]] Aligned 4
; CHECK: OpFunctionPointerCallINTEL %[[TyInt64]] %[[FP]] %[[#]]
; CHECK: OpFunctionEnd
; CHECK: %[[DefFunFp]] = OpFunction %[[TyInt64]] None %[[TyFun]]
target triple = "spir64-unknown-unknown"
; CHECK: %[[DefFunFp]] = OpFunction %[[TyInt64]] None %[[TyFun]]
define spir_kernel void @test() {
entry: