diff --git a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp index c61bd566c7676f..113f4cfc31c104 100644 --- a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp +++ b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp @@ -3915,6 +3915,49 @@ void AffineParallelOp::setSteps(ArrayRef newSteps) { setStepsAttr(getBodyBuilder().getI64ArrayAttr(newSteps)); } +// check whether resultType match op or not in affine.parallel +static bool isResultTypeMatchAtomicRMWKind(Type resultType, + arith::AtomicRMWKind op) { + switch (op) { + case arith::AtomicRMWKind::addf: + return isa(resultType); + case arith::AtomicRMWKind::addi: + return isa(resultType); + case arith::AtomicRMWKind::assign: + return true; + case arith::AtomicRMWKind::mulf: + return isa(resultType); + case arith::AtomicRMWKind::muli: + return isa(resultType); + case arith::AtomicRMWKind::maximumf: + return isa(resultType); + case arith::AtomicRMWKind::minimumf: + return isa(resultType); + case arith::AtomicRMWKind::maxs: { + auto intType = llvm::dyn_cast(resultType); + return intType && intType.isSigned(); + } + case arith::AtomicRMWKind::mins: { + auto intType = llvm::dyn_cast(resultType); + return intType && intType.isSigned(); + } + case arith::AtomicRMWKind::maxu: { + auto intType = llvm::dyn_cast(resultType); + return intType && intType.isUnsigned(); + } + case arith::AtomicRMWKind::minu: { + auto intType = llvm::dyn_cast(resultType); + return intType && intType.isUnsigned(); + } + case arith::AtomicRMWKind::ori: + return isa(resultType); + case arith::AtomicRMWKind::andi: + return isa(resultType); + default: + return false; + } +} + LogicalResult AffineParallelOp::verify() { auto numDims = getNumDims(); if (getLowerBoundsGroups().getNumElements() != numDims || @@ -3946,11 +3989,16 @@ LogicalResult AffineParallelOp::verify() { if (getReductions().size() != getNumResults()) return emitOpError("a reduction must be specified for each output"); - // Verify reduction ops are all valid - for (Attribute attr : getReductions()) { + // Verify reduction ops are all valid and each result type matches reduction + // ops + for (auto it : llvm::enumerate((getReductions()))) { + Attribute attr = it.value(); auto intAttr = llvm::dyn_cast(attr); if (!intAttr || !arith::symbolizeAtomicRMWKind(intAttr.getInt())) return emitOpError("invalid reduction attribute"); + auto kind = arith::symbolizeAtomicRMWKind(intAttr.getInt()).value(); + if (!isResultTypeMatchAtomicRMWKind(getResult(it.index()).getType(), kind)) + return emitOpError("result type cannot match reduction attribute"); } // Verify that the bound operands are valid dimension/symbols. diff --git a/mlir/test/Dialect/Affine/invalid.mlir b/mlir/test/Dialect/Affine/invalid.mlir index 1dc3451ed7db87..1bcb6fc4a365dd 100644 --- a/mlir/test/Dialect/Affine/invalid.mlir +++ b/mlir/test/Dialect/Affine/invalid.mlir @@ -297,6 +297,18 @@ func.func @affine_parallel(%arg0 : index, %arg1 : index, %arg2 : index) { // ----- +func.func @affine_parallel(%arg0 : index, %arg1 : index, %arg2 : index) { + %0 = memref.alloc() : memref<100x100xi32> + // expected-error@+1 {{result type cannot match reduction attribute}} + %1 = affine.parallel (%i, %j) = (0, 0) to (100, 100) step (10, 10) reduce ("minimumf") -> (i32) { + %2 = affine.load %0[%i, %j] : memref<100x100xi32> + affine.yield %2 : i32 + } + return +} + +// ----- + func.func @vector_load_invalid_vector_type() { %0 = memref.alloc() : memref<100xf32> affine.for %i0 = 0 to 16 step 8 {