This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Fold abs of known sign operand when source is sub
ClosedPublic

Authored by Allen on Jul 27 2023, 7:35 PM.

Diff Detail

Event Timeline

Allen created this revision.Jul 27 2023, 7:35 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 27 2023, 7:35 PM
Allen requested review of this revision.Jul 27 2023, 7:35 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 27 2023, 7:35 PM
Allen updated this revision to Diff 545003.Jul 27 2023, 7:42 PM

Can you add alive2 links?

llvm/test/Transforms/InstCombine/abs-intrinsic.ll
533

Can you split the new tests to a separate commit?

Allen added a comment.Jul 28 2023, 2:08 AM

Can you add alive2 links?

Thanks for your attention, I already put the each alive2 link before the matched cases, so do you mean I also should put that in the MR description ?

nikic added inline comments.Jul 28 2023, 2:18 AM
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
1032

This looks incorrect from the perspective of the getKnownSign() API. You are returning sign 1 for value 0, which is incorrect. It does work out for the abs optimization because -0 == 0, but it may matter in other cases.

Allen updated this revision to Diff 545095.Jul 28 2023, 4:16 AM
Allen edited the summary of this revision. (Show Details)

address comment, move the transform out of the getKnownSign to avoid breaking the perspective of the getKnownSign() API

Allen marked 2 inline comments as done.Jul 28 2023, 4:19 AM
Allen added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
1032

thanks, I move it out of the getKnownSign() API.

llvm/test/Transforms/InstCombine/abs-intrinsic.ll
533
Allen marked 2 inline comments as done.Jul 31 2023, 8:44 PM

ping ?

Can you add alive2 links?

Thanks for your attention, I already put the each alive2 link before the matched cases, so do you mean I also should put that in the MR description ?

Bah sorry for not following up on this, but where are the alive2 links?

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
1540

This is incorrect if the abs allows INT_MIN: https://alive2.llvm.org/ce/z/YNcdRW
It needs to create a new sub w.o nsw.

Allen added a comment.Aug 1 2023, 1:19 AM

Thanks for your attention, I already put the each alive2 link before the matched cases, so do you mean I also should put that in the MR description ?

Bah sorry for not following up on this, but where are the alive2 links?

hi @goldstein.w.n,I usually add the alive2 links before the matched cases because there is more than 1 case, such as line 455 in file Transforms/InstCombine/abs-intrinsic.ll (it is more obvious on D156521)
do you mean it is better to put that in the MR description, too ?

Thanks for your attention, I already put the each alive2 link before the matched cases, so do you mean I also should put that in the MR description ?

Bah sorry for not following up on this, but where are the alive2 links?

hi @goldstein.w.n,I usually add the alive2 links before the matched cases because there is more than 1 case, such as line 455 in file Transforms/InstCombine/abs-intrinsic.ll (it is more obvious on D156521)
do you mean it is better to put that in the MR description, too ?

MR?

I mean the commit message of this patch (and summary in phab if the two should ever desync).

Allen updated this revision to Diff 546334.Aug 2 2023, 12:15 AM
Allen edited the summary of this revision. (Show Details)

Address comment
1、Add proof link alive2, https://alive2.llvm.org/ce/z/KkeEsd
2、abs allows INT_MIN,and its related tests

goldstein.w.n added inline comments.Aug 2 2023, 9:18 AM
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
1521

You also do abs(x-y) --> x - y where x > y.

Allen updated this revision to Diff 546681.Aug 2 2023, 8:29 PM

rebase for 2 new tests

Allen marked 2 inline comments as done.Aug 2 2023, 8:36 PM
Allen added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
1521

Apply your comment, thanks

Allen added a reviewer: arsenm.Aug 4 2023, 3:28 AM
goldstein.w.n added inline comments.Aug 4 2023, 9:14 AM
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
1537

Sorry, noticed this a bit late, but is there a way we can avoid essentially recreating the logic below and instead just add something like getKnownSignOrZero which wraps getKnownSign but also tries SLE if the SLT analysis isn't fruitful? Then you could just replace the function here and we wouldn't need to duplicate so much codes.

Allen updated this revision to Diff 547467.Aug 5 2023, 2:32 AM
Allen marked an inline comment as done.

Address comment, and a new helper function getKnownSignOrZero

Allen marked an inline comment as done.
Allen added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
1537

Thanks for your idea. I try that, it work, but will missing nsw for some case, such as src_sub_abs_sleF.
This is because we don't create the a nsw with the two operands of IIOperand, ie
when we match match(IIOperand, m_NSWSub(m_Value(X), m_Value(Y)))

  • BinaryOperator::CreateNSWSub(Y, X) will final has a extra nsw
  • BinaryOperator::CreateNSWNeg(IIOperand) will miss the nsw, so need a following PR D157187
goldstein.w.n accepted this revision.Aug 5 2023, 8:42 PM

LGTM.

llvm/test/Transforms/InstCombine/abs-intrinsic.ll
578

Imo missing a test with sge i8 %y, %x. Also missing a test where the sub is in the false case (i.e sgt %x, %y but abs in false case).

This revision is now accepted and ready to land.Aug 5 2023, 8:42 PM
Allen updated this revision to Diff 547635.Aug 6 2023, 8:43 PM
Allen marked an inline comment as done.

rebase after 2 new tests sub_abs_sgeT_swap and sub_abs_sgeT_false

Allen marked an inline comment as done.Aug 6 2023, 8:48 PM
Allen added inline comments.
llvm/test/Transforms/InstCombine/abs-intrinsic.ll
578

Done, thanks! Added new cases sub_abs_sgeT_swap and sub_abs_sgeT_false

This revision was landed with ongoing or failed builds.Aug 6 2023, 9:00 PM
This revision was automatically updated to reflect the committed changes.
Allen marked an inline comment as done.