<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/99093>99093</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Implement the `clip` 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 `clip` clang builtin,
- [ ] Link `clip` clang builtin with `hlsl_intrinsics.h`
- [ ] Add sema checks for `clip` to `CheckHLSLBuiltinFunctionCall` in `SemaChecking.cpp`
- [ ] Add codegen for `clip` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp`
- [ ] Add codegen tests to `clang/test/CodeGenHLSL/builtins/clip.hlsl`
- [ ] Add sema tests to `clang/test/SemaHLSL/BuiltIns/clip-errors.hlsl`
- [ ] Create the `int_dx_clip` intrinsic in `IntrinsicsDirectX.td`
- [ ] Create the `DXILOpMapping` of `int_dx_clip` to `82` in `DXIL.td`
- [ ] Create the `clip.ll` and `clip_errors.ll` tests in `llvm/test/CodeGen/DirectX/`
- [ ] Create the `int_spv_clip` intrinsic in `IntrinsicsSPIRV.td`
- [ ] In SPIRVInstructionSelector.cpp create the `clip` lowering and map it to `int_spv_clip` in `SPIRVInstructionSelector::selectIntrinsic`.
- [ ] Create SPIR-V backend test case in `llvm/test/CodeGen/SPIRV/hlsl-intrinsics/clip.ll`
## DirectX
| DXIL Opcode | DXIL OpName | Shader Model | Shader Stages |
| ----------- | ----------- | ------------ | ------------- |
| 82 | Discard | 6.0 | ('pixel',) |
## SPIR-V
# [OpDemoteToHelperInvocation](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpDemoteToHelperInvocation):
## Description:
(OpDemoteToHelperInvocationEXT)**
Demote this fragment shader [invocation](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Invocation) to a helper
invocation. Any stores to memory after this instruction are suppressed
and the fragment does not write outputs to the framebuffer.
Unlike the [**OpTerminateInvocation**](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpTerminateInvocation)
instruction, this does not necessarily terminate the invocation which
might be needed for derivative calculations. It is not considered a flow
control instruction (flow control does not become non-uniform) and does
not terminate the block. The implementation may terminate helper
invocations before the end of the shader as an optimization, but doing
so must not affect derivative calculations and does not make control
flow non-uniform.
After an invocation executes this instruction, any subsequent load of
**HelperInvocation** within that invocation will load an undefined value
unless the **HelperInvocation** [built-in variable](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#BuiltIn) is
[decorated](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Decoration) with **Volatile** or the load included
**Volatile** in its [**Memory Operands**](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Memory_Operands)
This instruction is only valid in the **Fragment** [Execution
Model](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Execution_Model).
[Capability](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Capability):
**DemoteToHelperInvocation**
[Missing before](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Unified) **version 1.6**.
| Word Count | Opcode | Results | Operands |
|------------|--------|---------|----------|
| 1 | 5380 | | |
## Test Case(s)
### Example 1
```hlsl
//dxc clip_test.hlsl -T lib_6_8 -enable-16bit-types -O0
[numthreads(1, 1, 1)]
[shader("pixel")]
void fn( ) : SV_Target {
float4 p1;
clip(p1.a);
}
```
## HLSL:
Discards the current pixel if the specified value is less than zero.
| clip(*x*) |
|-----------|
## Parameters
| Item | Description |
|--------------------------------------------------------|----------------------------------------|
| <span id="x"></span><span id="X"></span>*x*<br/> | \[in\] The specified value.<br/> |
## Return Value
None.
## Remarks
Use the **clip** HLSL intrinsic function to simulate clipping planes if each component of the *x* parameter represents the distance from a plane.
Also, use the **clip** function to test for alpha behavior, as shown in the following example:
```
clip( Input.Color.A < 0.1f ? -1:1 );
```
## 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* | [**scalar**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md), **vector**, or **matrix** | [**float**](/windows/desktop/WinProg/windows-data-types) | any |
## 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) | yes (pixel shader only) |
| [Shader Model 3 (DirectX HLSL)](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-sm3.md) | yes (pixel shader only) |
| [Shader Model 2 (DirectX HLSL)](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-sm2.md) | yes (pixel shader only) |
| [Shader Model 1 (DirectX HLSL)](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-sm1.md) | yes (pixel shader 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/eJzMWd1u2zgWfhrmhrAgU7FjX_hCseOZAO10MEk7vQso6djihiK1JOUkffrFIfXnxGm22w2mhttYJHV-v3P4UeLWir0CWJHZJZltznjjSm1WO26-aSXPMl08rSaUzC4pmW3odVVLqEA5SuZxLkVN5j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