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

    <tr>
        <th>Summary</th>
        <td>
            Implement the `pack_clamp_s8` 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_s8` clang builtin,
- [ ] Link `pack_clamp_s8` clang builtin with `hlsl_intrinsics.h`
- [ ] Add sema checks for `pack_clamp_s8` to `CheckHLSLBuiltinFunctionCall` in `SemaChecking.cpp`
- [ ] Add codegen for `pack_clamp_s8` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp`
- [ ] Add codegen tests to `clang/test/CodeGenHLSL/builtins/pack_clamp_s8.hlsl`
- [ ] Add sema tests to `clang/test/SemaHLSL/BuiltIns/pack_clamp_s8-errors.hlsl`
- [ ] Create the `int_dx_pack_clamp_s8` intrinsic in `IntrinsicsDirectX.td`
- [ ] Create the `DXILOpMapping` of `int_dx_pack_clamp_s8` to  `220` in `DXIL.td`
- [ ] Create the  `pack_clamp_s8.ll` and `pack_clamp_s8_errors.ll` tests in `llvm/test/CodeGen/DirectX/`
- [ ] Create the `int_spv_pack_clamp_s8` intrinsic in `IntrinsicsSPIRV.td`
- [ ] In SPIRVInstructionSelector.cpp create the `pack_clamp_s8` lowering and map  it to `int_spv_pack_clamp_s8` in `SPIRVInstructionSelector::selectIntrinsic`.
- [ ] Create SPIR-V backend test case in `llvm/test/CodeGen/SPIRV/hlsl-intrinsics/pack_clamp_s8.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>&lt;id&gt;</em><br />
<em>x</em></p></td>
<td
class="tableblock halign-left valign-top"><p><em>&lt;id&gt;</em><br />
<em>minVal</em></p></td>
<td
class="tableblock halign-left valign-top"><p><em>&lt;id&gt;</em><br />
<em>maxVal</em></p></td>
<td></td>
</tr>
</tbody>
</table>



## Test Case(s)

 
 ### Example 1
```hlsl
//dxc pack_clamp_s8_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export int8_t4_packed fn(int4 p1) {
 return pack_clamp_s8(p1);
}
```
## HLSL:

## Syntax


```syntax
p32i8 pack_clamp_s8(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) | [**int8_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/eJzUWF9z4ygS_zT4hZJKRrZiP_jBceK9VE12pta5uX1zIQlLXBCoACXOfvqrBll_xo6ze7sPmSlPRXQ33c2vge6GGsMLydgKzW_R_G5CG1sqvTpQ_YeSYpKq_G0VYDS_xWh-hx-qWrCKSYtREtU0e95nglb13ixQEuFMUFngtOHCconIBkXrfuoXLp8_noVfuS1BrBRG7Lm0mkvDMxOWKIlG-tZ5jg2rKM5Klj0bfFD6onqrgLwBoX992X259Xa2jcwsV3JDhQApLkFqxyrqJLkswqyuL9nMVM4KJj-wd19xOzB3f6x1b2bzS0v-yIZlxppWo4MJkS3QENluVM5-YRJsILJt0TOIbEcOhQDju8hdUQ9ItLqdrw_nugOmtdLmoomNZtQybEsGurm0-_y4P4OqC2-Ly0MX7juuWWZ_D21-XfPd7w9fvtaPtK65LEClOlwzaBUGNiFRHwxQcd3OWZhDv2eozM9Y-xYUL-EB9naEeKnOwofItl0qItuPQTT1y19Bcfft4bfvF9b2ILFjPUhjdePOwY4JllmlYUfibGT4zKBQr0xzWTgAKlpjzG27i6446c7XO1ZRvEbx2rh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