Skip to content

Commit bca2951

Browse files
committed
remove typ field from Expr
update optimization passes to keep IR in linear form
1 parent 62aee92 commit bca2951

File tree

14 files changed

+278
-378
lines changed

14 files changed

+278
-378
lines changed

base/boot.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@
8282
#mutable struct Expr
8383
# head::Symbol
8484
# args::Array{Any,1}
85-
# typ::Any
8685
#end
8786

8887
#struct LineNumberNode

base/expr.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ end
3434

3535
copy(e::Expr) = (n = Expr(e.head);
3636
n.args = copy_exprargs(e.args);
37-
n.typ = e.typ;
3837
n)
3938

4039
# copy parts of an AST that the compiler mutates

base/inference.jl

Lines changed: 228 additions & 306 deletions
Large diffs are not rendered by default.

base/interactiveutil.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,6 @@ function code_warntype(io::IO, f, @nospecialize(t))
386386
print(emph_io, "\nBody:\n ")
387387
body = Expr(:body)
388388
body.args = src.code
389-
body.typ = rettype
390389
# Fix slot names and types in function body
391390
show_unquoted(IOContext(emph_io, :SOURCEINFO => src, :SOURCE_SLOTNAMES => slotnames),
392391
body, 2)

base/precompile.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -865,8 +865,6 @@ precompile(Tuple{typeof(Core.Inference.copy!), Array{Any, 1}, Core.Inference.Gen
865865
precompile(Tuple{typeof(Core.Inference._widen_all_consts!), Expr, Array{Bool, 1}})
866866
precompile(Tuple{typeof(Core.Inference._delete!), Core.Inference.IntSet, Int64})
867867
precompile(Tuple{typeof(Core.Inference.promote_type), Type{Float16}, Type{Int64}})
868-
precompile(Tuple{typeof(Core.Inference.mk_tuplecall), Array{Any, 1}, Core.Inference.InferenceState})
869-
precompile(Tuple{typeof(Core.Inference.inlining_pass), Expr, Core.Inference.InferenceState, Array{Any, 1}, Int64})
870868
precompile(Tuple{typeof(Core.Inference.annotate_slot_load!), Expr, Array{Any, 1}, Core.Inference.InferenceState, Array{Bool, 1}})
871869
precompile(Tuple{typeof(Core.Inference.record_slot_assign!), Core.Inference.InferenceState})
872870
precompile(Tuple{typeof(Core.Inference.type_annotate!), Core.Inference.InferenceState})
@@ -1078,7 +1076,6 @@ precompile(Tuple{typeof(Base.setindex!), Base.Dict{Any, Any}, Bool, Symbol})
10781076
precompile(Tuple{typeof(Base._setindex!), Base.Dict{Any, Any}, Bool, Symbol, Int64})
10791077
precompile(Tuple{typeof(Base.merge!), Base.Dict{Any, Any}, Base.Dict{Any, Any}, Base.Dict{Any, Any}})
10801078
precompile(Tuple{Type{Symbol}, Symbol})
1081-
precompile(Tuple{typeof(Core.Inference.mk_getfield), TypedSlot, Int64, Type{String}})
10821079
precompile(Tuple{getfield(Core, Symbol("#kw#Type")), Array{Any, 1}, Type{Base.Cmd}, Base.Cmd})
10831080
precompile(Tuple{typeof(Base.uv_status_string), Base.PipeEndpoint})
10841081
precompile(Tuple{typeof(Base._fd), Base.PipeEndpoint})
@@ -1325,7 +1322,6 @@ precompile(Tuple{typeof(Base.convert), Type{Base.AbstractChannel}, Base.Channel{
13251322
precompile(Tuple{typeof(Base.ndigits0z), UInt8})
13261323
precompile(Tuple{typeof(Base.setindex!), Base.Dict{Any, Any}, Base.Distributed.RemoteValue, Base.Distributed.RRID})
13271324
precompile(Tuple{typeof(Base.dec), UInt8, Int64, Bool})
1328-
precompile(Tuple{typeof(Core.Inference.mk_getfield), TypedSlot, Int64, Type{Integer}})
13291325
precompile(Tuple{typeof(Base.ht_keyindex2), Base.Dict{Any, Any}, Base.Distributed.RRID})
13301326
precompile(Tuple{typeof(Base._setindex!), Base.Dict{Any, Any}, Base.Distributed.RemoteValue, Base.Distributed.RRID, Int64})
13311327
precompile(Tuple{typeof(Core.Inference.length), Tuple{Core.Inference.Const, DataType, Core.Inference.Const}})

base/serialize.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ function serialize(s::AbstractSerializer, ex::Expr)
331331
write(s.io, Int32(l))
332332
end
333333
serialize(s, ex.head)
334-
serialize(s, ex.typ)
335334
for a in ex.args
336335
serialize(s, a)
337336
end
@@ -966,9 +965,7 @@ function deserialize_expr(s::AbstractSerializer, len)
966965
e = Expr(:temp)
967966
resolve_ref_immediately(s, e)
968967
e.head = deserialize(s)::Symbol
969-
ty = deserialize(s)
970968
e.args = Any[ deserialize(s) for i = 1:len ]
971-
e.typ = ty
972969
e
973970
end
974971

base/show.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -876,9 +876,9 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
876876
ex.head == :return)
877877
show_type = false
878878
end
879-
if !emphstate && ex.typ === Any
879+
#if !emphstate && ex.typ === Any
880880
show_type = false
881-
end
881+
#end
882882
# dot (i.e. "x.y"), but not compact broadcast exps
883883
if head === :(.) && !is_expr(args[2], :tuple)
884884
func_prec = operator_precedence(head)
@@ -1219,7 +1219,7 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
12191219
end
12201220
print(io, "))")
12211221
end
1222-
show_type && show_expr_type(io, ex.typ, emphstate)
1222+
#show_type && show_expr_type(io, ex.typ, emphstate)
12231223
nothing
12241224
end
12251225

src/builtins.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,6 @@ jl_expr_t *jl_exprn(jl_sym_t *head, size_t n)
10051005
jl_expr_type);
10061006
ex->head = head;
10071007
ex->args = ar;
1008-
ex->etype = (jl_value_t*)jl_any_type;
10091008
JL_GC_POP();
10101009
return ex;
10111010
}
@@ -1023,7 +1022,6 @@ JL_CALLABLE(jl_f__expr)
10231022
jl_expr_type);
10241023
ex->head = (jl_sym_t*)args[0];
10251024
ex->args = ar;
1026-
ex->etype = (jl_value_t*)jl_any_type;
10271025
JL_GC_POP();
10281026
return (jl_value_t*)ex;
10291027
}

