How to create a constantOp in Pass

I want to create a constantOp like this:

  %c-1_i32 = arith.constant -1 : i32

how can I do it? or someone can explain how the builder.create to be used?

Have you gone through Toy Tutorial - MLIR already?
This is probably a good idea to try a few chapters there to begin with (the answer to your question should be covered in the code from chapter 2 already).

2 Likes

Hi there. Here is how you create a constant Op on arith.

Value zero = rewriter.create<arith::ConstantOp>(
loc, elementType, rewriter.getZeroAttr(elementType));

The arguments explains themself, for the last call of rewriter.getZeroAttr it returns an Attribute to help you build an appropriate zero.

1 Like

Emm, the toy tutorial is confusing. I think I have asked a wrong question. I want to create it in pass. How can I get it? what should the builder should cooperate with pass? Like runOnOperation():

void DerecursionPass::runOnOperation() {
    func::FuncOp func {getOperation()};
    Block* FuncBlock=&(func.front());
    OpBuilder builder(&(FuncBlock->back()));
    //How can I use builder here or where else?
    builder.create<arith::ConstantOp>(FuncBlock->back().getLoc(),builder.getI32Type());
}

I guess that the mlir pass should be implemented with pattern-match and not operate op? But how can I get them?
Ok, I get it. Just like:

OpBuilder builder(&(FuncBlock->back()));
    builder.create<arith::ConstantOp>(FuncBlock->back().getLoc(),builder.getI32Type(),builder.
    getZeroAttr(builder.getI32Type()));