Skip to content

Commit

Permalink
add an option in insertGpuAllocs to skip the func args copy
Browse files Browse the repository at this point in the history
  • Loading branch information
zhczhong committed Oct 9, 2024
1 parent 1fd2e00 commit 32a6408
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
3 changes: 3 additions & 0 deletions include/imex/Transforms/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
#include <memory>

namespace imex {
struct InsertGPUAllocsOptions;
//===----------------------------------------------------------------------===//
// Passes
//===----------------------------------------------------------------------===//
std::unique_ptr<mlir::Pass> createSerializeSPIRVPass();
std::unique_ptr<mlir::Pass>
createInsertGPUAllocsPass(const char *clientAPI = "vulkan");
std::unique_ptr<mlir::Pass>
createInsertGPUAllocsPass(const InsertGPUAllocsOptions &);
std::unique_ptr<mlir::Pass> createSetSPIRVCapabilitiesPass();
std::unique_ptr<mlir::Pass>
createSetSPIRVAbiAttributePass(const char *clientAPI = "vulkan");
Expand Down
6 changes: 5 additions & 1 deletion include/imex/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ def InsertGPUAllocs : Pass<"insert-gpu-allocs", "::mlir::func::FuncOp"> {
Option<"clientAPI", "client-api", "std::string", /*default=*/"\"opencl\"",
"The client API to use for inserting gpu allocs">,
Option<"inRegions", "in-regions", "bool", "false",
"Add gpu allocs only for memref.AllocOps within GPU regions">
"Add gpu allocs only for memref.AllocOps within GPU regions">,
Option<"isUsmArgs", "is-usm-args", "bool", "false",
"Whether to use USM(unified shared memory) func args, in which the "
"host and device could access the same buffer and there is no need "
"to add memcpy explicitly">
];
}

Expand Down
30 changes: 21 additions & 9 deletions lib/Transforms/InsertGPUAllocs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ class InsertGPUAllocsPass final
explicit InsertGPUAllocsPass() : m_clientAPI("vulkan") {}
explicit InsertGPUAllocsPass(const mlir::StringRef &clientAPI)
: m_clientAPI(clientAPI) {}
explicit InsertGPUAllocsPass(const imex::InsertGPUAllocsOptions &options)
: InsertGPUAllocsBase<InsertGPUAllocsPass>(options) {
if (clientAPI == "opencl") {
m_clientAPI = "opencl";
}
}

mlir::LogicalResult
initializeOptions(mlir::StringRef options,
Expand Down Expand Up @@ -540,15 +546,17 @@ class InsertGPUAllocsPass final
// This is the case where the inputs are passed as arguments to the
// function. This code will add the IR for memory allocation on the device
// with gpu.alloc and insert a memref.copy from host to device
for (const auto &it : gpuBufferParams) {
auto param = block.getArgument(it.first);
if (isGpuAddrSpace(param))
continue;
auto access = getAccessType(param);
access.hostRead = true;
access.hostWrite = true;
builder.setInsertionPointToStart(&block);
add_gpu_alloc(builder, param, access, term);
if (!isUsmArgs.getValue()) {
for (const auto &it : gpuBufferParams) {
auto param = block.getArgument(it.first);
if (isGpuAddrSpace(param))
continue;
auto access = getAccessType(param);
access.hostRead = true;
access.hostWrite = true;
builder.setInsertionPointToStart(&block);
add_gpu_alloc(builder, param, access, term);
}
}

// CallOp Case: This is the case where the memref producer is coming
Expand Down Expand Up @@ -580,4 +588,8 @@ namespace imex {
std::unique_ptr<mlir::Pass> createInsertGPUAllocsPass(const char *clientAPI) {
return std::make_unique<InsertGPUAllocsPass>(clientAPI);
}
std::unique_ptr<mlir::Pass>
createInsertGPUAllocsPass(const InsertGPUAllocsOptions &option) {
return std::make_unique<InsertGPUAllocsPass>(option);
}
} // namespace imex

0 comments on commit 32a6408

Please sign in to comment.