[Mlir-commits] [mlir] [mlir][emitc][cf] add 'cf.switch' support in CppEmitter (PR #101478)

Simon Camphausen llvmlistbot at llvm.org
Fri Aug 2 05:25:34 PDT 2024


simon-camp wrote:

> I'd add some background, since @EtoAndruwa works on my team.
> 
> The only reason we went for `cf.switch` is that emitc already has `cf.br` and `cf.cond_br`. In general, I'd favor an `emitc.switch` operation which always lowers without `goto` and uses simple `break`, so the `case` labels are not terminators and you can not _jump_ from them, so it'll look more like `emitc.if`, from what I understand in terms of child basic blocks.
> 
> In general, I'd prefer if `cf` dialect will lower to `emitc` dialect first and then be used by emitter, since it'll result in improved code gen and you won't need to account for `cf` when writing passes, since you can require lowering to `emitc` first.
> 
> > We can add a switch op, but it shouldn't be a terminator in my opinion. So we would be able to lower scf.index_switch to it, but we couldn't use it for cf.switch (as of now).
> 
> I wonder how the `scf.yield` will be lowered (though, I guess you can just specify that you can not `yield` result and say that each `case` should end with `emitc.break`, which yields nothing), though, you can likely define a variable just before the `switch` and assign to it, but all in all, `scf.index_swicth` like `emitc.switch` looks more appealing to us for already said `goto` stuff above, so +1 here.

That's exactly how `scf.if` is lowered to `emitc.if`; by declaring result variables up front and lowering `scf.yield` into assignments and an empty `emitc.yield` as terminator.

Do your `cf.switch` ops come from a lowering of `scf.index_switch` ops? Then adding an `emitc.switch` op and adding a pattern to SCFToEmitC conversion would be easiest, I guess.

https://github.com/llvm/llvm-project/pull/101478


More information about the Mlir-commits mailing list