Skip to content

Commit 5d2e538

Browse files
authored
Merge pull request #19846 from JuliaLang/jb/fix18838
fix #18838, populate `source` field of generated functions
2 parents 887815c + 8b708b4 commit 5d2e538

File tree

6 files changed

+23
-23
lines changed

6 files changed

+23
-23
lines changed

src/alloc.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ JL_DLLEXPORT jl_code_info_t *jl_code_for_staged(jl_method_instance_t *linfo)
484484
jl_expr_t *ex = NULL;
485485
jl_value_t *linenum = NULL;
486486
jl_svec_t *sparam_vals = env;
487-
jl_method_instance_t *generator = linfo->def->unspecialized;
487+
jl_method_instance_t *generator = linfo->def->generator;
488488
assert(linfo != generator);
489489
assert(linfo->def->isstaged);
490490
jl_code_info_t *func = NULL;
@@ -509,7 +509,7 @@ JL_DLLEXPORT jl_code_info_t *jl_code_for_staged(jl_method_instance_t *linfo)
509509
jl_array_t *argnames = jl_alloc_vec_any(nargs);
510510
jl_array_ptr_set(ex->args, 0, argnames);
511511
for (i = 0; i < nargs; i++)
512-
jl_array_ptr_set(argnames, i, jl_array_ptr_ref(((jl_code_info_t*)generator->inferred)->slotnames, i));
512+
jl_array_ptr_set(argnames, i, jl_array_ptr_ref(linfo->def->source->slotnames, i));
513513

