Body-only kernels
A body-only kernel splits a host-launched kernel at its boundary. Waveflow generates the part that is fully determined by what you declare; you write everything else as ordinary code:
| Generated by Waveflow | Written by you |
|---|---|
the kernel’s prototype and every #pragma HLS INTERFACE |
the whole kernel body, in C++ (<kernel>_<body>_impl.tpp) |
| a register map whose offsets match what Vitis assigns | a pure, bit-exact Python model of the kernel |
| one call from the top into your body, with every argument | the module’s Python body for pysim: a port wrapper plus, optionally, a timing model |
| the schemas’ C++ headers and serializers | the test scenarios, their expected responses, and a C++ testbench |
It is the right default when you want the traditional flow – a Vitis kernel, a bit-exact Python model, and the kernel’s timing and resources from synthesis and co-simulation – without writing the kernel’s control flow in a restricted Python dialect. The worked example is Streaming polynomial.
Declaring one
Name the body with cpp_body on a HostActivated. The module declares ports and a
register map, and writes no on_start:
@dataclass
class PolyAccel(HostActivated):
cpp_kernel_name: ClassVar[str | None] = "poly"
cpp_namespace: ClassVar[str | None] = "poly_impl"
cpp_body: ClassVar[str | None] = "body"
def __post_init__(self) -> None:
super().__post_init__()
self.s_in = StreamIFSlave( name=..., sim=self.sim, bitwidth=self.in_bw)
self.m_out = StreamIFMaster(name=..., sim=self.sim, bitwidth=self.out_bw)
self.regmap = VitisRegMap({"halted": ..., "error": ..., "tx_id": ...}) # status only
...
def body(self) -> ProcessGen[None]:
... # the kernel for pysim: ordinary Python, never translated
What is generated
The top is the pragmas and one call. Every kernel argument is forwarded, in the
kernel’s own order – streams, register fields, then any m_axi pointers:
void poly(hls::stream<streamutils::axi4s_word<32>>& s_in,
hls::stream<streamutils::axi4s_word<32>>& m_out,
ap_uint<1>& halted, ap_uint<8>& error, ap_uint<16>& tx_id) {
#pragma HLS INTERFACE axis port=s_in
#pragma HLS INTERFACE axis port=m_out
#pragma HLS INTERFACE s_axilite port=halted bundle=control
...
poly_impl::body(s_in, m_out, halted, error, tx_id);
}
The header declares the body with the same parameters. The register fields are
references, so the body writes status directly – halted = 1; – exactly as a
hand-written Vitis kernel would.
Several word widths need no second module. The body is a template on the streams’
width parameters, and param_supports asks for one generated top per variant:
param_supports: ClassVar[dict] = {"bw64": {"in_bw": 64, "out_bw": 64}}
gives poly (32-bit) and poly_bw64 (64-bit) in the same gen/poly.cpp, both calling the
same body. Synthesize or simulate a variant by naming it as the Vitis top
(set_top poly_bw64).
Generating the boundary is worth it because it is easy to get subtly wrong by hand: a misspelled pragma often still compiles, into a different interface, and the register map must agree with the offsets the host uses.
Writing the body
The generator writes <kernel>_<body>_impl.tpp once, as a stub marked
TODO: implement <body>, and never overwrites it afterwards. Three rules:
- Keep
#pragma HLS INLINE. The interface pragmas are on the generated top; a body that is not inlined is a separate function whose ports may not bind to them. - Pack words only with the generated serializers (
read_axi4_stream,read_axi4_stream_lane, …), never by hand. - For floating point, write each multiply and add as its own statement if the Python model is to match bit for bit: an expression the compiler can fuse into a multiply-add rounds once instead of twice.
The Python model
Write the model as a pure function plus a thin port wrapper:
- the pure function (
poly_eval,poly_stream_modelin the example) is bit-exact and has no simulator in it. It is what a system simulation calls, and what the C++ is checked against; - the module’s Python body reads and writes the ports and calls the pure function.
This is what pysim runs. To model timing, add it here (
proc_latency,proc_iiin the example); the pure function never changes.
In pysim, a body-only module’s on_start is supplied for you: it runs the body.
Testing: one stimulus, every implementation
Python writes each scenario’s stimulus once, as a burst bundle, with
write_bursts([StreamBurst(words, tlast=...), ...], path). A burst with tlast=False
is how a scenario sends a malformed transaction. Every implementation reads the same
files:
- the pure model reads them with
read_bursts; - the C++ testbench plays them into the kernel with
wf::play_stream<W>(dir, s_in)and records the response withwf::record_stream<W>(m_out, dir), both from the generatedinclude/bundle_tb.h; - the pysim testbench writes them into the module’s input port.
Check every implementation against expected responses computed from each scenario’s intent, not against each other: two implementations written by the same person, or the same AI, can agree on the same mistake. On the wire TLAST is the only boundary, so a recorded stream splits at TLAST and nowhere else.
The C++ testbench is ordinary C++, so it can loop over transactions and scenarios freely. This is the main reason to prefer it over a generated testbench, whose body must be straight-line.
When to use something else
- The kernel’s control loop can be expressed in the extractor’s vocabulary, and only
the datapath needs C++: keep an extracted
on_startand attach the datapath as a stream, block or complex hook. - A free-running kernel (
FreeRunMod):cpp_bodyis forHostActivatedtoday. - A later input depends on an earlier output (closed-loop control): the stimulus cannot be written ahead of time, so you need the free-running flow’s concurrent BFM testbench instead.