blob: de417fb8e3eb4a0c393884373dac7734fa513406 [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#ifndef __BIFROST_COMPILER_H
28#define __BIFROST_COMPILER_H
29
Alyssa Rosenzweig29acd7b2020-03-02 20:40:52 -050030#include "bifrost.h"
Alyssa Rosenzweig7ac62122020-03-02 20:38:26 -050031#include "compiler/nir/nir.h"
32
Alyssa Rosenzweige7dc2a72020-03-02 20:06:34 -050033/* Bifrost opcodes are tricky -- the same op may exist on both FMA and
34 * ADD with two completely different opcodes, and opcodes can be varying
35 * length in some cases. Then we have different opcodes for int vs float
36 * and then sometimes even for different typesizes. Further, virtually
37 * every op has a number of flags which depend on the op. In constrast
38 * to Midgard where you have a strict ALU/LDST/TEX division and within
39 * ALU you have strict int/float and that's it... here it's a *lot* more
40 * involved. As such, we use something much higher level for our IR,
41 * encoding "classes" of operations, letting the opcode details get
42 * sorted out at emit time.
43 *
44 * Please keep this list alphabetized. Please use a dictionary if you
45 * don't know how to do that.
46 */
47
48enum bi_class {
49 BI_ADD,
50 BI_ATEST,
51 BI_BRANCH,
52 BI_CMP,
53 BI_BLEND,
54 BI_BITWISE,
55 BI_CONVERT,
56 BI_CSEL,
57 BI_DISCARD,
Alyssa Rosenzweigcb3cd8a2020-03-03 08:57:03 -050058 BI_EXTRACT,
Alyssa Rosenzweige7dc2a72020-03-02 20:06:34 -050059 BI_FMA,
60 BI_FREXP,
61 BI_LOAD,
Alyssa Rosenzweig1ead0d32020-03-06 09:52:09 -050062 BI_LOAD_UNIFORM,
Alyssa Rosenzweige7dc2a72020-03-02 20:06:34 -050063 BI_LOAD_ATTR,
64 BI_LOAD_VAR,
65 BI_LOAD_VAR_ADDRESS,
Alyssa Rosenzweigcb3cd8a2020-03-03 08:57:03 -050066 BI_MAKE_VEC,
Alyssa Rosenzweige7dc2a72020-03-02 20:06:34 -050067 BI_MINMAX,
68 BI_MOV,
69 BI_SHIFT,
70 BI_STORE,
71 BI_STORE_VAR,
72 BI_SPECIAL, /* _FAST, _TABLE on supported GPUs */
Alyssa Rosenzweig5896db92020-03-03 08:35:51 -050073 BI_SWIZZLE,
Alyssa Rosenzweige7dc2a72020-03-02 20:06:34 -050074 BI_TEX,
75 BI_ROUND,
Alyssa Rosenzweig7ac62122020-03-02 20:38:26 -050076 BI_NUM_CLASSES
Alyssa Rosenzweige7dc2a72020-03-02 20:06:34 -050077};
78
Alyssa Rosenzweig7ac62122020-03-02 20:38:26 -050079/* Properties of a class... */
80extern unsigned bi_class_props[BI_NUM_CLASSES];
81
82/* abs/neg/outmod valid for a float op */
83#define BI_MODS (1 << 0)
84
Alyssa Rosenzweig34165c72020-03-02 20:46:37 -050085/* Generic enough that little class-specific information is required. In other
86 * words, it acts as a "normal" ALU op, even if the encoding ends up being
87 * irregular enough to warrant a separate class */
88#define BI_GENERIC (1 << 1)
89
Alyssa Rosenzweigd69bf8d2020-03-02 20:52:36 -050090/* Accepts a bifrost_roundmode */
91#define BI_ROUNDMODE (1 << 2)
92
Alyssa Rosenzweig99f3c1f2020-03-02 21:53:13 -050093/* Can be scheduled to FMA */
94#define BI_SCHED_FMA (1 << 3)
95
96/* Can be scheduled to ADD */
97#define BI_SCHED_ADD (1 << 4)
98
99/* Most ALU ops can do either, actually */
100#define BI_SCHED_ALL (BI_SCHED_FMA | BI_SCHED_ADD)
101
Alyssa Rosenzweigc70a1982020-03-03 08:16:50 -0500102/* Along with setting BI_SCHED_ADD, eats up the entire cycle, so FMA must be
103 * nopped out. Used for _FAST operations. */
104#define BI_SCHED_SLOW (1 << 5)
105
Alyssa Rosenzweig5896db92020-03-03 08:35:51 -0500106/* Swizzling allowed for the 8/16-bit source */
107#define BI_SWIZZLABLE (1 << 6)
108
Alyssa Rosenzweig07228a62020-03-03 13:55:33 -0500109/* For scheduling purposes this is a high latency instruction and must be at
110 * the end of a clause. Implies ADD */
111#define BI_SCHED_HI_LATENCY ((1 << 7) | BI_SCHED_ADD)
112
Alyssa Rosenzweig230be612020-03-02 20:24:03 -0500113/* It can't get any worse than csel4... can it? */
114#define BIR_SRC_COUNT 4
115
Alyssa Rosenzweig1ead0d32020-03-06 09:52:09 -0500116/* Class-specific data for BI_LOAD, BI_LD_ATTR, BI_LD_VAR_ADDR */
Alyssa Rosenzweigaa2f12d2020-03-02 21:19:16 -0500117struct bi_load {
Alyssa Rosenzweig1ead0d32020-03-06 09:52:09 -0500118 /* Note: LD_ATTR does not support indirects */
Alyssa Rosenzweigaa2f12d2020-03-02 21:19:16 -0500119 unsigned location;
120
Alyssa Rosenzweig1ead0d32020-03-06 09:52:09 -0500121 /* Number of vector channels */
Alyssa Rosenzweigaa2f12d2020-03-02 21:19:16 -0500122 unsigned channels;
123};
124
Alyssa Rosenzweig9643b9d2020-03-02 21:48:51 -0500125/* BI_LD_VARY */
126struct bi_load_vary {
127 /* All parameters used here. Indirect location specified in
128 * src1 and ignoring location, if present. */
129 struct bi_load load;
130
131 enum bifrost_interp_mode interp_mode;
132 bool reuse;
133 bool flat;
134};
135
Alyssa Rosenzweig47451bb2020-03-03 13:48:13 -0500136/* BI_BRANCH encoding the details of the branch itself as well as a pointer to
137 * the target. We forward declare bi_block since this is mildly circular (not
138 * strictly, but this order of the file makes more sense I think)
139 *
140 * We define our own enum of conditions since the conditions in the hardware
141 * packed in crazy ways that would make manipulation unweildly (meaning changes
142 * based on port swapping, etc), so we defer dealing with that until emit time.
143 * Likewise, we expose NIR types instead of the crazy branch types, although
144 * the restrictions do eventually apply of course. */
145
146struct bi_block;
147
148enum bi_cond {
149 BI_COND_ALWAYS,
150 BI_COND_LT,
151 BI_COND_LE,
152 BI_COND_GE,
153 BI_COND_GT,
154 BI_COND_EQ,
155 BI_COND_NE,
156};
157
158struct bi_branch {
159 /* Types are specified in src_types and must be compatible (either both
160 * int, or both float, 16/32, and same size or 32/16 if float. Types
161 * ignored if BI_COND_ALWAYS is set for an unconditional branch. */
162
163 enum bi_cond cond;
164 struct bi_block *target;
165};
166
Alyssa Rosenzweig44ebc272020-03-03 07:58:05 -0500167/* Opcodes within a class */
168enum bi_minmax_op {
169 BI_MINMAX_MIN,
170 BI_MINMAX_MAX
171};
172
173enum bi_bitwise_op {
174 BI_BITWISE_AND,
175 BI_BITWISE_OR,
176 BI_BITWISE_XOR
177};
178
179enum bi_round_op {
180 BI_ROUND_MODE, /* use round mode */
181 BI_ROUND_ROUND /* i.e.: fround() */
182};
183
Alyssa Rosenzweige7dc2a72020-03-02 20:06:34 -0500184typedef struct {
185 struct list_head link; /* Must be first */
186 enum bi_class type;
Alyssa Rosenzweig230be612020-03-02 20:24:03 -0500187
188 /* Indices, see bir_ssa_index etc. Note zero is special cased
189 * to "no argument" */
190 unsigned dest;
191 unsigned src[BIR_SRC_COUNT];
Alyssa Rosenzweig29acd7b2020-03-02 20:40:52 -0500192
Alyssa Rosenzweigcb3cd8a2020-03-03 08:57:03 -0500193 /* If one of the sources has BIR_INDEX_CONSTANT... Also, for
194 * BI_EXTRACT, the component index is stored here. */
Alyssa Rosenzweigb5bdd892020-03-03 07:47:29 -0500195 union {
196 uint64_t u64;
197 uint32_t u32;
198 uint16_t u16[2];
199 uint8_t u8[4];
200 } constant;
201
Alyssa Rosenzweig29acd7b2020-03-02 20:40:52 -0500202 /* Floating-point modifiers, type/class permitting. If not
203 * allowed for the type/class, these are ignored. */
204 enum bifrost_outmod outmod;
205 bool src_abs[BIR_SRC_COUNT];
206 bool src_neg[BIR_SRC_COUNT];
Alyssa Rosenzweigd69bf8d2020-03-02 20:52:36 -0500207
208 /* Round mode (requires BI_ROUNDMODE) */
209 enum bifrost_roundmode roundmode;
Alyssa Rosenzweigb93aec62020-03-02 20:53:47 -0500210
Alyssa Rosenzweigc42002d2020-03-02 22:03:05 -0500211 /* Destination type. Usually the type of the instruction
212 * itself, but if sources and destination have different
213 * types, the type of the destination wins (so f2i would be
214 * int). Zero if there is no destination. Bitsize included */
215 nir_alu_type dest_type;
216
Alyssa Rosenzweig8929fe02020-03-03 08:37:15 -0500217 /* Source types if required by the class */
218 nir_alu_type src_types[BIR_SRC_COUNT];
219
Alyssa Rosenzweig5896db92020-03-03 08:35:51 -0500220 /* If the source type is 8-bit or 16-bit such that SIMD is possible, and
221 * the class has BI_SWIZZLABLE, this is a swizzle for the input. Swizzles
222 * in practice only occur with one-source arguments (conversions,
223 * dedicated swizzle ops) and as component selection on two-sources
224 * where it is unambiguous which is which. Bounds are 32/type_size. */
225 unsigned swizzle[4];
226
Alyssa Rosenzweig44ebc272020-03-03 07:58:05 -0500227 /* A class-specific op from which the actual opcode can be derived
228 * (along with the above information) */
229
230 union {
231 enum bi_minmax_op minmax;
232 enum bi_bitwise_op bitwise;
233 enum bi_round_op round;
234 } op;
235
Alyssa Rosenzweigb93aec62020-03-02 20:53:47 -0500236 /* Union for class-specific information */
237 union {
238 enum bifrost_minmax_mode minmax;
Alyssa Rosenzweigaa2f12d2020-03-02 21:19:16 -0500239 struct bi_load load;
Alyssa Rosenzweig9643b9d2020-03-02 21:48:51 -0500240 struct bi_load_vary load_vary;
Alyssa Rosenzweig47451bb2020-03-03 13:48:13 -0500241 struct bi_branch branch;
Alyssa Rosenzweig546c3012020-03-05 07:46:00 -0500242
243 /* For CSEL, the comparison op. BI_COND_ALWAYS doesn't make
244 * sense here but you can always just use a move for that */
245 enum bi_cond csel_cond;
Alyssa Rosenzweig92a4f262020-03-06 09:25:58 -0500246
247 /* For BLEND -- the location 0-7 */
248 unsigned blend_location;
Alyssa Rosenzweigb93aec62020-03-02 20:53:47 -0500249 };
Alyssa Rosenzweige7dc2a72020-03-02 20:06:34 -0500250} bi_instruction;
251
Alyssa Rosenzweiga35854c2020-03-02 22:00:07 -0500252/* Scheduling takes place in two steps. Step 1 groups instructions within a
253 * block into distinct clauses (bi_clause). Step 2 schedules instructions
254 * within a clause into FMA/ADD pairs (bi_bundle).
255 *
256 * A bi_bundle contains two paired instruction pointers. If a slot is unfilled,
257 * leave it NULL; the emitter will fill in a nop.
258 */
259
Alyssa Rosenzweige7dc2a72020-03-02 20:06:34 -0500260typedef struct {
Alyssa Rosenzweiga35854c2020-03-02 22:00:07 -0500261 bi_instruction *fma;
262 bi_instruction *add;
263} bi_bundle;
264
265typedef struct {
266 struct list_head link;
267
268 /* A clause can have 8 instructions in bundled FMA/ADD sense, so there
269 * can be 8 bundles. But each bundle can have both an FMA and an ADD,
270 * so a clause can have up to 16 bi_instructions. Whether bundles or
271 * instructions are used depends on where in scheduling we are. */
272
273 unsigned instruction_count;
274 unsigned bundle_count;
275
276 union {
277 bi_instruction *instructions[16];
278 bi_bundle bundles[8];
279 };
Alyssa Rosenzweigfba1d122020-03-03 08:09:18 -0500280
281 /* For scoreboarding -- the clause ID (this is not globally unique!)
282 * and its dependencies in terms of other clauses, computed during
283 * scheduling and used when emitting code. Dependencies expressed as a
284 * bitfield matching the hardware, except shifted by a clause (the
285 * shift back to the ISA's off-by-one encoding is worked out when
286 * emitting clauses) */
287 unsigned scoreboard_id;
288 uint8_t dependencies;
289
290 /* Back-to-back corresponds directly to the back-to-back bit. Branch
291 * conditional corresponds to the branch conditional bit except that in
292 * the emitted code it's always set if back-to-bit is, whereas we use
293 * the actual value (without back-to-back so to speak) internally */
294 bool back_to_back;
295 bool branch_conditional;
296
297 /* Corresponds to the usual bit but shifted by a clause */
298 bool data_register_write_barrier;
Alyssa Rosenzweigd3370bd2020-03-03 13:01:41 -0500299
300 /* Constants read by this clause. ISA limit. */
301 uint64_t constants[8];
302 unsigned constant_count;
Alyssa Rosenzweiga35854c2020-03-02 22:00:07 -0500303} bi_clause;
304
305typedef struct bi_block {
Alyssa Rosenzweige7dc2a72020-03-02 20:06:34 -0500306 struct list_head link; /* must be first */
Alyssa Rosenzweiga35854c2020-03-02 22:00:07 -0500307 unsigned name; /* Just for pretty-printing */
308
309 /* If true, uses clauses; if false, uses instructions */
310 bool scheduled;
311
312 union {
313 struct list_head instructions; /* pre-schedule, list of bi_instructions */
314 struct list_head clauses; /* list of bi_clause */
315 };
Alyssa Rosenzweig2afddc42020-03-03 13:47:13 -0500316
317 /* Control flow graph */
318 struct set *predecessors;
319 struct bi_block *successors[2];
Alyssa Rosenzweige7dc2a72020-03-02 20:06:34 -0500320} bi_block;
321
Alyssa Rosenzweigeceaea42020-03-02 19:47:11 -0500322typedef struct {
323 nir_shader *nir;
Alyssa Rosenzweig0d291842020-03-05 10:11:39 -0500324 gl_shader_stage stage;
Alyssa Rosenzweige7dc2a72020-03-02 20:06:34 -0500325 struct list_head blocks; /* list of bi_block */
Alyssa Rosenzweig0b26cb12020-03-03 14:27:05 -0500326 uint32_t quirks;
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -0500327
328 /* During NIR->BIR */
Alyssa Rosenzweigd86659c2020-03-06 09:43:43 -0500329 nir_function_impl *impl;
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -0500330 bi_block *current_block;
331 unsigned block_name_count;
Alyssa Rosenzweig55dab922020-03-05 16:44:49 -0500332 bi_block *after_block;
Alyssa Rosenzweig987aea12020-03-05 17:03:53 -0500333 bi_block *break_block;
334 bi_block *continue_block;
Alyssa Rosenzweigdabb6c62020-03-06 09:26:44 -0500335 bool emitted_atest;
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -0500336
Alyssa Rosenzweigd86659c2020-03-06 09:43:43 -0500337 /* For creating temporaries */
338 unsigned temp_alloc;
339
Alyssa Rosenzweig83c45622020-03-05 10:25:19 -0500340 /* Stats for shader-db */
341 unsigned instruction_count;
Alyssa Rosenzweig987aea12020-03-05 17:03:53 -0500342 unsigned loop_count;
Alyssa Rosenzweig55dab922020-03-05 16:44:49 -0500343} bi_context;
344
345static inline bi_instruction *
346bi_emit(bi_context *ctx, bi_instruction ins)
347{
348 bi_instruction *u = rzalloc(ctx, bi_instruction);
349 memcpy(u, &ins, sizeof(ins));
350 list_addtail(&u->link, &ctx->current_block->instructions);
351 return u;
352}
353
354static inline void
355bi_remove_instruction(bi_instruction *ins)
356{
357 list_del(&ins->link);
358}
Alyssa Rosenzweigeceaea42020-03-02 19:47:11 -0500359
Alyssa Rosenzweig230be612020-03-02 20:24:03 -0500360/* So we can distinguish between SSA/reg/sentinel quickly */
361#define BIR_NO_ARG (0)
362#define BIR_IS_REG (1)
363
Alyssa Rosenzweiga2c12652020-03-03 07:45:33 -0500364/* If high bits are set, instead of SSA/registers, we have specials indexed by
365 * the low bits if necessary.
366 *
367 * Fixed register: do not allocate register, do not collect $200.
368 * Uniform: access a uniform register given by low bits.
369 * Constant: access the specified constant
370 * Zero: special cased to avoid wasting a constant
371 */
372
373#define BIR_INDEX_REGISTER (1 << 31)
374#define BIR_INDEX_UNIFORM (1 << 30)
375#define BIR_INDEX_CONSTANT (1 << 29)
376#define BIR_INDEX_ZERO (1 << 28)
377
378/* Keep me synced please so we can check src & BIR_SPECIAL */
379
380#define BIR_SPECIAL ((BIR_INDEX_REGISTER | BIR_INDEX_UNIFORM) | \
381 (BIR_INDEX_CONSTANT | BIR_INDEX_ZERO)
382
Alyssa Rosenzweig230be612020-03-02 20:24:03 -0500383static inline unsigned
Alyssa Rosenzweigd86659c2020-03-06 09:43:43 -0500384bi_make_temp(bi_context *ctx)
385{
386 return (ctx->impl->ssa_alloc + 1 + ctx->temp_alloc++) << 1;
387}
388
389static inline unsigned
390bi_make_temp_reg(bi_context *ctx)
391{
392 return ((ctx->impl->reg_alloc + ctx->temp_alloc++) << 1) | BIR_IS_REG;
393}
394
395static inline unsigned
Alyssa Rosenzweig230be612020-03-02 20:24:03 -0500396bir_ssa_index(nir_ssa_def *ssa)
397{
398 /* Off-by-one ensures BIR_NO_ARG is skipped */
399 return ((ssa->index + 1) << 1) | 0;
400}
401
402static inline unsigned
403bir_src_index(nir_src *src)
404{
405 if (src->is_ssa)
406 return bir_ssa_index(src->ssa);
407 else {
408 assert(!src->reg.indirect);
409 return (src->reg.reg->index << 1) | BIR_IS_REG;
410 }
411}
412
413static inline unsigned
414bir_dest_index(nir_dest *dst)
415{
416 if (dst->is_ssa)
417 return bir_ssa_index(&dst->ssa);
418 else {
419 assert(!dst->reg.indirect);
420 return (dst->reg.reg->index << 1) | BIR_IS_REG;
421 }
422}
423
Alyssa Rosenzweig8ec67182020-03-03 14:32:28 -0500424/* Iterators for Bifrost IR */
425
426#define bi_foreach_block(ctx, v) \
427 list_for_each_entry(bi_block, v, &ctx->blocks, link)
428
429#define bi_foreach_block_from(ctx, from, v) \
430 list_for_each_entry_from(bi_block, v, from, &ctx->blocks, link)
431
432#define bi_foreach_instr_in_block(block, v) \
433 list_for_each_entry(bi_instruction, v, &block->instructions, link)
434
435#define bi_foreach_instr_in_block_rev(block, v) \
436 list_for_each_entry_rev(bi_instruction, v, &block->instructions, link)
437
438#define bi_foreach_instr_in_block_safe(block, v) \
439 list_for_each_entry_safe(bi_instruction, v, &block->instructions, link)
440
441#define bi_foreach_instr_in_block_safe_rev(block, v) \
442 list_for_each_entry_safe_rev(bi_instruction, v, &block->instructions, link)
443
444#define bi_foreach_instr_in_block_from(block, v, from) \
445 list_for_each_entry_from(bi_instruction, v, from, &block->instructions, link)
446
447#define bi_foreach_instr_in_block_from_rev(block, v, from) \
448 list_for_each_entry_from_rev(bi_instruction, v, from, &block->instructions, link)
449
450#define bi_foreach_clause_in_block(block, v) \
451 list_for_each_entry(bi_clause, v, &block->clauses, link)
452
453#define bi_foreach_instr_global(ctx, v) \
454 bi_foreach_block(ctx, v_block) \
455 bi_foreach_instr_in_block(v_block, v)
456
457#define bi_foreach_instr_global_safe(ctx, v) \
458 bi_foreach_block(ctx, v_block) \
459 bi_foreach_instr_in_block_safe(v_block, v)
460
461#define bi_foreach_successor(blk, v) \
462 bi_block *v; \
463 bi_block **_v; \
464 for (_v = &blk->successors[0], \
465 v = *_v; \
466 v != NULL && _v < &blk->successors[2]; \
467 _v++, v = *_v) \
468
469/* Based on set_foreach, expanded with automatic type casts */
470
471#define bi_foreach_predecessor(blk, v) \
472 struct set_entry *_entry_##v; \
473 bi_block *v; \
474 for (_entry_##v = _mesa_set_next_entry(blk->predecessors, NULL), \
475 v = (bi_block *) (_entry_##v ? _entry_##v->key : NULL); \
476 _entry_##v != NULL; \
477 _entry_##v = _mesa_set_next_entry(blk->predecessors, _entry_##v), \
478 v = (bi_block *) (_entry_##v ? _entry_##v->key : NULL))
479
480#define bi_foreach_src(ins, v) \
481 for (unsigned v = 0; v < ARRAY_SIZE(ins->src); ++v)
482
Alyssa Rosenzweig5d16a812020-03-04 09:19:06 -0500483/* BIR manipulation */
484
485bool bi_has_outmod(bi_instruction *ins);
486bool bi_has_source_mods(bi_instruction *ins);
487bool bi_is_src_swizzled(bi_instruction *ins, unsigned s);
488
Alyssa Rosenzweigeceaea42020-03-02 19:47:11 -0500489#endif