514514
jl_expr_t *scopeblock = jl_exprn(jl_symbol("scope-block"), 1);
515515
jl_array_ptr_set(ex->args, 1, scopeblock);
@@ -642,6 +642,7 @@ JL_DLLEXPORT jl_method_t *jl_new_method_uninit(void)
642642
m->module = ptls->current_module;
643643
m->source = NULL;
644644
m->unspecialized = NULL;
645+
m->generator = NULL;
645646
m->name = NULL;
646647
m->file = empty_sym;
647648
m->line = 0;
@@ -689,12 +690,10 @@ jl_method_t *jl_new_method(jl_code_info_t *definition,
689690
root = (jl_value_t*)m;
690691
jl_method_set_source(m, definition);
691692
if (isstaged) {
692-
// remove the code from `->source` (since generic source isn't present)
693-
// and use the `->unspecialized` field to be the source generator
694-
m->unspecialized = jl_get_specialized(m, jl_anytuple_type, jl_emptysvec);
695-
jl_gc_wb(m, m->unspecialized);
696-
m->unspecialized->inferred = (jl_value_t*)m->source;
697-
m->source = NULL;
693+
// create and store generator for generated functions
694+
m->generator = jl_get_specialized(m, jl_anytuple_type, jl_emptysvec);
695+
jl_gc_wb(m, m->generator);
696+
m->generator->inferred = (jl_value_t*)m->source;
698697
}
699698

700699
#ifdef RECORD_METHOD_ORDER

src/codegen.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,9 +1297,7 @@ void jl_extern_c(jl_function_t *f, jl_value_t *rt, jl_value_t *argt, char *name)
12971297
extern "C" JL_DLLEXPORT
12981298
void *jl_get_llvmf_defn(jl_method_instance_t *linfo, size_t world, bool getwrapper, bool optimize, const jl_cgparams_t params)
12991299
{
1300-
// `source` is `NULL` for generated functions.
1301-
// The `isstaged` check can be removed if that is not the case anymore.
1302-
if (linfo->def && linfo->def->source == NULL && !linfo->def->isstaged) {
1300+
if (linfo->def && linfo->def->source == NULL) {
13031301
// not a generic function
13041302
return NULL;
13051303
}
@@ -1376,9 +1374,7 @@ void *jl_get_llvmf_defn(jl_method_instance_t *linfo, size_t world, bool getwrapp
13761374
extern "C" JL_DLLEXPORT
13771375
void *jl_get_llvmf_decl(jl_method_instance_t *linfo, size_t world, bool getwrapper, const jl_cgparams_t params)
13781376
{
1379-
// `source` is `NULL` for generated functions.
1380-
// The `isstaged` check can be removed if that is not the case anymore.
1381-
if (linfo->def && linfo->def->source == NULL && !linfo->def->isstaged) {
1377+
if (linfo->def && linfo->def->source == NULL) {
13821378
// not a generic function
13831379
return NULL;
13841380
}

src/dump.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,7 @@ static void jl_serialize_value_(jl_serializer_state *s, jl_value_t *v)
920920
jl_serialize_value(s, (jl_value_t*)m->roots);
921921
jl_serialize_value(s, (jl_value_t*)m->source);
922922
jl_serialize_value(s, (jl_value_t*)m->unspecialized);
923+
jl_serialize_value(s, (jl_value_t*)m->generator);
923924
jl_serialize_value(s, (jl_value_t*)m->invokes.unknown);
924925
write_int8(s->s, m->needs_sparam_vals_ducttape);
925926
}
@@ -1625,6 +1626,9 @@ static jl_value_t *jl_deserialize_value_method(jl_serializer_state *s, jl_value_
16251626
m->unspecialized = (jl_method_instance_t*)jl_deserialize_value(s, (jl_value_t**)&m->unspecialized);
16261627
if (m->unspecialized)
16271628
jl_gc_wb(m, m->unspecialized);
1629+
m->generator = (jl_method_instance_t*)jl_deserialize_value(s, (jl_value_t**)&m->generator);
1630+
if (m->generator)
1631+
jl_gc_wb(m, m->generator);
16281632
m->invokes.unknown = jl_deserialize_value(s, (jl_value_t**)&m->invokes);
16291633
jl_gc_wb(m, m->invokes.unknown);
16301634
m->needs_sparam_vals_ducttape = read_int8(s->s);

src/jltypes.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3893,7 +3893,7 @@ void jl_init_types(void)
38933893
jl_method_type =
38943894
jl_new_datatype(jl_symbol("Method"),
38953895
jl_any_type, jl_emptysvec,
3896-
jl_svec(20,
3896+
jl_svec(21,
38973897
jl_symbol("name"),
38983898
jl_symbol("module"),
38993899
jl_symbol("file"),
@@ -3907,14 +3907,15 @@ void jl_init_types(void)
39073907
jl_symbol("sparam_syms"),
39083908
jl_symbol("source"),
39093909
jl_symbol("unspecialized"),
3910+
jl_symbol("generator"),
39103911
jl_symbol("roots"),
39113912
jl_symbol("invokes"),
39123913
jl_symbol("nargs"),
39133914
jl_symbol("called"),
39143915
jl_symbol("isva"),
39153916
jl_symbol("isstaged"),
39163917
jl_symbol("needs_sparam_vals_ducttape")),
3917-
jl_svec(20,
3918+
jl_svec(21,
39183919
jl_sym_type,
39193920
jl_module_type,
39203921
jl_sym_type,
@@ -3928,6 +3929,7 @@ void jl_init_types(void)
39283929
jl_simplevector_type,
39293930
jl_code_info_type,
39303931
jl_any_type, // jl_method_instance_type
3932+
jl_any_type, // jl_method_instance_type
39313933
jl_array_any_type,
39323934
jl_any_type,
39333935
jl_int32_type,
@@ -4037,6 +4039,7 @@ void jl_init_types(void)
40374039
#endif
40384040
jl_svecset(jl_methtable_type->types, 8, jl_int32_type); // uint32_t
40394041
jl_svecset(jl_method_type->types, 12, jl_method_instance_type);
4042+
jl_svecset(jl_method_type->types, 13, jl_method_instance_type);
40404043
jl_svecset(jl_method_instance_type->types, 12, jl_voidpointer_type);
40414044
jl_svecset(jl_method_instance_type->types, 13, jl_voidpointer_type);
40424045
jl_svecset(jl_method_instance_type->types, 14, jl_voidpointer_type);

src/julia.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,10 @@ typedef struct _jl_method_t {
241241
// table of all argument types for which we've inferred or compiled this code
242242
union jl_typemap_t specializations;
243243

244-
// sparams are the symbols in the tvars vector
245-
jl_svec_t *sparam_syms;
246-
// the code AST template
247-
jl_code_info_t *source; // null for builtins and staged functions
248-
// unspecialized executable thunk (for isstaged, code for the generator), or null
249-
struct _jl_method_instance_t *unspecialized;
244+
jl_svec_t *sparam_syms; // symbols corresponding to the tvars vector
245+
jl_code_info_t *source; // original code template, null for builtins
246+
struct _jl_method_instance_t *unspecialized; // unspecialized executable method instance, or null
247+
struct _jl_method_instance_t *generator; // executable code-generating function if isstaged
250248
jl_array_t *roots; // pointers in generated code (shared to reduce memory), or null
251249

252250
// cache of specializations of this method for invoke(), i.e.

src/toplevel.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ void print_func_loc(JL_STREAM *s, jl_method_t *m);
717717

718718
void jl_check_static_parameter_conflicts(jl_method_t *m, jl_svec_t *t)
719719
{
720-
jl_code_info_t *src = m->isstaged ? (jl_code_info_t*)m->unspecialized->inferred : m->source;
720+
jl_code_info_t *src = m->source;
721721
size_t nvars = jl_array_len(src->slotnames);
722722

723723
size_t i, n = jl_svec_len(t);

0 commit comments

Comments
 (0)