This is an archive of the discontinued LLVM Phabricator instance.

[mlir][SCF] Add canonicalization pattern for scf::For to eliminate yields that just forward.
ClosedPublic

Authored by nicolasvasilache on Nov 4 2020, 2:02 AM.

Details

Summary

For instance:

func @for_yields_3(%lb : index, %ub : index, %step : index) -> (i32, i32, i32) {
  %a = call @make_i32() : () -> (i32)
  %b = call @make_i32() : () -> (i32)
  %r:3 = scf.for %i = %lb to %ub step %step iter_args(%0 = %a, %1 = %a, %2 = %b) -> (i32, i32, i32) {
    %c = call @make_i32() : () -> (i32)
    scf.yield %0, %c, %2 : i32, i32, i32
  }
  return %r#0, %r#1, %r#2 : i32, i32, i32
}

Canonicalizes as:

func @for_yields_3(%arg0: index, %arg1: index, %arg2: index) -> (i32, i32, i32) {
  %0 = call @make_i32() : () -> i32
  %1 = call @make_i32() : () -> i32
  %2 = scf.for %arg3 = %arg0 to %arg1 step %arg2 iter_args(%arg4 = %0) -> (i32) {
    %3 = call @make_i32() : () -> i32
    scf.yield %3 : i32
  }
  return %0, %2, %1 : i32, i32, i32
}

Diff Detail

Event Timeline

nicolasvasilache requested review of this revision.Nov 4 2020, 2:02 AM
ftynse accepted this revision.Nov 4 2020, 2:49 AM
ftynse added inline comments.
mlir/lib/Dialect/SCF/SCF.cpp
365

Nit: spurious /

This revision is now accepted and ready to land.Nov 4 2020, 2:49 AM
bkramer accepted this revision.Nov 4 2020, 3:30 AM
nicolasvasilache marked an inline comment as done.Nov 4 2020, 4:00 AM
nicolasvasilache added inline comments.
mlir/lib/Dialect/SCF/SCF.cpp
365

Addressed before landing but did not update phab.