From 965b7cbe790ba8ba229cc5565feb36bd1819d5de Mon Sep 17 00:00:00 2001 From: Andy Leiserson Date: Sat, 24 Jan 2026 14:31:28 -0800 Subject: [PATCH] feat(deno): Associate external memory with GPUDevice Enable some tests that previously OOMed in CI. --- cts_runner/test.lst | 14 ++++++-------- deno_webgpu/adapter.rs | 28 +++++++++++++++++++++++----- deno_webgpu/device.rs | 9 +++++++++ 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/cts_runner/test.lst b/cts_runner/test.lst index 6ffe490c5..3ad585b08 100644 --- a/cts_runner/test.lst +++ b/cts_runner/test.lst @@ -17,7 +17,6 @@ fails-if(vulkan) webgpu:api,operation,command_buffer,copyTextureToTexture:copy_d fails-if(vulkan) webgpu:api,operation,command_buffer,copyTextureToTexture:copy_depth_stencil:format="depth32float" fails-if(vulkan) webgpu:api,operation,command_buffer,copyTextureToTexture:copy_depth_stencil:format="depth32float-stencil8" webgpu:api,operation,command_buffer,copyTextureToTexture:copy_depth_stencil:format="stencil8" -// Fails with OOM in CI. fails-if(dx12) webgpu:api,operation,command_buffer,image_copy:offsets_and_sizes:* webgpu:api,operation,command_buffer,image_copy:undefined_params:initMethod="CopyB2T";checkMethod="FullCopyT2B";dimension="1d" webgpu:api,operation,command_buffer,image_copy:undefined_params:initMethod="CopyB2T";checkMethod="FullCopyT2B";dimension="2d" @@ -54,12 +53,12 @@ webgpu:api,operation,vertex_state,correctness:setVertexBuffer_offset_and_attribu webgpu:api,validation,buffer,create:* webgpu:api,validation,buffer,destroy:* -// Limit tests fail with OOM in CI. -fails-if(dx12) webgpu:api,validation,capability_checks,limits,maxBindGroups:setBindGroup,* +webgpu:api,validation,capability_checks,limits,maxBindGroups:setBindGroup,* //FAIL: other `maxInterStageShaderVariables` cases w/ limitTest ∈ [underDefault, overMaximum] -fails-if(dx12) webgpu:api,validation,capability_checks,limits,maxInterStageShaderVariables:createRenderPipeline,at_over:limitTest="atDefault";* -fails-if(dx12) webgpu:api,validation,capability_checks,limits,maxInterStageShaderVariables:createRenderPipeline,at_over:limitTest="atMaximum";* -fails-if(dx12) webgpu:api,validation,capability_checks,limits,maxInterStageShaderVariables:createRenderPipeline,at_over:limitTest="betweenDefaultAndMaximum";* +// https://github.com/gfx-rs/wgpu/issues/8945 +webgpu:api,validation,capability_checks,limits,maxInterStageShaderVariables:createRenderPipeline,at_over:limitTest="atDefault";* +webgpu:api,validation,capability_checks,limits,maxInterStageShaderVariables:createRenderPipeline,at_over:limitTest="atMaximum";* +webgpu:api,validation,capability_checks,limits,maxInterStageShaderVariables:createRenderPipeline,at_over:limitTest="betweenDefaultAndMaximum";* webgpu:api,validation,createBindGroup:binding_must_contain_resource_defined_in_layout:* webgpu:api,validation,createBindGroup:buffer_offset_and_size_for_bind_groups_match:* webgpu:api,validation,createBindGroup:buffer,effective_buffer_binding_size:* @@ -150,8 +149,7 @@ fails-if(dx12) webgpu:api,validation,image_copy,buffer_texture_copies:offset_and webgpu:api,validation,image_copy,buffer_texture_copies:sample_count:* webgpu:api,validation,image_copy,buffer_texture_copies:texture_buffer_usages:* webgpu:api,validation,image_copy,layout_related:copy_end_overflows_u64:* -// Fails with OOM in CI. -fails-if(dx12) webgpu:api,validation,image_copy,layout_related:offset_alignment:* +webgpu:api,validation,image_copy,layout_related:offset_alignment:* webgpu:api,validation,image_copy,texture_related:* webgpu:api,validation,queue,buffer_mapped:copyBufferToBuffer:* webgpu:api,validation,queue,buffer_mapped:copyBufferToTexture:* diff --git a/deno_webgpu/adapter.rs b/deno_webgpu/adapter.rs index 31dafe1cf..7a98f2c6b 100644 --- a/deno_webgpu/adapter.rs +++ b/deno_webgpu/adapter.rs @@ -13,6 +13,7 @@ use deno_core::V8TaskSpawner; use deno_core::WebIDL; use super::device::GPUDevice; +use super::device::DEVICE_EXTERNAL_MEMORY_SIZE; use super::queue::GPUQueue; use crate::error::GPUGenericError; use crate::webidl::features_to_feature_names; @@ -161,6 +162,11 @@ impl GPUAdapter { None, )?; + // Associate external memory with the device to encourage V8 to garbage + // collect devices promptly. + scope + .adjust_amount_of_external_allocated_memory(DEVICE_EXTERNAL_MEMORY_SIZE); + let spawner = state.borrow::().clone(); let lost_resolver = v8::PromiseResolver::new(scope).unwrap(); let lost_promise = lost_resolver.get_promise(scope); @@ -195,6 +201,7 @@ impl GPUAdapter { lost_promise: v8::Global::new(scope, lost_promise), limits: SameObject::new(), features: SameObject::new(), + weak: std::sync::OnceLock::new(), }; let device = deno_core::cppgc::make_cppgc_object(scope, device); let weak_device = v8::Weak::new(scope, device); @@ -207,13 +214,24 @@ impl GPUAdapter { let null = v8::null(scope); set_event_target_data.call(scope, null.into(), &[device.into()]); + let finalizer = v8::Weak::with_finalizer( + scope, + device, + Box::new(move |isolate| { + isolate.adjust_amount_of_external_allocated_memory( + -DEVICE_EXTERNAL_MEMORY_SIZE, + ); + }), + ); + // Now that the device is fully constructed, give the error handler a - // weak reference to it. + // weak reference to it, and store the finalizer weak reference. let device = device.cast::(); - deno_core::cppgc::try_unwrap_cppgc_object::(scope, device) - .unwrap() - .error_handler - .set_device(weak_device); + let device_ref = + deno_core::cppgc::try_unwrap_cppgc_object::(scope, device) + .unwrap(); + device_ref.error_handler.set_device(weak_device); + device_ref.weak.set(finalizer).unwrap(); Ok(v8::Global::new(scope, device)) } diff --git a/deno_webgpu/device.rs b/deno_webgpu/device.rs index 55932b0b6..d3f7811db 100644 --- a/deno_webgpu/device.rs +++ b/deno_webgpu/device.rs @@ -37,6 +37,12 @@ use crate::shader::GPUCompilationInfo; use crate::webidl::features_to_feature_names; use crate::Instance; +/// External memory associated with device and queue, to encourage V8 to garbage +/// collect devices promptly. This seems to be particularly important when +/// running CTS tests under `webgpu:api,validation,capability_checks,limits,*` +/// on DX12 in wgpu CI, where any smaller power of two results in OOM errors. +pub(crate) const DEVICE_EXTERNAL_MEMORY_SIZE: i64 = 1 << 24; // 16 MB + pub struct GPUDevice { pub instance: Instance, pub id: wgpu_core::id::DeviceId, @@ -52,6 +58,9 @@ pub struct GPUDevice { pub error_handler: super::error::ErrorHandler, pub lost_promise: v8::Global, + + // Weak reference to the JS object so we can attach a finalizer. + pub(crate) weak: std::sync::OnceLock>, } impl Drop for GPUDevice {