2020-06-19 15:30:34 +08:00
|
|
|
# RUN: %PYTHON %s
|
|
|
|
|
2020-11-14 10:06:59 +08:00
|
|
|
from npcomp.compiler.numpy.backend import iree
|
|
|
|
from npcomp.compiler.numpy.frontend import *
|
|
|
|
from npcomp.compiler.numpy import test_config
|
|
|
|
from npcomp.compiler.numpy.target import *
|
|
|
|
from npcomp.compiler.utils import logging
|
2020-06-19 15:30:34 +08:00
|
|
|
|
|
|
|
# TODO: This should all exist in a high level API somewhere.
|
|
|
|
from _npcomp import mlir
|
|
|
|
|
2020-06-20 08:17:22 +08:00
|
|
|
|
|
|
|
logging.enable()
|
2020-06-19 15:30:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
def compile_function(f):
|
2020-06-29 07:52:25 +08:00
|
|
|
fe = ImportFrontend(config=test_config.create_test_config(
|
|
|
|
target_factory=GenericTarget32))
|
2020-06-20 08:17:22 +08:00
|
|
|
fe.import_global_function(f)
|
|
|
|
compiler = iree.CompilerBackend()
|
|
|
|
vm_blob = compiler.compile(fe.ir_module)
|
|
|
|
loaded_m = compiler.load(vm_blob)
|
|
|
|
return loaded_m[f.__name__]
|
2020-06-19 15:30:34 +08:00
|
|
|
|
|
|
|
|
2020-06-20 08:17:22 +08:00
|
|
|
@compile_function
|
2020-06-19 15:30:34 +08:00
|
|
|
def int_add(a: int, b: int):
|
|
|
|
return a + b
|
|
|
|
|
2020-06-20 08:17:22 +08:00
|
|
|
result = int_add(5, 6)
|
|
|
|
assert result == 11
|
2020-06-20 08:25:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
@compile_function
|
|
|
|
def simple_control_flow(a: int, b: int):
|
|
|
|
return (a * b) and (a - b)
|
|
|
|
|
|
|
|
assert simple_control_flow(5, 6) == -1
|
|
|
|
assert simple_control_flow(-1, 0) == 0
|