copapy/tests/test_compile_armv7.py

116 lines
3.1 KiB
Python
Raw Normal View History

2025-11-17 08:04:10 +00:00
import os
import subprocess
2025-11-17 08:04:10 +00:00
import warnings
2025-11-17 08:04:10 +00:00
import pytest
import copapy as cp
import copapy.backend as backend
from copapy import NumLike, _binwrite
from copapy.backend import Store, add_read_value_remote, compile_to_dag
if os.name == "nt":
2025-11-17 08:04:10 +00:00
# On Windows wsl and qemu-user is required:
# sudo apt install qemu-user
qemu_command = ["wsl", "qemu-arm"]
2025-11-17 08:04:10 +00:00
else:
qemu_command = ["qemu-arm"]
2025-11-17 08:04:10 +00:00
def run_command(command: list[str]) -> str:
result = subprocess.run(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf8",
check=False,
)
assert result.returncode != 11, (
f"SIGSEGV (segmentation fault)\n -Error occurred: {result.stderr}\n -Output: {result.stdout}"
)
assert result.returncode == 0, (
f"\n -Error occurred: {result.stderr}\n -Output: {result.stdout}"
)
2025-11-17 08:04:10 +00:00
return result.stdout
def check_for_qemu() -> bool:
command = qemu_command + ["--version"]
2025-11-17 08:04:10 +00:00
try:
result = subprocess.run(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False
)
2025-11-17 08:04:10 +00:00
except Exception:
return False
return result.returncode == 0
def function(c1: NumLike, c2: NumLike) -> tuple[NumLike, ...]:
i1 = c1 // 3.3 + 5
i2 = c2 * 5 + c1
r1 = i1 + i2 * 55 / 4
r2 = 4 * i2 + 5
return i1, i2, r1, r2
@pytest.mark.runner
def test_compile():
2025-12-06 17:09:25 +00:00
t1 = cp.vector([10, 11, 12]) + cp.vector(cp.value(v) for v in range(3))
2025-11-23 16:46:18 +00:00
t2 = t1.sum()
2025-11-17 08:04:10 +00:00
2025-12-06 17:09:25 +00:00
t3 = cp.vector(cp.value(1 / (v + 1)) for v in range(3))
2025-11-23 16:46:18 +00:00
t4 = ((t3 * t1) * 2).sum()
t5 = ((t3 * t1) * 2).magnitude()
2025-11-17 08:04:10 +00:00
2025-11-23 16:46:18 +00:00
ret = (t2, t4, t5)
2025-11-17 08:04:10 +00:00
out = [Store(r) for r in ret]
2025-11-17 08:04:10 +00:00
sdb = backend.stencil_db_from_package("armv7")
2025-11-17 08:04:10 +00:00
il, variables = compile_to_dag(out, sdb)
# run program command
il.write_com(_binwrite.Command.RUN_PROG)
# il.write_com(_binwrite.Command.DUMP_CODE)
2025-11-17 08:04:10 +00:00
2025-12-23 16:54:57 +00:00
for v in ret:
assert isinstance(v, cp.value)
add_read_value_remote(il, variables, v.net)
2025-11-17 08:04:10 +00:00
il.write_com(_binwrite.Command.END_COM)
# print('* Data to runner:')
# il.print()
2025-11-17 08:04:10 +00:00
il.to_file("build/runner/test-armv7.copapy")
2025-11-17 08:04:10 +00:00
if not check_for_qemu():
warnings.warn("qemu-armv7 not found, test skipped!", UserWarning)
elif "wsl" in qemu_command:
warnings.warn("qemu-armv7 seams not work on wsl1, test skipped!", UserWarning)
elif not os.path.isfile("build/runner/coparun-armv7"):
2025-11-23 16:46:18 +00:00
warnings.warn("armv7 runner not found, aarch64 test skipped!", UserWarning)
2025-11-17 08:04:10 +00:00
else:
command = [
"build/runner/coparun-armv7",
"build/runner/test-armv7.copapy",
"build/runner/test-armv7.copapy.bin",
]
2025-11-17 08:04:10 +00:00
result = run_command(qemu_command + command)
print("* Output from runner:\n--")
2025-11-17 08:04:10 +00:00
print(result)
print("--")
2025-11-17 08:04:10 +00:00
assert "Return value: 1" in result
2025-11-17 08:04:10 +00:00
# Compare to x86_64 reference results
assert " size=4 data=24 00 00 00" in result
assert " size=4 data=56 55 25 42" in result
assert " size=4 data=B4 F9 C8 41" in result
if __name__ == "__main__":
# test_example()
2025-11-17 08:04:10 +00:00
test_compile()