<s> from __future__ import print_function , division , absolute_import <EOL> import functools <EOL> import locale <EOL> import weakref <EOL> import llvmlite . llvmpy . core as lc <EOL> import llvmlite . llvmpy . passes as lp <EOL> import llvmlite . binding as ll <EOL> import llvmlite . ir as llvmir <EOL> from numba import config , utils <EOL> from numba . runtime . atomicops import remove_redundant_nrt_refct <EOL> _x86arch = frozenset ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def _is_x86 ( triple ) : <EOL> arch = triple . split ( '<STR_LIT:->' ) [ <NUM_LIT:0> ] <EOL> return arch in _x86arch <EOL> def dump ( header , body ) : <EOL> print ( header . center ( <NUM_LIT> , '<STR_LIT:->' ) ) <EOL> print ( body ) <EOL> print ( '<STR_LIT:=>' * <NUM_LIT> ) <EOL> class CodeLibrary ( object ) : <EOL> """<STR_LIT>""" <EOL> _finalized = False <EOL> _object_caching_enabled = False <EOL> def __init__ ( self , codegen , name ) : <EOL> self . _codegen = codegen <EOL> self . _name = name <EOL> self . _linking_libraries = set ( ) <EOL> self . _final_module = ll . parse_assembly ( <EOL> str ( self . _codegen . _create_empty_module ( self . _name ) ) ) <EOL> self . _final_module . name = self . _name <EOL> self . _final_module . __library = weakref . proxy ( self ) <EOL> self . _shared_module = None <EOL> @ property <EOL> def codegen ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _codegen <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . _name , id ( self ) ) <EOL> def _raise_if_finalized ( self ) : <EOL> if self . _finalized : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> % ( self , ) ) <EOL> def _ensure_finalized ( self ) : <EOL> if not self . _finalized : <EOL> self . finalize ( ) <EOL> def _optimize_functions ( self , ll_module ) : <EOL> """<STR_LIT>""" <EOL> ll_module . data_layout = self . _codegen . _data_layout <EOL> with self . _codegen . _function_pass_manager ( ll_module ) as fpm : <EOL> for func in ll_module . functions : <EOL> fpm . initialize ( ) <EOL> fpm . run ( func ) <EOL> fpm . finalize ( ) <EOL> def _optimize_final_module ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _codegen . _mpm . run ( self . _final_module ) <EOL> def _get_module_for_linking ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _ensure_finalized ( ) <EOL> if self . _shared_module is not None : <EOL> return self . _shared_module <EOL> mod = self . _final_module <EOL> to_fix = [ ] <EOL> nfuncs = <NUM_LIT:0> <EOL> for fn in mod . functions : <EOL> nfuncs += <NUM_LIT:1> <EOL> if not fn . is_declaration and fn . linkage == ll . Linkage . external : <EOL> to_fix . append ( fn . name ) <EOL> if nfuncs == <NUM_LIT:0> : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( self , ) ) <EOL> if to_fix : <EOL> mod = mod . clone ( ) <EOL> for name in to_fix : <EOL> mod . get_function ( name ) . linkage = '<STR_LIT>' <EOL> self . _shared_module = mod <EOL> return mod <EOL> def create_ir_module ( self , name ) : <EOL> """<STR_LIT>""" <EOL> self . _raise_if_finalized ( ) <EOL> ir_module = self . _codegen . _create_empty_module ( name ) <EOL> return ir_module <EOL> def add_linking_library ( self , library ) : <EOL> """<STR_LIT>""" <EOL> library . _ensure_finalized ( ) <EOL> self . _linking_libraries . add ( library ) <EOL> def add_ir_module ( self , ir_module ) : <EOL> """<STR_LIT>""" <EOL> self . _raise_if_finalized ( ) <EOL> assert isinstance ( ir_module , llvmir . Module ) <EOL> ll_module = ll . parse_assembly ( str ( ir_module ) ) <EOL> ll_module . name = ir_module . name <EOL> ll_module . verify ( ) <EOL> self . add_llvm_module ( ll_module ) <EOL> def add_llvm_module ( self , ll_module ) : <EOL> self . _optimize_functions ( ll_module ) <EOL> ll_module = remove_redundant_nrt_refct ( ll_module ) <EOL> self . _final_module . link_in ( ll_module ) <EOL> def finalize ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _codegen . _check_llvm_bugs ( ) <EOL> self . _raise_if_finalized ( ) <EOL> if config . DUMP_FUNC_OPT : <EOL> dump ( "<STR_LIT>" % self . _name , self . get_llvm_str ( ) ) <EOL> for library in self . _linking_libraries : <EOL> self . _final_module . link_in ( <EOL> library . _get_module_for_linking ( ) , preserve = True ) <EOL> for library in self . _codegen . _libraries : <EOL> self . _final_module . link_in ( <EOL> library . _get_module_for_linking ( ) , preserve = True ) <EOL> self . _optimize_final_module ( ) <EOL> self . _final_module . verify ( ) <EOL> self . _finalize_final_module ( ) <EOL> def _finalize_final_module ( self ) : <EOL> """<STR_LIT>""" <EOL> cleanup = self . _codegen . _add_module ( self . _final_module ) <EOL> if cleanup : <EOL> utils . finalize ( self , cleanup ) <EOL> self . _finalize_specific ( ) <EOL> self . _finalized = True <EOL> if config . DUMP_OPTIMIZED : <EOL> dump ( "<STR_LIT>" % self . _name , self . get_llvm_str ( ) ) <EOL> if config . DUMP_ASSEMBLY : <EOL> asm = self . get_asm_str ( ) <EOL> if asm : <EOL> dump ( "<STR_LIT>" % self . _name , self . get_asm_str ( ) ) <EOL> def get_defined_functions ( self ) : <EOL> """<STR_LIT>""" <EOL> mod = self . _final_module <EOL> for fn in mod . functions : <EOL> if not fn . is_declaration : <EOL> yield fn <EOL> def get_function ( self , name ) : <EOL> return self . _final_module . get_function ( name ) <EOL> def get_llvm_str ( self ) : <EOL> """<STR_LIT>""" <EOL> return str ( self . _final_module ) <EOL> def get_asm_str ( self ) : <EOL> """<STR_LIT>""" <EOL> return str ( self . _codegen . _tm . emit_assembly ( self . _final_module ) ) <EOL> def enable_object_caching ( self ) : <EOL> self . _object_caching_enabled = True <EOL> self . _compiled_object = None <EOL> self . _compiled = False <EOL> def _get_compiled_object ( self ) : <EOL> if not self . _object_caching_enabled : <EOL> raise ValueError ( "<STR_LIT>" % ( self , ) ) <EOL> if self . _compiled_object is None : <EOL> raise RuntimeError ( "<STR_LIT>" % ( self , ) ) <EOL> return self . _compiled_object <EOL> def _set_compiled_object ( self , value ) : <EOL> if not self . _object_caching_enabled : <EOL> raise ValueError ( "<STR_LIT>" % ( self , ) ) <EOL> if self . _compiled : <EOL> raise ValueError ( "<STR_LIT>" % ( self , ) ) <EOL> self . _compiled_object = value <EOL> @ classmethod <EOL> def _dump_elf ( cls , buf ) : <EOL> """<STR_LIT>""" <EOL> from elftools . elf . elffile import ELFFile <EOL> from elftools . elf import descriptions <EOL> from io import BytesIO <EOL> f = ELFFile ( BytesIO ( buf ) ) <EOL> print ( "<STR_LIT>" ) <EOL> for sec in f . iter_sections ( ) : <EOL> if sec [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> symbols = sorted ( sec . iter_symbols ( ) , key = lambda sym : sym . name ) <EOL> print ( "<STR_LIT>" ) <EOL> for sym in symbols : <EOL> if not sym . name : <EOL> continue <EOL> print ( "<STR_LIT>" <EOL> % ( sym . name . decode ( ) , <EOL> sym [ '<STR_LIT>' ] , <EOL> sym [ '<STR_LIT>' ] , <EOL> descriptions . describe_symbol_type ( sym [ '<STR_LIT>' ] [ '<STR_LIT:type>' ] ) , <EOL> descriptions . describe_symbol_bind ( sym [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , <EOL> ) ) <EOL> print ( ) <EOL> @ classmethod <EOL> def _object_compiled_hook ( cls , ll_module , buf ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self = ll_module . __library <EOL> except AttributeError : <EOL> return <EOL> if self . _object_caching_enabled : <EOL> self . _compiled = True <EOL> self . _compiled_object = buf <EOL> @ classmethod <EOL> def _object_getbuffer_hook ( cls , ll_module ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self = ll_module . __library <EOL> except AttributeError : <EOL> return <EOL> if self . _object_caching_enabled and self . _compiled_object : <EOL> buf = self . _compiled_object <EOL> self . _compiled_object = None <EOL> return buf <EOL> def serialize_using_bitcode ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _ensure_finalized ( ) <EOL> return ( self . _name , '<STR_LIT>' , self . _final_module . as_bitcode ( ) ) <EOL> def serialize_using_object_code ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _ensure_finalized ( ) <EOL> data = ( self . _get_compiled_object ( ) , <EOL> self . _get_module_for_linking ( ) . as_bitcode ( ) ) <EOL> return ( self . _name , '<STR_LIT:object>' , data ) <EOL> @ classmethod <EOL> def _unserialize ( cls , codegen , state ) : <EOL> name , kind , data = state <EOL> self = codegen . create_library ( name ) <EOL> assert isinstance ( self , cls ) <EOL> if kind == '<STR_LIT>' : <EOL> self . _final_module = ll . parse_bitcode ( data ) <EOL> self . _finalize_final_module ( ) <EOL> return self <EOL> elif kind == '<STR_LIT:object>' : <EOL> object_code , shared_bitcode = data <EOL> self . enable_object_caching ( ) <EOL> self . _set_compiled_object ( object_code ) <EOL> self . _shared_module = ll . parse_bitcode ( shared_bitcode ) <EOL> self . _finalize_final_module ( ) <EOL> return self <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % ( kind , ) ) <EOL> class AOTCodeLibrary ( CodeLibrary ) : <EOL> def emit_native_object ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _ensure_finalized ( ) <EOL> return self . _codegen . _tm . emit_object ( self . _final_module ) <EOL> def emit_bitcode ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _ensure_finalized ( ) <EOL> return self . _final_module . as_bitcode ( ) <EOL> def _finalize_specific ( self ) : <EOL> pass <EOL> class JITCodeLibrary ( CodeLibrary ) : <EOL> def get_pointer_to_function ( self , name ) : <EOL> """<STR_LIT>""" <EOL> self . _ensure_finalized ( ) <EOL> return self . _codegen . _engine . get_function_address ( name ) <EOL> def _finalize_specific ( self ) : <EOL> self . _codegen . _engine . finalize_object ( ) <EOL> class BaseCPUCodegen ( object ) : <EOL> _llvm_initialized = False <EOL> def __init__ ( self , module_name ) : <EOL> initialize_llvm ( ) <EOL> self . _libraries = set ( ) <EOL> self . _data_layout = None <EOL> self . _llvm_module = ll . parse_assembly ( <EOL> str ( self . _create_empty_module ( module_name ) ) ) <EOL> self . _llvm_module . name = "<STR_LIT>" <EOL> self . _init ( self . _llvm_module ) <EOL> def _init ( self , llvm_module ) : <EOL> assert list ( llvm_module . global_variables ) == [ ] , "<STR_LIT>" <EOL> target = ll . Target . from_triple ( ll . get_process_triple ( ) ) <EOL> tm_options = dict ( opt = config . OPT ) <EOL> self . _tm_features = self . _customize_tm_features ( ) <EOL> self . _customize_tm_options ( tm_options ) <EOL> tm = target . create_target_machine ( ** tm_options ) <EOL> engine = ll . create_mcjit_compiler ( llvm_module , tm ) <EOL> self . _tm = tm <EOL> self . _engine = engine <EOL> self . _target_data = engine . target_data <EOL> self . _data_layout = str ( self . _target_data ) <EOL> self . _mpm = self . _module_pass_manager ( ) <EOL> self . _engine . set_object_cache ( self . _library_class . _object_compiled_hook , <EOL> self . _library_class . _object_getbuffer_hook ) <EOL> def _create_empty_module ( self , name ) : <EOL> ir_module = lc . Module ( name ) <EOL> ir_module . triple = ll . get_process_triple ( ) <EOL> if self . _data_layout : <EOL> ir_module . data_layout = self . _data_layout <EOL> return ir_module <EOL> @ property <EOL> def target_data ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _target_data <EOL> def add_linking_library ( self , library ) : <EOL> """<STR_LIT>""" <EOL> library . _ensure_finalized ( ) <EOL> self . _libraries . add ( library ) <EOL> def create_library ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . _library_class ( self , name ) <EOL> def unserialize_library ( self , serialized ) : <EOL> return self . _library_class . _unserialize ( self , serialized ) <EOL> def _module_pass_manager ( self ) : <EOL> pm = ll . create_module_pass_manager ( ) <EOL> dl = ll . create_target_data ( self . _data_layout ) <EOL> dl . add_pass ( pm ) <EOL> self . _tm . add_analysis_passes ( pm ) <EOL> with self . _pass_manager_builder ( ) as pmb : <EOL> pmb . populate ( pm ) <EOL> return pm <EOL> def _function_pass_manager ( self , llvm_module ) : <EOL> pm = ll . create_function_pass_manager ( llvm_module ) <EOL> self . _target_data . add_pass ( pm ) <EOL> self . _tm . add_analysis_passes ( pm ) <EOL> with self . _pass_manager_builder ( ) as pmb : <EOL> pmb . populate ( pm ) <EOL> return pm <EOL> def _pass_manager_builder ( self ) : <EOL> """<STR_LIT>""" <EOL> pmb = lp . create_pass_manager_builder ( <EOL> opt = config . OPT , loop_vectorize = config . LOOP_VECTORIZE ) <EOL> return pmb <EOL> def _check_llvm_bugs ( self ) : <EOL> """<STR_LIT>""" <EOL> ir = """<STR_LIT>""" <EOL> mod = ll . parse_assembly ( ir ) <EOL> ir_out = str ( mod ) <EOL> if "<STR_LIT>" in ir_out or "<STR_LIT>" in ir_out : <EOL> return <EOL> if "<STR_LIT:1.0>" in ir_out : <EOL> loc = locale . getlocale ( ) <EOL> raise RuntimeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( loc , ) ) <EOL> raise AssertionError ( "<STR_LIT>" % ( ir_out , ) ) <EOL> def magic_tuple ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . _llvm_module . triple , ll . get_host_cpu_name ( ) , <EOL> self . _tm_features ) <EOL> class AOTCPUCodegen ( BaseCPUCodegen ) : <EOL> """<STR_LIT>""" <EOL> _library_class = AOTCodeLibrary <EOL> def __init__ ( self , module_name , cpu_name = None ) : <EOL> self . _cpu_name = cpu_name or '<STR_LIT>' <EOL> BaseCPUCodegen . __init__ ( self , module_name ) <EOL> def _customize_tm_options ( self , options ) : <EOL> cpu_name = self . _cpu_name <EOL> if cpu_name == '<STR_LIT:host>' : <EOL> cpu_name = ll . get_host_cpu_name ( ) <EOL> options [ '<STR_LIT>' ] = cpu_name <EOL> options [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> options [ '<STR_LIT>' ] = '<STR_LIT:default>' <EOL> options [ '<STR_LIT>' ] = self . _tm_features <EOL> def _customize_tm_features ( self ) : <EOL> return '<STR_LIT>' <EOL> def _add_module ( self , module ) : <EOL> pass <EOL> class JITCPUCodegen ( BaseCPUCodegen ) : <EOL> """<STR_LIT>""" <EOL> _library_class = JITCodeLibrary <EOL> def _customize_tm_options ( self , options ) : <EOL> options [ '<STR_LIT>' ] = ll . get_host_cpu_name ( ) <EOL> options [ '<STR_LIT>' ] = '<STR_LIT:default>' <EOL> options [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> options [ '<STR_LIT>' ] = self . _tm_features <EOL> options [ '<STR_LIT>' ] = True <EOL> def _customize_tm_features ( self ) : <EOL> features = ll . get_host_cpu_features ( ) <EOL> if not config . ENABLE_AVX : <EOL> for k in features : <EOL> if k . startswith ( '<STR_LIT>' ) : <EOL> features [ k ] = False <EOL> return features . flatten ( ) <EOL> def _add_module ( self , module ) : <EOL> self . _engine . add_module ( module ) <EOL> return functools . partial ( self . _engine . remove_module , module ) <EOL> _llvm_initialized = False <EOL> def initialize_llvm ( ) : <EOL> global _llvm_initialized <EOL> if not _llvm_initialized : <EOL> ll . initialize ( ) <EOL> ll . initialize_native_target ( ) <EOL> ll . initialize_native_asmprinter ( ) <EOL> _llvm_initialized = True </s>
