From 8f90705e266dc71648a483f1715a7ded44eaecc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa?= Date: Fri, 19 Jun 2015 01:17:07 +0100 Subject: [PATCH] support 3.6 --- JIT/CMakeLists.txt | 3 + JIT/HPP/JIT_Compiler.hpp | 40 +++++---- JIT/HPP/PassPrinters.hh | 184 +++++++++++++++++++-------------------- JIT/HPP/yaam_macros.hpp | 2 +- JIT/JIT_Compiler.cpp | 119 +++++++++++++++---------- JIT/jit_debugpreds.c | 2 +- JIT/jit_traced.c | 5 +- 7 files changed, 200 insertions(+), 155 deletions(-) diff --git a/JIT/CMakeLists.txt b/JIT/CMakeLists.txt index d1c2ca5b4..02ce1d4b6 100644 --- a/JIT/CMakeLists.txt +++ b/JIT/CMakeLists.txt @@ -14,6 +14,9 @@ set(LIBJIT_SOURCES jit_transformpreds.c JIT_Compiler.cpp JIT_Init.cpp + HPP/JIT.hpp + HPP/JIT_Compiler.hpp + HPP/jit_predicates.hpp ) # The following variables are defined: diff --git a/JIT/HPP/JIT_Compiler.hpp b/JIT/HPP/JIT_Compiler.hpp index 395e3dc10..3f76001fd 100644 --- a/JIT/HPP/JIT_Compiler.hpp +++ b/JIT/HPP/JIT_Compiler.hpp @@ -3,45 +3,54 @@ #ifdef __cplusplus -#include "llvm/IR/LLVMContext.h" -#include "llvm/IR/Module.h" -#include "llvm/IR/Constants.h" -#include "llvm/IR/DerivedTypes.h" -#include "llvm/IR/Instructions.h" #include "llvm/Linker/Linker.h" #include "llvm/PassManager.h" #include "llvm/Analysis/CallGraphSCCPass.h" +#include "llvm/Analysis/Passes.h" #include "llvm/ADT/Statistic.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringSet.h" #include "llvm/ADT/Triple.h" -#include "llvm/IR/IRPrintingPasses.h" #include "llvm/CodeGen/Passes.h" -#include "llvm/CodeGen/MachineCodeInfo.h" -#include "llvm/ExecutionEngine/JIT.h" +#include "llvm/CodeGen/MachineBlockFrequencyInfo.h" +#include "llvm/CodeGen/MachineBranchProbabilityInfo.h" +#include "llvm/CodeGen/MachineFrameInfo.h" +#include "llvm/CodeGen/MachineJumpTableInfo.h" +#include "llvm/CodeGen/MachineLoopInfo.h" +#include "llvm/CodeGen/MachineBranchProbabilityInfo.h" +#include "llvm/CodeGen/MachineModuleInfo.h" +#include "llvm/CodeGen/MachineRegionInfo.h" +#include "llvm/ExecutionEngine/ExecutionEngine.h" +#include "llvm/ExecutionEngine/MCJIT.h" #include "llvm/ExecutionEngine/Interpreter.h" #include "llvm/ExecutionEngine/GenericValue.h" -#include "llvm/ExecutionEngine/JITMemoryManager.h" #include "llvm/ExecutionEngine/JITEventListener.h" -#include "llvm/Support/DataStream.h" +#include "llvm/ExecutionEngine/SectionMemoryManager.h" +#include "llvm/IR/Constants.h" +#include "llvm/IR/DataLayout.h" +#include "llvm/IR/DerivedTypes.h" +#include "llvm/IR/Instructions.h" +#include "llvm/IR/IRBuilder.h" +#include "llvm/IR/IRPrintingPasses.h" +#include "llvm/IR/LegacyPassManager.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Module.h" +#include "llvm/IR/TypeBuilder.h" +#include "llvm/IR/Verifier.h" #include "llvm/IRReader/IRReader.h" #include "llvm/Support/Host.h" #include "llvm/Support/Path.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/raw_os_ostream.h" -#include "llvm/IR/IRBuilder.h" //#include "llvm/Support/PathV1.h" -#include "llvm/IR/TypeBuilder.h" #include "llvm/Support/ToolOutputFile.h" -#include "llvm/IR/DataLayout.h" #include "llvm/Target/TargetMachine.h" #include "llvm/Target/TargetOptions.h" #include "llvm/Target/TargetLibraryInfo.h" #include "llvm/Analysis/Passes.h" #include "llvm/Analysis/IVUsers.h" #include "llvm/Analysis/Lint.h" -#include "llvm/IR/DebugInfo.h" -#include "llvm/IR/Verifier.h" #include "llvm/Analysis/LoopPass.h" #include "llvm/Analysis/RegionPass.h" #include "llvm/Analysis/CFGPrinter.h" @@ -59,6 +68,7 @@ #include "llvm/Transforms/Utils/Cloning.h" #include "llvm/Transforms/ObjCARC.h" #include "llvm/Support/FileSystem.h" +#include "llvm/Pass.h" using namespace llvm; diff --git a/JIT/HPP/PassPrinters.hh b/JIT/HPP/PassPrinters.hh index 91205b5e5..f39a7148e 100644 --- a/JIT/HPP/PassPrinters.hh +++ b/JIT/HPP/PassPrinters.hh @@ -88,96 +88,96 @@ struct ModulePassPrinter : public ModulePass { char ModulePassPrinter::ID = 0; -struct FunctionPassPrinter : public FunctionPass { - const PassInfo *PassToPrint; - static char ID; - std::string PassName; - - FunctionPassPrinter(const PassInfo *PassInfo) - : FunctionPass(ID), PassToPrint(PassInfo) { - std::string PassToPrintName = PassToPrint->getPassName(); - PassName = "FunctionPass Printer: " + PassToPrintName; - } - - virtual bool runOnFunction(Function &F) { - errs() << "Printing analysis '" << PassToPrint->getPassName() - << "' for function '" << F.getName() << "':\n"; - - // Get and print pass... - getAnalysisID(PassToPrint->getTypeInfo()).print(errs(), - F.getParent()); - return false; - } - - virtual const char *getPassName() const { return PassName.c_str(); } - - virtual void getAnalysisUsage(AnalysisUsage &AU) const { - AU.addRequiredID(PassToPrint->getTypeInfo()); - AU.setPreservesAll(); - } -}; - -char FunctionPassPrinter::ID = 0; - -struct LoopPassPrinter : public LoopPass { - static char ID; - const PassInfo *PassToPrint; - std::string PassName; - - LoopPassPrinter(const PassInfo *PassInfo) : - LoopPass(ID), PassToPrint(PassInfo) { - std::string PassToPrintName = PassToPrint->getPassName(); - PassName = "LoopPass Printer: " + PassToPrintName; - } - - - virtual bool runOnLoop(Loop *L, LPPassManager &LPM) { - errs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n"; - - // Get and print pass... - getAnalysisID(PassToPrint->getTypeInfo()).print(errs(), - L->getHeader()->getParent()->getParent()); - return false; - } - - virtual const char *getPassName() const { return PassName.c_str(); } - - virtual void getAnalysisUsage(AnalysisUsage &AU) const { - AU.addRequiredID(PassToPrint->getTypeInfo()); - AU.setPreservesAll(); - } -}; - -char LoopPassPrinter::ID = 0; - -struct RegionPassPrinter : public RegionPass { - static char ID; - const PassInfo *PassToPrint; - std::string PassName; - - RegionPassPrinter(const PassInfo *PassInfo) : RegionPass(ID), - PassToPrint(PassInfo) { - std::string PassToPrintName = PassToPrint->getPassName(); - PassName = "RegionPass Printer: " + PassToPrintName; - } - - virtual bool runOnRegion(Region *R, RGPassManager &RGM) { - errs() << "Printing analysis '" << PassToPrint->getPassName() << "' for " - << "region: '" << R->getNameStr() << "' in function '" - << R->getEntry()->getParent()->getName() << "':\n"; - - // Get and print pass... - getAnalysisID(PassToPrint->getTypeInfo()).print(errs(), - R->getEntry()->getParent()->getParent()); - return false; - } - - virtual const char *getPassName() const { return PassName.c_str(); } - - virtual void getAnalysisUsage(AnalysisUsage &AU) const { - AU.addRequiredID(PassToPrint->getTypeInfo()); - AU.setPreservesAll(); - } -}; - +struct FunctionPassPrinter : public FunctionPass { + const PassInfo *PassToPrint; + static char ID; + std::string PassName; + + FunctionPassPrinter(const PassInfo *PassInfo) + : FunctionPass(ID), PassToPrint(PassInfo) { + std::string PassToPrintName = PassToPrint->getPassName(); + PassName = "FunctionPass Printer: " + PassToPrintName; + } + + virtual bool runOnFunction(Function &F) { + errs() << "Printing analysis '" << PassToPrint->getPassName() + << "' for function '" << F.getName() << "':\n"; + + // Get and print pass... + getAnalysisID(PassToPrint->getTypeInfo()).print(errs(), + F.getParent()); + return false; + } + + virtual const char *getPassName() const { return PassName.c_str(); } + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.addRequiredID(PassToPrint->getTypeInfo()); + AU.setPreservesAll(); + } +}; + +char FunctionPassPrinter::ID = 0; + +struct LoopPassPrinter : public LoopPass { + static char ID; + const PassInfo *PassToPrint; + std::string PassName; + + LoopPassPrinter(const PassInfo *PassInfo) : + LoopPass(ID), PassToPrint(PassInfo) { + std::string PassToPrintName = PassToPrint->getPassName(); + PassName = "LoopPass Printer: " + PassToPrintName; + } + + + virtual bool runOnLoop(Loop *L, LPPassManager &LPM) { + errs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n"; + + // Get and print pass... + getAnalysisID(PassToPrint->getTypeInfo()).print(errs(), + L->getHeader()->getParent()->getParent()); + return false; + } + + virtual const char *getPassName() const { return PassName.c_str(); } + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.addRequiredID(PassToPrint->getTypeInfo()); + AU.setPreservesAll(); + } +}; + +char LoopPassPrinter::ID = 0; + +struct RegionPassPrinter : public RegionPass { + static char ID; + const PassInfo *PassToPrint; + std::string PassName; + + RegionPassPrinter(const PassInfo *PassInfo) : RegionPass(ID), + PassToPrint(PassInfo) { + std::string PassToPrintName = PassToPrint->getPassName(); + PassName = "RegionPass Printer: " + PassToPrintName; + } + + virtual bool runOnRegion(Region *R, RGPassManager &RGM) { + errs() << "Printing analysis '" << PassToPrint->getPassName() << "' for " + << "region: '" << R->getNameStr() << "' in function '" + << R->getEntry()->getParent()->getName() << "':\n"; + + // Get and print pass... + getAnalysisID(PassToPrint->getTypeInfo()).print(errs(), + R->getEntry()->getParent()->getParent()); + return false; + } + + virtual const char *getPassName() const { return PassName.c_str(); } + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.addRequiredID(PassToPrint->getTypeInfo()); + AU.setPreservesAll(); + } +}; + char RegionPassPrinter::ID = 0; diff --git a/JIT/HPP/yaam_macros.hpp b/JIT/HPP/yaam_macros.hpp index 4faec1f85..0ab9de8f2 100644 --- a/JIT/HPP/yaam_macros.hpp +++ b/JIT/HPP/yaam_macros.hpp @@ -97,7 +97,7 @@ set_last_deeply(BlocksContext* b, BlocksContext** last) { yaam_block == COUNT_RETRY_LOGICAL_END || \ yaam_block == COUNT_TRUST_LOGICAL_END || \ yaam_block == LOCK_LU_END || \ - yaam_block == TRY_AND_MARK_YAPOR_THREADS_NOYAPOR_IF || \ + yaam_block == TRY_AND_MARK_YAPOR_THREADS_NOYAPOR_IF #else /* defined(YAPOR) || defined(THREADS) */ #define YAAM_BLOCK_IS_SIMPLEB_MULTIPLE_DESTINY(yaam_block) \ yaam_block == YAAM_UNIFYBOUND || \ diff --git a/JIT/JIT_Compiler.cpp b/JIT/JIT_Compiler.cpp index e30a774b0..f17774fa1 100644 --- a/JIT/JIT_Compiler.cpp +++ b/JIT/JIT_Compiler.cpp @@ -2,7 +2,19 @@ #include #include #include -using namespace std; +//using namespace std; +//using namespace llvm; + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "PassPrinters.hh" #define FREE_ALLOCATED() \ free(buffer); \ @@ -18,7 +30,7 @@ using namespace std; fprintf(stderr, "Oops -- basicblock printer\n"); \ exit(1); \ case PT_Region: \ - Pass.add(new RegionPassPrinter(PInfo)); \ + Pass.add(new RegionPassPrinter(PInfo)); \ break; \ case PT_Loop: \ Pass.add(new LoopPassPrinter(PInfo)); \ @@ -30,7 +42,7 @@ using namespace std; Pass.add(new CallGraphSCCPassPrinter(PInfo)); \ break; \ default: \ - Pass.add(new ModulePassPrinter(PInfo)); \ + Pass.add(new ModulePassPrinter(PInfo)); \ break; \ } @@ -39,8 +51,6 @@ using namespace std; Kind = PASS->getPassKind(); \ ADD_PASS_ACCORDING_TO_KIND(); -#include "PassPrinters.hh" - void JIT_Compiler::analyze_module(llvm::Module* &M) { PassManager Pass; // 'Pass' stores analysis passes to be applied @@ -48,9 +58,9 @@ void JIT_Compiler::analyze_module(llvm::Module* &M) const PassInfo *PInfo; PassKind Kind; - + Pass.add(TLI); // First, I add on 'Pass' the Target Info of Module - Pass.add(new DataLayoutPass(M)); // Second, I must add Target Data on 'Pass' + Pass.add(new DataLayoutPass()); // Second, I must add Target Data on 'Pass' for (int i = 0; i < ExpEnv.analysis_struc.n; i++) { /* * 'ExpEnv.analysis_struc.act_an' contains sorted analysis passes * @@ -217,15 +227,15 @@ void JIT_Compiler::optimize_module(llvm::Module* &M) /* Initializes PassManager for Function */ std::shared_ptr FPM; - FPM.reset(new FunctionPassManager(M)); - FPM->add(new DataLayoutPass(M)); + FPM.reset(new legacy::FunctionPassManager(M)); + FPM->add(new DataLayoutPass()); PassManagerBuilder Builder; // aid to 'FPM' and 'MPM' /* Initializes PassManager for Function */ PassManager MPM; TargetLibraryInfo *TLI = new TargetLibraryInfo(Triple(M->getTargetTriple())); MPM.add(TLI); - MPM.add(new DataLayoutPass(M)); + MPM.add(new DataLayoutPass()); /* Populates 'Builder' */ Builder.OptLevel = ExpEnv.transform_struc.optlevel; @@ -249,9 +259,10 @@ void JIT_Compiler::optimize_module(llvm::Module* &M) * Use 'link_time_opt/1', 'link_time_opt/3', 'enable_link_time_opt/0', or 'enable_link_time_opt/2' to change * */ if (ExpEnv.transform_struc.link_time_opt.enabled) - Builder.populateLTOPassManager(MPM, + Builder.populateLTOPassManager(MPM /*, (bool)ExpEnv.transform_struc.link_time_opt.internalize, - (bool)ExpEnv.transform_struc.link_time_opt.runinliner); + (bool)ExpEnv.transform_struc.link_time_opt.runinliner + */); /***/ //set_regalloc_pass(MPM); @@ -271,7 +282,7 @@ void JIT_Compiler::optimize_module(llvm::Module* &M) PassManager Pass; // 'Pass' stores transform passes to be applied TargetLibraryInfo *TLI = new TargetLibraryInfo(Triple(M->getTargetTriple())); Pass.add(TLI); // First, I add on 'Pass' the Target Info of Module - Pass.add(new DataLayoutPass(M)); // Second, I must add Target Data on 'Pass' + Pass.add(new DataLayoutPass()); // Second, I must add Target Data on 'Pass' for (int i = 0; i < ExpEnv.transform_struc.n; i++) { /* * 'ExpEnv.transform_struc.act_tr' contains sorted transform passes * @@ -654,8 +665,15 @@ void* JIT_Compiler::compile_all(LLVMContext* &Context, yamop* p) * I need to read it to Module * * for this, I'll use 'parseBitcodeFile' * */ - Module *Mod = *parseBitcodeFile(em->get(), *Context); + ErrorOr ModuleOrErr = + parseBitcodeFile(em.get()->getMemBufferRef(), *Context); + std::unique_ptr M; + + if (std::error_code ec = ModuleOrErr.getError()) + errs() << ec.message(); + /* at last, get M */ + M.reset(ModuleOrErr.get()); /* * verify module correctness * * predicates: * @@ -664,7 +682,7 @@ void* JIT_Compiler::compile_all(LLVMContext* &Context, yamop* p) * verify_module_both/0 * */ if (ExpEnv.analysis_struc.pointtoverifymodule == BEFORE || ExpEnv.analysis_struc.pointtoverifymodule == BOTH) { - if (verifyModule(*Mod)) { + if (verifyModule(*M)) { errs() << "ERROR:: Module not built correctly!\n"; exit(1); } @@ -677,6 +695,7 @@ void* JIT_Compiler::compile_all(LLVMContext* &Context, yamop* p) errs() << "Module before optimization::\n" << *Mod; #endif + llvm::Module *Mod = M.get(); /* Analyze module -- analysis predicates */ analyze_module(Mod); /* Optimize module -- transform predicates */ @@ -684,18 +703,18 @@ void* JIT_Compiler::compile_all(LLVMContext* &Context, yamop* p) /* Computing size of optimized module */ { + std::error_code ErrorInfo; /* Open file 'tmp.bc' which will be filled by optimized Module */ - std::shared_ptr Out; - std::string ErrorInfo; + std::unique_ptr Out; Out.reset(new tool_output_file("tmp.bc", ErrorInfo, llvm::sys::fs::F_None)); - if (!ErrorInfo.empty()) { - errs() << ErrorInfo << '\n'; + if (ErrorInfo) { + errs() << ErrorInfo.message() << '\n'; exit(1); } /* 'createPrintModulePass(arg)' will print Module (now optimized) to on file represented by 'arg' */ PassManager Pass; Pass.add(createPrintModulePass(Out->os())); - Pass.run(*Mod); + Pass.run(*M); /* 'Out->keep()' will keep printed module to file and will close file */ Out->keep(); @@ -708,7 +727,7 @@ void* JIT_Compiler::compile_all(LLVMContext* &Context, yamop* p) close(Outtmp); remove("tmp.bc"); } - /***/ + /***/\ #if YAP_DBG_PREDS /* for debug... print module after optimizing it */ @@ -741,63 +760,63 @@ void* JIT_Compiler::compile_all(LLVMContext* &Context, yamop* p) /* Creating EngineBuilder -- called 'builder' */ Function *EntryFn; - EngineBuilder builder(Mod); - builder.setErrorStr(&errStr); - builder.setJITMemoryManager(JITMemoryManager::CreateDefaultMemManager()); - builder.setEngineKind(EngineKind::JIT); - // codegen predicate 'engine_opt_level/1' + + llvm::CodeGenOpt::Level level; switch(ExpEnv.codegen_struc.struc_enginebuilder.engineoptlevel) { case 0: - builder.setOptLevel(CodeGenOpt::None); + level = CodeGenOpt::None; break; case 1: - builder.setOptLevel(CodeGenOpt::Less); + level = CodeGenOpt::Less; break; case 2: - builder.setOptLevel(CodeGenOpt::Default); + level = CodeGenOpt::Default; break; case 3: - builder.setOptLevel(CodeGenOpt::Aggressive); + level = CodeGenOpt::Aggressive; break; } // codegen predicate 'relocmodel/1' + llvm::Reloc::Model relocmodel; switch(ExpEnv.codegen_struc.struc_enginebuilder.relocmodel) { case 0: - builder.setRelocationModel(Reloc::Default); + relocmodel = Reloc::Default; break; case 1: - builder.setRelocationModel(Reloc::Static); + relocmodel = Reloc::Static; break; case 2: - builder.setRelocationModel(Reloc::PIC_); + relocmodel = Reloc::PIC_; break; case 3: - builder.setRelocationModel(Reloc::DynamicNoPIC); + relocmodel = Reloc::DynamicNoPIC; break; } // codegen predicate 'codemodel/1' + llvm::CodeModel::Model codemodel; switch(ExpEnv.codegen_struc.struc_enginebuilder.codemodel) { case 0: - builder.setCodeModel(CodeModel::Default); + codemodel = CodeModel::Default; break; case 1: - builder.setCodeModel(CodeModel::JITDefault); + codemodel = CodeModel::JITDefault; break; case 2: - builder.setCodeModel(CodeModel::Small); + codemodel = CodeModel::Small; break; case 3: - builder.setCodeModel(CodeModel::Kernel); + codemodel = CodeModel::Kernel; break; case 4: - builder.setCodeModel(CodeModel::Medium); + codemodel = CodeModel::Medium; break; case 5: - builder.setCodeModel(CodeModel::Large); + codemodel = CodeModel::Large; break; } + // MCJIT is default from 3.6 // codegen predicates 'enable_mcjit/0' or 'disable_mcjit/0' - builder.setUseMCJIT((bool)ExpEnv.codegen_struc.struc_enginebuilder.usemcjit); + // builder.setUseMCJIT((bool)ExpEnv.codegen_struc.struc_enginebuilder.usemcjit); llvm::TargetOptions Options; { /* codegen predicates 'enable_framepointer_elimination/0' or 'disable_framepointer_elimination/0' */ @@ -843,11 +862,23 @@ void* JIT_Compiler::compile_all(LLVMContext* &Context, yamop* p) Options.FloatABIType = FloatABI::Hard; break; } - builder.setTargetOptions(Options); - /***/ + + // codegen predicate 'engine_opt_level/1' /* Creating ExecutionEngine from EngineBuilder (builder) */ - ExecutionEngine *EE = builder.create(); + ExecutionEngine *EE = + EngineBuilder(std::move(M)) + .setErrorStr(&errStr) + .setOptLevel( level ) + .setRelocationModel( relocmodel ) + .setCodeModel( codemodel ) + .setEngineKind(EngineKind::JIT) + .setTargetOptions(Options) + // check class in Kaleidoscope example. + // .setMCJITMemoryManager(std::unique_ptr( + // new HelpingMemoryManager(this))) + .setMCJITMemoryManager(std::unique_ptr(new SectionMemoryManager())) + .create(); if (!EE) { if (!errStr.empty()) errs() << "Error creating Execution Engine: " << errStr << "\n"; diff --git a/JIT/jit_debugpreds.c b/JIT/jit_debugpreds.c index 996ceb255..24bd9c7d7 100644 --- a/JIT/jit_debugpreds.c +++ b/JIT/jit_debugpreds.c @@ -2377,7 +2377,7 @@ p_no_exit_on_error( USES_REGS1 ) #endif void -Yap_InitJitDebugPreds( USES_REGS1 ) +Yap_InitJitDebugPreds( void ) { #if YAP_DBG_PREDS Yap_InitCPred("no_print_instruction", 1, p_no_print_instruction, SafePredFlag); diff --git a/JIT/jit_traced.c b/JIT/jit_traced.c index 47f4e11e4..2ff6929eb 100644 --- a/JIT/jit_traced.c +++ b/JIT/jit_traced.c @@ -191,12 +191,13 @@ CACHE_A1(); op_numbers opcode = _Ystop; goto critical_lbl; - nextop_write: + //nextop_write: opcode = Yap_op_from_opcode( PREG->y_u.o.opcw ); goto op_switch; - nextop: + + // nextop: opcode = Yap_op_from_opcode( PREG->opc );