@@ -501,14 +501,13 @@ def __init__(self,
501501 module ,
502502 globalDefinitions ,
503503 globalDefinitionLlvmValues ,
504- function ,
505504 converter ,
506505 builder ,
507506 arg_assignments ,
508507 output_type ,
509- external_function_references
508+ external_function_references ,
509+ compilerCache ,
510510 ):
511- self .function = function
512511
513512 # dict from name to GlobalVariableDefinition
514513 self .globalDefinitions = globalDefinitions
@@ -522,6 +521,7 @@ def __init__(self,
522521 self .external_function_references = external_function_references
523522 self .tags_initialized = {}
524523 self .stack_slots = {}
524+ self .compilerCache = compilerCache
525525
526526 def tags_as (self , new_tags ):
527527 class scoper ():
@@ -648,7 +648,24 @@ def namedCallTargetToLLVM(self, target):
648648 llvmlite .ir .Function (self .module , func_type , target .name )
649649
650650 func = self .external_function_references [target .name ]
651- elif target .name in self .converter ._externallyDefinedFunctionTypes :
651+ elif target .name in self .converter ._function_definitions :
652+ func = self .converter ._functions_by_name [target .name ]
653+
654+ if func .module is not self .module :
655+ # first, see if we'd like to inline this module
656+ if (
657+ self .converter .totalFunctionComplexity (target .name ) < CROSS_MODULE_INLINE_COMPLEXITY
658+ ):
659+ func = self .converter .repeatFunctionInModule (target .name , self .module )
660+ else :
661+ if target .name not in self .external_function_references :
662+ self .external_function_references [target .name ] = \
663+ llvmlite .ir .Function (self .module , func .function_type , func .name )
664+
665+ func = self .external_function_references [target .name ]
666+ else :
667+ # TODO: decide whether to inline based on something in the compiler cache
668+ assert self .compilerCache is not None and self .compilerCache .hasSymbol (target .name )
652669 # this function is defined in a shared object that we've loaded from a prior
653670 # invocation
654671 if target .name not in self .external_function_references :
@@ -665,22 +682,6 @@ def namedCallTargetToLLVM(self, target):
665682 )
666683
667684 func = self .external_function_references [target .name ]
668- else :
669- func = self .converter ._functions_by_name [target .name ]
670-
671- if func .module is not self .module :
672- # first, see if we'd like to inline this module
673- if (
674- self .converter .totalFunctionComplexity (target .name ) < CROSS_MODULE_INLINE_COMPLEXITY
675- and self .converter .canBeInlined (target .name )
676- ):
677- func = self .converter .repeatFunctionInModule (target .name , self .module )
678- else :
679- if target .name not in self .external_function_references :
680- self .external_function_references [target .name ] = \
681- llvmlite .ir .Function (self .module , func .function_type , func .name )
682-
683- func = self .external_function_references [target .name ]
684685
685686 return TypedLLVMValue (
686687 func ,
@@ -1484,16 +1485,12 @@ def define(fname, output, inputs, vararg=False):
14841485
14851486
14861487class Converter :
1487- def __init__ (self ):
1488+ def __init__ (self , compilerCache ):
14881489 object .__init__ (self )
14891490 self ._modules = {}
14901491 self ._functions_by_name = {}
14911492 self ._function_definitions = {}
14921493
1493- # a map from function name to function type for functions that
1494- # are defined in external shared objects and linked in to this one.
1495- self ._externallyDefinedFunctionTypes = {}
1496-
14971494 # total number of instructions in each function, by name
14981495 self ._function_complexity = {}
14991496
@@ -1502,12 +1499,7 @@ def __init__(self):
15021499 self ._printAllNativeCalls = os .getenv ("TP_COMPILER_LOG_NATIVE_CALLS" )
15031500 self .verbose = False
15041501
1505- def markExternal (self , functionNameToType ):
1506- """Provide type signatures for a set of external functions."""
1507- self ._externallyDefinedFunctionTypes .update (functionNameToType )
1508-
1509- def canBeInlined (self , name ):
1510- return name not in self ._externallyDefinedFunctionTypes
1502+ self .compilerCache = compilerCache
15111503
15121504 def totalFunctionComplexity (self , name ):
15131505 """Return the total number of instructions contained in a function.
@@ -1621,19 +1613,19 @@ def add_functions(self, names_to_definitions):
16211613 TypedLLVMValue (func .args [i ], definition .args [i ][1 ])
16221614
16231615 block = func .append_basic_block ('entry' )
1624- builder = llvmlite .ir .IRBuilder (block )
1616+ builder = llvmlite .ir .IRBuilder (block ) # this shares state with func
16251617
16261618 try :
16271619 func_converter = FunctionConverter (
16281620 module ,
16291621 globalDefinitions ,
16301622 globalDefinitionsLlvmValues ,
1631- func ,
16321623 self ,
16331624 builder ,
16341625 arg_assignments ,
16351626 definition .output_type ,
1636- external_function_references
1627+ external_function_references ,
1628+ self .compilerCache ,
16371629 )
16381630
16391631 func_converter .setup ()
0 commit comments