This is an archive of the discontinued LLVM Phabricator instance.

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

Authored by bcl5980 on Mar 18 2022, 8:43 AM.

Details

Summary

When abs source comes from (x - y) , we need to check if exist x > y dominating the instruction or not.

Fix https://github.com/llvm/llvm-project/issues/54132

Diff Detail

Event Timeline

bcl5980 created this revision.Mar 18 2022, 8:43 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 18 2022, 8:43 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
bcl5980 requested review of this revision.Mar 18 2022, 8:43 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 18 2022, 8:43 AM
bcl5980 updated this revision to Diff 416551.Mar 18 2022, 10:12 AM

update test

RKSimon added a subscriber: RKSimon.
RKSimon added inline comments.
llvm/test/Transforms/InstCombine/abs-intrinsic.ll
430

where are the actual codegen checks?

bcl5980 added inline comments.Mar 18 2022, 5:24 PM
llvm/test/Transforms/InstCombine/abs-intrinsic.ll
430

; CHECK-NOT: llvm.abs.i32
If the code works, llvm.abs.i32 will be removed.

craig.topper added inline comments.
llvm/test/Transforms/InstCombine/abs-intrinsic.ll
430

Checks in InstCombine tests are usually autogenerated with utils/update_test_checks.py as the top of this file says. Please use that.

bcl5980 updated this revision to Diff 416649.Mar 18 2022, 6:36 PM

update test

bcl5980 updated this revision to Diff 416650.Mar 18 2022, 6:37 PM

update test

bcl5980 added inline comments.Mar 18 2022, 6:43 PM
llvm/test/Transforms/InstCombine/abs-intrinsic.ll
430

I'm sorry for the terrible test case. I'm an beginner on llvm so can someone tell me how the update_test_checks.py works and what should I do next? Or is there any document or sample to show how it works?

craig.topper added inline comments.Mar 18 2022, 6:53 PM
llvm/test/Transforms/InstCombine/abs-intrinsic.ll
445

I think the nsw flag is required to make this transform valid, but code doesn't check for it.

bcl5980 updated this revision to Diff 416652.Mar 18 2022, 7:04 PM

add nsw check

bcl5980 marked an inline comment as done.Mar 19 2022, 6:57 AM
bcl5980 marked an inline comment as done.
llvm/test/Transforms/InstCombine/abs-intrinsic.ll
430

With my build directory at llvm-project/build, I run something like this. You can name the tests you want to autogenerate as parameters.

./llvm/utils/update_test_checks.py --opt-binary=build/bin/opt llvm/test/Transforms/InstCombine/abs-intrinsic.ll
bcl5980 updated this revision to Diff 416886.Mar 21 2022, 4:04 AM

use update_test_checks.py to autogen test check golden

bcl5980 marked an inline comment as done.Mar 21 2022, 4:07 AM
bcl5980 added inline comments.
llvm/test/Transforms/InstCombine/abs-intrinsic.ll
430

Thanks for the sample

peterwaller-arm accepted this revision.Mar 21 2022, 4:15 AM

Seems reasonable to me.

This revision is now accepted and ready to land.Mar 21 2022, 4:15 AM
spatel requested changes to this revision.Mar 21 2022, 7:53 AM
spatel added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
815–817

This code would be shorter using 'match':

Value *X, *Y;
if (match(Op, m_NSWSub(m_Value(X), m_Value(Y))))
  return isImpliedByDomCondition(ICmpInst::ICMP_SLT, X, Y, CxtI, DL);
llvm/test/Transforms/InstCombine/abs-intrinsic.ll
429

We should have at least 3 more tests:

  1. Check when the dominating condition is 'false' - change the icmp predicate to 'slt'?
  2. A negative test (no transform should be made) for wrong predicate - change the icmp predicate to 'ugt' or some other value to make the transform invalid.
  3. A negative test (no transform should be made) when there is no 'nsw' on the sub.
This revision now requires changes to proceed.Mar 21 2022, 7:53 AM
bcl5980 updated this revision to Diff 416956.Mar 21 2022, 8:21 AM
bcl5980 marked an inline comment as done.

update based on spatel's suggestion

  1. use match pattern
  2. add more tests

Do you have commit access? It would be good to push the original tests as a preliminary patch.

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

Why did the 'add' operand change from %y to %x?

Do you have commit access? It would be good to push the original tests as a preliminary patch.

I have no commit access, can you help me to do this?
name: chenglin.bi
email: chenglin.bi@cixcomputing.com

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

when I change the condition code from sgt to slt, it become x < y
so abs(x - y) become y - x, I need to change the pattern to y - x + x then return only y
sub_abs_gt :
[ x>y ? abs (x-y) + y : 0 ] -> [ x > y ? x : 0 ]
sub_abs_lt :
[ x< y ? abs (x-y) + x : 0 ] -> [ x < y ? y : 0 ]

Allen added a subscriber: Allen.Mar 21 2022, 8:56 AM

Do you have commit access? It would be good to push the original tests as a preliminary patch.

I have no commit access, can you help me to do this?
name: chenglin.bi
email: chenglin.bi@cixcomputing.com

Sure - let's make one small change, and then I can commit for you.

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

I see. It's fine to include a test with the 'add' from the motivating bug report, but the 'add' is not part of the minimal transform. We should remove it completely from at least these first 2 tests, so we can check that only this transform is tested.

bcl5980 updated this revision to Diff 417178.Mar 21 2022, 9:04 PM

minimize tests

bcl5980 marked 4 inline comments as done.Mar 21 2022, 9:05 PM
bcl5980 marked an inline comment as done.

I committed the tests with baseline CHECK lines:
01a2ba5dfbee
...and one more test:
c4d74a93f65c

Please rebase and test with Alive2 that we have correct behavior when the 2nd parameter to abs is 'false'.

spatel added inline comments.Mar 22 2022, 8:35 AM
llvm/test/Transforms/InstCombine/abs-intrinsic.ll
461

This should have propagated the 'nsw', right?
https://alive2.llvm.org/ce/z/5q5YHb

If this is an existing problem (but might be invisible before this patch), I think it is ok to mark this with a 'TODO' comment.

bcl5980 updated this revision to Diff 417314.Mar 22 2022, 9:00 AM

Fix [-(x - y) -> y -x ] missing nsw flag

spatel added inline comments.Mar 22 2022, 11:26 AM
llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
241 ↗(On Diff #417314)

This change should be an independent patch with its own minimal tests:
https://alive2.llvm.org/ce/z/Ppps-u

bcl5980 updated this revision to Diff 417441.Mar 22 2022, 4:59 PM

remove neg nsw flag fix. Will fix it on next commit

spatel accepted this revision.Mar 23 2022, 6:12 AM

LGTM

This revision is now accepted and ready to land.Mar 23 2022, 6:12 AM
This revision was landed with ongoing or failed builds.Mar 23 2022, 12:21 PM
This revision was automatically updated to reflect the committed changes.
bcl5980 added a comment.EditedMar 27 2022, 8:33 AM

@spatel, Thanks for the detail review.