<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/99097>99097</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Implement the `ddx_coarse` 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 `ddx_coarse` clang builtin,
- [ ] Link `ddx_coarse` clang builtin with `hlsl_intrinsics.h`
- [ ] Add sema checks for `ddx_coarse` to `CheckHLSLBuiltinFunctionCall` in `SemaChecking.cpp`
- [ ] Add codegen for `ddx_coarse` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp`
- [ ] Add codegen tests to `clang/test/CodeGenHLSL/builtins/ddx_coarse.hlsl`
- [ ] Add sema tests to `clang/test/SemaHLSL/BuiltIns/ddx_coarse-errors.hlsl`
- [ ] Create the `int_dx_ddx_coarse` intrinsic in `IntrinsicsDirectX.td`
- [ ] Create the `DXILOpMapping` of `int_dx_ddx_coarse` to `83` in `DXIL.td`
- [ ] Create the `ddx_coarse.ll` and `ddx_coarse_errors.ll` tests in `llvm/test/CodeGen/DirectX/`
- [ ] Create the `int_spv_ddx_coarse` intrinsic in `IntrinsicsSPIRV.td`
- [ ] In SPIRVInstructionSelector.cpp create the `ddx_coarse` lowering and map it to `int_spv_ddx_coarse` in `SPIRVInstructionSelector::selectIntrinsic`.
- [ ] Create SPIR-V backend test case in `llvm/test/CodeGen/SPIRV/hlsl-intrinsics/ddx_coarse.ll`
## DirectX
| DXIL Opcode | DXIL OpName | Shader Model | Shader Stages |
| ----------- | ----------- | ------------ | ------------- |
| 83 | DerivCoarseX | 6.0 | ('library', 'pixel', 'compute', 'amplification', 'mesh', 'node') |
## SPIR-V
# [OpDPdxCoarse](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpDPdxCoarse):
## Description:
Result is the partial derivative of *P* with respect to the window *x*
coordinate. Uses local differencing based on the value of *P* for the
current fragment’s neighbors, and possibly, but not necessarily,
includes the value of *P* for the current fragment. That is, over a
given area, the implementation can compute *x* derivatives in fewer
unique locations than would be allowed for
[**OpDPdxFine**](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpDPdxFine).
*Result Type* must be a scalar or vector of [*floating-point
type*](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Floating) using the IEEE 754 encoding. The component width must
be 32 bits.
The type of *P* must be the same as *Result Type*. *P* is the value to
take the derivative of.
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):
**DerivativeControl**
<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>Word Count</th>
<th>Opcode</th>
<th>Results</th>
<th>Operands</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p>4</p></td>
<td class="tableblock halign-left valign-top"><p>213</p></td>
<td
class="tableblock halign-left valign-top"><p><em><id></em><br />
<em>Result Type</em></p></td>
<td class="tableblock halign-left valign-top"><p><a
href="#ResultId"><em>Result <id></em></a></p></td>
<td
class="tableblock halign-left valign-top"><p><em><id></em><br />
<em>P</em></p></td>
</tr>
</tbody>
</table>
## Test Case(s)
### Example 1
```hlsl
//dxc ddx_coarse_test.hlsl -T lib_6_8 -enable-16bit-types -O0
export float4 fn(float4 p1) {
return ddx_coarse(p1);
}
```
## HLSL:
Computes a low precision partial derivative with respect to the screen-space x-coordinate.
## Syntax
``` syntax
float ddx_coarse(
in float value
);
```
## Parameters
<dl> <dt>
*value* \[in\]
</dt> <dd>
Type: **float**
The input value.
</dd> </dl>
## Return value
Type: **float**
The low precision partial derivative of *value*.
## Remarks
The following overloaded versions are also available:
``` syntax
float2 ddx_coarse(float2 value);
float3 ddx_coarse(float3 value);
float4 ddx_coarse(float4 value);
```
### Minimum Shader Model
This function is supported in the following shader models.
| Shader Model | Supported |
|-----------------------------------------------------------------------------|-----------|
| [Shader Model 5](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/d3d11-graphics-reference-sm5.md) and higher shader models | yes |
This function is supported in the following types of shaders:
| Vertex | Hull | Domain | Geometry | Pixel | Compute |
|--------|------|--------|----------|-------|---------|
| | | | | 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/eJzUWd1TI7kR_2vEi2pcYw02-MEPxsZ3VO1mqWNzuTdKM2rbChppImmMyV-famk-wcAluc0HtbWMWq3--HWr1RLcObnXAEsyuyGzzQWv_cHY5Y7bvxutLnIjXpYJJbMbSmYbeldWCkrQnpJ5KsTpsTDcOiDzlBaK6z3Na6m81IStSbrq132R-umTJfRZ-gPyHJRTj1J7K7WThZscyDwdCVsJQR2UnBYHKJ4c3Rn7VrY3SFsjx89fHr7cRCXbWhdeGr3mSiGX1Mj1ACUPnFLvJ0VVnVNYGAF70B8puy2lH-i6PVW217H-qSF_psCD866RGAAibIs0wrZrI-An0KiDsG2DmyNs21szQfTeBewD2YhBIzgYevdKcALWGuvOyl9b4B6oPwAKlto_itPjGKEung0cd118N9JC4X-bePGx2M1vd1--VV95VUm9R5Fm9642byjOXWc9_rj8Yx3jsE5ignAtxvTHBoc4HQGNGpQ6lm9iRdi2cZCw7ee4uer4u4F7uL_75dczLt1pGqbutPO2Dun-AA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