diff --git a/mlir/include/mlir/IR/OpBase.td b/mlir/include/mlir/IR/OpBase.td --- a/mlir/include/mlir/IR/OpBase.td +++ b/mlir/include/mlir/IR/OpBase.td @@ -967,6 +967,24 @@ let baseAttr = attr; } +// Decorates an optional attribute to have an (unvalidated) default value +// return by ODS generated accessors if not present. +class DefaultValuedOptionalAttr : + Attr { + // Construct this attribute with the input attribute and change only + // the default value. + // Note: this has to be kept up to date with Attr above. + let storageType = attr.storageType; + let returnType = attr.returnType; + let convertFromStorage = attr.convertFromStorage; + let constBuilderCall = attr.constBuilderCall; + let defaultValue = val; + let valueType = attr.valueType; + let isOptional = 1; + + let baseAttr = attr; +} + // Decorates an attribute as optional. The return type of the generated // attribute accessor method will be Optional<>. class OptionalAttr : Attr { diff --git a/mlir/test/mlir-tblgen/op-attribute.td b/mlir/test/mlir-tblgen/op-attribute.td --- a/mlir/test/mlir-tblgen/op-attribute.td +++ b/mlir/test/mlir-tblgen/op-attribute.td @@ -158,7 +158,7 @@ def AgetOp : Op { let arguments = (ins SomeAttr:$aAttr, - DefaultValuedAttr:$bAttr, + DefaultValuedOptionalAttr:$bAttr, OptionalAttr:$cAttr ); } @@ -255,7 +255,7 @@ // DEF-NEXT: odsState.addAttribute(getCAttrAttrName(odsState.name), cAttr); // DEF: void AgetOp::build( -// DEF: some-return-type aAttr, some-return-type bAttr, /*optional*/some-attr-kind cAttr +// DEF: some-return-type aAttr, /*optional*/some-return-type bAttr, /*optional*/some-attr-kind cAttr // DEF: odsState.addAttribute(getAAttrAttrName(odsState.name), some-const-builder-call(odsBuilder, aAttr)); // DEF: void AgetOp::build(