Skip to content

Implement the normalize HLSL Function #99139

Description

@farzonl
  • Implement normalize clang builtin,
  • Link normalize clang builtin with hlsl_intrinsics.h
  • Add sema checks for normalize to CheckHLSLBuiltinFunctionCall in SemaChecking.cpp
  • Add codegen for normalize to EmitHLSLBuiltinExpr in CGBuiltin.cpp
  • Add codegen tests to clang/test/CodeGenHLSL/builtins/normalize.hlsl
  • Add sema tests to clang/test/SemaHLSL/BuiltIns/normalize-errors.hlsl
  • Create the int_dx_normalize intrinsic in IntrinsicsDirectX.td
  • Create an intrinsic expansion int_dx_normalize in llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
  • Create the normalize.ll and normalize_errors.ll tests in llvm/test/CodeGen/DirectX/
  • Create the int_spv_normalize intrinsic in IntrinsicsSPIRV.td
  • In SPIRVInstructionSelector.cpp create the normalize lowering and map it to int_spv_normalize in SPIRVInstructionSelector::selectIntrinsic.
  • Create SPIR-V backend test case in llvm/test/CodeGen/SPIRV/hlsl-intrinsics/normalize.ll

DirectX

DXIL Opcode DXIL OpName Shader Model Shader Stages
56 Dot4 6.0 ()

SPIR-V

Normalize:

Description:

Normalize

Result is the vector in the same direction as x but with a length of
1.

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.

Number Operand 1 Operand 2 Operand 3 Operand 4

69

<id>
x

Test Case(s)

Example 1

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

export float4 fn(float4 p1) {
    return normalize(p1);
}

HLSL:

Normalizes the specified floating-point vector according to x / length(x).

ret normalize(x)

Parameters

Item Description
x
[in] The specified floating-point vector.

Return Value

The normalized x parameter. If the length of the x parameter is 0, the result is indefinite.

Remarks

The normalize HLSL intrinsic function uses the following formula: x / length(x).

Type Description

Name Template Type Component Type Size
x vector float any
ret same as input x float 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 only)

See also

Intrinsic Functions (DirectX HLSL)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

HLSLHLSL Language Supportbackend:DirectXbackend:SPIR-Vbot:HLSLclang:codegenIR generation bugs: mangling, exceptions, etc.metaissueIssue to collect references to a group of similar or related issues.

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions