| Issue |
239342
|
| Summary |
[SPIR-V] OpPhi/OpSelect gets a null pointer operand of the old pointee type
|
| Labels |
backend:SPIR-V
|
| Assignees |
|
| Reporter |
maleadt
|
Assisted by: Opus 5.5 and Sol 6.1
The SPIR-V back-end emits an `OpPhi` of float pointers with the `i8` `OpConstantNull` as one incoming value, which spirv-val rejects. LLVM 22.1.8 compiles this correctly; 23.1.1, 23.1.4 and main don't.
```llvm
target triple = "spirv64-unknown-unknown"
define spir_kernel void @k(i1 %c, ptr %p) {
entry:
br label %header
header:
%a = phi ptr [ null, %entry ], [ null, %body ], [ %b, %latch ]
br i1 %c, label %cond, label %latch
cond:
br i1 %c, label %latch, label %body
body:
%v = load float, ptr %a, align 4
br i1 %c, label %exit, label %header
latch:
%b = phi ptr [ null, %cond ], [ %p, %header ]
%dead = icmp eq i64 0, 0
br label %header
exit:
store float %v, ptr addrspace(3) null
ret void
uselistorder label %header, { 1, 0, 2 }
}
```
```
$ llc -O0 -filetype=obj phi.ll -o phi.spv && spirv-val phi.spv
error: line 50: OpPhi's result type <id> '11[%_ptr_Function_float]' does not match incoming value <id> '19[%19]' type <id> '17[%_ptr_Function_uchar]'.
%29 = OpPhi %_ptr_Function_float %19 %cond %p %header
```
The same pattern with a `select` in place of `%b` trips an assertion in an assertions build:
```
SPIRVInstructionSelector.cpp: bool SPIRVInstructionSelector::selectSelect(...): Assertion `ResType == GR.getSPIRVTypeForVReg(SelectFirstArg) && ResType == GR.getSPIRVTypeForVReg(SelectSecondArg)' failed.
```
The cause is in `SPIRVEmitIntrinsics`. LoopSimplify adds `%header.backedge` with `%a.be = phi [null, %body], [%b, %latch]`. Then:
1. The forward pass gives `%b` the `i8` fallback type.
2. The backward pass validates `%b`'s operands against `i8`, so its null needs no cast.
3. The pass over the PHIs reaches `%a.be`, which is typed `float` through `%a` and the load. It retypes its operand `%b` to `float` with `updateAssignType` and `propagateElemTypeRec`. That only updates the users of `%b`. The operands of `%b` were validated against `i8` and aren't checked again.
4. Postprocessing revisits `%b` only for `%p`. The null constant has no use list, so it never comes up again and keeps the `i8` type.
The bug predates 4a773b9f35fc (#187519). That commit's parent compiles the reproducer above, but fails on the same IR after `opt -passes=loop-simplify`. #187519 started running LoopSimplify for non-shader targets, which made the trigger common. The `uselistorder` line matters because the predecessor order of `%header` decides the layout of the blocks LoopSimplify creates, and therefore the order in which the PHIs are visited.
This was found through Julia's OpenCL back-end (KernelAbstractions on POCL), for reductions with `Union{Missing,Float32}` accumulators.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs