KhronosGroup / KhronosGroup/SPIRV-LLVM-Translator
Variable not in the first block in the function
- Dominant language
- LLVM
- Stars
- 625
- Forks
- 279
- Avg merge
- 3d 5h
- Merged PRs (30d)
- 34
Description
In MLIR or LLVMIR, allocas are not required to be in the beginning of a function. While in SPIR-V, according to https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#_logical_layout_of_a_module, "All OpVariable instructions in a function must be in the first block in the function."
We expects the translator to move all allocas to the first block of a function.
We noticed incorrect result when there exist alloca not in the first block in the function.
Reproducer: [reproducer.zip](https://github.com/KhronosGroup/SPIRV-LLVM-Translator/files/10970669/reproducer.zip)
In the reproducer, there are 2 LLVM IR files (`good.ll` and `bad.ll`), the only difference is the location of one alloca instruction.
We can see from `bad.spt` (created from `bad.ll`) at line 1279 that `4 Variable 249 250 7` is placed not in the first block in the function.
We can see from below that using `good.ll` in the `script`, it generates the expected result. While using `bad.ll` in the `script`, it hits an assertion.
```
bash-4.4$ ./script good
Test passed.
bash-4.4$ ./script bad
linear-sub_group.cpp.tmp.out: linear-sub_group.cpp:51: int main(int, char **): Assertion `output[idx] == idx' failed.
./script: line 42: 3238678 Aborted (core dumped) "env" "ONEAPI_DEVICE_SELECTOR=opencl:cpu" "./linear-sub_group.cpp.tmp.out"
```
Contributor guide
Research direction
Start by examining the supplied good.ll and bad.ll files and running the script with each reproducer. Compare the generated bad.spt around line 1279 and the assertion output; done means the alloca-derived OpVariable is placed in the function's first block and the bad case no longer produces the incorrect result.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100