[Mlir-commits] [mlir] [mlir][bytecode] Fix crashes when reading bytecode with unsupported types (PR #186354)

Mehdi Amini llvmlistbot at llvm.org
Mon Mar 16 04:12:30 PDT 2026


================
@@ -34,22 +34,25 @@ namespace {
 // TODO: Move these to separate file.
 
 // Returns the bitwidth if known, else return 0.
-static unsigned getIntegerBitWidth(DialectBytecodeReader &reader, Type type) {
-  if (auto intType = dyn_cast<IntegerType>(type)) {
+static std::optional<unsigned> getIntegerBitWidth(DialectBytecodeReader &reader,
+                                                  Type type) {
+  if (auto intType = dyn_cast<IntegerType>(type))
     return intType.getWidth();
-  }
-  if (llvm::isa<IndexType>(type)) {
+  if (llvm::isa<IndexType>(type))
     return IndexType::kInternalStorageBitWidth;
-  }
   reader.emitError()
       << "expected integer or index type for IntegerAttr, but got: " << type;
-  return 0;
+  return std::nullopt;
 }
 
 static LogicalResult readAPIntWithKnownWidth(DialectBytecodeReader &reader,
                                              Type type, FailureOr<APInt> &val) {
-  unsigned bitWidth = getIntegerBitWidth(reader, type);
-  val = reader.readAPIntWithKnownWidth(bitWidth);
+  std::optional<unsigned> bitWidth = getIntegerBitWidth(reader, type);
+  // getIntegerBitWidth returns 0 and emits an error for unsupported types.
----------------
joker-eph wrote:

Stale comment: `getIntegerBitWidth` no longer returns `0` — it now returns `std::nullopt`. Should read: `getIntegerBitWidth returns std::nullopt and emits an error for unsupported types.`

https://github.com/llvm/llvm-project/pull/186354


More information about the Mlir-commits mailing list