ac43a29ce8fed64e9243c1e3665932946975e5c8
2 * Copyright © 2013 Intel Corporation
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:
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
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.
25 * \file opt_vectorize.cpp
27 * Combines scalar assignments of the same expression (modulo swizzle) to
28 * multiple channels of the same variable into a single vectorized expression
31 * Many generated shaders contain scalarized code. That is, they contain
39 * r1.xyz = log2(v0.xyz);
41 * We look for consecutive assignments of the same expression (modulo swizzle)
42 * to each channel of the same variable.
44 * For instance, we want to convert these three scalar operations
46 * (assign (x) (var_ref r1) (expression float log2 (swiz x (var_ref v0))))
47 * (assign (y) (var_ref r1) (expression float log2 (swiz y (var_ref v0))))
48 * (assign (z) (var_ref r1) (expression float log2 (swiz z (var_ref v0))))
50 * into a single vector operation
52 * (assign (xyz) (var_ref r1) (expression vec3 log2 (swiz xyz (var_ref v0))))
56 #include "ir_visitor.h"
57 #include "ir_optimization.h"
58 #include "glsl_types.h"
59 #include "program/prog_instruction.h"
63 class ir_vectorize_visitor
: public ir_hierarchical_visitor
{
71 current_assignment
= NULL
;
72 last_assignment
= NULL
;
77 ir_vectorize_visitor()
83 virtual ir_visitor_status
visit_enter(ir_assignment
*);
84 virtual ir_visitor_status
visit_enter(ir_swizzle
*);
86 virtual ir_visitor_status
visit_leave(ir_assignment
*);
90 ir_assignment
*assignment
[4];
91 ir_assignment
*current_assignment
, *last_assignment
;
98 } /* unnamed namespace */
101 * Rewrites the swizzles and types of a right-hand side of an assignment.
103 * From the example above, this function would be called (by visit_tree()) on
104 * the nodes of the tree (expression float log2 (swiz z (var_ref v0))),
105 * rewriting it into (expression vec3 log2 (swiz xyz (var_ref v0))).
107 * The function modifies only ir_expressions and ir_swizzles. For expressions
108 * it sets a new type and swizzles any scalar dereferences into appropriately
109 * sized vector arguments. For example, if combining
111 * (assign (x) (var_ref r1) (expression float + (swiz x (var_ref v0) (var_ref v1))))
112 * (assign (y) (var_ref r1) (expression float + (swiz y (var_ref v0) (var_ref v1))))
114 * where v1 is a scalar, rewrite_swizzle() would insert a swizzle on
115 * (var_ref v1) such that the final result was
117 * (assign (xy) (var_ref r1) (expression vec2 + (swiz xy (var_ref v0))
118 * (swiz xx (var_ref v1))))
120 * For swizzles, it sets a new type, and if the variable being swizzled is a
121 * vector it overwrites the swizzle mask with the ir_swizzle_mask passed as the
122 * data parameter. If the swizzled variable is scalar, then the swizzle was
123 * added by an earlier call to rewrite_swizzle() on an expression, so the
124 * mask should not be modified.
127 rewrite_swizzle(ir_instruction
*ir
, void *data
)
129 ir_swizzle_mask
*mask
= (ir_swizzle_mask
*)data
;
131 switch (ir
->ir_type
) {
132 case ir_type_swizzle
: {
133 ir_swizzle
*swz
= (ir_swizzle
*)ir
;
134 if (swz
->val
->type
->is_vector()) {
137 swz
->type
= glsl_type::get_instance(swz
->type
->base_type
,
138 mask
->num_components
, 1);
141 case ir_type_expression
: {
142 ir_expression
*expr
= (ir_expression
*)ir
;
143 expr
->type
= glsl_type::get_instance(expr
->type
->base_type
,
144 mask
->num_components
, 1);
145 for (unsigned i
= 0; i
< 4; i
++) {
146 if (expr
->operands
[i
]) {
147 ir_dereference
*deref
= expr
->operands
[i
]->as_dereference();
148 if (deref
&& deref
->type
->is_scalar()) {
149 expr
->operands
[i
] = new(ir
) ir_swizzle(deref
, 0, 0, 0, 0,
150 mask
->num_components
);
162 * Attempt to vectorize the previously saved assignments, and clear them from
165 * If the assignments are able to be combined, it modifies in-place the last
166 * assignment seen to be an equivalent vector form of the scalar assignments.
167 * It then removes the other now obsolete scalar assignments.
170 ir_vectorize_visitor::try_vectorize()
172 if (this->last_assignment
&& this->channels
> 1) {
173 ir_swizzle_mask mask
= {0, 0, 0, 0, channels
, 0};
175 this->last_assignment
->write_mask
= 0;
177 for (unsigned i
= 0, j
= 0; i
< 4; i
++) {
178 if (this->assignment
[i
]) {
179 this->last_assignment
->write_mask
|= 1 << i
;
181 if (this->assignment
[i
] != this->last_assignment
) {
182 this->assignment
[i
]->remove();
186 case 0: mask
.x
= i
; break;
187 case 1: mask
.y
= i
; break;
188 case 2: mask
.z
= i
; break;
189 case 3: mask
.w
= i
; break;
196 visit_tree(this->last_assignment
->rhs
, rewrite_swizzle
, &mask
);
198 this->progress
= true;
204 * Returns whether the write mask is a single channel.
207 single_channel_write_mask(unsigned write_mask
)
209 return write_mask
!= 0 && (write_mask
& (write_mask
- 1)) == 0;
213 * Translates single-channeled write mask to single-channeled swizzle.
216 write_mask_to_swizzle(unsigned write_mask
)
218 switch (write_mask
) {
219 case WRITEMASK_X
: return SWIZZLE_X
;
220 case WRITEMASK_Y
: return SWIZZLE_Y
;
221 case WRITEMASK_Z
: return SWIZZLE_Z
;
222 case WRITEMASK_W
: return SWIZZLE_W
;
224 assert(!"not reached");
229 * Returns whether a single-channeled write mask matches a swizzle.
232 write_mask_matches_swizzle(unsigned write_mask
,
233 const ir_swizzle
*swz
)
235 return ((write_mask
== WRITEMASK_X
&& swz
->mask
.x
== SWIZZLE_X
) ||
236 (write_mask
== WRITEMASK_Y
&& swz
->mask
.x
== SWIZZLE_Y
) ||
237 (write_mask
== WRITEMASK_Z
&& swz
->mask
.x
== SWIZZLE_Z
) ||
238 (write_mask
== WRITEMASK_W
&& swz
->mask
.x
== SWIZZLE_W
));
242 * Upon entering an ir_assignment, attempt to vectorize the currently tracked
243 * assignments if the current assignment is not suitable. Keep a pointer to
244 * the current assignment.
247 ir_vectorize_visitor::visit_enter(ir_assignment
*ir
)
249 ir_dereference
*lhs
= this->last_assignment
!= NULL
?
250 this->last_assignment
->lhs
: NULL
;
251 ir_rvalue
*rhs
= this->last_assignment
!= NULL
?
252 this->last_assignment
->rhs
: NULL
;
255 this->channels
>= 4 ||
256 !single_channel_write_mask(ir
->write_mask
) ||
257 (lhs
&& !ir
->lhs
->equals(lhs
)) ||
258 (rhs
&& !ir
->rhs
->equals(rhs
, ir_type_swizzle
))) {
262 this->current_assignment
= ir
;
264 return visit_continue
;
268 * Upon entering an ir_swizzle, set ::has_swizzle if we're visiting from an
269 * ir_assignment (i.e., that ::current_assignment is set) and the swizzle mask
270 * matches the current assignment's write mask.
272 * If the write mask doesn't match the swizzle mask, remove the current
273 * assignment from further consideration.
276 ir_vectorize_visitor::visit_enter(ir_swizzle
*ir
)
278 if (this->current_assignment
) {
279 if (write_mask_matches_swizzle(this->current_assignment
->write_mask
, ir
)) {
280 this->has_swizzle
= true;
282 this->current_assignment
= NULL
;
285 return visit_continue
;
289 * Upon leaving an ir_assignment, save a pointer to it in ::assignment[] if
290 * the swizzle mask(s) found were appropriate. Also save a pointer in
291 * ::last_assignment so that we can compare future assignments with it.
293 * Finally, clear ::current_assignment and ::has_swizzle.
296 ir_vectorize_visitor::visit_leave(ir_assignment
*ir
)
298 if (this->has_swizzle
&& this->current_assignment
) {
299 assert(this->current_assignment
== ir
);
301 unsigned channel
= write_mask_to_swizzle(this->current_assignment
->write_mask
);
302 this->assignment
[channel
] = ir
;
305 this->last_assignment
= this->current_assignment
;
307 this->current_assignment
= NULL
;
308 this->has_swizzle
= false;
309 return visit_continue
;
313 * Combines scalar assignments of the same expression (modulo swizzle) to
314 * multiple channels of the same variable into a single vectorized expression
318 do_vectorize(exec_list
*instructions
)
320 ir_vectorize_visitor v
;
324 /* Try to vectorize the last assignments seen. */