Skip to content

mlir/lib/Dialect/GPU/Transforms: improve context management in SerializeToCubin #65779

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 1 commit into from
Oct 20, 2023
Merged
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
11 changes: 9 additions & 2 deletions mlir/lib/Dialect/GPU/Transforms/SerializeToCubin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ SerializeToCubinPass::serializeISA(const std::string &isa) {
CUdevice device;
RETURN_ON_CUDA_ERROR(cuDeviceGet(&device, 0));
CUcontext context;
RETURN_ON_CUDA_ERROR(cuCtxCreate(&context, 0, device));
// Use the primary context.
RETURN_ON_CUDA_ERROR(cuDevicePrimaryCtxRetain(&context, device));
// Push the primary context so that the next CUDA operations
// actually use it.
RETURN_ON_CUDA_ERROR(cuCtxPushCurrent(context));
CUlinkState linkState;

CUjit_option jitOptions[] = {CU_JIT_ERROR_LOG_BUFFER,
Expand Down Expand Up @@ -146,7 +150,10 @@ SerializeToCubinPass::serializeISA(const std::string &isa) {

// This will also destroy the cubin data.
RETURN_ON_CUDA_ERROR(cuLinkDestroy(linkState));
RETURN_ON_CUDA_ERROR(cuCtxDestroy(context));
// Pop and release the primary context.
CUcontext poppedContext;
RETURN_ON_CUDA_ERROR(cuCtxPopCurrent(&poppedContext));
RETURN_ON_CUDA_ERROR(cuDevicePrimaryCtxRelease(device));

return result;
}
Expand Down