Skip to content

Commit 04b391a

Browse files
committed
some IR size improvements
- serialize Method's enclosing Module more compactly - use Int32 instead of Int for location indices - use UInt8 instead of Int8 and serialize it more compactly
1 parent 8fc7a16 commit 04b391a

15 files changed

Lines changed: 84 additions & 47 deletions

File tree

base/boot.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,11 @@ end
564564

565565
import .Intrinsics: eq_int, trunc_int, lshr_int, sub_int, shl_int, bitcast, sext_int, zext_int, and_int
566566

567-
throw_inexacterror(f::Symbol, T::Type, val) = (@_noinline_meta; throw(InexactError(f, T, val)))
567+
throw_inexacterror(f::Symbol, T::Type, @nospecialize(val)) = (@_noinline_meta; throw(InexactError(f, T, val)))
568568

569569
function is_top_bit_set(x)
570570
@_inline_meta
571-
eq_int(trunc_int(Int8, lshr_int(x, sub_int(shl_int(sizeof(x), 3), 1))), trunc_int(Int8, 1))
571+
eq_int(trunc_int(UInt8, lshr_int(x, sub_int(shl_int(sizeof(x), 3), 1))), trunc_int(UInt8, 1))
572572
end
573573

574574
function is_top_bit_set(x::Union{Int8,UInt8})

