i965: Silence warning.
[mesa.git] / src / mesa / drivers / dri / i965 / intel_asm_annotation.c
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 "brw_cfg.h"
25 #include "brw_eu.h"
26 #include "brw_context.h"
27 #include "intel_debug.h"
28 #include "intel_asm_annotation.h"
29 #include "program/prog_print.h"
30 #include "program/prog_instruction.h"
31 #include "main/macros.h"
32 #include "glsl/nir/nir.h"
33
34 void
35 dump_assembly(void *assembly, int num_annotations, struct annotation *annotation,
36 const struct brw_device_info *devinfo)
37 {
38 const char *last_annotation_string = NULL;
39 const void *last_annotation_ir = NULL;
40
41 for (int i = 0; i < num_annotations; i++) {
42 int start_offset = annotation[i].offset;
43 int end_offset = annotation[i + 1].offset;
44
45 if (annotation[i].block_start) {
46 fprintf(stderr, " START B%d", annotation[i].block_start->num);
47 foreach_list_typed(struct bblock_link, predecessor_link, link,
48 &annotation[i].block_start->parents) {
49 struct bblock_t *predecessor_block = predecessor_link->block;
50 fprintf(stderr, " <-B%d", predecessor_block->num);
51 }
52 fprintf(stderr, "\n");
53 }
54
55 if (last_annotation_ir != annotation[i].ir) {
56 last_annotation_ir = annotation[i].ir;
57 if (last_annotation_ir) {
58 fprintf(stderr, " ");
59 nir_print_instr(annotation[i].ir, stderr);
60 fprintf(stderr, "\n");
61 }
62 }
63
64 if (last_annotation_string != annotation[i].annotation) {
65 last_annotation_string = annotation[i].annotation;
66 if (last_annotation_string)
67 fprintf(stderr, " %s\n", last_annotation_string);
68 }
69
70 brw_disassemble(devinfo, assembly, start_offset, end_offset, stderr);
71
72 if (annotation[i].error) {
73 fputs(annotation[i].error, stderr);
74 }
75
76 if (annotation[i].block_end) {
77 fprintf(stderr, " END B%d", annotation[i].block_end->num);
78 foreach_list_typed(struct bblock_link, successor_link, link,
79 &annotation[i].block_end->children) {
80 struct bblock_t *successor_block = successor_link->block;
81 fprintf(stderr, " ->B%d", successor_block->num);
82 }
83 fprintf(stderr, "\n");
84 }
85 }
86 fprintf(stderr, "\n");
87 }
88
89 static bool
90 annotation_array_ensure_space(struct annotation_info *annotation)
91 {
92 if (annotation->ann_size <= annotation->ann_count) {
93 int old_size = annotation->ann_size;
94 annotation->ann_size = MAX2(1024, annotation->ann_size * 2);
95 annotation->ann = reralloc(annotation->mem_ctx, annotation->ann,
96 struct annotation, annotation->ann_size);
97 if (!annotation->ann)
98 return false;
99
100 memset(annotation->ann + old_size, 0,
101 (annotation->ann_size - old_size) * sizeof(struct annotation));
102 }
103
104 return true;
105 }
106
107 void annotate(const struct brw_device_info *devinfo,
108 struct annotation_info *annotation, const struct cfg_t *cfg,
109 struct backend_instruction *inst, unsigned offset)
110 {
111 if (annotation->mem_ctx == NULL)
112 annotation->mem_ctx = ralloc_context(NULL);
113
114 if (!annotation_array_ensure_space(annotation))
115 return;
116
117 struct annotation *ann = &annotation->ann[annotation->ann_count++];
118 ann->offset = offset;
119 if ((INTEL_DEBUG & DEBUG_ANNOTATION) != 0) {
120 ann->ir = inst->ir;
121 ann->annotation = inst->annotation;
122 }
123
124 if (bblock_start(cfg->blocks[annotation->cur_block]) == inst) {
125 ann->block_start = cfg->blocks[annotation->cur_block];
126 }
127
128 if (bblock_end(cfg->blocks[annotation->cur_block]) == inst) {
129 ann->block_end = cfg->blocks[annotation->cur_block];
130 annotation->cur_block++;
131 }
132
133 /* Merge this annotation with the previous if possible. */
134 struct annotation *prev = annotation->ann_count > 1 ?
135 &annotation->ann[annotation->ann_count - 2] : NULL;
136 if (prev != NULL &&
137 ann->ir == prev->ir &&
138 ann->annotation == prev->annotation &&
139 ann->block_start == NULL &&
140 prev->block_end == NULL) {
141 if (ann->block_end == NULL)
142 annotation->ann_count--;
143 return;
144 }
145
146 /* There is no hardware DO instruction on Gen6+, so since DO always
147 * starts a basic block, we need to set the .block_start of the next
148 * instruction's annotation with a pointer to the bblock started by
149 * the DO.
150 *
151 * There's also only complication from emitting an annotation without
152 * a corresponding hardware instruction to disassemble.
153 */
154 if (devinfo->gen >= 6 && inst->opcode == BRW_OPCODE_DO) {
155 annotation->ann_count--;
156 }
157 }
158
159 void
160 annotation_finalize(struct annotation_info *annotation,
161 unsigned next_inst_offset)
162 {
163 if (!annotation->ann_count)
164 return;
165
166 if (annotation->ann_count == annotation->ann_size) {
167 annotation->ann = reralloc(annotation->mem_ctx, annotation->ann,
168 struct annotation, annotation->ann_size + 1);
169 }
170 annotation->ann[annotation->ann_count].offset = next_inst_offset;
171 }
172
173 void
174 annotation_insert_error(struct annotation_info *annotation, unsigned offset,
175 const char *error)
176 {
177 struct annotation *ann;
178
179 if (!annotation->ann_count)
180 return;
181
182 /* We may have to split an annotation, so ensure we have enough space
183 * allocated for that case up front.
184 */
185 if (!annotation_array_ensure_space(annotation))
186 return;
187
188 assume(annotation->ann_count > 0);
189
190 for (int i = 0; i < annotation->ann_count; i++) {
191 struct annotation *cur = &annotation->ann[i];
192 struct annotation *next = &annotation->ann[i + 1];
193 ann = cur;
194
195 if (next->offset <= offset)
196 continue;
197
198 if (offset + sizeof(brw_inst) != next->offset) {
199 memmove(next, cur,
200 (annotation->ann_count - i + 2) * sizeof(struct annotation));
201 cur->error = NULL;
202 cur->error_length = 0;
203 cur->block_end = NULL;
204 next->offset = offset + sizeof(brw_inst);
205 next->block_start = NULL;
206 annotation->ann_count++;
207 }
208 break;
209 }
210
211 if (ann->error)
212 ralloc_strcat(&ann->error, error);
213 else
214 ann->error = ralloc_strdup(annotation->mem_ctx, error);
215 }