-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
gf.c
1990 lines (1888 loc) · 70.9 KB
/
gf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is a part of Julia. License is MIT: http://julialang.org/license
/*
Generic Functions
. method table and lookup
. GF constructor, add_method
. dispatch
. static parameter inference
. method specialization, invoking type inference
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#ifdef _OS_WINDOWS_
#include <malloc.h>
#endif
#include "julia.h"
#include "julia_internal.h"
#ifdef __cplusplus
extern "C" {
#endif
static jl_value_t *jl_apply_unspecialized(jl_function_t *meth, jl_value_t **args, uint32_t nargs)
{
jl_function_t *unspecialized = meth->linfo->unspecialized;
assert(unspecialized != jl_bottom_func);
if (meth->env == (jl_value_t*)jl_emptysvec) {
return jl_apply(unspecialized, args, nargs);
}
else {
jl_function_t *closuremeth = jl_new_closure(unspecialized->fptr, meth->env, unspecialized->linfo);
JL_GC_PUSH1(&closuremeth);
jl_value_t *v = jl_apply(closuremeth, args, nargs);
JL_GC_POP();
return v;
}
}
static jl_methtable_t *new_method_table(jl_sym_t *name, jl_module_t *module)
{
jl_methtable_t *mt = (jl_methtable_t*)jl_gc_allocobj(sizeof(jl_methtable_t));
jl_set_typeof(mt, jl_methtable_type);
mt->name = name;
mt->module = module;
mt->defs = (jl_methlist_t*)jl_nothing;
mt->cache = (jl_methlist_t*)jl_nothing;
mt->cache_arg1 = (jl_array_t*)jl_nothing;
mt->cache_targ = (jl_array_t*)jl_nothing;
mt->max_args = 0;
mt->kwsorter = NULL;
#ifdef JL_GF_PROFILE
mt->ncalls = 0;
#endif
return mt;
}
static int cache_match_by_type(jl_value_t **types, size_t n, jl_tupletype_t *sig, int va)
{
if (!va && n > jl_datatype_nfields(sig))
return 0;
if (jl_datatype_nfields(sig) > n) {
if (!(n == jl_datatype_nfields(sig)-1 && va))
return 0;
}
size_t i;
for(i=0; i < n; i++) {
jl_value_t *decl = jl_field_type(sig, i);
if (i == jl_datatype_nfields(sig)-1) {
if (va) {
jl_value_t *t = jl_tparam0(decl);
for(; i < n; i++) {
if (!jl_subtype(types[i], t, 0))
return 0;
}
return 1;
}
}
jl_value_t *a = types[i];
if (jl_is_datatype(a) && jl_is_datatype(decl) &&
((jl_datatype_t*)decl)->name == jl_type_type->name &&
((jl_datatype_t*)a )->name == jl_type_type->name) {
jl_value_t *tp0 = jl_tparam0(decl);
if (tp0 == (jl_value_t*)jl_typetype_tvar) {
// in the case of Type{T}, the types don't have
// to match exactly either. this is cached as Type{T}.
// analogous to the situation with tuples.
}
else {
if (!jl_types_equal(jl_tparam0(a), tp0))
return 0;
}
}
else if (decl == (jl_value_t*)jl_any_type) {
}
else {
if (!jl_types_equal(a, decl))
return 0;
}
}
return 1;
}
static inline int cache_match(jl_value_t **args, size_t n, jl_tupletype_t *sig,
int va, size_t lensig)
{
// NOTE: This function is a huge performance hot spot!!
for(size_t i=0; i < n; i++) {
jl_value_t *decl = jl_field_type(sig, i);
if (i == lensig-1) {
if (va) {
jl_value_t *t = jl_tparam0(decl);
for(; i < n; i++) {
if (!jl_subtype(args[i], t, 1))
return 0;
}
return 1;
}
}
jl_value_t *a = args[i];
if (decl == (jl_value_t*)jl_any_type) {
}
else if ((jl_value_t*)jl_typeof(a) == decl) {
/*
we know there are only concrete types here, and types are
hash-consed, so pointer comparison should work.
*/
}
else if (jl_is_type_type(decl) && jl_is_type(a)) {
jl_value_t *tp0 = jl_tparam0(decl);
if (tp0 == (jl_value_t*)jl_typetype_tvar) {
// in the case of Type{T}, the types don't have
// to match exactly either. this is cached as Type{T}.
// analogous to the situation with tuples.
}
else {
if (a!=tp0 && !jl_types_equal(a,tp0))
return 0;
}
}
else {
return 0;
}
}
return 1;
}
static inline
jl_methlist_t *mtcache_hash_lookup(jl_array_t *a, jl_value_t *ty, int tparam)
{
uptrint_t uid = ((jl_datatype_t*)ty)->uid;
jl_methlist_t *ml = (jl_methlist_t*)jl_cellref(a, uid & (a->nrows-1));
if (ml && ml!=(void*)jl_nothing) {
jl_value_t *t = jl_field_type(ml->sig, 0);
if (tparam) t = jl_tparam0(t);
if (t == ty)
return ml;
}
return (jl_methlist_t*)jl_nothing;
}
static void mtcache_rehash(jl_array_t **pa, jl_value_t* parent)
{
size_t len = (*pa)->nrows;
jl_value_t **d = (jl_value_t**)(*pa)->data;
jl_array_t *n = jl_alloc_cell_1d(len*2);
jl_value_t **nd = (jl_value_t**)n->data;
size_t i;
for(i=0; i < len; i++) {
jl_methlist_t *ml = (jl_methlist_t*)d[i];
if (ml && ml!=(jl_methlist_t*)jl_nothing) {
jl_value_t *t = jl_field_type(ml->sig,0);
if (jl_is_type_type(t))
t = jl_tparam0(t);
uptrint_t uid = ((jl_datatype_t*)t)->uid;
nd[uid & (len*2-1)] = (jl_value_t*)ml;
}
}
jl_gc_wb(parent, n);
*pa = n;
}
static jl_methlist_t **mtcache_hash_bp(jl_array_t **pa, jl_value_t *ty,
int tparam, jl_value_t* parent)
{
uptrint_t uid;
if (jl_is_datatype(ty) && (uid = ((jl_datatype_t*)ty)->uid)) {
while (1) {
jl_methlist_t **pml = &((jl_methlist_t**)jl_array_data(*pa))[uid & ((*pa)->nrows-1)];
if (*pml == NULL || *pml == (jl_methlist_t*)jl_nothing) {
*pml = (jl_methlist_t*)jl_nothing;
return pml;
}
jl_value_t *t = jl_field_type((*pml)->sig,0);
if (tparam) t = jl_tparam0(t);
if (t == ty)
return pml;
mtcache_rehash(pa, parent);
}
}
return NULL;
}
/*
Method caches are divided into three parts: one for signatures where
the first argument is a singleton kind (Type{Foo}), one indexed by the
UID of the first argument's type in normal cases, and a fallback
table of everything else.
*/
static jl_function_t *jl_method_table_assoc_exact_by_type(jl_methtable_t *mt, jl_tupletype_t *types)
{
jl_methlist_t *ml = (jl_methlist_t*)jl_nothing;
if (jl_datatype_nfields(types) > 0) {
jl_value_t *ty = jl_tparam0(types);
if (jl_is_type_type(ty)) {
jl_value_t *a0 = jl_tparam0(ty);
if (mt->cache_targ != (void*)jl_nothing && jl_is_datatype(a0)) {
ml = mtcache_hash_lookup(mt->cache_targ, a0, 1);
if (ml!=(jl_methlist_t*)jl_nothing)
goto mt_assoc_bt_lkup;
}
}
if (mt->cache_arg1 != (void*)jl_nothing && jl_is_datatype(ty)) {
ml = mtcache_hash_lookup(mt->cache_arg1, ty, 0);
}
}
if (ml == (void*)jl_nothing)
ml = mt->cache;
mt_assoc_bt_lkup:
while (ml != (void*)jl_nothing) {
if (cache_match_by_type(jl_svec_data(types->parameters), jl_datatype_nfields(types),
ml->sig, ml->va)) {
return ml->func;
}
ml = ml->next;
}
return jl_bottom_func;
}
static jl_function_t *jl_method_table_assoc_exact(jl_methtable_t *mt, jl_value_t **args, size_t n)
{
// NOTE: This function is a huge performance hot spot!!
jl_methlist_t *ml = (jl_methlist_t*)jl_nothing;
if (n > 0) {
jl_value_t *a0 = args[0];
jl_value_t *ty = (jl_value_t*)jl_typeof(a0);
if (mt->cache_targ != (void*)jl_nothing && ty == (jl_value_t*)jl_datatype_type) {
ml = mtcache_hash_lookup(mt->cache_targ, a0, 1);
if (ml != (void*)jl_nothing)
goto mt_assoc_lkup;
}
assert(jl_is_datatype(ty));
if (mt->cache_arg1 != (void*)jl_nothing) {
ml = mtcache_hash_lookup(mt->cache_arg1, ty, 0);
if (ml != (void*)jl_nothing) {
if (ml->next==(void*)jl_nothing && n==1 && jl_datatype_nfields(ml->sig)==1)
return ml->func;
if (n==2) {
// some manually-unrolled common special cases
jl_value_t *a1 = args[1];
if (!jl_is_tuple(a1)) { // issue #6426
jl_methlist_t *mn = ml;
if (jl_datatype_nfields(mn->sig)==2 &&
jl_tparam(mn->sig,1)==(jl_value_t*)jl_typeof(a1))
return mn->func;
mn = mn->next;
if (mn!=(void*)jl_nothing && jl_datatype_nfields(mn->sig)==2 &&
jl_tparam(mn->sig,1)==(jl_value_t*)jl_typeof(a1))
return mn->func;
}
}
}
}
}
if (ml == (void*)jl_nothing)
ml = mt->cache;
mt_assoc_lkup:
while (ml != (void*)jl_nothing) {
size_t lensig = jl_datatype_nfields(ml->sig);
if (lensig == n || (ml->va && lensig <= n+1)) {
if (cache_match(args, n, ml->sig, ml->va, lensig)) {
return ml->func;
}
}
ml = ml->next;
}
return jl_bottom_func;
}
// return a new lambda-info that has some extra static parameters merged in.
jl_lambda_info_t *jl_add_static_parameters(jl_lambda_info_t *l, jl_svec_t *sp)
{
JL_GC_PUSH1(&sp);
if (jl_svec_len(l->sparams) > 0)
sp = jl_svec_append(sp, l->sparams);
jl_lambda_info_t *nli = jl_new_lambda_info(l->ast, sp, l->module);
nli->name = l->name;
nli->fptr = l->fptr;
nli->file = l->file;
nli->line = l->line;
nli->def = l->def;
JL_GC_POP();
return nli;
}
jl_function_t *jl_instantiate_method(jl_function_t *f, jl_svec_t *sp)
{
if (f->linfo == NULL)
return f;
jl_function_t *nf = jl_new_closure(f->fptr, f->env, NULL);
JL_GC_PUSH1(&nf);
nf->linfo = jl_add_static_parameters(f->linfo, sp);
jl_gc_wb(nf, nf->linfo);
JL_GC_POP();
return nf;
}
// append values of static parameters to closure environment
static jl_function_t *with_appended_env(jl_function_t *meth, jl_svec_t *sparams)
{
if (sparams == jl_emptysvec)
return meth;
jl_value_t *temp = (jl_value_t*)jl_alloc_svec(jl_svec_len(sparams)/2);
JL_GC_PUSH1(&temp);
size_t i;
for(i=0; i < jl_svec_len(temp); i++) {
jl_svecset(temp, i, jl_svecref(sparams,i*2+1));
}
temp = (jl_value_t*)jl_svec_append((jl_svec_t*)meth->env, (jl_svec_t*)temp);
meth = jl_new_closure(meth->fptr, temp, meth->linfo);
JL_GC_POP();
return meth;
}
// make a new method that calls the generated code from the given linfo
jl_function_t *jl_reinstantiate_method(jl_function_t *f, jl_lambda_info_t *li)
{
return jl_new_closure(NULL, f->env, li);
}
static
jl_methlist_t *jl_method_list_insert(jl_methlist_t **pml, jl_tupletype_t *type,
jl_function_t *method, jl_svec_t *tvars,
int check_amb, int8_t isstaged, jl_value_t *parent);
jl_function_t *jl_method_cache_insert(jl_methtable_t *mt, jl_tupletype_t *type,
jl_function_t *method)
{
jl_methlist_t **pml = &mt->cache;
jl_value_t* cache_array = NULL;
if (jl_datatype_nfields(type) > 0) {
jl_value_t *t0 = jl_tparam0(type);
uptrint_t uid=0;
// if t0 != jl_typetype_type and the argument is Type{...}, this
// method has specializations for singleton kinds and we use
// the table indexed for that purpose.
if (t0 != (jl_value_t*)jl_typetype_type && jl_is_type_type(t0)) {
jl_value_t *a0 = jl_tparam0(t0);
if (jl_is_datatype(a0))
uid = ((jl_datatype_t*)a0)->uid;
if (uid > 0) {
if (mt->cache_targ == (void*)jl_nothing) {
mt->cache_targ = jl_alloc_cell_1d(16);
jl_gc_wb(mt, mt->cache_targ);
}
pml = mtcache_hash_bp(&mt->cache_targ, a0, 1, (jl_value_t*)mt);
cache_array = (jl_value_t*)mt->cache_targ;
goto ml_do_insert;
}
}
if (jl_is_datatype(t0))
uid = ((jl_datatype_t*)t0)->uid;
if (uid > 0) {
if (mt->cache_arg1 == (void*)jl_nothing) {
mt->cache_arg1 = jl_alloc_cell_1d(16);
jl_gc_wb(mt, mt->cache_arg1);
}
pml = mtcache_hash_bp(&mt->cache_arg1, t0, 0, (jl_value_t*)mt);
cache_array = (jl_value_t*)mt->cache_arg1;
}
}
ml_do_insert:
return jl_method_list_insert(pml, type, method, jl_emptysvec, 0, 0, cache_array ? cache_array : (jl_value_t*)mt)->func;
}
/*
run type inference on lambda "li" in-place, for given argument types.
"def" is the original method definition of which this is an instance;
can be equal to "li" if not applicable.
*/
int jl_in_inference = 0;
void jl_type_infer(jl_lambda_info_t *li, jl_tupletype_t *argtypes, jl_lambda_info_t *def)
{
int last_ii = jl_in_inference;
jl_in_inference = 1;
if (jl_typeinf_func != NULL) {
// TODO: this should be done right before code gen, so if it is
// interrupted we can try again the next time the function is
// called
assert(li->inInference == 0);
li->inInference = 1;
jl_value_t *fargs[4];
fargs[0] = (jl_value_t*)li;
fargs[1] = (jl_value_t*)argtypes;
fargs[2] = (jl_value_t*)jl_emptysvec;
fargs[3] = (jl_value_t*)def;
#ifdef TRACE_INFERENCE
jl_printf(JL_STDERR,"inference on %s", li->name->name);
jl_static_show_func_sig(JL_STDERR, (jl_value_t*)argtypes);
jl_printf(JL_STDERR, "\n");
#endif
#ifdef ENABLE_INFERENCE
jl_value_t *newast = jl_apply(jl_typeinf_func, fargs, 4);
li->ast = jl_fieldref(newast, 0);
jl_gc_wb(li, li->ast);
li->inferred = 1;
#endif
li->inInference = 0;
}
jl_in_inference = last_ii;
}
jl_value_t *jl_nth_slot_type(jl_tupletype_t *sig, size_t i)
{
size_t len = jl_datatype_nfields(sig);
if (len == 0)
return NULL;
if (i < len-1)
return jl_tparam(sig, i);
if (jl_is_vararg_type(jl_tparam(sig,len-1)))
return jl_tparam0(jl_tparam(sig,len-1));
if (i == len-1)
return jl_tparam(sig, i);
return NULL;
}
static int very_general_type(jl_value_t *t)
{
return (t && (t==(jl_value_t*)jl_any_type || t == (jl_value_t*)jl_type_type ||
(jl_is_typevar(t) &&
((jl_tvar_t*)t)->ub==(jl_value_t*)jl_any_type)));
}
static int is_kind(jl_value_t *v)
{
return (v==(jl_value_t*)jl_uniontype_type ||
v==(jl_value_t*)jl_datatype_type ||
v==(jl_value_t*)jl_typector_type);
}
static jl_value_t *ml_matches(jl_methlist_t *ml, jl_value_t *type,
jl_sym_t *name, int lim);
static jl_function_t *cache_method(jl_methtable_t *mt, jl_tupletype_t *type,
jl_function_t *method, jl_tupletype_t *decl,
jl_svec_t *sparams, int8_t isstaged)
{
size_t i;
int need_guard_entries = 0;
jl_value_t *temp=NULL;
jl_value_t *temp2=NULL;
jl_function_t *newmeth=NULL;
jl_svec_t *newparams=NULL;
jl_svec_t *limited=NULL;
jl_tupletype_t *origtype = type; // TODO: root?
JL_GC_PUSH5(&temp, &temp2, &newmeth, &newparams, &limited);
size_t np = jl_nparams(type);
newparams = jl_svec_copy(type->parameters);
for (i=0; i < np; i++) {
jl_value_t *elt = jl_tparam(type,i);
jl_value_t *decl_i = jl_nth_slot_type(decl,i);
if (!isstaged && jl_is_type_type(elt) && jl_is_tuple_type(jl_tparam0(elt)) &&
!(jl_subtype(decl_i, (jl_value_t*)jl_type_type, 0) && !is_kind(decl_i))) {
jl_methlist_t *curr = mt->defs;
int ok=1;
while (curr != (void*)jl_nothing) {
jl_value_t *slottype = jl_nth_slot_type(curr->sig, i);
if (slottype && curr->func!=method) {
if (jl_is_type_type(slottype) &&
jl_type_intersection(slottype, decl_i) != jl_bottom_type) {
ok=0;
break;
}
}
curr = curr->next;
}
if (ok) {
elt = jl_typeof(jl_tparam0(elt));
jl_svecset(newparams, i, elt);
}
}
int set_to_any = 0;
if (decl_i == jl_ANY_flag) {
// don't specialize on slots marked ANY
jl_svecset(newparams, i, (jl_value_t*)jl_any_type);
temp2 = (jl_value_t*)jl_svec_copy(newparams);
temp2 = (jl_value_t*)jl_apply_tuple_type((jl_svec_t*)temp2);
int nintr=0;
jl_methlist_t *curr = mt->defs;
// if this method is the only match even with the current slot
// set to Any, then it is safe to cache it that way.
while (curr != (void*)jl_nothing && curr->func!=method) {
if (jl_type_intersection((jl_value_t*)curr->sig,
(jl_value_t*)temp2) !=
(jl_value_t*)jl_bottom_type) {
nintr++;
break;
}
curr = curr->next;
}
if (nintr) {
// TODO: even if different specializations of this slot need
// separate cache entries, have them share code.
jl_svecset(newparams, i, jl_tparam(type, i));
}
else {
set_to_any = 1;
}
}
if (set_to_any || isstaged) {
}
else if (jl_is_type_type(elt) && jl_is_type_type(jl_tparam0(elt)) &&
// give up on specializing static parameters for Type{Type{Type{...}}}
(jl_is_type_type(jl_tparam0(jl_tparam0(elt))) ||
decl_i==NULL || !jl_has_typevars(decl_i))) {
/*
actual argument was Type{...}, we computed its type as
Type{Type{...}}. we must avoid unbounded nesting here, so
cache the signature as Type{T}, unless something more
specific like Type{Type{Int32}} was actually declared.
this can be determined using a type intersection.
*/
if (i < jl_nparams(decl)) {
jl_value_t *declt = jl_tparam(decl,i);
// for T..., intersect with T
if (jl_is_vararg_type(declt))
declt = jl_tparam0(declt);
jl_value_t *di = jl_type_intersection(declt, (jl_value_t*)jl_typetype_type);
if (is_kind(di))
// issue #11355: DataType has a UID and so takes precedence in the cache
jl_svecset(newparams, i, (jl_value_t*)jl_typetype_type);
else
jl_svecset(newparams, i, di);
// TODO: recompute static parameter values, so in extreme cases we
// can give `T=Type` instead of `T=Type{Type{Type{...`.
}
else {
jl_svecset(newparams, i, (jl_value_t*)jl_typetype_type);
}
need_guard_entries = 1;
assert(jl_svecref(newparams,i) != (jl_value_t*)jl_bottom_type);
}
else if (jl_is_type_type(elt) && very_general_type(decl_i) &&
!jl_has_typevars(decl_i)) {
/*
here's a fairly complex heuristic: if this argument slot's
declared type is Any, and no definition overlaps with Type
for this slot, then don't specialize for every Type that
might be passed.
Since every type x has its own type Type{x}, this would be
excessive specialization for an Any slot.
TypeConstructors are problematic because they can be alternate
representations of any type. Extensionally, TC == TC.body, but
typeof(TC) != typeof(TC.body). This creates an ambiguity:
Type{TC} is type-equal to Type{TC.body}, yet a slot
x::TypeConstructor matches the first but not the second, while
also matching all other TypeConstructors. This means neither
Type{TC} nor TypeConstructor is more specific.
To solve this, we identify "kind slots", which are slots
for which some definition specifies a kind (e.g. DataType).
Those tend to be in reflective functions that look at types
themselves. For these slots we specialize on jl_typeof(T) instead
of Type{T}, i.e. the kind of the type rather than the specific
type.
*/
int ok=1, kindslot=0;
jl_methlist_t *curr = mt->defs;
jl_value_t *kind = (jl_value_t*)jl_typeof(jl_tparam0(elt));
while (curr != (void*)jl_nothing) {
jl_value_t *slottype = jl_nth_slot_type(curr->sig, i);
if (slottype && curr->func!=method) {
if (slottype == kind) {
ok=0;
break;
}
if (is_kind(slottype))
kindslot=1;
}
curr = curr->next;
}
if (ok) {
if (kindslot) {
jl_svecset(newparams, i, kind);
}
else {
curr = mt->defs;
while (curr != (void*)jl_nothing) {
jl_value_t *slottype = jl_nth_slot_type(curr->sig, i);
if (slottype && curr->func!=method) {
if (!very_general_type(slottype) &&
jl_type_intersection(slottype, (jl_value_t*)jl_type_type) !=
(jl_value_t*)jl_bottom_type) {
ok=0;
break;
}
}
curr = curr->next;
}
if (ok) {
jl_svecset(newparams, i, jl_typetype_type);
need_guard_entries = 1;
}
}
}
}
else if (is_kind(decl_i)) {
// if a slot is specialized for a particular kind, it can be
// considered a reflective method and so only needs to be
// specialized for type representation, not type extent.
jl_methlist_t *curr = mt->defs;
int ok=1;
while (curr != (void*)jl_nothing) {
jl_value_t *slottype = jl_nth_slot_type(curr->sig, i);
if (slottype && curr->func!=method) {
if (jl_is_type_type(slottype) &&
jl_type_intersection(slottype, decl_i) != jl_bottom_type) {
ok=0;
break;
}
}
curr = curr->next;
}
if (ok)
jl_svecset(newparams, i, decl_i);
}
}
// for varargs methods, only specialize up to max_args.
// in general, here we want to find the biggest type that's not a
// supertype of any other method signatures. so far we are conservative
// and the types we find should be bigger.
if (!isstaged && jl_nparams(type) > mt->max_args && jl_is_va_tuple(decl)) {
size_t nspec = mt->max_args + 2;
limited = jl_alloc_svec(nspec);
for(i=0; i < nspec-1; i++) {
jl_svecset(limited, i, jl_svecref(newparams, i));
}
jl_value_t *lasttype = jl_svecref(newparams,i-1);
// if all subsequent arguments are subtypes of lasttype, specialize
// on that instead of decl. for example, if decl is
// (Any...)
// and type is
// (Symbol, Symbol, Symbol)
// then specialize as (Symbol...), but if type is
// (Symbol, Int32, Expr)
// then specialize as (Any...)
size_t j = i;
int all_are_subtypes=1;
for(; j < jl_svec_len(newparams); j++) {
if (!jl_subtype(jl_svecref(newparams,j), lasttype, 0)) {
all_are_subtypes = 0;
break;
}
}
if (all_are_subtypes) {
// avoid Type{Type{...}...}...
if (jl_is_type_type(lasttype) && jl_is_type_type(jl_tparam0(lasttype)))
lasttype = (jl_value_t*)jl_type_type;
jl_svecset(limited, i, jl_wrap_vararg(lasttype));
}
else {
jl_value_t *lastdeclt = jl_tparam(decl,jl_nparams(decl)-1);
if (jl_svec_len(sparams) > 0) {
lastdeclt = (jl_value_t*)
jl_instantiate_type_with((jl_value_t*)lastdeclt,
sparams->data,
jl_svec_len(sparams)/2);
}
jl_svecset(limited, i, lastdeclt);
}
type = jl_apply_tuple_type(limited);
temp2 = (jl_value_t*)type;
// now there is a problem: the computed signature is more
// general than just the given arguments, so it might conflict
// with another definition that doesn't have cache instances yet.
// to fix this, we insert guard cache entries for all intersections
// of this signature and definitions. those guard entries will
// supersede this one in conflicted cases, alerting us that there
// should actually be a cache miss.
need_guard_entries = 1;
}
else {
type = jl_apply_tuple_type(newparams);
temp2 = (jl_value_t*)type;
}
if (need_guard_entries) {
temp = ml_matches(mt->defs, (jl_value_t*)type, lambda_sym, -1);
int unmatched_tvars = 0;
for(i=0; i < jl_array_len(temp); i++) {
jl_value_t *m = jl_cellref(temp, i);
jl_value_t *env = jl_svecref(m,1);
for(int k=1; k < jl_svec_len(env); k+=2) {
if (jl_is_typevar(jl_svecref(env,k))) {
unmatched_tvars = 1; break;
}
}
if (unmatched_tvars) {
// if distinguishing a guard entry from the generalized signature
// would require matching type vars then bail out, since the
// method cache matching algorithm cannot do that.
type = origtype; break;
}
}
if (!unmatched_tvars) {
for(i=0; i < jl_array_len(temp); i++) {
jl_value_t *m = jl_cellref(temp, i);
if (((jl_methlist_t*)jl_svecref(m,2))->func != method) {
jl_method_cache_insert(mt, (jl_tupletype_t*)jl_svecref(m, 0), jl_bottom_func);
}
}
}
}
// here we infer types and specialize the method
jl_array_t *lilist=NULL;
jl_lambda_info_t *li=NULL;
if (method->linfo && method->linfo->specializations!=NULL) {
// reuse code already generated for this combination of lambda and
// arguments types. this happens for inner generic functions where
// a new closure is generated on each call to the enclosing function.
lilist = method->linfo->specializations;
int k;
for(k=0; k < lilist->nrows; k++) {
li = (jl_lambda_info_t*)jl_cellref(lilist, k);
if (jl_types_equal((jl_value_t*)li->specTypes, (jl_value_t*)type))
break;
}
if (k == lilist->nrows) lilist=NULL;
}
if (lilist != NULL && !li->inInference) {
assert(li);
newmeth = jl_reinstantiate_method(method, li);
(void)jl_method_cache_insert(mt, type, newmeth);
JL_GC_POP();
return newmeth;
}
else {
if (jl_options.compile_enabled == JL_OPTIONS_COMPILE_OFF) {
if (method->linfo->unspecialized == NULL) {
jl_printf(JL_STDERR,"code missing for %s", method->linfo->name->name);
jl_static_show_func_sig(JL_STDERR, (jl_value_t*)type);
jl_printf(JL_STDERR, " sysimg may not have been built with --compile=all\n");
exit(1);
}
jl_function_t *unspec = method->linfo->unspecialized;
if (method->env == (jl_value_t*)jl_emptysvec)
newmeth = unspec;
else
newmeth = jl_new_closure(unspec->fptr, method->env, unspec->linfo);
if (sparams != jl_emptysvec)
newmeth = with_appended_env(newmeth, sparams);
(void)jl_method_cache_insert(mt, type, newmeth);
JL_GC_POP();
return newmeth;
}
else {
newmeth = jl_instantiate_method(method, sparams);
}
}
/*
if "method" itself can ever be compiled, for example for use as
an unspecialized method (see below), then newmeth->fptr might point
to some slow compiled code instead of jl_trampoline, meaning our
type-inferred code would never get compiled. this can be fixed with
the commented-out snippet below.
NOTE: this is now needed when we start with a system image compiled
with --compile=all.
*/
/*
assert(!(newmeth->linfo && newmeth->linfo->ast) ||
newmeth->fptr == &jl_trampoline);
*/
if (newmeth->linfo && newmeth->linfo->ast && newmeth->fptr != &jl_trampoline) {
newmeth->fptr = &jl_trampoline;
}
(void)jl_method_cache_insert(mt, type, newmeth);
if (newmeth->linfo != NULL && newmeth->linfo->sparams == jl_emptysvec) {
// when there are no static parameters, one unspecialized version
// of a function can be shared among all cached specializations.
if (method->linfo->unspecialized == NULL) {
method->linfo->unspecialized =
jl_instantiate_method(method, jl_emptysvec);
if (method->env != (jl_value_t*)jl_emptysvec)
method->linfo->unspecialized->env = NULL;
jl_gc_wb(method->linfo, method->linfo->unspecialized);
}
newmeth->linfo->unspecialized = method->linfo->unspecialized;
jl_gc_wb(newmeth->linfo, newmeth->linfo->unspecialized);
}
if (newmeth->linfo != NULL && newmeth->linfo->ast != NULL) {
newmeth->linfo->specTypes = type;
jl_gc_wb(newmeth->linfo, type);
jl_array_t *spe = method->linfo->specializations;
if (spe == NULL) {
spe = jl_alloc_cell_1d(1);
jl_cellset(spe, 0, newmeth->linfo);
}
else {
jl_cell_1d_push(spe, (jl_value_t*)newmeth->linfo);
}
method->linfo->specializations = spe;
jl_gc_wb(method->linfo, method->linfo->specializations);
jl_type_infer(newmeth->linfo, type, method->linfo);
}
JL_GC_POP();
return newmeth;
}
static jl_value_t *lookup_match(jl_value_t *a, jl_value_t *b, jl_svec_t **penv,
jl_svec_t *tvars)
{
jl_value_t *ti = jl_type_intersection_matching(a, b, penv, tvars);
if (ti == (jl_value_t*)jl_bottom_type)
return ti;
JL_GC_PUSH1(&ti);
assert(jl_is_svec(*penv));
jl_value_t **ee = (jl_value_t**)alloca(sizeof(void*) * jl_svec_len(*penv));
int n=0;
// only keep vars in tvars list
jl_value_t **tvs;
int tvarslen;
if (jl_is_typevar(tvars)) {
tvs = (jl_value_t**)&tvars;
tvarslen = 1;
}
else {
tvs = jl_svec_data(tvars);
tvarslen = jl_svec_len(tvars);
}
int l = jl_svec_len(*penv);
for(int i=0; i < l; i+=2) {
jl_value_t *v = jl_svecref(*penv,i);
jl_value_t *val = jl_svecref(*penv,i+1);
for(int j=0; j < tvarslen; j++) {
if (v == tvs[j]) {
ee[n++] = v;
ee[n++] = val;
/*
since "a" is a concrete type, we assume that
(a∩b != Union{}) => a<:b. However if a static parameter is
forced to equal Union{}, then part of "b" might become Union{},
and therefore a subtype of "a". For example
(Type{Union{}},Int) ∩ (Type{T},T)
issue #5254
*/
if (val == (jl_value_t*)jl_bottom_type) {
if (!jl_subtype(a, ti, 0)) {
JL_GC_POP();
return (jl_value_t*)jl_bottom_type;
}
}
}
}
}
if (n != l) {
jl_svec_t *en = jl_alloc_svec_uninit(n);
memcpy(en->data, ee, n*sizeof(void*));
*penv = en;
}
JL_GC_POP();
return ti;
}
DLLEXPORT jl_function_t *jl_instantiate_staged(jl_methlist_t *m, jl_tupletype_t *tt, jl_svec_t *env)
{
jl_expr_t *ex = NULL;
jl_expr_t *oldast = NULL;
jl_function_t *func = NULL;
JL_GC_PUSH3(&ex, &oldast, &func);
if (jl_is_expr(m->func->linfo->ast))
oldast = (jl_expr_t*)m->func->linfo->ast;
else
oldast = (jl_expr_t*)jl_uncompress_ast(m->func->linfo, m->func->linfo->ast);
assert(oldast->head == lambda_sym);
ex = jl_exprn(arrow_sym, 2);
jl_array_t *oldargnames = jl_lam_args(oldast);
jl_expr_t *argnames = jl_exprn(tuple_sym, jl_array_len(oldargnames));
jl_cellset(ex->args, 0, argnames);
for (size_t i = 0; i < jl_array_len(oldargnames); ++i) {
jl_value_t *arg = jl_cellref(oldargnames,i);
if (jl_is_expr(arg)) {
assert(((jl_expr_t*)arg)->head == colons_sym);
arg = jl_cellref(((jl_expr_t*)arg)->args,0);
assert(jl_is_symbol(arg));
jl_expr_t *dd_expr = jl_exprn(dots_sym,1);
jl_cellset(dd_expr->args,0,arg);
jl_cellset(argnames->args,i,dd_expr);
}
else {
assert(jl_is_symbol(arg));
jl_cellset(argnames->args,i,arg);
}
}
func = with_appended_env(m->func, env);
jl_expr_t *body = jl_exprn(jl_symbol("block"), 2);
jl_cellset(ex->args, 1, body);
jl_expr_t *linenode = jl_exprn(line_sym, 2);
jl_cellset(body->args, 0, linenode);
jl_cellset(linenode->args, 0, jl_box_long(m->func->linfo->line));
jl_cellset(linenode->args, 1, m->func->linfo->file);
jl_cellset(body->args, 1, jl_apply(func, jl_svec_data(tt->parameters), jl_nparams(tt)));
if (m->tvars != jl_emptysvec) {
// mark this function as having the same static parameters as the generator
size_t nsp = jl_is_typevar(m->tvars) ? 1 : jl_svec_len(m->tvars);
oldast = jl_exprn(jl_symbol("with-static-parameters"), nsp+1);
jl_exprarg(oldast,0) = (jl_value_t*)ex;
// (with-static-parameters func_expr sp_1 sp_2 ...)
if (jl_is_typevar(m->tvars)) {
jl_exprarg(oldast,1) = (jl_value_t*)((jl_tvar_t*)m->tvars)->name;
}
else {
for(size_t i=0; i < nsp; i++)
jl_exprarg(oldast,i+1) = (jl_value_t*)((jl_tvar_t*)jl_svecref(m->tvars,i))->name;
}
ex = oldast;
}
func = (jl_function_t*)jl_toplevel_eval_in(m->func->linfo->module, (jl_value_t*)ex, 1); // need to eval macros in the right module, but not give a warning for the `eval` call unless that results in a call to `eval`
func->linfo->name = m->func->linfo->name;
JL_GC_POP();
return func;
}
static jl_function_t *jl_mt_assoc_by_type(jl_methtable_t *mt, jl_datatype_t *tt, int cache, int inexact)
{
jl_methlist_t *m = mt->defs;
size_t nargs = jl_nparams(tt);
size_t i;
jl_value_t *ti=(jl_value_t*)jl_bottom_type;
jl_tupletype_t *newsig=NULL;
jl_svec_t *env = jl_emptysvec;
jl_function_t *func = NULL;
JL_GC_PUSH3(&env, &newsig, &func);
while (m != (void*)jl_nothing) {
if (m->tvars!=jl_emptysvec) {
ti = lookup_match((jl_value_t*)tt, (jl_value_t*)m->sig, &env, m->tvars);
if (ti != (jl_value_t*)jl_bottom_type) {
// parametric methods only match if all typevars are matched by
// non-typevars.
for(i=1; i < jl_svec_len(env); i+=2) {
if (jl_is_typevar(jl_svecref(env,i))) {
if (inexact) {
// "inexact" means the given type is compile-time,
// where a failure to determine the value of a
// static parameter is inconclusive.
// this is issue #3182, see test/core.jl
JL_GC_POP();
return jl_bottom_func;
}
break;
}
}
if (i >= jl_svec_len(env))
break;
ti = (jl_value_t*)jl_bottom_type;
}
}
else if (jl_tuple_subtype(jl_svec_data(tt->parameters), nargs, m->sig, 0)) {
break;
}
m = m->next;
}
if (ti == (jl_value_t*)jl_bottom_type) {
if (m != (void*)jl_nothing) {
func = m->func;
if (m->isstaged)
func = jl_instantiate_staged(m,tt,env);
if (!cache) {
JL_GC_POP();
return func;
}
jl_function_t *res = cache_method(mt, tt, func, m->sig, jl_emptysvec, m->isstaged);
JL_GC_POP();
return res;
}
JL_GC_POP();
return jl_bottom_func;