Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010 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 |
| 21 | * DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * \file ir_validate.cpp |
| 26 | * |
| 27 | * Attempts to verify that various invariants of the IR tree are true. |
| 28 | * |
| 29 | * In particular, at the moment it makes sure that no single |
| 30 | * ir_instruction node except for ir_variable appears multiple times |
| 31 | * in the ir tree. ir_variable does appear multiple times: Once as a |
| 32 | * declaration in an exec_list, and multiple times as the endpoint of |
| 33 | * a dereference chain. |
| 34 | */ |
| 35 | |
| 36 | #include <inttypes.h> |
| 37 | #include "ir.h" |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 38 | #include "ir_hierarchical_visitor.h" |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 39 | #include "hash_table.h" |
Eric Anholt | f141fa6 | 2010-07-20 17:18:57 -0700 | [diff] [blame] | 40 | #include "glsl_types.h" |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 41 | |
| 42 | class ir_validate : public ir_hierarchical_visitor { |
| 43 | public: |
| 44 | ir_validate() |
| 45 | { |
Ian Romanick | d1a1ee5 | 2010-07-06 14:49:14 -0700 | [diff] [blame] | 46 | this->ht = hash_table_ctor(0, hash_table_pointer_hash, |
| 47 | hash_table_pointer_compare); |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 48 | |
Ian Romanick | c67016d | 2010-07-02 13:30:23 -0700 | [diff] [blame] | 49 | this->current_function = NULL; |
| 50 | |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 51 | this->callback = ir_validate::validate_ir; |
| 52 | this->data = ht; |
| 53 | } |
| 54 | |
| 55 | ~ir_validate() |
| 56 | { |
| 57 | hash_table_dtor(this->ht); |
| 58 | } |
| 59 | |
| 60 | virtual ir_visitor_status visit(ir_variable *v); |
Ian Romanick | 8baf21b | 2010-07-12 13:55:32 -0700 | [diff] [blame] | 61 | virtual ir_visitor_status visit(ir_dereference_variable *ir); |
Eric Anholt | 432b787 | 2010-07-22 16:24:49 -0700 | [diff] [blame] | 62 | virtual ir_visitor_status visit(ir_if *ir); |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 63 | |
Ian Romanick | c67016d | 2010-07-02 13:30:23 -0700 | [diff] [blame] | 64 | virtual ir_visitor_status visit_enter(ir_function *ir); |
| 65 | virtual ir_visitor_status visit_leave(ir_function *ir); |
| 66 | virtual ir_visitor_status visit_enter(ir_function_signature *ir); |
| 67 | |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 68 | static void validate_ir(ir_instruction *ir, void *data); |
| 69 | |
Ian Romanick | c67016d | 2010-07-02 13:30:23 -0700 | [diff] [blame] | 70 | ir_function *current_function; |
| 71 | |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 72 | struct hash_table *ht; |
| 73 | }; |
| 74 | |
Ian Romanick | 8baf21b | 2010-07-12 13:55:32 -0700 | [diff] [blame] | 75 | |
| 76 | ir_visitor_status |
| 77 | ir_validate::visit(ir_dereference_variable *ir) |
| 78 | { |
| 79 | if ((ir->var == NULL) || (ir->var->as_variable() == NULL)) { |
| 80 | printf("ir_dereference_variable @ %p does not specify a variable %p\n", |
| 81 | ir, ir->var); |
| 82 | abort(); |
| 83 | } |
| 84 | |
| 85 | if (hash_table_find(ht, ir->var) == NULL) { |
| 86 | printf("ir_dereference_variable @ %p specifies undeclared variable " |
| 87 | "`%s' @ %p\n", |
| 88 | ir, ir->var->name, ir->var); |
| 89 | abort(); |
| 90 | } |
| 91 | |
Ian Romanick | 506880b | 2010-07-12 15:46:16 -0700 | [diff] [blame] | 92 | this->validate_ir(ir, this->data); |
| 93 | |
Ian Romanick | 8baf21b | 2010-07-12 13:55:32 -0700 | [diff] [blame] | 94 | return visit_continue; |
| 95 | } |
| 96 | |
Eric Anholt | 432b787 | 2010-07-22 16:24:49 -0700 | [diff] [blame] | 97 | ir_visitor_status |
| 98 | ir_validate::visit(ir_if *ir) |
| 99 | { |
| 100 | if (ir->condition->type != glsl_type::bool_type) { |
| 101 | printf("ir_if condition %s type instead of bool.\n", |
| 102 | ir->condition->type->name); |
| 103 | ir->print(); |
| 104 | printf("\n"); |
| 105 | abort(); |
| 106 | } |
Eric Anholt | 6a1401e | 2010-07-27 00:18:33 -0700 | [diff] [blame^] | 107 | |
| 108 | return visit_continue; |
Eric Anholt | 432b787 | 2010-07-22 16:24:49 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Ian Romanick | 8baf21b | 2010-07-12 13:55:32 -0700 | [diff] [blame] | 111 | |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 112 | ir_visitor_status |
Ian Romanick | c67016d | 2010-07-02 13:30:23 -0700 | [diff] [blame] | 113 | ir_validate::visit_enter(ir_function *ir) |
| 114 | { |
| 115 | /* Function definitions cannot be nested. |
| 116 | */ |
| 117 | if (this->current_function != NULL) { |
| 118 | printf("Function definition nested inside another function " |
| 119 | "definition:\n"); |
| 120 | printf("%s %p inside %s %p\n", |
| 121 | ir->name, ir, |
| 122 | this->current_function->name, this->current_function); |
| 123 | abort(); |
| 124 | } |
| 125 | |
| 126 | /* Store the current function hierarchy being traversed. This is used |
| 127 | * by the function signature visitor to ensure that the signatures are |
| 128 | * linked with the correct functions. |
| 129 | */ |
| 130 | this->current_function = ir; |
| 131 | |
| 132 | this->validate_ir(ir, this->data); |
| 133 | |
| 134 | return visit_continue; |
| 135 | } |
| 136 | |
| 137 | ir_visitor_status |
| 138 | ir_validate::visit_leave(ir_function *ir) |
| 139 | { |
| 140 | (void) ir; |
| 141 | |
| 142 | this->current_function = NULL; |
| 143 | return visit_continue; |
| 144 | } |
| 145 | |
| 146 | ir_visitor_status |
| 147 | ir_validate::visit_enter(ir_function_signature *ir) |
| 148 | { |
| 149 | if (this->current_function != ir->function()) { |
| 150 | printf("Function signature nested inside wrong function " |
| 151 | "definition:\n"); |
| 152 | printf("%p inside %s %p instead of %s %p\n", |
| 153 | ir, |
| 154 | this->current_function->name, this->current_function, |
| 155 | ir->function_name(), ir->function()); |
| 156 | abort(); |
| 157 | } |
| 158 | |
| 159 | this->validate_ir(ir, this->data); |
| 160 | |
| 161 | return visit_continue; |
| 162 | } |
| 163 | |
| 164 | ir_visitor_status |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 165 | ir_validate::visit(ir_variable *ir) |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 166 | { |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 167 | /* An ir_variable is the one thing that can (and will) appear multiple times |
Ian Romanick | 8baf21b | 2010-07-12 13:55:32 -0700 | [diff] [blame] | 168 | * in an IR tree. It is added to the hashtable so that it can be used |
| 169 | * in the ir_dereference_variable handler to ensure that a variable is |
| 170 | * declared before it is dereferenced. |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 171 | */ |
Ian Romanick | 8baf21b | 2010-07-12 13:55:32 -0700 | [diff] [blame] | 172 | hash_table_insert(ht, ir, ir); |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 173 | return visit_continue; |
| 174 | } |
| 175 | |
| 176 | void |
| 177 | ir_validate::validate_ir(ir_instruction *ir, void *data) |
| 178 | { |
| 179 | struct hash_table *ht = (struct hash_table *) data; |
| 180 | |
| 181 | if (hash_table_find(ht, ir)) { |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 182 | printf("Instruction node present twice in ir tree:\n"); |
| 183 | ir->print(); |
| 184 | printf("\n"); |
| 185 | abort(); |
| 186 | } |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 187 | hash_table_insert(ht, ir, ir); |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void |
Eric Anholt | d16044a | 2010-07-19 09:05:42 -0700 | [diff] [blame] | 191 | check_node_type(ir_instruction *ir, void *data) |
| 192 | { |
| 193 | if (ir->ir_type <= ir_type_unset || ir->ir_type >= ir_type_max) { |
| 194 | printf("Instruction node with unset type\n"); |
| 195 | ir->print(); printf("\n"); |
| 196 | } |
Eric Anholt | f141fa6 | 2010-07-20 17:18:57 -0700 | [diff] [blame] | 197 | assert(ir->type != glsl_type::error_type); |
Eric Anholt | d16044a | 2010-07-19 09:05:42 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 201 | validate_ir_tree(exec_list *instructions) |
| 202 | { |
| 203 | ir_validate v; |
| 204 | |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 205 | v.run(instructions); |
Eric Anholt | d16044a | 2010-07-19 09:05:42 -0700 | [diff] [blame] | 206 | |
| 207 | foreach_iter(exec_list_iterator, iter, *instructions) { |
| 208 | ir_instruction *ir = (ir_instruction *)iter.get(); |
| 209 | |
| 210 | visit_tree(ir, check_node_type, NULL); |
| 211 | } |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 212 | } |