This is an archive of the discontinued LLVM Phabricator instance.

[Matrix] Optimize matrix transposes around additions
ClosedPublic

Authored by thegameg on Sep 10 2022, 10:44 PM.

Details

Summary

First, sink the transposes to the operands to simplify redudant
ones. Then, lift them to reduce the number of realized transposes.

(A + B)^T -> A^T + B^T -> (A + B)^T

See tests for more examples.

Diff Detail

Event Timeline

thegameg created this revision.Sep 10 2022, 10:44 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 10 2022, 10:44 PM
thegameg requested review of this revision.Sep 10 2022, 10:44 PM
fhahn added inline comments.Sep 26 2022, 6:41 AM
llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp
865–866

Now also supports adds.

914–915

Now also handles TT additions.

917

Could the lambda just return Value * instead of Instruction *? setShapeInfo and ReplaceAllUsesWith should work with Value *?

921

as the loop body gets bigger, it seems like it would be good to move this to a separate function to reduce the indentation level.

Same for the other loop above.

926

Should we introduce a new m_Transpose helper to make this a bit more compact?

935

This is the same as for the earlier if, right? Can we share this cleanup code? It's easy to miss

thegameg updated this revision to Diff 463029.Sep 26 2022, 3:17 PM
thegameg marked 4 inline comments as done.
thegameg added inline comments.
llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp
917

It could, but we would need to then downcast back to Instruction for the iterator update:

// If we replaced I with a new instruction, continue from there.
if (NewInst)
  II = std::next(BasicBlock::reverse_iterator(NewInst));
926

Good idea, let me do it in a separate patch so it doesn't clutter the diff here.

thegameg updated this revision to Diff 465468.Oct 5 2022, 10:55 AM

Rebase for build to pass.

fhahn accepted this revision.Jan 11 2023, 8:07 AM

LGTM, thanks!

This revision is now accepted and ready to land.Jan 11 2023, 8:07 AM
This revision was landed with ongoing or failed builds.Jan 11 2023, 3:23 PM
This revision was automatically updated to reflect the committed changes.