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 "ir.h" 37 #include "ir_hierarchical_visitor.h" 38 #include "util/hash_table.h" 39 #include "util/macros.h" 40 #include "util/set.h" 41 #include "compiler/glsl_types.h" 42 43 namespace { 44 45 class ir_validate : public ir_hierarchical_visitor { 46 public: 47 ir_validate() 48 { 49 this->ir_set = _mesa_set_create(NULL, _mesa_hash_pointer, 50 _mesa_key_pointer_equal); 51 52 this->current_function = NULL; 53 54 this->callback_enter = ir_validate::validate_ir; 55 this->data_enter = ir_set; 56 } 57 58 ~ir_validate() 59 { 60 _mesa_set_destroy(this->ir_set, NULL); 61 } 62 63 virtual ir_visitor_status visit(ir_variable *v); 64 virtual ir_visitor_status visit(ir_dereference_variable *ir); 65 66 virtual ir_visitor_status visit_enter(ir_discard *ir); 67 virtual ir_visitor_status visit_enter(ir_if *ir); 68 69 virtual ir_visitor_status visit_enter(ir_function *ir); 70 virtual ir_visitor_status visit_leave(ir_function *ir); 71 virtual ir_visitor_status visit_enter(ir_function_signature *ir); 72 73 virtual ir_visitor_status visit_leave(ir_expression *ir); 74 virtual ir_visitor_status visit_leave(ir_swizzle *ir); 75 76 virtual ir_visitor_status visit_enter(class ir_dereference_array *); 77 78 virtual ir_visitor_status visit_enter(ir_assignment *ir); 79 virtual ir_visitor_status visit_enter(ir_call *ir); 80 81 static void validate_ir(ir_instruction *ir, void *data); 82 83 ir_function *current_function; 84 85 struct set *ir_set; 86 }; 87 88 } /* anonymous namespace */ 89 90 ir_visitor_status 91 ir_validate::visit(ir_dereference_variable *ir) 92 { 93 if ((ir->var == NULL) || (ir->var->as_variable() == NULL)) { 94 printf("ir_dereference_variable @ %p does not specify a variable %p\n", 95 (void *) ir, (void *) ir->var); 96 abort(); 97 } 98 99 if (_mesa_set_search(ir_set, ir->var) == NULL) { 100 printf("ir_dereference_variable @ %p specifies undeclared variable " 101 "`%s' @ %p\n", 102 (void *) ir, ir->var->name, (void *) ir->var); 103 abort(); 104 } 105 106 this->validate_ir(ir, this->data_enter); 107 108 return visit_continue; 109 } 110 111 ir_visitor_status 112 ir_validate::visit_enter(class ir_dereference_array *ir) 113 { 114 if (!ir->array->type->is_array() && !ir->array->type->is_matrix() && 115 !ir->array->type->is_vector()) { 116 printf("ir_dereference_array @ %p does not specify an array, a vector " 117 "or a matrix\n", 118 (void *) ir); 119 ir->print(); 120 printf("\n"); 121 abort(); 122 } 123 124 if (!ir->array_index->type->is_scalar()) { 125 printf("ir_dereference_array @ %p does not have scalar index: %s\n", 126 (void *) ir, ir->array_index->type->name); 127 abort(); 128 } 129 130 if (!ir->array_index->type->is_integer()) { 131 printf("ir_dereference_array @ %p does not have integer index: %s\n", 132 (void *) ir, ir->array_index->type->name); 133 abort(); 134 } 135 136 return visit_continue; 137 } 138 139 ir_visitor_status 140 ir_validate::visit_enter(ir_discard *ir) 141 { 142 if (ir->condition && ir->condition->type != glsl_type::bool_type) { 143 printf("ir_discard condition %s type instead of bool.\n", 144 ir->condition->type->name); 145 ir->print(); 146 printf("\n"); 147 abort(); 148 } 149 150 return visit_continue; 151 } 152 153 ir_visitor_status 154 ir_validate::visit_enter(ir_if *ir) 155 { 156 if (ir->condition->type != glsl_type::bool_type) { 157 printf("ir_if condition %s type instead of bool.\n", 158 ir->condition->type->name); 159 ir->print(); 160 printf("\n"); 161 abort(); 162 } 163 164 return visit_continue; 165 } 166 167 168 ir_visitor_status 169 ir_validate::visit_enter(ir_function *ir) 170 { 171 /* Function definitions cannot be nested. 172 */ 173 if (this->current_function != NULL) { 174 printf("Function definition nested inside another function " 175 "definition:\n"); 176 printf("%s %p inside %s %p\n", 177 ir->name, (void *) ir, 178 this->current_function->name, (void *) this->current_function); 179 abort(); 180 } 181 182 /* Store the current function hierarchy being traversed. This is used 183 * by the function signature visitor to ensure that the signatures are 184 * linked with the correct functions. 185 */ 186 this->current_function = ir; 187 188 this->validate_ir(ir, this->data_enter); 189 190 /* Verify that all of the things stored in the list of signatures are, 191 * in fact, function signatures. 192 */ 193 foreach_in_list(ir_instruction, sig, &ir->signatures) { 194 if (sig->ir_type != ir_type_function_signature) { 195 printf("Non-signature in signature list of function `%s'\n", 196 ir->name); 197 abort(); 198 } 199 } 200 201 return visit_continue; 202 } 203 204 ir_visitor_status 205 ir_validate::visit_leave(ir_function *ir) 206 { 207 assert(ralloc_parent(ir->name) == ir); 208 209 this->current_function = NULL; 210 return visit_continue; 211 } 212 213 ir_visitor_status 214 ir_validate::visit_enter(ir_function_signature *ir) 215 { 216 if (this->current_function != ir->function()) { 217 printf("Function signature nested inside wrong function " 218 "definition:\n"); 219 printf("%p inside %s %p instead of %s %p\n", 220 (void *) ir, 221 this->current_function->name, (void *) this->current_function, 222 ir->function_name(), (void *) ir->function()); 223 abort(); 224 } 225 226 if (ir->return_type == NULL) { 227 printf("Function signature %p for function %s has NULL return type.\n", 228 (void *) ir, ir->function_name()); 229 abort(); 230 } 231 232 this->validate_ir(ir, this->data_enter); 233 234 return visit_continue; 235 } 236 237 ir_visitor_status 238 ir_validate::visit_leave(ir_expression *ir) 239 { 240 for (unsigned i = ir->num_operands; i < 4; i++) { 241 assert(ir->operands[i] == NULL); 242 } 243 244 for (unsigned i = 0; i < ir->num_operands; i++) { 245 assert(ir->operands[i] != NULL); 246 } 247 248 switch (ir->operation) { 249 case ir_unop_bit_not: 250 assert(ir->operands[0]->type == ir->type); 251 break; 252 case ir_unop_logic_not: 253 assert(ir->type->is_boolean()); 254 assert(ir->operands[0]->type->is_boolean()); 255 break; 256 257 case ir_unop_neg: 258 assert(ir->type == ir->operands[0]->type); 259 break; 260 261 case ir_unop_abs: 262 case ir_unop_sign: 263 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT || 264 ir->operands[0]->type->is_float() || 265 ir->operands[0]->type->is_double() || 266 ir->operands[0]->type->base_type == GLSL_TYPE_INT64); 267 assert(ir->type == ir->operands[0]->type); 268 break; 269 270 case ir_unop_rcp: 271 case ir_unop_rsq: 272 case ir_unop_sqrt: 273 assert(ir->type->is_float() || 274 ir->type->is_double()); 275 assert(ir->type == ir->operands[0]->type); 276 break; 277 278 case ir_unop_exp: 279 case ir_unop_log: 280 case ir_unop_exp2: 281 case ir_unop_log2: 282 case ir_unop_saturate: 283 assert(ir->operands[0]->type->is_float()); 284 assert(ir->type == ir->operands[0]->type); 285 break; 286 287 case ir_unop_f2i: 288 assert(ir->operands[0]->type->is_float()); 289 assert(ir->type->base_type == GLSL_TYPE_INT); 290 break; 291 case ir_unop_f2u: 292 assert(ir->operands[0]->type->is_float()); 293 assert(ir->type->base_type == GLSL_TYPE_UINT); 294 break; 295 case ir_unop_i2f: 296 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT); 297 assert(ir->type->is_float()); 298 break; 299 case ir_unop_f2b: 300 assert(ir->operands[0]->type->is_float()); 301 assert(ir->type->is_boolean()); 302 break; 303 case ir_unop_b2f: 304 assert(ir->operands[0]->type->is_boolean()); 305 assert(ir->type->is_float()); 306 break; 307 case ir_unop_i2b: 308 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT); 309 assert(ir->type->is_boolean()); 310 break; 311 case ir_unop_b2i: 312 assert(ir->operands[0]->type->is_boolean()); 313 assert(ir->type->base_type == GLSL_TYPE_INT); 314 break; 315 case ir_unop_u2f: 316 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT); 317 assert(ir->type->is_float()); 318 break; 319 case ir_unop_i2u: 320 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT); 321 assert(ir->type->base_type == GLSL_TYPE_UINT); 322 break; 323 case ir_unop_u2i: 324 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT); 325 assert(ir->type->base_type == GLSL_TYPE_INT); 326 break; 327 case ir_unop_bitcast_i2f: 328 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT); 329 assert(ir->type->is_float()); 330 break; 331 case ir_unop_bitcast_f2i: 332 assert(ir->operands[0]->type->is_float()); 333 assert(ir->type->base_type == GLSL_TYPE_INT); 334 break; 335 case ir_unop_bitcast_u2f: 336 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT); 337 assert(ir->type->is_float()); 338 break; 339 case ir_unop_bitcast_f2u: 340 assert(ir->operands[0]->type->is_float()); 341 assert(ir->type->base_type == GLSL_TYPE_UINT); 342 break; 343 344 case ir_unop_bitcast_u642d: 345 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64); 346 assert(ir->type->is_double()); 347 break; 348 case ir_unop_bitcast_i642d: 349 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64); 350 assert(ir->type->is_double()); 351 break; 352 case ir_unop_bitcast_d2u64: 353 assert(ir->operands[0]->type->is_double()); 354 assert(ir->type->base_type == GLSL_TYPE_UINT64); 355 break; 356 case ir_unop_bitcast_d2i64: 357 assert(ir->operands[0]->type->is_double()); 358 assert(ir->type->base_type == GLSL_TYPE_INT64); 359 break; 360 case ir_unop_i642i: 361 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64); 362 assert(ir->type->base_type == GLSL_TYPE_INT); 363 break; 364 case ir_unop_u642i: 365 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64); 366 assert(ir->type->base_type == GLSL_TYPE_INT); 367 break; 368 case ir_unop_i642u: 369 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64); 370 assert(ir->type->base_type == GLSL_TYPE_UINT); 371 break; 372 case ir_unop_u642u: 373 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64); 374 assert(ir->type->base_type == GLSL_TYPE_UINT); 375 break; 376 case ir_unop_i642b: 377 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64); 378 assert(ir->type->is_boolean()); 379 break; 380 case ir_unop_i642f: 381 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64); 382 assert(ir->type->is_float()); 383 break; 384 case ir_unop_u642f: 385 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64); 386 assert(ir->type->is_float()); 387 break; 388 case ir_unop_i642d: 389 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64); 390 assert(ir->type->is_double()); 391 break; 392 case ir_unop_u642d: 393 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64); 394 assert(ir->type->is_double()); 395 break; 396 case ir_unop_i2i64: 397 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT); 398 assert(ir->type->base_type == GLSL_TYPE_INT64); 399 break; 400 case ir_unop_u2i64: 401 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT); 402 assert(ir->type->base_type == GLSL_TYPE_INT64); 403 break; 404 case ir_unop_b2i64: 405 assert(ir->operands[0]->type->is_boolean()); 406 assert(ir->type->base_type == GLSL_TYPE_INT64); 407 break; 408 case ir_unop_f2i64: 409 assert(ir->operands[0]->type->is_float()); 410 assert(ir->type->base_type == GLSL_TYPE_INT64); 411 break; 412 case ir_unop_d2i64: 413 assert(ir->operands[0]->type->is_double()); 414 assert(ir->type->base_type == GLSL_TYPE_INT64); 415 break; 416 case ir_unop_i2u64: 417 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT); 418 assert(ir->type->base_type == GLSL_TYPE_UINT64); 419 break; 420 case ir_unop_u2u64: 421 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT); 422 assert(ir->type->base_type == GLSL_TYPE_UINT64); 423 break; 424 case ir_unop_f2u64: 425 assert(ir->operands[0]->type->is_float()); 426 assert(ir->type->base_type == GLSL_TYPE_UINT64); 427 break; 428 case ir_unop_d2u64: 429 assert(ir->operands[0]->type->is_double()); 430 assert(ir->type->base_type == GLSL_TYPE_UINT64); 431 break; 432 case ir_unop_u642i64: 433 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT64); 434 assert(ir->type->base_type == GLSL_TYPE_INT64); 435 break; 436 case ir_unop_i642u64: 437 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT64); 438 assert(ir->type->base_type == GLSL_TYPE_UINT64); 439 break; 440 case ir_unop_trunc: 441 case ir_unop_round_even: 442 case ir_unop_ceil: 443 case ir_unop_floor: 444 case ir_unop_fract: 445 assert(ir->operands[0]->type->is_float() || 446 ir->operands[0]->type->is_double()); 447 assert(ir->operands[0]->type == ir->type); 448 break; 449 case ir_unop_sin: 450 case ir_unop_cos: 451 case ir_unop_dFdx: 452 case ir_unop_dFdx_coarse: 453 case ir_unop_dFdx_fine: 454 case ir_unop_dFdy: 455 case ir_unop_dFdy_coarse: 456 case ir_unop_dFdy_fine: 457 assert(ir->operands[0]->type->is_float()); 458 assert(ir->operands[0]->type == ir->type); 459 break; 460 461 case ir_unop_pack_snorm_2x16: 462 case ir_unop_pack_unorm_2x16: 463 case ir_unop_pack_half_2x16: 464 assert(ir->type == glsl_type::uint_type); 465 assert(ir->operands[0]->type == glsl_type::vec2_type); 466 break; 467 468 case ir_unop_pack_snorm_4x8: 469 case ir_unop_pack_unorm_4x8: 470 assert(ir->type == glsl_type::uint_type); 471 assert(ir->operands[0]->type == glsl_type::vec4_type); 472 break; 473 474 case ir_unop_pack_double_2x32: 475 assert(ir->type == glsl_type::double_type); 476 assert(ir->operands[0]->type == glsl_type::uvec2_type); 477 break; 478 479 case ir_unop_pack_int_2x32: 480 assert(ir->type == glsl_type::int64_t_type); 481 assert(ir->operands[0]->type == glsl_type::ivec2_type); 482 break; 483 484 case ir_unop_pack_uint_2x32: 485 assert(ir->type == glsl_type::uint64_t_type); 486 assert(ir->operands[0]->type == glsl_type::uvec2_type); 487 break; 488 489 case ir_unop_pack_sampler_2x32: 490 assert(ir->type->is_sampler()); 491 assert(ir->operands[0]->type == glsl_type::uvec2_type); 492 break; 493 494 case ir_unop_pack_image_2x32: 495 assert(ir->type->is_image()); 496 assert(ir->operands[0]->type == glsl_type::uvec2_type); 497 break; 498 499 case ir_unop_unpack_snorm_2x16: 500 case ir_unop_unpack_unorm_2x16: 501 case ir_unop_unpack_half_2x16: 502 assert(ir->type == glsl_type::vec2_type); 503 assert(ir->operands[0]->type == glsl_type::uint_type); 504 break; 505 506 case ir_unop_unpack_snorm_4x8: 507 case ir_unop_unpack_unorm_4x8: 508 assert(ir->type == glsl_type::vec4_type); 509 assert(ir->operands[0]->type == glsl_type::uint_type); 510 break; 511 512 case ir_unop_unpack_double_2x32: 513 assert(ir->type == glsl_type::uvec2_type); 514 assert(ir->operands[0]->type == glsl_type::double_type); 515 break; 516 517 case ir_unop_unpack_int_2x32: 518 assert(ir->type == glsl_type::ivec2_type); 519 assert(ir->operands[0]->type == glsl_type::int64_t_type); 520 break; 521 522 case ir_unop_unpack_uint_2x32: 523 assert(ir->type == glsl_type::uvec2_type); 524 assert(ir->operands[0]->type == glsl_type::uint64_t_type); 525 break; 526 527 case ir_unop_unpack_sampler_2x32: 528 assert(ir->type == glsl_type::uvec2_type); 529 assert(ir->operands[0]->type->is_sampler()); 530 break; 531 532 case ir_unop_unpack_image_2x32: 533 assert(ir->type == glsl_type::uvec2_type); 534 assert(ir->operands[0]->type->is_image()); 535 break; 536 537 case ir_unop_bitfield_reverse: 538 assert(ir->operands[0]->type == ir->type); 539 assert(ir->type->is_integer()); 540 break; 541 542 case ir_unop_bit_count: 543 case ir_unop_find_msb: 544 case ir_unop_find_lsb: 545 assert(ir->operands[0]->type->vector_elements == ir->type->vector_elements); 546 assert(ir->operands[0]->type->is_integer()); 547 assert(ir->type->base_type == GLSL_TYPE_INT); 548 break; 549 550 case ir_unop_noise: 551 /* XXX what can we assert here? */ 552 break; 553 554 case ir_unop_interpolate_at_centroid: 555 assert(ir->operands[0]->type == ir->type); 556 assert(ir->operands[0]->type->is_float()); 557 break; 558 559 case ir_unop_get_buffer_size: 560 assert(ir->type == glsl_type::int_type); 561 assert(ir->operands[0]->type == glsl_type::uint_type); 562 break; 563 564 case ir_unop_ssbo_unsized_array_length: 565 assert(ir->type == glsl_type::int_type); 566 assert(ir->operands[0]->type->is_array()); 567 assert(ir->operands[0]->type->is_unsized_array()); 568 break; 569 570 case ir_unop_d2f: 571 assert(ir->operands[0]->type->is_double()); 572 assert(ir->type->is_float()); 573 break; 574 case ir_unop_f2d: 575 assert(ir->operands[0]->type->is_float()); 576 assert(ir->type->is_double()); 577 break; 578 case ir_unop_d2i: 579 assert(ir->operands[0]->type->is_double()); 580 assert(ir->type->base_type == GLSL_TYPE_INT); 581 break; 582 case ir_unop_i2d: 583 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT); 584 assert(ir->type->is_double()); 585 break; 586 case ir_unop_d2u: 587 assert(ir->operands[0]->type->is_double()); 588 assert(ir->type->base_type == GLSL_TYPE_UINT); 589 break; 590 case ir_unop_u2d: 591 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT); 592 assert(ir->type->is_double()); 593 break; 594 case ir_unop_d2b: 595 assert(ir->operands[0]->type->is_double()); 596 assert(ir->type->is_boolean()); 597 break; 598 599 case ir_unop_frexp_sig: 600 assert(ir->operands[0]->type->is_float() || 601 ir->operands[0]->type->is_double()); 602 assert(ir->type->is_double()); 603 break; 604 case ir_unop_frexp_exp: 605 assert(ir->operands[0]->type->is_float() || 606 ir->operands[0]->type->is_double()); 607 assert(ir->type->base_type == GLSL_TYPE_INT); 608 break; 609 case ir_unop_subroutine_to_int: 610 assert(ir->operands[0]->type->base_type == GLSL_TYPE_SUBROUTINE); 611 assert(ir->type->base_type == GLSL_TYPE_INT); 612 break; 613 614 case ir_binop_add: 615 case ir_binop_sub: 616 case ir_binop_mul: 617 case ir_binop_div: 618 case ir_binop_mod: 619 case ir_binop_min: 620 case ir_binop_max: 621 case ir_binop_pow: 622 assert(ir->operands[0]->type->base_type == 623 ir->operands[1]->type->base_type); 624 625 if (ir->operands[0]->type->is_scalar()) 626 assert(ir->operands[1]->type == ir->type); 627 else if (ir->operands[1]->type->is_scalar()) 628 assert(ir->operands[0]->type == ir->type); 629 else if (ir->operands[0]->type->is_vector() && 630 ir->operands[1]->type->is_vector()) { 631 assert(ir->operands[0]->type == ir->operands[1]->type); 632 assert(ir->operands[0]->type == ir->type); 633 } 634 break; 635 636 case ir_binop_imul_high: 637 assert(ir->type == ir->operands[0]->type); 638 assert(ir->type == ir->operands[1]->type); 639 assert(ir->type->is_integer()); 640 break; 641 642 case ir_binop_carry: 643 case ir_binop_borrow: 644 assert(ir->type == ir->operands[0]->type); 645 assert(ir->type == ir->operands[1]->type); 646 assert(ir->type->base_type == GLSL_TYPE_UINT); 647 break; 648 649 case ir_binop_less: 650 case ir_binop_gequal: 651 case ir_binop_equal: 652 case ir_binop_nequal: 653 /* The semantics of the IR operators differ from the GLSL <, >, <=, >=, 654 * ==, and != operators. The IR operators perform a component-wise 655 * comparison on scalar or vector types and return a boolean scalar or 656 * vector type of the same size. 657 */ 658 assert(ir->type->is_boolean()); 659 assert(ir->operands[0]->type == ir->operands[1]->type); 660 assert(ir->operands[0]->type->is_vector() 661 || ir->operands[0]->type->is_scalar()); 662 assert(ir->operands[0]->type->vector_elements 663 == ir->type->vector_elements); 664 break; 665 666 case ir_binop_all_equal: 667 case ir_binop_any_nequal: 668 /* GLSL == and != operate on scalars, vectors, matrices and arrays, and 669 * return a scalar boolean. The IR matches that. 670 */ 671 assert(ir->type == glsl_type::bool_type); 672 assert(ir->operands[0]->type == ir->operands[1]->type); 673 break; 674 675 case ir_binop_lshift: 676 case ir_binop_rshift: 677 assert(ir->operands[0]->type->is_integer_32_64() && 678 ir->operands[1]->type->is_integer()); 679 if (ir->operands[0]->type->is_scalar()) { 680 assert(ir->operands[1]->type->is_scalar()); 681 } 682 if (ir->operands[0]->type->is_vector() && 683 ir->operands[1]->type->is_vector()) { 684 assert(ir->operands[0]->type->components() == 685 ir->operands[1]->type->components()); 686 } 687 assert(ir->type == ir->operands[0]->type); 688 break; 689 690 case ir_binop_bit_and: 691 case ir_binop_bit_xor: 692 case ir_binop_bit_or: 693 assert(ir->operands[0]->type->base_type == 694 ir->operands[1]->type->base_type); 695 assert(ir->type->is_integer_32_64()); 696 if (ir->operands[0]->type->is_vector() && 697 ir->operands[1]->type->is_vector()) { 698 assert(ir->operands[0]->type->vector_elements == 699 ir->operands[1]->type->vector_elements); 700 } 701 break; 702 703 case ir_binop_logic_and: 704 case ir_binop_logic_xor: 705 case ir_binop_logic_or: 706 assert(ir->type->is_boolean()); 707 assert(ir->operands[0]->type->is_boolean()); 708 assert(ir->operands[1]->type->is_boolean()); 709 break; 710 711 case ir_binop_dot: 712 assert(ir->type == glsl_type::float_type || 713 ir->type == glsl_type::double_type); 714 assert(ir->operands[0]->type->is_float() || 715 ir->operands[0]->type->is_double()); 716 assert(ir->operands[0]->type->is_vector()); 717 assert(ir->operands[0]->type == ir->operands[1]->type); 718 break; 719 720 case ir_binop_ubo_load: 721 assert(ir->operands[0]->type == glsl_type::uint_type); 722 723 assert(ir->operands[1]->type == glsl_type::uint_type); 724 break; 725 726 case ir_binop_ldexp: 727 assert(ir->operands[0]->type == ir->type); 728 assert(ir->operands[0]->type->is_float() || 729 ir->operands[0]->type->is_double()); 730 assert(ir->operands[1]->type->base_type == GLSL_TYPE_INT); 731 assert(ir->operands[0]->type->components() == 732 ir->operands[1]->type->components()); 733 break; 734 735 case ir_binop_vector_extract: 736 assert(ir->operands[0]->type->is_vector()); 737 assert(ir->operands[1]->type->is_scalar() 738 && ir->operands[1]->type->is_integer()); 739 break; 740 741 case ir_binop_interpolate_at_offset: 742 assert(ir->operands[0]->type == ir->type); 743 assert(ir->operands[0]->type->is_float()); 744 assert(ir->operands[1]->type->components() == 2); 745 assert(ir->operands[1]->type->is_float()); 746 break; 747 748 case ir_binop_interpolate_at_sample: 749 assert(ir->operands[0]->type == ir->type); 750 assert(ir->operands[0]->type->is_float()); 751 assert(ir->operands[1]->type == glsl_type::int_type); 752 break; 753 754 case ir_triop_fma: 755 assert(ir->type->is_float() || 756 ir->type->is_double()); 757 assert(ir->type == ir->operands[0]->type); 758 assert(ir->type == ir->operands[1]->type); 759 assert(ir->type == ir->operands[2]->type); 760 break; 761 762 case ir_triop_lrp: 763 assert(ir->operands[0]->type->is_float() || 764 ir->operands[0]->type->is_double()); 765 assert(ir->operands[0]->type == ir->operands[1]->type); 766 assert(ir->operands[2]->type == ir->operands[0]->type || 767 ir->operands[2]->type == glsl_type::float_type || 768 ir->operands[2]->type == glsl_type::double_type); 769 break; 770 771 case ir_triop_csel: 772 assert(ir->operands[0]->type->is_boolean()); 773 assert(ir->type->vector_elements == ir->operands[0]->type->vector_elements); 774 assert(ir->type == ir->operands[1]->type); 775 assert(ir->type == ir->operands[2]->type); 776 break; 777 778 case ir_triop_bitfield_extract: 779 assert(ir->type->is_integer()); 780 assert(ir->operands[0]->type == ir->type); 781 assert(ir->operands[1]->type == ir->type); 782 assert(ir->operands[2]->type == ir->type); 783 break; 784 785 case ir_triop_vector_insert: 786 assert(ir->operands[0]->type->is_vector()); 787 assert(ir->operands[1]->type->is_scalar()); 788 assert(ir->operands[0]->type->base_type == ir->operands[1]->type->base_type); 789 assert(ir->operands[2]->type->is_scalar() 790 && ir->operands[2]->type->is_integer()); 791 assert(ir->type == ir->operands[0]->type); 792 break; 793 794 case ir_quadop_bitfield_insert: 795 assert(ir->type->is_integer()); 796 assert(ir->operands[0]->type == ir->type); 797 assert(ir->operands[1]->type == ir->type); 798 assert(ir->operands[2]->type == ir->type); 799 assert(ir->operands[3]->type == ir->type); 800 break; 801 802 case ir_quadop_vector: 803 /* The vector operator collects some number of scalars and generates a 804 * vector from them. 805 * 806 * - All of the operands must be scalar. 807 * - Number of operands must matche the size of the resulting vector. 808 * - Base type of the operands must match the base type of the result. 809 */ 810 assert(ir->type->is_vector()); 811 switch (ir->type->vector_elements) { 812 case 2: 813 assert(ir->operands[0]->type->is_scalar()); 814 assert(ir->operands[0]->type->base_type == ir->type->base_type); 815 assert(ir->operands[1]->type->is_scalar()); 816 assert(ir->operands[1]->type->base_type == ir->type->base_type); 817 assert(ir->operands[2] == NULL); 818 assert(ir->operands[3] == NULL); 819 break; 820 case 3: 821 assert(ir->operands[0]->type->is_scalar()); 822 assert(ir->operands[0]->type->base_type == ir->type->base_type); 823 assert(ir->operands[1]->type->is_scalar()); 824 assert(ir->operands[1]->type->base_type == ir->type->base_type); 825 assert(ir->operands[2]->type->is_scalar()); 826 assert(ir->operands[2]->type->base_type == ir->type->base_type); 827 assert(ir->operands[3] == NULL); 828 break; 829 case 4: 830 assert(ir->operands[0]->type->is_scalar()); 831 assert(ir->operands[0]->type->base_type == ir->type->base_type); 832 assert(ir->operands[1]->type->is_scalar()); 833 assert(ir->operands[1]->type->base_type == ir->type->base_type); 834 assert(ir->operands[2]->type->is_scalar()); 835 assert(ir->operands[2]->type->base_type == ir->type->base_type); 836 assert(ir->operands[3]->type->is_scalar()); 837 assert(ir->operands[3]->type->base_type == ir->type->base_type); 838 break; 839 default: 840 /* The is_vector assertion above should prevent execution from ever 841 * getting here. 842 */ 843 assert(!"Should not get here."); 844 break; 845 } 846 } 847 848 return visit_continue; 849 } 850 851 ir_visitor_status 852 ir_validate::visit_leave(ir_swizzle *ir) 853 { 854 unsigned int chans[4] = {ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w}; 855 856 for (unsigned int i = 0; i < ir->type->vector_elements; i++) { 857 if (chans[i] >= ir->val->type->vector_elements) { 858 printf("ir_swizzle @ %p specifies a channel not present " 859 "in the value.\n", (void *) ir); 860 ir->print(); 861 abort(); 862 } 863 } 864 865 return visit_continue; 866 } 867 868 ir_visitor_status 869 ir_validate::visit(ir_variable *ir) 870 { 871 /* An ir_variable is the one thing that can (and will) appear multiple times 872 * in an IR tree. It is added to the hashtable so that it can be used 873 * in the ir_dereference_variable handler to ensure that a variable is 874 * declared before it is dereferenced. 875 */ 876 if (ir->name && ir->is_name_ralloced()) 877 assert(ralloc_parent(ir->name) == ir); 878 879 _mesa_set_add(ir_set, ir); 880 881 /* If a variable is an array, verify that the maximum array index is in 882 * bounds. There was once an error in AST-to-HIR conversion that set this 883 * to be out of bounds. 884 */ 885 if (ir->type->array_size() > 0) { 886 if (ir->data.max_array_access >= (int)ir->type->length) { 887 printf("ir_variable has maximum access out of bounds (%d vs %d)\n", 888 ir->data.max_array_access, ir->type->length - 1); 889 ir->print(); 890 abort(); 891 } 892 } 893 894 /* If a variable is an interface block (or an array of interface blocks), 895 * verify that the maximum array index for each interface member is in 896 * bounds. 897 */ 898 if (ir->is_interface_instance()) { 899 const glsl_struct_field *fields = 900 ir->get_interface_type()->fields.structure; 901 for (unsigned i = 0; i < ir->get_interface_type()->length; i++) { 902 if (fields[i].type->array_size() > 0 && 903 !fields[i].implicit_sized_array) { 904 const int *const max_ifc_array_access = 905 ir->get_max_ifc_array_access(); 906 907 assert(max_ifc_array_access != NULL); 908 909 if (max_ifc_array_access[i] >= (int)fields[i].type->length) { 910 printf("ir_variable has maximum access out of bounds for " 911 "field %s (%d vs %d)\n", fields[i].name, 912 max_ifc_array_access[i], fields[i].type->length); 913 ir->print(); 914 abort(); 915 } 916 } 917 } 918 } 919 920 if (ir->constant_initializer != NULL && !ir->data.has_initializer) { 921 printf("ir_variable didn't have an initializer, but has a constant " 922 "initializer value.\n"); 923 ir->print(); 924 abort(); 925 } 926 927 if (ir->data.mode == ir_var_uniform 928 && is_gl_identifier(ir->name) 929 && ir->get_state_slots() == NULL) { 930 printf("built-in uniform has no state\n"); 931 ir->print(); 932 abort(); 933 } 934 935 return visit_continue; 936 } 937 938 ir_visitor_status 939 ir_validate::visit_enter(ir_assignment *ir) 940 { 941 const ir_dereference *const lhs = ir->lhs; 942 if (lhs->type->is_scalar() || lhs->type->is_vector()) { 943 if (ir->write_mask == 0) { 944 printf("Assignment LHS is %s, but write mask is 0:\n", 945 lhs->type->is_scalar() ? "scalar" : "vector"); 946 ir->print(); 947 abort(); 948 } 949 950 int lhs_components = 0; 951 for (int i = 0; i < 4; i++) { 952 if (ir->write_mask & (1 << i)) 953 lhs_components++; 954 } 955 956 if (lhs_components != ir->rhs->type->vector_elements) { 957 printf("Assignment count of LHS write mask channels enabled not\n" 958 "matching RHS vector size (%d LHS, %d RHS).\n", 959 lhs_components, ir->rhs->type->vector_elements); 960 ir->print(); 961 abort(); 962 } 963 } 964 965 this->validate_ir(ir, this->data_enter); 966 967 return visit_continue; 968 } 969 970 ir_visitor_status 971 ir_validate::visit_enter(ir_call *ir) 972 { 973 ir_function_signature *const callee = ir->callee; 974 975 if (callee->ir_type != ir_type_function_signature) { 976 printf("IR called by ir_call is not ir_function_signature!\n"); 977 abort(); 978 } 979 980 if (ir->return_deref) { 981 if (ir->return_deref->type != callee->return_type) { 982 printf("callee type %s does not match return storage type %s\n", 983 callee->return_type->name, ir->return_deref->type->name); 984 abort(); 985 } 986 } else if (callee->return_type != glsl_type::void_type) { 987 printf("ir_call has non-void callee but no return storage\n"); 988 abort(); 989 } 990 991 const exec_node *formal_param_node = callee->parameters.get_head_raw(); 992 const exec_node *actual_param_node = ir->actual_parameters.get_head_raw(); 993 while (true) { 994 if (formal_param_node->is_tail_sentinel() 995 != actual_param_node->is_tail_sentinel()) { 996 printf("ir_call has the wrong number of parameters:\n"); 997 goto dump_ir; 998 } 999 if (formal_param_node->is_tail_sentinel()) { 1000 break; 1001 } 1002 const ir_variable *formal_param 1003 = (const ir_variable *) formal_param_node; 1004 const ir_rvalue *actual_param 1005 = (const ir_rvalue *) actual_param_node; 1006 if (formal_param->type != actual_param->type) { 1007 printf("ir_call parameter type mismatch:\n"); 1008 goto dump_ir; 1009 } 1010 if (formal_param->data.mode == ir_var_function_out 1011 || formal_param->data.mode == ir_var_function_inout) { 1012 if (!actual_param->is_lvalue()) { 1013 printf("ir_call out/inout parameters must be lvalues:\n"); 1014 goto dump_ir; 1015 } 1016 } 1017 formal_param_node = formal_param_node->next; 1018 actual_param_node = actual_param_node->next; 1019 } 1020 1021 return visit_continue; 1022 1023 dump_ir: 1024 ir->print(); 1025 printf("callee:\n"); 1026 callee->print(); 1027 abort(); 1028 return visit_stop; 1029 } 1030 1031 void 1032 ir_validate::validate_ir(ir_instruction *ir, void *data) 1033 { 1034 struct set *ir_set = (struct set *) data; 1035 1036 if (_mesa_set_search(ir_set, ir)) { 1037 printf("Instruction node present twice in ir tree:\n"); 1038 ir->print(); 1039 printf("\n"); 1040 abort(); 1041 } 1042 _mesa_set_add(ir_set, ir); 1043 } 1044 1045 MAYBE_UNUSED static void 1046 check_node_type(ir_instruction *ir, void *data) 1047 { 1048 (void) data; 1049 1050 if (ir->ir_type >= ir_type_max) { 1051 printf("Instruction node with unset type\n"); 1052 ir->print(); printf("\n"); 1053 } 1054 ir_rvalue *value = ir->as_rvalue(); 1055 if (value != NULL) 1056 assert(value->type != glsl_type::error_type); 1057 } 1058 1059 void 1060 validate_ir_tree(exec_list *instructions) 1061 { 1062 /* We shouldn't have any reason to validate IR in a release build, 1063 * and it's half composed of assert()s anyway which wouldn't do 1064 * anything. 1065 */ 1066 #ifdef DEBUG 1067 ir_validate v; 1068 1069 v.run(instructions); 1070 1071 foreach_in_list(ir_instruction, ir, instructions) { 1072 visit_tree(ir, check_node_type, NULL); 1073 } 1074 #endif 1075 } 1076