• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Alyssa Rosenzweig
3  * Copyright (C) 2021 Collabora, Ltd.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTAagxLITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIAagxLITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 #include "agx_compiler.h"
26 
27 /* Validatation doesn't make sense in release builds */
28 #ifndef NDEBUG
29 
30 #define agx_validate_assert(stmt) if (!(stmt)) { return false; }
31 
32 /*
33  * If a block contains phi nodes, they must come at the start of the block. If a
34  * block contains control flow, it must come after a p_logical_end marker.
35  * Therefore the form of a valid block is:
36  *
37  *       Phi nodes
38  *       General instructions
39  *       Logical end
40  *       Control flow instructions
41  *
42  * Validate that this form is satisfied.
43  *
44  * XXX: This only applies before we delete the logical end instructions, maybe
45  * that should be deferred though?
46  */
47 enum agx_block_state {
48    AGX_BLOCK_STATE_PHI = 0,
49    AGX_BLOCK_STATE_BODY = 1,
50    AGX_BLOCK_STATE_CF = 2
51 };
52 
53 static bool
agx_validate_block_form(agx_block * block)54 agx_validate_block_form(agx_block *block)
55 {
56    enum agx_block_state state = AGX_BLOCK_STATE_PHI;
57 
58    agx_foreach_instr_in_block(block, I) {
59       switch (I->op) {
60       case AGX_OPCODE_PHI:
61          agx_validate_assert(state == AGX_BLOCK_STATE_PHI);
62          break;
63 
64       default:
65          agx_validate_assert(state != AGX_BLOCK_STATE_CF);
66          state = AGX_BLOCK_STATE_BODY;
67          break;
68 
69       case AGX_OPCODE_P_LOGICAL_END:
70          agx_validate_assert(state != AGX_BLOCK_STATE_CF);
71          state = AGX_BLOCK_STATE_CF;
72          break;
73 
74       case AGX_OPCODE_JMP_EXEC_ANY:
75       case AGX_OPCODE_JMP_EXEC_NONE:
76       case AGX_OPCODE_POP_EXEC:
77       case AGX_OPCODE_IF_ICMP:
78       case AGX_OPCODE_ELSE_ICMP:
79       case AGX_OPCODE_WHILE_ICMP:
80       case AGX_OPCODE_IF_FCMP:
81       case AGX_OPCODE_ELSE_FCMP:
82       case AGX_OPCODE_WHILE_FCMP:
83          agx_validate_assert(state == AGX_BLOCK_STATE_CF);
84          break;
85       }
86    }
87 
88    return true;
89 }
90 
91 void
agx_validate(agx_context * ctx,const char * after)92 agx_validate(agx_context *ctx, const char *after)
93 {
94    bool fail = false;
95 
96    if (agx_debug & AGX_DBG_NOVALIDATE)
97       return;
98 
99    agx_foreach_block(ctx, block) {
100       if (!agx_validate_block_form(block)) {
101          fprintf(stderr, "Invalid block form after %s\n", after);
102          agx_print_block(block, stdout);
103          fail = true;
104       }
105    }
106 
107    /* TODO: Validate more invariants */
108 
109    if (fail) {
110       agx_print_shader(ctx, stderr);
111       exit(1);
112    }
113 }
114 
115 #endif /* NDEBUG */
116