How can I replace the entire IR with a modified Operation clone in a Pass?


I am creating a Pass which “trials” several possibilities on how to modify the IR, then applies the best solution. So I need to clone the IR for each trial, modify the clones, then replace the IR with the most beneficial clone. i.e. something like:

void MyPass::runOnOperation() {
    mlir::Operation *cloneOp = getOperation()->clone();

    // Make changes to cloneOp

    // How do I replace the main operation with my clone, such that getOperation() is identical to cloneOp?

But as you can see, I cannot see how to replace the IR once I have selected the clone that I want to use. Please can you help?

Thank you!

I have just discovered that the following appears to work:


So I’ll go with this unless anyone has any better suggestions.

1 Like