<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/99107>99107</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Implement the `distance` HLSL Function
</td>
</tr>
<tr>
<th>Labels</th>
<td>
metabug,
backend:DirectX,
HLSL,
backend:SPIR-V,
bot:HLSL
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
farzonl
</td>
</tr>
</table>
<pre>
- [ ] Implement `distance` clang builtin,
- [ ] Link `distance` clang builtin with `hlsl_intrinsics.h`
- [ ] Add sema checks for `distance` to `CheckHLSLBuiltinFunctionCall` in `SemaChecking.cpp`
- [ ] Add codegen for `distance` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp`
- [ ] Add codegen tests to `clang/test/CodeGenHLSL/builtins/distance.hlsl`
- [ ] Add sema tests to `clang/test/SemaHLSL/BuiltIns/distance-errors.hlsl`
- [ ] Create the `int_dx_distance` intrinsic in `IntrinsicsDirectX.td`
- [ ] Create the `DXILOpMapping` of `int_dx_distance` to `24` in `DXIL.td`
- [ ] Create the `distance.ll` and `distance_errors.ll` tests in `llvm/test/CodeGen/DirectX/`
- [ ] 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`
## DirectX
| DXIL Opcode | DXIL OpName | Shader Model | Shader Stages |
| ----------- | ----------- | ------------ | ------------- |
| 24 | Sqrt | 6.0 | () |
## 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.
<table>
<colgroup>
<col style="width: 20%" />
<col style="width: 20%" />
<col style="width: 20%" />
<col style="width: 20%" />
<col style="width: 20%" />
</colgroup>
<thead>
<tr>
<th>Number</th>
<th>Operand 1</th>
<th>Operand 2</th>
<th>Operand 3</th>
<th>Operand 4</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p>67</p></td>
<td
class="tableblock halign-left valign-top"><p><em><id></em><br />
<em>p0</em></p></td>
<td
class="tableblock halign-left valign-top"><p><em><id></em><br />
<em>p1</em></p></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
## Test Case(s)
### Example 1
```hlsl
//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 |
|--------------------------------------------------------|----------------------------------------------------------------|
| <span id="x"></span><span id="X"></span>*x*<br/> | \[in\] The first floating-point vector to compare.<br/> |
| <span id="y"></span><span id="Y"></span>*y*<br/> | \[in\] The second floating-point vector to compare.<br/> |
## Return Value
A floating-point, scalar value that represents the distance between the *x* parameter and the *y* parameter.
## Type Description
| Name | [**Template Type**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md) | [**Component Type**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md) | Size |
|-------|-------------------------------------------------------------------------------------|----------------------------------------------------------------|--------------------------------|
| *x* | [**vector**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md) | [**float**](/windows/desktop/WinProg/windows-data-types) | any |
| *y* | [**vector**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md) | [**float**](/windows/desktop/WinProg/windows-data-types) | same dimension(s) as input *x* |
| *ret* | [**scalar**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md) | [**float**](/windows/desktop/WinProg/windows-data-types) | 1 |
## Minimum Shader Model
This function is supported in the following shader models.
| Shader Model | Supported |
|------------------------------------------------------------------------------------|-----------|
| [Shader Model 2 (DirectX HLSL)](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-sm2.md) and higher shader models | yes |
| [Shader Model 1 (DirectX HLSL)](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-sm1.md) | vs\_1\_1 |
## See also
<dl> <dt>
[**Intrinsic Functions (DirectX HLSL)**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md)
</dt> </dl>
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsGV1v47jx19AvhAyZ8kf84AdHjq8Bdm8Pm2C7fQpoaSyxoUiVpJP4fn0xpCxLseOkvaJN0RpOJHKG88X5Is2tFYUCWJDJNZmsBnznSm0WW25-10oONjrfLyJKJteUTFb0tqolVKAcJdM4F9ZxlQGZxjSTXBV0sxPSCUVYSuLlcdUXoR4vLqDPwpWIUUorH4RyRigrMjssyTTukVrmObVQcZqVkD1autXmNWWncSZF-J--3H25DizWO5U5oVXKpUQsoRDrDiruMYUqhlldn2OX6RwKUG-zuqmE63C6eanNkUP6SzP9HnkH1tmGojcOYWucI2yd6hx-AYU8CFs3NrOErQ-yDNFub5rqAmXUvyHrxbztkY3AGG3sWeqpAe6AuhKQrFDuIX956Nqm3cXGELftrq6Egcz9HLr8MtHVz9sv3-qvvK6FKpCk3r7By2mKEDY-2h0Xv8uhtV_wCa7y7uxDo34ABisG4lI-VSfbQ9i60Yyw9fvmsvXTB-1199vt9x9ndLlV1INulXVm5737DiRkTht0Npqd1RV5Sf0MRqjCa1zxmlLhGgc5L5qPlTd4kWRJkqX1o1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