Skip to content

julia_gc: include julia.h globally for better CHANGED_BAG - #6490

Open
fingolfin wants to merge 1 commit into
masterfrom
mh/julia-gc-global-header
Open

julia_gc: include julia.h globally for better CHANGED_BAG#6490
fingolfin wants to merge 1 commit into
masterfrom
mh/julia-gc-global-header

Conversation

@fingolfin

@fingolfin fingolfin commented Aug 5, 2026

Copy link
Copy Markdown
Member

CHANGED_BAG is supposed to have minimal overhead, which normally is achieved by inlining it. But we never had this for the Julia GC, mainly because it was painful to do so in our C++ source files. This PR addresses this.

As a side effect, this enables future work I have planned that will need the Julia headers globally.

Note that CHANGED_BAG cannot simply call jl_gc_wb_back: Julia declares that as static inline, and C forbids referencing an identifier with internal linkage from an inline function with external linkage. Marking CHANGED_BAG as static inline instead is not an option either, as it is used by other EXPORT_INLINE functions (such as PushPlist), which then run into the very same problem. So we inline a copy of Julia's write barrier, which only refers to jl_gc_queue_root.

Since src/gasman.h now includes julia.h, all GAP kernel extensions need the Julia headers, too, so we pass those on to packages via sysinfo.gap. The JULIA=yes CI job now also runs testmockpkg to catch regressions here.

This also means that macros defined by julia.h can now clash with code in kernel extensions. One such clash affects GAP.jl's JuliaInterface, which declares jl_n_threads itself; the CI with GAP.jl jobs will therefore keep failing until oscar-system/GAP.jl#1412 has been merged.

@fingolfin
fingolfin requested a review from lgoettgens August 5, 2026 16:29
@fingolfin fingolfin added kind: enhancement Label for issues suggesting enhancements; and for pull requests implementing enhancements topic: build system topic: kernel release notes: not needed PRs introducing changes that are wholly irrelevant to the release notes topic: julia Julia GC integration and related matters labels Aug 5, 2026
@codecov

codecov Bot commented Aug 5, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.33333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 78.98%. Comparing base (fa26483) to head (ed1db4a).

Files with missing lines Patch % Lines
src/gasman.h 75.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6490      +/-   ##
==========================================
- Coverage   79.01%   78.98%   -0.03%     
==========================================
  Files         683      683              
  Lines      294105   294106       +1     
  Branches     8656     8655       -1     
==========================================
- Hits       232379   232306      -73     
- Misses      59919    59992      +73     
- Partials     1807     1808       +1     

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

@fingolfin
fingolfin marked this pull request as draft August 5, 2026 21:23
@fingolfin

Copy link
Copy Markdown
Member Author

This may need some more refining...

lgoettgens pushed a commit to oscar-system/GAP.jl that referenced this pull request Aug 10, 2026
`THREADSAFE_GAP_JL` is never defined, so `BEGIN_GAP_SYNC` and
`END_GAP_SYNC` always expanded to no-ops, and `BeginGapSync` and
`EndGapSync` were compiled into the kernel extension without ever being
declared or called. The one function that did run, `InitGapSync`, merely
initialized a mutex nobody locks and set a flag nobody reads.

It also declared `extern int jl_n_threads` itself, which since Julia 1.9
conflicts with the `_Atomic(int)` declaration in julia.h. That went
unnoticed as long as julia.h was not in scope in this file, but it breaks
the build once GAP's src/gasman.h includes julia.h, see
gap-system/gap#6490.

AI disclosure: Claude Code (Opus 5) diagnosed the issue, drafted the
change, and verified it locally.

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
@lgoettgens

Copy link
Copy Markdown
Member

oscar-system/GAP.jl#1412 is now available in a GAP.jl release

@fingolfin
fingolfin force-pushed the mh/julia-gc-global-header branch from 674a54f to 7ff0308 Compare August 15, 2026 19:36
@fingolfin
fingolfin force-pushed the mh/julia-gc-global-header branch 2 times, most recently from 5629d9c to d24abca Compare August 28, 2026 07:24
@fingolfin
fingolfin marked this pull request as ready for review August 28, 2026 11:12
@fingolfin
fingolfin force-pushed the mh/julia-gc-global-header branch from d24abca to 407641f Compare August 28, 2026 11:16
@fingolfin

Copy link
Copy Markdown
Member Author

This should be ready now

Comment thread src/intfuncs.c Outdated
Comment on lines +188 to +189
// Julia's headers also define FORCE_INLINE, so get rid of that first
#undef FORCE_INLINE

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems quite a random place to do this. Wouldn't it be more sensible to put it right after including a julia header?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a single file in GAP which uses FORCE_INLINE, namely this one so it's not that random... That said, we then should just get rid for FORCE_INLINE here. Which I just did

Comment thread src/intfuncs.c Outdated
Comment on lines 180 to 182

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated observation: these three lines already appear in lines 171-173 right above

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

@fingolfin
fingolfin force-pushed the mh/julia-gc-global-header branch from 407641f to cf51c97 Compare August 31, 2026 21:45
CHANGED_BAG is supposed to have minimal overhead, which normally is
achieved by inlining it. But we never had this for the Julia GC,
mainly because it was painful to do so in our C++ source files. This
PR addresses this.

Unfortunately, simply inlining CHANGED_BAG by calling jl_gc_wb_back
does not work: Julia declares that function static inline, and C
forbids referencing an identifier with internal linkage from an
inline function with external linkage. Making CHANGED_BAG itself
static inline is not an option either, as it is used by other
EXPORT_INLINE functions, which then run into the very same problem;
and turning all of those into static inline would drop symbols from
libgap which GAP.jl relies on. So instead inline a copy of Julia's
write barrier, which only refers to jl_gc_queue_root.

Since src/gasman.h now includes julia.h, packages need the Julia
headers as well, so pass them on via sysinfo.gap. The flags reported
by julia-config.jl are quoted, which does not survive the way
sysinfo.gap is consumed, hence strip the quotes; and resolve the
clash between Julia's FORCE_INLINE and the one used by our copy of
MurmurHash3.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@fingolfin
fingolfin force-pushed the mh/julia-gc-global-header branch from cf51c97 to ed1db4a Compare August 31, 2026 21:46
@fingolfin
fingolfin enabled auto-merge (squash) August 31, 2026 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind: enhancement Label for issues suggesting enhancements; and for pull requests implementing enhancements release notes: not needed PRs introducing changes that are wholly irrelevant to the release notes topic: build system topic: julia Julia GC integration and related matters topic: kernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants