Allow initialization of other targets.

- Remove PTX/NVPTX targets from default built and init
This commit is contained in:
Siu Kwan Lam 2013-05-03 10:45:02 -05:00
commit a357987597
6 changed files with 48 additions and 67 deletions

View file

@ -2305,28 +2305,3 @@ if api.llvm.InitializeNativeTargetAsmPrinter():
# should user trigger the initialization? # should user trigger the initialization?
raise llvm.LLVMException("No native asm printer!?") raise llvm.LLVMException("No native asm printer!?")
#===----------------------------------------------------------------------===
# Initialization
#===----------------------------------------------------------------------===
HAS_PTX = HAS_NVPTX = False
if True: # use PTX?
try:
api.LLVMInitializePTXTarget()
api.LLVMInitializePTXTargetInfo()
api.LLVMInitializePTXTargetMC()
api.LLVMInitializePTXAsmPrinter()
HAS_PTX = True
except AttributeError:
try:
api.LLVMInitializeNVPTXTarget()
api.LLVMInitializeNVPTXTargetInfo()
api.LLVMInitializeNVPTXTargetMC()
api.LLVMInitializeNVPTXAsmPrinter()
HAS_NVPTX = True
except AttributeError:
pass

View file

@ -222,6 +222,24 @@ class ExecutionEngine(llvm.Wrapper):
# Target machine # Target machine
#===----------------------------------------------------------------------=== #===----------------------------------------------------------------------===
def initialize_target(target, noraise=False):
"""Initialize target by name.
It is safe to initialize the same target multiple times.
"""
prefix = 'LLVMInitialize'
postfixes = ['Target', 'TargetInfo', 'TargetMC', 'AsmPrinter']
try:
for postfix in postfixes:
getattr(api, '%s%s%s' % (prefix, target, postfix))()
except AttributeError:
if noraise:
return False
else:
raise
else:
return True
def print_registered_targets(): def print_registered_targets():
''' '''
Note: print directly to stdout Note: print directly to stdout

View file

@ -636,12 +636,13 @@ class TestTargetMachines(TestCase):
self.assertTrue(le.get_host_cpu_name()) self.assertTrue(le.get_host_cpu_name())
def test_ptx(self): def test_ptx(self):
if lc.HAS_PTX: if le.initialize_target('PTX', noraise=True):
arch = 'ptx64' arch = 'ptx64'
elif lc.HAS_NVPTX: elif le.initialize_target('NVPTX', noraise=True):
arch = 'nvptx64' arch = 'nvptx64'
else: else:
return # skip this test return # skip this test
print(arch) print(arch)
m, func = self._build_module() m, func = self._build_module()
func.calling_convention = lc.CC_PTX_KERNEL # set calling conv func.calling_convention = lc.CC_PTX_KERNEL # set calling conv
@ -650,7 +651,7 @@ class TestTargetMachines(TestCase):
self.assertTrue(ptxtm.cpu) self.assertTrue(ptxtm.cpu)
ptxasm = ptxtm.emit_assembly(m) ptxasm = ptxtm.emit_assembly(m)
self.assertIn('foo', ptxasm) self.assertIn('foo', ptxasm)
if lc.HAS_NVPTX: if arch == 'nvptx64':
self.assertIn('.address_size 64', ptxasm) self.assertIn('.address_size 64', ptxasm)
self.assertIn('sm_20', ptxasm) self.assertIn('sm_20', ptxasm)

View file

@ -841,7 +841,8 @@ class Attr(object):
# #
# Pick-up environ var # Pick-up environ var
# #
PTX_SUPPORT = os.environ.get('LLVMPY_PTX_SUPPORT', '')
TARGETS_BUILT = os.environ.get('LLVM_TARGETS_BUILT', '').split()
def _parse_llvm_version(ver): def _parse_llvm_version(ver):
import re import re

View file

