<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/99134>99134</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Implement the `length` 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 `length` clang builtin,
- [ ] Link `length` clang builtin with `hlsl_intrinsics.h`
- [ ] Add sema checks for `length` to `CheckHLSLBuiltinFunctionCall` in `SemaChecking.cpp`
- [ ] 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 the `DXILOpMapping` of `int_dx_length` to  `24` in `DXIL.td`
- [ ] 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\] <sup>2</sup> +
*x* \[1\] <sup>2</sup> + …).  
  
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*.

<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>66</p></td>
<td
class="tableblock halign-left valign-top"><p><em>&lt;id&gt;</em><br />
<em>x</em></p></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>



## Test Case(s)

 
 ### Example 1
```hlsl
//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                                     |
|--------------------------------------------------------|-------------------------------------------------|
| <span id="x"></span><span id="X"></span>*x*<br/> | The specified floating-point vector.<br/> |



 

## Return Value

A floating-point scalar that represents the length of the *x* 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  |
| *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)                          | yes (vs\_1\_1 only) |



 

## 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/eJzsGdtu2zj2a-gXQoZM2a794AdHjmcDtNOiCbp9C2jpWOKWIjUknct8_eKQsi6x42SnA2wXWMOIxXPIc79R4daKQgGsyOyKzDYjfnClNqs9N39qJUc7nT-vIkpmV5TMNvSmqiVUoBwl81iCKlxJ5jHNJFcF3R2EdEIRlpJ43Z35KNSPC9vpo3Al4ktp5b1QzghlRWbHuHVAaJ3n1ELFaVZC9sPSvTZDuk7jOkXsPz7efrwKDLYHlTmhVcqlxF1C4a5bqLjfKVQxzur6HLNM51CAeo3RdSVcj8_1U206-ulvDfgt4g6ssw1FbxjCtggjbJvqHH4DhTwI2zb2soRtgyRjtNirRrpAF3VviHohb3pEIzBGG3uWdmqAO6CuBCQqlLvPn-47q7S-a0xw0_pyIwxk7vvY5ZdJbr7ffPxcf-J1LVSBJPX-LCenKcLZtLM3Hr1Mv3PhOAQCV3kHu2_0DqhgvEBZyofqxCeEbRulCNu-bSdbP7zLULdfbr5-O6PGjaIedaOsMwcfzrcgIXPaYHzRbMCx4yT1IxihCq9rxWtKhWti4pxYPjFe4UOSNUnW1q9agck8Hp9THYlE3-iOZz9A5d6cNOMW3jCp503YFoMv6kpBF_GyiUn8soSwhB69EGAfUoqBQD_XmF60t_6dV2F9W_IcDP2kc5B9wK3jBViENISi7kPfWJ8Coh4hNg18_jDOP8zHsf8lbEH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