llvm / llvm/llvm-project

[Matrix] Implement matrix support for the `mad` HLSL Function

Open
#184,510 0 comments 0 reactions 0 assignees View on GitHub
bot:HLSL HLSL
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

> Note: This issue tracks matrix support for `mad`. The original implementation issue is [here](https://github.com/llvm/llvm-project/issues/83736).

Matrix requirements:
- [ ] https://github.com/llvm/wg-hlsl/issues/387

## HLSL:
Performs an arithmetic multiply/add operation on three values.

## Syntax

``` syntax
numeric mad(
in numeric mvalue,
in numeric avalue,
in numeric bvalue
);
```

## Parameters

*mvalue* \[in\]

Type: **numeric**

The multiplication value.

*avalue* \[in\]

Type: **numeric**

The first addition value.

*bvalue* \[in\]

Type: **numeric**

The second addition value.

## Return value

Type: **numeric**

The result of *mvalue* \* *avalue* + *bvalue*.

## Remarks

### Minimum Shader Model

This function is supported in the following shader models.

 

This function is supported in the following types of shaders:

| Vertex | Hull | Domain | Geometry | Pixel | Compute |
|--------|------|--------|----------|-------|---------|
| x | x | x | x | x | x |

 

Shader authors can use the **mad** instrinsic to explicitly target the **mad** hardware instruction in the compiled shader output, which is particularly useful with shaders that mark results with the [precise](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src/direct3dhlsl/dx-graphics-hlsl-appendix-keywords.md) keyword. The **mad** instruction can be implemented in hardware as either "fused," which offers higher precision than implementing a **mul** instruction followed by an **add** instruction, or as a **mul** + **add**.

If shader authors use the **mad** instrinsic to calculate a result that the shader marked as precise, they indicate to the hardware to use any valid implementation of the **mad** instruction (fused or not) as long as the implementation is consistent for all uses of that **mad** intrinsic in any shader on that hardware. Shaders can then take advantage of potential performance improvements by using a native **mad** instruction (versus **mul** + **add**) on some hardware. The result of performing a native **mad** hardware instruction might or might not be different than performing a **mul** followed by an **add**. However, whatever the result is, the result must be consistent for the same operation to occur in multiple shaders or different parts of a shader.

## See also

[Intrinsic Functions](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src/direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md)

[Shader Model 5](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src/direct3dhlsl/d3d11-graphics-reference-sm5.md)

Contributor guide

Open the contributing guide

Research direction

Start with the original implementation issue (llvm/llvm-project#83736) and the linked HLSL matrix requirement (wg-hlsl#387), then compare them with the documented mad syntax and semantics in this issue. Done means matrix arguments and results are supported for the HLSL mad intrinsic and the matrix requirement is satisfied.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.