Issue 233623
Summary [SPIR-V] Invalid pointee types for pointer members of constant aggregates (call arguments / return values)
Labels new issue
Assignees
Reporter jporra
    ### Description

A constant aggregate used as a call argument (or returned as a value) that contains an `addrspacecast` of a global whose pointee type is not `i8` lowers to invalid SPIR-V. The `ptr` member of the aggregate is lowered as `i8*` (untyped pointer member), while the pointer value keeps its deduced pointee type (`[2 x i8]` here); nothing reconciles the two.

### Reproducer

```llvm
@s = internal addrspace(1) constant [2 x i8] c" \00", align 16

define spir_func void @callee({ ptr, i64 } %arg) {
  ret void
}

define spir_func void @caller() {
  call void @callee({ ptr, i64 } { ptr addrspacecast (ptr addrspace(1) @s to ptr), i64 1 })
  ret void
}
```

```console
$ clang -target spirv64 -c -O0 test.ll -o test.spv && spirv-val test.spv
error: line 35: Expected input and Result Type to point to the same type: SpecConstantOp
  %19 = OpSpecConstantOp %_ptr_Function_uchar GenericCastToPtr %18
```

Relevant output from `clang -target spirv64 -S -O0`:

```spirv
%5  = OpTypePointer Function %4              ; i8*, the lowered struct member type
%7  = OpTypeStruct %5 %6                     ; { ptr, i64 }
%10 = OpTypeArray %4 %9                      ; [2 x i8]
%13 = OpTypePointer Generic %10              ; [2 x i8]*
%17 = OpVariable %11 CrossWorkgroup %16      ; @s
%18 = OpSpecConstantOp %13 PtrCastToGeneric %17    ; OK: ... [2 x i8]* -> Generic [2 x i8]*
%19 = OpSpecConstantOp %5 GenericCastToPtr %18     ; invalid: input points to [2 x i8], result to i8
%20 = OpSpecConstantComposite %7 %19 %12
%23 = OpFunctionCall %2 %25 %20
```

Returning the same constant reproduces as well. At `-O2` the trivial callee gets inlined and the call (with it) disappears, masking the bug; `-O0` reproduces reliably. Verified on clang 23.1.1 (6dfe1677ab8dffbc6ec13d53a1e0215d75147689); the relevant code on current main is unchanged.

### Second manifestation

When the composite is materialized as an instruction rather than a module-scope constant — which is what our frontend pipeline (Mojo/MAX kernels, string literals passed in `{ ptr, i64 }` spans) produces — the cast chain instead stays internally consistent at `[2 x i8]` and the composite becomes the invalid instruction:

```spirv
%_struct_17 = OpTypeStruct %_ptr_Function_uchar %ulong    ; member: i8*
%2466 = OpPtrCastToGeneric %_ptr_Generic__arr_uchar_uint_2 %102
%2467 = OpGenericCastToPtr %_ptr_Function__arr_uchar_uint_2 %2466   ; consistent: [2 x i8]
%2482 = OpCompositeConstruct %_struct_17 %2467 %ulong_1             ; [2 x i8]* vs member i8*
```

```
error: line 3147: Expected Constituent type to be equal to the corresponding member type of Result Type struct
  %2482 = OpCompositeConstruct %_struct_17 %2467 %ulong_1
```

Intel's IGC crashes outright while translating such modules.

### Analysis

Same root cause, two paths:

1. **Spec-constant path** — `isASCastInGVar()` in `SPIRVInstructionSelector.cpp` selects the branch whose existing TODO admits the problem ("OpConstantComposite expects i8*, so we are forced to forget a correct value of ResType and use general i8* instead. Maybe this should be addressed in the emit-intrinsic step..."): the final cast result is forced to `i8*`, but the intermediate Generic pointer built by `buildConstGenericPtr()` preserves the source pointee type, so the two halves of the cast chain disagree (`PtrCastToGeneric`/`GenericCastToPtr` require matching pointee types).

2. **Instruction path** — `isASCastInGVar()` only recognizes `OpConstantComposite`/`OpSpecConstantComposite`/`OpVariable`, not `OpCompositeConstruct`, so the cast takes the regular path, keeps the deduced `[2 x i8]`, and the constituent then doesn't match the `i8*` member of the composite.

#227599 fixes pointer-member pointee mismatches for `insertvalue`/`extractvalue`, but a constant aggregate passed directly as a call argument or returned as a value never goes through those instructions, so it remains invalid.

### Expected

Valid SPIR-V: cast the pointer consistently to the member's pointer type along the whole chain (e.g. an `OpSpecConstantOp Bitcast` after the generic round-trip, or an `OpBitcast` before `OpCompositeConstruct`), or deduce the member type from the constant — and make both materialization paths agree.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to