Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

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