<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/99096>99096</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Implement the `ddx` 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` clang builtin,
- [ ] Link `ddx` clang builtin with `hlsl_intrinsics.h`
- [ ] Add sema checks for `ddx` to `CheckHLSLBuiltinFunctionCall` in `SemaChecking.cpp`
- [ ] Add codegen for `ddx` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp`
- [ ] Add codegen tests to `clang/test/CodeGenHLSL/builtins/ddx.hlsl`
- [ ] Add sema tests to `clang/test/SemaHLSL/BuiltIns/ddx-errors.hlsl`
- [ ] Create the `int_dx_ddx` intrinsic in `IntrinsicsDirectX.td`
- [ ] Create the `DXILOpMapping` of `int_dx_ddx` to `83` in `DXIL.td`
- [ ] Create the `ddx.ll` and `ddx_errors.ll` tests in `llvm/test/CodeGen/DirectX/`
- [ ] Create the `int_spv_ddx` intrinsic in `IntrinsicsSPIRV.td`
- [ ] In SPIRVInstructionSelector.cpp create the `ddx` lowering and map it to `int_spv_ddx` in `SPIRVInstructionSelector::selectIntrinsic`.
- [ ] Create SPIR-V backend test case in `llvm/test/CodeGen/SPIRV/hlsl-intrinsics/ddx.ll`
## DirectX
| DXIL Opcode | DXIL OpName | Shader Model | Shader Stages |
| ----------- | ----------- | ------------ | ------------- |
| 83 | DerivCoarseX | 6.0 | ('library', 'pixel', 'compute', 'amplification', 'mesh', 'node') |
## SPIR-V
# [OpDPdx](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpDPdx):
## Description:
Same result as either [**OpDPdxFine**](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpDPdxFine) or
[**OpDPdxCoarse**](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpDPdxCoarse) on *P*. Selection of which one is
based on external factors.
*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):
**Shader**
<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>207</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_test.hlsl -T lib_6_8 -enable-16bit-types -O0
export float4 fn(float4 p1) {
return ddx(p1);
}
```
## HLSL:
Returns the partial derivative of the specified value with respect to the screen-space x-coordinate.
| *ret* ddx(*x*) |
|----------------|
This function computes the partial derivative with respect to the screen-space x-coordinate. To compute the partial derivative with respect to the screen-space y-coordinate, use the [**ddy**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-ddy.md) function.
This function is only supported in pixel shaders.
## Parameters
| Item | Description |
|--------------------------------------------------------|----------------------------------------|
| <span id="x"></span><span id="X"></span>*x*<br/> | \[in\] The specified value.<br/> |
## Return Value
The partial derivative of the *x* parameter.
## 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 |
| *ret* | same as input *x* | [**float**](/windows/desktop/WinProg/windows-data-types) | same dimension(s) as input *x* |
## 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 |
| [Shader Model 4](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-sm4.md) | yes |
| [Shader Model 3 (DirectX HLSL)](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-sm3.md) | yes |
| [Shader Model 2 (DirectX HLSL)](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-sm2.md) | yes in ps\_2\_x; unsupported in ps\_2\_0. |
| [Shader Model 1 (DirectX HLSL)](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-sm1.md) | no |
This function is supported in the following types of shaders:
| Vertex | Hull | Domain | Geometry | Pixel | Compute |
|--------|------|--------|----------|-------|---------|
| | | | | x | |
## 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/eJzUOltv46rWv4a-IFsOTtLkIQ9p0syuNPNNNa1mz1tFDIn5isEC3Cb71x8tsB27Sdq57J6ZY1WtWcC6sy641FqxVZzP0OgKjZYXtHK5NrMNNf9oJS_Wmu1nEUajK4xGS3xTlJIXXDmMxgljOzROcCap2uJ1JaQTCpEFSuaHDR-Fejy3Fj8Ll8NkLq18EMoZoazIbJyjcdLDMmcMW15QnOU8e7R4o00HqdMwWMDUXx_vPl4F7KtKZU5otaBSwiqhYNUdL6hfKdQ2zsryFKVMM77l6iSV60K4DpHrXWkOyBcfavBbmB23ztYYvUoQWQEMkdVCM_6BK6CByKrWlEVkxdguBkWd1c0rSEHqGqPn8KbBGHFjtLEnES8Mp45jl3PAKJR7YLuHWhmtsWrJb1rjLYXhmfsWO_Y6vuW3m4-fy0-0LIXaAkq9OSbjNAbgJD3oGPa9jry2WRzMThWrAQ-1rAEetBVwSvlUHFkAkVUtCyKrt3Vjy6e3lXN3e_Pl6wnubxT2UzfKOlN5t73jkmdOG3AlnPXI1WSkfuZGqK0XsaAlxsLV5j9iyLv-GQoonaN0bv2oZRWNk_iUxIAk-orXNHvkinkt4oxa_oYmPW1EVuBn0eGk124ta9-DH5IikuJG8wF2ucBgdvy5hAOEO-P_o0UY3-WUcYM_acZlF3Dn6JZbgNSIosOD3xgfA6IOokkaGOFGPC00NZZ_84BxnPi_iEwQuZRibajZI3KJCMAuS7Hj8jDMdFFWjh8AtCil2IiMgokO4ILb_DBSmoUt04ahVnPBQC0I7Pe5XN6yHRotEZnkzpUWLE5WiKwM3wrrzD5-zI1W2sbabGtrRWAuW3JvpEqJjeBs0Fgyzl0hEUlrzGQKGHv24zYzovQy-CmMUTK_A2sZbivpMLWYC5dzAxwiMkdkHrCthOJh_H4cBxpTrA2w3KcfbPneHDRU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