blob: 790cfaf75a6516745c81ec9af695ec8fd39391fb [file] [log] [blame]
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001/*
Alyssa Rosenzweig11554462019-05-19 23:20:34 +00002 * Copyright (C) 2018-2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00003 *
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <sys/mman.h>
27#include <fcntl.h>
28#include <stdint.h>
29#include <stdlib.h>
30#include <stdio.h>
31#include <err.h>
32
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +010033#include "main/mtypes.h"
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +000034#include "compiler/glsl/glsl_to_nir.h"
35#include "compiler/nir_types.h"
36#include "main/imports.h"
37#include "compiler/nir/nir_builder.h"
38#include "util/half_float.h"
Alyssa Rosenzweig213b6282019-06-18 09:02:20 -070039#include "util/u_math.h"
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +010040#include "util/u_debug.h"
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +000041#include "util/u_dynarray.h"
42#include "util/list.h"
43#include "main/mtypes.h"
44
45#include "midgard.h"
46#include "midgard_nir.h"
47#include "midgard_compile.h"
Alyssa Rosenzweig11554462019-05-19 23:20:34 +000048#include "midgard_ops.h"
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +000049#include "helpers.h"
Alyssa Rosenzweig11554462019-05-19 23:20:34 +000050#include "compiler.h"
Alyssa Rosenzweigfcf144d2019-11-19 20:55:42 -050051#include "midgard_quirks.h"
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +000052
53#include "disassemble.h"
54
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +010055static const struct debug_named_value debug_options[] = {
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -070056 {"msgs", MIDGARD_DBG_MSGS, "Print debug messages"},
57 {"shaders", MIDGARD_DBG_SHADERS, "Dump shaders in NIR and MIR"},
Alyssa Rosenzweig138e40d2019-07-08 16:42:29 -070058 {"shaderdb", MIDGARD_DBG_SHADERDB, "Prints shader-db statistics"},
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -070059 DEBUG_NAMED_VALUE_END
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +010060};
61
62DEBUG_GET_ONCE_FLAGS_OPTION(midgard_debug, "MIDGARD_MESA_DEBUG", debug_options, 0)
63
Alyssa Rosenzweig138e40d2019-07-08 16:42:29 -070064unsigned SHADER_DB_COUNT = 0;
65
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +010066int midgard_debug = 0;
67
68#define DBG(fmt, ...) \
69 do { if (midgard_debug & MIDGARD_DBG_MSGS) \
70 fprintf(stderr, "%s:%d: "fmt, \
71 __FUNCTION__, __LINE__, ##__VA_ARGS__); } while (0)
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -070072static midgard_block *
73create_empty_block(compiler_context *ctx)
74{
75 midgard_block *blk = rzalloc(ctx, midgard_block);
76
77 blk->predecessors = _mesa_set_create(blk,
78 _mesa_hash_pointer,
79 _mesa_key_pointer_equal);
80
81 blk->source_id = ctx->block_source_count++;
82
83 return blk;
84}
85
Alyssa Rosenzweigc0fb2602019-04-21 03:29:47 +000086static void
87midgard_block_add_successor(midgard_block *block, midgard_block *successor)
88{
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -070089 assert(block);
90 assert(successor);
91
92 /* Deduplicate */
93 for (unsigned i = 0; i < block->nr_successors; ++i) {
94 if (block->successors[i] == successor)
95 return;
96 }
97
Alyssa Rosenzweigc0fb2602019-04-21 03:29:47 +000098 block->successors[block->nr_successors++] = successor;
99 assert(block->nr_successors <= ARRAY_SIZE(block->successors));
Alyssa Rosenzweigaeeeef12019-08-15 08:11:10 -0700100
101 /* Note the predecessor in the other direction */
102 _mesa_set_add(successor->predecessors, block);
Alyssa Rosenzweigc0fb2602019-04-21 03:29:47 +0000103}
104
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -0700105static void
106schedule_barrier(compiler_context *ctx)
107{
108 midgard_block *temp = ctx->after_block;
109 ctx->after_block = create_empty_block(ctx);
110 ctx->block_count++;
111 list_addtail(&ctx->after_block->link, &ctx->blocks);
112 list_inithead(&ctx->after_block->instructions);
113 midgard_block_add_successor(ctx->current_block, ctx->after_block);
114 ctx->current_block = ctx->after_block;
115 ctx->after_block = temp;
116}
117
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000118/* Helpers to generate midgard_instruction's using macro magic, since every
119 * driver seems to do it that way */
120
121#define EMIT(op, ...) emit_mir_instruction(ctx, v_##op(__VA_ARGS__));
Alyssa Rosenzweig56f9b472019-06-14 16:03:01 -0700122
Alyssa Rosenzweigd4bcca12019-08-02 15:25:02 -0700123#define M_LOAD_STORE(name, store) \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000124 static midgard_instruction m_##name(unsigned ssa, unsigned address) { \
125 midgard_instruction i = { \
126 .type = TAG_LOAD_STORE_4, \
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -0700127 .mask = 0xF, \
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -0700128 .dest = ~0, \
Alyssa Rosenzweigccbc9a42019-12-19 10:35:18 -0500129 .src = { ~0, ~0, ~0, ~0 }, \
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -0400130 .swizzle = SWIZZLE_IDENTITY_4, \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000131 .load_store = { \
132 .op = midgard_op_##name, \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000133 .address = address \
134 } \
135 }; \
Alyssa Rosenzweigd4bcca12019-08-02 15:25:02 -0700136 \
137 if (store) \
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -0700138 i.src[0] = ssa; \
Alyssa Rosenzweigd4bcca12019-08-02 15:25:02 -0700139 else \
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -0700140 i.dest = ssa; \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000141 \
142 return i; \
143 }
144
Alyssa Rosenzweigd4bcca12019-08-02 15:25:02 -0700145#define M_LOAD(name) M_LOAD_STORE(name, false)
146#define M_STORE(name) M_LOAD_STORE(name, true)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000147
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000148/* Inputs a NIR ALU source, with modifiers attached if necessary, and outputs
149 * the corresponding Midgard source */
150
151static midgard_vector_alu_src
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700152vector_alu_modifiers(nir_alu_src *src, bool is_int, unsigned broadcast_count,
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -0700153 bool half, bool sext)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000154{
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -0400155 /* Figure out how many components there are so we can adjust.
156 * Specifically we want to broadcast the last channel so things like
157 * ball2/3 work.
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700158 */
159
Alyssa Rosenzweigc3a46e72019-10-30 16:29:28 -0400160 if (broadcast_count && src) {
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700161 uint8_t last_component = src->swizzle[broadcast_count - 1];
162
163 for (unsigned c = broadcast_count; c < NIR_MAX_VEC_COMPONENTS; ++c) {
164 src->swizzle[c] = last_component;
165 }
166 }
167
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000168 midgard_vector_alu_src alu_src = {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000169 .rep_low = 0,
170 .rep_high = 0,
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -0400171 .half = half
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000172 };
173
Alyssa Rosenzweigfcdfb672019-04-22 03:25:42 +0000174 if (is_int) {
Alyssa Rosenzweigfcdfb672019-04-22 03:25:42 +0000175 alu_src.mod = midgard_int_normal;
176
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700177 /* Sign/zero-extend if needed */
178
179 if (half) {
180 alu_src.mod = sext ?
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -0700181 midgard_int_sign_extend
182 : midgard_int_zero_extend;
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700183 }
184
Alyssa Rosenzweigfcdfb672019-04-22 03:25:42 +0000185 /* These should have been lowered away */
Alyssa Rosenzweigc3a46e72019-10-30 16:29:28 -0400186 if (src)
187 assert(!(src->abs || src->negate));
Alyssa Rosenzweigfcdfb672019-04-22 03:25:42 +0000188 } else {
Alyssa Rosenzweigc3a46e72019-10-30 16:29:28 -0400189 if (src)
190 alu_src.mod = (src->abs << 0) | (src->negate << 1);
Alyssa Rosenzweigfcdfb672019-04-22 03:25:42 +0000191 }
192
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000193 return alu_src;
194}
195
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000196/* load/store instructions have both 32-bit and 16-bit variants, depending on
197 * whether we are using vectors composed of highp or mediump. At the moment, we
198 * don't support half-floats -- this requires changes in other parts of the
199 * compiler -- therefore the 16-bit versions are commented out. */
200
Alyssa Rosenzweig74ab80b2019-05-14 04:11:36 +0000201//M_LOAD(ld_attr_16);
202M_LOAD(ld_attr_32);
203//M_LOAD(ld_vary_16);
204M_LOAD(ld_vary_32);
Alyssa Rosenzweigec2f0b52019-08-13 08:51:40 -0700205M_LOAD(ld_ubo_int4);
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -0700206M_LOAD(ld_int4);
207M_STORE(st_int4);
Alyssa Rosenzweig2d1e18e2020-01-02 12:28:54 -0500208M_LOAD(ld_color_buffer_32u);
Alyssa Rosenzweig74ab80b2019-05-14 04:11:36 +0000209//M_STORE(st_vary_16);
210M_STORE(st_vary_32);
Alyssa Rosenzweig9ae4d362019-08-16 07:50:12 -0700211M_LOAD(ld_cubemap_coords);
Alyssa Rosenzweig7229af72019-08-06 13:47:17 -0700212M_LOAD(ld_compute_id);
Alyssa Rosenzweig564a7822020-01-08 15:11:45 -0500213M_LOAD(pack_colour);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000214
215static midgard_instruction
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000216v_branch(bool conditional, bool invert)
217{
218 midgard_instruction ins = {
219 .type = TAG_ALU_4,
Alyssa Rosenzweig5abb7b52019-02-17 22:09:09 +0000220 .unit = ALU_ENAB_BRANCH,
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000221 .compact_branch = true,
222 .branch = {
223 .conditional = conditional,
224 .invert_conditional = invert
Alyssa Rosenzweig29416a82019-07-30 12:20:24 -0700225 },
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -0700226 .dest = ~0,
Alyssa Rosenzweigccbc9a42019-12-19 10:35:18 -0500227 .src = { ~0, ~0, ~0, ~0 },
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000228 };
229
230 return ins;
231}
232
Alyssa Rosenzweig5abb7b52019-02-17 22:09:09 +0000233static midgard_branch_extended
234midgard_create_branch_extended( midgard_condition cond,
235 midgard_jmp_writeout_op op,
236 unsigned dest_tag,
237 signed quadword_offset)
238{
Alyssa Rosenzweig13ee87c2019-07-29 09:15:32 -0700239 /* The condition code is actually a LUT describing a function to
240 * combine multiple condition codes. However, we only support a single
241 * condition code at the moment, so we just duplicate over a bunch of
242 * times. */
243
Alyssa Rosenzweig5abb7b52019-02-17 22:09:09 +0000244 uint16_t duplicated_cond =
245 (cond << 14) |
246 (cond << 12) |
247 (cond << 10) |
248 (cond << 8) |
249 (cond << 6) |
250 (cond << 4) |
251 (cond << 2) |
252 (cond << 0);
253
254 midgard_branch_extended branch = {
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +0000255 .op = op,
Alyssa Rosenzweig5abb7b52019-02-17 22:09:09 +0000256 .dest_tag = dest_tag,
257 .offset = quadword_offset,
258 .cond = duplicated_cond
259 };
260
261 return branch;
262}
263
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000264static void
265attach_constants(compiler_context *ctx, midgard_instruction *ins, void *constants, int name)
266{
267 ins->has_constants = true;
268 memcpy(&ins->constants, constants, 16);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000269}
270
271static int
Timothy Arceri035759b2019-03-29 12:39:48 +1100272glsl_type_size(const struct glsl_type *type, bool bindless)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000273{
274 return glsl_count_attribute_slots(type, false);
275}
276
277/* Lower fdot2 to a vector multiplication followed by channel addition */
278static void
279midgard_nir_lower_fdot2_body(nir_builder *b, nir_alu_instr *alu)
280{
281 if (alu->op != nir_op_fdot2)
282 return;
283
284 b->cursor = nir_before_instr(&alu->instr);
285
286 nir_ssa_def *src0 = nir_ssa_for_alu_src(b, alu, 0);
287 nir_ssa_def *src1 = nir_ssa_for_alu_src(b, alu, 1);
288
289 nir_ssa_def *product = nir_fmul(b, src0, src1);
290
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -0700291 nir_ssa_def *sum = nir_fadd(b,
292 nir_channel(b, product, 0),
293 nir_channel(b, product, 1));
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000294
295 /* Replace the fdot2 with this sum */
296 nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa, nir_src_for_ssa(sum));
297}
298
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +0000299static int
Alyssa Rosenzweig2efa0252019-08-01 11:03:15 -0700300midgard_sysval_for_ssbo(nir_intrinsic_instr *instr)
301{
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -0700302 /* This is way too meta */
303 bool is_store = instr->intrinsic == nir_intrinsic_store_ssbo;
304 unsigned idx_idx = is_store ? 1 : 0;
305
306 nir_src index = instr->src[idx_idx];
Alyssa Rosenzweig2efa0252019-08-01 11:03:15 -0700307 assert(nir_src_is_const(index));
308 uint32_t uindex = nir_src_as_uint(index);
309
310 return PAN_SYSVAL(SSBO, uindex);
311}
312
313static int
Alyssa Rosenzweig4e07e7b2019-11-21 08:42:28 -0500314midgard_sysval_for_sampler(nir_intrinsic_instr *instr)
315{
316 /* TODO: indirect samplers !!! */
317 nir_src index = instr->src[0];
318 assert(nir_src_is_const(index));
319 uint32_t uindex = nir_src_as_uint(index);
320
321 return PAN_SYSVAL(SAMPLER, uindex);
322}
323
324static int
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +0000325midgard_nir_sysval_for_intrinsic(nir_intrinsic_instr *instr)
326{
327 switch (instr->intrinsic) {
328 case nir_intrinsic_load_viewport_scale:
329 return PAN_SYSVAL_VIEWPORT_SCALE;
330 case nir_intrinsic_load_viewport_offset:
331 return PAN_SYSVAL_VIEWPORT_OFFSET;
Alyssa Rosenzweig15954ab2019-08-06 14:07:10 -0700332 case nir_intrinsic_load_num_work_groups:
333 return PAN_SYSVAL_NUM_WORK_GROUPS;
Alyssa Rosenzweig2efa0252019-08-01 11:03:15 -0700334 case nir_intrinsic_load_ssbo:
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -0700335 case nir_intrinsic_store_ssbo:
Alyssa Rosenzweig2efa0252019-08-01 11:03:15 -0700336 return midgard_sysval_for_ssbo(instr);
Alyssa Rosenzweig4e07e7b2019-11-21 08:42:28 -0500337 case nir_intrinsic_load_sampler_lod_parameters_pan:
338 return midgard_sysval_for_sampler(instr);
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +0000339 default:
Alyssa Rosenzweig61892742019-08-21 09:15:56 -0700340 return ~0;
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +0000341 }
342}
343
Boris Brezillonbd49c8f2019-06-14 09:59:20 +0200344static int sysval_for_instr(compiler_context *ctx, nir_instr *instr,
345 unsigned *dest)
346{
347 nir_intrinsic_instr *intr;
348 nir_dest *dst = NULL;
Boris Brezillonc3558862019-06-17 22:13:04 +0200349 nir_tex_instr *tex;
Boris Brezillonbd49c8f2019-06-14 09:59:20 +0200350 int sysval = -1;
351
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -0700352 bool is_store = false;
353
Boris Brezillonbd49c8f2019-06-14 09:59:20 +0200354 switch (instr->type) {
355 case nir_instr_type_intrinsic:
356 intr = nir_instr_as_intrinsic(instr);
357 sysval = midgard_nir_sysval_for_intrinsic(intr);
358 dst = &intr->dest;
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -0700359 is_store |= intr->intrinsic == nir_intrinsic_store_ssbo;
Boris Brezillonbd49c8f2019-06-14 09:59:20 +0200360 break;
Boris Brezillonc3558862019-06-17 22:13:04 +0200361 case nir_instr_type_tex:
362 tex = nir_instr_as_tex(instr);
363 if (tex->op != nir_texop_txs)
364 break;
365
366 sysval = PAN_SYSVAL(TEXTURE_SIZE,
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -0700367 PAN_TXS_SYSVAL_ID(tex->texture_index,
368 nir_tex_instr_dest_size(tex) -
369 (tex->is_array ? 1 : 0),
370 tex->is_array));
Boris Brezillonc3558862019-06-17 22:13:04 +0200371 dst = &tex->dest;
372 break;
Boris Brezillonbd49c8f2019-06-14 09:59:20 +0200373 default:
374 break;
375 }
376
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -0700377 if (dest && dst && !is_store)
Boris Brezillonbd49c8f2019-06-14 09:59:20 +0200378 *dest = nir_dest_index(ctx, dst);
379
380 return sysval;
381}
382
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +0000383static void
384midgard_nir_assign_sysval_body(compiler_context *ctx, nir_instr *instr)
385{
Boris Brezillonbd49c8f2019-06-14 09:59:20 +0200386 int sysval;
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +0000387
Boris Brezillonbd49c8f2019-06-14 09:59:20 +0200388 sysval = sysval_for_instr(ctx, instr, NULL);
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +0000389 if (sysval < 0)
390 return;
391
392 /* We have a sysval load; check if it's already been assigned */
393
394 if (_mesa_hash_table_u64_search(ctx->sysval_to_id, sysval))
395 return;
396
397 /* It hasn't -- so assign it now! */
398
399 unsigned id = ctx->sysval_count++;
400 _mesa_hash_table_u64_insert(ctx->sysval_to_id, sysval, (void *) ((uintptr_t) id + 1));
401 ctx->sysvals[id] = sysval;
402}
403
404static void
405midgard_nir_assign_sysvals(compiler_context *ctx, nir_shader *shader)
406{
407 ctx->sysval_count = 0;
408
409 nir_foreach_function(function, shader) {
410 if (!function->impl) continue;
411
412 nir_foreach_block(block, function->impl) {
413 nir_foreach_instr_safe(instr, block) {
414 midgard_nir_assign_sysval_body(ctx, instr);
415 }
416 }
417 }
418}
419
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000420static bool
421midgard_nir_lower_fdot2(nir_shader *shader)
422{
423 bool progress = false;
424
425 nir_foreach_function(function, shader) {
426 if (!function->impl) continue;
427
428 nir_builder _b;
429 nir_builder *b = &_b;
430 nir_builder_init(b, function->impl);
431
432 nir_foreach_block(block, function->impl) {
433 nir_foreach_instr_safe(instr, block) {
434 if (instr->type != nir_instr_type_alu) continue;
435
436 nir_alu_instr *alu = nir_instr_as_alu(instr);
437 midgard_nir_lower_fdot2_body(b, alu);
438
439 progress |= true;
440 }
441 }
442
443 nir_metadata_preserve(function->impl, nir_metadata_block_index | nir_metadata_dominance);
444
445 }
446
447 return progress;
448}
449
Alyssa Rosenzweiga2f1a062019-07-08 12:40:34 -0700450/* Flushes undefined values to zero */
451
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000452static void
Alyssa Rosenzweigbda2bb32019-11-21 08:45:27 -0500453optimise_nir(nir_shader *nir, unsigned quirks)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000454{
455 bool progress;
Ian Romanickd41cdef2018-08-18 16:42:04 -0700456 unsigned lower_flrp =
457 (nir->options->lower_flrp16 ? 16 : 0) |
458 (nir->options->lower_flrp32 ? 32 : 0) |
459 (nir->options->lower_flrp64 ? 64 : 0);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000460
461 NIR_PASS(progress, nir, nir_lower_regs_to_ssa);
Rhys Perry8b98d092019-02-05 15:56:24 +0000462 NIR_PASS(progress, nir, nir_lower_idiv, nir_lower_idiv_fast);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000463
Alyssa Rosenzweig44a6c382019-08-14 08:44:40 -0700464 nir_lower_tex_options lower_tex_options = {
465 .lower_txs_lod = true,
Alyssa Rosenzweig4c43b352019-11-21 13:40:00 -0500466 .lower_txp = ~0,
467 .lower_tex_without_implicit_lod =
468 (quirks & MIDGARD_EXPLICIT_LOD),
Alyssa Rosenzweigc57337b2019-12-19 11:12:50 -0500469
470 /* TODO: we have native gradient.. */
471 .lower_txd = true,
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000472 };
473
Alyssa Rosenzweig44a6c382019-08-14 08:44:40 -0700474 NIR_PASS(progress, nir, nir_lower_tex, &lower_tex_options);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000475
Alyssa Rosenzweigc57337b2019-12-19 11:12:50 -0500476 /* Must lower fdot2 after tex is lowered */
477 NIR_PASS(progress, nir, midgard_nir_lower_fdot2);
478
Alyssa Rosenzweigbda2bb32019-11-21 08:45:27 -0500479 /* T720 is broken. */
480
481 if (quirks & MIDGARD_BROKEN_LOD)
482 NIR_PASS_V(nir, midgard_nir_lod_errata);
483
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000484 do {
485 progress = false;
486
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000487 NIR_PASS(progress, nir, nir_lower_var_copies);
488 NIR_PASS(progress, nir, nir_lower_vars_to_ssa);
489
490 NIR_PASS(progress, nir, nir_copy_prop);
Boris Brezillon440b0d62020-01-06 14:31:38 +0100491 NIR_PASS(progress, nir, nir_opt_remove_phis);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000492 NIR_PASS(progress, nir, nir_opt_dce);
493 NIR_PASS(progress, nir, nir_opt_dead_cf);
494 NIR_PASS(progress, nir, nir_opt_cse);
495 NIR_PASS(progress, nir, nir_opt_peephole_select, 64, false, true);
496 NIR_PASS(progress, nir, nir_opt_algebraic);
497 NIR_PASS(progress, nir, nir_opt_constant_folding);
Ian Romanickd41cdef2018-08-18 16:42:04 -0700498
499 if (lower_flrp != 0) {
Ian Romanick1f1007a2019-05-08 07:32:43 -0700500 bool lower_flrp_progress = false;
Ian Romanickd41cdef2018-08-18 16:42:04 -0700501 NIR_PASS(lower_flrp_progress,
502 nir,
503 nir_lower_flrp,
504 lower_flrp,
505 false /* always_precise */,
506 nir->options->lower_ffma);
507 if (lower_flrp_progress) {
508 NIR_PASS(progress, nir,
509 nir_opt_constant_folding);
510 progress = true;
511 }
512
513 /* Nothing should rematerialize any flrps, so we only
514 * need to do this lowering once.
515 */
516 lower_flrp = 0;
517 }
518
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000519 NIR_PASS(progress, nir, nir_opt_undef);
Alyssa Rosenzweiga2f1a062019-07-08 12:40:34 -0700520 NIR_PASS(progress, nir, nir_undef_to_zero);
521
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000522 NIR_PASS(progress, nir, nir_opt_loop_unroll,
523 nir_var_shader_in |
524 nir_var_shader_out |
525 nir_var_function_temp);
526
Alyssa Rosenzweig94029702019-06-17 11:12:51 -0700527 NIR_PASS(progress, nir, nir_opt_vectorize);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000528 } while (progress);
529
530 /* Must be run at the end to prevent creation of fsin/fcos ops */
531 NIR_PASS(progress, nir, midgard_nir_scale_trig);
532
533 do {
534 progress = false;
535
536 NIR_PASS(progress, nir, nir_opt_dce);
537 NIR_PASS(progress, nir, nir_opt_algebraic);
538 NIR_PASS(progress, nir, nir_opt_constant_folding);
539 NIR_PASS(progress, nir, nir_copy_prop);
540 } while (progress);
541
542 NIR_PASS(progress, nir, nir_opt_algebraic_late);
Alyssa Rosenzweig726f0262019-05-07 02:52:08 +0000543
544 /* We implement booleans as 32-bit 0/~0 */
545 NIR_PASS(progress, nir, nir_lower_bool_to_int32);
546
547 /* Now that booleans are lowered, we can run out late opts */
Alyssa Rosenzweigeffe6fb02019-03-25 02:49:04 +0000548 NIR_PASS(progress, nir, midgard_nir_lower_algebraic_late);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000549
Alyssa Rosenzweigeffe6fb02019-03-25 02:49:04 +0000550 /* Lower mods for float ops only. Integer ops don't support modifiers
551 * (saturate doesn't make sense on integers, neg/abs require dedicated
552 * instructions) */
553
554 NIR_PASS(progress, nir, nir_lower_to_source_mods, nir_lower_float_source_mods);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000555 NIR_PASS(progress, nir, nir_copy_prop);
556 NIR_PASS(progress, nir, nir_opt_dce);
557
558 /* Take us out of SSA */
559 NIR_PASS(progress, nir, nir_lower_locals_to_regs);
560 NIR_PASS(progress, nir, nir_convert_from_ssa, true);
561
562 /* We are a vector architecture; write combine where possible */
563 NIR_PASS(progress, nir, nir_move_vec_src_uses_to_dest);
564 NIR_PASS(progress, nir, nir_lower_vec_to_movs);
565
566 NIR_PASS(progress, nir, nir_opt_dce);
567}
568
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000569/* Do not actually emit a load; instead, cache the constant for inlining */
570
571static void
572emit_load_const(compiler_context *ctx, nir_load_const_instr *instr)
573{
574 nir_ssa_def def = instr->def;
575
Boris Brezillon15c92d12020-01-20 15:00:57 +0100576 midgard_constants *consts = rzalloc(NULL, midgard_constants);
577
578 assert(instr->def.num_components * instr->def.bit_size <= sizeof(*consts) * 8);
579
580#define RAW_CONST_COPY(bits) \
581 nir_const_value_to_array(consts->u##bits, instr->value, \
582 instr->def.num_components, u##bits)
583
584 switch (instr->def.bit_size) {
585 case 64:
586 RAW_CONST_COPY(64);
587 break;
588 case 32:
589 RAW_CONST_COPY(32);
590 break;
591 case 16:
592 RAW_CONST_COPY(16);
593 break;
594 case 8:
595 RAW_CONST_COPY(8);
596 break;
597 default:
598 unreachable("Invalid bit_size for load_const instruction\n");
599 }
Alyssa Rosenzweig9beb3392019-07-26 11:30:06 -0700600
601 /* Shifted for SSA, +1 for off-by-one */
Boris Brezillon15c92d12020-01-20 15:00:57 +0100602 _mesa_hash_table_u64_insert(ctx->ssa_constants, (def.index << 1) + 1, consts);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000603}
604
Alyssa Rosenzweige1693012019-07-24 12:52:27 -0700605/* Normally constants are embedded implicitly, but for I/O and such we have to
606 * explicitly emit a move with the constant source */
607
608static void
609emit_explicit_constant(compiler_context *ctx, unsigned node, unsigned to)
610{
611 void *constant_value = _mesa_hash_table_u64_search(ctx->ssa_constants, node + 1);
612
613 if (constant_value) {
Alyssa Rosenzweigc3a46e72019-10-30 16:29:28 -0400614 midgard_instruction ins = v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT), to);
Alyssa Rosenzweige1693012019-07-24 12:52:27 -0700615 attach_constants(ctx, &ins, constant_value, node + 1);
616 emit_mir_instruction(ctx, ins);
617 }
618}
619
Alyssa Rosenzweig726f0262019-05-07 02:52:08 +0000620static bool
621nir_is_non_scalar_swizzle(nir_alu_src *src, unsigned nr_components)
622{
623 unsigned comp = src->swizzle[0];
624
625 for (unsigned c = 1; c < nr_components; ++c) {
626 if (src->swizzle[c] != comp)
627 return true;
628 }
629
630 return false;
631}
632
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000633#define ALU_CASE(nir, _op) \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000634 case nir_op_##nir: \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000635 op = midgard_alu_op_##_op; \
Alyssa Rosenzweig0ed8cca2019-07-01 17:35:25 -0700636 assert(src_bitsize == dst_bitsize); \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000637 break;
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700638
639#define ALU_CASE_BCAST(nir, _op, count) \
640 case nir_op_##nir: \
641 op = midgard_alu_op_##_op; \
642 broadcast_swizzle = count; \
Alyssa Rosenzweig0ed8cca2019-07-01 17:35:25 -0700643 assert(src_bitsize == dst_bitsize); \
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700644 break;
Alyssa Rosenzweig7bc91b42019-04-24 23:42:30 +0000645static bool
646nir_is_fzero_constant(nir_src src)
647{
648 if (!nir_src_is_const(src))
649 return false;
650
651 for (unsigned c = 0; c < nir_src_num_components(src); ++c) {
652 if (nir_src_comp_as_float(src, c) != 0.0)
653 return false;
654 }
655
656 return true;
657}
658
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700659/* Analyze the sizes of the inputs to determine which reg mode. Ops needed
660 * special treatment override this anyway. */
Alyssa Rosenzweig4df80ca2019-07-01 15:26:22 -0700661
662static midgard_reg_mode
663reg_mode_for_nir(nir_alu_instr *instr)
664{
665 unsigned src_bitsize = nir_src_bit_size(instr->src[0].src);
666
667 switch (src_bitsize) {
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -0700668 case 8:
669 return midgard_reg_mode_8;
670 case 16:
671 return midgard_reg_mode_16;
672 case 32:
673 return midgard_reg_mode_32;
674 case 64:
675 return midgard_reg_mode_64;
676 default:
677 unreachable("Invalid bit size");
Alyssa Rosenzweig4df80ca2019-07-01 15:26:22 -0700678 }
679}
680
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000681static void
682emit_alu(compiler_context *ctx, nir_alu_instr *instr)
683{
Alyssa Rosenzweig8f887322019-07-29 15:11:12 -0700684 /* Derivatives end up emitted on the texture pipe, not the ALUs. This
685 * is handled elsewhere */
686
687 if (instr->op == nir_op_fddx || instr->op == nir_op_fddy) {
688 midgard_emit_derivatives(ctx, instr);
689 return;
690 }
691
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000692 bool is_ssa = instr->dest.dest.is_ssa;
693
Alyssa Rosenzweig4ed23b12019-02-07 04:56:13 +0000694 unsigned dest = nir_dest_index(ctx, &instr->dest.dest);
Alyssa Rosenzweigf42e5be2019-07-01 15:28:37 -0700695 unsigned nr_components = nir_dest_num_components(instr->dest.dest);
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000696 unsigned nr_inputs = nir_op_infos[instr->op].num_inputs;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000697
698 /* Most Midgard ALU ops have a 1:1 correspondance to NIR ops; these are
699 * supported. A few do not and are commented for now. Also, there are a
700 * number of NIR ops which Midgard does not support and need to be
701 * lowered, also TODO. This switch block emits the opcode and calling
702 * convention of the Midgard instruction; actual packing is done in
703 * emit_alu below */
704
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000705 unsigned op;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000706
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700707 /* Number of components valid to check for the instruction (the rest
708 * will be forced to the last), or 0 to use as-is. Relevant as
709 * ball-type instructions have a channel count in NIR but are all vec4
710 * in Midgard */
711
712 unsigned broadcast_swizzle = 0;
713
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700714 /* What register mode should we operate in? */
715 midgard_reg_mode reg_mode =
716 reg_mode_for_nir(instr);
717
Alyssa Rosenzweig4df80ca2019-07-01 15:26:22 -0700718 /* Do we need a destination override? Used for inline
719 * type conversion */
720
721 midgard_dest_override dest_override =
722 midgard_dest_override_none;
723
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700724 /* Should we use a smaller respective source and sign-extend? */
725
726 bool half_1 = false, sext_1 = false;
727 bool half_2 = false, sext_2 = false;
728
Alyssa Rosenzweig0ed8cca2019-07-01 17:35:25 -0700729 unsigned src_bitsize = nir_src_bit_size(instr->src[0].src);
730 unsigned dst_bitsize = nir_dest_bit_size(instr->dest.dest);
731
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000732 switch (instr->op) {
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000733 ALU_CASE(fadd, fadd);
734 ALU_CASE(fmul, fmul);
735 ALU_CASE(fmin, fmin);
736 ALU_CASE(fmax, fmax);
737 ALU_CASE(imin, imin);
738 ALU_CASE(imax, imax);
Alyssa Rosenzweig2e7555b2019-04-05 05:16:54 +0000739 ALU_CASE(umin, umin);
740 ALU_CASE(umax, umax);
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000741 ALU_CASE(ffloor, ffloor);
Alyssa Rosenzweigc6be9962019-02-23 01:12:10 +0000742 ALU_CASE(fround_even, froundeven);
743 ALU_CASE(ftrunc, ftrunc);
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000744 ALU_CASE(fceil, fceil);
745 ALU_CASE(fdot3, fdot3);
746 ALU_CASE(fdot4, fdot4);
747 ALU_CASE(iadd, iadd);
748 ALU_CASE(isub, isub);
749 ALU_CASE(imul, imul);
Alyssa Rosenzweig9f14e202019-06-05 15:18:35 +0000750
751 /* Zero shoved as second-arg */
752 ALU_CASE(iabs, iabsdiff);
753
Jason Ekstrandf2dc0f22019-05-06 11:45:46 -0500754 ALU_CASE(mov, imov);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000755
Alyssa Rosenzweig3fb88422019-03-25 00:25:01 +0000756 ALU_CASE(feq32, feq);
757 ALU_CASE(fne32, fne);
758 ALU_CASE(flt32, flt);
759 ALU_CASE(ieq32, ieq);
760 ALU_CASE(ine32, ine);
761 ALU_CASE(ilt32, ilt);
Alyssa Rosenzweigb8739c22019-03-26 04:00:33 +0000762 ALU_CASE(ult32, ult);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000763
Alyssa Rosenzweig3208c9d2019-03-25 01:13:12 +0000764 /* We don't have a native b2f32 instruction. Instead, like many
765 * GPUs, we exploit booleans as 0/~0 for false/true, and
766 * correspondingly AND
767 * by 1.0 to do the type conversion. For the moment, prime us
768 * to emit:
769 *
770 * iand [whatever], #0
771 *
772 * At the end of emit_alu (as MIR), we'll fix-up the constant
773 */
774
775 ALU_CASE(b2f32, iand);
776 ALU_CASE(b2i32, iand);
777
Alyssa Rosenzweigae43b8f2019-03-25 00:53:46 +0000778 /* Likewise, we don't have a dedicated f2b32 instruction, but
Alyssa Rosenzweig3208c9d2019-03-25 01:13:12 +0000779 * we can do a "not equal to 0.0" test. */
Alyssa Rosenzweigae43b8f2019-03-25 00:53:46 +0000780
781 ALU_CASE(f2b32, fne);
Alyssa Rosenzweig5b95fef2019-03-25 00:56:48 +0000782 ALU_CASE(i2b32, ine);
Alyssa Rosenzweigae43b8f2019-03-25 00:53:46 +0000783
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000784 ALU_CASE(frcp, frcp);
785 ALU_CASE(frsq, frsqrt);
786 ALU_CASE(fsqrt, fsqrt);
787 ALU_CASE(fexp2, fexp2);
788 ALU_CASE(flog2, flog2);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000789
Boris Brezillonfcceeaf2020-01-20 22:05:14 +0100790 ALU_CASE(f2i64, f2i_rtz);
791 ALU_CASE(f2u64, f2u_rtz);
792 ALU_CASE(i2f64, i2f_rtz);
793 ALU_CASE(u2f64, u2f_rtz);
794
Alyssa Rosenzweig73bf6692019-06-05 15:03:02 -0700795 ALU_CASE(f2i32, f2i_rtz);
796 ALU_CASE(f2u32, f2u_rtz);
797 ALU_CASE(i2f32, i2f_rtz);
798 ALU_CASE(u2f32, u2f_rtz);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000799
Alyssa Rosenzweigd8c084d2019-07-01 17:41:20 -0700800 ALU_CASE(f2i16, f2i_rtz);
801 ALU_CASE(f2u16, f2u_rtz);
802 ALU_CASE(i2f16, i2f_rtz);
803 ALU_CASE(u2f16, u2f_rtz);
804
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000805 ALU_CASE(fsin, fsin);
806 ALU_CASE(fcos, fcos);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000807
Alyssa Rosenzweig159abd52019-07-26 11:15:31 -0700808 /* We'll set invert */
809 ALU_CASE(inot, imov);
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000810 ALU_CASE(iand, iand);
811 ALU_CASE(ior, ior);
812 ALU_CASE(ixor, ixor);
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000813 ALU_CASE(ishl, ishl);
814 ALU_CASE(ishr, iasr);
815 ALU_CASE(ushr, ilsr);
816
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700817 ALU_CASE_BCAST(b32all_fequal2, fball_eq, 2);
818 ALU_CASE_BCAST(b32all_fequal3, fball_eq, 3);
Alyssa Rosenzweig3fb88422019-03-25 00:25:01 +0000819 ALU_CASE(b32all_fequal4, fball_eq);
Alyssa Rosenzweig53664102019-03-25 00:12:06 +0000820
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700821 ALU_CASE_BCAST(b32any_fnequal2, fbany_neq, 2);
822 ALU_CASE_BCAST(b32any_fnequal3, fbany_neq, 3);
Alyssa Rosenzweig3fb88422019-03-25 00:25:01 +0000823 ALU_CASE(b32any_fnequal4, fbany_neq);
Alyssa Rosenzweig53664102019-03-25 00:12:06 +0000824
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700825 ALU_CASE_BCAST(b32all_iequal2, iball_eq, 2);
826 ALU_CASE_BCAST(b32all_iequal3, iball_eq, 3);
Alyssa Rosenzweig3fb88422019-03-25 00:25:01 +0000827 ALU_CASE(b32all_iequal4, iball_eq);
Alyssa Rosenzweig53664102019-03-25 00:12:06 +0000828
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700829 ALU_CASE_BCAST(b32any_inequal2, ibany_neq, 2);
830 ALU_CASE_BCAST(b32any_inequal3, ibany_neq, 3);
Alyssa Rosenzweig3fb88422019-03-25 00:25:01 +0000831 ALU_CASE(b32any_inequal4, ibany_neq);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000832
Alyssa Rosenzweig659aa3d2019-05-26 03:16:37 +0000833 /* Source mods will be shoved in later */
834 ALU_CASE(fabs, fmov);
835 ALU_CASE(fneg, fmov);
836 ALU_CASE(fsat, fmov);
837
Alyssa Rosenzweig4df80ca2019-07-01 15:26:22 -0700838 /* For size conversion, we use a move. Ideally though we would squash
839 * these ops together; maybe that has to happen after in NIR as part of
840 * propagation...? An earlier algebraic pass ensured we step down by
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700841 * only / exactly one size. If stepping down, we use a dest override to
842 * reduce the size; if stepping up, we use a larger-sized move with a
843 * half source and a sign/zero-extension modifier */
Alyssa Rosenzweig4df80ca2019-07-01 15:26:22 -0700844
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700845 case nir_op_i2i8:
846 case nir_op_i2i16:
847 case nir_op_i2i32:
Alyssa Rosenzweig2655a302019-11-04 22:21:20 -0500848 case nir_op_i2i64:
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700849 /* If we end up upscale, we'll need a sign-extend on the
850 * operand (the second argument) */
851
852 sext_2 = true;
Alyssa Rosenzweig14a2032f2019-08-21 09:20:17 -0700853 /* fallthrough */
Alyssa Rosenzweig4df80ca2019-07-01 15:26:22 -0700854 case nir_op_u2u8:
855 case nir_op_u2u16:
Alyssa Rosenzweig2655a302019-11-04 22:21:20 -0500856 case nir_op_u2u32:
Boris Brezillonf53a0792020-01-20 16:03:52 +0100857 case nir_op_u2u64:
858 case nir_op_f2f16:
Boris Brezillone1f9e8d2020-01-20 16:05:31 +0100859 case nir_op_f2f32:
860 case nir_op_f2f64: {
861 if (instr->op == nir_op_f2f16 || instr->op == nir_op_f2f32 ||
862 instr->op == nir_op_f2f64)
Boris Brezillonf53a0792020-01-20 16:03:52 +0100863 op = midgard_alu_op_fmov;
864 else
865 op = midgard_alu_op_imov;
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700866
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700867 if (dst_bitsize == (src_bitsize * 2)) {
868 /* Converting up */
869 half_2 = true;
870
871 /* Use a greater register mode */
872 reg_mode++;
873 } else if (src_bitsize == (dst_bitsize * 2)) {
874 /* Converting down */
875 dest_override = midgard_dest_override_lower;
876 }
877
Alyssa Rosenzweig4df80ca2019-07-01 15:26:22 -0700878 break;
879 }
880
Alyssa Rosenzweig7b78af82019-03-26 04:01:33 +0000881 /* For greater-or-equal, we lower to less-or-equal and flip the
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000882 * arguments */
883
Alyssa Rosenzweig7b78af82019-03-26 04:01:33 +0000884 case nir_op_fge:
885 case nir_op_fge32:
886 case nir_op_ige32:
887 case nir_op_uge32: {
888 op =
889 instr->op == nir_op_fge ? midgard_alu_op_fle :
890 instr->op == nir_op_fge32 ? midgard_alu_op_fle :
891 instr->op == nir_op_ige32 ? midgard_alu_op_ile :
892 instr->op == nir_op_uge32 ? midgard_alu_op_ule :
893 0;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000894
895 /* Swap via temporary */
896 nir_alu_src temp = instr->src[1];
897 instr->src[1] = instr->src[0];
898 instr->src[0] = temp;
899
900 break;
901 }
902
Alyssa Rosenzweig3fb88422019-03-25 00:25:01 +0000903 case nir_op_b32csel: {
Alyssa Rosenzweig726f0262019-05-07 02:52:08 +0000904 /* Midgard features both fcsel and icsel, depending on
905 * the type of the arguments/output. However, as long
906 * as we're careful we can _always_ use icsel and
907 * _never_ need fcsel, since the latter does additional
908 * floating-point-specific processing whereas the
909 * former just moves bits on the wire. It's not obvious
910 * why these are separate opcodes, save for the ability
911 * to do things like sat/pos/abs/neg for free */
Alyssa Rosenzweig3d7874c2019-05-03 01:54:16 +0000912
Alyssa Rosenzweig726f0262019-05-07 02:52:08 +0000913 bool mixed = nir_is_non_scalar_swizzle(&instr->src[0], nr_components);
914 op = mixed ? midgard_alu_op_icsel_v : midgard_alu_op_icsel;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000915
Alyssa Rosenzweig726f0262019-05-07 02:52:08 +0000916 /* The condition is the first argument; move the other
917 * arguments up one to be a binary instruction for
Alyssa Rosenzweigd3b3daa2019-09-23 08:00:51 -0400918 * Midgard with the condition last */
Alyssa Rosenzweig8b15f8a2019-04-21 00:09:13 +0000919
Alyssa Rosenzweigd3b3daa2019-09-23 08:00:51 -0400920 nir_alu_src temp = instr->src[2];
921
922 instr->src[2] = instr->src[0];
923 instr->src[0] = instr->src[1];
924 instr->src[1] = temp;
925
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000926 break;
927 }
928
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000929 default:
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +0100930 DBG("Unhandled ALU op %s\n", nir_op_infos[instr->op].name);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000931 assert(0);
932 return;
933 }
934
Alyssa Rosenzweig0a13bab2019-05-15 01:16:51 +0000935 /* Midgard can perform certain modifiers on output of an ALU op */
Alyssa Rosenzweig67804812019-06-05 15:17:45 -0700936 unsigned outmod;
Alyssa Rosenzweig7bc91b42019-04-24 23:42:30 +0000937
Alyssa Rosenzweig67804812019-06-05 15:17:45 -0700938 if (midgard_is_integer_out_op(op)) {
939 outmod = midgard_outmod_int_wrap;
940 } else {
941 bool sat = instr->dest.saturate || instr->op == nir_op_fsat;
942 outmod = sat ? midgard_outmod_sat : midgard_outmod_none;
943 }
Alyssa Rosenzweig659aa3d2019-05-26 03:16:37 +0000944
Alyssa Rosenzweig7bc91b42019-04-24 23:42:30 +0000945 /* fmax(a, 0.0) can turn into a .pos modifier as an optimization */
946
947 if (instr->op == nir_op_fmax) {
948 if (nir_is_fzero_constant(instr->src[0].src)) {
949 op = midgard_alu_op_fmov;
950 nr_inputs = 1;
951 outmod = midgard_outmod_pos;
952 instr->src[0] = instr->src[1];
953 } else if (nir_is_fzero_constant(instr->src[1].src)) {
954 op = midgard_alu_op_fmov;
955 nr_inputs = 1;
956 outmod = midgard_outmod_pos;
957 }
958 }
959
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000960 /* Fetch unit, quirks, etc information */
Alyssa Rosenzweig1f345bc2019-04-24 01:15:15 +0000961 unsigned opcode_props = alu_opcode_props[op].props;
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000962 bool quirk_flipped_r24 = opcode_props & QUIRK_FLIPPED_R24;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000963
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000964 /* src0 will always exist afaik, but src1 will not for 1-argument
965 * instructions. The latter can only be fetched if the instruction
966 * needs it, or else we may segfault. */
967
Alyssa Rosenzweig4ed23b12019-02-07 04:56:13 +0000968 unsigned src0 = nir_alu_src_index(ctx, &instr->src[0]);
Alyssa Rosenzweig8e369962019-08-30 10:42:05 -0700969 unsigned src1 = nr_inputs >= 2 ? nir_alu_src_index(ctx, &instr->src[1]) : ~0;
970 unsigned src2 = nr_inputs == 3 ? nir_alu_src_index(ctx, &instr->src[2]) : ~0;
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -0400971 assert(nr_inputs <= 3);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000972
973 /* Rather than use the instruction generation helpers, we do it
974 * ourselves here to avoid the mess */
975
976 midgard_instruction ins = {
977 .type = TAG_ALU_4,
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -0700978 .src = {
979 quirk_flipped_r24 ? ~0 : src0,
980 quirk_flipped_r24 ? src0 : src1,
Alyssa Rosenzweig8e369962019-08-30 10:42:05 -0700981 src2,
Alyssa Rosenzweigccbc9a42019-12-19 10:35:18 -0500982 ~0
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -0700983 },
984 .dest = dest,
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000985 };
986
Alyssa Rosenzweig8e369962019-08-30 10:42:05 -0700987 nir_alu_src *nirmods[3] = { NULL };
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000988
Alyssa Rosenzweig8e369962019-08-30 10:42:05 -0700989 if (nr_inputs >= 2) {
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000990 nirmods[0] = &instr->src[0];
991 nirmods[1] = &instr->src[1];
992 } else if (nr_inputs == 1) {
993 nirmods[quirk_flipped_r24] = &instr->src[0];
994 } else {
995 assert(0);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000996 }
997
Alyssa Rosenzweig8e369962019-08-30 10:42:05 -0700998 if (nr_inputs == 3)
999 nirmods[2] = &instr->src[2];
1000
Alyssa Rosenzweig659aa3d2019-05-26 03:16:37 +00001001 /* These were lowered to a move, so apply the corresponding mod */
1002
1003 if (instr->op == nir_op_fneg || instr->op == nir_op_fabs) {
1004 nir_alu_src *s = nirmods[quirk_flipped_r24];
1005
1006 if (instr->op == nir_op_fneg)
1007 s->negate = !s->negate;
1008
1009 if (instr->op == nir_op_fabs)
1010 s->abs = !s->abs;
1011 }
1012
Alyssa Rosenzweigfcdfb672019-04-22 03:25:42 +00001013 bool is_int = midgard_is_integer_op(op);
1014
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -07001015 ins.mask = mask_of(nr_components);
1016
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001017 midgard_vector_alu alu = {
1018 .op = op,
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -07001019 .reg_mode = reg_mode,
Alyssa Rosenzweig4df80ca2019-07-01 15:26:22 -07001020 .dest_override = dest_override,
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001021 .outmod = outmod,
1022
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -07001023 .src1 = vector_alu_srco_unsigned(vector_alu_modifiers(nirmods[0], is_int, broadcast_swizzle, half_1, sext_1)),
1024 .src2 = vector_alu_srco_unsigned(vector_alu_modifiers(nirmods[1], is_int, broadcast_swizzle, half_2, sext_2)),
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001025 };
1026
1027 /* Apply writemask if non-SSA, keeping in mind that we can't write to components that don't exist */
1028
1029 if (!is_ssa)
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -07001030 ins.mask &= instr->dest.write_mask;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001031
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001032 for (unsigned m = 0; m < 3; ++m) {
1033 if (!nirmods[m])
1034 continue;
1035
1036 for (unsigned c = 0; c < NIR_MAX_VEC_COMPONENTS; ++c)
1037 ins.swizzle[m][c] = nirmods[m]->swizzle[c];
1038
1039 /* Replicate. TODO: remove when vec16 lands */
1040 for (unsigned c = NIR_MAX_VEC_COMPONENTS; c < MIR_VEC_COMPONENTS; ++c)
1041 ins.swizzle[m][c] = nirmods[m]->swizzle[NIR_MAX_VEC_COMPONENTS - 1];
1042 }
1043
1044 if (nr_inputs == 3) {
1045 /* Conditions can't have mods */
1046 assert(!nirmods[2]->abs);
1047 assert(!nirmods[2]->negate);
1048 }
1049
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001050 ins.alu = alu;
1051
1052 /* Late fixup for emulated instructions */
1053
Alyssa Rosenzweig3208c9d2019-03-25 01:13:12 +00001054 if (instr->op == nir_op_b2f32 || instr->op == nir_op_b2i32) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001055 /* Presently, our second argument is an inline #0 constant.
1056 * Switch over to an embedded 1.0 constant (that can't fit
1057 * inline, since we're 32-bit, not 16-bit like the inline
1058 * constants) */
1059
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07001060 ins.has_inline_constant = false;
1061 ins.src[1] = SSA_FIXED_REGISTER(REGISTER_CONSTANT);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001062 ins.has_constants = true;
Alyssa Rosenzweig9da46032019-03-24 16:07:31 +00001063
Boris Brezillon15c92d12020-01-20 15:00:57 +01001064 if (instr->op == nir_op_b2f32)
1065 ins.constants.f32[0] = 1.0f;
1066 else
1067 ins.constants.i32[0] = 1;
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001068
1069 for (unsigned c = 0; c < 16; ++c)
1070 ins.swizzle[1][c] = 0;
Alyssa Rosenzweig88c59792019-06-05 15:24:51 +00001071 } else if (nr_inputs == 1 && !quirk_flipped_r24) {
1072 /* Lots of instructions need a 0 plonked in */
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07001073 ins.has_inline_constant = false;
1074 ins.src[1] = SSA_FIXED_REGISTER(REGISTER_CONSTANT);
Alyssa Rosenzweig3208c9d2019-03-25 01:13:12 +00001075 ins.has_constants = true;
Boris Brezillon15c92d12020-01-20 15:00:57 +01001076 ins.constants.u32[0] = 0;
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001077
1078 for (unsigned c = 0; c < 16; ++c)
1079 ins.swizzle[1][c] = 0;
Alyssa Rosenzweigbcabcfe2019-04-25 04:25:33 +00001080 } else if (instr->op == nir_op_inot) {
Alyssa Rosenzweig159abd52019-07-26 11:15:31 -07001081 ins.invert = true;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001082 }
1083
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +00001084 if ((opcode_props & UNITS_ALL) == UNIT_VLUT) {
1085 /* To avoid duplicating the lookup tables (probably), true LUT
1086 * instructions can only operate as if they were scalars. Lower
1087 * them here by changing the component. */
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001088
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -07001089 unsigned orig_mask = ins.mask;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001090
1091 for (int i = 0; i < nr_components; ++i) {
Alyssa Rosenzweig2c9e1242019-06-17 11:49:44 -07001092 /* Mask the associated component, dropping the
1093 * instruction if needed */
1094
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -07001095 ins.mask = 1 << i;
1096 ins.mask &= orig_mask;
Alyssa Rosenzweig2c9e1242019-06-17 11:49:44 -07001097
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -07001098 if (!ins.mask)
Alyssa Rosenzweig2c9e1242019-06-17 11:49:44 -07001099 continue;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001100
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001101 for (unsigned j = 0; j < MIR_VEC_COMPONENTS; ++j)
1102 ins.swizzle[0][j] = nirmods[0]->swizzle[i]; /* Pull from the correct component */
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001103
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001104 emit_mir_instruction(ctx, ins);
1105 }
1106 } else {
1107 emit_mir_instruction(ctx, ins);
1108 }
1109}
1110
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +00001111#undef ALU_CASE
1112
Alyssa Rosenzweig1798f6b2019-11-15 15:16:53 -05001113static void
1114mir_set_intr_mask(nir_instr *instr, midgard_instruction *ins, bool is_read)
Alyssa Rosenzweig65e6cb42019-08-13 09:11:48 -07001115{
1116 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
Alyssa Rosenzweig1798f6b2019-11-15 15:16:53 -05001117 unsigned nir_mask = 0;
1118 unsigned dsize = 0;
Alyssa Rosenzweig65e6cb42019-08-13 09:11:48 -07001119
Alyssa Rosenzweig1798f6b2019-11-15 15:16:53 -05001120 if (is_read) {
1121 nir_mask = mask_of(nir_intrinsic_dest_components(intr));
1122 dsize = nir_dest_bit_size(intr->dest);
1123 } else {
1124 nir_mask = nir_intrinsic_write_mask(intr);
1125 dsize = 32;
1126 }
1127
1128 /* Once we have the NIR mask, we need to normalize to work in 32-bit space */
1129 unsigned bytemask = mir_to_bytemask(mir_mode_for_destsize(dsize), nir_mask);
1130 mir_set_bytemask(ins, bytemask);
1131
1132 if (dsize == 64)
1133 ins->load_64 = true;
Alyssa Rosenzweig65e6cb42019-08-13 09:11:48 -07001134}
1135
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001136/* Uniforms and UBOs use a shared code path, as uniforms are just (slightly
1137 * optimized) versions of UBO #0 */
1138
Alyssa Rosenzweig59d30fd2020-01-10 17:47:57 -05001139static midgard_instruction *
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001140emit_ubo_read(
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001141 compiler_context *ctx,
Alyssa Rosenzweig65e6cb42019-08-13 09:11:48 -07001142 nir_instr *instr,
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001143 unsigned dest,
1144 unsigned offset,
1145 nir_src *indirect_offset,
Alyssa Rosenzweig59d30fd2020-01-10 17:47:57 -05001146 unsigned indirect_shift,
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001147 unsigned index)
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001148{
1149 /* TODO: half-floats */
1150
Alyssa Rosenzweigbc9a7d02019-11-15 14:19:34 -05001151 midgard_instruction ins = m_ld_ubo_int4(dest, 0);
Boris Brezillon15c92d12020-01-20 15:00:57 +01001152 ins.constants.u32[0] = offset;
Alyssa Rosenzweigda736512019-12-19 11:12:25 -05001153
1154 if (instr->type == nir_instr_type_intrinsic)
1155 mir_set_intr_mask(instr, &ins, true);
Alyssa Rosenzweig3174bc92019-07-16 14:10:08 -07001156
1157 if (indirect_offset) {
Alyssa Rosenzweige7fd14c2019-10-26 15:50:38 -04001158 ins.src[2] = nir_src_index(ctx, indirect_offset);
Alyssa Rosenzweig59d30fd2020-01-10 17:47:57 -05001159 ins.load_store.arg_2 = (indirect_shift << 5);
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001160 } else {
Alyssa Rosenzweigc9087722019-08-01 13:29:01 -07001161 ins.load_store.arg_2 = 0x1E;
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001162 }
Alyssa Rosenzweig3174bc92019-07-16 14:10:08 -07001163
Alyssa Rosenzweigc9087722019-08-01 13:29:01 -07001164 ins.load_store.arg_1 = index;
1165
Alyssa Rosenzweige7ac46b2019-08-02 17:09:54 -07001166 return emit_mir_instruction(ctx, ins);
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001167}
1168
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001169/* SSBO reads are like UBO reads if you squint */
1170
1171static void
1172emit_ssbo_access(
1173 compiler_context *ctx,
1174 nir_instr *instr,
1175 bool is_read,
1176 unsigned srcdest,
1177 unsigned offset,
1178 nir_src *indirect_offset,
1179 unsigned index)
1180{
1181 /* TODO: types */
1182
1183 midgard_instruction ins;
1184
1185 if (is_read)
1186 ins = m_ld_int4(srcdest, offset);
1187 else
1188 ins = m_st_int4(srcdest, offset);
1189
1190 /* SSBO reads use a generic memory read interface, so we need the
1191 * address of the SSBO as the first argument. This is a sysval. */
1192
1193 unsigned addr = make_compiler_temp(ctx);
1194 emit_sysval_read(ctx, instr, addr, 2);
1195
Alyssa Rosenzweige7fd14c2019-10-26 15:50:38 -04001196 /* The source array:
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001197 *
Alyssa Rosenzweige7fd14c2019-10-26 15:50:38 -04001198 * src[0] = store ? value : unused
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001199 * src[1] = arg_1
1200 * src[2] = arg_2
1201 *
1202 * We would like arg_1 = the address and
1203 * arg_2 = the offset.
1204 */
1205
Alyssa Rosenzweige7fd14c2019-10-26 15:50:38 -04001206 ins.src[1] = addr;
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001207
1208 /* TODO: What is this? It looks superficially like a shift << 5, but
Alyssa Rosenzweig1a4153b2019-08-30 17:29:17 -07001209 * arg_1 doesn't take a shift Should it be E0 or A0? We also need the
1210 * indirect offset. */
1211
1212 if (indirect_offset) {
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001213 ins.load_store.arg_1 |= 0xE0;
Alyssa Rosenzweige7fd14c2019-10-26 15:50:38 -04001214 ins.src[2] = nir_src_index(ctx, indirect_offset);
Alyssa Rosenzweig1a4153b2019-08-30 17:29:17 -07001215 } else {
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001216 ins.load_store.arg_2 = 0x7E;
Alyssa Rosenzweig1a4153b2019-08-30 17:29:17 -07001217 }
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001218
1219 /* TODO: Bounds check */
1220
1221 /* Finally, we emit the direct offset */
1222
1223 ins.load_store.varying_parameters = (offset & 0x1FF) << 1;
1224 ins.load_store.address = (offset >> 9);
Alyssa Rosenzweig1798f6b2019-11-15 15:16:53 -05001225 mir_set_intr_mask(instr, &ins, is_read);
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001226
1227 emit_mir_instruction(ctx, ins);
1228}
1229
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001230static void
Alyssa Rosenzweig15fae1e2019-06-04 23:26:09 +00001231emit_varying_read(
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001232 compiler_context *ctx,
1233 unsigned dest, unsigned offset,
1234 unsigned nr_comp, unsigned component,
Alyssa Rosenzweigc17a4412019-12-27 15:32:50 -05001235 nir_src *indirect_offset, nir_alu_type type, bool flat)
Alyssa Rosenzweig15fae1e2019-06-04 23:26:09 +00001236{
1237 /* XXX: Half-floats? */
1238 /* TODO: swizzle, mask */
1239
1240 midgard_instruction ins = m_ld_vary_32(dest, offset);
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -07001241 ins.mask = mask_of(nr_comp);
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001242
1243 for (unsigned i = 0; i < ARRAY_SIZE(ins.swizzle[0]); ++i)
1244 ins.swizzle[0][i] = MIN2(i + component, COMPONENT_W);
Alyssa Rosenzweig15fae1e2019-06-04 23:26:09 +00001245
1246 midgard_varying_parameter p = {
1247 .is_varying = 1,
1248 .interpolation = midgard_interp_default,
Alyssa Rosenzweigc17a4412019-12-27 15:32:50 -05001249 .flat = flat,
Alyssa Rosenzweig15fae1e2019-06-04 23:26:09 +00001250 };
1251
1252 unsigned u;
1253 memcpy(&u, &p, sizeof(p));
1254 ins.load_store.varying_parameters = u;
1255
Alyssa Rosenzweig513d02c2019-08-01 14:28:34 -07001256 if (indirect_offset)
Alyssa Rosenzweige7fd14c2019-10-26 15:50:38 -04001257 ins.src[2] = nir_src_index(ctx, indirect_offset);
Alyssa Rosenzweig513d02c2019-08-01 14:28:34 -07001258 else
Alyssa Rosenzweigc9087722019-08-01 13:29:01 -07001259 ins.load_store.arg_2 = 0x1E;
Alyssa Rosenzweig15fae1e2019-06-04 23:26:09 +00001260
Alyssa Rosenzweigc9087722019-08-01 13:29:01 -07001261 ins.load_store.arg_1 = 0x9E;
1262
Alyssa Rosenzweig9b97ed12019-06-28 09:30:59 -07001263 /* Use the type appropriate load */
1264 switch (type) {
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001265 case nir_type_uint:
1266 case nir_type_bool:
1267 ins.load_store.op = midgard_op_ld_vary_32u;
1268 break;
1269 case nir_type_int:
1270 ins.load_store.op = midgard_op_ld_vary_32i;
1271 break;
1272 case nir_type_float:
1273 ins.load_store.op = midgard_op_ld_vary_32;
1274 break;
1275 default:
1276 unreachable("Attempted to load unknown type");
1277 break;
Alyssa Rosenzweig9b97ed12019-06-28 09:30:59 -07001278 }
1279
Alyssa Rosenzweig15fae1e2019-06-04 23:26:09 +00001280 emit_mir_instruction(ctx, ins);
1281}
1282
Alyssa Rosenzweig6e688902019-12-19 13:24:17 -05001283static void
1284emit_attr_read(
1285 compiler_context *ctx,
1286 unsigned dest, unsigned offset,
1287 unsigned nr_comp, nir_alu_type t)
1288{
1289 midgard_instruction ins = m_ld_attr_32(dest, offset);
1290 ins.load_store.arg_1 = 0x1E;
1291 ins.load_store.arg_2 = 0x1E;
1292 ins.mask = mask_of(nr_comp);
1293
1294 /* Use the type appropriate load */
1295 switch (t) {
1296 case nir_type_uint:
1297 case nir_type_bool:
1298 ins.load_store.op = midgard_op_ld_attr_32u;
1299 break;
1300 case nir_type_int:
1301 ins.load_store.op = midgard_op_ld_attr_32i;
1302 break;
1303 case nir_type_float:
1304 ins.load_store.op = midgard_op_ld_attr_32;
1305 break;
1306 default:
1307 unreachable("Attempted to load unknown type");
1308 break;
1309 }
1310
1311 emit_mir_instruction(ctx, ins);
1312}
1313
Alyssa Rosenzweigfa687402019-08-02 11:06:21 -07001314void
Alyssa Rosenzweig63e240d2019-08-02 17:10:18 -07001315emit_sysval_read(compiler_context *ctx, nir_instr *instr, signed dest_override,
1316 unsigned nr_components)
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001317{
Alyssa Rosenzweig6d8490f2019-07-11 15:34:56 -07001318 unsigned dest = 0;
1319
Boris Brezillonbd49c8f2019-06-14 09:59:20 +02001320 /* Figure out which uniform this is */
1321 int sysval = sysval_for_instr(ctx, instr, &dest);
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001322 void *val = _mesa_hash_table_u64_search(ctx->sysval_to_id, sysval);
1323
Alyssa Rosenzweigfa687402019-08-02 11:06:21 -07001324 if (dest_override >= 0)
1325 dest = dest_override;
1326
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001327 /* Sysvals are prefix uniforms */
1328 unsigned uniform = ((uintptr_t) val) - 1;
1329
Alyssa Rosenzweig6a466c02019-04-20 23:52:42 +00001330 /* Emit the read itself -- this is never indirect */
Alyssa Rosenzweig63e240d2019-08-02 17:10:18 -07001331 midgard_instruction *ins =
Alyssa Rosenzweig59d30fd2020-01-10 17:47:57 -05001332 emit_ubo_read(ctx, instr, dest, uniform * 16, NULL, 0, 0);
Alyssa Rosenzweig63e240d2019-08-02 17:10:18 -07001333
1334 ins->mask = mask_of(nr_components);
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001335}
1336
Alyssa Rosenzweig7229af72019-08-06 13:47:17 -07001337static unsigned
1338compute_builtin_arg(nir_op op)
1339{
1340 switch (op) {
1341 case nir_intrinsic_load_work_group_id:
1342 return 0x14;
1343 case nir_intrinsic_load_local_invocation_id:
1344 return 0x10;
1345 default:
1346 unreachable("Invalid compute paramater loaded");
1347 }
1348}
1349
Alyssa Rosenzweigdff49862019-08-12 12:36:46 -07001350static void
1351emit_fragment_store(compiler_context *ctx, unsigned src, unsigned rt)
1352{
Alyssa Rosenzweig5e06d902019-08-30 11:06:33 -07001353 emit_explicit_constant(ctx, src, src);
Alyssa Rosenzweigdff49862019-08-12 12:36:46 -07001354
Alyssa Rosenzweigdff49862019-08-12 12:36:46 -07001355 struct midgard_instruction ins =
Alyssa Rosenzweig02f503e2019-12-30 18:53:04 -05001356 v_branch(false, false);
1357
1358 ins.writeout = true;
Alyssa Rosenzweigdff49862019-08-12 12:36:46 -07001359
1360 /* Add dependencies */
Alyssa Rosenzweig76529832019-08-30 11:01:15 -07001361 ins.src[0] = src;
Boris Brezillon15c92d12020-01-20 15:00:57 +01001362 ins.constants.u32[0] = rt * 0x100;
Alyssa Rosenzweigdff49862019-08-12 12:36:46 -07001363
1364 /* Emit the branch */
Alyssa Rosenzweig02f503e2019-12-30 18:53:04 -05001365 midgard_instruction *br = emit_mir_instruction(ctx, ins);
Alyssa Rosenzweig281cc6f2019-11-23 12:43:55 -05001366 schedule_barrier(ctx);
Alyssa Rosenzweig60396342019-11-23 16:08:02 -05001367
Alyssa Rosenzweig5bc62af2020-01-02 12:27:59 -05001368 assert(rt < ARRAY_SIZE(ctx->writeout_branch));
1369 assert(!ctx->writeout_branch[rt]);
1370 ctx->writeout_branch[rt] = br;
1371
1372 /* Push our current location = current block count - 1 = where we'll
1373 * jump to. Maybe a bit too clever for my own good */
1374
1375 br->branch.target_block = ctx->block_count - 1;
Alyssa Rosenzweigdff49862019-08-12 12:36:46 -07001376}
1377
Alyssa Rosenzweig7229af72019-08-06 13:47:17 -07001378static void
1379emit_compute_builtin(compiler_context *ctx, nir_intrinsic_instr *instr)
1380{
1381 unsigned reg = nir_dest_index(ctx, &instr->dest);
1382 midgard_instruction ins = m_ld_compute_id(reg, 0);
1383 ins.mask = mask_of(3);
1384 ins.load_store.arg_1 = compute_builtin_arg(instr->intrinsic);
1385 emit_mir_instruction(ctx, ins);
1386}
Alyssa Rosenzweig306800d2019-12-19 13:31:21 -05001387
1388static unsigned
1389vertex_builtin_arg(nir_op op)
1390{
1391 switch (op) {
1392 case nir_intrinsic_load_vertex_id:
1393 return PAN_VERTEX_ID;
1394 case nir_intrinsic_load_instance_id:
1395 return PAN_INSTANCE_ID;
1396 default:
1397 unreachable("Invalid vertex builtin");
1398 }
1399}
1400
1401static void
1402emit_vertex_builtin(compiler_context *ctx, nir_intrinsic_instr *instr)
1403{
1404 unsigned reg = nir_dest_index(ctx, &instr->dest);
1405 emit_attr_read(ctx, reg, vertex_builtin_arg(instr->intrinsic), 1, nir_type_int);
1406}
1407
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001408static void
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001409emit_intrinsic(compiler_context *ctx, nir_intrinsic_instr *instr)
1410{
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001411 unsigned offset = 0, reg;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001412
1413 switch (instr->intrinsic) {
1414 case nir_intrinsic_discard_if:
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001415 case nir_intrinsic_discard: {
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00001416 bool conditional = instr->intrinsic == nir_intrinsic_discard_if;
1417 struct midgard_instruction discard = v_branch(conditional, false);
1418 discard.branch.target_type = TARGET_DISCARD;
Alyssa Rosenzweigd6e4e362019-08-26 13:59:29 -07001419
1420 if (conditional)
1421 discard.src[0] = nir_src_index(ctx, &instr->src[0]);
1422
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00001423 emit_mir_instruction(ctx, discard);
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07001424 schedule_barrier(ctx);
1425
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001426 break;
1427 }
1428
1429 case nir_intrinsic_load_uniform:
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001430 case nir_intrinsic_load_ubo:
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001431 case nir_intrinsic_load_ssbo:
Alyssa Rosenzweigc17a4412019-12-27 15:32:50 -05001432 case nir_intrinsic_load_input:
1433 case nir_intrinsic_load_interpolated_input: {
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001434 bool is_uniform = instr->intrinsic == nir_intrinsic_load_uniform;
1435 bool is_ubo = instr->intrinsic == nir_intrinsic_load_ubo;
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001436 bool is_ssbo = instr->intrinsic == nir_intrinsic_load_ssbo;
Alyssa Rosenzweigc17a4412019-12-27 15:32:50 -05001437 bool is_flat = instr->intrinsic == nir_intrinsic_load_input;
1438 bool is_interp = instr->intrinsic == nir_intrinsic_load_interpolated_input;
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001439
Alyssa Rosenzweigbbc050b2019-06-27 15:33:07 -07001440 /* Get the base type of the intrinsic */
Alyssa Rosenzweig8d747492019-06-27 14:13:10 -07001441 /* TODO: Infer type? Does it matter? */
1442 nir_alu_type t =
Alyssa Rosenzweigc17a4412019-12-27 15:32:50 -05001443 (is_ubo || is_ssbo) ? nir_type_uint :
1444 (is_interp) ? nir_type_float :
1445 nir_intrinsic_type(instr);
1446
Alyssa Rosenzweigbbc050b2019-06-27 15:33:07 -07001447 t = nir_alu_type_get_base_type(t);
1448
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001449 if (!(is_ubo || is_ssbo)) {
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001450 offset = nir_intrinsic_base(instr);
1451 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001452
Alyssa Rosenzweigc1715b52019-05-22 02:44:12 +00001453 unsigned nr_comp = nir_intrinsic_dest_components(instr);
Alyssa Rosenzweig6a466c02019-04-20 23:52:42 +00001454
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001455 nir_src *src_offset = nir_get_io_offset_src(instr);
1456
1457 bool direct = nir_src_is_const(*src_offset);
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001458 nir_src *indirect_offset = direct ? NULL : src_offset;
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001459
1460 if (direct)
1461 offset += nir_src_as_uint(*src_offset);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001462
Alyssa Rosenzweig43568f22019-06-06 08:16:04 -07001463 /* We may need to apply a fractional offset */
Alyssa Rosenzweigc17a4412019-12-27 15:32:50 -05001464 int component = (is_flat || is_interp) ?
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001465 nir_intrinsic_component(instr) : 0;
Alyssa Rosenzweig4ed23b12019-02-07 04:56:13 +00001466 reg = nir_dest_index(ctx, &instr->dest);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001467
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001468 if (is_uniform && !ctx->is_blend) {
Alyssa Rosenzweig59d30fd2020-01-10 17:47:57 -05001469 emit_ubo_read(ctx, &instr->instr, reg, (ctx->sysval_count + offset) * 16, indirect_offset, 4, 0);
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001470 } else if (is_ubo) {
1471 nir_src index = instr->src[0];
1472
Alyssa Rosenzweig59d30fd2020-01-10 17:47:57 -05001473 /* TODO: Is indirect block number possible? */
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001474 assert(nir_src_is_const(index));
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001475
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001476 uint32_t uindex = nir_src_as_uint(index) + 1;
Alyssa Rosenzweig59d30fd2020-01-10 17:47:57 -05001477 emit_ubo_read(ctx, &instr->instr, reg, offset, indirect_offset, 0, uindex);
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001478 } else if (is_ssbo) {
1479 nir_src index = instr->src[0];
1480 assert(nir_src_is_const(index));
1481 uint32_t uindex = nir_src_as_uint(index);
1482
1483 emit_ssbo_access(ctx, &instr->instr, true, reg, offset, indirect_offset, uindex);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001484 } else if (ctx->stage == MESA_SHADER_FRAGMENT && !ctx->is_blend) {
Alyssa Rosenzweigc17a4412019-12-27 15:32:50 -05001485 emit_varying_read(ctx, reg, offset, nr_comp, component, indirect_offset, t, is_flat);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001486 } else if (ctx->is_blend) {
Alyssa Rosenzweig39104222019-05-06 02:12:41 +00001487 /* For blend shaders, load the input color, which is
1488 * preloaded to r0 */
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001489
Alyssa Rosenzweigc3a46e72019-10-30 16:29:28 -04001490 midgard_instruction move = v_mov(SSA_FIXED_REGISTER(0), reg);
Alyssa Rosenzweig005d9b12019-05-20 00:46:48 +00001491 emit_mir_instruction(ctx, move);
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07001492 schedule_barrier(ctx);
Alyssa Rosenzweig6e688902019-12-19 13:24:17 -05001493 } else if (ctx->stage == MESA_SHADER_VERTEX) {
1494 emit_attr_read(ctx, reg, offset, nr_comp, t);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001495 } else {
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +01001496 DBG("Unknown load\n");
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001497 assert(0);
1498 }
1499
1500 break;
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001501 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001502
Alyssa Rosenzweigc17a4412019-12-27 15:32:50 -05001503 /* Artefact of load_interpolated_input. TODO: other barycentric modes */
1504 case nir_intrinsic_load_barycentric_pixel:
1505 break;
1506
Alyssa Rosenzweig1686ef82019-07-01 17:23:58 -07001507 /* Reads 128-bit value raw off the tilebuffer during blending, tasty */
1508
Alyssa Rosenzweig541b3292019-07-01 15:02:40 -07001509 case nir_intrinsic_load_raw_output_pan:
Alyssa Rosenzweig843874c2019-11-06 21:50:32 -05001510 case nir_intrinsic_load_output_u8_as_fp16_pan:
Alyssa Rosenzweig39104222019-05-06 02:12:41 +00001511 reg = nir_dest_index(ctx, &instr->dest);
Alyssa Rosenzweig541b3292019-07-01 15:02:40 -07001512 assert(ctx->is_blend);
Alyssa Rosenzweig1686ef82019-07-01 17:23:58 -07001513
Alyssa Rosenzweig843874c2019-11-06 21:50:32 -05001514 /* T720 and below use different blend opcodes with slightly
1515 * different semantics than T760 and up */
1516
Alyssa Rosenzweig2d1e18e2020-01-02 12:28:54 -05001517 midgard_instruction ld = m_ld_color_buffer_32u(reg, 0);
Alyssa Rosenzweigfcf144d2019-11-19 20:55:42 -05001518 bool old_blend = ctx->quirks & MIDGARD_OLD_BLEND;
Alyssa Rosenzweig843874c2019-11-06 21:50:32 -05001519
1520 if (instr->intrinsic == nir_intrinsic_load_output_u8_as_fp16_pan) {
1521 ld.load_store.op = old_blend ?
1522 midgard_op_ld_color_buffer_u8_as_fp16_old :
1523 midgard_op_ld_color_buffer_u8_as_fp16;
1524
1525 if (old_blend) {
1526 ld.load_store.address = 1;
1527 ld.load_store.arg_2 = 0x1E;
1528 }
1529
1530 for (unsigned c = 2; c < 16; ++c)
1531 ld.swizzle[0][c] = 0;
1532 }
1533
Alyssa Rosenzweig1a4153b2019-08-30 17:29:17 -07001534 emit_mir_instruction(ctx, ld);
Alyssa Rosenzweig39104222019-05-06 02:12:41 +00001535 break;
1536
1537 case nir_intrinsic_load_blend_const_color_rgba: {
1538 assert(ctx->is_blend);
1539 reg = nir_dest_index(ctx, &instr->dest);
1540
1541 /* Blend constants are embedded directly in the shader and
1542 * patched in, so we use some magic routing */
1543
Alyssa Rosenzweigc3a46e72019-10-30 16:29:28 -04001544 midgard_instruction ins = v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT), reg);
Alyssa Rosenzweig39104222019-05-06 02:12:41 +00001545 ins.has_constants = true;
1546 ins.has_blend_constant = true;
1547 emit_mir_instruction(ctx, ins);
1548 break;
1549 }
1550
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001551 case nir_intrinsic_store_output:
Karol Herbst1aabb792019-03-29 21:40:45 +01001552 assert(nir_src_is_const(instr->src[1]) && "no indirect outputs");
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001553
Karol Herbst1aabb792019-03-29 21:40:45 +01001554 offset = nir_intrinsic_base(instr) + nir_src_as_uint(instr->src[1]);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001555
Alyssa Rosenzweig4ed23b12019-02-07 04:56:13 +00001556 reg = nir_src_index(ctx, &instr->src[0]);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001557
1558 if (ctx->stage == MESA_SHADER_FRAGMENT) {
Alyssa Rosenzweigdff49862019-08-12 12:36:46 -07001559 emit_fragment_store(ctx, reg, offset);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001560 } else if (ctx->stage == MESA_SHADER_VERTEX) {
Alyssa Rosenzweiga3ae3cb2019-06-17 12:35:57 -07001561 /* We should have been vectorized, though we don't
1562 * currently check that st_vary is emitted only once
1563 * per slot (this is relevant, since there's not a mask
1564 * parameter available on the store [set to 0 by the
1565 * blob]). We do respect the component by adjusting the
Alyssa Rosenzweig233c0fa2019-07-24 12:54:59 -07001566 * swizzle. If this is a constant source, we'll need to
1567 * emit that explicitly. */
1568
1569 emit_explicit_constant(ctx, reg, reg);
Alyssa Rosenzweiga3ae3cb2019-06-17 12:35:57 -07001570
Boris Brezillon6af63c92020-01-16 11:20:06 +01001571 unsigned dst_component = nir_intrinsic_component(instr);
Alyssa Rosenzweig27887212019-08-15 16:53:03 -07001572 unsigned nr_comp = nir_src_num_components(instr->src[0]);
Alyssa Rosenzweigde8d49a2019-06-06 09:15:26 -07001573
Alyssa Rosenzweig233c0fa2019-07-24 12:54:59 -07001574 midgard_instruction st = m_st_vary_32(reg, offset);
Alyssa Rosenzweigc9087722019-08-01 13:29:01 -07001575 st.load_store.arg_1 = 0x9E;
1576 st.load_store.arg_2 = 0x1E;
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001577
Alyssa Rosenzweig66c26962019-12-27 14:25:00 -05001578 switch (nir_alu_type_get_base_type(nir_intrinsic_type(instr))) {
1579 case nir_type_uint:
1580 case nir_type_bool:
1581 st.load_store.op = midgard_op_st_vary_32u;
1582 break;
1583 case nir_type_int:
1584 st.load_store.op = midgard_op_st_vary_32i;
1585 break;
1586 case nir_type_float:
1587 st.load_store.op = midgard_op_st_vary_32;
1588 break;
1589 default:
1590 unreachable("Attempted to store unknown type");
1591 break;
1592 }
1593
Boris Brezillon6af63c92020-01-16 11:20:06 +01001594 /* nir_intrinsic_component(store_intr) encodes the
1595 * destination component start. Source component offset
1596 * adjustment is taken care of in
1597 * install_registers_instr(), when offset_swizzle() is
1598 * called.
1599 */
1600 unsigned src_component = COMPONENT_X;
1601
1602 assert(nr_comp > 0);
1603 for (unsigned i = 0; i < ARRAY_SIZE(st.swizzle); ++i) {
1604 st.swizzle[0][i] = src_component;
1605 if (i >= dst_component && i < dst_component + nr_comp - 1)
1606 src_component++;
1607 }
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001608
Alyssa Rosenzweig4aced182019-06-06 08:21:27 -07001609 emit_mir_instruction(ctx, st);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001610 } else {
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +01001611 DBG("Unknown store\n");
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001612 assert(0);
1613 }
1614
1615 break;
1616
Alyssa Rosenzweig541b3292019-07-01 15:02:40 -07001617 /* Special case of store_output for lowered blend shaders */
1618 case nir_intrinsic_store_raw_output_pan:
1619 assert (ctx->stage == MESA_SHADER_FRAGMENT);
1620 reg = nir_src_index(ctx, &instr->src[0]);
Alyssa Rosenzweig8555bff2019-11-26 08:48:33 -05001621
1622 if (ctx->quirks & MIDGARD_OLD_BLEND) {
1623 /* Suppose reg = qr0.xyzw. That means 4 8-bit ---> 1 32-bit. So
1624 * reg = r0.x. We want to splatter. So we can do a 32-bit move
1625 * of:
1626 *
1627 * imov r0.xyzw, r0.xxxx
1628 */
1629
1630 unsigned expanded = make_compiler_temp(ctx);
1631
1632 midgard_instruction splatter = v_mov(reg, expanded);
1633
1634 for (unsigned c = 0; c < 16; ++c)
1635 splatter.swizzle[1][c] = 0;
1636
1637 emit_mir_instruction(ctx, splatter);
Alyssa Rosenzweiga2d55032019-11-23 21:44:16 -05001638 emit_fragment_store(ctx, expanded, ctx->blend_rt);
Alyssa Rosenzweig8555bff2019-11-26 08:48:33 -05001639 } else
Alyssa Rosenzweiga2d55032019-11-23 21:44:16 -05001640 emit_fragment_store(ctx, reg, ctx->blend_rt);
Alyssa Rosenzweig541b3292019-07-01 15:02:40 -07001641
1642 break;
1643
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001644 case nir_intrinsic_store_ssbo:
1645 assert(nir_src_is_const(instr->src[1]));
1646
1647 bool direct_offset = nir_src_is_const(instr->src[2]);
1648 offset = direct_offset ? nir_src_as_uint(instr->src[2]) : 0;
1649 nir_src *indirect_offset = direct_offset ? NULL : &instr->src[2];
1650 reg = nir_src_index(ctx, &instr->src[0]);
1651
1652 uint32_t uindex = nir_src_as_uint(instr->src[1]);
1653
1654 emit_explicit_constant(ctx, reg, reg);
1655 emit_ssbo_access(ctx, &instr->instr, false, reg, offset, indirect_offset, uindex);
1656 break;
1657
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001658 case nir_intrinsic_load_viewport_scale:
1659 case nir_intrinsic_load_viewport_offset:
Alyssa Rosenzweig15954ab2019-08-06 14:07:10 -07001660 case nir_intrinsic_load_num_work_groups:
Alyssa Rosenzweig4e07e7b2019-11-21 08:42:28 -05001661 case nir_intrinsic_load_sampler_lod_parameters_pan:
Alyssa Rosenzweig61892742019-08-21 09:15:56 -07001662 emit_sysval_read(ctx, &instr->instr, ~0, 3);
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001663 break;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001664
Alyssa Rosenzweig7229af72019-08-06 13:47:17 -07001665 case nir_intrinsic_load_work_group_id:
1666 case nir_intrinsic_load_local_invocation_id:
1667 emit_compute_builtin(ctx, instr);
1668 break;
1669
Alyssa Rosenzweig306800d2019-12-19 13:31:21 -05001670 case nir_intrinsic_load_vertex_id:
1671 case nir_intrinsic_load_instance_id:
1672 emit_vertex_builtin(ctx, instr);
1673 break;
1674
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001675 default:
1676 printf ("Unhandled intrinsic\n");
1677 assert(0);
1678 break;
1679 }
1680}
1681
1682static unsigned
1683midgard_tex_format(enum glsl_sampler_dim dim)
1684{
1685 switch (dim) {
Alyssa Rosenzweig83c02a52019-06-17 14:26:08 -07001686 case GLSL_SAMPLER_DIM_1D:
1687 case GLSL_SAMPLER_DIM_BUF:
1688 return MALI_TEX_1D;
1689
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001690 case GLSL_SAMPLER_DIM_2D:
1691 case GLSL_SAMPLER_DIM_EXTERNAL:
Alyssa Rosenzweig44a6c382019-08-14 08:44:40 -07001692 case GLSL_SAMPLER_DIM_RECT:
Alyssa Rosenzweig83c02a52019-06-17 14:26:08 -07001693 return MALI_TEX_2D;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001694
1695 case GLSL_SAMPLER_DIM_3D:
Alyssa Rosenzweig83c02a52019-06-17 14:26:08 -07001696 return MALI_TEX_3D;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001697
1698 case GLSL_SAMPLER_DIM_CUBE:
Alyssa Rosenzweig83c02a52019-06-17 14:26:08 -07001699 return MALI_TEX_CUBE;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001700
1701 default:
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +01001702 DBG("Unknown sampler dim type\n");
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001703 assert(0);
1704 return 0;
1705 }
1706}
1707
Alyssa Rosenzweig213b6282019-06-18 09:02:20 -07001708/* Tries to attach an explicit LOD / bias as a constant. Returns whether this
1709 * was successful */
1710
1711static bool
1712pan_attach_constant_bias(
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001713 compiler_context *ctx,
1714 nir_src lod,
1715 midgard_texture_word *word)
Alyssa Rosenzweig213b6282019-06-18 09:02:20 -07001716{
1717 /* To attach as constant, it has to *be* constant */
1718
1719 if (!nir_src_is_const(lod))
1720 return false;
1721
1722 float f = nir_src_as_float(lod);
1723
1724 /* Break into fixed-point */
1725 signed lod_int = f;
1726 float lod_frac = f - lod_int;
1727
1728 /* Carry over negative fractions */
1729 if (lod_frac < 0.0) {
1730 lod_int--;
1731 lod_frac += 1.0;
1732 }
1733
1734 /* Encode */
1735 word->bias = float_to_ubyte(lod_frac);
1736 word->bias_int = lod_int;
1737
1738 return true;
1739}
1740
Alyssa Rosenzweige32af4b2019-06-26 16:12:28 -07001741static enum mali_sampler_type
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001742midgard_sampler_type(nir_alu_type t) {
1743 switch (nir_alu_type_get_base_type(t))
1744 {
1745 case nir_type_float:
1746 return MALI_SAMPLER_FLOAT;
1747 case nir_type_int:
1748 return MALI_SAMPLER_SIGNED;
1749 case nir_type_uint:
1750 return MALI_SAMPLER_UNSIGNED;
1751 default:
1752 unreachable("Unknown sampler type");
Alyssa Rosenzweige32af4b2019-06-26 16:12:28 -07001753 }
1754}
1755
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001756static void
Boris Brezillon5c17f842019-06-17 21:47:46 +02001757emit_texop_native(compiler_context *ctx, nir_tex_instr *instr,
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001758 unsigned midgard_texop)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001759{
1760 /* TODO */
1761 //assert (!instr->sampler);
1762 //assert (!instr->texture_array_size);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001763
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001764 int texture_index = instr->texture_index;
1765 int sampler_index = texture_index;
1766
Alyssa Rosenzweigb0e89412019-06-18 09:02:35 -07001767 /* No helper to build texture words -- we do it all here */
1768 midgard_instruction ins = {
1769 .type = TAG_TEXTURE_4,
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -07001770 .mask = 0xF,
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07001771 .dest = nir_dest_index(ctx, &instr->dest),
Alyssa Rosenzweigccbc9a42019-12-19 10:35:18 -05001772 .src = { ~0, ~0, ~0, ~0 },
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001773 .swizzle = SWIZZLE_IDENTITY_4,
Alyssa Rosenzweigb0e89412019-06-18 09:02:35 -07001774 .texture = {
1775 .op = midgard_texop,
1776 .format = midgard_tex_format(instr->sampler_dim),
1777 .texture_handle = texture_index,
1778 .sampler_handle = sampler_index,
Alyssa Rosenzweigb0e89412019-06-18 09:02:35 -07001779
1780 /* TODO: half */
1781 .in_reg_full = 1,
1782 .out_full = 1,
1783
Alyssa Rosenzweige32af4b2019-06-26 16:12:28 -07001784 .sampler_type = midgard_sampler_type(instr->dest_type),
Alyssa Rosenzweig1a53bed2019-12-16 17:13:46 -05001785 .shadow = instr->is_shadow,
Alyssa Rosenzweigb0e89412019-06-18 09:02:35 -07001786 }
1787 };
Alyssa Rosenzweig8429bee2019-06-14 16:03:39 -07001788
Alyssa Rosenzweigd183f842019-12-16 17:02:36 -05001789 /* We may need a temporary for the coordinate */
1790
Alyssa Rosenzweig66013cb2019-12-16 17:14:04 -05001791 bool needs_temp_coord =
1792 (midgard_texop == TEXTURE_OP_TEXEL_FETCH) ||
Alyssa Rosenzweig6b7243f2019-12-20 17:25:05 -05001793 (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE) ||
Alyssa Rosenzweig66013cb2019-12-16 17:14:04 -05001794 (instr->is_shadow);
1795
Alyssa Rosenzweigd183f842019-12-16 17:02:36 -05001796 unsigned coords = needs_temp_coord ? make_compiler_temp_reg(ctx) : 0;
1797
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001798 for (unsigned i = 0; i < instr->num_srcs; ++i) {
Alyssa Rosenzweiga19ca342019-06-11 09:23:05 -07001799 int index = nir_src_index(ctx, &instr->src[i].src);
Alyssa Rosenzweigedc8e412019-08-15 16:41:53 -07001800 unsigned nr_components = nir_src_num_components(instr->src[i].src);
Alyssa Rosenzweiga19ca342019-06-11 09:23:05 -07001801
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001802 switch (instr->src[i].src_type) {
1803 case nir_tex_src_coord: {
Alyssa Rosenzweigb6946d32019-07-25 08:44:53 -07001804 emit_explicit_constant(ctx, index, index);
1805
Alyssa Rosenzweig9e5a1412019-12-20 17:01:29 -05001806 unsigned coord_mask = mask_of(instr->coord_components);
1807
Alyssa Rosenzweigbc4c8532020-01-06 21:31:46 -05001808 bool flip_zw = (instr->sampler_dim == GLSL_SAMPLER_DIM_2D) && (coord_mask & (1 << COMPONENT_Z));
1809
1810 if (flip_zw)
1811 coord_mask ^= ((1 << COMPONENT_Z) | (1 << COMPONENT_W));
1812
Alyssa Rosenzweig6b7243f2019-12-20 17:25:05 -05001813 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE) {
1814 /* texelFetch is undefined on samplerCube */
1815 assert(midgard_texop != TEXTURE_OP_TEXEL_FETCH);
1816
1817 /* For cubemaps, we use a special ld/st op to
1818 * select the face and copy the xy into the
1819 * texture register */
1820
1821 midgard_instruction ld = m_ld_cubemap_coords(coords, 0);
1822 ld.src[1] = index;
1823 ld.mask = 0x3; /* xy */
1824 ld.load_store.arg_1 = 0x20;
1825 ld.swizzle[1][3] = COMPONENT_X;
1826 emit_mir_instruction(ctx, ld);
1827
1828 /* xyzw -> xyxx */
1829 ins.swizzle[1][2] = instr->is_shadow ? COMPONENT_Z : COMPONENT_X;
1830 ins.swizzle[1][3] = COMPONENT_X;
1831 } else if (needs_temp_coord) {
Alyssa Rosenzweigd183f842019-12-16 17:02:36 -05001832 /* mov coord_temp, coords */
1833 midgard_instruction mov = v_mov(index, coords);
Alyssa Rosenzweig9e5a1412019-12-20 17:01:29 -05001834 mov.mask = coord_mask;
Alyssa Rosenzweigbc4c8532020-01-06 21:31:46 -05001835
1836 if (flip_zw)
1837 mov.swizzle[1][COMPONENT_W] = COMPONENT_Z;
1838
Alyssa Rosenzweigd183f842019-12-16 17:02:36 -05001839 emit_mir_instruction(ctx, mov);
1840 } else {
1841 coords = index;
1842 }
1843
Alyssa Rosenzweig6b7243f2019-12-20 17:25:05 -05001844 ins.src[1] = coords;
1845
Alyssa Rosenzweigb6946d32019-07-25 08:44:53 -07001846 /* Texelfetch coordinates uses all four elements
1847 * (xyz/index) regardless of texture dimensionality,
1848 * which means it's necessary to zero the unused
1849 * components to keep everything happy */
1850
1851 if (midgard_texop == TEXTURE_OP_TEXEL_FETCH) {
Alyssa Rosenzweig9e5a1412019-12-20 17:01:29 -05001852 /* mov index.zw, #0, or generalized */
Alyssa Rosenzweigd183f842019-12-16 17:02:36 -05001853 midgard_instruction mov =
1854 v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT), coords);
Alyssa Rosenzweigb6946d32019-07-25 08:44:53 -07001855 mov.has_constants = true;
Alyssa Rosenzweig9e5a1412019-12-20 17:01:29 -05001856 mov.mask = coord_mask ^ 0xF;
Alyssa Rosenzweigb6946d32019-07-25 08:44:53 -07001857 emit_mir_instruction(ctx, mov);
1858 }
1859
Alyssa Rosenzweigb6946d32019-07-25 08:44:53 -07001860 if (instr->sampler_dim == GLSL_SAMPLER_DIM_2D) {
Alyssa Rosenzweig4cd3dc92020-01-06 21:36:20 -05001861 /* Array component in w but NIR wants it in z,
1862 * but if we have a temp coord we already fixed
1863 * that up */
1864
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001865 if (nr_components == 3) {
1866 ins.swizzle[1][2] = COMPONENT_Z;
Alyssa Rosenzweig4cd3dc92020-01-06 21:36:20 -05001867 ins.swizzle[1][3] = needs_temp_coord ? COMPONENT_W : COMPONENT_Z;
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001868 } else if (nr_components == 2) {
Alyssa Rosenzweig66013cb2019-12-16 17:14:04 -05001869 ins.swizzle[1][2] =
1870 instr->is_shadow ? COMPONENT_Z : COMPONENT_X;
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001871 ins.swizzle[1][3] = COMPONENT_X;
1872 } else
Alyssa Rosenzweigedc8e412019-08-15 16:41:53 -07001873 unreachable("Invalid texture 2D components");
Alyssa Rosenzweig70b3e5d2019-03-28 04:27:13 +00001874 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001875
Alyssa Rosenzweig64b2fe92019-12-20 12:38:24 -05001876 if (midgard_texop == TEXTURE_OP_TEXEL_FETCH) {
1877 /* We zeroed */
1878 ins.swizzle[1][2] = COMPONENT_Z;
1879 ins.swizzle[1][3] = COMPONENT_W;
1880 }
1881
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001882 break;
1883 }
1884
Alyssa Rosenzweig4012e062019-06-11 09:43:08 -07001885 case nir_tex_src_bias:
1886 case nir_tex_src_lod: {
Alyssa Rosenzweigb0e89412019-06-18 09:02:35 -07001887 /* Try as a constant if we can */
1888
1889 bool is_txf = midgard_texop == TEXTURE_OP_TEXEL_FETCH;
1890 if (!is_txf && pan_attach_constant_bias(ctx, instr->src[i].src, &ins.texture))
1891 break;
1892
Alyssa Rosenzweigb0e89412019-06-18 09:02:35 -07001893 ins.texture.lod_register = true;
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001894 ins.src[2] = index;
Alyssa Rosenzweig72e57492019-12-20 12:34:20 -05001895
1896 for (unsigned c = 0; c < MIR_VEC_COMPONENTS; ++c)
1897 ins.swizzle[2][c] = COMPONENT_X;
1898
Alyssa Rosenzweigb6946d32019-07-25 08:44:53 -07001899 emit_explicit_constant(ctx, index, index);
Alyssa Rosenzweigb0e89412019-06-18 09:02:35 -07001900
Alyssa Rosenzweiga19ca342019-06-11 09:23:05 -07001901 break;
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001902 };
Alyssa Rosenzweiga19ca342019-06-11 09:23:05 -07001903
Alyssa Rosenzweigccbc9a42019-12-19 10:35:18 -05001904 case nir_tex_src_offset: {
1905 ins.texture.offset_register = true;
1906 ins.src[3] = index;
1907
1908 for (unsigned c = 0; c < MIR_VEC_COMPONENTS; ++c)
1909 ins.swizzle[3][c] = (c > COMPONENT_Z) ? 0 : c;
1910
1911 emit_explicit_constant(ctx, index, index);
Alyssa Rosenzweig4ec1f952019-12-20 12:58:10 -05001912 break;
Alyssa Rosenzweigccbc9a42019-12-19 10:35:18 -05001913 };
1914
Alyssa Rosenzweig66013cb2019-12-16 17:14:04 -05001915 case nir_tex_src_comparator: {
Alyssa Rosenzweig66013cb2019-12-16 17:14:04 -05001916 unsigned comp = COMPONENT_Z;
1917
1918 /* mov coord_temp.foo, coords */
1919 midgard_instruction mov = v_mov(index, coords);
1920 mov.mask = 1 << comp;
1921
1922 for (unsigned i = 0; i < MIR_VEC_COMPONENTS; ++i)
1923 mov.swizzle[1][i] = COMPONENT_X;
1924
1925 emit_mir_instruction(ctx, mov);
1926 break;
1927 }
1928
Alyssa Rosenzweig5062b612019-06-11 09:55:18 -07001929 default:
1930 unreachable("Unknown texture source type\n");
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001931 }
1932 }
1933
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001934 emit_mir_instruction(ctx, ins);
1935
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001936 /* Used for .cont and .last hinting */
1937 ctx->texture_op_count++;
1938}
1939
1940static void
Boris Brezillon5c17f842019-06-17 21:47:46 +02001941emit_tex(compiler_context *ctx, nir_tex_instr *instr)
1942{
1943 switch (instr->op) {
1944 case nir_texop_tex:
1945 case nir_texop_txb:
1946 emit_texop_native(ctx, instr, TEXTURE_OP_NORMAL);
1947 break;
1948 case nir_texop_txl:
1949 emit_texop_native(ctx, instr, TEXTURE_OP_LOD);
1950 break;
Alyssa Rosenzweigf4bb7f02019-06-21 16:17:34 -07001951 case nir_texop_txf:
1952 emit_texop_native(ctx, instr, TEXTURE_OP_TEXEL_FETCH);
1953 break;
Boris Brezillonc3558862019-06-17 22:13:04 +02001954 case nir_texop_txs:
Alyssa Rosenzweig61892742019-08-21 09:15:56 -07001955 emit_sysval_read(ctx, &instr->instr, ~0, 4);
Boris Brezillonc3558862019-06-17 22:13:04 +02001956 break;
Boris Brezillon5c17f842019-06-17 21:47:46 +02001957 default:
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001958 unreachable("Unhanlded texture op");
Boris Brezillon5c17f842019-06-17 21:47:46 +02001959 }
1960}
1961
1962static void
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001963emit_jump(compiler_context *ctx, nir_jump_instr *instr)
1964{
1965 switch (instr->type) {
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001966 case nir_jump_break: {
1967 /* Emit a branch out of the loop */
1968 struct midgard_instruction br = v_branch(false, false);
1969 br.branch.target_type = TARGET_BREAK;
1970 br.branch.target_break = ctx->current_loop_depth;
1971 emit_mir_instruction(ctx, br);
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001972 break;
1973 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001974
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001975 default:
1976 DBG("Unknown jump type %d\n", instr->type);
1977 break;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001978 }
1979}
1980
1981static void
1982emit_instr(compiler_context *ctx, struct nir_instr *instr)
1983{
1984 switch (instr->type) {
1985 case nir_instr_type_load_const:
1986 emit_load_const(ctx, nir_instr_as_load_const(instr));
1987 break;
1988
1989 case nir_instr_type_intrinsic:
1990 emit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
1991 break;
1992
1993 case nir_instr_type_alu:
1994 emit_alu(ctx, nir_instr_as_alu(instr));
1995 break;
1996
1997 case nir_instr_type_tex:
1998 emit_tex(ctx, nir_instr_as_tex(instr));
1999 break;
2000
2001 case nir_instr_type_jump:
2002 emit_jump(ctx, nir_instr_as_jump(instr));
2003 break;
2004
2005 case nir_instr_type_ssa_undef:
2006 /* Spurious */
2007 break;
2008
2009 default:
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +01002010 DBG("Unhandled instruction type\n");
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002011 break;
2012 }
2013}
2014
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002015
2016/* ALU instructions can inline or embed constants, which decreases register
2017 * pressure and saves space. */
2018
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002019#define CONDITIONAL_ATTACH(idx) { \
2020 void *entry = _mesa_hash_table_u64_search(ctx->ssa_constants, alu->src[idx] + 1); \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002021\
2022 if (entry) { \
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002023 attach_constants(ctx, alu, entry, alu->src[idx] + 1); \
2024 alu->src[idx] = SSA_FIXED_REGISTER(REGISTER_CONSTANT); \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002025 } \
2026}
2027
2028static void
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07002029inline_alu_constants(compiler_context *ctx, midgard_block *block)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002030{
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07002031 mir_foreach_instr_in_block(block, alu) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002032 /* Other instructions cannot inline constants */
2033 if (alu->type != TAG_ALU_4) continue;
Alyssa Rosenzweig5e06d902019-08-30 11:06:33 -07002034 if (alu->compact_branch) continue;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002035
2036 /* If there is already a constant here, we can do nothing */
2037 if (alu->has_constants) continue;
2038
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002039 CONDITIONAL_ATTACH(0);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002040
2041 if (!alu->has_constants) {
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002042 CONDITIONAL_ATTACH(1)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002043 } else if (!alu->inline_constant) {
2044 /* Corner case: _two_ vec4 constants, for instance with a
2045 * csel. For this case, we can only use a constant
2046 * register for one, we'll have to emit a move for the
2047 * other. Note, if both arguments are constants, then
2048 * necessarily neither argument depends on the value of
2049 * any particular register. As the destination register
2050 * will be wiped, that means we can spill the constant
2051 * to the destination register.
2052 */
2053
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002054 void *entry = _mesa_hash_table_u64_search(ctx->ssa_constants, alu->src[1] + 1);
2055 unsigned scratch = alu->dest;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002056
2057 if (entry) {
Alyssa Rosenzweigc3a46e72019-10-30 16:29:28 -04002058 midgard_instruction ins = v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT), scratch);
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002059 attach_constants(ctx, &ins, entry, alu->src[1] + 1);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002060
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002061 /* Set the source */
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002062 alu->src[1] = scratch;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002063
2064 /* Inject us -before- the last instruction which set r31 */
Boris Brezillon938c5b02019-08-28 09:17:21 +02002065 mir_insert_instruction_before(ctx, mir_prev_op(alu), ins);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002066 }
2067 }
2068 }
2069}
2070
Alyssa Rosenzweig62a5ee32019-07-26 14:25:25 -07002071/* Being a little silly with the names, but returns the op that is the bitwise
2072 * inverse of the op with the argument switched. I.e. (f and g are
2073 * contrapositives):
2074 *
2075 * f(a, b) = ~g(b, a)
2076 *
2077 * Corollary: if g is the contrapositve of f, f is the contrapositive of g:
2078 *
2079 * f(a, b) = ~g(b, a)
2080 * ~f(a, b) = g(b, a)
2081 * ~f(a, b) = ~h(a, b) where h is the contrapositive of g
2082 * f(a, b) = h(a, b)
2083 *
2084 * Thus we define this function in pairs.
2085 */
2086
2087static inline midgard_alu_op
2088mir_contrapositive(midgard_alu_op op)
2089{
2090 switch (op) {
2091 case midgard_alu_op_flt:
2092 return midgard_alu_op_fle;
2093 case midgard_alu_op_fle:
2094 return midgard_alu_op_flt;
2095
2096 case midgard_alu_op_ilt:
2097 return midgard_alu_op_ile;
2098 case midgard_alu_op_ile:
2099 return midgard_alu_op_ilt;
2100
2101 default:
2102 unreachable("No known contrapositive");
2103 }
2104}
2105
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002106/* Midgard supports two types of constants, embedded constants (128-bit) and
2107 * inline constants (16-bit). Sometimes, especially with scalar ops, embedded
2108 * constants can be demoted to inline constants, for space savings and
2109 * sometimes a performance boost */
2110
2111static void
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07002112embedded_to_inline_constant(compiler_context *ctx, midgard_block *block)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002113{
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07002114 mir_foreach_instr_in_block(block, ins) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002115 if (!ins->has_constants) continue;
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002116 if (ins->has_inline_constant) continue;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002117
2118 /* Blend constants must not be inlined by definition */
2119 if (ins->has_blend_constant) continue;
2120
Alyssa Rosenzweige92caad2019-07-01 20:02:57 -07002121 /* We can inline 32-bit (sometimes) or 16-bit (usually) */
2122 bool is_16 = ins->alu.reg_mode == midgard_reg_mode_16;
2123 bool is_32 = ins->alu.reg_mode == midgard_reg_mode_32;
2124
2125 if (!(is_16 || is_32))
2126 continue;
2127
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002128 /* src1 cannot be an inline constant due to encoding
2129 * restrictions. So, if possible we try to flip the arguments
2130 * in that case */
2131
2132 int op = ins->alu.op;
2133
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002134 if (ins->src[0] == SSA_FIXED_REGISTER(REGISTER_CONSTANT)) {
Alyssa Rosenzweig62a5ee32019-07-26 14:25:25 -07002135 bool flip = alu_opcode_props[op].props & OP_COMMUTES;
2136
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002137 switch (op) {
Alyssa Rosenzweig62a5ee32019-07-26 14:25:25 -07002138 /* Conditionals can be inverted */
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002139 case midgard_alu_op_flt:
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002140 case midgard_alu_op_ilt:
Alyssa Rosenzweig62a5ee32019-07-26 14:25:25 -07002141 case midgard_alu_op_fle:
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002142 case midgard_alu_op_ile:
Alyssa Rosenzweig62a5ee32019-07-26 14:25:25 -07002143 ins->alu.op = mir_contrapositive(ins->alu.op);
2144 ins->invert = true;
2145 flip = true;
2146 break;
2147
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002148 case midgard_alu_op_fcsel:
2149 case midgard_alu_op_icsel:
Alyssa Rosenzweig1f345bc2019-04-24 01:15:15 +00002150 DBG("Missed non-commutative flip (%s)\n", alu_opcode_props[op].name);
Alyssa Rosenzweigbb1aff32019-04-24 02:18:28 +00002151 default:
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002152 break;
Alyssa Rosenzweigbb1aff32019-04-24 02:18:28 +00002153 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002154
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04002155 if (flip)
2156 mir_flip(ins);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002157 }
2158
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002159 if (ins->src[1] == SSA_FIXED_REGISTER(REGISTER_CONSTANT)) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002160 /* Extract the source information */
2161
2162 midgard_vector_alu_src *src;
2163 int q = ins->alu.src2;
2164 midgard_vector_alu_src *m = (midgard_vector_alu_src *) &q;
2165 src = m;
2166
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04002167 /* Component is from the swizzle. Take a nonzero component */
2168 assert(ins->mask);
2169 unsigned first_comp = ffs(ins->mask) - 1;
2170 unsigned component = ins->swizzle[1][first_comp];
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002171
2172 /* Scale constant appropriately, if we can legally */
2173 uint16_t scaled_constant = 0;
2174
Boris Brezillon15c92d12020-01-20 15:00:57 +01002175 if (is_16) {
2176 scaled_constant = ins->constants.u16[component];
2177 } else if (midgard_is_integer_op(op)) {
2178 scaled_constant = ins->constants.u32[component];
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002179
2180 /* Constant overflow after resize */
Boris Brezillon15c92d12020-01-20 15:00:57 +01002181 if (scaled_constant != ins->constants.u32[component])
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002182 continue;
2183 } else {
Boris Brezillon15c92d12020-01-20 15:00:57 +01002184 float original = ins->constants.f32[component];
Alyssa Rosenzweig39786142019-04-28 15:46:47 +00002185 scaled_constant = _mesa_float_to_half(original);
2186
2187 /* Check for loss of precision. If this is
2188 * mediump, we don't care, but for a highp
2189 * shader, we need to pay attention. NIR
2190 * doesn't yet tell us which mode we're in!
2191 * Practically this prevents most constants
2192 * from being inlined, sadly. */
2193
2194 float fp32 = _mesa_half_to_float(scaled_constant);
2195
2196 if (fp32 != original)
2197 continue;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002198 }
2199
2200 /* We don't know how to handle these with a constant */
2201
Alyssa Rosenzweigc45487b2019-07-26 11:52:30 -07002202 if (mir_nontrivial_source2_mod_simple(ins) || src->rep_low || src->rep_high) {
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +01002203 DBG("Bailing inline constant...\n");
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002204 continue;
2205 }
2206
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04002207 /* Make sure that the constant is not itself a vector
2208 * by checking if all accessed values are the same. */
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002209
Boris Brezillon15c92d12020-01-20 15:00:57 +01002210 const midgard_constants *cons = &ins->constants;
2211 uint32_t value = is_16 ? cons->u16[component] : cons->u32[component];
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002212
2213 bool is_vector = false;
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -07002214 unsigned mask = effective_writemask(&ins->alu, ins->mask);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002215
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04002216 for (unsigned c = 0; c < MIR_VEC_COMPONENTS; ++c) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002217 /* We only care if this component is actually used */
2218 if (!(mask & (1 << c)))
2219 continue;
2220
Boris Brezillon15c92d12020-01-20 15:00:57 +01002221 uint32_t test = is_16 ?
2222 cons->u16[ins->swizzle[1][c]] :
2223 cons->u32[ins->swizzle[1][c]];
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002224
2225 if (test != value) {
2226 is_vector = true;
2227 break;
2228 }
2229 }
2230
2231 if (is_vector)
2232 continue;
2233
2234 /* Get rid of the embedded constant */
2235 ins->has_constants = false;
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002236 ins->src[1] = ~0;
2237 ins->has_inline_constant = true;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002238 ins->inline_constant = scaled_constant;
2239 }
2240 }
2241}
2242
Alyssa Rosenzweigae20bee2019-06-06 11:19:13 -07002243/* Dead code elimination for branches at the end of a block - only one branch
2244 * per block is legal semantically */
2245
2246static void
2247midgard_opt_cull_dead_branch(compiler_context *ctx, midgard_block *block)
2248{
2249 bool branched = false;
2250
2251 mir_foreach_instr_in_block_safe(block, ins) {
2252 if (!midgard_is_branch_unit(ins->unit)) continue;
2253
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07002254 if (branched)
Alyssa Rosenzweigae20bee2019-06-06 11:19:13 -07002255 mir_remove_instruction(ins);
Alyssa Rosenzweigae20bee2019-06-06 11:19:13 -07002256
2257 branched = true;
2258 }
2259}
2260
Alyssa Rosenzweig4a03d372019-05-23 03:01:32 +00002261/* fmov.pos is an idiom for fpos. Propoagate the .pos up to the source, so then
2262 * the move can be propagated away entirely */
2263
2264static bool
Alyssa Rosenzweig67804812019-06-05 15:17:45 -07002265mir_compose_float_outmod(midgard_outmod_float *outmod, midgard_outmod_float comp)
Alyssa Rosenzweig4a03d372019-05-23 03:01:32 +00002266{
2267 /* Nothing to do */
2268 if (comp == midgard_outmod_none)
2269 return true;
2270
2271 if (*outmod == midgard_outmod_none) {
2272 *outmod = comp;
2273 return true;
2274 }
2275
2276 /* TODO: Compose rules */
2277 return false;
2278}
2279
2280static bool
2281midgard_opt_pos_propagate(compiler_context *ctx, midgard_block *block)
2282{
2283 bool progress = false;
2284
2285 mir_foreach_instr_in_block_safe(block, ins) {
2286 if (ins->type != TAG_ALU_4) continue;
2287 if (ins->alu.op != midgard_alu_op_fmov) continue;
2288 if (ins->alu.outmod != midgard_outmod_pos) continue;
2289
2290 /* TODO: Registers? */
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002291 unsigned src = ins->src[1];
Alyssa Rosenzweig9beb3392019-07-26 11:30:06 -07002292 if (src & IS_REG) continue;
Alyssa Rosenzweig4a03d372019-05-23 03:01:32 +00002293
2294 /* There might be a source modifier, too */
2295 if (mir_nontrivial_source2_mod(ins)) continue;
2296
2297 /* Backpropagate the modifier */
2298 mir_foreach_instr_in_block_from_rev(block, v, mir_prev_op(ins)) {
2299 if (v->type != TAG_ALU_4) continue;
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002300 if (v->dest != src) continue;
Alyssa Rosenzweig4a03d372019-05-23 03:01:32 +00002301
Alyssa Rosenzweig67804812019-06-05 15:17:45 -07002302 /* Can we even take a float outmod? */
2303 if (midgard_is_integer_out_op(v->alu.op)) continue;
2304
2305 midgard_outmod_float temp = v->alu.outmod;
2306 progress |= mir_compose_float_outmod(&temp, ins->alu.outmod);
Alyssa Rosenzweig4a03d372019-05-23 03:01:32 +00002307
2308 /* Throw in the towel.. */
2309 if (!progress) break;
2310
2311 /* Otherwise, transfer the modifier */
2312 v->alu.outmod = temp;
2313 ins->alu.outmod = midgard_outmod_none;
2314
2315 break;
2316 }
2317 }
2318
2319 return progress;
2320}
2321
Alyssa Rosenzweig5bc62af2020-01-02 12:27:59 -05002322static unsigned
Alyssa Rosenzweig60396342019-11-23 16:08:02 -05002323emit_fragment_epilogue(compiler_context *ctx, unsigned rt)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002324{
Alyssa Rosenzweig02f503e2019-12-30 18:53:04 -05002325 /* Loop to ourselves */
2326
2327 struct midgard_instruction ins = v_branch(false, false);
2328 ins.writeout = true;
2329 ins.branch.target_block = ctx->block_count - 1;
Boris Brezillon15c92d12020-01-20 15:00:57 +01002330 ins.constants.u32[0] = rt * 0x100;
Alyssa Rosenzweig02f503e2019-12-30 18:53:04 -05002331 emit_mir_instruction(ctx, ins);
2332
Alyssa Rosenzweig3448b262019-12-03 10:37:01 -05002333 ctx->current_block->epilogue = true;
Alyssa Rosenzweig60396342019-11-23 16:08:02 -05002334 schedule_barrier(ctx);
Alyssa Rosenzweig5bc62af2020-01-02 12:27:59 -05002335 return ins.branch.target_block;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002336}
2337
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002338static midgard_block *
2339emit_block(compiler_context *ctx, nir_block *block)
2340{
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002341 midgard_block *this_block = ctx->after_block;
2342 ctx->after_block = NULL;
2343
2344 if (!this_block)
Alyssa Rosenzweigaeeeef12019-08-15 08:11:10 -07002345 this_block = create_empty_block(ctx);
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002346
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002347 list_addtail(&this_block->link, &ctx->blocks);
2348
2349 this_block->is_scheduled = false;
2350 ++ctx->block_count;
2351
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002352 /* Set up current block */
2353 list_inithead(&this_block->instructions);
2354 ctx->current_block = this_block;
2355
2356 nir_foreach_instr(instr, block) {
2357 emit_instr(ctx, instr);
2358 ++ctx->instruction_count;
2359 }
2360
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002361 return this_block;
2362}
2363
2364static midgard_block *emit_cf_list(struct compiler_context *ctx, struct exec_list *list);
2365
2366static void
2367emit_if(struct compiler_context *ctx, nir_if *nif)
2368{
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002369 midgard_block *before_block = ctx->current_block;
2370
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002371 /* Speculatively emit the branch, but we can't fill it in until later */
2372 EMIT(branch, true, true);
2373 midgard_instruction *then_branch = mir_last_in_block(ctx->current_block);
Alyssa Rosenzweigd6e4e362019-08-26 13:59:29 -07002374 then_branch->src[0] = nir_src_index(ctx, &nif->condition);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002375
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002376 /* Emit the two subblocks. */
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002377 midgard_block *then_block = emit_cf_list(ctx, &nif->then_list);
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002378 midgard_block *end_then_block = ctx->current_block;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002379
2380 /* Emit a jump from the end of the then block to the end of the else */
2381 EMIT(branch, false, false);
2382 midgard_instruction *then_exit = mir_last_in_block(ctx->current_block);
2383
2384 /* Emit second block, and check if it's empty */
2385
2386 int else_idx = ctx->block_count;
2387 int count_in = ctx->instruction_count;
2388 midgard_block *else_block = emit_cf_list(ctx, &nif->else_list);
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002389 midgard_block *end_else_block = ctx->current_block;
Alyssa Rosenzweig2c747092019-02-17 05:14:24 +00002390 int after_else_idx = ctx->block_count;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002391
2392 /* Now that we have the subblocks emitted, fix up the branches */
2393
2394 assert(then_block);
2395 assert(else_block);
2396
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002397 if (ctx->instruction_count == count_in) {
2398 /* The else block is empty, so don't emit an exit jump */
2399 mir_remove_instruction(then_exit);
Alyssa Rosenzweig2c747092019-02-17 05:14:24 +00002400 then_branch->branch.target_block = after_else_idx;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002401 } else {
2402 then_branch->branch.target_block = else_idx;
Alyssa Rosenzweig2c747092019-02-17 05:14:24 +00002403 then_exit->branch.target_block = after_else_idx;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002404 }
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002405
2406 /* Wire up the successors */
2407
Alyssa Rosenzweigaeeeef12019-08-15 08:11:10 -07002408 ctx->after_block = create_empty_block(ctx);
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002409
2410 midgard_block_add_successor(before_block, then_block);
2411 midgard_block_add_successor(before_block, else_block);
2412
2413 midgard_block_add_successor(end_then_block, ctx->after_block);
2414 midgard_block_add_successor(end_else_block, ctx->after_block);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002415}
2416
2417static void
2418emit_loop(struct compiler_context *ctx, nir_loop *nloop)
2419{
2420 /* Remember where we are */
2421 midgard_block *start_block = ctx->current_block;
2422
Alyssa Rosenzweig521ac6e2019-04-21 16:22:44 +00002423 /* Allocate a loop number, growing the current inner loop depth */
2424 int loop_idx = ++ctx->current_loop_depth;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002425
2426 /* Get index from before the body so we can loop back later */
2427 int start_idx = ctx->block_count;
2428
2429 /* Emit the body itself */
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002430 midgard_block *loop_block = emit_cf_list(ctx, &nloop->body);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002431
2432 /* Branch back to loop back */
2433 struct midgard_instruction br_back = v_branch(false, false);
2434 br_back.branch.target_block = start_idx;
2435 emit_mir_instruction(ctx, br_back);
2436
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002437 /* Mark down that branch in the graph. */
2438 midgard_block_add_successor(start_block, loop_block);
2439 midgard_block_add_successor(ctx->current_block, loop_block);
Alyssa Rosenzweigc0fb2602019-04-21 03:29:47 +00002440
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002441 /* Find the index of the block about to follow us (note: we don't add
2442 * one; blocks are 0-indexed so we get a fencepost problem) */
2443 int break_block_idx = ctx->block_count;
2444
2445 /* Fix up the break statements we emitted to point to the right place,
2446 * now that we can allocate a block number for them */
Alyssa Rosenzweigaeeeef12019-08-15 08:11:10 -07002447 ctx->after_block = create_empty_block(ctx);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002448
2449 list_for_each_entry_from(struct midgard_block, block, start_block, &ctx->blocks, link) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002450 mir_foreach_instr_in_block(block, ins) {
2451 if (ins->type != TAG_ALU_4) continue;
2452 if (!ins->compact_branch) continue;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002453
2454 /* We found a branch -- check the type to see if we need to do anything */
2455 if (ins->branch.target_type != TARGET_BREAK) continue;
2456
2457 /* It's a break! Check if it's our break */
2458 if (ins->branch.target_break != loop_idx) continue;
2459
2460 /* Okay, cool, we're breaking out of this loop.
2461 * Rewrite from a break to a goto */
2462
2463 ins->branch.target_type = TARGET_GOTO;
2464 ins->branch.target_block = break_block_idx;
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002465
2466 midgard_block_add_successor(block, ctx->after_block);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002467 }
2468 }
Alyssa Rosenzweig521ac6e2019-04-21 16:22:44 +00002469
2470 /* Now that we've finished emitting the loop, free up the depth again
2471 * so we play nice with recursion amid nested loops */
2472 --ctx->current_loop_depth;
Alyssa Rosenzweig7ad65162019-07-09 11:10:49 -07002473
2474 /* Dump loop stats */
2475 ++ctx->loop_count;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002476}
2477
2478static midgard_block *
2479emit_cf_list(struct compiler_context *ctx, struct exec_list *list)
2480{
2481 midgard_block *start_block = NULL;
2482
2483 foreach_list_typed(nir_cf_node, node, node, list) {
2484 switch (node->type) {
2485 case nir_cf_node_block: {
2486 midgard_block *block = emit_block(ctx, nir_cf_node_as_block(node));
2487
2488 if (!start_block)
2489 start_block = block;
2490
2491 break;
2492 }
2493
2494 case nir_cf_node_if:
2495 emit_if(ctx, nir_cf_node_as_if(node));
2496 break;
2497
2498 case nir_cf_node_loop:
2499 emit_loop(ctx, nir_cf_node_as_loop(node));
2500 break;
2501
2502 case nir_cf_node_function:
2503 assert(0);
2504 break;
2505 }
2506 }
2507
2508 return start_block;
2509}
2510
Alyssa Rosenzweig5e55c112019-02-17 03:35:03 +00002511/* Due to lookahead, we need to report the first tag executed in the command
2512 * stream and in branch targets. An initial block might be empty, so iterate
2513 * until we find one that 'works' */
2514
2515static unsigned
2516midgard_get_first_tag_from_block(compiler_context *ctx, unsigned block_idx)
2517{
2518 midgard_block *initial_block = mir_get_block(ctx, block_idx);
2519
2520 unsigned first_tag = 0;
2521
Alyssa Rosenzweig73c40d62019-07-31 15:49:30 -07002522 mir_foreach_block_from(ctx, initial_block, v) {
Alyssa Rosenzweig45ac8ea2019-11-04 10:32:49 -05002523 if (v->quadword_count) {
2524 midgard_bundle *initial_bundle =
2525 util_dynarray_element(&v->bundles, midgard_bundle, 0);
Alyssa Rosenzweig5e55c112019-02-17 03:35:03 +00002526
Alyssa Rosenzweig5e55c112019-02-17 03:35:03 +00002527 first_tag = initial_bundle->tag;
2528 break;
2529 }
Alyssa Rosenzweig73c40d62019-07-31 15:49:30 -07002530 }
Alyssa Rosenzweig5e55c112019-02-17 03:35:03 +00002531
Alyssa Rosenzweig5e55c112019-02-17 03:35:03 +00002532 return first_tag;
2533}
2534
Alyssa Rosenzweig67fe2af2019-12-27 16:01:34 -05002535static unsigned
2536pan_format_from_nir_base(nir_alu_type base)
2537{
2538 switch (base) {
2539 case nir_type_int:
2540 return MALI_FORMAT_SINT;
2541 case nir_type_uint:
2542 case nir_type_bool:
2543 return MALI_FORMAT_UINT;
2544 case nir_type_float:
2545 return MALI_CHANNEL_FLOAT;
2546 default:
2547 unreachable("Invalid base");
2548 }
2549}
2550
2551static unsigned
2552pan_format_from_nir_size(nir_alu_type base, unsigned size)
2553{
2554 if (base == nir_type_float) {
2555 switch (size) {
2556 case 16: return MALI_FORMAT_SINT;
2557 case 32: return MALI_FORMAT_UNORM;
2558 default:
2559 unreachable("Invalid float size for format");
2560 }
2561 } else {
2562 switch (size) {
2563 case 1:
2564 case 8: return MALI_CHANNEL_8;
2565 case 16: return MALI_CHANNEL_16;
2566 case 32: return MALI_CHANNEL_32;
2567 default:
2568 unreachable("Invalid int size for format");
2569 }
2570 }
2571}
2572
2573static enum mali_format
2574pan_format_from_glsl(const struct glsl_type *type)
2575{
2576 enum glsl_base_type glsl_base = glsl_get_base_type(glsl_without_array(type));
2577 nir_alu_type t = nir_get_nir_type_for_glsl_base_type(glsl_base);
2578
2579 unsigned base = nir_alu_type_get_base_type(t);
2580 unsigned size = nir_alu_type_get_type_size(t);
2581
2582 return pan_format_from_nir_base(base) |
2583 pan_format_from_nir_size(base, size) |
2584 MALI_NR_CHANNELS(4);
2585}
2586
Alyssa Rosenzweig5bc62af2020-01-02 12:27:59 -05002587/* For each fragment writeout instruction, generate a writeout loop to
2588 * associate with it */
2589
2590static void
2591mir_add_writeout_loops(compiler_context *ctx)
2592{
2593 for (unsigned rt = 0; rt < ARRAY_SIZE(ctx->writeout_branch); ++rt) {
2594 midgard_instruction *br = ctx->writeout_branch[rt];
2595 if (!br) continue;
2596
2597 unsigned popped = br->branch.target_block;
2598 midgard_block_add_successor(mir_get_block(ctx, popped - 1), ctx->current_block);
2599 br->branch.target_block = emit_fragment_epilogue(ctx, rt);
2600
2601 /* If we have more RTs, we'll need to restore back after our
2602 * loop terminates */
2603
2604 if ((rt + 1) < ARRAY_SIZE(ctx->writeout_branch) && ctx->writeout_branch[rt + 1]) {
2605 midgard_instruction uncond = v_branch(false, false);
2606 uncond.branch.target_block = popped;
2607 emit_mir_instruction(ctx, uncond);
2608 midgard_block_add_successor(ctx->current_block, mir_get_block(ctx, popped));
2609 schedule_barrier(ctx);
2610 } else {
2611 /* We're last, so we can terminate here */
2612 br->last_writeout = true;
2613 }
2614 }
2615}
2616
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002617int
Alyssa Rosenzweig35418f62019-12-16 18:05:21 -05002618midgard_compile_shader_nir(nir_shader *nir, midgard_program *program, bool is_blend, unsigned blend_rt, unsigned gpu_id, bool shaderdb)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002619{
2620 struct util_dynarray *compiled = &program->compiled;
2621
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002622 midgard_debug = debug_get_option_midgard_debug();
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +01002623
Alyssa Rosenzweig4fa09322019-08-15 08:10:46 -07002624 /* TODO: Bound against what? */
2625 compiler_context *ctx = rzalloc(NULL, compiler_context);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002626
Alyssa Rosenzweig4fa09322019-08-15 08:10:46 -07002627 ctx->nir = nir;
Alyssa Rosenzweig4fa09322019-08-15 08:10:46 -07002628 ctx->stage = nir->info.stage;
2629 ctx->is_blend = is_blend;
2630 ctx->alpha_ref = program->alpha_ref;
Alyssa Rosenzweiga2d55032019-11-23 21:44:16 -05002631 ctx->blend_rt = blend_rt;
Alyssa Rosenzweigfcf144d2019-11-19 20:55:42 -05002632 ctx->quirks = midgard_get_quirks(gpu_id);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002633
Alyssa Rosenzweig3174bc92019-07-16 14:10:08 -07002634 /* Start off with a safe cutoff, allowing usage of all 16 work
2635 * registers. Later, we'll promote uniform reads to uniform registers
2636 * if we determine it is beneficial to do so */
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002637 ctx->uniform_cutoff = 8;
2638
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002639 /* Initialize at a global (not block) level hash tables */
2640
2641 ctx->ssa_constants = _mesa_hash_table_u64_create(NULL);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002642 ctx->hash_to_temp = _mesa_hash_table_u64_create(NULL);
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00002643 ctx->sysval_to_id = _mesa_hash_table_u64_create(NULL);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002644
Alyssa Rosenzweigb98955e2019-03-15 23:25:55 +00002645 /* Record the varying mapping for the command stream's bookkeeping */
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002646
Alyssa Rosenzweigb98955e2019-03-15 23:25:55 +00002647 struct exec_list *varyings =
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002648 ctx->stage == MESA_SHADER_VERTEX ? &nir->outputs : &nir->inputs;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002649
Alyssa Rosenzweigde8d49a2019-06-06 09:15:26 -07002650 unsigned max_varying = 0;
Alyssa Rosenzweigb98955e2019-03-15 23:25:55 +00002651 nir_foreach_variable(var, varyings) {
2652 unsigned loc = var->data.driver_location;
Alyssa Rosenzweig1f7b3882019-04-20 23:39:29 +00002653 unsigned sz = glsl_type_size(var->type, FALSE);
2654
Boris Brezillon749c5442019-06-13 14:56:02 +02002655 for (int c = 0; c < sz; ++c) {
2656 program->varyings[loc + c] = var->data.location + c;
Alyssa Rosenzweig67fe2af2019-12-27 16:01:34 -05002657 program->varying_type[loc + c] = pan_format_from_glsl(var->type);
Boris Brezillon749c5442019-06-13 14:56:02 +02002658 max_varying = MAX2(max_varying, loc + c);
Alyssa Rosenzweig1f7b3882019-04-20 23:39:29 +00002659 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002660 }
2661
Alyssa Rosenzweigde8d49a2019-06-06 09:15:26 -07002662 /* Lower gl_Position pre-optimisation, but after lowering vars to ssa
2663 * (so we don't accidentally duplicate the epilogue since mesa/st has
2664 * messed with our I/O quite a bit already) */
2665
2666 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
Alyssa Rosenzweig1e2cb3e2019-04-07 16:37:28 +00002667
Alyssa Rosenzweigbb483a92019-07-10 11:30:00 -07002668 if (ctx->stage == MESA_SHADER_VERTEX) {
Alyssa Rosenzweig1e2cb3e2019-04-07 16:37:28 +00002669 NIR_PASS_V(nir, nir_lower_viewport_transform);
Alyssa Rosenzweig20237162019-08-26 12:14:11 -07002670 NIR_PASS_V(nir, nir_lower_point_size, 1.0, 1024.0);
Alyssa Rosenzweigbb483a92019-07-10 11:30:00 -07002671 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002672
2673 NIR_PASS_V(nir, nir_lower_var_copies);
2674 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
2675 NIR_PASS_V(nir, nir_split_var_copies);
2676 NIR_PASS_V(nir, nir_lower_var_copies);
2677 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
2678 NIR_PASS_V(nir, nir_lower_var_copies);
2679 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00002680
Eric Anholt771adff2019-04-08 16:32:01 -07002681 NIR_PASS_V(nir, nir_lower_io, nir_var_all, glsl_type_size, 0);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002682
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002683 /* Optimisation passes */
2684
Alyssa Rosenzweigbda2bb32019-11-21 08:45:27 -05002685 optimise_nir(nir, ctx->quirks);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002686
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002687 if (midgard_debug & MIDGARD_DBG_SHADERS) {
2688 nir_print_shader(nir, stdout);
2689 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002690
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00002691 /* Assign sysvals and counts, now that we're sure
2692 * (post-optimisation) */
2693
2694 midgard_nir_assign_sysvals(ctx, nir);
2695
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002696 program->uniform_count = nir->num_uniforms;
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00002697 program->sysval_count = ctx->sysval_count;
2698 memcpy(program->sysvals, ctx->sysvals, sizeof(ctx->sysvals[0]) * ctx->sysval_count);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002699
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002700 nir_foreach_function(func, nir) {
2701 if (!func->impl)
2702 continue;
2703
2704 list_inithead(&ctx->blocks);
2705 ctx->block_count = 0;
2706 ctx->func = func;
2707
2708 emit_cf_list(ctx, &func->impl->body);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002709 break; /* TODO: Multi-function shaders */
2710 }
2711
2712 util_dynarray_init(compiled, NULL);
2713
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07002714 /* Per-block lowering before opts */
2715
2716 mir_foreach_block(ctx, block) {
2717 inline_alu_constants(ctx, block);
2718 midgard_opt_promote_fmov(ctx, block);
2719 embedded_to_inline_constant(ctx, block);
2720 }
Alyssa Rosenzweig4d995e02019-04-22 04:58:53 +00002721 /* MIR-level optimizations */
Alyssa Rosenzweig84f09ff2019-04-21 16:11:11 +00002722
Alyssa Rosenzweig4d995e02019-04-22 04:58:53 +00002723 bool progress = false;
2724
2725 do {
2726 progress = false;
2727
2728 mir_foreach_block(ctx, block) {
Alyssa Rosenzweig4a03d372019-05-23 03:01:32 +00002729 progress |= midgard_opt_pos_propagate(ctx, block);
Alyssa Rosenzweig4d995e02019-04-22 04:58:53 +00002730 progress |= midgard_opt_copy_prop(ctx, block);
2731 progress |= midgard_opt_dead_code_eliminate(ctx, block);
Alyssa Rosenzweig9ce75822019-07-24 15:37:24 -07002732 progress |= midgard_opt_combine_projection(ctx, block);
2733 progress |= midgard_opt_varying_projection(ctx, block);
Alyssa Rosenzweig620c2712019-07-26 13:14:55 -07002734 progress |= midgard_opt_not_propagate(ctx, block);
Alyssa Rosenzweigd066ca352019-07-26 13:32:54 -07002735 progress |= midgard_opt_fuse_src_invert(ctx, block);
Alyssa Rosenzweigb821e1b2019-07-26 13:08:54 -07002736 progress |= midgard_opt_fuse_dest_invert(ctx, block);
Alyssa Rosenzweigc20063a2019-09-28 12:39:15 -04002737 progress |= midgard_opt_csel_invert(ctx, block);
Afonso Bordado3e1e4ad2019-12-10 13:18:00 +00002738 progress |= midgard_opt_drop_cmp_invert(ctx, block);
Afonso Bordado525cbe82019-12-27 17:09:51 +00002739 progress |= midgard_opt_invert_branch(ctx, block);
Alyssa Rosenzweig4d995e02019-04-22 04:58:53 +00002740 }
2741 } while (progress);
Alyssa Rosenzweig84f09ff2019-04-21 16:11:11 +00002742
Alyssa Rosenzweig159abd52019-07-26 11:15:31 -07002743 mir_foreach_block(ctx, block) {
2744 midgard_lower_invert(ctx, block);
Alyssa Rosenzweig8f887322019-07-29 15:11:12 -07002745 midgard_lower_derivatives(ctx, block);
Alyssa Rosenzweig159abd52019-07-26 11:15:31 -07002746 }
2747
Alyssa Rosenzweigae20bee2019-06-06 11:19:13 -07002748 /* Nested control-flow can result in dead branches at the end of the
2749 * block. This messes with our analysis and is just dead code, so cull
2750 * them */
2751 mir_foreach_block(ctx, block) {
2752 midgard_opt_cull_dead_branch(ctx, block);
2753 }
2754
Alyssa Rosenzweig159abd52019-07-26 11:15:31 -07002755 /* Ensure we were lowered */
2756 mir_foreach_instr_global(ctx, ins) {
2757 assert(!ins->invert);
2758 }
2759
Alyssa Rosenzweig5bc62af2020-01-02 12:27:59 -05002760 if (ctx->stage == MESA_SHADER_FRAGMENT)
2761 mir_add_writeout_loops(ctx);
2762
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002763 /* Schedule! */
Robert Foss62adb652020-01-15 01:14:16 +01002764 midgard_schedule_program(ctx);
Alyssa Rosenzweig9dc3b182019-12-06 09:32:38 -05002765 mir_ra(ctx);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002766
2767 /* Now that all the bundles are scheduled and we can calculate block
2768 * sizes, emit actual branch instructions rather than placeholders */
2769
2770 int br_block_idx = 0;
2771
2772 mir_foreach_block(ctx, block) {
2773 util_dynarray_foreach(&block->bundles, midgard_bundle, bundle) {
2774 for (int c = 0; c < bundle->instruction_count; ++c) {
Alyssa Rosenzweig3c7abbf2019-05-22 04:33:21 +00002775 midgard_instruction *ins = bundle->instructions[c];
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002776
Alyssa Rosenzweig5abb7b52019-02-17 22:09:09 +00002777 if (!midgard_is_branch_unit(ins->unit)) continue;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002778
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00002779 /* Parse some basic branch info */
2780 bool is_compact = ins->unit == ALU_ENAB_BR_COMPACT;
2781 bool is_conditional = ins->branch.conditional;
2782 bool is_inverted = ins->branch.invert_conditional;
2783 bool is_discard = ins->branch.target_type == TARGET_DISCARD;
Alyssa Rosenzweig02f503e2019-12-30 18:53:04 -05002784 bool is_writeout = ins->writeout;
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00002785
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002786 /* Determine the block we're jumping to */
2787 int target_number = ins->branch.target_block;
2788
Alyssa Rosenzweig3c7abbf2019-05-22 04:33:21 +00002789 /* Report the destination tag */
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00002790 int dest_tag = is_discard ? 0 : midgard_get_first_tag_from_block(ctx, target_number);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002791
Alyssa Rosenzweig3c7abbf2019-05-22 04:33:21 +00002792 /* Count up the number of quadwords we're
2793 * jumping over = number of quadwords until
2794 * (br_block_idx, target_number) */
2795
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002796 int quadword_offset = 0;
2797
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00002798 if (is_discard) {
Alyssa Rosenzweig7f75b2b2019-07-30 17:07:25 -07002799 /* Ignored */
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00002800 } else if (target_number > br_block_idx) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002801 /* Jump forward */
2802
2803 for (int idx = br_block_idx + 1; idx < target_number; ++idx) {
2804 midgard_block *blk = mir_get_block(ctx, idx);
2805 assert(blk);
2806
2807 quadword_offset += blk->quadword_count;
2808 }
2809 } else {
2810 /* Jump backwards */
2811
2812 for (int idx = br_block_idx; idx >= target_number; --idx) {
2813 midgard_block *blk = mir_get_block(ctx, idx);
2814 assert(blk);
2815
2816 quadword_offset -= blk->quadword_count;
2817 }
2818 }
2819
Alyssa Rosenzweig5abb7b52019-02-17 22:09:09 +00002820 /* Unconditional extended branches (far jumps)
2821 * have issues, so we always use a conditional
2822 * branch, setting the condition to always for
2823 * unconditional. For compact unconditional
2824 * branches, cond isn't used so it doesn't
2825 * matter what we pick. */
2826
2827 midgard_condition cond =
2828 !is_conditional ? midgard_condition_always :
2829 is_inverted ? midgard_condition_false :
2830 midgard_condition_true;
2831
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00002832 midgard_jmp_writeout_op op =
2833 is_discard ? midgard_jmp_writeout_op_discard :
Alyssa Rosenzweig02f503e2019-12-30 18:53:04 -05002834 is_writeout ? midgard_jmp_writeout_op_writeout :
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00002835 (is_compact && !is_conditional) ? midgard_jmp_writeout_op_branch_uncond :
2836 midgard_jmp_writeout_op_branch_cond;
2837
Alyssa Rosenzweig5abb7b52019-02-17 22:09:09 +00002838 if (!is_compact) {
2839 midgard_branch_extended branch =
2840 midgard_create_branch_extended(
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00002841 cond, op,
Alyssa Rosenzweig5abb7b52019-02-17 22:09:09 +00002842 dest_tag,
2843 quadword_offset);
2844
2845 memcpy(&ins->branch_extended, &branch, sizeof(branch));
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00002846 } else if (is_conditional || is_discard) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002847 midgard_branch_cond branch = {
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00002848 .op = op,
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002849 .dest_tag = dest_tag,
2850 .offset = quadword_offset,
Alyssa Rosenzweig5abb7b52019-02-17 22:09:09 +00002851 .cond = cond
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002852 };
2853
Alyssa Rosenzweig5abb7b52019-02-17 22:09:09 +00002854 assert(branch.offset == quadword_offset);
2855
2856 memcpy(&ins->br_compact, &branch, sizeof(branch));
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002857 } else {
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00002858 assert(op == midgard_jmp_writeout_op_branch_uncond);
2859
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002860 midgard_branch_uncond branch = {
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00002861 .op = op,
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002862 .dest_tag = dest_tag,
2863 .offset = quadword_offset,
2864 .unknown = 1
2865 };
2866
Alyssa Rosenzweig5abb7b52019-02-17 22:09:09 +00002867 assert(branch.offset == quadword_offset);
2868
2869 memcpy(&ins->br_compact, &branch, sizeof(branch));
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002870 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002871 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002872 }
2873
2874 ++br_block_idx;
2875 }
2876
2877 /* Emit flat binary from the instruction arrays. Iterate each block in
2878 * sequence. Save instruction boundaries such that lookahead tags can
2879 * be assigned easily */
2880
2881 /* Cache _all_ bundles in source order for lookahead across failed branches */
2882
2883 int bundle_count = 0;
2884 mir_foreach_block(ctx, block) {
2885 bundle_count += block->bundles.size / sizeof(midgard_bundle);
2886 }
2887 midgard_bundle **source_order_bundles = malloc(sizeof(midgard_bundle *) * bundle_count);
2888 int bundle_idx = 0;
2889 mir_foreach_block(ctx, block) {
2890 util_dynarray_foreach(&block->bundles, midgard_bundle, bundle) {
2891 source_order_bundles[bundle_idx++] = bundle;
2892 }
2893 }
2894
2895 int current_bundle = 0;
2896
Alyssa Rosenzweig2a79afc2019-05-23 01:56:03 +00002897 /* Midgard prefetches instruction types, so during emission we
2898 * need to lookahead. Unless this is the last instruction, in
Alyssa Rosenzweig5bc62af2020-01-02 12:27:59 -05002899 * which we return 1. */
Alyssa Rosenzweig2a79afc2019-05-23 01:56:03 +00002900
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002901 mir_foreach_block(ctx, block) {
Alyssa Rosenzweigd3ad8d62019-06-06 11:19:44 -07002902 mir_foreach_bundle_in_block(block, bundle) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002903 int lookahead = 1;
2904
Alyssa Rosenzweig5bc62af2020-01-02 12:27:59 -05002905 if (!bundle->last_writeout && (current_bundle + 1 < bundle_count))
2906 lookahead = source_order_bundles[current_bundle + 1]->tag;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002907
2908 emit_binary_bundle(ctx, bundle, compiled, lookahead);
2909 ++current_bundle;
2910 }
2911
2912 /* TODO: Free deeper */
2913 //util_dynarray_fini(&block->instructions);
2914 }
2915
2916 free(source_order_bundles);
2917
Alyssa Rosenzweig5e55c112019-02-17 03:35:03 +00002918 /* Report the very first tag executed */
2919 program->first_tag = midgard_get_first_tag_from_block(ctx, 0);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002920
2921 /* Deal with off-by-one related to the fencepost problem */
2922 program->work_register_count = ctx->work_registers + 1;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002923 program->uniform_cutoff = ctx->uniform_cutoff;
2924
2925 program->blend_patch_offset = ctx->blend_constant_offset;
Alyssa Rosenzweigf0d00612019-07-19 16:23:52 -07002926 program->tls_size = ctx->tls_size;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002927
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002928 if (midgard_debug & MIDGARD_DBG_SHADERS)
Alyssa Rosenzweigac14fac2019-11-07 09:31:02 -05002929 disassemble_midgard(program->compiled.data, program->compiled.size, gpu_id, ctx->stage);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002930
Alyssa Rosenzweig35418f62019-12-16 18:05:21 -05002931 if (midgard_debug & MIDGARD_DBG_SHADERDB || shaderdb) {
Alyssa Rosenzweig19bceb52019-08-30 13:57:20 -07002932 unsigned nr_bundles = 0, nr_ins = 0;
Alyssa Rosenzweig138e40d2019-07-08 16:42:29 -07002933
2934 /* Count instructions and bundles */
2935
Alyssa Rosenzweig138e40d2019-07-08 16:42:29 -07002936 mir_foreach_block(ctx, block) {
2937 nr_bundles += util_dynarray_num_elements(
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002938 &block->bundles, midgard_bundle);
Alyssa Rosenzweig2d739f62019-07-09 11:16:57 -07002939
Alyssa Rosenzweig67909c82019-08-30 13:08:16 -07002940 mir_foreach_bundle_in_block(block, bun)
2941 nr_ins += bun->instruction_count;
Alyssa Rosenzweig138e40d2019-07-08 16:42:29 -07002942 }
2943
2944 /* Calculate thread count. There are certain cutoffs by
2945 * register count for thread count */
2946
2947 unsigned nr_registers = program->work_register_count;
2948
2949 unsigned nr_threads =
2950 (nr_registers <= 4) ? 4 :
2951 (nr_registers <= 8) ? 2 :
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002952 1;
Alyssa Rosenzweig138e40d2019-07-08 16:42:29 -07002953
2954 /* Dump stats */
2955
2956 fprintf(stderr, "shader%d - %s shader: "
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002957 "%u inst, %u bundles, %u quadwords, "
Alyssa Rosenzweige8dca7e2019-07-22 06:32:48 -07002958 "%u registers, %u threads, %u loops, "
Alyssa Rosenzweig1a4153b2019-08-30 17:29:17 -07002959 "%u:%u spills:fills\n",
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002960 SHADER_DB_COUNT++,
2961 gl_shader_stage_name(ctx->stage),
Alyssa Rosenzweig19bceb52019-08-30 13:57:20 -07002962 nr_ins, nr_bundles, ctx->quadword_count,
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002963 nr_registers, nr_threads,
Alyssa Rosenzweige8dca7e2019-07-22 06:32:48 -07002964 ctx->loop_count,
2965 ctx->spills, ctx->fills);
Alyssa Rosenzweig138e40d2019-07-08 16:42:29 -07002966 }
2967
Alyssa Rosenzweig4fa09322019-08-15 08:10:46 -07002968 ralloc_free(ctx);
Alyssa Rosenzweig138e40d2019-07-08 16:42:29 -07002969
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002970 return 0;
2971}