Skip to content

[gpu] Add acquire/release context accessors to all GPU backends - #9431

Open
derek-gerstmann wants to merge 1 commit into
mainfrom
dg/add_gpu_context_accessors
Open

[gpu] Add acquire/release context accessors to all GPU backends#9431
derek-gerstmann wants to merge 1 commit into
mainfrom
dg/add_gpu_context_accessors

Conversation

@derek-gerstmann

Copy link
Copy Markdown
Contributor

Summary

The per-backend halide_*_acquire_context() / halide_*_release_context() functions can't be overridden on Windows: the runtime-library symbols are strong and are only referenced internally to the runtime libs, so the weak-symbol replacement that works on Linux/macOS doesn't apply there. The CUDA backend already provides runtime accessors (halide_set_cuda_acquire_context() / halide_set_cuda_release_context()) that swap a function pointer instead, which works on all platforms.

This PR brings the OpenCL, Metal, D3D12Compute, Vulkan, and WebGPU backends in line with CUDA.

Changes

  • Runtime: each backend's acquire/release context function is split into a halide_default_* implementation plus a dispatcher that calls through a settable pointer, with new halide_set_*_acquire_context / halide_set_*_release_context accessors and matching typedefs in the public runtime headers.
  • JIT: new JITHandlers fields, merge_handlers entries, per-context handler shims, and make_module() hookup, mirroring the CUDA path.
  • runtime_api.cpp: register the new accessors — and the CUDA ones, which had been missing — in halide_runtime_api_functions[].
  • Tests: test/correctness/custom_{opencl,metal,d3d12compute,vulkan,webgpu}_context.cpp, modeled on custom_cuda_context.cpp.

Testing

Full build passes. The Metal test passes end-to-end on an Apple GPU (HL_JIT_TARGET=host-metal); the other backends' tests [SKIP] unless their target is enabled.

🤖 Generated with Claude Code

The acquire_context()/release_context() functions in each GPU runtime
cannot be overridden on Windows: the runtime-library symbols are strong
and are only referenced internally within the runtime libs, so the
weak-symbol replacement used on other platforms does not work there. The
CUDA backend already worked around this with
halide_set_cuda_acquire_context()/halide_set_cuda_release_context()
accessors that swap a function pointer at runtime.

Apply the same pattern to the OpenCL, Metal, D3D12Compute, Vulkan, and
WebGPU backends so they are consistent with CUDA:

- Split each backend's acquire/release context function into a
  halide_default_* implementation plus a dispatcher that calls through a
  settable function pointer, and add halide_set_*_acquire_context /
  halide_set_*_release_context accessors with matching typedefs in the
  public runtime headers.
- Wire the handlers into the JIT: add JITHandlers fields, merge_handlers
  entries, per-context handler shims, and make_module() hookup, mirroring
  the CUDA handling.
- Register the new setters (and the previously-missing CUDA ones) in
  halide_runtime_api_functions[].
- Add per-backend correctness tests modeled on custom_cuda_context.cpp.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
@derek-gerstmann derek-gerstmann changed the title Add acquire/release context accessors to all GPU backends [gpu] Add acquire/release context accessors to all GPU backends Sep 5, 2026
@codecov

codecov Bot commented Sep 5, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 18.65672% with 109 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.11%. Comparing base (a3690b3) to head (7fdd70d).

Files with missing lines Patch % Lines
src/JITModule.cpp 18.65% 93 Missing and 16 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9431      +/-   ##
==========================================
- Coverage   70.12%   70.11%   -0.02%     
==========================================
  Files         261      261              
  Lines       79405    79539     +134     
  Branches    19362    19397      +35     
==========================================
+ Hits        55684    55765      +81     
- Misses      17896    17958      +62     
+ Partials     5825     5816       -9     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

2 participants