Skip to content

Commit 7908246

Browse files
committed
resolve all globals to GlobalRef very early
part of #10403
1 parent 14af2d1 commit 7908246

File tree

9 files changed

+141
-120
lines changed

9 files changed

+141
-120
lines changed

base/inference.jl

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,77 +1921,6 @@ function _sym_repl(s::Union{Symbol,GenSym}, from1, from2, to1, to2, deflt)
19211921
return deflt
19221922
end
19231923

1924-
# return an expr to evaluate "from.sym" in module "to"
1925-
function resolve_relative(sym, locals, args, from, to, orig)
1926-
if sym in locals || sym in args
1927-
return GlobalRef(from, sym)
1928-
end
1929-
if is(from,to)
1930-
return orig
1931-
end
1932-
const_from = (isconst(from,sym) && isdefined(from,sym))
1933-
const_to = (isconst(to,sym) && isdefined(to,sym))
1934-
if const_from
1935-
if const_to && is(eval(from,sym), eval(to,sym))
1936-
return orig
1937-
end
1938-
m = _topmod()
1939-
if is(from, m) || is(from, Core)
1940-
return TopNode(sym)
1941-
end
1942-
end
1943-
return GlobalRef(from, sym)
1944-
end
1945-
1946-
# annotate symbols with their original module for inlining
1947-
function resolve_globals(e::ANY, locals, args, from, to, env1, env2)
1948-
if isa(e,Symbol)
1949-
s = e::Symbol
1950-
if contains_is(env1, s) || contains_is(env2, s)
1951-
return s
1952-
end
1953-
return resolve_relative(s, locals, args, from, to, s)
1954-
end
1955-
if isa(e,SymbolNode)
1956-
s = e::SymbolNode
1957-
name = s.name
1958-
if contains_is(env1, name) || contains_is(env2, name)
1959-
return s
1960-
end
1961-
return resolve_relative(name, locals, args, from, to, s)
1962-
end
1963-
if !isa(e,Expr)
1964-
return e
1965-
end
1966-
e = e::Expr
1967-
if e.head === :(=)
1968-
# remove_redundant_temp_vars can only handle Symbols
1969-
# on the LHS of assignments, so we make sure not to put
1970-
# something else there
1971-
e2 = resolve_globals(e.args[1]::Union{Symbol,GenSym}, locals, args, from, to, env1, env2)
1972-
if isa(e2, GlobalRef)
1973-
# abort when trying to inline a function which assigns to a global
1974-
# variable in a different module, since `Mod.X=V` isn't allowed
1975-
throw(e2)
1976-
# e2 = e2::GetfieldNode
1977-
# e = Expr(:call, top_setfield, e2.value, qn(e2.name),
1978-
# resolve_globals(e.args[2], locals, args, from, to, env1, env2))
1979-
# e.typ = e2.typ
1980-
else
1981-
e.args[1] = e2::Union{Symbol,GenSym}
1982-
e.args[2] = resolve_globals(e.args[2], locals, args, from, to, env1, env2)
1983-
end
1984-
elseif !is(e.head,:line)
1985-
for i=1:length(e.args)
1986-
subex = e.args[i]
1987-
if !(isa(subex,Number) || isa(subex,AbstractString))
1988-
e.args[i] = resolve_globals(subex, locals, args, from, to, env1, env2)
1989-
end
1990-
end
1991-
end
1992-
e
1993-
end
1994-
19951924
# count occurrences up to n+1
19961925
function occurs_more(e::ANY, pred, n)
19971926
if isa(e,Expr)
@@ -2320,7 +2249,6 @@ function inlineable(f::ANY, e::Expr, atype::ANY, sv::StaticVarInfo, enclosing_as
23202249

23212250
body = Expr(:block)
23222251
body.args = without_linenums(ast.args[3].args)::Array{Any,1}
2323-
need_mod_annotate = true
23242252
cost::Int = 1000
23252253
if incompletematch
23262254
cost *= 4
@@ -2351,7 +2279,6 @@ function inlineable(f::ANY, e::Expr, atype::ANY, sv::StaticVarInfo, enclosing_as
23512279
end
23522280
body.args = Any[Expr(:return, newcall)]
23532281
ast = Expr(:lambda, newnames, Any[[], locals, [], 0], body)
2354-
need_mod_annotate = false
23552282
needcopy = false
23562283
else
23572284
return NF
@@ -2434,26 +2361,6 @@ function inlineable(f::ANY, e::Expr, atype::ANY, sv::StaticVarInfo, enclosing_as
24342361
end
24352362
end
24362363

2437-
# annotate variables in the body expression with their module
2438-
if need_mod_annotate
2439-
mfrom = linfo.module; mto = (inference_stack::CallStack).mod
2440-
enc_capt = enclosing_ast.args[2][2]
2441-
if !isempty(enc_capt)
2442-
# add captured var names to list of locals
2443-
enc_vars = vcat(enc_locllist, map(vi->vi[1], enc_capt))
2444-
else
2445-
enc_vars = enc_locllist
2446-
end
2447-
try
2448-
body = resolve_globals(body, enc_vars, enclosing_ast.args[1], mfrom, mto, args, spnames)
2449-
catch ex
2450-
if isa(ex,GlobalRef)
2451-
return NF
2452-
end
2453-
rethrow(ex)
2454-
end
2455-
end
2456-
24572364
# see if each argument occurs only once in the body expression
24582365
stmts = []
24592366
stmts_free = true # true = all entries of stmts are effect_free

base/show.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ show(io::IO, s::Symbol) = show_unquoted_quote_expr(io, s, 0, 0)
253253
# While this isn’t true of ALL show methods, it is of all ASTs.
254254

255255
typealias ExprNode Union{Expr, QuoteNode, SymbolNode, LineNumberNode,
256-
LabelNode, GotoNode, TopNode}
256+
LabelNode, GotoNode, TopNode, GlobalRef}
257257
# Operators have precedence levels from 1-N, and show_unquoted defaults to a
258258
# precedence level of 0 (the fourth argument). The top-level print and show
259259
# methods use a precedence of -1 to specially allow space-separated macro syntax
@@ -410,6 +410,7 @@ show_unquoted(io::IO, ex::LineNumberNode, ::Int, ::Int) = show_linenumber(io, ex
410410
show_unquoted(io::IO, ex::LabelNode, ::Int, ::Int) = print(io, ex.label, ": ")
411411
show_unquoted(io::IO, ex::GotoNode, ::Int, ::Int) = print(io, "goto ", ex.label)
412412
show_unquoted(io::IO, ex::TopNode, ::Int, ::Int) = print(io,"top(",ex.name,')')
413+
show_unquoted(io::IO, ex::GlobalRef, ::Int, ::Int) = print(io, ex.mod, '.', ex.name)
413414

414415
function show_unquoted(io::IO, ex::SymbolNode, ::Int, ::Int)
415416
print(io, ex.name)
@@ -494,16 +495,20 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
494495
# function call
495496
elseif head == :call && nargs >= 1
496497
func = args[1]
497-
func_prec = operator_precedence(func)
498+
fname = isa(func,GlobalRef) ? func.name : func
499+
func_prec = operator_precedence(fname)
500+
if func_prec > 0 || fname in uni_ops
501+
func = fname
502+
end
498503
func_args = args[2:end]
499504

500505
if in(ex.args[1], (:box, TopNode(:box), :throw)) || ismodulecall(ex)
501506
show_expr_type_emphasize::Bool = show_type = false
502507
end
503508

504509
# scalar multiplication (i.e. "100x")
505-
if (func == :(*) && length(func_args)==2 &&
506-
isa(func_args[1], Real) && isa(func_args[2], Symbol))
510+
if (func == :(*) &&
511+
length(func_args)==2 && isa(func_args[1], Real) && isa(func_args[2], Symbol))
507512
if func_prec <= prec
508513
show_enclosed_list(io, '(', func_args, "", ')', indent, func_prec)
509514
else

src/ast.c

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ static jl_value_t *full_list_of_lists(value_t e, int expronly)
198198
return (jl_value_t*)ar;
199199
}
200200

201+
static jl_value_t *resolve_globals(jl_value_t *expr, jl_lambda_info_t *lam);
202+
201203
static jl_value_t *scm_to_julia(value_t e, int expronly)
202204
{
203205
int en = jl_gc_enable(0);
@@ -317,7 +319,9 @@ static jl_value_t *scm_to_julia_(value_t e, int eo)
317319
jl_cellset(ex->args, i, scm_to_julia_(car_(e), eo));
318320
e = cdr_(e);
319321
}
320-
return (jl_value_t*)jl_new_lambda_info((jl_value_t*)ex, jl_emptysvec);
322+
jl_lambda_info_t *nli = jl_new_lambda_info((jl_value_t*)ex, jl_emptysvec);
323+
resolve_globals(nli->ast, nli);
324+
return (jl_value_t*)nli;
321325
}
322326

323327
e = cdr_(e);
@@ -971,6 +975,63 @@ jl_value_t *skip_meta(jl_array_t *body)
971975
return body1;
972976
}
973977

