Module: Mesa
Branch: main
Commit: f7ae92b868d887dbb5a089b5e622c7071a076363
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=f7ae92b868d887dbb5a089b5e622c7071a076363

Author: Lionel Landwerlin <lionel.g.landwer...@intel.com>
Date:   Thu Nov 30 19:12:14 2023 +0200

nir: include printfs from linked shaders

Once lowered low enough, it's not always possible to tell what strings
are used. So include them all when linking another shader.

Signed-off-by: Lionel Landwerlin <lionel.g.landwer...@intel.com>
Reviewed-by: Karol Herbst <kher...@redhat.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/26505>

---

 src/compiler/nir/nir_functions.c | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/src/compiler/nir/nir_functions.c b/src/compiler/nir/nir_functions.c
index baefb023b69..d17ebd83ead 100644
--- a/src/compiler/nir/nir_functions.c
+++ b/src/compiler/nir/nir_functions.c
@@ -289,6 +289,7 @@ nir_inline_functions(nir_shader *shader)
 struct lower_link_state {
    struct hash_table *shader_var_remap;
    const nir_shader *link_shader;
+   unsigned printf_index_offset;
 };
 
 static bool
@@ -335,6 +336,23 @@ lower_calls_vars_instr(struct nir_builder *b,
          ncall->callee = nir_function_clone(b->shader, new_func);
       break;
    }
+   case nir_instr_type_intrinsic: {
+      /* Reindex the offset of the printf intrinsic by the number of already
+       * present printfs in the shader where functions are linked into.
+       */
+      if (state->printf_index_offset == 0)
+         return false;
+
+      nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
+      if (intrin->intrinsic != nir_intrinsic_printf)
+         return false;
+
+      b->cursor = nir_before_instr(instr);
+      nir_src_rewrite(&intrin->src[0],
+                      nir_iadd_imm(b, intrin->src[0].ssa,
+                                      state->printf_index_offset));
+      break;
+   }
    default:
       break;
    }
@@ -396,6 +414,7 @@ nir_link_shader_functions(nir_shader *shader,
    struct lower_link_state state = {
       .shader_var_remap = copy_vars,
       .link_shader = link_shader,
+      .printf_index_offset = shader->printf_info_count,
    };
    /* do progress passes inside the pass */
    do {
@@ -412,6 +431,27 @@ nir_link_shader_functions(nir_shader *shader,
       overall_progress |= progress;
    } while (progress);
 
+   if (overall_progress && link_shader->printf_info_count > 0) {
+      shader->printf_info = reralloc(shader, shader->printf_info,
+                                     u_printf_info,
+                                     shader->printf_info_count +
+                                     link_shader->printf_info_count);
+
+      for (unsigned i = 0; i < link_shader->printf_info_count; i++){
+         const u_printf_info *src_info = &link_shader->printf_info[i];
+         u_printf_info *dst_info = 
&shader->printf_info[shader->printf_info_count++];
+
+         dst_info->num_args = src_info->num_args;
+         dst_info->arg_sizes = ralloc_array(shader, unsigned, 
dst_info->num_args);
+         memcpy(dst_info->arg_sizes, src_info->arg_sizes,
+                sizeof(dst_info->arg_sizes[0]) * dst_info->num_args);
+
+         dst_info->string_size = src_info->string_size;
+         dst_info->strings = ralloc_size(shader, dst_info->string_size);
+         memcpy(dst_info->strings, src_info->strings, dst_info->string_size);
+      }
+   }
+
    ralloc_free(ra_ctx);
 
    return overall_progress;

Reply via email to