• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2013 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 #include "ir.h"
24 #include "ir_builder.h"
25 #include "ir_rvalue_visitor.h"
26 #include "ir_optimization.h"
27 #include "main/mtypes.h"
28 
29 using namespace ir_builder;
30 
31 namespace {
32 
33 class vector_deref_visitor : public ir_rvalue_enter_visitor {
34 public:
vector_deref_visitor(void * mem_ctx,gl_shader_stage shader_stage)35    vector_deref_visitor(void *mem_ctx, gl_shader_stage shader_stage)
36       : progress(false), shader_stage(shader_stage),
37         factory(&factory_instructions, mem_ctx)
38    {
39    }
40 
~vector_deref_visitor()41    virtual ~vector_deref_visitor()
42    {
43    }
44 
45    virtual void handle_rvalue(ir_rvalue **rv);
46    virtual ir_visitor_status visit_enter(ir_assignment *ir);
47 
48    bool progress;
49    gl_shader_stage shader_stage;
50    exec_list factory_instructions;
51    ir_factory factory;
52 };
53 
54 } /* anonymous namespace */
55 
56 ir_visitor_status
visit_enter(ir_assignment * ir)57 vector_deref_visitor::visit_enter(ir_assignment *ir)
58 {
59    if (!ir->lhs || ir->lhs->ir_type != ir_type_dereference_array)
60       return ir_rvalue_enter_visitor::visit_enter(ir);
61 
62    ir_dereference_array *const deref = (ir_dereference_array *) ir->lhs;
63    if (!deref->array->type->is_vector())
64       return ir_rvalue_enter_visitor::visit_enter(ir);
65 
66    /* SSBOs and shared variables are backed by memory and may be accessed by
67     * multiple threads simultaneously.  It's not safe to lower a single
68     * component store to a load-vec-store because it may race with writes to
69     * other components.
70     */
71    ir_variable *var = deref->variable_referenced();
72    if (var->data.mode == ir_var_shader_storage ||
73        var->data.mode == ir_var_shader_shared)
74       return ir_rvalue_enter_visitor::visit_enter(ir);
75 
76    ir_rvalue *const new_lhs = deref->array;
77 
78    void *mem_ctx = ralloc_parent(ir);
79    ir_constant *old_index_constant =
80       deref->array_index->constant_expression_value(mem_ctx);
81    if (!old_index_constant) {
82       if (shader_stage == MESA_SHADER_TESS_CTRL &&
83           deref->variable_referenced()->data.mode == ir_var_shader_out) {
84          /* Tessellation control shader outputs act as if they have memory
85           * backing them and if we have writes from multiple threads
86           * targeting the same vec4 (this can happen for patch outputs), the
87           * load-vec-store pattern of ir_triop_vector_insert doesn't work.
88           * Instead, we have to lower to a series of conditional write-masked
89           * assignments.
90           */
91          ir_variable *const src_temp =
92             factory.make_temp(ir->rhs->type, "scalar_tmp");
93 
94          /* The newly created variable declaration goes before the assignment
95           * because we're going to set it as the new LHS.
96           */
97          ir->insert_before(factory.instructions);
98          ir->set_lhs(new(mem_ctx) ir_dereference_variable(src_temp));
99 
100          ir_variable *const arr_index =
101             factory.make_temp(deref->array_index->type, "index_tmp");
102          factory.emit(assign(arr_index, deref->array_index));
103 
104          for (unsigned i = 0; i < new_lhs->type->vector_elements; i++) {
105             ir_constant *const cmp_index =
106                ir_constant::zero(factory.mem_ctx, deref->array_index->type);
107             cmp_index->value.u[0] = i;
108 
109             ir_rvalue *const lhs_clone = new_lhs->clone(factory.mem_ctx, NULL);
110             ir_dereference_variable *const src_temp_deref =
111                new(mem_ctx) ir_dereference_variable(src_temp);
112 
113             if (new_lhs->ir_type != ir_type_swizzle) {
114                assert(lhs_clone->as_dereference());
115                ir_assignment *cond_assign =
116                   new(mem_ctx) ir_assignment(lhs_clone->as_dereference(),
117                                              src_temp_deref,
118                                              equal(arr_index, cmp_index),
119                                              WRITEMASK_X << i);
120                factory.emit(cond_assign);
121             } else {
122                ir_assignment *cond_assign =
123                   new(mem_ctx) ir_assignment(swizzle(lhs_clone, i, 1),
124                                              src_temp_deref,
125                                              equal(arr_index, cmp_index));
126                factory.emit(cond_assign);
127             }
128          }
129          ir->insert_after(factory.instructions);
130       } else {
131          ir->rhs = new(mem_ctx) ir_expression(ir_triop_vector_insert,
132                                               new_lhs->type,
133                                               new_lhs->clone(mem_ctx, NULL),
134                                               ir->rhs,
135                                               deref->array_index);
136          ir->write_mask = (1 << new_lhs->type->vector_elements) - 1;
137          ir->set_lhs(new_lhs);
138       }
139    } else {
140       unsigned index = old_index_constant->get_uint_component(0);
141 
142       if (index >= new_lhs->type->vector_elements) {
143          /* Section 5.11 (Out-of-Bounds Accesses) of the GLSL 4.60 spec says:
144           *
145           *  In the subsections described above for array, vector, matrix and
146           *  structure accesses, any out-of-bounds access produced undefined
147           *  behavior.... Out-of-bounds writes may be discarded or overwrite
148           *  other variables of the active program.
149           */
150          ir->remove();
151          return visit_continue;
152       }
153 
154       if (new_lhs->ir_type != ir_type_swizzle) {
155          ir->set_lhs(new_lhs);
156          ir->write_mask = 1 << index;
157       } else {
158          /* If the "new" LHS is a swizzle, use the set_lhs helper to instead
159           * swizzle the RHS.
160           */
161          unsigned component[1] = { index };
162          ir->set_lhs(new(mem_ctx) ir_swizzle(new_lhs, component, 1));
163       }
164    }
165 
166    return ir_rvalue_enter_visitor::visit_enter(ir);
167 }
168 
169 void
handle_rvalue(ir_rvalue ** rv)170 vector_deref_visitor::handle_rvalue(ir_rvalue **rv)
171 {
172    if (*rv == NULL || (*rv)->ir_type != ir_type_dereference_array)
173       return;
174 
175    ir_dereference_array *const deref = (ir_dereference_array *) *rv;
176    if (!deref->array->type->is_vector())
177       return;
178 
179    /* Back-ends need to be able to handle derefs on vectors for SSBOs, UBOs,
180     * and shared variables.  They have to handle it for writes anyway so we
181     * may as well require it for reads.
182     */
183    ir_variable *var = deref->variable_referenced();
184    if (var && (var->data.mode == ir_var_shader_storage ||
185                var->data.mode == ir_var_shader_shared ||
186                (var->data.mode == ir_var_uniform &&
187                 var->get_interface_type())))
188       return;
189 
190    void *mem_ctx = ralloc_parent(deref);
191    *rv = new(mem_ctx) ir_expression(ir_binop_vector_extract,
192                                     deref->array,
193                                     deref->array_index);
194 }
195 
196 bool
lower_vector_derefs(gl_linked_shader * shader)197 lower_vector_derefs(gl_linked_shader *shader)
198 {
199    vector_deref_visitor v(shader->ir, shader->Stage);
200 
201    visit_list_elements(&v, shader->ir);
202 
203    return v.progress;
204 }
205