<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/96513>96513</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[SPIR-V] Creation of UniformConstant kind of variables emits invalid SPIR-V code
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
VyacheslavLevytskyy
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
VyacheslavLevytskyy
</td>
</tr>
</table>
<pre>
Creation of UniformConstant kind of variables may emit invalid SPIR-V code by reusing composite constants of array type with a wrong number of components. The key to understanding the issue is the following minimal reproducer that basically contains the biggest part of addressing the issue:
```
%Vec3 = type { <3 x i8> }
%Vec16 = type { <16 x i8> }
define spir_kernel void @foo(ptr addrspace(1) noundef align 16 %arg) {
%a1 = getelementptr inbounds %Vec3, ptr addrspace(1) %arg, i64 1
call void @llvm.memset.p1.i64(ptr addrspace(1) align 4 %a1, i8 0, i64 3, i1 false)
%a2 = getelementptr inbounds %Vec3, ptr addrspace(1) %arg, i64 1
call void @llvm.memset.p1.i64(ptr addrspace(1) align 4 %a2, i8 1, i64 3, i1 false)
ret void
}
define spir_kernel void @bar(ptr addrspace(1) noundef align 16 %arg) {
%a1 = getelementptr inbounds %Vec16, ptr addrspace(1) %arg, i64 1
call void @llvm.memset.p1.i64(ptr addrspace(1) align 4 %a1, i8 0, i64 16, i1 false)
%a2 = getelementptr inbounds %Vec16, ptr addrspace(1) %arg, i64 1
call void @llvm.memset.p1.i64(ptr addrspace(1) align 4 %a2, i8 1, i64 16, i1 false)
ret void
}
declare void @llvm.memset.p1.i64(ptr addrspace(1) nocapture writeonly, i8, i64, i1 immarg)
```
Here vectors of different length are set with @llvm.memset that is implemented via variables of UniformConstant kinds initialized by an array of the corresponding size. Wrong way of creating constants of array type makes it impossible for SPIRV Backend to distinguish between [1, 1, 1] and [1, 1, 1, 1, 1, ...] (repeated 16 times).
spirv-val catches that as
```
error: line 14146: Initializer type must match the type pointed to by the Result Type
%126 = OpVariable %_ptr_UniformConstant__arr_uchar_uint_16 UniformConstant %80
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVk1v4zYT_jX0ZRBBpD5sHXzYJG_wLlCgxXabHg1KGslTU6RAUna1v74gpWyzidPd9tAuYNAQNXz4zIfmGekc9Rpxz4pbJsTjLJsjOiXPP-B59u40z0wIVtxv5OSPxu6vGGxq0877O4vSk9FgOvhFU2fscGe081J7OJFuw_5ZWpK1QgeDnAEH8kD6LBW18PNP7z_cPEJjWoR6BouTI91DY4bROPIIzQrmApC0Vs7g5xHhQv4IEi7W6B70NNRog0U8qFF7l8DHI8IJZ_AGJt2iDThtQPdHBHJuCmt86IxS5hJeDaRpkAosjta0U4MW_FF6qKWjRio1B0Jekl4O1tT36DyM0vpIsG0tOvfFJSx7x9J7lj6tZbr-lkdRPGKTAcvuF8fY9hZYdpfB70A7lv0P2Pb-uSkvX9ny8opxXFvsSCO4kezhhFajgrOhFliedsYwsRu9jaTdKBtkYseZqECbEK8OpKJeQ7hQFNL24RXb3i7IEDd55NKjR4UDah_gSNfhvIPVNSbu4Oo1T6h3QGUO_Ak3hPkzS6XOQzLg4NAnI0-ozN8ivZDNF1oRdAfpE3gkQRw6qRwyUa13BVvxfbogVhf4V1wAiz5etab9m_NfS_uv5J-X30MBLCyuhO8bS-A_8eJVDbzlxVdqoFHS4j_gok0jRz9ZhIslj0areWG08lnJ0DAs1XG9wcX1_xgYYOONjZ28pa5Di9qDQt2HVm4RHPqlr3_JcunA5ICGcUkQtnAm-UxX3hAfB6TJk1T0CdugL1KvGmK62KAbYy260Sy64OgTJvBrlJTLYtREeYuKdF2HBnlCB0HRgmI5qlWQExuF7RFuZXNC3QYJaskFoIncEWr0F0QNrLiNuV2X4h6kbl_ufrEkSRLMmNhZHFGGSPASPA3omKiS5yEPH_355iwVNNIH8V7iKN1fqBFaayzL3oEKbYPnPC_D0_vPUbSr15PzMATYGMa4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