Each hsa_executable_load_agent_code_object gave its image a VRAM buffer
of its own and wrote it at once. On build 265 a load cost 0.44-0.50 ms
and 6 IOKit calls: the device snapshot (RuntimeBuild and three QueryInfo
tags), BOAlloc (a KFD allocation and its GPU mapping) and an SDMA BOCopy
from the staging buffer. Destroying one cost 0.36 ms and a BOFree. Every
code object held one of the driver's buffer slots: 8192 slots shared by
every client of the GPU, linear-scanned on each buffer call. HRX takes a
second slot per executable for its kernel-args table, so LSE loading one
kernel per executable ran out near 4,000 executables.
Code objects now come from a per-connection code heap (code_heap.h), as
ROCr's loader carves code segments out of its code pools. Its chunks are
4 MiB of VRAM (an image that does not fit gets a chunk of its own),
divided by CodeRangeAllocator: best fit on the device address at the
alignment the image's segments ask for, sizes rounded to 4 KiB so ranges
handed out in turn are adjacent, freed ranges coalesced and reused. A
chunk left empty goes back to the driver, but for one standard chunk
kept for the next loads. A heap that cannot grow fails the load and says
why on stderr.
A load reserves its range, relocates the image to it and stages it in a
4 MiB host-mapped buffer, with no driver call. Before the next doorbell
of the connection's queues, or a driver launch, everything staged is
uploaded by one SDMA copy per run of adjacent ranges, then the one
CodeSync that load owes runs as before. A full staging buffer flushes
on its own, and an image larger than it is written at once after what
was staged before it. Drivers before CodeSync upload and sync each load,
as before. The device snapshot is read once per connection. Address
queries and a new load's overlap check use an address-ordered index.
On the R9700 with build 265's driver, 1000 small code objects load in
0.009 ms each with 0.02 IOKit calls each (was 0.437 ms and 6.2), 262 of
LSE's packed code objects in 3.4 ms (was 130 ms), and destroying 1000
takes 1.2 ms (was 438 ms). 20,000 code objects load in one process and
their kernels run. LSE's 262 executables hold 5 chunks instead of 262
buffers.
test_code_heap: the allocator (alignment, best fit, reuse, coalescing,
20,000 random loads and unloads checked against a model), the heap
against the fake transport (one copy per batch, staging overflow, large
images, reuse before upload, chunk release) and the loader on drivers
with and without CodeSync (250 loads in one chunk, bytes on the device,
address queries, reuse after destroy, a driver launch naming the chunk
and offset).