[IR Parser] Fix a crash handling zero width integer attributes.
authorChris Lattner <clattner@nondot.org>
Mon, 11 Jan 2021 02:32:57 +0000 (18:32 -0800)
committerChris Lattner <clattner@nondot.org>
Mon, 11 Jan 2021 05:18:01 +0000 (21:18 -0800)
llvm::APInt cannot hold zero bit values, therefore we shouldn't try
to form them.

Differential Revision: https://reviews.llvm.org/D94384

mlir/lib/Parser/AttributeParser.cpp
mlir/test/IR/invalid-ops.mlir

index e78237e..859e8e2 100644 (file)
@@ -334,6 +334,11 @@ static Optional<APInt> buildAttributeAPInt(Type type, bool isNegative,
   // Extend or truncate the bitwidth to the right size.
   unsigned width = type.isIndex() ? IndexType::kInternalStorageBitWidth
                                   : type.getIntOrFloatBitWidth();
+
+  // APInt cannot hold a zero bit value.
+  if (width == 0)
+    return llvm::None;
+
   if (width > result.getBitWidth()) {
     result = result.zext(width);
   } else if (width < result.getBitWidth()) {
index 595e3fe..ff39611 100644 (file)
@@ -1252,3 +1252,11 @@ func @subtensor_wrong_static_type(%t: tensor<8x16x4xf32>, %idx : index) {
 
   return
 }
+
+// -----
+
+func @no_zero_bit_integer_attrs() {
+  // expected-error @+1 {{integer constant out of range for attribute}}
+  %x = "some.op"(){value = 0 : i0} : () -> f32
+  return
+}
\ No newline at end of file