-
Notifications
You must be signed in to change notification settings - Fork 216
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
Add fast math flag translation for OpenCL std lib #2762
Open
MrSidims
wants to merge
3
commits into
KhronosGroup:main
Choose a base branch
from
MrSidims:fast-math-wip
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+153
−4
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
; RUN: llvm-as %s -o %t.bc | ||
; RUN: llvm-spirv -spirv-text %t.bc -o - | FileCheck %s --check-prefix=CHECK-SPIRV | ||
; RUN: llvm-spirv %t.bc -o %t.spv | ||
; RUN: spirv-val %t.spv | ||
; RUN: llvm-spirv -r %t.spv -o - | llvm-dis -o - | FileCheck %s --check-prefix=CHECK-LLVM-OCL | ||
; RUN: llvm-spirv -r --spirv-target-env=SPV-IR %t.spv -o - | llvm-dis -o - | FileCheck %s --check-prefix=CHECK-LLVM-SPV | ||
|
||
; RUN: llvm-spirv -spirv-text --spirv-max-version=1.5 %t.bc -o - | FileCheck %s --check-prefix=CHECK-SPIRV-NEG | ||
|
||
; CHECK-SPIRV: Decorate [[#FPDec1:]] FPFastMathMode 3 | ||
; CHECK-SPIRV: Decorate [[#FPDec2:]] FPFastMathMode 2 | ||
; CHECK-SPIRV: Decorate [[#FPDec3:]] FPFastMathMode 16 | ||
; CHECK-SPIRV: ExtInst [[#]] [[#FPDec1]] [[#]] fmax [[#]] [[#]] | ||
; CHECK-SPIRV: ExtInst [[#]] [[#FPDec2]] [[#]] fmin [[#]] [[#]] | ||
; CHECK-SPIRV: ExtInst [[#]] [[#FPDec3]] [[#]] fmax [[#]] [[#]] | ||
|
||
; CHECK-SPIRV-NEG-NOT: Decorate [[#]] FPFastMathMode [[#]] | ||
|
||
; CHECK-LLVM-OCL: call nnan ninf spir_func float @_Z4fmaxff(float %[[#]], float %[[#]]) | ||
; CHECK-LLVM-OCL: call ninf spir_func float @_Z4fminff(float %[[#]], float %[[#]]) | ||
; CHECK-LLVM-OCL: call fast spir_func float @_Z4fmaxff(float %[[#]], float %[[#]]) | ||
|
||
; CHECK-LLVM-SPV: call nnan ninf spir_func float @_Z16__spirv_ocl_fmaxff(float %[[#]], float %[[#]]) | ||
; CHECK-LLVM-SPV: call ninf spir_func float @_Z16__spirv_ocl_fminff(float %[[#]], float %[[#]]) | ||
; CHECK-LLVM-SPV: call fast spir_func float @_Z16__spirv_ocl_fmaxff(float %[[#]], float %[[#]]) | ||
|
||
; ModuleID = 'test.bc' | ||
source_filename = "test.cpp" | ||
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64" | ||
target triple = "spir64-unknown-unknown" | ||
|
||
@__spirv_BuiltInGlobalInvocationId = external dso_local local_unnamed_addr addrspace(1) constant <3 x i64>, align 32 | ||
|
||
declare dso_local spir_func noundef nofpclass(nan inf) float @_Z16__spirv_ocl_fmaxff(float noundef nofpclass(nan inf), float noundef nofpclass(nan inf)) local_unnamed_addr | ||
|
||
declare dso_local spir_func noundef nofpclass(nan inf) float @_Z16__spirv_ocl_fminff(float noundef nofpclass(inf), float noundef nofpclass(nan inf)) local_unnamed_addr | ||
|
||
define weak_odr dso_local spir_kernel void @nofpclass_all(ptr addrspace(1) noundef align 4 %_arg_data, ptr addrspace(1) noundef align 4 %_arg_dat1, ptr addrspace(1) noundef align 4 %_arg_dat2) local_unnamed_addr { | ||
entry: | ||
%0 = load i64, ptr addrspace(1) @__spirv_BuiltInGlobalInvocationId, align 32 | ||
%arrayidx.i = getelementptr inbounds float, ptr addrspace(1) %_arg_data, i64 %0 | ||
%arrayidx3.i = getelementptr inbounds float, ptr addrspace(1) %_arg_dat1, i64 %0 | ||
%cmp.i = icmp ult i64 %0, 2147483648 | ||
%arrayidx5.i = getelementptr inbounds float, ptr addrspace(1) %_arg_dat2, i64 %0 | ||
%1 = load float, ptr addrspace(1) %arrayidx3.i, align 4 | ||
%2 = load float, ptr addrspace(1) %arrayidx5.i, align 4 | ||
%call.i.i = tail call spir_func noundef nofpclass(nan inf) float @_Z16__spirv_ocl_fmaxff(float noundef nofpclass(nan inf) %1, float noundef nofpclass(nan inf) %2) | ||
store float %call.i.i, ptr addrspace(1) %arrayidx.i, align 4 | ||
ret void | ||
} | ||
|
||
define weak_odr dso_local spir_kernel void @nofpclass_part(ptr addrspace(1) noundef align 4 %_arg_data, ptr addrspace(1) noundef align 4 %_arg_dat1, ptr addrspace(1) noundef align 4 %_arg_dat2) local_unnamed_addr { | ||
entry: | ||
%0 = load i64, ptr addrspace(1) @__spirv_BuiltInGlobalInvocationId, align 32 | ||
%arrayidx.i = getelementptr inbounds float, ptr addrspace(1) %_arg_data, i64 %0 | ||
%arrayidx3.i = getelementptr inbounds float, ptr addrspace(1) %_arg_dat1, i64 %0 | ||
%cmp.i = icmp ult i64 %0, 2147483648 | ||
%arrayidx5.i = getelementptr inbounds float, ptr addrspace(1) %_arg_dat2, i64 %0 | ||
%1 = load float, ptr addrspace(1) %arrayidx3.i, align 4 | ||
%2 = load float, ptr addrspace(1) %arrayidx5.i, align 4 | ||
%call.i.i = tail call spir_func noundef nofpclass(nan inf) float @_Z16__spirv_ocl_fminff(float noundef nofpclass(inf) %1, float noundef nofpclass(nan inf) %2) | ||
store float %call.i.i, ptr addrspace(1) %arrayidx.i, align 4 | ||
ret void | ||
} | ||
|
||
define weak_odr dso_local spir_kernel void @nofpclass_fast(ptr addrspace(1) noundef align 4 %_arg_data, ptr addrspace(1) noundef align 4 %_arg_dat1, ptr addrspace(1) noundef align 4 %_arg_dat2) local_unnamed_addr { | ||
entry: | ||
%0 = load i64, ptr addrspace(1) @__spirv_BuiltInGlobalInvocationId, align 32 | ||
%arrayidx.i = getelementptr inbounds float, ptr addrspace(1) %_arg_data, i64 %0 | ||
%arrayidx3.i = getelementptr inbounds float, ptr addrspace(1) %_arg_dat1, i64 %0 | ||
%cmp.i = icmp ult i64 %0, 2147483648 | ||
%arrayidx5.i = getelementptr inbounds float, ptr addrspace(1) %_arg_dat2, i64 %0 | ||
%1 = load float, ptr addrspace(1) %arrayidx3.i, align 4 | ||
%2 = load float, ptr addrspace(1) %arrayidx5.i, align 4 | ||
%call.i.i = tail call fast spir_func noundef nofpclass(nan inf) float @_Z16__spirv_ocl_fmaxff(float noundef nofpclass(nan inf) %1, float noundef nofpclass(nan inf) %2) | ||
store float %call.i.i, ptr addrspace(1) %arrayidx.i, align 4 | ||
ret void | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MrSidims, could you check if SPIRVReader handles fast-math flags for FNeg and FCmp correctly, please?
If not, please, file a tracker to follow-up.
Unfortunately, SPIR-V format is not able to translate LLVM semantics correctly. @bashbaug, SPIR-V doesn't allow applying fast-math flags to OpSelect and OpCall instructions, but LLVM IR can have fast math flags attached to
select
andcall
instructions. Do you know if this is going to be addressed by future SPIR-V versions?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a part of SPV_KHR_float_controls2:
It may be a bit too much and some instructions can or should be excluded from the list, but it is what it is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given:
Do we need some updates to the OpenCL Extended Instruction set, or perhaps the OpenCL SPIR-V environment spec? Feel free to file a SPIR-V or OpenCL issue for tracking, if so, so this doesn't slip through the cracks.
We should also add some targeted CTS tests for this functionality, even if they aren't exhaustive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bader according to passing tests https://github.com/KhronosGroup/SPIRV-LLVM-Translator/blob/main/test/transcoding/fcmp.ll and https://github.com/KhronosGroup/SPIRV-LLVM-Translator/blob/main/test/transcoding/fneg.ll it handles correctly. Manual run also doesn't show anything fishy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bashbaug no, https://registry.khronos.org/SPIR-V/specs/unified1/OpenCL.ExtendedInstructionSet.100.pdf already contains the required lines like:
and there are other several references about it.
But I believe Alexey had another question related to
select
andcall
instructions.We should, https://github.com/KhronosGroup/OpenCL-CTS/pulls?q=is%3Apr+is%3Aopen+FPFastMathMode search shows nothing