This is an archive of the discontinued LLVM Phabricator instance.

[LoopUnrollAndJam] Visit phi operand dependencies in post-order
ClosedPublic

Authored by caojoshua on Dec 16 2022, 5:45 PM.

Details

Summary

Fixes https://github.com/llvm/llvm-project/issues/58565

The previous implementation visits operands in pre-order, but this does
not guarantee an instruction is visited before its uses. This can cause
instructions to be copied in the incorrect order. For example:

a = ...
b = add a, 1
c = add a, b
d = add b, a

Pre-order visits does not guarantee the order in which a and b are
visited. LoopUnrollAndJam may incorrectly insert b before a.

This patch implements post-order visits. By visiting dependencies first,
we guarantee that an instruction's dependencies are visited first.

Diff Detail

Event Timeline

caojoshua created this revision.Dec 16 2022, 5:45 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 16 2022, 5:45 PM
caojoshua edited the summary of this revision. (Show Details)Dec 16 2022, 5:46 PM
caojoshua edited the summary of this revision. (Show Details)Dec 16 2022, 5:54 PM
caojoshua added reviewers: dmgreen, fhahn.
caojoshua published this revision for review.Dec 16 2022, 5:56 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 16 2022, 5:56 PM

Thanks for the patch. It looks OK to me.

llvm/lib/Transforms/Utils/LoopUnrollAndJam.cpp
142–145

It can be:

std::function<bool (Instruction *I)> ProcessInstr = [&](Instruction *I) {

Then is needn't pass ProcessInstr as a parameter.

178

This needs a bit of formatting.

Alternatively, If I is in AftBlocks I don't think it can be in InsertLocBB too? InsertLocBB should a block before the inner loop.

llvm/test/Transforms/LoopUnrollAndJam/dependencies_visit_order.ll
4

Does it need both run lines?

caojoshua updated this revision to Diff 486430.Jan 4 2023, 4:46 PM
  • explicit type of ProcessInstr, do not pass it to itself as parameter
  • if I is in AftBlocks, we don't need to check its parent is in LocBB
  • remove unnecessary run from test
caojoshua marked 3 inline comments as done.Jan 4 2023, 4:47 PM
caojoshua added inline comments.
llvm/lib/Transforms/Utils/LoopUnrollAndJam.cpp
142–145

Yep. Thanks for C++help.

178

Formatted with clang-format. Removed check for InsertLocBB.

llvm/test/Transforms/LoopUnrollAndJam/dependencies_visit_order.ll
4

Nope. Removed second line.

caojoshua marked 3 inline comments as done.Jan 4 2023, 4:48 PM
dmgreen accepted this revision.Jan 5 2023, 12:02 AM

Thanks. LGTM

This revision is now accepted and ready to land.Jan 5 2023, 12:02 AM
This revision was landed with ongoing or failed builds.Jan 5 2023, 12:11 AM
This revision was automatically updated to reflect the committed changes.