Skip to content

Commit be50c8d

Browse files
committed
Merge pull request #13765 from JuliaLang/kf/backportllvm-0.4
Backport some LLVM API changes (+ related functionality) to 0.4
2 parents 2cdef5d + 285166e commit be50c8d

File tree

3 files changed

+91
-41
lines changed

3 files changed

+91
-41
lines changed

deps/Makefile

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -583,36 +583,33 @@ ifneq ($(LLVM_LLDB_TAR),)
583583
mkdir -p llvm-$(LLVM_VER)/tools/lldb && \
584584
$(TAR) -C llvm-$(LLVM_VER)/tools/lldb --strip-components 1 -xf $(LLVM_LLDB_TAR)
585585
endif # LLVM_LLDB_TAR
586-
else
586+
else # LLVM_VER
587587
ifeq ($(BUILD_LLVM_CLANG),1)
588588
([ ! -d llvm-$(LLVM_VER)/tools/clang ] && \
589589
git clone $(LLVM_GIT_URL_CLANG) llvm-$(LLVM_VER)/tools/clang ) || \
590590
(cd llvm-$(LLVM_VER)/tools/clang && \
591591
git pull --ff-only)
592+
([ ! -d llvm-$(LLVM_VER)/projects/compiler-rt ] && \
593+
git clone $(LLVM_GIT_URL_COMPILER_RT) llvm-$(LLVM_VER)/projects/compiler-rt ) || \
594+
(cd llvm-$(LLVM_VER)/projects/compiler-rt && \
595+
git pull --ff-only)
596+
ifneq ($(LLVM_GIT_VER_CLANG),)
597+
(cd llvm-$(LLVM_VER)/tools/clang && \
598+
git checkout $(LLVM_GIT_VER_CLANG))
599+
endif # LLVM_GIT_VER_CLANG
592600
endif # BUILD_LLVM_CLANG
593601
ifeq ($(BUILD_LLDB),1)
594602
([ ! -d llvm-$(LLVM_VER)/tools/lldb ] && \
595603
git clone $(LLVM_GIT_URL_LLDB) llvm-$(LLVM_VER)/tools/lldb ) || \
596604
(cd llvm-$(LLVM_VER)/tools/lldb && \
597605
git pull --ff-only)
606+
ifneq ($(LLVM_GIT_VER_LLDB),)
607+
(cd $(LLVM_VER)/tools/lldb && \
608+
git checkout $(LLVM_GIT_VER_LLDB))
609+
endif # LLVM_GIT_VER_CLANG
598610
endif # BUILD_LLDB
599611
endif # LLVM_VER
600-
ifeq ($(LLVM_VER),svn)
601-
ifeq ($(BUILD_LLVM_CLANG),1)
602-
([ ! -d llvm-$(LLVM_VER)/tools/clang ] && \
603-
git clone $(LLVM_GIT_URL_CLANG) llvm-$(LLVM_VER)/tools/clang ) || \
604-
(cd llvm-$(LLVM_VER)/tools/clang && \
605-
git pull --ff-only)
606-
([ ! -d llvm-$(LLVM_VER)/projects/compiler-rt ] && \
607-
git clone $(LLVM_GIT_URL_COMPILER_RT) llvm-$(LLVM_VER)/projects/compiler-rt ) || \
608-
(cd llvm-$(LLVM_VER)/projects/compiler-rt && \
609-
git pull --ff-only)
610-
ifneq ($(LLVM_CLANG_VER),)
611-
(cd llvm-$(LLVM_VER) && \
612-
git checkout $(LLVM_GIT_VER))
613-
endif # LLVM_CLANG_VER
614-
endif # BUILD_LLVM_CLANG
615-
endif # LLVM_VER
612+
touch -c $@
616613

617614
# Apply version-specific LLVM patches
618615
ifeq ($(LLVM_VER),3.3)

src/cgutils.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,11 @@ static void jl_dump_shadow(char *fname, int jit_model, const char *sysimg_data,
463463
CodeGenOpt::Aggressive // -O3
464464
));
465465

