Skip to content

Commit 50f30f4

Browse files
authored
Merge pull request #18094 from JuliaLang/jn/thread-fixes
a couple threading fixes
2 parents 0f805fb + b6b26df commit 50f30f4

File tree

2 files changed

+49
-35
lines changed

2 files changed

+49
-35
lines changed

base/inference.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,11 @@ function typeinf_edge(method::Method, atypes::ANY, sparams::SimpleVector, needtr
14691469
end
14701470
end
14711471

1472-
if caller === nothing && in_typeinf_loop
1472+
ccall(:jl_typeinf_begin, Void, ())
1473+
thread_in_typeinf_loop = in_typeinf_loop::Bool
1474+
ccall(:jl_typeinf_end, Void, ())
1475+
1476+
if caller === nothing && thread_in_typeinf_loop
14731477
# if the caller needed the ast, but we are already in the typeinf loop
14741478
# then just return early -- we can't fulfill this request
14751479
# if the client was inlining, then this means we decided not to try to infer this

src/jltypes.c

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,14 @@ static void extend(jl_value_t *var, jl_value_t *val, cenv_t *soln)
392392
}
393393

394394
static jl_value_t *jl_type_intersect(jl_value_t *a, jl_value_t *b,
395-
cenv_t *penv, cenv_t *eqc, variance_t var);
395+
cenv_t *penv, cenv_t *eqc,
396+
int *recheck_tuple_intersection,
397+
variance_t var);
396398

