[Mlir-commits] [mlir] [mlirbc] Switch generator to enable write's with failures. (PR #182464)

Jacques Pienaar llvmlistbot at llvm.org
Thu Feb 26 08:59:59 PST 2026


https://github.com/jpienaar updated https://github.com/llvm/llvm-project/pull/182464

>From 67e8d9e679a6d4fac446b4623cb78ddeb2748d0a Mon Sep 17 00:00:00 2001
From: Jacques Pienaar <jpienaar at google.com>
Date: Fri, 20 Feb 2026 11:05:54 +0200
Subject: [PATCH 1/4] [mlirbc] Switch generator to enable write's with
 failures.

Previously one had to have a matching case per entry (e.g., one could use a
printer predicate, but the assumption was one woujld never fallback).
---
 mlir/lib/Dialect/LLVMIR/IR/LLVMDialectBytecode.cpp |  2 +-
 mlir/tools/mlir-tblgen/BytecodeDialectGen.cpp      | 11 +++++++----
 2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialectBytecode.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialectBytecode.cpp
index 41d1f80580cf7..493078cb883e8 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialectBytecode.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialectBytecode.cpp
@@ -24,7 +24,7 @@ namespace {
 
 // Provide some forward declarations of the functions that will be generated by
 // the include below.
-static void write(DIExpressionElemAttr attribute,
+static LogicalResult write(DIExpressionElemAttr attribute,
                   DialectBytecodeWriter &writer);
 static LogicalResult writeAttribute(Attribute attribute,
                                     DialectBytecodeWriter &writer);
diff --git a/mlir/tools/mlir-tblgen/BytecodeDialectGen.cpp b/mlir/tools/mlir-tblgen/BytecodeDialectGen.cpp
index a759ad4d68197..7a4b9bd8929d9 100644
--- a/mlir/tools/mlir-tblgen/BytecodeDialectGen.cpp
+++ b/mlir/tools/mlir-tblgen/BytecodeDialectGen.cpp
@@ -301,7 +301,7 @@ void Generator::emitPrint(StringRef kind, StringRef type,
     return;
 
   char const *head =
-      R"(static void write({0} {1}, DialectBytecodeWriter &writer) )";
+      R"(static LogicalResult write({0} {1}, DialectBytecodeWriter &writer) )";
   mlir::raw_indented_ostream os(output);
   os << formatv(head, type, kind);
   auto funScope = os.scope("{\n", "}\n\n");
@@ -341,10 +341,14 @@ void Generator::emitPrint(StringRef kind, StringRef type,
     }
 
     if (!pred.empty()) {
+      os << "return success();\n";
       os.unindent();
       os << "}\n";
+    } else {
+      os << "return success();\n";
     }
   }
+  os << "return failure();\n";
 }
 
 void Generator::emitPrintHelper(const Record *memberRec, StringRef kind,
@@ -418,7 +422,7 @@ void Generator::emitPrintDispatch(StringRef kind, ArrayRef<std::string> vec) {
 
     os << "\n.Case([&](" << type << " t)";
     auto caseScope = os.scope(" {\n", "})");
-    os << "return write(t, writer), success();\n";
+    os << "return write(t, writer);\n";
   }
   os << "\n.Default([&](" << capitalize(kind) << ") { return failure(); });\n";
 }
@@ -475,9 +479,8 @@ static bool emitBCRW(const RecordKeeper &records, raw_ostream &os) {
     gen.emitParseDispatch(kind, *vec);
 
     SmallVector<std::string> types;
-    for (const auto &it : perType) {
+    for (const auto &it : perType)
       types.push_back(it.first);
-    }
     gen.emitPrintDispatch(kind, types);
   }
 

>From 7da32b0d228754bbebff58263721c7066243b1cc Mon Sep 17 00:00:00 2001
From: Jacques Pienaar <jpienaar at google.com>
Date: Fri, 20 Feb 2026 11:53:53 +0200
Subject: [PATCH 2/4] Fixup format

---
 mlir/lib/Dialect/LLVMIR/IR/LLVMDialectBytecode.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialectBytecode.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialectBytecode.cpp
index 493078cb883e8..d737a5648313c 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialectBytecode.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialectBytecode.cpp
@@ -25,7 +25,7 @@ namespace {
 // Provide some forward declarations of the functions that will be generated by
 // the include below.
 static LogicalResult write(DIExpressionElemAttr attribute,
-                  DialectBytecodeWriter &writer);
+                           DialectBytecodeWriter &writer);
 static LogicalResult writeAttribute(Attribute attribute,
                                     DialectBytecodeWriter &writer);
 

>From 0bbc183654758d93763fae39bee05fe6772485b9 Mon Sep 17 00:00:00 2001
From: Jacques Pienaar <jpienaar at google.com>
Date: Fri, 20 Feb 2026 13:26:30 +0200
Subject: [PATCH 3/4] Add test to show fallthrough

---
 mlir/test/mlir-tblgen/bytecode-write.td | 28 +++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
 create mode 100644 mlir/test/mlir-tblgen/bytecode-write.td

diff --git a/mlir/test/mlir-tblgen/bytecode-write.td b/mlir/test/mlir-tblgen/bytecode-write.td
new file mode 100644
index 0000000000000..ab5a3eea0f0f1
--- /dev/null
+++ b/mlir/test/mlir-tblgen/bytecode-write.td
@@ -0,0 +1,28 @@
+// RUN: mlir-tblgen -gen-bytecode -bytecode-dialect=Test -I %S/../../include %s 2>&1 | FileCheck %s
+
+// Test to show fall through behavior.
+
+include "mlir/IR/BytecodeBase.td"
+
+def Float64Type : DialectType<(type)> {
+  let printerPredicate = "false";
+}
+
+def TestDialectTypes : DialectTypes<"Test"> {
+  let elems = [
+      ReservedOrDead,
+      Float64Type
+  ];
+}
+
+// CHECK-LABEL: static LogicalResult write(Float64Type type, DialectBytecodeWriter &writer) {
+// CHECK:         if (false) {
+// CHECK:           writer.writeVarInt(/* Float64Type */ 1);
+// CHECK:           return success();
+// CHECK:         }
+// CHECK:         return failure();
+// CHECK-LABEL: static LogicalResult writeType(Type type,
+// CHECK:         return TypeSwitch<Type, LogicalResult>(type)
+// CHECK-NEXT:       .Case([&](Float64Type t) {
+// CHECK-NEXT:         return write(t, writer);
+

>From c5170723b7871a16891246edcd4e5b955f3a2eea Mon Sep 17 00:00:00 2001
From: Jacques Pienaar <jpienaar at google.com>
Date: Thu, 26 Feb 2026 18:59:25 +0200
Subject: [PATCH 4/4] Remove unneeded forward declarations

---
 mlir/lib/Dialect/LLVMIR/IR/LLVMDialectBytecode.cpp | 7 -------
 mlir/tools/mlir-tblgen/BytecodeDialectGen.cpp      | 2 +-
 2 files changed, 1 insertion(+), 8 deletions(-)

diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialectBytecode.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialectBytecode.cpp
index d737a5648313c..597690e7c77a2 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialectBytecode.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialectBytecode.cpp
@@ -22,13 +22,6 @@ using namespace mlir::LLVM;
 
 namespace {
 
-// Provide some forward declarations of the functions that will be generated by
-// the include below.
-static LogicalResult write(DIExpressionElemAttr attribute,
-                           DialectBytecodeWriter &writer);
-static LogicalResult writeAttribute(Attribute attribute,
-                                    DialectBytecodeWriter &writer);
-
 //===--------------------------------------------------------------------===//
 // Optional ArrayRefs
 //
diff --git a/mlir/tools/mlir-tblgen/BytecodeDialectGen.cpp b/mlir/tools/mlir-tblgen/BytecodeDialectGen.cpp
index 7a4b9bd8929d9..d8004a663aaee 100644
--- a/mlir/tools/mlir-tblgen/BytecodeDialectGen.cpp
+++ b/mlir/tools/mlir-tblgen/BytecodeDialectGen.cpp
@@ -315,7 +315,7 @@ void Generator::emitPrint(StringRef kind, StringRef type,
         StringRef pred = rec->getValueAsString("printerPredicate");
         if (vec.size() > 1 && pred.empty())
           PrintError(rec->getLoc(),
-                     "Requires parsing predicate given common cType");
+                     "Requires printing predicate given common cType");
       }
       PrintFatalError("Unspecified for shared cType " + type);
     }



More information about the Mlir-commits mailing list