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

    <tr>
        <th>Summary</th>
        <td>
            Implement the `normalize` 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 `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 the `DXILOpMapping` of `int_dx_normalize` to  `56` in `DXIL.td`
- [ ] 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.

<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>69</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 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 |
|--------------------------------------------------------|--------------------------------------------------------|
| <span id="x"></span><span id="X"></span>*x*<br/> | \[in\] The specified floating-point vector.<br/> |



 

## 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**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-length.md)(*x*).

## 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* | same as input *x* | [**float**](/windows/desktop/WinProg/windows-data-types) | 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)](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/eJzUGV1z2jr214gXjRkjgwMPPBAT7mamve00mW7fMsIWtjay5JHkhPTX7xzJn4HAzu3s3i5Dg6VzdL6_5FJjeC4ZW6PFLVpsJ7S2hdLrA9U_lRSTvcre1gFGi1uMFlt8X1aClUxajOJQKl1SwX8yFIc4FVTmeF9zYblEJEHhpj_2icvnyyfwK7cFoBTCiCcurebS8NRMCxSHI1qbLMOGlRSnBUufDT4ofULaKthKAOEfnx4-3Xoeu1qmliuZUCEAi0vAemAldZhc5tO0qs7xS1XGciYv8LoruR2wujtWumeR_NFsX6NvmbGmoejMg8gO9hDZJSpjfzAJPBDZNVYziOw6YaZgug-tdYE0WKCh6-S8H9MNmNZKm7PkE82oZdgWDOhyaZ-y49PIPJ0rG1vcd67dcs1S-2Nqs8tUtz_uP32pPtOq4jIHkurwETOrMIAWcW97OH2VRW9DHxlUZqPtp8YEHupN6ckL8VKeOAmRXaMcIrvrJjPVy39qs4ev99--n1HnXmIHupfG6tpF-QMTLLVKQ8zh9Ly6wEyoV6a5zJ3SJa0w5raJkw-Ec1nzATcUbVC0MW7ViY3icHrOBkAk-I73NH1mMnN2xSk17IptHW9EdhCQQV8qRrkgmlCFL4kQiXDrEb93k2CIDPylgtzDg_WftPTrh4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