Skip to content

[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

Merged
merged 1 commit into from
Jul 29, 2024
Merged

Conversation

hashemthomas1
Copy link
Contributor

No description provided.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

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
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

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.

@llvmbot
Copy link
Member

llvmbot commented Jul 26, 2024

@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-function-specialization

Author: Thomas Hashem (hashemthomas1)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/100831.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/SCCPSolver.cpp (+1-1)
  • (added) llvm/test/Transforms/SCCP/float-denormal-simplification.ll (+21)
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"}

Copy link
Contributor

@nikic nikic left a 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.

@hashemthomas1
Copy link
Contributor Author

If this PR is fully approved please go ahead an merge it as I don't have write access

@nikic nikic merged commit e6fa09f into llvm:main Jul 29, 2024
7 checks passed
Copy link

@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
by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

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.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@arsenm
Copy link
Contributor

arsenm commented Jul 29, 2024

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.

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
Copy link
Contributor

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

Copy link
Contributor Author

@hashemthomas1 hashemthomas1 Jul 29, 2024

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.

Copy link
Contributor

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

Copy link
Contributor Author

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 👍

Copy link
Contributor Author

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:

  1. The binary operation that reaches SimplifyBinOp in this MR must have at least one operand that has the ValueLatticeElement tag: constant.
  2. 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.

@nikic
Copy link
Contributor

nikic commented Jul 29, 2024

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.

Disagree, we should never be flushing denormals for non-strict constants folding

That doesn't match at all what was implemented in https://reviews.llvm.org/D116952.

@arsenm
Copy link
Contributor

arsenm commented Jul 29, 2024

That doesn't match at all what was implemented in https://reviews.llvm.org/D116952.

Correct, I think that should be reverted

@hashemthomas1 hashemthomas1 deleted the sccp_simplify_Q branch July 30, 2024 07:17
@AZero13
Copy link
Contributor

AZero13 commented Aug 2, 2024

Is this worth backporting to 19.x?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants