[Mlir-commits] [mlir] af3ed4a - [mlir] Introduce DefaultValuedOptionalAttr

Jacques Pienaar llvmlistbot at llvm.org
Thu Jul 28 14:43:20 PDT 2022


Author: Jacques Pienaar
Date: 2022-07-28T14:43:13-07:00
New Revision: af3ed4a2a760580e9f0e04c7320edafc575d02b8

URL: https://github.com/llvm/llvm-project/commit/af3ed4a2a760580e9f0e04c7320edafc575d02b8
DIFF: https://github.com/llvm/llvm-project/commit/af3ed4a2a760580e9f0e04c7320edafc575d02b8.diff

LOG: [mlir] Introduce DefaultValuedOptionalAttr

Currently DefaultValuedAttr is confusingly actually default valued &
optional but that was an artifact of development and longstanding TODO
to address. Add new attribute that matches this behavior for cases where
that is actually the desired behavior before addressing TODO (e.g., this
is an incremental step to fixing DefaultValuedAttr).

Differential Revision: https://reviews.llvm.org/D130679

Added: 
    

Modified: 
    mlir/include/mlir/IR/OpBase.td
    mlir/test/mlir-tblgen/op-attribute.td

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/IR/OpBase.td b/mlir/include/mlir/IR/OpBase.td
index cd581fce0b341..56a960f69131f 100644
--- a/mlir/include/mlir/IR/OpBase.td
+++ b/mlir/include/mlir/IR/OpBase.td
@@ -967,6 +967,24 @@ class DefaultValuedAttr<Attr attr, string val> :
   let baseAttr = attr;
 }
 
+// Decorates an optional attribute to have an (unvalidated) default value
+// return by ODS generated accessors if not present.
+class DefaultValuedOptionalAttr<Attr attr, string val> :
+    Attr<attr.predicate, attr.summary> {
+  // 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 attr> : Attr<attr.predicate, attr.summary> {

diff  --git a/mlir/test/mlir-tblgen/op-attribute.td b/mlir/test/mlir-tblgen/op-attribute.td
index 91476f545580a..89a3bba32dac4 100644
--- a/mlir/test/mlir-tblgen/op-attribute.td
+++ b/mlir/test/mlir-tblgen/op-attribute.td
@@ -158,7 +158,7 @@ def Test2_Dialect : Dialect {
 def AgetOp : Op<Test2_Dialect, "a_get_op", []> {
   let arguments = (ins
       SomeAttr:$aAttr,
-      DefaultValuedAttr<SomeAttr, "4.2">:$bAttr,
+      DefaultValuedOptionalAttr<SomeAttr, "4.2">:$bAttr,
       OptionalAttr<SomeAttr>:$cAttr
   );
 }
@@ -255,7 +255,7 @@ def AgetOp : Op<Test2_Dialect, "a_get_op", []> {
 // 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(


        


More information about the Mlir-commits mailing list