Skip to content

Implement the distance HLSL Function #99107

@farzonl

Description

@farzonl
  • implement distance in the hlsl_intrinsics.h
  • Implement distance spirv target builtin in clang/include/clang/Basic/BuiltinsSPIRV.td
  • Add a spirv fast path in hlsl_detail.h in the form
#if (__has_builtin(__builtin_spirv_distance))
  return __builtin_spirv_distance(...);
#else
  return ...; // regular behavior
#endif
  • Add codegen for spirv distance builtin to EmitSPIRVBuiltinExpr in CGBuiltin.cpp
  • Add HLSL codegen tests to clang/test/CodeGenHLSL/builtins/distance.hlsl
  • Add SPIRV builtin codegen tests to clang/test/CodeGenSPIRV/Builtins/distance.c
  • Add sema tests to clang/test/SemaHLSL/BuiltIns/distance-errors.hlsl
  • Add spirv sema tests to clang/test/CodeGenSPIRV/Builtins/distance-errors.c
  • Create the int_spv_distance intrinsic in IntrinsicsSPIRV.td
  • In SPIRVInstructionSelector.cpp create the distance lowering and map it to int_spv_distance in SPIRVInstructionSelector::selectIntrinsic.
  • Create SPIR-V backend test case in llvm/test/CodeGen/SPIRV/hlsl-intrinsics/distance.ll
  • Create SPIR-V backend test case in llvm/test/CodeGen/SPIRV/opencl/distance.ll

DirectX

There are no DXIL opcodes found for distance.

SPIR-V

Distance:

Description:

Distance

Result is the distance between p0 and p1, i.e., length(p0 -
p1).

The operands must all 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
the operands.

Number Operand 1 Operand 2 Operand 3 Operand 4

67

<id>
p0

<id>
p1

Test Case(s)

Example 1

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

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

HLSL:

Returns a distance scalar between two vectors.

ret distance(x, y)

Parameters

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

Return Value

A floating-point, scalar value that represents the distance between the x parameter and the y parameter.

Type Description

Name Template Type Component Type Size
x vector float any
y vector float same dimension(s) as input x
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) vs_1_1

See also

Intrinsic Functions (DirectX HLSL)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Closed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions