KhronosGroup / KhronosGroup/SPIRV-LLVM-Translator

SPIRV validation error with global variables

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

Description

I ran into another issue trying to switch from optimized LLVM IR to using `spirv-opt`:

```llvm
target triple = "spir64-unknown-unknown"
@exception = private unnamed_addr constant [1 x i32] [i32 42]
```

This generates the following SPIRV (as disassembled by `spirv-dis`):

```
; SPIR-V
; Version: 1.0
; Generator: Khronos LLVM/SPIR-V Translator; 14
; Bound: 10
; Schema: 0
OpCapability Addresses
OpCapability Kernel
OpCapability Int64
%1 = OpExtInstImport "OpenCL.std"
OpMemoryModel Physical64 OpenCL
OpSource Unknown 0
OpName %exception "exception"
OpDecorate %exception Constant
%uint = OpTypeInt 32 0
%ulong = OpTypeInt 64 0
%uint_42 = OpConstant %uint 42
%ulong_1 = OpConstant %ulong 1
%_arr_uint_ulong_1 = OpTypeArray %uint %ulong_1
%_ptr_Function__arr_uint_ulong_1 = OpTypePointer Function %_arr_uint_ulong_1
%7 = OpConstantComposite %_arr_uint_ulong_1 %uint_42
%exception = OpVariable %_ptr_Function__arr_uint_ulong_1 Function %7
```

... which fails to validate or optimize:

```
error: line 16: Variables can not have a function[7] storage class outside of a function
%exception = OpVariable %_ptr_Function__arr_uint_ulong_1 Function %7
```

Before, I was optimizing in LLVM and passing the SPIRV binaries generated by `llvm-spirv` directly to the Intel driver, containing the same kind of global variables, without any issue. It's only now that I'm switching to `spirv-opt`, which performs validation, that I'm running into this issue.

Contributor guide

Open the contributing guide

Research direction

Use the provided LLVM IR snippet as the minimal reproducer and inspect the SPIR-V emitted by llvm-spirv, focusing on the global variable's storage class. Run spirv-opt or the SPIR-V validator against the generated module; done means the same input produces SPIR-V that validates and can be optimized without the reported error.

Written by the indexing model from the issue text.

Assessment

Domain
compilers, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.