Hi!
I have one proposal regarding enum attributes support and I’d like to hear your opinion.
For now, such attributes are represented as raw StringAttr
/IntegerAttr
in the generated code. All checks for value validity and conversion from raw format to actual enum value is done in the code, which uses the enum attribute (Operation, for example).
I’d like to propose generating separate decorator classes for enum attributes in the same way as it is done for struct attributes. Those classes will incapsulate all validation/conversion logic.
For example:
enum class SomeEnum {...};
class SomeEnumAttr : public ::mlir::StringAttr {
public:
using ::mlir::StringAttr::StringAttr;
static bool classof(::mlir::Attribute attr) {
if (!::mlir::StringAttr::classof(attr)) {
return false;
}
// Check that the string represents SomeEnum value
return symbolizeEnum<SomeEnum>(attr.cast<::mlir::StringAttr>.getValue()).hasValue();
}
static SomeEnumAttr get(SomeEnum val, ::mlir::MLIRContext* context) {
return ::mlir::StringAttr::get(stringifyEnum(val), context).cast<SomeEnumAttr>()
}
SomeEnum getValue() const {
return symbolizeEnum<SomeEnum>(::mlir::StringAttr::getValue()).getValue();
}
};
IMHO, this will simply the code, which uses enum attrs (no need to duplicate the validation checks).