diff --git a/mlir/lib/Dialect/StandardOps/IR/Ops.cpp b/mlir/lib/Dialect/StandardOps/IR/Ops.cpp --- a/mlir/lib/Dialect/StandardOps/IR/Ops.cpp +++ b/mlir/lib/Dialect/StandardOps/IR/Ops.cpp @@ -1138,7 +1138,7 @@ if (!value) return op.emitOpError("requires a 'value' attribute"); - auto type = op.getType(); + Type type = op.getType(); if (!value.getType().isa() && type != value.getType()) return op.emitOpError() << "requires attribute's type (" << value.getType() << ") to match op's return type (" << type << ")"; @@ -1147,10 +1147,14 @@ return success(); if (auto intAttr = value.dyn_cast()) { + IntegerType intType = type.cast(); + if (!intType.isSignless()) + return op.emitOpError("requires integer result types to be signless"); + // If the type has a known bitwidth we verify that the value can be // represented with the given bitwidth. - auto bitwidth = type.cast().getWidth(); - auto intVal = intAttr.getValue(); + unsigned bitwidth = intType.getWidth(); + APInt intVal = intAttr.getValue(); if (!intVal.isSignedIntN(bitwidth) && !intVal.isIntN(bitwidth)) return op.emitOpError("requires 'value' to be an integer within the " "range of the integer result type"); @@ -1230,9 +1234,13 @@ // SymbolRefAttr can only be used with a function type. if (value.isa()) return type.isa(); - // Otherwise, the attribute must have the same type as 'type'. + // The attribute must have the same type as 'type'. if (value.getType() != type) return false; + // If the type is an integer type, it must be signless. + if (IntegerType integerTy = type.dyn_cast()) + if (!integerTy.isSignless()) + return false; // Finally, check that the attribute kind is handled. return value.isa(); } diff --git a/mlir/test/Dialect/Standard/invalid.mlir b/mlir/test/Dialect/Standard/invalid.mlir --- a/mlir/test/Dialect/Standard/invalid.mlir +++ b/mlir/test/Dialect/Standard/invalid.mlir @@ -298,3 +298,18 @@ return } +// ----- + +func @non_signless_constant() { + // expected-error @+1 {{requires integer result types to be signless}} + %0 = constant 0 : ui32 + return +} + +// ----- + +func @non_signless_constant() { + // expected-error @+1 {{requires integer result types to be signless}} + %0 = constant 0 : si32 + return +}