This is an archive of the discontinued LLVM Phabricator instance.

[LoopUnswitch] unswitch if cond is in select form of and/or as well
ClosedPublic

Authored by aqjune on Mar 2 2021, 1:22 AM.

Details

Summary

Hello all,
I'm trying to fix unsafe propagation of poison values in and/or conditions by using
equivalent select forms (select i1 A, i1 B, i1 false and select i1 A, i1 true, i1 false)
instead.
D93065 has links to patches for this.

This patch allows unswitch to happen if the condition is in this form as well.
collectHomogenousInstGraphLoopInvariants is updated to keep traversal if
Root and the visiting I matches both m_LogicalOr()/m_LogicalAnd().
Other than this, the remaining changes are almost straightforward and simply replaces
Instruction::And/Or check with match(m_LogicalOr()/m_LogicalAnd()).

Diff Detail

Event Timeline

aqjune created this revision.Mar 2 2021, 1:22 AM
aqjune requested review of this revision.Mar 2 2021, 1:22 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 2 2021, 1:22 AM
nikic added a subscriber: nikic.Mar 4 2021, 9:26 AM
nikic added inline comments.
llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch.ll
4212

This doesn't look safe. Previously, if %v1 is false then %cond1 is not used. Now we unconditionally branch on %cond1.

Or is the logic here that unswitching requires insertion of freeze for normal and/or anyway, and we leave this as a miscompile until that is resolved?

aqjune added inline comments.Mar 4 2021, 7:59 PM
llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch.ll
4212

Yes, I think the issue can resolved in a separate patch; anyway loop unswitch requires insertion of freeze.

nikic accepted this revision.Mar 7 2021, 6:49 AM

LGTM

llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
145–150

I don't think the opcode check is needed here.

llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch.ll
4212

Okay, that's fine by me. We did the same for the SimplifyCFG switch transform.

This revision is now accepted and ready to land.Mar 7 2021, 6:49 AM
This revision was landed with ongoing or failed builds.Mar 7 2021, 8:20 AM
This revision was automatically updated to reflect the committed changes.
aqjune marked an inline comment as done.Mar 7 2021, 8:21 AM

I see multiple MLIR JIT failures with this patch or D95026 (they landed too close and the bot picked them together), still looking which one: it only shows up in a bootstrap build.