[Mlir-commits] [mlir] 32134a6 - [mlirbc] Switch generator to enable write's with failures. (#182464)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Thu Feb 26 21:10:54 PST 2026
Author: Jacques Pienaar
Date: 2026-02-27T07:10:49+02:00
New Revision: 32134a64b195f7804698418b6f416e761d890dea
URL: https://github.com/llvm/llvm-project/commit/32134a64b195f7804698418b6f416e761d890dea
DIFF: https://github.com/llvm/llvm-project/commit/32134a64b195f7804698418b6f416e761d890dea.diff
LOG: [mlirbc] Switch generator to enable write's with failures. (#182464)
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) and just always return success.
Added:
mlir/test/mlir-tblgen/bytecode-write.td
Modified:
mlir/lib/Dialect/LLVMIR/IR/LLVMDialectBytecode.cpp
mlir/tools/mlir-tblgen/BytecodeDialectGen.cpp
Removed:
################################################################################
diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialectBytecode.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialectBytecode.cpp
index 41d1f80580cf7..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 void write(DIExpressionElemAttr attribute,
- DialectBytecodeWriter &writer);
-static LogicalResult writeAttribute(Attribute attribute,
- DialectBytecodeWriter &writer);
-
//===--------------------------------------------------------------------===//
// Optional ArrayRefs
//
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);
+
diff --git a/mlir/tools/mlir-tblgen/BytecodeDialectGen.cpp b/mlir/tools/mlir-tblgen/BytecodeDialectGen.cpp
index a759ad4d68197..d8004a663aaee 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");
@@ -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);
}
@@ -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);
}
More information about the Mlir-commits
mailing list