Skip to content

Commit

Permalink
Fix building error & add alias scope tests
Browse files Browse the repository at this point in the history
Co-authored-by: Valentin Churavy <vchuravy@users.noreply.github.com>
  • Loading branch information
YingboMa and vchuravy committed Feb 9, 2019
1 parent cf41dd4 commit 9a0c203
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 26 deletions.
5 changes: 5 additions & 0 deletions src/builtins.c
Expand Up @@ -1059,6 +1059,11 @@ JL_CALLABLE(jl_f_arrayref)
return jl_arrayref(a, i);
}

JL_CALLABLE(jl_f_const_arrayref)
{
return jl_f_arrayref(F, args, nargs);
}

JL_CALLABLE(jl_f_arrayset)
{
JL_NARGSV(arrayset, 4);
Expand Down
6 changes: 3 additions & 3 deletions src/cgutils.cpp
Expand Up @@ -1239,9 +1239,9 @@ static Value *emit_bounds_check(jl_codectx_t &ctx, const jl_cgval_t &ainfo, jl_v

static Value *emit_unbox(jl_codectx_t &ctx, Type *to, const jl_cgval_t &x, jl_value_t *jt, Value* dest = NULL, MDNode *tbaa_dest = nullptr, bool isVolatile = false);

static jl_cgval_t typed_load(jl_codectx_t *ctx, Value *ptr, Value *idx_0based, jl_value_t *jltype,
MDNode *tbaa, MDNode *aliasscope,
bool maybe_null_if_boxed, unsigned alignment = 0)
static jl_cgval_t typed_load(jl_codectx_t &ctx, Value *ptr, Value *idx_0based, jl_value_t *jltype,
MDNode *tbaa, MDNode *aliasscope,
bool maybe_null_if_boxed = true, unsigned alignment = 0)
{
bool isboxed;
Type *elty = julia_type_to_llvm(jltype, &isboxed);
Expand Down
55 changes: 34 additions & 21 deletions src/codegen.cpp
Expand Up @@ -2574,7 +2574,7 @@ static bool emit_builtin_call(jl_codectx_t &ctx, jl_cgval_t *ret, jl_value_t *f,
*ret = mark_julia_slot(lv, ety, ctx.builder.CreateNUWAdd(ConstantInt::get(T_int8, 1), tindex), tbaa_arraybuf);
}
else {
MDNode *aliasscope = (f == jl_builtin_const_arrayref) ? ctx->aliasscope : nullptr;
MDNode *aliasscope = (f == jl_builtin_const_arrayref) ? ctx.aliasscope : nullptr;
*ret = typed_load(ctx,
emit_arrayptr(ctx, ary, ary_ex),
idx, ety,
Expand Down Expand Up @@ -2679,7 +2679,7 @@ static bool emit_builtin_call(jl_codectx_t &ctx, jl_cgval_t *ret, jl_value_t *f,
emit_arrayptr(ctx, ary, ary_ex, isboxed),
idx, val, ety,
!isboxed ? tbaa_arraybuf : tbaa_ptrarraybuf,
ctx->aliasscope, data_owner, 0);
ctx.aliasscope, data_owner, 0);
}
}
*ret = ary;
Expand Down Expand Up @@ -2762,7 +2762,7 @@ static bool emit_builtin_call(jl_codectx_t &ctx, jl_cgval_t *ret, jl_value_t *f,
jl_true);
}
Value *ptr = maybe_decay_tracked(data_pointer(ctx, obj));
*ret = typed_load(ctx, ptr, vidx, jt, obj.tbaa, false);
*ret = typed_load(ctx, ptr, vidx, jt, obj.tbaa, nullptr, false);
return true;
}
}
Expand Down Expand Up @@ -5933,17 +5933,12 @@ static std::unique_ptr<Module> emit_function(
ssize_t line;
bool is_user_code;
unsigned inlined_at;
MDNode *scope_list;
};
std::vector<DebugLineTable> linetable;
std::vector<Metadata*> scope_stack;
std::vector<MDNode*> scope_list_stack;
{
size_t nlocs = jl_array_len(src->linetable);
std::map<std::tuple<StringRef, StringRef>, DISubprogram*> subprograms;
linetable.resize(nlocs + 1);
static MDBuilder *mbuilder = new MDBuilder(jl_LLVMContext);
MDNode *alias_domain = mbuilder->createAliasScopeDomain(ctx.name);
for (size_t i = 0; i < nlocs; i++) {
// LineInfoNode(mod::Module, method::Symbol, file::Symbol, line::Int, inlined_at::Int)
jl_value_t *locinfo = jl_array_ptr_ref(src->linetable, i);
Expand Down Expand Up @@ -5981,19 +5976,39 @@ static std::unique_ptr<Module> emit_function(
DebugLoc inl_loc = (info.inlined_at == 0) ? DebugLoc::get(0, 0, SP, NULL) : linetable.at(info.inlined_at).loc;
info.loc = DebugLoc::get(info.line, 0, inl_SP, inl_loc);
}
} else if (expr->head == aliasscope_sym) {
MDNode *scope = mbuilder->createAliasScope("aliasscope", alias_domain);
scope_stack.push_back(scope);
MDNode *scope_list = MDNode::get(jl_LLVMContext, ArrayRef<Metadata*>(scope_stack), MDNode::FL_Yes);
scope_list_stack.push_back(scope_list);
info.scope_list = scope_list;
} else if (expr->head == popaliasscope_sym) {
scope_stack.pop_back();
scope_list_stack.pop_back();
info.scope_list = scope_list_stack.back();
}
}
}

