Debian Codegraph @

src/panfrost/compiler/bifrost/bi_validate.c

raw
1 /*
2 * Copyright (C) 2021 Collabora, Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "util/u_memory.h"
7 #include "bi_debug.h"
8 #include "compiler.h"
9
10 /* Validatation doesn't make sense in release builds */
11 #ifndef NDEBUG
12
13 /* Validate that all sources are initialized in all read components. This is
14 * required for correct register allocation. We check a weaker condition, that
15 * all sources that are read are written at some point (equivalently, the live
16 * set is empty at the start of the program). TODO: Strengthen */
17
18 bool
19 bi_validate_initialization(bi_context *ctx)
20 {
21 bool success = true;
22
23 /* Calculate the live set */
24 bi_block *entry = bi_entry_block(ctx);
25 bi_compute_liveness_ssa(ctx);
26
27 /* Validate that the live set is indeed empty */
28 for (unsigned i = 0; i < ctx->ssa_alloc; ++i) {
29 if (BITSET_TEST(entry->ssa_live_in, i)) {
30 fprintf(stderr, "%u\n", i);
31 success = false;
32 }
33 }
34
35 return success;
36 }
37
38 /*
39 * Validate that there are no bi_registers accessed except at the beginning of
40 * the start block, and that preloads are unique. This ensures RA can coalesce
41 * preloads without interference tracking.
42 */
43 static bool
44 bi_validate_preload(bi_context *ctx)
45 {
46 bool start = true;
47 uint64_t preloaded = 0;
48
49 bi_foreach_block(ctx, block) {
50 bi_foreach_instr_in_block(block, I) {
51 /* No instruction should have a register destination */
52 bi_foreach_dest(I, d) {
53 if (I->dest[d].type == BI_INDEX_REGISTER)
54 return false;
55 }
56
57 /* Preloads are register moves at the start */
58 bool is_preload = start && I->op == BI_OPCODE_MOV_I32 &&
59 I->src[0].type == BI_INDEX_REGISTER;
60
61 /* After the first nonpreload, we're done preloading */
62 start &= is_preload;
63
64 /* Only preloads may have a register source */
65 bi_foreach_src(I, s) {
66 if (I->src[s].type == BI_INDEX_REGISTER && !is_preload)
67 return false;
68 }
69
70 /* Check uniqueness */
71 if (is_preload) {
72 unsigned r = I->src[0].value;
73
74 if (preloaded & BITFIELD64_BIT(r))
75 return false;
76
77 preloaded |= BITFIELD64_BIT(r);
78 }
79 }
80
81 /* Only the first block may preload */
82 start = false;
83 }
84
85 return true;
86 }
87
88 /*
89 * Type check the dimensionality of sources and destinations. This occurs in two
90 * passes, first to gather all destination sizes, second to validate all source
91 * sizes. Depends on SSA form.
92 */
93 static bool
94 bi_validate_width(bi_context *ctx)
95 {
96 bool succ = true;
97 uint8_t *width = calloc(ctx->ssa_alloc, sizeof(uint8_t));
98
99 bi_foreach_instr_global(ctx, I) {
100 bi_foreach_dest(I, d) {
101 assert(bi_is_ssa(I->dest[d]));
102
103 unsigned v = I->dest[d].value;
104 assert(width[v] == 0 && "broken SSA");
105
106 width[v] = bi_count_write_registers(I, d);
107 }
108 }
109
110 bi_foreach_instr_global(ctx, I) {
111 bi_foreach_ssa_src(I, s) {
112 unsigned v = I->src[s].value;
113 unsigned n = bi_count_read_registers(I, s);
114
115 if (width[v] != n) {
116 succ = false;
117 fprintf(stderr, "source %u, expected width %u, got width %u\n", s,
118 n, width[v]);
119 bi_print_instr(I, stderr);
120 fprintf(stderr, "\n");
121 }
122 }
123 }
124
125 free(width);
126 return succ;
127 }
128
129 /*
130 * Validate that all destinations of the instruction are present.
131 */
132 static bool
133 bi_validate_dest(bi_context *ctx)
134 {
135 bool succ = true;
136
137 bi_foreach_instr_global(ctx, I) {
138 bi_foreach_dest(I, d) {
139 if (bi_is_null(I->dest[d])) {
140 succ = false;
141 fprintf(stderr, "expected dest %u", d);
142 bi_print_instr(I, stderr);
143 fprintf(stderr, "\n");
144 }
145 }
146 }
147
148 return succ;
149 }
150
151 /*
152 * Validate that phis only appear at the beginning of blocks.
153 */
154 static bool
155 bi_validate_phi_ordering(bi_context *ctx)
156 {
157 bi_foreach_block(ctx, block) {
158 bool start = true;
159
160 bi_foreach_instr_in_block(block, I) {
161 if (start)
162 start = I->op == BI_OPCODE_PHI;
163 else if (I->op == BI_OPCODE_PHI)
164 return false;
165 }
166 }
167
168 return true;
169 }
170
171 void
172 bi_validate(bi_context *ctx, const char *after)
173 {
174 bool fail = false;
175
176 if (bifrost_debug & BIFROST_DBG_NOVALIDATE)
177 return;
178
179 if (!bi_validate_initialization(ctx)) {
180 fprintf(stderr, "Uninitialized data read after %s\n", after);
181 fail = true;
182 }
183
184 if (!bi_validate_preload(ctx)) {
185 fprintf(stderr, "Unexpected preload after %s\n", after);
186 fail = true;
187 }
188
189 if (!bi_validate_width(ctx)) {
190 fprintf(stderr, "Unexpected vector width after %s\n", after);
191 fail = true;
192 }
193
194 if (!bi_validate_dest(ctx)) {
195 fprintf(stderr, "Unexpected source/dest after %s\n", after);
196 fail = true;
197 }
198
199 if (!bi_validate_phi_ordering(ctx)) {
200 fprintf(stderr, "Unexpected phi ordering after %s\n", after);
201 fail = true;
202 }
203
204 if (fail) {
205 bi_print_shader(ctx, stderr);
206 exit(1);
207 }
208 }
209
210 #endif /* NDEBUG */