Issue 141136
Summary [DirectX] Legalize dynamic vector indexing
Labels new issue
Assignees
Reporter Icohedron
    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
}
```


_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to