Adding target specific passes.

This commit is contained in:
Siu Kwan Lam 2013-01-02 17:00:59 -06:00
commit 3b399f9d8b
5 changed files with 149 additions and 3 deletions

View file

@ -1036,6 +1036,7 @@ _wrap_none2str(LLVMDumpPasses)
_wrap_objstr2obj(LLVMAddPassByName, LLVMPassManagerRef, int)
_wrap_objobj2none(LLVMAddPass, LLVMPassManagerRef, LLVMPassRef)
_wrap_none2none(LLVMInitializePasses)
_wrap_none2obj(LLVMInitializeNativeTarget, int)
@ -1061,7 +1062,11 @@ _wrap_none2none(LLVMInitializePTXAsmPrinter)
_wrap_str2obj(LLVMCreatePassByName, LLVMPassRef)
_wrap_obj2none(LLVMDisposePass, LLVMPassRef)
_wrap_obj2str(LLVMGetPassName, LLVMPassRef)
_wrap_objobj2none(LLVMAddPass, LLVMPassManagerRef, LLVMPassRef)
_wrap_obj2none(LLVMPassDump, LLVMPassRef)
_wrap_str2obj(LLVMCreateTargetLibraryInfo, LLVMPassRef)
_wrap_obj2obj(LLVMCreateTargetTransformInfo, LLVMTargetMachineRef, LLVMPassRef)
/*===----------------------------------------------------------------------===*/
/* Target Machine */
@ -1096,6 +1101,30 @@ _wLLVMTargetMachineLookup(PyObject * self, PyObject * args)
LLVMPY_CATCH_ALL
}
static PyObject *
_wLLVMCreateTargetMachine(PyObject * self, PyObject * args)
{
LLVMPY_TRY
const char *triple;
const char *cpu;
const char *features;
int opt;
if (!PyArg_ParseTuple(args, "sssi", &triple, &cpu, &features, &opt))
return NULL;
std::string error;
LLVMTargetMachineRef tm = LLVMCreateTargetMachine(triple, cpu, features, opt,
error);
if(!error.empty()){
PyErr_SetString(PyExc_RuntimeError, error.c_str());
return NULL;
}
return pycap_new<LLVMTargetMachineRef>(tm);
LLVMPY_CATCH_ALL
}
static PyObject *
_wLLVMTargetMachineEmitFile(PyObject * self, PyObject * args)
{
@ -1482,6 +1511,9 @@ _wLLVMParseEnvOpts(PyObject *self, PyObject *args)
_wrap_obj2obj(LLVMInlineFunction, LLVMValueRef, int)
_wrap_none2str(LLVMDefaultTargetTriple)
/* Expose the void* inside a PyCObject as a PyLong. This allows us to
* use it as a unique ID. */
static PyObject *
@ -1937,6 +1969,7 @@ static PyMethodDef core_methods[] = {
_method( LLVMDisposePassManager )
_method( LLVMDumpPasses )
_method( LLVMAddPassByName )
_method( LLVMAddPass )
_method( LLVMInitializePasses )
_method( LLVMInitializeNativeTarget )
@ -1958,6 +1991,7 @@ static PyMethodDef core_methods[] = {
/* Target Machine */
_method( LLVMTargetMachineFromEngineBuilder )
_method( LLVMDisposeTargetMachine )
_method( LLVMCreateTargetMachine )
_method( LLVMTargetMachineLookup )
_method( LLVMTargetMachineEmitFile )
_method( LLVMTargetMachineGetTargetData )
@ -2024,13 +2058,16 @@ static PyMethodDef core_methods[] = {
_method( LLVMCreatePassByName )
_method( LLVMDisposePass )
_method( LLVMGetPassName )
_method( LLVMAddPass )
_method( LLVMPassDump )
_method( LLVMCreateTargetLibraryInfo )
_method( LLVMCreateTargetTransformInfo )
/* Misc */
_method( LLVMGetIntrinsic )
_method( LLVMLoadLibraryPermanently )
_method( LLVMParseEnvOpts )
_method( LLVMDefaultTargetTriple )
_method( LLVMInlineFunction )
_method( PyCObjectVoidPtrToPyLong )
{ NULL }

View file

@ -257,8 +257,21 @@ def get_host_cpu_name():
'''
return _core.LLVMGetHostCPUName()
def get_default_triple():
'''return the target triple of the host in str-rep
'''
return _core.LLVMDefaultTargetTriple()
class TargetMachine(object):
@staticmethod
def new(triple='', cpu='', features='', opt=2):
if not triple and not cpu:
triple = get_default_triple()
cpu = get_host_cpu_name()
ptr = _core.LLVMCreateTargetMachine(triple, cpu, features, opt)
return TargetMachine(ptr)
@staticmethod
def lookup(arch, cpu='', features='', opt=2):
'''create a targetmachine given an architecture name

View file

@ -75,6 +75,7 @@
#include "llvm/Support/SourceMgr.h"
#include <llvm/InlineAsm.h>
#include <llvm/Support/PassNameParser.h>
#include <llvm/Target/TargetLibraryInfo.h>
#if LLVM_VERSION_MAJOR >= 3 && LLVM_VERSION_MINOR >= 2
@ -143,6 +144,23 @@ const CodeGenOpt::Level OptLevelMap[] = {
};
} // end anony namespace
LLVMPassRef LLVMCreateTargetTransformInfo(LLVMTargetMachineRef tmref){
using namespace llvm;
TargetMachine * tm = unwrap(tmref);
Pass * tti = new TargetTransformInfo(tm->getScalarTargetTransformInfo(),
tm->getVectorTargetTransformInfo());
return wrap(tti);
}
LLVMPassRef LLVMCreateTargetLibraryInfo(const char * triple){
using namespace llvm;
Pass * tli = new TargetLibraryInfo(Triple(triple));
return wrap(tli);
}
const char * LLVMDefaultTargetTriple(){
return strdup(llvm::sys::getDefaultTargetTriple().c_str());
}
LLVMPassRef LLVMCreatePassByName(const char *name){
using namespace llvm;
@ -164,6 +182,11 @@ const char * LLVMGetPassName(LLVMPassRef passref){
return unwrap(passref)->getPassName();
}
void LLVMPassDump(LLVMPassRef passref){
using namespace llvm;
return unwrap(passref)->dump();
}
void LLVMAddPass(LLVMPassManagerRef pmref, LLVMPassRef passref){
using namespace llvm;
unwrap(pmref)->add(unwrap(passref));
@ -354,6 +377,30 @@ LLVMTargetMachineRef LLVMTargetMachineLookup(const char *arch, const char *cpu,
return wrap(tm);
}
LLVMTargetMachineRef LLVMCreateTargetMachine(const char *triple,
const char *cpu,
const char *features,
int opt,
std::string &error)
{
using namespace llvm;
std::string TheTriple = triple;
const Target * TheTarget = TargetRegistry::lookupTarget(TheTriple, error);
if (!TheTarget) return NULL;
TargetOptions no_target_options;
TargetMachine * tm = TheTarget->createTargetMachine(TheTriple, cpu, features,
no_target_options,
Reloc::Default,
CodeModel::Default,
OptLevelMap[opt]);
if (!tm) {
error = "Cannot create target machine";
return NULL;
}
return wrap(tm);
}
LLVMTargetMachineRef LLVMTargetMachineFromEngineBuilder(LLVMEngineBuilderRef eb)
{

View file

@ -54,6 +54,23 @@
extern "C" {
#endif
/*
* Wraps new TargetTransformInfo(
* TargetMachine::getScalarTargetTransformInfo,
* TargetMachine::getVectorTargetTransformInfo)
*/
LLVMPassRef LLVMCreateTargetTransformInfo(LLVMTargetMachineRef tmref);
/*
* Wraps new TargetLibraryInfo
*/
LLVMPassRef LLVMCreateTargetLibraryInfo(const char * triple);
/*
* Wraps llvm::getDefaultTargetTriple
*/
const char * LLVMDefaultTargetTriple();
/*
* Wraps Pass::lookupPassInfo and PassInfo::createPass
*/
@ -74,6 +91,11 @@ const char * LLVMGetPassName(LLVMPassRef passref);
*/
void LLVMAddPass(LLVMPassManagerRef pmref, LLVMPassRef passref);
/*
* Wraps Pass::dump
*/
void LLVMPassDump(LLVMPassRef passref);
/*
* Wraps llvm:InlineAsm::get
*/
@ -176,6 +198,10 @@ LLVMTargetMachineRef LLVMTargetMachineLookup(const char *arch, const char *cpu,
const char *features, int opt,
std::string &error);
LLVMTargetMachineRef LLVMCreateTargetMachine(const char *arch, const char *cpu,
const char *features, int opt,
std::string &error);
/*
* Wraps EngineBuilder::selectTarget
*/

View file

@ -209,6 +209,7 @@ class Pass(llvm.Ownable):
'''
def __init__(self, ptr):
llvm.Ownable.__init__(self, ptr, _core.LLVMDisposePass)
self.__name = ''
@staticmethod
def new(name):
@ -225,6 +226,8 @@ class Pass(llvm.Ownable):
@property
def name(self):
'''The name used in PassRegistry.
'''
return self.__name
@property
@ -302,6 +305,26 @@ class TargetData(Pass):
return _core.LLVMOffsetOfElement(self.ptr, ty.ptr, el)
#===----------------------------------------------------------------------===
# Target Library Info
#===----------------------------------------------------------------------===
class TargetLibraryInfo(Pass):
@staticmethod
def new(triple):
ptr = _core.LLVMCreateTargetLibraryInfo(triple)
return TargetLibraryInfo(ptr)
#===----------------------------------------------------------------------===
# Target Transform Info
#===----------------------------------------------------------------------===
class TargetTransformInfo(Pass):
@staticmethod
def new(targetmachine):
ptr = _core.LLVMCreateTargetTransformInfo(targetmachine.ptr)
return TargetTransformInfo(ptr)
#===----------------------------------------------------------------------===
# Misc.