466+
#ifdef LLVM38
467+
legacy::PassManager PM;
468+
#else
466469
PassManager PM;
470+
#endif
467471
if (!dump_as_bc) {
468472
#ifndef LLVM37
469473
PM.add(new TargetLibraryInfo(Triple(TM->getTargetTriple())));

src/codegen.cpp

Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,11 @@ static Module *jl_Module;
174174
#endif
175175
static MDBuilder *mbuilder;
176176
static std::map<int, std::string> argNumberStrings;
177+
#ifdef LLVM38
178+
static legacy::FunctionPassManager *FPM;
179+
#else
177180
static FunctionPassManager *FPM;
181+
#endif
178182

179183
#ifdef LLVM37
180184
// No DataLayout pass needed anymore.
@@ -926,7 +930,7 @@ void jl_extern_c(jl_function_t *f, jl_value_t *rt, jl_value_t *argt, char *name)
926930
if (llvmf) {
927931
#ifndef LLVM35
928932
new GlobalAlias(llvmf->getType(), GlobalValue::ExternalLinkage, name, llvmf, llvmf->getParent());
929-
#elif defined(LLVM37)
933+
#elif defined(LLVM37) && !defined(LLVM38)
930934
GlobalAlias::create(cast<PointerType>(llvmf->getType()),
931935
GlobalValue::ExternalLinkage, name, llvmf, llvmf->getParent());
932936
#else
@@ -4239,7 +4243,9 @@ static Function *emit_function(jl_lambda_info_t *lam)
42394243
continue;
42404244
ditypes.push_back(julia_type_to_di(jl_tparam(lam->specTypes,i),ctx.dbuilder,false));
42414245
}
4242-
#ifdef LLVM36
4246+
#ifdef LLVM38
4247+
subrty = ctx.dbuilder->createSubroutineType(ctx.dbuilder->getOrCreateTypeArray(ditypes));
4248+
#elif defined(LLVM36)
42434249
subrty = ctx.dbuilder->createSubroutineType(topfile,ctx.dbuilder->getOrCreateTypeArray(ditypes));
42444250
#else
42454251
subrty = ctx.dbuilder->createSubroutineType(topfile,ctx.dbuilder->getOrCreateArray(ditypes));
@@ -5025,7 +5031,10 @@ static void init_julia_llvm_env(Module *m)
50255031
// Third argument (length(argv))
50265032
diargs.push_back(julia_type_to_di((jl_value_t*)jl_int32_type,&dbuilder,false));
50275033

5028-
#ifdef LLVM36
5034+
#ifdef LLVM38
5035+
jl_di_func_sig = dbuilder.createSubroutineType(
5036+
dbuilder.getOrCreateTypeArray(diargs));
5037+
#elif defined(LLVM36)
50295038
jl_di_func_sig = dbuilder.createSubroutineType(julia_h,
50305039
dbuilder.getOrCreateTypeArray(diargs));
50315040
#else
@@ -5500,7 +5509,11 @@ static void init_julia_llvm_env(Module *m)
55005509
add_named_global(diff_gc_total_bytes_func, (void*)*jl_gc_diff_total_bytes);
55015510

55025511
// set up optimization passes
5512+
#ifdef LLVM38
5513+
FPM = new legacy::FunctionPassManager(m);
5514+
#else
55035515
FPM = new FunctionPassManager(m);
5516+
#endif
55045517

55055518
#ifdef LLVM37
55065519
// No DataLayout pass needed anymore.
@@ -5527,9 +5540,18 @@ static void init_julia_llvm_env(Module *m)
55275540
#ifndef LLVM37
55285541
jl_TargetMachine->addAnalysisPasses(*FPM);
55295542
#endif
5543+
#ifdef LLVM38
5544+
FPM->add(createTypeBasedAAWrapperPass());
5545+
#else
55305546
FPM->add(createTypeBasedAliasAnalysisPass());
5531-
if (jl_options.opt_level>=1)
5547+
#endif
5548+
if (jl_options.opt_level>=1) {
5549+
#ifdef LLVM38
5550+
FPM->add(createBasicAAWrapperPass());
5551+
#else
55325552
FPM->add(createBasicAliasAnalysisPass());
5553+
#endif
5554+
}
55335555
// list of passes from vmkit
55345556
FPM->add(createCFGSimplificationPass()); // Clean up disgusting code
55355557
FPM->add(createPromoteMemoryToRegisterPass());// Kill useless allocas
@@ -5614,6 +5636,50 @@ static void init_julia_llvm_env(Module *m)
56145636
FPM->doInitialization();
56155637
}
56165638

