CIRCT 24.0.0git
Loading...
Searching...
No Matches
test_loopback_cpp.py
Go to the documentation of this file.
1# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2# See https://llvm.org/LICENSE.txt for license information.
3# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5from __future__ import annotations
6
7import os
8from pathlib import Path
9import shutil
10import subprocess
11import sys
12
13# The Trace backend uses ';' as the connection-string separator on Windows.
14_TRACE_SEP = ";" if os.name == "nt" else ":"
15
16import pytest
17
18from esiaccel.cosim.pytest import cosim_test
19
20from .conftest import (HW_DIR, SW_DIR, build_cpp_test, check_lines,
21 get_runtime_root, require_tool, run_probe)
22
23LOOPBACK_PROBES = [
24 ("depth_constant", ["depth: 0x5", "depth_constant ok"]),
25 ("loopback_i8", ["loopback_i8 ok: 0x5a"]),
26 ("struct_func", ["struct_func ok: b=-7 x=-6 y=-7"]),
27 ("odd_struct_func",
28 ["odd_struct_func ok: a=2749 b=-20 p=10 q=-5 r0=4 r1=6"]),
29 ("array_func", ["array_func ok: -3 -2"]),
30 ("serial_coord_translate", ["serial_coord_translate ok"]),
31]
32
33LOOPBACK_TYPED_PROBES = [
34 ("depth_constant", ["depth: 0x5", "depth_constant ok"]),
35 ("loopback_i8", ["loopback_i8 ok: 0x5a"]),
36 ("sint4_loopback", ["sint4_loopback ok: pos=5 neg=-3"]),
37 ("struct_func", ["struct_func ok: b=-7 x=-6 y=-7"]),
38 ("odd_struct_func",
39 ["odd_struct_func ok: a=2749 b=-20 p=10 q=-5 r0=4 r1=6"]),
40 ("array_func", ["array_func ok: -3 -2"]),
41 ("serial_coord_translate", ["serial_coord_translate ok"]),
42]
43
44
45def _build_loopback_codegen(tmp_path: Path, host: str, port: int) -> Path:
46 """Run live-connection codegen and build the loopback_test binary.
47
48 This test deliberately uses ``esiaccel.codegen --platform cosim`` (live
49 connection) rather than ``--file`` (offline manifest) to exercise that
50 codegen path.
51 """
52 require_tool("cmake")
53 require_tool("ninja")
54
55 runtime_root = get_runtime_root()
56
57 include_dir = tmp_path / "include"
58 generated_dir = include_dir / "loopback"
59 generated_dir.mkdir(parents=True, exist_ok=True)
60
61 subprocess.run(
62 [
63 sys.executable,
64 "-m",
65 "esiaccel.codegen",
66 "--platform",
67 "cosim",
68 "--connection",
69 f"{host}:{port}",
70 "--output-dir",
71 str(generated_dir),
72 ],
73 check=True,
74 )
75
76 # Verify generated header content (LOOPBACK-H checks).
77 header_path = generated_dir / "LoopbackIP.h"
78 assert header_path.exists(), "Generated header LoopbackIP.h not found"
79 header_content = header_path.read_text()
80 check_lines(header_content, [
81 "/// Generated header for esi_system module LoopbackIP.",
82 "#pragma once",
83 '#include "types.h"',
84 "namespace esi_system {",
85 "class LoopbackIP {",
86 "static constexpr uint32_t depth = 0x5;",
87 "} // namespace esi_system",
88 ])
89
90 build_dir = tmp_path / "loopback-build"
91 result = subprocess.run(
92 [
93 "cmake",
94 "-G",
95 "Ninja",
96 "-S",
97 str(SW_DIR),
98 "-B",
99 str(build_dir),
100 "-DCMAKE_BUILD_TYPE=Release",
101 f"-DLOOPBACK_GENERATED_DIR={include_dir}",
102 f"-DESI_RUNTIME_ROOT={runtime_root}",
103 ],
104 capture_output=True,
105 text=True,
106 )
107 assert result.returncode == 0, (
108 f"cmake configure failed (rc={result.returncode}):\n"
109 f"--- stdout ---\n{result.stdout}\n"
110 f"--- stderr ---\n{result.stderr}")
111
112 result = subprocess.run(
113 [
114 "cmake", "--build",
115 str(build_dir), "--target", "loopback_test", "--config", "Release"
116 ],
117 capture_output=True,
118 text=True,
119 )
120 assert result.returncode == 0, (
121 f"cmake build failed (rc={result.returncode}):\n"
122 f"--- stdout ---\n{result.stdout}\n"
123 f"--- stderr ---\n{result.stderr}")
124
125 return build_dir / "loopback_test"
126
127
128@cosim_test(HW_DIR / "loopback.py")
130 """Tests for esiquery against the loopback design."""
131
132 def test_esi_version(self, conn) -> None:
133 assert conn.sysinfo().esi_version() == 1
134 assert conn.get_service_mmio().read(0x10) == 1
135 assert conn.manifest().api_version == 1
136
137 def test_loopback_cpp_codegen(self, tmp_path: Path, host: str,
138 port: int) -> None:
139 """Build against live-connection codegen and run all probes."""
140 binary = _build_loopback_codegen(tmp_path, host, port)
141 for probe, expected in LOOPBACK_PROBES:
142 run_probe(binary, host, port, probe, expected)
143
144 def test_loopback_typed_cpp_codegen(self, host: str, port: int,
145 sources_dir: Path) -> None:
146 binary = build_cpp_test(sources_dir, "loopback_typed_test", "loopback")
147 for probe, expected in LOOPBACK_TYPED_PROBES:
148 run_probe(binary, host, port, probe, expected)
149
150 def test_loopback_query_info(self, sources_dir: Path) -> None:
151 """Verify esiquery info output against the generated manifest
152 (QUERY-INFO checks)."""
153 require_tool("esiquery")
154 manifest = sources_dir / "esi_system_manifest.json"
155 assert manifest.exists(), "Manifest not found"
156 result = subprocess.run(
157 ["esiquery", "trace", f"w{_TRACE_SEP}{manifest}", "info"],
158 check=True,
159 capture_output=True,
160 text=True,
161 )
162 check_lines(result.stdout, [
163 "API version: 1",
164 "* Module information",
165 "- LoopbackIP v0.0",
166 "IP which simply echos bytes",
167 "Constants:",
168 "depth: 5",
169 "Extra metadata:",
170 "foo: 1",
171 ])
172
173 def test_loopback_query_hier(self, sources_dir: Path) -> None:
174 """Verify esiquery hier output against the generated manifest
175 (QUERY-HIER checks)."""
176 require_tool("esiquery")
177 manifest = sources_dir / "esi_system_manifest.json"
178 assert manifest.exists(), "Manifest not found"
179 result = subprocess.run(
180 ["esiquery", "trace", f"w{_TRACE_SEP}{manifest}", "hier"],
181 check=True,
182 capture_output=True,
183 text=True,
184 )
185 check_lines(result.stdout, [
186 "* Design hierarchy",
187 "func1: function uint16(uint16)",
188 "structFunc: function ResultStruct(ArgStruct)",
189 "arrayFunc: function ResultArray(sint8[1])",
190 "* Instance: loopback_inst[0]",
191 "loopback_tohw:",
192 "recv: bits8",
193 "loopback_fromhw:",
194 "send: bits8",
195 "mysvc_recv:",
196 "recv: void",
197 "mysvc_send:",
198 "send: void",
199 "* Instance: loopback_inst[1]",
200 ])
None test_loopback_typed_cpp_codegen(self, str host, int port, Path sources_dir)
None test_loopback_cpp_codegen(self, Path tmp_path, str host, int port)
Path _build_loopback_codegen(Path tmp_path, str host, int port)