397399
static jl_value_t *intersect_union(jl_uniontype_t *a, jl_value_t *b,
398-
cenv_t *penv, cenv_t *eqc, variance_t var)
400+
cenv_t *penv, cenv_t *eqc,
401+
int *recheck_tuple_intersection,
402+
variance_t var)
399403
{
400404
int eq0 = eqc->n, co0 = penv->n;
401405
size_t i, l = jl_svec_len(a->types);
@@ -411,11 +415,11 @@ static jl_value_t *intersect_union(jl_uniontype_t *a, jl_value_t *b,
411415
for(i=0; i < l; i++) {
412416
int eq_l = eqc->n, co_l = penv->n;
413417
jl_value_t *ti = jl_type_intersect(jl_svecref(a->types,i), b,
414-
penv, eqc, var);
418+
penv, eqc, recheck_tuple_intersection, var);
415419
if (ti == (jl_value_t*)jl_bottom_type) {
416420
eqc->n = eq0; penv->n = co0;
417421
ti = jl_type_intersect(jl_svecref(a->types,i), b,
418-
penv, eqc, var);
422+
penv, eqc, recheck_tuple_intersection, var);
419423
if (ti != (jl_value_t*)jl_bottom_type) {
420424
// tvar conflict among union elements; keep the conflicting
421425
// constraints rolled back
@@ -553,10 +557,10 @@ to be returned, with one exception illustrated by:
553557
where typeintersect(B,C) == Bottom.
554558
*/
555559

556-
int recheck_tuple_intersection = 0; // "flag" above
557-
558560
static jl_value_t *intersect_tuple(jl_datatype_t *a, jl_datatype_t *b,
559-
cenv_t *penv, cenv_t *eqc, variance_t var)
561+
cenv_t *penv, cenv_t *eqc,
562+
int *recheck_tuple_intersection, // "flag" above
563+
variance_t var)
560564
{
561565
jl_svec_t *ap = a->parameters, *bp = b->parameters;
562566
size_t alenr = jl_svec_len(ap), blenr = jl_svec_len(bp);
@@ -590,7 +594,7 @@ static jl_value_t *intersect_tuple(jl_datatype_t *a, jl_datatype_t *b,
590594
// Do we need to store "at least N" constraints in penv?
591595
// Formerly, typeintersect(Tuple{A,Vararg{B}}, NTuple{N,C}) did that
592596
if (akind == JL_VARARG_BOUND || bkind == JL_VARARG_BOUND)
593-
recheck_tuple_intersection = 1;
597+
*recheck_tuple_intersection = 1;
594598
}
595599
if (bottom) return (jl_value_t*) jl_bottom_type;
596600
if (n == 0) return jl_typeof(jl_emptytuple);
@@ -626,17 +630,17 @@ static jl_value_t *intersect_tuple(jl_datatype_t *a, jl_datatype_t *b,
626630
bi++;
627631
}
628632
assert(ae!=NULL && be!=NULL);
629-
ce = jl_type_intersect(ae,be,penv,eqc,var);
633+
ce = jl_type_intersect(ae, be, penv, eqc, recheck_tuple_intersection, var);
630634
if (ce == (jl_value_t*)jl_bottom_type) {
631635
if (var!=invariant && aseq && bseq) {
632636
// (X∩Y)==∅ → (X...)∩(Y...) == ()
633637
// We don't need to set bindings here because
634-
// recheck_tuple_intersection=1
638+
// *recheck_tuple_intersection = 1
635639
if (n == 1) {
636640
JL_GC_POP();
637641
return (jl_value_t*)jl_typeof(jl_emptytuple);
638642
}
639-
jl_svec_set_len_unsafe(tc,jl_svec_len(tc)-1);
643+
jl_svec_set_len_unsafe(tc, jl_svec_len(tc) - 1);
640644
goto done_intersect_tuple;
641645
}
642646
JL_GC_POP();
@@ -653,7 +657,9 @@ static jl_value_t *intersect_tuple(jl_datatype_t *a, jl_datatype_t *b,
653657
}
654658

655659
static jl_value_t *intersect_tag(jl_datatype_t *a, jl_datatype_t *b,
656-
cenv_t *penv, cenv_t *eqc, variance_t var)
660+
cenv_t *penv, cenv_t *eqc,
661+
int *recheck_tuple_intersection,
662+
variance_t var)
657663
{
658664
assert(a->name == b->name);
659665
assert(jl_svec_len(a->parameters) == jl_svec_len(b->parameters));
@@ -678,7 +684,7 @@ static jl_value_t *intersect_tag(jl_datatype_t *a, jl_datatype_t *b,
678684
return (jl_value_t*)jl_bottom_type;
679685
}
680686
}
681-
ti = jl_type_intersect(ap,bp,penv,eqc,invariant);
687+
ti = jl_type_intersect(ap, bp, penv, eqc, recheck_tuple_intersection, invariant);
682688
if (bp == (jl_value_t*)jl_bottom_type &&
683689
!((jl_tvar_t*)ap)->bound) {
684690
// "Union{}" as a type parameter
@@ -687,7 +693,7 @@ static jl_value_t *intersect_tag(jl_datatype_t *a, jl_datatype_t *b,
687693
}
688694
}
689695
else if (jl_is_typevar(bp)) {
690-
ti = jl_type_intersect(ap,bp,penv,eqc,invariant);
696+
ti = jl_type_intersect(ap, bp, penv, eqc, recheck_tuple_intersection, invariant);
691697
if (ap == (jl_value_t*)jl_bottom_type &&
692698
!((jl_tvar_t*)bp)->bound) {
693699
// "Union{}" as a type parameter
@@ -701,7 +707,7 @@ static jl_value_t *intersect_tag(jl_datatype_t *a, jl_datatype_t *b,
701707
if (tva || tvb) {
702708
if (jl_subtype_invariant(ap,bp,0) ||
703709
jl_subtype_invariant(bp,ap,0)) {
704-
ti = jl_type_intersect(ap,bp,penv,eqc,invariant);
710+
ti = jl_type_intersect(ap, bp, penv, eqc, recheck_tuple_intersection, invariant);
705711
}
706712
else {
707713
ti = (jl_value_t*)jl_bottom_type;
@@ -786,7 +792,9 @@ static int match_intersection_mode = 0;
786792
static jl_value_t *meet_tvars(jl_tvar_t *a, jl_tvar_t *b);
787793

788794
static jl_value_t *intersect_typevar(jl_tvar_t *a, jl_value_t *b,
789-
cenv_t *penv, cenv_t *eqc, variance_t var)
795+
cenv_t *penv, cenv_t *eqc,
796+
int *recheck_tuple_intersection,
797+
variance_t var)
790798
{
791799
jl_value_t *both=NULL;
792800
jl_tvar_t *new_b=NULL;
@@ -828,7 +836,7 @@ static jl_value_t *intersect_typevar(jl_tvar_t *a, jl_value_t *b,
828836
}
829837
}
830838
else {
831-
b = jl_type_intersect(a->ub, b, penv, eqc, covariant);
839+
b = jl_type_intersect(a->ub, b, penv, eqc, recheck_tuple_intersection, covariant);
832840
if (b == jl_bottom_type) {
833841
JL_GC_POP();
834842
return b;
@@ -924,7 +932,7 @@ static jl_value_t *intersect_typevar(jl_tvar_t *a, jl_value_t *b,
924932
return (jl_value_t*)a;
925933
}
926934

927-
static jl_value_t *approxify_type(jl_datatype_t *dt, jl_svec_t *pp)
935+
static jl_value_t *approxify_type(jl_datatype_t *dt, jl_svec_t *pp, int *recheck_tuple_intersection)
928936
{
929937
size_t i, l = jl_svec_len(dt->parameters);
930938
jl_svec_t *p = jl_alloc_svec(l);
@@ -942,7 +950,9 @@ static jl_value_t *approxify_type(jl_datatype_t *dt, jl_svec_t *pp)
942950
}
943951

944952
static jl_value_t *jl_type_intersect(jl_value_t *a, jl_value_t *b,
945-
cenv_t *penv, cenv_t *eqc, variance_t var)
953+
cenv_t *penv, cenv_t *eqc,
954+
int *recheck_tuple_intersection,
955+
variance_t var)
946956
{
947957
if (jl_is_typector(a))
948958
a = (jl_value_t*)((jl_typector_t*)a)->body;
@@ -953,13 +963,13 @@ static jl_value_t *jl_type_intersect(jl_value_t *a, jl_value_t *b,
953963
if (var == covariant && !((jl_tvar_t*)a)->bound)
954964
a = ((jl_tvar_t*)a)->ub;
955965
else if (a != jl_ANY_flag)
956-
return intersect_typevar((jl_tvar_t*)a, b, penv, eqc, var);
966+
return intersect_typevar((jl_tvar_t*)a, b, penv, eqc, recheck_tuple_intersection, var);
957967
}
958968
if (jl_is_typevar(b)) {
959969
if (var == covariant && !((jl_tvar_t*)b)->bound)
960970
b = ((jl_tvar_t*)b)->ub;
961971
else if (b != jl_ANY_flag)
962-
return intersect_typevar((jl_tvar_t*)b, a, penv, eqc, var);
972+
return intersect_typevar((jl_tvar_t*)b, a, penv, eqc, recheck_tuple_intersection, var);
963973
}
964974
if (a == (jl_value_t*)jl_bottom_type || b == (jl_value_t*)jl_bottom_type)
965975
return (jl_value_t*)jl_bottom_type;
@@ -971,27 +981,27 @@ static jl_value_t *jl_type_intersect(jl_value_t *a, jl_value_t *b,
971981
}
972982
// union
973983
if (jl_is_uniontype(a))
974-
return intersect_union((jl_uniontype_t*)a, b, penv, eqc, var);
984+
return intersect_union((jl_uniontype_t*)a, b, penv, eqc, recheck_tuple_intersection, var);
975985
if (jl_is_uniontype(b))
976-
return intersect_union((jl_uniontype_t*)b, a, penv, eqc, var);
986+
return intersect_union((jl_uniontype_t*)b, a, penv, eqc, recheck_tuple_intersection, var);
977987
if (a == (jl_value_t*)jl_any_type || a == jl_ANY_flag) return b;
978988
if (b == (jl_value_t*)jl_any_type || b == jl_ANY_flag) return a;
979989
// tuple
980990
if (jl_is_tuple_type(a)) {
981991
if (jl_is_tuple_type(b)) {
982-
return intersect_tuple((jl_datatype_t*)a, (jl_datatype_t*)b, penv,eqc,var);
992+
return intersect_tuple((jl_datatype_t*)a, (jl_datatype_t*)b, penv, eqc, recheck_tuple_intersection, var);
983993
}
984994
}
985995
if (jl_is_tuple_type(b)) {
986-
return jl_type_intersect(b, a, penv,eqc,var);
996+
return jl_type_intersect(b, a, penv, eqc, recheck_tuple_intersection, var);
987997
}
988998
// tag
989999
if (!jl_is_datatype(a) || !jl_is_datatype(b))
9901000
return (jl_value_t*)jl_bottom_type;
9911001
jl_datatype_t *tta = (jl_datatype_t*)a;
9921002
jl_datatype_t *ttb = (jl_datatype_t*)b;
9931003
if (tta->name == ttb->name)
994-
return (jl_value_t*)intersect_tag(tta, ttb, penv, eqc, var);
1004+
return (jl_value_t*)intersect_tag(tta, ttb, penv, eqc, recheck_tuple_intersection, var);
9951005
jl_datatype_t *super = NULL;
9961006
jl_datatype_t *sub = NULL;
9971007
jl_value_t *env = NULL;
@@ -1055,10 +1065,10 @@ static jl_value_t *jl_type_intersect(jl_value_t *a, jl_value_t *b,
10551065
if (var == covariant &&
10561066
sub == (jl_datatype_t*)sub->name->primary &&
10571067
jl_has_typevars_from((jl_value_t*)sub->super, ((jl_datatype_t*)sub->name->primary)->parameters))
1058-
env = approxify_type((jl_datatype_t*)sub->super, ((jl_datatype_t*)sub->name->primary)->parameters);
1068+
env = approxify_type((jl_datatype_t*)sub->super, ((jl_datatype_t*)sub->name->primary)->parameters, recheck_tuple_intersection);
10591069
else
10601070
env = (jl_value_t*)sub->super;
1061-
super = (jl_datatype_t*)jl_type_intersect((jl_value_t*)env, (jl_value_t*)super, penv, eqc, var);
1071+
super = (jl_datatype_t*)jl_type_intersect((jl_value_t*)env, (jl_value_t*)super, penv, eqc, recheck_tuple_intersection, var);
10621072

10631073
if ((jl_value_t*)super == jl_bottom_type) {
10641074
JL_GC_POP();
@@ -1128,7 +1138,7 @@ static jl_value_t *jl_type_intersect(jl_value_t *a, jl_value_t *b,
11281138
for(int e=0; e < jl_svec_len(env); e+=2) {
11291139
if (jl_svecref(env, e) == tp) {
11301140
elt = jl_type_intersect(elt, jl_svecref(env, e+1),
1131-
penv, eqc, invariant);
1141+
penv, eqc, recheck_tuple_intersection, invariant);
11321142
// note: elt might be Union{} if "Union{}" was the type parameter
11331143
break;
11341144
}
@@ -1490,14 +1500,14 @@ jl_value_t *jl_type_intersection_matching(jl_value_t *a, jl_value_t *b,
14901500
jl_value_t **pti = &rts[0];
14911501
jl_value_t **extraroot = &rts[1];
14921502

1493-
recheck_tuple_intersection = 0;
1503+
int recheck_tuple_intersection = 0;
14941504
JL_TRY {
14951505
// This is kind of awful, but an inner call to instantiate_type
14961506
// might fail due to a mismatched type parameter. The problem is
14971507
// that we allow Range{T} to exist, even though the declaration of
14981508
// Range specifies Range{T<:Real}. Therefore intersection cannot see
14991509
// that some parameter values actually don't match.
1500-
*pti = jl_type_intersect(a, b, &env, &eqc, covariant);
1510+
*pti = jl_type_intersect(a, b, &env, &eqc, &recheck_tuple_intersection, covariant);
15011511
}
15021512
JL_CATCH {
15031513
*pti = (jl_value_t*)jl_bottom_type;
@@ -1511,8 +1521,8 @@ jl_value_t *jl_type_intersection_matching(jl_value_t *a, jl_value_t *b,
15111521
int e;
15121522

15131523
if (recheck_tuple_intersection) {
1514-
for(e=0; e < eqc.n; e+=2) {
1515-
jl_value_t *val = eqc.data[e+1];
1524+
for (e = 0; e < eqc.n; e += 2) {
1525+
jl_value_t *val = eqc.data[e + 1];
15161526
if (jl_is_long(val))
15171527
break;
15181528
}
@@ -1526,7 +1536,7 @@ jl_value_t *jl_type_intersection_matching(jl_value_t *a, jl_value_t *b,
15261536
to find all other constraints on N first, then do intersection
15271537
again with that knowledge.
15281538
*/
1529-
*pti = jl_type_intersect(a, b, &env, &eqc, covariant);
1539+
*pti = jl_type_intersect(a, b, &env, &eqc, &recheck_tuple_intersection, covariant);
15301540
if (*pti == (jl_value_t*)jl_bottom_type) {
15311541
JL_GC_POP();
15321542
return *pti;

0 commit comments

Comments
 (0)