Skip to content

Commit 0b9364d

Browse files
authored
Merge pull request #17225 from JuliaLang/jb/fix17098
fix #17098, bad codegen from too many i1 casts
2 parents 8ecd657 + f142192 commit 0b9364d

File tree

3 files changed

+27
-40
lines changed

3 files changed

+27
-40
lines changed

src/cgutils.cpp

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ static DIType julia_type_to_di(jl_value_t *jt, DIBuilder *dbuilder, bool isboxed
113113
#endif
114114
}
115115
if (jl_is_bitstype(jt)) {
116-
uint64_t SizeInBits = jdt == jl_bool_type ? 1 : 8*jdt->size;
116+
uint64_t SizeInBits = 8*jdt->size;
117117
#ifdef LLVM37
118118
llvm::DIType *t = dbuilder->createBasicType(jl_symbol_name(jdt->name->name),SizeInBits,8*jdt->alignment,llvm::dwarf::DW_ATE_unsigned);
119119
jdt->ditype = t;
@@ -301,7 +301,7 @@ JL_DLLEXPORT Type *julia_type_to_llvm(jl_value_t *jt, bool *isboxed)
301301
{
302302
// this function converts a Julia Type into the equivalent LLVM type
303303
if (isboxed) *isboxed = false;
304-
if (jt == (jl_value_t*)jl_bool_type) return T_int1;
304+
if (jt == (jl_value_t*)jl_bool_type) return T_int8;
305305
if (jt == (jl_value_t*)jl_bottom_type) return T_void;
306306
if (!jl_is_leaf_type(jt)) {
307307
if (isboxed) *isboxed = true;
@@ -849,11 +849,6 @@ static jl_cgval_t typed_load(Value *ptr, Value *idx_0based, jl_value_t *jltype,
849849
assert(elty != NULL);
850850
if (type_is_ghost(elty))
851851
return ghostValue(jltype);
852-
bool isbool=false;
853-
if (elty == T_int1) {
854-
elty = T_int8;
855-
isbool = true;
856-
}
857852
Value *data;
858853
if (ptr->getType()->getContainedType(0) != elty)
859854
data = builder.CreatePointerCast(ptr, PointerType::get(elty, 0));
@@ -878,8 +873,6 @@ static jl_cgval_t typed_load(Value *ptr, Value *idx_0based, jl_value_t *jltype,
878873
null_pointer_check(elt, ctx);
879874
}
880875
//}
881-
if (isbool)
882-
return mark_julia_type(builder.CreateTrunc(elt, T_int1), false, jltype, ctx);
883876
return mark_julia_type(elt, isboxed, jltype, ctx);
884877
}
885878

@@ -892,9 +885,6 @@ static void typed_store(Value *ptr, Value *idx_0based, const jl_cgval_t &rhs,
892885
assert(elty != NULL);
893886
if (type_is_ghost(elty))
894887
return;
895-
if (elty == T_int1) {
896-
elty = T_int8;
897-
}
898888
Value *r;
899889
if (jl_isbits(jltype) && ((jl_datatype_t*)jltype)->size > 0) {
900890
r = emit_unbox(elty, rhs, jltype);
@@ -1065,9 +1055,6 @@ static bool emit_getfield_unknownidx(jl_cgval_t *ret, const jl_cgval_t &strct, V
10651055
if (sizeof(void*) != sizeof(int))
10661056
idx0 = builder.CreateTrunc(idx0, T_int32); // llvm3.3 requires this, harmless elsewhere
10671057
Value *fld = builder.CreateExtractElement(strct.V, idx0);
1068-
if (jt == (jl_value_t*)jl_bool_type) {
1069-
fld = builder.CreateTrunc(fld, T_int1);
1070-
}
10711058
*ret = mark_julia_type(fld, false, jt, ctx);
10721059
return true;
10731060
}
@@ -1128,9 +1115,6 @@ static jl_cgval_t emit_getfield_knownidx(const jl_cgval_t &strct, unsigned idx,
11281115
assert( strct.V->getType()->isSingleValueType() );
11291116
fldv = strct.V;
11301117
}
1131-
if (jfty == (jl_value_t*)jl_bool_type) {
1132-
fldv = builder.CreateTrunc(fldv, T_int1);
1133-
}
11341118
assert(!jl_field_isptr(jt, idx));
11351119
return mark_julia_type(fldv, false, jfty, ctx);
11361120
}
@@ -1480,7 +1464,8 @@ static Value *boxed(const jl_cgval_t &vinfo, jl_codectx_t *ctx, bool gcrooted)
14801464
Type *t = julia_type_to_llvm(vinfo.typ);
14811465
assert(!type_is_ghost(t)); // should have been handled by isghost above!
14821466

1483-
1467+
if (jt == (jl_value_t*)jl_bool_type)
1468+
return julia_bool(builder.CreateTrunc(as_value(t,vinfo), T_int1));
14841469
if (t == T_int1)
14851470
return julia_bool(as_value(t,vinfo));
14861471

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

17101693
if (init_as_value) {

src/codegen.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,7 +2090,7 @@ static bool emit_builtin_call(jl_cgval_t *ret, jl_value_t *f, jl_value_t **args,
20902090
if (rt1) {
20912091
rt2 = static_eval(args[2], ctx, true);
20922092
if (rt2) {
2093-
*ret = mark_julia_type(ConstantInt::get(T_int1, jl_egal(rt1, rt2)), false, jl_bool_type, ctx);
2093+
*ret = mark_julia_type(ConstantInt::get(T_int8, jl_egal(rt1, rt2)), false, jl_bool_type, ctx);
20942094
JL_GC_POP();
20952095
return true;
20962096
}
@@ -2107,7 +2107,7 @@ static bool emit_builtin_call(jl_cgval_t *ret, jl_value_t *f, jl_value_t **args,
21072107
Value *ans = emit_f_is(v1, v2, ctx);
21082108
mark_gc_use(v1);
21092109
mark_gc_use(v2);
2110-
*ret = mark_julia_type(ans, false, jl_bool_type, ctx);
2110+
*ret = mark_julia_type(builder.CreateZExt(ans,T_int8), false, jl_bool_type, ctx);
21112111
JL_GC_POP();
21122112
return true;
21132113
}
@@ -2170,24 +2170,24 @@ static bool emit_builtin_call(jl_cgval_t *ret, jl_value_t *f, jl_value_t **args,
21702170
jl_value_t *tp0 = jl_tparam0(ty);
21712171
if (jl_subtype(arg, tp0, 0)) {
21722172
emit_expr(args[1], ctx); // TODO remove if no side effects
2173-
*ret = mark_julia_type(ConstantInt::get(T_int1, 1), false, jl_bool_type, ctx);
2173+
*ret = mark_julia_type(ConstantInt::get(T_int8, 1), false, jl_bool_type, ctx);
21742174
JL_GC_POP();
21752175
return true;
21762176
}
21772177
if (!jl_subtype(tp0, (jl_value_t*)jl_type_type, 0)) {
21782178
if (jl_is_leaf_type(arg)) {
21792179
emit_expr(args[1], ctx); // TODO remove if no side effects
2180-
*ret = mark_julia_type(ConstantInt::get(T_int1, 0), false, jl_bool_type, ctx);
2180+
*ret = mark_julia_type(ConstantInt::get(T_int8, 0), false, jl_bool_type, ctx);
21812181
JL_GC_POP();
21822182
return true;
21832183
}
21842184
if (jl_is_leaf_type(tp0)) {
21852185
jl_cgval_t arg1 = emit_expr(args[1], ctx);
2186-
*ret = mark_julia_type(
2187-
builder.CreateICmpEQ(emit_typeof_boxed(arg1,ctx),
2188-
literal_pointer_val(tp0)),
2189-
false,
2190-
jl_bool_type, ctx);
2186+
*ret = mark_julia_type(builder.CreateZExt(builder.CreateICmpEQ(emit_typeof_boxed(arg1,ctx),
2187+
literal_pointer_val(tp0)),
2188+
T_int8),
2189+
false,
2190+
jl_bool_type, ctx);
21912191
JL_GC_POP();
21922192
return true;
21932193
}
@@ -2202,7 +2202,7 @@ static bool emit_builtin_call(jl_cgval_t *ret, jl_value_t *f, jl_value_t **args,
22022202
jl_is_type_type(rt2) && !jl_is_typevar(jl_tparam0(rt2))) {
22032203
int issub = jl_subtype(jl_tparam0(rt1), jl_tparam0(rt2), 0);
22042204
// TODO: emit args[1] and args[2] in case of side effects?
2205-
*ret = mark_julia_type(ConstantInt::get(T_int1, issub), false, jl_bool_type, ctx);
2205+
*ret = mark_julia_type(ConstantInt::get(T_int8, issub), false, jl_bool_type, ctx);
22062206
JL_GC_POP();
22072207
return true;
22082208
}
@@ -3046,9 +3046,9 @@ static Value *emit_condition(const jl_cgval_t &condV, const std::string &msg,
30463046
jl_codectx_t *ctx)
30473047
{
30483048
if (condV.typ == (jl_value_t*)jl_bool_type) {
3049-
Value *cond = emit_unbox(T_int1, condV, (jl_value_t*)jl_bool_type);
3050-
assert(cond->getType() == T_int1);
3051-
return builder.CreateXor(cond, ConstantInt::get(T_int1,1));
3049+
Value *cond = emit_unbox(T_int8, condV, (jl_value_t*)jl_bool_type);
3050+
assert(cond->getType() == T_int8);
3051+
return builder.CreateXor(builder.CreateTrunc(cond,T_int1), ConstantInt::get(T_int1,1));
30523052
}
30533053
emit_typecheck(condV, (jl_value_t*)jl_bool_type, msg, ctx);
30543054
if (condV.isboxed) {

src/intrinsics.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ static Constant *julia_const_to_llvm(jl_value_t *e, bool nested=false)
151151
return NULL;
152152

153153
if (e == jl_true)
154-
return ConstantInt::get(T_int1, 1);
154+
return ConstantInt::get(T_int8, 1);
155155
if (e == jl_false)
156-
return ConstantInt::get(T_int1, 0);
156+
return ConstantInt::get(T_int8, 0);
157157

158158
if (jl_is_cpointer_type(jt))
159159
return ConstantExpr::getIntToPtr(ConstantInt::get(T_size, jl_unbox_long(e)), julia_type_to_llvm((jl_value_t*)bt));
@@ -412,8 +412,6 @@ static Type *staticeval_bitstype(jl_value_t *bt)
412412
static int get_bitstype_nbits(jl_value_t *bt)
413413
{
414414
assert(jl_is_bitstype(bt));
415-
if (bt == (jl_value_t*)jl_bool_type)
416-
return 1;
417415
return jl_datatype_size(bt)*8;
418416
}
419417

@@ -1127,10 +1125,16 @@ static jl_cgval_t emit_intrinsic(intrinsic f, jl_value_t **args, size_t nargs,
11271125
}
11281126
jl_value_t *newtyp = NULL;
11291127
// TODO: compare the type validity of x,y,z before emitting the intrinsic
1130-
Value *r = emit_untyped_intrinsic(f, x, y, z, nargs, ctx, (jl_datatype_t**)&newtyp);
1128+
Value *r;
1129+
if (f == not_int && xinfo.typ == (jl_value_t*)jl_bool_type)
1130+
r = builder.CreateXor(x, ConstantInt::get(T_int8, 1, true));
1131+
else
1132+
r = emit_untyped_intrinsic(f, x, y, z, nargs, ctx, (jl_datatype_t**)&newtyp);
11311133
if (!newtyp && r->getType() != x->getType())
11321134
// cast back to the exact original type (e.g. float vs. int) before remarking as a julia type
11331135
r = builder.CreateBitCast(r, x->getType());
1136+
if (r->getType() == T_int1)
1137+
r = builder.CreateZExt(r, T_int8);
11341138
return mark_julia_type(r, false, newtyp ? newtyp : xinfo.typ, ctx);
11351139
}
11361140
#endif
@@ -1139,7 +1143,7 @@ static jl_cgval_t emit_intrinsic(intrinsic f, jl_value_t **args, size_t nargs,
11391143
}
11401144

11411145
static Value *emit_untyped_intrinsic(intrinsic f, Value *x, Value *y, Value *z, size_t nargs,
1142-
jl_codectx_t *ctx, jl_datatype_t **newtyp)
1146+
jl_codectx_t *ctx, jl_datatype_t **newtyp)
11431147
{
11441148
Type *t = x->getType();
11451149
Value *fy;

0 commit comments

Comments
 (0)