Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

insert gpu2host copies when returning views of gpuAlloced memrefs #666

Closed
wants to merge 1 commit into from
Closed
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
42 changes: 33 additions & 9 deletions lib/Transforms/InsertGPUAllocs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,16 +263,40 @@ class InsertGPUAllocsPass final
alloc.getSymbolOperands(), hostShared);
auto allocResult = gpuAlloc.getResult(0);
builder.setInsertionPoint(term);
for (mlir::OpOperand &use : alloc.getResult().getUses()) {
if (use.getOwner() == term) {
auto newAlloc = builder.create<mlir::memref::AllocOp>(
loc, alloc.getType(), alloc.getDynamicSizes(),
alloc.getSymbolOperands());
builder.create<mlir::memref::CopyOp>(loc, allocResult,
newAlloc.getResult());
use.set(newAlloc.getResult());
}

// follow the users of alloc if they are view-like
// insert copy if they are terminator
auto insertCopyIfViewInTerminal = [&](auto &use) -> bool {
auto _insertCopyIfViewInTerminal = [&](auto &use, auto _insertCopyIfViewInTerminal_) -> bool {
auto user = use.getOwner();
if (user == term) {
auto newAlloc = builder.create<mlir::memref::AllocOp>(
loc, alloc.getType(), alloc.getDynamicSizes(),
alloc.getSymbolOperands());
builder.create<mlir::memref::CopyOp>(loc, allocResult,
newAlloc.getResult());
auto castop = builder.create<mlir::memref::CastOp>(
loc, use.get().getType(), newAlloc);
use.set(castop.getResult());
return true;
}
if (::mlir::isa<::mlir::ViewLikeOpInterface>(user)) {
assert(user->getNumResults() == 1);
for (auto &_use : user->getResult(0).getUses()) {
if (_insertCopyIfViewInTerminal_(_use, _insertCopyIfViewInTerminal_))
return true;
}
}
// on all other cases we do nothing
return true;
};
return _insertCopyIfViewInTerminal(use, _insertCopyIfViewInTerminal);
};

for (auto &use : alloc.getResult().getUses()) {
insertCopyIfViewInTerminal(use);
}

alloc.replaceAllUsesWith(allocResult);
builder.create<mlir::gpu::DeallocOp>(loc, std::nullopt, allocResult);
alloc.erase();
Expand Down
Loading