blob: 96b4e3874a810fbf076241fdfd83d7dc946e61c4 [file] [log] [blame]
Alyssa Rosenzweigeceaea42020-03-02 19:47:11 -05001/*
2 * Copyright (C) 2020 Collabora Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors (Collabora):
24 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
25 */
26
27#include "main/mtypes.h"
28#include "compiler/glsl/glsl_to_nir.h"
29#include "compiler/nir_types.h"
Alyssa Rosenzweigeceaea42020-03-02 19:47:11 -050030#include "compiler/nir/nir_builder.h"
Tomeu Vizoso07b31f32020-04-30 09:29:10 +020031#include "util/u_debug.h"
Alyssa Rosenzweigeceaea42020-03-02 19:47:11 -050032
33#include "disassemble.h"
34#include "bifrost_compile.h"
Alyssa Rosenzweig3a1baaf2020-03-10 08:20:59 -040035#include "bifrost_nir.h"
Alyssa Rosenzweigeceaea42020-03-02 19:47:11 -050036#include "compiler.h"
Alyssa Rosenzweig0b26cb12020-03-03 14:27:05 -050037#include "bi_quirks.h"
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -050038#include "bi_print.h"
39
Tomeu Vizoso07b31f32020-04-30 09:29:10 +020040static const struct debug_named_value debug_options[] = {
41 {"msgs", BIFROST_DBG_MSGS, "Print debug messages"},
42 {"shaders", BIFROST_DBG_SHADERS, "Dump shaders in NIR and MIR"},
43 DEBUG_NAMED_VALUE_END
44};
45
46DEBUG_GET_ONCE_FLAGS_OPTION(bifrost_debug, "BIFROST_MESA_DEBUG", debug_options, 0)
47
48int bifrost_debug = 0;
49
50#define DBG(fmt, ...) \
51 do { if (bifrost_debug & BIFROST_DBG_MSGS) \
52 fprintf(stderr, "%s:%d: "fmt, \
53 __FUNCTION__, __LINE__, ##__VA_ARGS__); } while (0)
54
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -050055static bi_block *emit_cf_list(bi_context *ctx, struct exec_list *list);
Alyssa Rosenzweig65c8dcc2020-03-05 17:10:46 -050056static bi_instruction *bi_emit_branch(bi_context *ctx);
Alyssa Rosenzweig65c8dcc2020-03-05 17:10:46 -050057
58static void
59emit_jump(bi_context *ctx, nir_jump_instr *instr)
60{
61 bi_instruction *branch = bi_emit_branch(ctx);
62
63 switch (instr->type) {
64 case nir_jump_break:
Alyssa Rosenzweig6627b202020-05-01 18:13:54 -040065 branch->branch_target = ctx->break_block;
Alyssa Rosenzweig65c8dcc2020-03-05 17:10:46 -050066 break;
67 case nir_jump_continue:
Alyssa Rosenzweig6627b202020-05-01 18:13:54 -040068 branch->branch_target = ctx->continue_block;
Alyssa Rosenzweig65c8dcc2020-03-05 17:10:46 -050069 break;
70 default:
71 unreachable("Unhandled jump type");
72 }
73
Alyssa Rosenzweig6627b202020-05-01 18:13:54 -040074 pan_block_add_successor(&ctx->current_block->base, &branch->branch_target->base);
Alyssa Rosenzweig65c8dcc2020-03-05 17:10:46 -050075}
76
Alyssa Rosenzweig69c66ff2020-03-09 19:52:56 -040077static bi_instruction
78bi_load(enum bi_class T, nir_intrinsic_instr *instr)
Alyssa Rosenzweig07671822020-03-05 17:50:18 -050079{
Alyssa Rosenzweig69c66ff2020-03-09 19:52:56 -040080 bi_instruction load = {
81 .type = T,
Alyssa Rosenzweigb2c6cf22020-04-24 17:20:28 -040082 .vector_channels = instr->num_components,
Alyssa Rosenzweig69c66ff2020-03-09 19:52:56 -040083 .src = { BIR_INDEX_CONSTANT },
Alyssa Rosenzweigdf693042020-04-14 20:09:00 -040084 .src_types = { nir_type_uint32 },
Alyssa Rosenzweig69c66ff2020-03-09 19:52:56 -040085 .constant = { .u64 = nir_intrinsic_base(instr) },
Alyssa Rosenzweig07671822020-03-05 17:50:18 -050086 };
87
Alyssa Rosenzweig69c66ff2020-03-09 19:52:56 -040088 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
89
90 if (info->has_dest)
Alyssa Rosenzweigfbbe3d42020-04-27 16:04:05 -040091 load.dest = pan_dest_index(&instr->dest);
Alyssa Rosenzweig69c66ff2020-03-09 19:52:56 -040092
Jason Ekstrand0aa08ae2020-09-30 21:20:53 -050093 if (info->has_dest && nir_intrinsic_has_dest_type(instr))
94 load.dest_type = nir_intrinsic_dest_type(instr);
Alyssa Rosenzweig69c66ff2020-03-09 19:52:56 -040095
Alyssa Rosenzweig07671822020-03-05 17:50:18 -050096 nir_src *offset = nir_get_io_offset_src(instr);
97
98 if (nir_src_is_const(*offset))
Alyssa Rosenzweig69c66ff2020-03-09 19:52:56 -040099 load.constant.u64 += nir_src_as_uint(*offset);
Alyssa Rosenzweig07671822020-03-05 17:50:18 -0500100 else
Alyssa Rosenzweigfbbe3d42020-04-27 16:04:05 -0400101 load.src[0] = pan_src_index(offset);
Alyssa Rosenzweig07671822020-03-05 17:50:18 -0500102
Alyssa Rosenzweig69c66ff2020-03-09 19:52:56 -0400103 return load;
104}
105
106static void
107bi_emit_ld_vary(bi_context *ctx, nir_intrinsic_instr *instr)
108{
109 bi_instruction ins = bi_load(BI_LOAD_VAR, instr);
110 ins.load_vary.interp_mode = BIFROST_INTERP_DEFAULT; /* TODO */
111 ins.load_vary.reuse = false; /* TODO */
112 ins.load_vary.flat = instr->intrinsic != nir_intrinsic_load_interpolated_input;
Alyssa Rosenzweig37f14c92020-03-18 11:55:10 -0400113 ins.dest_type = nir_type_float | nir_dest_bit_size(instr->dest);
Alyssa Rosenzweig8dd3a812020-07-31 18:48:27 -0400114 ins.format = ins.dest_type;
Alyssa Rosenzweig37f14c92020-03-18 11:55:10 -0400115
116 if (nir_src_is_const(*nir_get_io_offset_src(instr))) {
117 /* Zero it out for direct */
118 ins.src[1] = BIR_INDEX_ZERO;
119 } else {
120 /* R61 contains sample mask stuff, TODO RA XXX */
121 ins.src[1] = BIR_INDEX_REGISTER | 61;
122 }
123
Alyssa Rosenzweig07671822020-03-05 17:50:18 -0500124 bi_emit(ctx, ins);
125}
126
127static void
Alyssa Rosenzweigdabb6c62020-03-06 09:26:44 -0500128bi_emit_frag_out(bi_context *ctx, nir_intrinsic_instr *instr)
129{
130 if (!ctx->emitted_atest) {
131 bi_instruction ins = {
Alyssa Rosenzweigb18d0ef2020-03-18 23:02:12 -0400132 .type = BI_ATEST,
133 .src = {
134 BIR_INDEX_REGISTER | 60 /* TODO: RA */,
Alyssa Rosenzweigfbbe3d42020-04-27 16:04:05 -0400135 pan_src_index(&instr->src[0])
Alyssa Rosenzweigb18d0ef2020-03-18 23:02:12 -0400136 },
137 .src_types = {
138 nir_type_uint32,
Jason Ekstrand0aa08ae2020-09-30 21:20:53 -0500139 nir_intrinsic_src_type(instr)
Alyssa Rosenzweigb18d0ef2020-03-18 23:02:12 -0400140 },
141 .swizzle = {
142 { 0 },
143 { 3, 0 } /* swizzle out the alpha */
144 },
145 .dest = BIR_INDEX_REGISTER | 60 /* TODO: RA */,
146 .dest_type = nir_type_uint32,
Alyssa Rosenzweigdabb6c62020-03-06 09:26:44 -0500147 };
148
149 bi_emit(ctx, ins);
Alyssa Rosenzweigdabb6c62020-03-06 09:26:44 -0500150 ctx->emitted_atest = true;
151 }
152
153 bi_instruction blend = {
154 .type = BI_BLEND,
155 .blend_location = nir_intrinsic_base(instr),
156 .src = {
Alyssa Rosenzweigfbbe3d42020-04-27 16:04:05 -0400157 pan_src_index(&instr->src[0]),
Alyssa Rosenzweiga4fb8872020-03-18 23:12:23 -0400158 BIR_INDEX_REGISTER | 60 /* Can this be arbitrary? */,
Alyssa Rosenzweig3d63a472020-08-03 12:48:25 -0400159 /* Blend descriptor */
160 BIR_INDEX_PASS | BIFROST_SRC_CONST_LO,
161 BIR_INDEX_PASS | BIFROST_SRC_CONST_HI,
Alyssa Rosenzweig795646d2020-03-09 14:09:04 -0400162 },
Alyssa Rosenzweig116c5412020-03-11 21:45:32 -0400163 .src_types = {
Jason Ekstrand0aa08ae2020-09-30 21:20:53 -0500164 nir_intrinsic_src_type(instr),
Alyssa Rosenzweig3439c242020-04-09 23:04:41 -0400165 nir_type_uint32
Alyssa Rosenzweig116c5412020-03-11 21:45:32 -0400166 },
Alyssa Rosenzweig795646d2020-03-09 14:09:04 -0400167 .swizzle = {
Alyssa Rosenzweig3439c242020-04-09 23:04:41 -0400168 { 0, 1, 2, 3 },
169 { 0 }
Alyssa Rosenzweiga4fb8872020-03-18 23:12:23 -0400170 },
171 .dest = BIR_INDEX_REGISTER | 48 /* Looks like magic */,
172 .dest_type = nir_type_uint32,
Alyssa Rosenzweigb2c6cf22020-04-24 17:20:28 -0400173 .vector_channels = 4
Alyssa Rosenzweigdabb6c62020-03-06 09:26:44 -0500174 };
175
Alyssa Rosenzweig5a3088e2020-08-05 18:10:41 -0400176 assert(blend.blend_location < 8);
Alyssa Rosenzweig1a8f1a32020-04-23 19:26:01 -0400177 assert(ctx->blend_types);
Tomeu Vizoso3c98c452020-04-24 08:40:51 +0200178 assert(blend.src_types[0]);
Alyssa Rosenzweig1a8f1a32020-04-23 19:26:01 -0400179 ctx->blend_types[blend.blend_location] = blend.src_types[0];
180
Alyssa Rosenzweigdabb6c62020-03-06 09:26:44 -0500181 bi_emit(ctx, blend);
Alyssa Rosenzweigdabb6c62020-03-06 09:26:44 -0500182}
183
Alyssa Rosenzweig1097c692020-03-21 15:25:54 -0400184static bi_instruction
185bi_load_with_r61(enum bi_class T, nir_intrinsic_instr *instr)
186{
187 bi_instruction ld = bi_load(T, instr);
188 ld.src[1] = BIR_INDEX_REGISTER | 61; /* TODO: RA */
189 ld.src[2] = BIR_INDEX_REGISTER | 62;
Alyssa Rosenzweig1097c692020-03-21 15:25:54 -0400190 ld.src_types[1] = nir_type_uint32;
191 ld.src_types[2] = nir_type_uint32;
Jason Ekstrand0aa08ae2020-09-30 21:20:53 -0500192 ld.format = nir_intrinsic_dest_type(instr);
Alyssa Rosenzweig1097c692020-03-21 15:25:54 -0400193 return ld;
194}
195
Alyssa Rosenzweigdabb6c62020-03-06 09:26:44 -0500196static void
Alyssa Rosenzweig48910e82020-03-06 09:44:19 -0500197bi_emit_st_vary(bi_context *ctx, nir_intrinsic_instr *instr)
198{
Alyssa Rosenzweig1097c692020-03-21 15:25:54 -0400199 bi_instruction address = bi_load_with_r61(BI_LOAD_VAR_ADDRESS, instr);
Alyssa Rosenzweig69c66ff2020-03-09 19:52:56 -0400200 address.dest = bi_make_temp(ctx);
Alyssa Rosenzweig9458b012020-03-20 12:25:08 -0400201 address.dest_type = nir_type_uint32;
Alyssa Rosenzweigb2c6cf22020-04-24 17:20:28 -0400202 address.vector_channels = 3;
Alyssa Rosenzweig48910e82020-03-06 09:44:19 -0500203
Alyssa Rosenzweigaba7f092020-04-14 20:20:37 -0400204 unsigned nr = nir_intrinsic_src_components(instr, 0);
205 assert(nir_intrinsic_write_mask(instr) == ((1 << nr) - 1));
206
Alyssa Rosenzweig48910e82020-03-06 09:44:19 -0500207 bi_instruction st = {
208 .type = BI_STORE_VAR,
209 .src = {
Alyssa Rosenzweigfbbe3d42020-04-27 16:04:05 -0400210 pan_src_index(&instr->src[0]),
Alyssa Rosenzweig9458b012020-03-20 12:25:08 -0400211 address.dest, address.dest, address.dest,
Alyssa Rosenzweig795646d2020-03-09 14:09:04 -0400212 },
Alyssa Rosenzweig116c5412020-03-11 21:45:32 -0400213 .src_types = {
Alyssa Rosenzweig9458b012020-03-20 12:25:08 -0400214 nir_type_uint32,
215 nir_type_uint32, nir_type_uint32, nir_type_uint32,
Alyssa Rosenzweig116c5412020-03-11 21:45:32 -0400216 },
Alyssa Rosenzweig795646d2020-03-09 14:09:04 -0400217 .swizzle = {
Alyssa Rosenzweigaba7f092020-04-14 20:20:37 -0400218 { 0 },
Alyssa Rosenzweig9458b012020-03-20 12:25:08 -0400219 { 0 }, { 1 }, { 2}
Alyssa Rosenzweig9213b252020-03-20 12:38:53 -0400220 },
Alyssa Rosenzweigb2c6cf22020-04-24 17:20:28 -0400221 .vector_channels = nr,
Alyssa Rosenzweig48910e82020-03-06 09:44:19 -0500222 };
223
Alyssa Rosenzweigaba7f092020-04-14 20:20:37 -0400224 for (unsigned i = 0; i < nr; ++i)
225 st.swizzle[0][i] = i;
226
Alyssa Rosenzweig48910e82020-03-06 09:44:19 -0500227 bi_emit(ctx, address);
228 bi_emit(ctx, st);
229}
230
231static void
Alyssa Rosenzweig1ead0d32020-03-06 09:52:09 -0500232bi_emit_ld_uniform(bi_context *ctx, nir_intrinsic_instr *instr)
233{
Alyssa Rosenzweig69c66ff2020-03-09 19:52:56 -0400234 bi_instruction ld = bi_load(BI_LOAD_UNIFORM, instr);
235 ld.src[1] = BIR_INDEX_ZERO; /* TODO: UBO index */
Alyssa Rosenzweig6f5b7882020-07-31 17:29:50 -0400236 ld.segment = BI_SEGMENT_UBO;
Alyssa Rosenzweig218785c2020-03-10 16:20:18 -0400237
238 /* TODO: Indirect access, since we need to multiply by the element
239 * size. I believe we can get this lowering automatically via
240 * nir_lower_io (as mul instructions) with the proper options, but this
241 * is TODO */
242 assert(ld.src[0] & BIR_INDEX_CONSTANT);
243 ld.constant.u64 += ctx->sysvals.sysval_count;
244 ld.constant.u64 *= 16;
245
Alyssa Rosenzweig1ead0d32020-03-06 09:52:09 -0500246 bi_emit(ctx, ld);
247}
248
249static void
Alyssa Rosenzweig218785c2020-03-10 16:20:18 -0400250bi_emit_sysval(bi_context *ctx, nir_instr *instr,
251 unsigned nr_components, unsigned offset)
252{
253 nir_dest nir_dest;
254
255 /* Figure out which uniform this is */
256 int sysval = panfrost_sysval_for_instr(instr, &nir_dest);
257 void *val = _mesa_hash_table_u64_search(ctx->sysvals.sysval_to_id, sysval);
258
259 /* Sysvals are prefix uniforms */
260 unsigned uniform = ((uintptr_t) val) - 1;
261
262 /* Emit the read itself -- this is never indirect */
263
264 bi_instruction load = {
265 .type = BI_LOAD_UNIFORM,
Alyssa Rosenzweig6f5b7882020-07-31 17:29:50 -0400266 .segment = BI_SEGMENT_UBO,
Alyssa Rosenzweigb2c6cf22020-04-24 17:20:28 -0400267 .vector_channels = nr_components,
Alyssa Rosenzweig8bb16132020-03-20 11:38:21 -0400268 .src = { BIR_INDEX_CONSTANT, BIR_INDEX_ZERO },
Alyssa Rosenzweigdf693042020-04-14 20:09:00 -0400269 .src_types = { nir_type_uint32, nir_type_uint32 },
Alyssa Rosenzweig218785c2020-03-10 16:20:18 -0400270 .constant = { (uniform * 16) + offset },
Alyssa Rosenzweigfbbe3d42020-04-27 16:04:05 -0400271 .dest = pan_dest_index(&nir_dest),
Alyssa Rosenzweig218785c2020-03-10 16:20:18 -0400272 .dest_type = nir_type_uint32, /* TODO */
273 };
274
275 bi_emit(ctx, load);
276}
277
Alyssa Rosenzweig47c84ee2020-05-01 14:55:04 -0400278/* gl_FragCoord.xy = u16_to_f32(R59.xy) + 0.5
279 * gl_FragCoord.z = ld_vary(fragz)
280 * gl_FragCoord.w = ld_vary(fragw)
281 */
282
283static void
284bi_emit_ld_frag_coord(bi_context *ctx, nir_intrinsic_instr *instr)
285{
286 /* Future proofing for mediump fragcoord at some point.. */
287 nir_alu_type T = nir_type_float32;
288
289 /* First, sketch a combine */
290 bi_instruction combine = {
291 .type = BI_COMBINE,
292 .dest_type = nir_type_uint32,
293 .dest = pan_dest_index(&instr->dest),
294 .src_types = { T, T, T, T },
295 };
296
297 /* Second, handle xy */
298 for (unsigned i = 0; i < 2; ++i) {
299 bi_instruction conv = {
300 .type = BI_CONVERT,
301 .dest_type = T,
302 .dest = bi_make_temp(ctx),
303 .src = {
304 /* TODO: RA XXX */
305 BIR_INDEX_REGISTER | 59
306 },
307 .src_types = { nir_type_uint16 },
308 .swizzle = { { i } }
309 };
310
311 bi_instruction add = {
312 .type = BI_ADD,
313 .dest_type = T,
314 .dest = bi_make_temp(ctx),
315 .src = { conv.dest, BIR_INDEX_CONSTANT },
316 .src_types = { T, T },
317 };
318
319 float half = 0.5;
320 memcpy(&add.constant.u32, &half, sizeof(float));
321
322 bi_emit(ctx, conv);
323 bi_emit(ctx, add);
324
325 combine.src[i] = add.dest;
326 }
327
328 /* Third, zw */
329 for (unsigned i = 0; i < 2; ++i) {
330 bi_instruction load = {
331 .type = BI_LOAD_VAR,
332 .load_vary = {
333 .interp_mode = BIFROST_INTERP_DEFAULT,
334 .reuse = false,
335 .flat = true
336 },
337 .vector_channels = 1,
338 .dest_type = nir_type_float32,
Alyssa Rosenzweig8dd3a812020-07-31 18:48:27 -0400339 .format = nir_type_float32,
Alyssa Rosenzweig47c84ee2020-05-01 14:55:04 -0400340 .dest = bi_make_temp(ctx),
Alyssa Rosenzweig2ff53872020-08-03 12:48:44 -0400341 .src = {
342 BIR_INDEX_CONSTANT,
343 BIR_INDEX_PASS | BIFROST_SRC_CONST_LO
344 },
Alyssa Rosenzweig47c84ee2020-05-01 14:55:04 -0400345 .src_types = { nir_type_uint32, nir_type_uint32 },
346 .constant = {
347 .u32 = (i == 0) ? BIFROST_FRAGZ : BIFROST_FRAGW
348 }
349 };
350
351 bi_emit(ctx, load);
352
353 combine.src[i + 2] = load.dest;
354 }
355
356 /* Finally, emit the combine */
357 bi_emit(ctx, combine);
358}
359
Alyssa Rosenzweig218785c2020-03-10 16:20:18 -0400360static void
Alyssa Rosenzweig7d867f72020-05-01 18:26:18 -0400361bi_emit_discard(bi_context *ctx, nir_intrinsic_instr *instr)
362{
363 /* Goofy lowering */
364 bi_instruction discard = {
365 .type = BI_DISCARD,
366 .cond = BI_COND_EQ,
367 .src_types = { nir_type_uint32, nir_type_uint32 },
368 .src = { BIR_INDEX_ZERO, BIR_INDEX_ZERO },
369 };
370
371 bi_emit(ctx, discard);
372}
373
374static void
Alyssa Rosenzweig8ab5c972020-05-01 18:36:42 -0400375bi_fuse_cond(bi_instruction *csel, nir_alu_src cond,
376 unsigned *constants_left, unsigned *constant_shift,
377 unsigned comps, bool float_only);
378
379static void
Alyssa Rosenzweigc9ab7322020-05-01 18:24:11 -0400380bi_emit_discard_if(bi_context *ctx, nir_intrinsic_instr *instr)
381{
382 nir_src cond = instr->src[0];
383 nir_alu_type T = nir_type_uint | nir_src_bit_size(cond);
384
385 bi_instruction discard = {
386 .type = BI_DISCARD,
387 .cond = BI_COND_NE,
388 .src_types = { T, T },
389 .src = {
390 pan_src_index(&cond),
391 BIR_INDEX_ZERO
392 },
393 };
394
Alyssa Rosenzweig8ab5c972020-05-01 18:36:42 -0400395 /* Try to fuse in the condition */
396 unsigned constants_left = 1, constant_shift = 0;
397
398 /* Scalar so no swizzle */
399 nir_alu_src wrap = {
400 .src = instr->src[0]
401 };
402
403 /* May or may not succeed but we're optimistic */
404 bi_fuse_cond(&discard, wrap, &constants_left, &constant_shift, 1, true);
405
Alyssa Rosenzweigc9ab7322020-05-01 18:24:11 -0400406 bi_emit(ctx, discard);
407}
408
409static void
Alyssa Rosenzweig07671822020-03-05 17:50:18 -0500410emit_intrinsic(bi_context *ctx, nir_intrinsic_instr *instr)
411{
412
413 switch (instr->intrinsic) {
414 case nir_intrinsic_load_barycentric_pixel:
415 /* stub */
416 break;
417 case nir_intrinsic_load_interpolated_input:
Alyssa Rosenzweig59b476e2020-03-06 09:33:52 -0500418 case nir_intrinsic_load_input:
419 if (ctx->stage == MESA_SHADER_FRAGMENT)
420 bi_emit_ld_vary(ctx, instr);
421 else if (ctx->stage == MESA_SHADER_VERTEX)
Alyssa Rosenzweig1097c692020-03-21 15:25:54 -0400422 bi_emit(ctx, bi_load_with_r61(BI_LOAD_ATTR, instr));
Alyssa Rosenzweig59b476e2020-03-06 09:33:52 -0500423 else {
424 unreachable("Unsupported shader stage");
425 }
Alyssa Rosenzweig07671822020-03-05 17:50:18 -0500426 break;
Alyssa Rosenzweig59b476e2020-03-06 09:33:52 -0500427
Alyssa Rosenzweigdabb6c62020-03-06 09:26:44 -0500428 case nir_intrinsic_store_output:
429 if (ctx->stage == MESA_SHADER_FRAGMENT)
430 bi_emit_frag_out(ctx, instr);
Alyssa Rosenzweig48910e82020-03-06 09:44:19 -0500431 else if (ctx->stage == MESA_SHADER_VERTEX)
432 bi_emit_st_vary(ctx, instr);
433 else
434 unreachable("Unsupported shader stage");
Alyssa Rosenzweigdabb6c62020-03-06 09:26:44 -0500435 break;
Alyssa Rosenzweig1ead0d32020-03-06 09:52:09 -0500436
437 case nir_intrinsic_load_uniform:
438 bi_emit_ld_uniform(ctx, instr);
439 break;
440
Alyssa Rosenzweig47c84ee2020-05-01 14:55:04 -0400441 case nir_intrinsic_load_frag_coord:
442 bi_emit_ld_frag_coord(ctx, instr);
443 break;
444
Alyssa Rosenzweig7d867f72020-05-01 18:26:18 -0400445 case nir_intrinsic_discard:
446 bi_emit_discard(ctx, instr);
447 break;
448
Alyssa Rosenzweigc9ab7322020-05-01 18:24:11 -0400449 case nir_intrinsic_discard_if:
450 bi_emit_discard_if(ctx, instr);
451 break;
452
Alyssa Rosenzweig218785c2020-03-10 16:20:18 -0400453 case nir_intrinsic_load_ssbo_address:
454 bi_emit_sysval(ctx, &instr->instr, 1, 0);
455 break;
456
Jason Ekstrand97501642020-09-22 03:24:45 -0500457 case nir_intrinsic_get_ssbo_size:
Alyssa Rosenzweig218785c2020-03-10 16:20:18 -0400458 bi_emit_sysval(ctx, &instr->instr, 1, 8);
459 break;
460
461 case nir_intrinsic_load_viewport_scale:
462 case nir_intrinsic_load_viewport_offset:
463 case nir_intrinsic_load_num_work_groups:
464 case nir_intrinsic_load_sampler_lod_parameters_pan:
465 bi_emit_sysval(ctx, &instr->instr, 3, 0);
466 break;
467
Alyssa Rosenzweig07671822020-03-05 17:50:18 -0500468 default:
Alyssa Rosenzweigc4883902020-05-01 14:13:10 -0400469 unreachable("Unknown intrinsic");
Alyssa Rosenzweig07671822020-03-05 17:50:18 -0500470 break;
471 }
472}
473
474static void
Alyssa Rosenzweig51e537c2020-03-06 16:29:35 -0500475emit_load_const(bi_context *ctx, nir_load_const_instr *instr)
476{
477 /* Make sure we've been lowered */
Alyssa Rosenzweig0e73d872020-06-02 19:30:56 -0400478 assert(instr->def.num_components <= (32 / instr->def.bit_size));
479
480 /* Accumulate all the channels of the constant, as if we did an
481 * implicit SEL over them */
482 uint32_t acc = 0;
483
484 for (unsigned i = 0; i < instr->def.num_components; ++i) {
485 unsigned v = nir_const_value_as_uint(instr->value[i], instr->def.bit_size);
486 acc |= (v << (i * instr->def.bit_size));
487 }
Alyssa Rosenzweig51e537c2020-03-06 16:29:35 -0500488
489 bi_instruction move = {
490 .type = BI_MOV,
Alyssa Rosenzweigfbbe3d42020-04-27 16:04:05 -0400491 .dest = pan_ssa_index(&instr->def),
Alyssa Rosenzweig0e73d872020-06-02 19:30:56 -0400492 .dest_type = nir_type_uint32,
Alyssa Rosenzweig51e537c2020-03-06 16:29:35 -0500493 .src = {
494 BIR_INDEX_CONSTANT
495 },
Alyssa Rosenzweig02ad1472020-03-30 20:54:51 -0400496 .src_types = {
Alyssa Rosenzweig0e73d872020-06-02 19:30:56 -0400497 nir_type_uint32,
Alyssa Rosenzweig02ad1472020-03-30 20:54:51 -0400498 },
Alyssa Rosenzweig51e537c2020-03-06 16:29:35 -0500499 .constant = {
Alyssa Rosenzweig0e73d872020-06-02 19:30:56 -0400500 .u32 = acc
Alyssa Rosenzweig51e537c2020-03-06 16:29:35 -0500501 }
502 };
503
504 bi_emit(ctx, move);
505}
506
Alyssa Rosenzweig12299de2020-03-21 17:37:47 -0400507#define BI_CASE_CMP(op) \
508 case op##8: \
509 case op##16: \
510 case op##32: \
511
Alyssa Rosenzweig929baf32020-03-09 20:19:51 -0400512static enum bi_class
513bi_class_for_nir_alu(nir_op op)
514{
515 switch (op) {
Alyssa Rosenzweigc8622342020-03-09 21:10:41 -0400516 case nir_op_fadd:
Alyssa Rosenzweigacab7882020-03-10 07:56:14 -0400517 case nir_op_fsub:
Alyssa Rosenzweigc8622342020-03-09 21:10:41 -0400518 return BI_ADD;
Alyssa Rosenzweigcf3c3562020-05-04 14:04:35 -0400519
520 case nir_op_iadd:
Alyssa Rosenzweig55f0d812020-03-10 08:03:20 -0400521 case nir_op_isub:
Alyssa Rosenzweig1a94dae2020-05-04 14:00:13 -0400522 return BI_IMATH;
Alyssa Rosenzweigc8622342020-03-09 21:10:41 -0400523
Chris Forbesa0a70872020-07-26 15:54:14 -0700524 case nir_op_imul:
525 return BI_IMUL;
526
Alyssa Rosenzweiga077da62020-04-28 14:36:17 -0400527 case nir_op_iand:
528 case nir_op_ior:
529 case nir_op_ixor:
Chris Forbes539ea082020-07-26 11:37:42 -0700530 case nir_op_inot:
Chris Forbes946ff9b2020-07-26 12:18:54 -0700531 case nir_op_ishl:
Alyssa Rosenzweiga077da62020-04-28 14:36:17 -0400532 return BI_BITWISE;
533
Alyssa Rosenzweig12299de2020-03-21 17:37:47 -0400534 BI_CASE_CMP(nir_op_flt)
535 BI_CASE_CMP(nir_op_fge)
536 BI_CASE_CMP(nir_op_feq)
Karol Herbste5899c12020-08-18 19:51:57 +0200537 BI_CASE_CMP(nir_op_fneu)
Alyssa Rosenzweig12299de2020-03-21 17:37:47 -0400538 BI_CASE_CMP(nir_op_ilt)
539 BI_CASE_CMP(nir_op_ige)
540 BI_CASE_CMP(nir_op_ieq)
541 BI_CASE_CMP(nir_op_ine)
Chris Forbes718d4442020-07-26 12:41:17 -0700542 BI_CASE_CMP(nir_op_uge)
Alyssa Rosenzweig05413502020-03-10 08:21:35 -0400543 return BI_CMP;
544
Alyssa Rosenzweig12299de2020-03-21 17:37:47 -0400545 case nir_op_b8csel:
546 case nir_op_b16csel:
547 case nir_op_b32csel:
Alyssa Rosenzweigd3823552020-03-10 08:32:56 -0400548 return BI_CSEL;
549
Alyssa Rosenzweigc8622342020-03-09 21:10:41 -0400550 case nir_op_i2i8:
551 case nir_op_i2i16:
552 case nir_op_i2i32:
553 case nir_op_i2i64:
554 case nir_op_u2u8:
555 case nir_op_u2u16:
556 case nir_op_u2u32:
557 case nir_op_u2u64:
558 case nir_op_f2i16:
559 case nir_op_f2i32:
560 case nir_op_f2i64:
561 case nir_op_f2u16:
562 case nir_op_f2u32:
563 case nir_op_f2u64:
564 case nir_op_i2f16:
565 case nir_op_i2f32:
566 case nir_op_i2f64:
567 case nir_op_u2f16:
568 case nir_op_u2f32:
569 case nir_op_u2f64:
Alyssa Rosenzweigaa77d812020-03-27 14:40:04 -0400570 case nir_op_f2f16:
571 case nir_op_f2f32:
572 case nir_op_f2f64:
573 case nir_op_f2fmp:
Alyssa Rosenzweigc8622342020-03-09 21:10:41 -0400574 return BI_CONVERT;
575
Alyssa Rosenzweige0a51d52020-03-22 17:31:23 -0400576 case nir_op_vec2:
577 case nir_op_vec3:
578 case nir_op_vec4:
579 return BI_COMBINE;
580
581 case nir_op_vec8:
582 case nir_op_vec16:
583 unreachable("should've been lowered");
584
Alyssa Rosenzweigf6d96aa2020-03-11 15:15:41 -0400585 case nir_op_ffma:
Alyssa Rosenzweigc8622342020-03-09 21:10:41 -0400586 case nir_op_fmul:
587 return BI_FMA;
588
589 case nir_op_imin:
590 case nir_op_imax:
591 case nir_op_umin:
592 case nir_op_umax:
593 case nir_op_fmin:
594 case nir_op_fmax:
595 return BI_MINMAX;
596
Alyssa Rosenzweig5a5896c2020-03-09 21:02:51 -0400597 case nir_op_fsat:
Alyssa Rosenzweig1216a632020-03-10 07:52:24 -0400598 case nir_op_fneg:
599 case nir_op_fabs:
Alyssa Rosenzweig6b7077e2020-03-19 16:58:48 -0400600 return BI_FMOV;
Alyssa Rosenzweigc8622342020-03-09 21:10:41 -0400601 case nir_op_mov:
602 return BI_MOV;
603
Alyssa Rosenzweigf81b67b2020-03-27 20:28:09 -0400604 case nir_op_fround_even:
605 case nir_op_fceil:
606 case nir_op_ffloor:
607 case nir_op_ftrunc:
608 return BI_ROUND;
609
Alyssa Rosenzweig8ed79c92020-03-09 21:20:20 -0400610 case nir_op_frcp:
611 case nir_op_frsq:
Chris Forbes1882b1e2020-07-27 11:51:31 -0700612 case nir_op_iabs:
Alyssa Rosenzweig8ed79c92020-03-09 21:20:20 -0400613 return BI_SPECIAL;
614
Alyssa Rosenzweigc8622342020-03-09 21:10:41 -0400615 default:
616 unreachable("Unknown ALU op");
Alyssa Rosenzweig929baf32020-03-09 20:19:51 -0400617 }
618}
619
Alyssa Rosenzweig5a02c8712020-03-21 18:12:31 -0400620/* Gets a bi_cond for a given NIR comparison opcode. In soft mode, it will
621 * return BI_COND_ALWAYS as a sentinel if it fails to do so (when used for
622 * optimizations). Otherwise it will bail (when used for primary code
623 * generation). */
624
Alyssa Rosenzweig05413502020-03-10 08:21:35 -0400625static enum bi_cond
Alyssa Rosenzweig5a02c8712020-03-21 18:12:31 -0400626bi_cond_for_nir(nir_op op, bool soft)
Alyssa Rosenzweig05413502020-03-10 08:21:35 -0400627{
628 switch (op) {
Alyssa Rosenzweig12299de2020-03-21 17:37:47 -0400629 BI_CASE_CMP(nir_op_flt)
630 BI_CASE_CMP(nir_op_ilt)
Alyssa Rosenzweig05413502020-03-10 08:21:35 -0400631 return BI_COND_LT;
Alyssa Rosenzweig12299de2020-03-21 17:37:47 -0400632
633 BI_CASE_CMP(nir_op_fge)
634 BI_CASE_CMP(nir_op_ige)
Chris Forbes718d4442020-07-26 12:41:17 -0700635 BI_CASE_CMP(nir_op_uge)
Alyssa Rosenzweig05413502020-03-10 08:21:35 -0400636 return BI_COND_GE;
Alyssa Rosenzweig12299de2020-03-21 17:37:47 -0400637
638 BI_CASE_CMP(nir_op_feq)
639 BI_CASE_CMP(nir_op_ieq)
Alyssa Rosenzweig05413502020-03-10 08:21:35 -0400640 return BI_COND_EQ;
Alyssa Rosenzweig12299de2020-03-21 17:37:47 -0400641
Karol Herbste5899c12020-08-18 19:51:57 +0200642 BI_CASE_CMP(nir_op_fneu)
Alyssa Rosenzweig12299de2020-03-21 17:37:47 -0400643 BI_CASE_CMP(nir_op_ine)
Alyssa Rosenzweig05413502020-03-10 08:21:35 -0400644 return BI_COND_NE;
645 default:
Alyssa Rosenzweig5a02c8712020-03-21 18:12:31 -0400646 if (soft)
647 return BI_COND_ALWAYS;
648 else
649 unreachable("Invalid compare");
Alyssa Rosenzweig05413502020-03-10 08:21:35 -0400650 }
651}
652
Alyssa Rosenzweig929baf32020-03-09 20:19:51 -0400653static void
Alyssa Rosenzweig3f786ed2020-03-21 18:13:49 -0400654bi_copy_src(bi_instruction *alu, nir_alu_instr *instr, unsigned i, unsigned to,
Alyssa Rosenzweigb2c6cf22020-04-24 17:20:28 -0400655 unsigned *constants_left, unsigned *constant_shift, unsigned comps)
Alyssa Rosenzweig3f786ed2020-03-21 18:13:49 -0400656{
657 unsigned bits = nir_src_bit_size(instr->src[i].src);
658 unsigned dest_bits = nir_dest_bit_size(instr->dest.dest);
659
660 alu->src_types[to] = nir_op_infos[instr->op].input_types[i]
661 | bits;
662
663 /* Try to inline a constant */
664 if (nir_src_is_const(instr->src[i].src) && *constants_left && (dest_bits == bits)) {
Alyssa Rosenzweigd772bf02020-04-15 10:39:42 -0400665 uint64_t mask = (1ull << dest_bits) - 1;
666 uint64_t cons = nir_src_as_uint(instr->src[i].src);
Alyssa Rosenzweig3f786ed2020-03-21 18:13:49 -0400667
Alyssa Rosenzweigd772bf02020-04-15 10:39:42 -0400668 /* Try to reuse a constant */
669 for (unsigned i = 0; i < (*constant_shift); i += dest_bits) {
670 if (((alu->constant.u64 >> i) & mask) == cons) {
671 alu->src[to] = BIR_INDEX_CONSTANT | i;
672 return;
673 }
674 }
675
676 alu->constant.u64 |= cons << *constant_shift;
Alyssa Rosenzweig3f786ed2020-03-21 18:13:49 -0400677 alu->src[to] = BIR_INDEX_CONSTANT | (*constant_shift);
678 --(*constants_left);
Alyssa Rosenzweig4d0f9412020-04-17 15:52:03 -0400679 (*constant_shift) += MAX2(dest_bits, 32); /* lo/hi */
Alyssa Rosenzweig3f786ed2020-03-21 18:13:49 -0400680 return;
681 }
682
Alyssa Rosenzweigfbbe3d42020-04-27 16:04:05 -0400683 alu->src[to] = pan_src_index(&instr->src[i].src);
Alyssa Rosenzweig3f786ed2020-03-21 18:13:49 -0400684
Alyssa Rosenzweigb2c6cf22020-04-24 17:20:28 -0400685 /* Copy swizzle for all vectored components, replicating last component
686 * to fill undersized */
687
688 unsigned vec = alu->type == BI_COMBINE ? 1 :
689 MAX2(1, 32 / dest_bits);
690
691 for (unsigned j = 0; j < vec; ++j)
692 alu->swizzle[to][j] = instr->src[i].swizzle[MIN2(j, comps - 1)];
Alyssa Rosenzweig3f786ed2020-03-21 18:13:49 -0400693}
694
695static void
Alyssa Rosenzweig201a11a2020-05-01 18:31:22 -0400696bi_fuse_cond(bi_instruction *csel, nir_alu_src cond,
697 unsigned *constants_left, unsigned *constant_shift,
698 unsigned comps, bool float_only)
Alyssa Rosenzweig3f786ed2020-03-21 18:13:49 -0400699{
700 /* Bail for vector weirdness */
701 if (cond.swizzle[0] != 0)
702 return;
703
704 if (!cond.src.is_ssa)
705 return;
706
707 nir_ssa_def *def = cond.src.ssa;
708 nir_instr *parent = def->parent_instr;
709
710 if (parent->type != nir_instr_type_alu)
711 return;
712
713 nir_alu_instr *alu = nir_instr_as_alu(parent);
714
715 /* Try to match a condition */
716 enum bi_cond bcond = bi_cond_for_nir(alu->op, true);
717
718 if (bcond == BI_COND_ALWAYS)
719 return;
720
Alyssa Rosenzweig201a11a2020-05-01 18:31:22 -0400721 /* Some instructions can't compare ints */
722 if (float_only) {
723 nir_alu_type T = nir_op_infos[alu->op].input_types[0];
724 T = nir_alu_type_get_base_type(T);
725
726 if (T != nir_type_float)
727 return;
728 }
729
Alyssa Rosenzweig3f786ed2020-03-21 18:13:49 -0400730 /* We found one, let's fuse it in */
Alyssa Rosenzweig95fc71e2020-04-27 14:15:57 -0400731 csel->cond = bcond;
Alyssa Rosenzweigb2c6cf22020-04-24 17:20:28 -0400732 bi_copy_src(csel, alu, 0, 0, constants_left, constant_shift, comps);
733 bi_copy_src(csel, alu, 1, 1, constants_left, constant_shift, comps);
Alyssa Rosenzweig3f786ed2020-03-21 18:13:49 -0400734}
735
736static void
Alyssa Rosenzweig929baf32020-03-09 20:19:51 -0400737emit_alu(bi_context *ctx, nir_alu_instr *instr)
738{
Alyssa Rosenzweig8e522062020-04-14 18:52:21 -0400739 /* Try some special functions */
740 switch (instr->op) {
741 case nir_op_fexp2:
742 bi_emit_fexp2(ctx, instr);
743 return;
Alyssa Rosenzweig031ad0e2020-04-14 19:50:24 -0400744 case nir_op_flog2:
745 bi_emit_flog2(ctx, instr);
746 return;
Alyssa Rosenzweig8e522062020-04-14 18:52:21 -0400747 default:
748 break;
749 }
750
751 /* Otherwise, assume it's something we can handle normally */
Alyssa Rosenzweig929baf32020-03-09 20:19:51 -0400752 bi_instruction alu = {
753 .type = bi_class_for_nir_alu(instr->op),
Alyssa Rosenzweigfbbe3d42020-04-27 16:04:05 -0400754 .dest = pan_dest_index(&instr->dest.dest),
Alyssa Rosenzweig929baf32020-03-09 20:19:51 -0400755 .dest_type = nir_op_infos[instr->op].output_type
756 | nir_dest_bit_size(instr->dest.dest),
757 };
758
Alyssa Rosenzweig8ed79c92020-03-09 21:20:20 -0400759 /* TODO: Implement lowering of special functions for older Bifrost */
760 assert((alu.type != BI_SPECIAL) || !(ctx->quirks & BIFROST_NO_FAST_OP));
761
Alyssa Rosenzweigb2c6cf22020-04-24 17:20:28 -0400762 unsigned comps = nir_dest_num_components(instr->dest.dest);
Alyssa Rosenzweige0a51d52020-03-22 17:31:23 -0400763
Alyssa Rosenzweigb2c6cf22020-04-24 17:20:28 -0400764 if (alu.type != BI_COMBINE)
765 assert(comps <= MAX2(1, 32 / comps));
Alyssa Rosenzweige0a51d52020-03-22 17:31:23 -0400766
Alyssa Rosenzweigb2c6cf22020-04-24 17:20:28 -0400767 if (!instr->dest.dest.is_ssa) {
768 for (unsigned i = 0; i < comps; ++i)
769 assert(instr->dest.write_mask);
Alyssa Rosenzweig929baf32020-03-09 20:19:51 -0400770 }
771
Alyssa Rosenzweig48e50ef2020-03-09 20:32:00 -0400772 /* We inline constants as we go. This tracks how many constants have
773 * been inlined, since we're limited to 64-bits of constants per
774 * instruction */
775
776 unsigned dest_bits = nir_dest_bit_size(instr->dest.dest);
777 unsigned constants_left = (64 / dest_bits);
778 unsigned constant_shift = 0;
779
Alyssa Rosenzweig02ad1472020-03-30 20:54:51 -0400780 if (alu.type == BI_COMBINE)
781 constants_left = 0;
782
Alyssa Rosenzweig929baf32020-03-09 20:19:51 -0400783 /* Copy sources */
784
785 unsigned num_inputs = nir_op_infos[instr->op].num_inputs;
786 assert(num_inputs <= ARRAY_SIZE(alu.src));
787
Alyssa Rosenzweig8eefb272020-04-05 19:22:01 -0400788 for (unsigned i = 0; i < num_inputs; ++i) {
789 unsigned f = 0;
790
791 if (i && alu.type == BI_CSEL)
792 f++;
793
Alyssa Rosenzweigb2c6cf22020-04-24 17:20:28 -0400794 bi_copy_src(&alu, instr, i, i + f, &constants_left, &constant_shift, comps);
Alyssa Rosenzweig8eefb272020-04-05 19:22:01 -0400795 }
Alyssa Rosenzweig929baf32020-03-09 20:19:51 -0400796
797 /* Op-specific fixup */
798 switch (instr->op) {
799 case nir_op_fmul:
800 alu.src[2] = BIR_INDEX_ZERO; /* FMA */
Alyssa Rosenzweigb5148b62020-03-27 15:53:12 -0400801 alu.src_types[2] = alu.src_types[1];
Alyssa Rosenzweig929baf32020-03-09 20:19:51 -0400802 break;
Alyssa Rosenzweig5a5896c2020-03-09 21:02:51 -0400803 case nir_op_fsat:
Alyssa Rosenzweig6b7077e2020-03-19 16:58:48 -0400804 alu.outmod = BIFROST_SAT; /* FMOV */
Alyssa Rosenzweig5a5896c2020-03-09 21:02:51 -0400805 break;
Alyssa Rosenzweig1216a632020-03-10 07:52:24 -0400806 case nir_op_fneg:
Alyssa Rosenzweig6b7077e2020-03-19 16:58:48 -0400807 alu.src_neg[0] = true; /* FMOV */
Alyssa Rosenzweig1216a632020-03-10 07:52:24 -0400808 break;
809 case nir_op_fabs:
Alyssa Rosenzweig6b7077e2020-03-19 16:58:48 -0400810 alu.src_abs[0] = true; /* FMOV */
Alyssa Rosenzweig1216a632020-03-10 07:52:24 -0400811 break;
Alyssa Rosenzweigacab7882020-03-10 07:56:14 -0400812 case nir_op_fsub:
Alyssa Rosenzweig6b7077e2020-03-19 16:58:48 -0400813 alu.src_neg[1] = true; /* FADD */
Alyssa Rosenzweigacab7882020-03-10 07:56:14 -0400814 break;
Alyssa Rosenzweigcf3c3562020-05-04 14:04:35 -0400815 case nir_op_iadd:
816 alu.op.imath = BI_IMATH_ADD;
Alyssa Rosenzweig33710ff2020-07-31 16:47:05 -0400817 /* Carry */
818 alu.src[2] = BIR_INDEX_ZERO;
Alyssa Rosenzweigcf3c3562020-05-04 14:04:35 -0400819 break;
820 case nir_op_isub:
821 alu.op.imath = BI_IMATH_SUB;
Alyssa Rosenzweig33710ff2020-07-31 16:47:05 -0400822 /* Borrow */
823 alu.src[2] = BIR_INDEX_ZERO;
Alyssa Rosenzweigcf3c3562020-05-04 14:04:35 -0400824 break;
Chris Forbes1882b1e2020-07-27 11:51:31 -0700825 case nir_op_iabs:
826 alu.op.special = BI_SPECIAL_IABS;
827 break;
Chris Forbes539ea082020-07-26 11:37:42 -0700828 case nir_op_inot:
Alyssa Rosenzweigd2158a52020-09-09 17:46:58 -0400829 /* no dedicated bitwise not, but we can invert sources. convert to ~(a | 0) */
Chris Forbes539ea082020-07-26 11:37:42 -0700830 alu.op.bitwise = BI_BITWISE_OR;
Alyssa Rosenzweigd2158a52020-09-09 17:46:58 -0400831 alu.bitwise.dest_invert = true;
Chris Forbes539ea082020-07-26 11:37:42 -0700832 alu.src[1] = BIR_INDEX_ZERO;
Chris Forbes946ff9b2020-07-26 12:18:54 -0700833 /* zero shift */
834 alu.src[2] = BIR_INDEX_ZERO;
Alyssa Rosenzweig08b105d2020-09-09 17:40:22 -0400835 alu.src_types[2] = nir_type_uint8;
Chris Forbes946ff9b2020-07-26 12:18:54 -0700836 break;
837 case nir_op_ishl:
838 alu.op.bitwise = BI_BITWISE_OR;
839 /* move src1 to src2 and replace with zero. underlying op is (src0 << src2) | src1 */
840 alu.src[2] = alu.src[1];
Alyssa Rosenzweig08b105d2020-09-09 17:40:22 -0400841 alu.src_types[2] = nir_type_uint8;
Chris Forbes946ff9b2020-07-26 12:18:54 -0700842 alu.src[1] = BIR_INDEX_ZERO;
Chris Forbes539ea082020-07-26 11:37:42 -0700843 break;
Chris Forbesa0a70872020-07-26 15:54:14 -0700844 case nir_op_imul:
845 alu.op.imul = BI_IMUL_IMUL;
846 break;
Alyssa Rosenzweigc8622342020-03-09 21:10:41 -0400847 case nir_op_fmax:
848 case nir_op_imax:
849 case nir_op_umax:
850 alu.op.minmax = BI_MINMAX_MAX; /* MINMAX */
851 break;
Alyssa Rosenzweig8ed79c92020-03-09 21:20:20 -0400852 case nir_op_frcp:
853 alu.op.special = BI_SPECIAL_FRCP;
854 break;
855 case nir_op_frsq:
856 alu.op.special = BI_SPECIAL_FRSQ;
857 break;
Alyssa Rosenzweig12299de2020-03-21 17:37:47 -0400858 BI_CASE_CMP(nir_op_flt)
859 BI_CASE_CMP(nir_op_ilt)
860 BI_CASE_CMP(nir_op_fge)
861 BI_CASE_CMP(nir_op_ige)
862 BI_CASE_CMP(nir_op_feq)
863 BI_CASE_CMP(nir_op_ieq)
Karol Herbste5899c12020-08-18 19:51:57 +0200864 BI_CASE_CMP(nir_op_fneu)
Alyssa Rosenzweig12299de2020-03-21 17:37:47 -0400865 BI_CASE_CMP(nir_op_ine)
Chris Forbes718d4442020-07-26 12:41:17 -0700866 BI_CASE_CMP(nir_op_uge)
Alyssa Rosenzweig95fc71e2020-04-27 14:15:57 -0400867 alu.cond = bi_cond_for_nir(instr->op, false);
Alyssa Rosenzweig05413502020-03-10 08:21:35 -0400868 break;
Alyssa Rosenzweigf81b67b2020-03-27 20:28:09 -0400869 case nir_op_fround_even:
Alyssa Rosenzweigf81b67b2020-03-27 20:28:09 -0400870 alu.roundmode = BIFROST_RTE;
871 break;
872 case nir_op_fceil:
Alyssa Rosenzweigf81b67b2020-03-27 20:28:09 -0400873 alu.roundmode = BIFROST_RTP;
874 break;
875 case nir_op_ffloor:
Alyssa Rosenzweigf81b67b2020-03-27 20:28:09 -0400876 alu.roundmode = BIFROST_RTN;
877 break;
878 case nir_op_ftrunc:
Alyssa Rosenzweigf81b67b2020-03-27 20:28:09 -0400879 alu.roundmode = BIFROST_RTZ;
880 break;
Alyssa Rosenzweiga077da62020-04-28 14:36:17 -0400881 case nir_op_iand:
882 alu.op.bitwise = BI_BITWISE_AND;
Chris Forbes946ff9b2020-07-26 12:18:54 -0700883 /* zero shift */
884 alu.src[2] = BIR_INDEX_ZERO;
Alyssa Rosenzweig08b105d2020-09-09 17:40:22 -0400885 alu.src_types[2] = nir_type_uint8;
Alyssa Rosenzweiga077da62020-04-28 14:36:17 -0400886 break;
887 case nir_op_ior:
888 alu.op.bitwise = BI_BITWISE_OR;
Chris Forbes946ff9b2020-07-26 12:18:54 -0700889 /* zero shift */
890 alu.src[2] = BIR_INDEX_ZERO;
Alyssa Rosenzweig08b105d2020-09-09 17:40:22 -0400891 alu.src_types[2] = nir_type_uint8;
Alyssa Rosenzweiga077da62020-04-28 14:36:17 -0400892 break;
893 case nir_op_ixor:
894 alu.op.bitwise = BI_BITWISE_XOR;
Chris Forbes946ff9b2020-07-26 12:18:54 -0700895 /* zero shift */
896 alu.src[2] = BIR_INDEX_ZERO;
Alyssa Rosenzweig08b105d2020-09-09 17:40:22 -0400897 alu.src_types[2] = nir_type_uint8;
Alyssa Rosenzweiga077da62020-04-28 14:36:17 -0400898 break;
Chris Forbesf6aa0712020-07-04 15:26:42 -0700899 case nir_op_f2i32:
900 alu.roundmode = BIFROST_RTZ;
901 break;
Alyssa Rosenzweig1b09c692020-06-02 19:29:25 -0400902
903 case nir_op_f2f16:
904 case nir_op_i2i16:
905 case nir_op_u2u16: {
906 if (nir_src_bit_size(instr->src[0].src) != 32)
907 break;
908
909 /* Should have been const folded */
910 assert(!nir_src_is_const(instr->src[0].src));
911
912 alu.src_types[1] = alu.src_types[0];
913 alu.src[1] = alu.src[0];
914
915 unsigned last = nir_dest_num_components(instr->dest.dest) - 1;
916 assert(last <= 1);
917
918 alu.swizzle[1][0] = instr->src[0].swizzle[last];
919 break;
920 }
921
Alyssa Rosenzweig929baf32020-03-09 20:19:51 -0400922 default:
923 break;
924 }
925
Alyssa Rosenzweig3f786ed2020-03-21 18:13:49 -0400926 if (alu.type == BI_CSEL) {
Alyssa Rosenzweig5cdc31a2020-03-21 21:19:14 -0400927 /* Default to csel3 */
Alyssa Rosenzweig95fc71e2020-04-27 14:15:57 -0400928 alu.cond = BI_COND_NE;
Alyssa Rosenzweig8eefb272020-04-05 19:22:01 -0400929 alu.src[1] = BIR_INDEX_ZERO;
930 alu.src_types[1] = alu.src_types[0];
Alyssa Rosenzweig5cdc31a2020-03-21 21:19:14 -0400931
Alyssa Rosenzweig31a41bb2020-05-01 17:34:47 -0400932 /* TODO: Reenable cond fusing when we can split up registers
933 * when scheduling */
934#if 0
Alyssa Rosenzweig201a11a2020-05-01 18:31:22 -0400935 bi_fuse_cond(&alu, instr->src[0],
936 &constants_left, &constant_shift, comps, false);
Alyssa Rosenzweig31a41bb2020-05-01 17:34:47 -0400937#endif
Alyssa Rosenzweig3f786ed2020-03-21 18:13:49 -0400938 }
939
Alyssa Rosenzweig929baf32020-03-09 20:19:51 -0400940 bi_emit(ctx, alu);
941}
942
Alyssa Rosenzweig0769036a2020-04-21 12:15:29 -0400943/* TEX_COMPACT instructions assume normal 2D f32 operation but are more
944 * space-efficient and with simpler RA/scheduling requirements*/
945
946static void
947emit_tex_compact(bi_context *ctx, nir_tex_instr *instr)
948{
Alyssa Rosenzweigcd5fe3b2020-04-21 13:00:44 -0400949 bi_instruction tex = {
950 .type = BI_TEX,
951 .op = { .texture = BI_TEX_COMPACT },
Alyssa Rosenzweig5f35cda2020-04-30 16:10:55 -0400952 .texture = {
953 .texture_index = instr->texture_index,
954 .sampler_index = instr->sampler_index,
Alyssa Rosenzweig67d89562020-08-03 12:47:57 -0400955 .compute_lod = instr->op == nir_texop_tex,
Alyssa Rosenzweig5f35cda2020-04-30 16:10:55 -0400956 },
Alyssa Rosenzweigfbbe3d42020-04-27 16:04:05 -0400957 .dest = pan_dest_index(&instr->dest),
Alyssa Rosenzweigcd5fe3b2020-04-21 13:00:44 -0400958 .dest_type = instr->dest_type,
959 .src_types = { nir_type_float32, nir_type_float32 },
Alyssa Rosenzweigb2c6cf22020-04-24 17:20:28 -0400960 .vector_channels = 4
Alyssa Rosenzweigcd5fe3b2020-04-21 13:00:44 -0400961 };
962
963 for (unsigned i = 0; i < instr->num_srcs; ++i) {
Alyssa Rosenzweigfbbe3d42020-04-27 16:04:05 -0400964 int index = pan_src_index(&instr->src[i].src);
Alyssa Rosenzweig6650fa22020-05-27 11:43:37 -0400965
966 /* We were checked ahead-of-time */
967 if (instr->src[i].src_type == nir_tex_src_lod)
968 continue;
969
Alyssa Rosenzweigcd5fe3b2020-04-21 13:00:44 -0400970 assert (instr->src[i].src_type == nir_tex_src_coord);
971
972 tex.src[0] = index;
973 tex.src[1] = index;
974 tex.swizzle[0][0] = 0;
975 tex.swizzle[1][0] = 1;
976 }
977
978 bi_emit(ctx, tex);
Alyssa Rosenzweig0769036a2020-04-21 12:15:29 -0400979}
980
981static void
982emit_tex_full(bi_context *ctx, nir_tex_instr *instr)
983{
984 unreachable("stub");
985}
986
Alyssa Rosenzweig731dfc62020-05-27 11:41:42 -0400987/* Normal textures ops are tex for frag shaders and txl for vertex shaders with
988 * lod a constant 0. Anything else needs a full texture op. */
989
990static bool
991bi_is_normal_tex(gl_shader_stage stage, nir_tex_instr *instr)
992{
993 if (stage == MESA_SHADER_FRAGMENT)
994 return instr->op == nir_texop_tex;
995
996 if (instr->op != nir_texop_txl)
997 return false;
998
999 for (unsigned i = 0; i < instr->num_srcs; ++i) {
1000 if (instr->src[i].src_type != nir_tex_src_lod)
1001 continue;
1002
1003 nir_src src = instr->src[i].src;
1004
1005 if (!nir_src_is_const(src))
1006 continue;
1007
1008 if (nir_src_as_uint(src) != 0)
1009 continue;
1010 }
1011
1012 return true;
1013}
1014
Alyssa Rosenzweig0769036a2020-04-21 12:15:29 -04001015static void
1016emit_tex(bi_context *ctx, nir_tex_instr *instr)
1017{
1018 nir_alu_type base = nir_alu_type_get_base_type(instr->dest_type);
1019 unsigned sz = nir_dest_bit_size(instr->dest);
1020 instr->dest_type = base | sz;
1021
Alyssa Rosenzweig731dfc62020-05-27 11:41:42 -04001022 bool is_normal = bi_is_normal_tex(ctx->stage, instr);
Alyssa Rosenzweig0769036a2020-04-21 12:15:29 -04001023 bool is_2d = instr->sampler_dim == GLSL_SAMPLER_DIM_2D ||
1024 instr->sampler_dim == GLSL_SAMPLER_DIM_EXTERNAL;
1025 bool is_f = base == nir_type_float && (sz == 16 || sz == 32);
1026
1027 bool is_compact = is_normal && is_2d && is_f && !instr->is_shadow;
1028
1029 if (is_compact)
1030 emit_tex_compact(ctx, instr);
1031 else
1032 emit_tex_full(ctx, instr);
1033}
1034
Alyssa Rosenzweig51e537c2020-03-06 16:29:35 -05001035static void
Alyssa Rosenzweig65c8dcc2020-03-05 17:10:46 -05001036emit_instr(bi_context *ctx, struct nir_instr *instr)
1037{
1038 switch (instr->type) {
Alyssa Rosenzweig65c8dcc2020-03-05 17:10:46 -05001039 case nir_instr_type_load_const:
1040 emit_load_const(ctx, nir_instr_as_load_const(instr));
1041 break;
1042
1043 case nir_instr_type_intrinsic:
1044 emit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
1045 break;
1046
1047 case nir_instr_type_alu:
1048 emit_alu(ctx, nir_instr_as_alu(instr));
1049 break;
1050
1051 case nir_instr_type_tex:
1052 emit_tex(ctx, nir_instr_as_tex(instr));
1053 break;
Alyssa Rosenzweig65c8dcc2020-03-05 17:10:46 -05001054
1055 case nir_instr_type_jump:
1056 emit_jump(ctx, nir_instr_as_jump(instr));
1057 break;
1058
1059 case nir_instr_type_ssa_undef:
1060 /* Spurious */
1061 break;
1062
1063 default:
Alyssa Rosenzweig0769036a2020-04-21 12:15:29 -04001064 unreachable("Unhandled instruction type");
Alyssa Rosenzweig65c8dcc2020-03-05 17:10:46 -05001065 break;
1066 }
1067}
1068
1069
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -05001070
1071static bi_block *
1072create_empty_block(bi_context *ctx)
1073{
1074 bi_block *blk = rzalloc(ctx, bi_block);
1075
Alyssa Rosenzweig9b75f412020-03-11 14:35:38 -04001076 blk->base.predecessors = _mesa_set_create(blk,
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -05001077 _mesa_hash_pointer,
1078 _mesa_key_pointer_equal);
1079
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -05001080 return blk;
1081}
1082
1083static bi_block *
1084emit_block(bi_context *ctx, nir_block *block)
1085{
Alyssa Rosenzweig9a00cf32020-03-05 16:45:16 -05001086 if (ctx->after_block) {
1087 ctx->current_block = ctx->after_block;
1088 ctx->after_block = NULL;
1089 } else {
1090 ctx->current_block = create_empty_block(ctx);
1091 }
1092
Alyssa Rosenzweig9b75f412020-03-11 14:35:38 -04001093 list_addtail(&ctx->current_block->base.link, &ctx->blocks);
1094 list_inithead(&ctx->current_block->base.instructions);
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -05001095
1096 nir_foreach_instr(instr, block) {
Alyssa Rosenzweig65c8dcc2020-03-05 17:10:46 -05001097 emit_instr(ctx, instr);
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -05001098 ++ctx->instruction_count;
1099 }
1100
1101 return ctx->current_block;
1102}
1103
Alyssa Rosenzweig9a00cf32020-03-05 16:45:16 -05001104/* Emits an unconditional branch to the end of the current block, returning a
1105 * pointer so the user can fill in details */
1106
1107static bi_instruction *
1108bi_emit_branch(bi_context *ctx)
1109{
1110 bi_instruction branch = {
1111 .type = BI_BRANCH,
Alyssa Rosenzweig6627b202020-05-01 18:13:54 -04001112 .cond = BI_COND_ALWAYS
Alyssa Rosenzweig9a00cf32020-03-05 16:45:16 -05001113 };
1114
1115 return bi_emit(ctx, branch);
1116}
1117
1118/* Sets a condition for a branch by examing the NIR condition. If we're
1119 * familiar with the condition, we unwrap it to fold it into the branch
1120 * instruction. Otherwise, we consume the condition directly. We
1121 * generally use 1-bit booleans which allows us to use small types for
1122 * the conditions.
1123 */
1124
1125static void
1126bi_set_branch_cond(bi_instruction *branch, nir_src *cond, bool invert)
1127{
1128 /* TODO: Try to unwrap instead of always bailing */
Alyssa Rosenzweigfbbe3d42020-04-27 16:04:05 -04001129 branch->src[0] = pan_src_index(cond);
Alyssa Rosenzweig9a00cf32020-03-05 16:45:16 -05001130 branch->src[1] = BIR_INDEX_ZERO;
Alyssa Rosenzweigd619ff02020-05-28 12:39:14 -04001131 branch->src_types[0] = branch->src_types[1] = nir_type_uint |
1132 nir_src_bit_size(*cond);
Alyssa Rosenzweig6627b202020-05-01 18:13:54 -04001133 branch->cond = invert ? BI_COND_EQ : BI_COND_NE;
Alyssa Rosenzweig9a00cf32020-03-05 16:45:16 -05001134}
1135
1136static void
1137emit_if(bi_context *ctx, nir_if *nif)
1138{
1139 bi_block *before_block = ctx->current_block;
1140
1141 /* Speculatively emit the branch, but we can't fill it in until later */
1142 bi_instruction *then_branch = bi_emit_branch(ctx);
1143 bi_set_branch_cond(then_branch, &nif->condition, true);
1144
1145 /* Emit the two subblocks. */
1146 bi_block *then_block = emit_cf_list(ctx, &nif->then_list);
1147 bi_block *end_then_block = ctx->current_block;
1148
1149 /* Emit a jump from the end of the then block to the end of the else */
1150 bi_instruction *then_exit = bi_emit_branch(ctx);
1151
1152 /* Emit second block, and check if it's empty */
1153
1154 int count_in = ctx->instruction_count;
1155 bi_block *else_block = emit_cf_list(ctx, &nif->else_list);
1156 bi_block *end_else_block = ctx->current_block;
1157 ctx->after_block = create_empty_block(ctx);
1158
1159 /* Now that we have the subblocks emitted, fix up the branches */
1160
1161 assert(then_block);
1162 assert(else_block);
1163
1164 if (ctx->instruction_count == count_in) {
1165 /* The else block is empty, so don't emit an exit jump */
1166 bi_remove_instruction(then_exit);
Alyssa Rosenzweig6627b202020-05-01 18:13:54 -04001167 then_branch->branch_target = ctx->after_block;
Alyssa Rosenzweige42a5df2020-05-27 18:27:08 -04001168 pan_block_add_successor(&end_then_block->base, &ctx->after_block->base); /* fallthrough */
Alyssa Rosenzweig9a00cf32020-03-05 16:45:16 -05001169 } else {
Alyssa Rosenzweig6627b202020-05-01 18:13:54 -04001170 then_branch->branch_target = else_block;
1171 then_exit->branch_target = ctx->after_block;
1172 pan_block_add_successor(&end_then_block->base, &then_exit->branch_target->base);
Alyssa Rosenzweige42a5df2020-05-27 18:27:08 -04001173 pan_block_add_successor(&end_else_block->base, &ctx->after_block->base); /* fallthrough */
Alyssa Rosenzweig9a00cf32020-03-05 16:45:16 -05001174 }
1175
Alyssa Rosenzweig6627b202020-05-01 18:13:54 -04001176 pan_block_add_successor(&before_block->base, &then_branch->branch_target->base); /* then_branch */
Alyssa Rosenzweig9b75f412020-03-11 14:35:38 -04001177 pan_block_add_successor(&before_block->base, &then_block->base); /* fallthrough */
Alyssa Rosenzweig9a00cf32020-03-05 16:45:16 -05001178}
1179
Alyssa Rosenzweig987aea12020-03-05 17:03:53 -05001180static void
1181emit_loop(bi_context *ctx, nir_loop *nloop)
1182{
1183 /* Remember where we are */
1184 bi_block *start_block = ctx->current_block;
1185
1186 bi_block *saved_break = ctx->break_block;
1187 bi_block *saved_continue = ctx->continue_block;
1188
1189 ctx->continue_block = create_empty_block(ctx);
1190 ctx->break_block = create_empty_block(ctx);
1191 ctx->after_block = ctx->continue_block;
1192
1193 /* Emit the body itself */
1194 emit_cf_list(ctx, &nloop->body);
1195
1196 /* Branch back to loop back */
1197 bi_instruction *br_back = bi_emit_branch(ctx);
Alyssa Rosenzweig6627b202020-05-01 18:13:54 -04001198 br_back->branch_target = ctx->continue_block;
Alyssa Rosenzweig9b75f412020-03-11 14:35:38 -04001199 pan_block_add_successor(&start_block->base, &ctx->continue_block->base);
1200 pan_block_add_successor(&ctx->current_block->base, &ctx->continue_block->base);
Alyssa Rosenzweig987aea12020-03-05 17:03:53 -05001201
1202 ctx->after_block = ctx->break_block;
1203
1204 /* Pop off */
1205 ctx->break_block = saved_break;
1206 ctx->continue_block = saved_continue;
1207 ++ctx->loop_count;
1208}
1209
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -05001210static bi_block *
1211emit_cf_list(bi_context *ctx, struct exec_list *list)
1212{
1213 bi_block *start_block = NULL;
1214
1215 foreach_list_typed(nir_cf_node, node, node, list) {
1216 switch (node->type) {
1217 case nir_cf_node_block: {
1218 bi_block *block = emit_block(ctx, nir_cf_node_as_block(node));
1219
1220 if (!start_block)
1221 start_block = block;
1222
1223 break;
1224 }
1225
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -05001226 case nir_cf_node_if:
1227 emit_if(ctx, nir_cf_node_as_if(node));
1228 break;
1229
1230 case nir_cf_node_loop:
1231 emit_loop(ctx, nir_cf_node_as_loop(node));
1232 break;
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -05001233
1234 default:
1235 unreachable("Unknown control flow");
1236 }
1237 }
1238
1239 return start_block;
1240}
Alyssa Rosenzweigeceaea42020-03-02 19:47:11 -05001241
Alyssa Rosenzweig0d291842020-03-05 10:11:39 -05001242static int
1243glsl_type_size(const struct glsl_type *type, bool bindless)
1244{
1245 return glsl_count_attribute_slots(type, false);
1246}
1247
1248static void
1249bi_optimize_nir(nir_shader *nir)
1250{
1251 bool progress;
1252 unsigned lower_flrp = 16 | 32 | 64;
1253
1254 NIR_PASS(progress, nir, nir_lower_regs_to_ssa);
1255 NIR_PASS(progress, nir, nir_lower_idiv, nir_lower_idiv_fast);
1256
1257 nir_lower_tex_options lower_tex_options = {
1258 .lower_txs_lod = true,
1259 .lower_txp = ~0,
1260 .lower_tex_without_implicit_lod = true,
1261 .lower_txd = true,
1262 };
1263
1264 NIR_PASS(progress, nir, nir_lower_tex, &lower_tex_options);
Alyssa Rosenzweig51e537c2020-03-06 16:29:35 -05001265 NIR_PASS(progress, nir, nir_lower_alu_to_scalar, NULL, NULL);
1266 NIR_PASS(progress, nir, nir_lower_load_const_to_scalar);
Alyssa Rosenzweig0d291842020-03-05 10:11:39 -05001267
1268 do {
1269 progress = false;
1270
1271 NIR_PASS(progress, nir, nir_lower_var_copies);
1272 NIR_PASS(progress, nir, nir_lower_vars_to_ssa);
1273
1274 NIR_PASS(progress, nir, nir_copy_prop);
1275 NIR_PASS(progress, nir, nir_opt_remove_phis);
1276 NIR_PASS(progress, nir, nir_opt_dce);
1277 NIR_PASS(progress, nir, nir_opt_dead_cf);
1278 NIR_PASS(progress, nir, nir_opt_cse);
1279 NIR_PASS(progress, nir, nir_opt_peephole_select, 64, false, true);
1280 NIR_PASS(progress, nir, nir_opt_algebraic);
1281 NIR_PASS(progress, nir, nir_opt_constant_folding);
1282
1283 if (lower_flrp != 0) {
1284 bool lower_flrp_progress = false;
1285 NIR_PASS(lower_flrp_progress,
1286 nir,
1287 nir_lower_flrp,
1288 lower_flrp,
Marek Olšákac55b1a2020-07-22 22:13:16 -04001289 false /* always_precise */);
Alyssa Rosenzweig0d291842020-03-05 10:11:39 -05001290 if (lower_flrp_progress) {
1291 NIR_PASS(progress, nir,
1292 nir_opt_constant_folding);
1293 progress = true;
1294 }
1295
1296 /* Nothing should rematerialize any flrps, so we only
1297 * need to do this lowering once.
1298 */
1299 lower_flrp = 0;
1300 }
1301
1302 NIR_PASS(progress, nir, nir_opt_undef);
1303 NIR_PASS(progress, nir, nir_opt_loop_unroll,
1304 nir_var_shader_in |
1305 nir_var_shader_out |
1306 nir_var_function_temp);
1307 } while (progress);
1308
1309 NIR_PASS(progress, nir, nir_opt_algebraic_late);
Alyssa Rosenzweig12299de2020-03-21 17:37:47 -04001310 NIR_PASS(progress, nir, nir_lower_bool_to_int32);
Alyssa Rosenzweig3a1baaf2020-03-10 08:20:59 -04001311 NIR_PASS(progress, nir, bifrost_nir_lower_algebraic_late);
Alyssa Rosenzweig51e537c2020-03-06 16:29:35 -05001312 NIR_PASS(progress, nir, nir_lower_alu_to_scalar, NULL, NULL);
1313 NIR_PASS(progress, nir, nir_lower_load_const_to_scalar);
Alyssa Rosenzweig0d291842020-03-05 10:11:39 -05001314
1315 /* Take us out of SSA */
1316 NIR_PASS(progress, nir, nir_lower_locals_to_regs);
Alyssa Rosenzweig330e9a62020-03-09 19:56:35 -04001317 NIR_PASS(progress, nir, nir_move_vec_src_uses_to_dest);
Alyssa Rosenzweige0a51d52020-03-22 17:31:23 -04001318 NIR_PASS(progress, nir, nir_convert_from_ssa, true);
Alyssa Rosenzweig50d3f4d2020-03-19 17:21:49 -04001319}
1320
Alyssa Rosenzweigeceaea42020-03-02 19:47:11 -05001321void
Alyssa Rosenzweige6f5ae82020-03-10 16:09:44 -04001322bifrost_compile_shader_nir(nir_shader *nir, panfrost_program *program, unsigned product_id)
Alyssa Rosenzweigeceaea42020-03-02 19:47:11 -05001323{
Tomeu Vizoso07b31f32020-04-30 09:29:10 +02001324 bifrost_debug = debug_get_option_bifrost_debug();
1325
Alyssa Rosenzweigeceaea42020-03-02 19:47:11 -05001326 bi_context *ctx = rzalloc(NULL, bi_context);
1327 ctx->nir = nir;
Alyssa Rosenzweig0d291842020-03-05 10:11:39 -05001328 ctx->stage = nir->info.stage;
Alyssa Rosenzweig0b26cb12020-03-03 14:27:05 -05001329 ctx->quirks = bifrost_get_quirks(product_id);
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -05001330 list_inithead(&ctx->blocks);
Alyssa Rosenzweigeceaea42020-03-02 19:47:11 -05001331
Alyssa Rosenzweig0d291842020-03-05 10:11:39 -05001332 /* Lower gl_Position pre-optimisation, but after lowering vars to ssa
1333 * (so we don't accidentally duplicate the epilogue since mesa/st has
1334 * messed with our I/O quite a bit already) */
1335
1336 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
1337
1338 if (ctx->stage == MESA_SHADER_VERTEX) {
1339 NIR_PASS_V(nir, nir_lower_viewport_transform);
1340 NIR_PASS_V(nir, nir_lower_point_size, 1.0, 1024.0);
1341 }
1342
1343 NIR_PASS_V(nir, nir_split_var_copies);
1344 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
1345 NIR_PASS_V(nir, nir_lower_var_copies);
1346 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
Jason Ekstrandb019b222020-06-10 17:54:25 -05001347 NIR_PASS_V(nir, nir_lower_io, nir_var_shader_in | nir_var_shader_out,
1348 glsl_type_size, 0);
Alyssa Rosenzweig0d291842020-03-05 10:11:39 -05001349 NIR_PASS_V(nir, nir_lower_ssbo);
Alyssa Rosenzweig9c7d30f2020-04-30 09:27:36 +02001350 NIR_PASS_V(nir, nir_lower_mediump_outputs);
Alyssa Rosenzweig0d291842020-03-05 10:11:39 -05001351
Alyssa Rosenzweig0d291842020-03-05 10:11:39 -05001352 bi_optimize_nir(nir);
Tomeu Vizoso07b31f32020-04-30 09:29:10 +02001353
1354 if (bifrost_debug & BIFROST_DBG_SHADERS) {
1355 nir_print_shader(nir, stdout);
1356 }
Alyssa Rosenzweigeceaea42020-03-02 19:47:11 -05001357
Alyssa Rosenzweig680fb052020-08-18 08:31:42 -04001358 panfrost_nir_assign_sysvals(&ctx->sysvals, ctx, nir);
Alyssa Rosenzweig218785c2020-03-10 16:20:18 -04001359 program->sysval_count = ctx->sysvals.sysval_count;
1360 memcpy(program->sysvals, ctx->sysvals.sysvals, sizeof(ctx->sysvals.sysvals[0]) * ctx->sysvals.sysval_count);
Alyssa Rosenzweig1a8f1a32020-04-23 19:26:01 -04001361 ctx->blend_types = program->blend_types;
Alyssa Rosenzweig218785c2020-03-10 16:20:18 -04001362
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -05001363 nir_foreach_function(func, nir) {
1364 if (!func->impl)
1365 continue;
1366
Alyssa Rosenzweigd86659c2020-03-06 09:43:43 -05001367 ctx->impl = func->impl;
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -05001368 emit_cf_list(ctx, &func->impl->body);
1369 break; /* TODO: Multi-function shaders */
1370 }
1371
Alyssa Rosenzweigc6979922020-05-28 14:44:33 -04001372 unsigned block_source_count = 0;
1373
Alyssa Rosenzweig50d3f4d2020-03-19 17:21:49 -04001374 bi_foreach_block(ctx, _block) {
1375 bi_block *block = (bi_block *) _block;
Alyssa Rosenzweigc6979922020-05-28 14:44:33 -04001376
1377 /* Name blocks now that we're done emitting so the order is
1378 * consistent */
1379 block->base.name = block_source_count++;
1380
Alyssa Rosenzweige0a51d52020-03-22 17:31:23 -04001381 bi_lower_combine(ctx, block);
Alyssa Rosenzweig50d3f4d2020-03-19 17:21:49 -04001382 }
1383
Alyssa Rosenzweig58f91712020-03-11 15:10:32 -04001384 bool progress = false;
1385
1386 do {
1387 progress = false;
1388
1389 bi_foreach_block(ctx, _block) {
1390 bi_block *block = (bi_block *) _block;
1391 progress |= bi_opt_dead_code_eliminate(ctx, block);
1392 }
1393 } while(progress);
1394
Tomeu Vizoso07b31f32020-04-30 09:29:10 +02001395 if (bifrost_debug & BIFROST_DBG_SHADERS)
1396 bi_print_shader(ctx, stdout);
Alyssa Rosenzweigb329f8c2020-03-06 19:25:00 -05001397 bi_schedule(ctx);
Alyssa Rosenzweige8139ef2020-03-11 20:39:36 -04001398 bi_register_allocate(ctx);
Tomeu Vizoso07b31f32020-04-30 09:29:10 +02001399 if (bifrost_debug & BIFROST_DBG_SHADERS)
1400 bi_print_shader(ctx, stdout);
Alyssa Rosenzweig9269c852020-03-12 14:16:22 -04001401 bi_pack(ctx, &program->compiled);
Tomeu Vizoso07b31f32020-04-30 09:29:10 +02001402
1403 if (bifrost_debug & BIFROST_DBG_SHADERS)
1404 disassemble_bifrost(stdout, program->compiled.data, program->compiled.size, true);
Alyssa Rosenzweig0d291842020-03-05 10:11:39 -05001405
Alyssa Rosenzweigeceaea42020-03-02 19:47:11 -05001406 ralloc_free(ctx);
1407}