Skip to content

Implement the sign HLSL Function #70078

Closed
Closed
@llvm-beanz

Description

@llvm-beanz
  • 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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Closed

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions