Skip to content

Commit

Permalink
Handle constant sized array allocations using no extension (#2713)
Browse files Browse the repository at this point in the history
In LLVM, array allocations might have constant size:

%array = alloca i32, i64 4, align 4
Represent this kind of allocations using OpVariable + OpBitcast.

Before this patch, the SPV_INTEL_variable_length_array extension was used.

Signed-off-by: Victor Perez <victor.perez@codeplay.com>
  • Loading branch information
victor-eds authored Sep 10, 2024
1 parent c38378c commit ea2fcc1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/SPIRV/SPIRVWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2260,14 +2260,14 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
SPIRVValue *Length = transValue(Alc->getArraySize(), BB);
assert(Length && "Couldn't translate array size!");

if (isSpecConstantOpCode(Length->getOpCode())) {
// SPIR-V arrays length can be expressed using a specialization
// constant.
if (isConstantOpCode(Length->getOpCode())) {
// Length can be any constant instruction, either a specialization
// constant or a non-specialization constant.
//
// Spec Constant Length Arrays need special treatment, as the allocation
// type will be 'OpTypePointer(Function, OpTypeArray(ElementType,
// Length))', we need to bitcast the obtained pointer to the expected
// type: 'OpTypePointer(Function, ElementType).
// Array allocations need special treatment: as the allocation type will
// be 'OpTypePointer(Function, OpTypeArray(ElementType, Length))', we
// need to bitcast the obtained pointer to the expected type:
// 'OpTypePointer(Function, ElementType).
SPIRVType *AllocationType = BM->addPointerType(
StorageClassFunction,
BM->addArrayType(transType(Alc->getAllocatedType()), Length));
Expand Down
52 changes: 52 additions & 0 deletions test/array-alloca.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
; RUN: llvm-as %s -o %t.bc
; RUN: llvm-spirv %t.bc -o %t.spv

; Validation test.
; RUN: spirv-val %t.spv

; SPIR-V codegen test.
; RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck %s --check-prefix=CHECK-SPIRV

; Roundtrip test.
; RUN: llvm-spirv -r %t.spv -o - | llvm-dis -o - | FileCheck %s --check-prefix=CHECK-LLVM

target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"
target triple = "spir64-unknown-unknown"

; CHECK-SPIRV: Decorate [[#ARR:]] Alignment 4
; CHECK-SPIRV: Decorate [[#BITARR:]] Alignment 4

; CHECK-SPIRV: TypeInt [[#I32:]] 32 0
; CHECK-SPIRV: TypeInt [[#I64:]] 64 0
; CHECK-SPIRV: Constant [[#I64]] [[#SIZE:]] 4 0
; CHECK-SPIRV: TypeVoid [[#VOID:]]
; CHECK-SPIRV: TypeFunction [[#FUNCTY:]] [[#VOID]]
; CHECK-SPIRV: TypePointer [[#PTRTY:]] [[#FUNCSTORAGE:]] [[#I32]]
; CHECK-SPIRV: TypeArray [[#ARRTY:]] [[#I32]] [[#SIZE]]
; CHECK-SPIRV: TypePointer [[#ARRPTRTY:]] [[#FUNCSTORAGE]] [[#ARRTY]]

; CHECK-SPIRV: Function [[#VOID]] {{.*}} [[#FUNCTY]]
; CHECK-SPIRV: Variable [[#ARRPTRTY]] [[#ARR]] [[#FUNCSTORAGE]]
; CHECK-SPIRV: Bitcast [[#PTRTY]] [[#BITARR]] [[#ARR]]

; Generated LLVM is different, but equivalent as an array type is allocated.
; CHECK-LLVM: define spir_func void @test_array_alloca()
; CHECK-LLVM: %[[#ALLOC:]] = alloca [4 x i32], align 4
; CHECK-LLVM: %{{.*}} = bitcast ptr %[[#ALLOC]] to ptr
define dso_local void @test_array_alloca() #0 {
%arr = alloca i32, i64 4, align 4
ret void
}

attributes #0 = { nounwind "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-realign-stack" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }

!opencl.enable.FP_CONTRACT = !{}
!opencl.spir.version = !{!1}
!opencl.ocl.version = !{!1}
!opencl.used.extensions = !{!2}
!opencl.used.optional.core.features = !{!3}
!opencl.compiler.options = !{!2}

!1 = !{i32 1, i32 2}
!2 = !{}
!3 = !{}

0 comments on commit ea2fcc1

Please sign in to comment.