Implement extension SPV_KHR_float_controls2#3475
Implement extension SPV_KHR_float_controls2#3475jmmartinez wants to merge 10 commits intoKhronosGroup:mainfrom
Conversation
We shouldn't, as you have quoted: "This rule implies that a function appearing in both call graphs of two distinct entry points may behave differently in each case.". Runtime should be able to pass fast math controls from a caller to a callee.
I'm a bit worried about bloating size of SPIR-V modules in this case. In general I'd suggest to align behaviour of the translator and SPIR-V backend in areas where it's possible. So I'd expect llvm-spirv's implementation resulting in the same SPIR-V as llvm/llvm-project#146941 aka there should be FPFastMathDefault set. |
|
I'll go on vacation in a few hours, and I'm afraid I will not have time to review this before I leave. Feel free to merge this without my approval, and I'll make sure I review when I'm back, even if it's a post-merge review. I did want to bring up a couple of related issues, though. Hopefully they can be resolved by this PR. |
Then the current implementation should be good, since it doesn't propagate anything.
I see. Then I should fix this implementation to always emit a |
I've addressed this in b691977 . This commit emits an |
|
This one is tricky. I've added a commit related to this, but I'll file a separate patch since this issue is not related to the |
cd6a10d to
57840f3
Compare
Fine with me. Most (if not all) of the folks working on the translator are currently on holidays (including myself), so guess review will be done a bit later :) (unless there is a super urgency - in this case I can take a look before New Year) |
No problem! It's not urgent. |
test/extensions/KHR/SPV_KHR_float_controls2/execution_mode_default.ll
Outdated
Show resolved
Hide resolved
14519f7 to
8fa049e
Compare
MrSidims
left a comment
There was a problem hiding this comment.
LGTM
I'd like to hear from @maarquitos14 before merging.
|
Just in case, I'd like to bring the attention to one of my previous messages about the issue #3125 : Currently, this PR maps LLVM's In the issue it is suggested that we'd better translate Then, if we map LLVM's to SPIRV and back to LLVM we end up with different semantics: To avoid this, we could translate |
Thanks for bringing the attention back. I believe we should do one thing at a time and fix behaviour in unrelated to this PR patch. |
8fa049e to
dd4806c
Compare
I plan to look at this today/tomorrow. |
That works for me, thanks. Just highlighted it here to make sure it worked well with the current implementation. |
@jmmartinez ping me if you do create a separate patch for this. |
maarquitos14
left a comment
There was a problem hiding this comment.
First pass. I'll do a second pass to check tests.
test/extensions/KHR/SPV_KHR_float_controls2/execution_mode_default.ll
Outdated
Show resolved
Hide resolved
test/extensions/KHR/SPV_KHR_float_controls2/execution_mode_id.spvasm
Outdated
Show resolved
Hide resolved
test/extensions/KHR/SPV_KHR_float_controls2/execution_mode_default.ll
Outdated
Show resolved
Hide resolved
test/extensions/KHR/SPV_KHR_float_controls2/extension_not_needed.ll
Outdated
Show resolved
Hide resolved
test/extensions/KHR/SPV_KHR_float_controls2/extension_not_needed_but_used.ll
Outdated
Show resolved
Hide resolved
2abce3e to
1108325
Compare
| // Get the scalar type to handle vector operands. And get the first operand | ||
| // type (instead of the result) due to fcmp instructions. | ||
| Type *FloatType = Inst->getOperand(0)->getType()->getScalarType(); | ||
| auto Func2FMF = FuncToFastMathFlags.find({Inst->getFunction(), FloatType}); |
There was a problem hiding this comment.
I'm tempted to remove this FuncToFastMathFlags stuff.
It's used to set the FPFastMathFlags that are attached to the execution mode to the individual instructions of a kernel.
But since we're preserving the FPFastMathFlags in the metadata; I'm thinking that this is not needed anymore.
@maarquitos14 should I remove this ?
There was a problem hiding this comment.
I believe that this logic should be still placed somewhere as the middleend and backend are unlikely to know about this metadata out of the box and honestly it feels like for optimization passes it's easier to work with individual instruction flags. IMHO resolving ExecutionMode to FP flag right away in the SPIR-V consumer won't harm and actually make implementation lower-level drivers friendly.
There was a problem hiding this comment.
I see @MrSidims's point, but at the same time, I think if we do this, the translation wouldn't be 100% accurate. Maybe I'm too picky. Honestly, I don't have a strong opinion here, let's see what other folks think.
MrSidims
left a comment
There was a problem hiding this comment.
Functionally LGTM, but lets make tests passing :)
test/extensions/KHR/SPV_KHR_float_controls2/execution_mode_contract_off.ll
Outdated
Show resolved
Hide resolved
test/extensions/KHR/SPV_KHR_float_controls2/execution_mode_contract_off.ll
Outdated
Show resolved
Hide resolved
test/extensions/KHR/SPV_KHR_float_controls2/execution_mode_contract_off.ll
Outdated
Show resolved
Hide resolved
My bad ! In my defense... It passed in my machine. There was one matrix test failing though (but also over main so I didn't look much into it). |
2c6901c to
f521131
Compare
maarquitos14
left a comment
There was a problem hiding this comment.
I'll do one extra pass through the whole PR to make sure I didn't miss anything, but I'm quite happy with how it looks now. Great work @jmmartinez!
| // Get the scalar type to handle vector operands. And get the first operand | ||
| // type (instead of the result) due to fcmp instructions. | ||
| Type *FloatType = Inst->getOperand(0)->getType()->getScalarType(); | ||
| auto Func2FMF = FuncToFastMathFlags.find({Inst->getFunction(), FloatType}); |
There was a problem hiding this comment.
I see @MrSidims's point, but at the same time, I think if we do this, the translation wouldn't be 100% accurate. Maybe I'm too picky. Honestly, I don't have a strong opinion here, let's see what other folks think.
maarquitos14
left a comment
There was a problem hiding this comment.
Some more nits and questions, but overall LGTM.
test/extensions/KHR/SPV_KHR_float_controls2/execution_mode_contract_off.ll
Outdated
Show resolved
Hide resolved
test/extensions/KHR/SPV_KHR_float_controls2/execution_mode_id_vec.spvasm
Show resolved
Hide resolved
test/extensions/KHR/SPV_KHR_float_controls2/execution_mode_signed_zero_inf_nan_preserve.ll
Outdated
Show resolved
Hide resolved
test/extensions/KHR/SPV_KHR_float_controls2/execution_mode_signed_zero_inf_nan_preserve.ll
Outdated
Show resolved
Hide resolved
|
Also, in terms of deprecations, the spec says: I think we still need to cover 2. and 5., the rest are covered/not necessary. |
f521131 to
2ff9456
Compare
The |
I've addressed .5 in the last commit, but I probably have to revisit it. |
With this extension, the execution modes `ContractionOff and `SignedZeroInfNanPreserve` are deprecated and we should use `FPFastMathDefault` instead. Additionally, the `FPFastMathMode` mode `Fast` bit is also deprecated.
``` error: ‘V’ may be used uninitialized [-Werror=maybe-uninitialized] ```
c6fce01 to
083a212
Compare
|
CI fully green. There was an error due to |
| // Get the scalar type to handle vector operands. And get the first operand | ||
| // type (instead of the result) due to fcmp instructions. | ||
| Type *FloatType = Inst->getOperand(0)->getType()->getScalarType(); | ||
| auto Func2FMF = FuncToFastMathFlags.find({Inst->getFunction(), FloatType}); |
First attempt at implementing SPV_KHR_float_controls2.
ExecutionModeFPFastMathDefaultfor every kernel, and if instructions in that kernel do not specify a particularFPFastMathMode, we use the kernel one.ExecutionModeFPFastMathDefaultis not propagated down to the callees.afnflag. If we mapfadd fast float %a, %bto SPIRV and back, it becomesfadd reassoc nnan ninf nsz arcp contract float %a, %blosing theafnflag.ContractionOffandSignedZeroInfNanPreserveare translated to aExecutionModeFPFastMathDefaultwith all flags set to 0.