1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
/*
* Copyright © 2010 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/**
* \file ir_mat_op_to_vec.cpp
*
* Breaks matrix operation expressions down to a series of vector operations.
*
* Generally this is how we have to codegen matrix operations for a
* GPU, so this gives us the chance to constant fold operations on a
* column or row.
*/
#include "ir.h"
#include "ir_expression_flattening.h"
#include "glsl_types.h"
class ir_mat_op_to_vec_visitor : public ir_hierarchical_visitor {
public:
ir_mat_op_to_vec_visitor()
{
this->made_progress = false;
}
ir_visitor_status visit_leave(ir_assignment *);
ir_rvalue *get_column(ir_variable *var, int i);
bool made_progress;
};
static bool
mat_op_to_vec_predicate(ir_instruction *ir)
{
ir_expression *expr = ir->as_expression();
unsigned int i;
if (!expr)
return false;
for (i = 0; i < expr->get_num_operands(); i++) {
if (expr->operands[i]->type->is_matrix())
return true;
}
return false;
}
bool
do_mat_op_to_vec(exec_list *instructions)
{
ir_mat_op_to_vec_visitor v;
/* Pull out any matrix expression to a separate assignment to a
* temp. This will make our handling of the breakdown to
* operations on the matrix's vector components much easier.
*/
do_expression_flattening(instructions, mat_op_to_vec_predicate);
visit_list_elements(&v, instructions);
return v.made_progress;
}
ir_rvalue *
ir_mat_op_to_vec_visitor::get_column(ir_variable *var, int i)
{
ir_dereference *deref;
if (!var->type->is_matrix()) {
deref = new(base_ir) ir_dereference_variable(var);
} else {
deref = new(base_ir) ir_dereference_variable(var);
deref = new(base_ir) ir_dereference_array(deref,
new(base_ir) ir_constant(i));
}
return deref;
}
ir_visitor_status
ir_mat_op_to_vec_visitor::visit_leave(ir_assignment *assign)
{
ir_expression *expr = assign->rhs->as_expression();
bool found_matrix = false;
unsigned int i, matrix_columns = 1;
ir_variable *op_var[2];
if (!expr)
return visit_continue;
for (i = 0; i < expr->get_num_operands(); i++) {
if (expr->operands[i]->type->is_matrix()) {
found_matrix = true;
matrix_columns = expr->operands[i]->type->matrix_columns;
break;
}
}
if (!found_matrix)
return visit_continue;
/* FINISHME: see below */
if (expr->operation == ir_binop_mul)
return visit_continue;
ir_dereference_variable *lhs_deref = assign->lhs->as_dereference_variable();
assert(lhs_deref);
ir_variable *result_var = lhs_deref->var;
/* Store the expression operands in temps so we can use them
* multiple times.
*/
for (i = 0; i < expr->get_num_operands(); i++) {
ir_assignment *assign;
op_var[i] = new(base_ir) ir_variable(expr->operands[i]->type,
"mat_op_to_vec");
base_ir->insert_before(op_var[i]);
lhs_deref = new(base_ir) ir_dereference_variable(op_var[i]);
assign = new(base_ir) ir_assignment(lhs_deref,
expr->operands[i],
NULL);
base_ir->insert_before(assign);
}
/* OK, time to break down this matrix operation. */
switch (expr->operation) {
case ir_binop_add:
case ir_binop_sub:
case ir_binop_div:
case ir_binop_mod:
/* For most operations, the matrix version is just going
* column-wise through and applying the operation to each column
* if available.
*/
for (i = 0; i < matrix_columns; i++) {
ir_rvalue *op0 = get_column(op_var[0], i);
ir_rvalue *op1 = get_column(op_var[1], i);
ir_rvalue *result = get_column(result_var, i);
ir_expression *column_expr;
ir_assignment *column_assign;
column_expr = new(base_ir) ir_expression(expr->operation,
result->type,
op0,
op1);
column_assign = new(base_ir) ir_assignment(result,
column_expr,
NULL);
base_ir->insert_before(column_assign);
}
break;
case ir_binop_mul:
/* FINISHME */
return visit_continue;
break;
default:
printf("FINISHME: Handle matrix operation for %s\n", expr->operator_string());
abort();
}
assign->remove();
this->made_progress = true;
return visit_continue;
}
|