[Mlir-commits] [mlir] [mlir][spirv] Fix int type declaration duplication when serializing (PR #143108)

Davide Grohmann llvmlistbot at llvm.org
Mon Jun 9 01:51:25 PDT 2025


davidegrohmann wrote:

> Would it be possible to add a test for this?

There is already a test checking that a mix of unsigned/signless ints in the same SPIRV module can be serialized/deserialized to/from MLIR and SPIRV (see mlir/test/Target/SPIRV/constant.mlir). The test pass since the deserializer does not fail on the invalid SPIRV code, validating the spriv assembly from that test fails with the following without the code fix in this patch:
```
error: line 49: Duplicate non-aggregate type declarations are not allowed. Opcode: TypeInt id: 29
  %uint_1 = OpTypeInt 32 0
```
The only way to test this properly is to add a validation step on the produced SPIRV assembly, but that cannot be achieve easily at the moment.

SPIR-V assembly from constants.mlir before the change (marked double declaration by hand):
```
; SPIR-V
; Version: 1.0
; Generator: LLVM MLIR SPIR-V Serializer; 21
; Bound: 209
; Schema: 0
               OpCapability Shader
               OpCapability Int64
               OpCapability Int16
               OpCapability Int8
               OpCapability Float64
               OpCapability Float16
               OpMemoryModel Logical GLSL450
               OpEntryPoint GLCompute %bool_const "bool_const"
               OpName %bool_const "bool_const"
               OpName %i32_const "i32_const"
               OpName %si32_const "si32_const"
               OpName %ui32_const "ui32_const"
               OpName %i64_const "i64_const"
               OpName %i16_const "i16_const"
               OpName %i8_const "i8_const"
               OpName %float_const "float_const"
               OpName %double_const "double_const"
               OpName %half_const "half_const"
               OpName %bool_vector_const "bool_vector_const"
               OpName %int_vector_const "int_vector_const"
               OpName %fp_vector_const "fp_vector_const"
               OpName %ui64_array_const "ui64_array_const"
               OpName %si32_array_const "si32_array_const"
               OpName %float_array_const "float_array_const"
               OpName %ignore_not_used_const "ignore_not_used_const"
               OpName %materialize_const_at_each_use "materialize_const_at_each_use"
               OpName %const_variable "const_variable"
               OpName %multi_dimensions_const "multi_dimensions_const"
               OpName %multi_dimensions_splat_const "multi_dimensions_splat_const"
               OpName %signless_int_const_bit_extension "signless_int_const_bit_extension"
               OpName %signed_int_const_bit_extension "signed_int_const_bit_extension"
               OpDecorate %_arr_uint_uint_3 ArrayStride 4
               OpDecorate %_arr__arr_uint_uint_3_uint_2 ArrayStride 12
               OpDecorate %_arr__arr__arr_uint_uint_3_uint_2_uint_2 ArrayStride 24
       %void = OpTypeVoid
          %1 = OpTypeFunction %void
       %bool = OpTypeBool
       %true = OpConstantTrue %bool
      %false = OpConstantFalse %bool
%_ptr_Function_bool = OpTypePointer Function %bool
       %uint = OpTypeInt 32 0                       <=================
     %uint_0 = OpConstant %uint 0
    %uint_10 = OpConstant %uint 10
%uint_4294967291 = OpConstant %uint 4294967291
        %int = OpTypeInt 32 1
      %int_0 = OpConstant %int 0
     %int_10 = OpConstant %int 10
     %int_n5 = OpConstant %int -5
     %uint_1 = OpTypeInt 32 0                     <=================
   %uint_1_0 = OpConstant %uint_1 0
  %uint_1_10 = OpConstant %uint_1 10
  [...]
```
After the patch:
```
; SPIR-V
; Version: 1.0
; Generator: LLVM MLIR SPIR-V Serializer; 21
; Bound: 206
; Schema: 0
               OpCapability Shader
               OpCapability Int64
               OpCapability Int16
               OpCapability Int8
               OpCapability Float64
               OpCapability Float16
               OpMemoryModel Logical GLSL450
               OpEntryPoint GLCompute %bool_const "bool_const"
               OpName %bool_const "bool_const"
               OpName %i32_const "i32_const"
               OpName %si32_const "si32_const"
               OpName %ui32_const "ui32_const"
               OpName %i64_const "i64_const"
               OpName %i16_const "i16_const"
               OpName %i8_const "i8_const"
               OpName %float_const "float_const"
               OpName %double_const "double_const"
               OpName %half_const "half_const"
               OpName %bool_vector_const "bool_vector_const"
               OpName %int_vector_const "int_vector_const"
               OpName %fp_vector_const "fp_vector_const"
               OpName %ui64_array_const "ui64_array_const"
               OpName %si32_array_const "si32_array_const"
               OpName %float_array_const "float_array_const"
               OpName %ignore_not_used_const "ignore_not_used_const"
               OpName %materialize_const_at_each_use "materialize_const_at_each_use"
               OpName %const_variable "const_variable"
               OpName %multi_dimensions_const "multi_dimensions_const"
               OpName %multi_dimensions_splat_const "multi_dimensions_splat_const"
               OpName %signless_int_const_bit_extension "signless_int_const_bit_extension"
               OpName %signed_int_const_bit_extension "signed_int_const_bit_extension"
               OpDecorate %_arr_uint_uint_3 ArrayStride 4
               OpDecorate %_arr__arr_uint_uint_3_uint_2 ArrayStride 12
               OpDecorate %_arr__arr__arr_uint_uint_3_uint_2_uint_2 ArrayStride 24
       %void = OpTypeVoid
          %1 = OpTypeFunction %void
       %bool = OpTypeBool
       %true = OpConstantTrue %bool
      %false = OpConstantFalse %bool
%_ptr_Function_bool = OpTypePointer Function %bool
       %uint = OpTypeInt 32 0
     %uint_0 = OpConstant %uint 0
    %uint_10 = OpConstant %uint 10
%uint_4294967291 = OpConstant %uint 4294967291
        %int = OpTypeInt 32 1
      %int_0 = OpConstant %int 0
     %int_10 = OpConstant %int 10
     %int_n5 = OpConstant %int -5
   %uint_0_0 = OpConstant %uint 0
  %uint_10_0 = OpConstant %uint 10
%uint_4294967291_0 = OpConstant %uint 4294967291
      %ulong = OpTypeInt 64 0
[...]
```


https://github.com/llvm/llvm-project/pull/143108


More information about the Mlir-commits mailing list