From 3b399f9d8bf7e272fb0d3f7822e30f01ddc74895 Mon Sep 17 00:00:00 2001 From: Siu Kwan Lam Date: Wed, 2 Jan 2013 17:00:59 -0600 Subject: [PATCH] Adding target specific passes. --- llvm/_core.cpp | 43 ++++++++++++++++++++++++++++++++++++++++--- llvm/ee.py | 13 +++++++++++++ llvm/extra.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ llvm/extra.h | 26 ++++++++++++++++++++++++++ llvm/passes.py | 23 +++++++++++++++++++++++ 5 files changed, 149 insertions(+), 3 deletions(-) diff --git a/llvm/_core.cpp b/llvm/_core.cpp index 0b53138..b3fb298 100644 --- a/llvm/_core.cpp +++ b/llvm/_core.cpp @@ -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(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 } diff --git a/llvm/ee.py b/llvm/ee.py index ebe5e87..8469138 100644 --- a/llvm/ee.py +++ b/llvm/ee.py @@ -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 diff --git a/llvm/extra.cpp b/llvm/extra.cpp index e58220d..10d090a 100644 --- a/llvm/extra.cpp +++ b/llvm/extra.cpp @@ -75,6 +75,7 @@ #include "llvm/Support/SourceMgr.h" #include #include +#include #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) { diff --git a/llvm/extra.h b/llvm/extra.h index 49a75d1..3f2a413 100644 --- a/llvm/extra.h +++ b/llvm/extra.h @@ -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 */ diff --git a/llvm/passes.py b/llvm/passes.py index 71de28d..106b86d 100644 --- a/llvm/passes.py +++ b/llvm/passes.py @@ -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.