← Back to Pull requests

codegen: Emit less dead IR for array accesses #65014

Closed
unknown closed this pull request without merging on Nov 17, 2026 13:05 UTC
View on GitHub
unknown unknown commented on Nov 16, 2026 13:22 UTC
Author

LLVM gets rid of this dead IR quite cheaply so the point here is not really one of performance but more of "cleanliness" when reading the LLVM IR. I am not sure what the "policy" on this is w.r.t getting rid of instructions at this stage vs letting LLVM handle it.

using InteractiveUtils

get_unchecked(a, i) = @inbounds a[i]
ref_get(r) = Core.memoryrefget(r, :not_atomic, false)

code_llvm(get_unchecked, Tuple{Vector{Float64}, Int}; optimize=false, debuginfo=:none)
code_llvm(ref_get, Tuple{MemoryRef{Float64}}; optimize=false, debuginfo=:none)

code_warntype(get_unchecked, Tuple{Vector{Float64}, Int})
code_warntype(ref_get, Tuple{MemoryRef{Float64}})

Unchecked array read: 36 → 18 instructions

--- before
+++ after
@@ -1,32 +1,10 @@
 ; Function Signature: get_unchecked(Array{Float64, 1}, Int64)
 define double @julia_get_unchecked_ID(ptr noundef nonnull align 8 dereferenceable(24) %"a::Array", i64 signext %"i::Int64") #0 {
 top:
-  %"a::Array.size" = alloca i64, align 8
-  call void @llvm.lifetime.start.p0(ptr %"a::Array.size")
   %pgcstack = call ptr @julia.get_pgcstack()
-  br i1 true, label %L14, label %L3
-
-L3:                                               ; preds = %top
-  %bounds_index = sub i64 %"i::Int64", 1
-  %"a::Array.size_ptr" = getelementptr inbounds i8, ptr %"a::Array", i32 16
-  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %"a::Array.size", ptr align 8 %"a::Array.size_ptr", i64 8, i1 false)
-  %bitcast = load i64, ptr %"a::Array.size", align 8
-  %in_range = icmp ult i64 %bounds_index, %bitcast
-  %out_of_range = xor i1 %in_range, true
-  br i1 %out_of_range, label %L11, label %L10
-
-L10:                                              ; preds = %L3
-  br label %L13
-
-L11:                                              ; preds = %L3
-  call void @j__throw_boundserror_indices_ID(ptr %"a::Array", i64 signext %"i::Int64") #7
-  call void @llvm.trap()
-  unreachable
-
-L13:                                              ; preds = %L10
   br label %L14
 
-L14:                                              ; preds = %L13, %top
+L14:                                              ; preds = %top
   %data_ptr = getelementptr inbounds nuw { ptr, ptr }, ptr %"a::Array", i32 0, i32 0
   %memoryref_data = load ptr, ptr %data_ptr, align 8
   %mem_ptr = getelementptr inbounds nuw { ptr, ptr }, ptr %"a::Array", i32 0, i32 1
@@ -38,8 +16,6 @@
   %memoryref_data_byteoffset = getelementptr i8, ptr %memoryref_data, i64 %memoryref_byteoffset
   %indexed_ref0 = insertvalue { ptr, ptr } zeroinitializer, ptr %memoryref_data_byteoffset, 0
   %memory_ref = insertvalue { ptr, ptr } %indexed_ref0, ptr %memoryref_mem, 1
-  %length_ptr = getelementptr inbounds nuw { i64, ptr }, ptr %memoryref_mem, i32 0, i32 0
-  %memory_len = load i64, ptr %length_ptr, align 8
   %loaded_data = call ptr @julia.gc_loaded(ptr %memoryref_mem, ptr %memoryref_data)
   %memoryref_data1 = getelementptr inbounds i8, ptr %loaded_data, i64 %memoryref_byteoffset
   %value = load double, ptr %memoryref_data1, align 8
@@ -47,8 +23,4 @@
 
 L18:                                              ; preds = %L14
   ret double %value
-
-after_noret:                                      ; No predecessors!
-  call void @llvm.trap()
-  unreachable
 }

Unchecked memory-reference read: 14 → 8 instructions

--- before
+++ after
@@ -2,12 +2,6 @@
 define double @julia_ref_get_ID(ptr noundef nonnull readonly align 8 captures(none) dereferenceable(16) %"r::GenericMemoryRef", ptr noundef nonnull readonly align 8 captures(none) dereferenceable(8) %.roots.r) #0 {
 top:
   %pgcstack = call ptr @julia.get_pgcstack()
-  %unused_data = load ptr, ptr %"r::GenericMemoryRef", align 8
-  %unused_mem = load ptr, ptr %.roots.r, align 8
-  %unused_ref0 = insertvalue { ptr, ptr } zeroinitializer, ptr %unused_data, 0
-  %unused_ref = insertvalue { ptr, ptr } %unused_ref0, ptr %unused_mem, 1
-  %length_ptr = getelementptr inbounds nuw { i64, ptr }, ptr %unused_mem, i32 0, i32 0
-  %memory_len = load i64, ptr %length_ptr, align 8
   %data = load ptr, ptr %"r::GenericMemoryRef", align 8
   %mem = load ptr, ptr %.roots.r, align 8
   %ref0 = insertvalue { ptr, ptr } zeroinitializer, ptr %data, 0

Optimized IR is unchanged for both examples.

Diff: 546 lines (+409 / -137) across 6 files Reviewer: KristofferC Review duration: 1423m
All Vouch review confidence checks passed
Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 546 lines diff across 6 files
0.48
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
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.377
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #65014"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% 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: 48% (MEDIUM) · Higher = larger blast radius
48%
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: 60.5% · Lower = rushed or shallow review
60.5%
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: 19.0% · Safe to merge
19.0%