[Mlir-commits] [mlir] [mlir][ODS] Add `ConstantEnumCase` (PR #78992)
Mehdi Amini
llvmlistbot at llvm.org
Mon Jan 22 10:26:20 PST 2024
================
@@ -417,4 +417,54 @@ class EnumAttr<Dialect dialect, EnumAttrInfo enumInfo, string name = "",
let assemblyFormat = "$value";
}
+class _symbolToValue<EnumAttrInfo enumAttrInfo, string case> {
+ defvar cases =
+ !filter(iter, enumAttrInfo.enumerants, !eq(iter.str, case));
+
+ assert !not(!empty(cases)), "failed to find enum-case '" # case # "'";
+
+ // `!empty` check to not cause an error if the cases are empty.
+ // The assertion catches the issue later and emits a proper error message.
+ string value = enumAttrInfo.cppType # "::"
+ # !if(!empty(cases), "", !head(cases).symbol);
+}
+
+class _bitSymbolsToValue<BitEnumAttr bitEnumAttr, string case> {
+ defvar pos = !find(case, "|");
+
+ // Recursive instantiation looking up the symbol before the `|` in
+ // enum cases.
+ string value = !if(
+ !eq(pos, -1), /*baseCase=*/_symbolToValue<bitEnumAttr, case>.value,
+ /*rec=*/_symbolToValue<bitEnumAttr, !substr(case, 0, pos)>.value # "|"
+ # _bitSymbolsToValue<bitEnumAttr, !substr(case, !add(pos, 1))>.value
+ );
+}
+
+class ConstantEnumCaseBase<Attr attribute,
+ EnumAttrInfo enumAttrInfo, string case>
+ : ConstantAttr<attribute,
+ !if(!isa<BitEnumAttr>(enumAttrInfo),
+ _bitSymbolsToValue<!cast<BitEnumAttr>(enumAttrInfo), case>.value,
+ _symbolToValue<enumAttrInfo, case>.value
+ )
+>;
+
+/// Constant attribute for defining enum values. `attribute` should be one of
----------------
joker-eph wrote:
> Constant attribute for defining enum values
This does not define a new attribute, seems misleading to me, can you try to reformulate?
https://github.com/llvm/llvm-project/pull/78992
More information about the Mlir-commits
mailing list