2 * Copyright (C) 2020 Collabora Ltd.
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:
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
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
23 * Authors (Collabora):
24 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
27 #ifndef __BIFROST_COMPILER_H
28 #define __BIFROST_COMPILER_H
31 #include "compiler/nir/nir.h"
32 #include "panfrost/util/pan_ir.h"
34 /* Bifrost opcodes are tricky -- the same op may exist on both FMA and
35 * ADD with two completely different opcodes, and opcodes can be varying
36 * length in some cases. Then we have different opcodes for int vs float
37 * and then sometimes even for different typesizes. Further, virtually
38 * every op has a number of flags which depend on the op. In constrast
39 * to Midgard where you have a strict ALU/LDST/TEX division and within
40 * ALU you have strict int/float and that's it... here it's a *lot* more
41 * involved. As such, we use something much higher level for our IR,
42 * encoding "classes" of operations, letting the opcode details get
43 * sorted out at emit time.
45 * Please keep this list alphabetized. Please use a dictionary if you
46 * don't know how to do that.
73 BI_SPECIAL
, /* _FAST, _TABLE on supported GPUs */
80 /* Properties of a class... */
81 extern unsigned bi_class_props
[BI_NUM_CLASSES
];
83 /* abs/neg/outmod valid for a float op */
84 #define BI_MODS (1 << 0)
86 /* Generic enough that little class-specific information is required. In other
87 * words, it acts as a "normal" ALU op, even if the encoding ends up being
88 * irregular enough to warrant a separate class */
89 #define BI_GENERIC (1 << 1)
91 /* Accepts a bifrost_roundmode */
92 #define BI_ROUNDMODE (1 << 2)
94 /* Can be scheduled to FMA */
95 #define BI_SCHED_FMA (1 << 3)
97 /* Can be scheduled to ADD */
98 #define BI_SCHED_ADD (1 << 4)
100 /* Most ALU ops can do either, actually */
101 #define BI_SCHED_ALL (BI_SCHED_FMA | BI_SCHED_ADD)
103 /* Along with setting BI_SCHED_ADD, eats up the entire cycle, so FMA must be
104 * nopped out. Used for _FAST operations. */
105 #define BI_SCHED_SLOW (1 << 5)
107 /* Swizzling allowed for the 8/16-bit source */
108 #define BI_SWIZZLABLE (1 << 6)
110 /* For scheduling purposes this is a high latency instruction and must be at
111 * the end of a clause. Implies ADD */
112 #define BI_SCHED_HI_LATENCY ((1 << 7) | BI_SCHED_ADD)
114 /* It can't get any worse than csel4... can it? */
115 #define BIR_SRC_COUNT 4
117 /* Class-specific data for BI_LOAD, BI_LD_ATTR, BI_LD_VAR_ADDR */
119 /* Note: LD_ATTR does not support indirects */
122 /* Number of vector channels */
127 struct bi_load_vary
{
128 /* All parameters used here. Indirect location specified in
129 * src1 and ignoring location, if present. */
132 enum bifrost_interp_mode interp_mode
;
137 /* BI_BRANCH encoding the details of the branch itself as well as a pointer to
138 * the target. We forward declare bi_block since this is mildly circular (not
139 * strictly, but this order of the file makes more sense I think)
141 * We define our own enum of conditions since the conditions in the hardware
142 * packed in crazy ways that would make manipulation unweildly (meaning changes
143 * based on port swapping, etc), so we defer dealing with that until emit time.
144 * Likewise, we expose NIR types instead of the crazy branch types, although
145 * the restrictions do eventually apply of course. */
160 /* Types are specified in src_types and must be compatible (either both
161 * int, or both float, 16/32, and same size or 32/16 if float. Types
162 * ignored if BI_COND_ALWAYS is set for an unconditional branch. */
165 struct bi_block
*target
;
168 /* Opcodes within a class */
181 BI_ROUND_MODE
, /* use round mode */
182 BI_ROUND_ROUND
/* i.e.: fround() */
186 struct list_head link
; /* Must be first */
189 /* Indices, see bir_ssa_index etc. Note zero is special cased
190 * to "no argument" */
192 unsigned src
[BIR_SRC_COUNT
];
194 /* If one of the sources has BIR_INDEX_CONSTANT... Also, for
195 * BI_EXTRACT, the component index is stored here. */
203 /* Floating-point modifiers, type/class permitting. If not
204 * allowed for the type/class, these are ignored. */
205 enum bifrost_outmod outmod
;
206 bool src_abs
[BIR_SRC_COUNT
];
207 bool src_neg
[BIR_SRC_COUNT
];
209 /* Round mode (requires BI_ROUNDMODE) */
210 enum bifrost_roundmode roundmode
;
212 /* Destination type. Usually the type of the instruction
213 * itself, but if sources and destination have different
214 * types, the type of the destination wins (so f2i would be
215 * int). Zero if there is no destination. Bitsize included */
216 nir_alu_type dest_type
;
218 /* Source types if required by the class */
219 nir_alu_type src_types
[BIR_SRC_COUNT
];
221 /* If the source type is 8-bit or 16-bit such that SIMD is possible, and
222 * the class has BI_SWIZZLABLE, this is a swizzle for the input. Swizzles
223 * in practice only occur with one-source arguments (conversions,
224 * dedicated swizzle ops) and as component selection on two-sources
225 * where it is unambiguous which is which. Bounds are 32/type_size. */
228 /* A class-specific op from which the actual opcode can be derived
229 * (along with the above information) */
232 enum bi_minmax_op minmax
;
233 enum bi_bitwise_op bitwise
;
234 enum bi_round_op round
;
237 /* Union for class-specific information */
239 enum bifrost_minmax_mode minmax
;
241 struct bi_load_vary load_vary
;
242 struct bi_branch branch
;
244 /* For CSEL, the comparison op. BI_COND_ALWAYS doesn't make
245 * sense here but you can always just use a move for that */
246 enum bi_cond csel_cond
;
248 /* For BLEND -- the location 0-7 */
249 unsigned blend_location
;
253 /* Scheduling takes place in two steps. Step 1 groups instructions within a
254 * block into distinct clauses (bi_clause). Step 2 schedules instructions
255 * within a clause into FMA/ADD pairs (bi_bundle).
257 * A bi_bundle contains two paired instruction pointers. If a slot is unfilled,
258 * leave it NULL; the emitter will fill in a nop.
267 struct list_head link
;
269 /* A clause can have 8 instructions in bundled FMA/ADD sense, so there
270 * can be 8 bundles. But each bundle can have both an FMA and an ADD,
271 * so a clause can have up to 16 bi_instructions. Whether bundles or
272 * instructions are used depends on where in scheduling we are. */
274 unsigned instruction_count
;
275 unsigned bundle_count
;
278 bi_instruction
*instructions
[16];
279 bi_bundle bundles
[8];
282 /* For scoreboarding -- the clause ID (this is not globally unique!)
283 * and its dependencies in terms of other clauses, computed during
284 * scheduling and used when emitting code. Dependencies expressed as a
285 * bitfield matching the hardware, except shifted by a clause (the
286 * shift back to the ISA's off-by-one encoding is worked out when
287 * emitting clauses) */
288 unsigned scoreboard_id
;
289 uint8_t dependencies
;
291 /* Back-to-back corresponds directly to the back-to-back bit. Branch
292 * conditional corresponds to the branch conditional bit except that in
293 * the emitted code it's always set if back-to-bit is, whereas we use
294 * the actual value (without back-to-back so to speak) internally */
296 bool branch_conditional
;
298 /* Corresponds to the usual bit but shifted by a clause */
299 bool data_register_write_barrier
;
301 /* Constants read by this clause. ISA limit. */
302 uint64_t constants
[8];
303 unsigned constant_count
;
306 typedef struct bi_block
{
307 struct list_head link
; /* must be first */
308 unsigned name
; /* Just for pretty-printing */
310 /* If true, uses clauses; if false, uses instructions */
312 struct list_head instructions
; /* pre-schedule, list of bi_instructions */
313 struct list_head clauses
; /* list of bi_clause */
315 /* Control flow graph */
316 struct set
*predecessors
;
317 struct bi_block
*successors
[2];
322 gl_shader_stage stage
;
323 struct list_head blocks
; /* list of bi_block */
326 /* During NIR->BIR */
327 nir_function_impl
*impl
;
328 bi_block
*current_block
;
329 unsigned block_name_count
;
330 bi_block
*after_block
;
331 bi_block
*break_block
;
332 bi_block
*continue_block
;
335 /* For creating temporaries */
338 /* Stats for shader-db */
339 unsigned instruction_count
;
343 static inline bi_instruction
*
344 bi_emit(bi_context
*ctx
, bi_instruction ins
)
346 bi_instruction
*u
= rzalloc(ctx
, bi_instruction
);
347 memcpy(u
, &ins
, sizeof(ins
));
348 list_addtail(&u
->link
, &ctx
->current_block
->instructions
);
353 bi_remove_instruction(bi_instruction
*ins
)
355 list_del(&ins
->link
);
358 /* So we can distinguish between SSA/reg/sentinel quickly */
359 #define BIR_NO_ARG (0)
360 #define BIR_IS_REG (1)
362 /* If high bits are set, instead of SSA/registers, we have specials indexed by
363 * the low bits if necessary.
365 * Fixed register: do not allocate register, do not collect $200.
366 * Uniform: access a uniform register given by low bits.
367 * Constant: access the specified constant
368 * Zero: special cased to avoid wasting a constant
371 #define BIR_INDEX_REGISTER (1 << 31)
372 #define BIR_INDEX_UNIFORM (1 << 30)
373 #define BIR_INDEX_CONSTANT (1 << 29)
374 #define BIR_INDEX_ZERO (1 << 28)
376 /* Keep me synced please so we can check src & BIR_SPECIAL */
378 #define BIR_SPECIAL ((BIR_INDEX_REGISTER | BIR_INDEX_UNIFORM) | \
379 (BIR_INDEX_CONSTANT | BIR_INDEX_ZERO)
381 static inline unsigned
382 bi_make_temp(bi_context
*ctx
)
384 return (ctx
->impl
->ssa_alloc
+ 1 + ctx
->temp_alloc
++) << 1;
387 static inline unsigned
388 bi_make_temp_reg(bi_context
*ctx
)
390 return ((ctx
->impl
->reg_alloc
+ ctx
->temp_alloc
++) << 1) | BIR_IS_REG
;
393 static inline unsigned
394 bir_ssa_index(nir_ssa_def
*ssa
)
396 /* Off-by-one ensures BIR_NO_ARG is skipped */
397 return ((ssa
->index
+ 1) << 1) | 0;
400 static inline unsigned
401 bir_src_index(nir_src
*src
)
404 return bir_ssa_index(src
->ssa
);
406 assert(!src
->reg
.indirect
);
407 return (src
->reg
.reg
->index
<< 1) | BIR_IS_REG
;
411 static inline unsigned
412 bir_dest_index(nir_dest
*dst
)
415 return bir_ssa_index(&dst
->ssa
);
417 assert(!dst
->reg
.indirect
);
418 return (dst
->reg
.reg
->index
<< 1) | BIR_IS_REG
;
422 /* Iterators for Bifrost IR */
424 #define bi_foreach_block(ctx, v) \
425 list_for_each_entry(bi_block, v, &ctx->blocks, link)
427 #define bi_foreach_block_from(ctx, from, v) \
428 list_for_each_entry_from(bi_block, v, from, &ctx->blocks, link)
430 #define bi_foreach_instr_in_block(block, v) \
431 list_for_each_entry(bi_instruction, v, &block->instructions, link)
433 #define bi_foreach_instr_in_block_rev(block, v) \
434 list_for_each_entry_rev(bi_instruction, v, &block->instructions, link)
436 #define bi_foreach_instr_in_block_safe(block, v) \
437 list_for_each_entry_safe(bi_instruction, v, &block->instructions, link)
439 #define bi_foreach_instr_in_block_safe_rev(block, v) \
440 list_for_each_entry_safe_rev(bi_instruction, v, &block->instructions, link)
442 #define bi_foreach_instr_in_block_from(block, v, from) \
443 list_for_each_entry_from(bi_instruction, v, from, &block->instructions, link)
445 #define bi_foreach_instr_in_block_from_rev(block, v, from) \
446 list_for_each_entry_from_rev(bi_instruction, v, from, &block->instructions, link)
448 #define bi_foreach_clause_in_block(block, v) \
449 list_for_each_entry(bi_clause, v, &block->clauses, link)
451 #define bi_foreach_instr_global(ctx, v) \
452 bi_foreach_block(ctx, v_block) \
453 bi_foreach_instr_in_block(v_block, v)
455 #define bi_foreach_instr_global_safe(ctx, v) \
456 bi_foreach_block(ctx, v_block) \
457 bi_foreach_instr_in_block_safe(v_block, v)
459 #define bi_foreach_successor(blk, v) \
462 for (_v = &blk->successors[0], \
464 v != NULL && _v < &blk->successors[2]; \
467 /* Based on set_foreach, expanded with automatic type casts */
469 #define bi_foreach_predecessor(blk, v) \
470 struct set_entry *_entry_##v; \
472 for (_entry_##v = _mesa_set_next_entry(blk->predecessors, NULL), \
473 v = (bi_block *) (_entry_##v ? _entry_##v->key : NULL); \
474 _entry_##v != NULL; \
475 _entry_##v = _mesa_set_next_entry(blk->predecessors, _entry_##v), \
476 v = (bi_block *) (_entry_##v ? _entry_##v->key : NULL))
478 #define bi_foreach_src(ins, v) \
479 for (unsigned v = 0; v < ARRAY_SIZE(ins->src); ++v)
481 /* BIR manipulation */
483 bool bi_has_outmod(bi_instruction
*ins
);
484 bool bi_has_source_mods(bi_instruction
*ins
);
485 bool bi_is_src_swizzled(bi_instruction
*ins
, unsigned s
);
489 void bi_schedule(bi_context
*ctx
);