base/compiler/ssair/inlining2.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector
273273
stmt = compact.result[idx]
274274
linetable_offset = length(linetable)
275275
# Append the linetable of the inlined function to our line table
276-
inlined_at = compact.result_lines[idx]
276+
inlined_at = Int(compact.result_lines[idx])
277277
for entry in item.linetable
278278
push!(linetable, LineInfoNode(entry.mod, entry.method, entry.file, entry.line,
279279
(entry.inlined_at > 0 ? entry.inlined_at + linetable_offset : inlined_at)))
@@ -958,7 +958,7 @@ function assemble_inline_todo!(ir::IRCode, linetable::Vector{LineInfoNode}, sv::
958958
todo
959959
end
960960

961-
function mk_tuplecall!(compact::IncrementalCompact, args::Vector{Any}, line_idx::Int)
961+
function mk_tuplecall!(compact::IncrementalCompact, args::Vector{Any}, line_idx::Int32)
962962
e = Expr(:call, TOP_TUPLE, args...)
963963
etyp = tuple_tfunc(Tuple{Any[widenconst(compact_exprtype(compact, args[i])) for i in 1:length(args)]...})
964964
return insert_node_here!(compact, e, etyp, line_idx)

base/compiler/ssair/ir.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ copy(n::NewNode) = copy(n.pos, n.attach_after, n.typ, copy(n.node), n.line)
164164
struct IRCode
165165
stmts::Vector{Any}
166166
types::Vector{Any}
167-
lines::Vector{Int}
167+
lines::Vector{Int32}
168168
flags::Vector{UInt8}
169169
argtypes::Vector{Any}
170170
spvals::SimpleVector
@@ -173,12 +173,12 @@ struct IRCode
173173
new_nodes::Vector{NewNode}
174174
meta::Vector{Any}
175175

176-
function IRCode(stmts::Vector{Any}, types::Vector{Any}, lines::Vector{Int}, flags::Vector{UInt8},
176+
function IRCode(stmts::Vector{Any}, types::Vector{Any}, lines::Vector{Int32}, flags::Vector{UInt8},
177177
cfg::CFG, linetable::Vector{LineInfoNode}, argtypes::Vector{Any}, meta::Vector{Any},
178178
spvals::SimpleVector)
179179
return new(stmts, types, lines, flags, argtypes, spvals, linetable, cfg, NewNode[], meta)
180180
end
181-
function IRCode(ir::IRCode, stmts::Vector{Any}, types::Vector{Any}, lines::Vector{Int}, flags::Vector{UInt8},
181+
function IRCode(ir::IRCode, stmts::Vector{Any}, types::Vector{Any}, lines::Vector{Int32}, flags::Vector{UInt8},
182182
cfg::CFG, new_nodes::Vector{NewNode})
183183
return new(stmts, types, lines, flags, ir.argtypes, ir.spvals, ir.linetable, cfg, new_nodes, ir.meta)
184184
end
@@ -414,7 +414,7 @@ mutable struct IncrementalCompact
414414
ir::IRCode
415415
result::Vector{Any}
416416
result_types::Vector{Any}
417-
result_lines::Vector{Int}
417+
result_lines::Vector{Int32}
418418
result_flags::Vector{UInt8}
419419
result_bbs::Vector{BasicBlock}
420420
ssa_rename::Vector{Any}
@@ -439,7 +439,7 @@ mutable struct IncrementalCompact
439439
new_len = length(code.stmts) + length(code.new_nodes)
440440
result = Array{Any}(undef, new_len)
441441
result_types = Array{Any}(undef, new_len)
442-
result_lines = fill(0, new_len)
442+
result_lines = fill(Int32(0), new_len)
443443
result_flags = fill(0x00, new_len)
444444
used_ssas = fill(0, new_len)
445445
ssa_rename = Any[SSAValue(i) for i = 1:new_len]
@@ -567,7 +567,7 @@ function insert_node!(compact::IncrementalCompact, before, @nospecialize(typ), @
567567
end
568568
end
569569

570-
function insert_node_here!(compact::IncrementalCompact, @nospecialize(val), @nospecialize(typ), ltable_idx::Int, reverse_affinity=false)
570+
function insert_node_here!(compact::IncrementalCompact, @nospecialize(val), @nospecialize(typ), ltable_idx::Int32, reverse_affinity::Bool=false)
571571
if compact.result_idx > length(compact.result)
572572
@assert compact.result_idx == length(compact.result) + 1
573573
resize!(compact, compact.result_idx)

base/compiler/ssair/show.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function print_node(io::IO, idx::Int, @nospecialize(stmt), used, argnames, maxsi
9292
end
9393
end
9494

95-
function compute_inlining_depth(linetable, iline::Int)
95+
function compute_inlining_depth(linetable, iline::Int32)
9696
depth = 0
9797
while iline != 0
9898
linetable[iline].inlined_at == 0 && break
@@ -120,9 +120,9 @@ function default_expr_type_printer(io::IO, @nospecialize typ)
120120
nothing
121121
end
122122

123-
function compute_loc_stack(code::IRCode, line::Int)
123+
function compute_loc_stack(code::IRCode, line::Int32)
124124
stack = []
125-
line === 0 && return stack
125+
line == 0 && return stack
126126
inlined_at = code.linetable[line].inlined_at
127127
if inlined_at != 0
128128
push!(stack, inlined_at)
@@ -214,7 +214,7 @@ function compute_ir_line_annotations(code::IRCode)
214214
lineno = 0
215215
loc_method = ""
216216
print(buf, "")
217-
if line !== 0
217+
if line != 0
218218
stack = compute_loc_stack(code, line)
219219
lineno = code.linetable[stack[1]].line
220220
x = min(length(last_stack), length(stack))

base/compiler/ssair/slot2ssa.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ function domsort_ssa!(ir::IRCode, domtree::DomTree)
426426
nstmts = sum(length(ir.cfg.blocks[i].stmts) for i in result_order if i !== 0)
427427
result_stmts = Vector{Any}(undef, nstmts + ncritbreaks + nnewfallthroughs)
428428
result_types = Any[Any for i = 1:length(result_stmts)]
429-
result_ltable = fill(0, length(result_stmts))
429+
result_ltable = fill(Int32(0), length(result_stmts))
430430
result_flags = fill(0x00, length(result_stmts))
431431
inst_rename = Vector{Any}(undef, length(ir.stmts))
432432
for i = 1:length(ir.new_nodes)

base/compiler/typeinfer.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ function typeinf_ext(linfo::MethodInstance, params::Params)
596596
tree.slotflags = fill(0x00, Int(method.nargs))
597597
tree.slottypes = nothing
598598
tree.ssavaluetypes = 0
599-
tree.codelocs = Int[1]
599+
tree.codelocs = Int32[1]
600600
tree.linetable = [LineInfoNode(method.module, method.name, method.file, Int(method.line), 0)]
601601
tree.inferred = true
602602
tree.ssaflags = UInt8[]

base/essentials.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ function append_any(xs...)
401401
_growend!(out, 16)
402402
l += 16
403403
end
404-
Core.arrayset(true, out, y, i)
404+
arrayset(true, out, y, i)
405405
i += 1
406406
end
407407
end
@@ -410,7 +410,7 @@ function append_any(xs...)
410410
end
411411

412412
# simple Array{Any} operations needed for bootstrap
413-
@eval setindex!(A::Array{Any}, @nospecialize(x), i::Int) = Core.arrayset($(Expr(:boundscheck)), A, x, i)
413+
@eval setindex!(A::Array{Any}, @nospecialize(x), i::Int) = arrayset($(Expr(:boundscheck)), A, x, i)
414414

415415
"""
416416
precompile(f, args::Tuple{Vararg{Any}})

base/pointer.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,6 @@ isless(x::Ptr{T}, y::Ptr{T}) where {T} = x < y
152152
<(x::Ptr, y::Ptr) = UInt(x) < UInt(y)
153153
-(x::Ptr, y::Ptr) = UInt(x) - UInt(y)
154154

155-
+(x::Ptr, y::Integer) = oftype(x, Intrinsics.add_ptr(UInt(x), (y % UInt) % UInt))
156-
-(x::Ptr, y::Integer) = oftype(x, Intrinsics.sub_ptr(UInt(x), (y % UInt) % UInt))
155+
+(x::Ptr, y::Integer) = oftype(x, add_ptr(UInt(x), (y % UInt) % UInt))
156+
-(x::Ptr, y::Integer) = oftype(x, sub_ptr(UInt(x), (y % UInt) % UInt))
157157
+(x::Integer, y::Ptr) = y + x

src/codegen.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3900,6 +3900,10 @@ static jl_cgval_t emit_expr(jl_codectx_t &ctx, jl_value_t *expr, ssize_t ssaval)
39003900
expr = jl_box_int64(val);
39013901
}
39023902
}
3903+
else if (jl_is_uint8(expr)) {
3904+
expr = jl_box_uint8(jl_unbox_uint8(expr));
3905+
needroot = false;
3906+
}
39033907
if (needroot && jl_is_method(ctx.linfo->def.method)) { // toplevel exprs and some integers are already rooted
39043908
jl_add_method_root(ctx, expr);
39053909
}
@@ -5964,7 +5968,7 @@ static std::unique_ptr<Module> emit_function(
59645968
}
59655969
size_t prev_loc = 0;
59665970
for (i = 0; i < stmtslen; i++) {
5967-
size_t loc = ((size_t*)jl_array_data(src->codelocs))[i];
5971+
size_t loc = ((int32_t*)jl_array_data(src->codelocs))[i];
59685972
StmtProp &cur_prop = stmtprops[i];
59695973
cur_prop.is_poploc = false;
59705974
if (loc > 0) {

src/dump.c

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ static const intptr_t LongPhic_tag = 27;
7878
static const intptr_t LiteralVal_tag = 28;
7979
static const intptr_t SmallInt64_tag = 29;
8080
static const intptr_t SmallDataType_tag= 30;
81-
static const intptr_t Int32_tag = 31;
8281
static const intptr_t Array1d_tag = 32;
8382
static const intptr_t Singleton_tag = 33;
8483
static const intptr_t CommonSym_tag = 34;
8584
static const intptr_t NearbyGlobal_tag = 35; // a GlobalRef pointing to tree_enclosing_module
8685
static const intptr_t CoreMod_tag = 36;
8786
static const intptr_t BaseMod_tag = 37;
8887
static const intptr_t BITypeName_tag = 38; // builtin TypeName
88+
static const intptr_t NearbyModule_tag = 39;
8989
static const intptr_t Null_tag = 253;
9090
static const intptr_t ShortBackRef_tag = 254;
9191
static const intptr_t BackRef_tag = 255;
@@ -442,9 +442,8 @@ static int is_ast_node(jl_value_t *v)
442442
return jl_is_symbol(v) || jl_is_slot(v) || jl_is_ssavalue(v) ||
443443
jl_is_uniontype(v) || jl_is_expr(v) || jl_is_newvarnode(v) ||
444444
jl_is_svec(v) || jl_is_tuple(v) || ((jl_datatype_t*)jl_typeof(v))->instance ||
445-
jl_is_int32(v) || jl_is_int64(v) || jl_is_bool(v) ||
446-
jl_is_quotenode(v) || jl_is_gotonode(v) ||
447-
jl_is_linenode(v) || jl_is_globalref(v) ||
445+
jl_is_int32(v) || jl_is_int64(v) || jl_is_bool(v) || jl_is_uint8(v) ||
446+
jl_is_quotenode(v) || jl_is_gotonode(v) || jl_is_linenode(v) || jl_is_globalref(v) ||
448447
jl_is_phinode(v) || jl_is_phicnode(v) || jl_is_upsilonnode(v) || jl_is_pinode(v) ||
449448
jl_typeis(v, jl_lineinfonode_type);
450449
}
@@ -491,6 +490,10 @@ static void jl_serialize_value_(jl_serializer_state *s, jl_value_t *v, int as_li
491490
writetag(s->s, (jl_value_t*)BaseMod_tag);
492491
return;
493492
}
493+
else if (v == (jl_value_t*)s->tree_enclosing_module) {
494+
writetag(s->s, (jl_value_t*)NearbyModule_tag);
495+
return;
496+
}
494497
else if (!as_literal && !is_ast_node(v)) {
495498
writetag(s->s, (jl_value_t*)LiteralVal_tag);
496499
int id = literal_val_id(s, v);
@@ -499,7 +502,7 @@ static void jl_serialize_value_(jl_serializer_state *s, jl_value_t *v, int as_li
499502
return;
500503
}
501504
}
502-
else {
505+
else if (!jl_is_uint8(v)) {
503506
bp = ptrhash_bp(&backref_table, v);
504507
if (*bp != HT_NOTFOUND) {
505508
uintptr_t pos = (char*)*bp - (char*)HT_NOTFOUND - 1;
@@ -804,19 +807,29 @@ static void jl_serialize_value_(jl_serializer_state *s, jl_value_t *v, int as_li
804807
te = te->next;
805808
}
806809
}
807-
else {
808-
jl_datatype_t *t = (jl_datatype_t*)jl_typeof(v);
810+
else if (jl_typeis(v, jl_int64_type)) {
809811
void *data = jl_data_ptr(v);
810-
if (t == jl_int64_type &&
811-
*(int64_t*)data >= S32_MIN && *(int64_t*)data <= S32_MAX) {
812+
if (*(int64_t*)data >= S32_MIN && *(int64_t*)data <= S32_MAX) {
812813
writetag(s->s, (jl_value_t*)SmallInt64_tag);
813814
write_int32(s->s, (int32_t)*(int64_t*)data);
814815
}
815-
else if (t == jl_int32_type) {
816-
writetag(s->s, (jl_value_t*)Int32_tag);
817-
write_int32(s->s, (int32_t)*(int32_t*)data);
818-
}
819816
else {
817+
writetag(s->s, (jl_value_t*)jl_int64_type);
818+
write_int64(s->s, *(int64_t*)data);
819+
}
820+
}
821+
else if (jl_typeis(v, jl_int32_type)) {
822+
writetag(s->s, (jl_value_t*)jl_int32_type);
823+
write_int32(s->s, *(int32_t*)jl_data_ptr(v));
824+
}
825+
else if (jl_typeis(v, jl_uint8_type)) {
826+
writetag(s->s, (jl_value_t*)jl_uint8_type);
827+
write_int8(s->s, *(int8_t*)jl_data_ptr(v));
828+
}
829+
else {
830+
jl_datatype_t *t = (jl_datatype_t*)jl_typeof(v);
831+
void *data = jl_data_ptr(v);
832+
{
820833
if (v == t->instance) {
821834
if (s->mode == MODE_MODULE && !type_in_worklist(t)) {
822835
// also flag this in the backref table as special
@@ -1948,17 +1961,30 @@ static jl_value_t *jl_deserialize_value_(jl_serializer_state *s, jl_value_t *vta
19481961
arraylist_push(&backref_list, v);
19491962
return v;
19501963
}
1951-
else if (vtag == (jl_value_t*)Int32_tag) {
1964+
else if (vtag == (jl_value_t*)jl_int64_type) {
1965+
jl_value_t *v = jl_box_int64((int64_t)read_uint64(s->s));
1966+
if (usetable)
1967+
arraylist_push(&backref_list, v);
1968+
return v;
1969+
}
1970+
else if (vtag == (jl_value_t*)jl_int32_type) {
19521971
jl_value_t *v = jl_box_int32(read_int32(s->s));
19531972
if (usetable)
19541973
arraylist_push(&backref_list, v);
19551974
return v;
19561975
}
1976+
else if (vtag == (jl_value_t*)jl_uint8_type) {
1977+
return jl_box_uint8(read_uint8(s->s));
1978+
}
19571979
else if (vtag == (jl_value_t*)NearbyGlobal_tag) {
19581980
assert(s->tree_enclosing_module != NULL);
19591981
jl_value_t *sym = jl_deserialize_value(s, NULL);
19601982
return jl_module_globalref(s->tree_enclosing_module, (jl_sym_t*)sym);
19611983
}
1984+
else if (vtag == (jl_value_t*)NearbyModule_tag) {
1985+
assert(s->tree_enclosing_module != NULL);
1986+
return (jl_value_t*)s->tree_enclosing_module;
1987+
}
19621988
else if (vtag == (jl_value_t*)jl_globalref_type) {
19631989
return jl_deserialize_value_globalref(s);
19641990
}
@@ -2952,10 +2978,11 @@ void jl_init_serializer(void)
29522978
(void*)LongExpr_tag, (void*)LongPhi_tag, (void*)LongPhic_tag,
29532979
(void*)LiteralVal_tag, jl_string_type,
29542980
(void*)SmallInt64_tag, (void*)SmallDataType_tag, jl_typemap_entry_type,
2955-
(void*)Int32_tag, (void*)Array1d_tag, (void*)Singleton_tag,
2981+
(void*)Array1d_tag, (void*)Singleton_tag,
29562982
jl_module_type, jl_tvar_type, jl_method_instance_type, jl_method_type,
29572983
(void*)CommonSym_tag, (void*)NearbyGlobal_tag, jl_globalref_type,
29582984
(void*)CoreMod_tag, (void*)BaseMod_tag, (void*)BITypeName_tag,
2985+
(void*)NearbyModule_tag, jl_int32_type, jl_int64_type, jl_uint8_type,
29592986
// everything above here represents a class of object rather than only a literal
29602987

29612988
jl_emptysvec, jl_emptytuple, jl_false, jl_true, jl_nothing, jl_any_type,
@@ -2986,10 +3013,9 @@ void jl_init_serializer(void)
29863013
jl_box_int64(18), jl_box_int64(19), jl_box_int64(20),
29873014
jl_box_int64(21), jl_box_int64(22), jl_box_int64(23),
29883015
jl_box_int64(24), jl_box_int64(25), jl_box_int64(26),
2989-
jl_box_int64(27), jl_box_int64(28),
3016+
jl_box_int64(27),
29903017

2991-
jl_bool_type, jl_int32_type, jl_int64_type,
2992-
jl_gotonode_type, jl_linenumbernode_type, jl_lineinfonode_type,
3018+
jl_bool_type, jl_gotonode_type, jl_linenumbernode_type, jl_lineinfonode_type,
29933019
jl_quotenode_type, jl_pinode_type, jl_upsilonnode_type,
29943020
jl_type_type, jl_bottom_type, jl_ref_type,
29953021
jl_pointer_type, jl_vararg_type, jl_abstractarray_type, jl_void_type,
@@ -3000,7 +3026,7 @@ void jl_init_serializer(void)
30003026
jl_voidpointer_type, jl_newvarnode_type, jl_abstractstring_type,
30013027
jl_array_symbol_type, jl_anytuple_type, jl_tparam0(jl_anytuple_type),
30023028
jl_emptytuple_type, jl_array_uint8_type, jl_code_info_type,
3003-
jl_typeofbottom_type, jl_namedtuple_type, jl_array_int_type,
3029+
jl_typeofbottom_type, jl_namedtuple_type, jl_array_int32_type,
30043030

30053031
ptls->root_task,
30063032

0 commit comments

Comments
 (0)