This is an archive of the discontinued LLVM Phabricator instance.

[mlir][scf] Add an IndexSwitchOp
ClosedPublic

Authored by Mogball on Oct 14 2022, 5:18 PM.

Details

Summary

The scf.index_switch is a control-flow operation that branches to one of the
given regions based on the values of the argument and the cases. The
argument is always of type index.

Example:

mlir
%0 = scf.index_switch %arg0 -> i32
case 2 {
  %1 = arith.constant 10 : i32
  scf.yield %1 : i32
}
case 5 {
  %2 = arith.constant 20 : i32
  scf.yield %2 : i32
}
default {
  %3 = arith.constant 30 : i32
  scf.yield %3 : i32
}

Diff Detail

Event Timeline

Mogball created this revision.Oct 14 2022, 5:18 PM
Mogball requested review of this revision.Oct 14 2022, 5:18 PM
Mogball updated this revision to Diff 467983.Oct 14 2022, 5:44 PM

put the trait on the wrong op

Mogball updated this revision to Diff 468089.Oct 16 2022, 11:28 AM

actually allow any integer arg

Mogball updated this revision to Diff 468091.Oct 16 2022, 11:31 AM

fix mis-merge

Nice, I could have sworn we discussed an op like this ... It's a bit far out of my cache. I think this as is is equivalent than a nested sequence of scf conditionals wrt type constraints, is that accurate?

mlir/include/mlir/Dialect/SCF/IR/SCFOps.td
1022

Why is this allowing signless ints too?

1052–1053

Nit: keep sorted

mlir/lib/Dialect/SCF/IR/SCF.cpp
3435

So in the attribute we don't have fixed type but require all to be same? Wouldn't a dense array attr represent that Bette?

mlir/test/Dialect/SCF/invalid.mlir
431

Ensure this is in order too once switched

Mogball updated this revision to Diff 468115.Oct 16 2022, 8:34 PM
Mogball marked 4 inline comments as done.

review comments: revert back to index + dense array.

Also implement RegionBranchOpInterface and just use that for verification

I think this as is is equivalent than a nested sequence of scf conditionals wrt type constraints, is that accurate?

Yes. Now that you mention it, I actually remember that RFC too. I can add cf.switch as per that RFC as well since that's what I was planning to do next anyways.

mlir/include/mlir/Dialect/SCF/IR/SCFOps.td
1022

I had it fixed to index originally. I'll change it back.

cf.switch already exists. I would also expect an update posted to that RFC to make sure that agreement has been reached as per how scf.switch should be structured before moving forward.

cf.switch already exists.

Oh right.

I would also expect an update posted to that RFC to make sure that agreement has been reached as per how scf.switch should be structured before moving forward.

I noticed there was a hangup on what exactly scf.switch should encompass. I posted this patch to that thread. IMO more "involved" switch operations can always be added and called something different.

Mogball updated this revision to Diff 468116.Oct 16 2022, 9:10 PM

implement getRegionInvocationBounds while I'm at it

Mogball updated this revision to Diff 468119.Oct 16 2022, 9:37 PM

use a regular verifier to get better error messages

jpienaar accepted this revision.Oct 17 2022, 7:16 AM

Nice, I think this makes sense. Keeping it directed & inline with the others here. I would love pattern matching, but that indeed feels like a different level. Perhaps give a workday or two for folks to chime in.

mlir/lib/Dialect/SCF/IR/SCF.cpp
3510

Ha nice.

This revision is now accepted and ready to land.Oct 17 2022, 7:16 AM

I noticed there was a hangup on what exactly scf.switch should encompass. I posted this patch to that thread. IMO more "involved" switch operations can always be added and called something different.

Shall we name this one scf.indexed_switch or something unambiguous if we anticipate more a possible general switch?

mlir/include/mlir/Dialect/SCF/IR/SCFOps.td
1003

Description seems terse, for example it does not say anything about the default case.

Mogball updated this revision to Diff 468683.Oct 18 2022, 1:52 PM

rename to index_switch and expand description

Mogball retitled this revision from [mlir][scf] Add a SwitchOp to [mlir][scf] Add an IndexSwitchOp.Oct 18 2022, 1:53 PM
Mogball edited the summary of this revision. (Show Details)
Mogball marked 2 inline comments as done.Oct 18 2022, 1:53 PM
This revision was automatically updated to reflect the committed changes.

I noticed there was a hangup on what exactly scf.switch should encompass. I posted this patch to that thread. IMO more "involved" switch operations can always be added and called something different.

Shall we name this one scf.indexed_switch or something unambiguous if we anticipate more a possible general switch?

I don't think index_switch is a great name here. It kind of awkwardly diverges from the naming of the other ops, e.g. we don't have index_for even though it's not a truly general for loop. Can we consider renaming this to just switch?