← Back to Pull requests

RFC: codegen: root the result of `unsafe_pointer_to_objref` #65207

Open
Keno opened this pull request with 179 lines diff across 12 files
View on GitHub
Keno Keno commented on Nov 24, 2026 03:01 UTC
Author

unsafe_pointer_to_objref was lowered to an address space cast of the pointer. GC root placement treats such a cast as the definition of a tracked value, but it only sees the cast where the optimizer left it, and a cast has no effects that would keep it in place. InstCombine, for instance, turns a select of two casts into a cast of the select. When that moves the cast past a safepoint, the object is not rooted there, although the reference was created before it. An object that is only kept alive until unsafe_pointer_to_objref returns, for example by a GC.@preserve around the call, could thus be freed while the returned reference was still in use.

Lower it to a call of a new julia.pointer_to_objref intrinsic instead, which GC root placement turns into the cast once the roots are placed. The intrinsic is not speculatable and is declared to modify memory that only the runtime can access, so that the optimizer leaves the call where it is relative to safepoints and control flow.

This came up in the review of #65198.

Assisted-by: Claude Code (Claude Fable 5.2)

Diff: 179 lines (+166 / -13) across 12 files Reviewer: vtjnash Review duration: 16748m
All Vouch review confidence checks passed
Residual risk 0.11 = Change Risk (0.50) × (1 − Review Confidence (0.77)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.50 · 179 lines diff across 12 files
0.50
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.71 · 2 substantive comment(s)
0.71
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
77%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.311
Real PR Conversation Timeline
4 items
vtjnash vtjnash commented on code on Nov 24, 2026 04:19 UTC
Code Review
doc/src/devdocs/llvm.md:1
@@ -386,6 +388,30 @@ value is still tracked by the system. I.e. it is not valid to attempt to use the
 result of this operation to establish a global root - the optimizer may have
 already dropped the value.
 
+### Supporting [`unsafe_pointer_to_objref`](@ref)
+
+[`unsafe_pointer_to_objref`](@ref) goes the other way. It turns an untracked pointer
+into a reference to the object it points at, and that reference has to keep the
+object alive like any other one, even after whatever kept it alive until then is gone.
+An address space cast from 0 to Tracked does not achieve this. The GC root placement
+pass does treat such a cast as the definition of a new tracked value, but a cast has

Does it? I was pretty sure GC placement treats that as the definition of an unnecessary object to track (for our standard idiom for constants that don't need tracking)

vtjnash vtjnash approved these changes on Nov 24, 2026 04:23 UTC
Approved Reviewer

Approved these changes.

vchuravy vchuravy commented on code on Nov 24, 2026 09:25 UTC
Code Review
doc/src/devdocs/llvm.md:1
@@ -386,6 +388,34 @@ value is still tracked by the system. I.e. it is not valid to attempt to use the
 result of this operation to establish a global root - the optimizer may have
 already dropped the value.
 
+### Supporting [`unsafe_pointer_to_objref`](@ref)
+
+[`unsafe_pointer_to_objref`](@ref) goes the other way. It turns an untracked pointer
+into a reference to the object it points at, and that reference has to keep the
+object alive like any other one, even after whatever kept it alive until then is gone.
+An address space cast from 0 to Tracked does not achieve this. Such a cast is the idiom
+for a value that needs no root, like a constant that is loaded from a global, and the
+GC root placement pass handles it accordingly. It does root the result of a cast of
+another pointer where that is live across a safepoint, but a cast does not count when
+the pass checks whether the function has a safepoint after its first definition, and
+without one it places no roots at all. A cast also has no effects, so the optimizer is
+free to move it: past a safepoint at which nothing else roots the object anymore, or
+above the branch that decides whether the pointer refers to an object at all. We thus
+provide a second intrinsic:
+```llvm
+declare ptr addrspace(10) @julia.pointer_to_objref(ptr)
+```
+A call to it is the definition of a tracked value, which the GC root placement pass
+roots at every later safepoint where it is live. The call is not a safepoint itself,
+and is lowered to the corresponding address space cast after GC root lowering. To
+keep it in place until then, the intrinsic is not speculatable and is declared to
+modify memory that only the runtime can access, so the optimizer may neither
+execute it speculatively nor move it across another call. The caller remains
+responsible for the object being alive when the call is made. A pointer that
+`julia.pointer_from_objref` returned is no proof of that: as described above, the
+optimizer may already have removed the object it was taken from, or moved it to the
+stack.
+

The docs are very Claudish

Reviewers
Assignees
Keno Keno
Labels
risk: low confidence: 77.3% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 50.2% (MEDIUM) · Higher = larger blast radius
50.2%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 77.3% · Lower = rushed or shallow review
77.3%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 11.4% · Safe to merge
11.4%