This is an archive of the discontinued LLVM Phabricator instance.

[MLIR][SCF] Combine nested ifs with yields
ClosedPublic

Authored by wsmoses on Mar 17 2022, 10:02 AM.

Details

Summary

This patch extends the existing combine nested if
combination canonicalization to also handle ifs which
yield values

Diff Detail

Event Timeline

wsmoses created this revision.Mar 17 2022, 10:02 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 17 2022, 10:02 AM
wsmoses requested review of this revision.Mar 17 2022, 10:02 AM
ftynse edited the summary of this revision. (Show Details)Mar 18 2022, 2:58 AM
ftynse requested changes to this revision.Mar 18 2022, 3:08 AM

This needs to properly handle the cases where either the outer or the inner "scf.if" is missing the "else" block.

mlir/lib/Dialect/SCF/SCF.cpp
1768–1769

llvm::append_range(elseYield, op.elseYield().getOperands()) should work

1771

Nit: consider putting the operation name in quotes/backticks or prefixing it with scf. to make it easier to differentiate from the regular word that is also used here.

1777–1778

It shouldn't be necessary to loop over operation results, tup.value().cast<OpResult>().getResultNumber() gives you the position of the result in the list.

1779

There doesn't seem to be a check that nestedIf has the "else" block, so this may assert.

1780

elseYield may be empty if op doesn't have an "else" block, which could lead to out-of-bounds access here.

This revision now requires changes to proceed.Mar 18 2022, 3:08 AM
bondhugula added inline comments.
mlir/lib/Dialect/SCF/SCF.cpp
1802

!empty()

wsmoses updated this revision to Diff 416521.Mar 18 2022, 8:59 AM
wsmoses marked 6 inline comments as done.

Address comments and add test without else

mlir/lib/Dialect/SCF/SCF.cpp
1779

If the if returns a value it must have an else block, by definition.

1780

If thenYield contains values, the outer if returns values, and thus elseYield must also contain (the same number of) values.

ftynse accepted this revision.Mar 18 2022, 9:54 AM
ftynse added inline comments.
mlir/lib/Dialect/SCF/SCF.cpp
1779

Mind adding this to a comment above the "for", it's not obvious from the context?

This revision is now accepted and ready to land.Mar 18 2022, 9:54 AM
This revision was automatically updated to reflect the committed changes.
wsmoses marked an inline comment as done.