[Mlir-commits] [mlir] [mlir][python] Fix recursion error for __str__ (PR #174552)

Jiaxuan Bai llvmlistbot at llvm.org
Thu Jan 8 17:54:17 PST 2026


https://github.com/JiaxuanBai updated https://github.com/llvm/llvm-project/pull/174552

>From f7c85c1082f2c65fc62ef4b3781f955995cf5b75 Mon Sep 17 00:00:00 2001
From: Your Name <you at example.com>
Date: Tue, 6 Jan 2026 17:11:36 +0800
Subject: [PATCH 1/2] fix recursion error for __str__

---
 mlir/test/mlir-tblgen/enums-python-bindings.td  | 4 ++--
 mlir/tools/mlir-tblgen/EnumPythonBindingGen.cpp | 8 ++++----
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/mlir/test/mlir-tblgen/enums-python-bindings.td b/mlir/test/mlir-tblgen/enums-python-bindings.td
index cd23b6a2effb9..6d79f0999efed 100644
--- a/mlir/test/mlir-tblgen/enums-python-bindings.td
+++ b/mlir/test/mlir-tblgen/enums-python-bindings.td
@@ -90,8 +90,6 @@ def TestBitEnum_Attr : EnumAttr<Test_Dialect, TestBitEnum, "testbitenum">;
 // CHECK:         return bin(self).count("1")
 
 // CHECK:     def __str__(self):
-// CHECK:         if len(self) > 1:
-// CHECK:             return " | ".join(map(str, self))
 // CHECK:         if self is TestBitEnum.User:
 // CHECK:             return "user"
 // CHECK:         if self is TestBitEnum.Group:
@@ -100,6 +98,8 @@ def TestBitEnum_Attr : EnumAttr<Test_Dialect, TestBitEnum, "testbitenum">;
 // CHECK:             return "other"
 // CHECK:         if self is TestBitEnum.Any:
 // CHECK:             return "any"
+// CHECK:         if len(self) > 1:
+// CHECK:             return " | ".join(map(str, self))
 // CHECK:         raise ValueError("Unknown TestBitEnum enum entry.")
 
 // CHECK: @register_attribute_builder("TestBitEnum")
diff --git a/mlir/tools/mlir-tblgen/EnumPythonBindingGen.cpp b/mlir/tools/mlir-tblgen/EnumPythonBindingGen.cpp
index acc9b61d7121c..fcb81e7704321 100644
--- a/mlir/tools/mlir-tblgen/EnumPythonBindingGen.cpp
+++ b/mlir/tools/mlir-tblgen/EnumPythonBindingGen.cpp
@@ -71,15 +71,15 @@ static void emitEnumClass(EnumInfo enumInfo, raw_ostream &os) {
   }
 
   os << formatv("    def __str__(self):\n");
-  if (enumInfo.isBitEnum())
-    os << formatv("        if len(self) > 1:\n"
-                  "            return \"{0}\".join(map(str, self))\n",
-                  enumInfo.getDef().getValueAsString("separator"));
   for (const EnumCase &enumCase : enumInfo.getAllCases()) {
     os << formatv("        if self is {0}.{1}:\n", enumInfo.getEnumClassName(),
                   makePythonEnumCaseName(enumCase.getSymbol()));
     os << formatv("            return \"{0}\"\n", enumCase.getStr());
   }
+  if (enumInfo.isBitEnum())
+  os << formatv("        if len(self) > 1:\n"
+                "            return \"{0}\".join(map(str, self))\n",
+                enumInfo.getDef().getValueAsString("separator"));
   os << formatv("        raise ValueError(\"Unknown {0} enum entry.\")\n\n\n",
                 enumInfo.getEnumClassName());
   os << "\n";

>From a4c8be757af1dfe2fe6b6d2722b304079011f411 Mon Sep 17 00:00:00 2001
From: Your Name <you at example.com>
Date: Fri, 9 Jan 2026 09:53:05 +0800
Subject: [PATCH 2/2] Add related tests

---
 mlir/test/python/dialects/arith_dialect.py      | 12 ++++++++++++
 mlir/tools/mlir-tblgen/EnumPythonBindingGen.cpp |  6 +++---
 2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/mlir/test/python/dialects/arith_dialect.py b/mlir/test/python/dialects/arith_dialect.py
index a4cfb30240231..3b75e90555704 100644
--- a/mlir/test/python/dialects/arith_dialect.py
+++ b/mlir/test/python/dialects/arith_dialect.py
@@ -37,6 +37,18 @@ def testFastMathFlags():
             print(r)
 
 
+# CHECK-LABEL: TEST: testFastMathFlagsFast
+ at run
+def testFastMathFlagsFast():
+    with Context() as ctx, Location.unknown():
+        module = Module.create()
+        with InsertionPoint(module.body):
+            a = arith.ConstantOp(value=42.42, result=F32Type.get())
+            r = arith.AddFOp(a, a, fastmath=arith.FastMathFlags.fast)
+            # CHECK: %0 = arith.addf %cst, %cst fastmath<fast> : f32
+            print(r)
+
+
 # CHECK-LABEL: TEST: testArithValue
 @run
 def testArithValue():
diff --git a/mlir/tools/mlir-tblgen/EnumPythonBindingGen.cpp b/mlir/tools/mlir-tblgen/EnumPythonBindingGen.cpp
index fcb81e7704321..b9a06cb3a6dac 100644
--- a/mlir/tools/mlir-tblgen/EnumPythonBindingGen.cpp
+++ b/mlir/tools/mlir-tblgen/EnumPythonBindingGen.cpp
@@ -77,9 +77,9 @@ static void emitEnumClass(EnumInfo enumInfo, raw_ostream &os) {
     os << formatv("            return \"{0}\"\n", enumCase.getStr());
   }
   if (enumInfo.isBitEnum())
-  os << formatv("        if len(self) > 1:\n"
-                "            return \"{0}\".join(map(str, self))\n",
-                enumInfo.getDef().getValueAsString("separator"));
+    os << formatv("        if len(self) > 1:\n"
+                  "            return \"{0}\".join(map(str, self))\n",
+                  enumInfo.getDef().getValueAsString("separator"));
   os << formatv("        raise ValueError(\"Unknown {0} enum entry.\")\n\n\n",
                 enumInfo.getEnumClassName());
   os << "\n";



More information about the Mlir-commits mailing list