blob: 5b018d86e50328c6953b0fc80e9e50b4dfd32974 [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)
72
Alyssa Rosenzweig1f345bc2019-04-24 01:15:15 +000073static bool
74midgard_is_branch_unit(unsigned unit)
75{
76 return (unit == ALU_ENAB_BRANCH) || (unit == ALU_ENAB_BR_COMPACT);
77}
78
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -070079static midgard_block *
80create_empty_block(compiler_context *ctx)
81{
82 midgard_block *blk = rzalloc(ctx, midgard_block);
83
84 blk->predecessors = _mesa_set_create(blk,
85 _mesa_hash_pointer,
86 _mesa_key_pointer_equal);
87
88 blk->source_id = ctx->block_source_count++;
89
90 return blk;
91}
92
Alyssa Rosenzweigc0fb2602019-04-21 03:29:47 +000093static void
94midgard_block_add_successor(midgard_block *block, midgard_block *successor)
95{
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -070096 assert(block);
97 assert(successor);
98
99 /* Deduplicate */
100 for (unsigned i = 0; i < block->nr_successors; ++i) {
101 if (block->successors[i] == successor)
102 return;
103 }
104
Alyssa Rosenzweigc0fb2602019-04-21 03:29:47 +0000105 block->successors[block->nr_successors++] = successor;
106 assert(block->nr_successors <= ARRAY_SIZE(block->successors));
Alyssa Rosenzweigaeeeef12019-08-15 08:11:10 -0700107
108 /* Note the predecessor in the other direction */
109 _mesa_set_add(successor->predecessors, block);
Alyssa Rosenzweigc0fb2602019-04-21 03:29:47 +0000110}
111
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -0700112static void
113schedule_barrier(compiler_context *ctx)
114{
115 midgard_block *temp = ctx->after_block;
116 ctx->after_block = create_empty_block(ctx);
117 ctx->block_count++;
118 list_addtail(&ctx->after_block->link, &ctx->blocks);
119 list_inithead(&ctx->after_block->instructions);
120 midgard_block_add_successor(ctx->current_block, ctx->after_block);
121 ctx->current_block = ctx->after_block;
122 ctx->after_block = temp;
123}
124
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000125/* Helpers to generate midgard_instruction's using macro magic, since every
126 * driver seems to do it that way */
127
128#define EMIT(op, ...) emit_mir_instruction(ctx, v_##op(__VA_ARGS__));
Alyssa Rosenzweig56f9b472019-06-14 16:03:01 -0700129
Alyssa Rosenzweigd4bcca12019-08-02 15:25:02 -0700130#define M_LOAD_STORE(name, store) \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000131 static midgard_instruction m_##name(unsigned ssa, unsigned address) { \
132 midgard_instruction i = { \
133 .type = TAG_LOAD_STORE_4, \
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -0700134 .mask = 0xF, \
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -0700135 .dest = ~0, \
136 .src = { ~0, ~0, ~0 }, \
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -0400137 .swizzle = SWIZZLE_IDENTITY_4, \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000138 .load_store = { \
139 .op = midgard_op_##name, \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000140 .address = address \
141 } \
142 }; \
Alyssa Rosenzweigd4bcca12019-08-02 15:25:02 -0700143 \
144 if (store) \
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -0700145 i.src[0] = ssa; \
Alyssa Rosenzweigd4bcca12019-08-02 15:25:02 -0700146 else \
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -0700147 i.dest = ssa; \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000148 \
149 return i; \
150 }
151
Alyssa Rosenzweigd4bcca12019-08-02 15:25:02 -0700152#define M_LOAD(name) M_LOAD_STORE(name, false)
153#define M_STORE(name) M_LOAD_STORE(name, true)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000154
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000155/* Inputs a NIR ALU source, with modifiers attached if necessary, and outputs
156 * the corresponding Midgard source */
157
158static midgard_vector_alu_src
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700159vector_alu_modifiers(nir_alu_src *src, bool is_int, unsigned broadcast_count,
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -0700160 bool half, bool sext)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000161{
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -0400162 /* Figure out how many components there are so we can adjust.
163 * Specifically we want to broadcast the last channel so things like
164 * ball2/3 work.
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700165 */
166
Alyssa Rosenzweigc3a46e72019-10-30 16:29:28 -0400167 if (broadcast_count && src) {
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700168 uint8_t last_component = src->swizzle[broadcast_count - 1];
169
170 for (unsigned c = broadcast_count; c < NIR_MAX_VEC_COMPONENTS; ++c) {
171 src->swizzle[c] = last_component;
172 }
173 }
174
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000175 midgard_vector_alu_src alu_src = {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000176 .rep_low = 0,
177 .rep_high = 0,
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -0400178 .half = half
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000179 };
180
Alyssa Rosenzweigfcdfb672019-04-22 03:25:42 +0000181 if (is_int) {
Alyssa Rosenzweigfcdfb672019-04-22 03:25:42 +0000182 alu_src.mod = midgard_int_normal;
183
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700184 /* Sign/zero-extend if needed */
185
186 if (half) {
187 alu_src.mod = sext ?
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -0700188 midgard_int_sign_extend
189 : midgard_int_zero_extend;
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700190 }
191
Alyssa Rosenzweigfcdfb672019-04-22 03:25:42 +0000192 /* These should have been lowered away */
Alyssa Rosenzweigc3a46e72019-10-30 16:29:28 -0400193 if (src)
194 assert(!(src->abs || src->negate));
Alyssa Rosenzweigfcdfb672019-04-22 03:25:42 +0000195 } else {
Alyssa Rosenzweigc3a46e72019-10-30 16:29:28 -0400196 if (src)
197 alu_src.mod = (src->abs << 0) | (src->negate << 1);
Alyssa Rosenzweigfcdfb672019-04-22 03:25:42 +0000198 }
199
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000200 return alu_src;
201}
202
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000203/* load/store instructions have both 32-bit and 16-bit variants, depending on
204 * whether we are using vectors composed of highp or mediump. At the moment, we
205 * don't support half-floats -- this requires changes in other parts of the
206 * compiler -- therefore the 16-bit versions are commented out. */
207
Alyssa Rosenzweig74ab80b2019-05-14 04:11:36 +0000208//M_LOAD(ld_attr_16);
209M_LOAD(ld_attr_32);
210//M_LOAD(ld_vary_16);
211M_LOAD(ld_vary_32);
Alyssa Rosenzweigec2f0b52019-08-13 08:51:40 -0700212M_LOAD(ld_ubo_int4);
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -0700213M_LOAD(ld_int4);
214M_STORE(st_int4);
Alyssa Rosenzweig74ab80b2019-05-14 04:11:36 +0000215M_LOAD(ld_color_buffer_8);
216//M_STORE(st_vary_16);
217M_STORE(st_vary_32);
Alyssa Rosenzweig9ae4d362019-08-16 07:50:12 -0700218M_LOAD(ld_cubemap_coords);
Alyssa Rosenzweig7229af72019-08-06 13:47:17 -0700219M_LOAD(ld_compute_id);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000220
221static midgard_instruction
222v_alu_br_compact_cond(midgard_jmp_writeout_op op, unsigned tag, signed offset, unsigned cond)
223{
224 midgard_branch_cond branch = {
225 .op = op,
226 .dest_tag = tag,
227 .offset = offset,
228 .cond = cond
229 };
230
231 uint16_t compact;
232 memcpy(&compact, &branch, sizeof(branch));
233
234 midgard_instruction ins = {
235 .type = TAG_ALU_4,
236 .unit = ALU_ENAB_BR_COMPACT,
237 .prepacked_branch = true,
238 .compact_branch = true,
Alyssa Rosenzweig29416a82019-07-30 12:20:24 -0700239 .br_compact = compact,
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -0700240 .dest = ~0,
241 .src = { ~0, ~0, ~0 },
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000242 };
243
244 if (op == midgard_jmp_writeout_op_writeout)
245 ins.writeout = true;
246
247 return ins;
248}
249
250static midgard_instruction
251v_branch(bool conditional, bool invert)
252{
253 midgard_instruction ins = {
254 .type = TAG_ALU_4,
Alyssa Rosenzweig5abb7b52019-02-17 22:09:09 +0000255 .unit = ALU_ENAB_BRANCH,
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000256 .compact_branch = true,
257 .branch = {
258 .conditional = conditional,
259 .invert_conditional = invert
Alyssa Rosenzweig29416a82019-07-30 12:20:24 -0700260 },
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -0700261 .dest = ~0,
262 .src = { ~0, ~0, ~0 },
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000263 };
264
265 return ins;
266}
267
Alyssa Rosenzweig5abb7b52019-02-17 22:09:09 +0000268static midgard_branch_extended
269midgard_create_branch_extended( midgard_condition cond,
270 midgard_jmp_writeout_op op,
271 unsigned dest_tag,
272 signed quadword_offset)
273{
Alyssa Rosenzweig13ee87c2019-07-29 09:15:32 -0700274 /* The condition code is actually a LUT describing a function to
275 * combine multiple condition codes. However, we only support a single
276 * condition code at the moment, so we just duplicate over a bunch of
277 * times. */
278
Alyssa Rosenzweig5abb7b52019-02-17 22:09:09 +0000279 uint16_t duplicated_cond =
280 (cond << 14) |
281 (cond << 12) |
282 (cond << 10) |
283 (cond << 8) |
284 (cond << 6) |
285 (cond << 4) |
286 (cond << 2) |
287 (cond << 0);
288
289 midgard_branch_extended branch = {
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +0000290 .op = op,
Alyssa Rosenzweig5abb7b52019-02-17 22:09:09 +0000291 .dest_tag = dest_tag,
292 .offset = quadword_offset,
293 .cond = duplicated_cond
294 };
295
296 return branch;
297}
298
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000299static void
300attach_constants(compiler_context *ctx, midgard_instruction *ins, void *constants, int name)
301{
302 ins->has_constants = true;
303 memcpy(&ins->constants, constants, 16);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000304}
305
306static int
Timothy Arceri035759b2019-03-29 12:39:48 +1100307glsl_type_size(const struct glsl_type *type, bool bindless)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000308{
309 return glsl_count_attribute_slots(type, false);
310}
311
312/* Lower fdot2 to a vector multiplication followed by channel addition */
313static void
314midgard_nir_lower_fdot2_body(nir_builder *b, nir_alu_instr *alu)
315{
316 if (alu->op != nir_op_fdot2)
317 return;
318
319 b->cursor = nir_before_instr(&alu->instr);
320
321 nir_ssa_def *src0 = nir_ssa_for_alu_src(b, alu, 0);
322 nir_ssa_def *src1 = nir_ssa_for_alu_src(b, alu, 1);
323
324 nir_ssa_def *product = nir_fmul(b, src0, src1);
325
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -0700326 nir_ssa_def *sum = nir_fadd(b,
327 nir_channel(b, product, 0),
328 nir_channel(b, product, 1));
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000329
330 /* Replace the fdot2 with this sum */
331 nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa, nir_src_for_ssa(sum));
332}
333
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +0000334static int
Alyssa Rosenzweig2efa0252019-08-01 11:03:15 -0700335midgard_sysval_for_ssbo(nir_intrinsic_instr *instr)
336{
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -0700337 /* This is way too meta */
338 bool is_store = instr->intrinsic == nir_intrinsic_store_ssbo;
339 unsigned idx_idx = is_store ? 1 : 0;
340
341 nir_src index = instr->src[idx_idx];
Alyssa Rosenzweig2efa0252019-08-01 11:03:15 -0700342 assert(nir_src_is_const(index));
343 uint32_t uindex = nir_src_as_uint(index);
344
345 return PAN_SYSVAL(SSBO, uindex);
346}
347
348static int
Alyssa Rosenzweig4e07e7b2019-11-21 08:42:28 -0500349midgard_sysval_for_sampler(nir_intrinsic_instr *instr)
350{
351 /* TODO: indirect samplers !!! */
352 nir_src index = instr->src[0];
353 assert(nir_src_is_const(index));
354 uint32_t uindex = nir_src_as_uint(index);
355
356 return PAN_SYSVAL(SAMPLER, uindex);
357}
358
359static int
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +0000360midgard_nir_sysval_for_intrinsic(nir_intrinsic_instr *instr)
361{
362 switch (instr->intrinsic) {
363 case nir_intrinsic_load_viewport_scale:
364 return PAN_SYSVAL_VIEWPORT_SCALE;
365 case nir_intrinsic_load_viewport_offset:
366 return PAN_SYSVAL_VIEWPORT_OFFSET;
Alyssa Rosenzweig15954ab2019-08-06 14:07:10 -0700367 case nir_intrinsic_load_num_work_groups:
368 return PAN_SYSVAL_NUM_WORK_GROUPS;
Alyssa Rosenzweig2efa0252019-08-01 11:03:15 -0700369 case nir_intrinsic_load_ssbo:
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -0700370 case nir_intrinsic_store_ssbo:
Alyssa Rosenzweig2efa0252019-08-01 11:03:15 -0700371 return midgard_sysval_for_ssbo(instr);
Alyssa Rosenzweig4e07e7b2019-11-21 08:42:28 -0500372 case nir_intrinsic_load_sampler_lod_parameters_pan:
373 return midgard_sysval_for_sampler(instr);
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +0000374 default:
Alyssa Rosenzweig61892742019-08-21 09:15:56 -0700375 return ~0;
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +0000376 }
377}
378
Boris Brezillonbd49c8f2019-06-14 09:59:20 +0200379static int sysval_for_instr(compiler_context *ctx, nir_instr *instr,
380 unsigned *dest)
381{
382 nir_intrinsic_instr *intr;
383 nir_dest *dst = NULL;
Boris Brezillonc3558862019-06-17 22:13:04 +0200384 nir_tex_instr *tex;
Boris Brezillonbd49c8f2019-06-14 09:59:20 +0200385 int sysval = -1;
386
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -0700387 bool is_store = false;
388
Boris Brezillonbd49c8f2019-06-14 09:59:20 +0200389 switch (instr->type) {
390 case nir_instr_type_intrinsic:
391 intr = nir_instr_as_intrinsic(instr);
392 sysval = midgard_nir_sysval_for_intrinsic(intr);
393 dst = &intr->dest;
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -0700394 is_store |= intr->intrinsic == nir_intrinsic_store_ssbo;
Boris Brezillonbd49c8f2019-06-14 09:59:20 +0200395 break;
Boris Brezillonc3558862019-06-17 22:13:04 +0200396 case nir_instr_type_tex:
397 tex = nir_instr_as_tex(instr);
398 if (tex->op != nir_texop_txs)
399 break;
400
401 sysval = PAN_SYSVAL(TEXTURE_SIZE,
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -0700402 PAN_TXS_SYSVAL_ID(tex->texture_index,
403 nir_tex_instr_dest_size(tex) -
404 (tex->is_array ? 1 : 0),
405 tex->is_array));
Boris Brezillonc3558862019-06-17 22:13:04 +0200406 dst = &tex->dest;
407 break;
Boris Brezillonbd49c8f2019-06-14 09:59:20 +0200408 default:
409 break;
410 }
411
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -0700412 if (dest && dst && !is_store)
Boris Brezillonbd49c8f2019-06-14 09:59:20 +0200413 *dest = nir_dest_index(ctx, dst);
414
415 return sysval;
416}
417
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +0000418static void
419midgard_nir_assign_sysval_body(compiler_context *ctx, nir_instr *instr)
420{
Boris Brezillonbd49c8f2019-06-14 09:59:20 +0200421 int sysval;
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +0000422
Boris Brezillonbd49c8f2019-06-14 09:59:20 +0200423 sysval = sysval_for_instr(ctx, instr, NULL);
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +0000424 if (sysval < 0)
425 return;
426
427 /* We have a sysval load; check if it's already been assigned */
428
429 if (_mesa_hash_table_u64_search(ctx->sysval_to_id, sysval))
430 return;
431
432 /* It hasn't -- so assign it now! */
433
434 unsigned id = ctx->sysval_count++;
435 _mesa_hash_table_u64_insert(ctx->sysval_to_id, sysval, (void *) ((uintptr_t) id + 1));
436 ctx->sysvals[id] = sysval;
437}
438
439static void
440midgard_nir_assign_sysvals(compiler_context *ctx, nir_shader *shader)
441{
442 ctx->sysval_count = 0;
443
444 nir_foreach_function(function, shader) {
445 if (!function->impl) continue;
446
447 nir_foreach_block(block, function->impl) {
448 nir_foreach_instr_safe(instr, block) {
449 midgard_nir_assign_sysval_body(ctx, instr);
450 }
451 }
452 }
453}
454
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000455static bool
456midgard_nir_lower_fdot2(nir_shader *shader)
457{
458 bool progress = false;
459
460 nir_foreach_function(function, shader) {
461 if (!function->impl) continue;
462
463 nir_builder _b;
464 nir_builder *b = &_b;
465 nir_builder_init(b, function->impl);
466
467 nir_foreach_block(block, function->impl) {
468 nir_foreach_instr_safe(instr, block) {
469 if (instr->type != nir_instr_type_alu) continue;
470
471 nir_alu_instr *alu = nir_instr_as_alu(instr);
472 midgard_nir_lower_fdot2_body(b, alu);
473
474 progress |= true;
475 }
476 }
477
478 nir_metadata_preserve(function->impl, nir_metadata_block_index | nir_metadata_dominance);
479
480 }
481
482 return progress;
483}
484
Alyssa Rosenzweiga2f1a062019-07-08 12:40:34 -0700485/* Flushes undefined values to zero */
486
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000487static void
Alyssa Rosenzweigbda2bb32019-11-21 08:45:27 -0500488optimise_nir(nir_shader *nir, unsigned quirks)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000489{
490 bool progress;
Ian Romanickd41cdef2018-08-18 16:42:04 -0700491 unsigned lower_flrp =
492 (nir->options->lower_flrp16 ? 16 : 0) |
493 (nir->options->lower_flrp32 ? 32 : 0) |
494 (nir->options->lower_flrp64 ? 64 : 0);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000495
496 NIR_PASS(progress, nir, nir_lower_regs_to_ssa);
497 NIR_PASS(progress, nir, midgard_nir_lower_fdot2);
Rhys Perry8b98d092019-02-05 15:56:24 +0000498 NIR_PASS(progress, nir, nir_lower_idiv, nir_lower_idiv_fast);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000499
Alyssa Rosenzweig44a6c382019-08-14 08:44:40 -0700500 nir_lower_tex_options lower_tex_options = {
501 .lower_txs_lod = true,
Alyssa Rosenzweig4c43b352019-11-21 13:40:00 -0500502 .lower_txp = ~0,
503 .lower_tex_without_implicit_lod =
504 (quirks & MIDGARD_EXPLICIT_LOD),
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000505 };
506
Alyssa Rosenzweig44a6c382019-08-14 08:44:40 -0700507 NIR_PASS(progress, nir, nir_lower_tex, &lower_tex_options);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000508
Alyssa Rosenzweigbda2bb32019-11-21 08:45:27 -0500509 /* T720 is broken. */
510
511 if (quirks & MIDGARD_BROKEN_LOD)
512 NIR_PASS_V(nir, midgard_nir_lod_errata);
513
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000514 do {
515 progress = false;
516
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000517 NIR_PASS(progress, nir, nir_lower_var_copies);
518 NIR_PASS(progress, nir, nir_lower_vars_to_ssa);
519
520 NIR_PASS(progress, nir, nir_copy_prop);
521 NIR_PASS(progress, nir, nir_opt_dce);
522 NIR_PASS(progress, nir, nir_opt_dead_cf);
523 NIR_PASS(progress, nir, nir_opt_cse);
524 NIR_PASS(progress, nir, nir_opt_peephole_select, 64, false, true);
525 NIR_PASS(progress, nir, nir_opt_algebraic);
526 NIR_PASS(progress, nir, nir_opt_constant_folding);
Ian Romanickd41cdef2018-08-18 16:42:04 -0700527
528 if (lower_flrp != 0) {
Ian Romanick1f1007a2019-05-08 07:32:43 -0700529 bool lower_flrp_progress = false;
Ian Romanickd41cdef2018-08-18 16:42:04 -0700530 NIR_PASS(lower_flrp_progress,
531 nir,
532 nir_lower_flrp,
533 lower_flrp,
534 false /* always_precise */,
535 nir->options->lower_ffma);
536 if (lower_flrp_progress) {
537 NIR_PASS(progress, nir,
538 nir_opt_constant_folding);
539 progress = true;
540 }
541
542 /* Nothing should rematerialize any flrps, so we only
543 * need to do this lowering once.
544 */
545 lower_flrp = 0;
546 }
547
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000548 NIR_PASS(progress, nir, nir_opt_undef);
Alyssa Rosenzweiga2f1a062019-07-08 12:40:34 -0700549 NIR_PASS(progress, nir, nir_undef_to_zero);
550
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000551 NIR_PASS(progress, nir, nir_opt_loop_unroll,
552 nir_var_shader_in |
553 nir_var_shader_out |
554 nir_var_function_temp);
555
Alyssa Rosenzweig94029702019-06-17 11:12:51 -0700556 NIR_PASS(progress, nir, nir_opt_vectorize);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000557 } while (progress);
558
559 /* Must be run at the end to prevent creation of fsin/fcos ops */
560 NIR_PASS(progress, nir, midgard_nir_scale_trig);
561
562 do {
563 progress = false;
564
565 NIR_PASS(progress, nir, nir_opt_dce);
566 NIR_PASS(progress, nir, nir_opt_algebraic);
567 NIR_PASS(progress, nir, nir_opt_constant_folding);
568 NIR_PASS(progress, nir, nir_copy_prop);
569 } while (progress);
570
571 NIR_PASS(progress, nir, nir_opt_algebraic_late);
Alyssa Rosenzweig726f0262019-05-07 02:52:08 +0000572
573 /* We implement booleans as 32-bit 0/~0 */
574 NIR_PASS(progress, nir, nir_lower_bool_to_int32);
575
576 /* Now that booleans are lowered, we can run out late opts */
Alyssa Rosenzweigeffe6fb02019-03-25 02:49:04 +0000577 NIR_PASS(progress, nir, midgard_nir_lower_algebraic_late);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000578
Alyssa Rosenzweigeffe6fb02019-03-25 02:49:04 +0000579 /* Lower mods for float ops only. Integer ops don't support modifiers
580 * (saturate doesn't make sense on integers, neg/abs require dedicated
581 * instructions) */
582
583 NIR_PASS(progress, nir, nir_lower_to_source_mods, nir_lower_float_source_mods);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000584 NIR_PASS(progress, nir, nir_copy_prop);
585 NIR_PASS(progress, nir, nir_opt_dce);
586
587 /* Take us out of SSA */
588 NIR_PASS(progress, nir, nir_lower_locals_to_regs);
589 NIR_PASS(progress, nir, nir_convert_from_ssa, true);
590
591 /* We are a vector architecture; write combine where possible */
592 NIR_PASS(progress, nir, nir_move_vec_src_uses_to_dest);
593 NIR_PASS(progress, nir, nir_lower_vec_to_movs);
594
595 NIR_PASS(progress, nir, nir_opt_dce);
596}
597
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000598/* Do not actually emit a load; instead, cache the constant for inlining */
599
600static void
601emit_load_const(compiler_context *ctx, nir_load_const_instr *instr)
602{
603 nir_ssa_def def = instr->def;
604
Tomeu Vizoso554975b2019-05-07 17:28:36 +0200605 float *v = rzalloc_array(NULL, float, 4);
Alyssa Rosenzweig3c01a692019-08-21 10:50:31 -0700606 nir_const_value_to_array(v, instr->value, instr->def.num_components, f32);
Alyssa Rosenzweig9beb3392019-07-26 11:30:06 -0700607
608 /* Shifted for SSA, +1 for off-by-one */
609 _mesa_hash_table_u64_insert(ctx->ssa_constants, (def.index << 1) + 1, v);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000610}
611
Alyssa Rosenzweige1693012019-07-24 12:52:27 -0700612/* Normally constants are embedded implicitly, but for I/O and such we have to
613 * explicitly emit a move with the constant source */
614
615static void
616emit_explicit_constant(compiler_context *ctx, unsigned node, unsigned to)
617{
618 void *constant_value = _mesa_hash_table_u64_search(ctx->ssa_constants, node + 1);
619
620 if (constant_value) {
Alyssa Rosenzweigc3a46e72019-10-30 16:29:28 -0400621 midgard_instruction ins = v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT), to);
Alyssa Rosenzweige1693012019-07-24 12:52:27 -0700622 attach_constants(ctx, &ins, constant_value, node + 1);
623 emit_mir_instruction(ctx, ins);
624 }
625}
626
Alyssa Rosenzweig726f0262019-05-07 02:52:08 +0000627static bool
628nir_is_non_scalar_swizzle(nir_alu_src *src, unsigned nr_components)
629{
630 unsigned comp = src->swizzle[0];
631
632 for (unsigned c = 1; c < nr_components; ++c) {
633 if (src->swizzle[c] != comp)
634 return true;
635 }
636
637 return false;
638}
639
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000640#define ALU_CASE(nir, _op) \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000641 case nir_op_##nir: \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000642 op = midgard_alu_op_##_op; \
Alyssa Rosenzweig0ed8cca2019-07-01 17:35:25 -0700643 assert(src_bitsize == dst_bitsize); \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000644 break;
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700645
646#define ALU_CASE_BCAST(nir, _op, count) \
647 case nir_op_##nir: \
648 op = midgard_alu_op_##_op; \
649 broadcast_swizzle = count; \
Alyssa Rosenzweig0ed8cca2019-07-01 17:35:25 -0700650 assert(src_bitsize == dst_bitsize); \
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700651 break;
Alyssa Rosenzweig7bc91b42019-04-24 23:42:30 +0000652static bool
653nir_is_fzero_constant(nir_src src)
654{
655 if (!nir_src_is_const(src))
656 return false;
657
658 for (unsigned c = 0; c < nir_src_num_components(src); ++c) {
659 if (nir_src_comp_as_float(src, c) != 0.0)
660 return false;
661 }
662
663 return true;
664}
665
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700666/* Analyze the sizes of the inputs to determine which reg mode. Ops needed
667 * special treatment override this anyway. */
Alyssa Rosenzweig4df80ca2019-07-01 15:26:22 -0700668
669static midgard_reg_mode
670reg_mode_for_nir(nir_alu_instr *instr)
671{
672 unsigned src_bitsize = nir_src_bit_size(instr->src[0].src);
673
674 switch (src_bitsize) {
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -0700675 case 8:
676 return midgard_reg_mode_8;
677 case 16:
678 return midgard_reg_mode_16;
679 case 32:
680 return midgard_reg_mode_32;
681 case 64:
682 return midgard_reg_mode_64;
683 default:
684 unreachable("Invalid bit size");
Alyssa Rosenzweig4df80ca2019-07-01 15:26:22 -0700685 }
686}
687
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000688static void
689emit_alu(compiler_context *ctx, nir_alu_instr *instr)
690{
Alyssa Rosenzweig8f887322019-07-29 15:11:12 -0700691 /* Derivatives end up emitted on the texture pipe, not the ALUs. This
692 * is handled elsewhere */
693
694 if (instr->op == nir_op_fddx || instr->op == nir_op_fddy) {
695 midgard_emit_derivatives(ctx, instr);
696 return;
697 }
698
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000699 bool is_ssa = instr->dest.dest.is_ssa;
700
Alyssa Rosenzweig4ed23b12019-02-07 04:56:13 +0000701 unsigned dest = nir_dest_index(ctx, &instr->dest.dest);
Alyssa Rosenzweigf42e5be2019-07-01 15:28:37 -0700702 unsigned nr_components = nir_dest_num_components(instr->dest.dest);
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000703 unsigned nr_inputs = nir_op_infos[instr->op].num_inputs;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000704
705 /* Most Midgard ALU ops have a 1:1 correspondance to NIR ops; these are
706 * supported. A few do not and are commented for now. Also, there are a
707 * number of NIR ops which Midgard does not support and need to be
708 * lowered, also TODO. This switch block emits the opcode and calling
709 * convention of the Midgard instruction; actual packing is done in
710 * emit_alu below */
711
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000712 unsigned op;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000713
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700714 /* Number of components valid to check for the instruction (the rest
715 * will be forced to the last), or 0 to use as-is. Relevant as
716 * ball-type instructions have a channel count in NIR but are all vec4
717 * in Midgard */
718
719 unsigned broadcast_swizzle = 0;
720
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700721 /* What register mode should we operate in? */
722 midgard_reg_mode reg_mode =
723 reg_mode_for_nir(instr);
724
Alyssa Rosenzweig4df80ca2019-07-01 15:26:22 -0700725 /* Do we need a destination override? Used for inline
726 * type conversion */
727
728 midgard_dest_override dest_override =
729 midgard_dest_override_none;
730
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700731 /* Should we use a smaller respective source and sign-extend? */
732
733 bool half_1 = false, sext_1 = false;
734 bool half_2 = false, sext_2 = false;
735
Alyssa Rosenzweig0ed8cca2019-07-01 17:35:25 -0700736 unsigned src_bitsize = nir_src_bit_size(instr->src[0].src);
737 unsigned dst_bitsize = nir_dest_bit_size(instr->dest.dest);
738
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000739 switch (instr->op) {
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000740 ALU_CASE(fadd, fadd);
741 ALU_CASE(fmul, fmul);
742 ALU_CASE(fmin, fmin);
743 ALU_CASE(fmax, fmax);
744 ALU_CASE(imin, imin);
745 ALU_CASE(imax, imax);
Alyssa Rosenzweig2e7555b2019-04-05 05:16:54 +0000746 ALU_CASE(umin, umin);
747 ALU_CASE(umax, umax);
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000748 ALU_CASE(ffloor, ffloor);
Alyssa Rosenzweigc6be9962019-02-23 01:12:10 +0000749 ALU_CASE(fround_even, froundeven);
750 ALU_CASE(ftrunc, ftrunc);
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000751 ALU_CASE(fceil, fceil);
752 ALU_CASE(fdot3, fdot3);
753 ALU_CASE(fdot4, fdot4);
754 ALU_CASE(iadd, iadd);
755 ALU_CASE(isub, isub);
756 ALU_CASE(imul, imul);
Alyssa Rosenzweig9f14e202019-06-05 15:18:35 +0000757
758 /* Zero shoved as second-arg */
759 ALU_CASE(iabs, iabsdiff);
760
Jason Ekstrandf2dc0f22019-05-06 11:45:46 -0500761 ALU_CASE(mov, imov);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000762
Alyssa Rosenzweig3fb88422019-03-25 00:25:01 +0000763 ALU_CASE(feq32, feq);
764 ALU_CASE(fne32, fne);
765 ALU_CASE(flt32, flt);
766 ALU_CASE(ieq32, ieq);
767 ALU_CASE(ine32, ine);
768 ALU_CASE(ilt32, ilt);
Alyssa Rosenzweigb8739c22019-03-26 04:00:33 +0000769 ALU_CASE(ult32, ult);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000770
Alyssa Rosenzweig3208c9d2019-03-25 01:13:12 +0000771 /* We don't have a native b2f32 instruction. Instead, like many
772 * GPUs, we exploit booleans as 0/~0 for false/true, and
773 * correspondingly AND
774 * by 1.0 to do the type conversion. For the moment, prime us
775 * to emit:
776 *
777 * iand [whatever], #0
778 *
779 * At the end of emit_alu (as MIR), we'll fix-up the constant
780 */
781
782 ALU_CASE(b2f32, iand);
783 ALU_CASE(b2i32, iand);
784
Alyssa Rosenzweigae43b8f2019-03-25 00:53:46 +0000785 /* Likewise, we don't have a dedicated f2b32 instruction, but
Alyssa Rosenzweig3208c9d2019-03-25 01:13:12 +0000786 * we can do a "not equal to 0.0" test. */
Alyssa Rosenzweigae43b8f2019-03-25 00:53:46 +0000787
788 ALU_CASE(f2b32, fne);
Alyssa Rosenzweig5b95fef2019-03-25 00:56:48 +0000789 ALU_CASE(i2b32, ine);
Alyssa Rosenzweigae43b8f2019-03-25 00:53:46 +0000790
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000791 ALU_CASE(frcp, frcp);
792 ALU_CASE(frsq, frsqrt);
793 ALU_CASE(fsqrt, fsqrt);
794 ALU_CASE(fexp2, fexp2);
795 ALU_CASE(flog2, flog2);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000796
Alyssa Rosenzweig73bf6692019-06-05 15:03:02 -0700797 ALU_CASE(f2i32, f2i_rtz);
798 ALU_CASE(f2u32, f2u_rtz);
799 ALU_CASE(i2f32, i2f_rtz);
800 ALU_CASE(u2f32, u2f_rtz);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000801
Alyssa Rosenzweigd8c084d2019-07-01 17:41:20 -0700802 ALU_CASE(f2i16, f2i_rtz);
803 ALU_CASE(f2u16, f2u_rtz);
804 ALU_CASE(i2f16, i2f_rtz);
805 ALU_CASE(u2f16, u2f_rtz);
806
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000807 ALU_CASE(fsin, fsin);
808 ALU_CASE(fcos, fcos);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000809
Alyssa Rosenzweig159abd52019-07-26 11:15:31 -0700810 /* We'll set invert */
811 ALU_CASE(inot, imov);
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000812 ALU_CASE(iand, iand);
813 ALU_CASE(ior, ior);
814 ALU_CASE(ixor, ixor);
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000815 ALU_CASE(ishl, ishl);
816 ALU_CASE(ishr, iasr);
817 ALU_CASE(ushr, ilsr);
818
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700819 ALU_CASE_BCAST(b32all_fequal2, fball_eq, 2);
820 ALU_CASE_BCAST(b32all_fequal3, fball_eq, 3);
Alyssa Rosenzweig3fb88422019-03-25 00:25:01 +0000821 ALU_CASE(b32all_fequal4, fball_eq);
Alyssa Rosenzweig53664102019-03-25 00:12:06 +0000822
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700823 ALU_CASE_BCAST(b32any_fnequal2, fbany_neq, 2);
824 ALU_CASE_BCAST(b32any_fnequal3, fbany_neq, 3);
Alyssa Rosenzweig3fb88422019-03-25 00:25:01 +0000825 ALU_CASE(b32any_fnequal4, fbany_neq);
Alyssa Rosenzweig53664102019-03-25 00:12:06 +0000826
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700827 ALU_CASE_BCAST(b32all_iequal2, iball_eq, 2);
828 ALU_CASE_BCAST(b32all_iequal3, iball_eq, 3);
Alyssa Rosenzweig3fb88422019-03-25 00:25:01 +0000829 ALU_CASE(b32all_iequal4, iball_eq);
Alyssa Rosenzweig53664102019-03-25 00:12:06 +0000830
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700831 ALU_CASE_BCAST(b32any_inequal2, ibany_neq, 2);
832 ALU_CASE_BCAST(b32any_inequal3, ibany_neq, 3);
Alyssa Rosenzweig3fb88422019-03-25 00:25:01 +0000833 ALU_CASE(b32any_inequal4, ibany_neq);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000834
Alyssa Rosenzweig659aa3d2019-05-26 03:16:37 +0000835 /* Source mods will be shoved in later */
836 ALU_CASE(fabs, fmov);
837 ALU_CASE(fneg, fmov);
838 ALU_CASE(fsat, fmov);
839
Alyssa Rosenzweig4df80ca2019-07-01 15:26:22 -0700840 /* For size conversion, we use a move. Ideally though we would squash
841 * these ops together; maybe that has to happen after in NIR as part of
842 * propagation...? An earlier algebraic pass ensured we step down by
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700843 * only / exactly one size. If stepping down, we use a dest override to
844 * reduce the size; if stepping up, we use a larger-sized move with a
845 * half source and a sign/zero-extension modifier */
Alyssa Rosenzweig4df80ca2019-07-01 15:26:22 -0700846
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700847 case nir_op_i2i8:
848 case nir_op_i2i16:
849 case nir_op_i2i32:
Alyssa Rosenzweig2655a302019-11-04 22:21:20 -0500850 case nir_op_i2i64:
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700851 /* If we end up upscale, we'll need a sign-extend on the
852 * operand (the second argument) */
853
854 sext_2 = true;
Alyssa Rosenzweig14a2032f2019-08-21 09:20:17 -0700855 /* fallthrough */
Alyssa Rosenzweig4df80ca2019-07-01 15:26:22 -0700856 case nir_op_u2u8:
857 case nir_op_u2u16:
Alyssa Rosenzweig2655a302019-11-04 22:21:20 -0500858 case nir_op_u2u32:
859 case nir_op_u2u64: {
Alyssa Rosenzweig4df80ca2019-07-01 15:26:22 -0700860 op = midgard_alu_op_imov;
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700861
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700862 if (dst_bitsize == (src_bitsize * 2)) {
863 /* Converting up */
864 half_2 = true;
865
866 /* Use a greater register mode */
867 reg_mode++;
868 } else if (src_bitsize == (dst_bitsize * 2)) {
869 /* Converting down */
870 dest_override = midgard_dest_override_lower;
871 }
872
Alyssa Rosenzweig4df80ca2019-07-01 15:26:22 -0700873 break;
874 }
875
Alyssa Rosenzweig954c6af2019-07-01 17:38:26 -0700876 case nir_op_f2f16: {
877 assert(src_bitsize == 32);
878
879 op = midgard_alu_op_fmov;
880 dest_override = midgard_dest_override_lower;
881 break;
882 }
883
884 case nir_op_f2f32: {
885 assert(src_bitsize == 16);
886
887 op = midgard_alu_op_fmov;
888 half_2 = true;
889 reg_mode++;
890 break;
891 }
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -0700892
Alyssa Rosenzweig954c6af2019-07-01 17:38:26 -0700893
Alyssa Rosenzweig7b78af82019-03-26 04:01:33 +0000894 /* For greater-or-equal, we lower to less-or-equal and flip the
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000895 * arguments */
896
Alyssa Rosenzweig7b78af82019-03-26 04:01:33 +0000897 case nir_op_fge:
898 case nir_op_fge32:
899 case nir_op_ige32:
900 case nir_op_uge32: {
901 op =
902 instr->op == nir_op_fge ? midgard_alu_op_fle :
903 instr->op == nir_op_fge32 ? midgard_alu_op_fle :
904 instr->op == nir_op_ige32 ? midgard_alu_op_ile :
905 instr->op == nir_op_uge32 ? midgard_alu_op_ule :
906 0;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000907
908 /* Swap via temporary */
909 nir_alu_src temp = instr->src[1];
910 instr->src[1] = instr->src[0];
911 instr->src[0] = temp;
912
913 break;
914 }
915
Alyssa Rosenzweig3fb88422019-03-25 00:25:01 +0000916 case nir_op_b32csel: {
Alyssa Rosenzweig726f0262019-05-07 02:52:08 +0000917 /* Midgard features both fcsel and icsel, depending on
918 * the type of the arguments/output. However, as long
919 * as we're careful we can _always_ use icsel and
920 * _never_ need fcsel, since the latter does additional
921 * floating-point-specific processing whereas the
922 * former just moves bits on the wire. It's not obvious
923 * why these are separate opcodes, save for the ability
924 * to do things like sat/pos/abs/neg for free */
Alyssa Rosenzweig3d7874c2019-05-03 01:54:16 +0000925
Alyssa Rosenzweig726f0262019-05-07 02:52:08 +0000926 bool mixed = nir_is_non_scalar_swizzle(&instr->src[0], nr_components);
927 op = mixed ? midgard_alu_op_icsel_v : midgard_alu_op_icsel;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000928
Alyssa Rosenzweig726f0262019-05-07 02:52:08 +0000929 /* The condition is the first argument; move the other
930 * arguments up one to be a binary instruction for
Alyssa Rosenzweigd3b3daa2019-09-23 08:00:51 -0400931 * Midgard with the condition last */
Alyssa Rosenzweig8b15f8a2019-04-21 00:09:13 +0000932
Alyssa Rosenzweigd3b3daa2019-09-23 08:00:51 -0400933 nir_alu_src temp = instr->src[2];
934
935 instr->src[2] = instr->src[0];
936 instr->src[0] = instr->src[1];
937 instr->src[1] = temp;
938
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000939 break;
940 }
941
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000942 default:
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +0100943 DBG("Unhandled ALU op %s\n", nir_op_infos[instr->op].name);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000944 assert(0);
945 return;
946 }
947
Alyssa Rosenzweig0a13bab2019-05-15 01:16:51 +0000948 /* Midgard can perform certain modifiers on output of an ALU op */
Alyssa Rosenzweig67804812019-06-05 15:17:45 -0700949 unsigned outmod;
Alyssa Rosenzweig7bc91b42019-04-24 23:42:30 +0000950
Alyssa Rosenzweig67804812019-06-05 15:17:45 -0700951 if (midgard_is_integer_out_op(op)) {
952 outmod = midgard_outmod_int_wrap;
953 } else {
954 bool sat = instr->dest.saturate || instr->op == nir_op_fsat;
955 outmod = sat ? midgard_outmod_sat : midgard_outmod_none;
956 }
Alyssa Rosenzweig659aa3d2019-05-26 03:16:37 +0000957
Alyssa Rosenzweig7bc91b42019-04-24 23:42:30 +0000958 /* fmax(a, 0.0) can turn into a .pos modifier as an optimization */
959
960 if (instr->op == nir_op_fmax) {
961 if (nir_is_fzero_constant(instr->src[0].src)) {
962 op = midgard_alu_op_fmov;
963 nr_inputs = 1;
964 outmod = midgard_outmod_pos;
965 instr->src[0] = instr->src[1];
966 } else if (nir_is_fzero_constant(instr->src[1].src)) {
967 op = midgard_alu_op_fmov;
968 nr_inputs = 1;
969 outmod = midgard_outmod_pos;
970 }
971 }
972
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000973 /* Fetch unit, quirks, etc information */
Alyssa Rosenzweig1f345bc2019-04-24 01:15:15 +0000974 unsigned opcode_props = alu_opcode_props[op].props;
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000975 bool quirk_flipped_r24 = opcode_props & QUIRK_FLIPPED_R24;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000976
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000977 /* src0 will always exist afaik, but src1 will not for 1-argument
978 * instructions. The latter can only be fetched if the instruction
979 * needs it, or else we may segfault. */
980
Alyssa Rosenzweig4ed23b12019-02-07 04:56:13 +0000981 unsigned src0 = nir_alu_src_index(ctx, &instr->src[0]);
Alyssa Rosenzweig8e369962019-08-30 10:42:05 -0700982 unsigned src1 = nr_inputs >= 2 ? nir_alu_src_index(ctx, &instr->src[1]) : ~0;
983 unsigned src2 = nr_inputs == 3 ? nir_alu_src_index(ctx, &instr->src[2]) : ~0;
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -0400984 assert(nr_inputs <= 3);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000985
986 /* Rather than use the instruction generation helpers, we do it
987 * ourselves here to avoid the mess */
988
989 midgard_instruction ins = {
990 .type = TAG_ALU_4,
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -0700991 .src = {
992 quirk_flipped_r24 ? ~0 : src0,
993 quirk_flipped_r24 ? src0 : src1,
Alyssa Rosenzweig8e369962019-08-30 10:42:05 -0700994 src2,
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -0700995 },
996 .dest = dest,
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000997 };
998
Alyssa Rosenzweig8e369962019-08-30 10:42:05 -0700999 nir_alu_src *nirmods[3] = { NULL };
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001000
Alyssa Rosenzweig8e369962019-08-30 10:42:05 -07001001 if (nr_inputs >= 2) {
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +00001002 nirmods[0] = &instr->src[0];
1003 nirmods[1] = &instr->src[1];
1004 } else if (nr_inputs == 1) {
1005 nirmods[quirk_flipped_r24] = &instr->src[0];
1006 } else {
1007 assert(0);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001008 }
1009
Alyssa Rosenzweig8e369962019-08-30 10:42:05 -07001010 if (nr_inputs == 3)
1011 nirmods[2] = &instr->src[2];
1012
Alyssa Rosenzweig659aa3d2019-05-26 03:16:37 +00001013 /* These were lowered to a move, so apply the corresponding mod */
1014
1015 if (instr->op == nir_op_fneg || instr->op == nir_op_fabs) {
1016 nir_alu_src *s = nirmods[quirk_flipped_r24];
1017
1018 if (instr->op == nir_op_fneg)
1019 s->negate = !s->negate;
1020
1021 if (instr->op == nir_op_fabs)
1022 s->abs = !s->abs;
1023 }
1024
Alyssa Rosenzweigfcdfb672019-04-22 03:25:42 +00001025 bool is_int = midgard_is_integer_op(op);
1026
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -07001027 ins.mask = mask_of(nr_components);
1028
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001029 midgard_vector_alu alu = {
1030 .op = op,
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -07001031 .reg_mode = reg_mode,
Alyssa Rosenzweig4df80ca2019-07-01 15:26:22 -07001032 .dest_override = dest_override,
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001033 .outmod = outmod,
1034
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -07001035 .src1 = vector_alu_srco_unsigned(vector_alu_modifiers(nirmods[0], is_int, broadcast_swizzle, half_1, sext_1)),
1036 .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 +00001037 };
1038
1039 /* Apply writemask if non-SSA, keeping in mind that we can't write to components that don't exist */
1040
1041 if (!is_ssa)
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -07001042 ins.mask &= instr->dest.write_mask;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001043
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001044 for (unsigned m = 0; m < 3; ++m) {
1045 if (!nirmods[m])
1046 continue;
1047
1048 for (unsigned c = 0; c < NIR_MAX_VEC_COMPONENTS; ++c)
1049 ins.swizzle[m][c] = nirmods[m]->swizzle[c];
1050
1051 /* Replicate. TODO: remove when vec16 lands */
1052 for (unsigned c = NIR_MAX_VEC_COMPONENTS; c < MIR_VEC_COMPONENTS; ++c)
1053 ins.swizzle[m][c] = nirmods[m]->swizzle[NIR_MAX_VEC_COMPONENTS - 1];
1054 }
1055
1056 if (nr_inputs == 3) {
1057 /* Conditions can't have mods */
1058 assert(!nirmods[2]->abs);
1059 assert(!nirmods[2]->negate);
1060 }
1061
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001062 ins.alu = alu;
1063
1064 /* Late fixup for emulated instructions */
1065
Alyssa Rosenzweig3208c9d2019-03-25 01:13:12 +00001066 if (instr->op == nir_op_b2f32 || instr->op == nir_op_b2i32) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001067 /* Presently, our second argument is an inline #0 constant.
1068 * Switch over to an embedded 1.0 constant (that can't fit
1069 * inline, since we're 32-bit, not 16-bit like the inline
1070 * constants) */
1071
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07001072 ins.has_inline_constant = false;
1073 ins.src[1] = SSA_FIXED_REGISTER(REGISTER_CONSTANT);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001074 ins.has_constants = true;
Alyssa Rosenzweig9da46032019-03-24 16:07:31 +00001075
Alyssa Rosenzweig3208c9d2019-03-25 01:13:12 +00001076 if (instr->op == nir_op_b2f32) {
Alyssa Rosenzweig0acb5c12019-08-23 16:02:49 -07001077 float f = 1.0f;
1078 memcpy(&ins.constants, &f, sizeof(float));
Alyssa Rosenzweig3208c9d2019-03-25 01:13:12 +00001079 } else {
Alyssa Rosenzweig0acb5c12019-08-23 16:02:49 -07001080 ins.constants[0] = 1;
Alyssa Rosenzweig3208c9d2019-03-25 01:13:12 +00001081 }
1082
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001083
1084 for (unsigned c = 0; c < 16; ++c)
1085 ins.swizzle[1][c] = 0;
Alyssa Rosenzweig88c59792019-06-05 15:24:51 +00001086 } else if (nr_inputs == 1 && !quirk_flipped_r24) {
1087 /* Lots of instructions need a 0 plonked in */
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07001088 ins.has_inline_constant = false;
1089 ins.src[1] = SSA_FIXED_REGISTER(REGISTER_CONSTANT);
Alyssa Rosenzweig3208c9d2019-03-25 01:13:12 +00001090 ins.has_constants = true;
Alyssa Rosenzweig0acb5c12019-08-23 16:02:49 -07001091 ins.constants[0] = 0;
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001092
1093 for (unsigned c = 0; c < 16; ++c)
1094 ins.swizzle[1][c] = 0;
Alyssa Rosenzweigbcabcfe2019-04-25 04:25:33 +00001095 } else if (instr->op == nir_op_inot) {
Alyssa Rosenzweig159abd52019-07-26 11:15:31 -07001096 ins.invert = true;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001097 }
1098
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +00001099 if ((opcode_props & UNITS_ALL) == UNIT_VLUT) {
1100 /* To avoid duplicating the lookup tables (probably), true LUT
1101 * instructions can only operate as if they were scalars. Lower
1102 * them here by changing the component. */
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001103
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -07001104 unsigned orig_mask = ins.mask;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001105
1106 for (int i = 0; i < nr_components; ++i) {
Alyssa Rosenzweig2c9e1242019-06-17 11:49:44 -07001107 /* Mask the associated component, dropping the
1108 * instruction if needed */
1109
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -07001110 ins.mask = 1 << i;
1111 ins.mask &= orig_mask;
Alyssa Rosenzweig2c9e1242019-06-17 11:49:44 -07001112
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -07001113 if (!ins.mask)
Alyssa Rosenzweig2c9e1242019-06-17 11:49:44 -07001114 continue;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001115
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001116 for (unsigned j = 0; j < MIR_VEC_COMPONENTS; ++j)
1117 ins.swizzle[0][j] = nirmods[0]->swizzle[i]; /* Pull from the correct component */
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001118
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001119 emit_mir_instruction(ctx, ins);
1120 }
1121 } else {
1122 emit_mir_instruction(ctx, ins);
1123 }
1124}
1125
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +00001126#undef ALU_CASE
1127
Alyssa Rosenzweig1798f6b2019-11-15 15:16:53 -05001128static void
1129mir_set_intr_mask(nir_instr *instr, midgard_instruction *ins, bool is_read)
Alyssa Rosenzweig65e6cb42019-08-13 09:11:48 -07001130{
1131 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
Alyssa Rosenzweig1798f6b2019-11-15 15:16:53 -05001132 unsigned nir_mask = 0;
1133 unsigned dsize = 0;
Alyssa Rosenzweig65e6cb42019-08-13 09:11:48 -07001134
Alyssa Rosenzweig1798f6b2019-11-15 15:16:53 -05001135 if (is_read) {
1136 nir_mask = mask_of(nir_intrinsic_dest_components(intr));
1137 dsize = nir_dest_bit_size(intr->dest);
1138 } else {
1139 nir_mask = nir_intrinsic_write_mask(intr);
1140 dsize = 32;
1141 }
1142
1143 /* Once we have the NIR mask, we need to normalize to work in 32-bit space */
1144 unsigned bytemask = mir_to_bytemask(mir_mode_for_destsize(dsize), nir_mask);
1145 mir_set_bytemask(ins, bytemask);
1146
1147 if (dsize == 64)
1148 ins->load_64 = true;
Alyssa Rosenzweig65e6cb42019-08-13 09:11:48 -07001149}
1150
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001151/* Uniforms and UBOs use a shared code path, as uniforms are just (slightly
1152 * optimized) versions of UBO #0 */
1153
Alyssa Rosenzweige7ac46b2019-08-02 17:09:54 -07001154midgard_instruction *
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001155emit_ubo_read(
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001156 compiler_context *ctx,
Alyssa Rosenzweig65e6cb42019-08-13 09:11:48 -07001157 nir_instr *instr,
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001158 unsigned dest,
1159 unsigned offset,
1160 nir_src *indirect_offset,
1161 unsigned index)
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001162{
1163 /* TODO: half-floats */
1164
Alyssa Rosenzweigbc9a7d02019-11-15 14:19:34 -05001165 midgard_instruction ins = m_ld_ubo_int4(dest, 0);
1166 ins.constants[0] = offset;
Alyssa Rosenzweig1798f6b2019-11-15 15:16:53 -05001167 mir_set_intr_mask(instr, &ins, true);
Alyssa Rosenzweig3174bc92019-07-16 14:10:08 -07001168
1169 if (indirect_offset) {
Alyssa Rosenzweige7fd14c2019-10-26 15:50:38 -04001170 ins.src[2] = nir_src_index(ctx, indirect_offset);
Alyssa Rosenzweig513d02c2019-08-01 14:28:34 -07001171 ins.load_store.arg_2 = 0x80;
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001172 } else {
Alyssa Rosenzweigc9087722019-08-01 13:29:01 -07001173 ins.load_store.arg_2 = 0x1E;
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001174 }
Alyssa Rosenzweig3174bc92019-07-16 14:10:08 -07001175
Alyssa Rosenzweigc9087722019-08-01 13:29:01 -07001176 ins.load_store.arg_1 = index;
1177
Alyssa Rosenzweige7ac46b2019-08-02 17:09:54 -07001178 return emit_mir_instruction(ctx, ins);
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001179}
1180
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001181/* SSBO reads are like UBO reads if you squint */
1182
1183static void
1184emit_ssbo_access(
1185 compiler_context *ctx,
1186 nir_instr *instr,
1187 bool is_read,
1188 unsigned srcdest,
1189 unsigned offset,
1190 nir_src *indirect_offset,
1191 unsigned index)
1192{
1193 /* TODO: types */
1194
1195 midgard_instruction ins;
1196
1197 if (is_read)
1198 ins = m_ld_int4(srcdest, offset);
1199 else
1200 ins = m_st_int4(srcdest, offset);
1201
1202 /* SSBO reads use a generic memory read interface, so we need the
1203 * address of the SSBO as the first argument. This is a sysval. */
1204
1205 unsigned addr = make_compiler_temp(ctx);
1206 emit_sysval_read(ctx, instr, addr, 2);
1207
Alyssa Rosenzweige7fd14c2019-10-26 15:50:38 -04001208 /* The source array:
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001209 *
Alyssa Rosenzweige7fd14c2019-10-26 15:50:38 -04001210 * src[0] = store ? value : unused
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001211 * src[1] = arg_1
1212 * src[2] = arg_2
1213 *
1214 * We would like arg_1 = the address and
1215 * arg_2 = the offset.
1216 */
1217
Alyssa Rosenzweige7fd14c2019-10-26 15:50:38 -04001218 ins.src[1] = addr;
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001219
1220 /* TODO: What is this? It looks superficially like a shift << 5, but
Alyssa Rosenzweig1a4153b2019-08-30 17:29:17 -07001221 * arg_1 doesn't take a shift Should it be E0 or A0? We also need the
1222 * indirect offset. */
1223
1224 if (indirect_offset) {
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001225 ins.load_store.arg_1 |= 0xE0;
Alyssa Rosenzweige7fd14c2019-10-26 15:50:38 -04001226 ins.src[2] = nir_src_index(ctx, indirect_offset);
Alyssa Rosenzweig1a4153b2019-08-30 17:29:17 -07001227 } else {
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001228 ins.load_store.arg_2 = 0x7E;
Alyssa Rosenzweig1a4153b2019-08-30 17:29:17 -07001229 }
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001230
1231 /* TODO: Bounds check */
1232
1233 /* Finally, we emit the direct offset */
1234
1235 ins.load_store.varying_parameters = (offset & 0x1FF) << 1;
1236 ins.load_store.address = (offset >> 9);
Alyssa Rosenzweig1798f6b2019-11-15 15:16:53 -05001237 mir_set_intr_mask(instr, &ins, is_read);
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001238
1239 emit_mir_instruction(ctx, ins);
1240}
1241
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001242static void
Alyssa Rosenzweig15fae1e2019-06-04 23:26:09 +00001243emit_varying_read(
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001244 compiler_context *ctx,
1245 unsigned dest, unsigned offset,
1246 unsigned nr_comp, unsigned component,
1247 nir_src *indirect_offset, nir_alu_type type)
Alyssa Rosenzweig15fae1e2019-06-04 23:26:09 +00001248{
1249 /* XXX: Half-floats? */
1250 /* TODO: swizzle, mask */
1251
1252 midgard_instruction ins = m_ld_vary_32(dest, offset);
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -07001253 ins.mask = mask_of(nr_comp);
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001254
1255 for (unsigned i = 0; i < ARRAY_SIZE(ins.swizzle[0]); ++i)
1256 ins.swizzle[0][i] = MIN2(i + component, COMPONENT_W);
Alyssa Rosenzweig15fae1e2019-06-04 23:26:09 +00001257
1258 midgard_varying_parameter p = {
1259 .is_varying = 1,
1260 .interpolation = midgard_interp_default,
1261 .flat = /*var->data.interpolation == INTERP_MODE_FLAT*/ 0
1262 };
1263
1264 unsigned u;
1265 memcpy(&u, &p, sizeof(p));
1266 ins.load_store.varying_parameters = u;
1267
Alyssa Rosenzweig513d02c2019-08-01 14:28:34 -07001268 if (indirect_offset)
Alyssa Rosenzweige7fd14c2019-10-26 15:50:38 -04001269 ins.src[2] = nir_src_index(ctx, indirect_offset);
Alyssa Rosenzweig513d02c2019-08-01 14:28:34 -07001270 else
Alyssa Rosenzweigc9087722019-08-01 13:29:01 -07001271 ins.load_store.arg_2 = 0x1E;
Alyssa Rosenzweig15fae1e2019-06-04 23:26:09 +00001272
Alyssa Rosenzweigc9087722019-08-01 13:29:01 -07001273 ins.load_store.arg_1 = 0x9E;
1274
Alyssa Rosenzweig9b97ed12019-06-28 09:30:59 -07001275 /* Use the type appropriate load */
1276 switch (type) {
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001277 case nir_type_uint:
1278 case nir_type_bool:
1279 ins.load_store.op = midgard_op_ld_vary_32u;
1280 break;
1281 case nir_type_int:
1282 ins.load_store.op = midgard_op_ld_vary_32i;
1283 break;
1284 case nir_type_float:
1285 ins.load_store.op = midgard_op_ld_vary_32;
1286 break;
1287 default:
1288 unreachable("Attempted to load unknown type");
1289 break;
Alyssa Rosenzweig9b97ed12019-06-28 09:30:59 -07001290 }
1291
Alyssa Rosenzweig15fae1e2019-06-04 23:26:09 +00001292 emit_mir_instruction(ctx, ins);
1293}
1294
Alyssa Rosenzweigfa687402019-08-02 11:06:21 -07001295void
Alyssa Rosenzweig63e240d2019-08-02 17:10:18 -07001296emit_sysval_read(compiler_context *ctx, nir_instr *instr, signed dest_override,
1297 unsigned nr_components)
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001298{
Alyssa Rosenzweig6d8490f2019-07-11 15:34:56 -07001299 unsigned dest = 0;
1300
Boris Brezillonbd49c8f2019-06-14 09:59:20 +02001301 /* Figure out which uniform this is */
1302 int sysval = sysval_for_instr(ctx, instr, &dest);
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001303 void *val = _mesa_hash_table_u64_search(ctx->sysval_to_id, sysval);
1304
Alyssa Rosenzweigfa687402019-08-02 11:06:21 -07001305 if (dest_override >= 0)
1306 dest = dest_override;
1307
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001308 /* Sysvals are prefix uniforms */
1309 unsigned uniform = ((uintptr_t) val) - 1;
1310
Alyssa Rosenzweig6a466c02019-04-20 23:52:42 +00001311 /* Emit the read itself -- this is never indirect */
Alyssa Rosenzweig63e240d2019-08-02 17:10:18 -07001312 midgard_instruction *ins =
Alyssa Rosenzweigcf3bb102019-08-13 09:13:31 -07001313 emit_ubo_read(ctx, instr, dest, uniform * 16, NULL, 0);
Alyssa Rosenzweig63e240d2019-08-02 17:10:18 -07001314
1315 ins->mask = mask_of(nr_components);
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001316}
1317
Alyssa Rosenzweig7229af72019-08-06 13:47:17 -07001318static unsigned
1319compute_builtin_arg(nir_op op)
1320{
1321 switch (op) {
1322 case nir_intrinsic_load_work_group_id:
1323 return 0x14;
1324 case nir_intrinsic_load_local_invocation_id:
1325 return 0x10;
1326 default:
1327 unreachable("Invalid compute paramater loaded");
1328 }
1329}
1330
Alyssa Rosenzweigdff49862019-08-12 12:36:46 -07001331/* Emit store for a fragment shader, which is encoded via a fancy branch. TODO:
1332 * Handle MRT here */
1333
1334static void
1335emit_fragment_store(compiler_context *ctx, unsigned src, unsigned rt)
1336{
Alyssa Rosenzweig5e06d902019-08-30 11:06:33 -07001337 emit_explicit_constant(ctx, src, src);
Alyssa Rosenzweigdff49862019-08-12 12:36:46 -07001338
1339 /* If we're doing MRT, we need to specify the render target */
1340
1341 midgard_instruction rt_move = {
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07001342 .dest = ~0
Alyssa Rosenzweigdff49862019-08-12 12:36:46 -07001343 };
1344
1345 if (rt != 0) {
1346 /* We'll write to r1.z */
Alyssa Rosenzweigc3a46e72019-10-30 16:29:28 -04001347 rt_move = v_mov(~0, SSA_FIXED_REGISTER(1));
Alyssa Rosenzweigdff49862019-08-12 12:36:46 -07001348 rt_move.mask = 1 << COMPONENT_Z;
1349 rt_move.unit = UNIT_SADD;
1350
1351 /* r1.z = (rt * 0x100) */
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07001352 rt_move.has_inline_constant = true;
Alyssa Rosenzweigdff49862019-08-12 12:36:46 -07001353 rt_move.inline_constant = (rt * 0x100);
1354
1355 /* r1 */
1356 ctx->work_registers = MAX2(ctx->work_registers, 1);
1357
1358 /* Do the write */
1359 emit_mir_instruction(ctx, rt_move);
1360 }
1361
1362 /* Next, generate the branch. For R render targets in the writeout, the
1363 * i'th render target jumps to pseudo-offset [2(R-1) + i] */
1364
Alyssa Rosenzweig76529832019-08-30 11:01:15 -07001365 unsigned outputs = ctx->is_blend ? 1 : ctx->nir->num_outputs;
1366 unsigned offset = (2 * (outputs - 1)) + rt;
Alyssa Rosenzweigdff49862019-08-12 12:36:46 -07001367
1368 struct midgard_instruction ins =
1369 v_alu_br_compact_cond(midgard_jmp_writeout_op_writeout, TAG_ALU_4, offset, midgard_condition_always);
1370
1371 /* Add dependencies */
Alyssa Rosenzweig76529832019-08-30 11:01:15 -07001372 ins.src[0] = src;
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07001373 ins.src[1] = rt_move.dest;
Alyssa Rosenzweigdff49862019-08-12 12:36:46 -07001374
1375 /* Emit the branch */
1376 emit_mir_instruction(ctx, ins);
1377}
1378
Alyssa Rosenzweig7229af72019-08-06 13:47:17 -07001379static void
1380emit_compute_builtin(compiler_context *ctx, nir_intrinsic_instr *instr)
1381{
1382 unsigned reg = nir_dest_index(ctx, &instr->dest);
1383 midgard_instruction ins = m_ld_compute_id(reg, 0);
1384 ins.mask = mask_of(3);
1385 ins.load_store.arg_1 = compute_builtin_arg(instr->intrinsic);
1386 emit_mir_instruction(ctx, ins);
1387}
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001388static void
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001389emit_intrinsic(compiler_context *ctx, nir_intrinsic_instr *instr)
1390{
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001391 unsigned offset = 0, reg;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001392
1393 switch (instr->intrinsic) {
1394 case nir_intrinsic_discard_if:
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001395 case nir_intrinsic_discard: {
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00001396 bool conditional = instr->intrinsic == nir_intrinsic_discard_if;
1397 struct midgard_instruction discard = v_branch(conditional, false);
1398 discard.branch.target_type = TARGET_DISCARD;
Alyssa Rosenzweigd6e4e362019-08-26 13:59:29 -07001399
1400 if (conditional)
1401 discard.src[0] = nir_src_index(ctx, &instr->src[0]);
1402
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00001403 emit_mir_instruction(ctx, discard);
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07001404 schedule_barrier(ctx);
1405
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001406 break;
1407 }
1408
1409 case nir_intrinsic_load_uniform:
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001410 case nir_intrinsic_load_ubo:
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001411 case nir_intrinsic_load_ssbo:
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001412 case nir_intrinsic_load_input: {
1413 bool is_uniform = instr->intrinsic == nir_intrinsic_load_uniform;
1414 bool is_ubo = instr->intrinsic == nir_intrinsic_load_ubo;
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001415 bool is_ssbo = instr->intrinsic == nir_intrinsic_load_ssbo;
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001416
Alyssa Rosenzweigbbc050b2019-06-27 15:33:07 -07001417 /* Get the base type of the intrinsic */
Alyssa Rosenzweig8d747492019-06-27 14:13:10 -07001418 /* TODO: Infer type? Does it matter? */
1419 nir_alu_type t =
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001420 (is_ubo || is_ssbo) ? nir_type_uint : nir_intrinsic_type(instr);
Alyssa Rosenzweigbbc050b2019-06-27 15:33:07 -07001421 t = nir_alu_type_get_base_type(t);
1422
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001423 if (!(is_ubo || is_ssbo)) {
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001424 offset = nir_intrinsic_base(instr);
1425 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001426
Alyssa Rosenzweigc1715b52019-05-22 02:44:12 +00001427 unsigned nr_comp = nir_intrinsic_dest_components(instr);
Alyssa Rosenzweig6a466c02019-04-20 23:52:42 +00001428
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001429 nir_src *src_offset = nir_get_io_offset_src(instr);
1430
1431 bool direct = nir_src_is_const(*src_offset);
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001432 nir_src *indirect_offset = direct ? NULL : src_offset;
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001433
1434 if (direct)
1435 offset += nir_src_as_uint(*src_offset);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001436
Alyssa Rosenzweig43568f22019-06-06 08:16:04 -07001437 /* We may need to apply a fractional offset */
1438 int component = instr->intrinsic == nir_intrinsic_load_input ?
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001439 nir_intrinsic_component(instr) : 0;
Alyssa Rosenzweig4ed23b12019-02-07 04:56:13 +00001440 reg = nir_dest_index(ctx, &instr->dest);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001441
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001442 if (is_uniform && !ctx->is_blend) {
Alyssa Rosenzweigcf3bb102019-08-13 09:13:31 -07001443 emit_ubo_read(ctx, &instr->instr, reg, (ctx->sysval_count + offset) * 16, indirect_offset, 0);
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001444 } else if (is_ubo) {
1445 nir_src index = instr->src[0];
1446
1447 /* We don't yet support indirect UBOs. For indirect
1448 * block numbers (if that's possible), we don't know
1449 * enough about the hardware yet. For indirect sources,
1450 * we know what we need but we need to add some NIR
1451 * support for lowering correctly with respect to
1452 * 128-bit reads */
1453
1454 assert(nir_src_is_const(index));
1455 assert(nir_src_is_const(*src_offset));
1456
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001457 uint32_t uindex = nir_src_as_uint(index) + 1;
Alyssa Rosenzweigcf3bb102019-08-13 09:13:31 -07001458 emit_ubo_read(ctx, &instr->instr, reg, offset, NULL, uindex);
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001459 } else if (is_ssbo) {
1460 nir_src index = instr->src[0];
1461 assert(nir_src_is_const(index));
1462 uint32_t uindex = nir_src_as_uint(index);
1463
1464 emit_ssbo_access(ctx, &instr->instr, true, reg, offset, indirect_offset, uindex);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001465 } else if (ctx->stage == MESA_SHADER_FRAGMENT && !ctx->is_blend) {
Alyssa Rosenzweig9b97ed12019-06-28 09:30:59 -07001466 emit_varying_read(ctx, reg, offset, nr_comp, component, !direct ? &instr->src[0] : NULL, t);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001467 } else if (ctx->is_blend) {
Alyssa Rosenzweig39104222019-05-06 02:12:41 +00001468 /* For blend shaders, load the input color, which is
1469 * preloaded to r0 */
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001470
Alyssa Rosenzweigc3a46e72019-10-30 16:29:28 -04001471 midgard_instruction move = v_mov(SSA_FIXED_REGISTER(0), reg);
Alyssa Rosenzweig005d9b12019-05-20 00:46:48 +00001472 emit_mir_instruction(ctx, move);
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07001473 schedule_barrier(ctx);
Alyssa Rosenzweig39104222019-05-06 02:12:41 +00001474 } else if (ctx->stage == MESA_SHADER_VERTEX) {
Alyssa Rosenzweig74ab80b2019-05-14 04:11:36 +00001475 midgard_instruction ins = m_ld_attr_32(reg, offset);
Alyssa Rosenzweigc9087722019-08-01 13:29:01 -07001476 ins.load_store.arg_1 = 0x1E;
1477 ins.load_store.arg_2 = 0x1E;
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -07001478 ins.mask = mask_of(nr_comp);
Alyssa Rosenzweigbbc050b2019-06-27 15:33:07 -07001479
1480 /* Use the type appropriate load */
1481 switch (t) {
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001482 case nir_type_uint:
1483 case nir_type_bool:
1484 ins.load_store.op = midgard_op_ld_attr_32u;
1485 break;
1486 case nir_type_int:
1487 ins.load_store.op = midgard_op_ld_attr_32i;
1488 break;
1489 case nir_type_float:
1490 ins.load_store.op = midgard_op_ld_attr_32;
1491 break;
1492 default:
1493 unreachable("Attempted to load unknown type");
1494 break;
Alyssa Rosenzweigbbc050b2019-06-27 15:33:07 -07001495 }
1496
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001497 emit_mir_instruction(ctx, ins);
1498 } else {
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +01001499 DBG("Unknown load\n");
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001500 assert(0);
1501 }
1502
1503 break;
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001504 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001505
Alyssa Rosenzweig1686ef82019-07-01 17:23:58 -07001506 /* Reads 128-bit value raw off the tilebuffer during blending, tasty */
1507
Alyssa Rosenzweig541b3292019-07-01 15:02:40 -07001508 case nir_intrinsic_load_raw_output_pan:
Alyssa Rosenzweig843874c2019-11-06 21:50:32 -05001509 case nir_intrinsic_load_output_u8_as_fp16_pan:
Alyssa Rosenzweig39104222019-05-06 02:12:41 +00001510 reg = nir_dest_index(ctx, &instr->dest);
Alyssa Rosenzweig541b3292019-07-01 15:02:40 -07001511 assert(ctx->is_blend);
Alyssa Rosenzweig1686ef82019-07-01 17:23:58 -07001512
Alyssa Rosenzweig843874c2019-11-06 21:50:32 -05001513 /* T720 and below use different blend opcodes with slightly
1514 * different semantics than T760 and up */
1515
Alyssa Rosenzweig1a4153b2019-08-30 17:29:17 -07001516 midgard_instruction ld = m_ld_color_buffer_8(reg, 0);
Alyssa Rosenzweigfcf144d2019-11-19 20:55:42 -05001517 bool old_blend = ctx->quirks & MIDGARD_OLD_BLEND;
Alyssa Rosenzweig843874c2019-11-06 21:50:32 -05001518
1519 if (instr->intrinsic == nir_intrinsic_load_output_u8_as_fp16_pan) {
1520 ld.load_store.op = old_blend ?
1521 midgard_op_ld_color_buffer_u8_as_fp16_old :
1522 midgard_op_ld_color_buffer_u8_as_fp16;
1523
1524 if (old_blend) {
1525 ld.load_store.address = 1;
1526 ld.load_store.arg_2 = 0x1E;
1527 }
1528
1529 for (unsigned c = 2; c < 16; ++c)
1530 ld.swizzle[0][c] = 0;
1531 }
1532
Alyssa Rosenzweig1a4153b2019-08-30 17:29:17 -07001533 emit_mir_instruction(ctx, ld);
Alyssa Rosenzweig39104222019-05-06 02:12:41 +00001534 break;
1535
1536 case nir_intrinsic_load_blend_const_color_rgba: {
1537 assert(ctx->is_blend);
1538 reg = nir_dest_index(ctx, &instr->dest);
1539
1540 /* Blend constants are embedded directly in the shader and
1541 * patched in, so we use some magic routing */
1542
Alyssa Rosenzweigc3a46e72019-10-30 16:29:28 -04001543 midgard_instruction ins = v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT), reg);
Alyssa Rosenzweig39104222019-05-06 02:12:41 +00001544 ins.has_constants = true;
1545 ins.has_blend_constant = true;
1546 emit_mir_instruction(ctx, ins);
1547 break;
1548 }
1549
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001550 case nir_intrinsic_store_output:
Karol Herbst1aabb792019-03-29 21:40:45 +01001551 assert(nir_src_is_const(instr->src[1]) && "no indirect outputs");
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001552
Karol Herbst1aabb792019-03-29 21:40:45 +01001553 offset = nir_intrinsic_base(instr) + nir_src_as_uint(instr->src[1]);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001554
Alyssa Rosenzweig4ed23b12019-02-07 04:56:13 +00001555 reg = nir_src_index(ctx, &instr->src[0]);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001556
1557 if (ctx->stage == MESA_SHADER_FRAGMENT) {
Alyssa Rosenzweigdff49862019-08-12 12:36:46 -07001558 /* Determine number of render targets */
1559 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
1571 unsigned 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
1578 for (unsigned i = 0; i < ARRAY_SIZE(st.swizzle[0]); ++i)
1579 st.swizzle[0][i] = MIN2(i + component, nr_comp);
1580
Alyssa Rosenzweig4aced182019-06-06 08:21:27 -07001581 emit_mir_instruction(ctx, st);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001582 } else {
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +01001583 DBG("Unknown store\n");
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001584 assert(0);
1585 }
1586
1587 break;
1588
Alyssa Rosenzweig541b3292019-07-01 15:02:40 -07001589 /* Special case of store_output for lowered blend shaders */
1590 case nir_intrinsic_store_raw_output_pan:
1591 assert (ctx->stage == MESA_SHADER_FRAGMENT);
1592 reg = nir_src_index(ctx, &instr->src[0]);
Alyssa Rosenzweig8555bff2019-11-26 08:48:33 -05001593
1594 if (ctx->quirks & MIDGARD_OLD_BLEND) {
1595 /* Suppose reg = qr0.xyzw. That means 4 8-bit ---> 1 32-bit. So
1596 * reg = r0.x. We want to splatter. So we can do a 32-bit move
1597 * of:
1598 *
1599 * imov r0.xyzw, r0.xxxx
1600 */
1601
1602 unsigned expanded = make_compiler_temp(ctx);
1603
1604 midgard_instruction splatter = v_mov(reg, expanded);
1605
1606 for (unsigned c = 0; c < 16; ++c)
1607 splatter.swizzle[1][c] = 0;
1608
1609 emit_mir_instruction(ctx, splatter);
Alyssa Rosenzweiga2d55032019-11-23 21:44:16 -05001610 emit_fragment_store(ctx, expanded, ctx->blend_rt);
Alyssa Rosenzweig8555bff2019-11-26 08:48:33 -05001611 } else
Alyssa Rosenzweiga2d55032019-11-23 21:44:16 -05001612 emit_fragment_store(ctx, reg, ctx->blend_rt);
Alyssa Rosenzweig541b3292019-07-01 15:02:40 -07001613
1614 break;
1615
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001616 case nir_intrinsic_store_ssbo:
1617 assert(nir_src_is_const(instr->src[1]));
1618
1619 bool direct_offset = nir_src_is_const(instr->src[2]);
1620 offset = direct_offset ? nir_src_as_uint(instr->src[2]) : 0;
1621 nir_src *indirect_offset = direct_offset ? NULL : &instr->src[2];
1622 reg = nir_src_index(ctx, &instr->src[0]);
1623
1624 uint32_t uindex = nir_src_as_uint(instr->src[1]);
1625
1626 emit_explicit_constant(ctx, reg, reg);
1627 emit_ssbo_access(ctx, &instr->instr, false, reg, offset, indirect_offset, uindex);
1628 break;
1629
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001630 case nir_intrinsic_load_viewport_scale:
1631 case nir_intrinsic_load_viewport_offset:
Alyssa Rosenzweig15954ab2019-08-06 14:07:10 -07001632 case nir_intrinsic_load_num_work_groups:
Alyssa Rosenzweig4e07e7b2019-11-21 08:42:28 -05001633 case nir_intrinsic_load_sampler_lod_parameters_pan:
Alyssa Rosenzweig61892742019-08-21 09:15:56 -07001634 emit_sysval_read(ctx, &instr->instr, ~0, 3);
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001635 break;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001636
Alyssa Rosenzweig7229af72019-08-06 13:47:17 -07001637 case nir_intrinsic_load_work_group_id:
1638 case nir_intrinsic_load_local_invocation_id:
1639 emit_compute_builtin(ctx, instr);
1640 break;
1641
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001642 default:
1643 printf ("Unhandled intrinsic\n");
1644 assert(0);
1645 break;
1646 }
1647}
1648
1649static unsigned
1650midgard_tex_format(enum glsl_sampler_dim dim)
1651{
1652 switch (dim) {
Alyssa Rosenzweig83c02a52019-06-17 14:26:08 -07001653 case GLSL_SAMPLER_DIM_1D:
1654 case GLSL_SAMPLER_DIM_BUF:
1655 return MALI_TEX_1D;
1656
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001657 case GLSL_SAMPLER_DIM_2D:
1658 case GLSL_SAMPLER_DIM_EXTERNAL:
Alyssa Rosenzweig44a6c382019-08-14 08:44:40 -07001659 case GLSL_SAMPLER_DIM_RECT:
Alyssa Rosenzweig83c02a52019-06-17 14:26:08 -07001660 return MALI_TEX_2D;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001661
1662 case GLSL_SAMPLER_DIM_3D:
Alyssa Rosenzweig83c02a52019-06-17 14:26:08 -07001663 return MALI_TEX_3D;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001664
1665 case GLSL_SAMPLER_DIM_CUBE:
Alyssa Rosenzweig83c02a52019-06-17 14:26:08 -07001666 return MALI_TEX_CUBE;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001667
1668 default:
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +01001669 DBG("Unknown sampler dim type\n");
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001670 assert(0);
1671 return 0;
1672 }
1673}
1674
Alyssa Rosenzweig213b6282019-06-18 09:02:20 -07001675/* Tries to attach an explicit LOD / bias as a constant. Returns whether this
1676 * was successful */
1677
1678static bool
1679pan_attach_constant_bias(
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001680 compiler_context *ctx,
1681 nir_src lod,
1682 midgard_texture_word *word)
Alyssa Rosenzweig213b6282019-06-18 09:02:20 -07001683{
1684 /* To attach as constant, it has to *be* constant */
1685
1686 if (!nir_src_is_const(lod))
1687 return false;
1688
1689 float f = nir_src_as_float(lod);
1690
1691 /* Break into fixed-point */
1692 signed lod_int = f;
1693 float lod_frac = f - lod_int;
1694
1695 /* Carry over negative fractions */
1696 if (lod_frac < 0.0) {
1697 lod_int--;
1698 lod_frac += 1.0;
1699 }
1700
1701 /* Encode */
1702 word->bias = float_to_ubyte(lod_frac);
1703 word->bias_int = lod_int;
1704
1705 return true;
1706}
1707
Alyssa Rosenzweige32af4b2019-06-26 16:12:28 -07001708static enum mali_sampler_type
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001709midgard_sampler_type(nir_alu_type t) {
1710 switch (nir_alu_type_get_base_type(t))
1711 {
1712 case nir_type_float:
1713 return MALI_SAMPLER_FLOAT;
1714 case nir_type_int:
1715 return MALI_SAMPLER_SIGNED;
1716 case nir_type_uint:
1717 return MALI_SAMPLER_UNSIGNED;
1718 default:
1719 unreachable("Unknown sampler type");
Alyssa Rosenzweige32af4b2019-06-26 16:12:28 -07001720 }
1721}
1722
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001723static void
Boris Brezillon5c17f842019-06-17 21:47:46 +02001724emit_texop_native(compiler_context *ctx, nir_tex_instr *instr,
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001725 unsigned midgard_texop)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001726{
1727 /* TODO */
1728 //assert (!instr->sampler);
1729 //assert (!instr->texture_array_size);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001730
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001731 int texture_index = instr->texture_index;
1732 int sampler_index = texture_index;
1733
Alyssa Rosenzweigb0e89412019-06-18 09:02:35 -07001734 /* No helper to build texture words -- we do it all here */
1735 midgard_instruction ins = {
1736 .type = TAG_TEXTURE_4,
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -07001737 .mask = 0xF,
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07001738 .dest = nir_dest_index(ctx, &instr->dest),
1739 .src = { ~0, ~0, ~0 },
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001740 .swizzle = SWIZZLE_IDENTITY_4,
Alyssa Rosenzweigb0e89412019-06-18 09:02:35 -07001741 .texture = {
1742 .op = midgard_texop,
1743 .format = midgard_tex_format(instr->sampler_dim),
1744 .texture_handle = texture_index,
1745 .sampler_handle = sampler_index,
Alyssa Rosenzweigb0e89412019-06-18 09:02:35 -07001746
1747 /* TODO: half */
1748 .in_reg_full = 1,
1749 .out_full = 1,
1750
Alyssa Rosenzweige32af4b2019-06-26 16:12:28 -07001751 .sampler_type = midgard_sampler_type(instr->dest_type),
Alyssa Rosenzweigb0e89412019-06-18 09:02:35 -07001752 }
1753 };
Alyssa Rosenzweig8429bee2019-06-14 16:03:39 -07001754
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001755 for (unsigned i = 0; i < instr->num_srcs; ++i) {
Alyssa Rosenzweiga19ca342019-06-11 09:23:05 -07001756 int index = nir_src_index(ctx, &instr->src[i].src);
Alyssa Rosenzweigedc8e412019-08-15 16:41:53 -07001757 unsigned nr_components = nir_src_num_components(instr->src[i].src);
Alyssa Rosenzweiga19ca342019-06-11 09:23:05 -07001758
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001759 switch (instr->src[i].src_type) {
1760 case nir_tex_src_coord: {
Alyssa Rosenzweigb6946d32019-07-25 08:44:53 -07001761 emit_explicit_constant(ctx, index, index);
1762
1763 /* Texelfetch coordinates uses all four elements
1764 * (xyz/index) regardless of texture dimensionality,
1765 * which means it's necessary to zero the unused
1766 * components to keep everything happy */
1767
1768 if (midgard_texop == TEXTURE_OP_TEXEL_FETCH) {
1769 unsigned old_index = index;
1770
1771 index = make_compiler_temp(ctx);
1772
1773 /* mov index, old_index */
Alyssa Rosenzweigc3a46e72019-10-30 16:29:28 -04001774 midgard_instruction mov = v_mov(old_index, index);
Alyssa Rosenzweigb6946d32019-07-25 08:44:53 -07001775 mov.mask = 0x3;
1776 emit_mir_instruction(ctx, mov);
1777
1778 /* mov index.zw, #0 */
Alyssa Rosenzweigc3a46e72019-10-30 16:29:28 -04001779 mov = v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT), index);
Alyssa Rosenzweigb6946d32019-07-25 08:44:53 -07001780 mov.has_constants = true;
1781 mov.mask = (1 << COMPONENT_Z) | (1 << COMPONENT_W);
1782 emit_mir_instruction(ctx, mov);
1783 }
1784
Alyssa Rosenzweig70b3e5d2019-03-28 04:27:13 +00001785 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE) {
Alyssa Rosenzweigfaf8ad42019-06-24 14:39:25 -07001786 /* texelFetch is undefined on samplerCube */
1787 assert(midgard_texop != TEXTURE_OP_TEXEL_FETCH);
1788
Alyssa Rosenzweigbe568402019-07-25 07:09:40 -07001789 /* For cubemaps, we use a special ld/st op to
1790 * select the face and copy the xy into the
Alyssa Rosenzweiga19ca342019-06-11 09:23:05 -07001791 * texture register */
Alyssa Rosenzweig70b3e5d2019-03-28 04:27:13 +00001792
Alyssa Rosenzweigb6946d32019-07-25 08:44:53 -07001793 unsigned temp = make_compiler_temp(ctx);
Alyssa Rosenzweig9ae4d362019-08-16 07:50:12 -07001794 midgard_instruction ld = m_ld_cubemap_coords(temp, 0);
Alyssa Rosenzweige7fd14c2019-10-26 15:50:38 -04001795 ld.src[1] = index;
Alyssa Rosenzweig9ae4d362019-08-16 07:50:12 -07001796 ld.mask = 0x3; /* xy */
1797 ld.load_store.arg_1 = 0x20;
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001798 ld.swizzle[1][3] = COMPONENT_X;
Alyssa Rosenzweig9ae4d362019-08-16 07:50:12 -07001799 emit_mir_instruction(ctx, ld);
Alyssa Rosenzweig70b3e5d2019-03-28 04:27:13 +00001800
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001801 ins.src[1] = temp;
1802 /* xyzw -> xyxx */
1803 ins.swizzle[1][2] = COMPONENT_X;
1804 ins.swizzle[1][3] = COMPONENT_X;
Alyssa Rosenzweig70b3e5d2019-03-28 04:27:13 +00001805 } else {
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001806 ins.src[1] = index;
Alyssa Rosenzweigb6946d32019-07-25 08:44:53 -07001807 }
Alyssa Rosenzweig70b3e5d2019-03-28 04:27:13 +00001808
Alyssa Rosenzweigb6946d32019-07-25 08:44:53 -07001809 if (instr->sampler_dim == GLSL_SAMPLER_DIM_2D) {
1810 /* Array component in w but NIR wants it in z */
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001811 if (nr_components == 3) {
1812 ins.swizzle[1][2] = COMPONENT_Z;
1813 ins.swizzle[1][3] = COMPONENT_Z;
1814 } else if (nr_components == 2) {
1815 ins.swizzle[1][2] = COMPONENT_X;
1816 ins.swizzle[1][3] = COMPONENT_X;
1817 } else
Alyssa Rosenzweigedc8e412019-08-15 16:41:53 -07001818 unreachable("Invalid texture 2D components");
Alyssa Rosenzweig70b3e5d2019-03-28 04:27:13 +00001819 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001820
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001821 break;
1822 }
1823
Alyssa Rosenzweig4012e062019-06-11 09:43:08 -07001824 case nir_tex_src_bias:
1825 case nir_tex_src_lod: {
Alyssa Rosenzweigb0e89412019-06-18 09:02:35 -07001826 /* Try as a constant if we can */
1827
1828 bool is_txf = midgard_texop == TEXTURE_OP_TEXEL_FETCH;
1829 if (!is_txf && pan_attach_constant_bias(ctx, instr->src[i].src, &ins.texture))
1830 break;
1831
Alyssa Rosenzweigb0e89412019-06-18 09:02:35 -07001832 ins.texture.lod_register = true;
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001833 ins.src[2] = index;
Alyssa Rosenzweigb6946d32019-07-25 08:44:53 -07001834 emit_explicit_constant(ctx, index, index);
Alyssa Rosenzweigb0e89412019-06-18 09:02:35 -07001835
Alyssa Rosenzweiga19ca342019-06-11 09:23:05 -07001836 break;
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001837 };
Alyssa Rosenzweiga19ca342019-06-11 09:23:05 -07001838
Alyssa Rosenzweig5062b612019-06-11 09:55:18 -07001839 default:
1840 unreachable("Unknown texture source type\n");
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001841 }
1842 }
1843
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001844 emit_mir_instruction(ctx, ins);
1845
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001846 /* Used for .cont and .last hinting */
1847 ctx->texture_op_count++;
1848}
1849
1850static void
Boris Brezillon5c17f842019-06-17 21:47:46 +02001851emit_tex(compiler_context *ctx, nir_tex_instr *instr)
1852{
1853 switch (instr->op) {
1854 case nir_texop_tex:
1855 case nir_texop_txb:
1856 emit_texop_native(ctx, instr, TEXTURE_OP_NORMAL);
1857 break;
1858 case nir_texop_txl:
1859 emit_texop_native(ctx, instr, TEXTURE_OP_LOD);
1860 break;
Alyssa Rosenzweigf4bb7f02019-06-21 16:17:34 -07001861 case nir_texop_txf:
1862 emit_texop_native(ctx, instr, TEXTURE_OP_TEXEL_FETCH);
1863 break;
Boris Brezillonc3558862019-06-17 22:13:04 +02001864 case nir_texop_txs:
Alyssa Rosenzweig61892742019-08-21 09:15:56 -07001865 emit_sysval_read(ctx, &instr->instr, ~0, 4);
Boris Brezillonc3558862019-06-17 22:13:04 +02001866 break;
Boris Brezillon5c17f842019-06-17 21:47:46 +02001867 default:
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001868 unreachable("Unhanlded texture op");
Boris Brezillon5c17f842019-06-17 21:47:46 +02001869 }
1870}
1871
1872static void
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001873emit_jump(compiler_context *ctx, nir_jump_instr *instr)
1874{
1875 switch (instr->type) {
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001876 case nir_jump_break: {
1877 /* Emit a branch out of the loop */
1878 struct midgard_instruction br = v_branch(false, false);
1879 br.branch.target_type = TARGET_BREAK;
1880 br.branch.target_break = ctx->current_loop_depth;
1881 emit_mir_instruction(ctx, br);
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001882 break;
1883 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001884
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001885 default:
1886 DBG("Unknown jump type %d\n", instr->type);
1887 break;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001888 }
1889}
1890
1891static void
1892emit_instr(compiler_context *ctx, struct nir_instr *instr)
1893{
1894 switch (instr->type) {
1895 case nir_instr_type_load_const:
1896 emit_load_const(ctx, nir_instr_as_load_const(instr));
1897 break;
1898
1899 case nir_instr_type_intrinsic:
1900 emit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
1901 break;
1902
1903 case nir_instr_type_alu:
1904 emit_alu(ctx, nir_instr_as_alu(instr));
1905 break;
1906
1907 case nir_instr_type_tex:
1908 emit_tex(ctx, nir_instr_as_tex(instr));
1909 break;
1910
1911 case nir_instr_type_jump:
1912 emit_jump(ctx, nir_instr_as_jump(instr));
1913 break;
1914
1915 case nir_instr_type_ssa_undef:
1916 /* Spurious */
1917 break;
1918
1919 default:
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +01001920 DBG("Unhandled instruction type\n");
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001921 break;
1922 }
1923}
1924
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001925
1926/* ALU instructions can inline or embed constants, which decreases register
1927 * pressure and saves space. */
1928
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07001929#define CONDITIONAL_ATTACH(idx) { \
1930 void *entry = _mesa_hash_table_u64_search(ctx->ssa_constants, alu->src[idx] + 1); \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001931\
1932 if (entry) { \
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07001933 attach_constants(ctx, alu, entry, alu->src[idx] + 1); \
1934 alu->src[idx] = SSA_FIXED_REGISTER(REGISTER_CONSTANT); \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001935 } \
1936}
1937
1938static void
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07001939inline_alu_constants(compiler_context *ctx, midgard_block *block)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001940{
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07001941 mir_foreach_instr_in_block(block, alu) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001942 /* Other instructions cannot inline constants */
1943 if (alu->type != TAG_ALU_4) continue;
Alyssa Rosenzweig5e06d902019-08-30 11:06:33 -07001944 if (alu->compact_branch) continue;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001945
1946 /* If there is already a constant here, we can do nothing */
1947 if (alu->has_constants) continue;
1948
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07001949 CONDITIONAL_ATTACH(0);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001950
1951 if (!alu->has_constants) {
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07001952 CONDITIONAL_ATTACH(1)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001953 } else if (!alu->inline_constant) {
1954 /* Corner case: _two_ vec4 constants, for instance with a
1955 * csel. For this case, we can only use a constant
1956 * register for one, we'll have to emit a move for the
1957 * other. Note, if both arguments are constants, then
1958 * necessarily neither argument depends on the value of
1959 * any particular register. As the destination register
1960 * will be wiped, that means we can spill the constant
1961 * to the destination register.
1962 */
1963
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07001964 void *entry = _mesa_hash_table_u64_search(ctx->ssa_constants, alu->src[1] + 1);
1965 unsigned scratch = alu->dest;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001966
1967 if (entry) {
Alyssa Rosenzweigc3a46e72019-10-30 16:29:28 -04001968 midgard_instruction ins = v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT), scratch);
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07001969 attach_constants(ctx, &ins, entry, alu->src[1] + 1);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001970
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001971 /* Set the source */
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07001972 alu->src[1] = scratch;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001973
1974 /* Inject us -before- the last instruction which set r31 */
Boris Brezillon938c5b02019-08-28 09:17:21 +02001975 mir_insert_instruction_before(ctx, mir_prev_op(alu), ins);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001976 }
1977 }
1978 }
1979}
1980
Alyssa Rosenzweig62a5ee32019-07-26 14:25:25 -07001981/* Being a little silly with the names, but returns the op that is the bitwise
1982 * inverse of the op with the argument switched. I.e. (f and g are
1983 * contrapositives):
1984 *
1985 * f(a, b) = ~g(b, a)
1986 *
1987 * Corollary: if g is the contrapositve of f, f is the contrapositive of g:
1988 *
1989 * f(a, b) = ~g(b, a)
1990 * ~f(a, b) = g(b, a)
1991 * ~f(a, b) = ~h(a, b) where h is the contrapositive of g
1992 * f(a, b) = h(a, b)
1993 *
1994 * Thus we define this function in pairs.
1995 */
1996
1997static inline midgard_alu_op
1998mir_contrapositive(midgard_alu_op op)
1999{
2000 switch (op) {
2001 case midgard_alu_op_flt:
2002 return midgard_alu_op_fle;
2003 case midgard_alu_op_fle:
2004 return midgard_alu_op_flt;
2005
2006 case midgard_alu_op_ilt:
2007 return midgard_alu_op_ile;
2008 case midgard_alu_op_ile:
2009 return midgard_alu_op_ilt;
2010
2011 default:
2012 unreachable("No known contrapositive");
2013 }
2014}
2015
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002016/* Midgard supports two types of constants, embedded constants (128-bit) and
2017 * inline constants (16-bit). Sometimes, especially with scalar ops, embedded
2018 * constants can be demoted to inline constants, for space savings and
2019 * sometimes a performance boost */
2020
2021static void
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07002022embedded_to_inline_constant(compiler_context *ctx, midgard_block *block)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002023{
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07002024 mir_foreach_instr_in_block(block, ins) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002025 if (!ins->has_constants) continue;
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002026 if (ins->has_inline_constant) continue;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002027
2028 /* Blend constants must not be inlined by definition */
2029 if (ins->has_blend_constant) continue;
2030
Alyssa Rosenzweige92caad2019-07-01 20:02:57 -07002031 /* We can inline 32-bit (sometimes) or 16-bit (usually) */
2032 bool is_16 = ins->alu.reg_mode == midgard_reg_mode_16;
2033 bool is_32 = ins->alu.reg_mode == midgard_reg_mode_32;
2034
2035 if (!(is_16 || is_32))
2036 continue;
2037
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002038 /* src1 cannot be an inline constant due to encoding
2039 * restrictions. So, if possible we try to flip the arguments
2040 * in that case */
2041
2042 int op = ins->alu.op;
2043
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002044 if (ins->src[0] == SSA_FIXED_REGISTER(REGISTER_CONSTANT)) {
Alyssa Rosenzweig62a5ee32019-07-26 14:25:25 -07002045 bool flip = alu_opcode_props[op].props & OP_COMMUTES;
2046
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002047 switch (op) {
Alyssa Rosenzweig62a5ee32019-07-26 14:25:25 -07002048 /* Conditionals can be inverted */
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002049 case midgard_alu_op_flt:
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002050 case midgard_alu_op_ilt:
Alyssa Rosenzweig62a5ee32019-07-26 14:25:25 -07002051 case midgard_alu_op_fle:
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002052 case midgard_alu_op_ile:
Alyssa Rosenzweig62a5ee32019-07-26 14:25:25 -07002053 ins->alu.op = mir_contrapositive(ins->alu.op);
2054 ins->invert = true;
2055 flip = true;
2056 break;
2057
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002058 case midgard_alu_op_fcsel:
2059 case midgard_alu_op_icsel:
Alyssa Rosenzweig1f345bc2019-04-24 01:15:15 +00002060 DBG("Missed non-commutative flip (%s)\n", alu_opcode_props[op].name);
Alyssa Rosenzweigbb1aff32019-04-24 02:18:28 +00002061 default:
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002062 break;
Alyssa Rosenzweigbb1aff32019-04-24 02:18:28 +00002063 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002064
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04002065 if (flip)
2066 mir_flip(ins);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002067 }
2068
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002069 if (ins->src[1] == SSA_FIXED_REGISTER(REGISTER_CONSTANT)) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002070 /* Extract the source information */
2071
2072 midgard_vector_alu_src *src;
2073 int q = ins->alu.src2;
2074 midgard_vector_alu_src *m = (midgard_vector_alu_src *) &q;
2075 src = m;
2076
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04002077 /* Component is from the swizzle. Take a nonzero component */
2078 assert(ins->mask);
2079 unsigned first_comp = ffs(ins->mask) - 1;
2080 unsigned component = ins->swizzle[1][first_comp];
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002081
2082 /* Scale constant appropriately, if we can legally */
2083 uint16_t scaled_constant = 0;
2084
Alyssa Rosenzweige92caad2019-07-01 20:02:57 -07002085 if (midgard_is_integer_op(op) || is_16) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002086 unsigned int *iconstants = (unsigned int *) ins->constants;
2087 scaled_constant = (uint16_t) iconstants[component];
2088
2089 /* Constant overflow after resize */
2090 if (scaled_constant != iconstants[component])
2091 continue;
2092 } else {
Alyssa Rosenzweig0acb5c12019-08-23 16:02:49 -07002093 float *f = (float *) ins->constants;
2094 float original = f[component];
Alyssa Rosenzweig39786142019-04-28 15:46:47 +00002095 scaled_constant = _mesa_float_to_half(original);
2096
2097 /* Check for loss of precision. If this is
2098 * mediump, we don't care, but for a highp
2099 * shader, we need to pay attention. NIR
2100 * doesn't yet tell us which mode we're in!
2101 * Practically this prevents most constants
2102 * from being inlined, sadly. */
2103
2104 float fp32 = _mesa_half_to_float(scaled_constant);
2105
2106 if (fp32 != original)
2107 continue;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002108 }
2109
2110 /* We don't know how to handle these with a constant */
2111
Alyssa Rosenzweigc45487b2019-07-26 11:52:30 -07002112 if (mir_nontrivial_source2_mod_simple(ins) || src->rep_low || src->rep_high) {
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +01002113 DBG("Bailing inline constant...\n");
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002114 continue;
2115 }
2116
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04002117 /* Make sure that the constant is not itself a vector
2118 * by checking if all accessed values are the same. */
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002119
Alyssa Rosenzweig0acb5c12019-08-23 16:02:49 -07002120 uint32_t *cons = ins->constants;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002121 uint32_t value = cons[component];
2122
2123 bool is_vector = false;
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -07002124 unsigned mask = effective_writemask(&ins->alu, ins->mask);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002125
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04002126 for (unsigned c = 0; c < MIR_VEC_COMPONENTS; ++c) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002127 /* We only care if this component is actually used */
2128 if (!(mask & (1 << c)))
2129 continue;
2130
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04002131 uint32_t test = cons[ins->swizzle[1][c]];
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002132
2133 if (test != value) {
2134 is_vector = true;
2135 break;
2136 }
2137 }
2138
2139 if (is_vector)
2140 continue;
2141
2142 /* Get rid of the embedded constant */
2143 ins->has_constants = false;
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002144 ins->src[1] = ~0;
2145 ins->has_inline_constant = true;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002146 ins->inline_constant = scaled_constant;
2147 }
2148 }
2149}
2150
Alyssa Rosenzweigae20bee2019-06-06 11:19:13 -07002151/* Dead code elimination for branches at the end of a block - only one branch
2152 * per block is legal semantically */
2153
2154static void
2155midgard_opt_cull_dead_branch(compiler_context *ctx, midgard_block *block)
2156{
2157 bool branched = false;
2158
2159 mir_foreach_instr_in_block_safe(block, ins) {
2160 if (!midgard_is_branch_unit(ins->unit)) continue;
2161
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07002162 if (branched)
Alyssa Rosenzweigae20bee2019-06-06 11:19:13 -07002163 mir_remove_instruction(ins);
Alyssa Rosenzweigae20bee2019-06-06 11:19:13 -07002164
2165 branched = true;
2166 }
2167}
2168
Alyssa Rosenzweig4a03d372019-05-23 03:01:32 +00002169/* fmov.pos is an idiom for fpos. Propoagate the .pos up to the source, so then
2170 * the move can be propagated away entirely */
2171
2172static bool
Alyssa Rosenzweig67804812019-06-05 15:17:45 -07002173mir_compose_float_outmod(midgard_outmod_float *outmod, midgard_outmod_float comp)
Alyssa Rosenzweig4a03d372019-05-23 03:01:32 +00002174{
2175 /* Nothing to do */
2176 if (comp == midgard_outmod_none)
2177 return true;
2178
2179 if (*outmod == midgard_outmod_none) {
2180 *outmod = comp;
2181 return true;
2182 }
2183
2184 /* TODO: Compose rules */
2185 return false;
2186}
2187
2188static bool
2189midgard_opt_pos_propagate(compiler_context *ctx, midgard_block *block)
2190{
2191 bool progress = false;
2192
2193 mir_foreach_instr_in_block_safe(block, ins) {
2194 if (ins->type != TAG_ALU_4) continue;
2195 if (ins->alu.op != midgard_alu_op_fmov) continue;
2196 if (ins->alu.outmod != midgard_outmod_pos) continue;
2197
2198 /* TODO: Registers? */
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002199 unsigned src = ins->src[1];
Alyssa Rosenzweig9beb3392019-07-26 11:30:06 -07002200 if (src & IS_REG) continue;
Alyssa Rosenzweig4a03d372019-05-23 03:01:32 +00002201
2202 /* There might be a source modifier, too */
2203 if (mir_nontrivial_source2_mod(ins)) continue;
2204
2205 /* Backpropagate the modifier */
2206 mir_foreach_instr_in_block_from_rev(block, v, mir_prev_op(ins)) {
2207 if (v->type != TAG_ALU_4) continue;
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002208 if (v->dest != src) continue;
Alyssa Rosenzweig4a03d372019-05-23 03:01:32 +00002209
Alyssa Rosenzweig67804812019-06-05 15:17:45 -07002210 /* Can we even take a float outmod? */
2211 if (midgard_is_integer_out_op(v->alu.op)) continue;
2212
2213 midgard_outmod_float temp = v->alu.outmod;
2214 progress |= mir_compose_float_outmod(&temp, ins->alu.outmod);
Alyssa Rosenzweig4a03d372019-05-23 03:01:32 +00002215
2216 /* Throw in the towel.. */
2217 if (!progress) break;
2218
2219 /* Otherwise, transfer the modifier */
2220 v->alu.outmod = temp;
2221 ins->alu.outmod = midgard_outmod_none;
2222
2223 break;
2224 }
2225 }
2226
2227 return progress;
2228}
2229
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002230static void
2231emit_fragment_epilogue(compiler_context *ctx)
2232{
Alyssa Rosenzweigdff49862019-08-12 12:36:46 -07002233 /* Just emit the last chunk with the branch */
Alyssa Rosenzweig61892742019-08-21 09:15:56 -07002234 EMIT(alu_br_compact_cond, midgard_jmp_writeout_op_writeout, TAG_ALU_4, ~0, midgard_condition_always);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002235}
2236
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002237static midgard_block *
2238emit_block(compiler_context *ctx, nir_block *block)
2239{
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002240 midgard_block *this_block = ctx->after_block;
2241 ctx->after_block = NULL;
2242
2243 if (!this_block)
Alyssa Rosenzweigaeeeef12019-08-15 08:11:10 -07002244 this_block = create_empty_block(ctx);
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002245
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002246 list_addtail(&this_block->link, &ctx->blocks);
2247
2248 this_block->is_scheduled = false;
2249 ++ctx->block_count;
2250
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002251 /* Set up current block */
2252 list_inithead(&this_block->instructions);
2253 ctx->current_block = this_block;
2254
2255 nir_foreach_instr(instr, block) {
2256 emit_instr(ctx, instr);
2257 ++ctx->instruction_count;
2258 }
2259
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002260 return this_block;
2261}
2262
2263static midgard_block *emit_cf_list(struct compiler_context *ctx, struct exec_list *list);
2264
2265static void
2266emit_if(struct compiler_context *ctx, nir_if *nif)
2267{
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002268 midgard_block *before_block = ctx->current_block;
2269
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002270 /* Speculatively emit the branch, but we can't fill it in until later */
2271 EMIT(branch, true, true);
2272 midgard_instruction *then_branch = mir_last_in_block(ctx->current_block);
Alyssa Rosenzweigd6e4e362019-08-26 13:59:29 -07002273 then_branch->src[0] = nir_src_index(ctx, &nif->condition);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002274
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002275 /* Emit the two subblocks. */
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002276 midgard_block *then_block = emit_cf_list(ctx, &nif->then_list);
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002277 midgard_block *end_then_block = ctx->current_block;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002278
2279 /* Emit a jump from the end of the then block to the end of the else */
2280 EMIT(branch, false, false);
2281 midgard_instruction *then_exit = mir_last_in_block(ctx->current_block);
2282
2283 /* Emit second block, and check if it's empty */
2284
2285 int else_idx = ctx->block_count;
2286 int count_in = ctx->instruction_count;
2287 midgard_block *else_block = emit_cf_list(ctx, &nif->else_list);
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002288 midgard_block *end_else_block = ctx->current_block;
Alyssa Rosenzweig2c747092019-02-17 05:14:24 +00002289 int after_else_idx = ctx->block_count;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002290
2291 /* Now that we have the subblocks emitted, fix up the branches */
2292
2293 assert(then_block);
2294 assert(else_block);
2295
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002296 if (ctx->instruction_count == count_in) {
2297 /* The else block is empty, so don't emit an exit jump */
2298 mir_remove_instruction(then_exit);
Alyssa Rosenzweig2c747092019-02-17 05:14:24 +00002299 then_branch->branch.target_block = after_else_idx;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002300 } else {
2301 then_branch->branch.target_block = else_idx;
Alyssa Rosenzweig2c747092019-02-17 05:14:24 +00002302 then_exit->branch.target_block = after_else_idx;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002303 }
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002304
2305 /* Wire up the successors */
2306
Alyssa Rosenzweigaeeeef12019-08-15 08:11:10 -07002307 ctx->after_block = create_empty_block(ctx);
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002308
2309 midgard_block_add_successor(before_block, then_block);
2310 midgard_block_add_successor(before_block, else_block);
2311
2312 midgard_block_add_successor(end_then_block, ctx->after_block);
2313 midgard_block_add_successor(end_else_block, ctx->after_block);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002314}
2315
2316static void
2317emit_loop(struct compiler_context *ctx, nir_loop *nloop)
2318{
2319 /* Remember where we are */
2320 midgard_block *start_block = ctx->current_block;
2321
Alyssa Rosenzweig521ac6e2019-04-21 16:22:44 +00002322 /* Allocate a loop number, growing the current inner loop depth */
2323 int loop_idx = ++ctx->current_loop_depth;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002324
2325 /* Get index from before the body so we can loop back later */
2326 int start_idx = ctx->block_count;
2327
2328 /* Emit the body itself */
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002329 midgard_block *loop_block = emit_cf_list(ctx, &nloop->body);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002330
2331 /* Branch back to loop back */
2332 struct midgard_instruction br_back = v_branch(false, false);
2333 br_back.branch.target_block = start_idx;
2334 emit_mir_instruction(ctx, br_back);
2335
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002336 /* Mark down that branch in the graph. */
2337 midgard_block_add_successor(start_block, loop_block);
2338 midgard_block_add_successor(ctx->current_block, loop_block);
Alyssa Rosenzweigc0fb2602019-04-21 03:29:47 +00002339
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002340 /* Find the index of the block about to follow us (note: we don't add
2341 * one; blocks are 0-indexed so we get a fencepost problem) */
2342 int break_block_idx = ctx->block_count;
2343
2344 /* Fix up the break statements we emitted to point to the right place,
2345 * now that we can allocate a block number for them */
Alyssa Rosenzweigaeeeef12019-08-15 08:11:10 -07002346 ctx->after_block = create_empty_block(ctx);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002347
2348 list_for_each_entry_from(struct midgard_block, block, start_block, &ctx->blocks, link) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002349 mir_foreach_instr_in_block(block, ins) {
2350 if (ins->type != TAG_ALU_4) continue;
2351 if (!ins->compact_branch) continue;
2352 if (ins->prepacked_branch) continue;
2353
2354 /* We found a branch -- check the type to see if we need to do anything */
2355 if (ins->branch.target_type != TARGET_BREAK) continue;
2356
2357 /* It's a break! Check if it's our break */
2358 if (ins->branch.target_break != loop_idx) continue;
2359
2360 /* Okay, cool, we're breaking out of this loop.
2361 * Rewrite from a break to a goto */
2362
2363 ins->branch.target_type = TARGET_GOTO;
2364 ins->branch.target_block = break_block_idx;
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002365
2366 midgard_block_add_successor(block, ctx->after_block);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002367 }
2368 }
Alyssa Rosenzweig521ac6e2019-04-21 16:22:44 +00002369
2370 /* Now that we've finished emitting the loop, free up the depth again
2371 * so we play nice with recursion amid nested loops */
2372 --ctx->current_loop_depth;
Alyssa Rosenzweig7ad65162019-07-09 11:10:49 -07002373
2374 /* Dump loop stats */
2375 ++ctx->loop_count;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002376}
2377
2378static midgard_block *
2379emit_cf_list(struct compiler_context *ctx, struct exec_list *list)
2380{
2381 midgard_block *start_block = NULL;
2382
2383 foreach_list_typed(nir_cf_node, node, node, list) {
2384 switch (node->type) {
2385 case nir_cf_node_block: {
2386 midgard_block *block = emit_block(ctx, nir_cf_node_as_block(node));
2387
2388 if (!start_block)
2389 start_block = block;
2390
2391 break;
2392 }
2393
2394 case nir_cf_node_if:
2395 emit_if(ctx, nir_cf_node_as_if(node));
2396 break;
2397
2398 case nir_cf_node_loop:
2399 emit_loop(ctx, nir_cf_node_as_loop(node));
2400 break;
2401
2402 case nir_cf_node_function:
2403 assert(0);
2404 break;
2405 }
2406 }
2407
2408 return start_block;
2409}
2410
Alyssa Rosenzweig5e55c112019-02-17 03:35:03 +00002411/* Due to lookahead, we need to report the first tag executed in the command
2412 * stream and in branch targets. An initial block might be empty, so iterate
2413 * until we find one that 'works' */
2414
2415static unsigned
2416midgard_get_first_tag_from_block(compiler_context *ctx, unsigned block_idx)
2417{
2418 midgard_block *initial_block = mir_get_block(ctx, block_idx);
2419
2420 unsigned first_tag = 0;
2421
Alyssa Rosenzweig73c40d62019-07-31 15:49:30 -07002422 mir_foreach_block_from(ctx, initial_block, v) {
Alyssa Rosenzweig45ac8ea2019-11-04 10:32:49 -05002423 if (v->quadword_count) {
2424 midgard_bundle *initial_bundle =
2425 util_dynarray_element(&v->bundles, midgard_bundle, 0);
Alyssa Rosenzweig5e55c112019-02-17 03:35:03 +00002426
Alyssa Rosenzweig5e55c112019-02-17 03:35:03 +00002427 first_tag = initial_bundle->tag;
2428 break;
2429 }
Alyssa Rosenzweig73c40d62019-07-31 15:49:30 -07002430 }
Alyssa Rosenzweig5e55c112019-02-17 03:35:03 +00002431
Alyssa Rosenzweig5e55c112019-02-17 03:35:03 +00002432 return first_tag;
2433}
2434
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002435int
Alyssa Rosenzweiga2d55032019-11-23 21:44:16 -05002436midgard_compile_shader_nir(nir_shader *nir, midgard_program *program, bool is_blend, unsigned blend_rt, unsigned gpu_id)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002437{
2438 struct util_dynarray *compiled = &program->compiled;
2439
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002440 midgard_debug = debug_get_option_midgard_debug();
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +01002441
Alyssa Rosenzweig4fa09322019-08-15 08:10:46 -07002442 /* TODO: Bound against what? */
2443 compiler_context *ctx = rzalloc(NULL, compiler_context);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002444
Alyssa Rosenzweig4fa09322019-08-15 08:10:46 -07002445 ctx->nir = nir;
Alyssa Rosenzweig4fa09322019-08-15 08:10:46 -07002446 ctx->stage = nir->info.stage;
2447 ctx->is_blend = is_blend;
2448 ctx->alpha_ref = program->alpha_ref;
Alyssa Rosenzweiga2d55032019-11-23 21:44:16 -05002449 ctx->blend_rt = blend_rt;
Alyssa Rosenzweigfcf144d2019-11-19 20:55:42 -05002450 ctx->quirks = midgard_get_quirks(gpu_id);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002451
Alyssa Rosenzweig3174bc92019-07-16 14:10:08 -07002452 /* Start off with a safe cutoff, allowing usage of all 16 work
2453 * registers. Later, we'll promote uniform reads to uniform registers
2454 * if we determine it is beneficial to do so */
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002455 ctx->uniform_cutoff = 8;
2456
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002457 /* Initialize at a global (not block) level hash tables */
2458
2459 ctx->ssa_constants = _mesa_hash_table_u64_create(NULL);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002460 ctx->hash_to_temp = _mesa_hash_table_u64_create(NULL);
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00002461 ctx->sysval_to_id = _mesa_hash_table_u64_create(NULL);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002462
Alyssa Rosenzweigb98955e2019-03-15 23:25:55 +00002463 /* Record the varying mapping for the command stream's bookkeeping */
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002464
Alyssa Rosenzweigb98955e2019-03-15 23:25:55 +00002465 struct exec_list *varyings =
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002466 ctx->stage == MESA_SHADER_VERTEX ? &nir->outputs : &nir->inputs;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002467
Alyssa Rosenzweigde8d49a2019-06-06 09:15:26 -07002468 unsigned max_varying = 0;
Alyssa Rosenzweigb98955e2019-03-15 23:25:55 +00002469 nir_foreach_variable(var, varyings) {
2470 unsigned loc = var->data.driver_location;
Alyssa Rosenzweig1f7b3882019-04-20 23:39:29 +00002471 unsigned sz = glsl_type_size(var->type, FALSE);
2472
Boris Brezillon749c5442019-06-13 14:56:02 +02002473 for (int c = 0; c < sz; ++c) {
2474 program->varyings[loc + c] = var->data.location + c;
2475 max_varying = MAX2(max_varying, loc + c);
Alyssa Rosenzweig1f7b3882019-04-20 23:39:29 +00002476 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002477 }
2478
Alyssa Rosenzweigde8d49a2019-06-06 09:15:26 -07002479 /* Lower gl_Position pre-optimisation, but after lowering vars to ssa
2480 * (so we don't accidentally duplicate the epilogue since mesa/st has
2481 * messed with our I/O quite a bit already) */
2482
2483 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
Alyssa Rosenzweig1e2cb3e2019-04-07 16:37:28 +00002484
Alyssa Rosenzweigbb483a92019-07-10 11:30:00 -07002485 if (ctx->stage == MESA_SHADER_VERTEX) {
Alyssa Rosenzweig1e2cb3e2019-04-07 16:37:28 +00002486 NIR_PASS_V(nir, nir_lower_viewport_transform);
Alyssa Rosenzweig20237162019-08-26 12:14:11 -07002487 NIR_PASS_V(nir, nir_lower_point_size, 1.0, 1024.0);
Alyssa Rosenzweigbb483a92019-07-10 11:30:00 -07002488 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002489
2490 NIR_PASS_V(nir, nir_lower_var_copies);
2491 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
2492 NIR_PASS_V(nir, nir_split_var_copies);
2493 NIR_PASS_V(nir, nir_lower_var_copies);
2494 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
2495 NIR_PASS_V(nir, nir_lower_var_copies);
2496 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00002497
Eric Anholt771adff2019-04-08 16:32:01 -07002498 NIR_PASS_V(nir, nir_lower_io, nir_var_all, glsl_type_size, 0);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002499
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002500 /* Optimisation passes */
2501
Alyssa Rosenzweigbda2bb32019-11-21 08:45:27 -05002502 optimise_nir(nir, ctx->quirks);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002503
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002504 if (midgard_debug & MIDGARD_DBG_SHADERS) {
2505 nir_print_shader(nir, stdout);
2506 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002507
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00002508 /* Assign sysvals and counts, now that we're sure
2509 * (post-optimisation) */
2510
2511 midgard_nir_assign_sysvals(ctx, nir);
2512
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002513 program->uniform_count = nir->num_uniforms;
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00002514 program->sysval_count = ctx->sysval_count;
2515 memcpy(program->sysvals, ctx->sysvals, sizeof(ctx->sysvals[0]) * ctx->sysval_count);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002516
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002517 nir_foreach_function(func, nir) {
2518 if (!func->impl)
2519 continue;
2520
2521 list_inithead(&ctx->blocks);
2522 ctx->block_count = 0;
2523 ctx->func = func;
2524
2525 emit_cf_list(ctx, &func->impl->body);
Alyssa Rosenzweigb4b2e112019-08-15 08:23:48 -07002526
2527 /* Emit empty exit block with successor */
2528
2529 struct midgard_block *semi_end = ctx->current_block;
2530
2531 struct midgard_block *end =
2532 emit_block(ctx, func->impl->end_block);
2533
Alyssa Rosenzweiga8eafb02019-08-27 12:20:06 -07002534 if (ctx->stage == MESA_SHADER_FRAGMENT)
2535 emit_fragment_epilogue(ctx);
2536
Alyssa Rosenzweigb4b2e112019-08-15 08:23:48 -07002537 midgard_block_add_successor(semi_end, end);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002538
2539 break; /* TODO: Multi-function shaders */
2540 }
2541
2542 util_dynarray_init(compiled, NULL);
2543
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07002544 /* Per-block lowering before opts */
2545
2546 mir_foreach_block(ctx, block) {
2547 inline_alu_constants(ctx, block);
2548 midgard_opt_promote_fmov(ctx, block);
2549 embedded_to_inline_constant(ctx, block);
2550 }
Alyssa Rosenzweig4d995e02019-04-22 04:58:53 +00002551 /* MIR-level optimizations */
Alyssa Rosenzweig84f09ff2019-04-21 16:11:11 +00002552
Alyssa Rosenzweig4d995e02019-04-22 04:58:53 +00002553 bool progress = false;
2554
2555 do {
2556 progress = false;
2557
2558 mir_foreach_block(ctx, block) {
Alyssa Rosenzweig4a03d372019-05-23 03:01:32 +00002559 progress |= midgard_opt_pos_propagate(ctx, block);
Alyssa Rosenzweig4d995e02019-04-22 04:58:53 +00002560 progress |= midgard_opt_copy_prop(ctx, block);
2561 progress |= midgard_opt_dead_code_eliminate(ctx, block);
Alyssa Rosenzweig9ce75822019-07-24 15:37:24 -07002562 progress |= midgard_opt_combine_projection(ctx, block);
2563 progress |= midgard_opt_varying_projection(ctx, block);
Alyssa Rosenzweig620c2712019-07-26 13:14:55 -07002564 progress |= midgard_opt_not_propagate(ctx, block);
Alyssa Rosenzweigd066ca352019-07-26 13:32:54 -07002565 progress |= midgard_opt_fuse_src_invert(ctx, block);
Alyssa Rosenzweigb821e1b2019-07-26 13:08:54 -07002566 progress |= midgard_opt_fuse_dest_invert(ctx, block);
Alyssa Rosenzweigc20063a2019-09-28 12:39:15 -04002567 progress |= midgard_opt_csel_invert(ctx, block);
Alyssa Rosenzweig4d995e02019-04-22 04:58:53 +00002568 }
2569 } while (progress);
Alyssa Rosenzweig84f09ff2019-04-21 16:11:11 +00002570
Alyssa Rosenzweig159abd52019-07-26 11:15:31 -07002571 mir_foreach_block(ctx, block) {
2572 midgard_lower_invert(ctx, block);
Alyssa Rosenzweig8f887322019-07-29 15:11:12 -07002573 midgard_lower_derivatives(ctx, block);
Alyssa Rosenzweig159abd52019-07-26 11:15:31 -07002574 }
2575
Alyssa Rosenzweigae20bee2019-06-06 11:19:13 -07002576 /* Nested control-flow can result in dead branches at the end of the
2577 * block. This messes with our analysis and is just dead code, so cull
2578 * them */
2579 mir_foreach_block(ctx, block) {
2580 midgard_opt_cull_dead_branch(ctx, block);
2581 }
2582
Alyssa Rosenzweig159abd52019-07-26 11:15:31 -07002583 /* Ensure we were lowered */
2584 mir_foreach_instr_global(ctx, ins) {
2585 assert(!ins->invert);
2586 }
2587
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002588 /* Schedule! */
2589 schedule_program(ctx);
Alyssa Rosenzweig9dc3b182019-12-06 09:32:38 -05002590 mir_ra(ctx);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002591
2592 /* Now that all the bundles are scheduled and we can calculate block
2593 * sizes, emit actual branch instructions rather than placeholders */
2594
2595 int br_block_idx = 0;
2596
2597 mir_foreach_block(ctx, block) {
2598 util_dynarray_foreach(&block->bundles, midgard_bundle, bundle) {
2599 for (int c = 0; c < bundle->instruction_count; ++c) {
Alyssa Rosenzweig3c7abbf2019-05-22 04:33:21 +00002600 midgard_instruction *ins = bundle->instructions[c];
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002601
Alyssa Rosenzweig5abb7b52019-02-17 22:09:09 +00002602 if (!midgard_is_branch_unit(ins->unit)) continue;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002603
2604 if (ins->prepacked_branch) continue;
2605
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00002606 /* Parse some basic branch info */
2607 bool is_compact = ins->unit == ALU_ENAB_BR_COMPACT;
2608 bool is_conditional = ins->branch.conditional;
2609 bool is_inverted = ins->branch.invert_conditional;
2610 bool is_discard = ins->branch.target_type == TARGET_DISCARD;
2611
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002612 /* Determine the block we're jumping to */
2613 int target_number = ins->branch.target_block;
2614
Alyssa Rosenzweig3c7abbf2019-05-22 04:33:21 +00002615 /* Report the destination tag */
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00002616 int dest_tag = is_discard ? 0 : midgard_get_first_tag_from_block(ctx, target_number);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002617
Alyssa Rosenzweig3c7abbf2019-05-22 04:33:21 +00002618 /* Count up the number of quadwords we're
2619 * jumping over = number of quadwords until
2620 * (br_block_idx, target_number) */
2621
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002622 int quadword_offset = 0;
2623
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00002624 if (is_discard) {
Alyssa Rosenzweig7f75b2b2019-07-30 17:07:25 -07002625 /* Ignored */
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00002626 } else if (target_number > br_block_idx) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002627 /* Jump forward */
2628
2629 for (int idx = br_block_idx + 1; idx < target_number; ++idx) {
2630 midgard_block *blk = mir_get_block(ctx, idx);
2631 assert(blk);
2632
2633 quadword_offset += blk->quadword_count;
2634 }
2635 } else {
2636 /* Jump backwards */
2637
2638 for (int idx = br_block_idx; idx >= target_number; --idx) {
2639 midgard_block *blk = mir_get_block(ctx, idx);
2640 assert(blk);
2641
2642 quadword_offset -= blk->quadword_count;
2643 }
2644 }
2645
Alyssa Rosenzweig5abb7b52019-02-17 22:09:09 +00002646 /* Unconditional extended branches (far jumps)
2647 * have issues, so we always use a conditional
2648 * branch, setting the condition to always for
2649 * unconditional. For compact unconditional
2650 * branches, cond isn't used so it doesn't
2651 * matter what we pick. */
2652
2653 midgard_condition cond =
2654 !is_conditional ? midgard_condition_always :
2655 is_inverted ? midgard_condition_false :
2656 midgard_condition_true;
2657
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00002658 midgard_jmp_writeout_op op =
2659 is_discard ? midgard_jmp_writeout_op_discard :
2660 (is_compact && !is_conditional) ? midgard_jmp_writeout_op_branch_uncond :
2661 midgard_jmp_writeout_op_branch_cond;
2662
Alyssa Rosenzweig5abb7b52019-02-17 22:09:09 +00002663 if (!is_compact) {
2664 midgard_branch_extended branch =
2665 midgard_create_branch_extended(
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00002666 cond, op,
Alyssa Rosenzweig5abb7b52019-02-17 22:09:09 +00002667 dest_tag,
2668 quadword_offset);
2669
2670 memcpy(&ins->branch_extended, &branch, sizeof(branch));
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00002671 } else if (is_conditional || is_discard) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002672 midgard_branch_cond branch = {
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00002673 .op = op,
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002674 .dest_tag = dest_tag,
2675 .offset = quadword_offset,
Alyssa Rosenzweig5abb7b52019-02-17 22:09:09 +00002676 .cond = cond
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002677 };
2678
Alyssa Rosenzweig5abb7b52019-02-17 22:09:09 +00002679 assert(branch.offset == quadword_offset);
2680
2681 memcpy(&ins->br_compact, &branch, sizeof(branch));
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002682 } else {
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00002683 assert(op == midgard_jmp_writeout_op_branch_uncond);
2684
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002685 midgard_branch_uncond branch = {
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00002686 .op = op,
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002687 .dest_tag = dest_tag,
2688 .offset = quadword_offset,
2689 .unknown = 1
2690 };
2691
Alyssa Rosenzweig5abb7b52019-02-17 22:09:09 +00002692 assert(branch.offset == quadword_offset);
2693
2694 memcpy(&ins->br_compact, &branch, sizeof(branch));
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002695 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002696 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002697 }
2698
2699 ++br_block_idx;
2700 }
2701
2702 /* Emit flat binary from the instruction arrays. Iterate each block in
2703 * sequence. Save instruction boundaries such that lookahead tags can
2704 * be assigned easily */
2705
2706 /* Cache _all_ bundles in source order for lookahead across failed branches */
2707
2708 int bundle_count = 0;
2709 mir_foreach_block(ctx, block) {
2710 bundle_count += block->bundles.size / sizeof(midgard_bundle);
2711 }
2712 midgard_bundle **source_order_bundles = malloc(sizeof(midgard_bundle *) * bundle_count);
2713 int bundle_idx = 0;
2714 mir_foreach_block(ctx, block) {
2715 util_dynarray_foreach(&block->bundles, midgard_bundle, bundle) {
2716 source_order_bundles[bundle_idx++] = bundle;
2717 }
2718 }
2719
2720 int current_bundle = 0;
2721
Alyssa Rosenzweig2a79afc2019-05-23 01:56:03 +00002722 /* Midgard prefetches instruction types, so during emission we
2723 * need to lookahead. Unless this is the last instruction, in
2724 * which we return 1. Or if this is the second to last and the
2725 * last is an ALU, then it's also 1... */
2726
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002727 mir_foreach_block(ctx, block) {
Alyssa Rosenzweigd3ad8d62019-06-06 11:19:44 -07002728 mir_foreach_bundle_in_block(block, bundle) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002729 int lookahead = 1;
2730
2731 if (current_bundle + 1 < bundle_count) {
2732 uint8_t next = source_order_bundles[current_bundle + 1]->tag;
2733
2734 if (!(current_bundle + 2 < bundle_count) && IS_ALU(next)) {
2735 lookahead = 1;
2736 } else {
2737 lookahead = next;
2738 }
2739 }
2740
2741 emit_binary_bundle(ctx, bundle, compiled, lookahead);
2742 ++current_bundle;
2743 }
2744
2745 /* TODO: Free deeper */
2746 //util_dynarray_fini(&block->instructions);
2747 }
2748
2749 free(source_order_bundles);
2750
Alyssa Rosenzweig5e55c112019-02-17 03:35:03 +00002751 /* Report the very first tag executed */
2752 program->first_tag = midgard_get_first_tag_from_block(ctx, 0);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002753
2754 /* Deal with off-by-one related to the fencepost problem */
2755 program->work_register_count = ctx->work_registers + 1;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002756 program->uniform_cutoff = ctx->uniform_cutoff;
2757
2758 program->blend_patch_offset = ctx->blend_constant_offset;
Alyssa Rosenzweigf0d00612019-07-19 16:23:52 -07002759 program->tls_size = ctx->tls_size;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002760
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002761 if (midgard_debug & MIDGARD_DBG_SHADERS)
Alyssa Rosenzweigac14fac2019-11-07 09:31:02 -05002762 disassemble_midgard(program->compiled.data, program->compiled.size, gpu_id, ctx->stage);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002763
Alyssa Rosenzweig138e40d2019-07-08 16:42:29 -07002764 if (midgard_debug & MIDGARD_DBG_SHADERDB) {
Alyssa Rosenzweig19bceb52019-08-30 13:57:20 -07002765 unsigned nr_bundles = 0, nr_ins = 0;
Alyssa Rosenzweig138e40d2019-07-08 16:42:29 -07002766
2767 /* Count instructions and bundles */
2768
Alyssa Rosenzweig138e40d2019-07-08 16:42:29 -07002769 mir_foreach_block(ctx, block) {
2770 nr_bundles += util_dynarray_num_elements(
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002771 &block->bundles, midgard_bundle);
Alyssa Rosenzweig2d739f62019-07-09 11:16:57 -07002772
Alyssa Rosenzweig67909c82019-08-30 13:08:16 -07002773 mir_foreach_bundle_in_block(block, bun)
2774 nr_ins += bun->instruction_count;
Alyssa Rosenzweig138e40d2019-07-08 16:42:29 -07002775 }
2776
2777 /* Calculate thread count. There are certain cutoffs by
2778 * register count for thread count */
2779
2780 unsigned nr_registers = program->work_register_count;
2781
2782 unsigned nr_threads =
2783 (nr_registers <= 4) ? 4 :
2784 (nr_registers <= 8) ? 2 :
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002785 1;
Alyssa Rosenzweig138e40d2019-07-08 16:42:29 -07002786
2787 /* Dump stats */
2788
2789 fprintf(stderr, "shader%d - %s shader: "
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002790 "%u inst, %u bundles, %u quadwords, "
Alyssa Rosenzweige8dca7e2019-07-22 06:32:48 -07002791 "%u registers, %u threads, %u loops, "
Alyssa Rosenzweig1a4153b2019-08-30 17:29:17 -07002792 "%u:%u spills:fills\n",
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002793 SHADER_DB_COUNT++,
2794 gl_shader_stage_name(ctx->stage),
Alyssa Rosenzweig19bceb52019-08-30 13:57:20 -07002795 nr_ins, nr_bundles, ctx->quadword_count,
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002796 nr_registers, nr_threads,
Alyssa Rosenzweige8dca7e2019-07-22 06:32:48 -07002797 ctx->loop_count,
2798 ctx->spills, ctx->fills);
Alyssa Rosenzweig138e40d2019-07-08 16:42:29 -07002799 }
2800
Alyssa Rosenzweig4fa09322019-08-15 08:10:46 -07002801 ralloc_free(ctx);
Alyssa Rosenzweig138e40d2019-07-08 16:42:29 -07002802
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002803 return 0;
2804}