forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintrinsics.cpp
1518 lines (1428 loc) · 58.4 KB
/
intrinsics.cpp
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
namespace JL_I {
#include "intrinsics.h"
}
#include "ccall.cpp"
using namespace JL_I;
static Function *runtime_func[num_intrinsics];
static void jl_init_intrinsic_functions_codegen(Module *m)
{
std::vector<Type *> args1(0); \
args1.push_back(T_pjlvalue); \
std::vector<Type *> args2(0); \
args2.push_back(T_pjlvalue); \
args2.push_back(T_pjlvalue); \
std::vector<Type *> args3(0); \
args3.push_back(T_pjlvalue); \
args3.push_back(T_pjlvalue); \
args3.push_back(T_pjlvalue); \
std::vector<Type *> args4(0); \
args4.push_back(T_pjlvalue); \
args4.push_back(T_pjlvalue); \
args4.push_back(T_pjlvalue); \
args4.push_back(T_pjlvalue);
#define ADD_I(name, nargs) do { \
Function *func = Function::Create(FunctionType::get(T_pjlvalue, args##nargs, false), \
Function::ExternalLinkage, "jl_"#name, m); \
runtime_func[name] = func; \
add_named_global(func, &jl_##name); \
} while (0);
#define ADD_HIDDEN ADD_I
#define ALIAS(alias, base) runtime_func[alias] = runtime_func[base];
ADD_HIDDEN(reinterpret, 2);
INTRINSICS
#undef ADD_I
#undef ADD_HIDDEN
#undef ALIAS
}
extern "C" JL_DLLEXPORT uint32_t jl_get_LLVM_VERSION(void);
JL_DLLEXPORT uint32_t jl_get_LLVM_VERSION(void)
{
return 10000 * LLVM_VERSION_MAJOR + 100 * LLVM_VERSION_MINOR
#ifdef LLVM_VERSION_PATCH
+ LLVM_VERSION_PATCH
#endif
;
}
extern "C" JL_DLLEXPORT int8_t jl_is_memdebug() {
#ifdef MEMDEBUG
return true;
#else
return false;
#endif
}
/*
low-level intrinsics design: TODO: fix description below
functions like add_int expect unboxed values of matching bit-length.
every operation that can return an unboxed value does so.
this maximizes opportunities for composing functions without
unnecessary boxing.
this means that box and unbox functions might do nothing except change
the type tag of a value.
boxing is delayed until absolutely necessary, and handled at the point
where the box is needed.
*/
static Type *FTnbits(size_t nb)
{
#ifndef DISABLE_FLOAT16
if (nb == 16)
return T_float16;
else
#endif
if (nb == 32)
return T_float32;
else if (nb == 64)
return T_float64;
else if (nb == 128)
return T_float128;
else
jl_error("Unsupported Float Size");
}
// convert int type to same-size float type
static Type *FT(Type *t)
{
if (t->isFloatingPointTy())
return t;
return FTnbits(t->getPrimitiveSizeInBits());
}
// reinterpret-cast to float
static Value *FP(Value *v)
{
if (v->getType()->isFloatingPointTy())
return v;
return emit_bitcast(v, FT(v->getType()));
}
// convert float type to same-size int type
static Type *JL_INTT(Type *t)
{
if (t->isIntegerTy())
return t;
if (t->isPointerTy())
return T_size;
if (t == T_float32) return T_int32;
if (t == T_float64) return T_int64;
assert(t == T_void);
return T_void;
}
// convert float type to same-size int type (as a Julia type)
static jl_value_t *JL_JLUINTT(Type *t)
{
assert(!t->isIntegerTy());
if (t == T_float32) return (jl_value_t*)jl_uint32_type;
if (t == T_float64) return (jl_value_t*)jl_uint64_type;
if (t == T_float16) return (jl_value_t*)jl_uint16_type;
assert(t == T_void);
return jl_bottom_type;
}
static jl_value_t *JL_JLSINTT(Type *t)
{
assert(!t->isIntegerTy());
if (t == T_float32) return (jl_value_t*)jl_int32_type;
if (t == T_float64) return (jl_value_t*)jl_int64_type;
if (t == T_float16) return (jl_value_t*)jl_int16_type;
assert(t == T_void);
return jl_bottom_type;
}
// reinterpret-cast to int
static Value *JL_INT(Value *v)
{
Type *t = v->getType();
if (t->isIntegerTy())
return v;
if (t->isPointerTy())
return builder.CreatePtrToInt(v, JL_INTT(t));
return emit_bitcast(v, JL_INTT(t));
}
static Value *uint_cnvt(Type *to, Value *x)
{
Type *t = x->getType();
if (t == to) return x;
if (to->getPrimitiveSizeInBits() < x->getType()->getPrimitiveSizeInBits())
return builder.CreateTrunc(x, to);
return builder.CreateZExt(x, to);
}
#define LLVM_FP(a,b) APFloat(a,b)
static Constant *julia_const_to_llvm(void *ptr, jl_value_t *bt)
{
// assume `jl_isbits(bt)`.
// `ptr` can point to a inline field, do not read the tag from it.
if (bt == (jl_value_t*)jl_bool_type)
return ConstantInt::get(T_int8, (*(uint8_t*)ptr) ? 1 : 0);
if (bt == (jl_value_t*)jl_ssavalue_type)
return NULL;
if (jl_is_vecelement_type(bt))
bt = jl_tparam0(bt);
if (jl_is_cpointer_type(bt))
return ConstantExpr::getIntToPtr(ConstantInt::get(T_size, *(uintptr_t*)ptr), julia_type_to_llvm(bt));
if (jl_is_bitstype(bt)) {
int nb = jl_datatype_size(bt);
// TODO: non-power-of-2 size datatypes may not be interpreted correctly on big-endian systems
switch (nb) {
case 1: {
uint8_t data8 = *(uint8_t*)ptr;
return ConstantInt::get(T_int8, data8);
}
case 2: {
uint16_t data16 = *(uint16_t*)ptr;
#ifndef DISABLE_FLOAT16
if (jl_is_floattype(bt))
return ConstantFP::get(jl_LLVMContext, LLVM_FP(APFloat::IEEEhalf,APInt(16,data16)));
#endif
return ConstantInt::get(T_int16, data16);
}
case 4: {
uint32_t data32 = *(uint32_t*)ptr;
if (jl_is_floattype(bt))
return ConstantFP::get(jl_LLVMContext, LLVM_FP(APFloat::IEEEsingle,APInt(32,data32)));
return ConstantInt::get(T_int32, data32);
}
case 8: {
uint64_t data64 = *(uint64_t*)ptr;
if (jl_is_floattype(bt))
return ConstantFP::get(jl_LLVMContext, LLVM_FP(APFloat::IEEEdouble,APInt(64,data64)));
return ConstantInt::get(T_int64, data64);
}
default:
size_t nw = (nb+sizeof(uint64_t)-1)/sizeof(uint64_t);
uint64_t *data = (uint64_t*)ptr;
APInt val;
#if !defined(_P64)
// malloc may not be 16-byte aligned on P32,
// but we must ensure that llvm's uint64_t reads don't fall
// off the end of a page
// where 16-byte alignment requirement == (8-byte typetag) % (uint64_t ArrayRef access)
if (nb % 16 != 0) {
uint64_t *data_a64 = (uint64_t*)alloca(sizeof(uint64_t)*nw);
memcpy(data_a64, data, nb);
val = APInt(8*nb, ArrayRef<uint64_t>(data_a64, nw));
}
else
#endif
val = APInt(8*nb, ArrayRef<uint64_t>(data, nw));
if (nb == 16 && jl_is_floattype(bt)) {
return ConstantFP::get(jl_LLVMContext,LLVM_FP(APFloat::IEEEquad,val));
// If we have a floating point type that's not hardware supported, just treat it like an integer for LLVM purposes
}
return ConstantInt::get(IntegerType::get(jl_LLVMContext,8*nb),val);
}
}
size_t nf = jl_datatype_nfields(bt);
Constant **fields = (Constant**)alloca(nf * sizeof(Constant*));
for (size_t i = 0; i < nf; i++) {
size_t offs = jl_field_offset((jl_datatype_t*)bt, i);
jl_value_t *ft = jl_field_type(bt, i);
Constant *val = julia_const_to_llvm((char*)ptr + offs, ft);
if (val == NULL)
return NULL;
fields[i] = val;
}
Type *t = julia_struct_to_llvm(bt, NULL);
if (type_is_ghost(t))
return UndefValue::get(NoopType);
if (t->isVectorTy())
return ConstantVector::get(ArrayRef<Constant*>(fields, nf));
if (StructType *st = dyn_cast<StructType>(t)) {
return ConstantStruct::get(st, ArrayRef<Constant*>(fields, nf));
}
else {
ArrayType *at = cast<ArrayType>(t);
return ConstantArray::get(at, ArrayRef<Constant*>(fields, nf));
}
}
static Constant *julia_const_to_llvm(jl_value_t *e)
{
if (e == jl_true)
return ConstantInt::get(T_int8, 1);
if (e == jl_false)
return ConstantInt::get(T_int8, 0);
jl_value_t *bt = jl_typeof(e);
if (!jl_isbits(bt))
return NULL;
return julia_const_to_llvm(e, bt);
}
static jl_cgval_t ghostValue(jl_value_t *ty);
// emit code to unpack a raw value from a box into registers or a stack slot
static Value *emit_unbox(Type *to, const jl_cgval_t &x, jl_value_t *jt, Value *dest, bool volatile_store)
{
assert(to != T_pjlvalue);
// TODO: fully validate that x.typ == jt?
if (x.isghost) {
if (type_is_ghost(to)) {
return NULL;
}
//emit_error("emit_unbox: a type mismatch error in occurred during codegen", ctx);
return UndefValue::get(to); // type mismatch error
}
Constant *c = x.constant ? julia_const_to_llvm(x.constant) : NULL;
if (!x.ispointer() || c) { // already unboxed, but sometimes need conversion
Value *unboxed = c ? c : x.V;
Type *ty = unboxed->getType();
// bools are stored internally as int8 (for now)
if (ty == T_int1 && to == T_int8)
unboxed = builder.CreateZExt(unboxed, T_int8);
else if (ty->isPointerTy() && !to->isPointerTy())
unboxed = builder.CreatePtrToInt(unboxed, to);
else if (!ty->isPointerTy() && to->isPointerTy())
unboxed = builder.CreateIntToPtr(unboxed, to);
else if (ty->isPointerTy() && to->isPointerTy())
// pointer types are going away anyways, and this can come up in ccall argument conversion
unboxed = builder.CreatePointerCast(unboxed, to);
else if (ty != to) {
// this can happen when a branch yielding a different type ends
// up being dead code, and type inference knows that the other
// branch's type is the only one that matters.
// assert(ty == T_void);
//emit_error("emit_unbox: a type mismatch error in occurred during codegen", ctx);
unboxed = UndefValue::get(to); // type mismatch error
}
if (!dest)
return unboxed;
builder.CreateStore(unboxed, dest, volatile_store);
return NULL;
}
// bools stored as int8, so an extra Trunc is needed to get an int1
Value *p = x.constant ? literal_pointer_val(x.constant) : x.V;
Type *ptype = (to == T_int1 ? T_pint8 : to->getPointerTo());
if (p->getType() != ptype)
p = emit_bitcast(p, ptype);
Value *unboxed = NULL;
if (to == T_int1)
unboxed = builder.CreateTrunc(tbaa_decorate(x.tbaa, builder.CreateLoad(p)), T_int1);
else if (jt == (jl_value_t*)jl_bool_type)
unboxed = builder.CreateZExt(builder.CreateTrunc(tbaa_decorate(x.tbaa, builder.CreateLoad(p)), T_int1), to);
if (unboxed) {
if (!dest)
return unboxed;
builder.CreateStore(unboxed, dest);
return NULL;
}
int alignment;
if (x.isboxed) {
// julia's gc gives 16-byte aligned addresses
alignment = 16;
}
else if (jt) {
alignment = julia_alignment(p, jt, 0);
}
else {
// stack has default alignment
alignment = 0;
}
if (dest) {
// callers using the dest argument only use it for a stack slot for now
alignment = 0;
MDNode *tbaa = x.tbaa;
// the memcpy intrinsic does not allow to specify different alias tags
// for the load part (x.tbaa) and the store part (tbaa_stack).
// since the tbaa lattice has to be a tree we have unfortunately
// x.tbaa ∪ tbaa_stack = tbaa_root if x.tbaa != tbaa_stack
if (tbaa != tbaa_stack)
tbaa = NULL;
builder.CreateMemCpy(dest, p, jl_datatype_size(jt), alignment, volatile_store, tbaa);
return NULL;
}
else {
Instruction *load;
if (alignment)
load = builder.CreateAlignedLoad(p, alignment);
else
load = builder.CreateLoad(p);
return tbaa_decorate(x.tbaa, load);
}
}
// unbox, trying to determine correct bitstype automatically
// returns some sort of raw, unboxed numeric type (e.g. in registers)
static Value *auto_unbox(const jl_cgval_t &v, jl_codectx_t *ctx)
{
jl_value_t *bt = v.typ;
if (!jl_is_bitstype(bt)) {
// This can be reached with a direct invalid call to an Intrinsic, such as:
// Intrinsics.neg_int("")
emit_error("auto_unbox: unable to determine argument type", ctx);
return UndefValue::get(T_void);
}
bool isboxed;
Type *to = julia_type_to_llvm(v.typ, &isboxed);
if (to == NULL || isboxed) {
// might be some sort of incomplete (but valid) Ptr{T} type, for example
unsigned int nb = jl_datatype_nbits(bt);
to = IntegerType::get(jl_LLVMContext, nb);
}
if (type_is_ghost(to)) {
return NULL;
}
assert(!to->isAggregateType()); // expecting some sort of jl_bitstype
return emit_unbox(to, v, bt);
}
static Value *auto_unbox(jl_value_t *x, jl_codectx_t *ctx)
{
jl_cgval_t v = emit_expr(x, ctx);
return auto_unbox(v, ctx);
}
static jl_value_t *staticeval_bitstype(jl_value_t *targ, const char *fname, jl_codectx_t *ctx)
{
// evaluate an argument at compile time to determine what type it is
jl_cgval_t bt_value = emit_expr(targ, ctx);
jl_value_t *bt = NULL;
if (jl_is_type_type(bt_value.typ))
bt = jl_tparam0(bt_value.typ);
if (!bt || !jl_is_bitstype(bt)) {
emit_error("expected bits type as first argument", ctx);
return NULL;
}
return bt;
}
static Type *staticeval_bitstype(jl_value_t *bt)
{
assert(jl_is_bitstype(bt));
bool isboxed;
Type *to = julia_type_to_llvm(bt, &isboxed);
if (to == NULL || isboxed) {
unsigned int nb = jl_datatype_nbits(bt);
to = IntegerType::get(jl_LLVMContext, nb);
}
assert(!to->isAggregateType()); // expecting a bits type
return to;
}
// figure out how many bits a bitstype has at compile time
static int get_bitstype_nbits(jl_value_t *bt)
{
assert(jl_is_bitstype(bt));
return jl_datatype_nbits(bt);
}
// put a bits type tag on some value (despite the name, this doesn't necessarily actually "box" the value however)
static jl_cgval_t generic_box(jl_value_t *targ, jl_value_t *x, jl_codectx_t *ctx)
{
// Examine the first argument //
jl_cgval_t bt_value = emit_expr(targ, ctx);
jl_cgval_t v = emit_expr(x, ctx);
jl_value_t *bt = NULL;
if (jl_is_type_type(bt_value.typ))
bt = jl_tparam0(bt_value.typ);
if (!bt || !jl_is_bitstype(bt)) {
// it's easier to throw a good error from C than llvm
Value *arg1 = boxed(bt_value, ctx);
Value *arg2 = boxed(v, ctx);
Value *func = prepare_call(runtime_func[reinterpret]);
#if JL_LLVM_VERSION >= 30700
Value *r = builder.CreateCall(func, {arg1, arg2});
#else
Value *r = builder.CreateCall2(func, arg1, arg2);
#endif
jl_value_t *et = expr_type(targ, ctx);
return mark_julia_type(r, true, jl_is_type_type(et) ? jl_tparam0(et) : (jl_value_t*)jl_any_type, ctx);
}
Type *llvmt = staticeval_bitstype(bt);
int nb = jl_datatype_size(bt);
// Examine the second argument //
bool isboxed;
Type *vxt = julia_type_to_llvm(v.typ, &isboxed);
if (!jl_is_datatype(v.typ)
|| !jl_is_bitstype(v.typ)
|| jl_datatype_size(v.typ) != nb) {
Value *typ = emit_typeof_boxed(v, ctx);
if (!jl_is_bitstype(v.typ)) {
if (isboxed) {
Value *isbits = emit_datatype_isbitstype(typ);
error_unless(isbits, "reinterpret: expected bitstype value for second argument", ctx);
}
else {
emit_error("reinterpret: expected bitstype value for second argument", ctx);
return jl_cgval_t();
}
}
if (jl_datatype_size(v.typ) != nb) {
if (isboxed) {
Value *size = emit_datatype_size(typ);
error_unless(builder.CreateICmpEQ(size, ConstantInt::get(T_int32, nb)),
"reinterpret: argument size does not match size of target type", ctx);
}
else {
emit_error("reinterpret: argument size does not match size of target type", ctx);
return jl_cgval_t();
}
}
}
assert(!v.isghost);
Value *vx = NULL;
if (!v.ispointer())
vx = v.V;
else if (v.constant)
vx = julia_const_to_llvm(v.constant);
if (v.ispointer() && vx == NULL) {
// try to load as original Type, to preserve llvm optimizations
// but if the v.typ is not well known, use llvmt
if (isboxed)
vxt = llvmt;
vx = tbaa_decorate(v.tbaa, builder.CreateLoad(data_pointer(v, ctx,
vxt == T_int1 ? T_pint8 : vxt->getPointerTo())));
}
vxt = vx->getType();
if (vxt != llvmt) {
if (llvmt == T_int1)
vx = builder.CreateTrunc(vx, llvmt);
else if (vxt == T_int1 && llvmt == T_int8)
vx = builder.CreateZExt(vx, llvmt);
else if (vxt->isPointerTy() && !llvmt->isPointerTy())
vx = builder.CreatePtrToInt(vx, llvmt);
else if (!vxt->isPointerTy() && llvmt->isPointerTy())
vx = builder.CreateIntToPtr(vx, llvmt);
else
vx = emit_bitcast(vx, llvmt);
}
if (jl_is_leaf_type(bt))
return mark_julia_type(vx, false, bt, ctx);
else
return mark_julia_type(
init_bits_value(emit_allocobj(ctx, nb, boxed(bt_value, ctx)),
vx, tbaa_immut),
true, bt, ctx);
}
// put a bits type tag on some value
static jl_cgval_t generic_unbox(jl_value_t *targ, jl_value_t *x, jl_codectx_t *ctx)
{
// Examine the first argument //
jl_cgval_t bt_value = emit_expr(targ, ctx);
jl_value_t *bt = NULL;
if (jl_is_type_type(bt_value.typ))
bt = jl_tparam0(bt_value.typ);
// Examine the second argument //
jl_cgval_t v = emit_expr(x, ctx);
if (bt == NULL || !jl_is_leaf_type(bt)) {
// dynamically-determined type; evaluate.
int nb, alignment;
Type *llvmt;
if (bt && jl_is_bitstype(bt)) {
// always fixed size
nb = jl_datatype_size(bt);
llvmt = staticeval_bitstype(bt);
alignment = ((jl_datatype_t*)bt)->layout->alignment;
}
else {
bt = v.typ;
if (!jl_is_leaf_type(bt) && !jl_is_bitstype(bt)) {
// TODO: currently doesn't handle the case where the type of neither argument is understood at compile time
// since codegen has no idea what size it might have
jl_error("codegen: failed during evaluation of a call to unbox");
return jl_cgval_t();
}
nb = jl_datatype_size(bt);
llvmt = staticeval_bitstype(bt);
alignment = ((jl_datatype_t*)bt)->layout->alignment;
}
Value *runtime_bt = boxed(bt_value, ctx);
// XXX: emit type validity check on runtime_bt (bitstype of size nb)
Value *newobj = emit_allocobj(ctx, nb, runtime_bt);
if (!v.ispointer()) {
tbaa_decorate(tbaa_value, builder.CreateAlignedStore(emit_unbox(llvmt, v, v.typ), builder.CreatePointerCast(newobj, llvmt->getPointerTo()), alignment));
}
else {
prepare_call(builder.CreateMemCpy(newobj, data_pointer(v, ctx, T_pint8), nb, alignment)->getCalledValue());
mark_gc_use(v);
}
return mark_julia_type(newobj, true, bt ? bt : (jl_value_t*)jl_any_type, ctx);
}
if (!jl_is_bitstype(bt)) {
// TODO: to accept arbitrary types, replace this function with a call to llvm_type_rewrite
emit_error("unbox: expected bits type as first argument", ctx);
return jl_cgval_t();
}
Type *llvmt = staticeval_bitstype(bt);
if (v.typ == bt)
return v;
Value *vx;
if (v.ispointer()) {
vx = tbaa_decorate(v.tbaa, builder.CreateLoad(data_pointer(v, ctx, llvmt->getPointerTo())));
}
else {
vx = v.V;
if (!jl_is_bitstype(v.typ)) {
emit_error("unbox: expected bits type value for second argument", ctx);
return jl_cgval_t();
}
}
Type *vxt = vx->getType();
if (llvmt == T_int1) {
vx = builder.CreateTrunc(vx, llvmt);
}
else if (vxt == T_int1 && llvmt == T_int8) {
vx = builder.CreateZExt(vx, llvmt);
}
else if (vxt != llvmt) {
// getPrimitiveSizeInBits() == 0 for pointers
// PtrToInt and IntToPtr ignore size differences
if (vxt->getPrimitiveSizeInBits() != llvmt->getPrimitiveSizeInBits() &&
!(vxt->isPointerTy() && llvmt->getPrimitiveSizeInBits() == sizeof(void*)*8) &&
!(llvmt->isPointerTy() && vxt->getPrimitiveSizeInBits() == sizeof(void*)*8)) {
emit_error("unbox: argument is of incorrect size", ctx);
return jl_cgval_t();
}
if (vxt->isPointerTy() && !llvmt->isPointerTy())
vx = builder.CreatePtrToInt(vx, llvmt);
else if (!vxt->isPointerTy() && llvmt->isPointerTy())
vx = builder.CreateIntToPtr(vx, llvmt);
else
vx = emit_bitcast(vx, llvmt);
}
return mark_julia_type(vx, false, bt, ctx);
}
// NOTE: signd (signed) only relevant if check == true
static jl_cgval_t generic_trunc(jl_value_t *targ, jl_value_t *x, jl_codectx_t *ctx, bool check, bool signd)
{
jl_value_t *jlto = staticeval_bitstype(targ, "trunc_int", ctx);
if (!jlto) return jl_cgval_t(); // jlto threw an error
Type *to = staticeval_bitstype(jlto);
Value *ix = JL_INT(auto_unbox(x, ctx));
if (ix->getType() == T_void) return jl_cgval_t(); // auto_unbox threw an error
Value *ans = builder.CreateTrunc(ix, to);
if (check) {
Value *back = signd ? builder.CreateSExt(ans, ix->getType()) :
builder.CreateZExt(ans, ix->getType());
raise_exception_unless(builder.CreateICmpEQ(back, ix),
literal_pointer_val(jl_inexact_exception), ctx);
}
return mark_julia_type(ans, false, jlto, ctx);
}
static jl_cgval_t generic_sext(jl_value_t *targ, jl_value_t *x, jl_codectx_t *ctx)
{
jl_value_t *jlto = staticeval_bitstype(targ, "sext_int", ctx);
if (!jlto) return jl_cgval_t(); // jlto threw an error
Type *to = staticeval_bitstype(jlto);
Value *ix = JL_INT(auto_unbox(x, ctx));
if (ix->getType() == T_void) return jl_cgval_t(); // auto_unbox threw an error
Value *ans = builder.CreateSExt(ix, to);
return mark_julia_type(ans, false, jlto, ctx);
}
static jl_cgval_t generic_zext(jl_value_t *targ, jl_value_t *x, jl_codectx_t *ctx)
{
jl_value_t *jlto = staticeval_bitstype(targ, "zext_int", ctx);
if (!jlto) return jl_cgval_t(); // jlto threw an error
Type *to = staticeval_bitstype(jlto);
Value *ix = JL_INT(auto_unbox(x, ctx));
if (ix->getType() == T_void) return jl_cgval_t(); // auto_unbox threw an error
Value *ans = builder.CreateZExt(ix, to);
return mark_julia_type(ans, false, jlto, ctx);
}
static jl_cgval_t emit_runtime_pointerref(jl_value_t *e, jl_value_t *i, jl_value_t *align, jl_codectx_t *ctx)
{
jl_cgval_t parg = emit_expr(e, ctx);
Value *iarg = boxed(emit_expr(i, ctx), ctx);
Value *alignarg = boxed(emit_expr(align, ctx), ctx);
#if JL_LLVM_VERSION >= 30700
Value *ret = builder.CreateCall(prepare_call(jlpref_func), { boxed(parg, ctx), iarg, alignarg });
#else
Value *ret = builder.CreateCall3(prepare_call(jlpref_func), boxed(parg, ctx), iarg, alignarg);
#endif
jl_value_t *ety;
if (jl_is_cpointer_type(parg.typ)) {
ety = jl_tparam0(parg.typ);
}
else {
ety = (jl_value_t*)jl_any_type;
}
return mark_julia_type(ret, true, ety, ctx);
}
static jl_cgval_t emit_pointerref(jl_value_t *e, jl_value_t *i, jl_value_t *align, jl_codectx_t *ctx)
{
jl_value_t *aty = expr_type(e, ctx);
if (!jl_is_cpointer_type(aty))
return emit_runtime_pointerref(e, i, align, ctx);
//jl_error("pointerref: expected pointer type as first argument");
jl_value_t *ety = jl_tparam0(aty);
if (jl_is_typevar(ety))
return emit_runtime_pointerref(e, i, align, ctx);
//jl_error("pointerref: invalid pointer");
if (expr_type(i, ctx) != (jl_value_t*)jl_long_type)
return emit_runtime_pointerref(e, i, align, ctx);
//jl_error("pointerref: invalid index type");
jl_cgval_t align_val = emit_expr(align, ctx);
if (align_val.constant == NULL || !jl_is_long(align_val.constant))
return emit_runtime_pointerref(e, i, align, ctx);
//jl_error("pointerref: invalid or non-statically evaluatable alignment")
Value *thePtr = auto_unbox(e,ctx);
Value *idx = emit_unbox(T_size, emit_expr(i, ctx), (jl_value_t*)jl_long_type);
Value *im1 = builder.CreateSub(idx, ConstantInt::get(T_size, 1));
if (!jl_isbits(ety)) {
if (ety == (jl_value_t*)jl_any_type)
return mark_julia_type(
builder.CreateAlignedLoad(builder.CreateGEP(
emit_bitcast(thePtr, T_ppjlvalue),
im1), jl_unbox_long(align_val.constant)),
true,
ety, ctx);
if (!jl_is_structtype(ety) || jl_is_array_type(ety) || !jl_is_leaf_type(ety)) {
emit_error("pointerref: invalid pointer type", ctx);
return jl_cgval_t();
}
assert(jl_is_datatype(ety));
uint64_t size = jl_datatype_size(ety);
Value *strct = emit_allocobj(ctx, size,
literal_pointer_val((jl_value_t*)ety));
im1 = builder.CreateMul(im1, ConstantInt::get(T_size,
LLT_ALIGN(size, ((jl_datatype_t*)ety)->layout->alignment)));
thePtr = builder.CreateGEP(emit_bitcast(thePtr, T_pint8), im1);
prepare_call(builder.CreateMemCpy(emit_bitcast(strct, T_pint8),
thePtr, size, 1)->getCalledValue());
return mark_julia_type(strct, true, ety, ctx);
}
return typed_load(thePtr, im1, ety, ctx, tbaa_data, jl_unbox_long(align_val.constant));
}
static jl_cgval_t emit_runtime_pointerset(jl_value_t *e, jl_value_t *x, jl_value_t *i, jl_value_t *align, jl_codectx_t *ctx)
{
jl_cgval_t parg = emit_expr(e, ctx);
Value *xarg = boxed(emit_expr(x, ctx), ctx);
Value *iarg = boxed(emit_expr(i, ctx), ctx);
Value *alignarg = boxed(emit_expr(align, ctx), ctx);
#if JL_LLVM_VERSION >= 30700
builder.CreateCall(prepare_call(jlpset_func), { boxed(parg, ctx), xarg, iarg, alignarg });
#else
builder.CreateCall4(prepare_call(jlpset_func), boxed(parg, ctx), xarg, iarg, alignarg);
#endif
return parg;
}
// e[i] = x
static jl_cgval_t emit_pointerset(jl_value_t *e, jl_value_t *x, jl_value_t *i, jl_value_t *align, jl_codectx_t *ctx)
{
jl_value_t *aty = expr_type(e, ctx);
if (!jl_is_cpointer_type(aty))
return emit_runtime_pointerset(e, x, i, align, ctx);
//jl_error("pointerset: expected pointer type as first argument");
jl_value_t *ety = jl_tparam0(aty);
if (jl_is_typevar(ety))
return emit_runtime_pointerset(e, x, i, align, ctx);
//jl_error("pointerset: invalid pointer");
jl_value_t *xty = expr_type(x, ctx);
jl_cgval_t val;
bool emitted = false;
if (!jl_subtype(xty, ety, 0)) {
emitted = true;
val = emit_expr(x, ctx);
emit_typecheck(val, ety, "pointerset: type mismatch in assign", ctx);
}
if (expr_type(i, ctx) != (jl_value_t*)jl_long_type)
return emit_runtime_pointerset(e, x, i, align, ctx);
//jl_error("pointerset: invalid index type");
jl_cgval_t align_val = emit_expr(align, ctx);
if (align_val.constant == NULL || !jl_is_long(align_val.constant))
return emit_runtime_pointerset(e, x, i, align, ctx);
//jl_error("pointerset: invalid or non-statically evaluatable alignment")
Value *idx = emit_unbox(T_size, emit_expr(i, ctx),(jl_value_t*)jl_long_type);
Value *im1 = builder.CreateSub(idx, ConstantInt::get(T_size, 1));
Value *thePtr = auto_unbox(e,ctx);
if (!jl_isbits(ety) && ety != (jl_value_t*)jl_any_type) {
if (!jl_is_structtype(ety) || jl_is_array_type(ety) || !jl_is_leaf_type(ety)) {
emit_error("pointerset: invalid pointer type", ctx);
return jl_cgval_t();
}
if (!emitted)
val = emit_expr(x, ctx);
assert(val.isboxed);
assert(jl_is_datatype(ety));
uint64_t size = jl_datatype_size(ety);
im1 = builder.CreateMul(im1, ConstantInt::get(T_size,
LLT_ALIGN(size, ((jl_datatype_t*)ety)->layout->alignment)));
prepare_call(builder.CreateMemCpy(builder.CreateGEP(emit_bitcast(thePtr, T_pint8), im1),
data_pointer(val, ctx, T_pint8), size, jl_unbox_long(align_val.constant))->getCalledValue());
}
else {
if (!emitted) {
val = emit_expr(x, ctx);
}
assert(jl_is_datatype(ety));
typed_store(thePtr, im1, val, ety, ctx, tbaa_data, NULL, jl_unbox_long(align_val.constant));
}
return mark_julia_type(thePtr, false, aty, ctx);
}
static Value *emit_checked_srem_int(Value *x, Value *den, jl_codectx_t *ctx)
{
Type *t = den->getType();
raise_exception_unless(builder.CreateICmpNE(den, ConstantInt::get(t,0)),
literal_pointer_val(jl_diverror_exception), ctx);
BasicBlock *m1BB = BasicBlock::Create(jl_LLVMContext,"minus1",ctx->f);
BasicBlock *okBB = BasicBlock::Create(jl_LLVMContext,"oksrem",ctx->f);
BasicBlock *cont = BasicBlock::Create(jl_LLVMContext,"after_srem",ctx->f);
PHINode *ret = PHINode::Create(t, 2);
builder.CreateCondBr(builder.CreateICmpEQ(den,ConstantInt::get(t,-1,true)),
m1BB, okBB);
builder.SetInsertPoint(m1BB);
builder.CreateBr(cont);
builder.SetInsertPoint(okBB);
Value *sremval = builder.CreateSRem(x, den);
builder.CreateBr(cont);
builder.SetInsertPoint(cont);
ret->addIncoming(// rem(typemin, -1) is undefined
ConstantInt::get(t,0), m1BB);
ret->addIncoming(sremval, okBB);
builder.Insert(ret);
return ret;
}
// Temporarily switch the builder to fast-math mode if requested
struct math_builder {
FastMathFlags old_fmf;
math_builder(jl_codectx_t *ctx, bool always_fast = false):
old_fmf(builder.getFastMathFlags())
{
if (jl_options.fast_math != JL_OPTIONS_FAST_MATH_OFF &&
(always_fast ||
jl_options.fast_math == JL_OPTIONS_FAST_MATH_ON)) {
FastMathFlags fmf;
fmf.setUnsafeAlgebra();
#if JL_LLVM_VERSION >= 30800
builder.setFastMathFlags(fmf);
#else
builder.SetFastMathFlags(fmf);
#endif
}
}
IRBuilder<>& operator()() const { return builder; }
~math_builder() {
#if JL_LLVM_VERSION >= 30800
builder.setFastMathFlags(old_fmf);
#else
builder.SetFastMathFlags(old_fmf);
#endif
}
};
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_value_t* xtyp);
static jl_cgval_t emit_intrinsic(intrinsic f, jl_value_t **args, size_t nargs,
jl_codectx_t *ctx)
{
assert(f < num_intrinsics);
if (f == fptoui && nargs == 1)
f = fptoui_auto;
if (f == fptosi && nargs == 1)
f = fptosi_auto;
if (f == cglobal && nargs == 1)
f = cglobal_auto;
unsigned expected_nargs = intrinsic_nargs[f];
if (expected_nargs && expected_nargs != nargs) {
jl_errorf("intrinsic #%d %s: wrong number of arguments", f, JL_I::jl_intrinsic_name((int)f));
}
switch (f) {
case ccall: return emit_ccall(args, nargs, ctx);
case cglobal_auto:
case cglobal: return emit_cglobal(args, nargs, ctx);
case llvmcall: return emit_llvmcall(args, nargs, ctx);
case arraylen:
return mark_julia_type(emit_arraylen(emit_expr(args[1], ctx), args[1], ctx), false,
jl_long_type, ctx);
#if 0 // this section enables runtime-intrinsics (e.g. for testing), and disables their llvm counterparts
default:
Value *r;
Value *func = prepare_call(runtime_func[f]);
if (nargs == 1) {
Value *x = boxed(emit_expr(args[1], ctx), ctx);
#if JL_LLVM_VERSION >= 30700
r = builder.CreateCall(func, {x});
#else
r = builder.CreateCall(func, x);
#endif
}
else if (nargs == 2) {
Value *x = boxed(emit_expr(args[1], ctx), ctx);
Value *y = boxed(emit_expr(args[2], ctx), ctx);
#if JL_LLVM_VERSION >= 30700
r = builder.CreateCall(func, {x, y});
#else
r = builder.CreateCall2(func, x, y);
#endif
}
else if (nargs == 3) {
Value *x = boxed(emit_expr(args[1], ctx), ctx);
Value *y = boxed(emit_expr(args[2], ctx), ctx);
Value *z = boxed(emit_expr(args[3], ctx), ctx);
#if JL_LLVM_VERSION >= 30700
r = builder.CreateCall(func, {x, y, z});
#else
r = builder.CreateCall3(func, x, y, z);
#endif
}
else {
assert(0);
}
return mark_julia_type(r, true, (jl_value_t*)jl_any_type, ctx);
#else
case pointerref:
return emit_pointerref(args[1], args[2], args[3], ctx);
case pointerset:
return emit_pointerset(args[1], args[2], args[3], args[4], ctx);
case box:
return generic_box(args[1], args[2], ctx);
case unbox:
return generic_unbox(args[1], args[2], ctx); // TODO: replace with generic_box
case trunc_int:
return generic_trunc(args[1], args[2], ctx, false, false);
case checked_trunc_sint:
return generic_trunc(args[1], args[2], ctx, true, true);
case checked_trunc_uint:
return generic_trunc(args[1], args[2], ctx, true, false);
case sext_int:
return generic_sext(args[1], args[2], ctx);
case zext_int:
return generic_zext(args[1], args[2], ctx);
case uitofp: {
jl_value_t *bt = staticeval_bitstype(args[1], "uitofp", ctx);
if (!bt) return jl_cgval_t();
int nb = get_bitstype_nbits(bt);
Value *xi = JL_INT(auto_unbox(args[2],ctx));
if (xi->getType() == T_void) return jl_cgval_t(); // auto_unbox threw an error
return mark_julia_type(builder.CreateUIToFP(xi, FTnbits(nb)), false, bt, ctx);
}
case sitofp: {
jl_value_t *bt = staticeval_bitstype(args[1], "sitofp", ctx);
if (!bt) return jl_cgval_t();
int nb = get_bitstype_nbits(bt);
Value *xi = JL_INT(auto_unbox(args[2],ctx));
if (xi->getType() == T_void) return jl_cgval_t(); // auto_unbox threw an error
return mark_julia_type(builder.CreateSIToFP(xi, FTnbits(nb)), false, bt, ctx);
}
case fptoui_auto: {
Value *x = FP(auto_unbox(args[1], ctx));
if (x->getType() == T_void) return jl_cgval_t(); // auto_unbox threw an error
return mark_julia_type(
builder.CreateFPToUI(FP(x), JL_INTT(x->getType())),
false,
JL_JLUINTT(x->getType()), ctx);
}
case fptoui: {
jl_value_t *bt = staticeval_bitstype(args[1], "sitofp", ctx);
if (!bt) return jl_cgval_t();
int nb = get_bitstype_nbits(bt);
Value *xf = FP(auto_unbox(args[2],ctx));
if (xf->getType() == T_void) return jl_cgval_t(); // auto_unbox threw an error
return mark_julia_type(builder.CreateFPToUI(xf, Type::getIntNTy(jl_LLVMContext, nb)), false, bt, ctx);
}
case fptosi_auto: {
Value *x = FP(auto_unbox(args[1], ctx));
return mark_julia_type(
builder.CreateFPToSI(FP(x), JL_INTT(x->getType())),
false,
JL_JLSINTT(x->getType()), ctx);
}
case fptosi: {
jl_value_t *bt = staticeval_bitstype(args[1], "sitofp", ctx);
if (!bt) return jl_cgval_t();
int nb = get_bitstype_nbits(bt);
Value *xf = FP(auto_unbox(args[2],ctx));
if (xf->getType() == T_void) return jl_cgval_t(); // auto_unbox threw an error
return mark_julia_type(builder.CreateFPToSI(xf, Type::getIntNTy(jl_LLVMContext, nb)), false, bt, ctx);
}
case fptrunc: {
jl_value_t *bt = staticeval_bitstype(args[1], "sitofp", ctx);
if (!bt) return jl_cgval_t();
int nb = get_bitstype_nbits(bt);
Value *xf = FP(auto_unbox(args[2],ctx));
if (xf->getType() == T_void) return jl_cgval_t(); // auto_unbox threw an error
return mark_julia_type(builder.CreateFPTrunc(xf, FTnbits(nb)), false, bt, ctx);
}
case fpext: {
jl_value_t *bt = staticeval_bitstype(args[1], "sitofp", ctx);
if (!bt) return jl_cgval_t();
int nb = get_bitstype_nbits(bt);
Value *x = auto_unbox(args[2],ctx);
if (x->getType() == T_void) return jl_cgval_t(); // auto_unbox threw an error
#ifdef JL_NEED_FLOATTEMP_VAR
// Target platform might carry extra precision.
// Force rounding to single precision first. The reason is that it's
// fine to keep working in extended precision as long as it's
// understood that everything is implicitly rounded to 23 bits,
// but if we start looking at more bits we need to actually do the
// rounding first instead of carrying around incorrect low bits.
Value *jlfloattemp_var = emit_static_alloca(FT(x->getType()));
builder.CreateStore(FP(x), jlfloattemp_var);
x = builder.CreateLoad(jlfloattemp_var, true);
#endif
return mark_julia_type(builder.CreateFPExt(x, FTnbits(nb)), false, bt, ctx);
}