Moving an op from one region to another

So let’s say I have an operation Op. I then have two single-blocked, nested regions, A and B.

I want to take the operation(s) out of B and place it in A.
A { B { Op; } }

A { Op; B {} } // Eventually, B is deleted as well.

Extra constraint: the Operands of Op may also be contained in A or B.

Is there an idiomatic way to do this? I see that the Region class has takeBody, but this is producing some long, winding errors. Perhaps it would be more helpful for me to produce a simple code example and the resulting errors.

PatternRewriter has inlineRegionBefore: PatternMatch.cpp - llvm/llvm-project - Sourcegraph. You can use it or borrow the implementation.

takeBody replaces the old body, so if “Operands of Op may also be contained in A”, it will remove their definitions.

1 Like