Multiple type support for input attribute

Hi, I’m trying to support the custom attribute by the complex dialect ops. What I want to do is pass the custom attribute in addition to the existing attribute. For instance, complex.constant is defined to get the ArrayAttr as input.

def ConstantOp : Complex_Op<"constant", [
    ConstantLike, NoSideEffect,
    DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>
  ]> {
  // ...
  let arguments = (ins ArrayAttr:$value);
}

How can I modify the op to get complex::NumberAttr too without breaking the existing code?
Making it AnyAttr breaks the semantics of the input so I want to write something like

let arguments = (ins ArrayAttr or complex::NumberAttr:$value)

Define a new attribute following https://github.com/llvm/llvm-project/blob/f9a2f6b6aecf0dd2b484d99458c99f799caee584/mlir/include/mlir/IR/OpBase.td#L1401 with the or inside the CPred.

But isn’t the intention here that it should be this new attribute? E.g., are you planning this just as a staging step?

I’ve already defined a new custom attribute like this. Do you mean adding CPred to this new attribute will make this treated as ArrayAttr right?

I agree with Jacques, the complex.number attribute should replace the ArrayAttr. That will require some changes of existing code, but I don’t know whether doing a “staging step” where both attributes are accepted will make it less work.

1 Like

Thank you. It makes sense to replace existing ops with complex.number completely so that it will be more consistent. I’m going to try to work on that.

I have another question. I’ve changed the complex.constant op to support the custom attribute like this.

def ConstantOp : Complex_Op<"constant", [
    ConstantLike, NoSideEffect,
    DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>
  ]> {
  //...
  let arguments = (ins "complex::NumberAttr":$value);
  //...
}

But it causes the following error.

/Users/sasakikai/dev/llvm-project/build/tools/mlir/include/mlir/Dialect/Complex/IR/ComplexOps.h.inc:490:20: error: no type named 'NumberAttr' in namespace 'mlir::complex'
  ::mlir::complex::NumberAttr getValueAttr();
  ~~~~~~~~~~~~~~~~~^
/Users/sasakikai/dev/llvm-project/build/tools/mlir/include/mlir/Dialect/Complex/IR/ComplexOps.h.inc:491:20: error: no type named 'NumberAttr' in namespace 'mlir::complex'
  ::mlir::complex::NumberAttr getValue();

How can we specify the dependencies on the custom attribute in the operation definition file (ODS)? To the best of my knowledge, this documentation depicts the way to define the custom attributes. But it’s not covering the way to give the attribute to arbitrary ops.

Can you try to change the order in mlir/Dialect/Complex/IR/Complex.h so that this line is before the ComplexOps line?

#define GET_ATTRDEF_CLASSES
#include “mlir/Dialect/Complex/IR/ComplexAttributes.h.inc”

#define GET_OP_CLASSES
#include “mlir/Dialect/Complex/IR/ComplexOps.h.inc”

Thanks @akuegel Let me try!