-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[SCCP] Add context to SimplifyQuery #100831
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
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-llvm-transforms @llvm/pr-subscribers-function-specialization Author: Thomas Hashem (hashemthomas1) ChangesFull diff: https://github.com/llvm/llvm-project/pull/100831.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/Utils/SCCPSolver.cpp b/llvm/lib/Transforms/Utils/SCCPSolver.cpp
index 2336466a25a17..c944859cc69b8 100644
--- a/llvm/lib/Transforms/Utils/SCCPSolver.cpp
+++ b/llvm/lib/Transforms/Utils/SCCPSolver.cpp
@@ -1503,7 +1503,7 @@ void SCCPInstVisitor::visitBinaryOperator(Instruction &I) {
Value *V2 = SCCPSolver::isConstant(V2State)
? getConstant(V2State, I.getOperand(1)->getType())
: I.getOperand(1);
- Value *R = simplifyBinOp(I.getOpcode(), V1, V2, SimplifyQuery(DL));
+ Value *R = simplifyBinOp(I.getOpcode(), V1, V2, SimplifyQuery(DL, &I));
auto *C = dyn_cast_or_null<Constant>(R);
if (C) {
// Conservatively assume that the result may be based on operands that may
diff --git a/llvm/test/Transforms/SCCP/float-denormal-simplification.ll b/llvm/test/Transforms/SCCP/float-denormal-simplification.ll
new file mode 100644
index 0000000000000..7a1461ab068a7
--- /dev/null
+++ b/llvm/test/Transforms/SCCP/float-denormal-simplification.ll
@@ -0,0 +1,21 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -passes=sccp -S %s | FileCheck %s
+
+define dso_local float @test_ieee() #0 {
+; CHECK-LABEL: @test_ieee(
+; CHECK-NEXT: ret float 0x36F4000000000000
+;
+ %1 = fmul float 2.802596928649634e-44, 2.000000e+00
+ ret float %1
+}
+
+define dso_local float @test_preserve_sign() #1 {
+; CHECK-LABEL: @test_preserve_sign(
+; CHECK-NEXT: ret float 0.000000e+00
+;
+ %1 = fmul float 2.802596928649634e-44, 2.000000e+00
+ ret float %1
+}
+
+attributes #0 = {"denormal-fp-math"="ieee,ieee"}
+attributes #1 = {"denormal-fp-math"="preserve-sign,preserve-sign"}
|
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.
LGTM, though we probably should probably refuse to fold denormals if context is missing, the current behavior doesn't make a lot of sense. But this change is good in any case.
b9b4817
to
6d1a724
Compare
If this PR is fully approved please go ahead an merge it as I don't have write access |
@hashemthomas1 Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested Please check whether problems have been caused by your change specifically, as How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
Disagree, we should never be flushing denormals for non-strict constants folding |
; CHECK-LABEL: @test_ieee( | ||
; CHECK-NEXT: ret float 0x36F4000000000000 | ||
; | ||
%1 = fmul float 2.802596928649634e-44, 2.000000e+00 |
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.
I would prefer a more meaningful test. We probably shouldn't be making constant folding dependent on the expected FP environment of the context function, and never flush them. This test should at minimum change to use a canonicalize intrinsic
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.
My goal was to imitate the tests in: /llvm/test/Transforms/InstSimplify/constant-fold-fp-denormal.ll
since they're minimal and reproduce the "bug" in IPSCCP due to the missing context.
Sorry, but I didn't understand the comment on how it could be more meaningful.
But I do understand your general point of view, my goal for now was to at least make things consistent.
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.
I mean passing the context should enable more optimizations than this. Replacing the fmul with a canonicalize should show the same thing, but be more true to the IR rules around where the flush would be guaranteed
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.
Understood.
I can work on adding tests that show other optimizations enabled by the context.
Replacing fmul with llvm.canonicalize doesn't reproduce the inconsistent behavior because the context was added to simplifyBinOp (canonicalize is handled elsewhere).
But, if you think the tests shouldn't look like this regardless, I'll work on changing them as well 👍
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.
I mean passing the context should enable more optimizations than this
After diving more into the code it felt like the amount of optimizations is scarce for two reasons:
- The binary operation that reaches SimplifyBinOp in this MR must have at least one operand that has the ValueLatticeElement tag: constant.
- Integers has ValueLatticeElement tag: constantrange.
This leaves: floats and vector types as candidates for other optimizations that can appear in the test.
Regarding floats, I didn't find other optimizations that use the context passed to the function for other than deciding to flush the inputs/output. The context was used to find the denormal-fp-math attribute.
Regarding vector types, I don't have much experience in that area, but I couldn't write a test with a missed optimization opportunity in case the context is missing.
Therefore, with my currently limited knowledge of the code in general, I couldn't come up with better tests.
That doesn't match at all what was implemented in https://reviews.llvm.org/D116952. |
Correct, I think that should be reverted |
Is this worth backporting to 19.x? |
No description provided.