This is an archive of the discontinued LLVM Phabricator instance.

[LoopFuse] Ensure loops are in loop simplified form under new PM
ClosedPublic

Authored by Narutoworld on Oct 26 2022, 12:23 PM.

Details

Summary

This patch tries to fix this issue.

Loop Fusion (Function Pass) requires loops in simplified form. With legacy-pm, loop-simplify pass is added as a dependency for loop-fusion. But the new pass manager does not always ensure this format. This patch tries to invoke simplifyLoop() on loops that are not in simplified form only for new PM.

Diff Detail

Event Timeline

Narutoworld created this revision.Oct 26 2022, 12:23 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 26 2022, 12:23 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
Narutoworld requested review of this revision.Oct 26 2022, 12:23 PM

test needs a little work, otherwise lg

llvm/lib/Transforms/Scalar/LoopFuse.cpp
557

I guess DependenceInfo doesn't need to be updated because it has no state

599

LI?

llvm/test/Transforms/LoopFusion/ensure_loop_simplify_form.ll
2
2

can you use llvm/utils/update_test_checks.py?

4

remove dso_local

9

we're trying to use undef less, perhaps make this a constant true/false or a function parameter

bjope added inline comments.Oct 26 2022, 3:01 PM
llvm/lib/Transforms/Scalar/LoopFuse.cpp
599

Is this safe. Afaik simplifyLoop might create new sub-loops. So I wonder if it wouldn't be safer to simplify loops before creating the LDT, to ensure the LDT include all simplified loops (and ensure that the LDT isn't having stale entries now when the loop has been transformed (not really sure if that can happen, but if doing all simplification first I wouldn't need to bother about knowing such things)).

Another thing is that this code is shared between the Legacy PM implementation and the new PM implementation. But since the legacy PM has required LoopSimplify before the pass we do not really need to bother about it here.

So I think it could be better to take care of enforcing loop simplify form in LoopFusePass::run instead. Before creating the LoopFuser.

602

I guess you only need to recalculate if simplifyLoop returned true this iteration of the loop over LDT. The Changed variable however is the accumulated state (so that one could be true even if no loops have been simplified, but rather we fused some loops in an earlier iteration).

Narutoworld edited the summary of this revision. (Show Details)

Update patch based on comments.

Narutoworld marked 8 inline comments as done.Oct 27 2022, 1:42 PM
Narutoworld added inline comments.
llvm/lib/Transforms/Scalar/LoopFuse.cpp
557

@aeubanks Thanks for the comment.

I think the DependenceInfo does not need to be updated, since it operates on instructions which are not modified by loopSimplify pass.
I remember I found an example in the codebase.

599

@bjope Thanks for your comment.
I totally agree with your suggestion, just update the patch.

aeubanks added inline comments.Oct 27 2022, 2:08 PM
llvm/lib/Transforms/Scalar/LoopFuse.cpp
2099

I believe this might be wrong because a previously cached PDT isn't going to be invalidated in the analysis manager after the simplifyLoop above. either make simplifyLoop take a PDT and update it accordingly (that wouldn't be trivial), or recalculate the PDT like in the previous iteration of this patch

Narutoworld marked an inline comment as done.

Recalculating post dominator tree for the whole function if any loops are transformed by simplifyLoop().

Narutoworld marked an inline comment as done.Oct 27 2022, 9:02 PM
Narutoworld added inline comments.
llvm/lib/Transforms/Scalar/LoopFuse.cpp
2099

Thanks @aeubanks
I fixes the patch based on your comment.

Narutoworld marked 2 inline comments as done.Oct 28 2022, 6:53 AM
aeubanks accepted this revision.Oct 28 2022, 4:01 PM

lgtm

I'd make the title something along the lines of [LoopFuse] Ensure loops are in loop simplified form under new PM

This revision is now accepted and ready to land.Oct 28 2022, 4:01 PM
Narutoworld retitled this revision from ensure loop-simplifed form when running loop-fusion pass with new-PM to [LoopFuse] Ensure loops are in loop simplified form under new PM.Oct 28 2022, 8:24 PM