forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.cpp
8133 lines (7680 loc) · 339 KB
/
codegen.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: https://julialang.org/license
#include "llvm-version.h"
#include "platform.h"
#if defined(_OS_WINDOWS_)
// use ELF because RuntimeDyld COFF i686 support didn't exist
// use ELF because RuntimeDyld COFF X86_64 doesn't seem to work (fails to generate function pointers)?
#define FORCE_ELF
#endif
#if defined(_CPU_X86_)
#define JL_NEED_FLOATTEMP_VAR 1
#endif
#if defined(_OS_WINDOWS_) || defined(_OS_FREEBSD_)
#define JL_DISABLE_FPO
#endif
#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS
#define __STDC_CONSTANT_MACROS
#endif
#include <setjmp.h>
#include <string>
#include <fstream>
#include <map>
#include <array>
#include <vector>
#include <set>
#include <functional>
// target machine computation
#include <llvm/CodeGen/TargetSubtargetInfo.h>
#include <llvm/Support/TargetRegistry.h>
#include <llvm/Target/TargetOptions.h>
#include <llvm/Support/Host.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Object/SymbolSize.h>
#include <llvm/InitializePasses.h>
// IR building
#include <llvm/IR/IntrinsicInst.h>
#include <llvm/Object/ObjectFile.h>
#include <llvm/IR/DIBuilder.h>
#include <llvm/AsmParser/Parser.h>
#include <llvm/DebugInfo/DIContext.h>
#include "llvm/IR/DebugInfoMetadata.h"
#include <llvm/IR/DerivedTypes.h>
#include <llvm/IR/Intrinsics.h>
#include <llvm/IR/Attributes.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/MDBuilder.h>
// support
#include <llvm/ADT/SmallBitVector.h>
#include <llvm/ADT/Optional.h>
#include <llvm/ADT/Statistic.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Support/FormattedStream.h>
#include <llvm/Support/SourceMgr.h> // for llvmcall
#include <llvm/Transforms/Utils/Cloning.h> // for llvmcall inlining
#include <llvm/Transforms/Utils/BasicBlockUtils.h>
#include <llvm/IR/Verifier.h> // for llvmcall validation
#include <llvm/IR/PassTimingInfo.h>
#include <llvm/Bitcode/BitcodeWriter.h>
// C API
#include <llvm-c/Types.h>
// for configuration options
#include <llvm/Support/PrettyStackTrace.h>
#include <llvm/Support/CommandLine.h>
#if JL_LLVM_VERSION >= 120000
#include <llvm/Support/Process.h>
#endif
#include <llvm/IR/InlineAsm.h>
#if defined(_CPU_ARM_) || defined(_CPU_AARCH64_)
# include <sys/utsname.h>
#endif
#if defined(USE_POLLY)
#include <polly/RegisterPasses.h>
#include <polly/ScopDetection.h>
#endif
#include <llvm/Target/TargetMachine.h>
using namespace llvm;
typedef Instruction TerminatorInst;
#if defined(_OS_WINDOWS_) && !defined(NOMINMAX)
#define NOMINMAX
#endif
#include "julia.h"
#include "julia_internal.h"
#include "jitlayers.h"
#include "codegen_shared.h"
#include "processor.h"
#include "julia_assert.h"
JL_STREAM *dump_emitted_mi_name_stream = NULL;
extern "C" JL_DLLEXPORT
void jl_dump_emitted_mi_name(void *s)
{
dump_emitted_mi_name_stream = (JL_STREAM*)s;
}
extern "C" {
#include "builtin_proto.h"
#ifdef HAVE_SSP
extern uintptr_t __stack_chk_guard;
extern void __stack_chk_fail();
#else
JL_DLLEXPORT uintptr_t __stack_chk_guard = (uintptr_t)0xBAD57ACCBAD67ACC; // 0xBADSTACKBADSTACK
JL_DLLEXPORT void __stack_chk_fail()
{
/* put your panic function or similar in here */
fprintf(stderr, "fatal error: stack corruption detected\n");
gc_debug_critical_error();
abort(); // end with abort, since the compiler destroyed the stack upon entry to this function, there's no going back now
}
#endif
#ifdef _OS_WINDOWS_
#if defined(_CPU_X86_64_)
#if defined(_COMPILER_GCC_)
extern void ___chkstk_ms(void);
#else
extern void __chkstk(void);
#endif
#else
#if defined(_COMPILER_GCC_)
#undef _alloca
extern void _alloca(void);
#else
extern void _chkstk(void);
#endif
#endif
//void *force_chkstk(void) {
// return alloca(40960);
//}
#endif
}
#if defined(_COMPILER_MICROSOFT_) && !defined(__alignof__)
#define __alignof__ __alignof
#endif
// llvm state
extern JITEventListener *CreateJuliaJITEventListener();
// for image reloading
bool imaging_mode = false;
// shared llvm state
JL_DLLEXPORT LLVMContext &jl_LLVMContext = *(new LLVMContext());
TargetMachine *jl_TargetMachine;
static DataLayout &jl_data_layout = *(new DataLayout(""));
#define jl_Module ctx.f->getParent()
#define jl_builderModule(builder) (builder).GetInsertBlock()->getParent()->getParent()
#define prepare_call(Callee) prepare_call_in(jl_Module, (Callee))
// types
static Type *T_jlvalue;
static Type *T_pjlvalue;
static Type *T_prjlvalue;
static Type *T_ppjlvalue;
static Type *T_pprjlvalue;
static Type *jl_array_llvmt;
static Type *jl_parray_llvmt;
static FunctionType *jl_func_sig;
static FunctionType *jl_func_sig_sparams;
static Type *T_pvoidfunc;
static IntegerType *T_int1;
static IntegerType *T_int8;
static IntegerType *T_int16;
static IntegerType *T_int32;
static IntegerType *T_int64;
static IntegerType *T_uint8;
static IntegerType *T_uint16;
static IntegerType *T_uint32;
static IntegerType *T_uint64;
static IntegerType *T_char;
static IntegerType *T_size;
static IntegerType *T_sigatomic;
static Type *T_float16;
static Type *T_float32;
static Type *T_float64;
static Type *T_float128;
static Type *T_pint8;
static Type *T_pint16;
static Type *T_pint32;
static Type *T_pint64;
static Type *T_psize;
static Type *T_pfloat32;
static Type *T_pfloat64;
static Type *T_ppint8;
static Type *T_pppint8;
static Type *T_void;
// type-based alias analysis nodes. Indentation of comments indicates hierarchy.
static MDNode *tbaa_root; // Everything
static MDNode *tbaa_gcframe; // GC frame
// LLVM should have enough info for alias analysis of non-gcframe stack slot
// this is mainly a place holder for `jl_cgval_t::tbaa`
static MDNode *tbaa_stack; // stack slot
static MDNode *tbaa_unionselbyte; // a selector byte in isbits Union struct fields
static MDNode *tbaa_data; // Any user data that `pointerset/ref` are allowed to alias
static MDNode *tbaa_binding; // jl_binding_t::value
static MDNode *tbaa_value; // jl_value_t, that is not jl_array_t
static MDNode *tbaa_mutab; // mutable type
static MDNode *tbaa_datatype; // datatype
static MDNode *tbaa_immut; // immutable type
static MDNode *tbaa_ptrarraybuf; // Data in an array of boxed values
static MDNode *tbaa_arraybuf; // Data in an array of POD
static MDNode *tbaa_array; // jl_array_t
static MDNode *tbaa_arrayptr; // The pointer inside a jl_array_t
static MDNode *tbaa_arraysize; // A size in a jl_array_t
static MDNode *tbaa_arraylen; // The len in a jl_array_t
static MDNode *tbaa_arrayflags; // The flags in a jl_array_t
static MDNode *tbaa_arrayoffset; // The offset in a jl_array_t
static MDNode *tbaa_arrayselbyte; // a selector byte in a isbits Union jl_array_t
static MDNode *tbaa_const; // Memory that is immutable by the time LLVM can see it
static Attribute Thunk;
// Basic DITypes
static DICompositeType *jl_value_dillvmt;
static DIDerivedType *jl_pvalue_dillvmt;
static DIDerivedType *jl_ppvalue_dillvmt;
static DISubroutineType *jl_di_func_sig;
static DISubroutineType *jl_di_func_null_sig;
// constants
static Constant *V_null;
static Constant *V_rnull;
static Constant *V_size0;
static bool type_is_ghost(Type *ty)
{
return (ty == T_void || ty->isEmptyTy());
}
// should agree with `Core.Compiler.hasuniquerep`
static bool type_has_unique_rep(jl_value_t *t)
{
if (t == (jl_value_t*)jl_typeofbottom_type)
return false;
if (t == jl_bottom_type)
return true;
if (jl_is_typevar(t))
return false;
if (!jl_is_kind(jl_typeof(t)))
return true;
if (jl_is_concrete_type(t))
return true;
if (jl_is_datatype(t)) {
jl_datatype_t *dt = (jl_datatype_t*)t;
if (dt->name != jl_tuple_typename) {
for (size_t i = 0; i < jl_nparams(dt); i++)
if (!type_has_unique_rep(jl_tparam(dt, i)))
return false;
return true;
}
}
return false;
}
static bool is_uniquerep_Type(jl_value_t *t)
{
return jl_is_type_type(t) && type_has_unique_rep(jl_tparam0(t));
}
class jl_codectx_t;
struct JuliaVariable {
public:
StringLiteral name;
bool isconst;
Type *(*_type)(LLVMContext &C);
JuliaVariable(const JuliaVariable&) = delete;
JuliaVariable(const JuliaVariable&&) = delete;
GlobalVariable *realize(Module *m) {
if (GlobalValue *V = m->getNamedValue(name))
return cast<GlobalVariable>(V);
return new GlobalVariable(*m, _type(m->getContext()),
isconst, GlobalVariable::ExternalLinkage,
NULL, name);
}
GlobalVariable *realize(jl_codectx_t &ctx);
};
static inline void add_named_global(JuliaVariable *name, void *addr)
{
add_named_global(name->name, addr);
}
struct JuliaFunction {
public:
StringLiteral name;
FunctionType *(*_type)(LLVMContext &C);
AttributeList (*_attrs)(LLVMContext &C);
JuliaFunction(const JuliaFunction&) = delete;
JuliaFunction(const JuliaFunction&&) = delete;
Function *realize(Module *m) {
if (GlobalValue *V = m->getNamedValue(name))
return cast<Function>(V);
Function *F = Function::Create(_type(m->getContext()),
Function::ExternalLinkage,
name, m);
if (_attrs)
F->setAttributes(_attrs(m->getContext()));
return F;
}
Function *realize(jl_codectx_t &ctx);
};
template<typename T>
static inline void add_named_global(JuliaFunction *name, T *addr)
{
// cast through integer to avoid c++ pedantic warning about casting between
// data and code pointers
add_named_global(name->name, (void*)(uintptr_t)addr);
}
template<typename T>
static inline void add_named_global(StringRef name, T *addr)
{
// cast through integer to avoid c++ pedantic warning about casting between
// data and code pointers
add_named_global(name, (void*)(uintptr_t)addr);
}
AttributeSet Attributes(LLVMContext &C, std::initializer_list<Attribute::AttrKind> attrkinds)
{
SmallVector<Attribute, 8> attrs(attrkinds.size());
for (size_t i = 0; i < attrkinds.size(); i++)
attrs[i] = Attribute::get(C, attrkinds.begin()[i]);
return AttributeSet::get(C, makeArrayRef(attrs));
}
static Type *get_pjlvalue(LLVMContext &C) { return T_pjlvalue; }
static FunctionType *get_func_sig(LLVMContext &C) { return jl_func_sig; }
static AttributeList get_func_attrs(LLVMContext &C)
{
return AttributeList::get(C,
AttributeSet::get(C, makeArrayRef({Thunk})),
Attributes(C, {Attribute::NonNull}),
None);
}
static AttributeList get_attrs_noreturn(LLVMContext &C)
{
return AttributeList::get(C,
Attributes(C, {Attribute::NoReturn}),
AttributeSet(),
None);
}
static AttributeList get_attrs_sext(LLVMContext &C)
{
return AttributeList::get(C,
AttributeSet(),
Attributes(C, {Attribute::NonNull}),
{Attributes(C, {Attribute::SExt})});
}
static AttributeList get_attrs_zext(LLVMContext &C)
{
return AttributeList::get(C,
AttributeSet(),
Attributes(C, {Attribute::NonNull}),
{Attributes(C, {Attribute::ZExt})});
}
// global vars
static const auto jlRTLD_DEFAULT_var = new JuliaVariable{
"jl_RTLD_DEFAULT_handle",
true,
[](LLVMContext &C) { return T_pint8; },
};
#ifdef _OS_WINDOWS_
static const auto jlexe_var = new JuliaVariable{
"jl_exe_handle",
true,
[](LLVMContext &C) { return T_pint8; },
};
static const auto jldll_var = new JuliaVariable{
"jl_libjulia_internal_handle",
true,
[](LLVMContext &C) { return T_pint8; },
};
#endif //_OS_WINDOWS_
static const auto jlstack_chk_guard_var = new JuliaVariable{
"__stack_chk_guard",
true,
get_pjlvalue,
};
static const auto jlgetworld_global = new JuliaVariable{
"jl_world_counter",
false,
[](LLVMContext &C) { return (Type*)T_size; },
};
static const auto jlboxed_int8_cache = new JuliaVariable{
"jl_boxed_int8_cache",
true,
[](LLVMContext &C) { return (Type*)ArrayType::get(T_pjlvalue, 256); },
};
static const auto jlboxed_uint8_cache = new JuliaVariable{
"jl_boxed_uint8_cache",
true,
[](LLVMContext &C) { return (Type*)ArrayType::get(T_pjlvalue, 256); },
};
static const auto jltls_states_func = new JuliaFunction{
"julia.ptls_states",
[](LLVMContext &C) { return FunctionType::get(PointerType::get(T_ppjlvalue, 0), false); },
nullptr,
};
// important functions
// Symbols are not gc-tracked, but we'll treat them as callee rooted anyway,
// because they may come from a gc-rooted location
static const auto jlnew_func = new JuliaFunction{
"jl_new_structv",
get_func_sig,
get_func_attrs,
};
static const auto jlsplatnew_func = new JuliaFunction{
"jl_new_structt",
[](LLVMContext &C) { return FunctionType::get(T_prjlvalue,
{T_prjlvalue, T_prjlvalue}, false); },
get_func_attrs,
};
static const auto jlthrow_func = new JuliaFunction{
"jl_throw",
[](LLVMContext &C) { return FunctionType::get(T_void,
{PointerType::get(T_jlvalue, AddressSpace::CalleeRooted)}, false); },
get_attrs_noreturn,
};
static const auto jlerror_func = new JuliaFunction{
"jl_error",
[](LLVMContext &C) { return FunctionType::get(T_void,
{T_pint8}, false); },
get_attrs_noreturn,
};
static const auto jltypeerror_func = new JuliaFunction{
"jl_type_error",
[](LLVMContext &C) { return FunctionType::get(T_void,
{T_pint8, T_prjlvalue, PointerType::get(T_jlvalue, AddressSpace::CalleeRooted)}, false); },
get_attrs_noreturn,
};
static const auto jlundefvarerror_func = new JuliaFunction{
"jl_undefined_var_error",
[](LLVMContext &C) { return FunctionType::get(T_void,
{PointerType::get(T_jlvalue, AddressSpace::CalleeRooted)}, false); },
get_attrs_noreturn,
};
static const auto jlboundserrorv_func = new JuliaFunction{
"jl_bounds_error_ints",
[](LLVMContext &C) { return FunctionType::get(T_void,
{PointerType::get(T_jlvalue, AddressSpace::CalleeRooted), T_psize, T_size}, false); },
get_attrs_noreturn,
};
static const auto jlboundserror_func = new JuliaFunction{
"jl_bounds_error_int",
[](LLVMContext &C) { return FunctionType::get(T_void,
{PointerType::get(T_jlvalue, AddressSpace::CalleeRooted), T_size}, false); },
get_attrs_noreturn,
};
static const auto jlvboundserror_func = new JuliaFunction{
"jl_bounds_error_tuple_int",
[](LLVMContext &C) { return FunctionType::get(T_void,
{T_pprjlvalue, T_size, T_size}, false); },
get_attrs_noreturn,
};
static const auto jluboundserror_func = new JuliaFunction{
"jl_bounds_error_unboxed_int",
[](LLVMContext &C) { return FunctionType::get(T_void,
{PointerType::get(T_int8, AddressSpace::Derived), T_pjlvalue, T_size}, false); },
get_attrs_noreturn,
};
static const auto jlcheckassign_func = new JuliaFunction{
"jl_checked_assignment",
[](LLVMContext &C) { return FunctionType::get(T_void,
{T_pjlvalue, PointerType::get(T_jlvalue, AddressSpace::CalleeRooted)}, false); },
nullptr,
};
static const auto jldeclareconst_func = new JuliaFunction{
"jl_declare_constant",
[](LLVMContext &C) { return FunctionType::get(T_void,
{T_pjlvalue}, false); },
nullptr,
};
static const auto jlgetbindingorerror_func = new JuliaFunction{
"jl_get_binding_or_error",
[](LLVMContext &C) { return FunctionType::get(T_pjlvalue,
{T_pjlvalue, T_pjlvalue}, false); },
nullptr,
};
static const auto jlboundp_func = new JuliaFunction{
"jl_boundp",
[](LLVMContext &C) { return FunctionType::get(T_int32,
{T_pjlvalue, T_pjlvalue}, false); },
nullptr,
};
static const auto jltopeval_func = new JuliaFunction{
"jl_toplevel_eval",
[](LLVMContext &C) { return FunctionType::get(T_pjlvalue,
{T_pjlvalue, T_pjlvalue}, false); },
[](LLVMContext &C) { return AttributeList::get(C,
AttributeSet(),
Attributes(C, {Attribute::NonNull}),
None); },
};
static const auto jlcopyast_func = new JuliaFunction{
"jl_copy_ast",
[](LLVMContext &C) { return FunctionType::get(T_prjlvalue,
{T_prjlvalue}, false); },
[](LLVMContext &C) { return AttributeList::get(C,
AttributeSet(),
Attributes(C, {Attribute::NonNull}),
None); },
};
//static const auto jlnsvec_func = new JuliaFunction{
// "jl_svec",
// [](LLVMContext &C) { return FunctionType::get(T_prjlvalue,
// {T_size}, true); },
// [](LLVMContext &C) { return AttributeList::get(C,
// AttributeSet(),
// Attributes(C, {Attribute::NonNull}),
// None); },
//};
static const auto jlapplygeneric_func = new JuliaFunction{
"jl_apply_generic",
get_func_sig,
get_func_attrs,
};
static const auto jlinvoke_func = new JuliaFunction{
"jl_invoke",
[](LLVMContext &C) { return FunctionType::get(T_prjlvalue,
{T_prjlvalue, T_pprjlvalue, T_uint32, T_prjlvalue}, false); },
[](LLVMContext &C) { return AttributeList::get(C,
AttributeSet(),
Attributes(C, {Attribute::NonNull}),
{AttributeSet(),
Attributes(C, {Attribute::ReadOnly, Attribute::NoCapture})}); },
};
static const auto jlmethod_func = new JuliaFunction{
"jl_method_def",
[](LLVMContext &C) { return FunctionType::get(T_prjlvalue,
{T_prjlvalue, T_prjlvalue, T_pjlvalue}, false); },
nullptr,
};
static const auto jlgenericfunction_func = new JuliaFunction{
"jl_generic_function_def",
[](LLVMContext &C) { return FunctionType::get(T_prjlvalue,
{T_pjlvalue, T_pjlvalue, T_pprjlvalue, T_pjlvalue, T_pjlvalue}, false); },
nullptr,
};
static const auto jlenter_func = new JuliaFunction{
"jl_enter_handler",
[](LLVMContext &C) { return FunctionType::get(T_void,
{T_pint8}, false); },
nullptr,
};
static const auto jl_current_exception_func = new JuliaFunction{
"jl_current_exception",
[](LLVMContext &C) { return FunctionType::get(T_prjlvalue, false); },
nullptr,
};
static const auto jlleave_func = new JuliaFunction{
"jl_pop_handler",
[](LLVMContext &C) { return FunctionType::get(T_void,
{T_int32}, false); },
nullptr,
};
static const auto jl_restore_excstack_func = new JuliaFunction{
"jl_restore_excstack",
[](LLVMContext &C) { return FunctionType::get(T_void,
{T_size}, false); },
nullptr,
};
static const auto jl_excstack_state_func = new JuliaFunction{
"jl_excstack_state",
[](LLVMContext &C) { return FunctionType::get(T_size, false); },
nullptr,
};
static const auto jlegal_func = new JuliaFunction{
"jl_egal",
[](LLVMContext &C) {
Type *T = PointerType::get(T_jlvalue, AddressSpace::CalleeRooted);
return FunctionType::get(T_int32, {T, T}, false); },
[](LLVMContext &C) { return AttributeList::get(C,
Attributes(C, {Attribute::ReadOnly, Attribute::NoUnwind, Attribute::ArgMemOnly}),
AttributeSet(),
None); },
};
static const auto jl_alloc_obj_func = new JuliaFunction{
"julia.gc_alloc_obj",
[](LLVMContext &C) { return FunctionType::get(T_prjlvalue,
{T_pint8, T_size, T_prjlvalue}, false); },
[](LLVMContext &C) { return AttributeList::get(C,
AttributeSet::get(C, makeArrayRef({Attribute::getWithAllocSizeArgs(C, 1, None)})), // returns %1 bytes
Attributes(C, {Attribute::NoAlias, Attribute::NonNull}),
None); },
};
static const auto jl_newbits_func = new JuliaFunction{
"jl_new_bits",
[](LLVMContext &C) { return FunctionType::get(T_prjlvalue,
{T_prjlvalue, T_pint8}, false); },
[](LLVMContext &C) { return AttributeList::get(C,
AttributeSet(),
Attributes(C, {Attribute::NonNull}),
None); },
};
// `julia.typeof` does read memory, but it is effectively readnone before we lower
// the allocation function. This is OK as long as we lower `julia.typeof` no later than
// `julia.gc_alloc_obj`.
static const auto jl_typeof_func = new JuliaFunction{
"julia.typeof",
[](LLVMContext &C) { return FunctionType::get(T_prjlvalue,
{T_prjlvalue}, false); },
[](LLVMContext &C) { return AttributeList::get(C,
Attributes(C, {Attribute::ReadNone, Attribute::NoUnwind, Attribute::NoRecurse}),
Attributes(C, {Attribute::NonNull}),
None); },
};
static const auto jl_loopinfo_marker_func = new JuliaFunction{
"julia.loopinfo_marker",
[](LLVMContext &C) { return FunctionType::get(T_void, false); },
[](LLVMContext &C) { return AttributeList::get(C,
Attributes(C, {Attribute::ReadOnly, Attribute::NoRecurse, Attribute::InaccessibleMemOnly}),
AttributeSet(),
None); },
};
static const auto jl_write_barrier_func = new JuliaFunction{
"julia.write_barrier",
[](LLVMContext &C) { return FunctionType::get(T_void,
{T_prjlvalue}, true); },
[](LLVMContext &C) { return AttributeList::get(C,
Attributes(C, {Attribute::NoUnwind, Attribute::NoRecurse, Attribute::InaccessibleMemOnly}),
AttributeSet(),
None); },
};
static const auto jlisa_func = new JuliaFunction{
"jl_isa",
[](LLVMContext &C) { return FunctionType::get(T_int32,
{T_prjlvalue, T_prjlvalue}, false); },
nullptr,
};
static const auto jlsubtype_func = new JuliaFunction{
"jl_subtype",
[](LLVMContext &C) { return FunctionType::get(T_int32,
{T_prjlvalue, T_prjlvalue}, false); },
nullptr,
};
static const auto jlapplytype_func = new JuliaFunction{
"jl_instantiate_type_in_env",
[](LLVMContext &C) { return FunctionType::get(T_prjlvalue,
{T_pjlvalue, T_pjlvalue, T_pprjlvalue}, false); },
[](LLVMContext &C) {
return AttributeList::get(C,
AttributeSet(),
AttributeSet::get(C, makeArrayRef({Attribute::get(C, Attribute::NonNull),
Attribute::getWithAlignment(C, Align(16))})),
None);
},
};
static const auto jl_object_id__func = new JuliaFunction{
"jl_object_id_",
[](LLVMContext &C) { return FunctionType::get(T_size,
{T_prjlvalue, PointerType::get(T_int8, AddressSpace::Derived)}, false); },
nullptr,
};
static const auto setjmp_func = new JuliaFunction{
jl_setjmp_name,
[](LLVMContext &C) { return FunctionType::get(T_int32,
{T_pint8,
#ifndef _OS_WINDOWS_
T_int32,
#endif
}, false); },
[](LLVMContext &C) { return AttributeList::get(C,
Attributes(C, {Attribute::ReturnsTwice}),
AttributeSet(),
None); },
};
static const auto memcmp_func = new JuliaFunction{
"memcmp",
[](LLVMContext &C) { return FunctionType::get(T_int32,
{T_pint8, T_pint8, T_size}, false); },
[](LLVMContext &C) { return AttributeList::get(C,
Attributes(C, {Attribute::ReadOnly, Attribute::NoUnwind, Attribute::ArgMemOnly}),
AttributeSet(),
None); },
// TODO: inferLibFuncAttributes(*memcmp_func, TLI);
};
static const auto jldlsym_func = new JuliaFunction{
"jl_load_and_lookup",
[](LLVMContext &C) { return FunctionType::get(T_pvoidfunc,
{T_pint8, T_pint8, PointerType::get(T_pint8, 0)}, false); },
nullptr,
};
static const auto jllazydlsym_func = new JuliaFunction{
"jl_lazy_load_and_lookup",
[](LLVMContext &C) { return FunctionType::get(T_pvoidfunc,
{T_prjlvalue, T_pint8}, false); },
nullptr,
};
static const auto jltypeassert_func = new JuliaFunction{
"jl_typeassert",
[](LLVMContext &C) { return FunctionType::get(T_void,
{T_prjlvalue, T_prjlvalue}, false); },
nullptr,
};
static const auto jlgetnthfieldchecked_func = new JuliaFunction{
"jl_get_nth_field_checked",
[](LLVMContext &C) { return FunctionType::get(T_prjlvalue,
{T_prjlvalue, T_size}, false); },
[](LLVMContext &C) { return AttributeList::get(C,
AttributeSet(),
Attributes(C, {Attribute::NonNull}),
None); },
};
static const auto jlgetcfunctiontrampoline_func = new JuliaFunction{
"jl_get_cfunction_trampoline",
[](LLVMContext &C) { return FunctionType::get(T_prjlvalue,
{
T_prjlvalue, // f (object)
T_pjlvalue, // result
T_pint8, // cache
T_pjlvalue, // fill
FunctionType::get(T_pint8, { T_pint8, T_ppjlvalue }, false)->getPointerTo(), // trampoline
T_pjlvalue, // env
T_pprjlvalue, // vals
}, false); },
[](LLVMContext &C) { return AttributeList::get(C,
AttributeSet(),
Attributes(C, {Attribute::NonNull}),
None); },
};
static const auto diff_gc_total_bytes_func = new JuliaFunction{
"jl_gc_diff_total_bytes",
[](LLVMContext &C) { return FunctionType::get(T_int64, false); },
nullptr,
};
static const auto sync_gc_total_bytes_func = new JuliaFunction{
"jl_gc_sync_total_bytes",
[](LLVMContext &C) { return FunctionType::get(T_int64,
{T_int64}, false); },
nullptr,
};
static const auto jlarray_data_owner_func = new JuliaFunction{
"jl_array_data_owner",
[](LLVMContext &C) { return FunctionType::get(T_prjlvalue,
{T_prjlvalue}, false); },
[](LLVMContext &C) { return AttributeList::get(C,
Attributes(C, {Attribute::ReadOnly, Attribute::NoUnwind}),
Attributes(C, {Attribute::NonNull}),
None); },
};
#define BOX_FUNC(ct,rt,at,attrs) \
static const auto box_##ct##_func = new JuliaFunction{ \
"jl_box_"#ct, \
[](LLVMContext &C) { return FunctionType::get(rt, \
{at}, false); }, \
attrs, \
}
BOX_FUNC(int16, T_prjlvalue, T_int16, get_attrs_sext);
BOX_FUNC(uint16, T_prjlvalue, T_int16, get_attrs_zext);
BOX_FUNC(int32, T_prjlvalue, T_int32, get_attrs_sext);
BOX_FUNC(uint32, T_prjlvalue, T_int32, get_attrs_zext);
BOX_FUNC(int64, T_prjlvalue, T_int64, get_attrs_sext);
BOX_FUNC(uint64, T_prjlvalue, T_int64, get_attrs_zext);
BOX_FUNC(char, T_prjlvalue, T_char, get_attrs_zext);
BOX_FUNC(float32, T_prjlvalue, T_float32, get_func_attrs);
BOX_FUNC(float64, T_prjlvalue, T_float64, get_func_attrs);
BOX_FUNC(ssavalue, T_prjlvalue, T_size, get_func_attrs);
#undef BOX_FUNC
// placeholder functions
static const auto gcroot_flush_func = new JuliaFunction{
"julia.gcroot_flush",
[](LLVMContext &C) { return FunctionType::get(T_void, false); },
nullptr,
};
static const auto gc_preserve_begin_func = new JuliaFunction{
"llvm.julia.gc_preserve_begin",
[](LLVMContext &C) { return FunctionType::get(Type::getTokenTy(C), true); },
nullptr,
};
static const auto gc_preserve_end_func = new JuliaFunction {
"llvm.julia.gc_preserve_end",
[](LLVMContext &C) { return FunctionType::get(T_void, {Type::getTokenTy(C)}, false); },
nullptr,
};
static const auto except_enter_func = new JuliaFunction{
"julia.except_enter",
[](LLVMContext &C) { return FunctionType::get(T_int32, false); },
[](LLVMContext &C) { return AttributeList::get(C,
AttributeSet::get(C, makeArrayRef({Attribute::get(C, Attribute::ReturnsTwice)})),
AttributeSet(),
None); },
};
static const auto pointer_from_objref_func = new JuliaFunction{
"julia.pointer_from_objref",
[](LLVMContext &C) { return FunctionType::get(T_pjlvalue,
{PointerType::get(T_jlvalue, AddressSpace::Derived)}, false); },
[](LLVMContext &C) { return AttributeList::get(C,
AttributeSet::get(C, makeArrayRef({Attribute::get(C, Attribute::ReadNone), Attribute::get(C, Attribute::NoUnwind)})),
Attributes(C, {Attribute::NonNull}),
None); },
};
static const auto jltuple_func = new JuliaFunction{"jl_f_tuple", get_func_sig, get_func_attrs};
static const auto jlgetfield_func = new JuliaFunction{"jl_f_getfield", get_func_sig, get_func_attrs};
static const std::map<jl_fptr_args_t, JuliaFunction*> builtin_func_map = {
{ &jl_f_is, new JuliaFunction{"jl_f_is", get_func_sig, get_func_attrs} },
{ &jl_f_typeof, new JuliaFunction{"jl_f_typeof", get_func_sig, get_func_attrs} },
{ &jl_f_sizeof, new JuliaFunction{"jl_f_sizeof", get_func_sig, get_func_attrs} },
{ &jl_f_issubtype, new JuliaFunction{"jl_f_issubtype", get_func_sig, get_func_attrs} },
{ &jl_f_isa, new JuliaFunction{"jl_f_isa", get_func_sig, get_func_attrs} },
{ &jl_f_typeassert, new JuliaFunction{"jl_f_typeassert", get_func_sig, get_func_attrs} },
{ &jl_f_ifelse, new JuliaFunction{"jl_f_ifelse", get_func_sig, get_func_attrs} },
{ &jl_f__apply_iterate, new JuliaFunction{"jl_f__apply_iterate", get_func_sig, get_func_attrs} },
{ &jl_f__apply_pure, new JuliaFunction{"jl_f__apply_pure", get_func_sig, get_func_attrs} },
{ &jl_f__call_latest, new JuliaFunction{"jl_f__call_latest", get_func_sig, get_func_attrs} },
{ &jl_f__call_in_world, new JuliaFunction{"jl_f__call_in_world", get_func_sig, get_func_attrs} },
{ &jl_f_throw, new JuliaFunction{"jl_f_throw", get_func_sig, get_func_attrs} },
{ &jl_f_tuple, jltuple_func },
{ &jl_f_svec, new JuliaFunction{"jl_f_svec", get_func_sig, get_func_attrs} },
{ &jl_f_applicable, new JuliaFunction{"jl_f_applicable", get_func_sig, get_func_attrs} },
{ &jl_f_invoke, new JuliaFunction{"jl_f_invoke", get_func_sig, get_func_attrs} },
{ &jl_f_invoke_kwsorter, new JuliaFunction{"jl_f_invoke_kwsorter", get_func_sig, get_func_attrs} },
{ &jl_f_isdefined, new JuliaFunction{"jl_f_isdefined", get_func_sig, get_func_attrs} },
{ &jl_f_getfield, jlgetfield_func },
{ &jl_f_setfield, new JuliaFunction{"jl_f_setfield", get_func_sig, get_func_attrs} },
{ &jl_f_fieldtype, new JuliaFunction{"jl_f_fieldtype", get_func_sig, get_func_attrs} },
{ &jl_f_nfields, new JuliaFunction{"jl_f_nfields", get_func_sig, get_func_attrs} },
{ &jl_f__expr, new JuliaFunction{"jl_f__expr", get_func_sig, get_func_attrs} },
{ &jl_f__typevar, new JuliaFunction{"jl_f__typevar", get_func_sig, get_func_attrs} },
{ &jl_f_arrayref, new JuliaFunction{"jl_f_arrayref", get_func_sig, get_func_attrs} },
{ &jl_f_const_arrayref, new JuliaFunction{"jl_f_const_arrayref", get_func_sig, get_func_attrs} },
{ &jl_f_arrayset, new JuliaFunction{"jl_f_arrayset", get_func_sig, get_func_attrs} },
{ &jl_f_arraysize, new JuliaFunction{"jl_f_arraysize", get_func_sig, get_func_attrs} },
{ &jl_f_apply_type, new JuliaFunction{"jl_f_apply_type", get_func_sig, get_func_attrs} },
};
static const auto jl_new_opaque_closure_jlcall_func = new JuliaFunction{"jl_new_opaque_closure_jlcall", get_func_sig, get_func_attrs};
static int globalUnique = 0;
// --- code generation ---
extern "C" {
int jl_default_debug_info_kind = (int) DICompileUnit::DebugEmissionKind::FullDebug;
jl_cgparams_t jl_default_cgparams = {1, 1, 0,
#ifdef _OS_WINDOWS_
0,
#else
1,
#endif
jl_default_debug_info_kind,
jl_rettype_inferred, NULL };
}
template<typename T>
static void add_return_attr(T *f, Attribute::AttrKind Kind)
{
f->addAttribute(AttributeList::ReturnIndex, Kind);
}
static MDNode *best_tbaa(jl_value_t *jt) {
jt = jl_unwrap_unionall(jt);
if (jt == (jl_value_t*)jl_datatype_type ||
(jl_is_type_type(jt) && jl_is_datatype(jl_tparam0(jt))))
return tbaa_datatype;
if (!jl_is_datatype(jt))
return tbaa_value;
if (jl_is_abstracttype(jt))
return tbaa_value;
// If we're here, we know all subtypes are (im)mutable, even if we
// don't know what the exact type is
return jl_is_mutable(jt) ? tbaa_mutab : tbaa_immut;
}
// tracks whether codegen is currently able to simply stack-allocate this type
// note that this includes jl_isbits, although codegen should work regardless
static bool jl_is_concrete_immutable(jl_value_t* t)
{
return jl_is_immutable_datatype(t) && ((jl_datatype_t*)t)->layout;
}
static bool jl_is_pointerfree(jl_value_t* t)
{
if (!jl_is_immutable_datatype(t))
return 0;
const jl_datatype_layout_t *layout = ((jl_datatype_t*)t)->layout;
return layout && layout->npointers == 0;
}
// these queries are usually related, but we split them out here
// for convenience and clarity (and because it changes the calling convention)
static bool deserves_stack(jl_value_t* t, bool pointerfree=false)
{
if (!jl_is_concrete_immutable(t))
return false;
return ((jl_datatype_t*)t)->isinlinealloc;
}
static bool deserves_argbox(jl_value_t* t)
{
return !deserves_stack(t);
}
static bool deserves_retbox(jl_value_t* t)
{
return deserves_argbox(t);
}
static bool deserves_sret(jl_value_t *dt, Type *T)
{
assert(jl_is_datatype(dt));
return (size_t)jl_datatype_size(dt) > sizeof(void*) && !T->isFloatingPointTy() && !T->isVectorTy();
}
// metadata tracking for a llvm Value* during codegen
struct jl_cgval_t {
Value *V; // may be of type T* or T, or set to NULL if ghost (or if the value has not been initialized yet, for a variable definition)
// For unions, we may need to keep a reference to the boxed part individually.
// If this is non-NULL, then, at runtime, we satisfy the invariant that (for the corresponding
// runtime values) if `(TIndex | 0x80) != 0`, then `Vboxed == V` (by value).
// For convenience, we also set this value of isboxed values, in which case
// it is equal (at compile time) to V.
// If this is non-NULL, it is always of type `T_prjlvalue`
Value *Vboxed;
Value *TIndex; // if `V` is an unboxed (tagged) Union described by `typ`, this gives the DataType index (1-based, small int) as an i8
jl_value_t *constant; // constant value (rooted in linfo.def.roots)
jl_value_t *typ; // the original type of V, never NULL
bool isboxed; // whether this value is a jl_value_t* allocated on the heap with the right type tag
bool isghost; // whether this value is "ghost"
MDNode *tbaa; // The related tbaa node. Non-NULL iff this holds an address.
bool ispointer() const
{
// whether this value is compatible with `data_pointer`
return tbaa != nullptr;
}
jl_cgval_t(Value *V, Value *gcroot, bool isboxed, jl_value_t *typ, Value *tindex) : // general constructor (with pointer type auto-detect)
V(V), // V is allowed to be NULL in a jl_varinfo_t context, but not during codegen contexts
Vboxed(isboxed ? V : nullptr),
TIndex(tindex),
constant(NULL),
typ(typ),
isboxed(isboxed),
isghost(false),
tbaa(isboxed ? best_tbaa(typ) : nullptr)
{
if (Vboxed)
assert(Vboxed->getType() == T_prjlvalue);
assert(gcroot == nullptr);
assert(!(isboxed && TIndex != NULL));
assert(TIndex == NULL || TIndex->getType() == T_int8);
}
explicit jl_cgval_t(jl_value_t *typ) : // ghost value constructor
// mark explicit to avoid being used implicitly for conversion from NULL (use jl_cgval_t() instead)
V(NULL),
Vboxed(NULL),
TIndex(NULL),
constant(((jl_datatype_t*)typ)->instance),
typ(typ),
isboxed(false),
isghost(true),
tbaa(nullptr)
{
assert(jl_is_datatype(typ));
assert(constant);
}
jl_cgval_t(const jl_cgval_t &v, jl_value_t *typ, Value *tindex) : // copy constructor with new type
V(v.V),
Vboxed(v.Vboxed),
TIndex(tindex),
constant(v.constant),
typ(typ),