xref: /aosp_15_r20/external/mesa3d/src/compiler/glsl/lower_vec_index_to_cond_assign.cpp (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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_vec_index_to_cond_assign.cpp
26  *
27  * Turns indexing into vector types to a series of conditional moves
28  * of each channel's swizzle into a temporary.
29  *
30  * Most GPUs don't have a native way to do this operation, and this
31  * works around that.  For drivers using both this pass and
32  * ir_vec_index_to_swizzle, there's a risk that this pass will happen
33  * before sufficient constant folding to find that the array index is
34  * constant.  However, we hope that other optimization passes,
35  * particularly constant folding of assignment conditions and copy
36  * propagation, will result in the same code in the end.
37  */
38 
39 #include "ir.h"
40 #include "ir_visitor.h"
41 #include "ir_optimization.h"
42 #include "compiler/glsl_types.h"
43 #include "ir_builder.h"
44 #include "program/prog_instruction.h"
45 
46 using namespace ir_builder;
47 
48 namespace {
49 
50 /**
51  * Visitor class for replacing expressions with ir_constant values.
52  */
53 
54 class ir_vec_index_to_cond_assign_visitor : public ir_hierarchical_visitor {
55 public:
ir_vec_index_to_cond_assign_visitor()56    ir_vec_index_to_cond_assign_visitor()
57       : progress(false)
58    {
59       /* empty */
60    }
61 
62    ir_rvalue *convert_vector_extract_to_cond_assign(ir_rvalue *ir);
63 
64    virtual ir_visitor_status visit_enter(ir_expression *);
65    virtual ir_visitor_status visit_enter(ir_swizzle *);
66    virtual ir_visitor_status visit_leave(ir_assignment *);
67    virtual ir_visitor_status visit_enter(ir_return *);
68    virtual ir_visitor_status visit_enter(ir_call *);
69    virtual ir_visitor_status visit_enter(ir_if *);
70 
71    bool progress;
72 };
73 
74 } /* anonymous namespace */
75 
76 ir_rvalue *
convert_vector_extract_to_cond_assign(ir_rvalue * ir)77 ir_vec_index_to_cond_assign_visitor::convert_vector_extract_to_cond_assign(ir_rvalue *ir)
78 {
79    ir_expression *const expr = ir->as_expression();
80 
81    if (expr == NULL)
82       return ir;
83 
84    if (expr->operation == ir_unop_interpolate_at_centroid ||
85        expr->operation == ir_binop_interpolate_at_offset ||
86        expr->operation == ir_binop_interpolate_at_sample) {
87       /* Lower interpolateAtXxx(some_vec[idx], ...) to
88        * interpolateAtXxx(some_vec, ...)[idx] before lowering to conditional
89        * assignments, to maintain the rule that the interpolant is an l-value
90        * referring to a (part of a) shader input.
91        *
92        * This is required when idx is dynamic (otherwise it gets lowered to
93        * a swizzle).
94        */
95       ir_expression *const interpolant = expr->operands[0]->as_expression();
96       if (!interpolant || interpolant->operation != ir_binop_vector_extract)
97          return ir;
98 
99       ir_rvalue *vec_input = interpolant->operands[0];
100       ir_expression *const vec_interpolate =
101          new(base_ir) ir_expression(expr->operation, vec_input->type,
102                                     vec_input, expr->operands[1]);
103 
104       this->progress = true;
105       return new(base_ir) ir_expression(ir_binop_vector_extract, ir->type,
106                                         vec_interpolate,
107                                         interpolant->operands[1]);
108    }
109 
110    return ir;
111 }
112 
113 ir_visitor_status
visit_enter(ir_expression * ir)114 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_expression *ir)
115 {
116    for (unsigned i = 0; i < ir->num_operands; i++)
117       ir->operands[i] = convert_vector_extract_to_cond_assign(ir->operands[i]);
118 
119    return visit_continue;
120 }
121 
122 ir_visitor_status
visit_enter(ir_swizzle * ir)123 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_swizzle *ir)
124 {
125    /* Can't be hit from normal GLSL, since you can't swizzle a scalar (which
126     * the result of indexing a vector is.  But maybe at some point we'll end up
127     * using swizzling of scalars for vector construction.
128     */
129    ir->val = convert_vector_extract_to_cond_assign(ir->val);
130 
131    return visit_continue;
132 }
133 
134 ir_visitor_status
visit_leave(ir_assignment * ir)135 ir_vec_index_to_cond_assign_visitor::visit_leave(ir_assignment *ir)
136 {
137    ir->rhs = convert_vector_extract_to_cond_assign(ir->rhs);
138 
139    return visit_continue;
140 }
141 
142 ir_visitor_status
visit_enter(ir_call * ir)143 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_call *ir)
144 {
145    foreach_in_list_safe(ir_rvalue, param, &ir->actual_parameters) {
146       ir_rvalue *new_param = convert_vector_extract_to_cond_assign(param);
147 
148       if (new_param != param) {
149          param->replace_with(new_param);
150       }
151    }
152 
153    return visit_continue;
154 }
155 
156 ir_visitor_status
visit_enter(ir_return * ir)157 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_return *ir)
158 {
159    if (ir->value)
160       ir->value = convert_vector_extract_to_cond_assign(ir->value);
161 
162    return visit_continue;
163 }
164 
165 ir_visitor_status
visit_enter(ir_if * ir)166 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_if *ir)
167 {
168    ir->condition = convert_vector_extract_to_cond_assign(ir->condition);
169 
170    return visit_continue;
171 }
172 
173 bool
do_vec_index_to_cond_assign(exec_list * instructions)174 do_vec_index_to_cond_assign(exec_list *instructions)
175 {
176    ir_vec_index_to_cond_assign_visitor v;
177 
178    visit_list_elements(&v, instructions);
179 
180    return v.progress;
181 }
182