Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions mlir/test/mlir-tblgen/enums-python-bindings.td
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ def MyEnum64 : I64EnumAttr<"MyEnum64", "An example 64-bit enum", [One64, Two64]>
// CHECK: def _myenum64(x, context):
// CHECK: return _ods_ir.IntegerAttr.get(_ods_ir.IntegerType.get_signless(64, context=context), int(x))

def User : I32BitEnumAttrCaseBit<"User", 0, "user">;
def Group : I32BitEnumAttrCaseBit<"Group", 1, "group">;
def Other : I32BitEnumAttrCaseBit<"Other", 2, "other">;

def TestBitEnum
: I32BitEnumAttr<"TestBitEnum", "", [
I32BitEnumAttrCaseBit<"User", 0, "user">,
I32BitEnumAttrCaseBit<"Group", 1, "group">,
I32BitEnumAttrCaseBit<"Other", 2, "other">,
]> {
: I32BitEnumAttr<
"TestBitEnum", "",
[User, Group, Other,
I32BitEnumAttrCaseGroup<"Any", [User, Group, Other], "any">]> {
let genSpecializedAttr = 0;
let separator = " | ";
}
Expand All @@ -79,9 +82,10 @@ def TestBitEnum_Attr : EnumAttr<Test_Dialect, TestBitEnum, "testbitenum">;
// CHECK: User = 1
// CHECK: Group = 2
// CHECK: Other = 4
// CHECK: Any = 7

// CHECK: def __iter__(self):
// CHECK: return iter([case for case in type(self) if (self & case) is case])
// CHECK: return iter([case for case in type(self) if (self & case) is case and self is not case])
// CHECK: def __len__(self):
// CHECK: return bin(self).count("1")

Expand All @@ -94,6 +98,8 @@ def TestBitEnum_Attr : EnumAttr<Test_Dialect, TestBitEnum, "testbitenum">;
// CHECK: return "group"
// CHECK: if self is TestBitEnum.Other:
// CHECK: return "other"
// CHECK: if self is TestBitEnum.Any:
// CHECK: return "any"
// CHECK: raise ValueError("Unknown TestBitEnum enum entry.")

// CHECK: @register_attribute_builder("TestBitEnum")
Expand Down
2 changes: 1 addition & 1 deletion mlir/tools/mlir-tblgen/EnumPythonBindingGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static void emitEnumClass(EnumInfo enumInfo, raw_ostream &os) {
if (enumInfo.isBitEnum()) {
os << formatv(" def __iter__(self):\n"
" return iter([case for case in type(self) if "
"(self & case) is case])\n");
"(self & case) is case and self is not case])\n");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the key part, do not allow iteration over exactly self's bit value otherwise recursion ensues.

os << formatv(" def __len__(self):\n"
" return bin(self).count(\"1\")\n");
os << "\n";
Expand Down
Loading