Using UnitAttr in op builder

Hi Folks:

I am trying to use mlir UnitAttr to indicate ‘constant’ that may or may not be present (e.g. variable).
My is like this:

def MyGlobal : myDialect_Op<"global", [Symbol]> {
  let arguments = (ins
  let assemblyFormat = [{
       (`constant` $constant^)?
       $sym_name `:`
       custom<GlobalMemrefOpTypeAndInitialValue>($type, $initial_value)

MLIR generates default builders in such as :

static void build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::mlir::TypeRange resultTypes, ::mlir::StringAttr sym_name, ::mlir::TypeAttr type, /*optional*/::mlir::Attribute initial_value, /*optional*/::mlir::UnitAttr constant);

which i can successfully use in my code to construct ‘global’ op when it is ‘constant’ e.g.

builder.create<mlir::myDialect::MyGlobal>(loc, nameAttr, typeAttr, initVal_attr, builder.getUnitAttr());

However, for the case where I do not want to specify ‘constant’ because i want to construct the ‘variable’ version , I find that no default constuctor(s) generated by mlir. I was hoping to use the same as above and not passing ‘builder.getUnitAttr()’ to indicate that it is not a 'constant
'. e.g.

builder.create<mlir::myDialect::MyGlobal>(loc, nameAttr, typeAttr, initVal_attr);

But that doesn’t work … ‘no matching function found’. Where am I going wrong and what is the solution.
Thanks a lot.

Any thoughts on this ?

Pass a nullptr or a default-constructed UnitAttr() instead.

1 Like

‘nullptr’ works. Thanks !
I did not understand the ‘default-constructed’ UnitAttr() part .
builder.getUnitAttr() is the only way i know of constructing it (and that would put constant)

All attribute types (should) have a default constructor in c++ sense. You can literally write Attribute() or UnitAttr() and have a null attribute, the same way you are getting one implicitly constructed from nullptr_t.