This is an archive of the discontinued LLVM Phabricator instance.

[MLIR][SCF] Fix nested if merging bug
ClosedPublic

Authored by wsmoses on Mar 20 2022, 5:35 PM.

Details

Summary

The current nested if merging has a bug. Specifically, consider the following code:

%r = scf.if %arg3 -> (i32) {
  scf.if %arg1 {
    "test.op"() : () -> ()
  }
  scf.yield %arg0 : i32
} else {
  scf.yield %arg2 : i32
}

When the above gets merged, it will become:

%r = scf.if %arg3 && %arg1-> (i32) {
  "test.op"() : () -> ()
  scf.yield %arg0 : i32
} else {
  scf.yield %arg2 : i32
}

However, this means that when only %arg3 is true, we will incorrectly return %arg2 instead
of %arg0. This change updates the behavior of the pass to only enable nested if merging where
the outer yield contains only values from the inner if, or values defined outside of the if.

In the case of the latter, they can turned into a select of only the outer if condition, thus
maintaining correctness.

Diff Detail

Event Timeline

wsmoses created this revision.Mar 20 2022, 5:35 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 20 2022, 5:35 PM
wsmoses requested review of this revision.Mar 20 2022, 5:35 PM
ftynse edited the summary of this revision. (Show Details)Mar 21 2022, 2:08 AM
ftynse accepted this revision.Mar 21 2022, 2:14 AM
ftynse added inline comments.
mlir/lib/Dialect/SCF/SCF.cpp
1791–1807

Nit: continue here and drop the else + unindent?

1799

Did you mean can be defined outside the "if"?

This revision is now accepted and ready to land.Mar 21 2022, 2:14 AM
This revision was automatically updated to reflect the committed changes.