From c7b9eb8b65cdc4795a9effd2c6643ac9dcdd8fbc Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 25 Nov 2026 14:19:37 +0100 Subject: [PATCH 1/2] Don't keep the deferred-free context in the package image The precompilation workload frees memory with the DEFER_FREE policy, which records the context in `oneL0.deferred_frees`. The workload cleanup did not reset it, so when the context was still alive while the image was written, the image kept it, with a null handle once loaded. Reclaiming memory after a failed allocation then swept deferred frees in that context, and crashed in zeMemAllocHost. Reset it both after the workload and when initializing the driver, like the extension properties cache. --- lib/level-zero/oneL0.jl | 1 + src/compiler/precompile.jl | 1 + test/level-zero.jl | 5 +++++ 3 files changed, 7 insertions(+) diff --git a/lib/level-zero/oneL0.jl b/lib/level-zero/oneL0.jl index 02849f09..c5a694d9 100644 --- a/lib/level-zero/oneL0.jl +++ b/lib/level-zero/oneL0.jl @@ -180,6 +180,7 @@ function initialize(; quiet::Bool=false) # Driver handles are process-local, so nothing keyed on one may survive into another # process through a precompiled image. empty!(extension_properties_cache) + @atomic deferred_frees.context = WeakRef() # Resolve the LTS master switch up front, before the driver-availability early # returns below: it gates codegen and behavior and must be set even on hosts diff --git a/src/compiler/precompile.jl b/src/compiler/precompile.jl index 61b1c628..cc70ed06 100644 --- a/src/compiler/precompile.jl +++ b/src/compiler/precompile.jl @@ -104,6 +104,7 @@ end empty!(free_report_slots) empty!(_compiler_configs) empty!(oneL0.extension_properties_cache) + Base.@atomic oneL0.deferred_frees.context = WeakRef() Base.@atomic oneAPIKernels.device_limits_cache.limits = oneAPIKernels.DeviceLimitsDict() empty!(oneAPIKernels.pending_events) _allocated_bytes[] = 0 diff --git a/test/level-zero.jl b/test/level-zero.jl index 95822690..ac1dd0ac 100644 --- a/test/level-zero.jl +++ b/test/level-zero.jl @@ -12,6 +12,11 @@ end # decide for other precompilation processes, and only applies while precompiling @test oneAPI.precompile_probe[] === nothing @test !oneAPI.can_precompile() + + # nor is the context the workload freed memory in, whose handle is null once loaded. + # checked in a fresh process, as freeing memory here replaces it. + script = "using oneAPI; exit(oneAPI.oneL0.deferred_frees.context.value === nothing ? 0 : 1)" + @test success(`$(Base.julia_cmd()) --project=$(Base.active_project()) -e $script`) end From f4abf2578059e84581c1a972b132278129b39041 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 25 Nov 2026 15:07:32 +0100 Subject: [PATCH 2/2] Tolerate streams finalized after the precompilation workload The workload cleanup forgets the report slabs, but a stream it created can still be finalized afterwards, typically at exit, where returning its slot failed with a KeyError. Such a slot belongs to a forgotten slab, so drop it. Also clear the slabs under the pool lock, which such a finalizer takes. --- src/compiler/exceptions.jl | 8 +++++++- src/compiler/precompile.jl | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/compiler/exceptions.jl b/src/compiler/exceptions.jl index 67f97f87..924b19c7 100644 --- a/src/compiler/exceptions.jl +++ b/src/compiler/exceptions.jl @@ -131,7 +131,13 @@ function allocate_report_slot(ctx::ZeContext, dev::ZeDevice, list) end release_report_slot(ctx::ZeContext, dev::ZeDevice, slot::ReportSlot) = - with_pool_lock(() -> push!(free_report_slots[(ctx, dev)], slot)) + with_pool_lock() do + # the slabs are forgotten when the precompilation workload resets its state, and + # streams it created can be finalized later + free = get(free_report_slots, (ctx, dev), nothing) + free === nothing || push!(free, slot) + return + end # Called when a stream is collected. Its kernels may still be executing, and writing the # report, in which case the slot is leaked, as is the command list (see its finalizer). diff --git a/src/compiler/precompile.jl b/src/compiler/precompile.jl index cc70ed06..e2842455 100644 --- a/src/compiler/precompile.jl +++ b/src/compiler/precompile.jl @@ -101,7 +101,7 @@ end empty!(stagings) empty!(staged_devices) empty!(eltype_support_cache) - empty!(free_report_slots) + with_pool_lock(() -> empty!(free_report_slots)) empty!(_compiler_configs) empty!(oneL0.extension_properties_cache) Base.@atomic oneL0.deferred_frees.context = WeakRef()