Skip to content

Commit 71ab071

Browse files
authored
begin to encapsulate global state variables (#59400)
Combine all exported (and some of the internal) global variables into a single (or rather a couple) explicit structs. The eventual goal is to support sub-runtime images, where it can be possible to load a trimmed image into the same process, and be able to distinguish the exported state for each without conflict. This also helps remove a lot of code duplication for defining these, since we can use the XX style to copy them in a group, instead of listing them explicitly.
1 parent 13b73ed commit 71ab071

15 files changed

+408
-712
lines changed

base/libdl.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,7 @@ function add_dependency!(ll::LazyLibrary, dep::LazyLibrary)
418418
end
419419

420420
# Register `jl_libdl_dlopen_func` so that `ccall()` lowering knows
421-
# how to call `dlopen()`, during bootstrap.
422-
# See `post_image_load_hooks` for non-bootstrapping.
421+
# how to call `dlopen()`.
423422
Base.unsafe_store!(cglobal(:jl_libdl_dlopen_func, Any), dlopen)
424423

425424
function dlopen(ll::LazyLibrary, flags::Integer = ll.flags; kwargs...)

cli/jl_exports.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
#include "../src/jl_exported_funcs.inc"
88

99
// Define pointer data as `const void * $(name);`
10-
#define XX(name) JL_DLLEXPORT const void * name;
10+
#define XX(name, type) JL_DLLEXPORT const void * jl_##name;
1111
JL_EXPORTED_DATA_POINTERS(XX)
12+
JL_CONST_GLOBAL_VARS(XX)
1213
#undef XX
1314

1415
// Define symbol data as `$(type) $(name);`

src/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
/julia_flisp.boot
1111
/julia_flisp.boot.inc
1212
/flisp.boot.inc
13+
/jl_data_globals_defs.inc
1314
/jl_internal_funcs.inc
1415

1516
/libjulia-debug.a

src/Makefile

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ ifeq ($(USE_SYSTEM_LIBUV),0)
128128
UV_HEADERS += uv.h
129129
UV_HEADERS += uv/*.h
130130
endif
131-
PUBLIC_HEADERS := $(BUILDDIR)/julia_version.h $(wildcard $(SRCDIR)/support/*.h) $(addprefix $(SRCDIR)/,work-stealing-queue.h gc-interface.h gc-tls-common.h julia.h julia_assert.h julia_threads.h julia_fasttls.h julia_locks.h julia_atomics.h jloptions.h)
131+
PUBLIC_HEADERS := $(BUILDDIR)/julia_version.h $(wildcard $(SRCDIR)/support/*.h) $(addprefix $(SRCDIR)/,work-stealing-queue.h gc-interface.h gc-tls-common.h julia.h julia_assert.h julia_threads.h julia_fasttls.h julia_locks.h julia_atomics.h jloptions.h jl_exported_data.inc)
132132
ifeq (${USE_THIRD_PARTY_GC},mmtk)
133133
PUBLIC_HEADERS += $(addprefix $(SRCDIR)/,gc-tls-mmtk.h gc-wb-mmtk.h)
134134
else
@@ -137,7 +137,7 @@ endif
137137
ifeq ($(OS),WINNT)
138138
PUBLIC_HEADERS += $(addprefix $(SRCDIR)/,win32_ucontext.h)
139139
endif
140-
HEADERS := $(PUBLIC_HEADERS) $(addprefix $(SRCDIR)/,julia_internal.h options.h timing.h passes.h) $(addprefix $(BUILDDIR)/,$(DTRACE_HEADERS) jl_internal_funcs.inc)
140+
HEADERS := $(PUBLIC_HEADERS) $(addprefix $(SRCDIR)/,julia_internal.h options.h timing.h passes.h jl_internal_data.inc) $(addprefix $(BUILDDIR)/,$(DTRACE_HEADERS) jl_internal_funcs.inc jl_data_globals_defs.inc)
141141
PUBLIC_HEADERS += $(addprefix $(SRCDIR)/,julia_gcext.h)
142142
PUBLIC_HEADER_TARGETS := $(addprefix $(build_includedir)/julia/,$(notdir $(PUBLIC_HEADERS)) $(UV_HEADERS))
143143

@@ -280,6 +280,13 @@ $(BUILDDIR)/jl_internal_funcs.inc: $(SRCDIR)/jl_exported_funcs.inc
280280
# loaded at the same time.
281281
grep 'XX(..*)' $< | sed -E 's/.*XX\((.+)\).*/#define \1 i\1/g' >$@
282282

283+
$(BUILDDIR)/jl_data_globals_defs.inc: $(SRCDIR)/jl_exported_data.inc
284+
# Generate `.inc` file that contains a list of `#define` macros to access global data through struct instances
285+
{ \
286+
grep 'XX(.*)' $< | sed -E 's/.*XX\(([^,]+), .*\).*/#define jl_\1 (sysimg_global.\1)/g'; \
287+
grep 'YY(.*)' $< | sed -E 's/.*YY\(([^,]+), .*\).*/#define jl_\1 (const_globals.jl\1)/g'; \
288+
} >$@
289+
283290
# source file rules
284291
$(BUILDDIR)/%.o: $(SRCDIR)/%.c $(HEADERS) | $(BUILDDIR)
285292
@$(call PRINT_CC, $(CC) $(LLVM_CFLAGS) $(SHIPFLAGS) $(JCPPFLAGS) $(JCFLAGS) $(DISABLE_ASSERTIONS) -c $< -o $@)
@@ -547,7 +554,7 @@ INCLUDED_CXX_FILES := \
547554
clean:
548555
-rm -fr $(build_shlibdir)/libjulia-internal* $(build_shlibdir)/libjulia-codegen*
549556
-rm -rf $(build_shlibdir)/libccalltest* $(build_shlibdir)/libllvmcalltest* $(build_shlibdir)/libccalllazyfoo* $(build_shlibdir)/libccalllazybar*
550-
-rm -f $(BUILDDIR)/julia_flisp.boot $(BUILDDIR)/julia_flisp.boot.inc $(BUILDDIR)/jl_internal_funcs.inc
557+
-rm -f $(BUILDDIR)/julia_flisp.boot $(BUILDDIR)/julia_flisp.boot.inc $(BUILDDIR)/jl_internal_funcs.inc $(BUILDDIR)/jl_data_globals_defs.inc
551558
-rm -f $(BUILDDIR)/*.dbg.obj $(BUILDDIR)/*.o $(BUILDDIR)/*.dwo $(BUILDDIR)/*.$(SHLIB_EXT) $(BUILDDIR)/*.a $(BUILDDIR)/*.h.gen
552559
-rm -f $(BUILDDIR)/julia.expmap
553560
-rm -f $(BUILDDIR)/julia_version.h
@@ -580,7 +587,7 @@ endif
580587
install-analysis-deps:
581588
$(MAKE) -C $(JULIAHOME)/deps $(addprefix install-,$(ANALYSIS_DEPS))
582589

583-
analyzegc-deps-check: $(BUILDDIR)/julia_version.h $(BUILDDIR)/julia_flisp.boot.inc $(BUILDDIR)/jl_internal_funcs.inc
590+
analyzegc-deps-check: $(addprefix $(BUILDDIR)/,julia_version.h julia_flisp.boot.inc jl_internal_funcs.inc jl_data_globals_defs.inc)
584591
ifeq ($(USE_BINARYBUILDER_LLVM),0)
585592
ifneq ($(BUILD_LLVM_CLANG),1)
586593
$(error Clang must be available to use the clang analyzer. Either build it (BUILD_LLVM_CLANG=1) or use BinaryBuilder)

src/ast.c

Lines changed: 3 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -20,104 +20,9 @@
2020
extern "C" {
2121
#endif
2222

23-
// head symbols for each expression type
24-
JL_DLLEXPORT jl_sym_t *jl_call_sym;
25-
JL_DLLEXPORT jl_sym_t *jl_invoke_sym;
26-
JL_DLLEXPORT jl_sym_t *jl_invoke_modify_sym;
27-
JL_DLLEXPORT jl_sym_t *jl_empty_sym;
28-
JL_DLLEXPORT jl_sym_t *jl_top_sym;
29-
JL_DLLEXPORT jl_sym_t *jl_module_sym;
30-
JL_DLLEXPORT jl_sym_t *jl_slot_sym;
31-
JL_DLLEXPORT jl_sym_t *jl_export_sym;
32-
JL_DLLEXPORT jl_sym_t *jl_public_sym;
33-
JL_DLLEXPORT jl_sym_t *jl_toplevel_sym;
34-
JL_DLLEXPORT jl_sym_t *jl_quote_sym;
35-
JL_DLLEXPORT jl_sym_t *jl_line_sym;
36-
JL_DLLEXPORT jl_sym_t *jl_incomplete_sym;
37-
JL_DLLEXPORT jl_sym_t *jl_goto_sym;
38-
JL_DLLEXPORT jl_sym_t *jl_goto_ifnot_sym;
39-
JL_DLLEXPORT jl_sym_t *jl_return_sym;
40-
JL_DLLEXPORT jl_sym_t *jl_lineinfo_sym;
41-
JL_DLLEXPORT jl_sym_t *jl_lambda_sym;
42-
JL_DLLEXPORT jl_sym_t *jl_assign_sym;
43-
JL_DLLEXPORT jl_sym_t *jl_globalref_sym;
44-
JL_DLLEXPORT jl_sym_t *jl_do_sym;
45-
JL_DLLEXPORT jl_sym_t *jl_method_sym;
46-
JL_DLLEXPORT jl_sym_t *jl_core_sym;
47-
JL_DLLEXPORT jl_sym_t *jl_enter_sym;
48-
JL_DLLEXPORT jl_sym_t *jl_leave_sym;
49-
JL_DLLEXPORT jl_sym_t *jl_pop_exception_sym;
50-
JL_DLLEXPORT jl_sym_t *jl_exc_sym;
51-
JL_DLLEXPORT jl_sym_t *jl_error_sym;
52-
JL_DLLEXPORT jl_sym_t *jl_new_sym;
53-
JL_DLLEXPORT jl_sym_t *jl_splatnew_sym;
54-
JL_DLLEXPORT jl_sym_t *jl_block_sym;
55-
JL_DLLEXPORT jl_sym_t *jl_new_opaque_closure_sym;
56-
JL_DLLEXPORT jl_sym_t *jl_opaque_closure_method_sym;
57-
JL_DLLEXPORT jl_sym_t *jl_const_sym;
58-
JL_DLLEXPORT jl_sym_t *jl_thunk_sym;
59-
JL_DLLEXPORT jl_sym_t *jl_foreigncall_sym;
60-
JL_DLLEXPORT jl_sym_t *jl_as_sym;
61-
JL_DLLEXPORT jl_sym_t *jl_global_sym;
62-
JL_DLLEXPORT jl_sym_t *jl_globaldecl_sym;
63-
JL_DLLEXPORT jl_sym_t *jl_local_sym;
64-
JL_DLLEXPORT jl_sym_t *jl_list_sym;
65-
JL_DLLEXPORT jl_sym_t *jl_dot_sym;
66-
JL_DLLEXPORT jl_sym_t *jl_newvar_sym;
67-
JL_DLLEXPORT jl_sym_t *jl_boundscheck_sym;
68-
JL_DLLEXPORT jl_sym_t *jl_inbounds_sym;
69-
JL_DLLEXPORT jl_sym_t *jl_copyast_sym;
70-
JL_DLLEXPORT jl_sym_t *jl_cfunction_sym;
71-
JL_DLLEXPORT jl_sym_t *jl_loopinfo_sym;
72-
JL_DLLEXPORT jl_sym_t *jl_meta_sym;
73-
JL_DLLEXPORT jl_sym_t *jl_inert_sym;
74-
JL_DLLEXPORT jl_sym_t *jl_polly_sym;
75-
JL_DLLEXPORT jl_sym_t *jl_unused_sym;
76-
JL_DLLEXPORT jl_sym_t *jl_static_parameter_sym;
77-
JL_DLLEXPORT jl_sym_t *jl_inline_sym;
78-
JL_DLLEXPORT jl_sym_t *jl_noinline_sym;
79-
JL_DLLEXPORT jl_sym_t *jl_generated_sym;
80-
JL_DLLEXPORT jl_sym_t *jl_generated_only_sym;
81-
JL_DLLEXPORT jl_sym_t *jl_isdefined_sym;
82-
JL_DLLEXPORT jl_sym_t *jl_propagate_inbounds_sym;
83-
JL_DLLEXPORT jl_sym_t *jl_specialize_sym;
84-
JL_DLLEXPORT jl_sym_t *jl_aggressive_constprop_sym;
85-
JL_DLLEXPORT jl_sym_t *jl_no_constprop_sym;
86-
JL_DLLEXPORT jl_sym_t *jl_purity_sym;
87-
JL_DLLEXPORT jl_sym_t *jl_nospecialize_sym;
88-
JL_DLLEXPORT jl_sym_t *jl_nospecializeinfer_sym;
89-
JL_DLLEXPORT jl_sym_t *jl_macrocall_sym;
90-
JL_DLLEXPORT jl_sym_t *jl_colon_sym;
91-
JL_DLLEXPORT jl_sym_t *jl_hygienicscope_sym;
92-
JL_DLLEXPORT jl_sym_t *jl_throw_undef_if_not_sym;
93-
JL_DLLEXPORT jl_sym_t *jl_getfield_undefref_sym;
94-
JL_DLLEXPORT jl_sym_t *jl_gc_preserve_begin_sym;
95-
JL_DLLEXPORT jl_sym_t *jl_gc_preserve_end_sym;
96-
JL_DLLEXPORT jl_sym_t *jl_coverageeffect_sym;
97-
JL_DLLEXPORT jl_sym_t *jl_escape_sym;
98-
JL_DLLEXPORT jl_sym_t *jl_aliasscope_sym;
99-
JL_DLLEXPORT jl_sym_t *jl_popaliasscope_sym;
100-
JL_DLLEXPORT jl_sym_t *jl_optlevel_sym;
101-
JL_DLLEXPORT jl_sym_t *jl_thismodule_sym;
102-
JL_DLLEXPORT jl_sym_t *jl_eval_sym;
103-
JL_DLLEXPORT jl_sym_t *jl_include_sym;
104-
JL_DLLEXPORT jl_sym_t *jl_atom_sym;
105-
JL_DLLEXPORT jl_sym_t *jl_statement_sym;
106-
JL_DLLEXPORT jl_sym_t *jl_all_sym;
107-
JL_DLLEXPORT jl_sym_t *jl_compile_sym;
108-
JL_DLLEXPORT jl_sym_t *jl_force_compile_sym;
109-
JL_DLLEXPORT jl_sym_t *jl_infer_sym;
110-
JL_DLLEXPORT jl_sym_t *jl_max_methods_sym;
111-
JL_DLLEXPORT jl_sym_t *jl_atomic_sym;
112-
JL_DLLEXPORT jl_sym_t *jl_not_atomic_sym;
113-
JL_DLLEXPORT jl_sym_t *jl_unordered_sym;
114-
JL_DLLEXPORT jl_sym_t *jl_monotonic_sym;
115-
JL_DLLEXPORT jl_sym_t *jl_acquire_sym;
116-
JL_DLLEXPORT jl_sym_t *jl_release_sym;
117-
JL_DLLEXPORT jl_sym_t *jl_acquire_release_sym;
118-
JL_DLLEXPORT jl_sym_t *jl_sequentially_consistent_sym;
119-
JL_DLLEXPORT jl_sym_t *jl_uninferred_sym;
120-
JL_DLLEXPORT jl_sym_t *jl_latestworld_sym;
23+
#define XX(name) JL_DLLEXPORT jl_sym_t *jl_##name;
24+
JL_COMMON_SYMBOLS(XX)
25+
#undef XX
12126

12227
static const uint8_t flisp_system_image[] = {
12328
#include <julia_flisp.boot.inc>

src/gf.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ extern "C" {
2727
_Atomic(int) allow_new_worlds = 1;
2828
JL_DLLEXPORT _Atomic(size_t) jl_world_counter = 1; // uses atomic acquire/release
2929
jl_mutex_t world_counter_lock;
30-
jl_methtable_t *jl_method_table;
3130

3231
JL_DLLEXPORT size_t jl_get_world_counter(void) JL_NOTSAFEPOINT
3332
{

src/init.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,6 @@ JL_DLLEXPORT void jl_postoutput_hook(void)
380380
}
381381

382382
void post_boot_hooks(void);
383-
void post_image_load_hooks(void);
384383

385384
JL_DLLEXPORT void *jl_libjulia_internal_handle;
386385
JL_DLLEXPORT void *jl_libjulia_handle;
@@ -616,9 +615,6 @@ static NOINLINE void _finish_jl_init_(jl_image_buf_t sysimage, jl_ptls_t ptls, j
616615
jl_n_threads_per_pool[JL_THREADPOOL_ID_INTERACTIVE] = 0;
617616
jl_n_threads_per_pool[JL_THREADPOOL_ID_DEFAULT] = 1;
618617
}
619-
else {
620-
post_image_load_hooks();
621-
}
622618
jl_start_threads();
623619
jl_start_gc_threads();
624620
uv_barrier_wait(&thread_init_done);
@@ -730,7 +726,7 @@ JL_DLLEXPORT void jl_init_(jl_image_buf_t sysimage)
730726
// Note that if we ever want to be able to unload Julia entirely, we will
731727
// have to dlclose() these handles.
732728
jl_libjulia_internal_handle = jl_find_dynamic_library_by_addr(&jl_load_dynamic_library, /* throw_err */ 1, 0);
733-
jl_libjulia_handle = jl_find_dynamic_library_by_addr(&jl_any_type, /* throw_err */ 1, 0);
729+
jl_libjulia_handle = jl_find_dynamic_library_by_addr(&jl_options, /* throw_err */ 1, 0);
734730
#ifdef _OS_WINDOWS_
735731
/* If this parameter is NULL, GetModuleHandle returns a handle to the file
736732
used to create the calling process (.exe file). */

0 commit comments

Comments
 (0)