llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.7k stars 11.87k forks source link

Implement the `step` HLSL Function #99157

Closed farzonl closed 1 month ago

farzonl commented 3 months ago

DirectX

There were no DXIL opcodes found for step.

SPIR-V

Step:

Description:

Step

Result is 0.0 if x \< edge; otherwise result is 1.0.

The operands must all be a scalar or vector whose component type is floating-point.

Result Type and the type of all operands must be the same type. Results are computed per component.

Number Operand 1 Operand 2 Operand 3 Operand 4

48

<id>
edge

<id>
x

Test Case(s)

Example 1

//dxc step_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export float4 fn(float4 p1, float4 p2) {
    return step(p1, p2);
}

HLSL:

Compares two values, returning 0 or 1 based on which value is greater.

ret step(y, x)

Parameters

Item Description
y
[in] The first floating-point value to compare.
x
[in] The second floating-point value to compare.

Return Value

1 if the x parameter is greater than or equal to the y parameter; otherwise, 0.

Remarks

This function uses the following formula: (x >= y) ? 1 : 0. The function returns either 0 or 1 depending on whether the x parameter is greater than the y parameter. To compute a smooth interpolation between 0 and 1, use the smoothstep HLSL intrinsic function.

Type Description

Name Template Type Component Type Size
y scalar, vector, or matrix float any
x same as input y float same dimension(s) as input y
ret same as input y float same dimension(s) as input y

Minimum Shader Model

This function is supported in the following shader models.

Shader Model Supported
Shader Model 2 (DirectX HLSL) and higher shader models yes
Shader Model 1 (DirectX HLSL) yes (vs_1_1 and ps_1_4)

See also

[**Intrinsic Functions (DirectX HLSL)**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md)
farzonl commented 2 months ago

@damyanp Adding refinment priority. This is used in DirectML shader Lighting.fxh

llvmbot commented 1 month ago

@llvm/issue-subscribers-clang-codegen

Author: Farzon Lotfi (farzonl)

- [ ] Implement `step` clang builtin, - [ ] Link `step` clang builtin with `hlsl_intrinsics.h` - [ ] Add sema checks for `step` to `CheckHLSLBuiltinFunctionCall` in `SemaChecking.cpp` - [ ] Add codegen for `step` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp` - [ ] Add codegen tests to `clang/test/CodeGenHLSL/builtins/step.hlsl` - [ ] Add sema tests to `clang/test/SemaHLSL/BuiltIns/step-errors.hlsl` - [ ] Create the `int_spv_step` intrinsic in `IntrinsicsSPIRV.td` - [ ] In SPIRVInstructionSelector.cpp create the `step` lowering and map it to `int_spv_step` in `SPIRVInstructionSelector::selectIntrinsic`. - [ ] Create SPIR-V backend test case in `llvm/test/CodeGen/SPIRV/hlsl-intrinsics/step.ll` ## DirectX There were no DXIL opcodes found for `step`. ## SPIR-V # Step: ## Description: **Step** Result is 0.0 if *x* \< *edge*; otherwise result is 1.0. The operands must all be a scalar or vector whose component type is floating-point. *Result Type* and the type of all operands must be the same type. Results are computed per component. <table> <colgroup> <col style="width: 20%" /> <col style="width: 20%" /> <col style="width: 20%" /> <col style="width: 20%" /> <col style="width: 20%" /> </colgroup> <thead> <tr> <th>Number</th> <th>Operand 1</th> <th>Operand 2</th> <th>Operand 3</th> <th>Operand 4</th> </tr> </thead> <tbody> <tr> <td class="tableblock halign-left valign-top"><p>48</p></td> <td class="tableblock halign-left valign-top"><p><em>&lt;id&gt;</em><br /> <em>edge</em></p></td> <td class="tableblock halign-left valign-top"><p><em>&lt;id&gt;</em><br /> <em>x</em></p></td> <td></td> <td></td> </tr> </tbody> </table> ## Test Case(s) ### Example 1 ```hlsl //dxc step_test.hlsl -T lib_6_8 -enable-16bit-types -O0 export float4 fn(float4 p1, float4 p2) { return step(p1, p2); } ``` ## HLSL: Compares two values, returning 0 or 1 based on which value is greater. | *ret* step(*y*, *x*) | |----------------------| ## Parameters | Item | Description | |--------------------------------------------------------|---------------------------------------------------------------| | <span id="y"></span><span id="Y"></span>*y*<br/> | \[in\] The first floating-point value to compare.<br/> | | <span id="x"></span><span id="X"></span>*x*<br/> | \[in\] The second floating-point value to compare.<br/> | ## Return Value 1 if the *x* parameter is greater than or equal to the *y* parameter; otherwise, 0. ## Remarks This function uses the following formula: (*x* >= *y*) ? 1 : 0. The function returns either 0 or 1 depending on whether the *x* parameter is greater than the *y* parameter. To compute a smooth interpolation between 0 and 1, use the [**smoothstep**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-smoothstep.md) HLSL intrinsic function. ## Type Description | Name | [**Template Type**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md) | [**Component Type**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md) | Size | |-------|----------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------|--------------------------------| | *y* | [**scalar**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md), **vector**, or **matrix** | [**float**](/windows/desktop/WinProg/windows-data-types) | any | | *x* | same as input *y* | [**float**](/windows/desktop/WinProg/windows-data-types) | same dimension(s) as input *y* | | *ret* | same as input *y* | [**float**](/windows/desktop/WinProg/windows-data-types) | same dimension(s) as input *y* | ## Minimum Shader Model This function is supported in the following shader models. | Shader Model | Supported | |------------------------------------------------------------------------------------|-----------------------------| | [Shader Model 2 (DirectX HLSL)](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-sm2.md) and higher shader models | yes | | [Shader Model 1 (DirectX HLSL)](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-sm1.md) | yes (vs\_1\_1 and ps\_1\_4) | ## See also <dl> <dt> [**Intrinsic Functions (DirectX HLSL)**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md) </dt> </dl>
llvmbot commented 1 month ago

