Skip to content

[MLIR] Modify lowering of gpu.alloc op to llvm #69969

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

Merged
merged 3 commits into from
Oct 25, 2023
Merged
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
13 changes: 11 additions & 2 deletions mlir/lib/Conversion/GPUCommon/GPUToLLVMConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,11 @@ LogicalResult ConvertAllocOpToGpuRuntimeCallPattern::matchAndRewrite(
// Allocate the underlying buffer and store a pointer to it in the MemRef
// descriptor.
Type elementPtrType = this->getElementPtrType(memRefType);
auto stream = adaptor.getAsyncDependencies().front();

auto nullPtr = rewriter.create<mlir::LLVM::ZeroOp>(loc, llvmPointerType);
Value stream = adaptor.getAsyncDependencies().empty()
? nullPtr
: adaptor.getAsyncDependencies().front();

auto isHostShared = rewriter.create<mlir::LLVM::ConstantOp>(
loc, llvmInt8Type, rewriter.getI8IntegerAttr(isShared));
Expand All @@ -855,7 +859,12 @@ LogicalResult ConvertAllocOpToGpuRuntimeCallPattern::matchAndRewrite(
auto memRefDescriptor = this->createMemRefDescriptor(
loc, memRefType, allocatedPtr, alignedPtr, shape, strides, rewriter);

rewriter.replaceOp(allocOp, {memRefDescriptor, stream});
if (allocOp.getAsyncToken()) {
// Async alloc: make dependent ops use the same stream.
rewriter.replaceOp(allocOp, {memRefDescriptor, stream});
} else {
rewriter.replaceOp(allocOp, {memRefDescriptor});
}

return success();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,22 @@ module attributes {gpu.container_module} {
gpu.wait [%3]
return
}

// CHECK-LABEL: llvm.func @alloc_sync
// CHECK-SAME: %[[size:.*]]: i64
func.func @alloc_sync(%size : index) {
// CHECK: %[[gep:.*]] = llvm.getelementptr {{.*}}[%[[size]]]
// CHECK: %[[size_bytes:.*]] = llvm.ptrtoint %[[gep]]
// CHECK: %[[nullptr:.*]] = llvm.mlir.zero
// CHECK: %[[isHostShared:.*]] = llvm.mlir.constant
// CHECK: llvm.call @mgpuMemAlloc(%[[size_bytes]], %[[nullptr]], %[[isHostShared]])
%0 = gpu.alloc host_shared (%size) : memref<?xf32>
// CHECK: %[[stream:.*]] = llvm.call @mgpuStreamCreate()
%1 = gpu.wait async
%2 = gpu.dealloc async [%1] %0 : memref<?xf32>
// CHECK: llvm.call @mgpuStreamSynchronize(%[[stream]])
// CHECK: llvm.call @mgpuStreamDestroy(%[[stream]])
gpu.wait [%2]
return
}
}