replaceOp() working

In toy tutorial chapter-3, the replaceOp() has been used as following:

matchAndRewrite(TransposeOp op,
                  mlir::PatternRewriter &rewriter) const override {
    // Look through the input of the current transpose.
    mlir::Value transposeInput = op.getOperand();
    TransposeOp transposeInputOp = transposeInput.getDefiningOp<TransposeOp>();

    // Input defined by another transpose? If not, no match.
    if (!transposeInputOp)
      return failure();

    // Otherwise, we have a redundant transpose. Use the rewriter.
    rewriter.replaceOp(op, {transposeInputOp.getOperand()});
    return success();

What is replaceOp doing in this code?

The operation is replaced with another one. This is implemented by replacing uses of op’s result with transposeInputOp.getOperand() and erasing op. You can see this effect by running code in the tutorial and experimenting with it.

1 Like

Thanks for the reply. I was confused that why does transposeInputOp gets removed while replaceOp() was called on op only?

Because transposeInputOp became dead after that, i.e., after op was replaced, there was no one using transposeInputOp’s results.

1 Like