support 3.6

This commit is contained in:
Vítor Santos Costa 2015-06-19 01:17:07 +01:00
parent 497cc27003
commit 8f90705e26
7 changed files with 200 additions and 155 deletions

View File

@ -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:

View File

@ -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;

View File

@ -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<Pass>(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<Pass>(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<Pass>(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<Pass>(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<Pass>(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<Pass>(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;

View File

@ -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 || \

View File

@ -2,7 +2,19 @@
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
using namespace std;
//using namespace std;
//using namespace llvm;
#include <llvm/Pass.h>
#include <llvm/PassAnalysisSupport.h>
#include <llvm/PassInfo.h>
#include <llvm/PassManager.h>
#include <llvm/PassRegistry.h>
#include <llvm/PassSupport.h>
#include <llvm/Analysis/CallGraphSCCPass.h>
#include <llvm/CodeGen/Passes.h>
#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<FunctionPassManager> 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<Module *> ModuleOrErr =
parseBitcodeFile(em.get()->getMemBufferRef(), *Context);
std::unique_ptr<Module> 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<tool_output_file> Out;
std::string ErrorInfo;
std::unique_ptr<tool_output_file> 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<HelpingMemoryManager>(
// new HelpingMemoryManager(this)))
.setMCJITMemoryManager(std::unique_ptr<SectionMemoryManager>(new SectionMemoryManager()))
.create();
if (!EE) {
if (!errStr.empty())
errs() << "Error creating Execution Engine: " << errStr << "\n";

View File

@ -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);

View File

@ -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 );