intel/disasm: Label support in shader disassembly for UIP/JIP
authorDanylo Piliaiev <danylo.piliaiev@globallogic.com>
Mon, 3 Jun 2019 11:55:23 +0000 (14:55 +0300)
committerMarge Bot <eric+marge@anholt.net>
Wed, 2 Sep 2020 10:33:29 +0000 (10:33 +0000)
Shader instructions which use UIP/JIP now get formatted with a label
in addition with immediate value, labels have "LABEL%d" format.

v2: - Consider brw_jump_scale when calculating label's offset

From: "Lonnberg, Toni" <toni.lonnberg@intel.com>
Signed-off-by: Danylo Piliaiev <danylo.piliaiev@globallogic.com>
Reviewed-by: Sagar Ghuge <sagar.ghuge@intel.com>
Reviewed-by: Matt Turner <mattst88@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4245>

18 files changed:
src/gallium/drivers/iris/iris_program_cache.c
src/intel/common/gen_disasm.c
src/intel/compiler/brw_compile_clip.c
src/intel/compiler/brw_compile_sf.c
src/intel/compiler/brw_disasm.c
src/intel/compiler/brw_disasm_info.c
src/intel/compiler/brw_disasm_info.h
src/intel/compiler/brw_eu.cpp
src/intel/compiler/brw_eu.h
src/intel/compiler/brw_eu_compact.c
src/intel/compiler/brw_fs_generator.cpp
src/intel/compiler/brw_vec4_generator.cpp
src/intel/compiler/test_eu_compact.cpp
src/intel/compiler/test_eu_validate.cpp
src/intel/tools/i965_disasm.c
src/mesa/drivers/dri/i965/brw_disk_cache.c
src/mesa/drivers/dri/i965/brw_ff_gs.c
src/mesa/drivers/dri/i965/brw_program_cache.c

index f7f7d63cce3ed032a8ea38fa735c901e0c4e9600..c29802d9ba1eb1dc270e5200188fa447db1a6c1b 100644 (file)
@@ -365,7 +365,7 @@ iris_print_program_cache(struct iris_context *ice)
       const struct keybox *keybox = entry->key;
       struct iris_compiled_shader *shader = entry->data;
       fprintf(stderr, "%s:\n", cache_name(keybox->cache_id));
-      brw_disassemble(devinfo, shader->map, 0,
-                      shader->prog_data->program_size, stderr);
+      brw_disassemble_with_labels(devinfo, shader->map, 0,
+                                  shader->prog_data->program_size, stderr);
    }
 }
