<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/99230>99230</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Implement the `pack_clamp_u8` 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 `pack_clamp_u8` clang builtin,
- [ ] Link `pack_clamp_u8` clang builtin with `hlsl_intrinsics.h`
- [ ] Add sema checks for `pack_clamp_u8` to `CheckHLSLBuiltinFunctionCall` in `SemaChecking.cpp`
- [ ] Add codegen for `pack_clamp_u8` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp`
- [ ] Add codegen tests to `clang/test/CodeGenHLSL/builtins/pack_clamp_u8.hlsl`
- [ ] Add sema tests to `clang/test/SemaHLSL/BuiltIns/pack_clamp_u8-errors.hlsl`
- [ ] Create the `int_dx_pack_clamp_u8` intrinsic in `IntrinsicsDirectX.td`
- [ ] Create the `DXILOpMapping` of `int_dx_pack_clamp_u8` to `220` in `DXIL.td`
- [ ] Create the `pack_clamp_u8.ll` and `pack_clamp_u8_errors.ll` tests in `llvm/test/CodeGen/DirectX/`
- [ ] Create the `int_spv_pack_clamp_u8` intrinsic in `IntrinsicsSPIRV.td`
- [ ] In SPIRVInstructionSelector.cpp create the `pack_clamp_u8` lowering and map it to `int_spv_pack_clamp_u8` in `SPIRVInstructionSelector::selectIntrinsic`.
- [ ] Create SPIR-V backend test case in `llvm/test/CodeGen/SPIRV/hlsl-intrinsics/pack_clamp_u8.ll`
## DirectX
| DXIL Opcode | DXIL OpName | Shader Model | Shader Stages |
| ----------- | ----------- | ------------ | ------------- |
| 220 | Pack4x8 | 6.6 | () |
## SPIR-V
# SClamp:
## Description:
**SClamp**
Result is min(max(*x*, *minVal*), *maxVal*), where *x*, *minVal* and
*maxVal* are interpreted as signed integers. Result is undefined if
*minVal* \> *maxVal*.
*Result Type* and the type of the operands must both be integer scalar
or integer vector types. *Result Type* and operand types must have the
same number of components with the same component width. Results are
computed per component.
<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>45</p></td>
<td
class="tableblock halign-left valign-top"><p><em><id></em><br />
<em>x</em></p></td>
<td
class="tableblock halign-left valign-top"><p><em><id></em><br />
<em>minVal</em></p></td>
<td
class="tableblock halign-left valign-top"><p><em><id></em><br />
<em>maxVal</em></p></td>
<td></td>
</tr>
</tbody>
</table>
## Test Case(s)
### Example 1
```hlsl
//dxc pack_clamp_u8_test.hlsl -T lib_6_8 -enable-16bit-types -O0
export uint8_t4_packed fn(int4 p1) {
return pack_clamp_u8(p1);
}
```
## HLSL:
## Syntax
```syntax
p32u8 pack_clamp_u8(sint16or32_only<4> v);
```
## Type Description
| Name | [**Template Type**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/../direct3dhlsl/dx-graphics-hlsl-data-types.md)| [**Component Type**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/../direct3dhlsl/dx-graphics-hlsl-data-types.md) | Size |
|-------|--------------------------------------------------------------------|----------------------------------------------------------------------|------|
| *ret* | [**scalar**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/../direct3dhlsl/dx-graphics-hlsl-scalar.md) | [**uint8_t4_packed**](../WinProg/windows-data-types) | 1 |
| *v* | [**vector**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/../direct3dhlsl/dx-graphics-hlsl-vector.md) | [**int**](../WinProg/windows-data-types) or [**int16_t**](https://github.com/microsoft/DirectXShaderCompiler/wiki/16-Bit-Scalar-Types) | 4 |
## Minimum Shader Model
This function is supported in the following shader models.
|Shader Model | Supported|
|-------------|----------|
|[Shader Model 6.6](https://microsoft.github.io/DirectX-Specs/d3d/HLSL_ShaderModel6_6) and higher shader models | yes |
## Shader Stages
## See also
- [**Intrinsic Functions (DirectX HLSL)**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/../direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md)
- **See [Pack Intrinsics](https://microsoft.github.io/DirectX-Specs/d3d/HLSL_SM_6_6_Pack_Unpack_Intrinsics#pack-intrinsics)**
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUWN9y46jSfxp8Q0klIVtxLnzhOPF-qZrsTK3zzdk7F5KwxAkCFaDE2ac_1SDrz9jj7J6zF9kpT0V0N93Nr4HuhhrDS8nYCi3u0OJ-RltbKb06UP2HkmKWqeJ9FWC0uMNocY8f60awmkmLURo1NH_Z54LWzb5dojTCuaCyxFnLheUSkQ2K1sPUL1y-fDwLv3FbgVgljNhzaTWXhucmrFAaTfStiwIbVlOcVyx_Mfig9EX1VgF5A0L_92X35c7b2bYyt1zJDRUCpLgEqR2rqZPksgzzprlkM1cFK5n8wN5Dze3I3MOx0YOZzS8d-SMblhlrOo0OJkS2QENku1EF-4VJsIHItkPPILKdOBQCjD9F7op6QKLT7Xx9PNcdMK2VNhdNbDSjlmFbMdDNpd0Xx_0ZVH14O1we-3Dfc81y-3toi-ua739__PK1eaJNw2UJKtXhmkGrMLAJiYZggIrrds7CHPo9Q2Vxxtp3oHgJD7C3I8RrfRY-RLbdUhHZfgyiaV7_Coq7b4-_fb-wtkeJHetRGqtbdw52TLDcKg07EucTw2cGhXpjmsvSAVDTBmNuu110xUl3vn5iFSVrlKyNG_XuozQKL-EBSoLvOKP5C5OFwxjn1LAPcHa2EdnCdg2Gi-XsxIhuM8OPJIg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