Skip to content
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

Ensure immutable isbits Union fields get set correctly #23367

Merged
merged 6 commits into from
Sep 7, 2017
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
18 changes: 11 additions & 7 deletions base/atomics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -345,28 +345,31 @@ for typ in atomictypes
irt = Base.libllvm_version >= v"3.6" ? "$ilt, $ilt*" : "$ilt*"
@eval getindex(x::Atomic{$typ}) =
llvmcall($"""
%rv = load atomic $rt %0 acquire, align $(alignment(typ))
%ptr = inttoptr i$WORD_SIZE %0 to $lt*
%rv = load atomic $rt %ptr acquire, align $(alignment(typ))
ret $lt %rv
""", $typ, Tuple{Ptr{$typ}}, unsafe_convert(Ptr{$typ}, x))
@eval setindex!(x::Atomic{$typ}, v::$typ) =
llvmcall($"""
store atomic $lt %1, $lt* %0 release, align $(alignment(typ))
%ptr = inttoptr i$WORD_SIZE %0 to $lt*
store atomic $lt %1, $lt* %ptr release, align $(alignment(typ))
ret void
""", Void, Tuple{Ptr{$typ},$typ}, unsafe_convert(Ptr{$typ}, x), v)
""", Void, Tuple{Ptr{$typ}, $typ}, unsafe_convert(Ptr{$typ}, x), v)

# Note: atomic_cas! succeeded (i.e. it stored "new") if and only if the result is "cmp"
if typ <: Integer
@eval atomic_cas!(x::Atomic{$typ}, cmp::$typ, new::$typ) =
llvmcall($"""
%rs = cmpxchg $lt* %0, $lt %1, $lt %2 acq_rel acquire
%ptr = inttoptr i$WORD_SIZE %0 to $lt*
%rs = cmpxchg $lt* %ptr, $lt %1, $lt %2 acq_rel acquire
%rv = extractvalue { $lt, i1 } %rs, 0
ret $lt %rv
""", $typ, Tuple{Ptr{$typ},$typ,$typ},
unsafe_convert(Ptr{$typ}, x), cmp, new)
else
@eval atomic_cas!(x::Atomic{$typ}, cmp::$typ, new::$typ) =
llvmcall($"""
%iptr = bitcast $lt* %0 to $ilt*
%iptr = inttoptr i$WORD_SIZE %0 to $ilt*
%icmp = bitcast $lt %1 to $ilt
%inew = bitcast $lt %2 to $ilt
%irs = cmpxchg $ilt* %iptr, $ilt %icmp, $ilt %inew acq_rel acquire
Expand All @@ -387,14 +390,15 @@ for typ in atomictypes
if typ <: Integer
@eval $fn(x::Atomic{$typ}, v::$typ) =
llvmcall($"""
%rv = atomicrmw $rmw $lt* %0, $lt %1 acq_rel
%ptr = inttoptr i$WORD_SIZE %0 to $lt*
%rv = atomicrmw $rmw $lt* %ptr, $lt %1 acq_rel
ret $lt %rv
""", $typ, Tuple{Ptr{$typ}, $typ}, unsafe_convert(Ptr{$typ}, x), v)
else
rmwop == :xchg || continue
@eval $fn(x::Atomic{$typ}, v::$typ) =
llvmcall($"""
%iptr = bitcast $lt* %0 to $ilt*
%iptr = inttoptr i$WORD_SIZE %0 to $ilt*
%ival = bitcast $lt %1 to $ilt
%irv = atomicrmw $rmw $ilt* %iptr, $ilt %ival acq_rel
%rv = bitcast $ilt %irv to $lt
Expand Down
32 changes: 8 additions & 24 deletions base/fastmath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -296,34 +296,18 @@ asin_fast(x::FloatTypes) = asin(x)

# explicit implementations

# FIXME: Change to `ccall((:sincos, libm))` when `Ref` calling convention can be
# stack allocated.
@inline function sincos_fast(v::Float64)
return Base.llvmcall("""
%f = bitcast i8 *%1 to void (double, double *, double *)*
%ps = alloca double
%pc = alloca double
call void %f(double %0, double *%ps, double *%pc)
%s = load double, double* %ps
%c = load double, double* %pc
%res0 = insertvalue [2 x double] undef, double %s, 0
%res = insertvalue [2 x double] %res0, double %c, 1
ret [2 x double] %res
""", Tuple{Float64,Float64}, Tuple{Float64,Ptr{Void}}, v, cglobal((:sincos, libm)))
s = Ref{Cdouble}()
c = Ref{Cdouble}()
ccall((:sincos, libm), Void, (Cdouble, Ptr{Cdouble}, Ptr{Cdouble}), v, s, c)
return (s[], c[])
end

@inline function sincos_fast(v::Float32)
return Base.llvmcall("""
%f = bitcast i8 *%1 to void (float, float *, float *)*
%ps = alloca float
%pc = alloca float
call void %f(float %0, float *%ps, float *%pc)
%s = load float, float* %ps
%c = load float, float* %pc
%res0 = insertvalue [2 x float] undef, float %s, 0
%res = insertvalue [2 x float] %res0, float %c, 1
ret [2 x float] %res
""", Tuple{Float32,Float32}, Tuple{Float32,Ptr{Void}}, v, cglobal((:sincosf, libm)))
s = Ref{Cfloat}()
c = Ref{Cfloat}()
ccall((:sincosf, libm), Void, (Cfloat, Ptr{Cfloat}, Ptr{Cfloat}), v, s, c)
return (s[], c[])
end

@inline function sincos_fast(v::Float16)
Expand Down
69 changes: 44 additions & 25 deletions src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,30 +77,40 @@ static int NOINLINE compare_svec(jl_svec_t *a, jl_svec_t *b)
// See comment above for an explanation of NOINLINE.
static int NOINLINE compare_fields(jl_value_t *a, jl_value_t *b, jl_datatype_t *dt)
{
size_t nf = jl_datatype_nfields(dt);
for (size_t f=0; f < nf; f++) {
size_t f, nf = jl_datatype_nfields(dt);
for (f = 0; f < nf; f++) {
size_t offs = jl_field_offset(dt, f);
char *ao = (char*)jl_data_ptr(a) + offs;
char *bo = (char*)jl_data_ptr(b) + offs;
int eq;
if (jl_field_isptr(dt, f)) {
jl_value_t *af = *(jl_value_t**)ao;
jl_value_t *bf = *(jl_value_t**)bo;
if (af == bf) eq = 1;
else if (af==NULL || bf==NULL) eq = 0;
else eq = jl_egal(af, bf);
if (af != bf) {
if (af == NULL || bf == NULL)
return 0;
if (!jl_egal(af, bf))
return 0;
}
}
else {
jl_datatype_t *ft = (jl_datatype_t*)jl_field_type(dt, f);
if (jl_is_uniontype(ft)) {
uint8_t asel = ((uint8_t*)ao)[jl_field_size(dt, f) - 1];
uint8_t bsel = ((uint8_t*)bo)[jl_field_size(dt, f) - 1];
if (asel != bsel)
return 0;
ft = (jl_datatype_t*)jl_nth_union_component((jl_value_t*)ft, asel);
}
if (!ft->layout->haspadding) {
eq = bits_equal(ao, bo, jl_field_size(dt, f));
if (!bits_equal(ao, bo, jl_field_size(dt, f)))
return 0;
}
else {
assert(jl_datatype_nfields(ft) > 0);
eq = compare_fields((jl_value_t*)ao, (jl_value_t*)bo, ft);
if (!compare_fields((jl_value_t*)ao, (jl_value_t*)bo, ft))
return 0;
}
}
if (!eq) return 0;
}
return 1;
}
Expand All @@ -127,9 +137,11 @@ JL_DLLEXPORT int jl_egal(jl_value_t *a, jl_value_t *b)
return 0;
return !memcmp(jl_string_data(a), jl_string_data(b), l);
}
if (dt->mutabl) return 0;
if (dt->mutabl)
return 0;
size_t sz = jl_datatype_size(dt);
if (sz == 0) return 1;
if (sz == 0)
return 1;
size_t nf = jl_datatype_nfields(dt);
if (nf == 0)
return bits_equal(jl_data_ptr(a), jl_data_ptr(b), sz);
Expand Down Expand Up @@ -161,10 +173,10 @@ static uintptr_t bits_hash(void *b, size_t sz)
static uintptr_t NOINLINE hash_svec(jl_svec_t *v)
{
uintptr_t h = 0;
size_t l = jl_svec_len(v);
for(size_t i = 0; i < l; i++) {
jl_value_t *x = jl_svecref(v,i);
uintptr_t u = x==NULL ? 0 : jl_object_id(x);
size_t i, l = jl_svec_len(v);
for (i = 0; i < l; i++) {
jl_value_t *x = jl_svecref(v, i);
uintptr_t u = (x == NULL) ? 0 : jl_object_id(x);
h = bitmix(h, u);
}
return h;
Expand All @@ -188,9 +200,11 @@ static uintptr_t jl_object_id_(jl_value_t *tv, jl_value_t *v)
if (dt == jl_typename_type)
return ((jl_typename_t*)v)->hash;
#ifdef _P64
if (v == jl_ANY_flag) return 0x31c472f68ee30bddULL;
if (v == jl_ANY_flag)
return 0x31c472f68ee30bddULL;
#else
if (v == jl_ANY_flag) return 0x8ee30bdd;
if (v == jl_ANY_flag)
return 0x8ee30bdd;
#endif
if (dt == jl_string_type) {
#ifdef _P64
Expand All @@ -199,24 +213,29 @@ static uintptr_t jl_object_id_(jl_value_t *tv, jl_value_t *v)
return memhash32_seed(jl_string_data(v), jl_string_len(v), 0xedc3b677);
#endif
}
if (dt->mutabl) return inthash((uintptr_t)v);
if (dt->mutabl)
return inthash((uintptr_t)v);
size_t sz = jl_datatype_size(tv);
uintptr_t h = jl_object_id(tv);
if (sz == 0) return ~h;
size_t nf = jl_datatype_nfields(dt);
if (nf == 0) {
if (sz == 0)
return ~h;
size_t f, nf = jl_datatype_nfields(dt);
if (nf == 0)
return bits_hash(jl_data_ptr(v), sz) ^ h;
}
for (size_t f=0; f < nf; f++) {
for (f = 0; f < nf; f++) {
size_t offs = jl_field_offset(dt, f);
char *vo = (char*)jl_data_ptr(v) + offs;
uintptr_t u;
if (jl_field_isptr(dt, f)) {
jl_value_t *f = *(jl_value_t**)vo;
u = f==NULL ? 0 : jl_object_id(f);
u = (f == NULL) ? 0 : jl_object_id(f);
}
else {
jl_datatype_t *fieldtype = (jl_datatype_t*)jl_field_type(dt, f);
if (jl_is_uniontype(fieldtype)) {
uint8_t sel = ((uint8_t*)vo)[jl_field_size(dt, f) - 1];
fieldtype = (jl_datatype_t*)jl_nth_union_component((jl_value_t*)fieldtype, sel);
}
assert(jl_is_datatype(fieldtype) && !fieldtype->abstract && !fieldtype->mutabl);
if (fieldtype->layout->haspadding)
u = jl_object_id_((jl_value_t*)fieldtype, (jl_value_t*)vo);
Expand Down Expand Up @@ -244,7 +263,7 @@ JL_CALLABLE(jl_f_is)
JL_NARGS(===, 2, 2);
if (args[0] == args[1])
return jl_true;
return jl_egal(args[0],args[1]) ? jl_true : jl_false;
return jl_egal(args[0], args[1]) ? jl_true : jl_false;
}

JL_CALLABLE(jl_f_typeof)
Expand Down
Loading