Skip to content

Commit a60197f

Browse files
vtjnashKristofferC
authored andcommitted
precompile: don't waste memory on useless inferred code (#56749)
We never have a reason to reference this data again since we already have native code generated for it, so it is simply wasting memory and download space. $ du -sh {old,new}/usr/share/julia/compiled 256M old 227M new (cherry picked from commit dfe6a13)
1 parent 45d3238 commit a60197f

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

base/compiler/effects.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ is_inaccessiblemem_or_argmemonly(effects::Effects) = effects.inaccessiblememonly
329329

330330
is_consistent_overlay(effects::Effects) = effects.nonoverlayed === CONSISTENT_OVERLAY
331331

332+
# (sync this with codegen.cpp and staticdata.c effects_foldable functions)
332333
function encode_effects(e::Effects)
333334
return ((e.consistent % UInt32) << 0) |
334335
((e.effect_free % UInt32) << 3) |

src/codegen.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9669,17 +9669,22 @@ jl_llvm_functions_t jl_emit_codeinst(
96699669
jl_gc_wb(codeinst, src);
96709670
}
96719671
}
9672-
// delete non-inlineable code, since it won't be needed again
9673-
// because we already emitted LLVM code from it and the native
9674-
// Julia-level optimization will never need to see it
9675-
else if (jl_is_method(def) && // don't delete toplevel code
9676-
inferred != jl_nothing && // and there is something to delete (test this before calling jl_ir_inlining_cost)
9677-
!effects_foldable(codeinst->ipo_purity_bits) && // don't delete code we may want for irinterp
9678-
((jl_ir_inlining_cost(inferred) == UINT16_MAX) || // don't delete inlineable code
9679-
jl_atomic_load_relaxed(&codeinst->invoke) == jl_fptr_const_return_addr) && // unless it is constant
9680-
!(params.imaging_mode || jl_options.incremental)) { // don't delete code when generating a precompile file
9681-
jl_atomic_store_release(&codeinst->inferred, jl_nothing);
9682-
}
9672+
}
9673+
// delete non-inlineable code, since it won't be needed again
9674+
// because we already emitted LLVM code from it and the native
9675+
// Julia-level optimization will never need to see it
9676+
else if (jl_is_method(def) && // don't delete toplevel code
9677+
def->source != NULL && // don't delete code from optimized opaque closures that can't be reconstructed
9678+
inferred != jl_nothing && // and there is something to delete (test this before calling jl_ir_inlining_cost)
9679+
((!effects_foldable(jl_atomic_load_relaxed(&codeinst->ipo_purity_bits)) && // don't delete code we may want for irinterp
9680+
(jl_ir_inlining_cost(inferred) == UINT16_MAX) && // don't delete inlineable code
9681+
!jl_generating_output()) || // don't delete code when generating a precompile file, trading memory in the short term for avoiding likely duplicating inference work for aotcompile
9682+
jl_atomic_load_relaxed(&codeinst->invoke) == jl_fptr_const_return_addr)) { // unless it is constant (although this shouldn't have had code in the first place)
9683+
// Never end up in a situation where the codeinst has no invoke, but also no source, so we never fall
9684+
// through the cracks of SOURCE_MODE_ABI.
9685+
jl_callptr_t expected = NULL;
9686+
jl_atomic_cmpswap_relaxed(&codeinst->invoke, &expected, jl_fptr_wait_for_compiled_addr);
9687+
jl_atomic_store_release(&codeinst->inferred, jl_nothing);
96839688
}
96849689
}
96859690
}

src/staticdata.c

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,16 @@ static uintptr_t jl_fptr_id(void *fptr)
725725
return *(uintptr_t*)pbp;
726726
}
727727

728+
static int effects_foldable(uint32_t effects)
729+
{
730+
// N.B.: This needs to be kept in sync with Core.Compiler.is_foldable(effects, true)
731+
return ((effects & 0x7) == 0) && // is_consistent(effects)
732+
(((effects >> 10) & 0x03) == 0) && // is_noub(effects)
733+
(((effects >> 3) & 0x03) == 0) && // is_effect_free(effects)
734+
((effects >> 6) & 0x01); // is_terminates(effects)
735+
}
736+
737+
728738
// `jl_queue_for_serialization` adds items to `serialization_order`
729739
#define jl_queue_for_serialization(s, v) jl_queue_for_serialization_((s), (jl_value_t*)(v), 1, 0)
730740
static void jl_queue_for_serialization_(jl_serializer_state *s, jl_value_t *v, int recursive, int immediate) JL_GC_DISABLED;
@@ -838,8 +848,24 @@ static void jl_insert_into_serialization_queue(jl_serializer_state *s, jl_value_
838848
// TODO: if (ci in ci->defs->cache)
839849
record_field_change((jl_value_t**)&ci->next, NULL);
840850
}
841-
if (jl_atomic_load_relaxed(&ci->inferred) && !is_relocatable_ci(&relocatable_ext_cis, ci))
842-
record_field_change((jl_value_t**)&ci->inferred, jl_nothing);
851+
jl_value_t *inferred = jl_atomic_load_relaxed(&ci->inferred);
852+
if (inferred && inferred != jl_nothing) { // disregard if there is nothing here to delete (e.g. builtins, unspecialized)
853+
if (!is_relocatable_ci(&relocatable_ext_cis, ci))
854+
record_field_change((jl_value_t**)&ci->inferred, jl_nothing);
855+
else if (jl_is_method(ci->def->def.method) && // don't delete toplevel code
856+
ci->def->def.method->source) { // don't delete code from optimized opaque closures that can't be reconstructed (and builtins)
857+
if (jl_atomic_load_relaxed(&ci->max_world) != ~(size_t)0 || // delete all code that cannot run
858+
jl_atomic_load_relaxed(&ci->invoke) == jl_fptr_const_return) { // delete all code that just returns a constant
859+
record_field_change((jl_value_t**)&ci->inferred, jl_nothing);
860+
}
861+
else if (native_functions && // don't delete any code if making a ji file
862+
!effects_foldable(jl_atomic_load_relaxed(&ci->ipo_purity_bits)) && // don't delete code we may want for irinterp
863+
jl_ir_inlining_cost(inferred) == UINT16_MAX) { // don't delete inlineable code
864+
// delete the code now: if we thought it was worth keeping, it would have been converted to object code
865+
record_field_change((jl_value_t**)&ci->inferred, jl_nothing);
866+
}
867+
}
868+
}
843869
}
844870

845871
if (immediate) // must be things that can be recursively handled, and valid as type parameters

0 commit comments

Comments
 (0)