<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/99113>99113</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Implement the `f32tof16` 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 `f32tof16` clang builtin,
- [ ] Link `f32tof16` clang builtin with `hlsl_intrinsics.h`
- [ ] Add sema checks for `f32tof16` to `CheckHLSLBuiltinFunctionCall` in `SemaChecking.cpp`
- [ ] Add codegen for `f32tof16` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp`
- [ ] Add codegen tests to `clang/test/CodeGenHLSL/builtins/f32tof16.hlsl`
- [ ] Add sema tests to `clang/test/SemaHLSL/BuiltIns/f32tof16-errors.hlsl`
- [ ] Create the `int_dx_f32tof16` intrinsic in `IntrinsicsDirectX.td`
- [ ] Create the `DXILOpMapping` of `int_dx_f32tof16` to `130` in `DXIL.td`
- [ ] Create the `f32tof16.ll` and `f32tof16_errors.ll` tests in `llvm/test/CodeGen/DirectX/`
- [ ] Create the `int_spv_f32tof16` intrinsic in `IntrinsicsSPIRV.td`
- [ ] In SPIRVInstructionSelector.cpp create the `f32tof16` lowering and map it to `int_spv_f32tof16` in `SPIRVInstructionSelector::selectIntrinsic`.
- [ ] Create SPIR-V backend test case in `llvm/test/CodeGen/SPIRV/hlsl-intrinsics/f32tof16.ll`
## DirectX
| DXIL Opcode | DXIL OpName | Shader Model | Shader Stages |
| ----------- | ----------- | ------------ | ------------- |
| 130 | LegacyF32ToF16 | 6.0 | () |
## SPIR-V
# PackHalf2x16:
## Description:
**PackHalf2x16**
Result is the unsigned integer obtained by converting the components of
a two-component floating-point vector to the 16-bit **OpTypeFloat**, and
then packing these two 16-bit integers into a 32-bit unsigned integer.
The first vector component specifies the 16 least-significant bits of
the result; the second component specifies the 16 most-significant
bits.
The *v* operand must be a vector of 2 components whose type is a 32-bit
floating-point.
*Result Type* must be a 32-bit integer 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>58</p></td>
<td
class="tableblock halign-left valign-top"><p><em><id></em><br />
<em>v</em></p></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
## Test Case(s)
### Example 1
```hlsl
//dxc f32tof16_test.hlsl -T lib_6_8 -enable-16bit-types -O0
export uint4 fn(float4 p1) {
return f32tof16(p1);
}
```
## HLSL:
Converts an input into a float16 type.
## Syntax
``` syntax
uint f32tof16(
in float value
);
```
## Parameters
<dl> <dt>
*value* \[in\]
</dt> <dd>
Type: **float**
The input value.
</dd> </dl>
## Return value
Type: **uint**
The converted value, stored in the low-half of the uint.
## Remarks
The following overloaded versions are also available:
``` syntax
uint2 f32tof16(float2 value);
uint3 f32tof16(float3 value);
uint4 f32tof16(float4 value);
```
### Minimum Shader Model
This function is supported in the following shader models.
| Shader Model | Supported |
|---------------------------------------------------------------------|-----------|
| [Shader Model 4](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-sm4.md) and higher shader models | yes |
Although this function is available in [Shader Model 4](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-sm4.md) and higher shader models, because it is emulated in 4.0 and 4.1, it is less perfomant on these shader models than it is on [Shader Model 5](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/d3d11-graphics-reference-sm5.md).
This function is supported in the following types of shaders:
| Vertex | Hull | Domain | Geometry | Pixel | Compute |
|--------|------|--------|----------|-------|---------|
| x | x | x | x | x | x |
## See also
<dl> <dt>
[Intrinsic Functions](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md)
</dt> <dt>
[Shader Model 5](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/d3d11-graphics-reference-sm5.md)
</dt> </dl>
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUWF9v2zgS_zTMCyFDomwnfvCDY8fbAO22aIJi3wJKGlm8UKRAUo5zn_4w1P_Yde_2Dne3gdGKw-HM8Df_SHJrxUEBrMninix2N7x2hTbrnJu_ayVvEp29rwNKFveULHb0sawklKAcJcswj5nTebQky5CmkqsDTWohnVCEbUm4GVZ9Fur16gL6JlyBHIW08kUoZ4SyIrWzgizDiahNllELJadpAemrpbk2HyU7jZQtzn_6_PT5vlGxr1XqhFZbLiVyCYVcT1ByzynUYZZW1SV1qc7gAOrnqh5K4UaaHk6VGTRsf2vJvxLvwDrbSvTgELZHGmH7rc7gN1Cog7B9i5klbN_ZMkPcfgrVFcm4_1asN_NxIjYAY7SxF6VvDXAH1BWAYoVyL9npZYxN78UWiMfeqzthIHV_zFx2Xejuj8fPX6svvKqEOqBInf9El9MUZ6I4HIDH1ddVjL05a6KCq2xMfWkBaCYbHBvpUh7LMwcRtm_3Rtj-14DZ6vhPIvb07fH7jwubeVTUTz0q60zt4_sJJKROGww3mk50jnVJ_QZGqIPfcckrSoVrQ-SyaT5bfqKLxBsSb6wf9UaTZTi7BAAKCX7QhKevoDIPKk25hV8A63UTtsdYDIYKMU4C2QYp_lhMWEw7bzS02y3FoKBfK8w5Ohr_zstm_FTwDAz9ojOQY8KT4wewSGkFBcMf_cX4nBCMBEVx6Oc_w4Gn7_uYPet9tPSk5ayZIuyOsFW3pt9cA2RPot94-vqJy5ydoiV6ZAIE2NSICp3WTjH8TZZ4CqUkbP75DraWjgrrw6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