From dcac2da10632c83737fce6da60fbc4dd09c01034 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sun, 10 Jan 2021 18:32:57 -0800 Subject: [PATCH] [IR Parser] Fix a crash handling zero width integer attributes. 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 | 5 +++++ mlir/test/IR/invalid-ops.mlir | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/mlir/lib/Parser/AttributeParser.cpp b/mlir/lib/Parser/AttributeParser.cpp index e78237e8e5a0..859e8e279917 100644 --- a/mlir/lib/Parser/AttributeParser.cpp +++ b/mlir/lib/Parser/AttributeParser.cpp @@ -334,6 +334,11 @@ static Optional 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()) { diff --git a/mlir/test/IR/invalid-ops.mlir b/mlir/test/IR/invalid-ops.mlir index 595e3fe3f123..ff39611eaea1 100644 --- a/mlir/test/IR/invalid-ops.mlir +++ b/mlir/test/IR/invalid-ops.mlir @@ -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