LLVMExecutionEngineRef mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, FunctionEmittedCb *emitted_cb, ExceptionTableCb *exception_cb) { std::string Error; force_pass_linking (); LLVMInitializeX86Target (); LLVMInitializeX86TargetInfo (); llvm::cl::ParseEnvironmentOptions("mono", "MONO_LLVM", "", false); mono_mm = new MonoJITMemoryManager (); mono_mm->alloc_cb = alloc_cb; #if LLVM_MAJOR_VERSION == 2 && LLVM_MINOR_VERSION < 8 DwarfExceptionHandling = true; #else JITExceptionHandling = true; #endif // PrettyStackTrace installs signal handlers which trip up libgc DisablePrettyStackTrace = true; ExecutionEngine *EE = ExecutionEngine::createJIT (unwrap (MP), &Error, mono_mm, CodeGenOpt::Default); if (!EE) { errs () << "Unable to create LLVM ExecutionEngine: " << Error << "\n"; g_assert_not_reached (); } EE->InstallExceptionTableRegister (exception_cb); EE->RegisterJITEventListener (new MonoJITEventListener (emitted_cb)); fpm = new FunctionPassManager (unwrap (MP)); fpm->add(new TargetData(*EE->getTargetData())); /* Add a default set of passes */ //createStandardFunctionPasses (fpm, 2); fpm->add(createInstructionCombiningPass()); fpm->add(createReassociatePass()); fpm->add(createGVNPass()); fpm->add(createCFGSimplificationPass()); /* The one used by opt is: * -simplifycfg -domtree -domfrontier -scalarrepl -instcombine -simplifycfg -basiccg -domtree -domfrontier -scalarrepl -simplify-libcalls -instcombine -simplifycfg -instcombine -simplifycfg -reassociate -domtree -loops -loopsimplify -domfrontier -loopsimplify -lcssa -loop-rotate -licm -lcssa -loop-unswitch -instcombine -scalar-evolution -loopsimplify -lcssa -iv-users -indvars -loop-deletion -loopsimplify -lcssa -loop-unroll -instcombine -memdep -gvn -memdep -memcpyopt -sccp -instcombine -domtree -memdep -dse -adce -gvn -simplifycfg -preverify -domtree -verify */ /* Add passes specified by the env variable */ /* Only the passes in force_pass_linking () can be used */ for (unsigned i = 0; i < PassList.size(); ++i) { const PassInfo *PassInf = PassList[i]; Pass *P = 0; if (PassInf->getNormalCtor()) P = PassInf->getNormalCtor()(); fpm->add (P); } return wrap(EE); }
LLVMExecutionEngineRef mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, FunctionEmittedCb *emitted_cb, ExceptionTableCb *exception_cb) { std::string Error; LLVMInitializeX86Target (); LLVMInitializeX86TargetInfo (); llvm::cl::ParseEnvironmentOptions("mono", "MONO_LLVM", "", false); mono_mm = new MonoJITMemoryManager (); mono_mm->alloc_cb = alloc_cb; mono_mm->emitted_cb = emitted_cb; DwarfExceptionHandling = true; // PrettyStackTrace installs signal handlers which trip up libgc DisablePrettyStackTrace = true; ExecutionEngine *EE = ExecutionEngine::createJIT (unwrap (MP), &Error, mono_mm, CodeGenOpt::Default); if (!EE) { errs () << "Unable to create LLVM ExecutionEngine: " << Error << "\n"; g_assert_not_reached (); } EE->InstallExceptionTableRegister (exception_cb); EE->RegisterJITEventListener (new MonoJITEventListener ()); fpm = new FunctionPassManager (unwrap (MP)); fpm->add(new TargetData(*EE->getTargetData())); /* Add a random set of passes */ /* Make this run-time configurable */ fpm->add(createInstructionCombiningPass()); fpm->add(createReassociatePass()); fpm->add(createGVNPass()); fpm->add(createCFGSimplificationPass()); /* Add passes specified by the env variable */ /* FIXME: This can only add passes which are linked in, thus are already used */ for (unsigned i = 0; i < PassList.size(); ++i) { const PassInfo *PassInf = PassList[i]; Pass *P = 0; if (PassInf->getNormalCtor()) P = PassInf->getNormalCtor()(); if (dynamic_cast<MachineFunctionPass*>(P) != 0) { errs () << PassInf->getPassName () << " is a machine function pass.\n"; } else { fpm->add (P); } } return wrap(EE); }
LLVMExecutionEngineRef mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, FunctionEmittedCb *emitted_cb, ExceptionTableCb *exception_cb, DlSymCb *dlsym_cb) { std::string Error; force_pass_linking (); #ifdef TARGET_ARM LLVMInitializeARMTarget (); LLVMInitializeARMTargetInfo (); LLVMInitializeARMTargetMC (); #else LLVMInitializeX86Target (); LLVMInitializeX86TargetInfo (); LLVMInitializeX86TargetMC (); #endif mono_mm = new MonoJITMemoryManager (); mono_mm->alloc_cb = alloc_cb; mono_mm->dlsym_cb = dlsym_cb; //JITExceptionHandling = true; // PrettyStackTrace installs signal handlers which trip up libgc DisablePrettyStackTrace = true; /* * The Default code model doesn't seem to work on amd64, * test_0_fields_with_big_offsets (among others) crashes, because LLVM tries to call * memset using a normal pcrel code which is in 32bit memory, while memset isn't. */ TargetOptions opts; opts.JITExceptionHandling = 1; EngineBuilder b (unwrap (MP)); #ifdef TARGET_AMD64 ExecutionEngine *EE = b.setJITMemoryManager (mono_mm).setTargetOptions (opts).setCodeModel (CodeModel::Large).setAllocateGVsWithCode (true).create (); #else ExecutionEngine *EE = b.setJITMemoryManager (mono_mm).setTargetOptions (opts).setAllocateGVsWithCode (true).create (); #endif g_assert (EE); #if 0 ExecutionEngine *EE = ExecutionEngine::createJIT (unwrap (MP), &Error, mono_mm, CodeGenOpt::Default, true, Reloc::Default, CodeModel::Large); if (!EE) { errs () << "Unable to create LLVM ExecutionEngine: " << Error << "\n"; g_assert_not_reached (); } #endif EE->InstallExceptionTableRegister (exception_cb); mono_event_listener = new MonoJITEventListener (emitted_cb); EE->RegisterJITEventListener (mono_event_listener); fpm = new FunctionPassManager (unwrap (MP)); fpm->add(new DataLayout(*EE->getDataLayout())); PassRegistry &Registry = *PassRegistry::getPassRegistry(); initializeCore(Registry); initializeScalarOpts(Registry); //initializeIPO(Registry); initializeAnalysis(Registry); initializeIPA(Registry); initializeTransformUtils(Registry); initializeInstCombine(Registry); //initializeInstrumentation(Registry); initializeTarget(Registry); llvm::cl::ParseEnvironmentOptions("mono", "MONO_LLVM", ""); if (PassList.size() > 0) { /* Use the passes specified by the env variable */ /* Only the passes in force_pass_linking () can be used */ for (unsigned i = 0; i < PassList.size(); ++i) { const PassInfo *PassInf = PassList[i]; Pass *P = 0; if (PassInf->getNormalCtor()) P = PassInf->getNormalCtor()(); fpm->add (P); } } else { /* Use the same passes used by 'opt' by default, without the ipo passes */ const char *opts = "-simplifycfg -domtree -domfrontier -scalarrepl -instcombine -simplifycfg -domtree -domfrontier -scalarrepl -simplify-libcalls -instcombine -simplifycfg -instcombine -simplifycfg -reassociate -domtree -loops -loop-simplify -domfrontier -loop-simplify -lcssa -loop-rotate -licm -lcssa -loop-unswitch -instcombine -scalar-evolution -loop-simplify -lcssa -iv-users -indvars -loop-deletion -loop-simplify -lcssa -loop-unroll -instcombine -memdep -gvn -memdep -memcpyopt -sccp -instcombine -domtree -memdep -dse -adce -gvn -simplifycfg -preverify -domtree -verify"; char **args; int i; args = g_strsplit (opts, " ", 1000); for (i = 0; args [i]; i++) ; llvm::cl::ParseCommandLineOptions (i, args, ""); g_strfreev (args); for (unsigned i = 0; i < PassList.size(); ++i) { const PassInfo *PassInf = PassList[i]; Pass *P = 0; if (PassInf->getNormalCtor()) P = PassInf->getNormalCtor()(); g_assert (P->getPassKind () == llvm::PT_Function || P->getPassKind () == llvm::PT_Loop); fpm->add (P); } /* fpm->add(createInstructionCombiningPass()); fpm->add(createReassociatePass()); fpm->add(createGVNPass()); fpm->add(createCFGSimplificationPass()); */ } return wrap(EE); }
LLVMBool lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef *OutJIT, lp_generated_code **OutCode, LLVMModuleRef M, LLVMMCJITMemoryManagerRef CMM, unsigned OptLevel, int useMCJIT, char **OutError) { using namespace llvm; std::string Error; #if HAVE_LLVM >= 0x0306 EngineBuilder builder(std::unique_ptr<Module>(unwrap(M))); #else EngineBuilder builder(unwrap(M)); #endif /** * LLVM 3.1+ haven't more "extern unsigned llvm::StackAlignmentOverride" and * friends for configuring code generation options, like stack alignment. */ TargetOptions options; #if defined(PIPE_ARCH_X86) options.StackAlignmentOverride = 4; #if HAVE_LLVM < 0x0304 options.RealignStack = true; #endif #endif #if defined(DEBUG) && HAVE_LLVM < 0x0307 options.JITEmitDebugInfo = true; #endif /* XXX: Workaround http://llvm.org/PR21435 */ #if defined(DEBUG) || defined(PROFILE) || \ (HAVE_LLVM >= 0x0303 && (defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64))) #if HAVE_LLVM < 0x0304 options.NoFramePointerElimNonLeaf = true; #endif #if HAVE_LLVM < 0x0307 options.NoFramePointerElim = true; #endif #endif builder.setEngineKind(EngineKind::JIT) .setErrorStr(&Error) .setTargetOptions(options) .setOptLevel((CodeGenOpt::Level)OptLevel); if (useMCJIT) { #if HAVE_LLVM < 0x0306 builder.setUseMCJIT(true); #endif #ifdef _WIN32 /* * MCJIT works on Windows, but currently only through ELF object format. * * XXX: We could use `LLVM_HOST_TRIPLE "-elf"` but LLVM_HOST_TRIPLE has * different strings for MinGW/MSVC, so better play it safe and be * explicit. */ # ifdef _WIN64 LLVMSetTarget(M, "x86_64-pc-win32-elf"); # else LLVMSetTarget(M, "i686-pc-win32-elf"); # endif #endif } llvm::SmallVector<std::string, 16> MAttrs; #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64) #if HAVE_LLVM >= 0x0400 /* llvm-3.7+ implements sys::getHostCPUFeatures for x86, * which allows us to enable/disable code generation based * on the results of cpuid. */ llvm::StringMap<bool> features; llvm::sys::getHostCPUFeatures(features); for (StringMapIterator<bool> f = features.begin(); f != features.end(); ++f) { MAttrs.push_back(((*f).second ? "+" : "-") + (*f).first().str()); } #else /* * We need to unset attributes because sometimes LLVM mistakenly assumes * certain features are present given the processor name. * * https://bugs.freedesktop.org/show_bug.cgi?id=92214 * http://llvm.org/PR25021 * http://llvm.org/PR19429 * http://llvm.org/PR16721 */ MAttrs.push_back(util_cpu_caps.has_sse ? "+sse" : "-sse" ); MAttrs.push_back(util_cpu_caps.has_sse2 ? "+sse2" : "-sse2" ); MAttrs.push_back(util_cpu_caps.has_sse3 ? "+sse3" : "-sse3" ); MAttrs.push_back(util_cpu_caps.has_ssse3 ? "+ssse3" : "-ssse3" ); #if HAVE_LLVM >= 0x0304 MAttrs.push_back(util_cpu_caps.has_sse4_1 ? "+sse4.1" : "-sse4.1"); #else MAttrs.push_back(util_cpu_caps.has_sse4_1 ? "+sse41" : "-sse41" ); #endif #if HAVE_LLVM >= 0x0304 MAttrs.push_back(util_cpu_caps.has_sse4_2 ? "+sse4.2" : "-sse4.2"); #else MAttrs.push_back(util_cpu_caps.has_sse4_2 ? "+sse42" : "-sse42" ); #endif /* * AVX feature is not automatically detected from CPUID by the X86 target * yet, because the old (yet default) JIT engine is not capable of * emitting the opcodes. On newer llvm versions it is and at least some * versions (tested with 3.3) will emit avx opcodes without this anyway. */ MAttrs.push_back(util_cpu_caps.has_avx ? "+avx" : "-avx"); MAttrs.push_back(util_cpu_caps.has_f16c ? "+f16c" : "-f16c"); if (HAVE_LLVM >= 0x0304) { MAttrs.push_back(util_cpu_caps.has_fma ? "+fma" : "-fma"); } else { /* * The old JIT in LLVM 3.3 has a bug encoding llvm.fmuladd.f32 and * llvm.fmuladd.v2f32 intrinsics when FMA is available. */ MAttrs.push_back("-fma"); } MAttrs.push_back(util_cpu_caps.has_avx2 ? "+avx2" : "-avx2"); /* disable avx512 and all subvariants */ #if HAVE_LLVM >= 0x0304 MAttrs.push_back("-avx512cd"); MAttrs.push_back("-avx512er"); MAttrs.push_back("-avx512f"); MAttrs.push_back("-avx512pf"); #endif #if HAVE_LLVM >= 0x0305 MAttrs.push_back("-avx512bw"); MAttrs.push_back("-avx512dq"); MAttrs.push_back("-avx512vl"); #endif #endif #endif #if defined(PIPE_ARCH_PPC) MAttrs.push_back(util_cpu_caps.has_altivec ? "+altivec" : "-altivec"); #if (HAVE_LLVM >= 0x0304) #if (HAVE_LLVM < 0x0400) /* * Make sure VSX instructions are disabled * See LLVM bugs: * https://llvm.org/bugs/show_bug.cgi?id=25503#c7 (fixed in 3.8.1) * https://llvm.org/bugs/show_bug.cgi?id=26775 (fixed in 3.8.1) * https://llvm.org/bugs/show_bug.cgi?id=33531 (fixed in 4.0) * https://llvm.org/bugs/show_bug.cgi?id=34647 (llc performance on certain unusual shader IR; intro'd in 4.0, pending as of 5.0) */ if (util_cpu_caps.has_altivec) { MAttrs.push_back("-vsx"); } #else /* * Bug 25503 is fixed, by the same fix that fixed * bug 26775, in versions of LLVM later than 3.8 (starting with 3.8.1). * BZ 33531 actually comprises more than one bug, all of * which are fixed in LLVM 4.0. * * With LLVM 4.0 or higher: * Make sure VSX instructions are ENABLED, unless * a) the entire -mattr option is overridden via GALLIVM_MATTRS, or * b) VSX instructions are explicitly enabled/disabled via GALLIVM_VSX=1 or 0. */ if (util_cpu_caps.has_altivec) { char *env_mattrs = getenv("GALLIVM_MATTRS"); if (env_mattrs) { MAttrs.push_back(env_mattrs); } else { boolean enable_vsx = true; char *env_vsx = getenv("GALLIVM_VSX"); if (env_vsx && env_vsx[0] == '0') { enable_vsx = false; } if (enable_vsx) MAttrs.push_back("+vsx"); else MAttrs.push_back("-vsx"); } } #endif #endif #endif builder.setMAttrs(MAttrs); if (gallivm_debug & (GALLIVM_DEBUG_IR | GALLIVM_DEBUG_ASM | GALLIVM_DEBUG_DUMP_BC)) { int n = MAttrs.size(); if (n > 0) { debug_printf("llc -mattr option(s): "); for (int i = 0; i < n; i++) debug_printf("%s%s", MAttrs[i].c_str(), (i < n - 1) ? "," : ""); debug_printf("\n"); } } #if HAVE_LLVM >= 0x0305 StringRef MCPU = llvm::sys::getHostCPUName(); /* * The cpu bits are no longer set automatically, so need to set mcpu manually. * Note that the MAttrs set above will be sort of ignored (since we should * not set any which would not be set by specifying the cpu anyway). * It ought to be safe though since getHostCPUName() should include bits * not only from the cpu but environment as well (for instance if it's safe * to use avx instructions which need OS support). According to * http://llvm.org/bugs/show_bug.cgi?id=19429 however if I understand this * right it may be necessary to specify older cpu (or disable mattrs) though * when not using MCJIT so no instructions are generated which the old JIT * can't handle. Not entirely sure if we really need to do anything yet. */ #if defined(PIPE_ARCH_LITTLE_ENDIAN) && defined(PIPE_ARCH_PPC_64) /* * Versions of LLVM prior to 4.0 lacked a table entry for "POWER8NVL", * resulting in (big-endian) "generic" being returned on * little-endian Power8NVL systems. The result was that code that * attempted to load the least significant 32 bits of a 64-bit quantity * from memory loaded the wrong half. This resulted in failures in some * Piglit tests, e.g. * .../arb_gpu_shader_fp64/execution/conversion/frag-conversion-explicit-double-uint */ if (MCPU == "generic") MCPU = "pwr8"; #endif builder.setMCPU(MCPU); if (gallivm_debug & (GALLIVM_DEBUG_IR | GALLIVM_DEBUG_ASM | GALLIVM_DEBUG_DUMP_BC)) { debug_printf("llc -mcpu option: %s\n", MCPU.str().c_str()); } #endif ShaderMemoryManager *MM = NULL; if (useMCJIT) { BaseMemoryManager* JMM = reinterpret_cast<BaseMemoryManager*>(CMM); MM = new ShaderMemoryManager(JMM); *OutCode = MM->getGeneratedCode(); #if HAVE_LLVM >= 0x0306 builder.setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager>(MM)); MM = NULL; // ownership taken by std::unique_ptr #elif HAVE_LLVM > 0x0303 builder.setMCJITMemoryManager(MM); #else builder.setJITMemoryManager(MM); #endif } else { #if HAVE_LLVM < 0x0306 BaseMemoryManager* JMM = reinterpret_cast<BaseMemoryManager*>(CMM); MM = new ShaderMemoryManager(JMM); *OutCode = MM->getGeneratedCode(); builder.setJITMemoryManager(MM); #else assert(0); #endif } ExecutionEngine *JIT; JIT = builder.create(); #if LLVM_USE_INTEL_JITEVENTS JITEventListener *JEL = JITEventListener::createIntelJITEventListener(); JIT->RegisterJITEventListener(JEL); #endif if (JIT) { *OutJIT = wrap(JIT); return 0; } lp_free_generated_code(*OutCode); *OutCode = 0; delete MM; *OutError = strdup(Error.c_str()); return 1; }
LLVMBool lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef *OutJIT, lp_generated_code **OutCode, LLVMModuleRef M, LLVMMCJITMemoryManagerRef CMM, unsigned OptLevel, int useMCJIT, char **OutError) { using namespace llvm; std::string Error; #if HAVE_LLVM >= 0x0306 EngineBuilder builder(std::unique_ptr<Module>(unwrap(M))); #else EngineBuilder builder(unwrap(M)); #endif /** * LLVM 3.1+ haven't more "extern unsigned llvm::StackAlignmentOverride" and * friends for configuring code generation options, like stack alignment. */ TargetOptions options; #if defined(PIPE_ARCH_X86) options.StackAlignmentOverride = 4; #if HAVE_LLVM < 0x0304 options.RealignStack = true; #endif #endif #if defined(DEBUG) && HAVE_LLVM < 0x0307 options.JITEmitDebugInfo = true; #endif /* XXX: Workaround http://llvm.org/PR21435 */ #if defined(DEBUG) || defined(PROFILE) || \ (HAVE_LLVM >= 0x0303 && (defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64))) #if HAVE_LLVM < 0x0304 options.NoFramePointerElimNonLeaf = true; #endif #if HAVE_LLVM < 0x0307 options.NoFramePointerElim = true; #endif #endif builder.setEngineKind(EngineKind::JIT) .setErrorStr(&Error) .setTargetOptions(options) .setOptLevel((CodeGenOpt::Level)OptLevel); if (useMCJIT) { #if HAVE_LLVM < 0x0306 builder.setUseMCJIT(true); #endif #ifdef _WIN32 /* * MCJIT works on Windows, but currently only through ELF object format. * * XXX: We could use `LLVM_HOST_TRIPLE "-elf"` but LLVM_HOST_TRIPLE has * different strings for MinGW/MSVC, so better play it safe and be * explicit. */ # ifdef _WIN64 LLVMSetTarget(M, "x86_64-pc-win32-elf"); # else LLVMSetTarget(M, "i686-pc-win32-elf"); # endif #endif } llvm::SmallVector<std::string, 16> MAttrs; #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64) /* * We need to unset attributes because sometimes LLVM mistakenly assumes * certain features are present given the processor name. * * https://bugs.freedesktop.org/show_bug.cgi?id=92214 * http://llvm.org/PR25021 * http://llvm.org/PR19429 * http://llvm.org/PR16721 */ MAttrs.push_back(util_cpu_caps.has_sse ? "+sse" : "-sse" ); MAttrs.push_back(util_cpu_caps.has_sse2 ? "+sse2" : "-sse2" ); MAttrs.push_back(util_cpu_caps.has_sse3 ? "+sse3" : "-sse3" ); MAttrs.push_back(util_cpu_caps.has_ssse3 ? "+ssse3" : "-ssse3" ); #if HAVE_LLVM >= 0x0304 MAttrs.push_back(util_cpu_caps.has_sse4_1 ? "+sse4.1" : "-sse4.1"); #else MAttrs.push_back(util_cpu_caps.has_sse4_1 ? "+sse41" : "-sse41" ); #endif #if HAVE_LLVM >= 0x0304 MAttrs.push_back(util_cpu_caps.has_sse4_2 ? "+sse4.2" : "-sse4.2"); #else MAttrs.push_back(util_cpu_caps.has_sse4_2 ? "+sse42" : "-sse42" ); #endif /* * AVX feature is not automatically detected from CPUID by the X86 target * yet, because the old (yet default) JIT engine is not capable of * emitting the opcodes. On newer llvm versions it is and at least some * versions (tested with 3.3) will emit avx opcodes without this anyway. */ MAttrs.push_back(util_cpu_caps.has_avx ? "+avx" : "-avx"); MAttrs.push_back(util_cpu_caps.has_f16c ? "+f16c" : "-f16c"); if (HAVE_LLVM >= 0x0304) { MAttrs.push_back(util_cpu_caps.has_fma ? "+fma" : "-fma"); } else { /* * The old JIT in LLVM 3.3 has a bug encoding llvm.fmuladd.f32 and * llvm.fmuladd.v2f32 intrinsics when FMA is available. */ MAttrs.push_back("-fma"); } MAttrs.push_back(util_cpu_caps.has_avx2 ? "+avx2" : "-avx2"); /* disable avx512 and all subvariants */ #if HAVE_LLVM >= 0x0304 MAttrs.push_back("-avx512cd"); MAttrs.push_back("-avx512er"); MAttrs.push_back("-avx512f"); MAttrs.push_back("-avx512pf"); #endif #if HAVE_LLVM >= 0x0305 MAttrs.push_back("-avx512bw"); MAttrs.push_back("-avx512dq"); MAttrs.push_back("-avx512vl"); #endif #endif #if defined(PIPE_ARCH_PPC) MAttrs.push_back(util_cpu_caps.has_altivec ? "+altivec" : "-altivec"); #if HAVE_LLVM >= 0x0304 /* * Make sure VSX instructions are disabled * See LLVM bug https://llvm.org/bugs/show_bug.cgi?id=25503#c7 */ if (util_cpu_caps.has_altivec) { MAttrs.push_back("-vsx"); } #endif #endif builder.setMAttrs(MAttrs); #if HAVE_LLVM >= 0x0305 StringRef MCPU = llvm::sys::getHostCPUName(); /* * The cpu bits are no longer set automatically, so need to set mcpu manually. * Note that the MAttrs set above will be sort of ignored (since we should * not set any which would not be set by specifying the cpu anyway). * It ought to be safe though since getHostCPUName() should include bits * not only from the cpu but environment as well (for instance if it's safe * to use avx instructions which need OS support). According to * http://llvm.org/bugs/show_bug.cgi?id=19429 however if I understand this * right it may be necessary to specify older cpu (or disable mattrs) though * when not using MCJIT so no instructions are generated which the old JIT * can't handle. Not entirely sure if we really need to do anything yet. */ builder.setMCPU(MCPU); #endif ShaderMemoryManager *MM = NULL; if (useMCJIT) { BaseMemoryManager* JMM = reinterpret_cast<BaseMemoryManager*>(CMM); MM = new ShaderMemoryManager(JMM); *OutCode = MM->getGeneratedCode(); #if HAVE_LLVM >= 0x0306 builder.setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager>(MM)); MM = NULL; // ownership taken by std::unique_ptr #elif HAVE_LLVM > 0x0303 builder.setMCJITMemoryManager(MM); #else builder.setJITMemoryManager(MM); #endif } else { #if HAVE_LLVM < 0x0306 BaseMemoryManager* JMM = reinterpret_cast<BaseMemoryManager*>(CMM); MM = new ShaderMemoryManager(JMM); *OutCode = MM->getGeneratedCode(); builder.setJITMemoryManager(MM); #else assert(0); #endif } ExecutionEngine *JIT; JIT = builder.create(); #if LLVM_USE_INTEL_JITEVENTS JITEventListener *JEL = JITEventListener::createIntelJITEventListener(); JIT->RegisterJITEventListener(JEL); #endif if (JIT) { *OutJIT = wrap(JIT); return 0; } lp_free_generated_code(*OutCode); *OutCode = 0; delete MM; *OutError = strdup(Error.c_str()); return 1; }
MonoEERef mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, FunctionEmittedCb *emitted_cb, ExceptionTableCb *exception_cb, DlSymCb *dlsym_cb, LLVMExecutionEngineRef *ee) { std::string Error; MonoEE *mono_ee; init_llvm (); mono_ee = new MonoEE (); MonoJITMemoryManager *mono_mm = new MonoJITMemoryManager (); mono_mm->alloc_cb = alloc_cb; mono_mm->dlsym_cb = dlsym_cb; mono_mm->exception_cb = exception_cb; mono_ee->mm = mono_mm; /* * The Default code model doesn't seem to work on amd64, * test_0_fields_with_big_offsets (among others) crashes, because LLVM tries to call * memset using a normal pcrel code which is in 32bit memory, while memset isn't. */ TargetOptions opts; opts.JITExceptionHandling = 1; StringRef cpu_name = sys::getHostCPUName (); // EngineBuilder no longer has a copy assignment operator (?) std::unique_ptr<Module> Owner(unwrap(MP)); EngineBuilder b (std::move(Owner)); ExecutionEngine *EE = b.setJITMemoryManager (mono_mm).setTargetOptions (opts).setAllocateGVsWithCode (true).setMCPU (cpu_name).create (); g_assert (EE); mono_ee->EE = EE; MonoJITEventListener *listener = new MonoJITEventListener (emitted_cb); EE->RegisterJITEventListener (listener); mono_ee->listener = listener; FunctionPassManager *fpm = new FunctionPassManager (unwrap (MP)); mono_ee->fpm = fpm; fpm->add(new DataLayoutPass(*EE->getDataLayout())); if (PassList.size() > 0) { /* Use the passes specified by the env variable */ /* Only the passes in force_pass_linking () can be used */ for (unsigned i = 0; i < PassList.size(); ++i) { const PassInfo *PassInf = PassList[i]; Pass *P = 0; if (PassInf->getNormalCtor()) P = PassInf->getNormalCtor()(); fpm->add (P); } } else { /* Use the same passes used by 'opt' by default, without the ipo passes */ const char *opts = "-simplifycfg -domtree -domfrontier -scalarrepl -instcombine -simplifycfg -domtree -domfrontier -scalarrepl -instcombine -simplifycfg -instcombine -simplifycfg -reassociate -domtree -loops -loop-simplify -domfrontier -loop-simplify -lcssa -loop-rotate -licm -lcssa -loop-unswitch -instcombine -scalar-evolution -loop-simplify -lcssa -iv-users -indvars -loop-deletion -loop-simplify -lcssa -loop-unroll -instcombine -memdep -gvn -memdep -memcpyopt -sccp -instcombine -domtree -memdep -dse -adce -gvn -simplifycfg"; char **args; int i; args = g_strsplit (opts, " ", 1000); for (i = 0; args [i]; i++) ; llvm::cl::ParseCommandLineOptions (i, args, ""); g_strfreev (args); for (unsigned i = 0; i < PassList.size(); ++i) { const PassInfo *PassInf = PassList[i]; Pass *P = 0; if (PassInf->getNormalCtor()) P = PassInf->getNormalCtor()(); g_assert (P->getPassKind () == llvm::PT_Function || P->getPassKind () == llvm::PT_Loop); fpm->add (P); } /* fpm->add(createInstructionCombiningPass()); fpm->add(createReassociatePass()); fpm->add(createGVNPass()); fpm->add(createCFGSimplificationPass()); */ } *ee = wrap (EE); return mono_ee; }
LLVMExecutionEngineRef mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, FunctionEmittedCb *emitted_cb, ExceptionTableCb *exception_cb) { std::string Error; force_pass_linking (); LLVMInitializeX86Target (); LLVMInitializeX86TargetInfo (); mono_mm = new MonoJITMemoryManager (); mono_mm->alloc_cb = alloc_cb; #if LLVM_MAJOR_VERSION == 2 && LLVM_MINOR_VERSION < 8 DwarfExceptionHandling = true; #else JITExceptionHandling = true; #endif // PrettyStackTrace installs signal handlers which trip up libgc DisablePrettyStackTrace = true; ExecutionEngine *EE = ExecutionEngine::createJIT (unwrap (MP), &Error, mono_mm, CodeGenOpt::Default); if (!EE) { errs () << "Unable to create LLVM ExecutionEngine: " << Error << "\n"; g_assert_not_reached (); } EE->InstallExceptionTableRegister (exception_cb); mono_event_listener = new MonoJITEventListener (emitted_cb); EE->RegisterJITEventListener (mono_event_listener); fpm = new FunctionPassManager (unwrap (MP)); fpm->add(new TargetData(*EE->getTargetData())); #if LLVM_CHECK_VERSION(2, 9) PassRegistry &Registry = *PassRegistry::getPassRegistry(); initializeCore(Registry); initializeScalarOpts(Registry); //initializeIPO(Registry); initializeAnalysis(Registry); initializeIPA(Registry); initializeTransformUtils(Registry); initializeInstCombine(Registry); //initializeInstrumentation(Registry); initializeTarget(Registry); #endif llvm::cl::ParseEnvironmentOptions("mono", "MONO_LLVM", "", false); if (PassList.size() > 0) { /* Use the passes specified by the env variable */ /* Only the passes in force_pass_linking () can be used */ for (unsigned i = 0; i < PassList.size(); ++i) { const PassInfo *PassInf = PassList[i]; Pass *P = 0; if (PassInf->getNormalCtor()) P = PassInf->getNormalCtor()(); fpm->add (P); } } else { /* Use the same passes used by 'opt' by default, without the ipo passes */ const char *opts = "-simplifycfg -domtree -domfrontier -scalarrepl -instcombine -simplifycfg -basiccg -domtree -domfrontier -scalarrepl -simplify-libcalls -instcombine -simplifycfg -instcombine -simplifycfg -reassociate -domtree -loops -loopsimplify -domfrontier -loopsimplify -lcssa -loop-rotate -licm -lcssa -loop-unswitch -instcombine -scalar-evolution -loopsimplify -lcssa -iv-users -indvars -loop-deletion -loopsimplify -lcssa -loop-unroll -instcombine -memdep -gvn -memdep -memcpyopt -sccp -instcombine -domtree -memdep -dse -adce -gvn -simplifycfg -preverify -domtree -verify"; char **args; int i; args = g_strsplit (opts, " ", 1000); for (i = 0; args [i]; i++) ; llvm::cl::ParseCommandLineOptions (i, args, "", false); g_strfreev (args); for (unsigned i = 0; i < PassList.size(); ++i) { const PassInfo *PassInf = PassList[i]; Pass *P = 0; if (PassInf->getNormalCtor()) P = PassInf->getNormalCtor()(); fpm->add (P); } /* fpm->add(createInstructionCombiningPass()); fpm->add(createReassociatePass()); fpm->add(createGVNPass()); fpm->add(createCFGSimplificationPass()); */ } return wrap(EE); }
int main() { ygt_print("Hello!\n"); InitializeNativeTarget(); InitializeNativeTargetAsmPrinter(); InitializeNativeTargetAsmParser(); InitializeNativeTargetDisassembler(); ygt_print("Creating LLVM context and module...\n"); LLVMContext ctx; auto m = make_unique<Module>("the_module", ctx); ygt_print("Creating StackMapHelper...\n"); StackMapHelper smhelper; ygt_print("Declaring yg_stack_swap as an LLVM-level function...\n"); declare_ss(ctx, *m); ygt_print("Constructing bar, the ss leaf...\n"); Function *bar; smid_t bar_smid; tie(bar, bar_smid) = make_ss_leaf("barss", &main_stack, &coro_stack, smhelper, ctx, *m); verifyFunction(*bar); ygt_print("Constructing foo, the caller...\n"); Function *foo; smid_t foo_smid; tie(foo, foo_smid) = make_caller("foo", bar, smhelper, ctx, *m); verifyFunction(*foo); m->dump(); Function *ss_func = m->getFunction("yg_stack_swap"); ygt_print("Creating execution engine...\n"); ExecutionEngine *ee = EngineBuilder(move(m)).setEngineKind(EngineKind::JIT).create(); ygt_print("Execution engine created.\n"); StackMapSectionRecorder smsr; ygt_print("Registering JIT event listener...\n"); ee->RegisterJITEventListener(&smsr); EHFrameSectionRegisterer reg; ygt_print("Registering EHFrame registerer...\n"); ee->RegisterJITEventListener(®); ygt_print("Adding global mapping yg_stack_swap...\n"); ee->addGlobalMapping(ss_func, reinterpret_cast<void*>(yg_stack_swap)); ygt_print("JIT compiling...\n"); ee->finalizeObject(); ygt_print("Adding stack map sections...\n"); smhelper.add_stackmap_sections(smsr, *ee); ygt_print("Registering EH frames...\n"); reg.registerEHFrameSections(); uintptr_t yg_stack_swap_addr = ee->getFunctionAddress("yg_stack_swap"); ygt_print("yg_stack_swap_addr=%" PRIxPTR ", yg_stack_swap=%p\n", yg_stack_swap_addr, yg_stack_swap); assert(yg_stack_swap_addr == reinterpret_cast<uintptr_t>(yg_stack_swap)); ygt_print("Getting foo...\n"); void (*the_real_foo)(int32_t, int64_t, float, double) = (void(*)(int32_t, int64_t, float, double)) ee->getFunctionAddress("foo"); ygt_print("the_real_foo == %p\n", the_real_foo); ygt_print("Prepare corotine for introspection...\n"); coro_stack = YGStack::alloc(8*1024*1024); coro_stack.init(reinterpret_cast<uintptr_t>(coro)); MyCtx my_ctx = { foo_smid, bar_smid, &smhelper }; ygt_print("Swap-stack to prepare the coroutine...\n"); yg_stack_swap(&main_stack, &coro_stack, reinterpret_cast<uintptr_t>(&my_ctx)); ygt_print("Back from coro.\n"); ygt_print("Calling the_real_foo...\n"); the_real_foo(100, 200, 300.0f, 400.0); ygt_print("Returned from foo.\n"); return 0; }