Skip to content

Commit

Permalink
[InsertGPUAllocs] Use gpu.memcpy for opencl instead of memref.copy
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyPavlenko committed Aug 30, 2024
1 parent 7d204dd commit 2120b27
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
15 changes: 10 additions & 5 deletions lib/Transforms/InsertGPUAllocs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,10 @@ class InsertGPUAllocsPass final
auto newAlloc = builder.create<mlir::memref::AllocOp>(
loc, alloc.getType(), alloc.getDynamicSizes(),
alloc.getSymbolOperands());
builder.create<mlir::memref::CopyOp>(loc, allocResult,
newAlloc.getResult());
builder.create<mlir::gpu::MemcpyOp>(
loc, /*asyncToken*/ static_cast<mlir::Type>(nullptr),
/*asyncDependencies*/ std::nullopt, newAlloc.getResult(),
allocResult);
use.set(newAlloc.getResult());
}
}
Expand Down Expand Up @@ -456,8 +458,9 @@ class InsertGPUAllocsPass final
/*symbolOperands*/ std::nullopt, hostShared);
auto allocResult = gpuAlloc.getResult(0);
if (access.hostWrite && access.deviceRead) {
auto copy =
builder.create<mlir::memref::CopyOp>(loc, op, allocResult);
auto copy = builder.create<mlir::gpu::MemcpyOp>(
loc, /*asyncToken*/ static_cast<mlir::Type>(nullptr),
/*asyncDependencies*/ std::nullopt, allocResult, op);
filter.insert(copy);
}

Expand All @@ -476,7 +479,9 @@ class InsertGPUAllocsPass final
op.replaceAllUsesExcept(allocResult, filter);
builder.setInsertionPoint(term);
if (access.hostRead && access.deviceWrite) {
builder.create<mlir::memref::CopyOp>(loc, allocResult, op);
builder.create<mlir::gpu::MemcpyOp>(
loc, /*asyncToken*/ static_cast<mlir::Type>(nullptr),
/*asyncDependencies*/ std::nullopt, op, allocResult);
}
builder.create<mlir::gpu::DeallocOp>(loc, std::nullopt, allocResult);
}
Expand Down
4 changes: 2 additions & 2 deletions test/Transforms/InsertGpuAllocs/add-gpu-alloc.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ func.func @addt(%arg0: memref<2x5xf32>, %arg1: memref<2x5xf32>) -> memref<2x5xf3
%c1 = arith.constant 1 : index
%c5 = arith.constant 5 : index
// OPENCL: %[[MEMREF0:.*]] = gpu.alloc host_shared () : memref<2x5xf32>
// OPENCL: memref.copy %arg1, %[[MEMREF0]] : memref<2x5xf32> to memref<2x5xf32>
// OPENCL: gpu.memcpy %[[MEMREF0]], %arg1 : memref<2x5xf32>, memref<2x5xf32>
// OPENCL: %[[MEMREF1:.*]] = gpu.alloc host_shared () : memref<2x5xf32>
// OPENCL: memref.copy %arg0, %[[MEMREF1]] : memref<2x5xf32> to memref<2x5xf32>
// OPENCL: gpu.memcpy %[[MEMREF1]], %arg0 : memref<2x5xf32>, memref<2x5xf32>
// VULKAN: %[[MEMREF0:.*]] = memref.alloc() : memref<2x5xf32>
// VULKAN: memref.copy %arg1, %[[MEMREF0]] : memref<2x5xf32> to memref<2x5xf32>
// VULKAN: %[[MEMREF1:.*]] = memref.alloc() : memref<2x5xf32>
Expand Down

0 comments on commit 2120b27

Please sign in to comment.