This is an archive of the discontinued LLVM Phabricator instance.

[SimplifyCFG] Be more aggressive when sinking into unreachable-post-dominated block
ClosedPublic

Authored by lebedev.ri on Jan 11 2022, 12:05 PM.

Details

Summary

I strongly believe we need some variant of this.

The main problem is e.g. that the glibc's assert has 4 parameters,
but the profitability check is only okay with one extra phi node,
so D116692 doesn't even trigger on most of the expected cases.

While that restriction probably makes sense in normal code, if we
are about to run off of a cliff (into an unreachable), this
successor block is unlikely so the cost to setup these PHI nodes
should not be on the hotpath, and shouldn't matter performance-wise.

Likewise, we don't sink if there are unconditional predecessors
UNLESS we'd sink at least one non-speculatable instruction,
which is a performance workaround, but if we are about to run into
unreachable, it shouldn't matter.

One open question is whether to only allow simple control flow
that directly unconditionally leads to unreachable,
or instead check that all the function terminator blocks
that post-dominate the block into which we are sinking
are unreachable (i.e. allow loops).

Diff Detail

Event Timeline

lebedev.ri created this revision.Jan 11 2022, 12:05 PM
lebedev.ri requested review of this revision.Jan 11 2022, 12:05 PM
efriedma added inline comments.Jan 11 2022, 12:23 PM
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
2056

Where is the implementation of IsBlockPostDominatedByDeoptOrUnreachable?

In any case, just from the name, I'm not sure this quite captures the property you want here. There's a difference between sinking into a block that ends in unreachable, vs. a block where the control flow eventually leads to unreachable. There could be a non-trivial amount of code between the code you're transforming, and the actual unreachable instruction. (e.g. the main loop of a program likely ends with a call to exit(), so all blocks in the main function are post-dominated by unreachable).

Actually upload the whole diff this time.

lebedev.ri added inline comments.Jan 11 2022, 12:29 PM
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
2056

Where is the implementation of IsBlockPostDominatedByDeoptOrUnreachable?

Uploaded now.

<...>

Yes, that's the question. Okay, let's not do that.

Don't allow loops inbeteen.

lebedev.ri marked an inline comment as done.Jan 11 2022, 12:34 PM

I think i'm just going to land this particular patch, especially now that it doesn't allow loops.

This revision was not accepted when it landed; it landed in state Needs Review.Jan 13 2022, 12:31 PM
This revision was automatically updated to reflect the committed changes.