Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ def MemRef_CastOp : MemRef_Op<"cast", [
}];

let hasFolder = 1;
let hasCanonicalizer = 1;
}

//===----------------------------------------------------------------------===//
Expand Down
29 changes: 29 additions & 0 deletions mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,35 @@ OpFoldResult CastOp::fold(FoldAdaptor adaptor) {
return succeeded(foldMemRefCast(*this)) ? getResult() : Value();
}

namespace {
struct HoistCastPos : public OpRewritePattern<CastOp> {
using OpRewritePattern<CastOp>::OpRewritePattern;
LogicalResult matchAndRewrite(CastOp castOp,
PatternRewriter &rewriter) const override {
if (auto *defineOp = castOp.getSource().getDefiningOp()) {
if (defineOp->getBlock() != castOp->getBlock()) {
rewriter.moveOpAfter(castOp.getOperation(), defineOp);
return success();
}
return failure();
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else {
}

Nit: no else-after-return

auto argument = cast<BlockArgument>(castOp.getSource());
if (argument.getOwner() != castOp->getBlock()) {
rewriter.moveOpBefore(castOp.getOperation(),
&argument.getOwner()->front());
return success();
}
return failure();
}
}
};
} // namespace

void CastOp::getCanonicalizationPatterns(RewritePatternSet &results,
MLIRContext *context) {
results.add<HoistCastPos>(context);
}

FailureOr<std::optional<SmallVector<Value>>>
CastOp::bubbleDownCasts(OpBuilder &builder) {
return bubbleDownCastsPassthroughOpImpl(*this, builder, getSourceMutable());
Expand Down
41 changes: 41 additions & 0 deletions mlir/test/Dialect/MemRef/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1367,3 +1367,44 @@ func.func @non_fold_view_same_source_res_types(%0: memref<?xi8>, %arg0 : index)
%res = memref.view %0[%c0][%arg0] : memref<?xi8> to memref<?xi8>
return %res : memref<?xi8>
}

// -----

// CHECK-LABEL: func @hoist_cast_pos
// CHECK-SAME: %[[ARG0:.*]]: memref<10xf32>,
// CHECK-SAME: %[[ARG1:.*]]: i1
func.func @hoist_cast_pos(%arg: memref<10xf32>, %arg1: i1) -> (memref<?xf32>) {
// CHECK: %[[CAST_0:.*]] = memref.cast %[[ARG0]]
// CHECK: %[[CAST_1:.*]] = memref.cast %[[ARG0]]
// CHECK-NEXT: cf.cond_br %[[ARG1]]
cf.cond_br %arg1, ^bb1, ^bb2
^bb1:
%cast = memref.cast %arg : memref<10xf32> to memref<?xf32>
// CHECK: return %[[CAST_1]]
return %cast : memref<?xf32>
^bb2:
%cast1 = memref.cast %arg : memref<10xf32> to memref<?xf32>
// CHECK: return %[[CAST_0]]
return %cast1 : memref<?xf32>
}

// -----

// CHECK-LABEL: func.func @hoist_cast_pos_alloc
// CHECK-SAME: %[[ARG0:.*]]: i1
func.func @hoist_cast_pos_alloc(%arg: i1) -> (memref<?xf32>) {
// CHECK: %[[ALLOC_0:.*]] = memref.alloc()
// CHECK: %[[CAST_0:.*]] = memref.cast %[[ALLOC_0]]
// CHECK: %[[CAST_1:.*]] = memref.cast %[[ALLOC_0]]
// CHECK-NEXT: cf.cond_br %[[ARG0]]
%alloc = memref.alloc() : memref<10xf32>
cf.cond_br %arg, ^bb1, ^bb2
^bb1:
%cast = memref.cast %alloc : memref<10xf32> to memref<?xf32>
// CHECK: return %[[CAST_1]]
return %cast : memref<?xf32>
^bb2:
%cast1 = memref.cast %alloc : memref<10xf32> to memref<?xf32>
// CHECK: return %[[CAST_0]]
return %cast1 : memref<?xf32>
}
4 changes: 2 additions & 2 deletions mlir/test/Dialect/SCF/one-shot-bufferize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -922,13 +922,13 @@ func.func @elide_copy_of_non_writing_scf_if(%c: i1, %p1: index, %p2: index, %f:
// CHECK-SAME: %[[pred:.*]]: index, %[[b:.*]]: memref<{{.*}}>, %[[c:.*]]: memref<{{.*}}>) -> memref<{{.*}}>
func.func @index_switch(%pred: index, %b: tensor<5xf32>, %c: tensor<5xf32>) -> tensor<5xf32> {
// Throw in a tensor that bufferizes to a different layout map.
// CHECK: %[[a:.*]] = memref.alloc() {{.*}} : memref<5xf32>
// CHECK: %[[a:.*]] = memref.alloc() {{.*}} : memref<5xf32>
// CHECK: %[[cast:.*]] = memref.cast %[[a]] : memref<5xf32> to memref<5xf32, strided<[?], offset: ?>>
%a = bufferization.alloc_tensor() : tensor<5xf32>

// CHECK: %[[r:.*]] = scf.index_switch %[[pred]] -> memref<5xf32, strided<[?], offset: ?>>
%0 = scf.index_switch %pred -> tensor<5xf32>
// CHECK: case 2 {
// CHECK: %[[cast:.*]] = memref.cast %[[a]] : memref<5xf32> to memref<5xf32, strided<[?], offset: ?>>
// CHECK: scf.yield %[[cast]]
case 2 {
scf.yield %a: tensor<5xf32>
Expand Down
Loading