[MLIR][ExecutionEngine] Tolerate CUDA_ERROR_DEINITIALIZED in mgpuModuleUnload#190563
[MLIR][ExecutionEngine] Tolerate CUDA_ERROR_DEINITIALIZED in mgpuModuleUnload#190563
Conversation
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-mlir-gpu @llvm/pr-subscribers-mlir-execution-engine Author: Jared Hoberock (jaredhoberock) Changes
ReproductionAny program that uses This script reproduces the error message from ContextThis matches how other projects handle the same shutdown ordering issue:
Full diff: https://github.com/llvm/llvm-project/pull/190563.diff 1 Files Affected:
diff --git a/mlir/lib/ExecutionEngine/CudaRuntimeWrappers.cpp b/mlir/lib/ExecutionEngine/CudaRuntimeWrappers.cpp
index 7bf6804902479..24c88b9fa587b 100644
--- a/mlir/lib/ExecutionEngine/CudaRuntimeWrappers.cpp
+++ b/mlir/lib/ExecutionEngine/CudaRuntimeWrappers.cpp
@@ -127,7 +127,12 @@ extern "C" MLIR_CUDA_WRAPPERS_EXPORT CUmodule mgpuModuleLoad(void *data) {
}
extern "C" MLIR_CUDA_WRAPPERS_EXPORT void mgpuModuleUnload(CUmodule module) {
- CUDA_REPORT_IF_ERROR(cuModuleUnload(module));
+ // At program exit, the CUDA primary context may already be destroyed.
+ // CUDA_ERROR_DEINITIALIZED is benign — the module's resources are already
+ // freed with the context.
+ CUresult result = cuModuleUnload(module);
+ if (result != CUDA_SUCCESS && result != CUDA_ERROR_DEINITIALIZED)
+ CUDA_REPORT_IF_ERROR(result);
}
extern "C" MLIR_CUDA_WRAPPERS_EXPORT CUfunction
|
|
This may be fixing #170833 ; we could reenable Is this the same issue? |
6ea02ea to
85a7ec6
Compare
|
I believe this is the same class of issue. The async.mlir test hits CUDA_ERROR_CONTEXT_IS_DESTROYED on stream/event dtors during shutdown, while the original report hits CUDA_ERROR_DEINITIALIZED on module unload. The PR is updated to handle both error codes across all five affected dtors, and re-enabled the FileCheck in async.mlir. async.mlir passes cleanly with the fix on my system. |
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
…urce cleanup During program exit, CUDA resource cleanup functions (module unload, stream/event destroy, etc.) may be called after the CUDA primary context has already been destroyed. This produces spurious error messages like 'cuModuleUnload(module)' failed with 'CUDA_ERROR_DEINITIALIZED' or 'cuEventDestroy(event)' failed with 'CUDA_ERROR_CONTEXT_IS_DESTROYED'. These errors are benign — all resources are already freed when the context is destroyed. Add CUDA_REPORT_IF_ERROR_IGNORE_SHUTDOWN macro that silences both CUDA_ERROR_DEINITIALIZED (4) and CUDA_ERROR_CONTEXT_IS_DESTROYED (709), and apply it to the five cleanup wrappers: mgpuModuleUnload, mgpuStreamDestroy, mgpuStreamWaitEvent, mgpuEventDestroy, and mgpuEventSynchronize. Re-enable FileCheck in async.mlir integration test. Fixes llvm#170833, fixes llvm#190563.
Head branch was pushed to by a user without write access
85a7ec6 to
4ef818b
Compare
|
Updated to fix clang-format |
|
@jaredhoberock Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
|
The buildbot failure on |
You need to open a new PR, did you? |
llvm#190563 re-enabled FileCheck on Integration/GPU/CUDA/async.mlir, but the buildbot has shown intermittent wrong-output failures: the test produces [42, 42] instead of the expected [84, 84]. This is distinct from the cleanup-time cuModuleUnload errors that llvm#190563 actually fixes; it is the underlying flakiness tracked by llvm#170833. Put the test back in its previously-disabled state.
|
You're right — apologies for the confusion. The revert was on a force-push that landed after the merge, so the merged commit still has FileCheck re-enabled. I've opened #190702 to actually re-disable it. Also, the merged commit incorrectly claims |
…0702) #190563 re-enabled FileCheck on `Integration/GPU/CUDA/async.mlir`, but the buildbot has shown intermittent wrong-output failures ([example](https://lab.llvm.org/buildbot/#/builders/116/builds/27026)): the test produces `[42, 42]` instead of the expected `[84, 84]`. This wrong-output flakiness is distinct from the cleanup-time `cuModuleUnload` errors that #190563 actually fixes — it's the underlying issue tracked by #170833. The merged commit message for #190563 incorrectly says `Fixes #170833`; that issue should be reopened, since the cleanup-error fix doesn't address the wrong-output behavior. This PR puts the test back in its previously-disabled state. The runtime cleanup fix in #190563 is unaffected.
…m#190702) llvm#190563 re-enabled FileCheck on `Integration/GPU/CUDA/async.mlir`, but the buildbot has shown intermittent wrong-output failures ([example](https://lab.llvm.org/buildbot/#/builders/116/builds/27026)): the test produces `[42, 42]` instead of the expected `[84, 84]`. This wrong-output flakiness is distinct from the cleanup-time `cuModuleUnload` errors that llvm#190563 actually fixes — it's the underlying issue tracked by llvm#170833. The merged commit message for llvm#190563 incorrectly says `Fixes llvm#170833`; that issue should be reopened, since the cleanup-error fix doesn't address the wrong-output behavior. This PR puts the test back in its previously-disabled state. The runtime cleanup fix in llvm#190563 is unaffected.
mgpuModuleUnloadmay be called from a global destructor (registered bySelectObjectAttr'sappendToGlobalDtors) after the CUDA primary context has already been destroyed during program shutdown. In this case,cuModuleUnloadreturnsCUDA_ERROR_DEINITIALIZED, which is benign since the module's resources are already freed with the context.Reproduction
Any program that uses
gpu.launch_funcand is AOT-compiled (viamlir-translate --mlir-to-llvmir | llc | cc -lmlir_cuda_runtime) will print'cuModuleUnload(module)' failed with '<unknown>'on exit. This is becauseSelectObjectAttrregisters the module unload as a global destructor, which runs after the CUDA primary context is released.This script reproduces the error message from
mgpuModuleUnloadon my system:Context
This matches how other projects handle the same shutdown ordering issue:
__attribute__((destructor))toatexit()cuModuleUnloadCUDA_ERROR_DEINITIALIZEDon module unloadFixes #170833