2 * Copyright (C) 2015-2018 Rob Clark <robclark@freedesktop.org>
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * Rob Clark <robclark@freedesktop.org>
27 #include "util/u_math.h"
29 #include "ir3_compiler.h"
30 #include "ir3_context.h"
31 #include "ir3_shader.h"
35 ir3_context_init(struct ir3_compiler
*compiler
,
36 struct ir3_shader_variant
*so
)
38 struct ir3_context
*ctx
= rzalloc(NULL
, struct ir3_context
);
40 if (compiler
->gpu_id
>= 400) {
41 if (so
->type
== MESA_SHADER_VERTEX
) {
42 ctx
->astc_srgb
= so
->key
.vastc_srgb
;
43 } else if (so
->type
== MESA_SHADER_FRAGMENT
) {
44 ctx
->astc_srgb
= so
->key
.fastc_srgb
;
48 if (so
->type
== MESA_SHADER_VERTEX
) {
49 ctx
->samples
= so
->key
.vsamples
;
50 } else if (so
->type
== MESA_SHADER_FRAGMENT
) {
51 ctx
->samples
= so
->key
.fsamples
;
55 ctx
->compiler
= compiler
;
57 ctx
->def_ht
= _mesa_hash_table_create(ctx
,
58 _mesa_hash_pointer
, _mesa_key_pointer_equal
);
59 ctx
->block_ht
= _mesa_hash_table_create(ctx
,
60 _mesa_hash_pointer
, _mesa_key_pointer_equal
);
62 /* TODO: maybe generate some sort of bitmask of what key
63 * lowers vs what shader has (ie. no need to lower
64 * texture clamp lowering if no texture sample instrs)..
65 * although should be done further up the stack to avoid
66 * creating duplicate variants..
69 if (ir3_key_lowers_nir(&so
->key
)) {
70 nir_shader
*s
= nir_shader_clone(ctx
, so
->shader
->nir
);
71 ctx
->s
= ir3_optimize_nir(so
->shader
, s
, &so
->key
);
73 /* fast-path for shader key that lowers nothing in NIR: */
74 ctx
->s
= so
->shader
->nir
;
77 /* this needs to be the last pass run, so do this here instead of
78 * in ir3_optimize_nir():
80 NIR_PASS_V(ctx
->s
, nir_lower_locals_to_regs
);
81 NIR_PASS_V(ctx
->s
, nir_convert_from_ssa
, true);
83 if (ir3_shader_debug
& IR3_DBG_DISASM
) {
84 DBG("dump nir%dv%d: type=%d, k={cts=%u,hp=%u}",
85 so
->shader
->id
, so
->id
, so
->type
,
86 so
->key
.color_two_side
, so
->key
.half_precision
);
87 nir_print_shader(ctx
->s
, stdout
);
90 if (shader_debug_enabled(so
->type
)) {
91 fprintf(stderr
, "NIR (final form) for %s shader:\n",
92 _mesa_shader_stage_to_string(so
->type
));
93 nir_print_shader(ctx
->s
, stderr
);
96 ir3_nir_scan_driver_consts(ctx
->s
, &so
->const_layout
);
98 so
->num_uniforms
= ctx
->s
->num_uniforms
;
99 so
->num_ubos
= ctx
->s
->info
.num_ubos
;
101 /* Layout of constant registers, each section aligned to vec4. Note
102 * that pointer size (ubo, etc) changes depending on generation.
107 * if (vertex shader) {
108 * driver params (IR3_DP_*)
109 * if (stream_output.num_outputs > 0)
110 * stream-out addresses
114 * Immediates go last mostly because they are inserted in the CP pass
115 * after the nir -> ir3 frontend.
117 unsigned constoff
= align(ctx
->s
->num_uniforms
, 4);
118 unsigned ptrsz
= ir3_pointer_size(ctx
);
120 memset(&so
->constbase
, ~0, sizeof(so
->constbase
));
122 if (so
->num_ubos
> 0) {
123 so
->constbase
.ubo
= constoff
;
124 constoff
+= align(ctx
->s
->info
.num_ubos
* ptrsz
, 4) / 4;
127 if (so
->const_layout
.ssbo_size
.count
> 0) {
128 unsigned cnt
= so
->const_layout
.ssbo_size
.count
;
129 so
->constbase
.ssbo_sizes
= constoff
;
130 constoff
+= align(cnt
, 4) / 4;
133 if (so
->const_layout
.image_dims
.count
> 0) {
134 unsigned cnt
= so
->const_layout
.image_dims
.count
;
135 so
->constbase
.image_dims
= constoff
;
136 constoff
+= align(cnt
, 4) / 4;
139 unsigned num_driver_params
= 0;
140 if (so
->type
== MESA_SHADER_VERTEX
) {
141 num_driver_params
= IR3_DP_VS_COUNT
;
142 } else if (so
->type
== MESA_SHADER_COMPUTE
) {
143 num_driver_params
= IR3_DP_CS_COUNT
;
146 so
->constbase
.driver_param
= constoff
;
147 constoff
+= align(num_driver_params
, 4) / 4;
149 if ((so
->type
== MESA_SHADER_VERTEX
) &&
150 (compiler
->gpu_id
< 500) &&
151 so
->shader
->stream_output
.num_outputs
> 0) {
152 so
->constbase
.tfbo
= constoff
;
153 constoff
+= align(IR3_MAX_SO_BUFFERS
* ptrsz
, 4) / 4;
156 so
->constbase
.immediate
= constoff
;
162 ir3_context_free(struct ir3_context
*ctx
)
171 /* allocate a n element value array (to be populated by caller) and
174 struct ir3_instruction
**
175 ir3_get_dst_ssa(struct ir3_context
*ctx
, nir_ssa_def
*dst
, unsigned n
)
177 struct ir3_instruction
**value
=
178 ralloc_array(ctx
->def_ht
, struct ir3_instruction
*, n
);
179 _mesa_hash_table_insert(ctx
->def_ht
, dst
, value
);
183 struct ir3_instruction
**
184 ir3_get_dst(struct ir3_context
*ctx
, nir_dest
*dst
, unsigned n
)
186 struct ir3_instruction
**value
;
189 value
= ir3_get_dst_ssa(ctx
, &dst
->ssa
, n
);
191 value
= ralloc_array(ctx
, struct ir3_instruction
*, n
);
194 /* NOTE: in non-ssa case, we don't really need to store last_dst
195 * but this helps us catch cases where put_dst() call is forgotten
197 compile_assert(ctx
, !ctx
->last_dst
);
198 ctx
->last_dst
= value
;
204 struct ir3_instruction
* const *
205 ir3_get_src(struct ir3_context
*ctx
, nir_src
*src
)
208 struct hash_entry
*entry
;
209 entry
= _mesa_hash_table_search(ctx
->def_ht
, src
->ssa
);
210 compile_assert(ctx
, entry
);
213 nir_register
*reg
= src
->reg
.reg
;
214 struct ir3_array
*arr
= ir3_get_array(ctx
, reg
);
215 unsigned num_components
= arr
->r
->num_components
;
216 struct ir3_instruction
*addr
= NULL
;
217 struct ir3_instruction
**value
=
218 ralloc_array(ctx
, struct ir3_instruction
*, num_components
);
220 if (src
->reg
.indirect
)
221 addr
= ir3_get_addr(ctx
, ir3_get_src(ctx
, src
->reg
.indirect
)[0],
222 reg
->num_components
);
224 for (unsigned i
= 0; i
< num_components
; i
++) {
225 unsigned n
= src
->reg
.base_offset
* reg
->num_components
+ i
;
226 compile_assert(ctx
, n
< arr
->length
);
227 value
[i
] = ir3_create_array_load(ctx
, arr
, n
, addr
);
235 put_dst(struct ir3_context
*ctx
, nir_dest
*dst
)
237 unsigned bit_size
= nir_dest_bit_size(*dst
);
240 for (unsigned i
= 0; i
< ctx
->last_dst_n
; i
++) {
241 struct ir3_instruction
*dst
= ctx
->last_dst
[i
];
242 dst
->regs
[0]->flags
|= IR3_REG_HALF
;
243 if (ctx
->last_dst
[i
]->opc
== OPC_META_FO
)
244 dst
->regs
[1]->instr
->regs
[0]->flags
|= IR3_REG_HALF
;
249 nir_register
*reg
= dst
->reg
.reg
;
250 struct ir3_array
*arr
= ir3_get_array(ctx
, reg
);
251 unsigned num_components
= ctx
->last_dst_n
;
252 struct ir3_instruction
*addr
= NULL
;
254 if (dst
->reg
.indirect
)
255 addr
= ir3_get_addr(ctx
, ir3_get_src(ctx
, dst
->reg
.indirect
)[0],
256 reg
->num_components
);
258 for (unsigned i
= 0; i
< num_components
; i
++) {
259 unsigned n
= dst
->reg
.base_offset
* reg
->num_components
+ i
;
260 compile_assert(ctx
, n
< arr
->length
);
261 if (!ctx
->last_dst
[i
])
263 ir3_create_array_store(ctx
, arr
, n
, ctx
->last_dst
[i
], addr
);
266 ralloc_free(ctx
->last_dst
);
268 ctx
->last_dst
= NULL
;
272 struct ir3_instruction
*
273 ir3_create_collect(struct ir3_context
*ctx
, struct ir3_instruction
*const *arr
,
276 struct ir3_block
*block
= ctx
->block
;
277 struct ir3_instruction
*collect
;
282 unsigned flags
= arr
[0]->regs
[0]->flags
& IR3_REG_HALF
;
284 collect
= ir3_instr_create2(block
, OPC_META_FI
, 1 + arrsz
);
285 ir3_reg_create(collect
, 0, flags
); /* dst */
286 for (unsigned i
= 0; i
< arrsz
; i
++) {
287 struct ir3_instruction
*elem
= arr
[i
];
289 /* Since arrays are pre-colored in RA, we can't assume that
290 * things will end up in the right place. (Ie. if a collect
291 * joins elements from two different arrays.) So insert an
294 * We could possibly skip this if all the collected elements
295 * are contiguous elements in a single array.. not sure how
296 * likely that is to happen.
298 * Fixes a problem with glamor shaders, that in effect do
305 * color = texture2D(tex, texcoord);
307 * In this case, texcoord will end up as nir registers (which
308 * translate to ir3 array's of length 1. And we can't assume
309 * the two (or more) arrays will get allocated in consecutive
313 if (elem
->regs
[0]->flags
& IR3_REG_ARRAY
) {
314 type_t type
= (flags
& IR3_REG_HALF
) ? TYPE_U16
: TYPE_U32
;
315 elem
= ir3_MOV(block
, elem
, type
);
318 compile_assert(ctx
, (elem
->regs
[0]->flags
& IR3_REG_HALF
) == flags
);
319 ir3_reg_create(collect
, 0, IR3_REG_SSA
| flags
)->instr
= elem
;
325 /* helper for instructions that produce multiple consecutive scalar
326 * outputs which need to have a split/fanout meta instruction inserted
329 ir3_split_dest(struct ir3_block
*block
, struct ir3_instruction
**dst
,
330 struct ir3_instruction
*src
, unsigned base
, unsigned n
)
332 struct ir3_instruction
*prev
= NULL
;
334 if ((n
== 1) && (src
->regs
[0]->wrmask
== 0x1)) {
339 for (int i
= 0, j
= 0; i
< n
; i
++) {
340 struct ir3_instruction
*split
= ir3_instr_create(block
, OPC_META_FO
);
341 ir3_reg_create(split
, 0, IR3_REG_SSA
);
342 ir3_reg_create(split
, 0, IR3_REG_SSA
)->instr
= src
;
343 split
->fo
.off
= i
+ base
;
346 split
->cp
.left
= prev
;
347 split
->cp
.left_cnt
++;
348 prev
->cp
.right
= split
;
349 prev
->cp
.right_cnt
++;
353 if (src
->regs
[0]->wrmask
& (1 << (i
+ base
)))
359 ir3_context_error(struct ir3_context
*ctx
, const char *format
, ...)
361 struct hash_table
*errors
= NULL
;
363 va_start(ap
, format
);
364 if (ctx
->cur_instr
) {
365 errors
= _mesa_hash_table_create(NULL
,
367 _mesa_key_pointer_equal
);
368 char *msg
= ralloc_vasprintf(errors
, format
, ap
);
369 _mesa_hash_table_insert(errors
, ctx
->cur_instr
, msg
);
371 _debug_vprintf(format
, ap
);
374 nir_print_shader_annotated(ctx
->s
, stdout
, errors
);
380 static struct ir3_instruction
*
381 create_addr(struct ir3_block
*block
, struct ir3_instruction
*src
, int align
)
383 struct ir3_instruction
*instr
, *immed
;
385 /* TODO in at least some cases, the backend could probably be
386 * made clever enough to propagate IR3_REG_HALF..
388 instr
= ir3_COV(block
, src
, TYPE_U32
, TYPE_S16
);
389 instr
->regs
[0]->flags
|= IR3_REG_HALF
;
396 /* src *= 2 => src <<= 1: */
397 immed
= create_immed(block
, 1);
398 immed
->regs
[0]->flags
|= IR3_REG_HALF
;
400 instr
= ir3_SHL_B(block
, instr
, 0, immed
, 0);
401 instr
->regs
[0]->flags
|= IR3_REG_HALF
;
402 instr
->regs
[1]->flags
|= IR3_REG_HALF
;
406 immed
= create_immed(block
, 3);
407 immed
->regs
[0]->flags
|= IR3_REG_HALF
;
409 instr
= ir3_MULL_U(block
, instr
, 0, immed
, 0);
410 instr
->regs
[0]->flags
|= IR3_REG_HALF
;
411 instr
->regs
[1]->flags
|= IR3_REG_HALF
;
414 /* src *= 4 => src <<= 2: */
415 immed
= create_immed(block
, 2);
416 immed
->regs
[0]->flags
|= IR3_REG_HALF
;
418 instr
= ir3_SHL_B(block
, instr
, 0, immed
, 0);
419 instr
->regs
[0]->flags
|= IR3_REG_HALF
;
420 instr
->regs
[1]->flags
|= IR3_REG_HALF
;
423 unreachable("bad align");
427 instr
= ir3_MOV(block
, instr
, TYPE_S16
);
428 instr
->regs
[0]->num
= regid(REG_A0
, 0);
429 instr
->regs
[0]->flags
|= IR3_REG_HALF
;
430 instr
->regs
[1]->flags
|= IR3_REG_HALF
;
435 /* caches addr values to avoid generating multiple cov/shl/mova
436 * sequences for each use of a given NIR level src as address
438 struct ir3_instruction
*
439 ir3_get_addr(struct ir3_context
*ctx
, struct ir3_instruction
*src
, int align
)
441 struct ir3_instruction
*addr
;
442 unsigned idx
= align
- 1;
444 compile_assert(ctx
, idx
< ARRAY_SIZE(ctx
->addr_ht
));
446 if (!ctx
->addr_ht
[idx
]) {
447 ctx
->addr_ht
[idx
] = _mesa_hash_table_create(ctx
,
448 _mesa_hash_pointer
, _mesa_key_pointer_equal
);
450 struct hash_entry
*entry
;
451 entry
= _mesa_hash_table_search(ctx
->addr_ht
[idx
], src
);
456 addr
= create_addr(ctx
->block
, src
, align
);
457 _mesa_hash_table_insert(ctx
->addr_ht
[idx
], src
, addr
);
462 struct ir3_instruction
*
463 ir3_get_predicate(struct ir3_context
*ctx
, struct ir3_instruction
*src
)
465 struct ir3_block
*b
= ctx
->block
;
466 struct ir3_instruction
*cond
;
468 /* NOTE: only cmps.*.* can write p0.x: */
469 cond
= ir3_CMPS_S(b
, src
, 0, create_immed(b
, 0), 0);
470 cond
->cat2
.condition
= IR3_COND_NE
;
472 /* condition always goes in predicate register: */
473 cond
->regs
[0]->num
= regid(REG_P0
, 0);
483 ir3_declare_array(struct ir3_context
*ctx
, nir_register
*reg
)
485 struct ir3_array
*arr
= rzalloc(ctx
, struct ir3_array
);
486 arr
->id
= ++ctx
->num_arrays
;
487 /* NOTE: sometimes we get non array regs, for example for arrays of
488 * length 1. See fs-const-array-of-struct-of-array.shader_test. So
489 * treat a non-array as if it was an array of length 1.
491 * It would be nice if there was a nir pass to convert arrays of
494 arr
->length
= reg
->num_components
* MAX2(1, reg
->num_array_elems
);
495 compile_assert(ctx
, arr
->length
> 0);
497 list_addtail(&arr
->node
, &ctx
->ir
->array_list
);
501 ir3_get_array(struct ir3_context
*ctx
, nir_register
*reg
)
503 list_for_each_entry (struct ir3_array
, arr
, &ctx
->ir
->array_list
, node
) {
507 ir3_context_error(ctx
, "bogus reg: %s\n", reg
->name
);
511 /* relative (indirect) if address!=NULL */
512 struct ir3_instruction
*
513 ir3_create_array_load(struct ir3_context
*ctx
, struct ir3_array
*arr
, int n
,
514 struct ir3_instruction
*address
)
516 struct ir3_block
*block
= ctx
->block
;
517 struct ir3_instruction
*mov
;
518 struct ir3_register
*src
;
520 mov
= ir3_instr_create(block
, OPC_MOV
);
521 mov
->cat1
.src_type
= TYPE_U32
;
522 mov
->cat1
.dst_type
= TYPE_U32
;
523 mov
->barrier_class
= IR3_BARRIER_ARRAY_R
;
524 mov
->barrier_conflict
= IR3_BARRIER_ARRAY_W
;
525 ir3_reg_create(mov
, 0, 0);
526 src
= ir3_reg_create(mov
, 0, IR3_REG_ARRAY
|
527 COND(address
, IR3_REG_RELATIV
));
528 src
->instr
= arr
->last_write
;
529 src
->size
= arr
->length
;
530 src
->array
.id
= arr
->id
;
531 src
->array
.offset
= n
;
534 ir3_instr_set_address(mov
, address
);
539 /* relative (indirect) if address!=NULL */
541 ir3_create_array_store(struct ir3_context
*ctx
, struct ir3_array
*arr
, int n
,
542 struct ir3_instruction
*src
, struct ir3_instruction
*address
)
544 struct ir3_block
*block
= ctx
->block
;
545 struct ir3_instruction
*mov
;
546 struct ir3_register
*dst
;
548 /* if not relative store, don't create an extra mov, since that
549 * ends up being difficult for cp to remove.
554 src
->barrier_class
|= IR3_BARRIER_ARRAY_W
;
555 src
->barrier_conflict
|= IR3_BARRIER_ARRAY_R
| IR3_BARRIER_ARRAY_W
;
557 dst
->flags
|= IR3_REG_ARRAY
;
558 dst
->instr
= arr
->last_write
;
559 dst
->size
= arr
->length
;
560 dst
->array
.id
= arr
->id
;
561 dst
->array
.offset
= n
;
563 arr
->last_write
= src
;
565 array_insert(block
, block
->keeps
, src
);
570 mov
= ir3_instr_create(block
, OPC_MOV
);
571 mov
->cat1
.src_type
= TYPE_U32
;
572 mov
->cat1
.dst_type
= TYPE_U32
;
573 mov
->barrier_class
= IR3_BARRIER_ARRAY_W
;
574 mov
->barrier_conflict
= IR3_BARRIER_ARRAY_R
| IR3_BARRIER_ARRAY_W
;
575 dst
= ir3_reg_create(mov
, 0, IR3_REG_ARRAY
|
576 COND(address
, IR3_REG_RELATIV
));
577 dst
->instr
= arr
->last_write
;
578 dst
->size
= arr
->length
;
579 dst
->array
.id
= arr
->id
;
580 dst
->array
.offset
= n
;
581 ir3_reg_create(mov
, 0, IR3_REG_SSA
)->instr
= src
;
584 ir3_instr_set_address(mov
, address
);
586 arr
->last_write
= mov
;
588 /* the array store may only matter to something in an earlier
589 * block (ie. loops), but since arrays are not in SSA, depth
590 * pass won't know this.. so keep all array stores:
592 array_insert(block
, block
->keeps
, mov
);