Skip to content

Commit

Permalink
Optimize Int8 and UInt8 boxing. (JuliaLang#36991)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyichao authored and simeonschaub committed Aug 11, 2020
1 parent 7fe031b commit 31f772f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
15 changes: 13 additions & 2 deletions src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2186,6 +2186,17 @@ static Value *as_value(jl_codectx_t &ctx, Type *to, const jl_cgval_t &v)
return emit_unbox(ctx, to, v, v.typ);
}

static Value *load_i8box(jl_codectx_t &ctx, Value *v, jl_datatype_t *ty)
{
auto jvar = ty == jl_int8_type ? jlboxed_int8_cache : jlboxed_uint8_cache;
Constant *gv = prepare_global_in(jl_Module, jvar);
Value *idx[] = {ConstantInt::get(T_int32, 0), ctx.builder.CreateZExt(v, T_int32)};
auto slot = ctx.builder.CreateInBoundsGEP(gv, idx);
return tbaa_decorate(tbaa_const, maybe_mark_load_dereferenceable(
ctx.builder.CreateAlignedLoad(T_pjlvalue, slot, sizeof(void*)), false,
(jl_value_t*)ty));
}

// some types have special boxing functions with small-value caches
// Returns T_prjlvalue
static Value *_boxed_special(jl_codectx_t &ctx, const jl_cgval_t &vinfo, Type *t)
Expand All @@ -2210,7 +2221,7 @@ static Value *_boxed_special(jl_codectx_t &ctx, const jl_cgval_t &vinfo, Type *t
assert(jl_is_datatype(jb));
Value *box = NULL;
if (jb == jl_int8_type)
box = track_pjlvalue(ctx, call_with_attrs(ctx, box_int8_func, as_value(ctx, t, vinfo)));
box = track_pjlvalue(ctx, load_i8box(ctx, as_value(ctx, t, vinfo), jb));
else if (jb == jl_int16_type)
box = call_with_attrs(ctx, box_int16_func, as_value(ctx, t, vinfo));
else if (jb == jl_int32_type)
Expand All @@ -2223,7 +2234,7 @@ static Value *_boxed_special(jl_codectx_t &ctx, const jl_cgval_t &vinfo, Type *t
// box = ctx.builder.CreateCall(box_float64_func, as_value(ctx, t, vinfo);
// for Float64, fall through to generic case below, to inline alloc & init of Float64 box. cheap, I know.
else if (jb == jl_uint8_type)
box = track_pjlvalue(ctx, call_with_attrs(ctx, box_uint8_func, as_value(ctx, t, vinfo)));
box = track_pjlvalue(ctx, load_i8box(ctx, as_value(ctx, t, vinfo), jb));
else if (jb == jl_uint16_type)
box = call_with_attrs(ctx, box_uint16_func, as_value(ctx, t, vinfo));
else if (jb == jl_uint32_type)
Expand Down
14 changes: 12 additions & 2 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,18 @@ static const auto jlgetworld_global = new JuliaVariable{
[](LLVMContext &C) { return (Type*)T_size; },
};

static const auto jlboxed_int8_cache = new JuliaVariable{
"jl_boxed_int8_cache",
true,
[](LLVMContext &C) { return (Type*)ArrayType::get(T_pjlvalue, 256); },
};

static const auto jlboxed_uint8_cache = new JuliaVariable{
"jl_boxed_uint8_cache",
true,
[](LLVMContext &C) { return (Type*)ArrayType::get(T_pjlvalue, 256); },
};

static const auto jltls_states_func = new JuliaFunction{
"julia.ptls_states",
[](LLVMContext &C) { return FunctionType::get(PointerType::get(T_ppjlvalue, 0), false); },
Expand Down Expand Up @@ -746,8 +758,6 @@ static const auto box_##ct##_func = new JuliaFunction{ \
{at}, false); }, \
attrs, \
}
BOX_FUNC(int8, T_pjlvalue, T_int8, get_attrs_sext);
BOX_FUNC(uint8, T_pjlvalue, T_int8, get_attrs_zext);
BOX_FUNC(int16, T_prjlvalue, T_int16, get_attrs_sext);
BOX_FUNC(uint16, T_prjlvalue, T_int16, get_attrs_zext);
BOX_FUNC(int32, T_prjlvalue, T_int32, get_attrs_sext);
Expand Down
12 changes: 6 additions & 6 deletions src/datatype.c
Original file line number Diff line number Diff line change
Expand Up @@ -819,15 +819,15 @@ JL_DLLEXPORT jl_value_t *jl_box_char(uint32_t x)
return v;
}

static jl_value_t *boxed_int8_cache[256];
JL_DLLEXPORT jl_value_t *jl_boxed_int8_cache[256];
JL_DLLEXPORT jl_value_t *jl_box_int8(int8_t x)
{
return boxed_int8_cache[(uint8_t)x];
return jl_boxed_int8_cache[(uint8_t)x];
}
static jl_value_t *boxed_uint8_cache[256];
JL_DLLEXPORT jl_value_t *jl_boxed_uint8_cache[256];
JL_DLLEXPORT jl_value_t *jl_box_uint8(uint8_t x)
{
return boxed_uint8_cache[x];
return jl_boxed_uint8_cache[x];
}

void jl_init_int32_int64_cache(void)
Expand All @@ -845,7 +845,7 @@ void jl_init_int32_int64_cache(void)
#endif
}
for(i=0; i < 256; i++) {
boxed_uint8_cache[i] = jl_permbox8(jl_uint8_type, i);
jl_boxed_uint8_cache[i] = jl_permbox8(jl_uint8_type, i);
}
}

Expand All @@ -856,7 +856,7 @@ void jl_init_box_caches(void)
boxed_char_cache[i] = jl_permbox32(jl_char_type, i << 24);
}
for(i=0; i < 256; i++) {
boxed_int8_cache[i] = jl_permbox8(jl_int8_type, i);
jl_boxed_int8_cache[i] = jl_permbox8(jl_int8_type, i);
}
for(i=0; i < NBOX_C; i++) {
boxed_int16_cache[i] = jl_permbox16(jl_int16_type, i-NBOX_C/2);
Expand Down

0 comments on commit 31f772f

Please sign in to comment.