diff --git a/mlir/include/mlir/IR/ExtensibleDialect.h b/mlir/include/mlir/IR/ExtensibleDialect.h --- a/mlir/include/mlir/IR/ExtensibleDialect.h +++ b/mlir/include/mlir/IR/ExtensibleDialect.h @@ -436,6 +436,18 @@ return verifyRegionFn(op); } + // Implementation for properties (unsupported right now here). + int getOpPropertyByteSize() final { return 0; } + void initProperties(void *prop) final {} + void deleteProperties(void *prop) final {} + LogicalResult setPropertiesFromAttr(Operation *op, Attribute attr, + InFlightDiagnostic *diag) final { + return failure(); + } + Attribute getPropertiesAsAttr(Operation *op) final { return {}; } + void copyProperties(void *lhs, void *rhs) final {} + llvm::hash_code hashProperties(void *prop) final { return {}; } + private: DynamicOpDefinition( StringRef name, ExtensibleDialect *dialect, 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 @@ -185,6 +185,56 @@ string cppClassName = cppClassNameParam; } + + +// Base class for defining properties. +class Property { + // User-readable one line summary used in error reporting messages. If empty, + // a generic message will be used. + string summary = desc; + code storageType = storageTypeParam; + code interfaceType = storageTypeParam; + + // The expression to convert from the storage type to the Interface + // type. For example, an enum can be stored as an int but returned as an + // enum class. + // + // Format: `storage` will contain the property in the storage type. + code convertFromStorage = "storage"; + + // The call expression to build a property storage from the interface type. + code assignToStorage = "storage = value"; + + // The call expression to convert from the storage type to an attribute. + // + // Format: `storage` is the storage type value, `ctx` is a MLIRContext*. The + // expression must result in an Attribute. + code convertToAttribute = [{ + convertToAttribute(ctx, storage) + }]; + + // The call expression to convert from an Attribute to the storage type. + // + // Format: `storage` is the storage value, `attr` is the attribute, + // and `diag` is an optional Diagnostic pointer to emit error. + // The expression must return a LogicalResult + code convertFromAttribute = [{ + return convertFromAttribute(storage, attr, diag); + }]; + + // The call expression to hash the property, `storage` is the variable to hash + // and the expression should define a llvm::hash_code. + code hashProperty = [{ + llvm::hash_value(storage); + }]; + + // Default value for the property. + string defaultValue = ?; + + // The full description of this attribute. + string description = ""; +} + // Subclass for constraints on an attribute. class AttrConstraint : Constraint; @@ -1049,6 +1099,17 @@ class DefaultValuedOptionalStrAttr : DefaultValuedOptionalAttr; +//===----------------------------------------------------------------------===// +// Primitive property kinds + +class ArrayProperty : + Property { + let interfaceType = "::llvm::ArrayRef<" # storageTypeParam # ">"; + let convertFromStorage = "storage"; + let assignToStorage = "::llvm::copy(value, storage)"; + +} + //===----------------------------------------------------------------------===// // Primitive attribute kinds @@ -2215,6 +2276,9 @@ // Dag containing the arguments of the op. Default to 0 arguments. dag arguments = (ins); + // Dag containing the properties of the op. Default to no properties. + dag properties = (ins); + // The list of results of the op. Default to 0 results. dag results = (outs); diff --git a/mlir/include/mlir/IR/OpDefinition.h b/mlir/include/mlir/IR/OpDefinition.h --- a/mlir/include/mlir/IR/OpDefinition.h +++ b/mlir/include/mlir/IR/OpDefinition.h @@ -203,6 +203,13 @@ /// in generic form. static void print(Operation *op, OpAsmPrinter &p, StringRef defaultDialect); + /// Parse properties as a Attribute. + static ParseResult genericParseProperties(OpAsmParser &parser, + Attribute &result); + + /// Print the properties as a Attribute. + static void genericPrintProperties(OpAsmPrinter &p, Attribute properties); + /// Print an operation name, eliding the dialect prefix if necessary. static void printOpName(Operation *op, OpAsmPrinter &p, StringRef defaultDialect); @@ -1460,13 +1467,17 @@ /// Returns true if this given Trait ID matches the IDs of any of the provided /// trait types `Traits`. template