Skip to content

Implement the length HLSL Function #99134

@farzonl

Description

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

DirectX

DXIL Opcode DXIL OpName Shader Model Shader Stages
24 Sqrt 6.0 ()

SPIR-V

Length:

Description:

Length

Result is the length of vector x, i.e., sqrt(x [0] 2 +
x [1] 2 + …).

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

Result Type must be a scalar of the same type as the component type of
x.

Number Operand 1 Operand 2 Operand 3 Operand 4

66

<id>
x

Test Case(s)

Example 1

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

export float fn(float4 p1) {
    return length(p1);
}

HLSL:

Returns the length of the specified floating-point vector.

ret length(x)

Parameters

Item Description
x
The specified floating-point vector.

Return Value

A floating-point scalar that represents the length of the x parameter.

Type Description

Name Template Type Component Type Size
x vector float any
ret scalar float 1

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)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions