Skip to content

Commit 4b768d5

Browse files
author
William Grant
committed
inspection methods on runtime
1 parent 2039e7a commit 4b768d5

File tree

2 files changed

+104
-18
lines changed

2 files changed

+104
-18
lines changed

typed_python/compiler/native_ast_to_llvm.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,7 +1498,6 @@ def __init__(self):
14981498
self._inlineRequests = []
14991499

15001500
self._printAllNativeCalls = os.getenv("TP_COMPILER_LOG_NATIVE_CALLS")
1501-
self._native_ast_str = ""
15021501
self.verbose = False
15031502

15041503
def markExternal(self, functionNameToType):
@@ -1576,28 +1575,30 @@ def add_functions(self, names_to_definitions):
15761575
)
15771576
func_type = llvmlite.ir.FunctionType(
15781577
type_to_llvm_type(function.output_type),
1579-
[type_to_llvm_type(x[1]) for x in function.args],
1580-
)
1581-
self._functions_by_name[name] = llvmlite.ir.Function(
1582-
module, func_type, name
1578+
[type_to_llvm_type(x[1]) for x in function.args]
15831579
)
1580+
self._functions_by_name[name] = llvmlite.ir.Function(module, func_type, name)
15841581

1585-
self._functions_by_name[name].linkage = "external"
1582+
self._functions_by_name[name].linkage = 'external'
15861583
self._function_definitions[name] = function
15871584

1588-
self._native_ast_str += "\n"
1589-
self._native_ast_str += "*************\n"
1590-
self._native_ast_str += "def %s(%s): #->%s\n" % (
1591-
name,
1592-
",".join(["%s=%s" % (k, str(t)) for k, t in function.args]),
1593-
str(function.output_type),
1594-
)
1595-
self._native_ast_str += native_ast.indent(str(function.body.body)) + "\n"
1596-
self._native_ast_str += "*************\n"
1597-
self._native_ast_str += "\n"
1598-
15991585
if self.verbose:
1600-
print(self._native_ast_str)
1586+
for name in names_to_definitions:
1587+
definition = names_to_definitions[name]
1588+
func = self._functions_by_name[name]
1589+
1590+
print()
1591+
print("*************")
1592+
print(
1593+
"def %s(%s): #->%s" % (
1594+
name,
1595+
",".join(["%s=%s" % (k, str(t)) for k, t in definition.args]),
1596+
str(definition.output_type)
1597+
)
1598+
)
1599+
print(native_ast.indent(str(definition.body.body)))
1600+
print("*************")
1601+
print()
16011602

16021603
globalDefinitions = {}
16031604
globalDefinitionsLlvmValues = {}

typed_python/compiler/runtime.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,3 +465,88 @@ def Compiled(pyFunc):
465465
f.resultTypeFor(*types)
466466

467467
return f
468+
469+
470+
def getNativeIRCode(typedFunc: Function) -> str:
471+
"""
472+
Given a function compiled with Entrypoint, return a text representation
473+
of the generated native (one layer prior to LLVM) code.
474+
475+
Args:
476+
typedFunc (Function): a decorated python function.
477+
478+
Returns:
479+
A string for the function bodies generated (including constructors and destructors)
480+
"""
481+
converter = Runtime.singleton().llvm_compiler.converter
482+
function_name = typedFunc.__name__
483+
# relies on us maintaining our naming conventions (tests would break otherwise)
484+
output_str = ""
485+
for key, value in converter._function_definitions.items():
486+
if function_name in key:
487+
output_str += f'Function {key}' + '_'*20 + '\n'
488+
output_str += str(value.body.body) + '\n'
489+
output_str += "_"*80 + '\n'
490+
491+
if not output_str:
492+
raise ValueError('no matching function definitions found - has the code been compiled (and run)?')
493+
494+
return output_str
495+
496+
497+
def getNativeIRString(typedFunc: Function) -> str:
498+
"""
499+
Given a function compiled with Entrypoint, return a text representation
500+
of the generated native (one layer prior to LLVM) code.
501+
502+
Args:
503+
typedFunc (Function): a decorated python function.
504+
505+
Returns:
506+
A string for the function bodies generated (including constructors and destructors)
507+
"""
508+
converter = Runtime.singleton().llvm_compiler.converter
509+
function_name = typedFunc.__name__
510+
# relies on us maintaining our naming conventions (tests would break otherwise)
511+
output_str = ""
512+
for key, value in converter._function_definitions.items():
513+
if function_name in key:
514+
output_str += f"Function {key}" + "_" * 20 + "\n"
515+
output_str += str(value.body.body) + "\n"
516+
output_str += "_" * 80 + "\n"
517+
518+
if not output_str:
519+
raise ValueError(
520+
"no matching function definitions found - has the code been compiled (and run)?"
521+
)
522+
523+
return output_str
524+
525+
526+
def getLLVMString(typedFunc: Function) -> str:
527+
"""
528+
Given a function compiled with Entrypoint, return a text representation
529+
of the generated LLVM code.
530+
531+
Args:
532+
typedFunc (Function): a decorated python function.
533+
534+
Returns:
535+
A string for the function bodies generated (including constructors and destructors)
536+
"""
537+
converter = Runtime.singleton().llvm_compiler.converter
538+
function_name = typedFunc.__name__
539+
# relies on us maintaining our naming conventions (tests would break otherwise)
540+
output_str = ""
541+
for key, value in converter._functions_by_name.items():
542+
if function_name in key:
543+
output_str += f"Function {key}" + "_" * 20 + "\n"
544+
output_str += str(value) + "\n"
545+
output_str += "_" * 80 + "\n"
546+
547+
if not output_str:
548+
raise ValueError(
549+
"no matching function definitions found - has the code been compiled (and run)?"
550+
)
551+
552+
return output_str

0 commit comments

Comments
 (0)