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

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

## DirectX

| DXIL Opcode | DXIL OpName | Shader Model | Shader Stages |
| ----------- | ----------- | ------------ | ------------- |
| 23 | Log | 6.0 | () |

## SPIR-V

# FMax:

## Description:
**FMax**  
  
Result is *y* if *x* \< *y*; otherwise result is *x*. Which operand is
the result is undefined if one of the operands is a NaN.  
  
The operands must all be a scalar or vector whose component type is
floating-point.  
  
*Result Type* and the type of all operands must be the same type.
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>40</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>y</em></p></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>



## Test Case(s)

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

export float4 fn(float p1, float p2, float p3) {
    return lit(p1, p2, p3);
}
```
## HLSL:

Returns a lighting coefficient vector.



| *ret* lit(*n\_dot\_l*, *n\_dot\_h*, *m*) |
|------------------------------------------|



 

This function returns a lighting coefficient vector (ambient, diffuse, specular, 1) where:

-   ambient = 1
-   diffuse = n · l < 0 ? 0 : n · l
-   specular = n · l < 0 \|\| n · h < 0 ? 0 : (n · h) ^ m

Where the vector n is the normal vector, l is the direction to light and h is the half vector.

## Parameters



| Item                                                                       | Description                                                                              |
|----------------------------------------------------------------------------|------------------------------------------------------------------------------------------|
| <span id="n_dot_l"></span><span id="N_DOT_L"></span>*n\_dot\_l*<br/> | \[in\] The dot product of the normalized surface normal and the light vector.<br/> |
| <span id="n_dot_h"></span><span id="N_DOT_H"></span>*n\_dot\_h*<br/> | \[in\] The dot product of the half-angle vector and the surface normal.<br/>       |
| <span id="m"></span><span id="M"></span>*m*<br/>                     | \[in\] A specular exponent.<br/>                                                   |



 

## Return Value

The lighting coefficient vector.

## 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 |
|-------------|-------------------------------------------------------------------------------------|----------------------------------------------------------------|------|
| *n\_dot\_l* | [**scalar**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md) | [**float**](/windows/desktop/WinProg/windows-data-types)                        | 1    |
| *n\_dot\_h* | [**scalar**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md) | [**float**](/windows/desktop/WinProg/windows-data-types)                        | 1    |
| *m*         | [**scalar**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md) | [**float**](/windows/desktop/WinProg/windows-data-types)                        | 1    |
| *ret*       | [**vector**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md) | [**float**](/windows/desktop/WinProg/windows-data-types)                        | 4    |



 

## Minimum Shader Model

This function is supported in the following shader models.



| Shader Model                                                                       | Supported           |
|------------------------------------------------------------------------------------|---------------------|
| [Shader Model 2 (DirectX HLSL)](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-sm2.md) and higher shader models | yes                 |
| [Shader Model 1 (DirectX HLSL)](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-sm1.md)                          | yes (vs\_1\_1 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/eJzsOVtv27jSv4Z5GciQKdu1H_zgyPFugKRdNEG7bwEl0Ra_UqRAUrn0138YUrak2LnsIgenB6h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