Eliminate the subsequent applications of `index_to_size` and `size_to_index`.
Differential Revision: https://reviews.llvm.org/D82082
let assemblyFormat = "$arg attr-dict";
let hasFolder = 1;
+ let hasCanonicalizer = 1;
}
def Shape_YieldOp : Shape_Op<"yield",
return {};
}
+void SizeToIndexOp::getCanonicalizationPatterns(
+ OwningRewritePatternList &patterns, MLIRContext *context) {
+ patterns.insert<IndexToSizeToIndexCanonicalization>(context);
+}
+
//===----------------------------------------------------------------------===//
// YieldOp
//===----------------------------------------------------------------------===//
}]>>;
// Canonicalization patterns.
+
def CstrBroadcastableEqOps : Pat<(Shape_CstrBroadcastableOp:$op $lhs, $rhs),
(Shape_ConstWitnessOp ConstBoolAttrTrue),
[(EqualBinaryOperands $lhs, $rhs)]>;
def CstrEqEqOps : Pat<(Shape_CstrEqOp:$op $shapes),
(Shape_ConstWitnessOp ConstBoolAttrTrue),
[(AllInputShapesEq $shapes)]>;
+
+def IndexToSizeToIndexCanonicalization : Pat<
+ (Shape_SizeToIndexOp (Shape_IndexToSizeOp $arg)),
+ (replaceWithValue $arg)>;
+
%rank = shape.rank %shape
return %rank : !shape.size
}
+
+// Canonicalize redundant conversion from `index` to `size` and back.
+// CHECK-LABEL: @index_to_size_to_index
+// CHECK-SAME: (%[[IDX:.*]]: index) -> index
+func @index_to_size_to_index(%index : index) -> index {
+ // CHECK: return %[[IDX]] : index
+ %size = shape.index_to_size %index
+ %result = shape.size_to_index %size
+ return %result : index
+}
+