Skip to content

Commit 71bdd2c

Browse files
rohanyRohan Yadav
andauthored
mlir/lib/Dialect/GPU/Transforms: improve context management in SerializeToCubin (#65779)
This commit adjusts the CUDA context management in the SerializeToCubin pass. In particular, it uses the device 0 primary context instead of creating a new CUDA context on each invocation of SerializeToCubin. This yields very large improvements in compile time, especially if an application (like a JIT compiler) is calling SerializeToCubin repeatedly. Differential Revision: https://reviews.llvm.org/D159487 Co-authored-by: Rohan Yadav <[email protected]>
1 parent 6a2f68b commit 71bdd2c

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

mlir/lib/Dialect/GPU/Transforms/SerializeToCubin.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ SerializeToCubinPass::serializeISA(const std::string &isa) {
110110
CUdevice device;
111111
RETURN_ON_CUDA_ERROR(cuDeviceGet(&device, 0));
112112
CUcontext context;
113-
RETURN_ON_CUDA_ERROR(cuCtxCreate(&context, 0, device));
113+
// Use the primary context.
114+
RETURN_ON_CUDA_ERROR(cuDevicePrimaryCtxRetain(&context, device));
115+
// Push the primary context so that the next CUDA operations
116+
// actually use it.
117+
RETURN_ON_CUDA_ERROR(cuCtxPushCurrent(context));
114118
CUlinkState linkState;
115119

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

147151
// This will also destroy the cubin data.
148152
RETURN_ON_CUDA_ERROR(cuLinkDestroy(linkState));
149-
RETURN_ON_CUDA_ERROR(cuCtxDestroy(context));
153+
// Pop and release the primary context.
154+
CUcontext poppedContext;
155+
RETURN_ON_CUDA_ERROR(cuCtxPopCurrent(&poppedContext));
156+
RETURN_ON_CUDA_ERROR(cuDevicePrimaryCtxRelease(device));
150157

151158
return result;
152159
}

0 commit comments

Comments
 (0)