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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
/*
* Copyright (c) 2015 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 lower_shared_reference.cpp
*
* IR lower pass to replace dereferences of compute shader shared variables
* with intrinsic function calls.
*
* This relieves drivers of the responsibility of allocating space for the
* shared variables in the shared memory region.
*/
#include "lower_buffer_access.h"
#include "ir_builder.h"
#include "main/macros.h"
#include "util/list.h"
#include "glsl_parser_extras.h"
using namespace ir_builder;
namespace {
struct var_offset {
struct list_head node;
const ir_variable *var;
unsigned offset;
};
class lower_shared_reference_visitor :
public lower_buffer_access::lower_buffer_access {
public:
lower_shared_reference_visitor(struct gl_shader *shader)
: list_ctx(ralloc_context(NULL)), shader(shader), shared_size(0u)
{
list_inithead(&var_offsets);
}
~lower_shared_reference_visitor()
{
ralloc_free(list_ctx);
}
enum {
shared_load_access,
shared_store_access,
shared_atomic_access,
} buffer_access_type;
void insert_buffer_access(void *mem_ctx, ir_dereference *deref,
const glsl_type *type, ir_rvalue *offset,
unsigned mask, int channel);
void handle_rvalue(ir_rvalue **rvalue);
ir_visitor_status visit_enter(ir_assignment *ir);
void handle_assignment(ir_assignment *ir);
unsigned get_shared_offset(const ir_variable *);
ir_call *shared_load(void *mem_ctx, const struct glsl_type *type,
ir_rvalue *offset);
ir_call *shared_store(void *mem_ctx, ir_rvalue *deref, ir_rvalue *offset,
unsigned write_mask);
void *list_ctx;
struct gl_shader *shader;
struct list_head var_offsets;
unsigned shared_size;
bool progress;
};
unsigned
lower_shared_reference_visitor::get_shared_offset(const ir_variable *var)
{
list_for_each_entry(var_offset, var_entry, &var_offsets, node) {
if (var_entry->var == var)
return var_entry->offset;
}
struct var_offset *new_entry = rzalloc(list_ctx, struct var_offset);
list_add(&new_entry->node, &var_offsets);
new_entry->var = var;
unsigned var_align = var->type->std430_base_alignment(false);
new_entry->offset = glsl_align(shared_size, var_align);
unsigned var_size = var->type->std430_size(false);
shared_size = new_entry->offset + var_size;
return new_entry->offset;
}
void
lower_shared_reference_visitor::handle_rvalue(ir_rvalue **rvalue)
{
if (!*rvalue)
return;
ir_dereference *deref = (*rvalue)->as_dereference();
if (!deref)
return;
ir_variable *var = deref->variable_referenced();
if (!var || var->data.mode != ir_var_shader_shared)
return;
buffer_access_type = shared_load_access;
void *mem_ctx = ralloc_parent(shader->ir);
ir_rvalue *offset = NULL;
unsigned const_offset = get_shared_offset(var);
bool row_major;
int matrix_columns;
assert(var->get_interface_type() == NULL);
const unsigned packing = GLSL_INTERFACE_PACKING_STD430;
setup_buffer_access(mem_ctx, var, deref,
&offset, &const_offset,
&row_major, &matrix_columns, packing);
/* Now that we've calculated the offset to the start of the
* dereference, walk over the type and emit loads into a temporary.
*/
const glsl_type *type = (*rvalue)->type;
ir_variable *load_var = new(mem_ctx) ir_variable(type,
"shared_load_temp",
ir_var_temporary);
base_ir->insert_before(load_var);
ir_variable *load_offset = new(mem_ctx) ir_variable(glsl_type::uint_type,
"shared_load_temp_offset",
ir_var_temporary);
base_ir->insert_before(load_offset);
base_ir->insert_before(assign(load_offset, offset));
deref = new(mem_ctx) ir_dereference_variable(load_var);
emit_access(mem_ctx, false, deref, load_offset, const_offset, row_major,
matrix_columns, packing, 0);
*rvalue = deref;
progress = true;
}
void
lower_shared_reference_visitor::handle_assignment(ir_assignment *ir)
{
if (!ir || !ir->lhs)
return;
ir_rvalue *rvalue = ir->lhs->as_rvalue();
if (!rvalue)
return;
ir_dereference *deref = ir->lhs->as_dereference();
if (!deref)
return;
ir_variable *var = ir->lhs->variable_referenced();
if (!var || var->data.mode != ir_var_shader_shared)
return;
buffer_access_type = shared_store_access;
/* We have a write to a shared variable, so declare a temporary and rewrite
* the assignment so that the temporary is the LHS.
*/
void *mem_ctx = ralloc_parent(shader->ir);
const glsl_type *type = rvalue->type;
ir_variable *store_var = new(mem_ctx) ir_variable(type,
"shared_store_temp",
ir_var_temporary);
base_ir->insert_before(store_var);
ir->lhs = new(mem_ctx) ir_dereference_variable(store_var);
ir_rvalue *offset = NULL;
unsigned const_offset = get_shared_offset(var);
bool row_major;
int matrix_columns;
assert(var->get_interface_type() == NULL);
const unsigned packing = GLSL_INTERFACE_PACKING_STD430;
setup_buffer_access(mem_ctx, var, deref,
&offset, &const_offset,
&row_major, &matrix_columns, packing);
deref = new(mem_ctx) ir_dereference_variable(store_var);
ir_variable *store_offset = new(mem_ctx) ir_variable(glsl_type::uint_type,
"shared_store_temp_offset",
ir_var_temporary);
base_ir->insert_before(store_offset);
base_ir->insert_before(assign(store_offset, offset));
/* Now we have to write the value assigned to the temporary back to memory */
emit_access(mem_ctx, true, deref, store_offset, const_offset, row_major,
matrix_columns, packing, ir->write_mask);
progress = true;
}
ir_visitor_status
lower_shared_reference_visitor::visit_enter(ir_assignment *ir)
{
handle_assignment(ir);
return rvalue_visit(ir);
}
void
lower_shared_reference_visitor::insert_buffer_access(void *mem_ctx,
ir_dereference *deref,
const glsl_type *type,
ir_rvalue *offset,
unsigned mask,
int channel)
{
if (buffer_access_type == shared_store_access) {
ir_call *store = shared_store(mem_ctx, deref, offset, mask);
base_ir->insert_after(store);
} else {
ir_call *load = shared_load(mem_ctx, type, offset);
base_ir->insert_before(load);
ir_rvalue *value = load->return_deref->as_rvalue()->clone(mem_ctx, NULL);
base_ir->insert_before(assign(deref->clone(mem_ctx, NULL),
value));
}
}
static bool
compute_shader_enabled(const _mesa_glsl_parse_state *state)
{
return state->stage == MESA_SHADER_COMPUTE;
}
ir_call *
lower_shared_reference_visitor::shared_store(void *mem_ctx,
ir_rvalue *deref,
ir_rvalue *offset,
unsigned write_mask)
{
exec_list sig_params;
ir_variable *offset_ref = new(mem_ctx)
ir_variable(glsl_type::uint_type, "offset" , ir_var_function_in);
sig_params.push_tail(offset_ref);
ir_variable *val_ref = new(mem_ctx)
ir_variable(deref->type, "value" , ir_var_function_in);
sig_params.push_tail(val_ref);
ir_variable *writemask_ref = new(mem_ctx)
ir_variable(glsl_type::uint_type, "write_mask" , ir_var_function_in);
sig_params.push_tail(writemask_ref);
ir_function_signature *sig = new(mem_ctx)
ir_function_signature(glsl_type::void_type, compute_shader_enabled);
assert(sig);
sig->replace_parameters(&sig_params);
sig->is_intrinsic = true;
ir_function *f = new(mem_ctx) ir_function("__intrinsic_store_shared");
f->add_signature(sig);
exec_list call_params;
call_params.push_tail(offset->clone(mem_ctx, NULL));
call_params.push_tail(deref->clone(mem_ctx, NULL));
call_params.push_tail(new(mem_ctx) ir_constant(write_mask));
return new(mem_ctx) ir_call(sig, NULL, &call_params);
}
ir_call *
lower_shared_reference_visitor::shared_load(void *mem_ctx,
const struct glsl_type *type,
ir_rvalue *offset)
{
exec_list sig_params;
ir_variable *offset_ref = new(mem_ctx)
ir_variable(glsl_type::uint_type, "offset_ref" , ir_var_function_in);
sig_params.push_tail(offset_ref);
ir_function_signature *sig =
new(mem_ctx) ir_function_signature(type, compute_shader_enabled);
assert(sig);
sig->replace_parameters(&sig_params);
sig->is_intrinsic = true;
ir_function *f = new(mem_ctx) ir_function("__intrinsic_load_shared");
f->add_signature(sig);
ir_variable *result = new(mem_ctx)
ir_variable(type, "shared_load_result", ir_var_temporary);
base_ir->insert_before(result);
ir_dereference_variable *deref_result = new(mem_ctx)
ir_dereference_variable(result);
exec_list call_params;
call_params.push_tail(offset->clone(mem_ctx, NULL));
return new(mem_ctx) ir_call(sig, deref_result, &call_params);
}
} /* unnamed namespace */
void
lower_shared_reference(struct gl_shader *shader, unsigned *shared_size)
{
if (shader->Stage != MESA_SHADER_COMPUTE)
return;
lower_shared_reference_visitor v(shader);
/* Loop over the instructions lowering references, because we take a deref
* of an shared variable array using a shared variable dereference as the
* index will produce a collection of instructions all of which have cloned
* shared variable dereferences for that array index.
*/
do {
v.progress = false;
visit_list_elements(&v, shader->ir);
} while (v.progress);
*shared_size = v.shared_size;
}
|