MLIR Operation modification rules

I have a question about when it is okay to modify a parent block/operation in an OpRewritePattern pass.

Documentation here says an operation pass “must not modify any state referenced or relied upon outside the current operation being operated on. This includes adding or removing operations from the parent block”.

But I can see some mlir algebraic simplification rewrite rules do indeed add operations to the rewritten operation’s parent block (in this case, POWIStrengthReduction::matchAndRewrite will rewrite the PowIOpTy with perhaps several multiplication and division operations). Wouldn’t this be a case of adding or removing operations from the parent block?

Also, I see that RewriterBase::startRootUpdate is not used here, even though the original PowIOp is replaced in place. In what situations must RewriterBase::startRootUpdate be used?

The doc you reference is about the pass, but you’re looking at the pattern.
A simplified mental model can be:

  • An operation pass running on a function shouldn’t modify anything in the parent block for the function (that is in general the module)
  • The pass may run patterns on the function body (recursively) and these only inherit (implicitly) the limit from the pass: they can’t modify anything outside of the function itself.
1 Like

The original isn’t modified in place without using a rewriter API, so it seems all safe. The “root update” notification is to instruct the rewriter of modifications done outside of the rewriter APIs, for example if you were directly doing something like op->setAttr(...).

1 Like