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

[HLSL] implement lerp intrinsic #70102

Closed
Tracked by #29
llvm-beanz opened this issue Oct 24, 2023 · 4 comments · Fixed by #84526
Closed
Tracked by #29

[HLSL] implement lerp intrinsic #70102

llvm-beanz opened this issue Oct 24, 2023 · 4 comments · Fixed by #84526
Assignees
Labels
backend:DirectX clang:codegen IR generation bugs: mangling, exceptions, etc. clang:frontend Language frontend issues, e.g. anything involving "Sema" HLSL HLSL Language Support

Comments

@llvm-beanz
Copy link
Collaborator

Implement HLSL lerp intrinsic:

https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-lerp

@llvm-beanz llvm-beanz converted this from a draft issue Oct 24, 2023
@llvm-beanz llvm-beanz added the HLSL HLSL Language Support label Oct 24, 2023
@davidcook-msft davidcook-msft self-assigned this Feb 26, 2024
farzonl added a commit to farzonl/llvm-project that referenced this issue Feb 26, 2024
This is the start of implementing the lerp intrinsic
https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-lerp
Builtins.td - defines the builtin
hlsl_intrinsics.h - defines the lerp api
DiagnosticSemaKinds.td - needed a new error to be inclusive for more
than two operands.
CGBuiltin.cpp - add the lerp intrinsic lowering
SemaChecking.cpp - type checks for lerp builtin
IntrinsicsDirectX.td - define the lerp intrinsic

this change implements the first half of llvm#70102
farzonl added a commit to farzonl/llvm-project that referenced this issue Feb 27, 2024
This is the start of implementing the lerp intrinsic
https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-lerp
Builtins.td - defines the builtin
hlsl_intrinsics.h - defines the lerp api
DiagnosticSemaKinds.td - needed a new error to be inclusive for more
than two operands.
CGBuiltin.cpp - add the lerp intrinsic lowering
SemaChecking.cpp - type checks for lerp builtin
IntrinsicsDirectX.td - define the lerp intrinsic

this change implements the first half of llvm#70102
python3kgae added a commit that referenced this issue Feb 29, 2024
This is the start of implementing the lerp intrinsic
https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-lerp
Builtins.td - defines the builtin
hlsl_intrinsics.h - defines the lerp api
DiagnosticSemaKinds.td - needed a new error to be inclusive for more
than two operands.
CGBuiltin.cpp - add the lerp intrinsic lowering
SemaChecking.cpp - type checks for lerp builtin
IntrinsicsDirectX.td - define the lerp intrinsic

this change implements the first half of #70102

Co-authored-by: Xiang Li <[email protected]>
@farzonl farzonl assigned farzonl and unassigned davidcook-msft Mar 1, 2024
@farzonl farzonl moved this to In Progress in HLSL Support Mar 1, 2024
@farzonl
Copy link
Member

farzonl commented Mar 1, 2024

The remaining work here is elementwise lerps.

Unlike other elementwise operations lerp tends to do an odd grouping.

for example:
https://godbolt.org/z/ses881sx7

export float3 dot1(float3 x, float3 y, float3 z) {
    return lerp(x, y, z);
}

This groups the z vector and extracts all its elements first

  %z.i0 = extractelement <3 x float> %z, i32 0, !dbg !41 ; line:3 col:12
  %z.i1 = extractelement <3 x float> %z, i32 1, !dbg !41 ; line:3 col:12
  %z.i2 = extractelement <3 x float> %z, i32 2, !dbg !41 ; line:3 col:12
  %x.i0 = extractelement <3 x float> %x, i32 0, !dbg !41 ; line:3 col:12
  %y.i0 = extractelement <3 x float> %y, i32 0, !dbg !41 ; line:3 col:12

It also groups the multiplies and adds

  %.i01 = fmul fast float %z.i0, %.i0, !dbg !41 ; line:3 col:12
  %.i12 = fmul fast float %z.i1, %.i1, !dbg !41 ; line:3 col:12
  %.i23 = fmul fast float %z.i2, %.i2, !dbg !41 ; line:3 col:12
  %.i04 = fadd fast float %.i01, %x.i0, !dbg !41 ; line:3 col:12
  %.i15 = fadd fast float %.i12, %x.i1, !dbg !41 ; line:3 col:12
  %.i26 = fadd fast float %.i23, %x.i2, !dbg !41 ; line:3 col:12

@llvm-beanz
Copy link
Collaborator Author

We do not need clang to match the instruction ordering emitted by DXC.

