Enable PTX backend

This commit is contained in:
Siu Kwan Lam 2012-08-14 15:05:22 -07:00
commit f85713a736
7 changed files with 176 additions and 20 deletions

View file

@ -888,16 +888,14 @@ _wrap_obj2none(LLVMDisposePassManager, LLVMPassManagerRef)
_wrap_none2str(LLVMDumpPasses)
_wrap_objstr2obj(LLVMAddPassByName, LLVMPassManagerRef, int)
static PyObject *
_wLLVMInitializePasses(PyObject * self, PyObject * args)
{
if (!PyArg_ParseTuple(args, ""))
return NULL;
LLVMInitializePasses();
Py_RETURN_NONE;
}
_wrap_none2none(LLVMInitializePasses)
_wrap_none2obj(LLVMInitializeNativeTarget, int)
_wrap_none2obj(LLVMInitializeNativeTargetAsmPrinter, int)
_wrap_none2none(LLVMInitializePTXTarget)
_wrap_none2none(LLVMInitializePTXTargetInfo)
_wrap_none2none(LLVMInitializePTXAsmPrinter)
/*===----------------------------------------------------------------------===*/
@ -1030,6 +1028,16 @@ _wLLVMTargetMachineEmitFile(PyObject * self, PyObject * args)
return ret;
}
_wrap_obj2obj(LLVMTargetMachineGetTargetData, LLVMTargetMachineRef,
LLVMTargetDataRef)
_wrap_obj2str(LLVMTargetMachineGetTargetName, LLVMTargetMachineRef)
_wrap_obj2str(LLVMTargetMachineGetTargetShortDescription, LLVMTargetMachineRef)
_wrap_obj2str(LLVMTargetMachineGetTriple, LLVMTargetMachineRef)
_wrap_obj2str(LLVMTargetMachineGetCPU, LLVMTargetMachineRef)
_wrap_obj2str(LLVMTargetMachineGetFS, LLVMTargetMachineRef)
_wrap_none2none(LLVMPrintRegisteredTargetsForVersion)
/*===----------------------------------------------------------------------===*/
/* Target Data */
/*===----------------------------------------------------------------------===*/
@ -1781,7 +1789,12 @@ static PyMethodDef core_methods[] = {
_method( LLVMDumpPasses )
_method( LLVMAddPassByName )
_method( LLVMInitializePasses )
_method( LLVMInitializeNativeTarget )
_method( LLVMInitializeNativeTargetAsmPrinter )
_method( LLVMInitializePTXTarget )
_method( LLVMInitializePTXTargetInfo )
_method( LLVMInitializePTXAsmPrinter )
/* Passes */
@ -1876,6 +1889,13 @@ static PyMethodDef core_methods[] = {
_method( LLVMTargetMachineFromEngineBuilder )
_method( LLVMDisposeTargetMachine )
_method( LLVMTargetMachineEmitFile )
_method( LLVMTargetMachineGetTargetData )
_method( LLVMTargetMachineGetTargetName )
_method( LLVMTargetMachineGetTargetShortDescription )
_method( LLVMTargetMachineGetTriple )
_method( LLVMTargetMachineGetCPU )
_method( LLVMTargetMachineGetFS )
_method( LLVMPrintRegisteredTargetsForVersion )
/* Target Data */
_method( LLVMCreateTargetData )

View file

@ -2143,5 +2143,12 @@ def inline_function(call):
#===----------------------------------------------------------------------===
if _core.LLVMInitializeNativeTarget():
raise llvm.LLVMException("No native target!?")
if _core.LLVMInitializeNativeTargetAsmPrinter():
# should this be an optional feature?
# should user trigger the initialization?
raise llvm.LLVMException("No native asm printer!?")
if True: # use PTX
_core.LLVMInitializePTXTarget()
_core.LLVMInitializePTXTargetInfo()
_core.LLVMInitializePTXAsmPrinter()

View file

@ -231,6 +231,12 @@ class EngineBuilder(object):
raise llvm.LLVMException(ret)
return ExecutionEngine(ret, self._module)
def select_target(self):
'''get the corresponding target machine
'''
ptr = _core.LLVMTargetMachineFromEngineBuilder(self.ptr)
return TargetMachine(ptr)
#===----------------------------------------------------------------------===
# Execution engine
#===----------------------------------------------------------------------===
@ -298,13 +304,13 @@ class ExecutionEngine(object):
# Target machine
#===----------------------------------------------------------------------===
def print_registered_targets():
'''
Note: print directly to stdout
'''
_core.LLVMPrintRegisteredTargetsForVersion()
class TargetMachine(object):
@staticmethod
def from_engine_builder(eb):
'''Construct a TargetMachine from an EngineBuilder
'''
ptr = _core.LLVMTargetMachineFromEngineBuilder(eb.ptr)
return TargetMachine(ptr)
def __init__(self, ptr):
self.ptr = ptr
@ -321,3 +327,30 @@ class TargetMachine(object):
'''returns byte string of the module as assembly code of the target machine
'''
return _core.LLVMTargetMachineEmitFile(self.ptr, module.ptr, False)
@property
def target_data(self):
'''get target data of this machine
'''
ptr = _core.LLVMTargetMachineGetTargetData(self.ptr)
return TargetData(ptr)
@property
def target_name(self):
return _core.LLVMTargetMachineGetTargetName(self.ptr)
@property
def target_short_description(self):
return _core.LLVMTargetMachineGetTargetShortDescription(self.ptr)
@property
def triple(self):
return _core.LLVMTargetMachineGetTriple(self.ptr)
@property
def cpu(self):
return _core.LLVMTargetMachineGetCPU(self.ptr)
@property
def feature_string(self):
return _core.LLVMTargetMachineGetFS(self.ptr)

View file

@ -54,6 +54,7 @@
#include "llvm/Support/CallSite.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/IntrinsicInst.h"
@ -114,6 +115,11 @@ char *do_print(W obj)
return strdup(buf.str().c_str());
}
int LLVMInitializeNativeTargetAsmPrinter()
{
return llvm::InitializeNativeTargetAsmPrinter();
}
LLVMTargetMachineRef LLVMTargetMachineFromEngineBuilder(LLVMEngineBuilderRef eb)
{
@ -134,8 +140,6 @@ unsigned char* LLVMTargetMachineEmitFile(LLVMTargetMachineRef tmref,
using namespace llvm;
assert(lenp);
InitializeNativeTargetAsmPrinter();
Module *modulep = unwrap(modref);
assert(modulep);
@ -190,6 +194,43 @@ unsigned char* LLVMTargetMachineEmitFile(LLVMTargetMachineRef tmref,
return bytes;
}
const char* LLVMTargetMachineGetTargetName(LLVMTargetMachineRef tm)
{
return strdup(llvm::unwrap(tm)->getTarget().getName());
}
const char* LLVMTargetMachineGetTargetShortDescription(LLVMTargetMachineRef tm)
{
return strdup(llvm::unwrap(tm)->getTarget().getShortDescription());
}
const char* LLVMTargetMachineGetTriple(LLVMTargetMachineRef tm)
{
return strdup(llvm::unwrap(tm)->getTargetTriple().str().c_str());
}
const char* LLVMTargetMachineGetCPU(LLVMTargetMachineRef tm)
{
return strdup(llvm::unwrap(tm)->getTargetCPU().str().c_str());
}
const char* LLVMTargetMachineGetFS(LLVMTargetMachineRef tm)
{
return strdup(llvm::unwrap(tm)->getTargetFeatureString().str().c_str());
}
void LLVMPrintRegisteredTargetsForVersion(){
llvm::TargetRegistry::printRegisteredTargetsForVersion();
}
LLVMTargetDataRef LLVMTargetMachineGetTargetData(LLVMTargetMachineRef tm)
{
using namespace llvm;
return wrap(new TargetData(*unwrap(tm)->getTargetData()));
}
unsigned char* LLVMGetNativeCodeFromModule(LLVMModuleRef module, int assembly,
unsigned * lenp)
{
@ -205,7 +246,6 @@ unsigned char* LLVMGetNativeCodeFromModule(LLVMModuleRef module, int assembly,
return LLVMTargetMachineEmitFile(wrap(tm), module, assembly, lenp);
}
static
llvm::AtomicOrdering atomic_ordering_from_string(const char * ordering)
{

View file

@ -45,6 +45,9 @@
extern "C" {
#endif
int LLVMInitializeNativeTargetAsmPrinter();
/*
* Wraps EngineBuilder::selectTarget
*/
@ -55,10 +58,48 @@ LLVMTargetMachineRef LLVMTargetMachineFromEngineBuilder(LLVMEngineBuilderRef eb)
*/
void LLVMDisposeTargetMachine(LLVMTargetMachineRef tm);
/*
* Wraps TargetMachine::addPassesToEmitFile
*/
unsigned char* LLVMTargetMachineEmitFile(LLVMTargetMachineRef tmref,
LLVMModuleRef modref,
int assembly, unsigned * lenp);
/*
* Wraps TargetMachine::getTargetData
*/
LLVMTargetDataRef LLVMTargetMachineGetTargetData(LLVMTargetMachineRef tm);
/*
* Wraps TargetMachine::getTarget().getName()
*/
const char* LLVMTargetMachineGetTargetName(LLVMTargetMachineRef tm);
/*
* Wraps TargetMachine::getTarget().getShortDescription()
*/
const char* LLVMTargetMachineGetTargetShortDescription(LLVMTargetMachineRef tm);
/*
* Wraps TargetMachine::getTargetTriple
*/
const char * LLVMTargetMachineGetTriple(LLVMTargetMachineRef tm);
/*
* Wraps TargetMachine::getTargetCPU
*/
const char * LLVMTargetMachineGetCPU(LLVMTargetMachineRef tm);
/*
* Wraps TargetMachine::getTargetFeatureString
*/
const char * LLVMTargetMachineGetFS(LLVMTargetMachineRef tm);
/*
* Wraps TargetRegister::printRegisteredTargetsForVersion
*/
void LLVMPrintRegisteredTargetsForVersion();
/*
* Wraps TargetMachine::addPassesToEmitFile
*/

View file

@ -146,6 +146,17 @@ PyObject *make_list_from_LLVMValueRef_array(LLVMValueRef *p, unsigned n);
******************************************************************************
*****************************************************************************/
#define _wrap_none2none(func) \
static PyObject * \
_w ## func(PyObject * self, PyObject * args) \
{ \
if (!PyArg_ParseTuple(args, "")) \
return NULL; \
func(); \
Py_RETURN_NONE; \
}
/**
* Wrap LLVM functions of the type
* outtype func(intype1 arg1)

View file

@ -78,11 +78,15 @@ def call_setup(llvm_config):
incdir = _run(llvm_config + ' --includedir')
libdir = _run(llvm_config + ' --libdir')
ldflags = _run(llvm_config + ' --ldflags')
ptx_components = ['ptx', 'ptxasmprinter', 'ptxcodegen', 'ptxdesc', 'ptxinfo']
libs_core, objs_core = get_libs_and_objs(llvm_config,
['core', 'analysis', 'scalaropts', 'executionengine',
'jit', 'native', 'interpreter', 'bitreader', 'bitwriter',
'instrumentation', 'ipa', 'ipo', 'transformutils',
'asmparser', 'linker', 'support'])
'asmparser', 'linker', 'support', 'vectorize']
+ ptx_components)
std_libs = [ 'pthread', 'm', 'stdc++' ]
extra_link_args = ["-fPIC"]
@ -91,7 +95,7 @@ def call_setup(llvm_config):
if "darwin" in sys.platform:
std_libs.append("ffi")
extra_link_args += ['-framework', 'Python']
ext_core = Extension(
'llvm._core',