Skip to content

Commit

Permalink
Elide boundscheck blocks when inbounds.
Browse files Browse the repository at this point in the history
  • Loading branch information
blakejohnson committed Dec 23, 2015
1 parent 8ba1d6c commit a6f6519
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1332,12 +1332,21 @@ static void emit_typecheck(const jl_cgval_t &x, jl_value_t *type, const std::str
builder.SetInsertPoint(passBB);
}

static bool is_inbounds(jl_codectx_t *ctx)
{
// inbounds rule is either of top two values on inbounds stack are true
bool inbounds = !ctx->inbounds.empty() && ctx->inbounds.back();
if (ctx->inbounds.size() > 1)
inbounds ^= ctx->inbounds[ctx->inbounds.size()-2];
return inbounds;
}

#define CHECK_BOUNDS 1
static Value *emit_bounds_check(const jl_cgval_t &ainfo, jl_value_t *ty, Value *i, Value *len, jl_codectx_t *ctx)
{
Value *im1 = builder.CreateSub(i, ConstantInt::get(T_size, 1));
#if CHECK_BOUNDS==1
if (((ctx->boundsCheck.empty() || ctx->boundsCheck.back()==true) &&
if ((!is_inbounds(ctx) &&
jl_options.check_bounds != JL_OPTIONS_CHECK_BOUNDS_OFF) ||
jl_options.check_bounds == JL_OPTIONS_CHECK_BOUNDS_ON) {
Value *ok = builder.CreateICmpULT(im1, len);
Expand Down Expand Up @@ -1842,7 +1851,7 @@ static Value *emit_array_nd_index(const jl_cgval_t &ainfo, jl_value_t *ex, size_
Value *i = ConstantInt::get(T_size, 0);
Value *stride = ConstantInt::get(T_size, 1);
#if CHECK_BOUNDS==1
bool bc = ((ctx->boundsCheck.empty() || ctx->boundsCheck.back()==true) &&
bool bc = (!is_inbounds(ctx) &&
jl_options.check_bounds != JL_OPTIONS_CHECK_BOUNDS_OFF) ||
jl_options.check_bounds == JL_OPTIONS_CHECK_BOUNDS_ON;
BasicBlock *failBB=NULL, *endBB=NULL;
Expand Down
11 changes: 9 additions & 2 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4432,8 +4432,8 @@ static void emit_function(jl_lambda_info_t *lam, jl_llvm_functions_t *declaratio
ctx.funcName = jl_symbol_name(lam->name);
ctx.vaName = NULL;
ctx.vaStack = false;
// ctx.inbounds.push_back(false);
// ctx.boundsCheck.push_back(true);
ctx.inbounds.push_back(false);
ctx.boundsCheck.push_back(false);
ctx.cyclectx = cyclectx;

// step 2. process var-info lists to see what vars are captured, need boxing
Expand Down Expand Up @@ -5340,6 +5340,13 @@ static void emit_function(jl_lambda_info_t *lam, jl_llvm_functions_t *declaratio
builder.SetInsertPoint(bb);
}
}
// boundscheck elision
else if (is_inbounds(&ctx) && !ctx.boundsCheck.empty() && ctx.boundsCheck.back()) {
// skip expression unless it modifies the boundsCheck stack
if (jl_is_expr(stmt) && ((jl_expr_t*)stmt)->head == boundscheck_sym) {
(void)emit_expr(stmt, &ctx, false, false);
}
}
else {
(void)emit_expr(stmt, &ctx, false, false);
}
Expand Down

0 comments on commit a6f6519

Please sign in to comment.