[Mlir-commits] [mlir] [mlir] Improve EnumProp, making it take an EnumInfo (PR #132349)
Krzysztof Drewniak
llvmlistbot at llvm.org
Thu Apr 10 13:58:32 PDT 2025
================
@@ -552,6 +553,139 @@ class EnumAttr<Dialect dialect, EnumInfo enumInfo, string name = "",
let assemblyFormat = "$value";
}
+// A property wrapping by a C++ enum. This class will automatically create bytecode
+// serialization logic for the given enum, as well as arranging for parser and
+// printer calls.
+class EnumProp<EnumInfo enumInfo> : Property<enumInfo.cppType, enumInfo.summary> {
+ EnumInfo enum = enumInfo;
+
+ let description = enum.description;
+ let predicate = !if(
+ !isa<BitEnumBase>(enum),
+ CPred<"(static_cast<" # enum.underlyingType # ">($_self) & ~" # !cast<BitEnumBase>(enum).validBits # ") == 0">,
+ Or<!foreach(case, enum.enumerants, CPred<"$_self == " # enum.cppType # "::" # case.symbol>)>);
+
+ let convertFromAttribute = [{
+ auto intAttr = ::mlir::dyn_cast_if_present<::mlir::IntegerAttr>($_attr);
+ if (!intAttr) {
+ return $_diag() << "expected IntegerAttr storage for }] #
+ enum.cppType # [{";
+ }
+ $_storage = static_cast<}] # enum.cppType # [{>(intAttr.getValue().getZExtValue());
+ return ::mlir::success();
+ }];
+
+ let convertToAttribute = [{
+ return ::mlir::IntegerAttr::get(::mlir::IntegerType::get($_ctxt, }] # enum.bitwidth
+ # [{), static_cast<}] # enum.underlyingType #[{>($_storage));
+ }];
+
+ let writeToMlirBytecode = [{
+ $_writer.writeVarInt(static_cast<uint64_t>($_storage));
+ }];
+
+ let readFromMlirBytecode = [{
+ uint64_t rawValue;
+ if (::mlir::failed($_reader.readVarInt(rawValue)))
+ return ::mlir::failure();
+ $_storage = static_cast<}] # enum.cppType # [{>(rawValue);
----------------
krzysz00 wrote:
The static cast will truncate if needed, no?
https://github.com/llvm/llvm-project/pull/132349
More information about the Mlir-commits
mailing list