Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 79 additions & 29 deletions src/asahi/libagx/copy.cl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* SPDX-License-Identifier: MIT
*/
#include "compiler/libcl/libcl.h"
#include "m5_as_metadata.h"

KERNEL(32)
libagx_fill(global uint32_t *address, uint32_t value)
Expand Down Expand Up @@ -469,21 +470,43 @@ libagx_fill_uint4(global uint4 *address, uint layer_stride_uint4, uint a,
* metadata has a zero size and occupies bytes 0..191. Every other offset is
* relative to the base and instance records reference bottom-level images by
* address, so a word copy that rewrites the base and ID words relocates an
* image.
* image. Driver metadata immediately follows the native image.
*
* The serialized form is the Vulkan header (driver UUID twice, serialized
* size, deserialized size, handle count), one bottom-level handle per
* instance in instance-index order, then the image.
* instance in instance-index order, then the native image with its driver
* metadata, followed by the retained IR and plan.
*/
#define AGX3_AS_EMPTY_BYTES 192
#define AGX3_AS_SERIAL_WORDS 7
#define AGX3_AS_SLACK 1023
#define AGX3_AS_INSTANCE_RECORD 26

static uint64_t
agx3_as_bytes(global uint64_t *image)
{
return image[9] ? image[9] : AGX3_AS_EMPTY_BYTES;
return image[9] ? image[9] : LIBAGX_M5_AS_EMPTY_IMAGE_BYTES;
}

static struct libagx_m5_as_metadata
agx3_as_metadata(global uint64_t *image, uint64_t image_bytes)
{
return *(global struct libagx_m5_as_metadata *)(image + image_bytes / 8);
}

static uint64_t
agx3_as_storage_bytes(uint64_t image_and_metadata_bytes,
struct libagx_m5_as_metadata metadata)
{
return max(image_and_metadata_bytes + AGX3_AS_SLACK,
metadata.retained_offset + metadata.retained_bytes);
}

static uint64_t
agx3_as_serial_bytes(uint32_t handles, uint64_t image_and_metadata_bytes,
struct libagx_m5_as_metadata metadata)
{
return (AGX3_AS_SERIAL_WORDS + (uint64_t)handles) * 8 +
image_and_metadata_bytes + metadata.retained_bytes;
}

static uint32_t
Expand Down Expand Up @@ -520,8 +543,9 @@ static void
agx3_as_copy_words(global uint64_t *dst, global uint64_t *src, uint32_t chunk,
uint64_t bytes, uint32_t dst_id, bool relocate)
{
for (uint32_t w = chunk * 2; w < chunk * 2 + 2; ++w) {
if ((uint64_t)w * 8 >= bytes)
uint64_t first = (uint64_t)chunk * 2;
for (uint64_t w = first; w < first + 2; ++w) {
if (w * 8 >= bytes)
return;
uint64_t value = src[w];
if (relocate && w == 18)
Expand All @@ -534,26 +558,37 @@ agx3_as_copy_words(global uint64_t *dst, global uint64_t *src, uint32_t chunk,

KERNEL(32)
libagx_agx3_as_copy(global uint64_t *dst, global uint64_t *src,
global uint64_t *dst_base, global uint64_t *src_base,
uint32_t dst_id)
{
agx3_as_copy_words(dst, src, cl_global_id.x, agx3_as_bytes(src), dst_id,
src[9] != 0);
uint32_t i = cl_global_id.x;
uint64_t bytes = agx3_as_bytes(src);
struct libagx_m5_as_metadata metadata = agx3_as_metadata(src, bytes);
agx3_as_copy_words(dst, src, i, bytes + LIBAGX_M5_AS_METADATA_BYTES,
dst_id, src[9] != 0);
if ((uint64_t)i * 16 < metadata.retained_bytes) {
uint64_t offset = metadata.retained_offset / 8;
agx3_as_copy_words(dst_base + offset, src_base + offset, i,
metadata.retained_bytes, 0, false);
}
}

KERNEL(32)
libagx_agx3_as_serialize(global uint64_t *dst, global uint64_t *src,
uint32_t uuid0, uint32_t uuid1, uint32_t uuid2,
uint32_t uuid3)
global uint64_t *src_base, uint32_t uuid0,
uint32_t uuid1, uint32_t uuid2, uint32_t uuid3)
{
uint32_t i = cl_global_id.x;
uint64_t bytes = agx3_as_bytes(src);
struct libagx_m5_as_metadata metadata = agx3_as_metadata(src, bytes);
uint64_t image_and_metadata_bytes = bytes + LIBAGX_M5_AS_METADATA_BYTES;
uint32_t handles = agx3_as_instance_slots(src);

if (i == 0) {
dst[0] = dst[2] = uuid0 | ((uint64_t)uuid1 << 32);
dst[1] = dst[3] = uuid2 | ((uint64_t)uuid3 << 32);
dst[4] = AGX3_AS_SERIAL_WORDS * 8 + (uint64_t)handles * 8 + bytes;
dst[5] = bytes + AGX3_AS_SLACK;
dst[4] = agx3_as_serial_bytes(handles, image_and_metadata_bytes, metadata);
dst[5] = agx3_as_storage_bytes(image_and_metadata_bytes, metadata);
dst[6] = handles;
}

Expand All @@ -562,19 +597,29 @@ libagx_agx3_as_serialize(global uint64_t *dst, global uint64_t *src,
dst[AGX3_AS_SERIAL_WORDS + i] = record ? record[11] : 0;
}

agx3_as_copy_words(dst + AGX3_AS_SERIAL_WORDS + handles, src, i, bytes, 0,
false);
global uint64_t *image = dst + AGX3_AS_SERIAL_WORDS + handles;
agx3_as_copy_words(image, src, i, image_and_metadata_bytes, 0, false);
if ((uint64_t)i * 16 < metadata.retained_bytes)
agx3_as_copy_words(image + image_and_metadata_bytes / 8,
src_base + metadata.retained_offset / 8,
i, metadata.retained_bytes, 0, false);
}

KERNEL(32)
libagx_agx3_as_deserialize(global uint64_t *dst, global uint64_t *src,
uint32_t dst_id)
global uint64_t *dst_base, uint32_t dst_id)
{
uint64_t handles = src[6];
global uint64_t *image = src + AGX3_AS_SERIAL_WORDS + handles;
uint64_t bytes = src[4] - AGX3_AS_SERIAL_WORDS * 8 - handles * 8;
agx3_as_copy_words(dst, image, cl_global_id.x, bytes, dst_id,
image[9] != 0);
uint32_t i = cl_global_id.x;
global uint64_t *image = src + AGX3_AS_SERIAL_WORDS + src[6];
uint64_t bytes = agx3_as_bytes(image);
struct libagx_m5_as_metadata metadata = agx3_as_metadata(image, bytes);
uint64_t image_and_metadata_bytes = bytes + LIBAGX_M5_AS_METADATA_BYTES;
agx3_as_copy_words(dst, image, i, image_and_metadata_bytes,
dst_id, image[9] != 0);
if ((uint64_t)i * 16 < metadata.retained_bytes)
agx3_as_copy_words(dst_base + metadata.retained_offset / 8,
image + image_and_metadata_bytes / 8,
i, metadata.retained_bytes, 0, false);
}

/* After deserialization, point each instance at the bottom-level handle the
Expand All @@ -594,11 +639,12 @@ libagx_agx3_as_relink(global uint64_t *dst, global uint64_t *src,
if (!record)
return;

record[11] = blas;
uint64_t native_blas = (blas + 1023ul) & ~1023ul;
record[11] = native_blas;
if (blas) {
global uint32_t *words = (global uint32_t *)record;
for (uint32_t id = 1; id <= table_count; ++id) {
if (table[2 * id] == blas) {
if (table[2 * id] == native_blas) {
words[1] = id;
break;
}
Expand All @@ -611,12 +657,16 @@ KERNEL(1)
libagx_agx3_as_property(global uint64_t *image, global uint64_t *report,
uint32_t kind)
{
uint64_t bytes = agx3_as_bytes(image);
uint64_t handles = agx3_as_instance_slots(image);
if (kind == 2)
uint32_t handles = agx3_as_instance_slots(image);
if (kind == 2) {
*report = handles;
else if (kind == 1)
*report = AGX3_AS_SERIAL_WORDS * 8 + handles * 8 + bytes;
else
*report = bytes + AGX3_AS_SLACK;
return;
}

uint64_t bytes = agx3_as_bytes(image);
struct libagx_m5_as_metadata metadata = agx3_as_metadata(image, bytes);
uint64_t image_and_metadata_bytes = bytes + LIBAGX_M5_AS_METADATA_BYTES;
*report = kind == 1
? agx3_as_serial_bytes(handles, image_and_metadata_bytes, metadata)
: agx3_as_storage_bytes(image_and_metadata_bytes, metadata);
}
23 changes: 23 additions & 0 deletions src/asahi/libagx/m5_as_metadata.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* Copyright 2026 Aurora Silicon
* SPDX-License-Identifier: MIT
*/
#ifndef LIBAGX_M5_AS_METADATA_H
#define LIBAGX_M5_AS_METADATA_H

#ifndef VULKAN
#include <stdint.h>
#endif

#define LIBAGX_M5_AS_EMPTY_IMAGE_BYTES 192u
#define LIBAGX_M5_AS_METADATA_BYTES 16u

/* Driver metadata immediately after the native image. The retained span
* starts after the reserved native image and metadata, at an 8-byte-aligned
* offset from the Vulkan AS base. A zero length means no retained IR/plan.
*/
struct libagx_m5_as_metadata {
uint64_t retained_offset;
uint64_t retained_bytes;
};

#endif
Loading
Loading