xref: /aosp_15_r20/external/mesa3d/src/intel/compiler/elk/elk_disasm_info.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2014 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "elk_cfg.h"
25 #include "elk_eu.h"
26 #include "elk_disasm.h"
27 #include "elk_disasm_info.h"
28 #include "dev/intel_debug.h"
29 #include "compiler/nir/nir.h"
30 
31 void
elk_dump_assembly(void * assembly,int start_offset,int end_offset,struct elk_disasm_info * disasm,const unsigned * block_latency)32 elk_dump_assembly(void *assembly, int start_offset, int end_offset,
33               struct elk_disasm_info *disasm, const unsigned *block_latency)
34 {
35    const struct elk_isa_info *isa = disasm->isa;
36    const char *last_annotation_string = NULL;
37    const void *last_annotation_ir = NULL;
38 
39    void *mem_ctx = ralloc_context(NULL);
40    const struct elk_label *root_label =
41       elk_label_assembly(isa, assembly, start_offset, end_offset, mem_ctx);
42 
43    foreach_list_typed(struct inst_group, group, link, &disasm->group_list) {
44       struct exec_node *next_node = exec_node_get_next(&group->link);
45       if (exec_node_is_tail_sentinel(next_node))
46          break;
47 
48       struct inst_group *next =
49          exec_node_data(struct inst_group, next_node, link);
50 
51       int start_offset = group->offset;
52       int end_offset = next->offset;
53 
54       if (group->block_start) {
55          fprintf(stderr, "   START B%d", group->block_start->num);
56          foreach_list_typed(struct elk_bblock_link, predecessor_link, link,
57                             &group->block_start->parents) {
58             struct elk_bblock_t *predecessor_block = predecessor_link->block;
59             fprintf(stderr, " <-B%d", predecessor_block->num);
60          }
61          if (block_latency)
62             fprintf(stderr, " (%u cycles)",
63                     block_latency[group->block_start->num]);
64          fprintf(stderr, "\n");
65       }
66 
67       if (last_annotation_ir != group->ir) {
68          last_annotation_ir = group->ir;
69          if (last_annotation_ir) {
70             fprintf(stderr, "   ");
71             nir_print_instr(group->ir, stderr);
72             fprintf(stderr, "\n");
73          }
74       }
75 
76       if (last_annotation_string != group->annotation) {
77          last_annotation_string = group->annotation;
78          if (last_annotation_string)
79             fprintf(stderr, "   %s\n", last_annotation_string);
80       }
81 
82       elk_disassemble(isa, assembly, start_offset, end_offset,
83                       root_label, stderr);
84 
85       if (group->error) {
86          fputs(group->error, stderr);
87       }
88 
89       if (group->block_end) {
90          fprintf(stderr, "   END B%d", group->block_end->num);
91          foreach_list_typed(struct elk_bblock_link, successor_link, link,
92                             &group->block_end->children) {
93             struct elk_bblock_t *successor_block = successor_link->block;
94             fprintf(stderr, " ->B%d", successor_block->num);
95          }
96          fprintf(stderr, "\n");
97       }
98    }
99    fprintf(stderr, "\n");
100 
101    ralloc_free(mem_ctx);
102 }
103 
104 struct elk_disasm_info *
elk_disasm_initialize(const struct elk_isa_info * isa,const struct elk_cfg_t * cfg)105 elk_disasm_initialize(const struct elk_isa_info *isa,
106                   const struct elk_cfg_t *cfg)
107 {
108    struct elk_disasm_info *disasm = ralloc(NULL, struct elk_disasm_info);
109    exec_list_make_empty(&disasm->group_list);
110    disasm->isa = isa;
111    disasm->cfg = cfg;
112    disasm->cur_block = 0;
113    disasm->use_tail = false;
114    return disasm;
115 }
116 
117 struct inst_group *
elk_disasm_new_inst_group(struct elk_disasm_info * disasm,unsigned next_inst_offset)118 elk_disasm_new_inst_group(struct elk_disasm_info *disasm, unsigned next_inst_offset)
119 {
120    struct inst_group *tail = rzalloc(disasm, struct inst_group);
121    tail->offset = next_inst_offset;
122    exec_list_push_tail(&disasm->group_list, &tail->link);
123    return tail;
124 }
125 
126 void
elk_disasm_annotate(struct elk_disasm_info * disasm,struct elk_backend_instruction * inst,unsigned offset)127 elk_disasm_annotate(struct elk_disasm_info *disasm,
128                 struct elk_backend_instruction *inst, unsigned offset)
129 {
130    const struct intel_device_info *devinfo = disasm->isa->devinfo;
131    const struct elk_cfg_t *cfg = disasm->cfg;
132 
133    struct inst_group *group;
134    if (!disasm->use_tail) {
135       group = elk_disasm_new_inst_group(disasm, offset);
136    } else {
137       disasm->use_tail = false;
138       group = exec_node_data(struct inst_group,
139                              exec_list_get_tail_raw(&disasm->group_list), link);
140    }
141 
142    if (INTEL_DEBUG(DEBUG_ANNOTATION)) {
143       group->ir = inst->ir;
144       group->annotation = inst->annotation;
145    }
146 
147    if (bblock_start(cfg->blocks[disasm->cur_block]) == inst) {
148       group->block_start = cfg->blocks[disasm->cur_block];
149    }
150 
151    /* There is no hardware DO instruction on Gfx6+, so since DO always
152     * starts a basic block, we need to set the .block_start of the next
153     * instruction's annotation with a pointer to the bblock started by
154     * the DO.
155     *
156     * There's also only complication from emitting an annotation without
157     * a corresponding hardware instruction to disassemble.
158     */
159    if (devinfo->ver >= 6 && inst->opcode == ELK_OPCODE_DO) {
160       disasm->use_tail = true;
161    }
162 
163    if (bblock_end(cfg->blocks[disasm->cur_block]) == inst) {
164       group->block_end = cfg->blocks[disasm->cur_block];
165       disasm->cur_block++;
166    }
167 }
168 
169 void
elk_disasm_insert_error(struct elk_disasm_info * disasm,unsigned offset,unsigned inst_size,const char * error)170 elk_disasm_insert_error(struct elk_disasm_info *disasm, unsigned offset,
171                     unsigned inst_size, const char *error)
172 {
173    foreach_list_typed(struct inst_group, cur, link, &disasm->group_list) {
174       struct exec_node *next_node = exec_node_get_next(&cur->link);
175       if (exec_node_is_tail_sentinel(next_node))
176          break;
177 
178       struct inst_group *next =
179          exec_node_data(struct inst_group, next_node, link);
180 
181       if (next->offset <= offset)
182          continue;
183 
184       if (offset + inst_size != next->offset) {
185          struct inst_group *new = ralloc(disasm, struct inst_group);
186          memcpy(new, cur, sizeof(struct inst_group));
187 
188          cur->error = NULL;
189          cur->error_length = 0;
190          cur->block_end = NULL;
191 
192          new->offset = offset + inst_size;
193          new->block_start = NULL;
194 
195          exec_node_insert_after(&cur->link, &new->link);
196       }
197 
198       if (cur->error)
199          ralloc_strcat(&cur->error, error);
200       else
201          cur->error = ralloc_strdup(disasm, error);
202       return;
203    }
204 }
205