Skip to content

Materialize isbits boxed constants as device globals#874

Open
maleadt wants to merge 4 commits into
mainfrom
tb/materialize-boxed-constants
Open

Materialize isbits boxed constants as device globals#874
maleadt wants to merge 4 commits into
mainfrom
tb/materialize-boxed-constants

Conversation

@maleadt

@maleadt maleadt commented Jul 10, 2026

Copy link
Copy Markdown
Member

Since JuliaLang/julia#55045 (1.14.0-DEV.1348), small isbits unions built from
constants are kept fully boxed: codegen emits a julia.constgv slot holding a
pointer to the host box, which device code then dereferences for the payload
or type tag. relocate_gvs! used to bake that host address into the IR, so any
such kernel reads host memory → ERROR_ILLEGAL_ADDRESS.

Fixing this upstream was rejected (JuliaLang/julia#62191): inline storage is a
host-side de-optimization, and flag-guarding every eager-box site doesn't
scale. Instead, this implements @topolarity's suggestion on the GPUCompiler
side, where the GC-related objections don't apply.

relocate_gvs! now inspects the object behind each constgv slot. Non-ghost,
non-Bool isbits objects are materialized: a private constant replica of
the box (header word + payload bytes, heap-compatible layout and alignment) is
emitted into the module and the slot points at its payload. Everything else —
types, symbols, singletons, Bools, mutable objects — keeps the baked address;
those are only ever used as identity tokens.

Notes:

  • The replica's header holds the same value codegen compares against in
    typeof/isa (smalltag immediate or type pointer), and === on isbits
    falls back to bits comparison, so no identity-sensitive behavior changes.
  • Resolution runs before optimize!, so payload and tag loads constant-fold;
    the device code ends up identical to what inline union storage produced
    before #55045. No de-optimization, host codegen untouched.
  • Every eager-boxing site funnels through literal_pointer_val_slot, so
    future upstream boxing patterns are covered automatically.
  • Session-dependence marking is now precise: relocate_gvs! reports whether
    any host address survived (baked slot, or a materialized header carrying a
    non-smalltag type pointer). Kernels whose constants are all smalltag
    (Int/UInt/Char etc.) no longer get wiped from package images.

Applies to 1.13+ (older versions bake addresses in Julia itself); the union
regression only exists on 1.14.0-DEV.1348+.

maleadt and others added 3 commits July 10, 2026 23:26
Since JuliaLang/julia#55045 (1.14.0-DEV.1348), small isbits unions built from
constants stay fully boxed, so `julia.constgv` slots may now be dereferenced
on device. Instead of baking the host address, emit a constant replica of the
box (header word + payload bytes) and point the slot at its payload; LLVM
folds the loads. Ghosts, Bools and non-isbits objects keep their identity via
baked addresses. `relocate_gvs!` now reports whether the module remained
session-portable.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Materialized constgv slots (with smalltag headers) contain no session data,
so smalltag-only kernels can stay in package images.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@codecov

codecov Bot commented Jul 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.55556% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.48%. Comparing base (1baffbb) to head (05e0d3d).

Files with missing lines Patch % Lines
src/jlgen.jl 95.12% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #874      +/-   ##
==========================================
+ Coverage   81.37%   81.48%   +0.10%     
==========================================
  Files          25       25              
  Lines        4769     4806      +37     
==========================================
+ Hits         3881     3916      +35     
- Misses        888      890       +2     

☔ 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.

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