Skip to content

Commit 36d1694

Browse files
Merge pull request #8867 from JuliaLang/sjk/tbaa
Use tbaa for heap-allocated immutables
2 parents d43dfa8 + 9a9bf1f commit 36d1694

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

src/cgutils.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ static Value *emit_nthptr_recast(Value *v, size_t n, MDNode *tbaa, Type* ptype)
865865
static Value *ghostValue(jl_value_t *ty);
866866

867867
static Value *typed_load(Value *ptr, Value *idx_0based, jl_value_t *jltype,
868-
jl_codectx_t *ctx)
868+
jl_codectx_t *ctx, MDNode* tbaa)
869869
{
870870
Type *elty = julia_type_to_llvm(jltype);
871871
assert(elty != NULL);
@@ -878,7 +878,7 @@ static Value *typed_load(Value *ptr, Value *idx_0based, jl_value_t *jltype,
878878
data = builder.CreateBitCast(ptr, PointerType::get(elty, 0));
879879
else
880880
data = ptr;
881-
Value *elt = tbaa_decorate(tbaa_user, builder.CreateLoad(builder.CreateGEP(data, idx_0based), false));
881+
Value *elt = tbaa_decorate(tbaa, builder.CreateLoad(builder.CreateGEP(data, idx_0based), false));
882882
if (elty == jl_pvalue_llvmt) {
883883
null_pointer_check(elt, ctx);
884884
}
@@ -890,7 +890,7 @@ static Value *typed_load(Value *ptr, Value *idx_0based, jl_value_t *jltype,
890890
static Value *emit_unbox(Type *to, Value *x, jl_value_t *jt);
891891

892892
static void typed_store(Value *ptr, Value *idx_0based, Value *rhs,
893-
jl_value_t *jltype, jl_codectx_t *ctx)
893+
jl_value_t *jltype, jl_codectx_t *ctx, MDNode* tbaa)
894894
{
895895
Type *elty = julia_type_to_llvm(jltype);
896896
assert(elty != NULL);
@@ -906,7 +906,7 @@ static void typed_store(Value *ptr, Value *idx_0based, Value *rhs,
906906
data = builder.CreateBitCast(ptr, PointerType::get(elty, 0));
907907
else
908908
data = ptr;
909-
tbaa_decorate(tbaa_user, builder.CreateStore(rhs, builder.CreateGEP(data, idx_0based)));
909+
tbaa_decorate(tbaa, builder.CreateStore(rhs, builder.CreateGEP(data, idx_0based)));
910910
}
911911

912912
// --- convert boolean value to julia ---

src/codegen.cpp

+10-7
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ static Type *T_void;
240240
// type-based alias analysis nodes. Indentation of comments indicates hierarchy.
241241
static MDNode* tbaa_user; // User data
242242
static MDNode* tbaa_value; // Julia value
243+
static MDNode* tbaa_immut; // Data inside a heap-allocated immutable
243244
static MDNode* tbaa_array; // Julia array
244245
static MDNode* tbaa_arrayptr; // The pointer inside a jl_array_t
245246
static MDNode* tbaa_arraysize; // A size in a jl_array_t
@@ -1487,13 +1488,14 @@ static Value *emit_getfield(jl_value_t *expr, jl_sym_t *name, jl_codectx_t *ctx)
14871488
ConstantInt::get(T_size,
14881489
sty->fields[idx].offset + sizeof(void*)));
14891490
JL_GC_POP();
1491+
MDNode *tbaa = sty->mutabl ? tbaa_user : tbaa_immut;
14901492
if (sty->fields[idx].isptr) {
1491-
Value *fldv = builder.CreateLoad(builder.CreateBitCast(addr,jl_ppvalue_llvmt));
1493+
Value *fldv = tbaa_decorate(tbaa, builder.CreateLoad(builder.CreateBitCast(addr,jl_ppvalue_llvmt)));
14921494
null_pointer_check(fldv, ctx);
14931495
return fldv;
14941496
}
14951497
else {
1496-
return typed_load(addr, ConstantInt::get(T_size, 0), jfty, ctx);
1498+
return typed_load(addr, ConstantInt::get(T_size, 0), jfty, ctx, tbaa);
14971499
}
14981500
}
14991501
else {
@@ -1551,7 +1553,7 @@ static void emit_setfield(jl_datatype_t *sty, Value *strct, size_t idx,
15511553
builder.CreateBitCast(addr, jl_ppvalue_llvmt));
15521554
}
15531555
else {
1554-
typed_store(addr, ConstantInt::get(T_size, 0), rhs, jfty, ctx);
1556+
typed_store(addr, ConstantInt::get(T_size, 0), rhs, jfty, ctx, sty->mutabl ? tbaa_user : tbaa_immut);
15551557
}
15561558
}
15571559
else {
@@ -2064,7 +2066,7 @@ static Value *emit_known_call(jl_value_t *ff, jl_value_t **args, size_t nargs,
20642066
assert(((jl_datatype_t*)ety)->instance != NULL);
20652067
return literal_pointer_val(((jl_datatype_t*)ety)->instance);
20662068
}
2067-
return typed_load(emit_arrayptr(ary, args[1], ctx), idx, ety, ctx);
2069+
return typed_load(emit_arrayptr(ary, args[1], ctx), idx, ety, ctx, tbaa_user);
20682070
}
20692071
}
20702072
}
@@ -2097,7 +2099,7 @@ static Value *emit_known_call(jl_value_t *ff, jl_value_t **args, size_t nargs,
20972099
else {
20982100
typed_store(emit_arrayptr(ary,args[1],ctx), idx,
20992101
ety==(jl_value_t*)jl_any_type ? emit_expr(args[2],ctx) : emit_unboxed(args[2],ctx),
2100-
ety, ctx);
2102+
ety, ctx, tbaa_user);
21012103
}
21022104
JL_GC_POP();
21032105
return ary;
@@ -2152,7 +2154,7 @@ static Value *emit_known_call(jl_value_t *ff, jl_value_t **args, size_t nargs,
21522154
idx = emit_bounds_check(idx, ConstantInt::get(T_size, nfields), ctx);
21532155
Value *ptr = data_pointer(strct);
21542156
JL_GC_POP();
2155-
return typed_load(ptr, idx, jt, ctx);
2157+
return typed_load(ptr, idx, jt, ctx, stt->mutabl ? tbaa_user : tbaa_immut);
21562158
}
21572159
else {
21582160
idx = builder.CreateSub(idx, ConstantInt::get(T_size, 1));
@@ -2179,7 +2181,7 @@ static Value *emit_known_call(jl_value_t *ff, jl_value_t **args, size_t nargs,
21792181
jl_value_t *jt = jl_t0(stt->types);
21802182
idx = emit_bounds_check(idx, ConstantInt::get(T_size, nfields), ctx);
21812183
Value *ptr = builder.CreateGEP(tempSpace, ConstantInt::get(T_size, 0));
2182-
fld = typed_load(ptr, idx, jt, ctx);
2184+
fld = typed_load(ptr, idx, jt, ctx, stt->mutabl ? tbaa_user : tbaa_immut);
21832185
builder.CreateCall(Intrinsic::getDeclaration(jl_Module,Intrinsic::stackrestore),
21842186
stacksave);
21852187
}
@@ -4188,6 +4190,7 @@ static void init_julia_llvm_env(Module *m)
41884190
MDNode* tbaa_root = mbuilder->createTBAARoot("jtbaa");
41894191
tbaa_user = tbaa_make_child("jtbaa_user",tbaa_root);
41904192
tbaa_value = tbaa_make_child("jtbaa_value",tbaa_root);
4193+
tbaa_immut = tbaa_make_child("jtbaa_immut",tbaa_root);
41914194
tbaa_array = tbaa_make_child("jtbaa_array",tbaa_value);
41924195
tbaa_arrayptr = tbaa_make_child("jtbaa_arrayptr",tbaa_array);
41934196
tbaa_arraysize = tbaa_make_child("jtbaa_arraysize",tbaa_array);

src/intrinsics.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ static Value *emit_pointerref(jl_value_t *e, jl_value_t *i, jl_codectx_t *ctx)
744744
thePtr, size, 1);
745745
return mark_julia_type(strct, ety);
746746
}
747-
return typed_load(thePtr, im1, ety, ctx);
747+
return typed_load(thePtr, im1, ety, ctx, tbaa_user);
748748
}
749749

750750
static Value *emit_runtime_pointerset(jl_value_t *e, jl_value_t *x, jl_value_t *i, jl_codectx_t *ctx)
@@ -803,7 +803,7 @@ static Value *emit_pointerset(jl_value_t *e, jl_value_t *x, jl_value_t *i, jl_co
803803
else
804804
val = emit_unboxed(x,ctx);
805805
}
806-
typed_store(thePtr, im1, val, ety, ctx);
806+
typed_store(thePtr, im1, val, ety, ctx, tbaa_user);
807807
}
808808
return mark_julia_type(thePtr, aty);
809809
}

0 commit comments

Comments
 (0)