@llvm/issue-subscribers-clang-frontend

Author: Farzon Lotfi (farzonl)

- [ ] Implement `step` clang builtin, - [ ] Link `step` clang builtin with `hlsl_intrinsics.h` - [ ] Add sema checks for `step` to `CheckHLSLBuiltinFunctionCall` in `SemaChecking.cpp` - [ ] Add codegen for `step` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp` - [ ] Add codegen tests to `clang/test/CodeGenHLSL/builtins/step.hlsl` - [ ] Add sema tests to `clang/test/SemaHLSL/BuiltIns/step-errors.hlsl` - [ ] Create the `int_spv_step` intrinsic in `IntrinsicsSPIRV.td` - [ ] In SPIRVInstructionSelector.cpp create the `step` lowering and map it to `int_spv_step` in `SPIRVInstructionSelector::selectIntrinsic`. - [ ] Create SPIR-V backend test case in `llvm/test/CodeGen/SPIRV/hlsl-intrinsics/step.ll` ## DirectX There were no DXIL opcodes found for `step`. ## SPIR-V # Step: ## Description: **Step** Result is 0.0 if *x* \< *edge*; otherwise result is 1.0. The operands must all be a scalar or vector whose component type is floating-point. *Result Type* and the type of all operands must be the same type. Results are computed per component. <table> <colgroup> <col style="width: 20%" /> <col style="width: 20%" /> <col style="width: 20%" /> <col style="width: 20%" /> <col style="width: 20%" /> </colgroup> <thead> <tr> <th>Number</th> <th>Operand 1</th> <th>Operand 2</th> <th>Operand 3</th> <th>Operand 4</th> </tr> </thead> <tbody> <tr> <td class="tableblock halign-left valign-top"><p>48</p></td> <td class="tableblock halign-left valign-top"><p><em>&lt;id&gt;</em><br /> <em>edge</em></p></td> <td class="tableblock halign-left valign-top"><p><em>&lt;id&gt;</em><br /> <em>x</em></p></td> <td></td> <td></td> </tr> </tbody> </table> ## Test Case(s) ### Example 1 ```hlsl //dxc step_test.hlsl -T lib_6_8 -enable-16bit-types -O0 export float4 fn(float4 p1, float4 p2) { return step(p1, p2); } ``` ## HLSL: Compares two values, returning 0 or 1 based on which value is greater. | *ret* step(*y*, *x*) | |----------------------| ## Parameters | Item | Description | |--------------------------------------------------------|---------------------------------------------------------------| | <span id="y"></span><span id="Y"></span>*y*<br/> | \[in\] The first floating-point value to compare.<br/> | | <span id="x"></span><span id="X"></span>*x*<br/> | \[in\] The second floating-point value to compare.<br/> | ## Return Value 1 if the *x* parameter is greater than or equal to the *y* parameter; otherwise, 0. ## Remarks This function uses the following formula: (*x* >= *y*) ? 1 : 0. The function returns either 0 or 1 depending on whether the *x* parameter is greater than the *y* parameter. To compute a smooth interpolation between 0 and 1, use the [**smoothstep**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-smoothstep.md) HLSL intrinsic function. ## Type Description | Name | [**Template Type**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md) | [**Component Type**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md) | Size | |-------|----------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------|--------------------------------| | *y* | [**scalar**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md), **vector**, or **matrix** | [**float**](/windows/desktop/WinProg/windows-data-types) | any | | *x* | same as input *y* | [**float**](/windows/desktop/WinProg/windows-data-types) | same dimension(s) as input *y* | | *ret* | same as input *y* | [**float**](/windows/desktop/WinProg/windows-data-types) | same dimension(s) as input *y* | ## Minimum Shader Model This function is supported in the following shader models. | Shader Model | Supported | |------------------------------------------------------------------------------------|-----------------------------| | [Shader Model 2 (DirectX HLSL)](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-sm2.md) and higher shader models | yes | | [Shader Model 1 (DirectX HLSL)](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-sm1.md) | yes (vs\_1\_1 and ps\_1\_4) | ## See also <dl> <dt> [**Intrinsic Functions (DirectX HLSL)**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md) </dt> </dl>