Skip to content

Commit

Permalink
refactor emit_typeof to use mark_julia_const when possible
Browse files Browse the repository at this point in the history
now `emit_typeof(jl_cgval_t&)` returns a jl_cgval_t, and
`emit_typeof_boxed` returns a Value*.

also optimize away `typeof` in `inlineable` along with other
type functions, and check argument for effect_free.
  • Loading branch information
JeffBezanson committed Mar 1, 2016
1 parent 0b6cab6 commit 305b807
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
13 changes: 8 additions & 5 deletions base/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2231,12 +2231,15 @@ function inlineable(f::ANY, ft::ANY, e::Expr, atypes::Vector{Any}, sv::VarInfo,
return (isbits(atypes[2].parameters[1]),())
end
# special-case inliners for known pure functions that compute types
if isType(e.typ)
if (is(f,apply_type) || is(f,fieldtype) ||
if isType(e.typ) && !has_typevars(e.typ.parameters[1],true)
if (is(f,apply_type) || is(f,fieldtype) || is(f,typeof) ||
istopfunction(topmod, f, :typejoin) ||
istopfunction(topmod, f, :promote_type)) &&
!has_typevars(e.typ.parameters[1],true)
return (e.typ.parameters[1],())
istopfunction(topmod, f, :promote_type))
if effect_free(argexprs[2], sv, true)
return (e.typ.parameters[1],())
else
return (e.typ.parameters[1], Any[argexprs[2]])
end
end
end
if isa(f,IntrinsicFunction) || ft === IntrinsicFunction
Expand Down
4 changes: 2 additions & 2 deletions src/ccall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ static Value *julia_to_native(Type *to, bool toboxed, jl_value_t *jlto, const jl
}
else {
nbytes = tbaa_decorate(tbaa_datatype, builder.CreateLoad(
builder.CreateGEP(builder.CreatePointerCast(emit_typeof(jvinfo), T_pint32),
builder.CreateGEP(builder.CreatePointerCast(emit_typeof_boxed(jvinfo,ctx), T_pint32),
ConstantInt::get(T_size, offsetof(jl_datatype_t,size)/sizeof(int32_t))),
false));
ai = builder.CreateAlloca(T_int8, nbytes);
Expand All @@ -303,7 +303,7 @@ static Value *julia_to_native(Type *to, bool toboxed, jl_value_t *jlto, const jl
}
// emit maybe copy
*needStackRestore = true;
Value *jvt = emit_typeof(jvinfo);
Value *jvt = emit_typeof_boxed(jvinfo, ctx);
BasicBlock *mutableBB = BasicBlock::Create(getGlobalContext(),"is-mutable",ctx->f);
BasicBlock *immutableBB = BasicBlock::Create(getGlobalContext(),"is-immutable",ctx->f);
BasicBlock *afterBB = BasicBlock::Create(getGlobalContext(),"after",ctx->f);
Expand Down
20 changes: 12 additions & 8 deletions src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,9 @@ static Value *emit_typeptr_addr(Value *p)
return emit_nthptr_addr(p, -offset);
}

static Value *boxed(const jl_cgval_t &v, jl_codectx_t *ctx, bool gcooted=true);
static Value *boxed(const jl_cgval_t &v, jl_codectx_t *ctx, jl_value_t* type) = delete; // C++11 (temporary to prevent rebase error)

static Value *emit_typeof(Value *tt)
{
// given p, a jl_value_t*, compute its type tag
Expand All @@ -1086,17 +1089,21 @@ static Value *emit_typeof(Value *tt)
T_pjlvalue);
return tt;
}
static Value *emit_typeof(const jl_cgval_t &p)
static jl_cgval_t emit_typeof(const jl_cgval_t &p, jl_codectx_t *ctx)
{
// given p, compute its type
if (!p.constant && p.isboxed && !jl_is_leaf_type(p.typ)) {
return emit_typeof(p.V);
return mark_julia_type(emit_typeof(p.V), true, jl_datatype_type, ctx);
}
jl_value_t *aty = p.typ;
if (jl_is_type_type(aty)) // convert Int::Type{Int} ==> typeof(Int) ==> DataType
// but convert 1::Type{1} ==> typeof(1) ==> Int
aty = (jl_value_t*)jl_typeof(jl_tparam0(aty));
return literal_pointer_val(aty);
return mark_julia_const(aty);
}
static Value *emit_typeof_boxed(const jl_cgval_t &p, jl_codectx_t *ctx)
{
return boxed(emit_typeof(p, ctx), ctx);
}

static Value *emit_datatype_types(Value *dt)
Expand Down Expand Up @@ -1231,9 +1238,6 @@ static void null_pointer_check(Value *v, jl_codectx_t *ctx)
prepare_global(jlundeferr_var), ctx);
}

