diff --git a/Compiler/src/typeinfer.jl b/Compiler/src/typeinfer.jl index e3896870d82b8..7ff691c0360fa 100644 --- a/Compiler/src/typeinfer.jl +++ b/Compiler/src/typeinfer.jl @@ -1277,7 +1277,9 @@ function typeinf_ext_toplevel(methods::Vector{Any}, worlds::Vector{UInt}, trim:: # then we want to compile and emit this if item.def.primary_world <= this_world <= item.def.deleted_world ci = typeinf_ext(interp, item, SOURCE_MODE_NOT_REQUIRED) - ci isa CodeInstance && !use_const_api(ci) && push!(tocompile, ci) + if ci isa CodeInstance && (!use_const_api(ci) || trim) + push!(tocompile, ci) + end end elseif item isa SimpleVector push!(codeinfos, item[1]::Type) @@ -1292,7 +1294,7 @@ function typeinf_ext_toplevel(methods::Vector{Any}, worlds::Vector{UInt}, trim:: mi = get_ci_mi(callee) def = mi.def if use_const_api(callee) - src = codeinfo_for_const(interp, mi, code.rettype_const) + src = codeinfo_for_const(interp, mi, callee.rettype_const) elseif haskey(interp.codegen, callee) src = interp.codegen[callee] elseif isa(def, Method) && ccall(:jl_get_module_infer, Cint, (Any,), def.module) == 0 && !trim diff --git a/src/gf.c b/src/gf.c index 710dda208f0b2..c9d477dbf1747 100644 --- a/src/gf.c +++ b/src/gf.c @@ -3378,7 +3378,7 @@ JL_DLLEXPORT int jl_add_entrypoint(jl_tupletype_t *types) return 0; JL_GC_PROMISE_ROOTED(mi); if (jl_generating_output() && jl_options.trim) { - arraylist_push(jl_entrypoint_mis, mi); + arraylist_push(jl_entrypoint_list, mi); } return 1; } diff --git a/src/init.c b/src/init.c index e69467c75bd73..9fb159c973f41 100644 --- a/src/init.c +++ b/src/init.c @@ -44,7 +44,7 @@ extern BOOL (WINAPI *hSymRefreshModuleList)(HANDLE); // list of modules being deserialized with __init__ methods jl_array_t *jl_module_init_order; -arraylist_t *jl_entrypoint_mis; +arraylist_t *jl_entrypoint_list; JL_DLLEXPORT size_t jl_page_size; @@ -931,8 +931,8 @@ static NOINLINE void _finish_julia_init(JL_IMAGE_SEARCH rel, jl_ptls_t ptls, jl_ } if (jl_options.trim) { - jl_entrypoint_mis = (arraylist_t *)malloc_s(sizeof(arraylist_t)); - arraylist_new(jl_entrypoint_mis, 0); + jl_entrypoint_list = (arraylist_t *)malloc_s(sizeof(arraylist_t)); + arraylist_new(jl_entrypoint_list, 0); } if (jl_options.handle_signals == JL_OPTIONS_HANDLE_SIGNALS_ON) diff --git a/src/julia_internal.h b/src/julia_internal.h index 0da6d412c8a49..45332b058ee07 100644 --- a/src/julia_internal.h +++ b/src/julia_internal.h @@ -908,7 +908,7 @@ extern htable_t jl_current_modules JL_GLOBALLY_ROOTED; extern JL_DLLEXPORT jl_module_t *jl_precompile_toplevel_module JL_GLOBALLY_ROOTED; extern jl_genericmemory_t *jl_global_roots_list JL_GLOBALLY_ROOTED; extern jl_genericmemory_t *jl_global_roots_keyset JL_GLOBALLY_ROOTED; -extern arraylist_t *jl_entrypoint_mis; +extern arraylist_t *jl_entrypoint_list; JL_DLLEXPORT int jl_is_globally_rooted(jl_value_t *val JL_MAYBE_UNROOTED) JL_NOTSAFEPOINT; JL_DLLEXPORT jl_value_t *jl_as_global_root(jl_value_t *val, int insert) JL_GLOBALLY_ROOTED; extern jl_svec_t *precompile_field_replace JL_GLOBALLY_ROOTED; diff --git a/src/precompile_utils.c b/src/precompile_utils.c index 8906b3eb586d3..44b6e093b7460 100644 --- a/src/precompile_utils.c +++ b/src/precompile_utils.c @@ -370,12 +370,9 @@ static void *jl_precompile_trimmed(size_t world) jl_value_t *ccallable = NULL; JL_GC_PUSH2(&m, &ccallable); jl_method_instance_t *mi; - while (1) { - mi = (jl_method_instance_t*)arraylist_pop(jl_entrypoint_mis); - if (mi == NULL) - break; + for (size_t i = 0; i < jl_entrypoint_list->len ; i++) { + mi = (jl_method_instance_t*)jl_entrypoint_list->items[i]; assert(jl_is_method_instance(mi)); - jl_array_ptr_1d_push(m, (jl_value_t*)mi); ccallable = (jl_value_t *)mi->def.method->ccallable; if (ccallable) diff --git a/src/staticdata.c b/src/staticdata.c index ff352cd8c152f..7e05d734505cd 100644 --- a/src/staticdata.c +++ b/src/staticdata.c @@ -1788,8 +1788,19 @@ static void jl_write_values(jl_serializer_state *s) JL_GC_DISABLED else { newm->nroots_sysimg = m->roots ? jl_array_len(m->roots) : 0; } - if (m->ccallable) - arraylist_push(&s->ccallable_list, (void*)reloc_offset); + if (m->ccallable) { + if (jl_options.trim == JL_TRIM_NO) { + arraylist_push(&s->ccallable_list, (void*)reloc_offset); + } else { + for (size_t i = 0; i < jl_entrypoint_list->len; i++) { + jl_value_t *val = (jl_value_t*)jl_entrypoint_list->items[i]; + if (m == ((jl_method_instance_t*)val)->def.method) { + arraylist_push(&s->ccallable_list, (void*)reloc_offset); + break; + } + } + } + } } else if (jl_is_method_instance(v)) { assert(f == s->s); diff --git a/test/trimming/Makefile b/test/trimming/Makefile index d2da21eb71a88..4a889726204b8 100644 --- a/test/trimming/Makefile +++ b/test/trimming/Makefile @@ -33,7 +33,7 @@ LDFLAGS_ADD = -lm $(shell $(JULIA_CONFIG) --ldflags --ldlibs) -ljulia-internal release: hello$(EXE) hello.o: $(SRCDIR)/hello.jl $(BUILDSCRIPT) - $(JULIA) -t 1 -J $(BIN)/../lib/julia/sys.so --startup-file=no --history-file=no --output-o $@ --output-incremental=no --strip-ir --strip-metadata --experimental --trim $(BUILDSCRIPT) $(SRCDIR)/hello.jl --output-exe true + $(JULIA) -t 1 -J $(BIN)/../lib/julia/sys.so --startup-file=no --history-file=no --output-o $@ --output-incremental=no --strip-ir --strip-metadata --experimental --trim $(BUILDSCRIPT) $(SRCDIR)/hello.jl --output-exe false init.o: $(SRCDIR)/init.c $(CC) -c -o $@ $< $(CPPFLAGS_ADD) $(CPPFLAGS) $(CFLAGS_ADD) $(CFLAGS) diff --git a/test/trimming/hello.jl b/test/trimming/hello.jl index 307bf820f325b..33d60aab343fa 100644 --- a/test/trimming/hello.jl +++ b/test/trimming/hello.jl @@ -1,6 +1,21 @@ module MyApp + +# a ccallable that is used but not declared as an entrypoint. should not have a C name generated. +Base.@ccallable function foo()::Cint + Cint(1) +end + +# a ccallable that is an entrypoint but uses const_api. +Base.@ccallable function foo2()::Cint + Cint(2) +end + +Base.Experimental.entrypoint(foo2, ()) + Base.@ccallable function main()::Cint println(Core.stdout, "Hello, world!") + u = foo() + println(Core.stdout, u) return 0 end end diff --git a/test/trimming/trimming.jl b/test/trimming/trimming.jl index dfacae7f8e531..fb4d5f95326f5 100644 --- a/test/trimming/trimming.jl +++ b/test/trimming/trimming.jl @@ -2,6 +2,6 @@ using Test exe_path = joinpath(@__DIR__, "hello"*splitext(Base.julia_exename())[2]) -@test readchomp(`$exe_path`) == "Hello, world!" +@test readchomp(`$exe_path`) == "Hello, world!\n1" @test filesize(exe_path) < filesize(unsafe_string(Base.JLOptions().image_file))/10