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
12 changes: 10 additions & 2 deletions compiler/lib/Dialect/MemRef/Transforms/RemoveCopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,19 @@ class RemoveCopyPattern : public OpRewritePattern<memref::CopyOp> {
return false;
};

bool targetUsedInTerm = false;
if (target.getType() != src.getType()) {
// skip copy when it is used in a terminator
if (auto srcAlloc = src.getDefiningOp<memref::AllocOp>()) {
if (allocUseInTerminator(srcAlloc)) {
LLVM_DEBUG(llvm::dbgs() << "src is used in a terminator");
return failure();
}
}
if (auto targetAlloc = target.getDefiningOp<memref::AllocOp>()) {
if (allocUseInTerminator(targetAlloc)) {
return failure();
LLVM_DEBUG(llvm::dbgs() << "target is used in a terminator");
targetUsedInTerm = true;
}
}
}
Expand Down Expand Up @@ -254,8 +257,10 @@ class RemoveCopyPattern : public OpRewritePattern<memref::CopyOp> {
}

// now it is legal to rewrite.
LLVM_DEBUG(llvm::dbgs() << "it is legal to rewrite " << copyOp << "\n");
// we prefer target alloc over src alloc in this implementation
if (auto targetAlloc = target.getDefiningOp<memref::AllocOp>()) {
LLVM_DEBUG(llvm::dbgs() << "match target alloc: " << targetAlloc << "\n");
if (auto srcDef = src.getDefiningOp()) {
if (isa<memref::AllocOp, memref::SubViewOp, memref::ExpandShapeOp,
memref::ExpandShapeOp>(srcDef))
Expand All @@ -269,11 +274,13 @@ class RemoveCopyPattern : public OpRewritePattern<memref::CopyOp> {
return failure();
}

if (!anyIncompatibleUse(target, src)) {
LLVM_DEBUG(llvm::dbgs() << "check anyIncompatibleUse\n");
if (!anyIncompatibleUse(target, src) && !targetUsedInTerm) {
replaceUsesAndPropagateType(rewriter, targetAlloc, src);
return success();
}

LLVM_DEBUG(llvm::dbgs() << "check anyIncompatibleUseWithCast\n");
if (!anyIncompatibleUseWithCast(target, src)) {
// The memref of source and target are contiguous, cast source value to
// the same type with target. As `byre.alias` could handle source with
Expand Down Expand Up @@ -324,6 +331,7 @@ class RemoveCopyPattern : public OpRewritePattern<memref::CopyOp> {
}

if (auto srcAlloc = src.getDefiningOp<memref::AllocOp>()) {
LLVM_DEBUG(llvm::dbgs() << "match src alloc: " << srcAlloc << "\n");
if (auto targetDef = target.getDefiningOp()) {
if (isa<memref::AllocOp, memref::SubViewOp>(targetDef))
hoistUpOpInBlock(targetDef, domInfo);
Expand Down
22 changes: 10 additions & 12 deletions compiler/test/E2E/Host/AliasLike/01_HostOpt.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,17 @@ module {
}
func.func @main(%arg0: memref<512x200xf32>, %arg1: memref<512x200xf32>) -> (memref<128x2x100xf32>, memref<128x2x100xf32>, memref<1x100xf32>, memref<1x100xf32>, memref<512x200xf32>) attributes {__placeholder__byre.entry_point} {
%subview = memref.subview %arg0[0, 0] [128, 200] [1, 1] : memref<512x200xf32> to memref<128x200xf32, strided<[200, 1]>>
%subview_0 = memref.subview %arg1[10, 0] [128, 200] [1, 1] : memref<512x200xf32> to memref<128x200xf32, strided<[200, 1], offset: 2000>>
%expand_shape = memref.expand_shape %subview [[0], [1, 2]] output_shape [128, 2, 100] : memref<128x200xf32, strided<[200, 1]>> into memref<128x2x100xf32, strided<[200, 100, 1]>>
%expand_shape_1 = memref.expand_shape %subview_0 [[0], [1, 2]] output_shape [128, 2, 100] : memref<128x200xf32, strided<[200, 1], offset: 2000>> into memref<128x2x100xf32, strided<[200, 100, 1], offset: 2000>>
%subview_2 = memref.subview %arg0[0, 0] [1, 100] [1, 1] : memref<512x200xf32> to memref<1x100xf32, strided<[200, 1]>>
%subview_3 = memref.subview %arg1[10, 100] [1, 100] [1, 1] : memref<512x200xf32> to memref<1x100xf32, strided<[200, 1], offset: 2100>>
%subview_0 = memref.subview %arg1[10, 100] [1, 100] [1, 1] : memref<512x200xf32> to memref<1x100xf32, strided<[200, 1], offset: 2100>>
%subview_1 = memref.subview %arg1[10, 0] [128, 200] [1, 1] : memref<512x200xf32> to memref<128x200xf32, strided<[200, 1], offset: 2000>>
%expand_shape = memref.expand_shape %subview_1 [[0], [1, 2]] output_shape [128, 2, 100] : memref<128x200xf32, strided<[200, 1], offset: 2000>> into memref<128x2x100xf32, strided<[200, 100, 1], offset: 2000>>
%expand_shape_2 = memref.expand_shape %subview [[0], [1, 2]] output_shape [128, 2, 100] : memref<128x200xf32, strided<[200, 1]>> into memref<128x2x100xf32, strided<[200, 100, 1]>>
%0 = call @Unknown0(%arg0, %arg1) : (memref<512x200xf32>, memref<512x200xf32>) -> memref<512x200xf32>
%cast = memref.cast %expand_shape : memref<128x2x100xf32, strided<[200, 100, 1]>> to memref<128x2x100xf32>
%cast = memref.cast %expand_shape_2 : memref<128x2x100xf32, strided<[200, 100, 1]>> to memref<128x2x100xf32>
%alloc = memref.alloc() : memref<128x2x100xf32>
memref.copy %expand_shape_1, %alloc : memref<128x2x100xf32, strided<[200, 100, 1], offset: 2000>> to memref<128x2x100xf32>
%alloc_4 = memref.alloc() : memref<1x100xf32>
memref.copy %subview_2, %alloc_4 : memref<1x100xf32, strided<[200, 1]>> to memref<1x100xf32>
%alloc_5 = memref.alloc() : memref<1x100xf32>
memref.copy %subview_3, %alloc_5 : memref<1x100xf32, strided<[200, 1], offset: 2100>> to memref<1x100xf32>
return %cast, %alloc, %alloc_4, %alloc_5, %0 : memref<128x2x100xf32>, memref<128x2x100xf32>, memref<1x100xf32>, memref<1x100xf32>, memref<512x200xf32>
memref.copy %expand_shape, %alloc : memref<128x2x100xf32, strided<[200, 100, 1], offset: 2000>> to memref<128x2x100xf32>
%reinterpret_cast = memref.reinterpret_cast %arg0 to offset: [0], sizes: [1, 100], strides: [100, 1] : memref<512x200xf32> to memref<1x100xf32>
%alloc_3 = memref.alloc() : memref<1x100xf32>
memref.copy %subview_0, %alloc_3 : memref<1x100xf32, strided<[200, 1], offset: 2100>> to memref<1x100xf32>
return %cast, %alloc, %reinterpret_cast, %alloc_3, %0 : memref<128x2x100xf32>, memref<128x2x100xf32>, memref<1x100xf32>, memref<1x100xf32>, memref<512x200xf32>
}
}
8 changes: 4 additions & 4 deletions compiler/test/E2E/Host/AliasLike/02a_ByreHost.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ module attributes {byre.container_module} {
}
func.func @main(%arg0: memref<512x200xf32, "cpu"> {byre.argname = "Input0", byre.argtype = 1 : i32}, %arg1: memref<512x200xf32, "cpu"> {byre.argname = "Input1", byre.argtype = 1 : i32}, %arg2: memref<128x2x100xf32, "cpu"> {byre.argname = "Output0", byre.argtype = 2 : i32}, %arg3: memref<128x2x100xf32, "cpu"> {byre.argname = "Output1", byre.argtype = 2 : i32}, %arg4: memref<1x100xf32, "cpu"> {byre.argname = "Output2", byre.argtype = 2 : i32}, %arg5: memref<1x100xf32, "cpu"> {byre.argname = "Output3", byre.argtype = 2 : i32}, %arg6: memref<512x200xf32, "cpu"> {byre.argname = "Output4", byre.argtype = 2 : i32}) attributes {byre.entry_point} {
byre.compute @LLVMJITOp(%arg0, %arg1, %arg6) {kernel_name = "Unknown0", llvm_file_name = "host_kernels.ll", memory_effects = [1 : i32, 1 : i32, 2 : i32]} : memref<512x200xf32, "cpu">, memref<512x200xf32, "cpu">, memref<512x200xf32, "cpu">
%0 = "byre.alias"(%arg1) <{offset = 2000 : i64}> : (memref<512x200xf32, "cpu">) -> memref<128x2x100xf32, "cpu">
byre.copy(%0, %arg3) {callee = "cpu2cpu"} : memref<128x2x100xf32, "cpu">, memref<128x2x100xf32, "cpu">
%1 = "byre.alias"(%arg0) <{offset = 0 : i64}> : (memref<512x200xf32, "cpu">) -> memref<1x100xf32, "cpu">
byre.copy(%1, %arg4) {callee = "cpu2cpu"} : memref<1x100xf32, "cpu">, memref<1x100xf32, "cpu">
%0 = "byre.alias"(%arg0) <{offset = 0 : i64}> : (memref<512x200xf32, "cpu">) -> memref<1x100xf32, "cpu">
%1 = "byre.alias"(%arg1) <{offset = 2000 : i64}> : (memref<512x200xf32, "cpu">) -> memref<128x2x100xf32, "cpu">
byre.copy(%1, %arg3) {callee = "cpu2cpu"} : memref<128x2x100xf32, "cpu">, memref<128x2x100xf32, "cpu">
%2 = "byre.alias"(%arg1) <{offset = 2100 : i64}> : (memref<512x200xf32, "cpu">) -> memref<1x100xf32, "cpu">
byre.copy(%2, %arg5) {callee = "cpu2cpu"} : memref<1x100xf32, "cpu">, memref<1x100xf32, "cpu">
%3 = "byre.alias"(%arg0) <{offset = 0 : i64}> : (memref<512x200xf32, "cpu">) -> memref<128x2x100xf32, "cpu">
byre.copy(%3, %arg2) {callee = "cpu2cpu"} : memref<128x2x100xf32, "cpu">, memref<128x2x100xf32, "cpu">
byre.copy(%0, %arg4) {callee = "cpu2cpu"} : memref<1x100xf32, "cpu">, memref<1x100xf32, "cpu">
return
}
}
8 changes: 4 additions & 4 deletions compiler/test/E2E/Host/AliasLike/02b_ToLLVM.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ module attributes {byre.container_module} {
}
func.func @main(%arg0: memref<512x200xf32, "cpu"> {byre.argname = "Input0", byre.argtype = 1 : i32}, %arg1: memref<512x200xf32, "cpu"> {byre.argname = "Input1", byre.argtype = 1 : i32}, %arg2: memref<128x2x100xf32, "cpu"> {byre.argname = "Output0", byre.argtype = 2 : i32}, %arg3: memref<128x2x100xf32, "cpu"> {byre.argname = "Output1", byre.argtype = 2 : i32}, %arg4: memref<1x100xf32, "cpu"> {byre.argname = "Output2", byre.argtype = 2 : i32}, %arg5: memref<1x100xf32, "cpu"> {byre.argname = "Output3", byre.argtype = 2 : i32}, %arg6: memref<512x200xf32, "cpu"> {byre.argname = "Output4", byre.argtype = 2 : i32}) attributes {byre.entry_point} {
byre.compute @LLVMJITOp(%arg0, %arg1, %arg6) {kernel_name = "Unknown0", llvm_file_name = "host_kernels.ll", memory_effects = [1 : i32, 1 : i32, 2 : i32]} : memref<512x200xf32, "cpu">, memref<512x200xf32, "cpu">, memref<512x200xf32, "cpu">
%0 = "byre.alias"(%arg1) <{offset = 2000 : i64}> : (memref<512x200xf32, "cpu">) -> memref<128x2x100xf32, "cpu">
byre.copy(%0, %arg3) {callee = "cpu2cpu"} : memref<128x2x100xf32, "cpu">, memref<128x2x100xf32, "cpu">
%1 = "byre.alias"(%arg0) <{offset = 0 : i64}> : (memref<512x200xf32, "cpu">) -> memref<1x100xf32, "cpu">
byre.copy(%1, %arg4) {callee = "cpu2cpu"} : memref<1x100xf32, "cpu">, memref<1x100xf32, "cpu">
%0 = "byre.alias"(%arg0) <{offset = 0 : i64}> : (memref<512x200xf32, "cpu">) -> memref<1x100xf32, "cpu">
%1 = "byre.alias"(%arg1) <{offset = 2000 : i64}> : (memref<512x200xf32, "cpu">) -> memref<128x2x100xf32, "cpu">
byre.copy(%1, %arg3) {callee = "cpu2cpu"} : memref<128x2x100xf32, "cpu">, memref<128x2x100xf32, "cpu">
%2 = "byre.alias"(%arg1) <{offset = 2100 : i64}> : (memref<512x200xf32, "cpu">) -> memref<1x100xf32, "cpu">
byre.copy(%2, %arg5) {callee = "cpu2cpu"} : memref<1x100xf32, "cpu">, memref<1x100xf32, "cpu">
%3 = "byre.alias"(%arg0) <{offset = 0 : i64}> : (memref<512x200xf32, "cpu">) -> memref<128x2x100xf32, "cpu">
byre.copy(%3, %arg2) {callee = "cpu2cpu"} : memref<128x2x100xf32, "cpu">, memref<128x2x100xf32, "cpu">
byre.copy(%0, %arg4) {callee = "cpu2cpu"} : memref<1x100xf32, "cpu">, memref<1x100xf32, "cpu">
return
}
}
8 changes: 4 additions & 4 deletions compiler/test/E2E/Host/AliasLike/Output.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
module attributes {byre.container_module} {
func.func @main(%arg0: memref<512x200xf32, "cpu"> {byre.argname = "Input0", byre.argtype = 1 : i32}, %arg1: memref<512x200xf32, "cpu"> {byre.argname = "Input1", byre.argtype = 1 : i32}, %arg2: memref<128x2x100xf32, "cpu"> {byre.argname = "Output0", byre.argtype = 2 : i32}, %arg3: memref<128x2x100xf32, "cpu"> {byre.argname = "Output1", byre.argtype = 2 : i32}, %arg4: memref<1x100xf32, "cpu"> {byre.argname = "Output2", byre.argtype = 2 : i32}, %arg5: memref<1x100xf32, "cpu"> {byre.argname = "Output3", byre.argtype = 2 : i32}, %arg6: memref<512x200xf32, "cpu"> {byre.argname = "Output4", byre.argtype = 2 : i32}) attributes {byre.entry_point, device_file_name = "your_file"} {
byre.compute @LLVMJITOp(%arg0, %arg1, %arg6) {kernel_name = "Unknown0", llvm_file_name = "host_kernels.ll", memory_effects = [1 : i32, 1 : i32, 2 : i32]} : memref<512x200xf32, "cpu">, memref<512x200xf32, "cpu">, memref<512x200xf32, "cpu">
%0 = "byre.alias"(%arg1) <{offset = 2000 : i64}> : (memref<512x200xf32, "cpu">) -> memref<128x2x100xf32, "cpu">
byre.copy(%0, %arg3) {callee = "cpu2cpu"} : memref<128x2x100xf32, "cpu">, memref<128x2x100xf32, "cpu">
%1 = "byre.alias"(%arg0) <{offset = 0 : i64}> : (memref<512x200xf32, "cpu">) -> memref<1x100xf32, "cpu">
byre.copy(%1, %arg4) {callee = "cpu2cpu"} : memref<1x100xf32, "cpu">, memref<1x100xf32, "cpu">
%0 = "byre.alias"(%arg0) <{offset = 0 : i64}> : (memref<512x200xf32, "cpu">) -> memref<1x100xf32, "cpu">
%1 = "byre.alias"(%arg1) <{offset = 2000 : i64}> : (memref<512x200xf32, "cpu">) -> memref<128x2x100xf32, "cpu">
byre.copy(%1, %arg3) {callee = "cpu2cpu"} : memref<128x2x100xf32, "cpu">, memref<128x2x100xf32, "cpu">
%2 = "byre.alias"(%arg1) <{offset = 2100 : i64}> : (memref<512x200xf32, "cpu">) -> memref<1x100xf32, "cpu">
byre.copy(%2, %arg5) {callee = "cpu2cpu"} : memref<1x100xf32, "cpu">, memref<1x100xf32, "cpu">
%3 = "byre.alias"(%arg0) <{offset = 0 : i64}> : (memref<512x200xf32, "cpu">) -> memref<128x2x100xf32, "cpu">
byre.copy(%3, %arg2) {callee = "cpu2cpu"} : memref<128x2x100xf32, "cpu">, memref<128x2x100xf32, "cpu">
byre.copy(%0, %arg4) {callee = "cpu2cpu"} : memref<1x100xf32, "cpu">, memref<1x100xf32, "cpu">
return
}
}