978+
static int in_vinfo_array(jl_array_t *a, jl_value_t *v)
979+
{
980+
size_t i, l=jl_array_len(a);
981+
for(i=0; i<l; i++) {
982+
if (jl_cellref(jl_cellref(a,i),0) == v)
983+
return 1;
984+
}
985+
return 0;
986+
}
987+
988+
static int in_sym_array(jl_array_t *a, jl_value_t *v)
989+
{
990+
size_t i, l=jl_array_len(a);
991+
for(i=0; i<l; i++) {
992+
if (jl_cellref(a,i) == v)
993+
return 1;
994+
}
995+
return 0;
996+
}
997+
998+
static jl_value_t *resolve_globals(jl_value_t *expr, jl_lambda_info_t *lam)
999+
{
1000+
if (jl_is_symbol(expr)) {
1001+
if (lam->module == NULL)
1002+
return expr;
1003+
int is_local = in_vinfo_array(jl_lam_vinfo((jl_expr_t*)lam->ast), expr) ||
1004+
in_vinfo_array(jl_lam_capt((jl_expr_t*)lam->ast), expr) ||
1005+
in_sym_array(jl_lam_staticparams((jl_expr_t*)lam->ast), expr);
1006+
if (!is_local)
1007+
return jl_module_globalref(lam->module, (jl_sym_t*)expr);
1008+
}
1009+
else if (jl_is_lambda_info(expr)) {
1010+
jl_lambda_info_t *l = (jl_lambda_info_t*)expr;
1011+
(void)resolve_globals(l->ast, l);
1012+
}
1013+
else if (jl_is_expr(expr)) {
1014+
jl_expr_t *e = (jl_expr_t*)expr;
1015+
if (e->head == lambda_sym) {
1016+
(void)resolve_globals(jl_exprarg(e,2), lam);
1017+
}
1018+
else if (jl_is_toplevel_only_expr(expr) || e->head == const_sym || e->head == copyast_sym ||
1019+
e->head == global_sym || e->head == quote_sym || e->head == inert_sym ||
1020+
e->head == line_sym || e->head == meta_sym) {
1021+
}
1022+
else {
1023+
size_t i = 0;
1024+
if (e->head == method_sym || e->head == abstracttype_sym || e->head == compositetype_sym ||
1025+
e->head == bitstype_sym || e->head == macro_sym || e->head == module_sym)
1026+
i++;
1027+
for(; i < jl_array_len(e->args); i++) {
1028+
jl_exprargset(e, i, resolve_globals(jl_exprarg(e,i), lam));
1029+
}
1030+
}
1031+
}
1032+
return expr;
1033+
}
1034+
9741035
#ifdef __cplusplus
9751036
}
9761037
#endif

0 commit comments

Comments
 (0)