@ -1,8 +1,11 @@
import os
from binding import * from binding import *
from ..namespace import llvm, default from ..namespace import llvm, default
llvm.includes.add('llvm/Support/TargetSelect.h') llvm.includes.add('llvm/Support/TargetSelect.h')
InitializeNativeTarget = llvm.Function('InitializeNativeTarget') InitializeNativeTarget = llvm.Function('InitializeNativeTarget')
InitializeNativeTargetAsmPrinter = llvm.Function( InitializeNativeTargetAsmPrinter = llvm.Function(
'InitializeNativeTargetAsmPrinter', cast(Bool, bool)) 'InitializeNativeTargetAsmPrinter', cast(Bool, bool))
@ -11,20 +14,15 @@ InitializeNativeTargetAsmParser = llvm.Function(
InitializeNativeTargetDisassembler = llvm.Function( InitializeNativeTargetDisassembler = llvm.Function(
'InitializeNativeTargetDisassembler', cast(Bool, bool)) 'InitializeNativeTargetDisassembler', cast(Bool, bool))
#InitializeAllTargets = llvm.Function('InitializeAllTargets') #InitializeAllTargets = llvm.Function('InitializeAllTargets')
#InitializeAllTargetInfos = llvm.Function('InitializeAllTargetInfos') #InitializeAllTargetInfos = llvm.Function('InitializeAllTargetInfos')
#InitializeAllTargetMCs = llvm.Function('InitializeAllTargetMCs') #InitializeAllTargetMCs = llvm.Function('InitializeAllTargetMCs')
#InitializeAllAsmPrinters = llvm.Function('InitializeAllAsmPrinters') #InitializeAllAsmPrinters = llvm.Function('InitializeAllAsmPrinters')
if PTX_SUPPORT == 'PTX': for target in TARGETS_BUILT:
LLVMInitializePTXTarget = default.Function('LLVMInitializePTXTarget') decls = 'Target', 'TargetInfo', 'TargetMC', 'AsmPrinter'
LLVMInitializePTXTargetInfo = default.Function('LLVMInitializePTXTargetInfo') for k in map(lambda x: 'LLVMInitialize%s%s' % (target, x), decls):
LLVMInitializePTXTargetMC = default.Function('LLVMInitializePTXTargetMC') if k == 'LLVMInitializeCppBackendAsmPrinter':
LLVMInitializePTXAsmPrinter = default.Function('LLVMInitializePTXAsmPrinter') continue
globals()[k] = default.Function(k)
if PTX_SUPPORT == 'NVPTX':
LLVMInitializeNVPTXTarget = default.Function('LLVMInitializeNVPTXTarget')
LLVMInitializeNVPTXTargetInfo = default.Function('LLVMInitializeNVPTXTargetInfo')
LLVMInitializeNVPTXTargetMC = default.Function('LLVMInitializeNVPTXTargetMC')
LLVMInitializeNVPTXAsmPrinter = default.Function('LLVMInitializeNVPTXAsmPrinter')

View file

@ -1,3 +1,4 @@
from __future__ import print_function
import os import os
import sys import sys
from subprocess import Popen, PIPE, check_call from subprocess import Popen, PIPE, check_call
@ -37,6 +38,11 @@ def run_llvm_config(extra_args):
llvm_version = run_llvm_config(['--version']) llvm_version = run_llvm_config(['--version'])
print('LLVM version = %r' % llvm_version) print('LLVM version = %r' % llvm_version)
targets_built = run_llvm_config(['--targets-built'])
include_targets = set(os.environ.get('LLVMPY_TARGETS', '').split())
targets_built = ' '.join(set(targets_built.split()) & include_targets)
print('LLVM targets = %s' % targets_built)
def get_libs_and_objs(components): def get_libs_and_objs(components):
parts = run_llvm_config(['--libs'] + components).split() parts = run_llvm_config(['--libs'] + components).split()
libs = [] libs = []
@ -97,36 +103,20 @@ def determine_to_use_dynlink(libdir, llvm_version):
dynlink = determine_to_use_dynlink(libdir, llvm_version) dynlink = determine_to_use_dynlink(libdir, llvm_version)
ptx_support = ''
if dynlink: if dynlink:
print('Using dynamic linking') print('Using dynamic linking')
libs_core = ['LLVM-%s' % llvm_version] libs_core = ['LLVM-%s' % llvm_version]
objs_core = [] objs_core = []
else: else:
extra_components = set()
for tm in map(lambda s: s.lower(), targets_built.split()):
postfixes = ['', 'asmprinter', 'codegen', 'desc', 'info']
for postfix in postfixes:
extra_components.add(tm + postfix)
enabled_components = set(get_enabled_components()) enabled_components = set(get_enabled_components())
ptx_components = set(['ptx', extra_components = list(extra_components & enabled_components)
'ptxasmprinter',
'ptxcodegen',
'ptxdesc',
'ptxinfo'])
nvptx_components = set(['nvptx',
'nvptxasmprinter',
'nvptxcodegen',
'nvptxdesc',
'nvptxinfo'])
extra_components = []
if (nvptx_components & enabled_components) == nvptx_components:
print("Using NVPTX")
extra_components.extend(nvptx_components)
ptx_support = 'NVPTX'
elif (ptx_components & enabled_components) == ptx_components:
print("Using PTX")
extra_components.extend(ptx_components)
ptx_support = 'PTX'
else:
print("No CUDA support")
libs_core, objs_core = get_libs_and_objs( libs_core, objs_core = get_libs_and_objs(
['core', 'analysis', 'scalaropts', 'executionengine', ['core', 'analysis', 'scalaropts', 'executionengine',
@ -136,15 +126,13 @@ else:
+ extra_components) + extra_components)
if sys.platform == 'win32': if sys.platform == 'win32':
# If no PTX lib got added, disable PTX in the build pass
if 'LLVMPTXCodeGen' in libs_core:
ptx_support = 'ptx'
else: else:
macros.append(('_GNU_SOURCE', None)) macros.append(('_GNU_SOURCE', None))
# auto generate bindings # auto generate bindings
os.environ['LLVMPY_PTX_SUPPORT'] = ptx_support
os.environ['LLVMPY_LLVM_VERSION'] = llvm_version os.environ['LLVMPY_LLVM_VERSION'] = llvm_version
os.environ['LLVM_TARGETS_BUILT'] = targets_built
check_call([sys.executable, 'llvmpy/build.py']) check_call([sys.executable, 'llvmpy/build.py'])
# generate shared objects # generate shared objects