Skip to content

Commit 52aec60

Browse files
committed
Import of diff from JuliaGPU/julia.
1 parent 5632cba commit 52aec60

10 files changed

+224
-31
lines changed

base/reflection.jl

+17-4
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,15 @@ function uncompressed_ast(m::Method, s::CodeInfo)
493493
return s
494494
end
495495

496+
# this type mirrors jl_cghooks_t (documented in julia.h)
497+
immutable CodegenHooks
498+
module_setup::Any
499+
module_activation::Any
500+
501+
CodegenHooks(;module_setup=nothing, module_activation=nothing) =
502+
new(module_setup, module_activation)
503+
end
504+
496505
# this type mirrors jl_cgparams_t (documented in julia.h)
497506
immutable CodegenParams
498507
cached::Cint
@@ -504,14 +513,18 @@ immutable CodegenParams
504513
static_alloc::Cint
505514
dynamic_alloc::Cint
506515

516+
hooks::CodegenHooks
517+
507518
CodegenParams(;cached::Bool=true,
508519
runtime::Bool=true, exceptions::Bool=true,
509520
track_allocations::Bool=true, code_coverage::Bool=true,
510-
static_alloc::Bool=true, dynamic_alloc::Bool=true) =
521+
static_alloc::Bool=true, dynamic_alloc::Bool=true,
522+
hooks::CodegenHooks=CodegenHooks()) =
511523
new(Cint(cached),
512524
Cint(runtime), Cint(exceptions),
513525
Cint(track_allocations), Cint(code_coverage),
514-
Cint(static_alloc), Cint(dynamic_alloc))
526+
Cint(static_alloc), Cint(dynamic_alloc),
527+
hooks)
515528
end
516529