5639+
// Helper to figure out what features to set for the LLVM target
5640+
// If the user specifies native ( or does not specify ) we default
5641+
// using the API provided by LLVM
5642+
static inline SmallVector<std::string,10> getTargetFeatures() {
5643+
StringMap<bool> HostFeatures;
5644+
if( !strcmp(jl_options.cpu_target,"native") )
5645+
{
5646+
// On earlier versions of LLVM this is empty
5647+
llvm::sys::getHostCPUFeatures(HostFeatures);
5648+
}
5649+
5650+
// Platform specific overides follow
5651+
#if defined(_CPU_X86_64_) || defined(_CPU_X86_)
5652+
#ifndef USE_MCJIT
5653+
// Temporarily disable Haswell BMI2 features due to LLVM bug.
5654+
HostFeatures["bmi2"] = false;
5655+
HostFeatures["avx2"] = false;
5656+
#endif
5657+
#ifdef V128_BUG
5658+
HostFeatures["avx"] = false;
5659+
#endif
5660+
#endif
5661+
5662+
// Figure out if we know the cpu_target
5663+
std::string cpu = strcmp(jl_options.cpu_target,"native") ? jl_options.cpu_target : sys::getHostCPUName();
5664+
if (cpu.empty() || cpu == "generic") {
5665+
jl_printf(JL_STDERR, "WARNING: unable to determine host cpu name.\n");
5666+
#ifdef _CPU_ARM_
5667+
// Check if this is required when you have read the features directly from the processor
5668+
// the processors that don't have VFP are old and (hopefully) rare. this affects the platform calling convention.
5669+
HostFeatures["vfp2"] = true;
5670+
#endif
5671+
}
5672+
5673+
SmallVector<std::string,10> attr;
5674+
for( StringMap<bool>::const_iterator it = HostFeatures.begin(); it != HostFeatures.end(); it++ )
5675+
{
5676+
std::string att = it->getValue() ? it->getKey().str() :
5677+
std::string("-") + it->getKey().str();
5678+
attr.append( 1, att );
5679+
}
5680+
return attr;
5681+
}
5682+
56175683
extern "C" void jl_init_codegen(void)
56185684
{
56195685
#if defined(_OS_WINDOWS_) && defined(_CPU_X86_64_)
@@ -5679,19 +5745,7 @@ extern "C" void jl_init_codegen(void)
56795745
#ifdef USE_MCJIT
56805746
jl_mcjmm = new SectionMemoryManager();
56815747
#endif
5682-
const char *mattr[] = {
5683-
#if defined(_CPU_X86_64_) || defined(_CPU_X86_)
5684-
#ifndef USE_MCJIT
5685-
// Temporarily disable Haswell BMI2 features due to LLVM bug.
5686-
"-bmi2", "-avx2",
5687-
#endif
5688-
#ifdef V128_BUG
5689-
"-avx",
5690-
#endif
5691-
#endif
5692-
"", // padding to make sure this isn't an empty array (ref #11817)
5693-
};
5694-
SmallVector<std::string, 4> MAttrs(mattr, mattr+sizeof(mattr)/sizeof(mattr[0]));
5748+
56955749
#ifdef LLVM36
56965750
EngineBuilder eb(std::move(std::unique_ptr<Module>(engine_module)));
56975751
#else
@@ -5725,17 +5779,12 @@ extern "C" void jl_init_codegen(void)
57255779
#endif
57265780
#endif
57275781
std::string TheCPU = strcmp(jl_options.cpu_target,"native") ? jl_options.cpu_target : sys::getHostCPUName();
5728-
if (TheCPU.empty() || TheCPU == "generic") {
5729-
jl_printf(JL_STDERR, "WARNING: unable to determine host cpu name.\n");
5730-
#ifdef _CPU_ARM_
5731-
MAttrs.append(1, "+vfp2"); // the processors that don't have VFP are old and (hopefully) rare. this affects the platform calling convention.
5732-
#endif
5733-
}
5782+
SmallVector<std::string, 10> targetFeatures = getTargetFeatures( );
57345783
TargetMachine *targetMachine = eb.selectTarget(
57355784
TheTriple,
57365785
"",
57375786
TheCPU,
5738-
MAttrs);
5787+
targetFeatures);
57395788
assert(targetMachine && "Failed to select target machine -"
57405789
" Is the LLVM backend for this CPU enabled?");
57415790
jl_TargetMachine = targetMachine->getTarget().createTargetMachine(

0 commit comments

Comments
 (0)