Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | 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 lower_if_to_cond_assign.cpp
  26.  *
  27.  * This attempts to flatten if-statements to conditional assignments for
  28.  * GPUs with limited or no flow control support.
  29.  *
  30.  * It can't handle other control flow being inside of its block, such
  31.  * as calls or loops.  Hopefully loop unrolling and inlining will take
  32.  * care of those.
  33.  *
  34.  * Drivers for GPUs with no control flow support should simply call
  35.  *
  36.  *    lower_if_to_cond_assign(instructions)
  37.  *
  38.  * to attempt to flatten all if-statements.
  39.  *
  40.  * Some GPUs (such as i965 prior to gen6) do support control flow, but have a
  41.  * maximum nesting depth N.  Drivers for such hardware can call
  42.  *
  43.  *    lower_if_to_cond_assign(instructions, N)
  44.  *
  45.  * to attempt to flatten any if-statements appearing at depth > N.
  46.  */
  47.  
  48. #include "glsl_types.h"
  49. #include "ir.h"
  50.  
  51. class ir_if_to_cond_assign_visitor : public ir_hierarchical_visitor {
  52. public:
  53.    ir_if_to_cond_assign_visitor(unsigned max_depth)
  54.    {
  55.       this->progress = false;
  56.       this->max_depth = max_depth;
  57.       this->depth = 0;
  58.    }
  59.  
  60.    ir_visitor_status visit_enter(ir_if *);
  61.    ir_visitor_status visit_leave(ir_if *);
  62.  
  63.    bool progress;
  64.    unsigned max_depth;
  65.    unsigned depth;
  66. };
  67.  
  68. bool
  69. lower_if_to_cond_assign(exec_list *instructions, unsigned max_depth)
  70. {
  71.    ir_if_to_cond_assign_visitor v(max_depth);
  72.  
  73.    visit_list_elements(&v, instructions);
  74.  
  75.    return v.progress;
  76. }
  77.  
  78. void
  79. check_control_flow(ir_instruction *ir, void *data)
  80. {
  81.    bool *found_control_flow = (bool *)data;
  82.    switch (ir->ir_type) {
  83.    case ir_type_call:
  84.    case ir_type_discard:
  85.    case ir_type_loop:
  86.    case ir_type_loop_jump:
  87.    case ir_type_return:
  88.       *found_control_flow = true;
  89.       break;
  90.    default:
  91.       break;
  92.    }
  93. }
  94.  
  95. void
  96. move_block_to_cond_assign(void *mem_ctx,
  97.                           ir_if *if_ir, ir_variable *cond_var, bool then)
  98. {
  99.    exec_list *instructions;
  100.  
  101.    if (then) {
  102.       instructions = &if_ir->then_instructions;
  103.    } else {
  104.       instructions = &if_ir->else_instructions;
  105.    }
  106.  
  107.    foreach_iter(exec_list_iterator, iter, *instructions) {
  108.       ir_instruction *ir = (ir_instruction *)iter.get();
  109.  
  110.       if (ir->ir_type == ir_type_assignment) {
  111.          ir_assignment *assign = (ir_assignment *)ir;
  112.          ir_rvalue *cond_expr;
  113.          ir_dereference *deref = new(mem_ctx) ir_dereference_variable(cond_var);
  114.  
  115.          if (then) {
  116.             cond_expr = deref;
  117.          } else {
  118.             cond_expr = new(mem_ctx) ir_expression(ir_unop_logic_not,
  119.                                                    glsl_type::bool_type,
  120.                                                    deref,
  121.                                                    NULL);
  122.          }
  123.  
  124.          if (!assign->condition) {
  125.             assign->condition = cond_expr;
  126.          } else {
  127.             assign->condition = new(mem_ctx) ir_expression(ir_binop_logic_and,
  128.                                                            glsl_type::bool_type,
  129.                                                            cond_expr,
  130.                                                            assign->condition);
  131.          }
  132.       }
  133.  
  134.       /* Now, move from the if block to the block surrounding it. */
  135.       ir->remove();
  136.       if_ir->insert_before(ir);
  137.    }
  138. }
  139.  
  140. ir_visitor_status
  141. ir_if_to_cond_assign_visitor::visit_enter(ir_if *ir)
  142. {
  143.    (void) ir;
  144.    this->depth++;
  145.    return visit_continue;
  146. }
  147.  
  148. ir_visitor_status
  149. ir_if_to_cond_assign_visitor::visit_leave(ir_if *ir)
  150. {
  151.    /* Only flatten when beyond the GPU's maximum supported nesting depth. */
  152.    if (this->depth <= this->max_depth)
  153.       return visit_continue;
  154.  
  155.    this->depth--;
  156.  
  157.    bool found_control_flow = false;
  158.    ir_variable *cond_var;
  159.    ir_assignment *assign;
  160.    ir_dereference_variable *deref;
  161.  
  162.    /* Check that both blocks don't contain anything we can't support. */
  163.    foreach_iter(exec_list_iterator, then_iter, ir->then_instructions) {
  164.       ir_instruction *then_ir = (ir_instruction *)then_iter.get();
  165.       visit_tree(then_ir, check_control_flow, &found_control_flow);
  166.    }
  167.    foreach_iter(exec_list_iterator, else_iter, ir->else_instructions) {
  168.       ir_instruction *else_ir = (ir_instruction *)else_iter.get();
  169.       visit_tree(else_ir, check_control_flow, &found_control_flow);
  170.    }
  171.    if (found_control_flow)
  172.       return visit_continue;
  173.  
  174.    void *mem_ctx = ralloc_parent(ir);
  175.  
  176.    /* Store the condition to a variable so the assignment conditions are
  177.     * simpler.
  178.     */
  179.    cond_var = new(mem_ctx) ir_variable(glsl_type::bool_type,
  180.                                        "if_to_cond_assign_condition",
  181.                                        ir_var_temporary);
  182.    ir->insert_before(cond_var);
  183.  
  184.    deref = new(mem_ctx) ir_dereference_variable(cond_var);
  185.    assign = new(mem_ctx) ir_assignment(deref,
  186.                                        ir->condition, NULL);
  187.    ir->insert_before(assign);
  188.  
  189.    /* Now, move all of the instructions out of the if blocks, putting
  190.     * conditions on assignments.
  191.     */
  192.    move_block_to_cond_assign(mem_ctx, ir, cond_var, true);
  193.    move_block_to_cond_assign(mem_ctx, ir, cond_var, false);
  194.  
  195.    ir->remove();
  196.  
  197.    this->progress = true;
  198.  
  199.    return visit_continue;
  200. }
  201.