src/codegen.cpp

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ class jl_codectx_t {
576576
}
577577
};
578578

579-
static jl_cgval_t emit_expr(jl_codectx_t &ctx, jl_value_t *expr);
579+
static jl_cgval_t emit_expr(jl_codectx_t &ctx, jl_value_t *expr, jl_value_t *etype = (jl_value_t*)jl_any_type);
580580
static Value *global_binding_pointer(jl_codectx_t &ctx, jl_module_t *m, jl_sym_t *s,
581581
jl_binding_t **pbnd, bool assign);
582582
static jl_cgval_t emit_checked_var(jl_codectx_t &ctx, Value *bp, jl_sym_t *name, bool isvol, MDNode *tbaa);
@@ -2982,12 +2982,12 @@ static jl_cgval_t emit_call_function_object(jl_method_instance_t *li, jl_llvm_fu
29822982
return mark_julia_type(ctx, ret, true, inferred_retty);
29832983
}
29842984

2985-
static jl_cgval_t emit_invoke(jl_codectx_t &ctx, jl_expr_t *ex)
2985+
static jl_cgval_t emit_invoke(jl_codectx_t &ctx, jl_expr_t *ex, jl_value_t *etype)
29862986
{
29872987
jl_value_t **args = (jl_value_t**)jl_array_data(ex->args);
29882988
size_t arglen = jl_array_dim0(ex->args);
29892989
size_t nargs = arglen - 1;
2990-
jl_value_t *rt = ex->etype;
2990+
jl_value_t *rt = etype;
29912991
assert(arglen >= 2);
29922992

29932993
jl_cgval_t lival = emit_expr(ctx, args[0]);
@@ -3027,12 +3027,12 @@ static jl_cgval_t emit_invoke(jl_codectx_t &ctx, jl_expr_t *ex)
30273027
return result;
30283028
}
30293029

