Skip to content

Commit 2147346

Browse files
authored
[MLIR][Python] MLIR Enum Python bindings infinite recursion (#151584) (#151588)
Fixes an infinite recursion bug when using I32BitEnumAttrCaseGroup with python bindings. For more info, see issue: - #151584
1 parent 4d629f9 commit 2147346

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

mlir/test/mlir-tblgen/enums-python-bindings.td

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,15 @@ def MyEnum64 : I64EnumAttr<"MyEnum64", "An example 64-bit enum", [One64, Two64]>
6262
// CHECK: def _myenum64(x, context):
6363
// CHECK: return _ods_ir.IntegerAttr.get(_ods_ir.IntegerType.get_signless(64, context=context), int(x))
6464

65+
def User : I32BitEnumAttrCaseBit<"User", 0, "user">;
66+
def Group : I32BitEnumAttrCaseBit<"Group", 1, "group">;
67+
def Other : I32BitEnumAttrCaseBit<"Other", 2, "other">;
68+
6569
def TestBitEnum
66-
: I32BitEnumAttr<"TestBitEnum", "", [
67-
I32BitEnumAttrCaseBit<"User", 0, "user">,
68-
I32BitEnumAttrCaseBit<"Group", 1, "group">,
69-
I32BitEnumAttrCaseBit<"Other", 2, "other">,
70-
]> {
70+
: I32BitEnumAttr<
71+
"TestBitEnum", "",
72+
[User, Group, Other,
73+
I32BitEnumAttrCaseGroup<"Any", [User, Group, Other], "any">]> {
7174
let genSpecializedAttr = 0;
7275
let separator = " | ";
7376
}
@@ -79,9 +82,10 @@ def TestBitEnum_Attr : EnumAttr<Test_Dialect, TestBitEnum, "testbitenum">;
7982
// CHECK: User = 1
8083
// CHECK: Group = 2
8184
// CHECK: Other = 4
85+
// CHECK: Any = 7
8286

8387
// CHECK: def __iter__(self):
84-
// CHECK: return iter([case for case in type(self) if (self & case) is case])
88+
// CHECK: return iter([case for case in type(self) if (self & case) is case and self is not case])
8589
// CHECK: def __len__(self):
8690
// CHECK: return bin(self).count("1")
8791

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

99105
// CHECK: @register_attribute_builder("TestBitEnum")

mlir/tools/mlir-tblgen/EnumPythonBindingGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static void emitEnumClass(EnumInfo enumInfo, raw_ostream &os) {
6464
if (enumInfo.isBitEnum()) {
6565
os << formatv(" def __iter__(self):\n"
6666
" return iter([case for case in type(self) if "
67-
"(self & case) is case])\n");
67+
"(self & case) is case and self is not case])\n");
6868
os << formatv(" def __len__(self):\n"
6969
" return bin(self).count(\"1\")\n");
7070
os << "\n";

0 commit comments

Comments
 (0)