<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/141136>141136</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[DirectX] Legalize dynamic vector indexing
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Icohedron
</td>
</tr>
</table>
<pre>
Dynamic vector indexing (i.e., indexing a vector using a non-constant index) currently becomes scalarized into a series of `select` instructions, with the first `select` containing a poison value that will fail to pass validation. (See #139011)
https://godbolt.org/z/shnW4P1hz
```llvm
define float @_Z4testDv4_fi(<4 x float> %0, i32 %1) local_unnamed_addr #0 !dbg !14 {
%3 = icmp eq i32 %1, 0, !dbg !31
%4 = extractelement <4 x float> %0, i32 0, !dbg !31
%5 = select i1 %3, float %4, float poison, !dbg !31
%6 = icmp eq i32 %1, 1, !dbg !31
%7 = extractelement <4 x float> %0, i32 1, !dbg !31
%8 = select i1 %6, float %7, float %5, !dbg !31
%9 = icmp eq i32 %1, 2, !dbg !31
%10 = extractelement <4 x float> %0, i32 2, !dbg !31
%11 = select i1 %9, float %10, float %8, !dbg !31
%12 = icmp eq i32 %1, 3, !dbg !31
%13 = extractelement <4 x float> %0, i32 3, !dbg !31
%14 = select i1 %12, float %13, float %11, !dbg !31
ret float %14, !dbg !32
}
```
Instead of the above form currently generated by Clang, we should replicate DXC's behavior of transforming the vector into an array and using a getelementptr and load to return the value at the dynamic index.
```llvm
define float @"\01?test@@YAMV?$vector@M$03@@H@Z"(<4 x float> %v, i32 %i) #0 {
%1 = alloca [4 x float], align 4
%2 = extractelement <4 x float> %v, i64 0
%3 = getelementptr inbounds [4 x float], [4 x float]* %1, i32 0, i32 0
store float %2, float* %3, align 4
%4 = extractelement <4 x float> %v, i64 1
%5 = getelementptr inbounds [4 x float], [4 x float]* %1, i32 0, i32 1
store float %4, float* %5, align 4
%6 = extractelement <4 x float> %v, i64 2
%7 = getelementptr inbounds [4 x float], [4 x float]* %1, i32 0, i32 2
store float %6, float* %7, align 4
%8 = extractelement <4 x float> %v, i64 3
%9 = getelementptr inbounds [4 x float], [4 x float]* %1, i32 0, i32 3
store float %8, float* %9, align 4
%10 = getelementptr [4 x float], [4 x float]* %1, i32 0, i32 %i, !dbg !42 ; line:2 col:12
%11 = load float, float* %10, align 4, !dbg !42, !tbaa !43 ; line:2 col:12
ret float %11, !dbg !47 ; line:2 col:5
}
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysll1v2zYUhn8NfXNQg1-ypAtfOPGMFViBAQO2rjcBJR7bHGjSIym3ya8fKDlx5FhFgxUIEFM85-V5-fGQKkazc4hLUtyRYj1TXdr7sPzY-j3q4N2s8fpxuX506mBaOGGbfADjNH4zbgeEV2aOc8LvL9_Uc1QXh6bz7kPrXUzKpSGM8BraLgR0yT5Cg60_YITYKquCeUINxiUPCiIGgxH8FsiCRrTYJrKgYFxMoWuT8S7mob-atIe0R9iaENM4tvUuKeOGSo7eRO_gpGyHkPYqwVdjLWyVsZA8HFWMudNolbXn2d4fiEC4YKKmjBFeE7oidLVP6RiJWBG-IXyz87rxNs192BG-eSJ8E_fuL_k72z8N4WRBhz9rTwdCVxq3xiFsrVcJiKQPX2TCmNYn-bA1hFdE3Ev4NvQT8QsQXtB-jgXPv3MdYH2r7EPnnDqgflBah1wnBcKZbvLKMCaBlHeEriAnCSBiDaY9HAH_faV0D730JU2w5xTZp-C3FFSb0OIBXYLv1TapVPRKw6qAYX09OfY8A7yQl9awSFNKi0kbbCqlfK-NSaXqrY3FyEY5ahVTOvWkCT6Vwuh7XdyUykrsrYt6VDejo2Y1WROf9CEmc8R7fUxLybdGGB87Ge8ydntlA6ZXMfIqhucDXK5fH-PhVH90MaHSGU8ZPqrxJ4StD4dXbNuhw6ASamge4d4qt-uBhRD3vrMaAh6taVVCWH--J7yM0OBenYwPvWxQLmbFjK88xgt_Mx4dqBDUIyinX1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