3030-
static jl_cgval_t emit_call(jl_codectx_t &ctx, jl_expr_t *ex)
3030+
static jl_cgval_t emit_call(jl_codectx_t &ctx, jl_expr_t *ex, jl_value_t *etype)
30313031
{
30323032
jl_value_t **args = (jl_value_t**)jl_array_data(ex->args);
30333033
size_t nargs = jl_array_dim0(ex->args);
30343034
assert(nargs >= 1);
3035-
jl_value_t *rt = ex->etype;
3035+
jl_value_t *rt = etype;
30363036
jl_cgval_t f = emit_expr(ctx, args[0]);
30373037

30383038
if (f.constant && jl_typeis(f.constant, jl_intrinsic_type)) {
@@ -3453,18 +3453,13 @@ static void emit_assignment(jl_codectx_t &ctx, jl_value_t *l, jl_value_t *r)
34533453
ssize_t idx = ((jl_ssavalue_t*)l)->id;
34543454
assert(idx >= 0);
34553455
assert(!ctx.ssavalue_assigned.at(idx));
3456-
jl_cgval_t slot = emit_expr(ctx, r); // slot could be a jl_value_t (unboxed) or jl_value_t* (ispointer)
3457-
if (slot.isboxed || slot.TIndex) {
3458-
// see if inference suggested a different type for the ssavalue than the expression
3459-
// e.g. sometimes the information is inconsistent after inlining getfield on a Tuple
3460-
jl_value_t *ssavalue_types = (jl_value_t*)ctx.source->ssavaluetypes;
3461-
if (jl_is_array(ssavalue_types)) {
3462-
jl_value_t *declType = jl_array_ptr_ref(ssavalue_types, idx);
3463-
if (declType != slot.typ) {
3464-
slot = update_julia_type(ctx, slot, declType);
3465-
}
3466-
}
3456+
jl_value_t *declType = (jl_value_t*)jl_any_type;
3457+
// get expr type from its ssavalue
3458+
jl_value_t *ssavalue_types = (jl_value_t*)ctx.source->ssavaluetypes;
3459+
if (jl_is_array(ssavalue_types)) {
3460+
declType = jl_array_ptr_ref(ssavalue_types, idx);
34673461
}
3462+
jl_cgval_t slot = emit_expr(ctx, r, declType); // slot could be a jl_value_t (unboxed) or jl_value_t* (ispointer)
34683463
if (!slot.isboxed && !slot.isimmutable) {
34693464
// emit a copy of values stored in mutable slots
34703465
Value *dest;
@@ -3684,7 +3679,7 @@ static void emit_stmtpos(jl_codectx_t &ctx, jl_value_t *expr)
36843679
}
36853680
}
36863681

3687-
static jl_cgval_t emit_expr(jl_codectx_t &ctx, jl_value_t *expr)
3682+
static jl_cgval_t emit_expr(jl_codectx_t &ctx, jl_value_t *expr, jl_value_t *etype)
36883683
{
36893684
if (jl_is_symbol(expr)) {
36903685
jl_sym_t *sym = (jl_sym_t*)expr;
@@ -3754,15 +3749,14 @@ static jl_cgval_t emit_expr(jl_codectx_t &ctx, jl_value_t *expr)
37543749
return emit_isdefined(ctx, args[0]);
37553750
}
37563751
else if (head == invoke_sym) {
3757-
return emit_invoke(ctx, ex);
3752+
return emit_invoke(ctx, ex, etype);
37583753
}
37593754
else if (head == call_sym) {
3760-
jl_cgval_t res = emit_call(ctx, ex);
3755+
jl_cgval_t res = emit_call(ctx, ex, etype);
37613756
// some intrinsics (e.g. typeassert) can return a wider type
37623757
// than what's actually possible
3763-
jl_value_t *expr_t = ex->etype;
3764-
res = update_julia_type(ctx, res, expr_t);
3765-
if (res.typ == jl_bottom_type || expr_t == jl_bottom_type) {
3758+
res = update_julia_type(ctx, res, etype);
3759+
if (res.typ == jl_bottom_type || etype == jl_bottom_type) {
37663760
CreateTrap(ctx.builder);
37673761
}
37683762
return res;

src/dump.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,6 @@ static void jl_serialize_value_(jl_serializer_state *s, jl_value_t *v, int as_li
609609
write_int32(s->s, l);
610610
}
611611
jl_serialize_value(s, e->head);
612-
jl_serialize_value(s, e->etype);
613612
for (i = 0; i < l; i++) {
614613
jl_serialize_value(s, jl_exprarg(e, i));
615614
}
@@ -1385,8 +1384,6 @@ static jl_value_t *jl_deserialize_value_expr(jl_serializer_state *s, jl_value_t
13851384
jl_expr_t *e = jl_exprn((jl_sym_t*)jl_deserialize_value(s, NULL), len);
13861385
if (usetable)
13871386
backref_list.items[pos] = e;
1388-
e->etype = jl_deserialize_value(s, &e->etype);
1389-
jl_gc_wb(e, e->etype);
13901387
jl_value_t **data = (jl_value_t**)(e->args->data);
13911388
for (i = 0; i < len; i++) {
13921389
data[i] = jl_deserialize_value(s, &data[i]);

src/jltypes.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,10 +1961,9 @@ void jl_init_types(void)
19611961
jl_expr_type =
19621962
jl_new_datatype(jl_symbol("Expr"), core,
19631963
jl_any_type, jl_emptysvec,
1964-
jl_perm_symsvec(3, "head", "args", "typ"),
1965-
jl_svec(3, jl_sym_type, jl_array_any_type,
1966-
jl_any_type),
1967-
0, 1, 3);
1964+
jl_perm_symsvec(2, "head", "args"),
1965+
jl_svec(2, jl_sym_type, jl_array_any_type),
1966+
0, 1, 2);
19681967

19691968
jl_linenumbernode_type =
19701969
jl_new_datatype(jl_symbol("LineNumberNode"), core, jl_any_type, jl_emptysvec,

src/julia.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,6 @@ typedef struct {
470470
JL_DATA_TYPE
471471
jl_sym_t *head;
472472
jl_array_t *args;
473-
jl_value_t *etype;
474473
} jl_expr_t;
475474

476475
// constants and type objects -------------------------------------------------

src/rtutils.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,8 +808,7 @@ static size_t jl_static_show_x_(JL_STREAM *out, jl_value_t *v, jl_datatype_t *vt
808808
n += jl_printf(out, ",%c", sep);
809809
n += jl_static_show_x(out, jl_exprarg(e,i), depth);
810810
}
811-
n += jl_printf(out, ")::");
812-
n += jl_static_show_x(out, e->etype, depth);
811+
n += jl_printf(out, ")");
813812
}
814813
}
815814
else if (jl_is_array_type(vt)) {

test/inference.jl

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -411,34 +411,25 @@ end
411411
@inferred cat10880(Tuple{Int8,Int16}, Tuple{Int32})
412412

413413
# issue #19348
414-
function is_typed_expr(e::Expr)
415-
if e.head === :call ||
416-
e.head === :invoke ||
417-
e.head === :new ||
418-
e.head === :copyast ||
419-
e.head === :inert
420-
return true
421-
end
422-
return false
423-
end
424-
test_inferred_static(@nospecialize(other)) = true
425-
test_inferred_static(slot::TypedSlot) = @test isleaftype(slot.typ)
426-
function test_inferred_static(expr::Expr)
427-
if is_typed_expr(expr)
428-
@test isleaftype(expr.typ)
429-
end
430-
for a in expr.args
431-
test_inferred_static(a)
414+
function test_inferred_static(code::CodeInfo)
415+
@test all(isleaftype, code.slottypes)
416+
@test all(isleaftype, code.ssavaluetypes)
417+
for e in code.code
418+
test_inferred_static_expr(e)
432419
end
433420
end
434421
function test_inferred_static(arrow::Pair)
435422
code, rt = arrow
436423
@test isleaftype(rt)
437424
@test code.inferred
438-
@test all(x->isleaftype(x), code.slottypes)
439-
@test all(x->isleaftype(x), code.ssavaluetypes)
440-
for e in code.code
441-
test_inferred_static(e)
425+
test_inferred_static(code)
426+
end
427+
428+
test_inferred_static_expr(@nospecialize(other)) = true
429+
test_inferred_static_expr(slot::TypedSlot) = @test isleaftype(slot.typ)
430+
function test_inferred_static_expr(expr::Expr)
431+
for a in expr.args
432+
test_inferred_static_expr(a)
442433
end
443434
end
444435

@@ -484,7 +475,6 @@ for codetype in Any[
484475
local notconst(@nospecialize(other)) = true
485476
notconst(slot::TypedSlot) = @test isa(slot.typ, Type)
486477
function notconst(expr::Expr)
487-
@test isa(expr.typ, Type)
488478
for a in expr.args
489479
notconst(a)
490480
end
@@ -1049,12 +1039,24 @@ function test_const_return(@nospecialize(f), @nospecialize(t), @nospecialize(val
10491039
end
10501040
end
10511041

1052-
function find_call(code, func, narg)
1042+
function find_def(code, ssav::SSAValue)
1043+
for ex in code
1044+
if isa(ex, Expr) && ex.head === :(=) && ex.args[1] === ssav
1045+
return ex.args[2]
1046+
end
1047+
end
1048+
error("SSAValue assignment not found")
1049+
end
1050+
1051+
function find_call(code, func, narg, code0 = code)
10531052
for ex in code
10541053
isa(ex, Expr) || continue
10551054
ex = ex::Expr
10561055
if ex.head === :call && length(ex.args) == narg
10571056
farg = ex.args[1]
1057+
while isa(farg, SSAValue)
1058+
farg = find_def(code0, farg)
1059+
end
10581060
if isa(farg, GlobalRef)
10591061
farg = farg::GlobalRef
10601062
if isdefined(farg.mod, farg.name) && isconst(farg.mod, farg.name)
@@ -1067,7 +1069,7 @@ function find_call(code, func, narg)
10671069
elseif Core.Inference.is_meta_expr(ex)
10681070
continue
10691071
end
1070-
find_call(ex.args, func, narg) && return true
1072+
find_call(ex.args, func, narg, code0) && return true
10711073
end
10721074
return false
10731075
end

0 commit comments

Comments
 (0)