[Mlir-commits] [mlir] [mlir][bytecode] Avoid crash when test dialect version is missing (PR #170173)

Akimasa Watanuki llvmlistbot at llvm.org
Tue Mar 3 05:20:51 PST 2026


https://github.com/Men-cotton updated https://github.com/llvm/llvm-project/pull/170173

>From f4dfe450373ae793cd481a246ee0864f2453a676 Mon Sep 17 00:00:00 2001
From: mencotton <mencotton0410 at gmail.com>
Date: Tue, 2 Dec 2025 02:09:25 +0900
Subject: [PATCH 1/2] [mlir][bytecode] Avoid crash when test dialect version is
 missing

---
 mlir/test/Bytecode/roundtrip-missing-dialect.mlir | 6 ++++++
 mlir/test/lib/IR/TestBytecodeRoundtrip.cpp        | 8 ++++----
 2 files changed, 10 insertions(+), 4 deletions(-)
 create mode 100644 mlir/test/Bytecode/roundtrip-missing-dialect.mlir

diff --git a/mlir/test/Bytecode/roundtrip-missing-dialect.mlir b/mlir/test/Bytecode/roundtrip-missing-dialect.mlir
new file mode 100644
index 0000000000000..8a93fcfd55695
--- /dev/null
+++ b/mlir/test/Bytecode/roundtrip-missing-dialect.mlir
@@ -0,0 +1,6 @@
+// RUN: mlir-opt %s --test-bytecode-roundtrip=test-dialect-version=2.0 | FileCheck %s
+
+// CHECK-LABEL: func.func @main
+func.func @main() {
+  return
+}
diff --git a/mlir/test/lib/IR/TestBytecodeRoundtrip.cpp b/mlir/test/lib/IR/TestBytecodeRoundtrip.cpp
index 4894ad5294990..f6ee97831c9dd 100644
--- a/mlir/test/lib/IR/TestBytecodeRoundtrip.cpp
+++ b/mlir/test/lib/IR/TestBytecodeRoundtrip.cpp
@@ -141,8 +141,8 @@ struct TestBytecodeRoundtripPass
             DialectBytecodeWriter &writer) -> LogicalResult {
           // Do not override anything if version greater than 2.0.
           auto versionOr = writer.getDialectVersion<test::TestDialect>();
-          assert(succeeded(versionOr) && "expected reader to be able to access "
-                                         "the version for test dialect");
+          if (failed(versionOr))
+            return failure();
           const auto *version =
               reinterpret_cast<const test::TestDialectVersion *>(*versionOr);
           if (version->major_ >= 2)
@@ -166,8 +166,8 @@ struct TestBytecodeRoundtripPass
             Type &entry) -> LogicalResult {
           // Get test dialect version from the version map.
           auto versionOr = reader.getDialectVersion<test::TestDialect>();
-          assert(succeeded(versionOr) && "expected reader to be able to access "
-                                         "the version for test dialect");
+          if (failed(versionOr))
+            return success();
           const auto *version =
               reinterpret_cast<const test::TestDialectVersion *>(*versionOr);
           if (version->major_ >= 2)

>From 4aa8617f0c15b31575b646a1edd1b1d2a40ceeb0 Mon Sep 17 00:00:00 2001
From: mencotton <mencotton0410 at gmail.com>
Date: Tue, 3 Mar 2026 22:16:46 +0900
Subject: [PATCH 2/2] fix: add comments for fall through

---
 mlir/test/lib/IR/TestBytecodeRoundtrip.cpp | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/mlir/test/lib/IR/TestBytecodeRoundtrip.cpp b/mlir/test/lib/IR/TestBytecodeRoundtrip.cpp
index f6ee97831c9dd..e838cdcb3a23c 100644
--- a/mlir/test/lib/IR/TestBytecodeRoundtrip.cpp
+++ b/mlir/test/lib/IR/TestBytecodeRoundtrip.cpp
@@ -166,6 +166,8 @@ struct TestBytecodeRoundtripPass
             Type &entry) -> LogicalResult {
           // Get test dialect version from the version map.
           auto versionOr = reader.getDialectVersion<test::TestDialect>();
+          // Missing version is non-fatal (consistent with other test-dialect
+          // readers); return success() and fall through to default parsing.
           if (failed(versionOr))
             return success();
           const auto *version =
@@ -191,7 +193,8 @@ struct TestBytecodeRoundtripPass
                     widthAndSignedness & 0x3)),
                true))
             entry = IntegerType::get(reader.getContext(), width, signedness);
-          // Return nullopt to fall through the rest of the parsing code path.
+          // Return success() and fall through to default parsing when this
+          // callback does not override the entry.
           return success();
         });
     doRoundtripWithConfigs(op, writeConfig, parseConfig);



More information about the Mlir-commits mailing list