KhronosGroup / KhronosGroup/SPIRV-LLVM-Translator

kernel calling `alloca` in non-top block fails validation

Open
#3,389 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
LLVM
Stars
625
Forks
279
Avg merge
3d 5h
Merged PRs (30d)
34

Description

Reproducer:

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

define spir_kernel void @f() {
conversion:
br label %top

top: ; preds = %conversion
%As = alloca [1 x i64], align 8
ret void
}
```

gets translated to:

```spv
; SPIR-V
; Version: 1.0
; Generator: Khronos LLVM/SPIR-V Translator; 14
; Bound: 15
; Schema: 0
OpCapability Addresses
OpCapability Linkage
OpCapability Kernel
OpCapability Int64
%1 = OpExtInstImport "OpenCL.std"
OpMemoryModel Physical64 OpenCL
OpEntryPoint Kernel %12 "f"
OpSource Unknown 0
OpName %f "f"
OpName %conversion "conversion"
OpName %top "top"
OpName %As "As"
OpDecorate %f LinkageAttributes "f" Export
OpDecorate %As Alignment 8
%ulong = OpTypeInt 64 0
%ulong_1 = OpConstant %ulong 1
%void = OpTypeVoid
%3 = OpTypeFunction %void
%_arr_ulong_ulong_1 = OpTypeArray %ulong %ulong_1
%_ptr_Function__arr_ulong_ulong_1 = OpTypePointer Function %_arr_ulong_ulong_1
%f = OpFunction %void None %3
%conversion = OpLabel
OpBranch %top
%top = OpLabel
%As = OpVariable %_ptr_Function__arr_ulong_ulong_1 Function
OpReturn
OpFunctionEnd
%12 = OpFunction %void None %3
%13 = OpLabel
%14 = OpFunctionCall %void %f
OpReturn
OpFunctionEnd
```

which fails validation with:

```
error: line 25: All OpVariable instructions in a function must be the first instructions in the first block.
%As = OpVariable %_ptr_Function__arr_ulong_ulong_1 Function
```

Contributor guide

Open the contributing guide

Research direction

Start with the LLVM IR reproducer and the generated SPIR-V shown in the issue, then trace how an alloca in the non-top block is translated. Verify the fix by translating this reproducer and checking that the resulting SPIR-V passes validation without the OpVariable placement error.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.