Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
5 changes: 3 additions & 2 deletions src/asahi/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ endif
if with_tools.contains('drm-shim') and with_asahi_vk and with_tests
# Shader-object binaries: an exported binary imports and exports again
# unchanged; one whose root prefix or root metadata was edited and resealed
# is refused. M5 and G17P binaries carry the trimmed root prefix.
# is refused. All tested chips carry the trimmed root prefix.
foreach chip : [['g17s', ['--expect-root-size']],
['g17p', ['--expect-root-size']], ['g13g', []]]
['g17p', ['--expect-root-size']],
['g13g', ['--expect-root-size']]]
test(
'hk-' + chip[0] + '-shader-binary-root',
prog_python,
Expand Down
12 changes: 7 additions & 5 deletions src/asahi/vulkan/hk_agx3_vdm_decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ extern "C" {
* when compute shaders carry packed vec4 alternates, and to 50 when they
* carry compute-pair state in a third variant byte. AGX3 moves to 51 and
* classic to 39 when compute-pair binaries also carry the producer state
* needed to compile fused variants, and to 52 / 40 when compute shaders
* carry their proven submit-pack loop bounds. */
* needed to compile fused variants, to 52 / 40 when compute shaders carry
* their proven submit-pack loop bounds, and to 53 / 41 when AGX3 payloads
* carry the root prefix (AGX2 moves to 39). */
#define HK_AGX3_SHADER_BINARY_VERSION 53u
#define HK_CLASSIC_SHADER_BINARY_VERSION 40u
#define HK_CLASSIC_SHADER_BINARY_VERSION 41u
/* AGX2 binaries include attachmentless layer-zero root lowering, fs.reads_layer,
* and per-pipeline vertex input robustness in hk_shader_info.vs. */
#define HK_AGX2_SHADER_BINARY_VERSION 38u
* per-pipeline vertex input robustness in hk_shader_info.vs, and the compute
* root prefix. */
#define HK_AGX2_SHADER_BINARY_VERSION 39u

static inline uint32_t
hk_shader_binary_version(enum agx_chip chip)
Expand Down
2 changes: 1 addition & 1 deletion src/asahi/vulkan/hk_cmd_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -4850,7 +4850,7 @@ hk_agx2_upload_usc_words(struct hk_cmd_buffer *cmd, struct hk_device *dev,
uint64_t root_ptr;

if (sw_stage == MESA_SHADER_COMPUTE) {
root_ptr = hk_cmd_buffer_upload_root(cmd, VK_PIPELINE_BIND_POINT_COMPUTE);
root_ptr = hk_cmd_buffer_upload_compute_root(cmd, s);
} else {
root_ptr = cmd->state.gfx.root;
}
Expand Down
2 changes: 2 additions & 0 deletions src/asahi/vulkan/hk_cmd_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3897,6 +3897,8 @@ void hk_cmd_print_upload_stats(struct hk_cmd_buffer *cmd);

uint64_t hk_cmd_buffer_upload_root(struct hk_cmd_buffer *cmd,
VkPipelineBindPoint bind_point);
uint64_t hk_cmd_buffer_upload_compute_root(struct hk_cmd_buffer *cmd,
const struct hk_shader *s);

void hk_reserve_scratch_slow(struct hk_cmd_buffer *cmd, struct hk_cs *cs,
struct hk_shader *s);
Expand Down
13 changes: 10 additions & 3 deletions src/asahi/vulkan/hk_cmd_dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -2226,7 +2226,7 @@ hk_dispatch_m5_native(struct hk_cmd_buffer *cmd, struct hk_cs *cs,
return;
}
const uint64_t root = internal ? cmd->state.gfx.root :
hk_cmd_buffer_upload_root(cmd, VK_PIPELINE_BIND_POINT_COMPUTE);
hk_cmd_buffer_upload_compute_root(cmd, s);
if (!root)
return;
const uint64_t texture_pointers = s->info.agx3_indexed_sampler_valid
Expand Down Expand Up @@ -2514,7 +2514,7 @@ hk_dispatch_g17p_native(struct hk_cmd_buffer *cmd, struct hk_cs *cs,

const uint64_t root_ptr = internal_graphics
? cmd->state.gfx.root
: hk_cmd_buffer_upload_root(cmd, VK_PIPELINE_BIND_POINT_COMPUTE);
: hk_cmd_buffer_upload_compute_root(cmd, s);
if (!root_ptr) {
vk_command_buffer_set_error(&cmd->vk, VK_ERROR_OUT_OF_DEVICE_MEMORY);
return;
Expand Down Expand Up @@ -3014,7 +3014,7 @@ hk_dispatch_g17g_native(struct hk_cmd_buffer *cmd, struct hk_cs *cs,
}
const uint64_t root = internal
? cmd->state.gfx.root
: hk_cmd_buffer_upload_root(cmd, VK_PIPELINE_BIND_POINT_COMPUTE);
: hk_cmd_buffer_upload_compute_root(cmd, s);
uint64_t texture_pointers = 0;
if (s->info.agx3_indexed_sampler_valid) {
/* The sampled-image heap may be a chunked table (no single BO): its
Expand Down Expand Up @@ -3465,6 +3465,7 @@ hk_record_compute_pair(struct hk_cmd_buffer *cmd, struct hk_cs *cs,
memcpy(record.exec_local, selected->b.info.workgroup_size,
sizeof(record.exec_local));
record.root_va = desc->uploaded_root.gpu;
record.root_size = desc->uploaded_root.gpu ? desc->uploaded_root_size : 0;
memcpy(record.root_sets, desc->root.sets, sizeof(record.root_sets));
memcpy(record.push, desc->root.push, sizeof(record.push));
record.cs_group_count_addr = desc->root.cs.group_count_addr;
Expand Down Expand Up @@ -3579,6 +3580,11 @@ hk_fused_compute_shader_eligible_ranges(
return false;
if (memcmp(s->b.info.workgroup_size, p->exec_local, sizeof(p->exec_local)))
return false;
const size_t required_root =
s->root_descriptor_size ? s->root_descriptor_size
: sizeof(struct hk_root_descriptor_table);
if (p->root_size < required_root)
return false;
if (!p->exec_threads[0] || !p->exec_threads[1] || !p->exec_threads[2])
return false;

Expand Down Expand Up @@ -4131,6 +4137,7 @@ hk_record_copy4_pair(struct hk_cmd_buffer *cmd, struct hk_cs *cs,
memcpy(record->exec_local, selected->b.info.workgroup_size,
sizeof(record->exec_local));
record->root_va = desc->uploaded_root.gpu;
record->root_size = desc->uploaded_root.gpu ? desc->uploaded_root_size : 0;
memcpy(record->root_sets, desc->root.sets, sizeof(record->root_sets));
memcpy(record->push, desc->root.push, sizeof(record->push));
record->cs_group_count_addr = desc->root.cs.group_count_addr;
Expand Down
44 changes: 33 additions & 11 deletions src/asahi/vulkan/hk_cmd_upload.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,27 @@ hk_pool_root_alloc(struct hk_cmd_buffer *cmd, uint32_t size,
HK_CMD_BO_ROOT, cpu_cached_out);
}

__attribute__((hot)) uint64_t
hk_cmd_buffer_upload_root(struct hk_cmd_buffer *cmd,
VkPipelineBindPoint bind_point)
static uint64_t
hk_cmd_buffer_upload_root_for_shader(struct hk_cmd_buffer *cmd,
VkPipelineBindPoint bind_point,
const struct hk_shader *cs_shader)
{
struct hk_device *dev = hk_cmd_buffer_device(cmd);
struct hk_descriptor_state *desc = hk_get_descriptors_state(cmd, bind_point);
struct hk_root_descriptor_table *root = &desc->root;
const bool verify = unlikely(dev->dev.debug & AGX_DBG_VERIFY);

/* A compute shader's measured root prefix (hk_compile_nir()), or a native
* HAL200/M5 graphics pipeline's measured prefix; zero for an unmeasured
* shader, which takes the whole table. */
size_t root_size = sizeof(*root);
if (agx_chip_native_hal200(dev->dev.chip) || agx_chip_g17_m5(dev->dev.chip)) {
if (bind_point == VK_PIPELINE_BIND_POINT_COMPUTE) {
const struct hk_shader *s = hk_only_variant(cmd->state.cs.shader);
if (s)
root_size = hk_shader_root_size(s);
} else if (bind_point == VK_PIPELINE_BIND_POINT_GRAPHICS) {
root_size = MIN2(root_size, hk_gfx_root_descriptor_size(cmd));
}
if (bind_point == VK_PIPELINE_BIND_POINT_COMPUTE) {
if (cs_shader)
root_size = hk_shader_root_size(cs_shader);
} else if ((agx_chip_native_hal200(dev->dev.chip) ||
agx_chip_g17_m5(dev->dev.chip)) &&
bind_point == VK_PIPELINE_BIND_POINT_GRAPHICS) {
root_size = MIN2(root_size, hk_gfx_root_descriptor_size(cmd));
}

root->printf_buffer_address = dev->dev.printf_buffer_address;
Expand Down Expand Up @@ -149,6 +152,25 @@ hk_cmd_buffer_upload_root(struct hk_cmd_buffer *cmd,
return root_ptr.gpu;
}

__attribute__((hot)) uint64_t
hk_cmd_buffer_upload_compute_root(struct hk_cmd_buffer *cmd,
const struct hk_shader *s)
{
return hk_cmd_buffer_upload_root_for_shader(
cmd, VK_PIPELINE_BIND_POINT_COMPUTE, s);
}

__attribute__((hot)) uint64_t
hk_cmd_buffer_upload_root(struct hk_cmd_buffer *cmd,
VkPipelineBindPoint bind_point)
{
const struct hk_shader *cs_shader =
bind_point == VK_PIPELINE_BIND_POINT_COMPUTE
? hk_only_variant(cmd->state.cs.shader)
: NULL;
return hk_cmd_buffer_upload_root_for_shader(cmd, bind_point, cs_shader);
}

uint64_t
hk_cmd_upload_constants(struct hk_cmd_buffer *cmd, const struct hk_shader *s,
mesa_shader_stage stage)
Expand Down
1 change: 1 addition & 0 deletions src/asahi/vulkan/hk_compute_pair.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ struct hk_compute_pair_dispatch {
uint32_t exec_threads[3]; /* Effective global thread counts (for CDM emit) */
uint16_t exec_local[3]; /* Effective logical workgroup size */
uint64_t root_va;
uint32_t root_size;
uint64_t root_sets[HK_MAX_SETS];
uint8_t push[HK_MAX_PUSH_SIZE];
uint64_t cs_group_count_addr;
Expand Down
3 changes: 3 additions & 0 deletions src/asahi/vulkan/hk_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -6434,6 +6434,8 @@ hk_pack_runs(struct hk_device *dev, struct hk_queue *queue,
has_im2col, has_dw_a ? &dw_a_cfg : NULL,
dual == HK_COMPUTE_PAIR_DUAL_GEMM_DW ? &gemm_dw_cfg : NULL);
struct hk_compute_pair_dispatch eff_p = *p;
if (dual != HK_COMPUTE_PAIR_DUAL_NONE || has_im2col || has_dw_a)
eff_p.root_size = sizeof(struct hk_root_descriptor_table);
if (eff_alt_kind != p->alt_kind ||
(dual == HK_COMPUTE_PAIR_DUAL_GEMM_DW && gemm_dw_cfg.has_proj)) {
eff_p.alt_kind = eff_alt_kind;
Expand Down Expand Up @@ -7601,6 +7603,7 @@ hk_pack_runs(struct hk_device *dev, struct hk_queue *queue,
}
}
tmp_disp.root_va = root_va + root_off;
tmp_disp.root_size = sizeof(struct hk_root_descriptor_table);
}

const size_t unique = cs->compute_resources.size;
Expand Down
39 changes: 20 additions & 19 deletions src/asahi/vulkan/hk_shader.c
Original file line number Diff line number Diff line change
Expand Up @@ -6322,22 +6322,27 @@ hk_compile_nir(struct hk_device *dev, const VkAllocationCallbacks *pAllocator,
wg_coalesce_cand = true;
}

/* Measure while root markers are still explicit. Uniform lowering loads
* the root pointer and set bases separately, so retain that prefix too.
* Hardware VS and FS prologs/epilogs access only the draw arm preceding
* push constants, so flooring graphics stages at offsetof(push) preserves
/* Measure while root markers are still explicit, so that
* hk_cmd_buffer_upload_root() uploads only the prefix a compute shader
* reads (on every generation) or a native HAL200 graphics shader reads.
* Uniform lowering loads the root pointer and set bases separately, so
* retain that prefix too: it is also all that a compute launch preloads
* from the root on every generation (the AGX2 USC root uniform and the
* AGX3 Ioto root range are both root_desc_addr plus set_count set bases).
* Uniform DMA spans over the root come from loads measured here. Hardware
* VS and FS prologs/epilogs access only the draw arm preceding push
* constants, so flooring HAL200 graphics stages at offsetof(push) preserves
* every driver-synthesized root load while trimming unused push-constant,
* dynamic-buffer and trailing tail bytes. Round up for native
* uniform/global load granularity. */
const bool trimmed_root =
* uniform/global load granularity. Every other stage keeps the whole table. */
const bool gfx_trimmed_root =
agx_chip_native_hal200(dev->dev.chip) || agx_chip_g17_m5(dev->dev.chip);
if (trimmed_root)
shader->root_descriptor_size = sizeof(struct hk_root_descriptor_table);
if (trimmed_root &&
(sw_stage == MESA_SHADER_COMPUTE ||
sw_stage == MESA_SHADER_FRAGMENT ||
(sw_stage == MESA_SHADER_VERTEX &&
nir->info.stage == MESA_SHADER_VERTEX))) {
shader->root_descriptor_size = sizeof(struct hk_root_descriptor_table);
if (sw_stage == MESA_SHADER_COMPUTE ||
(gfx_trimmed_root &&
(sw_stage == MESA_SHADER_FRAGMENT ||
(sw_stage == MESA_SHADER_VERTEX &&
nir->info.stage == MESA_SHADER_VERTEX)))) {
shader->root_descriptor_size = hk_shader_root_descriptor_size(
nir, sw_stage, set_count, hk_root_descriptor_offset(sets),
hk_root_descriptor_offset(push),
Expand Down Expand Up @@ -11465,10 +11470,7 @@ hk_deserialize_shader(struct hk_device *dev, struct blob_reader *blob,
/* A short read leaves info as it was: use none of it. */
if (blob->overrun)
return vk_error(dev, VK_ERROR_INCOMPATIBLE_SHADER_BINARY_EXT);
const uint32_t root_descriptor_size =
(agx_chip_native_hal200(dev->dev.chip) || agx_chip_g17_m5(dev->dev.chip))
? blob_read_uint32(blob)
: 0;
const uint32_t root_descriptor_size = blob_read_uint32(blob);
uint8_t mesh_primitive_comps[64] = {0};
if (dev->dev.chip == AGX_CHIP_G17P && info.stage == MESA_SHADER_MESH)
blob_copy_bytes(blob, mesh_primitive_comps, sizeof(mesh_primitive_comps));
Expand Down Expand Up @@ -12914,8 +12916,7 @@ hk_shader_serialize(struct vk_device *vk_dev, const struct hk_shader *shader,
{
struct hk_device *dev = container_of(vk_dev, struct hk_device, vk);
blob_write_bytes(blob, &shader->info, sizeof(shader->info));
if (agx_chip_native_hal200(dev->dev.chip) || agx_chip_g17_m5(dev->dev.chip))
blob_write_uint32(blob, shader->root_descriptor_size);
blob_write_uint32(blob, shader->root_descriptor_size);
if (dev->dev.chip == AGX_CHIP_G17P &&
shader->info.stage == MESA_SHADER_MESH)
blob_write_bytes(blob, shader->g17p_mesh_primitive_comps,
Expand Down
5 changes: 3 additions & 2 deletions src/asahi/vulkan/hk_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,9 @@ struct hk_shader {
struct agx_shader_part b;

struct hk_shader_info info;
/* G17P-only root prefix; serialized separately in its version-35 payload.
* Keep hk_shader_info and version-34 payloads unchanged on other chips. */
/* Root table prefix a compute shader (or native HAL200 graphics shader)
* reads (whole table for other stages, zero when unmeasured); serialized
* after hk_shader_info. */
uint32_t root_descriptor_size;
/* G17P mesh: 32-bit words each varying slot takes in the per-primitive
* record (agx_gen1_mesh_record_layout()), serialized like the root prefix.
Expand Down
63 changes: 59 additions & 4 deletions src/asahi/vulkan/tests/hk_cpu_upload_bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "hk_buffer.h"
#include "hk_cmd_buffer.h"
#include "hk_compute_pair.h"
#include "hk_descriptor_set_layout.h"
#include "hk_device.h"
#include "hk_entrypoints.h"
Expand Down Expand Up @@ -719,15 +720,69 @@ reuse_correctness(struct fixture *f)
cs->variants[0].root_descriptor_size = small_B;
CHECK(hk_cmd_buffer_upload_root(cmd, VK_PIPELINE_BIND_POINT_COMPUTE) != full);
CHECK(cd->uploaded_root_size == small_B);
/* A retained or adversarial short-prefix value must have no effect on
* chips without trimmed root uploads, including the native-encoding M3 (G15) path. */
/* Alternate compute variants (identity_copy4, im2col_vec4, depthwise_vec4,
* byte_stencil) upload their own measured prefix rather than the bound API
* shader's. */
struct hk_shader alt_cs = {.root_descriptor_size = small_B * 2};
cs->depthwise_vec4 = &alt_cs;
const uint64_t alt_large = hk_cmd_buffer_upload_compute_root(cmd, cs->depthwise_vec4);
CHECK(alt_large != small && cd->uploaded_root_size == small_B * 2);
CHECK(hk_cmd_buffer_upload_compute_root(cmd, cs->depthwise_vec4) == alt_large);
alt_cs.root_descriptor_size = small_B / 2;
const uint64_t alt_small = hk_cmd_buffer_upload_compute_root(cmd, cs->depthwise_vec4);
CHECK(alt_small != alt_large && cd->uploaded_root_size == small_B / 2);
alt_cs.root_descriptor_size = 0;
CHECK(hk_cmd_buffer_upload_compute_root(cmd, cs->depthwise_vec4) != alt_small);
CHECK(cd->uploaded_root_size == sizeof(cd->root));
cs->depthwise_vec4 = NULL;

/* Submit-time compute-pair fusion must reject a fused shader whose root
* prefix exceeds what the producer uploaded (including depthwise_vec4). */
struct hk_physical_device pdev = {0};
pdev.vk.properties.maxComputeWorkGroupInvocations = 1024;
struct vk_physical_device *saved_pdev = f->dev.vk.physical;
f->dev.vk.physical = &pdev.vk;
struct agx_va fused_va = {.addr = 0x100000};
struct agx_bo fused_bo = {.va = &fused_va};
struct hk_linked_shader fused_linked = {
.b = {.bo = &fused_bo, .size = 64, .nr_gprs = 8},
};
struct hk_shader fused_cs = {
.only_linked = &fused_linked,
.root_descriptor_size = small_B,
.info = {.stage = MESA_SHADER_COMPUTE, .agx3_uses_root = true, .set_count = 1},
.b = {.info = {.stage = MESA_SHADER_COMPUTE,
.workgroup_size = {32, 4, 1},
.agx3_staging_known = true}},
};
struct hk_compute_pair_dispatch pair = {
.exec_threads = {32, 4, 1},
.exec_local = {32, 4, 1},
.root_va = alt_large,
.root_size = small_B,
.alt_kind = HK_COMPUTE_PAIR_ALT_DEPTHWISE_VEC4,
};
CHECK(hk_fused_compute_shader_eligible(&f->dev, &fused_cs, &pair));
fused_cs.root_descriptor_size = small_B * 2;
CHECK(!hk_fused_compute_shader_eligible(&f->dev, &fused_cs, &pair));
fused_cs.root_descriptor_size = 0;
CHECK(!hk_fused_compute_shader_eligible(&f->dev, &fused_cs, &pair));
pair.root_size = sizeof(cd->root);
CHECK(hk_fused_compute_shader_eligible(&f->dev, &fused_cs, &pair));
f->dev.vk.physical = saved_pdev;
/* Every chip uploads only the measured compute prefix, including the AGX2
* and native-encoding M3 paths, and an unmeasured shader the whole table. */
for (enum agx_chip chip = AGX_CHIP_G13G; chip < AGX_CHIP_UNKNOWN; ++chip) {
if (agx_chip_native_hal200(chip) || agx_chip_g17_m5(chip))
continue;
f->dev.dev.chip = chip;
cs->variants[0].root_descriptor_size = small_B;
CHECK(hk_cmd_buffer_upload_root(cmd, VK_PIPELINE_BIND_POINT_COMPUTE));
CHECK(cd->uploaded_root_size == small_B);
CHECK(memcmp(cd->uploaded_root.cpu, &cd->root, small_B) == 0);
cs->variants[0].root_descriptor_size = 0;
CHECK(hk_cmd_buffer_upload_root(cmd, VK_PIPELINE_BIND_POINT_COMPUTE));
CHECK(cd->uploaded_root_size == sizeof(cd->root));
}
cs->variants[0].root_descriptor_size = small_B;
const enum agx_chip trimmed_chips[] = {
AGX_CHIP_G17P, AGX_CHIP_G16G, AGX_CHIP_G17G, AGX_CHIP_G17S, AGX_CHIP_G17C
};
Expand Down
1 change: 1 addition & 0 deletions src/asahi/vulkan/tests/test-hk-compute-pair-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ check_fused_m5_dispatch(enum agx_chip chip, uint32_t clusters, uint32_t dies,
.exec_local = {64, 1, 1},
.exec_threads = {64, 1, 1},
.root_va = UINT64_C(0x200000000),
.root_size = sizeof(struct hk_root_descriptor_table),
};

uint8_t cs_bytes[HK_CS_SPACE_PADDING + 256];
Expand Down
2 changes: 1 addition & 1 deletion src/asahi/vulkan/tests/test-hk-execution-allowance.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ int main(void)
(chip == AGX_CHIP_G17P || chip == AGX_CHIP_G17G ||
chip == AGX_CHIP_G17S || chip == AGX_CHIP_G17C ||
chip == AGX_CHIP_G17D ? 53 :
chip <= AGX_CHIP_G15P ? 38 : 40));
chip <= AGX_CHIP_G15P ? 39 : 41));
assert(hk_as_build_flags(AGX_CHIP_G17P, 0, true) == 0);
assert(hk_as_build_flags(AGX_CHIP_G17P, DRM_ASAHI_FEATURE_RENDER_RAY_QUERY, true) == 0);
assert(hk_as_build_flags(AGX_CHIP_G17P, DRM_ASAHI_FEATURE_BOUNDED_AS_BUILD, false) == 0);
Expand Down
1 change: 1 addition & 0 deletions src/asahi/vulkan/tests/test-hk-fused-cprog.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ init_base_fixture(struct hk_device *dev, struct hk_physical_device *pdev,
p->exec_threads[1] = 16;
p->exec_threads[2] = 1;
p->root_va = UINT64_C(0x5a89abcde0);
p->root_size = sizeof(struct hk_root_descriptor_table);
}

static void
Expand Down