Add first version of mglib library#46
Conversation
There was a problem hiding this comment.
Pull request overview
This PR introduces a new lightweight mglib static library that provides event-driven, non-blocking wrapper APIs for iDMA and RedMulE, and integrates it into the build and test system. It also adds per-tile-aliased scratch storage support (.tile_bss) in the linker script and ensures it is zero-initialized at startup for correct per-tile state in multi-tile execution.
Changes:
- Add
mglib(mg_event / mg_idma / mg_redmule) wrappers and build integration. - Add per-tile
.tile_bssreservation in the linker script and clear it incrt0. - Add a new
test_mm_os_mglibtest and integrate it into the mesh test suite.
Reviewed changes
Copilot reviewed 13 out of 14 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| CMakeLists.txt | Adds mglib as a top-level subdirectory for the build. |
| mglib/CMakeLists.txt | Defines the mglib static library target, includes, and LTO-related compile flags. |
| mglib/include/mg_event.h | Adds a small generic event record + 8-bit wrapping sequence comparison helper. |
| mglib/include/mg_idma.h | Declares event-driven iDMA memcpy wrappers and wait routine. |
| mglib/include/mg_redmule.h | Declares event-driven RedMulE GEMM wrapper and wait routine. |
| mglib/src/mg_idma.c | Implements per-tile (tile-aliased) iDMA issue/completion bookkeeping and wait logic. |
| mglib/src/mg_redmule.c | Implements RedMulE job issue/wait bookkeeping using per-tile state. |
| targets/magia_v2/link.ld | Pins entrypoint to _start and adds fixed-size .tile_bss reservation in per-tile memory. |
| targets/magia_v2/src/crt0.S | Clears .tile_bss on every hart during startup. |
| tests/magia/mesh/CMakeLists.txt | Adds mm_os_mglib to the mesh test suite. |
| tests/magia/mesh/mm_os_mglib/CMakeLists.txt | Adds the new test_mm_os_mglib executable, LTO link options, and links against mglib. |
| tests/magia/mesh/mm_os_mglib/src/test.c | New matrix-multiply test variant that drives iDMA/RedMulE through mglib. |
| scripts/gvsoc2perfetto-rs/.gitignore | Ignores Rust /target build artifacts. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (((tile_w_max * x_id) + tile_w_max) > N_SIZE) { | ||
| tile_w = N_SIZE - (tile_w_max * x_id); | ||
| } else { |
| if (i != 0) { | ||
| mg_idma_wait(&eu_ctrl, 1, WAIT_MODE, &idma_evt_y); | ||
| } |
| mg_idma_memcpy_2d(&idma_ctrl, | ||
| &eu_ctrl, | ||
| WAIT_MODE, | ||
| 1, | ||
| axi_addr_y, | ||
| obi_addr_y, | ||
| len_y, | ||
| std_y, | ||
| reps_y, | ||
| &idma_evt_y, | ||
| NULL); | ||
| } |
| // consumed while waiting on a later id - in which case we must not | ||
| // wait on the hardware at all. | ||
| while (!mg_seq_ge(mg_idma_completed[idx], target)) { | ||
| // dnot yet the right one: spin back into the hardware wait. |
| static uint8_t mg_redmule_completed __attribute__((section(".tile_bss"))); | ||
|
|
||
| void mg_redmule_gemm(redmule_controller_t *ctrl, | ||
| eu_controller_t *eu, | ||
| eu_wait_mode_t mode, | ||
| uint32_t x, | ||
| uint32_t w, | ||
| uint32_t y, | ||
| uint16_t m, | ||
| uint16_t n, | ||
| uint16_t k, | ||
| mg_event_t *event, | ||
| mg_event_callback_t callback) | ||
| { | ||
| int32_t id; | ||
| while ((id = redmule_acquire(ctrl)) < 0) { | ||
| // Hardware queue (depth 2) is full: drain one completion pulse to | ||
| // free a slot before retrying the acquire. | ||
| if (eu_redmule_wait(eu, mode)) { | ||
| mg_redmule_completed++; | ||
| } | ||
| } | ||
| mg_event_init(event, id, callback); | ||
| redmule_gemm(ctrl, x, w, y, m, n, k); | ||
| } |
| * The event id is the real hardware-issued job id, obtained by reading the | ||
| * ACQUIRE register (redmule_api.acquire()). If the hardware's job queue | ||
| * (depth 2) is already full, this call blocks - draining completion pulses | ||
| * through `eu`/`mode`, exactly like mg_redmule_wait() does - until a slot | ||
| * frees and a fresh id can be acquired. |
d7854ce to
11dcb35
Compare
11dcb35 to
cba1088
Compare
|
RTL breaks because the |
This pull request introduces a new lightweight library,

mglib, providing thin, event-driven wrappers for iDMA and RedMulE, and integrates it into the build and test system. It also adds robust per-tile memory allocation and initialization support in the linker script and startup code, ensuring correct operation in multi-tile environments. Additionally, a new test is added to validate the new functionality.These changes provide a foundation for event-driven accelerator programming with per-tile state, targeting high utilization.
Compared to the HAL-based
test_mm_os_2, which was used as a reference, themglib-driventest_mm_os_mglibprovides a significant performance improvement by reducing the amount of time the tile spends without computing with RedMulE.With a sufficiently large workload,
mglibenables full RedMulE utilization, e.g. (test_mm_os_mglibwithM=N=K=256, 8 timeslots, 2x2 mesh size):Note: correct GVSOC behavior relies on this PR for
gvsoc-pulp: gvsoc/gvsoc-pulp#91 , which has been merged in upstreamgvsocin gvsoc/gvsoc#276Library and Event-Driven Accelerator Wrappers
mglibstatic library, which provides event-based, non-blocking wrappers for iDMA and RedMulE accelerators, including per-tile completion bookkeeping and callback support. (CMakeLists.txt,mglib/CMakeLists.txt,mglib/include/mg_event.h,mglib/include/mg_idma.h,mglib/include/mg_redmule.h,mglib/src/mg_idma.c,mglib/src/mg_redmule.c) [1] [2] [3] [4] [5] [6] [7]Per-Tile Memory Management
.tile_bsssection for per-tile-aliased scratch memory, reserved above the stack, and ensured it is zeroed by every hart in the startup code. This supports safe per-tile static data for the new library. (targets/magia_v2/link.ld,targets/magia_v2/src/crt0.S) [1] [2] [3]Testing and Build Integration
test_mm_os_mglib, undertests/magia/mesh, which exercises the new mglib wrappers and links against the new library with LTO and appropriate options for symbol resolution. (tests/magia/mesh/CMakeLists.txt,tests/magia/mesh/mm_os_mglib/CMakeLists.txt) [1] [2]Build and Tooling Quality-of-Life
/targetto the.gitignorefor the Rust-basedgvsoc2perfettotool to prevent build artifacts from being committed. (scripts/gvsoc2perfetto-rs/.gitignore)Other Linker Improvements
_startin the linker script to ensure correct boot behavior when using LTO. (targets/magia_v2/link.ld)Limitations
idmamodel currently used in GVSOC does not support any kind of hardware job queue. As a consequence, queueing is implemented entirely in software, which means that DMA jobs cannot be offloaded back-to-back.mglibAPI is in draft state, and there are likely some misunderstandings in how to divide between thehal,drivers, andmgliblayers.