Issue 177721
Summary [HLSL][Matrix] Initialization of a vector with a matrix is influenced by matrix memory layout
Labels clang:codegen, HLSL
Assignees
Reporter Icohedron
    Initialization of a vector with a matrix is influenced by matrix memory layout in Clang. However, in DXC this is not the case.

Library shader example: https://hlsl.godbolt.org/z/8W31vqM96
Compute shader example: https://hlsl.godbolt.org/z/r9Wboor8a

Given the following library shader example for simplicity:
```hlsl
export float4 fn3(float2x2 m) {
    float4 v = {m};
    return v;
}
```

Regardless of `-Zpc` or `-Zpr`, DXC emits the following IR:
```llvm
%class.matrix.float.2.2 = type { [2 x <2 x float>] }

define <4 x float> @"\01?fn3@@YA?AV?$vector@M$03@@V?$matrix@M$01$01@@@Z"(%class.matrix.float.2.2 %m) #0 {
  %1 = alloca <4 x float>, align 8
  %2 = bitcast <4 x float>* %1 to %class.matrix.float.2.2*
  store %class.matrix.float.2.2 %m, %class.matrix.float.2.2* %2, align 8
  %m1 = load <4 x float>, <4 x float>* %1, align 8
  ret <4 x float> %m1, !dbg !49
}
```

While clang with `-Xclang -fmatrix-memory-layout=column-major` emits
```llvm
define <4 x float> @_Z3fn3u11matrix_typeILm2ELm2EfE(<4 x float> returned %m) local_unnamed_addr #0 !dbg !12 {
  ret <4 x float> %m, !dbg !33
}
```

and with `-Xclang -fmatrix-memory-layout=row-major` clang emits
```llvm
define <4 x float> @_Z3fn3u11matrix_typeILm2ELm2EfE(<4 x float> %m) local_unnamed_addr #0 !dbg !12 {
  %m.i010 = extractelement <4 x float> %m, i32 0, !dbg !32
  %m.i211 = extractelement <4 x float> %m, i32 2, !dbg !32
  %m.i112 = extractelement <4 x float> %m, i32 1, !dbg !32
 %m.i313 = extractelement <4 x float> %m, i32 3, !dbg !32
  %vecinit6.upto014 = insertelement <4 x float> poison, float %m.i010, i32 0, !dbg !32
 %vecinit6.upto115 = insertelement <4 x float> %vecinit6.upto014, float %m.i211, i32 1, !dbg !32
  %vecinit6.upto216 = insertelement <4 x float> %vecinit6.upto115, float %m.i112, i32 2, !dbg !32
  %vecinit617 = insertelement <4 x float> %vecinit6.upto216, float %m.i313, i32 3, !dbg !32
 ret <4 x float> %vecinit617, !dbg !34
}
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to