517530
# Printing code representations in IR and assembly
@@ -542,9 +555,9 @@ function _dump_function(linfo::Core.MethodInstance, native::Bool, wrapper::Bool,
542555
throw(ArgumentError("'syntax' must be either :intel or :att"))
543556
end
544557
if native
545-
llvmf = ccall(:jl_get_llvmf_decl, Ptr{Void}, (Any, Bool, CodegenParams), linfo, wrapper, params)
558+
llvmf = ccall(:jl_get_llvmf_decl, Ptr{Void}, (Any, Bool, CodegenParams, CodegenHooks), linfo, wrapper, params, params.hooks)
546559
else
547-
llvmf = ccall(:jl_get_llvmf_defn, Ptr{Void}, (Any, Bool, Bool, CodegenParams), linfo, wrapper, optimize, params)
560+
llvmf = ccall(:jl_get_llvmf_defn, Ptr{Void}, (Any, Bool, Bool, CodegenParams, CodegenHooks), linfo, wrapper, optimize, params, params.hooks)
548561
end
549562
if llvmf == C_NULL
550563
error("could not compile the specified method")

deps/Versions.make

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
LLVM_VER = 3.7.1
1+
LLVM_VER = 3.9.0
22
LLVM_LIB_SUFFIX =
33
PCRE_VER = 10.22
44
DSFMT_VER = 2.2.3

deps/llvm.mk

+13-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ LLVM_CFLAGS += $(CFLAGS)
7878
LLVM_CXXFLAGS += $(CXXFLAGS)
7979
LLVM_CPPFLAGS += $(CPPFLAGS)
8080
LLVM_LDFLAGS += $(LDFLAGS)
81-
LLVM_TARGETS := host
81+
ifeq ($(LLVM_USE_CMAKE),1)
82+
LLVM_TARGETS := host;NVPTX
83+
else
84+
LLVM_TARGETS := host,nvptx
85+
endif
8286
LLVM_TARGET_FLAGS := --enable-targets=$(LLVM_TARGETS)
8387
LLVM_CMAKE += -DLLVM_TARGETS_TO_BUILD:STRING="$(LLVM_TARGETS)" -DCMAKE_BUILD_TYPE="$(LLVM_CMAKE_BUILDTYPE)"
8488
LLVM_CMAKE += -DLLVM_TOOLS_INSTALL_DIR=$(shell $(JULIAHOME)/contrib/relative_path.sh $(build_prefix) $(build_depsbindir))
@@ -480,6 +484,14 @@ $(eval $(call LLVM_PATCH,llvm-arm-fix-prel31))
480484
$(eval $(call LLVM_PATCH,llvm-D25865-cmakeshlib))
481485
endif # LLVM_VER
482486

487+
$(eval $(call LLVM_PATCH,llvm-asan))
488+
ifeq ($(LLVM_VER_SHORT),3.9)
489+
$(eval $(call LLVM_PATCH,llvm-D9168_argument_alignment))
490+
endif
491+
ifeq ($(BUILD_LLVM_CLANG),1)
492+
$(eval $(call LLVM_PATCH,compiler-rt-asan))
493+
endif
494+
483495
ifeq ($(LLVM_VER),3.7.1)
484496
ifeq ($(BUILD_LLDB),1)
485497
$(eval $(call LLVM_PATCH,lldb-3.7.1))

deps/patches/compiler-rt-asan.patch

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Index: lib/asan/asan_mapping.h
2+
===================================================================
3+
--- a/projects/compiler-rt/lib/asan/asan_mapping.h (revision 256758)
4+
+++ b/projects/compiler-rt/lib/asan/asan_mapping.h (working copy)
5+
@@ -146,7 +146,7 @@
6+
# elif SANITIZER_IOS
7+
# define SHADOW_OFFSET kIosShadowOffset64
8+
# else
9+
-# define SHADOW_OFFSET kDefaultShort64bitShadowOffset
10+
+# define SHADOW_OFFSET kDefaultShadowOffset64
11+
# endif
12+
# endif
13+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
Index: lib/Target/NVPTX/NVPTXISelLowering.h
2+
===================================================================
3+
--- a/lib/Target/NVPTX/NVPTXISelLowering.h
4+
+++ b/lib/Target/NVPTX/NVPTXISelLowering.h
5+
@@ -539,7 +539,8 @@
6+
SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override;
7+
8+
unsigned getArgumentAlignment(SDValue Callee, const ImmutableCallSite *CS,
9+
- Type *Ty, unsigned Idx) const;
10+
+ Type *Ty, unsigned Idx,
11+
+ const DataLayout &DL) const;
12+
};
13+
} // namespace llvm
14+
15+
Index: lib/Target/NVPTX/NVPTXISelLowering.cpp
16+
===================================================================
17+
--- a/lib/Target/NVPTX/NVPTXISelLowering.cpp
18+
+++ b/lib/Target/NVPTX/NVPTXISelLowering.cpp
19+
@@ -1024,11 +1024,15 @@
20+
return O.str();
21+
}
22+
23+
-unsigned
24+
-NVPTXTargetLowering::getArgumentAlignment(SDValue Callee,
25+
- const ImmutableCallSite *CS,
26+
- Type *Ty,
27+
- unsigned Idx) const {
28+
+unsigned NVPTXTargetLowering::getArgumentAlignment(SDValue Callee,
29+
+ const ImmutableCallSite *CS,
30+
+ Type *Ty, unsigned Idx,
31+
+ const DataLayout &DL) const {
32+
+ if (!CS) {
33+
+ // CallSite is zero, fallback to ABI type alignment
34+
+ return DL.getABITypeAlignment(Ty);
35+
+ }
36+
+
37+
unsigned Align = 0;
38+
const Value *DirectCallee = CS->getCalledFunction();
39+
40+
@@ -1046,7 +1050,7 @@
41+
42+
const Value *CalleeV = cast<CallInst>(CalleeI)->getCalledValue();
43+
// Ignore any bitcast instructions
44+
- while(isa<ConstantExpr>(CalleeV)) {
45+
+ while (isa<ConstantExpr>(CalleeV)) {
46+
const ConstantExpr *CE = cast<ConstantExpr>(CalleeV);
47+
if (!CE->isCast())
48+
break;
49+
@@ -1069,7 +1073,6 @@
50+
51+
// Call is indirect or alignment information is not available, fall back to
52+
// the ABI type alignment
53+
- auto &DL = CS->getCaller()->getParent()->getDataLayout();
54+
return DL.getABITypeAlignment(Ty);
55+
}
56+
57+
@@ -1126,7 +1129,8 @@
58+
ComputePTXValueVTs(*this, DAG.getDataLayout(), Ty, vtparts, &Offsets,
59+
0);
60+
61+
- unsigned align = getArgumentAlignment(Callee, CS, Ty, paramCount + 1);
62+
+ unsigned align =
63+
+ getArgumentAlignment(Callee, CS, Ty, paramCount + 1, DL);
64+
// declare .param .align <align> .b8 .param<n>[<size>];
65+
unsigned sz = DL.getTypeAllocSize(Ty);
66+
SDVTList DeclareParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
67+
@@ -1166,7 +1170,8 @@
68+
}
69+
if (Ty->isVectorTy()) {
70+
EVT ObjectVT = getValueType(DL, Ty);
71+
- unsigned align = getArgumentAlignment(Callee, CS, Ty, paramCount + 1);
72+
+ unsigned align =
73+
+ getArgumentAlignment(Callee, CS, Ty, paramCount + 1, DL);
74+
// declare .param .align <align> .b8 .param<n>[<size>];
75+
unsigned sz = DL.getTypeAllocSize(Ty);
76+
SDVTList DeclareParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
77+
@@ -1426,7 +1431,7 @@
78+
DeclareRetOps);
79+
InFlag = Chain.getValue(1);
80+
} else {
81+
- retAlignment = getArgumentAlignment(Callee, CS, retTy, 0);
82+
+ retAlignment = getArgumentAlignment(Callee, CS, retTy, 0, DL);
83+
SDVTList DeclareRetVTs = DAG.getVTList(MVT::Other, MVT::Glue);
84+
SDValue DeclareRetOps[] = { Chain,
85+
DAG.getConstant(retAlignment, dl, MVT::i32),
86+
@@ -1633,9 +1638,10 @@
87+
} else {
88+
SmallVector<EVT, 16> VTs;
89+
SmallVector<uint64_t, 16> Offsets;
90+
- ComputePTXValueVTs(*this, DAG.getDataLayout(), retTy, VTs, &Offsets, 0);
91+
+ auto &DL = DAG.getDataLayout();
92+
+ ComputePTXValueVTs(*this, DL, retTy, VTs, &Offsets, 0);
93+
assert(VTs.size() == Ins.size() && "Bad value decomposition");
94+
- unsigned RetAlign = getArgumentAlignment(Callee, CS, retTy, 0);
95+
+ unsigned RetAlign = getArgumentAlignment(Callee, CS, retTy, 0, DL);
96+
for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
97+
unsigned sz = VTs[i].getSizeInBits();
98+
unsigned AlignI = GreatestCommonDivisor64(RetAlign, Offsets[i]);

deps/patches/llvm-asan.patch

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
diff --git a/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/lib/Transforms/Instrumentation/AddressSanitizer.cpp
2+
index 43d1b37..3efe3d6 100644
3+
--- a/lib/Transforms/Instrumentation/AddressSanitizer.cpp
4+
+++ b/lib/Transforms/Instrumentation/AddressSanitizer.cpp
5+
@@ -404,7 +404,7 @@ static ShadowMapping getShadowMapping(Triple &TargetTriple, int LongSize,
6+
if (IsKasan)
7+
Mapping.Offset = kLinuxKasan_ShadowOffset64;
8+
else
9+
- Mapping.Offset = kSmallX86_64ShadowOffset;
10+
+ Mapping.Offset = kDefaultShadowOffset64;
11+
} else if (IsWindows && IsX86_64) {
12+
Mapping.Offset = kWindowsShadowOffset64;
13+
} else if (IsMIPS64)

src/cgutils.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ static Value *prepare_call(Value *Callee)
3939
((ctx)->params->feature)
4040

4141

42+
// --- hook checks ---
43+
44+
#define JL_HOOK_TEST(params,hook) \
45+
((params)->hooks.hook != jl_nothing)
46+
47+
// NOTE: this is just a throwing version of jl_call1...
48+
#define JL_HOOK_CALL1(params,hook,arg1) \
49+
jl_value_t **argv; \
50+
JL_GC_PUSHARGS(argv, 2); \
51+
argv[0] = (params)->hooks.hook; \
52+
argv[1] = arg1; \
53+
jl_apply(argv, 2); \
54+
JL_GC_POP();
55+
56+
4257
// --- string constants ---
4358
static StringMap<GlobalVariable*> stringConstants;
4459
static Value *stringConstPtr(IRBuilder<> &builder, const std::string &txt)

src/codegen.cpp

+36-24
Original file line numberDiff line numberDiff line change
@@ -923,25 +923,30 @@ jl_llvm_functions_t jl_compile_linfo(jl_method_instance_t *li, jl_code_info_t *s
923923
f = (Function*)decls.functionObject;
924924
specf = (Function*)decls.specFunctionObject;
925925

926-
// Step 4. Prepare debug info to receive this function
927-
// record that this function name came from this linfo,
928-
// so we can build a reverse mapping for debug-info.
929-
bool toplevel = li->def == NULL;
930-
if (!toplevel) {
931-
const DataLayout &DL =
932-
#if JL_LLVM_VERSION >= 30500
933-
m->getDataLayout();
934-
#else
935-
*jl_data_layout;
936-
#endif
937-
// but don't remember toplevel thunks because
938-
// they may not be rooted in the gc for the life of the program,
939-
// and the runtime doesn't notify us when the code becomes unreachable :(
940-
jl_add_linfo_in_flight((specf ? specf : f)->getName(), li, DL);
941-
}
942926

943-
// Step 5. Add the result to the execution engine now
944-
jl_finalize_module(m.release(), !toplevel);
927+
if (JL_HOOK_TEST(params, module_activation)) {
928+
JL_HOOK_CALL1(params, module_activation, jl_box_voidpointer(wrap(m.release())));
929+
} else {
930+
// Step 4. Prepare debug info to receive this function
931+
// record that this function name came from this linfo,
932+
// so we can build a reverse mapping for debug-info.
933+
bool toplevel = li->def == NULL;
934+
if (!toplevel) {
935+
const DataLayout &DL =
936+
#if JL_LLVM_VERSION >= 30500
937+
m->getDataLayout();
938+
#else
939+
*jl_data_layout;
940+
#endif
941+
// but don't remember toplevel thunks because
942+
// they may not be rooted in the gc for the life of the program,
943+
// and the runtime doesn't notify us when the code becomes unreachable :(
944+
jl_add_linfo_in_flight((specf ? specf : f)->getName(), li, DL);
945+
}
946+
947+
// Step 5. Add the result to the execution engine now
948+
jl_finalize_module(m.release(), !toplevel);
949+
}
945950

946951
if (li->jlcall_api != 2) {
947952
// if not inlineable, code won't be needed again
@@ -988,8 +993,13 @@ static Value *getModuleFlag(Module *m, StringRef Key)
988993
#define getModuleFlag(m,str) m->getModuleFlag(str)
989994
#endif
990995

991-
static void jl_setup_module(Module *m)
996+
static void jl_setup_module(Module *m, jl_cgparams_t *params = &jl_default_cgparams)
992997
{
998+
if (JL_HOOK_TEST(params, module_setup)) {
999+
JL_HOOK_CALL1(params, module_setup, jl_box_voidpointer(wrap(m)));
1000+
return;
1001+
}
1002+
9931003
// Some linkers (*cough* OS X) don't understand DWARF v4, so we use v2 in
9941004
// imaging mode. The structure of v4 is slightly nicer for debugging JIT
9951005
// code.
@@ -1256,8 +1266,9 @@ void jl_extern_c(jl_function_t *f, jl_value_t *rt, jl_value_t *argt, char *name)
12561266
// this is paired with jl_dump_function_ir and jl_dump_function_asm in particular ways:
12571267
// misuse will leak memory or cause read-after-free
12581268
extern "C" JL_DLLEXPORT
1259-
void *jl_get_llvmf_defn(jl_method_instance_t *linfo, bool getwrapper, bool optimize, jl_cgparams_t params)
1269+
void *jl_get_llvmf_defn(jl_method_instance_t *linfo, bool getwrapper, bool optimize, jl_cgparams_t params, jl_cghooks_t _hooks)
12601270
{
1271+
params.hooks = _hooks; // ccall doesn't know how to pass the nested jl_cghooks_t
12611272
// `source` is `NULL` for generated functions.
12621273
// The `isstaged` check can be removed if that is not the case anymore.
12631274
if (linfo->def && linfo->def->source == NULL && !linfo->def->isstaged) {
@@ -1338,8 +1349,9 @@ void *jl_get_llvmf_defn(jl_method_instance_t *linfo, bool getwrapper, bool optim
13381349

13391350

13401351
extern "C" JL_DLLEXPORT
1341-
void *jl_get_llvmf_decl(jl_method_instance_t *linfo, bool getwrapper, jl_cgparams_t params)
1352+
void *jl_get_llvmf_decl(jl_method_instance_t *linfo, bool getwrapper, jl_cgparams_t params, jl_cghooks_t _hooks)
13421353
{
1354+
params.hooks = _hooks; // ccall doesn't know how to pass the nested jl_cghooks_t
13431355
// `source` is `NULL` for generated functions.
13441356
// The `isstaged` check can be removed if that is not the case anymore.
13451357
if (linfo->def && linfo->def->source == NULL && !linfo->def->isstaged) {
@@ -1397,9 +1409,9 @@ void *jl_get_llvmf(jl_tupletype_t *tt, bool getwrapper, bool getdeclarations)
13971409
}
13981410
void *f;
13991411
if (getdeclarations)
1400-
f = jl_get_llvmf_decl(linfo, getwrapper, jl_default_cgparams);
1412+
f = jl_get_llvmf_decl(linfo, getwrapper, jl_default_cgparams, jl_no_cghooks);
14011413
else
1402-
f = jl_get_llvmf_defn(linfo, getwrapper, true, jl_default_cgparams);
1414+
f = jl_get_llvmf_defn(linfo, getwrapper, true, jl_default_cgparams, jl_no_cghooks);
14031415
JL_GC_POP();
14041416
return f;
14051417
}
@@ -4207,7 +4219,7 @@ static std::unique_ptr<Module> emit_function(jl_method_instance_t *lam, jl_code_
42074219

42084220
ctx.sret = false;
42094221
Module *M = new Module(ctx.name, jl_LLVMContext);
4210-
jl_setup_module(M);
4222+
jl_setup_module(M, params);
42114223
if (specsig) { // assumes !va and !needsparams
42124224
std::vector<Type*> fsig(0);
42134225
Type *rt;

src/jltypes.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ JL_DLLEXPORT jl_value_t *jl_emptytuple=NULL;
6868
jl_svec_t *jl_emptysvec;
6969
jl_value_t *jl_nothing;
7070

71+
jl_cghooks_t jl_no_cghooks;
7172
jl_cgparams_t jl_default_cgparams;
7273

7374
// --- type properties and predicates ---
@@ -3531,7 +3532,8 @@ void jl_init_types(void)
35313532
jl_type_type_mt = jl_new_method_table(jl_type_type->name->name, ptls->current_module);
35323533
jl_type_type->name->mt = jl_type_type_mt;
35333534

3534-
jl_default_cgparams = (jl_cgparams_t){1, 1, 1, 1, 1, 1, 1};
3535+
jl_no_cghooks = (jl_cghooks_t){jl_nothing, jl_nothing};
3536+
jl_default_cgparams = (jl_cgparams_t){1, 1, 1, 1, 1, 1, 1, jl_no_cghooks};
35353537

35363538
// initialize them. lots of cycles.
35373539
jl_datatype_type->name = jl_new_typename(jl_symbol("DataType"));

src/julia.h

+15
Original file line numberDiff line numberDiff line change
@@ -1757,6 +1757,19 @@ typedef struct {
17571757

17581758
// codegen interface ----------------------------------------------------------
17591759

1760+
typedef struct {
1761+
// module setup: prepare a module for code emission (data layout, DWARF version, ...)
1762+
// parameters: LLVMModuleRef as Ptr{Void}
1763+
// return value: none
1764+
jl_value_t *module_setup;
1765+
1766+
// module activation: registers debug info, adds module to JIT
1767+
// parameters: LLVMModuleRef as Ptr{Void}
1768+
// return value: none
1769+
jl_value_t *module_activation;
1770+
} jl_cghooks_t;
1771+
extern JL_DLLEXPORT jl_cghooks_t jl_no_cghooks;
1772+
17601773
typedef struct {
17611774
int cached; // can the compiler consult/mutate the compilation cache?
17621775

@@ -1767,6 +1780,8 @@ typedef struct {
17671780
int code_coverage; // can we measure coverage (don't if disallowed)?
17681781
int static_alloc; // is the compiler allowed to allocate statically?
17691782
int dynamic_alloc; // is the compiler allowed to allocate dynamically (requires runtime)?
1783+
1784+
jl_cghooks_t hooks;
17701785
} jl_cgparams_t;
17711786
extern JL_DLLEXPORT jl_cgparams_t jl_default_cgparams;
17721787

0 commit comments

Comments
 (0)