farzonl added a commit to farzonl/llvm-project that referenced this issue Mar 8, 2024
This change implements lowering for llvm#70076, llvm#70100, llvm#70072, & llvm#70102
CGBuiltin.cpp - - simplify lerp intrinsic
IntrinsicsDirectX.td - simplify lerp intrinsic
SemaChecking.cpp - remove unnecessary check
DXILIntrinsicExpansion.* - add intrinsic to instruction expansion cases
DXILOpLowering.cpp -  make sure DXILIntrinsicExpansion happens first
DirectX.h - changes to support new pass
DirectXTargetMachine.cpp - changes to support new pass
@farzonl farzonl moved this from In Progress to Needs Review in HLSL Support Mar 8, 2024
@damyanp damyanp moved this from Needs Review to Active in HLSL Support Mar 8, 2024
farzonl added a commit to farzonl/llvm-project that referenced this issue Mar 12, 2024
This change implements lowering for llvm#70076, llvm#70100, llvm#70072, & llvm#70102
CGBuiltin.cpp - - simplify lerp intrinsic
IntrinsicsDirectX.td - simplify lerp intrinsic
SemaChecking.cpp - remove unnecessary check
DXILIntrinsicExpansion.* - add intrinsic to instruction expansion cases
DXILOpLowering.cpp -  make sure DXILIntrinsicExpansion happens first
DirectX.h - changes to support new pass
DirectXTargetMachine.cpp - changes to support new pass
@farzonl farzonl linked a pull request Mar 12, 2024 that will close this issue
farzonl added a commit to farzonl/llvm-project that referenced this issue Mar 14, 2024
This change implements lowering for llvm#70076, llvm#70100, llvm#70072, & llvm#70102
`CGBuiltin.cpp` - - simplify `lerp` intrinsic
`IntrinsicsDirectX.td` - simplify `lerp` intrinsic
`SemaChecking.cpp` - remove unnecessary check
`DXILIntrinsicExpansion.*` - add intrinsic to instruction expansion cases
`DXILOpLowering.cpp` -  make sure `DXILIntrinsicExpansion` happens first
`DirectX.h` - changes to support new pass
`DirectXTargetMachine.cpp` - changes to support new pass

Why `any`, and `lerp` as instruction expansion just for DXIL?
-  SPIR-V there is an [OpAny](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpAny)
- SPIR-V has a GLSL lerp extension via [Fmix](https://registry.khronos.org/SPIR-V/specs/1.0/GLSL.std.450.html#FMix)

Why `exp` instruction expansion?
- We have an `exp2` opcode and `exp` reuses that opcode. So instruction expansion is a convenient way to do preprocessing.
- Further SPIR-V has a GLSL exp extension via  [Exp](https://registry.khronos.org/SPIR-V/specs/1.0/GLSL.std.450.html#Exp) and [Exp2](https://registry.khronos.org/SPIR-V/specs/1.0/GLSL.std.450.html#Exp2)

Why `rcp` as instruction expansion?
This one is a bit of the odd man out and might have to move to `cgbuiltins` when we better understand SPIRV requirements. However I included it because it seems like [fast math mode has an AllowRecip flag](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#_fp_fast_math_mode) which lets you compute the reciprocal without performing the division.  We don't have that in DXIL so thought to include it.
farzonl added a commit that referenced this issue Mar 15, 2024
This change implements lowering for #70076, #70100, #70072, & #70102 
`CGBuiltin.cpp` - - simplify `lerp` intrinsic
`IntrinsicsDirectX.td` - simplify `lerp` intrinsic
`SemaChecking.cpp` - remove unnecessary check
`DXILIntrinsicExpansion.*` - add intrinsic to instruction expansion
cases
`DXILOpLowering.cpp` - make sure `DXILIntrinsicExpansion` happens first
`DirectX.h` - changes to support new pass
`DirectXTargetMachine.cpp` - changes to support new pass

Why `any`, and `lerp` as instruction expansion just for DXIL?
- SPIR-V there is an
[OpAny](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpAny)
- SPIR-V has a GLSL lerp extension via
[Fmix](https://registry.khronos.org/SPIR-V/specs/1.0/GLSL.std.450.html#FMix)

Why `exp` instruction expansion?
- We have an `exp2` opcode and `exp` reuses that opcode. So instruction
expansion is a convenient way to do preprocessing.
- Further SPIR-V has a GLSL exp extension via
[Exp](https://registry.khronos.org/SPIR-V/specs/1.0/GLSL.std.450.html#Exp)
and
[Exp2](https://registry.khronos.org/SPIR-V/specs/1.0/GLSL.std.450.html#Exp2)

Why `rcp` as instruction expansion?
This one is a bit of the odd man out and might have to move to
`cgbuiltins` when we better understand SPIRV requirements. However I
included it because it seems like [fast math mode has an AllowRecip
flag](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#_fp_fast_math_mode)
which lets you compute the reciprocal without performing the division.
We don't have that in DXIL so thought to include it.
@github-project-automation github-project-automation bot moved this from Active to Done in HLSL Support Mar 15, 2024
@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:codegen IR generation bugs: mangling, exceptions, etc. backend:DirectX labels Mar 15, 2024
@llvmbot
Copy link
Member

llvmbot commented Mar 15, 2024

@llvm/issue-subscribers-clang-frontend

Author: Chris B (llvm-beanz)

Implement HLSL `lerp` intrinsic:

https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-lerp

@llvmbot
Copy link
Member

llvmbot commented Mar 15, 2024

@llvm/issue-subscribers-clang-codegen

Author: Chris B (llvm-beanz)

Implement HLSL `lerp` intrinsic:

https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-lerp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:DirectX clang:codegen IR generation bugs: mangling, exceptions, etc. clang:frontend Language frontend issues, e.g. anything involving "Sema" HLSL HLSL Language Support
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

5 participants