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/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 61b1c628..e2842455 100644 --- a/src/compiler/precompile.jl +++ b/src/compiler/precompile.jl @@ -101,9 +101,10 @@ 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() 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