Skip to content

Commit 1b37a2f

Browse files
committed
ircode: avoid serializing ssaflags in the common case when they are all zero
When not all-zero, run-length encoding would also probably be great here for lowered code (before inference).
1 parent 42e14d6 commit 1b37a2f

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

Compiler/test/interpreter_exec.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ let m = Meta.@lower 1 + 1
2323
]
2424
nstmts = length(src.code)
2525
src.ssavaluetypes = nstmts
26-
src.ssaflags = fill(UInt8(0x00), nstmts)
26+
src.ssaflags = fill(zero(UInt32), nstmts)
2727
src.debuginfo = Core.DebugInfo(:none)
2828
Compiler.verify_ir(Compiler.inflate_ir(src))
2929
global test29262 = true
@@ -63,7 +63,7 @@ let m = Meta.@lower 1 + 1
6363
]
6464
nstmts = length(src.code)
6565
src.ssavaluetypes = nstmts
66-
src.ssaflags = fill(UInt8(0x00), nstmts)
66+
src.ssaflags = fill(zero(UInt32), nstmts)
6767
src.debuginfo = Core.DebugInfo(:none)
6868
m.args[1] = copy(src)
6969
Compiler.verify_ir(Compiler.inflate_ir(src))
@@ -103,7 +103,7 @@ let m = Meta.@lower 1 + 1
103103
]
104104
nstmts = length(src.code)
105105
src.ssavaluetypes = nstmts
106-
src.ssaflags = fill(UInt8(0x00), nstmts)
106+
src.ssaflags = fill(zero(UInt32), nstmts)
107107
src.debuginfo = Core.DebugInfo(:none)
108108
Compiler.verify_ir(Compiler.inflate_ir(src))
109109
global test29262 = true

src/ircode.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,8 @@ static void jl_encode_value_(jl_ircode_state *s, jl_value_t *v, int as_literal)
549549

550550
static jl_code_info_flags_t code_info_flags(uint8_t propagate_inbounds, uint8_t has_fcall,
551551
uint8_t nospecializeinfer, uint8_t isva,
552-
uint8_t inlining, uint8_t constprop, uint8_t nargsmatchesmethod)
552+
uint8_t inlining, uint8_t constprop, uint8_t nargsmatchesmethod,
553+
jl_array_t *ssaflags)
553554
{
554555
jl_code_info_flags_t flags;
555556
flags.bits.propagate_inbounds = propagate_inbounds;
@@ -559,6 +560,11 @@ static jl_code_info_flags_t code_info_flags(uint8_t propagate_inbounds, uint8_t
559560
flags.bits.inlining = inlining;
560561
flags.bits.constprop = constprop;
561562
flags.bits.nargsmatchesmethod = nargsmatchesmethod;
563+
flags.bits.has_ssaflags = 0;
564+
const uint32_t *ssaflag_data = jl_array_data(ssaflags, uint32_t);
565+
for (size_t i = 0, l = jl_array_dim0(ssaflags); i < l; i++)
566+
if (ssaflag_data[i])
567+
flags.bits.has_ssaflags = 1;
562568
return flags;
563569
}
564570

@@ -1033,7 +1039,8 @@ JL_DLLEXPORT jl_string_t *jl_compress_ir(jl_method_t *m, jl_code_info_t *code)
10331039
jl_code_info_flags_t flags = code_info_flags(code->propagate_inbounds, code->has_fcall,
10341040
code->nospecializeinfer, code->isva,
10351041
code->inlining, code->constprop,
1036-
nargsmatchesmethod);
1042+
nargsmatchesmethod,
1043+
code->ssaflags);
10371044
write_uint16(s.s, checked_size(flags.packed, IR_DATASIZE_FLAGS));
10381045
write_uint16(s.s, checked_size(code->purity.bits, IR_DATASIZE_PURITY));
10391046
write_uint16(s.s, checked_size(code->inlining_cost, IR_DATASIZE_INLINING_COST));
@@ -1060,7 +1067,11 @@ JL_DLLEXPORT jl_string_t *jl_compress_ir(jl_method_t *m, jl_code_info_t *code)
10601067
}
10611068
s.ssaid = 0;
10621069
jl_encode_value_(&s, (jl_value_t*)code->ssavaluetypes, 1);
1063-
jl_encode_value_(&s, (jl_value_t*)code->ssaflags, 1);
1070+
assert(jl_typetagis(code->ssaflags, jl_array_uint32_type));
1071+
assert(jl_array_dim0(code->ssaflags) == l);
1072+
const uint32_t *ssaflags_data = jl_array_data(code->ssaflags, uint32_t);
1073+
if (flags.bits.has_ssaflags)
1074+
ios_write(s.s, (const char*)ssaflags_data, l * sizeof(*ssaflags_data));
10641075

10651076
// For opaque closure, also save the slottypes. We technically only need the first slot type,
10661077
// but this is simpler for now. We may want to refactor where this gets stored in the future.
@@ -1139,18 +1150,23 @@ JL_DLLEXPORT jl_code_info_t *jl_uncompress_ir(jl_method_t *m, jl_code_instance_t
11391150
code->nargs = read_int32(s.s);
11401151
}
11411152

1142-
size_t i, n = read_uint64(s.s);
1143-
code->code = jl_alloc_array_1d(jl_array_any_type, n);
1153+
size_t i, l = read_uint64(s.s);
1154+
code->code = jl_alloc_array_1d(jl_array_any_type, l);
11441155
jl_gc_wb(code, code->code);
1145-
for (i = 0; i < n; i++) {
1156+
for (i = 0; i < l; i++) {
11461157
s.ssaid = i;
11471158
jl_array_ptr_set(code->code, i, jl_decode_value(&s));
11481159
}
11491160
s.ssaid = 0;
11501161
code->ssavaluetypes = jl_decode_value(&s);
11511162
jl_gc_wb(code, code->ssavaluetypes);
1152-
code->ssaflags = (jl_array_t*)jl_decode_value(&s);
1163+
code->ssaflags = jl_alloc_array_1d(jl_array_uint32_type, l);
11531164
jl_gc_wb(code, code->ssaflags);
1165+
uint32_t *ssaflags_data = jl_array_data(code->ssaflags, uint32_t);
1166+
if (flags.bits.has_ssaflags)
1167+
ios_readall(s.s, (char*)ssaflags_data, l * sizeof(*ssaflags_data));
1168+
else
1169+
memset(ssaflags_data, 0, l * sizeof(*ssaflags_data));
11541170

11551171
if (m->is_for_opaque_closure) {
11561172
code->slottypes = jl_decode_value(&s);

src/julia_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ typedef struct {
651651
uint16_t nargsmatchesmethod:1;
652652
uint16_t inlining:2; // 0 = use heuristic; 1 = aggressive; 2 = none
653653
uint16_t constprop:2; // 0 = use heuristic; 1 = aggressive; 2 = none
654+
uint16_t has_ssaflags:1;
654655
} jl_code_info_flags_bitfield_t;
655656

656657
typedef union {

0 commit comments

Comments
 (0)