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

    <tr>
        <th>Summary</th>
        <td>
            Implement the `ddy` 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 `ddy` clang builtin,
- [ ] Link `ddy` clang builtin with `hlsl_intrinsics.h`
- [ ] Add sema checks for `ddy` to `CheckHLSLBuiltinFunctionCall` in `SemaChecking.cpp`
- [ ] Add codegen for `ddy` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp`
- [ ] Add codegen tests to `clang/test/CodeGenHLSL/builtins/ddy.hlsl`
- [ ] Add sema tests to `clang/test/SemaHLSL/BuiltIns/ddy-errors.hlsl`
- [ ] Create the `int_dx_ddy` intrinsic in `IntrinsicsDirectX.td`
- [ ] Create the `DXILOpMapping` of `int_dx_ddy` to  `84` in `DXIL.td`
- [ ] Create the  `ddy.ll` and `ddy_errors.ll` tests in `llvm/test/CodeGen/DirectX/`
- [ ] Create the `int_spv_ddy` intrinsic in `IntrinsicsSPIRV.td`
- [ ] In SPIRVInstructionSelector.cpp create the `ddy` lowering and map  it to `int_spv_ddy` in `SPIRVInstructionSelector::selectIntrinsic`.
- [ ] Create SPIR-V backend test case in `llvm/test/CodeGen/SPIRV/hlsl-intrinsics/ddy.ll`

## DirectX

| DXIL Opcode | DXIL OpName | Shader Model | Shader Stages |
| ----------- | ----------- | ------------ | ------------- |
| 84 | DerivCoarseY | 6.0 | ('library', 'pixel', 'compute', 'amplification', 'mesh', 'node') |

## SPIR-V

# [OpDPdy](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpDPdy):

## Description:
  
Same result as either [**OpDPdyFine**](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpDPdyFine) or
[**OpDPdyCoarse**](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpDPdyCoarse) 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>208</p></td>
<td
class="tableblock halign-left valign-top"><p><em>&lt;id&gt;</em><br />
<em>Result Type</em></p></td>
<td class="tableblock halign-left valign-top"><p><a
href="#ResultId"><em>Result &lt;id&gt;</em></a></p></td>
<td
class="tableblock halign-left valign-top"><p><em>&lt;id&gt;</em><br />
<em>P</em></p></td>
</tr>
</tbody>
</table>



## Test Case(s)

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

export float4 fn(float4 p1) {
    return ddy(p1);
}
```
## HLSL:

Returns the partial derivative of the specified value with respect to the screen-space y-coordinate.



| *ret* ddy(*x*) |
|----------------|



 

This function computes the partial derivative with respect to the screen-space y-coordinate. To compute the partial derivative with respect to the screen-space x-coordinate, use the [**ddx**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-ddx.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/eJzUGl1P47j215gXK1Xq0EIfeCiF7iLN3EHLaHbvE3Lj08YXx45sB9r99VfHTtKEFpiP5c7cCEF8bJ9vnw8H7pzcaIALMrkkk6sTXvvC2Is1t38brU5WRuwuEkoml5RMruhNWSkoQXtKpqkQOzJNaa643tBVLZWXmrAFSef7DR-kfnhpLX2SvsDJQjl1L7W3UjuZu1FBpukAy1wI6qDkNC8gf3B0bWwPqTc4WODU7x_uPlxG7Mta514aveBK4SqpcdUdlDyslHozyqvqGKXcCNiAPkrlupS-R-R6W9k98sVvDfgtzB6cdw3GoBLClggjbLkwAn4DjTQIWzaacoQthdiNUFEv6uYVpCh1gzFweNNiTMBaY91RxAsL3AP1BSBGqf292N43yuiM1Uh-0xnvSlrI_V8jL17Hd_XXzYdP1UdeVVJvEKVZH5LxhiLw_HSvY9z3OvLGZqNodq5FA7hvZI3wqK2IU6nH8sAChC0bWQhbvq0bVz2-rZy725s_vhzh_kbTMHWjnbd1cNs7UJB7Y9GVaD4g15BR5gms1JsgYskrSqVvzH_AUHD9FyiQbE6yuQujjlUyTUfHJEYkyRe64vkDaBG0SHPu4A1NBtqELdHPkv1Jb9xaNb6HPywjLKOt5iPsbEHR7PRThQeI9sb_4mUc3xVcgKUfjQDVB9x5vgGHkAZRsn_oG-NDQNJDdH4aGQErHxeGWwf_DoDpKA1_CTsn7EzJleV2R9gZYQg7q-QW1H6Ym7KqPewBvKyUXMuco4n24BJcsR9pI-KWWctQp7looA6E9vtUXd2KHZlcEXZeeF85tDhbEra0sJHO293oobBGGzcydtNYK0FzuQqCkW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