Skip to content

Add RAII destructors to CUDA driver objects#106

Open
badnikhil wants to merge 1 commit into
libmir:masterfrom
badnikhil:harden/raii-destructors-v2
Open

Add RAII destructors to CUDA driver objects#106
badnikhil wants to merge 1 commit into
libmir:masterfrom
badnikhil:harden/raii-destructors-v2

Conversation

@badnikhil

Copy link
Copy Markdown
Contributor

Cleanup of driver objects is fully manual (Buffer.release(), Context.detach(), Program.unload()), and Queue/Event have no cleanup path at all - a forgotten release() leaks device memory, streams, or modules.

Changes

  • Buffer, Queue, Context, Program and Event free their CUDA handle when the last owner goes out of scope. Copies share a malloc'd atomic refcount via postblit (refcounting over @disable this(this) because the public API copies these structs by value — Queue.enqueue's Call struct, Context.pop/current, kernel args).
  • The counter is malloc'd, not GC-allocated, so destructors running as GC finalizers never touch GC memory mid-sweep; destructors swallow driver errors (finalizer threads may have no current context).
  • release()/unload()/detach() now drop one owner and only the last owner frees — releasing one copy no longer invalidates surviving copies, and double release or release-then-destructor is a no-op.
  • Context.pop/current and UnifiedBuffer.asBuffer are non-owning views (null refcount, never destroy).
  • Event gains a cuEventCreate constructor so it participates in RAII.

Tests

New hardware tests in tests/main.d (RTX 2050, sm_86, all passing with the existing suite): refcount survives copies; last-owner-frees with no double free; release-then-destructor safety; non-owning views; cuMemGetInfo-verified checks that a scoped 128 MiB Buffer's destructor really returns device memory and copies free exactly once; 1000-iteration Queue+Event create/destroy churn.

Note: the memory-verification tests use cuMemGetInfo, added to bindbc-cuda

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant