blob: 203dc566ed202ac1ac95bff62de81da0f7cc29d1 [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;
Alyssa Rosenzweig7cc55df2020-10-02 12:02:36 -0400192 ld.format = instr->intrinsic == nir_intrinsic_store_output ?
193 nir_intrinsic_src_type(instr) :
194 nir_intrinsic_dest_type(instr);
Alyssa Rosenzweig1097c692020-03-21 15:25:54 -0400195 return ld;
196}
197
Alyssa Rosenzweigdabb6c62020-03-06 09:26:44 -0500198static void
Alyssa Rosenzweig48910e82020-03-06 09:44:19 -0500199bi_emit_st_vary(bi_context *ctx, nir_intrinsic_instr *instr)
200{
Alyssa Rosenzweig1097c692020-03-21 15:25:54 -0400201 bi_instruction address = bi_load_with_r61(BI_LOAD_VAR_ADDRESS, instr);
Alyssa Rosenzweig69c66ff2020-03-09 19:52:56 -0400202 address.dest = bi_make_temp(ctx);
Alyssa Rosenzweig9458b012020-03-20 12:25:08 -0400203 address.dest_type = nir_type_uint32;
Alyssa Rosenzweigb2c6cf22020-04-24 17:20:28 -0400204 address.vector_channels = 3;
Alyssa Rosenzweig48910e82020-03-06 09:44:19 -0500205
Alyssa Rosenzweigaba7f092020-04-14 20:20:37 -0400206 unsigned nr = nir_intrinsic_src_components(instr, 0);
207 assert(nir_intrinsic_write_mask(instr) == ((1 << nr) - 1));
208
Alyssa Rosenzweig48910e82020-03-06 09:44:19 -0500209 bi_instruction st = {
210 .type = BI_STORE_VAR,
211 .src = {
Alyssa Rosenzweigfbbe3d42020-04-27 16:04:05 -0400212 pan_src_index(&instr->src[0]),
Alyssa Rosenzweig9458b012020-03-20 12:25:08 -0400213 address.dest, address.dest, address.dest,
Alyssa Rosenzweig795646d2020-03-09 14:09:04 -0400214 },
Alyssa Rosenzweig116c5412020-03-11 21:45:32 -0400215 .src_types = {
Alyssa Rosenzweig9458b012020-03-20 12:25:08 -0400216 nir_type_uint32,
217 nir_type_uint32, nir_type_uint32, nir_type_uint32,
Alyssa Rosenzweig116c5412020-03-11 21:45:32 -0400218 },
Alyssa Rosenzweig795646d2020-03-09 14:09:04 -0400219 .swizzle = {
Alyssa Rosenzweigaba7f092020-04-14 20:20:37 -0400220 { 0 },
Alyssa Rosenzweig9458b012020-03-20 12:25:08 -0400221 { 0 }, { 1 }, { 2}
Alyssa Rosenzweig9213b252020-03-20 12:38:53 -0400222 },
Alyssa Rosenzweigb2c6cf22020-04-24 17:20:28 -0400223 .vector_channels = nr,
Alyssa Rosenzweig48910e82020-03-06 09:44:19 -0500224 };
225
Alyssa Rosenzweigaba7f092020-04-14 20:20:37 -0400226 for (unsigned i = 0; i < nr; ++i)
227 st.swizzle[0][i] = i;
228
Alyssa Rosenzweig48910e82020-03-06 09:44:19 -0500229 bi_emit(ctx, address);
230 bi_emit(ctx, st);
231}
232
233static void
Alyssa Rosenzweig1ead0d32020-03-06 09:52:09 -0500234bi_emit_ld_uniform(bi_context *ctx, nir_intrinsic_instr *instr)
235{
Alyssa Rosenzweig69c66ff2020-03-09 19:52:56 -0400236 bi_instruction ld = bi_load(BI_LOAD_UNIFORM, instr);
237 ld.src[1] = BIR_INDEX_ZERO; /* TODO: UBO index */
Alyssa Rosenzweig6f5b7882020-07-31 17:29:50 -0400238 ld.segment = BI_SEGMENT_UBO;
Alyssa Rosenzweig218785c2020-03-10 16:20:18 -0400239
240 /* TODO: Indirect access, since we need to multiply by the element
241 * size. I believe we can get this lowering automatically via
242 * nir_lower_io (as mul instructions) with the proper options, but this
243 * is TODO */
244 assert(ld.src[0] & BIR_INDEX_CONSTANT);
245 ld.constant.u64 += ctx->sysvals.sysval_count;
246 ld.constant.u64 *= 16;
247
Alyssa Rosenzweig1ead0d32020-03-06 09:52:09 -0500248 bi_emit(ctx, ld);
249}
250
251static void
Alyssa Rosenzweig218785c2020-03-10 16:20:18 -0400252bi_emit_sysval(bi_context *ctx, nir_instr *instr,
253 unsigned nr_components, unsigned offset)
254{
255 nir_dest nir_dest;
256
257 /* Figure out which uniform this is */
258 int sysval = panfrost_sysval_for_instr(instr, &nir_dest);
259 void *val = _mesa_hash_table_u64_search(ctx->sysvals.sysval_to_id, sysval);
260
261 /* Sysvals are prefix uniforms */
262 unsigned uniform = ((uintptr_t) val) - 1;
263
264 /* Emit the read itself -- this is never indirect */
265
266 bi_instruction load = {
267 .type = BI_LOAD_UNIFORM,
Alyssa Rosenzweig6f5b7882020-07-31 17:29:50 -0400268 .segment = BI_SEGMENT_UBO,
Alyssa Rosenzweigb2c6cf22020-04-24 17:20:28 -0400269 .vector_channels = nr_components,
Alyssa Rosenzweig8bb16132020-03-20 11:38:21 -0400270 .src = { BIR_INDEX_CONSTANT, BIR_INDEX_ZERO },
Alyssa Rosenzweigdf693042020-04-14 20:09:00 -0400271 .src_types = { nir_type_uint32, nir_type_uint32 },
Alyssa Rosenzweig218785c2020-03-10 16:20:18 -0400272 .constant = { (uniform * 16) + offset },
Alyssa Rosenzweigfbbe3d42020-04-27 16:04:05 -0400273 .dest = pan_dest_index(&nir_dest),
Alyssa Rosenzweig218785c2020-03-10 16:20:18 -0400274 .dest_type = nir_type_uint32, /* TODO */
275 };
276
277 bi_emit(ctx, load);
278}
279
Alyssa Rosenzweig47c84ee2020-05-01 14:55:04 -0400280/* gl_FragCoord.xy = u16_to_f32(R59.xy) + 0.5
281 * gl_FragCoord.z = ld_vary(fragz)
282 * gl_FragCoord.w = ld_vary(fragw)
283 */
284
285static void
286bi_emit_ld_frag_coord(bi_context *ctx, nir_intrinsic_instr *instr)
287{
288 /* Future proofing for mediump fragcoord at some point.. */
289 nir_alu_type T = nir_type_float32;
290
291 /* First, sketch a combine */
292 bi_instruction combine = {
293 .type = BI_COMBINE,
294 .dest_type = nir_type_uint32,
295 .dest = pan_dest_index(&instr->dest),
296 .src_types = { T, T, T, T },
297 };
298
299 /* Second, handle xy */
300 for (unsigned i = 0; i < 2; ++i) {
301 bi_instruction conv = {
302 .type = BI_CONVERT,
303 .dest_type = T,
304 .dest = bi_make_temp(ctx),
305 .src = {
306 /* TODO: RA XXX */
307 BIR_INDEX_REGISTER | 59
308 },
309 .src_types = { nir_type_uint16 },
310 .swizzle = { { i } }
311 };
312
313 bi_instruction add = {
314 .type = BI_ADD,
315 .dest_type = T,
316 .dest = bi_make_temp(ctx),
317 .src = { conv.dest, BIR_INDEX_CONSTANT },
318 .src_types = { T, T },
319 };
320
321 float half = 0.5;
322 memcpy(&add.constant.u32, &half, sizeof(float));
323
324 bi_emit(ctx, conv);
325 bi_emit(ctx, add);
326
327 combine.src[i] = add.dest;
328 }
329
330 /* Third, zw */
331 for (unsigned i = 0; i < 2; ++i) {
332 bi_instruction load = {
333 .type = BI_LOAD_VAR,
334 .load_vary = {
335 .interp_mode = BIFROST_INTERP_DEFAULT,
336 .reuse = false,
337 .flat = true
338 },
339 .vector_channels = 1,
340 .dest_type = nir_type_float32,
Alyssa Rosenzweig8dd3a812020-07-31 18:48:27 -0400341 .format = nir_type_float32,
Alyssa Rosenzweig47c84ee2020-05-01 14:55:04 -0400342 .dest = bi_make_temp(ctx),
Alyssa Rosenzweig2ff53872020-08-03 12:48:44 -0400343 .src = {
344 BIR_INDEX_CONSTANT,
345 BIR_INDEX_PASS | BIFROST_SRC_CONST_LO
346 },
Alyssa Rosenzweig47c84ee2020-05-01 14:55:04 -0400347 .src_types = { nir_type_uint32, nir_type_uint32 },
348 .constant = {
349 .u32 = (i == 0) ? BIFROST_FRAGZ : BIFROST_FRAGW
350 }
351 };
352
353 bi_emit(ctx, load);
354
355 combine.src[i + 2] = load.dest;
356 }
357
358 /* Finally, emit the combine */
359 bi_emit(ctx, combine);
360}
361
Alyssa Rosenzweig218785c2020-03-10 16:20:18 -0400362static void
Alyssa Rosenzweig7d867f72020-05-01 18:26:18 -0400363bi_emit_discard(bi_context *ctx, nir_intrinsic_instr *instr)
364{
365 /* Goofy lowering */
366 bi_instruction discard = {
367 .type = BI_DISCARD,
368 .cond = BI_COND_EQ,
369 .src_types = { nir_type_uint32, nir_type_uint32 },
370 .src = { BIR_INDEX_ZERO, BIR_INDEX_ZERO },
371 };
372
373 bi_emit(ctx, discard);
374}
375
376static void
Alyssa Rosenzweig8ab5c972020-05-01 18:36:42 -0400377bi_fuse_cond(bi_instruction *csel, nir_alu_src cond,
378 unsigned *constants_left, unsigned *constant_shift,
379 unsigned comps, bool float_only);
380
381static void
Alyssa Rosenzweigc9ab7322020-05-01 18:24:11 -0400382bi_emit_discard_if(bi_context *ctx, nir_intrinsic_instr *instr)
383{
384 nir_src cond = instr->src[0];
385 nir_alu_type T = nir_type_uint | nir_src_bit_size(cond);
386
387 bi_instruction discard = {
388 .type = BI_DISCARD,
389 .cond = BI_COND_NE,
390 .src_types = { T, T },
391 .src = {
392 pan_src_index(&cond),
393 BIR_INDEX_ZERO
394 },
395 };
396
Alyssa Rosenzweig8ab5c972020-05-01 18:36:42 -0400397 /* Try to fuse in the condition */
398 unsigned constants_left = 1, constant_shift = 0;
399
400 /* Scalar so no swizzle */
401 nir_alu_src wrap = {
402 .src = instr->src[0]
403 };
404
405 /* May or may not succeed but we're optimistic */
406 bi_fuse_cond(&discard, wrap, &constants_left, &constant_shift, 1, true);
407
Alyssa Rosenzweigc9ab7322020-05-01 18:24:11 -0400408 bi_emit(ctx, discard);
409}
410
411static void
Alyssa Rosenzweig07671822020-03-05 17:50:18 -0500412emit_intrinsic(bi_context *ctx, nir_intrinsic_instr *instr)
413{
414
415 switch (instr->intrinsic) {
416 case nir_intrinsic_load_barycentric_pixel:
417 /* stub */
418 break;
419 case nir_intrinsic_load_interpolated_input:
Alyssa Rosenzweig59b476e2020-03-06 09:33:52 -0500420 case nir_intrinsic_load_input:
421 if (ctx->stage == MESA_SHADER_FRAGMENT)
422 bi_emit_ld_vary(ctx, instr);
423 else if (ctx->stage == MESA_SHADER_VERTEX)
Alyssa Rosenzweig1097c692020-03-21 15:25:54 -0400424 bi_emit(ctx, bi_load_with_r61(BI_LOAD_ATTR, instr));
Alyssa Rosenzweig59b476e2020-03-06 09:33:52 -0500425 else {
426 unreachable("Unsupported shader stage");
427 }
Alyssa Rosenzweig07671822020-03-05 17:50:18 -0500428 break;
Alyssa Rosenzweig59b476e2020-03-06 09:33:52 -0500429
Alyssa Rosenzweigdabb6c62020-03-06 09:26:44 -0500430 case nir_intrinsic_store_output:
431 if (ctx->stage == MESA_SHADER_FRAGMENT)
432 bi_emit_frag_out(ctx, instr);
Alyssa Rosenzweig48910e82020-03-06 09:44:19 -0500433 else if (ctx->stage == MESA_SHADER_VERTEX)
434 bi_emit_st_vary(ctx, instr);
435 else
436 unreachable("Unsupported shader stage");
Alyssa Rosenzweigdabb6c62020-03-06 09:26:44 -0500437 break;
Alyssa Rosenzweig1ead0d32020-03-06 09:52:09 -0500438
439 case nir_intrinsic_load_uniform:
440 bi_emit_ld_uniform(ctx, instr);
441 break;
442
Alyssa Rosenzweig47c84ee2020-05-01 14:55:04 -0400443 case nir_intrinsic_load_frag_coord:
444 bi_emit_ld_frag_coord(ctx, instr);
445 break;
446
Alyssa Rosenzweig7d867f72020-05-01 18:26:18 -0400447 case nir_intrinsic_discard:
448 bi_emit_discard(ctx, instr);
449 break;
450
Alyssa Rosenzweigc9ab7322020-05-01 18:24:11 -0400451 case nir_intrinsic_discard_if:
452 bi_emit_discard_if(ctx, instr);
453 break;
454
Alyssa Rosenzweig218785c2020-03-10 16:20:18 -0400455 case nir_intrinsic_load_ssbo_address:
456 bi_emit_sysval(ctx, &instr->instr, 1, 0);
457 break;
458
Jason Ekstrand97501642020-09-22 03:24:45 -0500459 case nir_intrinsic_get_ssbo_size:
Alyssa Rosenzweig218785c2020-03-10 16:20:18 -0400460 bi_emit_sysval(ctx, &instr->instr, 1, 8);
461 break;
462
463 case nir_intrinsic_load_viewport_scale:
464 case nir_intrinsic_load_viewport_offset:
465 case nir_intrinsic_load_num_work_groups:
466 case nir_intrinsic_load_sampler_lod_parameters_pan:
467 bi_emit_sysval(ctx, &instr->instr, 3, 0);
468 break;
469
Alyssa Rosenzweig07671822020-03-05 17:50:18 -0500470 default:
Alyssa Rosenzweigc4883902020-05-01 14:13:10 -0400471 unreachable("Unknown intrinsic");
Alyssa Rosenzweig07671822020-03-05 17:50:18 -0500472 break;
473 }
474}
475
476static void
Alyssa Rosenzweig51e537c2020-03-06 16:29:35 -0500477emit_load_const(bi_context *ctx, nir_load_const_instr *instr)
478{
479 /* Make sure we've been lowered */
Alyssa Rosenzweig0e73d872020-06-02 19:30:56 -0400480 assert(instr->def.num_components <= (32 / instr->def.bit_size));
481
482 /* Accumulate all the channels of the constant, as if we did an
483 * implicit SEL over them */
484 uint32_t acc = 0;
485
486 for (unsigned i = 0; i < instr->def.num_components; ++i) {
487 unsigned v = nir_const_value_as_uint(instr->value[i], instr->def.bit_size);
488 acc |= (v << (i * instr->def.bit_size));
489 }
Alyssa Rosenzweig51e537c2020-03-06 16:29:35 -0500490
491 bi_instruction move = {
492 .type = BI_MOV,
Alyssa Rosenzweigfbbe3d42020-04-27 16:04:05 -0400493 .dest = pan_ssa_index(&instr->def),
Alyssa Rosenzweig0e73d872020-06-02 19:30:56 -0400494 .dest_type = nir_type_uint32,
Alyssa Rosenzweig51e537c2020-03-06 16:29:35 -0500495 .src = {
496 BIR_INDEX_CONSTANT
497 },
Alyssa Rosenzweig02ad1472020-03-30 20:54:51 -0400498 .src_types = {
Alyssa Rosenzweig0e73d872020-06-02 19:30:56 -0400499 nir_type_uint32,
Alyssa Rosenzweig02ad1472020-03-30 20:54:51 -0400500 },
Alyssa Rosenzweig51e537c2020-03-06 16:29:35 -0500501 .constant = {
Alyssa Rosenzweig0e73d872020-06-02 19:30:56 -0400502 .u32 = acc
Alyssa Rosenzweig51e537c2020-03-06 16:29:35 -0500503 }
504 };
505
506 bi_emit(ctx, move);
507}
508
Alyssa Rosenzweig12299de2020-03-21 17:37:47 -0400509#define BI_CASE_CMP(op) \
510 case op##8: \
511 case op##16: \
512 case op##32: \
513
Alyssa Rosenzweig929baf32020-03-09 20:19:51 -0400514static enum bi_class
515bi_class_for_nir_alu(nir_op op)
516{
517 switch (op) {
Alyssa Rosenzweigc8622342020-03-09 21:10:41 -0400518 case nir_op_fadd:
Alyssa Rosenzweigacab7882020-03-10 07:56:14 -0400519 case nir_op_fsub:
Alyssa Rosenzweigc8622342020-03-09 21:10:41 -0400520 return BI_ADD;
Alyssa Rosenzweigcf3c3562020-05-04 14:04:35 -0400521
522 case nir_op_iadd:
Alyssa Rosenzweig55f0d812020-03-10 08:03:20 -0400523 case nir_op_isub:
Alyssa Rosenzweig1a94dae2020-05-04 14:00:13 -0400524 return BI_IMATH;
Alyssa Rosenzweigc8622342020-03-09 21:10:41 -0400525
Chris Forbesa0a70872020-07-26 15:54:14 -0700526 case nir_op_imul:
527 return BI_IMUL;
528
Alyssa Rosenzweiga077da62020-04-28 14:36:17 -0400529 case nir_op_iand:
530 case nir_op_ior:
531 case nir_op_ixor:
Chris Forbes539ea082020-07-26 11:37:42 -0700532 case nir_op_inot:
Chris Forbes946ff9b2020-07-26 12:18:54 -0700533 case nir_op_ishl:
Alyssa Rosenzweiga077da62020-04-28 14:36:17 -0400534 return BI_BITWISE;
535
Alyssa Rosenzweig12299de2020-03-21 17:37:47 -0400536 BI_CASE_CMP(nir_op_flt)
537 BI_CASE_CMP(nir_op_fge)
538 BI_CASE_CMP(nir_op_feq)
Karol Herbste5899c12020-08-18 19:51:57 +0200539 BI_CASE_CMP(nir_op_fneu)
Alyssa Rosenzweig12299de2020-03-21 17:37:47 -0400540 BI_CASE_CMP(nir_op_ilt)
541 BI_CASE_CMP(nir_op_ige)
542 BI_CASE_CMP(nir_op_ieq)
543 BI_CASE_CMP(nir_op_ine)
Chris Forbes718d4442020-07-26 12:41:17 -0700544 BI_CASE_CMP(nir_op_uge)
Alyssa Rosenzweig05413502020-03-10 08:21:35 -0400545 return BI_CMP;
546
Alyssa Rosenzweig12299de2020-03-21 17:37:47 -0400547 case nir_op_b8csel:
548 case nir_op_b16csel:
549 case nir_op_b32csel:
Alyssa Rosenzweigd3823552020-03-10 08:32:56 -0400550 return BI_CSEL;
551
Alyssa Rosenzweigc8622342020-03-09 21:10:41 -0400552 case nir_op_i2i8:
553 case nir_op_i2i16:
554 case nir_op_i2i32:
555 case nir_op_i2i64:
556 case nir_op_u2u8:
557 case nir_op_u2u16:
558 case nir_op_u2u32:
559 case nir_op_u2u64:
560 case nir_op_f2i16:
561 case nir_op_f2i32:
562 case nir_op_f2i64:
563 case nir_op_f2u16:
564 case nir_op_f2u32:
565 case nir_op_f2u64:
566 case nir_op_i2f16:
567 case nir_op_i2f32:
568 case nir_op_i2f64:
569 case nir_op_u2f16:
570 case nir_op_u2f32:
571 case nir_op_u2f64:
Alyssa Rosenzweigaa77d812020-03-27 14:40:04 -0400572 case nir_op_f2f16:
573 case nir_op_f2f32:
574 case nir_op_f2f64:
575 case nir_op_f2fmp:
Alyssa Rosenzweigc8622342020-03-09 21:10:41 -0400576 return BI_CONVERT;
577
Alyssa Rosenzweige0a51d52020-03-22 17:31:23 -0400578 case nir_op_vec2:
579 case nir_op_vec3:
580 case nir_op_vec4:
581 return BI_COMBINE;
582
583 case nir_op_vec8:
584 case nir_op_vec16:
585 unreachable("should've been lowered");
586
Alyssa Rosenzweigf6d96aa2020-03-11 15:15:41 -0400587 case nir_op_ffma:
Alyssa Rosenzweigc8622342020-03-09 21:10:41 -0400588 case nir_op_fmul:
589 return BI_FMA;
590
591 case nir_op_imin:
592 case nir_op_imax:
593 case nir_op_umin:
594 case nir_op_umax:
595 case nir_op_fmin:
596 case nir_op_fmax:
597 return BI_MINMAX;
598
Alyssa Rosenzweig5a5896c2020-03-09 21:02:51 -0400599 case nir_op_fsat:
Alyssa Rosenzweig1216a632020-03-10 07:52:24 -0400600 case nir_op_fneg:
601 case nir_op_fabs:
Alyssa Rosenzweig6b7077e2020-03-19 16:58:48 -0400602 return BI_FMOV;
Alyssa Rosenzweigc8622342020-03-09 21:10:41 -0400603 case nir_op_mov:
604 return BI_MOV;
605
Alyssa Rosenzweigf81b67b2020-03-27 20:28:09 -0400606 case nir_op_fround_even:
607 case nir_op_fceil:
608 case nir_op_ffloor:
609 case nir_op_ftrunc:
610 return BI_ROUND;
611
Alyssa Rosenzweig8ed79c92020-03-09 21:20:20 -0400612 case nir_op_frcp:
613 case nir_op_frsq:
Chris Forbes1882b1e2020-07-27 11:51:31 -0700614 case nir_op_iabs:
Alyssa Rosenzweig8ed79c92020-03-09 21:20:20 -0400615 return BI_SPECIAL;
616
Alyssa Rosenzweigc8622342020-03-09 21:10:41 -0400617 default:
618 unreachable("Unknown ALU op");
Alyssa Rosenzweig929baf32020-03-09 20:19:51 -0400619 }
620}
621
Alyssa Rosenzweig5a02c8712020-03-21 18:12:31 -0400622/* Gets a bi_cond for a given NIR comparison opcode. In soft mode, it will
623 * return BI_COND_ALWAYS as a sentinel if it fails to do so (when used for
624 * optimizations). Otherwise it will bail (when used for primary code
625 * generation). */
626
Alyssa Rosenzweig05413502020-03-10 08:21:35 -0400627static enum bi_cond
Alyssa Rosenzweig5a02c8712020-03-21 18:12:31 -0400628bi_cond_for_nir(nir_op op, bool soft)
Alyssa Rosenzweig05413502020-03-10 08:21:35 -0400629{
630 switch (op) {
Alyssa Rosenzweig12299de2020-03-21 17:37:47 -0400631 BI_CASE_CMP(nir_op_flt)
632 BI_CASE_CMP(nir_op_ilt)
Alyssa Rosenzweig05413502020-03-10 08:21:35 -0400633 return BI_COND_LT;
Alyssa Rosenzweig12299de2020-03-21 17:37:47 -0400634
635 BI_CASE_CMP(nir_op_fge)
636 BI_CASE_CMP(nir_op_ige)
Chris Forbes718d4442020-07-26 12:41:17 -0700637 BI_CASE_CMP(nir_op_uge)
Alyssa Rosenzweig05413502020-03-10 08:21:35 -0400638 return BI_COND_GE;
Alyssa Rosenzweig12299de2020-03-21 17:37:47 -0400639
640 BI_CASE_CMP(nir_op_feq)
641 BI_CASE_CMP(nir_op_ieq)
Alyssa Rosenzweig05413502020-03-10 08:21:35 -0400642 return BI_COND_EQ;
Alyssa Rosenzweig12299de2020-03-21 17:37:47 -0400643
Karol Herbste5899c12020-08-18 19:51:57 +0200644 BI_CASE_CMP(nir_op_fneu)
Alyssa Rosenzweig12299de2020-03-21 17:37:47 -0400645 BI_CASE_CMP(nir_op_ine)
Alyssa Rosenzweig05413502020-03-10 08:21:35 -0400646 return BI_COND_NE;
647 default:
Alyssa Rosenzweig5a02c8712020-03-21 18:12:31 -0400648 if (soft)
649 return BI_COND_ALWAYS;
650 else
651 unreachable("Invalid compare");
Alyssa Rosenzweig05413502020-03-10 08:21:35 -0400652 }
653}
654
Alyssa Rosenzweig929baf32020-03-09 20:19:51 -0400655static void
Alyssa Rosenzweig3f786ed2020-03-21 18:13:49 -0400656bi_copy_src(bi_instruction *alu, nir_alu_instr *instr, unsigned i, unsigned to,
Alyssa Rosenzweigb2c6cf22020-04-24 17:20:28 -0400657 unsigned *constants_left, unsigned *constant_shift, unsigned comps)
Alyssa Rosenzweig3f786ed2020-03-21 18:13:49 -0400658{
659 unsigned bits = nir_src_bit_size(instr->src[i].src);
660 unsigned dest_bits = nir_dest_bit_size(instr->dest.dest);
661
662 alu->src_types[to] = nir_op_infos[instr->op].input_types[i]
663 | bits;
664
665 /* Try to inline a constant */
666 if (nir_src_is_const(instr->src[i].src) && *constants_left && (dest_bits == bits)) {
Alyssa Rosenzweigd772bf02020-04-15 10:39:42 -0400667 uint64_t mask = (1ull << dest_bits) - 1;
668 uint64_t cons = nir_src_as_uint(instr->src[i].src);
Alyssa Rosenzweig3f786ed2020-03-21 18:13:49 -0400669
Alyssa Rosenzweigd772bf02020-04-15 10:39:42 -0400670 /* Try to reuse a constant */
671 for (unsigned i = 0; i < (*constant_shift); i += dest_bits) {
672 if (((alu->constant.u64 >> i) & mask) == cons) {
673 alu->src[to] = BIR_INDEX_CONSTANT | i;
674 return;
675 }
676 }
677
678 alu->constant.u64 |= cons << *constant_shift;
Alyssa Rosenzweig3f786ed2020-03-21 18:13:49 -0400679 alu->src[to] = BIR_INDEX_CONSTANT | (*constant_shift);
680 --(*constants_left);
Alyssa Rosenzweig4d0f9412020-04-17 15:52:03 -0400681 (*constant_shift) += MAX2(dest_bits, 32); /* lo/hi */
Alyssa Rosenzweig3f786ed2020-03-21 18:13:49 -0400682 return;
683 }
684
Alyssa Rosenzweigfbbe3d42020-04-27 16:04:05 -0400685 alu->src[to] = pan_src_index(&instr->src[i].src);
Alyssa Rosenzweig3f786ed2020-03-21 18:13:49 -0400686
Alyssa Rosenzweigb2c6cf22020-04-24 17:20:28 -0400687 /* Copy swizzle for all vectored components, replicating last component
688 * to fill undersized */
689
690 unsigned vec = alu->type == BI_COMBINE ? 1 :
691 MAX2(1, 32 / dest_bits);
692
693 for (unsigned j = 0; j < vec; ++j)
694 alu->swizzle[to][j] = instr->src[i].swizzle[MIN2(j, comps - 1)];
Alyssa Rosenzweig3f786ed2020-03-21 18:13:49 -0400695}
696
697static void
Alyssa Rosenzweig201a11a2020-05-01 18:31:22 -0400698bi_fuse_cond(bi_instruction *csel, nir_alu_src cond,
699 unsigned *constants_left, unsigned *constant_shift,
700 unsigned comps, bool float_only)
Alyssa Rosenzweig3f786ed2020-03-21 18:13:49 -0400701{
702 /* Bail for vector weirdness */
703 if (cond.swizzle[0] != 0)
704 return;
705
706 if (!cond.src.is_ssa)
707 return;
708
709 nir_ssa_def *def = cond.src.ssa;
710 nir_instr *parent = def->parent_instr;
711
712 if (parent->type != nir_instr_type_alu)
713 return;
714
715 nir_alu_instr *alu = nir_instr_as_alu(parent);
716
717 /* Try to match a condition */
718 enum bi_cond bcond = bi_cond_for_nir(alu->op, true);
719
720 if (bcond == BI_COND_ALWAYS)
721 return;
722
Alyssa Rosenzweig201a11a2020-05-01 18:31:22 -0400723 /* Some instructions can't compare ints */
724 if (float_only) {
725 nir_alu_type T = nir_op_infos[alu->op].input_types[0];
726 T = nir_alu_type_get_base_type(T);
727
728 if (T != nir_type_float)
729 return;
730 }
731
Alyssa Rosenzweig3f786ed2020-03-21 18:13:49 -0400732 /* We found one, let's fuse it in */
Alyssa Rosenzweig95fc71e2020-04-27 14:15:57 -0400733 csel->cond = bcond;
Alyssa Rosenzweigb2c6cf22020-04-24 17:20:28 -0400734 bi_copy_src(csel, alu, 0, 0, constants_left, constant_shift, comps);
735 bi_copy_src(csel, alu, 1, 1, constants_left, constant_shift, comps);
Alyssa Rosenzweig3f786ed2020-03-21 18:13:49 -0400736}
737
738static void
Alyssa Rosenzweig929baf32020-03-09 20:19:51 -0400739emit_alu(bi_context *ctx, nir_alu_instr *instr)
740{
Alyssa Rosenzweig8e522062020-04-14 18:52:21 -0400741 /* Try some special functions */
742 switch (instr->op) {
743 case nir_op_fexp2:
744 bi_emit_fexp2(ctx, instr);
745 return;
Alyssa Rosenzweig031ad0e2020-04-14 19:50:24 -0400746 case nir_op_flog2:
747 bi_emit_flog2(ctx, instr);
748 return;
Alyssa Rosenzweig8e522062020-04-14 18:52:21 -0400749 default:
750 break;
751 }
752
753 /* Otherwise, assume it's something we can handle normally */
Alyssa Rosenzweig929baf32020-03-09 20:19:51 -0400754 bi_instruction alu = {
755 .type = bi_class_for_nir_alu(instr->op),
Alyssa Rosenzweigfbbe3d42020-04-27 16:04:05 -0400756 .dest = pan_dest_index(&instr->dest.dest),
Alyssa Rosenzweig929baf32020-03-09 20:19:51 -0400757 .dest_type = nir_op_infos[instr->op].output_type
758 | nir_dest_bit_size(instr->dest.dest),
759 };
760
Alyssa Rosenzweig8ed79c92020-03-09 21:20:20 -0400761 /* TODO: Implement lowering of special functions for older Bifrost */
762 assert((alu.type != BI_SPECIAL) || !(ctx->quirks & BIFROST_NO_FAST_OP));
763
Alyssa Rosenzweigb2c6cf22020-04-24 17:20:28 -0400764 unsigned comps = nir_dest_num_components(instr->dest.dest);
Alyssa Rosenzweige0a51d52020-03-22 17:31:23 -0400765
Alyssa Rosenzweigb2c6cf22020-04-24 17:20:28 -0400766 if (alu.type != BI_COMBINE)
767 assert(comps <= MAX2(1, 32 / comps));
Alyssa Rosenzweige0a51d52020-03-22 17:31:23 -0400768
Alyssa Rosenzweigb2c6cf22020-04-24 17:20:28 -0400769 if (!instr->dest.dest.is_ssa) {
770 for (unsigned i = 0; i < comps; ++i)
771 assert(instr->dest.write_mask);
Alyssa Rosenzweig929baf32020-03-09 20:19:51 -0400772 }
773
Alyssa Rosenzweig48e50ef2020-03-09 20:32:00 -0400774 /* We inline constants as we go. This tracks how many constants have
775 * been inlined, since we're limited to 64-bits of constants per
776 * instruction */
777
778 unsigned dest_bits = nir_dest_bit_size(instr->dest.dest);
779 unsigned constants_left = (64 / dest_bits);
780 unsigned constant_shift = 0;
781
Alyssa Rosenzweig02ad1472020-03-30 20:54:51 -0400782 if (alu.type == BI_COMBINE)
783 constants_left = 0;
784
Alyssa Rosenzweig929baf32020-03-09 20:19:51 -0400785 /* Copy sources */
786
787 unsigned num_inputs = nir_op_infos[instr->op].num_inputs;
788 assert(num_inputs <= ARRAY_SIZE(alu.src));
789
Alyssa Rosenzweig8eefb272020-04-05 19:22:01 -0400790 for (unsigned i = 0; i < num_inputs; ++i) {
791 unsigned f = 0;
792
793 if (i && alu.type == BI_CSEL)
794 f++;
795
Alyssa Rosenzweigb2c6cf22020-04-24 17:20:28 -0400796 bi_copy_src(&alu, instr, i, i + f, &constants_left, &constant_shift, comps);
Alyssa Rosenzweig8eefb272020-04-05 19:22:01 -0400797 }
Alyssa Rosenzweig929baf32020-03-09 20:19:51 -0400798
799 /* Op-specific fixup */
800 switch (instr->op) {
801 case nir_op_fmul:
802 alu.src[2] = BIR_INDEX_ZERO; /* FMA */
Alyssa Rosenzweigb5148b62020-03-27 15:53:12 -0400803 alu.src_types[2] = alu.src_types[1];
Alyssa Rosenzweig929baf32020-03-09 20:19:51 -0400804 break;
Alyssa Rosenzweig5a5896c2020-03-09 21:02:51 -0400805 case nir_op_fsat:
Alyssa Rosenzweig6b7077e2020-03-19 16:58:48 -0400806 alu.outmod = BIFROST_SAT; /* FMOV */
Alyssa Rosenzweig5a5896c2020-03-09 21:02:51 -0400807 break;
Alyssa Rosenzweig1216a632020-03-10 07:52:24 -0400808 case nir_op_fneg:
Alyssa Rosenzweig6b7077e2020-03-19 16:58:48 -0400809 alu.src_neg[0] = true; /* FMOV */
Alyssa Rosenzweig1216a632020-03-10 07:52:24 -0400810 break;
811 case nir_op_fabs:
Alyssa Rosenzweig6b7077e2020-03-19 16:58:48 -0400812 alu.src_abs[0] = true; /* FMOV */
Alyssa Rosenzweig1216a632020-03-10 07:52:24 -0400813 break;
Alyssa Rosenzweigacab7882020-03-10 07:56:14 -0400814 case nir_op_fsub:
Alyssa Rosenzweig6b7077e2020-03-19 16:58:48 -0400815 alu.src_neg[1] = true; /* FADD */
Alyssa Rosenzweigacab7882020-03-10 07:56:14 -0400816 break;
Alyssa Rosenzweigcf3c3562020-05-04 14:04:35 -0400817 case nir_op_iadd:
818 alu.op.imath = BI_IMATH_ADD;
Alyssa Rosenzweig33710ff2020-07-31 16:47:05 -0400819 /* Carry */
820 alu.src[2] = BIR_INDEX_ZERO;
Alyssa Rosenzweigcf3c3562020-05-04 14:04:35 -0400821 break;
822 case nir_op_isub:
823 alu.op.imath = BI_IMATH_SUB;
Alyssa Rosenzweig33710ff2020-07-31 16:47:05 -0400824 /* Borrow */
825 alu.src[2] = BIR_INDEX_ZERO;
Alyssa Rosenzweigcf3c3562020-05-04 14:04:35 -0400826 break;
Chris Forbes1882b1e2020-07-27 11:51:31 -0700827 case nir_op_iabs:
828 alu.op.special = BI_SPECIAL_IABS;
829 break;
Chris Forbes539ea082020-07-26 11:37:42 -0700830 case nir_op_inot:
Alyssa Rosenzweigd2158a52020-09-09 17:46:58 -0400831 /* no dedicated bitwise not, but we can invert sources. convert to ~(a | 0) */
Chris Forbes539ea082020-07-26 11:37:42 -0700832 alu.op.bitwise = BI_BITWISE_OR;
Alyssa Rosenzweigd2158a52020-09-09 17:46:58 -0400833 alu.bitwise.dest_invert = true;
Chris Forbes539ea082020-07-26 11:37:42 -0700834 alu.src[1] = BIR_INDEX_ZERO;
Chris Forbes946ff9b2020-07-26 12:18:54 -0700835 /* zero shift */
836 alu.src[2] = BIR_INDEX_ZERO;
Alyssa Rosenzweig08b105d2020-09-09 17:40:22 -0400837 alu.src_types[2] = nir_type_uint8;
Chris Forbes946ff9b2020-07-26 12:18:54 -0700838 break;
839 case nir_op_ishl:
840 alu.op.bitwise = BI_BITWISE_OR;
841 /* move src1 to src2 and replace with zero. underlying op is (src0 << src2) | src1 */
842 alu.src[2] = alu.src[1];
Alyssa Rosenzweig08b105d2020-09-09 17:40:22 -0400843 alu.src_types[2] = nir_type_uint8;
Chris Forbes946ff9b2020-07-26 12:18:54 -0700844 alu.src[1] = BIR_INDEX_ZERO;
Chris Forbes539ea082020-07-26 11:37:42 -0700845 break;
Chris Forbesa0a70872020-07-26 15:54:14 -0700846 case nir_op_imul:
847 alu.op.imul = BI_IMUL_IMUL;
848 break;
Alyssa Rosenzweigc8622342020-03-09 21:10:41 -0400849 case nir_op_fmax:
850 case nir_op_imax:
851 case nir_op_umax:
852 alu.op.minmax = BI_MINMAX_MAX; /* MINMAX */
853 break;
Alyssa Rosenzweig8ed79c92020-03-09 21:20:20 -0400854 case nir_op_frcp:
855 alu.op.special = BI_SPECIAL_FRCP;
856 break;
857 case nir_op_frsq:
858 alu.op.special = BI_SPECIAL_FRSQ;
859 break;
Alyssa Rosenzweig12299de2020-03-21 17:37:47 -0400860 BI_CASE_CMP(nir_op_flt)
861 BI_CASE_CMP(nir_op_ilt)
862 BI_CASE_CMP(nir_op_fge)
863 BI_CASE_CMP(nir_op_ige)
864 BI_CASE_CMP(nir_op_feq)
865 BI_CASE_CMP(nir_op_ieq)
Karol Herbste5899c12020-08-18 19:51:57 +0200866 BI_CASE_CMP(nir_op_fneu)
Alyssa Rosenzweig12299de2020-03-21 17:37:47 -0400867 BI_CASE_CMP(nir_op_ine)
Chris Forbes718d4442020-07-26 12:41:17 -0700868 BI_CASE_CMP(nir_op_uge)
Alyssa Rosenzweig95fc71e2020-04-27 14:15:57 -0400869 alu.cond = bi_cond_for_nir(instr->op, false);
Alyssa Rosenzweig05413502020-03-10 08:21:35 -0400870 break;
Alyssa Rosenzweigf81b67b2020-03-27 20:28:09 -0400871 case nir_op_fround_even:
Alyssa Rosenzweigf81b67b2020-03-27 20:28:09 -0400872 alu.roundmode = BIFROST_RTE;
873 break;
874 case nir_op_fceil:
Alyssa Rosenzweigf81b67b2020-03-27 20:28:09 -0400875 alu.roundmode = BIFROST_RTP;
876 break;
877 case nir_op_ffloor:
Alyssa Rosenzweigf81b67b2020-03-27 20:28:09 -0400878 alu.roundmode = BIFROST_RTN;
879 break;
880 case nir_op_ftrunc:
Alyssa Rosenzweigf81b67b2020-03-27 20:28:09 -0400881 alu.roundmode = BIFROST_RTZ;
882 break;
Alyssa Rosenzweiga077da62020-04-28 14:36:17 -0400883 case nir_op_iand:
884 alu.op.bitwise = BI_BITWISE_AND;
Chris Forbes946ff9b2020-07-26 12:18:54 -0700885 /* zero shift */
886 alu.src[2] = BIR_INDEX_ZERO;
Alyssa Rosenzweig08b105d2020-09-09 17:40:22 -0400887 alu.src_types[2] = nir_type_uint8;
Alyssa Rosenzweiga077da62020-04-28 14:36:17 -0400888 break;
889 case nir_op_ior:
890 alu.op.bitwise = BI_BITWISE_OR;
Chris Forbes946ff9b2020-07-26 12:18:54 -0700891 /* zero shift */
892 alu.src[2] = BIR_INDEX_ZERO;
Alyssa Rosenzweig08b105d2020-09-09 17:40:22 -0400893 alu.src_types[2] = nir_type_uint8;
Alyssa Rosenzweiga077da62020-04-28 14:36:17 -0400894 break;
895 case nir_op_ixor:
896 alu.op.bitwise = BI_BITWISE_XOR;
Chris Forbes946ff9b2020-07-26 12:18:54 -0700897 /* zero shift */
898 alu.src[2] = BIR_INDEX_ZERO;
Alyssa Rosenzweig08b105d2020-09-09 17:40:22 -0400899 alu.src_types[2] = nir_type_uint8;
Alyssa Rosenzweiga077da62020-04-28 14:36:17 -0400900 break;
Chris Forbesf6aa0712020-07-04 15:26:42 -0700901 case nir_op_f2i32:
902 alu.roundmode = BIFROST_RTZ;
903 break;
Alyssa Rosenzweig1b09c692020-06-02 19:29:25 -0400904
905 case nir_op_f2f16:
906 case nir_op_i2i16:
907 case nir_op_u2u16: {
908 if (nir_src_bit_size(instr->src[0].src) != 32)
909 break;
910
911 /* Should have been const folded */
912 assert(!nir_src_is_const(instr->src[0].src));
913
914 alu.src_types[1] = alu.src_types[0];
915 alu.src[1] = alu.src[0];
916
917 unsigned last = nir_dest_num_components(instr->dest.dest) - 1;
918 assert(last <= 1);
919
920 alu.swizzle[1][0] = instr->src[0].swizzle[last];
921 break;
922 }
923
Alyssa Rosenzweig929baf32020-03-09 20:19:51 -0400924 default:
925 break;
926 }
927
Alyssa Rosenzweig3f786ed2020-03-21 18:13:49 -0400928 if (alu.type == BI_CSEL) {
Alyssa Rosenzweig5cdc31a2020-03-21 21:19:14 -0400929 /* Default to csel3 */
Alyssa Rosenzweig95fc71e2020-04-27 14:15:57 -0400930 alu.cond = BI_COND_NE;
Alyssa Rosenzweig8eefb272020-04-05 19:22:01 -0400931 alu.src[1] = BIR_INDEX_ZERO;
932 alu.src_types[1] = alu.src_types[0];
Alyssa Rosenzweig5cdc31a2020-03-21 21:19:14 -0400933
Alyssa Rosenzweig31a41bb2020-05-01 17:34:47 -0400934 /* TODO: Reenable cond fusing when we can split up registers
935 * when scheduling */
936#if 0
Alyssa Rosenzweig201a11a2020-05-01 18:31:22 -0400937 bi_fuse_cond(&alu, instr->src[0],
938 &constants_left, &constant_shift, comps, false);
Alyssa Rosenzweig31a41bb2020-05-01 17:34:47 -0400939#endif
Alyssa Rosenzweig3f786ed2020-03-21 18:13:49 -0400940 }
941
Alyssa Rosenzweig929baf32020-03-09 20:19:51 -0400942 bi_emit(ctx, alu);
943}
944
Alyssa Rosenzweig0769036a2020-04-21 12:15:29 -0400945/* TEX_COMPACT instructions assume normal 2D f32 operation but are more
946 * space-efficient and with simpler RA/scheduling requirements*/
947
948static void
949emit_tex_compact(bi_context *ctx, nir_tex_instr *instr)
950{
Alyssa Rosenzweigcd5fe3b2020-04-21 13:00:44 -0400951 bi_instruction tex = {
952 .type = BI_TEX,
953 .op = { .texture = BI_TEX_COMPACT },
Alyssa Rosenzweig5f35cda2020-04-30 16:10:55 -0400954 .texture = {
955 .texture_index = instr->texture_index,
956 .sampler_index = instr->sampler_index,
Alyssa Rosenzweig67d89562020-08-03 12:47:57 -0400957 .compute_lod = instr->op == nir_texop_tex,
Alyssa Rosenzweig5f35cda2020-04-30 16:10:55 -0400958 },
Alyssa Rosenzweigfbbe3d42020-04-27 16:04:05 -0400959 .dest = pan_dest_index(&instr->dest),
Alyssa Rosenzweigcd5fe3b2020-04-21 13:00:44 -0400960 .dest_type = instr->dest_type,
961 .src_types = { nir_type_float32, nir_type_float32 },
Alyssa Rosenzweigb2c6cf22020-04-24 17:20:28 -0400962 .vector_channels = 4
Alyssa Rosenzweigcd5fe3b2020-04-21 13:00:44 -0400963 };
964
965 for (unsigned i = 0; i < instr->num_srcs; ++i) {
Alyssa Rosenzweigfbbe3d42020-04-27 16:04:05 -0400966 int index = pan_src_index(&instr->src[i].src);
Alyssa Rosenzweig6650fa22020-05-27 11:43:37 -0400967
968 /* We were checked ahead-of-time */
969 if (instr->src[i].src_type == nir_tex_src_lod)
970 continue;
971
Alyssa Rosenzweigcd5fe3b2020-04-21 13:00:44 -0400972 assert (instr->src[i].src_type == nir_tex_src_coord);
973
974 tex.src[0] = index;
975 tex.src[1] = index;
976 tex.swizzle[0][0] = 0;
977 tex.swizzle[1][0] = 1;
978 }
979
980 bi_emit(ctx, tex);
Alyssa Rosenzweig0769036a2020-04-21 12:15:29 -0400981}
982
983static void
984emit_tex_full(bi_context *ctx, nir_tex_instr *instr)
985{
986 unreachable("stub");
987}
988
Alyssa Rosenzweig731dfc62020-05-27 11:41:42 -0400989/* Normal textures ops are tex for frag shaders and txl for vertex shaders with
990 * lod a constant 0. Anything else needs a full texture op. */
991
992static bool
993bi_is_normal_tex(gl_shader_stage stage, nir_tex_instr *instr)
994{
995 if (stage == MESA_SHADER_FRAGMENT)
996 return instr->op == nir_texop_tex;
997
998 if (instr->op != nir_texop_txl)
999 return false;
1000
1001 for (unsigned i = 0; i < instr->num_srcs; ++i) {
1002 if (instr->src[i].src_type != nir_tex_src_lod)
1003 continue;
1004
1005 nir_src src = instr->src[i].src;
1006
1007 if (!nir_src_is_const(src))
1008 continue;
1009
1010 if (nir_src_as_uint(src) != 0)
1011 continue;
1012 }
1013
1014 return true;
1015}
1016
Alyssa Rosenzweig0769036a2020-04-21 12:15:29 -04001017static void
1018emit_tex(bi_context *ctx, nir_tex_instr *instr)
1019{
1020 nir_alu_type base = nir_alu_type_get_base_type(instr->dest_type);
1021 unsigned sz = nir_dest_bit_size(instr->dest);
1022 instr->dest_type = base | sz;
1023
Alyssa Rosenzweig731dfc62020-05-27 11:41:42 -04001024 bool is_normal = bi_is_normal_tex(ctx->stage, instr);
Alyssa Rosenzweig0769036a2020-04-21 12:15:29 -04001025 bool is_2d = instr->sampler_dim == GLSL_SAMPLER_DIM_2D ||
1026 instr->sampler_dim == GLSL_SAMPLER_DIM_EXTERNAL;
1027 bool is_f = base == nir_type_float && (sz == 16 || sz == 32);
1028
1029 bool is_compact = is_normal && is_2d && is_f && !instr->is_shadow;
1030
1031 if (is_compact)
1032 emit_tex_compact(ctx, instr);
1033 else
1034 emit_tex_full(ctx, instr);
1035}
1036
Alyssa Rosenzweig51e537c2020-03-06 16:29:35 -05001037static void
Alyssa Rosenzweig65c8dcc2020-03-05 17:10:46 -05001038emit_instr(bi_context *ctx, struct nir_instr *instr)
1039{
1040 switch (instr->type) {
Alyssa Rosenzweig65c8dcc2020-03-05 17:10:46 -05001041 case nir_instr_type_load_const:
1042 emit_load_const(ctx, nir_instr_as_load_const(instr));
1043 break;
1044
1045 case nir_instr_type_intrinsic:
1046 emit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
1047 break;
1048
1049 case nir_instr_type_alu:
1050 emit_alu(ctx, nir_instr_as_alu(instr));
1051 break;
1052
1053 case nir_instr_type_tex:
1054 emit_tex(ctx, nir_instr_as_tex(instr));
1055 break;
Alyssa Rosenzweig65c8dcc2020-03-05 17:10:46 -05001056
1057 case nir_instr_type_jump:
1058 emit_jump(ctx, nir_instr_as_jump(instr));
1059 break;
1060
1061 case nir_instr_type_ssa_undef:
1062 /* Spurious */
1063 break;
1064
1065 default:
Alyssa Rosenzweig0769036a2020-04-21 12:15:29 -04001066 unreachable("Unhandled instruction type");
Alyssa Rosenzweig65c8dcc2020-03-05 17:10:46 -05001067 break;
1068 }
1069}
1070
1071
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -05001072
1073static bi_block *
1074create_empty_block(bi_context *ctx)
1075{
1076 bi_block *blk = rzalloc(ctx, bi_block);
1077
Alyssa Rosenzweig9b75f412020-03-11 14:35:38 -04001078 blk->base.predecessors = _mesa_set_create(blk,
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -05001079 _mesa_hash_pointer,
1080 _mesa_key_pointer_equal);
1081
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -05001082 return blk;
1083}
1084
1085static bi_block *
1086emit_block(bi_context *ctx, nir_block *block)
1087{
Alyssa Rosenzweig9a00cf32020-03-05 16:45:16 -05001088 if (ctx->after_block) {
1089 ctx->current_block = ctx->after_block;
1090 ctx->after_block = NULL;
1091 } else {
1092 ctx->current_block = create_empty_block(ctx);
1093 }
1094
Alyssa Rosenzweig9b75f412020-03-11 14:35:38 -04001095 list_addtail(&ctx->current_block->base.link, &ctx->blocks);
1096 list_inithead(&ctx->current_block->base.instructions);
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -05001097
1098 nir_foreach_instr(instr, block) {
Alyssa Rosenzweig65c8dcc2020-03-05 17:10:46 -05001099 emit_instr(ctx, instr);
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -05001100 ++ctx->instruction_count;
1101 }
1102
1103 return ctx->current_block;
1104}
1105
Alyssa Rosenzweig9a00cf32020-03-05 16:45:16 -05001106/* Emits an unconditional branch to the end of the current block, returning a
1107 * pointer so the user can fill in details */
1108
1109static bi_instruction *
1110bi_emit_branch(bi_context *ctx)
1111{
1112 bi_instruction branch = {
1113 .type = BI_BRANCH,
Alyssa Rosenzweig6627b202020-05-01 18:13:54 -04001114 .cond = BI_COND_ALWAYS
Alyssa Rosenzweig9a00cf32020-03-05 16:45:16 -05001115 };
1116
1117 return bi_emit(ctx, branch);
1118}
1119
1120/* Sets a condition for a branch by examing the NIR condition. If we're
1121 * familiar with the condition, we unwrap it to fold it into the branch
1122 * instruction. Otherwise, we consume the condition directly. We
1123 * generally use 1-bit booleans which allows us to use small types for
1124 * the conditions.
1125 */
1126
1127static void
1128bi_set_branch_cond(bi_instruction *branch, nir_src *cond, bool invert)
1129{
1130 /* TODO: Try to unwrap instead of always bailing */
Alyssa Rosenzweigfbbe3d42020-04-27 16:04:05 -04001131 branch->src[0] = pan_src_index(cond);
Alyssa Rosenzweig9a00cf32020-03-05 16:45:16 -05001132 branch->src[1] = BIR_INDEX_ZERO;
Alyssa Rosenzweigd619ff02020-05-28 12:39:14 -04001133 branch->src_types[0] = branch->src_types[1] = nir_type_uint |
1134 nir_src_bit_size(*cond);
Alyssa Rosenzweig6627b202020-05-01 18:13:54 -04001135 branch->cond = invert ? BI_COND_EQ : BI_COND_NE;
Alyssa Rosenzweig9a00cf32020-03-05 16:45:16 -05001136}
1137
1138static void
1139emit_if(bi_context *ctx, nir_if *nif)
1140{
1141 bi_block *before_block = ctx->current_block;
1142
1143 /* Speculatively emit the branch, but we can't fill it in until later */
1144 bi_instruction *then_branch = bi_emit_branch(ctx);
1145 bi_set_branch_cond(then_branch, &nif->condition, true);
1146
1147 /* Emit the two subblocks. */
1148 bi_block *then_block = emit_cf_list(ctx, &nif->then_list);
1149 bi_block *end_then_block = ctx->current_block;
1150
1151 /* Emit a jump from the end of the then block to the end of the else */
1152 bi_instruction *then_exit = bi_emit_branch(ctx);
1153
1154 /* Emit second block, and check if it's empty */
1155
1156 int count_in = ctx->instruction_count;
1157 bi_block *else_block = emit_cf_list(ctx, &nif->else_list);
1158 bi_block *end_else_block = ctx->current_block;
1159 ctx->after_block = create_empty_block(ctx);
1160
1161 /* Now that we have the subblocks emitted, fix up the branches */
1162
1163 assert(then_block);
1164 assert(else_block);
1165
1166 if (ctx->instruction_count == count_in) {
1167 /* The else block is empty, so don't emit an exit jump */
1168 bi_remove_instruction(then_exit);
Alyssa Rosenzweig6627b202020-05-01 18:13:54 -04001169 then_branch->branch_target = ctx->after_block;
Alyssa Rosenzweige42a5df2020-05-27 18:27:08 -04001170 pan_block_add_successor(&end_then_block->base, &ctx->after_block->base); /* fallthrough */
Alyssa Rosenzweig9a00cf32020-03-05 16:45:16 -05001171 } else {
Alyssa Rosenzweig6627b202020-05-01 18:13:54 -04001172 then_branch->branch_target = else_block;
1173 then_exit->branch_target = ctx->after_block;
1174 pan_block_add_successor(&end_then_block->base, &then_exit->branch_target->base);
Alyssa Rosenzweige42a5df2020-05-27 18:27:08 -04001175 pan_block_add_successor(&end_else_block->base, &ctx->after_block->base); /* fallthrough */
Alyssa Rosenzweig9a00cf32020-03-05 16:45:16 -05001176 }
1177
Alyssa Rosenzweig6627b202020-05-01 18:13:54 -04001178 pan_block_add_successor(&before_block->base, &then_branch->branch_target->base); /* then_branch */
Alyssa Rosenzweig9b75f412020-03-11 14:35:38 -04001179 pan_block_add_successor(&before_block->base, &then_block->base); /* fallthrough */
Alyssa Rosenzweig9a00cf32020-03-05 16:45:16 -05001180}
1181
Alyssa Rosenzweig987aea12020-03-05 17:03:53 -05001182static void
1183emit_loop(bi_context *ctx, nir_loop *nloop)
1184{
1185 /* Remember where we are */
1186 bi_block *start_block = ctx->current_block;
1187
1188 bi_block *saved_break = ctx->break_block;
1189 bi_block *saved_continue = ctx->continue_block;
1190
1191 ctx->continue_block = create_empty_block(ctx);
1192 ctx->break_block = create_empty_block(ctx);
1193 ctx->after_block = ctx->continue_block;
1194
1195 /* Emit the body itself */
1196 emit_cf_list(ctx, &nloop->body);
1197
1198 /* Branch back to loop back */
1199 bi_instruction *br_back = bi_emit_branch(ctx);
Alyssa Rosenzweig6627b202020-05-01 18:13:54 -04001200 br_back->branch_target = ctx->continue_block;
Alyssa Rosenzweig9b75f412020-03-11 14:35:38 -04001201 pan_block_add_successor(&start_block->base, &ctx->continue_block->base);
1202 pan_block_add_successor(&ctx->current_block->base, &ctx->continue_block->base);
Alyssa Rosenzweig987aea12020-03-05 17:03:53 -05001203
1204 ctx->after_block = ctx->break_block;
1205
1206 /* Pop off */
1207 ctx->break_block = saved_break;
1208 ctx->continue_block = saved_continue;
1209 ++ctx->loop_count;
1210}
1211
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -05001212static bi_block *
1213emit_cf_list(bi_context *ctx, struct exec_list *list)
1214{
1215 bi_block *start_block = NULL;
1216
1217 foreach_list_typed(nir_cf_node, node, node, list) {
1218 switch (node->type) {
1219 case nir_cf_node_block: {
1220 bi_block *block = emit_block(ctx, nir_cf_node_as_block(node));
1221
1222 if (!start_block)
1223 start_block = block;
1224
1225 break;
1226 }
1227
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -05001228 case nir_cf_node_if:
1229 emit_if(ctx, nir_cf_node_as_if(node));
1230 break;
1231
1232 case nir_cf_node_loop:
1233 emit_loop(ctx, nir_cf_node_as_loop(node));
1234 break;
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -05001235
1236 default:
1237 unreachable("Unknown control flow");
1238 }
1239 }
1240
1241 return start_block;
1242}
Alyssa Rosenzweigeceaea42020-03-02 19:47:11 -05001243
Alyssa Rosenzweig0d291842020-03-05 10:11:39 -05001244static int
1245glsl_type_size(const struct glsl_type *type, bool bindless)
1246{
1247 return glsl_count_attribute_slots(type, false);
1248}
1249
1250static void
1251bi_optimize_nir(nir_shader *nir)
1252{
1253 bool progress;
1254 unsigned lower_flrp = 16 | 32 | 64;
1255
1256 NIR_PASS(progress, nir, nir_lower_regs_to_ssa);
1257 NIR_PASS(progress, nir, nir_lower_idiv, nir_lower_idiv_fast);
1258
1259 nir_lower_tex_options lower_tex_options = {
1260 .lower_txs_lod = true,
1261 .lower_txp = ~0,
1262 .lower_tex_without_implicit_lod = true,
1263 .lower_txd = true,
1264 };
1265
1266 NIR_PASS(progress, nir, nir_lower_tex, &lower_tex_options);
Alyssa Rosenzweig51e537c2020-03-06 16:29:35 -05001267 NIR_PASS(progress, nir, nir_lower_alu_to_scalar, NULL, NULL);
1268 NIR_PASS(progress, nir, nir_lower_load_const_to_scalar);
Alyssa Rosenzweig0d291842020-03-05 10:11:39 -05001269
1270 do {
1271 progress = false;
1272
1273 NIR_PASS(progress, nir, nir_lower_var_copies);
1274 NIR_PASS(progress, nir, nir_lower_vars_to_ssa);
1275
1276 NIR_PASS(progress, nir, nir_copy_prop);
1277 NIR_PASS(progress, nir, nir_opt_remove_phis);
1278 NIR_PASS(progress, nir, nir_opt_dce);
1279 NIR_PASS(progress, nir, nir_opt_dead_cf);
1280 NIR_PASS(progress, nir, nir_opt_cse);
1281 NIR_PASS(progress, nir, nir_opt_peephole_select, 64, false, true);
1282 NIR_PASS(progress, nir, nir_opt_algebraic);
1283 NIR_PASS(progress, nir, nir_opt_constant_folding);
1284
1285 if (lower_flrp != 0) {
1286 bool lower_flrp_progress = false;
1287 NIR_PASS(lower_flrp_progress,
1288 nir,
1289 nir_lower_flrp,
1290 lower_flrp,
Marek Olšákac55b1a2020-07-22 22:13:16 -04001291 false /* always_precise */);
Alyssa Rosenzweig0d291842020-03-05 10:11:39 -05001292 if (lower_flrp_progress) {
1293 NIR_PASS(progress, nir,
1294 nir_opt_constant_folding);
1295 progress = true;
1296 }
1297
1298 /* Nothing should rematerialize any flrps, so we only
1299 * need to do this lowering once.
1300 */
1301 lower_flrp = 0;
1302 }
1303
1304 NIR_PASS(progress, nir, nir_opt_undef);
1305 NIR_PASS(progress, nir, nir_opt_loop_unroll,
1306 nir_var_shader_in |
1307 nir_var_shader_out |
1308 nir_var_function_temp);
1309 } while (progress);
1310
1311 NIR_PASS(progress, nir, nir_opt_algebraic_late);
Alyssa Rosenzweig12299de2020-03-21 17:37:47 -04001312 NIR_PASS(progress, nir, nir_lower_bool_to_int32);
Alyssa Rosenzweig3a1baaf2020-03-10 08:20:59 -04001313 NIR_PASS(progress, nir, bifrost_nir_lower_algebraic_late);
Alyssa Rosenzweig51e537c2020-03-06 16:29:35 -05001314 NIR_PASS(progress, nir, nir_lower_alu_to_scalar, NULL, NULL);
1315 NIR_PASS(progress, nir, nir_lower_load_const_to_scalar);
Alyssa Rosenzweig0d291842020-03-05 10:11:39 -05001316
1317 /* Take us out of SSA */
1318 NIR_PASS(progress, nir, nir_lower_locals_to_regs);
Alyssa Rosenzweig330e9a62020-03-09 19:56:35 -04001319 NIR_PASS(progress, nir, nir_move_vec_src_uses_to_dest);
Alyssa Rosenzweige0a51d52020-03-22 17:31:23 -04001320 NIR_PASS(progress, nir, nir_convert_from_ssa, true);
Alyssa Rosenzweig50d3f4d2020-03-19 17:21:49 -04001321}
1322
Alyssa Rosenzweigeceaea42020-03-02 19:47:11 -05001323void
Boris Brezillon0a74a042020-10-08 10:09:56 +02001324bifrost_compile_shader_nir(nir_shader *nir, panfrost_program *program,
1325 const struct panfrost_compile_inputs *inputs)
Alyssa Rosenzweigeceaea42020-03-02 19:47:11 -05001326{
Tomeu Vizoso07b31f32020-04-30 09:29:10 +02001327 bifrost_debug = debug_get_option_bifrost_debug();
1328
Alyssa Rosenzweigeceaea42020-03-02 19:47:11 -05001329 bi_context *ctx = rzalloc(NULL, bi_context);
1330 ctx->nir = nir;
Alyssa Rosenzweig0d291842020-03-05 10:11:39 -05001331 ctx->stage = nir->info.stage;
Boris Brezillon0a74a042020-10-08 10:09:56 +02001332 ctx->quirks = bifrost_get_quirks(inputs->gpu_id);
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -05001333 list_inithead(&ctx->blocks);
Alyssa Rosenzweigeceaea42020-03-02 19:47:11 -05001334
Alyssa Rosenzweig0d291842020-03-05 10:11:39 -05001335 /* Lower gl_Position pre-optimisation, but after lowering vars to ssa
1336 * (so we don't accidentally duplicate the epilogue since mesa/st has
1337 * messed with our I/O quite a bit already) */
1338
1339 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
1340
1341 if (ctx->stage == MESA_SHADER_VERTEX) {
1342 NIR_PASS_V(nir, nir_lower_viewport_transform);
1343 NIR_PASS_V(nir, nir_lower_point_size, 1.0, 1024.0);
1344 }
1345
1346 NIR_PASS_V(nir, nir_split_var_copies);
1347 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
1348 NIR_PASS_V(nir, nir_lower_var_copies);
1349 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
Jason Ekstrandb019b222020-06-10 17:54:25 -05001350 NIR_PASS_V(nir, nir_lower_io, nir_var_shader_in | nir_var_shader_out,
1351 glsl_type_size, 0);
Alyssa Rosenzweig0d291842020-03-05 10:11:39 -05001352 NIR_PASS_V(nir, nir_lower_ssbo);
Alyssa Rosenzweig9c7d30f2020-04-30 09:27:36 +02001353 NIR_PASS_V(nir, nir_lower_mediump_outputs);
Alyssa Rosenzweig0d291842020-03-05 10:11:39 -05001354
Alyssa Rosenzweig0d291842020-03-05 10:11:39 -05001355 bi_optimize_nir(nir);
Tomeu Vizoso07b31f32020-04-30 09:29:10 +02001356
1357 if (bifrost_debug & BIFROST_DBG_SHADERS) {
1358 nir_print_shader(nir, stdout);
1359 }
Alyssa Rosenzweigeceaea42020-03-02 19:47:11 -05001360
Alyssa Rosenzweig680fb052020-08-18 08:31:42 -04001361 panfrost_nir_assign_sysvals(&ctx->sysvals, ctx, nir);
Alyssa Rosenzweig218785c2020-03-10 16:20:18 -04001362 program->sysval_count = ctx->sysvals.sysval_count;
1363 memcpy(program->sysvals, ctx->sysvals.sysvals, sizeof(ctx->sysvals.sysvals[0]) * ctx->sysvals.sysval_count);
Alyssa Rosenzweig1a8f1a32020-04-23 19:26:01 -04001364 ctx->blend_types = program->blend_types;
Alyssa Rosenzweig218785c2020-03-10 16:20:18 -04001365
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -05001366 nir_foreach_function(func, nir) {
1367 if (!func->impl)
1368 continue;
1369
Alyssa Rosenzweigd86659c2020-03-06 09:43:43 -05001370 ctx->impl = func->impl;
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -05001371 emit_cf_list(ctx, &func->impl->body);
1372 break; /* TODO: Multi-function shaders */
1373 }
1374
Alyssa Rosenzweigc6979922020-05-28 14:44:33 -04001375 unsigned block_source_count = 0;
1376
Alyssa Rosenzweig50d3f4d2020-03-19 17:21:49 -04001377 bi_foreach_block(ctx, _block) {
1378 bi_block *block = (bi_block *) _block;
Alyssa Rosenzweigc6979922020-05-28 14:44:33 -04001379
1380 /* Name blocks now that we're done emitting so the order is
1381 * consistent */
1382 block->base.name = block_source_count++;
1383
Alyssa Rosenzweige0a51d52020-03-22 17:31:23 -04001384 bi_lower_combine(ctx, block);
Alyssa Rosenzweig50d3f4d2020-03-19 17:21:49 -04001385 }
1386
Alyssa Rosenzweig58f91712020-03-11 15:10:32 -04001387 bool progress = false;
1388
1389 do {
1390 progress = false;
1391
1392 bi_foreach_block(ctx, _block) {
1393 bi_block *block = (bi_block *) _block;
1394 progress |= bi_opt_dead_code_eliminate(ctx, block);
1395 }
1396 } while(progress);
1397
Tomeu Vizoso07b31f32020-04-30 09:29:10 +02001398 if (bifrost_debug & BIFROST_DBG_SHADERS)
1399 bi_print_shader(ctx, stdout);
Alyssa Rosenzweigb329f8c2020-03-06 19:25:00 -05001400 bi_schedule(ctx);
Alyssa Rosenzweige8139ef2020-03-11 20:39:36 -04001401 bi_register_allocate(ctx);
Tomeu Vizoso07b31f32020-04-30 09:29:10 +02001402 if (bifrost_debug & BIFROST_DBG_SHADERS)
1403 bi_print_shader(ctx, stdout);
Alyssa Rosenzweig9269c852020-03-12 14:16:22 -04001404 bi_pack(ctx, &program->compiled);
Tomeu Vizoso07b31f32020-04-30 09:29:10 +02001405
1406 if (bifrost_debug & BIFROST_DBG_SHADERS)
1407 disassemble_bifrost(stdout, program->compiled.data, program->compiled.size, true);
Alyssa Rosenzweig0d291842020-03-05 10:11:39 -05001408
Alyssa Rosenzweigeceaea42020-03-02 19:47:11 -05001409 ralloc_free(ctx);
1410}