diff --git a/mlir/include/mlir/IR/OpBase.td b/mlir/include/mlir/IR/OpBase.td index 4611a4127298..ed0671e89f3c 100644 --- a/mlir/include/mlir/IR/OpBase.td +++ b/mlir/include/mlir/IR/OpBase.td @@ -256,11 +256,10 @@ def FloatLike : TypeConstraint; //===----------------------------------------------------------------------===// -// Attributes +// Attribute definitions //===----------------------------------------------------------------------===// -// A constraint on attributes. This can be used to check the validity of -// instruction attributes. +// Attribute constraint. It can be used to check the validity of attributes. class AttrConstraint { // The predicates that this attribute satisfies. // Format: {0} will be expanded to the attribute. @@ -276,21 +275,23 @@ class Attr : code storageType = ?; // The backing mlir::Attribute type code returnType = ?; // The underlying C++ value type - // Define converter method to convert from the storage type to the return + // The call expression to convert from the storage type to the return // type. For example, an enum can be stored as an int but returned as an // enum class. // - // Format: {0} will be expanded to the attribute. So - // '{0}.getValue().convertToFloat()' for 'FloatAttr val' will expand to - // 'getAttrOfType("val").getValue().convertToFloat()'. + // Format: {0} will be expanded to the attribute. + // + // For example, `{0}.getValue().getSExtValue()` for `IntegerAttr val` will + // expand to `getAttrOfType("val").getValue().getSExtValue()`. code convertFromStorage = "{0}.getValue()"; - // The call expression that builds an attribute from a constant value. + // The call expression to build an attribute from a constant value. // - // Format: {0} will be expanded to an instance of mlir::Builder, {1} will be - // expanded to the constant value of the attribute. For example, - // '{0}.getStringAttr("{1}")' for 'StringAttr:"foo"' will expand to - // 'builder.getStringAttr("foo")'. + // Format: {0} will be expanded to an instance of mlir::Builder, + // {1} will be expanded to the constant value of the attribute. + // + // For example, `{0}.getStringAttr("{1}")` for `StringAttr:"foo"` will expand + // to `builder.getStringAttr("foo")`. code constBuilderCall = ?; // Default value for attribute. @@ -303,14 +304,6 @@ class Attr : bit isOptional = 0b0; } -// Any attribute. -def AnyAttr : Attr, "any"> { - let storageType = "Attribute"; - let returnType = "Attribute"; - let convertFromStorage = "{0}"; - let constBuilderCall = "{1}"; -} - // Decorates an attribute to have an (unvalidated) default value if not present. class DefaultValuedAttr : Attr { @@ -326,14 +319,13 @@ class DefaultValuedAttr : // Decorates an attribute as optional. The return type of the generated // attribute accessor method will be Optional<>. -class OptionalAttr : - Attr { +class OptionalAttr : Attr { // Rewrite the attribute to be optional. // Note: this has to be kept up to date with Attr above. let storageType = attr.storageType; let returnType = "Optional<" # attr.returnType #">"; - let convertFromStorage = "{0} ? " # returnType # "({0}.getValue())" # - " : (llvm::None)"; + let convertFromStorage = "{0} ? " # returnType # "({0}.getValue())" + " : (llvm::None)"; let isOptional = 0b1; } @@ -346,18 +338,18 @@ class TypeBasedAttr : let storageType = attrName; } -// An attribute backed by a string type. -class StringBasedAttr : - Attr { - let constBuilderCall = [{ {0}.getStringAttr("{1}") }]; - let storageType = [{ StringAttr }]; - let returnType = [{ StringRef }]; +// Any attribute. +def AnyAttr : Attr, "any"> { + let storageType = "Attribute"; + let returnType = "Attribute"; + let convertFromStorage = "{0}"; + let constBuilderCall = "{1}"; } -// Base class for instantiating float attributes of fixed width. -class FloatAttrBase : - TypeBasedAttr { - let returnType = [{ APFloat }]; +def BoolAttr : Attr, "bool"> { + let storageType = [{ BoolAttr }]; + let returnType = [{ bool }]; + let constBuilderCall = [{ {0}.getBoolAttr({1}) }]; } // Base class for instantiating integer attributes of fixed width. @@ -366,32 +358,45 @@ class IntegerAttrBase : let returnType = [{ APInt }]; } -def BoolAttr : Attr, "bool"> { - let storageType = [{ BoolAttr }]; - let returnType = [{ bool }]; - let constBuilderCall = [{ {0}.getBoolAttr({1}) }]; +def I32Attr : IntegerAttrBase; +def I64Attr : IntegerAttrBase; + +// Base class for instantiating float attributes of fixed width. +class FloatAttrBase : + TypeBasedAttr { + let returnType = [{ APFloat }]; } -def ArrayAttr : Attr, "array"> { - let storageType = [{ ArrayAttr }]; - let returnType = [{ ArrayAttr }]; - code convertFromStorage = "{0}"; + +def F32Attr : FloatAttrBase; +def F64Attr : FloatAttrBase; + +// An attribute backed by a string type. +class StringBasedAttr : + Attr { + let constBuilderCall = [{ {0}.getStringAttr("{1}") }]; + let storageType = [{ StringAttr }]; + let returnType = [{ StringRef }]; } + +def StrAttr : StringBasedAttr, "string">; + class ElementsAttrBase : Attr { let storageType = [{ ElementsAttr }]; let returnType = [{ ElementsAttr }]; let convertFromStorage = "{0}"; } + def ElementsAttr: ElementsAttrBase, "constant vector/tensor">; -def F32Attr : FloatAttrBase; -def F64Attr : FloatAttrBase; -def I32Attr : IntegerAttrBase; -def I64Attr : IntegerAttrBase; -def StrAttr : StringBasedAttr, "string">; + +def ArrayAttr : Attr, "array"> { + let storageType = [{ ArrayAttr }]; + let returnType = [{ ArrayAttr }]; + code convertFromStorage = "{0}"; +} // Attributes containing functions. -def FunctionAttr - : Attr()">, "function"> { +def FunctionAttr : Attr()">, "function"> { let storageType = [{ FunctionAttr }]; let returnType = [{ Function * }]; let convertFromStorage = [{ {0}.getValue() }];