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

Author: Michael Tang <[email protected]>
Date:   Tue Oct  5 15:32:33 2021 -0700

nir: add nir_instr_as_str

Reviewed-by: Jesse Natalie <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/12510>

---

 src/compiler/nir/nir.h       |  1 +
 src/compiler/nir/nir_print.c | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
index 1760b453487..fb1de764bef 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -4308,6 +4308,7 @@ void nir_log_shader_annotated_tagged(enum mesa_log_level 
level, const char *tag,
 
 char *nir_shader_as_str(nir_shader *nir, void *mem_ctx);
 char *nir_shader_as_str_annotated(nir_shader *nir, struct hash_table 
*annotations, void *mem_ctx);
+char *nir_instr_as_str(const nir_instr *instr, void *mem_ctx);
 
 /** Shallow clone of a single instruction. */
 nir_instr *nir_instr_clone(nir_shader *s, const nir_instr *orig);
diff --git a/src/compiler/nir/nir_print.c b/src/compiler/nir/nir_print.c
index 8d5cb3cafeb..3162f0d3714 100644
--- a/src/compiler/nir/nir_print.c
+++ b/src/compiler/nir/nir_print.c
@@ -1801,7 +1801,27 @@ nir_print_instr(const nir_instr *instr, FILE *fp)
    }
 
    print_instr(instr, &state, 0);
+}
+
+char *
+nir_instr_as_str(const nir_instr *instr, void *mem_ctx)
+{
+   char *stream_data = NULL;
+   size_t stream_size = 0;
+   struct u_memstream mem;
+   if (u_memstream_open(&mem, &stream_data, &stream_size)) {
+      FILE *const stream = u_memstream_get(&mem);
+      nir_print_instr(instr, stream);
+      u_memstream_close(&mem);
+   }
 
+   char *str = ralloc_size(mem_ctx, stream_size + 1);
+   memcpy(str, stream_data, stream_size);
+   str[stream_size] = '\0';
+
+   free(stream_data);
+
+   return str;
 }
 
 void

Reply via email to