Issue 184922
Summary [HLSL] Implement the `transpose` HLSL Function
Labels backend:DirectX, HLSL
Assignees Icohedron
Reporter Icohedron
    - [ ] Implement `transpose` clang builtin in `Builtins.td`
- [ ] Link `transpose` clang builtin with `hlsl_alias_intrinsics.h`
- [ ] Add sema checks for `transpose` to `CheckHLSLBuiltinFunctionCall` in `SemaHLSL.cpp`
- [ ] Add codegen for `transpose` to `EmitHLSLBuiltinExpr` in `CGHLSLBuiltins.cpp`
  - Should lower to the `llvm.matrix.transpose` intrinsic
- [ ] Add codegen tests to `clang/test/CodeGenHLSL/builtins/transpose.hlsl`
- [ ] Add sema tests to `clang/test/SemaHLSL/BuiltIns/transpose-errors.hlsl`
- [ ] Implement lowering of the `llvm.matrix.transpose` intrinsic in the DXIL backend in `DXILIntrinsicExpansion.cpp`
- [ ] Add DXIL lowering test to `llvm/test/CodeGen/DirectX/matrix-transpose.ll`

## DirectX

There are no DXIL opcodes for `transpose`.

## SPIR-V
SPIR-V has the following opcode, but [the SPIR-V backend currently has an existing lowering of `llvm.matrix.transpose`](https://github.com/llvm/llvm-project/blob/23e4fe040b67e2dd419652830a87093a93ea1a97/llvm/lib/Target/SPIRV/SPIRVCombinerHelper.cpp#L222) which does not utilize it.
The description of the opcode is given below for informational purposes only, and does not need to be used in the implementation.

### [OpTranspose](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpTranspose):

#### Description:

Transpose a matrix.

*Result Type* must be an OpTypeMatrix whose *Column Count* is equal to the *Row Count* of *Matrix*, and whose *Column Type* is a vector whose *Component Count* is equal to the *Column Count* of *Matrix*.

*Matrix* must have type OpTypeMatrix. Its *Column Type* must be a vector whose *Component Type* is a *floating-point type*.

| Word Count | Opcode | Results | Operands |
|---|---|---|---|
| 4 | 84 | *\<id\>* *Result Type* | *Result \<id\>* | *\<id\>* *Matrix* |

**Note:** SPIR-V `OpTranspose` only supports floating-point matrices. Integer matrix transpose must be expanded manually.

## HLSL:

Transposes the specified input matrix.

```
ret transpose(x)
```

### Parameters

| Item | Description |
|---|---|
| *x* | \[in\] The specified matrix. |

### Return Value

The transposed value of the *x* parameter. If the dimensions of the source matrix are *rows* × *columns*, the resulting matrix is *columns* × *rows*.

### Type Description

| Name | Template Type | Component Type | Size |
|---|---|---|---|
| *x* | matrix | float, int, bool | any |
| *ret* | matrix | same as input *x* | rows = columns of *x*, columns = rows of *x* |

### Minimum Shader Model

| Shader Model | Supported |
|---|---|
| [Shader Model 1 (DirectX HLSL)](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-sm1) and higher shader models | yes |

## Test Case(s)

### Example 1
```hlsl
//dxc transpose_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export float2x3 fn(float3x2 m) {
    return transpose(m);
}
```

### Example 2
```hlsl
//dxc transpose_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export int3x4 fn(int4x3 m) {
    return transpose(m);
}
```

### Example 3
```hlsl
//dxc transpose_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export float4x4 fn(float4x4 m) {
    return transpose(m);
}
```

### Example 4
```hlsl
//dxc transpose_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export float1x4 fn(float4x1 m) {
    return transpose(m);
}
```

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to