-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
codegen.cpp
10107 lines (9540 loc) · 447 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
#undef DEBUG
#include "llvm-version.h"
#include "platform.h"
#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/MC/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>
#include <llvm/Analysis/InstructionSimplify.h>
// support
#include <llvm/ADT/SmallBitVector.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>
#include <llvm/Support/Process.h>
#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>
#include "llvm/Support/Path.h" // for llvm::sys::path
#include <llvm/Bitcode/BitcodeReader.h>
#include <llvm/Linker/Linker.h>
using namespace llvm;
static bool jl_fpo_disabled(const Triple &TT) {
#ifdef JL_DISABLE_FPO
return true;
#endif
#ifdef _COMPILER_MSAN_ENABLED_
// MSAN doesn't support FPO
return true;
#endif
if (TT.isOSLinux() || TT.isOSWindows() || TT.isOSFreeBSD()) {
return true;
}
return false;
}
static bool jl_floattemp_var_needed(const Triple &TT) {
#ifdef JL_NEED_FLOATTEMP_VAR
return true;
#endif
return TT.getArch() == Triple::x86;
}
//Drag some useful type functions into our namespace
//to reduce verbosity of our code
auto getInt1Ty(LLVMContext &ctxt) {
return Type::getInt1Ty(ctxt);
}
auto getInt8Ty(LLVMContext &ctxt) {
return Type::getInt8Ty(ctxt);
}
auto getInt16Ty(LLVMContext &ctxt) {
return Type::getInt16Ty(ctxt);
}
auto getInt32Ty(LLVMContext &ctxt) {
return Type::getInt32Ty(ctxt);
}
auto getInt64Ty(LLVMContext &ctxt) {
return Type::getInt64Ty(ctxt);
}
auto getHalfTy(LLVMContext &ctxt) {
return Type::getHalfTy(ctxt);
}
auto getFloatTy(LLVMContext &ctxt) {
return Type::getFloatTy(ctxt);
}
auto getDoubleTy(LLVMContext &ctxt) {
return Type::getDoubleTy(ctxt);
}
auto getBFloatTy(LLVMContext &ctxt) {
return Type::getBFloatTy(ctxt);
}
auto getFP128Ty(LLVMContext &ctxt) {
return Type::getFP128Ty(ctxt);
}
auto getVoidTy(LLVMContext &ctxt) {
return Type::getVoidTy(ctxt);
}
auto getCharTy(LLVMContext &ctxt) {
return getInt32Ty(ctxt);
}
auto getInt8PtrTy(LLVMContext &ctxt) {
return Type::getInt8PtrTy(ctxt);
}
auto getInt16PtrTy(LLVMContext &ctxt) {
return Type::getInt16PtrTy(ctxt);
}
auto getInt32PtrTy(LLVMContext &ctxt) {
return Type::getInt32PtrTy(ctxt);
}
auto getInt64PtrTy(LLVMContext &ctxt) {
return Type::getInt64PtrTy(ctxt);
}
auto getFloatPtrTy(LLVMContext &ctxt) {
return Type::getFloatPtrTy(ctxt);
}
auto getDoublePtrTy(LLVMContext &ctxt) {
return Type::getDoublePtrTy(ctxt);
}
typedef Instruction TerminatorInst;
#if defined(_OS_WINDOWS_) && !defined(NOMINMAX)
#define NOMINMAX
#endif
#include "jitlayers.h"
#include "processor.h"
#include "julia_assert.h"
#undef DEBUG_TYPE //LLVM occasionally likes to set DEBUG_TYPE in a header...
#define DEBUG_TYPE "julia_irgen_codegen"
void setName(jl_codegen_params_t ¶ms, Value *V, const Twine &Name)
{
// we do the constant check again later, duplicating it here just makes sure the assertion
// fires on debug builds even if debug info is not enabled
// note that if this assertion fires then the implication is that the caller of setName
// is not checking that setName is only called for non-folded instructions (e.g. folded bitcasts
// and 0-byte geps), which can result in information loss on the renamed instruction.
assert((isa<Constant>(V) || isa<Instruction>(V)) && "Should only set names on instructions!");
if (params.debug_level >= 2 && !isa<Constant>(V)) {
V->setName(Name);
}
}
void setName(jl_codegen_params_t ¶ms, Value *V, std::function<std::string()> GetName)
{
assert((isa<Constant>(V) || isa<Instruction>(V)) && "Should only set names on instructions!");
if (params.debug_level >= 2 && !isa<Constant>(V)) {
V->setName(Twine(GetName()));
}
}
void setNameWithField(jl_codegen_params_t ¶ms, Value *V, std::function<StringRef()> GetObjName, jl_datatype_t *jt, unsigned idx, const Twine &suffix)
{
assert((isa<Constant>(V) || isa<Instruction>(V)) && "Should only set names on instructions!");
if (params.debug_level >= 2 && !isa<Constant>(V)) {
if (jl_is_tuple_type(jt)){
V->setName(Twine(GetObjName()) + "[" + Twine(idx + 1) + "]"+ suffix);
return;
}
if (jl_is_namedtuple_type(jt)) {
auto names = jl_tparam0(jt);
assert(jl_is_tuple(names));
if (idx < jl_nfields(names)) {
auto name = jl_fieldref(names, idx);
assert(jl_is_symbol(name));
V->setName(Twine(GetObjName()) + "." + Twine(jl_symbol_name((jl_sym_t*)name)) + suffix);
return;
}
} else {
auto flds = jl_field_names(jt);
if (idx < jl_svec_len(flds)) {
auto name = jl_svecref(flds, idx);
assert(jl_is_symbol(name));
V->setName(Twine(GetObjName()) + "." + Twine(jl_symbol_name((jl_sym_t*)name)) + suffix);
return;
}
}
V->setName(Twine(GetObjName()) + "." + Twine("unknown field") + suffix);
}
}
STATISTIC(EmittedAllocas, "Number of allocas emitted");
STATISTIC(EmittedIntToPtrs, "Number of inttoptrs emitted");
STATISTIC(ModulesCreated, "Number of LLVM Modules created");
STATISTIC(EmittedBoxCompares, "Number of box compares emitted");
STATISTIC(EmittedBitsUnionCompares, "Number of bitsunion compares emitted");
STATISTIC(EmittedBitsCompares, "Number of bits compares emitted");
STATISTIC(EmittedEgals, "Number of egals emitted");
STATISTIC(EmittedOpfields, "Number of opfields emitted");
STATISTIC(EmittedBuiltinCalls, "Number of builtin calls emitted");
STATISTIC(EmittedJLCalls, "Number of jlcalls emitted");
STATISTIC(EmittedSpecfunCalls, "Number of specialized calls emitted");
STATISTIC(EmittedInvokes, "Number of invokes emitted");
STATISTIC(EmittedCalls, "Number of calls emitted");
STATISTIC(EmittedUndefVarErrors, "Number of undef var errors emitted");
STATISTIC(EmittedOpaqueClosureFunctions, "Number of opaque closures emitted");
STATISTIC(EmittedToJLInvokes, "Number of tojlinvoke calls emitted");
STATISTIC(EmittedCFuncInvalidates, "Number of C function invalidates emitted");
STATISTIC(GeneratedCFuncWrappers, "Number of C function wrappers generated");
STATISTIC(GeneratedCCallables, "Number of C-callable functions generated");
STATISTIC(GeneratedInvokeWrappers, "Number of invoke wrappers generated");
STATISTIC(EmittedFunctions, "Number of functions emitted");
extern "C" JL_DLLEXPORT_CODEGEN
void jl_dump_emitted_mi_name_impl(void *s)
{
**jl_ExecutionEngine->get_dump_emitted_mi_name_stream() = (ios_t*)s;
}
extern "C" {
#include "builtin_proto.h"
extern void __stack_chk_fail();
#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
}
// shared llvm state
#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
struct jl_typecache_t {
Type *T_size;
Type *T_jlvalue;
Type *T_pjlvalue;
Type *T_prjlvalue;
Type *T_ppjlvalue;
Type *T_pprjlvalue;
StructType *T_jlgenericmemory;
StructType *T_jlarray;
Type *T_pjlarray;
FunctionType *T_jlfunc;
FunctionType *T_jlfuncparams;
IntegerType *T_sigatomic;
Type *T_ppint8;
unsigned sizeof_ptr;
Align alignof_ptr;
bool initialized;
jl_typecache_t() :
T_jlvalue(nullptr), T_pjlvalue(nullptr), T_prjlvalue(nullptr),
T_ppjlvalue(nullptr), T_pprjlvalue(nullptr),
T_jlgenericmemory(nullptr), T_jlarray(nullptr), T_pjlarray(nullptr),
T_jlfunc(nullptr), T_jlfuncparams(nullptr), T_sigatomic(nullptr), T_ppint8(nullptr),
initialized(false) {}
void initialize(LLVMContext &context, const DataLayout &DL) {
if (initialized) {
return;
}
initialized = true;
T_ppint8 = PointerType::get(getInt8PtrTy(context), 0);
T_sigatomic = Type::getIntNTy(context, sizeof(sig_atomic_t) * 8);
T_size = DL.getIntPtrType(context);
sizeof_ptr = DL.getPointerSize();
// use pointer abi alignment for intptr_t
alignof_ptr = DL.getPointerABIAlignment(0);
T_jlvalue = JuliaType::get_jlvalue_ty(context);
T_pjlvalue = PointerType::get(T_jlvalue, 0);
T_prjlvalue = PointerType::get(T_jlvalue, AddressSpace::Tracked);
T_ppjlvalue = PointerType::get(T_pjlvalue, 0);
T_pprjlvalue = PointerType::get(T_prjlvalue, 0);
T_jlfunc = JuliaType::get_jlfunc_ty(context);
assert(T_jlfunc != NULL);
T_jlfuncparams = JuliaType::get_jlfuncparams_ty(context);
assert(T_jlfuncparams != NULL);
T_jlgenericmemory = StructType::get(context, { T_size, T_pprjlvalue /* [, real-owner] */ });
Type *vaelts[] = { PointerType::get(getInt8Ty(context), AddressSpace::Loaded),
PointerType::get(T_jlgenericmemory, AddressSpace::Tracked),
// dimsize[ndims]
};
T_jlarray = StructType::get(context, ArrayRef<Type*>(vaelts));
T_pjlarray = PointerType::get(T_jlarray, 0);
}
};
struct jl_tbaacache_t {
// type-based alias analysis nodes. Indentation of comments indicates hierarchy.
MDNode *tbaa_root; // Everything
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`
MDNode *tbaa_stack; // stack slot
MDNode *tbaa_unionselbyte; // a selector byte in isbits Union struct fields
MDNode *tbaa_data; // Any user data that `pointerset/ref` are allowed to alias
MDNode *tbaa_binding; // jl_binding_t::value
MDNode *tbaa_value; // jl_value_t, that is not jl_array_t or jl_genericmemory_t
MDNode *tbaa_mutab; // mutable type
MDNode *tbaa_datatype; // datatype
MDNode *tbaa_immut; // immutable type
MDNode *tbaa_ptrarraybuf; // Data in an array of boxed values
MDNode *tbaa_arraybuf; // Data in an array of POD
MDNode *tbaa_array; // jl_array_t or jl_genericmemory_t
MDNode *tbaa_arrayptr; // The pointer inside a jl_array_t (to memoryref)
MDNode *tbaa_arraysize; // A size in a jl_array_t
MDNode *tbaa_arrayselbyte; // a selector byte in a isbits Union jl_genericmemory_t
MDNode *tbaa_memoryptr; // The pointer inside a jl_genericmemory_t
MDNode *tbaa_memorylen; // The length in a jl_genericmemory_t
MDNode *tbaa_memoryown; // The owner in a foreign jl_genericmemory_t
MDNode *tbaa_const; // Memory that is immutable by the time LLVM can see it
bool initialized;
jl_tbaacache_t(): tbaa_root(nullptr), tbaa_gcframe(nullptr), tbaa_stack(nullptr),
tbaa_unionselbyte(nullptr), tbaa_data(nullptr), tbaa_binding(nullptr),
tbaa_value(nullptr), tbaa_mutab(nullptr), tbaa_datatype(nullptr),
tbaa_immut(nullptr), tbaa_ptrarraybuf(nullptr), tbaa_arraybuf(nullptr),
tbaa_array(nullptr), tbaa_arrayptr(nullptr), tbaa_arraysize(nullptr),
tbaa_arrayselbyte(nullptr), tbaa_memoryptr(nullptr), tbaa_memorylen(nullptr), tbaa_memoryown(nullptr),
tbaa_const(nullptr), initialized(false) {}
auto tbaa_make_child(MDBuilder &mbuilder, const char *name, MDNode *parent = nullptr, bool isConstant = false) {
MDNode *scalar = mbuilder.createTBAAScalarTypeNode(name, parent ? parent : tbaa_root);
MDNode *n = mbuilder.createTBAAStructTagNode(scalar, scalar, 0, isConstant);
return std::make_pair(n, scalar);
}
void initialize(llvm::LLVMContext &context) {
if (initialized) {
assert(&tbaa_root->getContext() == &context);
return;
}
initialized = true;
MDBuilder mbuilder(context);
MDNode *jtbaa = mbuilder.createTBAARoot("jtbaa");
tbaa_root = mbuilder.createTBAAScalarTypeNode("jtbaa", jtbaa);
tbaa_gcframe = tbaa_make_child(mbuilder, "jtbaa_gcframe").first;
MDNode *tbaa_stack_scalar;
std::tie(tbaa_stack, tbaa_stack_scalar) = tbaa_make_child(mbuilder, "jtbaa_stack");
tbaa_unionselbyte = tbaa_make_child(mbuilder, "jtbaa_unionselbyte", tbaa_stack_scalar).first;
MDNode *tbaa_data_scalar;
std::tie(tbaa_data, tbaa_data_scalar) = tbaa_make_child(mbuilder, "jtbaa_data");
tbaa_binding = tbaa_make_child(mbuilder, "jtbaa_binding", tbaa_data_scalar).first;
MDNode *tbaa_value_scalar;
std::tie(tbaa_value, tbaa_value_scalar) =
tbaa_make_child(mbuilder, "jtbaa_value", tbaa_data_scalar);
MDNode *tbaa_mutab_scalar;
std::tie(tbaa_mutab, tbaa_mutab_scalar) =
tbaa_make_child(mbuilder, "jtbaa_mutab", tbaa_value_scalar);
tbaa_datatype = tbaa_make_child(mbuilder, "jtbaa_datatype", tbaa_mutab_scalar).first;
tbaa_immut = tbaa_make_child(mbuilder, "jtbaa_immut", tbaa_value_scalar).first;
tbaa_arraybuf = tbaa_make_child(mbuilder, "jtbaa_arraybuf", tbaa_data_scalar).first;
tbaa_ptrarraybuf = tbaa_make_child(mbuilder, "jtbaa_ptrarraybuf", tbaa_data_scalar).first;
MDNode *tbaa_array_scalar;
std::tie(tbaa_array, tbaa_array_scalar) = tbaa_make_child(mbuilder, "jtbaa_array");
tbaa_arrayptr = tbaa_make_child(mbuilder, "jtbaa_arrayptr", tbaa_array_scalar).first;
tbaa_arraysize = tbaa_make_child(mbuilder, "jtbaa_arraysize", tbaa_array_scalar).first;
tbaa_arrayselbyte = tbaa_make_child(mbuilder, "jtbaa_arrayselbyte", tbaa_array_scalar).first;
tbaa_memoryptr = tbaa_make_child(mbuilder, "jtbaa_memoryptr", tbaa_array_scalar, true).first;
tbaa_memorylen = tbaa_make_child(mbuilder, "jtbaa_memorylen", tbaa_array_scalar, true).first;
tbaa_memoryown = tbaa_make_child(mbuilder, "jtbaa_memoryown", tbaa_array_scalar, true).first;
tbaa_const = tbaa_make_child(mbuilder, "jtbaa_const", nullptr, true).first;
}
};
struct jl_noaliascache_t {
// Each domain operates completely independently.
// "No aliasing" is inferred if it is implied by any domain.
// memory regions domain
struct jl_regions_t {
MDNode *gcframe; // GC frame
MDNode *stack; // Stack slot
MDNode *data; // Any user data that `pointerset/ref` are allowed to alias
MDNode *type_metadata; // Non-user-accessible type metadata incl. union selectors, etc.
MDNode *constant; // Memory that is immutable by the time LLVM can see it
jl_regions_t(): gcframe(nullptr), stack(nullptr), data(nullptr), type_metadata(nullptr), constant(nullptr) {}
void initialize(llvm::LLVMContext &context) {
MDBuilder mbuilder(context);
MDNode *domain = mbuilder.createAliasScopeDomain("jnoalias");
this->gcframe = mbuilder.createAliasScope("jnoalias_gcframe", domain);
this->stack = mbuilder.createAliasScope("jnoalias_stack", domain);
this->data = mbuilder.createAliasScope("jnoalias_data", domain);
this->type_metadata = mbuilder.createAliasScope("jnoalias_typemd", domain);
this->constant = mbuilder.createAliasScope("jnoalias_const", domain);
}
} regions;
// `@aliasscope` domain
struct jl_aliasscope_t {
MDNode *current;
jl_aliasscope_t(): current(nullptr) {}
// No init required, this->current is only used to store the currently active aliasscope
void initialize(llvm::LLVMContext &context) {}
} aliasscope;
bool initialized;
jl_noaliascache_t(): regions(), aliasscope(), initialized(false) {}
void initialize(llvm::LLVMContext &context) {
if (initialized) {
assert(®ions.constant->getContext() == &context);
return;
}
initialized = true;
regions.initialize(context);
aliasscope.initialize(context);
}
};
struct jl_debugcache_t {
// Basic DITypes
DIDerivedType *jl_pvalue_dillvmt;
DIDerivedType *jl_ppvalue_dillvmt;
DISubroutineType *jl_di_func_sig;
DISubroutineType *jl_di_func_null_sig;
bool initialized;
jl_debugcache_t()
: jl_pvalue_dillvmt(nullptr), jl_ppvalue_dillvmt(nullptr),
jl_di_func_sig(nullptr), jl_di_func_null_sig(nullptr), initialized(false) {}
void initialize(Module *m);
};
// constants
static bool type_is_ghost(Type *ty)
{
return (ty == getVoidTy(ty->getContext()) || 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)(Type *T_size);
JuliaVariable(const JuliaVariable&) = delete;
JuliaVariable(const JuliaVariable&&) = delete;
GlobalVariable *realize(Module *m) {
if (GlobalValue *V = m->getNamedValue(name))
return cast<GlobalVariable>(V);
auto T_size = m->getDataLayout().getIntPtrType(m->getContext());
return new GlobalVariable(*m, _type(T_size),
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);
}
typedef FunctionType *(*TypeFnContextOnly)(LLVMContext &C);
typedef FunctionType *(*TypeFnContextAndSizeT)(LLVMContext &C, Type *T_size);
typedef FunctionType *(*TypeFnContextAndTriple)(LLVMContext &C, const Triple &triple);
FunctionType *invoke_type(TypeFnContextOnly f, Module &M)
{
return f(M.getContext());
}
FunctionType *invoke_type(TypeFnContextAndSizeT f, Module &M)
{
return f(M.getContext(), M.getDataLayout().getIntPtrType(M.getContext()));
}
FunctionType *invoke_type(TypeFnContextAndTriple f, Module &M)
{
return f(M.getContext(), Triple(M.getTargetTriple()));
}
template<typename TypeFn_t = TypeFnContextOnly>
struct JuliaFunction {
public:
llvm::StringLiteral name;
TypeFn_t _type;
llvm::AttributeList (*_attrs)(llvm::LLVMContext &C);
JuliaFunction(const JuliaFunction&) = delete;
JuliaFunction(const JuliaFunction&&) = delete;
llvm::Function *realize(llvm::Module *m) {
if (llvm::GlobalValue *V = m->getNamedValue(name))
return llvm::cast<llvm::Function>(V);
llvm::Function *F = llvm::Function::Create(invoke_type(_type, *m),
llvm::Function::ExternalLinkage,
name, m);
if (_attrs)
F->setAttributes(_attrs(m->getContext()));
return F;
}
};
template<typename T, typename TypeFn_t>
static inline void add_named_global(JuliaFunction<TypeFn_t> *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, std::initializer_list<Attribute> extra={})
{
SmallVector<Attribute, 8> attrs(attrkinds.size() + extra.size());
for (size_t i = 0; i < attrkinds.size(); i++)
attrs[i] = Attribute::get(C, attrkinds.begin()[i]);
for (size_t i = 0; i < extra.size(); i++)
attrs[attrkinds.size() + i] = extra.begin()[i];
return AttributeSet::get(C, ArrayRef<Attribute>(attrs));
}
static Type *get_pjlvalue(LLVMContext &C) { return JuliaType::get_pjlvalue_ty(C); }
static FunctionType *get_func_sig(LLVMContext &C) { return JuliaType::get_jlfunc_ty(C); }
static FunctionType *get_func2_sig(LLVMContext &C) { return JuliaType::get_jlfunc2_ty(C); }
static FunctionType *get_func3_sig(LLVMContext &C) { return JuliaType::get_jlfunc3_ty(C); }
static FunctionType *get_donotdelete_sig(LLVMContext &C) {
return FunctionType::get(getVoidTy(C), true);
}
static AttributeList get_func_attrs(LLVMContext &C)
{
return AttributeList::get(C,
AttributeSet(),
Attributes(C, {Attribute::NonNull}),
{AttributeSet(),
Attributes(C, {Attribute::NoAlias, Attribute::ReadOnly, Attribute::NoCapture, Attribute::NoUndef})});
}
static AttributeList get_donotdelete_func_attrs(LLVMContext &C)
{
AttrBuilder FnAttrs(C);
#if JL_LLVM_VERSION >= 160000
FnAttrs.addMemoryAttr(MemoryEffects::inaccessibleMemOnly());
#else
FnAttrs.addAttribute(Attribute::InaccessibleMemOnly);
#endif
FnAttrs.addAttribute(Attribute::WillReturn);
FnAttrs.addAttribute(Attribute::NoUnwind);
return AttributeList::get(C,
AttributeSet::get(C, FnAttrs),
Attributes(C, {}),
None);
}
static AttributeList get_attrs_noreturn(LLVMContext &C)
{
return AttributeList::get(C,
Attributes(C, {Attribute::NoReturn}),
AttributeSet(),
None);
}
static AttributeList get_attrs_basic(LLVMContext &C)
{
return AttributeList::get(C,
AttributeSet(),
Attributes(C, {Attribute::NonNull}),
None);
}
static AttributeList get_attrs_box_float(LLVMContext &C, unsigned nbytes)
{
auto FnAttrs = AttrBuilder(C);
FnAttrs.addAttribute(Attribute::WillReturn);
FnAttrs.addAttribute(Attribute::NoUnwind);
#if JL_LLVM_VERSION >= 160000
FnAttrs.addMemoryAttr(MemoryEffects::inaccessibleMemOnly());
#else
FnAttrs.addAttribute(Attribute::InaccessibleMemOnly);
#endif
auto RetAttrs = AttrBuilder(C);
RetAttrs.addAttribute(Attribute::NonNull);
RetAttrs.addDereferenceableAttr(nbytes);
RetAttrs.addAlignmentAttr(Align(alignof(void*)));
return AttributeList::get(C,
AttributeSet::get(C, FnAttrs),
AttributeSet::get(C, RetAttrs),
None);
}
static AttributeList get_attrs_box_sext(LLVMContext &C, unsigned nbytes)
{
auto FnAttrs = AttrBuilder(C);
FnAttrs.addAttribute(Attribute::WillReturn);
FnAttrs.addAttribute(Attribute::NoUnwind);
#if JL_LLVM_VERSION >= 160000
FnAttrs.addMemoryAttr(MemoryEffects::inaccessibleMemOnly());
#else
FnAttrs.addAttribute(Attribute::InaccessibleMemOnly);
#endif
auto RetAttrs = AttrBuilder(C);
RetAttrs.addAttribute(Attribute::NonNull);
RetAttrs.addAttribute(Attribute::getWithDereferenceableBytes(C, nbytes));
RetAttrs.addDereferenceableAttr(nbytes);
RetAttrs.addAlignmentAttr(Align(alignof(void*)));
return AttributeList::get(C,
AttributeSet::get(C, FnAttrs),
AttributeSet::get(C, RetAttrs),
AttributeSet::get(C, {Attribute::get(C, Attribute::SExt)}));
}
static AttributeList get_attrs_box_zext(LLVMContext &C, unsigned nbytes)
{
auto FnAttrs = AttrBuilder(C);
FnAttrs.addAttribute(Attribute::WillReturn);
FnAttrs.addAttribute(Attribute::NoUnwind);
#if JL_LLVM_VERSION >= 160000
FnAttrs.addMemoryAttr(MemoryEffects::inaccessibleMemOnly());
#else
FnAttrs.addAttribute(Attribute::InaccessibleMemOnly);
#endif
auto RetAttrs = AttrBuilder(C);
RetAttrs.addAttribute(Attribute::NonNull);
RetAttrs.addDereferenceableAttr(nbytes);
RetAttrs.addAlignmentAttr(Align(alignof(void*)));
return AttributeList::get(C,
AttributeSet::get(C, FnAttrs),
AttributeSet::get(C, RetAttrs),
AttributeSet::get(C, {Attribute::get(C, Attribute::ZExt)}));
}
// global vars
static const auto jlRTLD_DEFAULT_var = new JuliaVariable{
XSTR(jl_RTLD_DEFAULT_handle),
true,
[](Type *T_size) -> Type * { return getInt8PtrTy(T_size->getContext()); },
};
static const auto jlexe_var = new JuliaVariable{
XSTR(jl_exe_handle),
true,
[](Type *T_size) -> Type * { return getInt8PtrTy(T_size->getContext()); },
};
static const auto jldll_var = new JuliaVariable{
XSTR(jl_libjulia_handle),
true,
[](Type *T_size) -> Type * { return getInt8PtrTy(T_size->getContext()); },
};
static const auto jldlli_var = new JuliaVariable{
XSTR(jl_libjulia_internal_handle),
true,
[](Type *T_size) -> Type * { return getInt8PtrTy(T_size->getContext()); },
};
static const auto jl_small_typeof_var = new JuliaVariable{
XSTR(jl_small_typeof),
true,
[](Type *T_size) -> Type * { return getInt8Ty(T_size->getContext()); },
};
static const auto jlstack_chk_guard_var = new JuliaVariable{
XSTR(__stack_chk_guard),
true,
[](Type *T_size) -> Type * { return get_pjlvalue(T_size->getContext()); },
};
static const auto jlgetworld_global = new JuliaVariable{
XSTR(jl_world_counter),
false,
[](Type *T_size) -> Type * { return T_size; },
};
static const auto jlboxed_int8_cache = new JuliaVariable{
XSTR(jl_boxed_int8_cache),
true,
[](Type *T_size) -> Type * { return ArrayType::get(get_pjlvalue(T_size->getContext()), 256); },
};
static const auto jlboxed_uint8_cache = new JuliaVariable{
XSTR(jl_boxed_uint8_cache),
true,
[](Type *T_size) -> Type * { return ArrayType::get(get_pjlvalue(T_size->getContext()), 256); },
};
static const auto jlpgcstack_func = new JuliaFunction<>{
"julia.get_pgcstack",
[](LLVMContext &C) { return FunctionType::get(PointerType::get(JuliaType::get_ppjlvalue_ty(C), 0), false); },
nullptr,
};
static const auto jladoptthread_func = new JuliaFunction<>{
"julia.get_pgcstack_or_new",
jlpgcstack_func->_type,
jlpgcstack_func->_attrs,
};
// 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<>{
XSTR(jl_new_structv),
get_func_sig,
get_func_attrs,
};
static const auto jlsplatnew_func = new JuliaFunction<>{
XSTR(jl_new_structt),
[](LLVMContext &C) {
auto T_prjlvalue = JuliaType::get_prjlvalue_ty(C);
return FunctionType::get(T_prjlvalue,
{T_prjlvalue, T_prjlvalue}, false);
},
get_attrs_basic,
};
static const auto jlthrow_func = new JuliaFunction<>{
XSTR(jl_throw),
[](LLVMContext &C) { return FunctionType::get(getVoidTy(C),
{PointerType::get(JuliaType::get_jlvalue_ty(C), AddressSpace::CalleeRooted)}, false); },
get_attrs_noreturn,
};
static const auto jlerror_func = new JuliaFunction<>{
XSTR(jl_error),
[](LLVMContext &C) { return FunctionType::get(getVoidTy(C),
{getInt8PtrTy(C)}, false); },
get_attrs_noreturn,
};
static const auto jlatomicerror_func = new JuliaFunction<>{
XSTR(jl_atomic_error),
[](LLVMContext &C) { return FunctionType::get(getVoidTy(C),
{getInt8PtrTy(C)}, false); },
get_attrs_noreturn,
};
static const auto jltypeerror_func = new JuliaFunction<>{
XSTR(jl_type_error),
[](LLVMContext &C) { return FunctionType::get(getVoidTy(C),
{getInt8PtrTy(C), JuliaType::get_prjlvalue_ty(C), PointerType::get(JuliaType::get_jlvalue_ty(C), AddressSpace::CalleeRooted)}, false); },
get_attrs_noreturn,
};
static const auto jlundefvarerror_func = new JuliaFunction<>{
XSTR(jl_undefined_var_error),
[](LLVMContext &C) {
Type *T = PointerType::get(JuliaType::get_jlvalue_ty(C), AddressSpace::CalleeRooted);
return FunctionType::get(getVoidTy(C), {T, T}, false);
},
get_attrs_noreturn,
};
static const auto jlhasnofield_func = new JuliaFunction<>{
XSTR(jl_has_no_field_error),
[](LLVMContext &C) { return FunctionType::get(getVoidTy(C),
{PointerType::get(JuliaType::get_jlvalue_ty(C), AddressSpace::CalleeRooted),
PointerType::get(JuliaType::get_jlvalue_ty(C), AddressSpace::CalleeRooted)}, false); },
get_attrs_noreturn,
};
static const auto jlboundserrorv_func = new JuliaFunction<TypeFnContextAndSizeT>{
XSTR(jl_bounds_error_ints),
[](LLVMContext &C, Type *T_size) { return FunctionType::get(getVoidTy(C),
{PointerType::get(JuliaType::get_jlvalue_ty(C), AddressSpace::CalleeRooted), T_size->getPointerTo(), T_size}, false); },
get_attrs_noreturn,
};
static const auto jlboundserror_func = new JuliaFunction<TypeFnContextAndSizeT>{
XSTR(jl_bounds_error_int),
[](LLVMContext &C, Type *T_size) { return FunctionType::get(getVoidTy(C),
{PointerType::get(JuliaType::get_jlvalue_ty(C), AddressSpace::CalleeRooted), T_size}, false); },
get_attrs_noreturn,
};
static const auto jlvboundserror_func = new JuliaFunction<TypeFnContextAndSizeT>{
XSTR(jl_bounds_error_tuple_int),
[](LLVMContext &C, Type *T_size) { return FunctionType::get(getVoidTy(C),
{JuliaType::get_pprjlvalue_ty(C), T_size, T_size}, false); },
get_attrs_noreturn,
};
static const auto jluboundserror_func = new JuliaFunction<TypeFnContextAndSizeT>{
XSTR(jl_bounds_error_unboxed_int),
[](LLVMContext &C, Type *T_size) {
return FunctionType::get(getVoidTy(C),
{PointerType::get(getInt8Ty(C), AddressSpace::Derived), JuliaType::get_pjlvalue_ty(C), T_size}, false); },
get_attrs_noreturn,
};
static const auto jlcheckassign_func = new JuliaFunction<>{
XSTR(jl_checked_assignment),
[](LLVMContext &C) {
auto T_pjlvalue = JuliaType::get_pjlvalue_ty(C);
return FunctionType::get(getVoidTy(C),
{T_pjlvalue, T_pjlvalue, T_pjlvalue, PointerType::get(JuliaType::get_jlvalue_ty(C), AddressSpace::CalleeRooted)}, false); },
nullptr,
};
static const auto jlcheckreplace_func = new JuliaFunction<>{
XSTR(jl_checked_replace),
[](LLVMContext &C) {
auto T_pjlvalue = JuliaType::get_pjlvalue_ty(C);
auto T_prjlvalue = JuliaType::get_prjlvalue_ty(C);
return FunctionType::get(T_prjlvalue,
{T_pjlvalue, T_pjlvalue, T_pjlvalue, T_prjlvalue, T_prjlvalue}, false); },
nullptr,
};
static const auto jlcheckmodify_func = new JuliaFunction<>{
XSTR(jl_checked_modify),
[](LLVMContext &C) {
auto T_pjlvalue = JuliaType::get_pjlvalue_ty(C);
auto T_prjlvalue = JuliaType::get_prjlvalue_ty(C);
return FunctionType::get(T_prjlvalue,
{T_pjlvalue, T_pjlvalue, T_pjlvalue, T_prjlvalue, T_prjlvalue}, false); },
nullptr,
};
static const auto jlcheckswap_func = new JuliaFunction<>{
XSTR(jl_checked_swap),
[](LLVMContext &C) {
auto T_pjlvalue = JuliaType::get_pjlvalue_ty(C);
auto T_prjlvalue = JuliaType::get_prjlvalue_ty(C);
return FunctionType::get(T_prjlvalue,
{T_pjlvalue, T_pjlvalue, T_pjlvalue, PointerType::get(JuliaType::get_jlvalue_ty(C), AddressSpace::CalleeRooted)}, false); },
nullptr,
};
static const auto jlcheckassignonce_func = new JuliaFunction<>{
XSTR(jl_checked_assignonce),
[](LLVMContext &C) {
auto T_pjlvalue = JuliaType::get_pjlvalue_ty(C);
auto T_prjlvalue = JuliaType::get_prjlvalue_ty(C);
return FunctionType::get(T_prjlvalue,
{T_pjlvalue, T_pjlvalue, T_pjlvalue, PointerType::get(JuliaType::get_jlvalue_ty(C), AddressSpace::CalleeRooted)}, false); },
nullptr,
};
static const auto jldeclareconst_func = new JuliaFunction<>{
XSTR(jl_declare_constant),
[](LLVMContext &C) {
auto T_pjlvalue = JuliaType::get_pjlvalue_ty(C);
return FunctionType::get(getVoidTy(C),
{T_pjlvalue, T_pjlvalue, T_pjlvalue}, false); },
nullptr,
};
static const auto jlgetbindingorerror_func = new JuliaFunction<>{
XSTR(jl_get_binding_or_error),
[](LLVMContext &C) {
auto T_pjlvalue = JuliaType::get_pjlvalue_ty(C);
return FunctionType::get(T_pjlvalue,
{T_pjlvalue, T_pjlvalue}, false);
},
nullptr,
};
static const auto jlgetbindingwrorerror_func = new JuliaFunction<>{
XSTR(jl_get_binding_wr),
[](LLVMContext &C) {
auto T_pjlvalue = JuliaType::get_pjlvalue_ty(C);
return FunctionType::get(T_pjlvalue,
{T_pjlvalue, T_pjlvalue}, false);
},
nullptr,
};
static const auto jlboundp_func = new JuliaFunction<>{
XSTR(jl_boundp),
[](LLVMContext &C) {
auto T_pjlvalue = JuliaType::get_pjlvalue_ty(C);
return FunctionType::get(getInt32Ty(C),
{T_pjlvalue, T_pjlvalue}, false);
},
nullptr,
};
static const auto jltopeval_func = new JuliaFunction<>{
XSTR(jl_toplevel_eval),
[](LLVMContext &C) {
auto T_pjlvalue = JuliaType::get_pjlvalue_ty(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<>{
XSTR(jl_copy_ast),
[](LLVMContext &C) {
auto T_prjlvalue = JuliaType::get_prjlvalue_ty(C);
return FunctionType::get(T_prjlvalue,
{T_prjlvalue}, false);
},
[](LLVMContext &C) { return AttributeList::get(C,
AttributeSet(),
Attributes(C, {Attribute::NonNull}),
None); },
};
static const auto jlapplygeneric_func = new JuliaFunction<>{
XSTR(jl_apply_generic),
get_func_sig,
get_func_attrs,
};
static const auto jlinvoke_func = new JuliaFunction<>{
XSTR(jl_invoke),
get_func2_sig,
[](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<>{
XSTR(jl_method_def),
[](LLVMContext &C) {
auto T_jlvalue = JuliaType::get_jlvalue_ty(C);
auto T_pjlvalue = PointerType::get(T_jlvalue, 0);
auto T_prjlvalue = PointerType::get(T_jlvalue, AddressSpace::Tracked);
return FunctionType::get(T_prjlvalue,
{T_prjlvalue, T_prjlvalue, T_prjlvalue, T_pjlvalue}, false);
},
nullptr,