static Value *boxed(const jl_cgval_t &v, jl_codectx_t *ctx, bool gcooted=true);
static Value *boxed(const jl_cgval_t &v, jl_codectx_t *ctx, jl_value_t* type) = delete; // C++11 (temporary to prevent rebase error)

static void emit_type_error(const jl_cgval_t &x, jl_value_t *type, const std::string &msg,
jl_codectx_t *ctx)
{
Expand Down Expand Up @@ -1272,7 +1276,7 @@ static void emit_typecheck(const jl_cgval_t &x, jl_value_t *type, const std::str
ConstantInt::get(T_int32,0));
}
else {
istype = builder.CreateICmpEQ(emit_typeof(x), literal_pointer_val(type));
istype = builder.CreateICmpEQ(emit_typeof_boxed(x,ctx), literal_pointer_val(type));
}
BasicBlock *failBB = BasicBlock::Create(getGlobalContext(),"fail",ctx->f);
BasicBlock *passBB = BasicBlock::Create(getGlobalContext(),"pass");
Expand Down Expand Up @@ -2055,7 +2059,7 @@ static Value *boxed(const jl_cgval_t &vinfo, jl_codectx_t *ctx, bool gcrooted)

static void emit_cpointercheck(const jl_cgval_t &x, const std::string &msg, jl_codectx_t *ctx)
{
Value *t = emit_typeof(x);
Value *t = emit_typeof_boxed(x,ctx);
emit_typecheck(mark_julia_type(t, true, jl_any_type, ctx), (jl_value_t*)jl_datatype_type, msg, ctx);

Value *istype =
Expand Down
5 changes: 2 additions & 3 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2309,8 +2309,7 @@ static bool emit_builtin_call(jl_cgval_t *ret, jl_value_t *f, jl_value_t **args,

else if (f==jl_builtin_typeof && nargs==1) {
jl_cgval_t arg1 = emit_expr(args[1], ctx);
Value *lty = emit_typeof(arg1);
*ret = mark_julia_type(lty, true, jl_datatype_type, ctx);
*ret = emit_typeof(arg1,ctx);
JL_GC_POP();
return true;
}
Expand Down Expand Up @@ -2380,7 +2379,7 @@ static bool emit_builtin_call(jl_cgval_t *ret, jl_value_t *f, jl_value_t **args,
if (jl_is_leaf_type(tp0)) {
jl_cgval_t arg1 = emit_expr(args[1], ctx);
*ret = mark_julia_type(
builder.CreateICmpEQ(emit_typeof(arg1),
builder.CreateICmpEQ(emit_typeof_boxed(arg1,ctx),
literal_pointer_val(tp0)),
false,
jl_bool_type, ctx);
Expand Down
2 changes: 1 addition & 1 deletion src/intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ static jl_cgval_t generic_box(jl_value_t *targ, jl_value_t *x, jl_codectx_t *ctx
if (!jl_is_datatype(v.typ)
|| !jl_is_bitstype(v.typ)
|| jl_datatype_size(v.typ) != nb) {
Value *typ = emit_typeof(v);
Value *typ = emit_typeof_boxed(v, ctx);
if (!jl_is_bitstype(v.typ)) {
if (isboxed) {
Value *isbits = emit_datatype_isbitstype(typ);
Expand Down

0 comments on commit 305b807

Please sign in to comment.