index 656613cd931b5c04db298d739eaf26734f4e4fab..375f2c37764febf3261ddd67aabd1a6ff3f698a8 100644 (file)
@@ -84,6 +84,10 @@ gen_disasm_disassemble(struct gen_disasm *disasm, const void *assembly,
 
    brw_validate_instructions(devinfo, assembly, start, end, disasm_info);
 
+   void *mem_ctx = ralloc_context(NULL);
+   const struct brw_label *root_label =
+      brw_label_assembly(devinfo, assembly, start, end, mem_ctx);
+
    foreach_list_typed(struct inst_group, group, link,
                       &disasm_info->group_list) {
       struct exec_node *next_node = exec_node_get_next(&group->link);
@@ -96,13 +100,15 @@ gen_disasm_disassemble(struct gen_disasm *disasm, const void *assembly,
       int start_offset = group->offset;
       int end_offset = next->offset;
 
-      brw_disassemble(devinfo, assembly, start_offset, end_offset, out);
+      brw_disassemble(devinfo, assembly, start_offset, end_offset,
+                      root_label, out);
 
       if (group->error) {
          fputs(group->error, out);
       }
    }
 
+   ralloc_free(mem_ctx);
    ralloc_free(disasm_info);
 }
 
index 682611223173fa0e15608aa4cb4df0a132fbfe43..e1e20e3ccbcc4b1c3bcc92b32e0ac0d333aa37bf 100644 (file)
@@ -87,8 +87,8 @@ brw_compile_clip(const struct brw_compiler *compiler,
 
    if (unlikely(INTEL_DEBUG & DEBUG_CLIP)) {
       fprintf(stderr, "clip:\n");
-      brw_disassemble(compiler->devinfo,
-                      program, 0, *final_assembly_size, stderr);
+      brw_disassemble_with_labels(compiler->devinfo,
+                                  program, 0, *final_assembly_size, stderr);
       fprintf(stderr, "\n");
    }
 
index b750d27f9496e07974079367d6bc8488a48dd465..d8aca0f710049d293e724214043f88a8d7b61cb3 100644 (file)
@@ -870,8 +870,8 @@ brw_compile_sf(const struct brw_compiler *compiler,
 
    if (unlikely(INTEL_DEBUG & DEBUG_SF)) {
       fprintf(stderr, "sf:\n");
-      brw_disassemble(compiler->devinfo,
-                      program, 0, *final_assembly_size, stderr);
+      brw_disassemble_with_labels(compiler->devinfo,
+                                  program, 0, *final_assembly_size, stderr);
       fprintf(stderr, "\n");
    }
 
index e447087094bfdf54c2462cbb085c93472b1f909d..87723678d60ca44e4b5e5b758933e2d376cab599 100644 (file)
@@ -1654,13 +1654,29 @@ brw_disassemble_imm(const struct gen_device_info *devinfo,
    brw_inst inst;
    inst.data[0] = (((uint64_t) dw1) << 32) | ((uint64_t) dw0);
    inst.data[1] = (((uint64_t) dw3) << 32) | ((uint64_t) dw2);
-   return brw_disassemble_inst(stderr, devinfo, &inst, false);
+   return brw_disassemble_inst(stderr, devinfo, &inst, false, 0, NULL);
 }
 #endif
 
+static void
+write_label(FILE *file, const struct gen_device_info *devinfo,
+            const struct brw_label *root_label,
+            int offset, int jump)
+{
+   if (root_label != NULL) {
+      int to_bytes_scale = sizeof(brw_inst) / brw_jump_scale(devinfo);
+      const struct brw_label *label =
+         brw_find_label(root_label, offset + jump * to_bytes_scale);
+      if (label != NULL) {
+         format(file, " LABEL%d", label->number);
+      }
+   }
+}
+
 int
 brw_disassemble_inst(FILE *file, const struct gen_device_info *devinfo,
-                     const brw_inst *inst, bool is_compacted)
+                     const brw_inst *inst, bool is_compacted,
+                     int offset, const struct brw_label *root_label)
 {
    int err = 0;
    int space = 0;
@@ -1736,16 +1752,23 @@ brw_disassemble_inst(FILE *file, const struct gen_device_info *devinfo,
    if (brw_has_uip(devinfo, opcode)) {
       /* Instructions that have UIP also have JIP. */
       pad(file, 16);
-      format(file, "JIP: %d", brw_inst_jip(devinfo, inst));
-      pad(file, 32);
-      format(file, "UIP: %d", brw_inst_uip(devinfo, inst));
+      string(file, "JIP: ");
+      write_label(file, devinfo, root_label, offset, brw_inst_jip(devinfo, inst));
+
+      pad(file, 38);
+      string(file, "UIP: ");
+      write_label(file, devinfo, root_label, offset, brw_inst_uip(devinfo, inst));
    } else if (brw_has_jip(devinfo, opcode)) {
-      pad(file, 16);
+      int jip;
       if (devinfo->gen >= 7) {
-         format(file, "JIP: %d", brw_inst_jip(devinfo, inst));
+         jip = brw_inst_jip(devinfo, inst);
       } else {
-         format(file, "JIP: %d", brw_inst_gen6_jump_count(devinfo, inst));
+         jip = brw_inst_gen6_jump_count(devinfo, inst);
       }
+
+      pad(file, 16);
+      string(file, "JIP: ");
+      write_label(file, devinfo, root_label, offset, jip);
    } else if (devinfo->gen < 6 && (opcode == BRW_OPCODE_BREAK ||
                                    opcode == BRW_OPCODE_CONTINUE ||
                                    opcode == BRW_OPCODE_ELSE)) {
index 14cea9a77bd27890536b00d70e5cda8e1f3fa272..079d1716b1ffcecef031710fdcff692f561efa9f 100644 (file)
@@ -31,13 +31,17 @@ __attribute__((weak)) void nir_print_instr(UNUSED const nir_instr *instr,
                                            UNUSED FILE *fp) {}
 
 void
-dump_assembly(void *assembly, struct disasm_info *disasm,
-              const unsigned *block_latency)
+dump_assembly(void *assembly, int start_offset, int end_offset,
+              struct disasm_info *disasm, const unsigned *block_latency)
 {
    const struct gen_device_info *devinfo = disasm->devinfo;
    const char *last_annotation_string = NULL;
    const void *last_annotation_ir = NULL;
 
+   void *mem_ctx = ralloc_context(NULL);
+   const struct brw_label *root_label =
+      brw_label_assembly(devinfo, assembly, start_offset, end_offset, mem_ctx);
+
    foreach_list_typed(struct inst_group, group, link, &disasm->group_list) {
       struct exec_node *next_node = exec_node_get_next(&group->link);
       if (exec_node_is_tail_sentinel(next_node))
@@ -77,7 +81,8 @@ dump_assembly(void *assembly, struct disasm_info *disasm,
             fprintf(stderr, "   %s\n", last_annotation_string);
       }
 
-      brw_disassemble(devinfo, assembly, start_offset, end_offset, stderr);
+      brw_disassemble(devinfo, assembly, start_offset, end_offset,
+                      root_label, stderr);
 
       if (group->error) {
          fputs(group->error, stderr);
@@ -94,6 +99,8 @@ dump_assembly(void *assembly, struct disasm_info *disasm,
       }
    }
    fprintf(stderr, "\n");
+
+   ralloc_free(mem_ctx);
 }
 
 struct disasm_info *
index 932c06b6c3014b287dfad8781635925273ca25e6..5bfa2074739ba10167b5d92b1ee3ed3dc171c1f8 100644 (file)
@@ -65,8 +65,8 @@ struct disasm_info {
 };
 
 void
-dump_assembly(void *assembly, struct disasm_info *disasm,
-              const unsigned *block_latency);
+dump_assembly(void *assembly, int start_offset, int end_offset,
+              struct disasm_info *disasm, const unsigned *block_latency);
 
 struct disasm_info *
 disasm_initialize(const struct gen_device_info *devinfo,
index 596894e434e8faeefdf6da4249077a76e932cf74..0a886b27ae2126bdfed1ecc9e11ab47595d49b25 100644 (file)
@@ -457,15 +457,84 @@ brw_create_label(struct brw_label **labels, int offset, void *mem_ctx)
    }
 }
 
+const struct brw_label *
+brw_label_assembly(const struct gen_device_info *devinfo,
+                   const void *assembly, int start, int end, void *mem_ctx)
+{
+   struct brw_label *root_label = NULL;
+
+   int to_bytes_scale = sizeof(brw_inst) / brw_jump_scale(devinfo);
+
+   for (int offset = start; offset < end;) {
+      const brw_inst *inst = (const brw_inst *) ((const char *) assembly + offset);
+      brw_inst uncompacted;
+
+      bool is_compact = brw_inst_cmpt_control(devinfo, inst);
+
+      if (is_compact) {
+         brw_compact_inst *compacted = (brw_compact_inst *)inst;
+         brw_uncompact_instruction(devinfo, &uncompacted, compacted);
+         inst = &uncompacted;
+      }
+
+      if (brw_has_uip(devinfo, brw_inst_opcode(devinfo, inst))) {
+         /* Instructions that have UIP also have JIP. */
+         brw_create_label(&root_label,
+            offset + brw_inst_uip(devinfo, inst) * to_bytes_scale, mem_ctx);
+         brw_create_label(&root_label,
+            offset + brw_inst_jip(devinfo, inst) * to_bytes_scale, mem_ctx);
+      } else if (brw_has_jip(devinfo, brw_inst_opcode(devinfo, inst))) {
+         int jip;
+         if (devinfo->gen >= 7) {
+            jip = brw_inst_jip(devinfo, inst);
+         } else {
+            jip = brw_inst_gen6_jump_count(devinfo, inst);
+         }
+
+         brw_create_label(&root_label, offset + jip * to_bytes_scale, mem_ctx);
+      }
+
+      if (is_compact) {
+         offset += sizeof(brw_compact_inst);
+      } else {
+         offset += sizeof(brw_inst);
+      }
+   }
+
+   return root_label;
+}
+
+void
+brw_disassemble_with_labels(const struct gen_device_info *devinfo,
+                            const void *assembly, int start, int end, FILE *out)
+{
+   void *mem_ctx = ralloc_context(NULL);
+   const struct brw_label *root_label =
+      brw_label_assembly(devinfo, assembly, start, end, mem_ctx);
+
+   brw_disassemble(devinfo, assembly, start, end, root_label, out);
+
+   ralloc_free(mem_ctx);
+}
+
 void
 brw_disassemble(const struct gen_device_info *devinfo,
-                const void *assembly, int start, int end, FILE *out)
+                const void *assembly, int start, int end,
+                const struct brw_label *root_label, FILE *out)
 {
    bool dump_hex = (INTEL_DEBUG & DEBUG_HEX) != 0;
 
    for (int offset = start; offset < end;) {
       const brw_inst *insn = (const brw_inst *)((char *)assembly + offset);
       brw_inst uncompacted;
+
+      if (root_label != NULL) {
+        const struct brw_label *label = brw_find_label(root_label, offset);
+        if (label != NULL) {
+           fprintf(out, "\nLABEL%d:\n", label->number);
+        }
+      }
+
       bool compacted = brw_inst_cmpt_control(devinfo, insn);
       if (0)
          fprintf(out, "0x%08x: ", offset);
@@ -490,8 +559,6 @@ brw_disassemble(const struct gen_device_info *devinfo,
 
          brw_uncompact_instruction(devinfo, &uncompacted, compacted);
          insn = &uncompacted;
-         offset += 8;
-      } else {
          if (dump_hex) {
             unsigned char * insn_ptr = ((unsigned char *)&insn[0]);
             for (int i = 0 ; i < 16; i = i + 4) {
@@ -502,10 +569,15 @@ brw_disassemble(const struct gen_device_info *devinfo,
                        insn_ptr[i + 3]);
             }
          }
-         offset += 16;
       }
 
-      brw_disassemble_inst(out, devinfo, insn, compacted);
+      brw_disassemble_inst(out, devinfo, insn, compacted, offset, root_label);
+
+      if (compacted) {
+         offset += sizeof(brw_compact_inst);
+      } else {
+         offset += sizeof(brw_inst);
+      }
    }
 }
 
index c08f3b877b94b7b6f36604a597983df38ea80ccb..fa7f0b7b6ed6670a9e19b3bb172526dd3cc7a72b 100644 (file)
@@ -173,9 +173,16 @@ bool brw_has_uip(const struct gen_device_info *devinfo, enum opcode opcode);
 const struct brw_label *brw_find_label(const struct brw_label *root, int offset);
 void brw_create_label(struct brw_label **labels, int offset, void *mem_ctx);
 int brw_disassemble_inst(FILE *file, const struct gen_device_info *devinfo,
-                         const struct brw_inst *inst, bool is_compacted);
+                         const struct brw_inst *inst, bool is_compacted,
+                         int offset, const struct brw_label *root_label);
+const struct brw_label *brw_label_assembly(const struct gen_device_info *devinfo,
+                                           const void *assembly, int start, int end,
+                                           void *mem_ctx);
+void brw_disassemble_with_labels(const struct gen_device_info *devinfo,
+                                 const void *assembly, int start, int end, FILE *out);
 void brw_disassemble(const struct gen_device_info *devinfo,
-                     const void *assembly, int start, int end, FILE *out);
+                     const void *assembly, int start, int end,
+                     const struct brw_label *root_label, FILE *out);
 const unsigned *brw_get_program( struct brw_codegen *p, unsigned *sz );
 
 bool brw_try_override_assembly(struct brw_codegen *p, int start_offset,
index 45e158fb2a62697e04997de904bd5ac886419154..86ac0d5d5c25d551b2c2ad7f9856de63bfbf2ae2 100644 (file)
@@ -2070,10 +2070,10 @@ void brw_debug_compact_uncompact(const struct gen_device_info *devinfo,
            devinfo->gen);
 
    fprintf(stderr, "  before: ");
-   brw_disassemble_inst(stderr, devinfo, orig, true);
+   brw_disassemble_inst(stderr, devinfo, orig, true, 0, NULL);
 
    fprintf(stderr, "  after:  ");
-   brw_disassemble_inst(stderr, devinfo, uncompacted, false);
+   brw_disassemble_inst(stderr, devinfo, uncompacted, false, 0, NULL);
 
    uint32_t *before_bits = (uint32_t *)orig;
    uint32_t *after_bits = (uint32_t *)uncompacted;
index 5b1f28950bf31afaa4d3e96cbd7da42f425ab89c..94eb84870f1200c4d4a26200f41425c6f1ee6956 100644 (file)
@@ -2561,7 +2561,8 @@ fs_generator::generate_code(const cfg_t *cfg, int dispatch_width,
 
       /* overriding the shader makes disasm_info invalid */
       if (!brw_try_override_assembly(p, start_offset, sha1buf)) {
-         dump_assembly(p->store, disasm_info, perf.block_latency);
+         dump_assembly(p->store, start_offset, p->next_insn_offset,
+                       disasm_info, perf.block_latency);
       } else {
          fprintf(stderr, "Successfully overrode shader with sha1 %s\n\n", sha1buf);
       }
index 3fe1edc38ac102ed157b719566f19521b3f33267..43fcdf70ff06576db7bdc170fc84712baf507c58 100644 (file)
@@ -2238,7 +2238,8 @@ generate_code(struct brw_codegen *p,
 
       /* overriding the shader makes disasm_info invalid */
       if (!brw_try_override_assembly(p, 0, sha1buf)) {
-         dump_assembly(p->store, disasm_info, perf.block_latency);
+         dump_assembly(p->store, 0, p->next_insn_offset,
+                       disasm_info, perf.block_latency);
       } else {
          fprintf(stderr, "Successfully overrode shader with sha1 %s\n\n", sha1buf);
       }
index bf87ff844b46d9bedc03e990826910d6f8cedd60..343f060b7481b35fbf045d33fb3fbb06cc6bd9c2 100644 (file)
@@ -49,7 +49,7 @@ test_compact_instruction(struct brw_codegen *p, brw_inst src)
       if (memcmp(&unchanged, &dst, sizeof(dst))) {
         fprintf(stderr, "Failed to compact, but dst changed\n");
         fprintf(stderr, "  Instruction: ");
-        brw_disassemble_inst(stderr, p->devinfo, &src, false);
+        brw_disassemble_inst(stderr, p->devinfo, &src, false, 0, NULL);
         return false;
       }
    }
index 03da003c0d6203a1eb7ae9e9adf4a60cc26e26bc..a2705199be804503f1f9cd8fd84313cb41cb0b86 100644 (file)
@@ -110,7 +110,7 @@ validate(struct brw_codegen *p)
                                         p->next_insn_offset, disasm);
 
    if (print) {
-      dump_assembly(p->store, disasm, NULL);
+      dump_assembly(p->store, 0, p->next_insn_offset, disasm, NULL);
    }
    ralloc_free(disasm);
 
index 471191571658a1ceb359d80da3221dfb8152d3b8..77691e2a62e1750b9b7d3fe41856ee3bdeeacd87 100644 (file)
@@ -241,7 +241,7 @@ int main(int argc, char *argv[])
    }
 
    /* Disassemble i965 instructions from buffer assembly */
-   brw_disassemble(devinfo, assembly, start, end, stdout);
+   brw_disassemble_with_labels(devinfo, assembly, start, end, stdout);
 
    result = EXIT_SUCCESS;
 
index 1b4cb437cbc227fdfddb72bd1b00487041d0a269..2b65236b449c6523594d5937ba7cd322e7a0e91f 100644 (file)
@@ -207,8 +207,8 @@ read_and_upload(struct brw_context *brw, struct disk_cache *cache,
       fprintf(stderr, "Native code for %s %s shader %s from disk cache:\n",
               nir->info.label ? nir->info.label : "unnamed",
               _mesa_shader_stage_to_string(nir->info.stage), nir->info.name);
-      brw_disassemble(&brw->screen->devinfo, program, 0,
-                      prog_data->program_size, stderr);
+      brw_disassemble_with_labels(&brw->screen->devinfo, program, 0,
+                                  prog_data->program_size, stderr);
    }
 
    brw_upload_cache(&brw->cache, cache_id, &prog_key, brw_prog_key_size(stage),
index b2c4ee12f4d1844f993df419fd5bcf86bc62d668..6ebbc38a534eb152a9c68daa96d3ab1c7d0ad310 100644 (file)
@@ -135,8 +135,8 @@ brw_codegen_ff_gs_prog(struct brw_context *brw,
 
    if (unlikely(INTEL_DEBUG & DEBUG_GS)) {
       fprintf(stderr, "gs:\n");
-      brw_disassemble(&brw->screen->devinfo, c.func.store,
-                      0, program_size, stderr);
+      brw_disassemble_with_labels(&brw->screen->devinfo, c.func.store,
+                                  0, program_size, stderr);
       fprintf(stderr, "\n");
     }
 
index 90999ab0a4f8d743a9a2d83818310aff8df86b6e..85de8c01d655fa024baecc8d15deef06dc8953cf 100644 (file)
@@ -516,8 +516,8 @@ brw_print_program_cache(struct brw_context *brw)
    for (unsigned i = 0; i < cache->size; i++) {
       for (item = cache->items[i]; item; item = item->next) {
          fprintf(stderr, "%s:\n", cache_name(i));
-         brw_disassemble(&brw->screen->devinfo, cache->map,
-                         item->offset, item->size, stderr);
+         brw_disassemble_with_labels(&brw->screen->devinfo, cache->map,
+                                     item->offset, item->size, stderr);
       }
    }
 }