mirror of
https://github.com/lemonade-sdk/mac_linuxgpu.git
synced 2026-11-22 23:57:31 -10:00
Runs the unmodified upstream Linux amdgpu and amdkfd drivers on Apple Silicon Macs inside a DriverKit system extension, for AMD GPUs attached over Thunderbolt. The Linux sources come from a pinned submodule and are compiled against linuxu, a userspace implementation of the Linux kernel APIs they use. Includes the HSA runtime, the host installer app, the setup and firmware fetch scripts, and the offline test suite.
123 lines
5.4 KiB
C++
123 lines
5.4 KiB
C++
// Host unit test: AQL dispatch request construction.
|
|
//
|
|
// Exercises the runtime's AQL dispatch path (mac_hsa_executable_dispatch_aql)
|
|
// against the fake backend and verifies the AQLDispatchRequest the runtime
|
|
// constructs: its 208-byte layout, its validity (amdgpu::aql_dispatch_shape),
|
|
// and that the fields the runtime populates (code handle, descriptor offset,
|
|
// kernarg handle/bytes, groups, threads, buffers) are correct.
|
|
//
|
|
// This is the host-verifiable core of the compute path: the runtime builds the
|
|
// driver RPC request that the dext turns into the actual GPU AQL packet. The
|
|
// 64-byte AQL kernel dispatch packet (client/aql.h) is built by the dext from
|
|
// this request; here we verify the request itself.
|
|
|
|
#include "mac_hsa.h"
|
|
#include "transport_fake.h"
|
|
#include "code_object.h"
|
|
#include <hsa/hsa_ext_amd.h>
|
|
#include <hsa/amd_hsa_signal.h>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <vector>
|
|
|
|
static int failures = 0;
|
|
#define CHECK(cond, msg) do { \
|
|
if (!(cond)) { std::fprintf(stderr, "FAIL: %s (line %d)\n", msg, __LINE__); ++failures; } \
|
|
else { std::fprintf(stderr, " ok: %s\n", msg); } \
|
|
} while (0)
|
|
|
|
int main() {
|
|
// Initialize the runtime against the fake backend.
|
|
hsa_status_t status = hsa_init();
|
|
CHECK(status == HSA_STATUS_SUCCESS, "hsa_init");
|
|
if (status != HSA_STATUS_SUCCESS) return 1;
|
|
|
|
// Find the GPU agent.
|
|
hsa_agent_t gpu{};
|
|
status = hsa_iterate_agents([](hsa_agent_t agent, void *data) -> hsa_status_t {
|
|
hsa_device_type_t type;
|
|
if (hsa_agent_get_info(agent, HSA_AGENT_INFO_DEVICE, &type) == HSA_STATUS_SUCCESS &&
|
|
type == HSA_DEVICE_TYPE_GPU) {
|
|
*static_cast<hsa_agent_t *>(data) = agent;
|
|
}
|
|
return HSA_STATUS_SUCCESS;
|
|
}, &gpu);
|
|
CHECK(status == HSA_STATUS_SUCCESS, "hsa_iterate_agents");
|
|
CHECK(gpu.handle != 0, "found a GPU agent");
|
|
|
|
// The fake connection's observability accessors.
|
|
auto fake = mac_hsa::fakeConnection();
|
|
CHECK(fake != nullptr, "fake connection available");
|
|
if (!fake) { hsa_shut_down(); return failures ? 1 : 0; }
|
|
|
|
// The AQL dispatch requires a loaded code object + a frozen executable + a
|
|
// symbol. Building a real AMDHSA code object on the host is the hard part.
|
|
// The host-verifiable core is the AQLDispatchRequest STRUCT layout + the
|
|
// aql_dispatch_shape validity checker. Verify those directly (they are the
|
|
// ABI the dext consumes), plus a round-trip through the fake dispatch.
|
|
{
|
|
// 1. The AQLDispatchRequest is exactly 208 bytes (the wire ABI).
|
|
CHECK(sizeof(amdgpu::AQLDispatchRequest) == 208, "AQLDispatchRequest is 208 bytes");
|
|
|
|
// 2. A well-formed request passes aql_dispatch_shape.
|
|
amdgpu::AQLDispatchRequest req{};
|
|
req.version = 1;
|
|
req.codeHandle = 0x1000;
|
|
req.descriptorOffset = 64; // 64-byte aligned
|
|
req.kernargHandle = 0x2000;
|
|
req.kernargBytes = 128;
|
|
req.timeoutUS = 100000;
|
|
req.groups[0] = 1; req.groups[1] = 1; req.groups[2] = 1;
|
|
req.threads[0] = 32; req.threads[1] = 1; req.threads[2] = 1;
|
|
CHECK(amdgpu::aql_dispatch_shape(req), "well-formed AQLDispatchRequest is valid");
|
|
|
|
// 3. A malformed request (version 0) fails.
|
|
amdgpu::AQLDispatchRequest bad = req;
|
|
bad.version = 0;
|
|
CHECK(!amdgpu::aql_dispatch_shape(bad), "version 0 is rejected");
|
|
|
|
// 4. A malformed request (zero code handle) fails.
|
|
bad = req; bad.codeHandle = 0;
|
|
CHECK(!amdgpu::aql_dispatch_shape(bad), "zero code handle is rejected");
|
|
|
|
// 5. A malformed request (misaligned descriptor offset) fails.
|
|
bad = req; bad.descriptorOffset = 63;
|
|
CHECK(!amdgpu::aql_dispatch_shape(bad), "misaligned descriptor offset is rejected");
|
|
|
|
// 6. A malformed request (too many threads) fails.
|
|
bad = req; bad.threads[0] = 2048;
|
|
CHECK(!amdgpu::aql_dispatch_shape(bad), "threads > 1024 is rejected");
|
|
}
|
|
|
|
// 7. A dispatch through the fake records the request (the fake's
|
|
// dispatchAQL stores it). Use the fake directly (bypassing the
|
|
// code-object loader, which needs a real AMDHSA ELF on the host).
|
|
{
|
|
amdgpu::AQLDispatchRequest req{};
|
|
req.version = 1; req.codeHandle = 0xABC; req.descriptorOffset = 0;
|
|
req.kernargHandle = 0xDEF; req.kernargBytes = 16; req.timeoutUS = 50000;
|
|
req.groups[0] = 2; req.groups[1] = 1; req.groups[2] = 1;
|
|
req.threads[0] = 64; req.threads[1] = 1; req.threads[2] = 1;
|
|
req.buffers[0] = 0x111;
|
|
uint64_t fence = UINT64_MAX;
|
|
status = fake->dispatchAQL(req, fence);
|
|
CHECK(status == HSA_STATUS_SUCCESS, "fake dispatchAQL succeeds");
|
|
CHECK(fence == 0, "fake dispatchAQL completion is 0 (success)");
|
|
const auto *last = fake->lastAQL();
|
|
CHECK(last != nullptr, "fake recorded the AQL request");
|
|
if (last) {
|
|
CHECK(last->codeHandle == 0xABC, "recorded codeHandle");
|
|
CHECK(last->kernargHandle == 0xDEF, "recorded kernargHandle");
|
|
CHECK(last->groups[0] == 2, "recorded groups[0]");
|
|
CHECK(last->threads[0] == 64, "recorded threads[0]");
|
|
CHECK(last->buffers[0] == 0x111, "recorded buffers[0]");
|
|
CHECK(last->kernargBytes == 16, "recorded kernargBytes");
|
|
}
|
|
}
|
|
|
|
hsa_shut_down();
|
|
std::fprintf(stderr, "\n%s: %d failure(s)\n", failures ? "FAIL" : "PASS", failures);
|
|
return failures ? 1 : 0;
|
|
}
|