Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the fma HLSL Function #99117

Open
12 tasks
Tracked by #99235
farzonl opened this issue Jul 16, 2024 · 0 comments
Open
12 tasks
Tracked by #99235

Implement the fma HLSL Function #99117

farzonl opened this issue Jul 16, 2024 · 0 comments
Labels
backend:DirectX backend:SPIR-V bot:HLSL HLSL HLSL Language Support metabug Issue to collect references to a group of similar or related issues.

Comments

@farzonl
Copy link
Member

farzonl commented Jul 16, 2024

  • Implement fma clang builtin,
  • Link fma clang builtin with hlsl_intrinsics.h
  • Add sema checks for fma to CheckHLSLBuiltinFunctionCall in SemaChecking.cpp
  • Add codegen for fma to EmitHLSLBuiltinExpr in CGBuiltin.cpp
  • Add codegen tests to clang/test/CodeGenHLSL/builtins/fma.hlsl
  • Add sema tests to clang/test/SemaHLSL/BuiltIns/fma-errors.hlsl
  • Create the int_dx_fma intrinsic in IntrinsicsDirectX.td
  • Create the DXILOpMapping of int_dx_fma to 47 in DXIL.td
  • Create the fma.ll and fma_errors.ll tests in llvm/test/CodeGen/DirectX/
  • Create the int_spv_fma intrinsic in IntrinsicsSPIRV.td
  • In SPIRVInstructionSelector.cpp create the fma lowering and map it to int_spv_fma in SPIRVInstructionSelector::selectIntrinsic.
  • Create SPIR-V backend test case in llvm/test/CodeGen/SPIRV/hlsl-intrinsics/fma.ll

DirectX

DXIL Opcode DXIL OpName Shader Model Shader Stages
47 Fma 6.0 ()

SPIR-V

Fma:

Description:

Fma

Computes a * b + c. In uses where this operation is decorated
with NoContraction:

  • fma is considered a single operation, whereas the expression a
    * b + c is considered two operations.

- The precision of fma can differ from the precision of the
expression a * b + c.

- fma will be computed with the same precision as any other fma
decorated with NoContraction, giving invariant results for the same
input values of a, b, and c.

Otherwise, in the absence of a NoContraction decoration, there are
no special constraints on the number of operations or difference in
precision between fma and the expression a * b +c.

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.

Number Operand 1 Operand 2 Operand 3 Operand 4

50

<id>
a

<id>
b

<id>
c

Test Case(s)

Example 1

//dxc fma_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export double4 fn(double4 p1, double4 p2, double4 p3) {
    return fma(p1, p2, p3);
}

HLSL:

Returns the double-precision fused multiply-addition of a * b + c.

ret fma(double a, b, c);

Parameters

a

[in] The first value in the fused multiply-addition.

b

[in] The second value in the fused multiply-addition.

c

[in] The third value in the fused multiply-addition.

Return Value

The double-precision fused multiply-addition of parameters a * b + c. The returned value must be accurate to 0.5 units of least precision (ULP).

Remarks

The fma intrinsic must support NaNs, INFs, and Denorms.

To use the fma intrinsic in your shader code, call the ID3D11Device::CheckFeatureSupport method with D3D11_FEATURE_D3D11_OPTIONS to verify that the Direct3D device supports the ExtendedDoublesShaderInstructions feature option. The fma intrinsic requires a WDDM 1.2 display driver, and all WDDM 1.2 display drivers must support fma. If your app creates a rendering device with feature level 11.0 or 11.1 and the compilation target is shader model 5 or later, the HLSL source code can use the fma intrinsic.

Type Description

Name Template Type Component Type Size
a scalar, vector, or matrix double any
b same as input a double same dimensions as input a
c same as input a double same dimensions as input a
ret same as input a double same dimensions as input a

Minimum Shader Model

This function is supported in the following shader models.

Shader Model Supported
Shader model 5 or later yes

Requirements

Requirement Value
Minimum supported client
Windows 8 [desktop apps | UWP apps]
Minimum supported server
Windows Server 2012 [desktop apps | UWP apps]
Header
Corecrt_math.h

See also

Intrinsic Functions

@farzonl farzonl added backend:DirectX backend:SPIR-V bot:HLSL HLSL HLSL Language Support metabug Issue to collect references to a group of similar or related issues. labels Jul 16, 2024
@damyanp damyanp moved this to Ready in HLSL Support Oct 30, 2024
@damyanp damyanp moved this from Ready to Planning in HLSL Support Oct 30, 2024
@davidcook-msft davidcook-msft moved this from Planning to Ready in HLSL Support Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:DirectX backend:SPIR-V bot:HLSL HLSL HLSL Language Support metabug Issue to collect references to a group of similar or related issues.
Projects
Status: Ready
Development

No branches or pull requests

1 participant