std::vector<MDNode*> aliasscopes;
MDNode* current_aliasscope = nullptr;
std::vector<Metadata*> scope_stack;
std::vector<MDNode*> scope_list_stack;
{
size_t nstmts = jl_array_len(src->code);
aliasscopes.resize(nstmts + 1, nullptr);
static MDBuilder *mbuilder = new MDBuilder(jl_LLVMContext);
MDNode *alias_domain = mbuilder->createAliasScopeDomain(ctx.name);
for (i = 0; i < nstmts; i++) {
jl_value_t *stmt = jl_array_ptr_ref(stmts, i);
jl_expr_t *expr = jl_is_expr(stmt) ? (jl_expr_t*)stmt : nullptr;
if (expr) {
if (expr->head == aliasscope_sym) {
MDNode *scope = mbuilder->createAliasScope("aliasscope", alias_domain);
scope_stack.push_back(scope);
MDNode *scope_list = MDNode::get(jl_LLVMContext, ArrayRef<Metadata*>(scope_stack));
scope_list_stack.push_back(scope_list);
current_aliasscope = scope_list;
} else if (expr->head == popaliasscope_sym) {
scope_stack.pop_back();
scope_list_stack.pop_back();
current_aliasscope = scope_list_stack.back();
}
}
aliasscopes[i+1] = current_aliasscope;
}
}

Instruction &prologue_end = ctx.builder.GetInsertBlock()->back();


Expand Down Expand Up @@ -6136,10 +6151,8 @@ static std::unique_ptr<Module> emit_function(
if (ctx.debug_enabled)
ctx.builder.SetCurrentDebugLocation(linetable[debuginfoloc].loc);
coverageVisitStmt(debuginfoloc);
ctx.aliasscope = linetable[debuginfoloc].scope_list;
} else {
ctx.aliasscope = nullptr;
}
ctx.aliasscope = aliasscopes[cursor];
jl_value_t *stmt = jl_array_ptr_ref(stmts, cursor);
jl_expr_t *expr = jl_is_expr(stmt) ? (jl_expr_t*)stmt : nullptr;
if (expr && expr->head == unreachable_sym) {
Expand Down
4 changes: 2 additions & 2 deletions src/intrinsics.cpp
Expand Up @@ -601,7 +601,7 @@ static jl_cgval_t emit_pointerref(jl_codectx_t &ctx, jl_cgval_t *argv)
Type *ptrty = julia_type_to_llvm(ety, &isboxed);
assert(!isboxed);
Value *thePtr = emit_unbox(ctx, ptrty->getPointerTo(), e, e.typ);
return typed_load(ctx, thePtr, im1, ety, tbaa_data, true, align_nb);
return typed_load(ctx, thePtr, im1, ety, tbaa_data, nullptr, true, align_nb);
}
}

Expand Down Expand Up @@ -664,7 +664,7 @@ static jl_cgval_t emit_pointerset(jl_codectx_t &ctx, jl_cgval_t *argv)
Type *ptrty = julia_type_to_llvm(ety, &isboxed);
assert(!isboxed);
thePtr = emit_unbox(ctx, ptrty->getPointerTo(), e, e.typ);
typed_store(ctx, thePtr, im1, x, ety, tbaa_data, NULL, align_nb);
typed_store(ctx, thePtr, im1, x, ety, tbaa_data, nullptr, nullptr, align_nb);
}
return mark_julia_type(ctx, thePtr, false, aty);
}
Expand Down
30 changes: 30 additions & 0 deletions test/compiler/codegen.jl
Expand Up @@ -334,3 +334,33 @@ mktemp() do f_22330, _
write(f_22330, str_22330)
@test success(`$(Base.julia_cmd()) --startup-file=no $f_22330`)
end

# Alias scope
macro aliasscope(body)
sym = gensym()
esc(quote
$(Expr(:aliasscope))
$sym = $body
$(Expr(:popaliasscope))
$sym
end)
end

struct Const{T<:Array}
a::T
end

@eval Base.getindex(A::Const, i1::Int) = Core.const_arrayref($(Expr(:boundscheck)), A.a, i1)
@eval Base.getindex(A::Const, i1::Int, i2::Int, I::Int...) = (Base.@_inline_meta; Core.const_arrayref($(Expr(:boundscheck)), A.a, i1, i2, I...))

function foo31018!(a, b)
@aliasscope for i in eachindex(a, b)
a[i] = Const(b)[i]
end
end
io = IOBuffer()
code_llvm(io, foo31018!, Tuple{Vector{Int}, Vector{Int}}, optimize=false, raw=true, dump_module=true)
str = String(take!(io))
@test occursin("alias.scope", str)
@test occursin("aliasscope", str)
@test occursin("noalias", str)

0 comments on commit 9a0c203

Please sign in to comment.