Skip to content

fix #17098, bad codegen from too many i1 casts #17225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 4 additions & 21 deletions src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ static DIType julia_type_to_di(jl_value_t *jt, DIBuilder *dbuilder, bool isboxed
#endif
}
if (jl_is_bitstype(jt)) {
uint64_t SizeInBits = jdt == jl_bool_type ? 1 : 8*jdt->size;
uint64_t SizeInBits = 8*jdt->size;
#ifdef LLVM37
llvm::DIType *t = dbuilder->createBasicType(jl_symbol_name(jdt->name->name),SizeInBits,8*jdt->alignment,llvm::dwarf::DW_ATE_unsigned);
jdt->ditype = t;
Expand Down Expand Up @@ -301,7 +301,7 @@ JL_DLLEXPORT Type *julia_type_to_llvm(jl_value_t *jt, bool *isboxed)
{
// this function converts a Julia Type into the equivalent LLVM type
if (isboxed) *isboxed = false;
if (jt == (jl_value_t*)jl_bool_type) return T_int1;
if (jt == (jl_value_t*)jl_bool_type) return T_int8;
if (jt == (jl_value_t*)jl_bottom_type) return T_void;
if (!jl_is_leaf_type(jt)) {
if (isboxed) *isboxed = true;
Expand Down Expand Up @@ -849,11 +849,6 @@ static jl_cgval_t typed_load(Value *ptr, Value *idx_0based, jl_value_t *jltype,
assert(elty != NULL);
if (type_is_ghost(elty))
return ghostValue(jltype);
bool isbool=false;
if (elty == T_int1) {
elty = T_int8;
isbool = true;
}
Value *data;
if (ptr->getType()->getContainedType(0) != elty)
data = builder.CreatePointerCast(ptr, PointerType::get(elty, 0));
Expand All @@ -878,8 +873,6 @@ static jl_cgval_t typed_load(Value *ptr, Value *idx_0based, jl_value_t *jltype,
null_pointer_check(elt, ctx);
}
//}
if (isbool)
return mark_julia_type(builder.CreateTrunc(elt, T_int1), false, jltype, ctx);
return mark_julia_type(elt, isboxed, jltype, ctx);
}

Expand All @@ -892,9 +885,6 @@ static void typed_store(Value *ptr, Value *idx_0based, const jl_cgval_t &rhs,
assert(elty != NULL);
if (type_is_ghost(elty))
return;
if (elty == T_int1) {
elty = T_int8;
}
Value *r;
if (jl_isbits(jltype) && ((jl_datatype_t*)jltype)->size > 0) {
r = emit_unbox(elty, rhs, jltype);
Expand Down Expand Up @@ -1065,9 +1055,6 @@ static bool emit_getfield_unknownidx(jl_cgval_t *ret, const jl_cgval_t &strct, V
if (sizeof(void*) != sizeof(int))
idx0 = builder.CreateTrunc(idx0, T_int32); // llvm3.3 requires this, harmless elsewhere
Value *fld = builder.CreateExtractElement(strct.V, idx0);
if (jt == (jl_value_t*)jl_bool_type) {
fld = builder.CreateTrunc(fld, T_int1);
}
*ret = mark_julia_type(fld, false, jt, ctx);
return true;
}
Expand Down Expand Up @@ -1128,9 +1115,6 @@ static jl_cgval_t emit_getfield_knownidx(const jl_cgval_t &strct, unsigned idx,
assert( strct.V->getType()->isSingleValueType() );
fldv = strct.V;
}
if (jfty == (jl_value_t*)jl_bool_type) {
fldv = builder.CreateTrunc(fldv, T_int1);
}
assert(!jl_field_isptr(jt, idx));
return mark_julia_type(fldv, false, jfty, ctx);
}
Expand Down Expand Up @@ -1480,7 +1464,8 @@ static Value *boxed(const jl_cgval_t &vinfo, jl_codectx_t *ctx, bool gcrooted)
Type *t = julia_type_to_llvm(vinfo.typ);
assert(!type_is_ghost(t)); // should have been handled by isghost above!


if (jt == (jl_value_t*)jl_bool_type)
return julia_bool(builder.CreateTrunc(as_value(t,vinfo), T_int1));
if (t == T_int1)
return julia_bool(as_value(t,vinfo));

Expand Down Expand Up @@ -1703,8 +1688,6 @@ static jl_cgval_t emit_new_struct(jl_value_t *ty, size_t nargs, jl_value_t **arg
// and use memcpy instead
dest = builder.CreateConstInBoundsGEP2_32(LLVM37_param(lt) strct, 0, i);
}
if (fty == T_int1)
fty = T_int8;
fval = emit_unbox(fty, fval_info, jtype, dest);

if (init_as_value) {
Expand Down
26 changes: 13 additions & 13 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2075,7 +2075,7 @@ static bool emit_builtin_call(jl_cgval_t *ret, jl_value_t *f, jl_value_t **args,
if (rt1) {
rt2 = static_eval(args[2], ctx, true);
if (rt2) {
*ret = mark_julia_type(ConstantInt::get(T_int1, jl_egal(rt1, rt2)), false, jl_bool_type, ctx);
*ret = mark_julia_type(ConstantInt::get(T_int8, jl_egal(rt1, rt2)), false, jl_bool_type, ctx);
JL_GC_POP();
return true;
}
Expand All @@ -2092,7 +2092,7 @@ static bool emit_builtin_call(jl_cgval_t *ret, jl_value_t *f, jl_value_t **args,
Value *ans = emit_f_is(v1, v2, ctx);
mark_gc_use(v1);
mark_gc_use(v2);
*ret = mark_julia_type(ans, false, jl_bool_type, ctx);
*ret = mark_julia_type(builder.CreateZExt(ans,T_int8), false, jl_bool_type, ctx);
JL_GC_POP();
return true;
}
Expand Down Expand Up @@ -2155,24 +2155,24 @@ static bool emit_builtin_call(jl_cgval_t *ret, jl_value_t *f, jl_value_t **args,
jl_value_t *tp0 = jl_tparam0(ty);
if (jl_subtype(arg, tp0, 0)) {
emit_expr(args[1], ctx); // TODO remove if no side effects
*ret = mark_julia_type(ConstantInt::get(T_int1, 1), false, jl_bool_type, ctx);
*ret = mark_julia_type(ConstantInt::get(T_int8, 1), false, jl_bool_type, ctx);
JL_GC_POP();
return true;
}
if (!jl_subtype(tp0, (jl_value_t*)jl_type_type, 0)) {
if (jl_is_leaf_type(arg)) {
emit_expr(args[1], ctx); // TODO remove if no side effects
*ret = mark_julia_type(ConstantInt::get(T_int1, 0), false, jl_bool_type, ctx);
*ret = mark_julia_type(ConstantInt::get(T_int8, 0), false, jl_bool_type, ctx);
JL_GC_POP();
return true;
}
if (jl_is_leaf_type(tp0)) {
jl_cgval_t arg1 = emit_expr(args[1], ctx);
*ret = mark_julia_type(
builder.CreateICmpEQ(emit_typeof_boxed(arg1,ctx),
literal_pointer_val(tp0)),
false,
jl_bool_type, ctx);
*ret = mark_julia_type(builder.CreateZExt(builder.CreateICmpEQ(emit_typeof_boxed(arg1,ctx),
literal_pointer_val(tp0)),
T_int8),
false,
jl_bool_type, ctx);
JL_GC_POP();
return true;
}
Expand All @@ -2187,7 +2187,7 @@ static bool emit_builtin_call(jl_cgval_t *ret, jl_value_t *f, jl_value_t **args,
jl_is_type_type(rt2) && !jl_is_typevar(jl_tparam0(rt2))) {
int issub = jl_subtype(jl_tparam0(rt1), jl_tparam0(rt2), 0);
// TODO: emit args[1] and args[2] in case of side effects?
*ret = mark_julia_type(ConstantInt::get(T_int1, issub), false, jl_bool_type, ctx);
*ret = mark_julia_type(ConstantInt::get(T_int8, issub), false, jl_bool_type, ctx);
JL_GC_POP();
return true;
}
Expand Down Expand Up @@ -3027,9 +3027,9 @@ static Value *emit_condition(const jl_cgval_t &condV, const std::string &msg,
jl_codectx_t *ctx)
{
if (condV.typ == (jl_value_t*)jl_bool_type) {
Value *cond = emit_unbox(T_int1, condV, (jl_value_t*)jl_bool_type);
assert(cond->getType() == T_int1);
return builder.CreateXor(cond, ConstantInt::get(T_int1,1));
Value *cond = emit_unbox(T_int8, condV, (jl_value_t*)jl_bool_type);
assert(cond->getType() == T_int8);
return builder.CreateXor(builder.CreateTrunc(cond,T_int1), ConstantInt::get(T_int1,1));
}
emit_typecheck(condV, (jl_value_t*)jl_bool_type, msg, ctx);
if (condV.isboxed) {
Expand Down
16 changes: 10 additions & 6 deletions src/intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ static Constant *julia_const_to_llvm(jl_value_t *e, bool nested=false)
return NULL;

if (e == jl_true)
return ConstantInt::get(T_int1, 1);
return ConstantInt::get(T_int8, 1);
if (e == jl_false)
return ConstantInt::get(T_int1, 0);
return ConstantInt::get(T_int8, 0);

if (jl_is_cpointer_type(jt))
return ConstantExpr::getIntToPtr(ConstantInt::get(T_size, jl_unbox_long(e)), julia_type_to_llvm((jl_value_t*)bt));
Expand Down Expand Up @@ -412,8 +412,6 @@ static Type *staticeval_bitstype(jl_value_t *bt)
static int get_bitstype_nbits(jl_value_t *bt)
{
assert(jl_is_bitstype(bt));
if (bt == (jl_value_t*)jl_bool_type)
return 1;
return jl_datatype_size(bt)*8;
}

Expand Down Expand Up @@ -1127,10 +1125,16 @@ static jl_cgval_t emit_intrinsic(intrinsic f, jl_value_t **args, size_t nargs,
}
jl_value_t *newtyp = NULL;
// TODO: compare the type validity of x,y,z before emitting the intrinsic
Value *r = emit_untyped_intrinsic(f, x, y, z, nargs, ctx, (jl_datatype_t**)&newtyp);
Value *r;
if (f == not_int && xinfo.typ == (jl_value_t*)jl_bool_type)
r = builder.CreateXor(x, ConstantInt::get(T_int8, 1, true));
else
r = emit_untyped_intrinsic(f, x, y, z, nargs, ctx, (jl_datatype_t**)&newtyp);
if (!newtyp && r->getType() != x->getType())
// cast back to the exact original type (e.g. float vs. int) before remarking as a julia type
r = builder.CreateBitCast(r, x->getType());
if (r->getType() == T_int1)
r = builder.CreateZExt(r, T_int8);
return mark_julia_type(r, false, newtyp ? newtyp : xinfo.typ, ctx);
}
#endif
Expand All @@ -1139,7 +1143,7 @@ static jl_cgval_t emit_intrinsic(intrinsic f, jl_value_t **args, size_t nargs,
}

static Value *emit_untyped_intrinsic(intrinsic f, Value *x, Value *y, Value *z, size_t nargs,
jl_codectx_t *ctx, jl_datatype_t **newtyp)
jl_codectx_t *ctx, jl_datatype_t **newtyp)
{
Type *t = x->getType();
Value *fy;
Expand Down