From eecd8390d030accd1b4a2e884b29fd18ee86d90c Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Thu, 28 Sep 2023 11:54:28 -0400 Subject: [PATCH] asahi,agx: Plumb libagx Signed-off-by: Alyssa Rosenzweig Part-of: --- src/asahi/compiler/agx_compile.c | 30 +++++++++++++++++++--- src/asahi/compiler/agx_compile.h | 7 +++-- src/asahi/compiler/agx_nir_lower_texture.c | 4 +-- src/asahi/compiler/meson.build | 2 +- src/asahi/lib/agx_meta.c | 6 ++++- src/asahi/lib/agx_meta.h | 1 + src/gallium/drivers/asahi/agx_state.c | 4 ++- 7 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/asahi/compiler/agx_compile.c b/src/asahi/compiler/agx_compile.c index 78800d8b8ae..a6b19dbf4aa 100644 --- a/src/asahi/compiler/agx_compile.c +++ b/src/asahi/compiler/agx_compile.c @@ -2733,6 +2733,22 @@ agx_compile_function_nir(nir_shader *nir, nir_function_impl *impl, return offset; } +static void +link_libagx(nir_shader *nir, const nir_shader *libagx) +{ + nir_link_shader_functions(nir, libagx); + NIR_PASS_V(nir, nir_inline_functions); + NIR_PASS_V(nir, nir_remove_non_entrypoints); + NIR_PASS_V(nir, nir_lower_vars_to_explicit_types, nir_var_function_temp, + glsl_get_cl_type_size_align); + NIR_PASS_V(nir, nir_opt_deref); + NIR_PASS_V(nir, nir_lower_vars_to_ssa); + NIR_PASS_V(nir, nir_lower_explicit_io, + nir_var_shader_temp | nir_var_function_temp | nir_var_mem_shared | + nir_var_mem_global, + nir_address_format_62bit_generic); +} + /* * Preprocess NIR. In particular, this lowers I/O. Drivers should call this * as soon as they don't need unlowered I/O. @@ -2749,7 +2765,8 @@ agx_compile_function_nir(nir_shader *nir, nir_function_impl *impl, * lowered here to avoid duplicate work with shader variants. */ void -agx_preprocess_nir(nir_shader *nir, bool support_lod_bias, bool allow_mediump, +agx_preprocess_nir(nir_shader *nir, const nir_shader *libagx, + bool support_lod_bias, bool allow_mediump, struct agx_uncompiled_shader_info *out) { if (out) @@ -2800,6 +2817,8 @@ agx_preprocess_nir(nir_shader *nir, bool support_lod_bias, bool allow_mediump, NIR_PASS_V(nir, nir_opt_dce); NIR_PASS_V(nir, agx_nir_lower_texture, support_lod_bias); + link_libagx(nir, libagx); + /* Runs before we lower away idiv, to work at all. But runs after lowering * textures, since the cube map array lowering generates division by 6. */ @@ -2860,8 +2879,10 @@ agx_compile_shader_nir(nir_shader *nir, struct agx_shader_key *key, if (nir->info.stage == MESA_SHADER_FRAGMENT) out->tag_write_disable = !nir->info.writes_memory; + bool needs_libagx = false; + /* Late tilebuffer lowering creates multisampled image stores */ - NIR_PASS_V(nir, agx_nir_lower_multisampled_image_store); + NIR_PASS(needs_libagx, nir, agx_nir_lower_multisampled_image_store); /* Late sysval lowering creates large loads. Load lowering creates unpacks */ nir_lower_mem_access_bit_sizes_options lower_mem_access_options = { @@ -2878,7 +2899,10 @@ agx_compile_shader_nir(nir_shader *nir, struct agx_shader_key *key, NIR_PASS_V(nir, nir_lower_load_const_to_scalar); if (nir->info.stage == MESA_SHADER_FRAGMENT) - NIR_PASS_V(nir, agx_nir_lower_interpolation); + NIR_PASS(needs_libagx, nir, agx_nir_lower_interpolation); + + if (needs_libagx) + link_libagx(nir, key->libagx); /* Late VBO lowering creates constant udiv instructions */ NIR_PASS_V(nir, nir_opt_idiv_const, 16); diff --git a/src/asahi/compiler/agx_compile.h b/src/asahi/compiler/agx_compile.h index 4aaad90c5cd..2f8990e8583 100644 --- a/src/asahi/compiler/agx_compile.h +++ b/src/asahi/compiler/agx_compile.h @@ -213,6 +213,9 @@ struct agx_shader_key { */ bool needs_g13x_coherency; + /* Library routines to link against */ + const nir_shader *libagx; + union { struct agx_vs_shader_key vs; struct agx_fs_shader_key fs; @@ -224,8 +227,8 @@ struct agx_shader_key { bool agx_nir_lower_texture_early(nir_shader *s); -void agx_preprocess_nir(nir_shader *nir, bool support_lod_bias, - bool allow_mediump, +void agx_preprocess_nir(nir_shader *nir, const nir_shader *libagx, + bool support_lod_bias, bool allow_mediump, struct agx_uncompiled_shader_info *out); bool agx_nir_lower_discard_zs_emit(nir_shader *s); diff --git a/src/asahi/compiler/agx_nir_lower_texture.c b/src/asahi/compiler/agx_nir_lower_texture.c index b12e3e1e402..7a2c1787c9d 100644 --- a/src/asahi/compiler/agx_nir_lower_texture.c +++ b/src/asahi/compiler/agx_nir_lower_texture.c @@ -612,8 +612,8 @@ calculate_twiddled_coordinates(nir_builder *b, nir_def *coord, } static nir_def * -image_texel_address(nir_builder *b, nir_intrinsic_instr *intr, - bool return_index) +image_texel_address(nir_builder *b, const nir_shader *libagx, + nir_intrinsic_instr *intr, bool return_index) { /* First, calculate the address of the PBE descriptor */ nir_def *desc_address = diff --git a/src/asahi/compiler/meson.build b/src/asahi/compiler/meson.build index 8130dbb3eb8..f83b572c16a 100644 --- a/src/asahi/compiler/meson.build +++ b/src/asahi/compiler/meson.build @@ -86,7 +86,7 @@ idep_agx_builder_h = declare_dependency( libasahi_compiler = static_library( 'asahi_compiler', - [libasahi_agx_files, agx_opcodes_c, agx_nir_algebraic_c], + [libasahi_agx_files, libagx_shaders, agx_opcodes_c, agx_nir_algebraic_c], dependencies: [idep_nir, idep_agx_opcodes_h, idep_agx_builder_h, idep_agx_pack], c_args : [no_override_init_args], gnu_symbol_visibility : 'hidden', diff --git a/src/asahi/lib/agx_meta.c b/src/asahi/lib/agx_meta.c index 0724176ec46..95968be44b7 100644 --- a/src/asahi/lib/agx_meta.c +++ b/src/asahi/lib/agx_meta.c @@ -17,7 +17,7 @@ agx_compile_meta_shader(struct agx_meta_cache *cache, nir_shader *shader, struct util_dynarray binary; util_dynarray_init(&binary, NULL); - agx_preprocess_nir(shader, false, false, NULL); + agx_preprocess_nir(shader, cache->dev->libagx, false, false, NULL); if (tib) { unsigned bindless_base = 0; agx_nir_lower_tilebuffer(shader, tib, NULL, &bindless_base, NULL, true); @@ -25,6 +25,8 @@ agx_compile_meta_shader(struct agx_meta_cache *cache, nir_shader *shader, shader, &(struct agx_msaa_state){.nr_samples = tib->nr_samples}); } + key->libagx = cache->dev->libagx; + struct agx_meta_shader *res = rzalloc(cache->ht, struct agx_meta_shader); agx_compile_shader_nir(shader, key, NULL, &binary, &res->info); @@ -200,6 +202,7 @@ agx_meta_init(struct agx_meta_cache *cache, struct agx_device *dev) { agx_pool_init(&cache->pool, dev, AGX_BO_EXEC | AGX_BO_LOW_VA, true); cache->ht = _mesa_hash_table_create(NULL, key_hash, key_compare); + cache->dev = dev; } void @@ -208,4 +211,5 @@ agx_meta_cleanup(struct agx_meta_cache *cache) agx_pool_cleanup(&cache->pool); _mesa_hash_table_destroy(cache->ht, NULL); cache->ht = NULL; + cache->dev = NULL; } diff --git a/src/asahi/lib/agx_meta.h b/src/asahi/lib/agx_meta.h index 4a1a6ab2b5f..901b32883f9 100644 --- a/src/asahi/lib/agx_meta.h +++ b/src/asahi/lib/agx_meta.h @@ -11,6 +11,7 @@ #include "pool.h" struct agx_meta_cache { + struct agx_device *dev; struct agx_pool pool; /* Map from agx_meta_key to agx_meta_shader */ diff --git a/src/gallium/drivers/asahi/agx_state.c b/src/gallium/drivers/asahi/agx_state.c index 454d72b2c23..0e3b4b68d36 100644 --- a/src/gallium/drivers/asahi/agx_state.c +++ b/src/gallium/drivers/asahi/agx_state.c @@ -1675,6 +1675,7 @@ agx_compile_variant(struct agx_device *dev, struct agx_uncompiled_shader *so, .needs_g13x_coherency = (dev->params.gpu_generation == 13 && dev->params.num_clusters_total > 1) || dev->params.num_dies > 1, + .libagx = dev->libagx, }; if (nir->info.stage == MESA_SHADER_FRAGMENT) @@ -1788,7 +1789,8 @@ agx_shader_initialize(struct agx_device *dev, struct agx_uncompiled_shader *so, } bool allow_mediump = !(dev->debug & AGX_DBG_NO16); - agx_preprocess_nir(nir, support_lod_bias, allow_mediump, &so->info); + agx_preprocess_nir(nir, dev->libagx, support_lod_bias, allow_mediump, + &so->info); blob_init(&so->serialized_nir); nir_serialize(&so->serialized_nir, nir, true);