From bc4a127d6e10318d48fa8b540b9c1ff7d62c8d29 Mon Sep 17 00:00:00 2001 From: Danylo Piliaiev Date: Mon, 3 Jun 2019 14:55:23 +0300 Subject: [PATCH] intel/disasm: Label support in shader disassembly for UIP/JIP 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" Signed-off-by: Danylo Piliaiev Reviewed-by: Sagar Ghuge Reviewed-by: Matt Turner Part-of: --- src/gallium/drivers/iris/iris_program_cache.c | 4 +- src/intel/common/gen_disasm.c | 8 +- src/intel/compiler/brw_compile_clip.c | 4 +- src/intel/compiler/brw_compile_sf.c | 4 +- src/intel/compiler/brw_disasm.c | 39 +++++++-- src/intel/compiler/brw_disasm_info.c | 13 ++- src/intel/compiler/brw_disasm_info.h | 4 +- src/intel/compiler/brw_eu.cpp | 82 +++++++++++++++++-- src/intel/compiler/brw_eu.h | 11 ++- src/intel/compiler/brw_eu_compact.c | 4 +- src/intel/compiler/brw_fs_generator.cpp | 3 +- src/intel/compiler/brw_vec4_generator.cpp | 3 +- src/intel/compiler/test_eu_compact.cpp | 2 +- src/intel/compiler/test_eu_validate.cpp | 2 +- src/intel/tools/i965_disasm.c | 2 +- src/mesa/drivers/dri/i965/brw_disk_cache.c | 4 +- src/mesa/drivers/dri/i965/brw_ff_gs.c | 4 +- src/mesa/drivers/dri/i965/brw_program_cache.c | 4 +- 18 files changed, 157 insertions(+), 40 deletions(-) diff --git a/src/gallium/drivers/iris/iris_program_cache.c b/src/gallium/drivers/iris/iris_program_cache.c index f7f7d63cce3..c29802d9ba1 100644 --- a/src/gallium/drivers/iris/iris_program_cache.c +++ b/src/gallium/drivers/iris/iris_program_cache.c @@ -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); } } diff --git a/src/intel/common/gen_disasm.c b/src/intel/common/gen_disasm.c index 656613cd931..375f2c37764 100644 --- a/src/intel/common/gen_disasm.c +++ b/src/intel/common/gen_disasm.c @@ -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); } diff --git a/src/intel/compiler/brw_compile_clip.c b/src/intel/compiler/brw_compile_clip.c index 68261122317..e1e20e3ccbc 100644 --- a/src/intel/compiler/brw_compile_clip.c +++ b/src/intel/compiler/brw_compile_clip.c @@ -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"); } diff --git a/src/intel/compiler/brw_compile_sf.c b/src/intel/compiler/brw_compile_sf.c index b750d27f949..d8aca0f7100 100644 --- a/src/intel/compiler/brw_compile_sf.c +++ b/src/intel/compiler/brw_compile_sf.c @@ -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"); } diff --git a/src/intel/compiler/brw_disasm.c b/src/intel/compiler/brw_disasm.c index e447087094b..87723678d60 100644 --- a/src/intel/compiler/brw_disasm.c +++ b/src/intel/compiler/brw_disasm.c @@ -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)) { diff --git a/src/intel/compiler/brw_disasm_info.c b/src/intel/compiler/brw_disasm_info.c index 14cea9a77bd..079d1716b1f 100644 --- a/src/intel/compiler/brw_disasm_info.c +++ b/src/intel/compiler/brw_disasm_info.c @@ -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 * diff --git a/src/intel/compiler/brw_disasm_info.h b/src/intel/compiler/brw_disasm_info.h index 932c06b6c30..5bfa2074739 100644 --- a/src/intel/compiler/brw_disasm_info.h +++ b/src/intel/compiler/brw_disasm_info.h @@ -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, diff --git a/src/intel/compiler/brw_eu.cpp b/src/intel/compiler/brw_eu.cpp index 596894e434e..0a886b27ae2 100644 --- a/src/intel/compiler/brw_eu.cpp +++ b/src/intel/compiler/brw_eu.cpp @@ -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); + } } } diff --git a/src/intel/compiler/brw_eu.h b/src/intel/compiler/brw_eu.h index c08f3b877b9..fa7f0b7b6ed 100644 --- a/src/intel/compiler/brw_eu.h +++ b/src/intel/compiler/brw_eu.h @@ -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, diff --git a/src/intel/compiler/brw_eu_compact.c b/src/intel/compiler/brw_eu_compact.c index 45e158fb2a6..86ac0d5d5c2 100644 --- a/src/intel/compiler/brw_eu_compact.c +++ b/src/intel/compiler/brw_eu_compact.c @@ -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; diff --git a/src/intel/compiler/brw_fs_generator.cpp b/src/intel/compiler/brw_fs_generator.cpp index 5b1f28950bf..94eb84870f1 100644 --- a/src/intel/compiler/brw_fs_generator.cpp +++ b/src/intel/compiler/brw_fs_generator.cpp @@ -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); } diff --git a/src/intel/compiler/brw_vec4_generator.cpp b/src/intel/compiler/brw_vec4_generator.cpp index 3fe1edc38ac..43fcdf70ff0 100644 --- a/src/intel/compiler/brw_vec4_generator.cpp +++ b/src/intel/compiler/brw_vec4_generator.cpp @@ -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); } diff --git a/src/intel/compiler/test_eu_compact.cpp b/src/intel/compiler/test_eu_compact.cpp index bf87ff844b4..343f060b748 100644 --- a/src/intel/compiler/test_eu_compact.cpp +++ b/src/intel/compiler/test_eu_compact.cpp @@ -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; } } diff --git a/src/intel/compiler/test_eu_validate.cpp b/src/intel/compiler/test_eu_validate.cpp index 03da003c0d6..a2705199be8 100644 --- a/src/intel/compiler/test_eu_validate.cpp +++ b/src/intel/compiler/test_eu_validate.cpp @@ -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); diff --git a/src/intel/tools/i965_disasm.c b/src/intel/tools/i965_disasm.c index 47119157165..77691e2a62e 100644 --- a/src/intel/tools/i965_disasm.c +++ b/src/intel/tools/i965_disasm.c @@ -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; diff --git a/src/mesa/drivers/dri/i965/brw_disk_cache.c b/src/mesa/drivers/dri/i965/brw_disk_cache.c index 1b4cb437cbc..2b65236b449 100644 --- a/src/mesa/drivers/dri/i965/brw_disk_cache.c +++ b/src/mesa/drivers/dri/i965/brw_disk_cache.c @@ -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), diff --git a/src/mesa/drivers/dri/i965/brw_ff_gs.c b/src/mesa/drivers/dri/i965/brw_ff_gs.c index b2c4ee12f4d..6ebbc38a534 100644 --- a/src/mesa/drivers/dri/i965/brw_ff_gs.c +++ b/src/mesa/drivers/dri/i965/brw_ff_gs.c @@ -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"); } diff --git a/src/mesa/drivers/dri/i965/brw_program_cache.c b/src/mesa/drivers/dri/i965/brw_program_cache.c index 90999ab0a4f..85de8c01d65 100644 --- a/src/mesa/drivers/dri/i965/brw_program_cache.c +++ b/src/mesa/drivers/dri/i965/brw_program_cache.c @@ -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); } } } -- 2.30.2