v3d: Refactor compiler entrypoints.
[mesa.git] / src / broadcom / compiler / vir.c
1 /*
2 * Copyright © 2016-2017 Broadcom
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 DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "broadcom/common/v3d_device_info.h"
25 #include "v3d_compiler.h"
26
27 int
28 vir_get_non_sideband_nsrc(struct qinst *inst)
29 {
30 switch (inst->qpu.type) {
31 case V3D_QPU_INSTR_TYPE_BRANCH:
32 return 0;
33 case V3D_QPU_INSTR_TYPE_ALU:
34 if (inst->qpu.alu.add.op != V3D_QPU_A_NOP)
35 return v3d_qpu_add_op_num_src(inst->qpu.alu.add.op);
36 else
37 return v3d_qpu_mul_op_num_src(inst->qpu.alu.mul.op);
38 }
39
40 return 0;
41 }
42
43 int
44 vir_get_nsrc(struct qinst *inst)
45 {
46 int nsrc = vir_get_non_sideband_nsrc(inst);
47
48 if (vir_has_implicit_uniform(inst))
49 nsrc++;
50
51 return nsrc;
52 }
53
54 bool
55 vir_has_implicit_uniform(struct qinst *inst)
56 {
57 switch (inst->qpu.type) {
58 case V3D_QPU_INSTR_TYPE_BRANCH:
59 return true;
60 case V3D_QPU_INSTR_TYPE_ALU:
61 switch (inst->dst.file) {
62 case QFILE_TLBU:
63 return true;
64 default:
65 return inst->has_implicit_uniform;
66 }
67 }
68 return false;
69 }
70
71 /* The sideband uniform for textures gets stored after the normal ALU
72 * arguments.
73 */
74 int
75 vir_get_implicit_uniform_src(struct qinst *inst)
76 {
77 if (!vir_has_implicit_uniform(inst))
78 return -1;
79 return vir_get_nsrc(inst) - 1;
80 }
81
82 /**
83 * Returns whether the instruction has any side effects that must be
84 * preserved.
85 */
86 bool
87 vir_has_side_effects(struct v3d_compile *c, struct qinst *inst)
88 {
89 switch (inst->qpu.type) {
90 case V3D_QPU_INSTR_TYPE_BRANCH:
91 return true;
92 case V3D_QPU_INSTR_TYPE_ALU:
93 switch (inst->qpu.alu.add.op) {
94 case V3D_QPU_A_SETREVF:
95 case V3D_QPU_A_SETMSF:
96 case V3D_QPU_A_VPMSETUP:
97 case V3D_QPU_A_STVPMV:
98 case V3D_QPU_A_STVPMD:
99 case V3D_QPU_A_STVPMP:
100 case V3D_QPU_A_VPMWT:
101 case V3D_QPU_A_TMUWT:
102 return true;
103 default:
104 break;
105 }
106
107 switch (inst->qpu.alu.mul.op) {
108 case V3D_QPU_M_MULTOP:
109 return true;
110 default:
111 break;
112 }
113 }
114
115 if (inst->qpu.sig.ldtmu ||
116 inst->qpu.sig.ldvary ||
117 inst->qpu.sig.wrtmuc ||
118 inst->qpu.sig.thrsw) {
119 return true;
120 }
121
122 return false;
123 }
124
125 bool
126 vir_is_float_input(struct qinst *inst)
127 {
128 /* XXX: More instrs */
129 switch (inst->qpu.type) {
130 case V3D_QPU_INSTR_TYPE_BRANCH:
131 return false;
132 case V3D_QPU_INSTR_TYPE_ALU:
133 switch (inst->qpu.alu.add.op) {
134 case V3D_QPU_A_FADD:
135 case V3D_QPU_A_FSUB:
136 case V3D_QPU_A_FMIN:
137 case V3D_QPU_A_FMAX:
138 case V3D_QPU_A_FTOIN:
139 return true;
140 default:
141 break;
142 }
143
144 switch (inst->qpu.alu.mul.op) {
145 case V3D_QPU_M_FMOV:
146 case V3D_QPU_M_VFMUL:
147 case V3D_QPU_M_FMUL:
148 return true;
149 default:
150 break;
151 }
152 }
153
154 return false;
155 }
156
157 bool
158 vir_is_raw_mov(struct qinst *inst)
159 {
160 if (inst->qpu.type != V3D_QPU_INSTR_TYPE_ALU ||
161 (inst->qpu.alu.mul.op != V3D_QPU_M_FMOV &&
162 inst->qpu.alu.mul.op != V3D_QPU_M_MOV)) {
163 return false;
164 }
165
166 if (inst->qpu.alu.add.output_pack != V3D_QPU_PACK_NONE ||
167 inst->qpu.alu.mul.output_pack != V3D_QPU_PACK_NONE) {
168 return false;
169 }
170
171 if (inst->qpu.flags.ac != V3D_QPU_COND_NONE ||
172 inst->qpu.flags.mc != V3D_QPU_COND_NONE)
173 return false;
174
175 return true;
176 }
177
178 bool
179 vir_is_add(struct qinst *inst)
180 {
181 return (inst->qpu.type == V3D_QPU_INSTR_TYPE_ALU &&
182 inst->qpu.alu.add.op != V3D_QPU_A_NOP);
183 }
184
185 bool
186 vir_is_mul(struct qinst *inst)
187 {
188 return (inst->qpu.type == V3D_QPU_INSTR_TYPE_ALU &&
189 inst->qpu.alu.mul.op != V3D_QPU_M_NOP);
190 }
191
192 bool
193 vir_is_tex(struct qinst *inst)
194 {
195 if (inst->dst.file == QFILE_MAGIC)
196 return v3d_qpu_magic_waddr_is_tmu(inst->dst.index);
197
198 if (inst->qpu.type == V3D_QPU_INSTR_TYPE_ALU &&
199 inst->qpu.alu.add.op == V3D_QPU_A_TMUWT) {
200 return true;
201 }
202
203 return false;
204 }
205
206 bool
207 vir_writes_r3(const struct v3d_device_info *devinfo, struct qinst *inst)
208 {
209 for (int i = 0; i < vir_get_nsrc(inst); i++) {
210 switch (inst->src[i].file) {
211 case QFILE_VPM:
212 return true;
213 default:
214 break;
215 }
216 }
217
218 if (devinfo->ver < 41 && (inst->qpu.sig.ldvary ||
219 inst->qpu.sig.ldtlb ||
220 inst->qpu.sig.ldtlbu ||
221 inst->qpu.sig.ldvpm)) {
222 return true;
223 }
224
225 return false;
226 }
227
228 bool
229 vir_writes_r4(const struct v3d_device_info *devinfo, struct qinst *inst)
230 {
231 switch (inst->dst.file) {
232 case QFILE_MAGIC:
233 switch (inst->dst.index) {
234 case V3D_QPU_WADDR_RECIP:
235 case V3D_QPU_WADDR_RSQRT:
236 case V3D_QPU_WADDR_EXP:
237 case V3D_QPU_WADDR_LOG:
238 case V3D_QPU_WADDR_SIN:
239 return true;
240 }
241 break;
242 default:
243 break;
244 }
245
246 if (devinfo->ver < 41 && inst->qpu.sig.ldtmu)
247 return true;
248
249 return false;
250 }
251
252 void
253 vir_set_unpack(struct qinst *inst, int src,
254 enum v3d_qpu_input_unpack unpack)
255 {
256 assert(src == 0 || src == 1);
257
258 if (vir_is_add(inst)) {
259 if (src == 0)
260 inst->qpu.alu.add.a_unpack = unpack;
261 else
262 inst->qpu.alu.add.b_unpack = unpack;
263 } else {
264 assert(vir_is_mul(inst));
265 if (src == 0)
266 inst->qpu.alu.mul.a_unpack = unpack;
267 else
268 inst->qpu.alu.mul.b_unpack = unpack;
269 }
270 }
271
272 void
273 vir_set_cond(struct qinst *inst, enum v3d_qpu_cond cond)
274 {
275 if (vir_is_add(inst)) {
276 inst->qpu.flags.ac = cond;
277 } else {
278 assert(vir_is_mul(inst));
279 inst->qpu.flags.mc = cond;
280 }
281 }
282
283 void
284 vir_set_pf(struct qinst *inst, enum v3d_qpu_pf pf)
285 {
286 if (vir_is_add(inst)) {
287 inst->qpu.flags.apf = pf;
288 } else {
289 assert(vir_is_mul(inst));
290 inst->qpu.flags.mpf = pf;
291 }
292 }
293
294 void
295 vir_set_uf(struct qinst *inst, enum v3d_qpu_uf uf)
296 {
297 if (vir_is_add(inst)) {
298 inst->qpu.flags.auf = uf;
299 } else {
300 assert(vir_is_mul(inst));
301 inst->qpu.flags.muf = uf;
302 }
303 }
304
305 #if 0
306 uint8_t
307 vir_channels_written(struct qinst *inst)
308 {
309 if (vir_is_mul(inst)) {
310 switch (inst->dst.pack) {
311 case QPU_PACK_MUL_NOP:
312 case QPU_PACK_MUL_8888:
313 return 0xf;
314 case QPU_PACK_MUL_8A:
315 return 0x1;
316 case QPU_PACK_MUL_8B:
317 return 0x2;
318 case QPU_PACK_MUL_8C:
319 return 0x4;
320 case QPU_PACK_MUL_8D:
321 return 0x8;
322 }
323 } else {
324 switch (inst->dst.pack) {
325 case QPU_PACK_A_NOP:
326 case QPU_PACK_A_8888:
327 case QPU_PACK_A_8888_SAT:
328 case QPU_PACK_A_32_SAT:
329 return 0xf;
330 case QPU_PACK_A_8A:
331 case QPU_PACK_A_8A_SAT:
332 return 0x1;
333 case QPU_PACK_A_8B:
334 case QPU_PACK_A_8B_SAT:
335 return 0x2;
336 case QPU_PACK_A_8C:
337 case QPU_PACK_A_8C_SAT:
338 return 0x4;
339 case QPU_PACK_A_8D:
340 case QPU_PACK_A_8D_SAT:
341 return 0x8;
342 case QPU_PACK_A_16A:
343 case QPU_PACK_A_16A_SAT:
344 return 0x3;
345 case QPU_PACK_A_16B:
346 case QPU_PACK_A_16B_SAT:
347 return 0xc;
348 }
349 }
350 unreachable("Bad pack field");
351 }
352 #endif
353
354 struct qreg
355 vir_get_temp(struct v3d_compile *c)
356 {
357 struct qreg reg;
358
359 reg.file = QFILE_TEMP;
360 reg.index = c->num_temps++;
361
362 if (c->num_temps > c->defs_array_size) {
363 uint32_t old_size = c->defs_array_size;
364 c->defs_array_size = MAX2(old_size * 2, 16);
365
366 c->defs = reralloc(c, c->defs, struct qinst *,
367 c->defs_array_size);
368 memset(&c->defs[old_size], 0,
369 sizeof(c->defs[0]) * (c->defs_array_size - old_size));
370
371 c->spillable = reralloc(c, c->spillable,
372 BITSET_WORD,
373 BITSET_WORDS(c->defs_array_size));
374 for (int i = old_size; i < c->defs_array_size; i++)
375 BITSET_SET(c->spillable, i);
376 }
377
378 return reg;
379 }
380
381 struct qinst *
382 vir_add_inst(enum v3d_qpu_add_op op, struct qreg dst, struct qreg src0, struct qreg src1)
383 {
384 struct qinst *inst = calloc(1, sizeof(*inst));
385
386 inst->qpu = v3d_qpu_nop();
387 inst->qpu.alu.add.op = op;
388
389 inst->dst = dst;
390 inst->src[0] = src0;
391 inst->src[1] = src1;
392 inst->uniform = ~0;
393
394 return inst;
395 }
396
397 struct qinst *
398 vir_mul_inst(enum v3d_qpu_mul_op op, struct qreg dst, struct qreg src0, struct qreg src1)
399 {
400 struct qinst *inst = calloc(1, sizeof(*inst));
401
402 inst->qpu = v3d_qpu_nop();
403 inst->qpu.alu.mul.op = op;
404
405 inst->dst = dst;
406 inst->src[0] = src0;
407 inst->src[1] = src1;
408 inst->uniform = ~0;
409
410 return inst;
411 }
412
413 struct qinst *
414 vir_branch_inst(enum v3d_qpu_branch_cond cond, struct qreg src)
415 {
416 struct qinst *inst = calloc(1, sizeof(*inst));
417
418 inst->qpu = v3d_qpu_nop();
419 inst->qpu.type = V3D_QPU_INSTR_TYPE_BRANCH;
420 inst->qpu.branch.cond = cond;
421 inst->qpu.branch.msfign = V3D_QPU_MSFIGN_NONE;
422 inst->qpu.branch.bdi = V3D_QPU_BRANCH_DEST_REL;
423 inst->qpu.branch.ub = true;
424 inst->qpu.branch.bdu = V3D_QPU_BRANCH_DEST_REL;
425
426 inst->dst = vir_reg(QFILE_NULL, 0);
427 inst->src[0] = src;
428 inst->uniform = ~0;
429
430 return inst;
431 }
432
433 static void
434 vir_emit(struct v3d_compile *c, struct qinst *inst)
435 {
436 switch (c->cursor.mode) {
437 case vir_cursor_add:
438 list_add(&inst->link, c->cursor.link);
439 break;
440 case vir_cursor_addtail:
441 list_addtail(&inst->link, c->cursor.link);
442 break;
443 }
444
445 c->cursor = vir_after_inst(inst);
446 c->live_intervals_valid = false;
447 }
448
449 /* Updates inst to write to a new temporary, emits it, and notes the def. */
450 struct qreg
451 vir_emit_def(struct v3d_compile *c, struct qinst *inst)
452 {
453 assert(inst->dst.file == QFILE_NULL);
454
455 /* If we're emitting an instruction that's a def, it had better be
456 * writing a register.
457 */
458 if (inst->qpu.type == V3D_QPU_INSTR_TYPE_ALU) {
459 assert(inst->qpu.alu.add.op == V3D_QPU_A_NOP ||
460 v3d_qpu_add_op_has_dst(inst->qpu.alu.add.op));
461 assert(inst->qpu.alu.mul.op == V3D_QPU_M_NOP ||
462 v3d_qpu_mul_op_has_dst(inst->qpu.alu.mul.op));
463 }
464
465 inst->dst = vir_get_temp(c);
466
467 if (inst->dst.file == QFILE_TEMP)
468 c->defs[inst->dst.index] = inst;
469
470 vir_emit(c, inst);
471
472 return inst->dst;
473 }
474
475 struct qinst *
476 vir_emit_nondef(struct v3d_compile *c, struct qinst *inst)
477 {
478 if (inst->dst.file == QFILE_TEMP)
479 c->defs[inst->dst.index] = NULL;
480
481 vir_emit(c, inst);
482
483 return inst;
484 }
485
486 struct qblock *
487 vir_new_block(struct v3d_compile *c)
488 {
489 struct qblock *block = rzalloc(c, struct qblock);
490
491 list_inithead(&block->instructions);
492
493 block->predecessors = _mesa_set_create(block,
494 _mesa_hash_pointer,
495 _mesa_key_pointer_equal);
496
497 block->index = c->next_block_index++;
498
499 return block;
500 }
501
502 void
503 vir_set_emit_block(struct v3d_compile *c, struct qblock *block)
504 {
505 c->cur_block = block;
506 c->cursor = vir_after_block(block);
507 list_addtail(&block->link, &c->blocks);
508 }
509
510 struct qblock *
511 vir_entry_block(struct v3d_compile *c)
512 {
513 return list_first_entry(&c->blocks, struct qblock, link);
514 }
515
516 struct qblock *
517 vir_exit_block(struct v3d_compile *c)
518 {
519 return list_last_entry(&c->blocks, struct qblock, link);
520 }
521
522 void
523 vir_link_blocks(struct qblock *predecessor, struct qblock *successor)
524 {
525 _mesa_set_add(successor->predecessors, predecessor);
526 if (predecessor->successors[0]) {
527 assert(!predecessor->successors[1]);
528 predecessor->successors[1] = successor;
529 } else {
530 predecessor->successors[0] = successor;
531 }
532 }
533
534 const struct v3d_compiler *
535 v3d_compiler_init(const struct v3d_device_info *devinfo)
536 {
537 struct v3d_compiler *compiler = rzalloc(NULL, struct v3d_compiler);
538 if (!compiler)
539 return NULL;
540
541 compiler->devinfo = devinfo;
542
543 if (!vir_init_reg_sets(compiler)) {
544 ralloc_free(compiler);
545 return NULL;
546 }
547
548 return compiler;
549 }
550
551 void
552 v3d_compiler_free(const struct v3d_compiler *compiler)
553 {
554 ralloc_free((void *)compiler);
555 }
556
557 static struct v3d_compile *
558 vir_compile_init(const struct v3d_compiler *compiler,
559 struct v3d_key *key,
560 nir_shader *s,
561 void (*debug_output)(const char *msg,
562 void *debug_output_data),
563 void *debug_output_data,
564 int program_id, int variant_id)
565 {
566 struct v3d_compile *c = rzalloc(NULL, struct v3d_compile);
567
568 c->compiler = compiler;
569 c->devinfo = compiler->devinfo;
570 c->key = key;
571 c->program_id = program_id;
572 c->variant_id = variant_id;
573 c->threads = 4;
574 c->debug_output = debug_output;
575 c->debug_output_data = debug_output_data;
576
577 s = nir_shader_clone(c, s);
578 c->s = s;
579
580 list_inithead(&c->blocks);
581 vir_set_emit_block(c, vir_new_block(c));
582
583 c->output_position_index = -1;
584 c->output_point_size_index = -1;
585 c->output_sample_mask_index = -1;
586
587 c->def_ht = _mesa_hash_table_create(c, _mesa_hash_pointer,
588 _mesa_key_pointer_equal);
589
590 return c;
591 }
592
593 static int
594 type_size_vec4(const struct glsl_type *type)
595 {
596 return glsl_count_attribute_slots(type, false);
597 }
598
599 static void
600 v3d_lower_nir(struct v3d_compile *c)
601 {
602 struct nir_lower_tex_options tex_options = {
603 .lower_txd = true,
604 .lower_rect = false, /* XXX: Use this on V3D 3.x */
605 .lower_txp = ~0,
606 /* Apply swizzles to all samplers. */
607 .swizzle_result = ~0,
608 };
609
610 /* Lower the format swizzle and (for 32-bit returns)
611 * ARB_texture_swizzle-style swizzle.
612 */
613 for (int i = 0; i < ARRAY_SIZE(c->key->tex); i++) {
614 for (int j = 0; j < 4; j++)
615 tex_options.swizzles[i][j] = c->key->tex[i].swizzle[j];
616
617 if (c->key->tex[i].clamp_s)
618 tex_options.saturate_s |= 1 << i;
619 if (c->key->tex[i].clamp_t)
620 tex_options.saturate_t |= 1 << i;
621 if (c->key->tex[i].clamp_r)
622 tex_options.saturate_r |= 1 << i;
623 }
624
625 NIR_PASS_V(c->s, nir_lower_tex, &tex_options);
626 }
627
628 static void
629 v3d_set_prog_data_uniforms(struct v3d_compile *c,
630 struct v3d_prog_data *prog_data)
631 {
632 int count = c->num_uniforms;
633 struct v3d_uniform_list *ulist = &prog_data->uniforms;
634
635 ulist->count = count;
636 ulist->data = ralloc_array(prog_data, uint32_t, count);
637 memcpy(ulist->data, c->uniform_data,
638 count * sizeof(*ulist->data));
639 ulist->contents = ralloc_array(prog_data, enum quniform_contents, count);
640 memcpy(ulist->contents, c->uniform_contents,
641 count * sizeof(*ulist->contents));
642 }
643
644 /* Copy the compiler UBO range state to the compiled shader, dropping out
645 * arrays that were never referenced by an indirect load.
646 *
647 * (Note that QIR dead code elimination of an array access still leaves that
648 * array alive, though)
649 */
650 static void
651 v3d_set_prog_data_ubo(struct v3d_compile *c,
652 struct v3d_prog_data *prog_data)
653 {
654 if (!c->num_ubo_ranges)
655 return;
656
657 prog_data->num_ubo_ranges = 0;
658 prog_data->ubo_ranges = ralloc_array(prog_data, struct v3d_ubo_range,
659 c->num_ubo_ranges);
660 for (int i = 0; i < c->num_ubo_ranges; i++) {
661 if (!c->ubo_range_used[i])
662 continue;
663
664 struct v3d_ubo_range *range = &c->ubo_ranges[i];
665 prog_data->ubo_ranges[prog_data->num_ubo_ranges++] = *range;
666 prog_data->ubo_size += range->size;
667 }
668
669 if (prog_data->ubo_size) {
670 if (V3D_DEBUG & V3D_DEBUG_SHADERDB) {
671 fprintf(stderr, "SHADER-DB: %s prog %d/%d: %d UBO uniforms\n",
672 vir_get_stage_name(c),
673 c->program_id, c->variant_id,
674 prog_data->ubo_size / 4);
675 }
676 }
677 }
678
679 static void
680 v3d_vs_set_prog_data(struct v3d_compile *c,
681 struct v3d_vs_prog_data *prog_data)
682 {
683 prog_data->base.num_inputs = c->num_inputs;
684
685 /* The vertex data gets format converted by the VPM so that
686 * each attribute channel takes up a VPM column. Precompute
687 * the sizes for the shader record.
688 */
689 for (int i = 0; i < ARRAY_SIZE(prog_data->vattr_sizes); i++) {
690 prog_data->vattr_sizes[i] = c->vattr_sizes[i];
691 prog_data->vpm_input_size += c->vattr_sizes[i];
692 }
693
694 prog_data->uses_vid = (c->s->info.system_values_read &
695 (1ull << SYSTEM_VALUE_VERTEX_ID));
696 prog_data->uses_iid = (c->s->info.system_values_read &
697 (1ull << SYSTEM_VALUE_INSTANCE_ID));
698
699 if (prog_data->uses_vid)
700 prog_data->vpm_input_size++;
701 if (prog_data->uses_iid)
702 prog_data->vpm_input_size++;
703
704 /* Input/output segment size are in sectors (8 rows of 32 bits per
705 * channel).
706 */
707 prog_data->vpm_input_size = align(prog_data->vpm_input_size, 8) / 8;
708 prog_data->vpm_output_size = align(c->num_vpm_writes, 8) / 8;
709
710 /* Set us up for shared input/output segments. This is apparently
711 * necessary for our VCM setup to avoid varying corruption.
712 */
713 prog_data->separate_segments = false;
714 prog_data->vpm_output_size = MAX2(prog_data->vpm_output_size,
715 prog_data->vpm_input_size);
716 prog_data->vpm_input_size = 0;
717
718 /* Compute VCM cache size. We set up our program to take up less than
719 * half of the VPM, so that any set of bin and render programs won't
720 * run out of space. We need space for at least one input segment,
721 * and then allocate the rest to output segments (one for the current
722 * program, the rest to VCM). The valid range of the VCM cache size
723 * field is 1-4 16-vertex batches, but GFXH-1744 limits us to 2-4
724 * batches.
725 */
726 assert(c->devinfo->vpm_size);
727 int sector_size = 16 * sizeof(uint32_t) * 8;
728 int vpm_size_in_sectors = c->devinfo->vpm_size / sector_size;
729 int half_vpm = vpm_size_in_sectors / 2;
730 int vpm_output_sectors = half_vpm - prog_data->vpm_input_size;
731 int vpm_output_batches = vpm_output_sectors / prog_data->vpm_output_size;
732 assert(vpm_output_batches >= 2);
733 prog_data->vcm_cache_size = CLAMP(vpm_output_batches - 1, 2, 4);
734 }
735
736 static void
737 v3d_set_fs_prog_data_inputs(struct v3d_compile *c,
738 struct v3d_fs_prog_data *prog_data)
739 {
740 prog_data->base.num_inputs = c->num_inputs;
741 memcpy(prog_data->input_slots, c->input_slots,
742 c->num_inputs * sizeof(*c->input_slots));
743
744 STATIC_ASSERT(ARRAY_SIZE(prog_data->flat_shade_flags) >
745 (V3D_MAX_FS_INPUTS - 1) / 24);
746 for (int i = 0; i < V3D_MAX_FS_INPUTS; i++) {
747 if (BITSET_TEST(c->flat_shade_flags, i))
748 prog_data->flat_shade_flags[i / 24] |= 1 << (i % 24);
749
750 if (BITSET_TEST(c->noperspective_flags, i))
751 prog_data->noperspective_flags[i / 24] |= 1 << (i % 24);
752
753 if (BITSET_TEST(c->centroid_flags, i))
754 prog_data->centroid_flags[i / 24] |= 1 << (i % 24);
755 }
756 }
757
758 static void
759 v3d_fs_set_prog_data(struct v3d_compile *c,
760 struct v3d_fs_prog_data *prog_data)
761 {
762 v3d_set_fs_prog_data_inputs(c, prog_data);
763 prog_data->writes_z = (c->s->info.outputs_written &
764 (1 << FRAG_RESULT_DEPTH));
765 prog_data->discard = (c->s->info.fs.uses_discard ||
766 c->fs_key->sample_alpha_to_coverage);
767 prog_data->uses_center_w = c->uses_center_w;
768 }
769
770 static void
771 v3d_set_prog_data(struct v3d_compile *c,
772 struct v3d_prog_data *prog_data)
773 {
774 prog_data->threads = c->threads;
775 prog_data->single_seg = !c->last_thrsw;
776 prog_data->spill_size = c->spill_size;
777
778 v3d_set_prog_data_uniforms(c, prog_data);
779 v3d_set_prog_data_ubo(c, prog_data);
780
781 if (c->s->info.stage == MESA_SHADER_VERTEX) {
782 v3d_vs_set_prog_data(c, (struct v3d_vs_prog_data *)prog_data);
783 } else {
784 assert(c->s->info.stage == MESA_SHADER_FRAGMENT);
785 v3d_fs_set_prog_data(c, (struct v3d_fs_prog_data *)prog_data);
786 }
787 }
788
789 static uint64_t *
790 v3d_return_qpu_insts(struct v3d_compile *c, uint32_t *final_assembly_size)
791 {
792 *final_assembly_size = c->qpu_inst_count * sizeof(uint64_t);
793
794 uint64_t *qpu_insts = malloc(*final_assembly_size);
795 if (!qpu_insts)
796 return NULL;
797
798 memcpy(qpu_insts, c->qpu_insts, *final_assembly_size);
799
800 vir_compile_destroy(c);
801
802 return qpu_insts;
803 }
804
805 static void
806 v3d_nir_lower_vs_early(struct v3d_compile *c)
807 {
808 /* Split our I/O vars and dead code eliminate the unused
809 * components.
810 */
811 NIR_PASS_V(c->s, nir_lower_io_to_scalar_early,
812 nir_var_shader_in | nir_var_shader_out);
813 uint64_t used_outputs[4] = {0};
814 for (int i = 0; i < c->vs_key->num_fs_inputs; i++) {
815 int slot = v3d_slot_get_slot(c->vs_key->fs_inputs[i]);
816 int comp = v3d_slot_get_component(c->vs_key->fs_inputs[i]);
817 used_outputs[comp] |= 1ull << slot;
818 }
819 NIR_PASS_V(c->s, nir_remove_unused_io_vars,
820 &c->s->outputs, used_outputs, NULL); /* demotes to globals */
821 NIR_PASS_V(c->s, nir_lower_global_vars_to_local);
822 v3d_optimize_nir(c->s);
823 NIR_PASS_V(c->s, nir_remove_dead_variables, nir_var_shader_in);
824 NIR_PASS_V(c->s, nir_lower_io, nir_var_shader_in | nir_var_shader_out,
825 type_size_vec4,
826 (nir_lower_io_options)0);
827 }
828
829 static void
830 v3d_fixup_fs_output_types(struct v3d_compile *c)
831 {
832 nir_foreach_variable(var, &c->s->outputs) {
833 uint32_t mask = 0;
834
835 switch (var->data.location) {
836 case FRAG_RESULT_COLOR:
837 mask = ~0;
838 break;
839 case FRAG_RESULT_DATA0:
840 case FRAG_RESULT_DATA1:
841 case FRAG_RESULT_DATA2:
842 case FRAG_RESULT_DATA3:
843 mask = 1 << (var->data.location - FRAG_RESULT_DATA0);
844 break;
845 }
846
847 if (c->fs_key->int_color_rb & mask) {
848 var->type =
849 glsl_vector_type(GLSL_TYPE_INT,
850 glsl_get_components(var->type));
851 } else if (c->fs_key->uint_color_rb & mask) {
852 var->type =
853 glsl_vector_type(GLSL_TYPE_UINT,
854 glsl_get_components(var->type));
855 }
856 }
857 }
858
859 static void
860 v3d_nir_lower_fs_early(struct v3d_compile *c)
861 {
862 if (c->fs_key->int_color_rb || c->fs_key->uint_color_rb)
863 v3d_fixup_fs_output_types(c);
864 }
865
866 static void
867 v3d_nir_lower_vs_late(struct v3d_compile *c)
868 {
869 if (c->vs_key->clamp_color)
870 NIR_PASS_V(c->s, nir_lower_clamp_color_outputs);
871
872 if (c->key->ucp_enables) {
873 NIR_PASS_V(c->s, nir_lower_clip_vs, c->key->ucp_enables,
874 false);
875 NIR_PASS_V(c->s, nir_lower_io_to_scalar,
876 nir_var_shader_out);
877 }
878
879 /* Note: VS output scalarizing must happen after nir_lower_clip_vs. */
880 NIR_PASS_V(c->s, nir_lower_io_to_scalar, nir_var_shader_out);
881 }
882
883 static void
884 v3d_nir_lower_fs_late(struct v3d_compile *c)
885 {
886 if (c->fs_key->light_twoside)
887 NIR_PASS_V(c->s, nir_lower_two_sided_color);
888
889 if (c->fs_key->clamp_color)
890 NIR_PASS_V(c->s, nir_lower_clamp_color_outputs);
891
892 if (c->fs_key->alpha_test) {
893 NIR_PASS_V(c->s, nir_lower_alpha_test,
894 c->fs_key->alpha_test_func,
895 false);
896 }
897
898 if (c->key->ucp_enables)
899 NIR_PASS_V(c->s, nir_lower_clip_fs, c->key->ucp_enables);
900
901 /* Note: FS input scalarizing must happen after
902 * nir_lower_two_sided_color, which only handles a vec4 at a time.
903 */
904 NIR_PASS_V(c->s, nir_lower_io_to_scalar, nir_var_shader_in);
905 }
906
907 uint64_t *v3d_compile(const struct v3d_compiler *compiler,
908 struct v3d_key *key,
909 struct v3d_prog_data **out_prog_data,
910 nir_shader *s,
911 void (*debug_output)(const char *msg,
912 void *debug_output_data),
913 void *debug_output_data,
914 int program_id, int variant_id,
915 uint32_t *final_assembly_size)
916 {
917 struct v3d_prog_data *prog_data;
918 struct v3d_compile *c = vir_compile_init(compiler, key, s,
919 debug_output, debug_output_data,
920 program_id, variant_id);
921
922 switch (c->s->info.stage) {
923 case MESA_SHADER_VERTEX:
924 c->vs_key = (struct v3d_vs_key *)key;
925 prog_data = rzalloc_size(NULL, sizeof(struct v3d_vs_prog_data));
926 break;
927 case MESA_SHADER_FRAGMENT:
928 c->fs_key = (struct v3d_fs_key *)key;
929 prog_data = rzalloc_size(NULL, sizeof(struct v3d_fs_prog_data));
930 break;
931 default:
932 unreachable("unsupported shader stage");
933 }
934
935 if (c->s->info.stage == MESA_SHADER_VERTEX) {
936 v3d_nir_lower_vs_early(c);
937 } else {
938 assert(c->s->info.stage == MESA_SHADER_FRAGMENT);
939 v3d_nir_lower_fs_early(c);
940 }
941
942 v3d_lower_nir(c);
943
944 if (c->s->info.stage == MESA_SHADER_VERTEX) {
945 v3d_nir_lower_vs_late(c);
946 } else {
947 assert(c->s->info.stage == MESA_SHADER_FRAGMENT);
948 v3d_nir_lower_fs_late(c);
949 }
950
951 NIR_PASS_V(c->s, v3d_nir_lower_io, c);
952 NIR_PASS_V(c->s, v3d_nir_lower_txf_ms, c);
953 NIR_PASS_V(c->s, nir_lower_idiv);
954
955 v3d_optimize_nir(c->s);
956 NIR_PASS_V(c->s, nir_lower_bool_to_int32);
957 NIR_PASS_V(c->s, nir_convert_from_ssa, true);
958
959 v3d_nir_to_vir(c);
960
961 v3d_set_prog_data(c, prog_data);
962
963 *out_prog_data = prog_data;
964 return v3d_return_qpu_insts(c, final_assembly_size);
965 }
966
967 void
968 vir_remove_instruction(struct v3d_compile *c, struct qinst *qinst)
969 {
970 if (qinst->dst.file == QFILE_TEMP)
971 c->defs[qinst->dst.index] = NULL;
972
973 assert(&qinst->link != c->cursor.link);
974
975 list_del(&qinst->link);
976 free(qinst);
977
978 c->live_intervals_valid = false;
979 }
980
981 struct qreg
982 vir_follow_movs(struct v3d_compile *c, struct qreg reg)
983 {
984 /* XXX
985 int pack = reg.pack;
986
987 while (reg.file == QFILE_TEMP &&
988 c->defs[reg.index] &&
989 (c->defs[reg.index]->op == QOP_MOV ||
990 c->defs[reg.index]->op == QOP_FMOV) &&
991 !c->defs[reg.index]->dst.pack &&
992 !c->defs[reg.index]->src[0].pack) {
993 reg = c->defs[reg.index]->src[0];
994 }
995
996 reg.pack = pack;
997 */
998 return reg;
999 }
1000
1001 void
1002 vir_compile_destroy(struct v3d_compile *c)
1003 {
1004 /* Defuse the assert that we aren't removing the cursor's instruction.
1005 */
1006 c->cursor.link = NULL;
1007
1008 vir_for_each_block(block, c) {
1009 while (!list_empty(&block->instructions)) {
1010 struct qinst *qinst =
1011 list_first_entry(&block->instructions,
1012 struct qinst, link);
1013 vir_remove_instruction(c, qinst);
1014 }
1015 }
1016
1017 ralloc_free(c);
1018 }
1019
1020 struct qreg
1021 vir_uniform(struct v3d_compile *c,
1022 enum quniform_contents contents,
1023 uint32_t data)
1024 {
1025 for (int i = 0; i < c->num_uniforms; i++) {
1026 if (c->uniform_contents[i] == contents &&
1027 c->uniform_data[i] == data) {
1028 return vir_reg(QFILE_UNIF, i);
1029 }
1030 }
1031
1032 uint32_t uniform = c->num_uniforms++;
1033
1034 if (uniform >= c->uniform_array_size) {
1035 c->uniform_array_size = MAX2(MAX2(16, uniform + 1),
1036 c->uniform_array_size * 2);
1037
1038 c->uniform_data = reralloc(c, c->uniform_data,
1039 uint32_t,
1040 c->uniform_array_size);
1041 c->uniform_contents = reralloc(c, c->uniform_contents,
1042 enum quniform_contents,
1043 c->uniform_array_size);
1044 }
1045
1046 c->uniform_contents[uniform] = contents;
1047 c->uniform_data[uniform] = data;
1048
1049 return vir_reg(QFILE_UNIF, uniform);
1050 }
1051
1052 static bool
1053 vir_can_set_flags(struct v3d_compile *c, struct qinst *inst)
1054 {
1055 if (c->devinfo->ver >= 40 && (v3d_qpu_reads_vpm(&inst->qpu) ||
1056 v3d_qpu_uses_sfu(&inst->qpu))) {
1057 return false;
1058 }
1059
1060 if (inst->qpu.type != V3D_QPU_INSTR_TYPE_ALU ||
1061 (inst->qpu.alu.add.op == V3D_QPU_A_NOP &&
1062 inst->qpu.alu.mul.op == V3D_QPU_M_NOP)) {
1063 return false;
1064 }
1065
1066 return true;
1067 }
1068
1069 void
1070 vir_PF(struct v3d_compile *c, struct qreg src, enum v3d_qpu_pf pf)
1071 {
1072 struct qinst *last_inst = NULL;
1073
1074 if (!list_empty(&c->cur_block->instructions)) {
1075 last_inst = (struct qinst *)c->cur_block->instructions.prev;
1076
1077 /* Can't stuff the PF into the last last inst if our cursor
1078 * isn't pointing after it.
1079 */
1080 struct vir_cursor after_inst = vir_after_inst(last_inst);
1081 if (c->cursor.mode != after_inst.mode ||
1082 c->cursor.link != after_inst.link)
1083 last_inst = NULL;
1084 }
1085
1086 if (src.file != QFILE_TEMP ||
1087 !c->defs[src.index] ||
1088 last_inst != c->defs[src.index] ||
1089 !vir_can_set_flags(c, last_inst)) {
1090 /* XXX: Make the MOV be the appropriate type */
1091 last_inst = vir_MOV_dest(c, vir_reg(QFILE_NULL, 0), src);
1092 }
1093
1094 vir_set_pf(last_inst, pf);
1095 }
1096
1097 #define OPTPASS(func) \
1098 do { \
1099 bool stage_progress = func(c); \
1100 if (stage_progress) { \
1101 progress = true; \
1102 if (print_opt_debug) { \
1103 fprintf(stderr, \
1104 "VIR opt pass %2d: %s progress\n", \
1105 pass, #func); \
1106 } \
1107 /*XXX vir_validate(c);*/ \
1108 } \
1109 } while (0)
1110
1111 void
1112 vir_optimize(struct v3d_compile *c)
1113 {
1114 bool print_opt_debug = false;
1115 int pass = 1;
1116
1117 while (true) {
1118 bool progress = false;
1119
1120 OPTPASS(vir_opt_copy_propagate);
1121 OPTPASS(vir_opt_dead_code);
1122 OPTPASS(vir_opt_small_immediates);
1123
1124 if (!progress)
1125 break;
1126
1127 pass++;
1128 }
1129 }
1130
1131 const char *
1132 vir_get_stage_name(struct v3d_compile *c)
1133 {
1134 if (c->vs_key && c->vs_key->is_coord)
1135 return "MESA_SHADER_COORD";
1136 else
1137 return gl_shader_stage_name(c->s->info.stage);
1138 }