Skip to content

Commit

Permalink
[mlir][Linalg] Use ConfinedAttr for dimensions of ReduceOp.
Browse files Browse the repository at this point in the history
We can use the new DenseArrayStrictlySorted constraint.

Differential Revision: https://reviews.llvm.org/D135246
  • Loading branch information
akuegel committed Oct 10, 2022
1 parent 101799b commit 2ae27dc
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 11 deletions.
3 changes: 2 additions & 1 deletion mlir/include/mlir/Dialect/Linalg/IR/LinalgStructuredOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ def ReduceOp : LinalgStructuredBase_Op<"reduce", [
// Output arg
Variadic<TensorOrMemref>:$inits,

DenseI64ArrayAttr:$dimensions
ConfinedAttr<DenseI64ArrayAttr,
[DenseArrayStrictlySorted<DenseI64ArrayAttr>]>:$dimensions
);
let results = (outs Variadic<TensorOrMemref>);
let regions = (region SizedRegion<1>:$combiner);
Expand Down
8 changes: 0 additions & 8 deletions mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1340,20 +1340,12 @@ LogicalResult ReduceOp::verify() {
auto initType = getInits()[0].getType().cast<ShapedType>();

DenseSet<int64_t> dimensionsToReduce;
int64_t lastDimension = -1;
for (int64_t dimension : dimensionsRef) {
if (dimension < 0 || dimension >= inputType.getRank()) {
return emitOpError()
<< "dimensions for reduction should be in the range [0, "
<< inputType.getRank() - 1 << "].";
}
if (dimension <= lastDimension) {
return emitOpError()
<< "reduction dimensions are not in increasing order: "
<< dimensionsRef;
}

lastDimension = dimension;
dimensionsToReduce.insert(dimension);
}

Expand Down
4 changes: 2 additions & 2 deletions mlir/test/Dialect/Linalg/invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ func.func @reduce_dimensions_out_of_range(%input: tensor<16x32x64xf32>,

func.func @reduce_duplicate_dimensions(%input: tensor<16x32x64xf32>,
%init: tensor<16xf32>) -> tensor<16xf32> {
// expected-error @+1 {{'linalg.reduce' op reduction dimensions are not in increasing order: 1, 1}}
// expected-error @+1 {{'linalg.reduce' op attribute 'dimensions' failed to satisfy constraint: i64 dense array attribute should be in increasing order}}
%reduce = linalg.reduce
ins(%input:tensor<16x32x64xf32>)
outs(%init:tensor<16xf32>)
Expand All @@ -442,7 +442,7 @@ func.func @reduce_duplicate_dimensions(%input: tensor<16x32x64xf32>,

func.func @reduce_non_increasing_dimensions(%input: tensor<16x32x64xf32>,
%init: tensor<16xf32>) -> tensor<16xf32> {
// expected-error @+1 {{'linalg.reduce' op reduction dimensions are not in increasing order: 2, 1}}
// expected-error @+1 {{'linalg.reduce' op attribute 'dimensions' failed to satisfy constraint: i64 dense array attribute should be in increasing order}}
%reduce = linalg.reduce
ins(%input:tensor<16x32x64xf32>)
outs(%init:tensor<16xf32>)
Expand Down

0 comments on commit 2ae27dc

Please sign in to comment.