This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Optimize multiplication where both operands are negated
ClosedPublic

Authored by zjaffal on Sep 12 2022, 6:44 AM.

Details

Summary

Handle the case where both operands are negated in matrix multiplication

Diff Detail

Event Timeline

zjaffal created this revision.Sep 12 2022, 6:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 12 2022, 6:44 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
zjaffal requested review of this revision.Sep 12 2022, 6:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 12 2022, 6:44 AM
zjaffal retitled this revision from [InstCombine] Optmize multiplication where both operands are negated to [InstCombine] Optimize multiplication where both operands are negated.
spatel added inline comments.Sep 12 2022, 9:15 AM
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
1870–1875

I commented earlier that we prefer creating a new instruction in transforms like this, but we might as well follow the code structure of this related transform. It doesn't seem to bother the worklist iteration.

llvm/test/Transforms/InstCombine/matrix-multiplication-negation.ll
126

Fix test comment.
Add FMF to at least one of these tests, so we can see that those are propagated.

138–150

Fix test comment.
Add extra uses to at least one of these tests (they should not change the transform).

thegameg added inline comments.Sep 12 2022, 9:29 AM
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
1837

Do we need to handle integers too? (m_Neg)

zjaffal updated this revision to Diff 459688.Sep 13 2022, 2:19 AM

update transformations

spatel added inline comments.Sep 13 2022, 6:15 AM
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
1834

I read "~" as "bitwise not". I expect this comment to be something like this:
// -[A] * -[B] --> [A] * [B]

1838–1839

The casting is unnecessary, and the code doesn't match the code comment. Capture the source values as "A" and "B" inside the match() statement (the same way it is done in the existing variants of this transform).

zjaffal updated this revision to Diff 459744.Sep 13 2022, 7:32 AM

use patterns

zjaffal marked 2 inline comments as done.Sep 13 2022, 7:32 AM
fhahn accepted this revision.Sep 13 2022, 8:25 AM

LGTM, with an additional nit. Let's wait a bit with landing in case there are any further comments.

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

nit: comment should be full sentences: capitalize first letter, end with period.

This revision is now accepted and ready to land.Sep 13 2022, 8:25 AM
spatel added inline comments.Sep 13 2022, 8:31 AM
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
1833

IMO, the words are just noise. The formula is clear enough.

1837

Again - it's easier to read when the code matches the comments:
Value *A, *B;

zjaffal updated this revision to Diff 460018.Sep 14 2022, 3:13 AM

rename variables

zjaffal updated this revision to Diff 460020.Sep 14 2022, 3:22 AM

rename variables

fhahn added inline comments.Sep 14 2022, 3:25 AM
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
1833

According to other comments in the file, that should just be -A * -B -> A * B

zjaffal updated this revision to Diff 460022.Sep 14 2022, 3:36 AM

Variable names match comments

spatel added inline comments.Sep 14 2022, 7:36 AM
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
1833

The "->" suggestion was not addressed, and we're back to "Optimise" which was requested to be changed/deleted.

Also, renaming the operands "NegA" and "NegB" doesn't make things clearer. In the planned next step to this patch, one of those names will be known to be incorrect. The extra words/code are not helping things.

Other than that, I have no more comments for this patch.

zjaffal updated this revision to Diff 460094.Sep 14 2022, 7:48 AM
zjaffal updated this revision to Diff 460098.Sep 14 2022, 8:03 AM
This revision was landed with ongoing or failed builds.Sep 14 2022, 8:30 AM
This revision was automatically updated to reflect the committed changes.