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 @@ -932,6 +932,9 @@ // convertFromStorage method to handle the case where the attribute is // not present. bit isOptional = 0; + + // Whether the getter/setter function of the attribute will be hidden. + bit isHidden = 0; // What is the base-level Attr instantiation that this Attr is built upon. // Unset means this is a base-level Attr. @@ -1010,6 +1013,19 @@ let baseAttr = attr; } +// Decorates an attribute as hidden. The hidden attribute will not have their +// getter/setter functions gererated. +class HiddenAttr : Attr { + // Rewrite the attribute to be hidden. + let storageType = attr.storageType; + let returnType = attr.returnType; + let convertFromStorage = attr.convertFromStorage; + let constBuilderCall = attr.constBuilderCall; + let isHidden = 1; + + let baseAttr = attr; +} + // Default-valued string-based attribute. Wraps the default value in escaped // quotes. class DefaultValuedStrAttr diff --git a/mlir/include/mlir/TableGen/Attribute.h b/mlir/include/mlir/TableGen/Attribute.h --- a/mlir/include/mlir/TableGen/Attribute.h +++ b/mlir/include/mlir/TableGen/Attribute.h @@ -86,6 +86,9 @@ // Returns whether this attribute is optional. bool isOptional() const; + // Returns whether this attribute is hidden. + bool isHidden() const; + // Returns true if this attribute is a derived attribute (i.e., a subclass // of `DerivedAttr`). bool isDerivedAttr() const; diff --git a/mlir/lib/TableGen/Attribute.cpp b/mlir/lib/TableGen/Attribute.cpp --- a/mlir/lib/TableGen/Attribute.cpp +++ b/mlir/lib/TableGen/Attribute.cpp @@ -111,6 +111,8 @@ bool Attribute::isOptional() const { return def->getValueAsBit("isOptional"); } +bool Attribute::isHidden() const { return def->getValueAsBit("isHidden"); } + StringRef Attribute::getAttrDefName() const { if (def->isAnonymous()) { return getBaseAttr().def->getName(); diff --git a/mlir/test/mlir-tblgen/op-hidden-attribute.mlir b/mlir/test/mlir-tblgen/op-hidden-attribute.mlir new file mode 100644 --- /dev/null +++ b/mlir/test/mlir-tblgen/op-hidden-attribute.mlir @@ -0,0 +1,42 @@ +// RUN: mlir-tblgen -gen-op-decls -I %S/../../include %s | FileCheck %s + +// RUN: mlir-tblgen -gen-op-defs -I %S/../../include %s | FileCheck %s --check-prefix=DEFS + +include "mlir/IR/AttrTypeBase.td" +include "mlir/IR/OpBase.td" + +def Test_Dialect : Dialect { + let name = "test"; + let cppNamespace = "NS"; +} + +class NS_Op traits> : + Op; + +def AOp : NS_Op<"a_op", []> { + let arguments = (ins + I32Attr:$attrWithAccessor, + HiddenAttr:$attrWithoutAccessor + ); +} + +// CHECK-LABEL: NS::AOp declarations +// CHECK: getAttrWithAccessor +// CHECK: setAttrWithAccessorAttr +// CHECK: setAttrWithAccessor + +// CHECK-NOT: getAttrWithoutAccessor +// CHECK-NOT: setAttrWithoutAccessorAttr +// CHECK-NOT: setAttrWithoutAccessor + +// DEFS-LABEL: NS::AOp definitions + +// DEFS: AOpAdaptor::getAttrWithAccessorAttr +// DEFS: AOpAdaptor::getAttrWithAccessor +// DEFS: AOp::setAttrWithAccessorAttr +// DEFS: AOp::setAttrWithAccessor + +// DEFS-NOT: AOpAdaptor::getAttrWithoutAccessorAttr +// DEFS-NOT: AOpAdaptor::getAttrWithoutAccessor +// DEFS-NOT: AOp::setAttrWithoutAccessorAttr +// DEFS-NOT: AOp::setAttrWithoutAccessor \ No newline at end of file diff --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp --- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp +++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp @@ -1015,6 +1015,9 @@ for (const NamedAttribute &namedAttr : op.getAttributes()) { for (StringRef name : op.getGetterNames(namedAttr.name)) { + if (namedAttr.attr.isHidden()) { + continue; + } if (namedAttr.attr.isDerivedAttr()) { emitDerivedAttr(name, namedAttr.attr); } else { @@ -1163,7 +1166,7 @@ }; for (const NamedAttribute &namedAttr : op.getAttributes()) { - if (namedAttr.attr.isDerivedAttr()) + if (namedAttr.attr.isDerivedAttr() || namedAttr.attr.isHidden()) continue; for (auto [setterName, getterName] : llvm::zip(op.getSetterNames(namedAttr.name), @@ -3044,7 +3047,7 @@ for (auto &namedAttr : op.getAttributes()) { const auto &name = namedAttr.name; const auto &attr = namedAttr.attr; - if (attr.isDerivedAttr()) + if (attr.isDerivedAttr() || attr.isHidden()) continue; for (const auto &emitName : op.getGetterNames(name)) { emitAttrWithStorageType(name, emitName, attr);