blob: 4c1fd438a23d80f563ea6c1b45982d45481b25bd [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"
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +000036#include "compiler/nir/nir_builder.h"
37#include "util/half_float.h"
Alyssa Rosenzweig213b6282019-06-18 09:02:20 -070038#include "util/u_math.h"
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +010039#include "util/u_debug.h"
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +000040#include "util/u_dynarray.h"
41#include "util/list.h"
42#include "main/mtypes.h"
43
44#include "midgard.h"
45#include "midgard_nir.h"
46#include "midgard_compile.h"
Alyssa Rosenzweig11554462019-05-19 23:20:34 +000047#include "midgard_ops.h"
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +000048#include "helpers.h"
Alyssa Rosenzweig11554462019-05-19 23:20:34 +000049#include "compiler.h"
Alyssa Rosenzweigfcf144d2019-11-19 20:55:42 -050050#include "midgard_quirks.h"
Icecream951e1eee92020-07-06 19:30:37 +120051#include "panfrost-quirks.h"
52#include "panfrost/util/pan_lower_framebuffer.h"
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +000053
54#include "disassemble.h"
55
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +010056static const struct debug_named_value debug_options[] = {
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -070057 {"msgs", MIDGARD_DBG_MSGS, "Print debug messages"},
58 {"shaders", MIDGARD_DBG_SHADERS, "Dump shaders in NIR and MIR"},
Alyssa Rosenzweig138e40d2019-07-08 16:42:29 -070059 {"shaderdb", MIDGARD_DBG_SHADERDB, "Prints shader-db statistics"},
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -070060 DEBUG_NAMED_VALUE_END
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +010061};
62
63DEBUG_GET_ONCE_FLAGS_OPTION(midgard_debug, "MIDGARD_MESA_DEBUG", debug_options, 0)
64
Alyssa Rosenzweig138e40d2019-07-08 16:42:29 -070065unsigned SHADER_DB_COUNT = 0;
66
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +010067int midgard_debug = 0;
68
69#define DBG(fmt, ...) \
70 do { if (midgard_debug & MIDGARD_DBG_MSGS) \
71 fprintf(stderr, "%s:%d: "fmt, \
72 __FUNCTION__, __LINE__, ##__VA_ARGS__); } while (0)
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -070073static midgard_block *
74create_empty_block(compiler_context *ctx)
75{
76 midgard_block *blk = rzalloc(ctx, midgard_block);
77
Alyssa Rosenzweig5aaaf7b2020-03-11 08:36:31 -040078 blk->base.predecessors = _mesa_set_create(blk,
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -070079 _mesa_hash_pointer,
80 _mesa_key_pointer_equal);
81
Alyssa Rosenzweig5aaaf7b2020-03-11 08:36:31 -040082 blk->base.name = ctx->block_source_count++;
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -070083
84 return blk;
85}
86
Alyssa Rosenzweigc0fb2602019-04-21 03:29:47 +000087static void
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -070088schedule_barrier(compiler_context *ctx)
89{
90 midgard_block *temp = ctx->after_block;
91 ctx->after_block = create_empty_block(ctx);
92 ctx->block_count++;
Alyssa Rosenzweig5aaaf7b2020-03-11 08:36:31 -040093 list_addtail(&ctx->after_block->base.link, &ctx->blocks);
94 list_inithead(&ctx->after_block->base.instructions);
95 pan_block_add_successor(&ctx->current_block->base, &ctx->after_block->base);
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -070096 ctx->current_block = ctx->after_block;
97 ctx->after_block = temp;
98}
99
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000100/* Helpers to generate midgard_instruction's using macro magic, since every
101 * driver seems to do it that way */
102
103#define EMIT(op, ...) emit_mir_instruction(ctx, v_##op(__VA_ARGS__));
Alyssa Rosenzweig56f9b472019-06-14 16:03:01 -0700104
Alyssa Rosenzweig714eba82020-04-27 19:01:40 -0400105#define M_LOAD_STORE(name, store, T) \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000106 static midgard_instruction m_##name(unsigned ssa, unsigned address) { \
107 midgard_instruction i = { \
108 .type = TAG_LOAD_STORE_4, \
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -0700109 .mask = 0xF, \
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -0700110 .dest = ~0, \
Alyssa Rosenzweigccbc9a42019-12-19 10:35:18 -0500111 .src = { ~0, ~0, ~0, ~0 }, \
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -0400112 .swizzle = SWIZZLE_IDENTITY_4, \
Italo Nicolabea6a652020-07-23 19:24:39 +0000113 .op = midgard_op_##name, \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000114 .load_store = { \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000115 .address = address \
116 } \
117 }; \
Alyssa Rosenzweigd4bcca12019-08-02 15:25:02 -0700118 \
Alyssa Rosenzweig714eba82020-04-27 19:01:40 -0400119 if (store) { \
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -0700120 i.src[0] = ssa; \
Alyssa Rosenzweig714eba82020-04-27 19:01:40 -0400121 i.src_types[0] = T; \
Alyssa Rosenzweig9915bb22020-05-07 10:12:38 -0400122 i.dest_type = T; \
Alyssa Rosenzweig714eba82020-04-27 19:01:40 -0400123 } else { \
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -0700124 i.dest = ssa; \
Alyssa Rosenzweig714eba82020-04-27 19:01:40 -0400125 i.dest_type = T; \
126 } \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000127 return i; \
128 }
129
Alyssa Rosenzweig714eba82020-04-27 19:01:40 -0400130#define M_LOAD(name, T) M_LOAD_STORE(name, false, T)
131#define M_STORE(name, T) M_LOAD_STORE(name, true, T)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000132
Alyssa Rosenzweig714eba82020-04-27 19:01:40 -0400133M_LOAD(ld_attr_32, nir_type_uint32);
134M_LOAD(ld_vary_32, nir_type_uint32);
135M_LOAD(ld_ubo_int4, nir_type_uint32);
136M_LOAD(ld_int4, nir_type_uint32);
137M_STORE(st_int4, nir_type_uint32);
138M_LOAD(ld_color_buffer_32u, nir_type_uint32);
Alyssa Rosenzweig36af05b2020-06-01 14:14:33 -0400139M_LOAD(ld_color_buffer_as_fp16, nir_type_float16);
Icecream952fbe7ca2020-07-09 23:44:41 +1200140M_LOAD(ld_color_buffer_as_fp32, nir_type_float32);
Alyssa Rosenzweig714eba82020-04-27 19:01:40 -0400141M_STORE(st_vary_32, nir_type_uint32);
142M_LOAD(ld_cubemap_coords, nir_type_uint32);
143M_LOAD(ld_compute_id, nir_type_uint32);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000144
145static midgard_instruction
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000146v_branch(bool conditional, bool invert)
147{
148 midgard_instruction ins = {
149 .type = TAG_ALU_4,
Alyssa Rosenzweig5abb7b52019-02-17 22:09:09 +0000150 .unit = ALU_ENAB_BRANCH,
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000151 .compact_branch = true,
152 .branch = {
153 .conditional = conditional,
154 .invert_conditional = invert
Alyssa Rosenzweig29416a82019-07-30 12:20:24 -0700155 },
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -0700156 .dest = ~0,
Alyssa Rosenzweigccbc9a42019-12-19 10:35:18 -0500157 .src = { ~0, ~0, ~0, ~0 },
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000158 };
159
160 return ins;
161}
162
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000163static void
164attach_constants(compiler_context *ctx, midgard_instruction *ins, void *constants, int name)
165{
166 ins->has_constants = true;
167 memcpy(&ins->constants, constants, 16);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000168}
169
170static int
Timothy Arceri035759b2019-03-29 12:39:48 +1100171glsl_type_size(const struct glsl_type *type, bool bindless)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000172{
173 return glsl_count_attribute_slots(type, false);
174}
175
176/* Lower fdot2 to a vector multiplication followed by channel addition */
Icecream9527516ba2020-09-05 17:00:37 +1200177static bool
178midgard_nir_lower_fdot2_instr(nir_builder *b, nir_instr *instr, void *data)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000179{
Icecream9527516ba2020-09-05 17:00:37 +1200180 if (instr->type != nir_instr_type_alu)
181 return false;
182
183 nir_alu_instr *alu = nir_instr_as_alu(instr);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000184 if (alu->op != nir_op_fdot2)
Icecream9527516ba2020-09-05 17:00:37 +1200185 return false;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000186
187 b->cursor = nir_before_instr(&alu->instr);
188
189 nir_ssa_def *src0 = nir_ssa_for_alu_src(b, alu, 0);
190 nir_ssa_def *src1 = nir_ssa_for_alu_src(b, alu, 1);
191
192 nir_ssa_def *product = nir_fmul(b, src0, src1);
193
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -0700194 nir_ssa_def *sum = nir_fadd(b,
195 nir_channel(b, product, 0),
196 nir_channel(b, product, 1));
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000197
198 /* Replace the fdot2 with this sum */
199 nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa, nir_src_for_ssa(sum));
Icecream9527516ba2020-09-05 17:00:37 +1200200
201 return true;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000202}
203
204static bool
205midgard_nir_lower_fdot2(nir_shader *shader)
206{
Icecream9527516ba2020-09-05 17:00:37 +1200207 return nir_shader_instructions_pass(shader,
208 midgard_nir_lower_fdot2_instr,
209 nir_metadata_block_index | nir_metadata_dominance,
210 NULL);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000211}
212
Alyssa Rosenzweig2486fe62020-08-27 14:55:11 -0400213static bool
214mdg_is_64(const nir_instr *instr, const void *_unused)
215{
216 const nir_alu_instr *alu = nir_instr_as_alu(instr);
217
218 if (nir_dest_bit_size(alu->dest.dest) == 64)
219 return true;
220
221 switch (alu->op) {
222 case nir_op_umul_high:
223 case nir_op_imul_high:
224 return true;
225 default:
226 return false;
227 }
228}
229
Alyssa Rosenzweiga2f1a062019-07-08 12:40:34 -0700230/* Flushes undefined values to zero */
231
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000232static void
Alyssa Rosenzweig7c793a42020-05-22 16:23:06 -0400233optimise_nir(nir_shader *nir, unsigned quirks, bool is_blend)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000234{
235 bool progress;
Ian Romanickd41cdef2018-08-18 16:42:04 -0700236 unsigned lower_flrp =
237 (nir->options->lower_flrp16 ? 16 : 0) |
238 (nir->options->lower_flrp32 ? 32 : 0) |
239 (nir->options->lower_flrp64 ? 64 : 0);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000240
241 NIR_PASS(progress, nir, nir_lower_regs_to_ssa);
Rhys Perry8b98d092019-02-05 15:56:24 +0000242 NIR_PASS(progress, nir, nir_lower_idiv, nir_lower_idiv_fast);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000243
Alyssa Rosenzweig44a6c382019-08-14 08:44:40 -0700244 nir_lower_tex_options lower_tex_options = {
245 .lower_txs_lod = true,
Alyssa Rosenzweig4c43b352019-11-21 13:40:00 -0500246 .lower_txp = ~0,
247 .lower_tex_without_implicit_lod =
248 (quirks & MIDGARD_EXPLICIT_LOD),
Alyssa Rosenzweig7dab5742020-08-28 09:48:38 -0400249 .lower_tg4_broadcom_swizzle = true,
Alyssa Rosenzweigc57337b2019-12-19 11:12:50 -0500250
251 /* TODO: we have native gradient.. */
252 .lower_txd = true,
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000253 };
254
Alyssa Rosenzweig44a6c382019-08-14 08:44:40 -0700255 NIR_PASS(progress, nir, nir_lower_tex, &lower_tex_options);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000256
Alyssa Rosenzweigc57337b2019-12-19 11:12:50 -0500257 /* Must lower fdot2 after tex is lowered */
258 NIR_PASS(progress, nir, midgard_nir_lower_fdot2);
259
Alyssa Rosenzweigbda2bb32019-11-21 08:45:27 -0500260 /* T720 is broken. */
261
262 if (quirks & MIDGARD_BROKEN_LOD)
263 NIR_PASS_V(nir, midgard_nir_lod_errata);
264
Alyssa Rosenzweigc495c6c2020-05-12 19:07:48 -0400265 NIR_PASS(progress, nir, midgard_nir_lower_algebraic_early);
266
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000267 do {
268 progress = false;
269
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000270 NIR_PASS(progress, nir, nir_lower_var_copies);
271 NIR_PASS(progress, nir, nir_lower_vars_to_ssa);
272
273 NIR_PASS(progress, nir, nir_copy_prop);
Boris Brezillon440b0d62020-01-06 14:31:38 +0100274 NIR_PASS(progress, nir, nir_opt_remove_phis);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000275 NIR_PASS(progress, nir, nir_opt_dce);
276 NIR_PASS(progress, nir, nir_opt_dead_cf);
277 NIR_PASS(progress, nir, nir_opt_cse);
278 NIR_PASS(progress, nir, nir_opt_peephole_select, 64, false, true);
279 NIR_PASS(progress, nir, nir_opt_algebraic);
280 NIR_PASS(progress, nir, nir_opt_constant_folding);
Ian Romanickd41cdef2018-08-18 16:42:04 -0700281
282 if (lower_flrp != 0) {
Ian Romanick1f1007a2019-05-08 07:32:43 -0700283 bool lower_flrp_progress = false;
Ian Romanickd41cdef2018-08-18 16:42:04 -0700284 NIR_PASS(lower_flrp_progress,
285 nir,
286 nir_lower_flrp,
287 lower_flrp,
Marek Olšákac55b1a2020-07-22 22:13:16 -0400288 false /* always_precise */);
Ian Romanickd41cdef2018-08-18 16:42:04 -0700289 if (lower_flrp_progress) {
290 NIR_PASS(progress, nir,
291 nir_opt_constant_folding);
292 progress = true;
293 }
294
295 /* Nothing should rematerialize any flrps, so we only
296 * need to do this lowering once.
297 */
298 lower_flrp = 0;
299 }
300
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000301 NIR_PASS(progress, nir, nir_opt_undef);
Alyssa Rosenzweiga2f1a062019-07-08 12:40:34 -0700302 NIR_PASS(progress, nir, nir_undef_to_zero);
303
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000304 NIR_PASS(progress, nir, nir_opt_loop_unroll,
305 nir_var_shader_in |
306 nir_var_shader_out |
307 nir_var_function_temp);
308
Eric Anholtf25e1692020-08-27 12:49:13 -0700309 NIR_PASS(progress, nir, nir_opt_vectorize, NULL, NULL);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000310 } while (progress);
311
Alyssa Rosenzweig2486fe62020-08-27 14:55:11 -0400312 NIR_PASS_V(nir, nir_lower_alu_to_scalar, mdg_is_64, NULL);
313
Alyssa Rosenzweigd838cb92020-06-16 13:07:02 -0400314 /* Run after opts so it can hit more */
315 if (!is_blend)
316 NIR_PASS(progress, nir, nir_fuse_io_16);
317
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000318 /* Must be run at the end to prevent creation of fsin/fcos ops */
319 NIR_PASS(progress, nir, midgard_nir_scale_trig);
320
321 do {
322 progress = false;
323
324 NIR_PASS(progress, nir, nir_opt_dce);
325 NIR_PASS(progress, nir, nir_opt_algebraic);
326 NIR_PASS(progress, nir, nir_opt_constant_folding);
327 NIR_PASS(progress, nir, nir_copy_prop);
328 } while (progress);
329
330 NIR_PASS(progress, nir, nir_opt_algebraic_late);
Alyssa Rosenzweig211dee42020-04-29 20:27:16 -0400331 NIR_PASS(progress, nir, nir_opt_algebraic_distribute_src_mods);
Alyssa Rosenzweig726f0262019-05-07 02:52:08 +0000332
333 /* We implement booleans as 32-bit 0/~0 */
334 NIR_PASS(progress, nir, nir_lower_bool_to_int32);
335
336 /* Now that booleans are lowered, we can run out late opts */
Alyssa Rosenzweigeffe6fb02019-03-25 02:49:04 +0000337 NIR_PASS(progress, nir, midgard_nir_lower_algebraic_late);
Alyssa Rosenzweig449e5de2020-04-30 13:46:35 -0400338 NIR_PASS(progress, nir, midgard_nir_cancel_inot);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000339
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000340 NIR_PASS(progress, nir, nir_copy_prop);
341 NIR_PASS(progress, nir, nir_opt_dce);
342
343 /* Take us out of SSA */
344 NIR_PASS(progress, nir, nir_lower_locals_to_regs);
345 NIR_PASS(progress, nir, nir_convert_from_ssa, true);
346
347 /* We are a vector architecture; write combine where possible */
348 NIR_PASS(progress, nir, nir_move_vec_src_uses_to_dest);
349 NIR_PASS(progress, nir, nir_lower_vec_to_movs);
350
351 NIR_PASS(progress, nir, nir_opt_dce);
352}
353
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000354/* Do not actually emit a load; instead, cache the constant for inlining */
355
356static void
357emit_load_const(compiler_context *ctx, nir_load_const_instr *instr)
358{
359 nir_ssa_def def = instr->def;
360
Boris Brezillon15c92d12020-01-20 15:00:57 +0100361 midgard_constants *consts = rzalloc(NULL, midgard_constants);
362
363 assert(instr->def.num_components * instr->def.bit_size <= sizeof(*consts) * 8);
364
365#define RAW_CONST_COPY(bits) \
366 nir_const_value_to_array(consts->u##bits, instr->value, \
367 instr->def.num_components, u##bits)
368
369 switch (instr->def.bit_size) {
370 case 64:
371 RAW_CONST_COPY(64);
372 break;
373 case 32:
374 RAW_CONST_COPY(32);
375 break;
376 case 16:
377 RAW_CONST_COPY(16);
378 break;
379 case 8:
380 RAW_CONST_COPY(8);
381 break;
382 default:
383 unreachable("Invalid bit_size for load_const instruction\n");
384 }
Alyssa Rosenzweig9beb3392019-07-26 11:30:06 -0700385
386 /* Shifted for SSA, +1 for off-by-one */
Boris Brezillon15c92d12020-01-20 15:00:57 +0100387 _mesa_hash_table_u64_insert(ctx->ssa_constants, (def.index << 1) + 1, consts);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000388}
389
Alyssa Rosenzweige1693012019-07-24 12:52:27 -0700390/* Normally constants are embedded implicitly, but for I/O and such we have to
391 * explicitly emit a move with the constant source */
392
393static void
394emit_explicit_constant(compiler_context *ctx, unsigned node, unsigned to)
395{
396 void *constant_value = _mesa_hash_table_u64_search(ctx->ssa_constants, node + 1);
397
398 if (constant_value) {
Alyssa Rosenzweigc3a46e72019-10-30 16:29:28 -0400399 midgard_instruction ins = v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT), to);
Alyssa Rosenzweige1693012019-07-24 12:52:27 -0700400 attach_constants(ctx, &ins, constant_value, node + 1);
401 emit_mir_instruction(ctx, ins);
402 }
403}
404
Alyssa Rosenzweig726f0262019-05-07 02:52:08 +0000405static bool
406nir_is_non_scalar_swizzle(nir_alu_src *src, unsigned nr_components)
407{
408 unsigned comp = src->swizzle[0];
409
410 for (unsigned c = 1; c < nr_components; ++c) {
411 if (src->swizzle[c] != comp)
412 return true;
413 }
414
415 return false;
416}
417
Italo Nicola8e221f52020-08-31 11:17:48 +0000418#define ATOMIC_CASE_IMPL(ctx, instr, nir, op, is_shared) \
419 case nir_intrinsic_##nir: \
420 emit_atomic(ctx, instr, is_shared, midgard_op_##op); \
421 break;
422
423#define ATOMIC_CASE(ctx, instr, nir, op) \
424 ATOMIC_CASE_IMPL(ctx, instr, shared_atomic_##nir, atomic_##op, true); \
425 ATOMIC_CASE_IMPL(ctx, instr, global_atomic_##nir, atomic_##op, false);
426
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000427#define ALU_CASE(nir, _op) \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000428 case nir_op_##nir: \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000429 op = midgard_alu_op_##_op; \
Alyssa Rosenzweig0ed8cca2019-07-01 17:35:25 -0700430 assert(src_bitsize == dst_bitsize); \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000431 break;
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700432
Alyssa Rosenzweig93513cd2020-05-25 14:19:11 -0400433#define ALU_CASE_RTZ(nir, _op) \
434 case nir_op_##nir: \
435 op = midgard_alu_op_##_op; \
436 roundmode = MIDGARD_RTZ; \
Alyssa Rosenzweig93513cd2020-05-25 14:19:11 -0400437 break;
438
Italo Nicolacea032a2020-09-23 05:41:38 +0000439#define ALU_CHECK_CMP() \
Alyssa Rosenzweig1108eaa2020-05-08 17:41:49 -0400440 assert(src_bitsize == 16 || src_bitsize == 32); \
441 assert(dst_bitsize == 16 || dst_bitsize == 32); \
442
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700443#define ALU_CASE_BCAST(nir, _op, count) \
444 case nir_op_##nir: \
445 op = midgard_alu_op_##_op; \
446 broadcast_swizzle = count; \
Italo Nicolacea032a2020-09-23 05:41:38 +0000447 ALU_CHECK_CMP(); \
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700448 break;
Alyssa Rosenzweigeb28a362020-05-07 10:12:24 -0400449
Italo Nicolacea032a2020-09-23 05:41:38 +0000450#define ALU_CASE_CMP(nir, _op) \
Alyssa Rosenzweigeb28a362020-05-07 10:12:24 -0400451 case nir_op_##nir: \
452 op = midgard_alu_op_##_op; \
Italo Nicolacea032a2020-09-23 05:41:38 +0000453 ALU_CHECK_CMP(); \
454 break;
Alyssa Rosenzweig4df80ca2019-07-01 15:26:22 -0700455
Alyssa Rosenzweig449e5de2020-04-30 13:46:35 -0400456/* Compare mir_lower_invert */
457static bool
458nir_accepts_inot(nir_op op, unsigned src)
459{
460 switch (op) {
461 case nir_op_ior:
Alyssa Rosenzweig6b023b32020-05-08 17:42:40 -0400462 case nir_op_iand: /* TODO: b2f16 */
Alyssa Rosenzweig449e5de2020-04-30 13:46:35 -0400463 case nir_op_ixor:
464 return true;
465 case nir_op_b32csel:
466 /* Only the condition */
467 return (src == 0);
468 default:
469 return false;
470 }
471}
472
Alyssa Rosenzweig29afa882020-05-04 17:33:52 -0400473static bool
474mir_accept_dest_mod(compiler_context *ctx, nir_dest **dest, nir_op op)
475{
476 if (pan_has_dest_mod(dest, op)) {
477 assert((*dest)->is_ssa);
478 BITSET_SET(ctx->already_emitted, (*dest)->ssa.index);
479 return true;
480 }
481
482 return false;
483}
484
Italo Nicola83592de2020-07-15 18:48:42 +0000485/* Look for floating point mods. We have the mods fsat, fsat_signed,
486 * and fpos. We also have the relations (note 3 * 2 = 6 cases):
487 *
488 * fsat_signed(fpos(x)) = fsat(x)
489 * fsat_signed(fsat(x)) = fsat(x)
490 * fpos(fsat_signed(x)) = fsat(x)
491 * fpos(fsat(x)) = fsat(x)
492 * fsat(fsat_signed(x)) = fsat(x)
493 * fsat(fpos(x)) = fsat(x)
494 *
495 * So by cases any composition of output modifiers is equivalent to
496 * fsat alone.
497 */
498static unsigned
499mir_determine_float_outmod(compiler_context *ctx, nir_dest **dest, unsigned prior_outmod)
500{
501 bool fpos = mir_accept_dest_mod(ctx, dest, nir_op_fclamp_pos);
502 bool fsat = mir_accept_dest_mod(ctx, dest, nir_op_fsat);
503 bool ssat = mir_accept_dest_mod(ctx, dest, nir_op_fsat_signed);
504 bool prior = (prior_outmod != midgard_outmod_none);
505 int count = (int) prior + (int) fpos + (int) ssat + (int) fsat;
506
507 return ((count > 1) || fsat) ? midgard_outmod_sat :
508 fpos ? midgard_outmod_pos :
509 ssat ? midgard_outmod_sat_signed :
510 prior_outmod;
511}
512
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000513static void
Alyssa Rosenzweigf8b881f2020-05-25 14:19:24 -0400514mir_copy_src(midgard_instruction *ins, nir_alu_instr *instr, unsigned i, unsigned to, bool *abs, bool *neg, bool *not, enum midgard_roundmode *roundmode, bool is_int, unsigned bcast_count)
Alyssa Rosenzweig6757c482020-04-27 18:55:11 -0400515{
Alyssa Rosenzweig22bb5a92020-04-29 18:08:26 -0400516 nir_alu_src src = instr->src[i];
Alyssa Rosenzweigb124f532020-04-29 18:10:43 -0400517
518 if (!is_int) {
519 if (pan_has_source_mod(&src, nir_op_fneg))
520 *neg = !(*neg);
521
522 if (pan_has_source_mod(&src, nir_op_fabs))
523 *abs = true;
524 }
525
Alyssa Rosenzweig449e5de2020-04-30 13:46:35 -0400526 if (nir_accepts_inot(instr->op, i) && pan_has_source_mod(&src, nir_op_inot))
527 *not = true;
528
Alyssa Rosenzweigf8b881f2020-05-25 14:19:24 -0400529 if (roundmode) {
530 if (pan_has_source_mod(&src, nir_op_fround_even))
531 *roundmode = MIDGARD_RTE;
532
533 if (pan_has_source_mod(&src, nir_op_ftrunc))
534 *roundmode = MIDGARD_RTZ;
535
536 if (pan_has_source_mod(&src, nir_op_ffloor))
537 *roundmode = MIDGARD_RTN;
538
539 if (pan_has_source_mod(&src, nir_op_fceil))
540 *roundmode = MIDGARD_RTP;
541 }
542
Alyssa Rosenzweig22bb5a92020-04-29 18:08:26 -0400543 unsigned bits = nir_src_bit_size(src.src);
Alyssa Rosenzweig6757c482020-04-27 18:55:11 -0400544
Alyssa Rosenzweig22bb5a92020-04-29 18:08:26 -0400545 ins->src[to] = nir_src_index(NULL, &src.src);
Alyssa Rosenzweig6757c482020-04-27 18:55:11 -0400546 ins->src_types[to] = nir_op_infos[instr->op].input_types[i] | bits;
Alyssa Rosenzweig22bb5a92020-04-29 18:08:26 -0400547
548 for (unsigned c = 0; c < NIR_MAX_VEC_COMPONENTS; ++c) {
549 ins->swizzle[to][c] = src.swizzle[
550 (!bcast_count || c < bcast_count) ? c :
551 (bcast_count - 1)];
552 }
Alyssa Rosenzweig6757c482020-04-27 18:55:11 -0400553}
554
Alyssa Rosenzweigd39f95b2020-05-04 15:45:47 -0400555/* Midgard features both fcsel and icsel, depending on whether you want int or
556 * float modifiers. NIR's csel is typeless, so we want a heuristic to guess if
557 * we should emit an int or float csel depending on what modifiers could be
558 * placed. In the absense of modifiers, this is probably arbitrary. */
559
560static bool
561mir_is_bcsel_float(nir_alu_instr *instr)
562{
563 nir_op intmods[] = {
564 nir_op_i2i8, nir_op_i2i16,
565 nir_op_i2i32, nir_op_i2i64
566 };
567
568 nir_op floatmods[] = {
569 nir_op_fabs, nir_op_fneg,
570 nir_op_f2f16, nir_op_f2f32,
571 nir_op_f2f64
572 };
573
574 nir_op floatdestmods[] = {
575 nir_op_fsat, nir_op_fsat_signed, nir_op_fclamp_pos,
576 nir_op_f2f16, nir_op_f2f32
577 };
578
579 signed score = 0;
580
581 for (unsigned i = 1; i < 3; ++i) {
582 nir_alu_src s = instr->src[i];
583 for (unsigned q = 0; q < ARRAY_SIZE(intmods); ++q) {
584 if (pan_has_source_mod(&s, intmods[q]))
585 score--;
586 }
587 }
588
589 for (unsigned i = 1; i < 3; ++i) {
590 nir_alu_src s = instr->src[i];
591 for (unsigned q = 0; q < ARRAY_SIZE(floatmods); ++q) {
592 if (pan_has_source_mod(&s, floatmods[q]))
593 score++;
594 }
595 }
596
597 for (unsigned q = 0; q < ARRAY_SIZE(floatdestmods); ++q) {
598 nir_dest *dest = &instr->dest.dest;
599 if (pan_has_dest_mod(&dest, floatdestmods[q]))
600 score++;
601 }
602
603 return (score > 0);
604}
605
Alyssa Rosenzweig6757c482020-04-27 18:55:11 -0400606static void
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000607emit_alu(compiler_context *ctx, nir_alu_instr *instr)
608{
Alyssa Rosenzweig22bb5a92020-04-29 18:08:26 -0400609 nir_dest *dest = &instr->dest.dest;
610
611 if (dest->is_ssa && BITSET_TEST(ctx->already_emitted, dest->ssa.index))
612 return;
613
Alyssa Rosenzweig8f887322019-07-29 15:11:12 -0700614 /* Derivatives end up emitted on the texture pipe, not the ALUs. This
615 * is handled elsewhere */
616
617 if (instr->op == nir_op_fddx || instr->op == nir_op_fddy) {
618 midgard_emit_derivatives(ctx, instr);
619 return;
620 }
621
Alyssa Rosenzweig22bb5a92020-04-29 18:08:26 -0400622 bool is_ssa = dest->is_ssa;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000623
Alyssa Rosenzweig22bb5a92020-04-29 18:08:26 -0400624 unsigned nr_components = nir_dest_num_components(*dest);
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000625 unsigned nr_inputs = nir_op_infos[instr->op].num_inputs;
Alyssa Rosenzweig04f76ad2020-04-27 18:58:21 -0400626 unsigned op = 0;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000627
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700628 /* Number of components valid to check for the instruction (the rest
629 * will be forced to the last), or 0 to use as-is. Relevant as
630 * ball-type instructions have a channel count in NIR but are all vec4
631 * in Midgard */
632
633 unsigned broadcast_swizzle = 0;
634
Alyssa Rosenzweig22bb5a92020-04-29 18:08:26 -0400635 /* Should we swap arguments? */
636 bool flip_src12 = false;
637
Eric Anholt4c24c822020-08-25 10:15:27 -0700638 ASSERTED unsigned src_bitsize = nir_src_bit_size(instr->src[0].src);
639 ASSERTED unsigned dst_bitsize = nir_dest_bit_size(*dest);
Alyssa Rosenzweig0ed8cca2019-07-01 17:35:25 -0700640
Alyssa Rosenzweig93513cd2020-05-25 14:19:11 -0400641 enum midgard_roundmode roundmode = MIDGARD_RTE;
642
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000643 switch (instr->op) {
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000644 ALU_CASE(fadd, fadd);
645 ALU_CASE(fmul, fmul);
646 ALU_CASE(fmin, fmin);
647 ALU_CASE(fmax, fmax);
648 ALU_CASE(imin, imin);
649 ALU_CASE(imax, imax);
Alyssa Rosenzweig2e7555b2019-04-05 05:16:54 +0000650 ALU_CASE(umin, umin);
651 ALU_CASE(umax, umax);
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000652 ALU_CASE(ffloor, ffloor);
Alyssa Rosenzweigc6be9962019-02-23 01:12:10 +0000653 ALU_CASE(fround_even, froundeven);
654 ALU_CASE(ftrunc, ftrunc);
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000655 ALU_CASE(fceil, fceil);
656 ALU_CASE(fdot3, fdot3);
657 ALU_CASE(fdot4, fdot4);
658 ALU_CASE(iadd, iadd);
659 ALU_CASE(isub, isub);
660 ALU_CASE(imul, imul);
Alyssa Rosenzweig3e2cb212020-08-27 14:35:23 -0400661 ALU_CASE(imul_high, imul);
662 ALU_CASE(umul_high, imul);
Alyssa Rosenzweig9f14e202019-06-05 15:18:35 +0000663
664 /* Zero shoved as second-arg */
665 ALU_CASE(iabs, iabsdiff);
666
Italo Nicolac9192d12020-09-19 10:36:08 +0000667 ALU_CASE(uabs_isub, iabsdiff);
668 ALU_CASE(uabs_usub, uabsdiff);
669
Jason Ekstrandf2dc0f22019-05-06 11:45:46 -0500670 ALU_CASE(mov, imov);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000671
Italo Nicolacea032a2020-09-23 05:41:38 +0000672 ALU_CASE_CMP(feq32, feq);
673 ALU_CASE_CMP(fneu32, fne);
674 ALU_CASE_CMP(flt32, flt);
675 ALU_CASE_CMP(ieq32, ieq);
676 ALU_CASE_CMP(ine32, ine);
677 ALU_CASE_CMP(ilt32, ilt);
678 ALU_CASE_CMP(ult32, ult);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000679
Alyssa Rosenzweig3208c9d2019-03-25 01:13:12 +0000680 /* We don't have a native b2f32 instruction. Instead, like many
681 * GPUs, we exploit booleans as 0/~0 for false/true, and
682 * correspondingly AND
683 * by 1.0 to do the type conversion. For the moment, prime us
684 * to emit:
685 *
686 * iand [whatever], #0
687 *
688 * At the end of emit_alu (as MIR), we'll fix-up the constant
689 */
690
Italo Nicolacea032a2020-09-23 05:41:38 +0000691 ALU_CASE_CMP(b2f32, iand);
692 ALU_CASE_CMP(b2f16, iand);
693 ALU_CASE_CMP(b2i32, iand);
Alyssa Rosenzweig3208c9d2019-03-25 01:13:12 +0000694
Alyssa Rosenzweigae43b8f2019-03-25 00:53:46 +0000695 /* Likewise, we don't have a dedicated f2b32 instruction, but
Alyssa Rosenzweig3208c9d2019-03-25 01:13:12 +0000696 * we can do a "not equal to 0.0" test. */
Alyssa Rosenzweigae43b8f2019-03-25 00:53:46 +0000697
Italo Nicolacea032a2020-09-23 05:41:38 +0000698 ALU_CASE_CMP(f2b32, fne);
699 ALU_CASE_CMP(i2b32, ine);
Alyssa Rosenzweigae43b8f2019-03-25 00:53:46 +0000700
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000701 ALU_CASE(frcp, frcp);
702 ALU_CASE(frsq, frsqrt);
703 ALU_CASE(fsqrt, fsqrt);
704 ALU_CASE(fexp2, fexp2);
705 ALU_CASE(flog2, flog2);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000706
Alyssa Rosenzweig93513cd2020-05-25 14:19:11 -0400707 ALU_CASE_RTZ(f2i64, f2i_rte);
708 ALU_CASE_RTZ(f2u64, f2u_rte);
709 ALU_CASE_RTZ(i2f64, i2f_rte);
710 ALU_CASE_RTZ(u2f64, u2f_rte);
Boris Brezillonfcceeaf2020-01-20 22:05:14 +0100711
Alyssa Rosenzweig93513cd2020-05-25 14:19:11 -0400712 ALU_CASE_RTZ(f2i32, f2i_rte);
713 ALU_CASE_RTZ(f2u32, f2u_rte);
714 ALU_CASE_RTZ(i2f32, i2f_rte);
715 ALU_CASE_RTZ(u2f32, u2f_rte);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000716
Alyssa Rosenzweig0ae01412020-05-25 14:46:40 -0400717 ALU_CASE_RTZ(f2i8, f2i_rte);
718 ALU_CASE_RTZ(f2u8, f2u_rte);
719
Alyssa Rosenzweig93513cd2020-05-25 14:19:11 -0400720 ALU_CASE_RTZ(f2i16, f2i_rte);
721 ALU_CASE_RTZ(f2u16, f2u_rte);
722 ALU_CASE_RTZ(i2f16, i2f_rte);
723 ALU_CASE_RTZ(u2f16, u2f_rte);
Alyssa Rosenzweigd8c084d2019-07-01 17:41:20 -0700724
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000725 ALU_CASE(fsin, fsin);
726 ALU_CASE(fcos, fcos);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000727
Alyssa Rosenzweig449e5de2020-04-30 13:46:35 -0400728 /* We'll get 0 in the second arg, so:
729 * ~a = ~(a | 0) = nor(a, 0) */
730 ALU_CASE(inot, inor);
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000731 ALU_CASE(iand, iand);
732 ALU_CASE(ior, ior);
733 ALU_CASE(ixor, ixor);
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000734 ALU_CASE(ishl, ishl);
735 ALU_CASE(ishr, iasr);
736 ALU_CASE(ushr, ilsr);
737
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700738 ALU_CASE_BCAST(b32all_fequal2, fball_eq, 2);
739 ALU_CASE_BCAST(b32all_fequal3, fball_eq, 3);
Italo Nicolacea032a2020-09-23 05:41:38 +0000740 ALU_CASE_CMP(b32all_fequal4, fball_eq);
Alyssa Rosenzweig53664102019-03-25 00:12:06 +0000741
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700742 ALU_CASE_BCAST(b32any_fnequal2, fbany_neq, 2);
743 ALU_CASE_BCAST(b32any_fnequal3, fbany_neq, 3);
Italo Nicolacea032a2020-09-23 05:41:38 +0000744 ALU_CASE_CMP(b32any_fnequal4, fbany_neq);
Alyssa Rosenzweig53664102019-03-25 00:12:06 +0000745
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700746 ALU_CASE_BCAST(b32all_iequal2, iball_eq, 2);
747 ALU_CASE_BCAST(b32all_iequal3, iball_eq, 3);
Italo Nicolacea032a2020-09-23 05:41:38 +0000748 ALU_CASE_CMP(b32all_iequal4, iball_eq);
Alyssa Rosenzweig53664102019-03-25 00:12:06 +0000749
Alyssa Rosenzweig195e2972019-06-19 07:23:27 -0700750 ALU_CASE_BCAST(b32any_inequal2, ibany_neq, 2);
751 ALU_CASE_BCAST(b32any_inequal3, ibany_neq, 3);
Italo Nicolacea032a2020-09-23 05:41:38 +0000752 ALU_CASE_CMP(b32any_inequal4, ibany_neq);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000753
Alyssa Rosenzweig659aa3d2019-05-26 03:16:37 +0000754 /* Source mods will be shoved in later */
755 ALU_CASE(fabs, fmov);
756 ALU_CASE(fneg, fmov);
757 ALU_CASE(fsat, fmov);
Alyssa Rosenzweig24e2e242020-05-04 16:12:41 -0400758 ALU_CASE(fsat_signed, fmov);
759 ALU_CASE(fclamp_pos, fmov);
Alyssa Rosenzweig659aa3d2019-05-26 03:16:37 +0000760
Alyssa Rosenzweig4df80ca2019-07-01 15:26:22 -0700761 /* For size conversion, we use a move. Ideally though we would squash
762 * these ops together; maybe that has to happen after in NIR as part of
763 * propagation...? An earlier algebraic pass ensured we step down by
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700764 * only / exactly one size. If stepping down, we use a dest override to
765 * reduce the size; if stepping up, we use a larger-sized move with a
766 * half source and a sign/zero-extension modifier */
Alyssa Rosenzweig4df80ca2019-07-01 15:26:22 -0700767
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700768 case nir_op_i2i8:
769 case nir_op_i2i16:
770 case nir_op_i2i32:
Alyssa Rosenzweig2655a302019-11-04 22:21:20 -0500771 case nir_op_i2i64:
Alyssa Rosenzweig4df80ca2019-07-01 15:26:22 -0700772 case nir_op_u2u8:
773 case nir_op_u2u16:
Alyssa Rosenzweig2655a302019-11-04 22:21:20 -0500774 case nir_op_u2u32:
Boris Brezillonf53a0792020-01-20 16:03:52 +0100775 case nir_op_u2u64:
776 case nir_op_f2f16:
Boris Brezillone1f9e8d2020-01-20 16:05:31 +0100777 case nir_op_f2f32:
778 case nir_op_f2f64: {
779 if (instr->op == nir_op_f2f16 || instr->op == nir_op_f2f32 ||
780 instr->op == nir_op_f2f64)
Boris Brezillonf53a0792020-01-20 16:03:52 +0100781 op = midgard_alu_op_fmov;
782 else
783 op = midgard_alu_op_imov;
Alyssa Rosenzweig7f807ef2019-07-01 16:44:00 -0700784
Alyssa Rosenzweig4df80ca2019-07-01 15:26:22 -0700785 break;
786 }
787
Alyssa Rosenzweig7b78af82019-03-26 04:01:33 +0000788 /* For greater-or-equal, we lower to less-or-equal and flip the
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000789 * arguments */
790
Alyssa Rosenzweig7b78af82019-03-26 04:01:33 +0000791 case nir_op_fge:
792 case nir_op_fge32:
793 case nir_op_ige32:
794 case nir_op_uge32: {
795 op =
796 instr->op == nir_op_fge ? midgard_alu_op_fle :
797 instr->op == nir_op_fge32 ? midgard_alu_op_fle :
798 instr->op == nir_op_ige32 ? midgard_alu_op_ile :
799 instr->op == nir_op_uge32 ? midgard_alu_op_ule :
800 0;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000801
Alyssa Rosenzweig22bb5a92020-04-29 18:08:26 -0400802 flip_src12 = true;
Italo Nicolacea032a2020-09-23 05:41:38 +0000803 ALU_CHECK_CMP();
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000804 break;
805 }
806
Alyssa Rosenzweig3fb88422019-03-25 00:25:01 +0000807 case nir_op_b32csel: {
Alyssa Rosenzweig726f0262019-05-07 02:52:08 +0000808 bool mixed = nir_is_non_scalar_swizzle(&instr->src[0], nr_components);
Alyssa Rosenzweigd39f95b2020-05-04 15:45:47 -0400809 bool is_float = mir_is_bcsel_float(instr);
810 op = is_float ?
811 (mixed ? midgard_alu_op_fcsel_v : midgard_alu_op_fcsel) :
812 (mixed ? midgard_alu_op_icsel_v : midgard_alu_op_icsel);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000813
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000814 break;
815 }
816
Alyssa Rosenzweig551d9902020-05-13 16:17:46 -0400817 case nir_op_unpack_32_2x16:
818 case nir_op_unpack_32_4x8:
819 case nir_op_pack_32_2x16:
820 case nir_op_pack_32_4x8: {
821 op = midgard_alu_op_imov;
822 break;
823 }
824
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000825 default:
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +0100826 DBG("Unhandled ALU op %s\n", nir_op_infos[instr->op].name);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000827 assert(0);
828 return;
829 }
830
Alyssa Rosenzweig72c1e3a2020-05-21 12:31:40 -0400831 /* Promote imov to fmov if it might help inline a constant */
832 if (op == midgard_alu_op_imov && nir_src_is_const(instr->src[0].src)
833 && nir_src_bit_size(instr->src[0].src) == 32
834 && nir_is_same_comp_swizzle(instr->src[0].swizzle,
835 nir_src_num_components(instr->src[0].src))) {
836 op = midgard_alu_op_fmov;
837 }
838
Alyssa Rosenzweig0a13bab2019-05-15 01:16:51 +0000839 /* Midgard can perform certain modifiers on output of an ALU op */
Alyssa Rosenzweig22bb5a92020-04-29 18:08:26 -0400840
841 unsigned outmod = 0;
Alyssa Rosenzweig22bb5a92020-04-29 18:08:26 -0400842 bool is_int = midgard_is_integer_op(op);
Alyssa Rosenzweig7bc91b42019-04-24 23:42:30 +0000843
Alyssa Rosenzweig3e2cb212020-08-27 14:35:23 -0400844 if (instr->op == nir_op_umul_high || instr->op == nir_op_imul_high) {
845 outmod = midgard_outmod_int_high;
846 } else if (midgard_is_integer_out_op(op)) {
Alyssa Rosenzweig67804812019-06-05 15:17:45 -0700847 outmod = midgard_outmod_int_wrap;
Alyssa Rosenzweig22bb5a92020-04-29 18:08:26 -0400848 } else if (instr->op == nir_op_fsat) {
849 outmod = midgard_outmod_sat;
850 } else if (instr->op == nir_op_fsat_signed) {
851 outmod = midgard_outmod_sat_signed;
852 } else if (instr->op == nir_op_fclamp_pos) {
853 outmod = midgard_outmod_pos;
Alyssa Rosenzweig67804812019-06-05 15:17:45 -0700854 }
Alyssa Rosenzweig659aa3d2019-05-26 03:16:37 +0000855
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000856 /* Fetch unit, quirks, etc information */
Alyssa Rosenzweig1f345bc2019-04-24 01:15:15 +0000857 unsigned opcode_props = alu_opcode_props[op].props;
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000858 bool quirk_flipped_r24 = opcode_props & QUIRK_FLIPPED_R24;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000859
Italo Nicola20969032020-07-13 16:19:08 +0000860 if (!midgard_is_integer_out_op(op)) {
Italo Nicola83592de2020-07-15 18:48:42 +0000861 outmod = mir_determine_float_outmod(ctx, &dest, outmod);
Alyssa Rosenzweig29afa882020-05-04 17:33:52 -0400862 }
863
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000864 midgard_instruction ins = {
865 .type = TAG_ALU_4,
Alyssa Rosenzweig22bb5a92020-04-29 18:08:26 -0400866 .dest = nir_dest_index(dest),
Alyssa Rosenzweigecf94662020-04-27 18:57:34 -0400867 .dest_type = nir_op_infos[instr->op].output_type
Alyssa Rosenzweig22bb5a92020-04-29 18:08:26 -0400868 | nir_dest_bit_size(*dest),
Alyssa Rosenzweig93513cd2020-05-25 14:19:11 -0400869 .roundmode = roundmode,
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000870 };
871
Alyssa Rosenzweigf8b881f2020-05-25 14:19:24 -0400872 enum midgard_roundmode *roundptr = (opcode_props & MIDGARD_ROUNDS) ?
873 &ins.roundmode : NULL;
874
Alyssa Rosenzweig6757c482020-04-27 18:55:11 -0400875 for (unsigned i = nr_inputs; i < ARRAY_SIZE(ins.src); ++i)
876 ins.src[i] = ~0;
877
878 if (quirk_flipped_r24) {
879 ins.src[0] = ~0;
Alyssa Rosenzweigf8b881f2020-05-25 14:19:24 -0400880 mir_copy_src(&ins, instr, 0, 1, &ins.src_abs[1], &ins.src_neg[1], &ins.src_invert[1], roundptr, is_int, broadcast_swizzle);
Alyssa Rosenzweig6757c482020-04-27 18:55:11 -0400881 } else {
Alyssa Rosenzweig22bb5a92020-04-29 18:08:26 -0400882 for (unsigned i = 0; i < nr_inputs; ++i) {
883 unsigned to = i;
884
885 if (instr->op == nir_op_b32csel) {
886 /* The condition is the first argument; move
887 * the other arguments up one to be a binary
888 * instruction for Midgard with the condition
889 * last */
890
891 if (i == 0)
892 to = 2;
Alyssa Rosenzweig449e5de2020-04-30 13:46:35 -0400893 else if (flip_src12)
894 to = 2 - i;
Alyssa Rosenzweig22bb5a92020-04-29 18:08:26 -0400895 else
896 to = i - 1;
897 } else if (flip_src12) {
898 to = 1 - to;
899 }
900
Alyssa Rosenzweigf8b881f2020-05-25 14:19:24 -0400901 mir_copy_src(&ins, instr, i, to, &ins.src_abs[to], &ins.src_neg[to], &ins.src_invert[to], roundptr, is_int, broadcast_swizzle);
Alyssa Rosenzweig449e5de2020-04-30 13:46:35 -0400902
903 /* (!c) ? a : b = c ? b : a */
904 if (instr->op == nir_op_b32csel && ins.src_invert[2]) {
905 ins.src_invert[2] = false;
906 flip_src12 ^= true;
907 }
Alyssa Rosenzweig22bb5a92020-04-29 18:08:26 -0400908 }
Alyssa Rosenzweig6757c482020-04-27 18:55:11 -0400909 }
910
Alyssa Rosenzweig659aa3d2019-05-26 03:16:37 +0000911 if (instr->op == nir_op_fneg || instr->op == nir_op_fabs) {
Alyssa Rosenzweig22bb5a92020-04-29 18:08:26 -0400912 /* Lowered to move */
Alyssa Rosenzweig659aa3d2019-05-26 03:16:37 +0000913 if (instr->op == nir_op_fneg)
Alyssa Rosenzweig1cd65352020-05-21 12:38:27 -0400914 ins.src_neg[1] ^= true;
Alyssa Rosenzweig659aa3d2019-05-26 03:16:37 +0000915
916 if (instr->op == nir_op_fabs)
Alyssa Rosenzweig1cd65352020-05-21 12:38:27 -0400917 ins.src_abs[1] = true;
Alyssa Rosenzweig659aa3d2019-05-26 03:16:37 +0000918 }
919
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -0700920 ins.mask = mask_of(nr_components);
921
Alyssa Rosenzweig22bb5a92020-04-29 18:08:26 -0400922 /* Apply writemask if non-SSA, keeping in mind that we can't write to
923 * components that don't exist. Note modifier => SSA => !reg => no
924 * writemask, so we don't have to worry about writemasks here.*/
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000925
926 if (!is_ssa)
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -0700927 ins.mask &= instr->dest.write_mask;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000928
Italo Nicolaf4c89bf2020-07-09 12:02:57 +0000929 ins.op = op;
Italo Nicola50113732020-07-15 18:43:18 +0000930 ins.outmod = outmod;
Italo Nicolaf4c89bf2020-07-09 12:02:57 +0000931
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000932 /* Late fixup for emulated instructions */
933
Alyssa Rosenzweig3208c9d2019-03-25 01:13:12 +0000934 if (instr->op == nir_op_b2f32 || instr->op == nir_op_b2i32) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000935 /* Presently, our second argument is an inline #0 constant.
936 * Switch over to an embedded 1.0 constant (that can't fit
937 * inline, since we're 32-bit, not 16-bit like the inline
938 * constants) */
939
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -0700940 ins.has_inline_constant = false;
941 ins.src[1] = SSA_FIXED_REGISTER(REGISTER_CONSTANT);
Alyssa Rosenzweig074815c2020-04-29 16:29:01 -0400942 ins.src_types[1] = nir_type_float32;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000943 ins.has_constants = true;
Alyssa Rosenzweig9da46032019-03-24 16:07:31 +0000944
Boris Brezillon15c92d12020-01-20 15:00:57 +0100945 if (instr->op == nir_op_b2f32)
946 ins.constants.f32[0] = 1.0f;
947 else
948 ins.constants.i32[0] = 1;
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -0400949
950 for (unsigned c = 0; c < 16; ++c)
951 ins.swizzle[1][c] = 0;
Alyssa Rosenzweig6b023b32020-05-08 17:42:40 -0400952 } else if (instr->op == nir_op_b2f16) {
953 ins.src[1] = SSA_FIXED_REGISTER(REGISTER_CONSTANT);
954 ins.src_types[1] = nir_type_float16;
955 ins.has_constants = true;
956 ins.constants.i16[0] = _mesa_float_to_half(1.0);
957
958 for (unsigned c = 0; c < 16; ++c)
959 ins.swizzle[1][c] = 0;
Alyssa Rosenzweig88c59792019-06-05 15:24:51 +0000960 } else if (nr_inputs == 1 && !quirk_flipped_r24) {
961 /* Lots of instructions need a 0 plonked in */
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -0700962 ins.has_inline_constant = false;
963 ins.src[1] = SSA_FIXED_REGISTER(REGISTER_CONSTANT);
Italo Nicolab1b0ce02020-07-10 14:51:52 +0000964 ins.src_types[1] = ins.src_types[0];
Alyssa Rosenzweig3208c9d2019-03-25 01:13:12 +0000965 ins.has_constants = true;
Boris Brezillon15c92d12020-01-20 15:00:57 +0100966 ins.constants.u32[0] = 0;
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -0400967
968 for (unsigned c = 0; c < 16; ++c)
969 ins.swizzle[1][c] = 0;
Alyssa Rosenzweig551d9902020-05-13 16:17:46 -0400970 } else if (instr->op == nir_op_pack_32_2x16) {
971 ins.dest_type = nir_type_uint16;
972 ins.mask = mask_of(nr_components * 2);
Alyssa Rosenzweige9c780b2020-05-13 18:41:52 -0400973 ins.is_pack = true;
Alyssa Rosenzweig551d9902020-05-13 16:17:46 -0400974 } else if (instr->op == nir_op_pack_32_4x8) {
975 ins.dest_type = nir_type_uint8;
976 ins.mask = mask_of(nr_components * 4);
Alyssa Rosenzweige9c780b2020-05-13 18:41:52 -0400977 ins.is_pack = true;
Alyssa Rosenzweig551d9902020-05-13 16:17:46 -0400978 } else if (instr->op == nir_op_unpack_32_2x16) {
979 ins.dest_type = nir_type_uint32;
980 ins.mask = mask_of(nr_components >> 1);
Alyssa Rosenzweige9c780b2020-05-13 18:41:52 -0400981 ins.is_pack = true;
Alyssa Rosenzweig551d9902020-05-13 16:17:46 -0400982 } else if (instr->op == nir_op_unpack_32_4x8) {
983 ins.dest_type = nir_type_uint32;
984 ins.mask = mask_of(nr_components >> 2);
Alyssa Rosenzweige9c780b2020-05-13 18:41:52 -0400985 ins.is_pack = true;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000986 }
987
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +0000988 if ((opcode_props & UNITS_ALL) == UNIT_VLUT) {
989 /* To avoid duplicating the lookup tables (probably), true LUT
990 * instructions can only operate as if they were scalars. Lower
991 * them here by changing the component. */
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000992
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -0700993 unsigned orig_mask = ins.mask;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +0000994
Alyssa Rosenzweig22bb5a92020-04-29 18:08:26 -0400995 unsigned swizzle_back[MIR_VEC_COMPONENTS];
996 memcpy(&swizzle_back, ins.swizzle[0], sizeof(swizzle_back));
997
Icecream95a6f0d7f2020-05-24 00:23:25 +1200998 midgard_instruction ins_split[MIR_VEC_COMPONENTS];
999 unsigned ins_count = 0;
1000
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001001 for (int i = 0; i < nr_components; ++i) {
Alyssa Rosenzweig2c9e1242019-06-17 11:49:44 -07001002 /* Mask the associated component, dropping the
1003 * instruction if needed */
1004
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -07001005 ins.mask = 1 << i;
1006 ins.mask &= orig_mask;
Alyssa Rosenzweig2c9e1242019-06-17 11:49:44 -07001007
Icecream95a6f0d7f2020-05-24 00:23:25 +12001008 for (unsigned j = 0; j < ins_count; ++j) {
1009 if (swizzle_back[i] == ins_split[j].swizzle[0][0]) {
1010 ins_split[j].mask |= ins.mask;
1011 ins.mask = 0;
1012 break;
1013 }
1014 }
1015
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -07001016 if (!ins.mask)
Alyssa Rosenzweig2c9e1242019-06-17 11:49:44 -07001017 continue;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001018
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001019 for (unsigned j = 0; j < MIR_VEC_COMPONENTS; ++j)
Alyssa Rosenzweig22bb5a92020-04-29 18:08:26 -04001020 ins.swizzle[0][j] = swizzle_back[i]; /* Pull from the correct component */
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001021
Icecream95a6f0d7f2020-05-24 00:23:25 +12001022 ins_split[ins_count] = ins;
1023
1024 ++ins_count;
1025 }
1026
1027 for (unsigned i = 0; i < ins_count; ++i) {
1028 emit_mir_instruction(ctx, ins_split[i]);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001029 }
1030 } else {
1031 emit_mir_instruction(ctx, ins);
1032 }
1033}
1034
Alyssa Rosenzweig97dcad82019-02-07 03:39:25 +00001035#undef ALU_CASE
1036
Alyssa Rosenzweig1798f6b2019-11-15 15:16:53 -05001037static void
1038mir_set_intr_mask(nir_instr *instr, midgard_instruction *ins, bool is_read)
Alyssa Rosenzweig65e6cb42019-08-13 09:11:48 -07001039{
1040 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
Alyssa Rosenzweig1798f6b2019-11-15 15:16:53 -05001041 unsigned nir_mask = 0;
1042 unsigned dsize = 0;
Alyssa Rosenzweig65e6cb42019-08-13 09:11:48 -07001043
Alyssa Rosenzweig1798f6b2019-11-15 15:16:53 -05001044 if (is_read) {
1045 nir_mask = mask_of(nir_intrinsic_dest_components(intr));
1046 dsize = nir_dest_bit_size(intr->dest);
1047 } else {
1048 nir_mask = nir_intrinsic_write_mask(intr);
1049 dsize = 32;
1050 }
1051
1052 /* Once we have the NIR mask, we need to normalize to work in 32-bit space */
Alyssa Rosenzweig9b8cb9f2020-03-09 20:19:29 -04001053 unsigned bytemask = pan_to_bytemask(dsize, nir_mask);
Alyssa Rosenzweigb91d7152020-05-11 15:06:53 -04001054 ins->dest_type = nir_type_uint | dsize;
Italo Nicola11012612020-08-26 14:56:13 +00001055 mir_set_bytemask(ins, bytemask);
Alyssa Rosenzweig65e6cb42019-08-13 09:11:48 -07001056}
1057
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001058/* Uniforms and UBOs use a shared code path, as uniforms are just (slightly
1059 * optimized) versions of UBO #0 */
1060
Alyssa Rosenzweig59d30fd2020-01-10 17:47:57 -05001061static midgard_instruction *
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001062emit_ubo_read(
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001063 compiler_context *ctx,
Alyssa Rosenzweig65e6cb42019-08-13 09:11:48 -07001064 nir_instr *instr,
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001065 unsigned dest,
1066 unsigned offset,
1067 nir_src *indirect_offset,
Alyssa Rosenzweig59d30fd2020-01-10 17:47:57 -05001068 unsigned indirect_shift,
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001069 unsigned index)
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001070{
1071 /* TODO: half-floats */
1072
Alyssa Rosenzweigbc9a7d02019-11-15 14:19:34 -05001073 midgard_instruction ins = m_ld_ubo_int4(dest, 0);
Boris Brezillon15c92d12020-01-20 15:00:57 +01001074 ins.constants.u32[0] = offset;
Alyssa Rosenzweigda736512019-12-19 11:12:25 -05001075
1076 if (instr->type == nir_instr_type_intrinsic)
1077 mir_set_intr_mask(instr, &ins, true);
Alyssa Rosenzweig3174bc92019-07-16 14:10:08 -07001078
1079 if (indirect_offset) {
Alyssa Rosenzweige7fd14c2019-10-26 15:50:38 -04001080 ins.src[2] = nir_src_index(ctx, indirect_offset);
Alyssa Rosenzweig074815c2020-04-29 16:29:01 -04001081 ins.src_types[2] = nir_type_uint32;
Alyssa Rosenzweig59d30fd2020-01-10 17:47:57 -05001082 ins.load_store.arg_2 = (indirect_shift << 5);
Alyssa Rosenzweig797fa872020-07-06 10:57:04 -04001083
1084 /* X component for the whole swizzle to prevent register
1085 * pressure from ballooning from the extra components */
1086 for (unsigned i = 0; i < ARRAY_SIZE(ins.swizzle[2]); ++i)
1087 ins.swizzle[2][i] = 0;
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001088 } else {
Alyssa Rosenzweigc9087722019-08-01 13:29:01 -07001089 ins.load_store.arg_2 = 0x1E;
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001090 }
Alyssa Rosenzweig3174bc92019-07-16 14:10:08 -07001091
Alyssa Rosenzweigc9087722019-08-01 13:29:01 -07001092 ins.load_store.arg_1 = index;
1093
Alyssa Rosenzweige7ac46b2019-08-02 17:09:54 -07001094 return emit_mir_instruction(ctx, ins);
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001095}
1096
Alyssa Rosenzweig3a310fb2020-02-05 15:17:44 -05001097/* Globals are like UBOs if you squint. And shared memory is like globals if
1098 * you squint even harder */
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001099
1100static void
Alyssa Rosenzweig31489372019-11-05 08:59:49 -05001101emit_global(
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001102 compiler_context *ctx,
1103 nir_instr *instr,
1104 bool is_read,
1105 unsigned srcdest,
Alyssa Rosenzweig0bb25e42020-02-27 09:41:17 -05001106 nir_src *offset,
Alyssa Rosenzweig3a310fb2020-02-05 15:17:44 -05001107 bool is_shared)
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001108{
1109 /* TODO: types */
1110
Dylan Baker8e369612018-09-14 12:57:32 -07001111 midgard_instruction ins;
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001112
1113 if (is_read)
Alyssa Rosenzweig31489372019-11-05 08:59:49 -05001114 ins = m_ld_int4(srcdest, 0);
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001115 else
Alyssa Rosenzweig31489372019-11-05 08:59:49 -05001116 ins = m_st_int4(srcdest, 0);
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001117
Alyssa Rosenzweig0bb25e42020-02-27 09:41:17 -05001118 mir_set_offset(ctx, &ins, offset, is_shared);
Alyssa Rosenzweig1798f6b2019-11-15 15:16:53 -05001119 mir_set_intr_mask(instr, &ins, is_read);
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001120
Alyssa Rosenzweig41184f82020-08-27 15:13:19 -04001121 /* Set a valid swizzle for masked out components */
1122 assert(ins.mask);
1123 unsigned first_component = __builtin_ffs(ins.mask) - 1;
1124
1125 for (unsigned i = 0; i < ARRAY_SIZE(ins.swizzle[0]); ++i) {
1126 if (!(ins.mask & (1 << i)))
1127 ins.swizzle[0][i] = first_component;
1128 }
1129
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001130 emit_mir_instruction(ctx, ins);
1131}
1132
Italo Nicola8e221f52020-08-31 11:17:48 +00001133/* If is_shared is off, the only other possible value are globals, since
1134 * SSBO's are being lowered to globals through a NIR pass. */
1135static void
1136emit_atomic(
1137 compiler_context *ctx,
1138 nir_intrinsic_instr *instr,
1139 bool is_shared,
1140 midgard_load_store_op op)
1141{
1142 unsigned bitsize = nir_src_bit_size(instr->src[1]);
1143 nir_alu_type type =
1144 (op == midgard_op_atomic_imin || op == midgard_op_atomic_imax) ?
1145 nir_type_int : nir_type_uint;
1146
1147 unsigned dest = nir_dest_index(&instr->dest);
1148 unsigned val = nir_src_index(ctx, &instr->src[1]);
1149 emit_explicit_constant(ctx, val, val);
1150
1151 midgard_instruction ins = {
1152 .type = TAG_LOAD_STORE_4,
1153 .mask = 0xF,
1154 .dest = dest,
1155 .src = { ~0, ~0, ~0, val },
1156 .src_types = { 0, 0, 0, type | bitsize },
1157 .op = op
1158 };
1159
1160 nir_src *src_offset = nir_get_io_offset_src(instr);
1161
1162 /* cmpxchg takes an extra value in arg_2, so we don't use it for the offset */
1163 if (op == midgard_op_atomic_cmpxchg) {
1164 unsigned addr = nir_src_index(ctx, src_offset);
1165
1166 ins.src[1] = addr;
1167 ins.src_types[1] = nir_type_uint | nir_src_bit_size(*src_offset);
1168
1169 unsigned xchg_val = nir_src_index(ctx, &instr->src[2]);
1170 emit_explicit_constant(ctx, xchg_val, xchg_val);
1171
1172 ins.src[2] = val;
1173 ins.src_types[2] = type | bitsize;
1174 ins.src[3] = xchg_val;
1175
1176 if (is_shared)
1177 ins.load_store.arg_1 |= 0x6E;
1178 } else {
1179 mir_set_offset(ctx, &ins, src_offset, is_shared);
1180 }
1181
1182 mir_set_intr_mask(&instr->instr, &ins, true);
1183
1184 emit_mir_instruction(ctx, ins);
1185}
1186
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001187static void
Alyssa Rosenzweig15fae1e2019-06-04 23:26:09 +00001188emit_varying_read(
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001189 compiler_context *ctx,
1190 unsigned dest, unsigned offset,
1191 unsigned nr_comp, unsigned component,
Alyssa Rosenzweigc17a4412019-12-27 15:32:50 -05001192 nir_src *indirect_offset, nir_alu_type type, bool flat)
Alyssa Rosenzweig15fae1e2019-06-04 23:26:09 +00001193{
1194 /* XXX: Half-floats? */
1195 /* TODO: swizzle, mask */
1196
1197 midgard_instruction ins = m_ld_vary_32(dest, offset);
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -07001198 ins.mask = mask_of(nr_comp);
Alyssa Rosenzweig2d168832020-06-04 11:32:59 -04001199 ins.dest_type = type;
1200
1201 if (type == nir_type_float16) {
1202 /* Ensure we are aligned so we can pack it later */
1203 ins.mask = mask_of(ALIGN_POT(nr_comp, 2));
1204 }
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001205
1206 for (unsigned i = 0; i < ARRAY_SIZE(ins.swizzle[0]); ++i)
1207 ins.swizzle[0][i] = MIN2(i + component, COMPONENT_W);
Alyssa Rosenzweig15fae1e2019-06-04 23:26:09 +00001208
1209 midgard_varying_parameter p = {
1210 .is_varying = 1,
1211 .interpolation = midgard_interp_default,
Alyssa Rosenzweigc17a4412019-12-27 15:32:50 -05001212 .flat = flat,
Alyssa Rosenzweig15fae1e2019-06-04 23:26:09 +00001213 };
1214
1215 unsigned u;
1216 memcpy(&u, &p, sizeof(p));
1217 ins.load_store.varying_parameters = u;
1218
Alyssa Rosenzweig074815c2020-04-29 16:29:01 -04001219 if (indirect_offset) {
Alyssa Rosenzweige7fd14c2019-10-26 15:50:38 -04001220 ins.src[2] = nir_src_index(ctx, indirect_offset);
Alyssa Rosenzweig074815c2020-04-29 16:29:01 -04001221 ins.src_types[2] = nir_type_uint32;
1222 } else
Alyssa Rosenzweigc9087722019-08-01 13:29:01 -07001223 ins.load_store.arg_2 = 0x1E;
Alyssa Rosenzweig15fae1e2019-06-04 23:26:09 +00001224
Alyssa Rosenzweigc9087722019-08-01 13:29:01 -07001225 ins.load_store.arg_1 = 0x9E;
1226
Alyssa Rosenzweig9b97ed12019-06-28 09:30:59 -07001227 /* Use the type appropriate load */
1228 switch (type) {
Alyssa Rosenzweig5f8dd412020-05-22 16:22:48 -04001229 case nir_type_uint32:
1230 case nir_type_bool32:
Italo Nicolabea6a652020-07-23 19:24:39 +00001231 ins.op = midgard_op_ld_vary_32u;
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001232 break;
Alyssa Rosenzweig5f8dd412020-05-22 16:22:48 -04001233 case nir_type_int32:
Italo Nicolabea6a652020-07-23 19:24:39 +00001234 ins.op = midgard_op_ld_vary_32i;
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001235 break;
Alyssa Rosenzweig5f8dd412020-05-22 16:22:48 -04001236 case nir_type_float32:
Italo Nicolabea6a652020-07-23 19:24:39 +00001237 ins.op = midgard_op_ld_vary_32;
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001238 break;
Alyssa Rosenzweig5f8dd412020-05-22 16:22:48 -04001239 case nir_type_float16:
Italo Nicolabea6a652020-07-23 19:24:39 +00001240 ins.op = midgard_op_ld_vary_16;
Alyssa Rosenzweig5f8dd412020-05-22 16:22:48 -04001241 break;
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001242 default:
1243 unreachable("Attempted to load unknown type");
1244 break;
Alyssa Rosenzweig9b97ed12019-06-28 09:30:59 -07001245 }
1246
Alyssa Rosenzweig15fae1e2019-06-04 23:26:09 +00001247 emit_mir_instruction(ctx, ins);
1248}
1249
Alyssa Rosenzweig6e688902019-12-19 13:24:17 -05001250static void
1251emit_attr_read(
1252 compiler_context *ctx,
1253 unsigned dest, unsigned offset,
1254 unsigned nr_comp, nir_alu_type t)
1255{
1256 midgard_instruction ins = m_ld_attr_32(dest, offset);
1257 ins.load_store.arg_1 = 0x1E;
1258 ins.load_store.arg_2 = 0x1E;
1259 ins.mask = mask_of(nr_comp);
1260
1261 /* Use the type appropriate load */
1262 switch (t) {
1263 case nir_type_uint:
1264 case nir_type_bool:
Italo Nicolabea6a652020-07-23 19:24:39 +00001265 ins.op = midgard_op_ld_attr_32u;
Alyssa Rosenzweig6e688902019-12-19 13:24:17 -05001266 break;
1267 case nir_type_int:
Italo Nicolabea6a652020-07-23 19:24:39 +00001268 ins.op = midgard_op_ld_attr_32i;
Alyssa Rosenzweig6e688902019-12-19 13:24:17 -05001269 break;
1270 case nir_type_float:
Italo Nicolabea6a652020-07-23 19:24:39 +00001271 ins.op = midgard_op_ld_attr_32;
Alyssa Rosenzweig6e688902019-12-19 13:24:17 -05001272 break;
1273 default:
1274 unreachable("Attempted to load unknown type");
1275 break;
1276 }
1277
1278 emit_mir_instruction(ctx, ins);
1279}
1280
Alyssa Rosenzweigfcbb3d42020-02-04 09:46:17 -05001281static void
Alyssa Rosenzweigb756a662020-03-10 16:19:33 -04001282emit_sysval_read(compiler_context *ctx, nir_instr *instr,
Alyssa Rosenzweigfcbb3d42020-02-04 09:46:17 -05001283 unsigned nr_components, unsigned offset)
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001284{
Alyssa Rosenzweig674b24d2020-03-10 15:54:17 -04001285 nir_dest nir_dest;
Alyssa Rosenzweig6d8490f2019-07-11 15:34:56 -07001286
Boris Brezillonbd49c8f2019-06-14 09:59:20 +02001287 /* Figure out which uniform this is */
Alyssa Rosenzweige6102672020-03-10 16:06:30 -04001288 int sysval = panfrost_sysval_for_instr(instr, &nir_dest);
Alyssa Rosenzweigc2ff3bb2020-03-10 16:00:56 -04001289 void *val = _mesa_hash_table_u64_search(ctx->sysvals.sysval_to_id, sysval);
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001290
Alyssa Rosenzweig674b24d2020-03-10 15:54:17 -04001291 unsigned dest = nir_dest_index(&nir_dest);
1292
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001293 /* Sysvals are prefix uniforms */
1294 unsigned uniform = ((uintptr_t) val) - 1;
1295
Alyssa Rosenzweig6a466c02019-04-20 23:52:42 +00001296 /* Emit the read itself -- this is never indirect */
Alyssa Rosenzweig63e240d2019-08-02 17:10:18 -07001297 midgard_instruction *ins =
Alyssa Rosenzweigfcbb3d42020-02-04 09:46:17 -05001298 emit_ubo_read(ctx, instr, dest, (uniform * 16) + offset, NULL, 0, 0);
Alyssa Rosenzweig63e240d2019-08-02 17:10:18 -07001299
1300 ins->mask = mask_of(nr_components);
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001301}
1302
Alyssa Rosenzweig7229af72019-08-06 13:47:17 -07001303static unsigned
1304compute_builtin_arg(nir_op op)
1305{
1306 switch (op) {
1307 case nir_intrinsic_load_work_group_id:
1308 return 0x14;
1309 case nir_intrinsic_load_local_invocation_id:
1310 return 0x10;
1311 default:
1312 unreachable("Invalid compute paramater loaded");
1313 }
1314}
1315
Alyssa Rosenzweigdff49862019-08-12 12:36:46 -07001316static void
Icecream95a6806342020-06-06 15:41:51 +12001317emit_fragment_store(compiler_context *ctx, unsigned src, unsigned src_z, unsigned src_s, enum midgard_rt_id rt)
Alyssa Rosenzweigdff49862019-08-12 12:36:46 -07001318{
Boris Brezillone1ba0cd2020-01-31 10:05:16 +01001319 assert(rt < ARRAY_SIZE(ctx->writeout_branch));
1320
1321 midgard_instruction *br = ctx->writeout_branch[rt];
1322
1323 assert(!br);
1324
Alyssa Rosenzweig5e06d902019-08-30 11:06:33 -07001325 emit_explicit_constant(ctx, src, src);
Alyssa Rosenzweigdff49862019-08-12 12:36:46 -07001326
Alyssa Rosenzweigdff49862019-08-12 12:36:46 -07001327 struct midgard_instruction ins =
Alyssa Rosenzweig02f503e2019-12-30 18:53:04 -05001328 v_branch(false, false);
1329
Icecream9592d3f1f2020-06-06 15:08:06 +12001330 bool depth_only = (rt == MIDGARD_ZS_RT);
1331
Icecream95a6806342020-06-06 15:41:51 +12001332 ins.writeout = depth_only ? 0 : PAN_WRITEOUT_C;
Alyssa Rosenzweigdff49862019-08-12 12:36:46 -07001333
1334 /* Add dependencies */
Alyssa Rosenzweig76529832019-08-30 11:01:15 -07001335 ins.src[0] = src;
Alyssa Rosenzweig074815c2020-04-29 16:29:01 -04001336 ins.src_types[0] = nir_type_uint32;
Icecream9592d3f1f2020-06-06 15:08:06 +12001337 ins.constants.u32[0] = depth_only ? 0xFF : (rt - MIDGARD_COLOR_RT0) * 0x100;
Icecream952a5504f2020-06-06 14:42:18 +12001338 for (int i = 0; i < 4; ++i)
1339 ins.swizzle[0][i] = i;
Alyssa Rosenzweigdff49862019-08-12 12:36:46 -07001340
Icecream95a6806342020-06-06 15:41:51 +12001341 if (~src_z) {
1342 emit_explicit_constant(ctx, src_z, src_z);
1343 ins.src[2] = src_z;
1344 ins.src_types[2] = nir_type_uint32;
1345 ins.writeout |= PAN_WRITEOUT_Z;
1346 }
1347 if (~src_s) {
1348 emit_explicit_constant(ctx, src_s, src_s);
1349 ins.src[3] = src_s;
1350 ins.src_types[3] = nir_type_uint32;
1351 ins.writeout |= PAN_WRITEOUT_S;
1352 }
1353
Alyssa Rosenzweigdff49862019-08-12 12:36:46 -07001354 /* Emit the branch */
Boris Brezillone1ba0cd2020-01-31 10:05:16 +01001355 br = emit_mir_instruction(ctx, ins);
Alyssa Rosenzweig281cc6f2019-11-23 12:43:55 -05001356 schedule_barrier(ctx);
Alyssa Rosenzweig5bc62af2020-01-02 12:27:59 -05001357 ctx->writeout_branch[rt] = br;
1358
1359 /* Push our current location = current block count - 1 = where we'll
1360 * jump to. Maybe a bit too clever for my own good */
1361
1362 br->branch.target_block = ctx->block_count - 1;
Alyssa Rosenzweigdff49862019-08-12 12:36:46 -07001363}
1364
Alyssa Rosenzweig7229af72019-08-06 13:47:17 -07001365static void
1366emit_compute_builtin(compiler_context *ctx, nir_intrinsic_instr *instr)
1367{
Alyssa Rosenzweig7c2647f2020-03-10 15:48:52 -04001368 unsigned reg = nir_dest_index(&instr->dest);
Alyssa Rosenzweig7229af72019-08-06 13:47:17 -07001369 midgard_instruction ins = m_ld_compute_id(reg, 0);
1370 ins.mask = mask_of(3);
Alyssa Rosenzweigd3747fb2020-02-12 08:39:29 -05001371 ins.swizzle[0][3] = COMPONENT_X; /* xyzx */
Alyssa Rosenzweig7229af72019-08-06 13:47:17 -07001372 ins.load_store.arg_1 = compute_builtin_arg(instr->intrinsic);
1373 emit_mir_instruction(ctx, ins);
1374}
Alyssa Rosenzweig306800d2019-12-19 13:31:21 -05001375
1376static unsigned
1377vertex_builtin_arg(nir_op op)
1378{
1379 switch (op) {
1380 case nir_intrinsic_load_vertex_id:
1381 return PAN_VERTEX_ID;
1382 case nir_intrinsic_load_instance_id:
1383 return PAN_INSTANCE_ID;
1384 default:
1385 unreachable("Invalid vertex builtin");
1386 }
1387}
1388
1389static void
1390emit_vertex_builtin(compiler_context *ctx, nir_intrinsic_instr *instr)
1391{
Alyssa Rosenzweig7c2647f2020-03-10 15:48:52 -04001392 unsigned reg = nir_dest_index(&instr->dest);
Alyssa Rosenzweig306800d2019-12-19 13:31:21 -05001393 emit_attr_read(ctx, reg, vertex_builtin_arg(instr->intrinsic), 1, nir_type_int);
1394}
1395
Alyssa Rosenzweig3f590982020-02-03 20:23:41 -05001396static void
Alyssa Rosenzweig80ebf112020-08-27 19:55:53 -04001397emit_special(compiler_context *ctx, nir_intrinsic_instr *instr, unsigned idx)
Alyssa Rosenzweigda2eed32020-07-15 09:56:24 -04001398{
1399 unsigned reg = nir_dest_index(&instr->dest);
1400
1401 midgard_instruction ld = m_ld_color_buffer_32u(reg, 0);
Italo Nicolabea6a652020-07-23 19:24:39 +00001402 ld.op = midgard_op_ld_color_buffer_32u_old;
Alyssa Rosenzweig80ebf112020-08-27 19:55:53 -04001403 ld.load_store.address = idx;
Alyssa Rosenzweigda2eed32020-07-15 09:56:24 -04001404 ld.load_store.arg_2 = 0x1E;
1405
1406 for (int i = 0; i < 4; ++i)
1407 ld.swizzle[0][i] = COMPONENT_X;
1408
1409 emit_mir_instruction(ctx, ld);
1410}
1411
1412static void
Alyssa Rosenzweig3f590982020-02-03 20:23:41 -05001413emit_control_barrier(compiler_context *ctx)
1414{
1415 midgard_instruction ins = {
1416 .type = TAG_TEXTURE_4,
Alyssa Rosenzweigfde1f2b2020-05-13 11:05:34 -04001417 .dest = ~0,
Alyssa Rosenzweig3f590982020-02-03 20:23:41 -05001418 .src = { ~0, ~0, ~0, ~0 },
Italo Nicola92c808c2020-07-29 19:10:25 +00001419 .op = TEXTURE_OP_BARRIER,
Alyssa Rosenzweig3f590982020-02-03 20:23:41 -05001420 };
1421
1422 emit_mir_instruction(ctx, ins);
1423}
1424
Alyssa Rosenzweigdb7b0eb2020-04-30 14:17:06 -04001425static unsigned
1426mir_get_branch_cond(nir_src *src, bool *invert)
1427{
1428 /* Wrap it. No swizzle since it's a scalar */
1429
1430 nir_alu_src alu = {
1431 .src = *src
1432 };
1433
1434 *invert = pan_has_source_mod(&alu, nir_op_inot);
1435 return nir_src_index(NULL, &alu.src);
1436}
1437
Icecream957781d2c2020-07-06 19:54:56 +12001438static uint8_t
Icecream95e7641922020-07-19 22:31:26 +12001439output_load_rt_addr(compiler_context *ctx, nir_intrinsic_instr *instr)
Icecream957781d2c2020-07-06 19:54:56 +12001440{
Icecream95e7641922020-07-19 22:31:26 +12001441 if (ctx->is_blend)
1442 return ctx->blend_rt;
1443
Icecream957781d2c2020-07-06 19:54:56 +12001444 const nir_variable *var;
Alyssa Rosenzweigdfaa4c52020-11-04 08:32:16 -05001445 var = nir_find_variable_with_driver_location(ctx->nir, nir_var_shader_out, nir_intrinsic_base(instr));
Icecream957781d2c2020-07-06 19:54:56 +12001446 assert(var);
1447
1448 unsigned loc = var->data.location;
1449
1450 if (loc == FRAG_RESULT_COLOR)
1451 loc = FRAG_RESULT_DATA0;
1452
1453 if (loc >= FRAG_RESULT_DATA0)
1454 return loc - FRAG_RESULT_DATA0;
1455
1456 if (loc == FRAG_RESULT_DEPTH)
1457 return 0x1F;
1458 if (loc == FRAG_RESULT_STENCIL)
1459 return 0x1E;
1460
Icecream956493d292020-07-14 15:06:09 +12001461 unreachable("Invalid RT to load from");
Icecream957781d2c2020-07-06 19:54:56 +12001462}
1463
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001464static void
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001465emit_intrinsic(compiler_context *ctx, nir_intrinsic_instr *instr)
1466{
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001467 unsigned offset = 0, reg;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001468
1469 switch (instr->intrinsic) {
1470 case nir_intrinsic_discard_if:
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001471 case nir_intrinsic_discard: {
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00001472 bool conditional = instr->intrinsic == nir_intrinsic_discard_if;
1473 struct midgard_instruction discard = v_branch(conditional, false);
1474 discard.branch.target_type = TARGET_DISCARD;
Alyssa Rosenzweigd6e4e362019-08-26 13:59:29 -07001475
Alyssa Rosenzweig074815c2020-04-29 16:29:01 -04001476 if (conditional) {
Alyssa Rosenzweigdb7b0eb2020-04-30 14:17:06 -04001477 discard.src[0] = mir_get_branch_cond(&instr->src[0],
1478 &discard.branch.invert_conditional);
Alyssa Rosenzweig074815c2020-04-29 16:29:01 -04001479 discard.src_types[0] = nir_type_uint32;
1480 }
Alyssa Rosenzweigd6e4e362019-08-26 13:59:29 -07001481
Alyssa Rosenzweig779e1402019-02-17 23:24:39 +00001482 emit_mir_instruction(ctx, discard);
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07001483 schedule_barrier(ctx);
1484
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001485 break;
1486 }
1487
1488 case nir_intrinsic_load_uniform:
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001489 case nir_intrinsic_load_ubo:
Alyssa Rosenzweig31489372019-11-05 08:59:49 -05001490 case nir_intrinsic_load_global:
Alyssa Rosenzweig3a310fb2020-02-05 15:17:44 -05001491 case nir_intrinsic_load_shared:
Alyssa Rosenzweigc17a4412019-12-27 15:32:50 -05001492 case nir_intrinsic_load_input:
1493 case nir_intrinsic_load_interpolated_input: {
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001494 bool is_uniform = instr->intrinsic == nir_intrinsic_load_uniform;
1495 bool is_ubo = instr->intrinsic == nir_intrinsic_load_ubo;
Alyssa Rosenzweig31489372019-11-05 08:59:49 -05001496 bool is_global = instr->intrinsic == nir_intrinsic_load_global;
Alyssa Rosenzweig3a310fb2020-02-05 15:17:44 -05001497 bool is_shared = instr->intrinsic == nir_intrinsic_load_shared;
Alyssa Rosenzweigc17a4412019-12-27 15:32:50 -05001498 bool is_flat = instr->intrinsic == nir_intrinsic_load_input;
1499 bool is_interp = instr->intrinsic == nir_intrinsic_load_interpolated_input;
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001500
Alyssa Rosenzweigbbc050b2019-06-27 15:33:07 -07001501 /* Get the base type of the intrinsic */
Alyssa Rosenzweig8d747492019-06-27 14:13:10 -07001502 /* TODO: Infer type? Does it matter? */
1503 nir_alu_type t =
Alyssa Rosenzweig3a310fb2020-02-05 15:17:44 -05001504 (is_ubo || is_global || is_shared) ? nir_type_uint :
Alyssa Rosenzweigc17a4412019-12-27 15:32:50 -05001505 (is_interp) ? nir_type_float :
Jason Ekstrand0aa08ae2020-09-30 21:20:53 -05001506 nir_intrinsic_dest_type(instr);
Alyssa Rosenzweigc17a4412019-12-27 15:32:50 -05001507
Alyssa Rosenzweigbbc050b2019-06-27 15:33:07 -07001508 t = nir_alu_type_get_base_type(t);
1509
Alyssa Rosenzweig31489372019-11-05 08:59:49 -05001510 if (!(is_ubo || is_global)) {
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001511 offset = nir_intrinsic_base(instr);
1512 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001513
Alyssa Rosenzweigc1715b52019-05-22 02:44:12 +00001514 unsigned nr_comp = nir_intrinsic_dest_components(instr);
Alyssa Rosenzweig6a466c02019-04-20 23:52:42 +00001515
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001516 nir_src *src_offset = nir_get_io_offset_src(instr);
1517
1518 bool direct = nir_src_is_const(*src_offset);
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001519 nir_src *indirect_offset = direct ? NULL : src_offset;
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001520
1521 if (direct)
1522 offset += nir_src_as_uint(*src_offset);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001523
Alyssa Rosenzweig43568f22019-06-06 08:16:04 -07001524 /* We may need to apply a fractional offset */
Alyssa Rosenzweigc17a4412019-12-27 15:32:50 -05001525 int component = (is_flat || is_interp) ?
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001526 nir_intrinsic_component(instr) : 0;
Alyssa Rosenzweig7c2647f2020-03-10 15:48:52 -04001527 reg = nir_dest_index(&instr->dest);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001528
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001529 if (is_uniform && !ctx->is_blend) {
Alyssa Rosenzweigc2ff3bb2020-03-10 16:00:56 -04001530 emit_ubo_read(ctx, &instr->instr, reg, (ctx->sysvals.sysval_count + offset) * 16, indirect_offset, 4, 0);
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001531 } else if (is_ubo) {
1532 nir_src index = instr->src[0];
1533
Alyssa Rosenzweig59d30fd2020-01-10 17:47:57 -05001534 /* TODO: Is indirect block number possible? */
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001535 assert(nir_src_is_const(index));
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001536
Alyssa Rosenzweig5e2c3d42019-06-20 15:51:31 -07001537 uint32_t uindex = nir_src_as_uint(index) + 1;
Alyssa Rosenzweig59d30fd2020-01-10 17:47:57 -05001538 emit_ubo_read(ctx, &instr->instr, reg, offset, indirect_offset, 0, uindex);
Alyssa Rosenzweig3a310fb2020-02-05 15:17:44 -05001539 } else if (is_global || is_shared) {
Alyssa Rosenzweig0bb25e42020-02-27 09:41:17 -05001540 emit_global(ctx, &instr->instr, true, reg, src_offset, is_shared);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001541 } else if (ctx->stage == MESA_SHADER_FRAGMENT && !ctx->is_blend) {
Alyssa Rosenzweig5f8dd412020-05-22 16:22:48 -04001542 emit_varying_read(ctx, reg, offset, nr_comp, component, indirect_offset, t | nir_dest_bit_size(instr->dest), is_flat);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001543 } else if (ctx->is_blend) {
Icecream9585954ec2020-06-25 22:21:50 +12001544 /* ctx->blend_input will be precoloured to r0/r2, where
Alyssa Rosenzweig277b6162020-06-12 16:45:24 -04001545 * the input is preloaded */
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001546
Icecream9585954ec2020-06-25 22:21:50 +12001547 unsigned *input = offset ? &ctx->blend_src1 : &ctx->blend_input;
1548
1549 if (*input == ~0)
1550 *input = reg;
Alyssa Rosenzweig277b6162020-06-12 16:45:24 -04001551 else
Icecream9585954ec2020-06-25 22:21:50 +12001552 emit_mir_instruction(ctx, v_mov(*input, reg));
Alyssa Rosenzweig6e688902019-12-19 13:24:17 -05001553 } else if (ctx->stage == MESA_SHADER_VERTEX) {
1554 emit_attr_read(ctx, reg, offset, nr_comp, t);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001555 } else {
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +01001556 DBG("Unknown load\n");
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001557 assert(0);
1558 }
1559
1560 break;
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001561 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001562
Alyssa Rosenzweigc17a4412019-12-27 15:32:50 -05001563 /* Artefact of load_interpolated_input. TODO: other barycentric modes */
1564 case nir_intrinsic_load_barycentric_pixel:
Tomeu Vizoso25042062020-01-03 09:42:11 +01001565 case nir_intrinsic_load_barycentric_centroid:
Alyssa Rosenzweigc17a4412019-12-27 15:32:50 -05001566 break;
1567
Alyssa Rosenzweig1686ef82019-07-01 17:23:58 -07001568 /* Reads 128-bit value raw off the tilebuffer during blending, tasty */
1569
Alyssa Rosenzweig36af05b2020-06-01 14:14:33 -04001570 case nir_intrinsic_load_raw_output_pan: {
Alyssa Rosenzweig7c2647f2020-03-10 15:48:52 -04001571 reg = nir_dest_index(&instr->dest);
Alyssa Rosenzweig1686ef82019-07-01 17:23:58 -07001572
Alyssa Rosenzweig843874c2019-11-06 21:50:32 -05001573 /* T720 and below use different blend opcodes with slightly
1574 * different semantics than T760 and up */
1575
Alyssa Rosenzweig2d1e18e2020-01-02 12:28:54 -05001576 midgard_instruction ld = m_ld_color_buffer_32u(reg, 0);
Alyssa Rosenzweig843874c2019-11-06 21:50:32 -05001577
Icecream95e7641922020-07-19 22:31:26 +12001578 ld.load_store.arg_2 = output_load_rt_addr(ctx, instr);
Icecream957781d2c2020-07-06 19:54:56 +12001579
Icecream95c20d1662020-07-16 14:16:11 +12001580 if (nir_src_is_const(instr->src[0])) {
1581 ld.load_store.arg_1 = nir_src_as_uint(instr->src[0]);
1582 } else {
1583 ld.load_store.varying_parameters = 2;
1584 ld.src[1] = nir_src_index(ctx, &instr->src[0]);
1585 ld.src_types[1] = nir_type_int32;
1586 }
1587
Alyssa Rosenzweig36af05b2020-06-01 14:14:33 -04001588 if (ctx->quirks & MIDGARD_OLD_BLEND) {
Italo Nicolabea6a652020-07-23 19:24:39 +00001589 ld.op = midgard_op_ld_color_buffer_32u_old;
Alyssa Rosenzweig5a175e42020-05-29 21:11:11 -04001590 ld.load_store.address = 16;
1591 ld.load_store.arg_2 = 0x1E;
Alyssa Rosenzweig843874c2019-11-06 21:50:32 -05001592 }
1593
Alyssa Rosenzweig1a4153b2019-08-30 17:29:17 -07001594 emit_mir_instruction(ctx, ld);
Alyssa Rosenzweig39104222019-05-06 02:12:41 +00001595 break;
Alyssa Rosenzweig36af05b2020-06-01 14:14:33 -04001596 }
1597
1598 case nir_intrinsic_load_output: {
1599 reg = nir_dest_index(&instr->dest);
Alyssa Rosenzweig36af05b2020-06-01 14:14:33 -04001600
Icecream952fbe7ca2020-07-09 23:44:41 +12001601 unsigned bits = nir_dest_bit_size(instr->dest);
1602
1603 midgard_instruction ld;
1604 if (bits == 16)
1605 ld = m_ld_color_buffer_as_fp16(reg, 0);
1606 else
1607 ld = m_ld_color_buffer_as_fp32(reg, 0);
Alyssa Rosenzweig36af05b2020-06-01 14:14:33 -04001608
Icecream95e7641922020-07-19 22:31:26 +12001609 ld.load_store.arg_2 = output_load_rt_addr(ctx, instr);
Icecream957781d2c2020-07-06 19:54:56 +12001610
Alyssa Rosenzweig36af05b2020-06-01 14:14:33 -04001611 for (unsigned c = 4; c < 16; ++c)
1612 ld.swizzle[0][c] = 0;
1613
1614 if (ctx->quirks & MIDGARD_OLD_BLEND) {
Icecream952fbe7ca2020-07-09 23:44:41 +12001615 if (bits == 16)
Italo Nicolabea6a652020-07-23 19:24:39 +00001616 ld.op = midgard_op_ld_color_buffer_as_fp16_old;
Icecream952fbe7ca2020-07-09 23:44:41 +12001617 else
Italo Nicolabea6a652020-07-23 19:24:39 +00001618 ld.op = midgard_op_ld_color_buffer_as_fp32_old;
Alyssa Rosenzweig36af05b2020-06-01 14:14:33 -04001619 ld.load_store.address = 1;
1620 ld.load_store.arg_2 = 0x1E;
1621 }
1622
1623 emit_mir_instruction(ctx, ld);
1624 break;
1625 }
Alyssa Rosenzweig39104222019-05-06 02:12:41 +00001626
1627 case nir_intrinsic_load_blend_const_color_rgba: {
1628 assert(ctx->is_blend);
Alyssa Rosenzweig7c2647f2020-03-10 15:48:52 -04001629 reg = nir_dest_index(&instr->dest);
Alyssa Rosenzweig39104222019-05-06 02:12:41 +00001630
Alyssa Rosenzweigc3a46e72019-10-30 16:29:28 -04001631 midgard_instruction ins = v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT), reg);
Alyssa Rosenzweig39104222019-05-06 02:12:41 +00001632 ins.has_constants = true;
Boris Brezillona5005c32020-10-08 10:58:53 +02001633 memcpy(ins.constants.f32, ctx->blend_constants, sizeof(ctx->blend_constants));
Alyssa Rosenzweig39104222019-05-06 02:12:41 +00001634 emit_mir_instruction(ctx, ins);
1635 break;
1636 }
1637
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001638 case nir_intrinsic_store_output:
Icecream95d37e9012020-06-06 17:25:08 +12001639 case nir_intrinsic_store_combined_output_pan:
Karol Herbst1aabb792019-03-29 21:40:45 +01001640 assert(nir_src_is_const(instr->src[1]) && "no indirect outputs");
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001641
Karol Herbst1aabb792019-03-29 21:40:45 +01001642 offset = nir_intrinsic_base(instr) + nir_src_as_uint(instr->src[1]);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001643
Alyssa Rosenzweig4ed23b12019-02-07 04:56:13 +00001644 reg = nir_src_index(ctx, &instr->src[0]);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001645
1646 if (ctx->stage == MESA_SHADER_FRAGMENT) {
Icecream95d37e9012020-06-06 17:25:08 +12001647 bool combined = instr->intrinsic ==
1648 nir_intrinsic_store_combined_output_pan;
1649
Boris Brezillonc68cd392020-01-31 09:22:50 +01001650 const nir_variable *var;
Alyssa Rosenzweigdfaa4c52020-11-04 08:32:16 -05001651 var = nir_find_variable_with_driver_location(ctx->nir, nir_var_shader_out,
Boris Brezillonc68cd392020-01-31 09:22:50 +01001652 nir_intrinsic_base(instr));
1653 assert(var);
Icecream9585954ec2020-06-25 22:21:50 +12001654
1655 /* Dual-source blend writeout is done by leaving the
1656 * value in r2 for the blend shader to use. */
1657 if (var->data.index) {
1658 if (instr->src[0].is_ssa) {
1659 emit_explicit_constant(ctx, reg, reg);
1660
1661 unsigned out = make_compiler_temp(ctx);
1662
1663 midgard_instruction ins = v_mov(reg, out);
1664 emit_mir_instruction(ctx, ins);
1665
1666 ctx->blend_src1 = out;
1667 } else {
1668 ctx->blend_src1 = reg;
1669 }
1670
1671 break;
1672 }
1673
1674 enum midgard_rt_id rt;
Boris Brezillonc68cd392020-01-31 09:22:50 +01001675 if (var->data.location == FRAG_RESULT_COLOR)
1676 rt = MIDGARD_COLOR_RT0;
1677 else if (var->data.location >= FRAG_RESULT_DATA0)
1678 rt = MIDGARD_COLOR_RT0 + var->data.location -
1679 FRAG_RESULT_DATA0;
Icecream95d37e9012020-06-06 17:25:08 +12001680 else if (combined)
1681 rt = MIDGARD_ZS_RT;
Boris Brezillonc68cd392020-01-31 09:22:50 +01001682 else
Eric Anholt4c24c822020-08-25 10:15:27 -07001683 unreachable("bad rt");
Boris Brezillonc68cd392020-01-31 09:22:50 +01001684
Icecream95d37e9012020-06-06 17:25:08 +12001685 unsigned reg_z = ~0, reg_s = ~0;
1686 if (combined) {
1687 unsigned writeout = nir_intrinsic_component(instr);
1688 if (writeout & PAN_WRITEOUT_Z)
1689 reg_z = nir_src_index(ctx, &instr->src[2]);
1690 if (writeout & PAN_WRITEOUT_S)
1691 reg_s = nir_src_index(ctx, &instr->src[3]);
1692 }
1693
1694 emit_fragment_store(ctx, reg, reg_z, reg_s, rt);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001695 } else if (ctx->stage == MESA_SHADER_VERTEX) {
Icecream95d37e9012020-06-06 17:25:08 +12001696 assert(instr->intrinsic == nir_intrinsic_store_output);
1697
Alyssa Rosenzweiga3ae3cb2019-06-17 12:35:57 -07001698 /* We should have been vectorized, though we don't
1699 * currently check that st_vary is emitted only once
1700 * per slot (this is relevant, since there's not a mask
1701 * parameter available on the store [set to 0 by the
1702 * blob]). We do respect the component by adjusting the
Alyssa Rosenzweig233c0fa2019-07-24 12:54:59 -07001703 * swizzle. If this is a constant source, we'll need to
1704 * emit that explicitly. */
1705
1706 emit_explicit_constant(ctx, reg, reg);
Alyssa Rosenzweiga3ae3cb2019-06-17 12:35:57 -07001707
Boris Brezillon6af63c92020-01-16 11:20:06 +01001708 unsigned dst_component = nir_intrinsic_component(instr);
Alyssa Rosenzweig27887212019-08-15 16:53:03 -07001709 unsigned nr_comp = nir_src_num_components(instr->src[0]);
Alyssa Rosenzweigde8d49a2019-06-06 09:15:26 -07001710
Alyssa Rosenzweig233c0fa2019-07-24 12:54:59 -07001711 midgard_instruction st = m_st_vary_32(reg, offset);
Alyssa Rosenzweigc9087722019-08-01 13:29:01 -07001712 st.load_store.arg_1 = 0x9E;
1713 st.load_store.arg_2 = 0x1E;
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001714
Jason Ekstrand0aa08ae2020-09-30 21:20:53 -05001715 switch (nir_alu_type_get_base_type(nir_intrinsic_src_type(instr))) {
Alyssa Rosenzweig66c26962019-12-27 14:25:00 -05001716 case nir_type_uint:
1717 case nir_type_bool:
Italo Nicolabea6a652020-07-23 19:24:39 +00001718 st.op = midgard_op_st_vary_32u;
Alyssa Rosenzweig66c26962019-12-27 14:25:00 -05001719 break;
1720 case nir_type_int:
Italo Nicolabea6a652020-07-23 19:24:39 +00001721 st.op = midgard_op_st_vary_32i;
Alyssa Rosenzweig66c26962019-12-27 14:25:00 -05001722 break;
1723 case nir_type_float:
Italo Nicolabea6a652020-07-23 19:24:39 +00001724 st.op = midgard_op_st_vary_32;
Alyssa Rosenzweig66c26962019-12-27 14:25:00 -05001725 break;
1726 default:
1727 unreachable("Attempted to store unknown type");
1728 break;
1729 }
1730
Boris Brezillon6af63c92020-01-16 11:20:06 +01001731 /* nir_intrinsic_component(store_intr) encodes the
1732 * destination component start. Source component offset
1733 * adjustment is taken care of in
1734 * install_registers_instr(), when offset_swizzle() is
1735 * called.
1736 */
1737 unsigned src_component = COMPONENT_X;
1738
1739 assert(nr_comp > 0);
1740 for (unsigned i = 0; i < ARRAY_SIZE(st.swizzle); ++i) {
1741 st.swizzle[0][i] = src_component;
1742 if (i >= dst_component && i < dst_component + nr_comp - 1)
1743 src_component++;
1744 }
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001745
Alyssa Rosenzweig4aced182019-06-06 08:21:27 -07001746 emit_mir_instruction(ctx, st);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001747 } else {
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +01001748 DBG("Unknown store\n");
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001749 assert(0);
1750 }
1751
1752 break;
1753
Alyssa Rosenzweig541b3292019-07-01 15:02:40 -07001754 /* Special case of store_output for lowered blend shaders */
1755 case nir_intrinsic_store_raw_output_pan:
1756 assert (ctx->stage == MESA_SHADER_FRAGMENT);
1757 reg = nir_src_index(ctx, &instr->src[0]);
Icecream95a6806342020-06-06 15:41:51 +12001758 emit_fragment_store(ctx, reg, ~0, ~0, ctx->blend_rt);
Alyssa Rosenzweig541b3292019-07-01 15:02:40 -07001759 break;
1760
Alyssa Rosenzweig31489372019-11-05 08:59:49 -05001761 case nir_intrinsic_store_global:
Alyssa Rosenzweig3a310fb2020-02-05 15:17:44 -05001762 case nir_intrinsic_store_shared:
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001763 reg = nir_src_index(ctx, &instr->src[0]);
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001764 emit_explicit_constant(ctx, reg, reg);
Alyssa Rosenzweig3a310fb2020-02-05 15:17:44 -05001765
Alyssa Rosenzweig0bb25e42020-02-27 09:41:17 -05001766 emit_global(ctx, &instr->instr, false, reg, &instr->src[1], instr->intrinsic == nir_intrinsic_store_shared);
Alyssa Rosenzweig419ddd62019-08-01 10:03:02 -07001767 break;
1768
Alyssa Rosenzweig31489372019-11-05 08:59:49 -05001769 case nir_intrinsic_load_ssbo_address:
Alyssa Rosenzweigb756a662020-03-10 16:19:33 -04001770 emit_sysval_read(ctx, &instr->instr, 1, 0);
Alyssa Rosenzweigfcbb3d42020-02-04 09:46:17 -05001771 break;
1772
Jason Ekstrand97501642020-09-22 03:24:45 -05001773 case nir_intrinsic_get_ssbo_size:
Alyssa Rosenzweigb756a662020-03-10 16:19:33 -04001774 emit_sysval_read(ctx, &instr->instr, 1, 8);
Alyssa Rosenzweig31489372019-11-05 08:59:49 -05001775 break;
Dylan Baker8e369612018-09-14 12:57:32 -07001776
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001777 case nir_intrinsic_load_viewport_scale:
1778 case nir_intrinsic_load_viewport_offset:
Alyssa Rosenzweig15954ab2019-08-06 14:07:10 -07001779 case nir_intrinsic_load_num_work_groups:
Alyssa Rosenzweig4e07e7b2019-11-21 08:42:28 -05001780 case nir_intrinsic_load_sampler_lod_parameters_pan:
Alyssa Rosenzweigb756a662020-03-10 16:19:33 -04001781 emit_sysval_read(ctx, &instr->instr, 3, 0);
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00001782 break;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001783
Alyssa Rosenzweig7229af72019-08-06 13:47:17 -07001784 case nir_intrinsic_load_work_group_id:
1785 case nir_intrinsic_load_local_invocation_id:
1786 emit_compute_builtin(ctx, instr);
1787 break;
1788
Alyssa Rosenzweig306800d2019-12-19 13:31:21 -05001789 case nir_intrinsic_load_vertex_id:
1790 case nir_intrinsic_load_instance_id:
1791 emit_vertex_builtin(ctx, instr);
1792 break;
1793
Alyssa Rosenzweig80ebf112020-08-27 19:55:53 -04001794 case nir_intrinsic_load_sample_mask_in:
1795 emit_special(ctx, instr, 96);
1796 break;
1797
Alyssa Rosenzweigda2eed32020-07-15 09:56:24 -04001798 case nir_intrinsic_load_sample_id:
Alyssa Rosenzweig80ebf112020-08-27 19:55:53 -04001799 emit_special(ctx, instr, 97);
Alyssa Rosenzweigda2eed32020-07-15 09:56:24 -04001800 break;
1801
Alyssa Rosenzweig3f590982020-02-03 20:23:41 -05001802 case nir_intrinsic_memory_barrier_buffer:
1803 case nir_intrinsic_memory_barrier_shared:
1804 break;
1805
1806 case nir_intrinsic_control_barrier:
1807 schedule_barrier(ctx);
1808 emit_control_barrier(ctx);
1809 schedule_barrier(ctx);
1810 break;
1811
Italo Nicolad7b6d2e2020-08-31 17:32:30 +00001812 ATOMIC_CASE(ctx, instr, add, add);
1813 ATOMIC_CASE(ctx, instr, and, and);
1814 ATOMIC_CASE(ctx, instr, comp_swap, cmpxchg);
1815 ATOMIC_CASE(ctx, instr, exchange, xchg);
1816 ATOMIC_CASE(ctx, instr, imax, imax);
1817 ATOMIC_CASE(ctx, instr, imin, imin);
1818 ATOMIC_CASE(ctx, instr, or, or);
1819 ATOMIC_CASE(ctx, instr, umax, umax);
1820 ATOMIC_CASE(ctx, instr, umin, umin);
1821 ATOMIC_CASE(ctx, instr, xor, xor);
1822
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001823 default:
Tomeu Vizosoae5e6402020-02-21 13:47:38 +01001824 fprintf(stderr, "Unhandled intrinsic %s\n", nir_intrinsic_infos[instr->intrinsic].name);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001825 assert(0);
1826 break;
1827 }
1828}
1829
Alyssa Rosenzweig1d0b3ef2020-08-05 18:11:15 -04001830/* Returns dimension with 0 special casing cubemaps */
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001831static unsigned
1832midgard_tex_format(enum glsl_sampler_dim dim)
1833{
1834 switch (dim) {
Alyssa Rosenzweig83c02a52019-06-17 14:26:08 -07001835 case GLSL_SAMPLER_DIM_1D:
1836 case GLSL_SAMPLER_DIM_BUF:
Alyssa Rosenzweig1d0b3ef2020-08-05 18:11:15 -04001837 return 1;
Alyssa Rosenzweig83c02a52019-06-17 14:26:08 -07001838
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001839 case GLSL_SAMPLER_DIM_2D:
Alyssa Rosenzweiga2748d42020-06-30 15:31:30 -04001840 case GLSL_SAMPLER_DIM_MS:
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001841 case GLSL_SAMPLER_DIM_EXTERNAL:
Alyssa Rosenzweig44a6c382019-08-14 08:44:40 -07001842 case GLSL_SAMPLER_DIM_RECT:
Alyssa Rosenzweig1d0b3ef2020-08-05 18:11:15 -04001843 return 2;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001844
1845 case GLSL_SAMPLER_DIM_3D:
Alyssa Rosenzweig1d0b3ef2020-08-05 18:11:15 -04001846 return 3;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001847
1848 case GLSL_SAMPLER_DIM_CUBE:
Alyssa Rosenzweig1d0b3ef2020-08-05 18:11:15 -04001849 return 0;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001850
1851 default:
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +01001852 DBG("Unknown sampler dim type\n");
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001853 assert(0);
1854 return 0;
1855 }
1856}
1857
Alyssa Rosenzweigc6c906e2020-05-21 18:02:38 -04001858/* Tries to attach an explicit LOD or bias as a constant. Returns whether this
Alyssa Rosenzweig213b6282019-06-18 09:02:20 -07001859 * was successful */
1860
1861static bool
1862pan_attach_constant_bias(
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001863 compiler_context *ctx,
1864 nir_src lod,
1865 midgard_texture_word *word)
Alyssa Rosenzweig213b6282019-06-18 09:02:20 -07001866{
1867 /* To attach as constant, it has to *be* constant */
1868
1869 if (!nir_src_is_const(lod))
1870 return false;
1871
1872 float f = nir_src_as_float(lod);
1873
1874 /* Break into fixed-point */
1875 signed lod_int = f;
1876 float lod_frac = f - lod_int;
1877
1878 /* Carry over negative fractions */
1879 if (lod_frac < 0.0) {
1880 lod_int--;
1881 lod_frac += 1.0;
1882 }
1883
1884 /* Encode */
1885 word->bias = float_to_ubyte(lod_frac);
1886 word->bias_int = lod_int;
1887
1888 return true;
1889}
1890
Alyssa Rosenzweigf6e19dd2020-08-28 08:35:19 -04001891static enum mali_texture_mode
1892mdg_texture_mode(nir_tex_instr *instr)
1893{
Alyssa Rosenzweig7dab5742020-08-28 09:48:38 -04001894 if (instr->op == nir_texop_tg4 && instr->is_shadow)
1895 return TEXTURE_GATHER_SHADOW;
1896 else if (instr->op == nir_texop_tg4)
1897 return TEXTURE_GATHER_X + instr->component;
1898 else if (instr->is_shadow)
Alyssa Rosenzweigf6e19dd2020-08-28 08:35:19 -04001899 return TEXTURE_SHADOW;
1900 else
1901 return TEXTURE_NORMAL;
1902}
1903
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001904static void
Boris Brezillon5c17f842019-06-17 21:47:46 +02001905emit_texop_native(compiler_context *ctx, nir_tex_instr *instr,
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07001906 unsigned midgard_texop)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001907{
1908 /* TODO */
1909 //assert (!instr->sampler);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001910
Italo Nicola83592de2020-07-15 18:48:42 +00001911 nir_dest *dest = &instr->dest;
1912
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001913 int texture_index = instr->texture_index;
1914 int sampler_index = texture_index;
1915
Alyssa Rosenzweig4fb02172020-04-27 19:11:19 -04001916 nir_alu_type dest_base = nir_alu_type_get_base_type(instr->dest_type);
Italo Nicola83592de2020-07-15 18:48:42 +00001917 nir_alu_type dest_type = dest_base | nir_dest_bit_size(*dest);
1918
1919 /* texture instructions support float outmods */
1920 unsigned outmod = midgard_outmod_none;
1921 if (dest_base == nir_type_float) {
1922 outmod = mir_determine_float_outmod(ctx, &dest, 0);
1923 }
Alyssa Rosenzweig4fb02172020-04-27 19:11:19 -04001924
Alyssa Rosenzweigb0e89412019-06-18 09:02:35 -07001925 midgard_instruction ins = {
1926 .type = TAG_TEXTURE_4,
Alyssa Rosenzweigf8b18a42019-07-01 18:51:48 -07001927 .mask = 0xF,
Italo Nicola83592de2020-07-15 18:48:42 +00001928 .dest = nir_dest_index(dest),
Alyssa Rosenzweigccbc9a42019-12-19 10:35:18 -05001929 .src = { ~0, ~0, ~0, ~0 },
Alyssa Rosenzweig4fb02172020-04-27 19:11:19 -04001930 .dest_type = dest_type,
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04001931 .swizzle = SWIZZLE_IDENTITY_4,
Italo Nicola83592de2020-07-15 18:48:42 +00001932 .outmod = outmod,
Italo Nicola92c808c2020-07-29 19:10:25 +00001933 .op = midgard_texop,
Alyssa Rosenzweigb0e89412019-06-18 09:02:35 -07001934 .texture = {
Alyssa Rosenzweigb0e89412019-06-18 09:02:35 -07001935 .format = midgard_tex_format(instr->sampler_dim),
1936 .texture_handle = texture_index,
1937 .sampler_handle = sampler_index,
Alyssa Rosenzweigf6e19dd2020-08-28 08:35:19 -04001938 .mode = mdg_texture_mode(instr)
Alyssa Rosenzweigb0e89412019-06-18 09:02:35 -07001939 }
1940 };
Alyssa Rosenzweig8429bee2019-06-14 16:03:39 -07001941
Alyssa Rosenzweig7dab5742020-08-28 09:48:38 -04001942 if (instr->is_shadow && !instr->is_new_style_shadow && instr->op != nir_texop_tg4)
Icecream95d1290e72020-05-12 10:16:31 +12001943 for (int i = 0; i < 4; ++i)
1944 ins.swizzle[0][i] = COMPONENT_X;
1945
Alyssa Rosenzweigd183f842019-12-16 17:02:36 -05001946 /* We may need a temporary for the coordinate */
1947
Alyssa Rosenzweig66013cb2019-12-16 17:14:04 -05001948 bool needs_temp_coord =
1949 (midgard_texop == TEXTURE_OP_TEXEL_FETCH) ||
Alyssa Rosenzweig6b7243f2019-12-20 17:25:05 -05001950 (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE) ||
Alyssa Rosenzweig66013cb2019-12-16 17:14:04 -05001951 (instr->is_shadow);
1952
Alyssa Rosenzweigd183f842019-12-16 17:02:36 -05001953 unsigned coords = needs_temp_coord ? make_compiler_temp_reg(ctx) : 0;
1954
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001955 for (unsigned i = 0; i < instr->num_srcs; ++i) {
Alyssa Rosenzweiga19ca342019-06-11 09:23:05 -07001956 int index = nir_src_index(ctx, &instr->src[i].src);
Alyssa Rosenzweigedc8e412019-08-15 16:41:53 -07001957 unsigned nr_components = nir_src_num_components(instr->src[i].src);
Alyssa Rosenzweig4fb02172020-04-27 19:11:19 -04001958 unsigned sz = nir_src_bit_size(instr->src[i].src);
1959 nir_alu_type T = nir_tex_instr_src_type(instr, i) | sz;
Alyssa Rosenzweiga19ca342019-06-11 09:23:05 -07001960
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00001961 switch (instr->src[i].src_type) {
1962 case nir_tex_src_coord: {
Alyssa Rosenzweigb6946d32019-07-25 08:44:53 -07001963 emit_explicit_constant(ctx, index, index);
1964
Alyssa Rosenzweig9e5a1412019-12-20 17:01:29 -05001965 unsigned coord_mask = mask_of(instr->coord_components);
1966
Alyssa Rosenzweigbc4c8532020-01-06 21:31:46 -05001967 bool flip_zw = (instr->sampler_dim == GLSL_SAMPLER_DIM_2D) && (coord_mask & (1 << COMPONENT_Z));
1968
1969 if (flip_zw)
1970 coord_mask ^= ((1 << COMPONENT_Z) | (1 << COMPONENT_W));
1971
Alyssa Rosenzweig6b7243f2019-12-20 17:25:05 -05001972 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE) {
1973 /* texelFetch is undefined on samplerCube */
1974 assert(midgard_texop != TEXTURE_OP_TEXEL_FETCH);
1975
1976 /* For cubemaps, we use a special ld/st op to
1977 * select the face and copy the xy into the
1978 * texture register */
1979
1980 midgard_instruction ld = m_ld_cubemap_coords(coords, 0);
1981 ld.src[1] = index;
Alyssa Rosenzweig4fb02172020-04-27 19:11:19 -04001982 ld.src_types[1] = T;
Alyssa Rosenzweig6b7243f2019-12-20 17:25:05 -05001983 ld.mask = 0x3; /* xy */
1984 ld.load_store.arg_1 = 0x20;
1985 ld.swizzle[1][3] = COMPONENT_X;
1986 emit_mir_instruction(ctx, ld);
1987
1988 /* xyzw -> xyxx */
1989 ins.swizzle[1][2] = instr->is_shadow ? COMPONENT_Z : COMPONENT_X;
1990 ins.swizzle[1][3] = COMPONENT_X;
1991 } else if (needs_temp_coord) {
Alyssa Rosenzweigd183f842019-12-16 17:02:36 -05001992 /* mov coord_temp, coords */
1993 midgard_instruction mov = v_mov(index, coords);
Alyssa Rosenzweig9e5a1412019-12-20 17:01:29 -05001994 mov.mask = coord_mask;
Alyssa Rosenzweigbc4c8532020-01-06 21:31:46 -05001995
1996 if (flip_zw)
1997 mov.swizzle[1][COMPONENT_W] = COMPONENT_Z;
1998
Alyssa Rosenzweigd183f842019-12-16 17:02:36 -05001999 emit_mir_instruction(ctx, mov);
2000 } else {
2001 coords = index;
2002 }
2003
Alyssa Rosenzweig6b7243f2019-12-20 17:25:05 -05002004 ins.src[1] = coords;
Alyssa Rosenzweig4fb02172020-04-27 19:11:19 -04002005 ins.src_types[1] = T;
Alyssa Rosenzweig6b7243f2019-12-20 17:25:05 -05002006
Alyssa Rosenzweigb6946d32019-07-25 08:44:53 -07002007 /* Texelfetch coordinates uses all four elements
2008 * (xyz/index) regardless of texture dimensionality,
2009 * which means it's necessary to zero the unused
2010 * components to keep everything happy */
2011
2012 if (midgard_texop == TEXTURE_OP_TEXEL_FETCH) {
Alyssa Rosenzweig9e5a1412019-12-20 17:01:29 -05002013 /* mov index.zw, #0, or generalized */
Alyssa Rosenzweigd183f842019-12-16 17:02:36 -05002014 midgard_instruction mov =
2015 v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT), coords);
Alyssa Rosenzweigb6946d32019-07-25 08:44:53 -07002016 mov.has_constants = true;
Alyssa Rosenzweig9e5a1412019-12-20 17:01:29 -05002017 mov.mask = coord_mask ^ 0xF;
Alyssa Rosenzweigb6946d32019-07-25 08:44:53 -07002018 emit_mir_instruction(ctx, mov);
2019 }
2020
Alyssa Rosenzweigb6946d32019-07-25 08:44:53 -07002021 if (instr->sampler_dim == GLSL_SAMPLER_DIM_2D) {
Alyssa Rosenzweig4cd3dc92020-01-06 21:36:20 -05002022 /* Array component in w but NIR wants it in z,
2023 * but if we have a temp coord we already fixed
2024 * that up */
2025
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04002026 if (nr_components == 3) {
2027 ins.swizzle[1][2] = COMPONENT_Z;
Alyssa Rosenzweig4cd3dc92020-01-06 21:36:20 -05002028 ins.swizzle[1][3] = needs_temp_coord ? COMPONENT_W : COMPONENT_Z;
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04002029 } else if (nr_components == 2) {
Alyssa Rosenzweig66013cb2019-12-16 17:14:04 -05002030 ins.swizzle[1][2] =
2031 instr->is_shadow ? COMPONENT_Z : COMPONENT_X;
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04002032 ins.swizzle[1][3] = COMPONENT_X;
2033 } else
Alyssa Rosenzweigedc8e412019-08-15 16:41:53 -07002034 unreachable("Invalid texture 2D components");
Alyssa Rosenzweig70b3e5d2019-03-28 04:27:13 +00002035 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002036
Alyssa Rosenzweig64b2fe92019-12-20 12:38:24 -05002037 if (midgard_texop == TEXTURE_OP_TEXEL_FETCH) {
2038 /* We zeroed */
2039 ins.swizzle[1][2] = COMPONENT_Z;
2040 ins.swizzle[1][3] = COMPONENT_W;
2041 }
2042
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002043 break;
2044 }
2045
Alyssa Rosenzweig4012e062019-06-11 09:43:08 -07002046 case nir_tex_src_bias:
2047 case nir_tex_src_lod: {
Alyssa Rosenzweigb0e89412019-06-18 09:02:35 -07002048 /* Try as a constant if we can */
2049
2050 bool is_txf = midgard_texop == TEXTURE_OP_TEXEL_FETCH;
2051 if (!is_txf && pan_attach_constant_bias(ctx, instr->src[i].src, &ins.texture))
2052 break;
2053
Alyssa Rosenzweigb0e89412019-06-18 09:02:35 -07002054 ins.texture.lod_register = true;
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04002055 ins.src[2] = index;
Alyssa Rosenzweig4fb02172020-04-27 19:11:19 -04002056 ins.src_types[2] = T;
Alyssa Rosenzweig72e57492019-12-20 12:34:20 -05002057
2058 for (unsigned c = 0; c < MIR_VEC_COMPONENTS; ++c)
2059 ins.swizzle[2][c] = COMPONENT_X;
2060
Alyssa Rosenzweigb6946d32019-07-25 08:44:53 -07002061 emit_explicit_constant(ctx, index, index);
Alyssa Rosenzweigb0e89412019-06-18 09:02:35 -07002062
Alyssa Rosenzweiga19ca342019-06-11 09:23:05 -07002063 break;
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002064 };
Alyssa Rosenzweiga19ca342019-06-11 09:23:05 -07002065
Alyssa Rosenzweigccbc9a42019-12-19 10:35:18 -05002066 case nir_tex_src_offset: {
2067 ins.texture.offset_register = true;
2068 ins.src[3] = index;
Alyssa Rosenzweig4fb02172020-04-27 19:11:19 -04002069 ins.src_types[3] = T;
Alyssa Rosenzweigccbc9a42019-12-19 10:35:18 -05002070
2071 for (unsigned c = 0; c < MIR_VEC_COMPONENTS; ++c)
2072 ins.swizzle[3][c] = (c > COMPONENT_Z) ? 0 : c;
2073
2074 emit_explicit_constant(ctx, index, index);
Alyssa Rosenzweig4ec1f952019-12-20 12:58:10 -05002075 break;
Alyssa Rosenzweigccbc9a42019-12-19 10:35:18 -05002076 };
2077
Alyssa Rosenzweig6d9f9512020-06-30 15:31:39 -04002078 case nir_tex_src_comparator:
2079 case nir_tex_src_ms_index: {
Alyssa Rosenzweig66013cb2019-12-16 17:14:04 -05002080 unsigned comp = COMPONENT_Z;
2081
2082 /* mov coord_temp.foo, coords */
2083 midgard_instruction mov = v_mov(index, coords);
2084 mov.mask = 1 << comp;
2085
2086 for (unsigned i = 0; i < MIR_VEC_COMPONENTS; ++i)
2087 mov.swizzle[1][i] = COMPONENT_X;
2088
2089 emit_mir_instruction(ctx, mov);
2090 break;
2091 }
2092
Tomeu Vizoso226c1ef2019-12-19 15:07:39 +01002093 default: {
Tomeu Vizosoae5e6402020-02-21 13:47:38 +01002094 fprintf(stderr, "Unknown texture source type: %d\n", instr->src[i].src_type);
Tomeu Vizoso226c1ef2019-12-19 15:07:39 +01002095 assert(0);
2096 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002097 }
2098 }
2099
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002100 emit_mir_instruction(ctx, ins);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002101}
2102
2103static void
Boris Brezillon5c17f842019-06-17 21:47:46 +02002104emit_tex(compiler_context *ctx, nir_tex_instr *instr)
2105{
2106 switch (instr->op) {
2107 case nir_texop_tex:
2108 case nir_texop_txb:
2109 emit_texop_native(ctx, instr, TEXTURE_OP_NORMAL);
2110 break;
2111 case nir_texop_txl:
Alyssa Rosenzweig7dab5742020-08-28 09:48:38 -04002112 case nir_texop_tg4:
Boris Brezillon5c17f842019-06-17 21:47:46 +02002113 emit_texop_native(ctx, instr, TEXTURE_OP_LOD);
2114 break;
Alyssa Rosenzweigf4bb7f02019-06-21 16:17:34 -07002115 case nir_texop_txf:
Alyssa Rosenzweig63a87222020-06-30 15:32:01 -04002116 case nir_texop_txf_ms:
Alyssa Rosenzweigf4bb7f02019-06-21 16:17:34 -07002117 emit_texop_native(ctx, instr, TEXTURE_OP_TEXEL_FETCH);
2118 break;
Boris Brezillonc3558862019-06-17 22:13:04 +02002119 case nir_texop_txs:
Alyssa Rosenzweigb756a662020-03-10 16:19:33 -04002120 emit_sysval_read(ctx, &instr->instr, 4, 0);
Boris Brezillonc3558862019-06-17 22:13:04 +02002121 break;
Tomeu Vizoso226c1ef2019-12-19 15:07:39 +01002122 default: {
Tomeu Vizosoae5e6402020-02-21 13:47:38 +01002123 fprintf(stderr, "Unhandled texture op: %d\n", instr->op);
Tomeu Vizoso226c1ef2019-12-19 15:07:39 +01002124 assert(0);
2125 }
Boris Brezillon5c17f842019-06-17 21:47:46 +02002126 }
2127}
2128
2129static void
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002130emit_jump(compiler_context *ctx, nir_jump_instr *instr)
2131{
2132 switch (instr->type) {
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002133 case nir_jump_break: {
2134 /* Emit a branch out of the loop */
2135 struct midgard_instruction br = v_branch(false, false);
2136 br.branch.target_type = TARGET_BREAK;
2137 br.branch.target_break = ctx->current_loop_depth;
2138 emit_mir_instruction(ctx, br);
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002139 break;
2140 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002141
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002142 default:
2143 DBG("Unknown jump type %d\n", instr->type);
2144 break;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002145 }
2146}
2147
2148static void
2149emit_instr(compiler_context *ctx, struct nir_instr *instr)
2150{
2151 switch (instr->type) {
2152 case nir_instr_type_load_const:
2153 emit_load_const(ctx, nir_instr_as_load_const(instr));
2154 break;
2155
2156 case nir_instr_type_intrinsic:
2157 emit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
2158 break;
2159
2160 case nir_instr_type_alu:
2161 emit_alu(ctx, nir_instr_as_alu(instr));
2162 break;
2163
2164 case nir_instr_type_tex:
2165 emit_tex(ctx, nir_instr_as_tex(instr));
2166 break;
2167
2168 case nir_instr_type_jump:
2169 emit_jump(ctx, nir_instr_as_jump(instr));
2170 break;
2171
2172 case nir_instr_type_ssa_undef:
2173 /* Spurious */
2174 break;
2175
2176 default:
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +01002177 DBG("Unhandled instruction type\n");
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002178 break;
2179 }
2180}
2181
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002182
2183/* ALU instructions can inline or embed constants, which decreases register
2184 * pressure and saves space. */
2185
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002186#define CONDITIONAL_ATTACH(idx) { \
2187 void *entry = _mesa_hash_table_u64_search(ctx->ssa_constants, alu->src[idx] + 1); \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002188\
2189 if (entry) { \
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002190 attach_constants(ctx, alu, entry, alu->src[idx] + 1); \
2191 alu->src[idx] = SSA_FIXED_REGISTER(REGISTER_CONSTANT); \
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002192 } \
2193}
2194
2195static void
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07002196inline_alu_constants(compiler_context *ctx, midgard_block *block)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002197{
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07002198 mir_foreach_instr_in_block(block, alu) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002199 /* Other instructions cannot inline constants */
2200 if (alu->type != TAG_ALU_4) continue;
Alyssa Rosenzweig5e06d902019-08-30 11:06:33 -07002201 if (alu->compact_branch) continue;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002202
2203 /* If there is already a constant here, we can do nothing */
2204 if (alu->has_constants) continue;
2205
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002206 CONDITIONAL_ATTACH(0);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002207
2208 if (!alu->has_constants) {
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002209 CONDITIONAL_ATTACH(1)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002210 } else if (!alu->inline_constant) {
2211 /* Corner case: _two_ vec4 constants, for instance with a
2212 * csel. For this case, we can only use a constant
2213 * register for one, we'll have to emit a move for the
Alyssa Rosenzweig3b10bcd2020-04-27 17:47:13 -04002214 * other. */
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002215
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002216 void *entry = _mesa_hash_table_u64_search(ctx->ssa_constants, alu->src[1] + 1);
Alyssa Rosenzweig3b10bcd2020-04-27 17:47:13 -04002217 unsigned scratch = make_compiler_temp(ctx);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002218
2219 if (entry) {
Alyssa Rosenzweigc3a46e72019-10-30 16:29:28 -04002220 midgard_instruction ins = v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT), scratch);
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002221 attach_constants(ctx, &ins, entry, alu->src[1] + 1);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002222
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002223 /* Set the source */
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002224 alu->src[1] = scratch;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002225
2226 /* Inject us -before- the last instruction which set r31 */
Boris Brezillon938c5b02019-08-28 09:17:21 +02002227 mir_insert_instruction_before(ctx, mir_prev_op(alu), ins);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002228 }
2229 }
2230 }
2231}
2232
Italo Nicola5f7e0182020-07-10 09:36:58 +00002233unsigned
2234max_bitsize_for_alu(midgard_instruction *ins)
2235{
2236 unsigned max_bitsize = 0;
2237 for (int i = 0; i < MIR_SRC_COUNT; i++) {
2238 if (ins->src[i] == ~0) continue;
2239 unsigned src_bitsize = nir_alu_type_get_type_size(ins->src_types[i]);
2240 max_bitsize = MAX2(src_bitsize, max_bitsize);
2241 }
2242 unsigned dst_bitsize = nir_alu_type_get_type_size(ins->dest_type);
2243 max_bitsize = MAX2(dst_bitsize, max_bitsize);
2244
2245 /* We don't have fp16 LUTs, so we'll want to emit code like:
2246 *
2247 * vlut.fsinr hr0, hr0
2248 *
2249 * where both input and output are 16-bit but the operation is carried
2250 * out in 32-bit
2251 */
2252
2253 switch (ins->op) {
2254 case midgard_alu_op_fsqrt:
2255 case midgard_alu_op_frcp:
2256 case midgard_alu_op_frsqrt:
2257 case midgard_alu_op_fsin:
2258 case midgard_alu_op_fcos:
2259 case midgard_alu_op_fexp2:
2260 case midgard_alu_op_flog2:
2261 max_bitsize = MAX2(max_bitsize, 32);
2262 break;
2263
2264 default:
2265 break;
2266 }
2267
Alyssa Rosenzweig3e2cb212020-08-27 14:35:23 -04002268 /* High implies computing at a higher bitsize, e.g umul_high of 32-bit
2269 * requires computing at 64-bit */
2270 if (midgard_is_integer_out_op(ins->op) && ins->outmod == midgard_outmod_int_high) {
2271 max_bitsize *= 2;
2272 assert(max_bitsize <= 64);
2273 }
2274
Italo Nicola5f7e0182020-07-10 09:36:58 +00002275 return max_bitsize;
2276}
2277
2278midgard_reg_mode
2279reg_mode_for_bitsize(unsigned bitsize)
2280{
2281 switch (bitsize) {
2282 /* use 16 pipe for 8 since we don't support vec16 yet */
2283 case 8:
2284 case 16:
2285 return midgard_reg_mode_16;
2286 case 32:
2287 return midgard_reg_mode_32;
2288 case 64:
2289 return midgard_reg_mode_64;
2290 default:
2291 unreachable("invalid bit size");
2292 }
2293}
2294
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002295/* Midgard supports two types of constants, embedded constants (128-bit) and
2296 * inline constants (16-bit). Sometimes, especially with scalar ops, embedded
2297 * constants can be demoted to inline constants, for space savings and
2298 * sometimes a performance boost */
2299
2300static void
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07002301embedded_to_inline_constant(compiler_context *ctx, midgard_block *block)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002302{
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07002303 mir_foreach_instr_in_block(block, ins) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002304 if (!ins->has_constants) continue;
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002305 if (ins->has_inline_constant) continue;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002306
Italo Nicola5f7e0182020-07-10 09:36:58 +00002307 unsigned max_bitsize = max_bitsize_for_alu(ins);
2308
Alyssa Rosenzweige92caad2019-07-01 20:02:57 -07002309 /* We can inline 32-bit (sometimes) or 16-bit (usually) */
Italo Nicola5f7e0182020-07-10 09:36:58 +00002310 bool is_16 = max_bitsize == 16;
2311 bool is_32 = max_bitsize == 32;
Alyssa Rosenzweige92caad2019-07-01 20:02:57 -07002312
2313 if (!(is_16 || is_32))
2314 continue;
2315
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002316 /* src1 cannot be an inline constant due to encoding
2317 * restrictions. So, if possible we try to flip the arguments
2318 * in that case */
2319
Italo Nicolaf4c89bf2020-07-09 12:02:57 +00002320 int op = ins->op;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002321
Alyssa Rosenzweigba9f3d12020-04-30 13:11:52 -04002322 if (ins->src[0] == SSA_FIXED_REGISTER(REGISTER_CONSTANT) &&
2323 alu_opcode_props[op].props & OP_COMMUTES) {
2324 mir_flip(ins);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002325 }
2326
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002327 if (ins->src[1] == SSA_FIXED_REGISTER(REGISTER_CONSTANT)) {
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04002328 /* Component is from the swizzle. Take a nonzero component */
2329 assert(ins->mask);
2330 unsigned first_comp = ffs(ins->mask) - 1;
2331 unsigned component = ins->swizzle[1][first_comp];
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002332
2333 /* Scale constant appropriately, if we can legally */
Icecream95d97aaad2020-06-05 20:17:27 +12002334 int16_t scaled_constant = 0;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002335
Boris Brezillon15c92d12020-01-20 15:00:57 +01002336 if (is_16) {
2337 scaled_constant = ins->constants.u16[component];
2338 } else if (midgard_is_integer_op(op)) {
2339 scaled_constant = ins->constants.u32[component];
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002340
2341 /* Constant overflow after resize */
Boris Brezillon15c92d12020-01-20 15:00:57 +01002342 if (scaled_constant != ins->constants.u32[component])
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002343 continue;
2344 } else {
Boris Brezillon15c92d12020-01-20 15:00:57 +01002345 float original = ins->constants.f32[component];
Alyssa Rosenzweig39786142019-04-28 15:46:47 +00002346 scaled_constant = _mesa_float_to_half(original);
2347
2348 /* Check for loss of precision. If this is
2349 * mediump, we don't care, but for a highp
2350 * shader, we need to pay attention. NIR
2351 * doesn't yet tell us which mode we're in!
2352 * Practically this prevents most constants
2353 * from being inlined, sadly. */
2354
2355 float fp32 = _mesa_half_to_float(scaled_constant);
2356
2357 if (fp32 != original)
2358 continue;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002359 }
2360
Alyssa Rosenzweig1cd65352020-05-21 12:38:27 -04002361 /* Should've been const folded */
2362 if (ins->src_abs[1] || ins->src_neg[1])
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002363 continue;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002364
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04002365 /* Make sure that the constant is not itself a vector
2366 * by checking if all accessed values are the same. */
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002367
Boris Brezillon15c92d12020-01-20 15:00:57 +01002368 const midgard_constants *cons = &ins->constants;
2369 uint32_t value = is_16 ? cons->u16[component] : cons->u32[component];
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002370
2371 bool is_vector = false;
Italo Nicolaf4c89bf2020-07-09 12:02:57 +00002372 unsigned mask = effective_writemask(ins->op, ins->mask);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002373
Alyssa Rosenzweig70072a22019-10-26 14:06:17 -04002374 for (unsigned c = 0; c < MIR_VEC_COMPONENTS; ++c) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002375 /* We only care if this component is actually used */
2376 if (!(mask & (1 << c)))
2377 continue;
2378
Boris Brezillon15c92d12020-01-20 15:00:57 +01002379 uint32_t test = is_16 ?
2380 cons->u16[ins->swizzle[1][c]] :
2381 cons->u32[ins->swizzle[1][c]];
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002382
2383 if (test != value) {
2384 is_vector = true;
2385 break;
2386 }
2387 }
2388
2389 if (is_vector)
2390 continue;
2391
2392 /* Get rid of the embedded constant */
2393 ins->has_constants = false;
Alyssa Rosenzweig75b6be22019-08-26 11:58:27 -07002394 ins->src[1] = ~0;
2395 ins->has_inline_constant = true;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002396 ins->inline_constant = scaled_constant;
2397 }
2398 }
2399}
2400
Alyssa Rosenzweigae20bee2019-06-06 11:19:13 -07002401/* Dead code elimination for branches at the end of a block - only one branch
2402 * per block is legal semantically */
2403
2404static void
Alyssa Rosenzweig1c2d4692020-04-30 13:13:24 -04002405midgard_cull_dead_branch(compiler_context *ctx, midgard_block *block)
Alyssa Rosenzweigae20bee2019-06-06 11:19:13 -07002406{
2407 bool branched = false;
2408
2409 mir_foreach_instr_in_block_safe(block, ins) {
2410 if (!midgard_is_branch_unit(ins->unit)) continue;
2411
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07002412 if (branched)
Alyssa Rosenzweigae20bee2019-06-06 11:19:13 -07002413 mir_remove_instruction(ins);
Alyssa Rosenzweigae20bee2019-06-06 11:19:13 -07002414
2415 branched = true;
2416 }
2417}
2418
Alyssa Rosenzweig622e3a82020-06-02 12:15:18 -04002419/* We want to force the invert on AND/OR to the second slot to legalize into
2420 * iandnot/iornot. The relevant patterns are for AND (and OR respectively)
2421 *
2422 * ~a & #b = ~a & ~(#~b)
2423 * ~a & b = b & ~a
2424 */
2425
2426static void
2427midgard_legalize_invert(compiler_context *ctx, midgard_block *block)
2428{
2429 mir_foreach_instr_in_block(block, ins) {
2430 if (ins->type != TAG_ALU_4) continue;
2431
Italo Nicolaf4c89bf2020-07-09 12:02:57 +00002432 if (ins->op != midgard_alu_op_iand &&
2433 ins->op != midgard_alu_op_ior) continue;
Alyssa Rosenzweig622e3a82020-06-02 12:15:18 -04002434
2435 if (ins->src_invert[1] || !ins->src_invert[0]) continue;
2436
2437 if (ins->has_inline_constant) {
2438 /* ~(#~a) = ~(~#a) = a, so valid, and forces both
2439 * inverts on */
2440 ins->inline_constant = ~ins->inline_constant;
2441 ins->src_invert[1] = true;
2442 } else {
2443 /* Flip to the right invert order. Note
2444 * has_inline_constant false by assumption on the
2445 * branch, so flipping makes sense. */
2446 mir_flip(ins);
2447 }
2448 }
2449}
2450
Alyssa Rosenzweig5bc62af2020-01-02 12:27:59 -05002451static unsigned
Alyssa Rosenzweig60396342019-11-23 16:08:02 -05002452emit_fragment_epilogue(compiler_context *ctx, unsigned rt)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002453{
Alyssa Rosenzweig02f503e2019-12-30 18:53:04 -05002454 /* Loop to ourselves */
Boris Brezillone1ba0cd2020-01-31 10:05:16 +01002455 midgard_instruction *br = ctx->writeout_branch[rt];
Alyssa Rosenzweig02f503e2019-12-30 18:53:04 -05002456 struct midgard_instruction ins = v_branch(false, false);
Icecream9592d3f1f2020-06-06 15:08:06 +12002457 ins.writeout = br->writeout;
Alyssa Rosenzweig02f503e2019-12-30 18:53:04 -05002458 ins.branch.target_block = ctx->block_count - 1;
Boris Brezillone1ba0cd2020-01-31 10:05:16 +01002459 ins.constants.u32[0] = br->constants.u32[0];
Icecream952a5504f2020-06-06 14:42:18 +12002460 memcpy(&ins.src_types, &br->src_types, sizeof(ins.src_types));
Alyssa Rosenzweig02f503e2019-12-30 18:53:04 -05002461 emit_mir_instruction(ctx, ins);
2462
Alyssa Rosenzweig3448b262019-12-03 10:37:01 -05002463 ctx->current_block->epilogue = true;
Alyssa Rosenzweig60396342019-11-23 16:08:02 -05002464 schedule_barrier(ctx);
Alyssa Rosenzweig5bc62af2020-01-02 12:27:59 -05002465 return ins.branch.target_block;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002466}
2467
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002468static midgard_block *
Icecream95ed4d2732020-07-08 13:15:09 +12002469emit_block_init(compiler_context *ctx)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002470{
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002471 midgard_block *this_block = ctx->after_block;
2472 ctx->after_block = NULL;
2473
2474 if (!this_block)
Alyssa Rosenzweigaeeeef12019-08-15 08:11:10 -07002475 this_block = create_empty_block(ctx);
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002476
Alyssa Rosenzweig5aaaf7b2020-03-11 08:36:31 -04002477 list_addtail(&this_block->base.link, &ctx->blocks);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002478
Alyssa Rosenzweigc5dd1d52020-03-11 08:22:08 -04002479 this_block->scheduled = false;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002480 ++ctx->block_count;
2481
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002482 /* Set up current block */
Alyssa Rosenzweig5aaaf7b2020-03-11 08:36:31 -04002483 list_inithead(&this_block->base.instructions);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002484 ctx->current_block = this_block;
2485
Icecream95ed4d2732020-07-08 13:15:09 +12002486 return this_block;
2487}
2488
2489static midgard_block *
2490emit_block(compiler_context *ctx, nir_block *block)
2491{
2492 midgard_block *this_block = emit_block_init(ctx);
2493
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002494 nir_foreach_instr(instr, block) {
2495 emit_instr(ctx, instr);
2496 ++ctx->instruction_count;
2497 }
2498
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002499 return this_block;
2500}
2501
2502static midgard_block *emit_cf_list(struct compiler_context *ctx, struct exec_list *list);
2503
2504static void
2505emit_if(struct compiler_context *ctx, nir_if *nif)
2506{
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002507 midgard_block *before_block = ctx->current_block;
2508
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002509 /* Speculatively emit the branch, but we can't fill it in until later */
Alyssa Rosenzweigdb7b0eb2020-04-30 14:17:06 -04002510 bool inv = false;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002511 EMIT(branch, true, true);
2512 midgard_instruction *then_branch = mir_last_in_block(ctx->current_block);
Alyssa Rosenzweigdb7b0eb2020-04-30 14:17:06 -04002513 then_branch->src[0] = mir_get_branch_cond(&nif->condition, &inv);
Alyssa Rosenzweig074815c2020-04-29 16:29:01 -04002514 then_branch->src_types[0] = nir_type_uint32;
Alyssa Rosenzweigdb7b0eb2020-04-30 14:17:06 -04002515 then_branch->branch.invert_conditional = !inv;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002516
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002517 /* Emit the two subblocks. */
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002518 midgard_block *then_block = emit_cf_list(ctx, &nif->then_list);
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002519 midgard_block *end_then_block = ctx->current_block;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002520
2521 /* Emit a jump from the end of the then block to the end of the else */
2522 EMIT(branch, false, false);
2523 midgard_instruction *then_exit = mir_last_in_block(ctx->current_block);
2524
2525 /* Emit second block, and check if it's empty */
2526
2527 int else_idx = ctx->block_count;
2528 int count_in = ctx->instruction_count;
2529 midgard_block *else_block = emit_cf_list(ctx, &nif->else_list);
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002530 midgard_block *end_else_block = ctx->current_block;
Alyssa Rosenzweig2c747092019-02-17 05:14:24 +00002531 int after_else_idx = ctx->block_count;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002532
2533 /* Now that we have the subblocks emitted, fix up the branches */
2534
2535 assert(then_block);
2536 assert(else_block);
2537
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002538 if (ctx->instruction_count == count_in) {
2539 /* The else block is empty, so don't emit an exit jump */
2540 mir_remove_instruction(then_exit);
Alyssa Rosenzweig2c747092019-02-17 05:14:24 +00002541 then_branch->branch.target_block = after_else_idx;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002542 } else {
2543 then_branch->branch.target_block = else_idx;
Alyssa Rosenzweig2c747092019-02-17 05:14:24 +00002544 then_exit->branch.target_block = after_else_idx;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002545 }
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002546
2547 /* Wire up the successors */
2548
Alyssa Rosenzweigaeeeef12019-08-15 08:11:10 -07002549 ctx->after_block = create_empty_block(ctx);
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002550
Alyssa Rosenzweig5aaaf7b2020-03-11 08:36:31 -04002551 pan_block_add_successor(&before_block->base, &then_block->base);
2552 pan_block_add_successor(&before_block->base, &else_block->base);
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002553
Alyssa Rosenzweig5aaaf7b2020-03-11 08:36:31 -04002554 pan_block_add_successor(&end_then_block->base, &ctx->after_block->base);
2555 pan_block_add_successor(&end_else_block->base, &ctx->after_block->base);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002556}
2557
2558static void
2559emit_loop(struct compiler_context *ctx, nir_loop *nloop)
2560{
2561 /* Remember where we are */
2562 midgard_block *start_block = ctx->current_block;
2563
Alyssa Rosenzweig521ac6e2019-04-21 16:22:44 +00002564 /* Allocate a loop number, growing the current inner loop depth */
2565 int loop_idx = ++ctx->current_loop_depth;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002566
2567 /* Get index from before the body so we can loop back later */
2568 int start_idx = ctx->block_count;
2569
2570 /* Emit the body itself */
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002571 midgard_block *loop_block = emit_cf_list(ctx, &nloop->body);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002572
2573 /* Branch back to loop back */
2574 struct midgard_instruction br_back = v_branch(false, false);
2575 br_back.branch.target_block = start_idx;
2576 emit_mir_instruction(ctx, br_back);
2577
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002578 /* Mark down that branch in the graph. */
Alyssa Rosenzweig5aaaf7b2020-03-11 08:36:31 -04002579 pan_block_add_successor(&start_block->base, &loop_block->base);
2580 pan_block_add_successor(&ctx->current_block->base, &loop_block->base);
Alyssa Rosenzweigc0fb2602019-04-21 03:29:47 +00002581
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002582 /* Find the index of the block about to follow us (note: we don't add
2583 * one; blocks are 0-indexed so we get a fencepost problem) */
2584 int break_block_idx = ctx->block_count;
2585
2586 /* Fix up the break statements we emitted to point to the right place,
2587 * now that we can allocate a block number for them */
Alyssa Rosenzweigaeeeef12019-08-15 08:11:10 -07002588 ctx->after_block = create_empty_block(ctx);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002589
Alyssa Rosenzweig5aaaf7b2020-03-11 08:36:31 -04002590 mir_foreach_block_from(ctx, start_block, _block) {
2591 mir_foreach_instr_in_block(((midgard_block *) _block), ins) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002592 if (ins->type != TAG_ALU_4) continue;
2593 if (!ins->compact_branch) continue;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002594
2595 /* We found a branch -- check the type to see if we need to do anything */
2596 if (ins->branch.target_type != TARGET_BREAK) continue;
2597
2598 /* It's a break! Check if it's our break */
2599 if (ins->branch.target_break != loop_idx) continue;
2600
2601 /* Okay, cool, we're breaking out of this loop.
2602 * Rewrite from a break to a goto */
2603
2604 ins->branch.target_type = TARGET_GOTO;
2605 ins->branch.target_block = break_block_idx;
Alyssa Rosenzweig9aeb7262019-08-02 13:48:27 -07002606
Alyssa Rosenzweig5aaaf7b2020-03-11 08:36:31 -04002607 pan_block_add_successor(_block, &ctx->after_block->base);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002608 }
2609 }
Alyssa Rosenzweig521ac6e2019-04-21 16:22:44 +00002610
2611 /* Now that we've finished emitting the loop, free up the depth again
2612 * so we play nice with recursion amid nested loops */
2613 --ctx->current_loop_depth;
Alyssa Rosenzweig7ad65162019-07-09 11:10:49 -07002614
2615 /* Dump loop stats */
2616 ++ctx->loop_count;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002617}
2618
2619static midgard_block *
2620emit_cf_list(struct compiler_context *ctx, struct exec_list *list)
2621{
2622 midgard_block *start_block = NULL;
2623
2624 foreach_list_typed(nir_cf_node, node, node, list) {
2625 switch (node->type) {
2626 case nir_cf_node_block: {
2627 midgard_block *block = emit_block(ctx, nir_cf_node_as_block(node));
2628
2629 if (!start_block)
2630 start_block = block;
2631
2632 break;
2633 }
2634
2635 case nir_cf_node_if:
2636 emit_if(ctx, nir_cf_node_as_if(node));
2637 break;
2638
2639 case nir_cf_node_loop:
2640 emit_loop(ctx, nir_cf_node_as_loop(node));
2641 break;
2642
2643 case nir_cf_node_function:
2644 assert(0);
2645 break;
2646 }
2647 }
2648
2649 return start_block;
2650}
2651
Alyssa Rosenzweig5e55c112019-02-17 03:35:03 +00002652/* Due to lookahead, we need to report the first tag executed in the command
2653 * stream and in branch targets. An initial block might be empty, so iterate
2654 * until we find one that 'works' */
2655
Italo Nicola8150c1d2020-07-29 20:14:55 +00002656unsigned
Alyssa Rosenzweig5e55c112019-02-17 03:35:03 +00002657midgard_get_first_tag_from_block(compiler_context *ctx, unsigned block_idx)
2658{
2659 midgard_block *initial_block = mir_get_block(ctx, block_idx);
2660
Alyssa Rosenzweig5aaaf7b2020-03-11 08:36:31 -04002661 mir_foreach_block_from(ctx, initial_block, _v) {
2662 midgard_block *v = (midgard_block *) _v;
Alyssa Rosenzweig45ac8ea2019-11-04 10:32:49 -05002663 if (v->quadword_count) {
2664 midgard_bundle *initial_bundle =
2665 util_dynarray_element(&v->bundles, midgard_bundle, 0);
Alyssa Rosenzweig5e55c112019-02-17 03:35:03 +00002666
Alyssa Rosenzweiga55a2e022020-02-04 09:28:06 -05002667 return initial_bundle->tag;
Alyssa Rosenzweig5e55c112019-02-17 03:35:03 +00002668 }
Alyssa Rosenzweig73c40d62019-07-31 15:49:30 -07002669 }
Alyssa Rosenzweig5e55c112019-02-17 03:35:03 +00002670
Alyssa Rosenzweiga55a2e022020-02-04 09:28:06 -05002671 /* Default to a tag 1 which will break from the shader, in case we jump
2672 * to the exit block (i.e. `return` in a compute shader) */
2673
2674 return 1;
Alyssa Rosenzweig5e55c112019-02-17 03:35:03 +00002675}
2676
Alyssa Rosenzweig5bc62af2020-01-02 12:27:59 -05002677/* For each fragment writeout instruction, generate a writeout loop to
2678 * associate with it */
2679
2680static void
2681mir_add_writeout_loops(compiler_context *ctx)
2682{
2683 for (unsigned rt = 0; rt < ARRAY_SIZE(ctx->writeout_branch); ++rt) {
2684 midgard_instruction *br = ctx->writeout_branch[rt];
2685 if (!br) continue;
2686
2687 unsigned popped = br->branch.target_block;
Alyssa Rosenzweig5aaaf7b2020-03-11 08:36:31 -04002688 pan_block_add_successor(&(mir_get_block(ctx, popped - 1)->base), &ctx->current_block->base);
Alyssa Rosenzweig5bc62af2020-01-02 12:27:59 -05002689 br->branch.target_block = emit_fragment_epilogue(ctx, rt);
Alyssa Rosenzweige27fd4b2020-04-27 20:34:36 -04002690 br->branch.target_type = TARGET_GOTO;
Alyssa Rosenzweig5bc62af2020-01-02 12:27:59 -05002691
2692 /* If we have more RTs, we'll need to restore back after our
2693 * loop terminates */
2694
2695 if ((rt + 1) < ARRAY_SIZE(ctx->writeout_branch) && ctx->writeout_branch[rt + 1]) {
2696 midgard_instruction uncond = v_branch(false, false);
2697 uncond.branch.target_block = popped;
Alyssa Rosenzweige27fd4b2020-04-27 20:34:36 -04002698 uncond.branch.target_type = TARGET_GOTO;
Alyssa Rosenzweig5bc62af2020-01-02 12:27:59 -05002699 emit_mir_instruction(ctx, uncond);
Alyssa Rosenzweig5aaaf7b2020-03-11 08:36:31 -04002700 pan_block_add_successor(&ctx->current_block->base, &(mir_get_block(ctx, popped)->base));
Alyssa Rosenzweig5bc62af2020-01-02 12:27:59 -05002701 schedule_barrier(ctx);
2702 } else {
2703 /* We're last, so we can terminate here */
2704 br->last_writeout = true;
2705 }
2706 }
2707}
2708
Boris Brezillon69c864b2020-10-17 12:08:17 +02002709panfrost_program *
2710midgard_compile_shader_nir(void *mem_ctx, nir_shader *nir,
Boris Brezillon0a74a042020-10-08 10:09:56 +02002711 const struct panfrost_compile_inputs *inputs)
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002712{
Boris Brezillon69c864b2020-10-17 12:08:17 +02002713 panfrost_program *program = rzalloc(mem_ctx, panfrost_program);
2714
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002715 struct util_dynarray *compiled = &program->compiled;
2716
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002717 midgard_debug = debug_get_option_midgard_debug();
Tomeu Vizosof0b1bbe2019-03-08 15:04:50 +01002718
Alyssa Rosenzweig4fa09322019-08-15 08:10:46 -07002719 /* TODO: Bound against what? */
2720 compiler_context *ctx = rzalloc(NULL, compiler_context);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002721
Alyssa Rosenzweig4fa09322019-08-15 08:10:46 -07002722 ctx->nir = nir;
Alyssa Rosenzweig4fa09322019-08-15 08:10:46 -07002723 ctx->stage = nir->info.stage;
Boris Brezillon0a74a042020-10-08 10:09:56 +02002724 ctx->is_blend = inputs->is_blend;
2725 ctx->blend_rt = MIDGARD_COLOR_RT0 + inputs->blend.rt;
Boris Brezillona5005c32020-10-08 10:58:53 +02002726 memcpy(ctx->blend_constants, inputs->blend.constants, sizeof(ctx->blend_constants));
Alyssa Rosenzweig277b6162020-06-12 16:45:24 -04002727 ctx->blend_input = ~0;
Icecream9585954ec2020-06-25 22:21:50 +12002728 ctx->blend_src1 = ~0;
Boris Brezillon0a74a042020-10-08 10:09:56 +02002729 ctx->quirks = midgard_get_quirks(inputs->gpu_id);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002730
Alyssa Rosenzweig3174bc92019-07-16 14:10:08 -07002731 /* Start off with a safe cutoff, allowing usage of all 16 work
2732 * registers. Later, we'll promote uniform reads to uniform registers
2733 * if we determine it is beneficial to do so */
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002734 ctx->uniform_cutoff = 8;
2735
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002736 /* Initialize at a global (not block) level hash tables */
2737
2738 ctx->ssa_constants = _mesa_hash_table_u64_create(NULL);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002739
Alyssa Rosenzweigde8d49a2019-06-06 09:15:26 -07002740 /* Lower gl_Position pre-optimisation, but after lowering vars to ssa
2741 * (so we don't accidentally duplicate the epilogue since mesa/st has
2742 * messed with our I/O quite a bit already) */
2743
2744 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
Alyssa Rosenzweig1e2cb3e2019-04-07 16:37:28 +00002745
Alyssa Rosenzweigbb483a92019-07-10 11:30:00 -07002746 if (ctx->stage == MESA_SHADER_VERTEX) {
Alyssa Rosenzweig1e2cb3e2019-04-07 16:37:28 +00002747 NIR_PASS_V(nir, nir_lower_viewport_transform);
Alyssa Rosenzweig20237162019-08-26 12:14:11 -07002748 NIR_PASS_V(nir, nir_lower_point_size, 1.0, 1024.0);
Alyssa Rosenzweigbb483a92019-07-10 11:30:00 -07002749 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002750
2751 NIR_PASS_V(nir, nir_lower_var_copies);
2752 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
2753 NIR_PASS_V(nir, nir_split_var_copies);
2754 NIR_PASS_V(nir, nir_lower_var_copies);
2755 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
2756 NIR_PASS_V(nir, nir_lower_var_copies);
2757 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00002758
Boris Brezillon0a74a042020-10-08 10:09:56 +02002759 unsigned pan_quirks = panfrost_get_quirks(inputs->gpu_id);
Icecream951e1eee92020-07-06 19:30:37 +12002760 NIR_PASS_V(nir, pan_lower_framebuffer,
Boris Brezillon0a74a042020-10-08 10:09:56 +02002761 inputs->rt_formats, inputs->is_blend, pan_quirks);
Icecream951e1eee92020-07-06 19:30:37 +12002762
Jason Ekstrandb019b222020-06-10 17:54:25 -05002763 NIR_PASS_V(nir, nir_lower_io, nir_var_shader_in | nir_var_shader_out,
2764 glsl_type_size, 0);
Alyssa Rosenzweig31489372019-11-05 08:59:49 -05002765 NIR_PASS_V(nir, nir_lower_ssbo);
Alyssa Rosenzweig42319c52020-11-04 08:37:55 -05002766 NIR_PASS_V(nir, pan_nir_lower_zs_store);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002767
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002768 /* Optimisation passes */
2769
Boris Brezillon0a74a042020-10-08 10:09:56 +02002770 optimise_nir(nir, ctx->quirks, inputs->is_blend);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002771
Alyssa Rosenzweig42319c52020-11-04 08:37:55 -05002772 NIR_PASS_V(nir, pan_nir_reorder_writeout);
Icecream950ff62632020-07-06 23:52:40 +12002773
Icecream95756441b2020-09-26 12:19:14 +12002774 if ((midgard_debug & MIDGARD_DBG_SHADERS) && !nir->info.internal) {
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002775 nir_print_shader(nir, stdout);
2776 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002777
Alyssa Rosenzweig7e8de5a2019-04-03 01:48:09 +00002778 /* Assign sysvals and counts, now that we're sure
2779 * (post-optimisation) */
2780
Alyssa Rosenzweig680fb052020-08-18 08:31:42 -04002781 panfrost_nir_assign_sysvals(&ctx->sysvals, ctx, nir);
Alyssa Rosenzweigc2ff3bb2020-03-10 16:00:56 -04002782 program->sysval_count = ctx->sysvals.sysval_count;
2783 memcpy(program->sysvals, ctx->sysvals.sysvals, sizeof(ctx->sysvals.sysvals[0]) * ctx->sysvals.sysval_count);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002784
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002785 nir_foreach_function(func, nir) {
2786 if (!func->impl)
2787 continue;
2788
2789 list_inithead(&ctx->blocks);
2790 ctx->block_count = 0;
2791 ctx->func = func;
Alyssa Rosenzweig22bb5a92020-04-29 18:08:26 -04002792 ctx->already_emitted = calloc(BITSET_WORDS(func->impl->ssa_alloc), sizeof(BITSET_WORD));
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002793
Boris Brezillon0a74a042020-10-08 10:09:56 +02002794 if (nir->info.outputs_read && !inputs->is_blend) {
Icecream95ed4d2732020-07-08 13:15:09 +12002795 emit_block_init(ctx);
2796
2797 struct midgard_instruction wait = v_branch(false, false);
2798 wait.branch.target_type = TARGET_TILEBUF_WAIT;
2799
2800 emit_mir_instruction(ctx, wait);
2801
2802 ++ctx->instruction_count;
2803 }
2804
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002805 emit_cf_list(ctx, &func->impl->body);
Alyssa Rosenzweig22bb5a92020-04-29 18:08:26 -04002806 free(ctx->already_emitted);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002807 break; /* TODO: Multi-function shaders */
2808 }
2809
Boris Brezillon69c864b2020-10-17 12:08:17 +02002810 util_dynarray_init(compiled, program);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002811
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07002812 /* Per-block lowering before opts */
2813
Alyssa Rosenzweig5aaaf7b2020-03-11 08:36:31 -04002814 mir_foreach_block(ctx, _block) {
2815 midgard_block *block = (midgard_block *) _block;
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07002816 inline_alu_constants(ctx, block);
Alyssa Rosenzweigcc2ba8e2019-08-30 10:53:13 -07002817 embedded_to_inline_constant(ctx, block);
2818 }
Alyssa Rosenzweig4d995e02019-04-22 04:58:53 +00002819 /* MIR-level optimizations */
Alyssa Rosenzweig84f09ff2019-04-21 16:11:11 +00002820
Alyssa Rosenzweig4d995e02019-04-22 04:58:53 +00002821 bool progress = false;
2822
2823 do {
2824 progress = false;
Alyssa Rosenzweigfc06b8b2020-05-06 17:34:09 -04002825 progress |= midgard_opt_dead_code_eliminate(ctx);
Alyssa Rosenzweig4d995e02019-04-22 04:58:53 +00002826
Alyssa Rosenzweig5aaaf7b2020-03-11 08:36:31 -04002827 mir_foreach_block(ctx, _block) {
2828 midgard_block *block = (midgard_block *) _block;
Alyssa Rosenzweig4d995e02019-04-22 04:58:53 +00002829 progress |= midgard_opt_copy_prop(ctx, block);
Alyssa Rosenzweig9ce75822019-07-24 15:37:24 -07002830 progress |= midgard_opt_combine_projection(ctx, block);
2831 progress |= midgard_opt_varying_projection(ctx, block);
Alyssa Rosenzweig4d995e02019-04-22 04:58:53 +00002832 }
2833 } while (progress);
Alyssa Rosenzweig84f09ff2019-04-21 16:11:11 +00002834
Alyssa Rosenzweig5aaaf7b2020-03-11 08:36:31 -04002835 mir_foreach_block(ctx, _block) {
2836 midgard_block *block = (midgard_block *) _block;
Alyssa Rosenzweig8f887322019-07-29 15:11:12 -07002837 midgard_lower_derivatives(ctx, block);
Alyssa Rosenzweig622e3a82020-06-02 12:15:18 -04002838 midgard_legalize_invert(ctx, block);
Alyssa Rosenzweig1c2d4692020-04-30 13:13:24 -04002839 midgard_cull_dead_branch(ctx, block);
Alyssa Rosenzweigae20bee2019-06-06 11:19:13 -07002840 }
2841
Alyssa Rosenzweig5bc62af2020-01-02 12:27:59 -05002842 if (ctx->stage == MESA_SHADER_FRAGMENT)
2843 mir_add_writeout_loops(ctx);
2844
Alyssa Rosenzweig9a7f0e22020-05-12 13:26:32 -04002845 /* Analyze now that the code is known but before scheduling creates
2846 * pipeline registers which are harder to track */
2847 mir_analyze_helper_terminate(ctx);
2848 mir_analyze_helper_requirements(ctx);
2849
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002850 /* Schedule! */
Robert Foss62adb652020-01-15 01:14:16 +01002851 midgard_schedule_program(ctx);
Alyssa Rosenzweig9dc3b182019-12-06 09:32:38 -05002852 mir_ra(ctx);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002853
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002854 /* Emit flat binary from the instruction arrays. Iterate each block in
2855 * sequence. Save instruction boundaries such that lookahead tags can
2856 * be assigned easily */
2857
2858 /* Cache _all_ bundles in source order for lookahead across failed branches */
2859
2860 int bundle_count = 0;
Alyssa Rosenzweig5aaaf7b2020-03-11 08:36:31 -04002861 mir_foreach_block(ctx, _block) {
2862 midgard_block *block = (midgard_block *) _block;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002863 bundle_count += block->bundles.size / sizeof(midgard_bundle);
2864 }
2865 midgard_bundle **source_order_bundles = malloc(sizeof(midgard_bundle *) * bundle_count);
2866 int bundle_idx = 0;
Alyssa Rosenzweig5aaaf7b2020-03-11 08:36:31 -04002867 mir_foreach_block(ctx, _block) {
2868 midgard_block *block = (midgard_block *) _block;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002869 util_dynarray_foreach(&block->bundles, midgard_bundle, bundle) {
2870 source_order_bundles[bundle_idx++] = bundle;
2871 }
2872 }
2873
2874 int current_bundle = 0;
2875
Alyssa Rosenzweig2a79afc2019-05-23 01:56:03 +00002876 /* Midgard prefetches instruction types, so during emission we
2877 * need to lookahead. Unless this is the last instruction, in
Alyssa Rosenzweig5bc62af2020-01-02 12:27:59 -05002878 * which we return 1. */
Alyssa Rosenzweig2a79afc2019-05-23 01:56:03 +00002879
Alyssa Rosenzweig5aaaf7b2020-03-11 08:36:31 -04002880 mir_foreach_block(ctx, _block) {
2881 midgard_block *block = (midgard_block *) _block;
Alyssa Rosenzweigd3ad8d62019-06-06 11:19:44 -07002882 mir_foreach_bundle_in_block(block, bundle) {
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002883 int lookahead = 1;
2884
Alyssa Rosenzweig5bc62af2020-01-02 12:27:59 -05002885 if (!bundle->last_writeout && (current_bundle + 1 < bundle_count))
2886 lookahead = source_order_bundles[current_bundle + 1]->tag;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002887
Alyssa Rosenzweig30a393f2020-05-21 19:14:23 -04002888 emit_binary_bundle(ctx, block, bundle, compiled, lookahead);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002889 ++current_bundle;
2890 }
2891
2892 /* TODO: Free deeper */
2893 //util_dynarray_fini(&block->instructions);
2894 }
2895
2896 free(source_order_bundles);
2897
Alyssa Rosenzweig5e55c112019-02-17 03:35:03 +00002898 /* Report the very first tag executed */
2899 program->first_tag = midgard_get_first_tag_from_block(ctx, 0);
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002900
2901 /* Deal with off-by-one related to the fencepost problem */
2902 program->work_register_count = ctx->work_registers + 1;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002903 program->uniform_cutoff = ctx->uniform_cutoff;
2904
Alyssa Rosenzweigf0d00612019-07-19 16:23:52 -07002905 program->tls_size = ctx->tls_size;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002906
Boris Brezillon0a74a042020-10-08 10:09:56 +02002907 if ((midgard_debug & MIDGARD_DBG_SHADERS) && !nir->info.internal) {
2908 disassemble_midgard(stdout,
2909 program->compiled.data,
2910 program->compiled.size,
2911 inputs->gpu_id, ctx->stage);
2912 }
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002913
Boris Brezillon0a74a042020-10-08 10:09:56 +02002914 if ((midgard_debug & MIDGARD_DBG_SHADERDB || inputs->shaderdb) &&
2915 !nir->info.internal) {
Alyssa Rosenzweig19bceb52019-08-30 13:57:20 -07002916 unsigned nr_bundles = 0, nr_ins = 0;
Alyssa Rosenzweig138e40d2019-07-08 16:42:29 -07002917
2918 /* Count instructions and bundles */
2919
Alyssa Rosenzweig5aaaf7b2020-03-11 08:36:31 -04002920 mir_foreach_block(ctx, _block) {
2921 midgard_block *block = (midgard_block *) _block;
Alyssa Rosenzweig138e40d2019-07-08 16:42:29 -07002922 nr_bundles += util_dynarray_num_elements(
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002923 &block->bundles, midgard_bundle);
Alyssa Rosenzweig2d739f62019-07-09 11:16:57 -07002924
Alyssa Rosenzweig67909c82019-08-30 13:08:16 -07002925 mir_foreach_bundle_in_block(block, bun)
2926 nr_ins += bun->instruction_count;
Alyssa Rosenzweig138e40d2019-07-08 16:42:29 -07002927 }
2928
2929 /* Calculate thread count. There are certain cutoffs by
2930 * register count for thread count */
2931
2932 unsigned nr_registers = program->work_register_count;
2933
2934 unsigned nr_threads =
2935 (nr_registers <= 4) ? 4 :
2936 (nr_registers <= 8) ? 2 :
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002937 1;
Alyssa Rosenzweig138e40d2019-07-08 16:42:29 -07002938
2939 /* Dump stats */
2940
2941 fprintf(stderr, "shader%d - %s shader: "
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002942 "%u inst, %u bundles, %u quadwords, "
Alyssa Rosenzweige8dca7e2019-07-22 06:32:48 -07002943 "%u registers, %u threads, %u loops, "
Alyssa Rosenzweig1a4153b2019-08-30 17:29:17 -07002944 "%u:%u spills:fills\n",
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002945 SHADER_DB_COUNT++,
Alyssa Rosenzweig014d2e42020-05-25 13:19:43 -04002946 ctx->is_blend ? "PAN_SHADER_BLEND" :
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002947 gl_shader_stage_name(ctx->stage),
Alyssa Rosenzweig19bceb52019-08-30 13:57:20 -07002948 nr_ins, nr_bundles, ctx->quadword_count,
Alyssa Rosenzweige4bd6fb2019-07-10 10:00:50 -07002949 nr_registers, nr_threads,
Alyssa Rosenzweige8dca7e2019-07-22 06:32:48 -07002950 ctx->loop_count,
2951 ctx->spills, ctx->fills);
Alyssa Rosenzweig138e40d2019-07-08 16:42:29 -07002952 }
2953
Alyssa Rosenzweig4fa09322019-08-15 08:10:46 -07002954 ralloc_free(ctx);
Alyssa Rosenzweig138e40d2019-07-08 16:42:29 -07002955
Boris Brezillon69c864b2020-10-17 12:08:17 +02002956 return program;
Alyssa Rosenzweige67e0722019-01-30 01:11:31 +00002957}