As you can see, I’m converting from foo.bar to goo.bar in the first line.
But when I’m doing the conversion for foo.baz, i have to first lookup foo.bar and then find what it was converted to so that i can pass that relative op(i.e. goo.bar) to goo.baz.
At the moment I’m walking the list and comparing getLoc() for the nodes of the same type. This looks to be working and i wanted to ask if this is the best practice and if not if there is a better way.
What do you mean by " can pass that relative op(i.e. goo.bar ) to goo.baz" ?
If you want to have the value defined by foo.bar to be used as operand for goo.baz, this is exactly what dialect conversion was designed to do. Inside ConversionPattern::matchAndRewrite(Operation *original, ArrayRef<Value> operands, ConversionPatternRewriter &rewriter), the second argument (operands) contains the values that should be used for operands of the current operation instead of original operands. In this case, it would contain %b (as opposed to original->getOperand(0) that will still contain %a, which can be useful to inspect the original type, for example). Note that the newly created operations must not use operands of the original operation since it may be deleted at the end of the process, and must use values from operands instead.
Is there an internal function that would lookup a type within an ArrayRef:
Does something like exist?
const auto op = [&]() -> Optional<Value> {
for (const auto val : operands) {
if (val.getType().isa<MyType>()) {
return val;
}
}
return llvm::None;
}();
However, for ODS-generated ops, we have a special class <OpName>OpAdaptor that can be constructed from ArrayRef<Value> and an attribute dictionary. This class has accessors to operands and attributes with exactly the same names as the main <OpName> class. I suppose you are trying to get a converted value that corresponds to a certain operand, and dispatching by type is not the right way to do it. <OpName>OpAdaptor adaptor(operands); adaptor.operand() is the idiomatic way.
I don’t know what mlir::MyType is, in particular which classes does it derive. So I can just read the error message: mlir::MyType cannot be converted to mlir::Value by any means known to C++. There is seemingly no relation with the second line.
It cannot possibly give you the result because the operation may not exist yet.
If you do create an op, you can just call op.out().getType() to get the type.
I don’t understand why you are trying to get Foo from operands when the definition of Foo_CreateOp has no operands ( IndexAttr is an attribute, not an operand). The loop code you post should not work, unless the definition you are working with is different from what you are showing.