This is an archive of the discontinued LLVM Phabricator instance.

[MLIR][SCF] Allow combining subsequent if statements that yield & negated condition
ClosedPublic

Authored by wsmoses on Mar 3 2022, 11:04 AM.

Details

Summary

This patch extends the existing if combining canonicalization to also handle the case where a value returned by the first if is used within the body of the second if.

This patch also extends if combining to support if's whose conditions are logical negations of each other.

Diff Detail

Event Timeline

wsmoses created this revision.Mar 3 2022, 11:04 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 3 2022, 11:04 AM
wsmoses requested review of this revision.Mar 3 2022, 11:04 AM
wsmoses updated this revision to Diff 412803.Mar 3 2022, 12:07 PM

Add support for negated conditions

wsmoses retitled this revision from [MLIR][SCF] Allow combining subsequent if statements where the first if statement returns a value to [MLIR][SCF] Allow combining subsequent if statements that yield & negated condition.Mar 3 2022, 12:08 PM
wsmoses edited the summary of this revision. (Show Details)
ftynse accepted this revision.Mar 4 2022, 5:19 AM

LGTM with comments addressed.

mlir/lib/Dialect/SCF/SCF.cpp
1543–1558

Something like rewriter.replaceOpWithinBlock or rewriter.replaceOpWithIf should work here.

mlir/test/Dialect/SCF/canonicalize.mlir
1140

Please don't match exact value names (%arg0), here and below.

This revision is now accepted and ready to land.Mar 4 2022, 5:19 AM
wsmoses marked an inline comment as done.Mar 4 2022, 8:11 AM
wsmoses added inline comments.
mlir/lib/Dialect/SCF/SCF.cpp
1543–1558

It's not clear to me this is possible as the values yielded by the if may be block arguments and thus not an op. I don't believe those methods work to replace a value (as opposed to an operation).

wsmoses updated this revision to Diff 413014.Mar 4 2022, 8:17 AM

Fix test

ftynse added inline comments.Mar 4 2022, 8:45 AM
mlir/lib/Dialect/SCF/SCF.cpp
1543–1558

This code is updating the uses of results of prevIf, not the uses of values that are yielded. So I expect something like

rewriter.replaceOpWithinBlock(prevIf, prevIf.thenYield().getOperands(), nextThen);
rewriter.replaceOpWithinBlock(prevIf, prevElseYielded, nextElse);

to work.