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" |
Aras Pranckevicius | 3174715 | 2010-07-29 12:40:49 +0300 | [diff] [blame] | 39 | #include "program/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 | |
Eric Anholt | 5533c6e | 2010-07-27 09:01:30 -0700 | [diff] [blame] | 68 | virtual ir_visitor_status visit_leave(ir_expression *ir); |
| 69 | |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 70 | static void validate_ir(ir_instruction *ir, void *data); |
| 71 | |
Ian Romanick | c67016d | 2010-07-02 13:30:23 -0700 | [diff] [blame] | 72 | ir_function *current_function; |
| 73 | |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 74 | struct hash_table *ht; |
| 75 | }; |
| 76 | |
Ian Romanick | 8baf21b | 2010-07-12 13:55:32 -0700 | [diff] [blame] | 77 | |
| 78 | ir_visitor_status |
| 79 | ir_validate::visit(ir_dereference_variable *ir) |
| 80 | { |
| 81 | if ((ir->var == NULL) || (ir->var->as_variable() == NULL)) { |
| 82 | printf("ir_dereference_variable @ %p does not specify a variable %p\n", |
| 83 | ir, ir->var); |
| 84 | abort(); |
| 85 | } |
| 86 | |
| 87 | if (hash_table_find(ht, ir->var) == NULL) { |
| 88 | printf("ir_dereference_variable @ %p specifies undeclared variable " |
| 89 | "`%s' @ %p\n", |
| 90 | ir, ir->var->name, ir->var); |
| 91 | abort(); |
| 92 | } |
| 93 | |
Ian Romanick | 506880b | 2010-07-12 15:46:16 -0700 | [diff] [blame] | 94 | this->validate_ir(ir, this->data); |
| 95 | |
Ian Romanick | 8baf21b | 2010-07-12 13:55:32 -0700 | [diff] [blame] | 96 | return visit_continue; |
| 97 | } |
| 98 | |
Eric Anholt | 432b787 | 2010-07-22 16:24:49 -0700 | [diff] [blame] | 99 | ir_visitor_status |
| 100 | ir_validate::visit(ir_if *ir) |
| 101 | { |
| 102 | if (ir->condition->type != glsl_type::bool_type) { |
| 103 | printf("ir_if condition %s type instead of bool.\n", |
| 104 | ir->condition->type->name); |
| 105 | ir->print(); |
| 106 | printf("\n"); |
| 107 | abort(); |
| 108 | } |
Eric Anholt | 6a1401e | 2010-07-27 00:18:33 -0700 | [diff] [blame] | 109 | |
| 110 | return visit_continue; |
Eric Anholt | 432b787 | 2010-07-22 16:24:49 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Ian Romanick | 8baf21b | 2010-07-12 13:55:32 -0700 | [diff] [blame] | 113 | |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 114 | ir_visitor_status |
Ian Romanick | c67016d | 2010-07-02 13:30:23 -0700 | [diff] [blame] | 115 | ir_validate::visit_enter(ir_function *ir) |
| 116 | { |
| 117 | /* Function definitions cannot be nested. |
| 118 | */ |
| 119 | if (this->current_function != NULL) { |
| 120 | printf("Function definition nested inside another function " |
| 121 | "definition:\n"); |
| 122 | printf("%s %p inside %s %p\n", |
| 123 | ir->name, ir, |
| 124 | this->current_function->name, this->current_function); |
| 125 | abort(); |
| 126 | } |
| 127 | |
| 128 | /* Store the current function hierarchy being traversed. This is used |
| 129 | * by the function signature visitor to ensure that the signatures are |
| 130 | * linked with the correct functions. |
| 131 | */ |
| 132 | this->current_function = ir; |
| 133 | |
| 134 | this->validate_ir(ir, this->data); |
| 135 | |
| 136 | return visit_continue; |
| 137 | } |
| 138 | |
| 139 | ir_visitor_status |
| 140 | ir_validate::visit_leave(ir_function *ir) |
| 141 | { |
Eric Anholt | ee7666b | 2010-08-02 12:08:52 -0700 | [diff] [blame^] | 142 | assert(talloc_parent(ir->name) == ir); |
Ian Romanick | c67016d | 2010-07-02 13:30:23 -0700 | [diff] [blame] | 143 | |
| 144 | this->current_function = NULL; |
| 145 | return visit_continue; |
| 146 | } |
| 147 | |
| 148 | ir_visitor_status |
| 149 | ir_validate::visit_enter(ir_function_signature *ir) |
| 150 | { |
| 151 | if (this->current_function != ir->function()) { |
| 152 | printf("Function signature nested inside wrong function " |
| 153 | "definition:\n"); |
| 154 | printf("%p inside %s %p instead of %s %p\n", |
| 155 | ir, |
| 156 | this->current_function->name, this->current_function, |
| 157 | ir->function_name(), ir->function()); |
| 158 | abort(); |
| 159 | } |
| 160 | |
| 161 | this->validate_ir(ir, this->data); |
| 162 | |
| 163 | return visit_continue; |
| 164 | } |
| 165 | |
| 166 | ir_visitor_status |
Eric Anholt | 5533c6e | 2010-07-27 09:01:30 -0700 | [diff] [blame] | 167 | ir_validate::visit_leave(ir_expression *ir) |
| 168 | { |
| 169 | switch (ir->operation) { |
| 170 | case ir_unop_bit_not: |
| 171 | assert(ir->operands[0]->type == ir->type); |
| 172 | break; |
| 173 | case ir_unop_logic_not: |
Eric Anholt | e75dbf6 | 2010-08-02 12:06:34 -0700 | [diff] [blame] | 174 | assert(ir->type->base_type == GLSL_TYPE_BOOL); |
| 175 | assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL); |
Eric Anholt | 5533c6e | 2010-07-27 09:01:30 -0700 | [diff] [blame] | 176 | break; |
| 177 | |
| 178 | case ir_unop_neg: |
| 179 | case ir_unop_abs: |
| 180 | case ir_unop_sign: |
| 181 | case ir_unop_rcp: |
| 182 | case ir_unop_rsq: |
| 183 | case ir_unop_sqrt: |
| 184 | case ir_unop_exp: |
| 185 | case ir_unop_log: |
| 186 | case ir_unop_exp2: |
| 187 | case ir_unop_log2: |
| 188 | assert(ir->type == ir->operands[0]->type); |
| 189 | break; |
| 190 | |
| 191 | case ir_unop_f2i: |
| 192 | assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 193 | assert(ir->type->base_type == GLSL_TYPE_INT); |
| 194 | break; |
| 195 | case ir_unop_i2f: |
| 196 | assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT); |
| 197 | assert(ir->type->base_type == GLSL_TYPE_FLOAT); |
| 198 | break; |
| 199 | case ir_unop_f2b: |
| 200 | assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 201 | assert(ir->type->base_type == GLSL_TYPE_BOOL); |
| 202 | break; |
| 203 | case ir_unop_b2f: |
| 204 | assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL); |
| 205 | assert(ir->type->base_type == GLSL_TYPE_FLOAT); |
| 206 | break; |
| 207 | case ir_unop_i2b: |
| 208 | assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT); |
| 209 | assert(ir->type->base_type == GLSL_TYPE_BOOL); |
| 210 | break; |
| 211 | case ir_unop_b2i: |
| 212 | assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL); |
| 213 | assert(ir->type->base_type == GLSL_TYPE_INT); |
| 214 | break; |
| 215 | case ir_unop_u2f: |
| 216 | assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT); |
| 217 | assert(ir->type->base_type == GLSL_TYPE_FLOAT); |
| 218 | break; |
| 219 | |
| 220 | case ir_unop_trunc: |
| 221 | case ir_unop_ceil: |
| 222 | case ir_unop_floor: |
| 223 | case ir_unop_fract: |
| 224 | case ir_unop_sin: |
| 225 | case ir_unop_cos: |
| 226 | case ir_unop_dFdx: |
| 227 | case ir_unop_dFdy: |
| 228 | assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 229 | assert(ir->operands[0]->type == ir->type); |
| 230 | break; |
| 231 | |
| 232 | case ir_binop_add: |
| 233 | case ir_binop_sub: |
| 234 | case ir_binop_mul: |
| 235 | case ir_binop_div: |
| 236 | case ir_binop_mod: |
| 237 | case ir_binop_min: |
| 238 | case ir_binop_max: |
| 239 | case ir_binop_pow: |
| 240 | if (ir->operands[0]->type->is_scalar()) |
| 241 | assert(ir->operands[1]->type == ir->type); |
| 242 | else if (ir->operands[1]->type->is_scalar()) |
| 243 | assert(ir->operands[0]->type == ir->type); |
| 244 | else if (ir->operands[0]->type->is_vector() && |
| 245 | ir->operands[1]->type->is_vector()) { |
| 246 | assert(ir->operands[0]->type == ir->operands[1]->type); |
| 247 | assert(ir->operands[0]->type == ir->type); |
| 248 | } |
| 249 | break; |
| 250 | case ir_binop_less: |
| 251 | case ir_binop_greater: |
| 252 | case ir_binop_lequal: |
| 253 | case ir_binop_gequal: |
| 254 | /* GLSL < > <= >= operators take scalar floats/ints, but in the |
| 255 | * IR we may want to do them for vectors instead to support the |
| 256 | * lessEqual() and friends builtins. |
| 257 | */ |
| 258 | assert(ir->type == glsl_type::bool_type); |
| 259 | assert(ir->operands[0]->type == ir->operands[1]->type); |
| 260 | break; |
| 261 | |
| 262 | case ir_binop_equal: |
| 263 | case ir_binop_nequal: |
| 264 | /* GLSL == and != operate on vectors and return a bool, and the |
| 265 | * IR matches that. We may want to switch up the IR to work on |
| 266 | * vectors and return a bvec and make the operators break down |
| 267 | * to ANDing/ORing the results of the vector comparison. |
| 268 | */ |
| 269 | assert(ir->type == glsl_type::bool_type); |
| 270 | assert(ir->operands[0]->type == ir->operands[1]->type); |
| 271 | break; |
| 272 | |
| 273 | case ir_binop_lshift: |
| 274 | case ir_binop_rshift: |
| 275 | case ir_binop_bit_and: |
| 276 | case ir_binop_bit_xor: |
| 277 | case ir_binop_bit_or: |
| 278 | assert(ir->operands[0]->type == ir->operands[1]->type); |
| 279 | assert(ir->type == ir->operands[0]->type); |
| 280 | assert(ir->type->base_type == GLSL_TYPE_INT || |
| 281 | ir->type->base_type == GLSL_TYPE_UINT); |
| 282 | break; |
| 283 | |
| 284 | case ir_binop_logic_and: |
| 285 | case ir_binop_logic_xor: |
| 286 | case ir_binop_logic_or: |
| 287 | assert(ir->type == glsl_type::bool_type); |
| 288 | assert(ir->operands[0]->type == glsl_type::bool_type); |
| 289 | assert(ir->operands[1]->type == glsl_type::bool_type); |
| 290 | break; |
| 291 | |
| 292 | case ir_binop_dot: |
| 293 | assert(ir->type == glsl_type::float_type); |
| 294 | assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 295 | assert(ir->operands[0]->type == ir->operands[1]->type); |
| 296 | break; |
| 297 | |
| 298 | case ir_binop_cross: |
| 299 | assert(ir->operands[0]->type == glsl_type::vec3_type); |
| 300 | assert(ir->operands[1]->type == glsl_type::vec3_type); |
| 301 | assert(ir->type == glsl_type::vec3_type); |
| 302 | break; |
| 303 | } |
| 304 | |
| 305 | return visit_continue; |
| 306 | } |
| 307 | |
| 308 | ir_visitor_status |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 309 | ir_validate::visit(ir_variable *ir) |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 310 | { |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 311 | /* 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] | 312 | * in an IR tree. It is added to the hashtable so that it can be used |
| 313 | * in the ir_dereference_variable handler to ensure that a variable is |
| 314 | * declared before it is dereferenced. |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 315 | */ |
Eric Anholt | ee7666b | 2010-08-02 12:08:52 -0700 | [diff] [blame^] | 316 | assert(talloc_parent(ir->name) == ir); |
| 317 | |
Ian Romanick | 8baf21b | 2010-07-12 13:55:32 -0700 | [diff] [blame] | 318 | hash_table_insert(ht, ir, ir); |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 319 | return visit_continue; |
| 320 | } |
| 321 | |
| 322 | void |
| 323 | ir_validate::validate_ir(ir_instruction *ir, void *data) |
| 324 | { |
| 325 | struct hash_table *ht = (struct hash_table *) data; |
| 326 | |
| 327 | if (hash_table_find(ht, ir)) { |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 328 | printf("Instruction node present twice in ir tree:\n"); |
| 329 | ir->print(); |
| 330 | printf("\n"); |
| 331 | abort(); |
| 332 | } |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 333 | hash_table_insert(ht, ir, ir); |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | void |
Eric Anholt | d16044a | 2010-07-19 09:05:42 -0700 | [diff] [blame] | 337 | check_node_type(ir_instruction *ir, void *data) |
| 338 | { |
| 339 | if (ir->ir_type <= ir_type_unset || ir->ir_type >= ir_type_max) { |
| 340 | printf("Instruction node with unset type\n"); |
| 341 | ir->print(); printf("\n"); |
| 342 | } |
Eric Anholt | f141fa6 | 2010-07-20 17:18:57 -0700 | [diff] [blame] | 343 | assert(ir->type != glsl_type::error_type); |
Eric Anholt | d16044a | 2010-07-19 09:05:42 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | void |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 347 | validate_ir_tree(exec_list *instructions) |
| 348 | { |
| 349 | ir_validate v; |
| 350 | |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 351 | v.run(instructions); |
Eric Anholt | d16044a | 2010-07-19 09:05:42 -0700 | [diff] [blame] | 352 | |
| 353 | foreach_iter(exec_list_iterator, iter, *instructions) { |
| 354 | ir_instruction *ir = (ir_instruction *)iter.get(); |
| 355 | |
| 356 | visit_tree(ir, check_node_type, NULL); |
| 357 | } |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 358 | } |