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_invalidate_liveness(ctx);
44         bi_compute_liveness(ctx);
45 
46         /* Validate that the live set is indeed empty */
47         for (unsigned i = 0; i < temp_count; ++i) {
48                 if (entry->live_in[i] == 0) continue;
49 
50                 fprintf(stderr, "%s%u\n", (i & PAN_IS_REG) ? "r" : "", i >> 1);
51                 success = false;
52         }
53 
54         return success;
55 }
56 
57 void
bi_validate(bi_context * ctx,const char * after)58 bi_validate(bi_context *ctx, const char *after)
59 {
60         bool fail = false;
61 
62         if (bifrost_debug & BIFROST_DBG_NOVALIDATE)
63                 return;
64 
65         if (!bi_validate_initialization(ctx)) {
66                 fprintf(stderr, "Uninitialized data read after %s\n", after);
67                 fail = true;
68         }
69 
70         /* TODO: Validate more invariants */
71 
72         if (fail) {
73                 bi_print_shader(ctx, stderr);
74                 exit(1);
75         }
76 }
77 
78 #endif /* NDEBUG */
79