Skip to content

Implement the sign HLSL Function #70078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
9 tasks
Tracked by #99235 ...
llvm-beanz opened this issue Oct 24, 2023 · 0 comments · Fixed by #101987, #101989 or #108396
Closed
9 tasks
Tracked by #99235 ...

Implement the sign HLSL Function #70078

llvm-beanz opened this issue Oct 24, 2023 · 0 comments · Fixed by #101987, #101989 or #108396
Labels
backend:SPIR-V HLSL HLSL Language Support

Comments

@llvm-beanz
Copy link
Collaborator

llvm-beanz commented Oct 24, 2023

  • Implement sign clang builtin,
  • Link sign clang builtin with hlsl_intrinsics.h
  • Add sema checks for sign to CheckHLSLBuiltinFunctionCall in SemaChecking.cpp
  • Add codegen for sign to EmitHLSLBuiltinExpr in CGBuiltin.cpp
  • Add codegen tests to clang/test/CodeGenHLSL/builtins/sign.hlsl
  • Add sema tests to clang/test/SemaHLSL/BuiltIns/sign-errors.hlsl
  • Create the int_spv_sign intrinsic in IntrinsicsSPIRV.td
  • In SPIRVInstructionSelector.cpp create the sign lowering and map it to int_spv_sign in SPIRVInstructionSelector::selectIntrinsic.
  • Create SPIR-V backend test case in llvm/test/CodeGen/SPIRV/hlsl-intrinsics/sign.ll

DirectX

There were no DXIL opcodes found for sign.

SPIR-V

FSign:

Description:

FSign

Result is 1.0 if x > 0, 0.0 if x = 0, or -1.0 if x < 0.

The operand x must be a scalar or vector whose component type is
floating-point.

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

Number Operand 1 Operand 2 Operand 3 Operand 4

6

<id>
x

Test Case(s)

Example 1

//dxc sign_test.hlsl -T lib_6_8  -enable-16bit-types -spirv -fspv-target-env=universal1.5 -fcgl -O0

export int4 fn(float4 p1) {
    return sign(p1);
}

Example 2

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

export int4 fn(uint4 p1) {
    return sign(p1);
}

Example 3

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

export int4 fn(int4 p1) {
    return sign(p1);
}

HLSL:

Returns the sign of x.

ret sign(x)

Parameters

Item Description
x
[in] The input value.

Return Value

Returns -1 if x is less than zero; 0 if x equals zero; and 1 if x is greater than zero.

Type Description

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

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)

@llvm-beanz llvm-beanz converted this from a draft issue Oct 24, 2023
@llvm-beanz llvm-beanz added the HLSL HLSL Language Support label Oct 24, 2023
@farzonl farzonl changed the title [HLSL] implement sign intrinsic Implement the sign HLSL Function Jul 16, 2024
@damyanp damyanp moved this to Active in HLSL Support Aug 29, 2024
farzonl pushed a commit that referenced this issue Sep 5, 2024
makes progress on #70078

### Changes
- Added `int_dx_sign` intrinsic in `IntrinsicsDirectX.td`
- Added expansion for `int_dx_sign in `DXILIntrinsicExpansion.cpp`
- Added DXIL backend test case

### Related PRs
- #101987
- #101989
farzonl pushed a commit that referenced this issue Sep 9, 2024
partially fixes #70078

### Changes
- Added `int_spv_sign` intrinsic in `IntrinsicsSPIRV.td`
- Added lowering and map to `int_spv_sign in
`SPIRVInstructionSelector.cpp`
- Added SPIR-V backend test case in
`llvm/test/CodeGen/SPIRV/hlsl-intrinsics/sign.ll`

### Related PRs
- #101988
- #101989
@github-project-automation github-project-automation bot moved this from Active to Done in HLSL Support Sep 9, 2024
farzonl pushed a commit that referenced this issue Sep 10, 2024
partially fixes #70078

### Changes
- Implemented `sign` clang builtin
- Linked `sign` clang builtin with `hlsl_intrinsics.h`
- Added sema checks for `sign` to `CheckHLSLBuiltinFunctionCall` in
`SemaChecking.cpp`
- Add codegen for `sign` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp`
- Add codegen tests to `clang/test/CodeGenHLSL/builtins/sign.hlsl`
- Add sema tests to `clang/test/SemaHLSL/BuiltIns/sign-errors.hlsl`

### Related PRs
- #101987
- #101988

### Discussion
- Should there be a `usign` intrinsic that handles the unsigned cases?
farzonl pushed a commit that referenced this issue Oct 10, 2024
- Add handling for unsigned integers to hlsl_elementwise_sign
- Use `select` instead of adding dx and spirv intrinsics for unsigned
integers as [discussed previously
](#101988 (comment))

fixes #70078

### Related PRs
- #101987
- #101988
- #101989

cc @farzonl @pow2clk @bob80905 @bogner @llvm-beanz
DanielCChen pushed a commit to DanielCChen/llvm-project that referenced this issue Oct 16, 2024
- Add handling for unsigned integers to hlsl_elementwise_sign
- Use `select` instead of adding dx and spirv intrinsics for unsigned
integers as [discussed previously
](llvm#101988 (comment))

fixes llvm#70078

### Related PRs
- llvm#101987
- llvm#101988
- llvm#101989

cc @farzonl @pow2clk @bob80905 @bogner @llvm-beanz
@damyanp damyanp moved this to Closed in HLSL Support Apr 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:SPIR-V HLSL HLSL Language Support
Projects
Status: Closed
2 participants