Skip to content

Commit f959585

Browse files
author
Frederik Gossen
committed
[MLIR][Shape] Fold shape.shape_eq
Fold `shape.shape_eq`. Differential Revision: https://reviews.llvm.org/D82533
1 parent 47cbd9f commit f959585

3 files changed

Lines changed: 68 additions & 0 deletions

File tree

‎mlir/include/mlir/Dialect/Shape/IR/ShapeOps.td‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ def Shape_ShapeEqOp : Shape_Op<"shape_eq", [Commutative, NoSideEffect]> {
154154
let results = (outs I1:$result);
155155

156156
let assemblyFormat = "$lhs `,` $rhs attr-dict `:` type($lhs) `,` type($rhs)";
157+
let hasFolder = 1;
157158
}
158159

159160
def Shape_FromExtentsOp : Shape_Op<"from_extents", [NoSideEffect]> {

‎mlir/lib/Dialect/Shape/IR/Shape.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,20 @@ void ConstSizeOp::getAsmResultNames(
475475

476476
OpFoldResult ConstWitnessOp::fold(ArrayRef<Attribute>) { return passingAttr(); }
477477

478+
//===----------------------------------------------------------------------===//
479+
// ShapeEqOp
480+
//===----------------------------------------------------------------------===//
481+
482+
OpFoldResult ShapeEqOp::fold(ArrayRef<Attribute> operands) {
483+
auto lhs = operands[0].dyn_cast_or_null<DenseIntElementsAttr>();
484+
if (lhs == nullptr)
485+
return {};
486+
auto rhs = operands[1].dyn_cast_or_null<DenseIntElementsAttr>();
487+
if (rhs == nullptr)
488+
return {};
489+
return BoolAttr::get(lhs == rhs, getContext());
490+
}
491+
478492
//===----------------------------------------------------------------------===//
479493
// IndexToSizeOp
480494
//===----------------------------------------------------------------------===//

‎mlir/test/Dialect/Shape/canonicalize.mlir‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,3 +596,56 @@ func @cstr_broadcastable_scalar_unranked(%arg0 : tensor<*xf32>, %arg1 : tensor<i
596596
"consume.witness"(%2) : (!shape.witness) -> ()
597597
return
598598
}
599+
600+
// -----
601+
602+
// Fold `shape_eq` for equal and constant shapes.
603+
// CHECK-LABEL: @shape_eq_fold_1
604+
func @shape_eq_fold_1() -> i1 {
605+
// CHECK: %[[RESULT:.*]] = constant true
606+
// CHECK: return %[[RESULT]] : i1
607+
%a = shape.const_shape [1, 2, 3]
608+
%b = shape.const_shape [1, 2, 3]
609+
%result = shape.shape_eq %a, %b : !shape.shape, !shape.shape
610+
return %result : i1
611+
}
612+
613+
// -----
614+
615+
// Fold `shape_eq` for different but constant shapes of same length.
616+
// CHECK-LABEL: @shape_eq_fold_0
617+
func @shape_eq_fold_0() -> i1 {
618+
// CHECK: %[[RESULT:.*]] = constant false
619+
// CHECK: return %[[RESULT]] : i1
620+
%a = shape.const_shape [1, 2, 3]
621+
%b = shape.const_shape [4, 5, 6]
622+
%result = shape.shape_eq %a, %b : !shape.shape, !shape.shape
623+
return %result : i1
624+
}
625+
626+
// -----
627+
628+
// Fold `shape_eq` for different but constant shapes of different length.
629+
// CHECK-LABEL: @shape_eq_fold_0
630+
func @shape_eq_fold_0() -> i1 {
631+
// CHECK: %[[RESULT:.*]] = constant false
632+
// CHECK: return %[[RESULT]] : i1
633+
%a = shape.const_shape [1, 2, 3, 4, 5, 6]
634+
%b = shape.const_shape [1, 2, 3]
635+
%result = shape.shape_eq %a, %b : !shape.shape, !shape.shape
636+
return %result : i1
637+
}
638+
639+
// -----
640+
641+
// Do not fold `shape_eq` for non-constant shapes.
642+
// CHECK-LABEL: @shape_eq_do_not_fold
643+
// CHECK-SAME: (%[[A:.*]]: !shape.shape) -> i1
644+
func @shape_eq_do_not_fold(%a : !shape.shape) -> i1 {
645+
// CHECK: %[[B:.*]] = shape.const_shape [4, 5, 6]
646+
// CHECK: %[[RESULT:.*]] = shape.shape_eq %[[A]], %[[B]] : !shape.shape, !shape.shape
647+
// CHECK: return %[[RESULT]] : i1
648+
%b = shape.const_shape [4, 5, 6]
649+
%result = shape.shape_eq %a, %b : !shape.shape, !shape.shape
650+
return %result : i1
651+
}

0 commit comments

Comments
 (0)