Skip to content

Commit

Permalink
add qualifier to filter based on specific arg types
Browse files Browse the repository at this point in the history
  • Loading branch information
William Grant committed Sep 28, 2022
1 parent de7cca1 commit f995abc
Showing 1 changed file with 45 additions and 5 deletions.
50 changes: 45 additions & 5 deletions typed_python/compiler/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,19 +393,25 @@ def resultTypeForCall(self, funcObj, argTypes, kwargTypes):
return _resultTypeCache[key]

@staticmethod
def getNativeIRString(typedFunc: Function) -> str:
def getNativeIRString(typedFunc: Function, *args, **kwargs) -> str:
"""
Given a function compiled with Entrypoint, return a text representation
of the generated native (one layer prior to LLVM) code.
Args:
typedFunc (Function): a decorated python function.
*args (Optional): these optional args should be the Types of the functions' positional arguments
**kwargs (Optional): these keyword args should be the Types of the functions' keyword arguments
Returns:
A string for the function bodies generated (including constructors and destructors)
"""
converter = Runtime.singleton().llvm_compiler.converter
function_name = typedFunc.__name__

if args or kwargs:
function_name = getFullFunctionNameWithArgs(typedFunc, args, kwargs)
else:
function_name = typedFunc.__name__
# relies on us maintaining our naming conventions (tests would break otherwise)
output_str = ""
for key, value in converter._function_definitions.items():
Expand All @@ -422,20 +428,25 @@ def getNativeIRString(typedFunc: Function) -> str:
return output_str

@staticmethod
def getLLVMString(typedFunc: Function) -> str:
def getLLVMString(typedFunc: Function, *args, **kwargs) -> str:
"""
Given a function compiled with Entrypoint, return a text representation
of the generated LLVM code.
Args:
typedFunc (Function): a decorated python function.
*args (Optional): these optional args should be the Types of the functions' positional arguments
**kwargs (Optional): these keyword args should be the Types of the functions' keyword arguments
Returns:
A string for the function bodies generated (including constructors and destructors)
"""
converter = Runtime.singleton().llvm_compiler.converter
function_name = typedFunc.__name__
# relies on us maintaining our naming conventions (tests would break otherwise)

if args or kwargs:
function_name = getFullFunctionNameWithArgs(typedFunc, args, kwargs)
else:
function_name = typedFunc.__name__ # relies on us maintaining our naming conventions (tests would break otherwise)
output_str = ""
for key, value in converter._functions_by_name.items():
if function_name in key:
Expand All @@ -451,6 +462,35 @@ def getLLVMString(typedFunc: Function) -> str:
return output_str


def getFullFunctionNameWithArgs(funcObj, argTypes, kwargTypes):
"""
Given a Function and a set of types, compile the function to generate the unique name
for that function+argument combination.
Args:
funcObj (Function): a typed_python Function.
argTypes (List): a list of the position arguments for the function.
kwargTypes (Dict): a key:value mapping for the functions' keywords arguments.
"""
assert isinstance(funcObj, typed_python._types.Function)
typeWrapper = lambda t: python_to_native_converter.typedPythonTypeToTypeWrapper(t)
funcObj = _types.prepareArgumentToBePassedToCompiler(funcObj)
argTypes = [typeWrapper(a) for a in argTypes]
kwargTypes = {k: typeWrapper(v) for k, v in kwargTypes.items()}

overload_index = 0
overload = funcObj.overloads[overload_index]

ExpressionConversionContext = typed_python.compiler.expression_conversion_context.ExpressionConversionContext
argumentSignature = ExpressionConversionContext.computeFunctionArgumentTypeSignature(overload, argTypes, kwargTypes)

if argumentSignature is not None:
callTarget = Runtime().singleton().compileFunctionOverload(funcObj, overload_index, argumentSignature, argumentsAreTypes=True)
else:
raise ValueError('no signature found.')
return callTarget.name


def NotCompiled(pyFunc, returnTypeOverride=None):
"""Decorate 'pyFunc' to prevent it from being compiled.
Expand Down

0 comments on commit f995abc

Please sign in to comment.