• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Collabora, Ltd.
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 FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #include "compiler.h"
25 #include "util/u_memory.h"
26 
27 /* Validatation doesn't make sense in release builds */
28 #ifndef NDEBUG
29 
30 /* Validate that all sources are initialized in all read components. This is
31  * required for correct register allocation. We check a weaker condition, that
32  * all sources that are read are written at some point (equivalently, the live
33  * set is empty at the start of the program). TODO: Strengthen */
34 
35 bool
bi_validate_initialization(bi_context * ctx)36 bi_validate_initialization(bi_context *ctx)
37 {
38         bool success = true;
39 
40         /* Calculate the live set */
41         bi_block *entry = bi_entry_block(ctx);
42         unsigned temp_count = bi_max_temp(ctx);
43         bi_compute_liveness(ctx);
44 
45         /* Validate that the live set is indeed empty */
46         for (unsigned i = 0; i < temp_count; ++i) {
47                 if (entry->live_in[i] == 0) continue;
48 
49                 fprintf(stderr, "%s%u\n", (i & PAN_IS_REG) ? "r" : "", i >> 1);
50                 success = false;
51         }
52 
53         return success;
54 }
55 
56 /*
57  * Validate that there are no bi_registers accessed except at the beginning of
58  * the start block, and that preloads are unique. This ensures RA can coalesce
59  * preloads without interference tracking.
60  */
61 static bool
bi_validate_preload(bi_context * ctx)62 bi_validate_preload(bi_context *ctx)
63 {
64         bool start = true;
65         uint64_t preloaded = 0;
66 
67         bi_foreach_block(ctx, block) {
68                 bi_foreach_instr_in_block(block, I) {
69                         /* No instruction should have a register destination */
70                         bi_foreach_dest(I, d) {
71                                 if (I->dest[d].type == BI_INDEX_REGISTER)
72                                         return false;
73                         }
74 
75                         /* Preloads are register moves at the start */
76                         bool is_preload =
77                                 start && I->op == BI_OPCODE_MOV_I32 &&
78                                 I->src[0].type == BI_INDEX_REGISTER;
79 
80                         /* After the first nonpreload, we're done preloading */
81                         start &= is_preload;
82 
83                         /* Only preloads may have a register source */
84                         bi_foreach_src(I, s) {
85                                 if (I->src[s].type == BI_INDEX_REGISTER && !is_preload)
86                                         return false;
87                         }
88 
89                         /* Check uniqueness */
90                         if (is_preload) {
91                                 unsigned r = I->src[0].value;
92 
93                                 if (preloaded & BITFIELD64_BIT(r))
94                                         return false;
95 
96                                 preloaded |= BITFIELD64_BIT(r);
97                         }
98                 }
99 
100                 /* Only the first block may preload */
101                 start = false;
102         }
103 
104         return true;
105 }
106 
107 /*
108  * Type check the dimensionality of sources and destinations. This occurs in two
109  * passes, first to gather all destination sizes, second to validate all source
110  * sizes. Depends on SSA form.
111  */
112 static bool
bi_validate_width(bi_context * ctx)113 bi_validate_width(bi_context *ctx)
114 {
115         bool succ = true;
116         uint8_t *width = calloc(ctx->ssa_alloc, sizeof(uint8_t));
117 
118         bi_foreach_instr_global(ctx, I) {
119                 bi_foreach_dest(I, d) {
120                         if (bi_is_null(I->dest[d])) continue;
121                         if (!bi_is_ssa(I->dest[d])) continue;
122 
123                         unsigned v = I->dest[d].value;
124                         assert(width[v] == 0 && "broken SSA");
125 
126                         width[v] = bi_count_write_registers(I, d);
127                 }
128         }
129 
130         bi_foreach_instr_global(ctx, I) {
131                 bi_foreach_src(I, s) {
132                         if (!bi_is_ssa(I->src[s])) continue;
133 
134                         unsigned v = I->src[s].value;
135                         unsigned n = bi_count_read_registers(I, s);
136 
137                         if (width[v] != n) {
138                                 succ = false;
139                                 fprintf(stderr,
140                                         "source %u, expected width %u, got width %u\n",
141                                         s, n, width[v]);
142                                 bi_print_instr(I, stderr);
143                                 fprintf(stderr, "\n");
144                         }
145                 }
146         }
147 
148         free(width);
149         return succ;
150 }
151 
152 void
bi_validate(bi_context * ctx,const char * after)153 bi_validate(bi_context *ctx, const char *after)
154 {
155         bool fail = false;
156 
157         if (bifrost_debug & BIFROST_DBG_NOVALIDATE)
158                 return;
159 
160         if (!bi_validate_initialization(ctx)) {
161                 fprintf(stderr, "Uninitialized data read after %s\n", after);
162                 fail = true;
163         }
164 
165         if (!bi_validate_preload(ctx)) {
166                 fprintf(stderr, "Unexpected preload after %s\n", after);
167                 fail = true;
168         }
169 
170         if (!bi_validate_width(ctx)) {
171                 fprintf(stderr, "Unexpected vector with after %s\n", after);
172                 fail = true;
173         }
174 
175         if (fail) {
176                 bi_print_shader(ctx, stderr);
177                 exit(1);
178         }
179 }
180 
181 #endif /* NDEBUG */
182