Jared Hoberock 7087ece044
[MLIR][ExecutionEngine] Tolerate CUDA_ERROR_DEINITIALIZED in mgpuModuleUnload (#190563)
`mgpuModuleUnload` may be called from a global destructor (registered by
`SelectObjectAttr`'s `appendToGlobalDtors`) after the CUDA primary
context has already been destroyed during program shutdown. In this
case, `cuModuleUnload` returns `CUDA_ERROR_DEINITIALIZED`, which is
benign since the module's resources are already freed with the context.

## Reproduction

Any program that uses `gpu.launch_func` and is AOT-compiled (via
`mlir-translate --mlir-to-llvmir | llc | cc -lmlir_cuda_runtime`) will
print `'cuModuleUnload(module)' failed with '<unknown>'` on exit. This
is because `SelectObjectAttr` registers the module unload as a global
destructor, which runs after the CUDA primary context is released.

This script reproduces the error message from `mgpuModuleUnload` on my
system:

```
#!/bin/bash
set -e

LLVM_BUILD=${LLVM_BUILD:-$HOME/dev/git/llvm-project-22/build}

cat > /tmp/repro.mlir << 'MLIR'
func.func @main() {
  %c1 = arith.constant 1 : index
  gpu.launch blocks(%bx, %by, %bz) in (%gx = %c1, %gy = %c1, %gz = %c1)
             threads(%tx, %ty, %tz) in (%bsx = %c1, %bsy = %c1, %bsz = %c1) {
    gpu.terminator
  }
  return
}
MLIR

$LLVM_BUILD/bin/mlir-opt /tmp/repro.mlir \
  -gpu-lower-to-nvvm-pipeline="cubin-format=fatbin" \
  | $LLVM_BUILD/bin/mlir-translate --mlir-to-llvmir -o /tmp/repro.ll

$LLVM_BUILD/bin/llc -relocation-model=pic -filetype=obj /tmp/repro.ll -o /tmp/repro.o

cc /tmp/repro.o \
  -L$LLVM_BUILD/lib -Wl,-rpath,$LLVM_BUILD/lib \
  -lmlir_cuda_runtime -lmlir_runner_utils -o /tmp/repro

echo "Running:"
/tmp/repro 2>&1
echo "Exit code: $?"
```
## Context

This matches how other projects handle the same shutdown ordering issue:
- Clang CUDA (D48613) switched module cleanup from
`__attribute__((destructor))` to `atexit()`
- GCC libgomp checks context validity before `cuModuleUnload`
- Apache TVM silently ignores `CUDA_ERROR_DEINITIALIZED` on module
unload

Fixes #170833
2026-04-06 21:11:58 +00:00

74 lines
2.7 KiB
MLIR

// RUN: mlir-opt %s \
// RUN: | mlir-opt -gpu-kernel-outlining \
// RUN: | mlir-opt -pass-pipeline='builtin.module(gpu.module(strip-debuginfo,convert-gpu-to-nvvm),nvvm-attach-target)' \
// RUN: | mlir-opt -gpu-async-region -gpu-to-llvm -reconcile-unrealized-casts -gpu-module-to-binary="format=%gpu_compilation_format" \
// RUN: | mlir-opt -async-to-async-runtime -async-runtime-ref-counting \
// RUN: | mlir-opt -convert-async-to-llvm -convert-func-to-llvm -convert-arith-to-llvm -convert-cf-to-llvm -reconcile-unrealized-casts \
// RUN: | mlir-runner \
// RUN: --shared-libs=%mlir_cuda_runtime \
// RUN: --shared-libs=%mlir_async_runtime \
// RUN: --shared-libs=%mlir_runner_utils \
// RUN: --entry-point-result=void -O0 \
// RUN: | FileCheck %s
func.func @main() {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%count = arith.constant 2 : index
// initialize h0 on host
%h0 = memref.alloc(%count) : memref<?xi32>
%h0_unranked = memref.cast %h0 : memref<?xi32> to memref<*xi32>
gpu.host_register %h0_unranked : memref<*xi32>
%v0 = arith.constant 42 : i32
memref.store %v0, %h0[%c0] : memref<?xi32>
memref.store %v0, %h0[%c1] : memref<?xi32>
// copy h0 to b0 on device.
%t0, %f0 = async.execute () -> !async.value<memref<?xi32>> {
%b0 = gpu.alloc(%count) : memref<?xi32>
gpu.memcpy %b0, %h0 : memref<?xi32>, memref<?xi32>
async.yield %b0 : memref<?xi32>
}
// copy h0 to b1 and b2 (fork)
%t1, %f1 = async.execute [%t0] (
%f0 as %b0 : !async.value<memref<?xi32>>
) -> !async.value<memref<?xi32>> {
%b1 = gpu.alloc(%count) : memref<?xi32>
gpu.memcpy %b1, %b0 : memref<?xi32>, memref<?xi32>
async.yield %b1 : memref<?xi32>
}
%t2, %f2 = async.execute [%t0] (
%f0 as %b0 : !async.value<memref<?xi32>>
) -> !async.value<memref<?xi32>> {
%b2 = gpu.alloc(%count) : memref<?xi32>
gpu.memcpy %b2, %b0 : memref<?xi32>, memref<?xi32>
async.yield %b2 : memref<?xi32>
}
// h0 = b1 + b2 (join).
%t3 = async.execute [%t1, %t2] (
%f1 as %b1 : !async.value<memref<?xi32>>,
%f2 as %b2 : !async.value<memref<?xi32>>
) {
gpu.launch blocks(%bx, %by, %bz) in (%grid_x = %c1, %grid_y = %c1, %grid_z = %c1)
threads(%tx, %ty, %tz) in (%block_x = %count, %block_y = %c1, %block_z = %c1) {
%v1 = memref.load %b1[%tx] : memref<?xi32>
%v2 = memref.load %b2[%tx] : memref<?xi32>
%sum = arith.addi %v1, %v2 : i32
memref.store %sum, %h0[%tx] : memref<?xi32>
gpu.terminator
}
async.yield
}
async.await %t3 : !async.token
// CHECK: [84, 84]
call @printMemrefI32(%h0_unranked) : (memref<*xi32>) -> ()
return
}
func.func private @printMemrefI32(memref<*xi32>)