Alyssa Rosenzweig | 9b8cb9f | 2020-03-09 20:19:29 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 24 | #ifndef __PAN_IR_H |
| 25 | #define __PAN_IR_H |
| 26 | |
| 27 | #include <stdint.h> |
Alyssa Rosenzweig | e610267 | 2020-03-10 16:06:30 -0400 | [diff] [blame] | 28 | #include "compiler/nir/nir.h" |
Alyssa Rosenzweig | 3a4524e | 2020-03-10 15:41:56 -0400 | [diff] [blame] | 29 | #include "util/u_dynarray.h" |
Alyssa Rosenzweig | e610267 | 2020-03-10 16:06:30 -0400 | [diff] [blame] | 30 | #include "util/hash_table.h" |
Alyssa Rosenzweig | 3a4524e | 2020-03-10 15:41:56 -0400 | [diff] [blame] | 31 | |
| 32 | /* Define the general compiler entry point */ |
| 33 | |
| 34 | #define MAX_SYSVAL_COUNT 32 |
| 35 | |
| 36 | /* Allow 2D of sysval IDs, while allowing nonparametric sysvals to equal |
| 37 | * their class for equal comparison */ |
| 38 | |
| 39 | #define PAN_SYSVAL(type, no) (((no) << 16) | PAN_SYSVAL_##type) |
| 40 | #define PAN_SYSVAL_TYPE(sysval) ((sysval) & 0xffff) |
| 41 | #define PAN_SYSVAL_ID(sysval) ((sysval) >> 16) |
| 42 | |
| 43 | /* Define some common types. We start at one for easy indexing of hash |
| 44 | * tables internal to the compiler */ |
| 45 | |
| 46 | enum { |
| 47 | PAN_SYSVAL_VIEWPORT_SCALE = 1, |
| 48 | PAN_SYSVAL_VIEWPORT_OFFSET = 2, |
| 49 | PAN_SYSVAL_TEXTURE_SIZE = 3, |
| 50 | PAN_SYSVAL_SSBO = 4, |
| 51 | PAN_SYSVAL_NUM_WORK_GROUPS = 5, |
| 52 | PAN_SYSVAL_SAMPLER = 7, |
| 53 | }; |
| 54 | |
| 55 | #define PAN_TXS_SYSVAL_ID(texidx, dim, is_array) \ |
| 56 | ((texidx) | ((dim) << 7) | ((is_array) ? (1 << 9) : 0)) |
| 57 | |
| 58 | #define PAN_SYSVAL_ID_TO_TXS_TEX_IDX(id) ((id) & 0x7f) |
| 59 | #define PAN_SYSVAL_ID_TO_TXS_DIM(id) (((id) >> 7) & 0x3) |
| 60 | #define PAN_SYSVAL_ID_TO_TXS_IS_ARRAY(id) !!((id) & (1 << 9)) |
| 61 | |
| 62 | /* Special attribute slots for vertex builtins. Sort of arbitrary but let's be |
| 63 | * consistent with the blob so we can compare traces easier. */ |
| 64 | |
| 65 | enum { |
| 66 | PAN_VERTEX_ID = 16, |
| 67 | PAN_INSTANCE_ID = 17, |
| 68 | PAN_MAX_ATTRIBUTE |
| 69 | }; |
| 70 | |
Alyssa Rosenzweig | e610267 | 2020-03-10 16:06:30 -0400 | [diff] [blame] | 71 | struct panfrost_sysvals { |
| 72 | /* The mapping of sysvals to uniforms, the count, and the off-by-one inverse */ |
| 73 | unsigned sysvals[MAX_SYSVAL_COUNT]; |
| 74 | unsigned sysval_count; |
| 75 | struct hash_table_u64 *sysval_to_id; |
| 76 | }; |
| 77 | |
| 78 | void |
Alyssa Rosenzweig | 680fb05 | 2020-08-18 08:31:42 -0400 | [diff] [blame] | 79 | panfrost_nir_assign_sysvals(struct panfrost_sysvals *ctx, void *memctx, nir_shader *shader); |
Alyssa Rosenzweig | e610267 | 2020-03-10 16:06:30 -0400 | [diff] [blame] | 80 | |
| 81 | int |
| 82 | panfrost_sysval_for_instr(nir_instr *instr, nir_dest *dest); |
| 83 | |
Alyssa Rosenzweig | 3a4524e | 2020-03-10 15:41:56 -0400 | [diff] [blame] | 84 | typedef struct { |
| 85 | int work_register_count; |
Alyssa Rosenzweig | 3a4524e | 2020-03-10 15:41:56 -0400 | [diff] [blame] | 86 | int uniform_cutoff; |
| 87 | |
Alyssa Rosenzweig | 1a8f1a3 | 2020-04-23 19:26:01 -0400 | [diff] [blame] | 88 | /* For Bifrost - output type for each RT */ |
Alyssa Rosenzweig | 5a3088e | 2020-08-05 18:10:41 -0400 | [diff] [blame] | 89 | nir_alu_type blend_types[8]; |
Alyssa Rosenzweig | 1a8f1a3 | 2020-04-23 19:26:01 -0400 | [diff] [blame] | 90 | |
Alyssa Rosenzweig | 3a4524e | 2020-03-10 15:41:56 -0400 | [diff] [blame] | 91 | /* Prepended before uniforms, mapping to SYSVAL_ names for the |
| 92 | * sysval */ |
| 93 | |
| 94 | unsigned sysval_count; |
| 95 | unsigned sysvals[MAX_SYSVAL_COUNT]; |
| 96 | |
Alyssa Rosenzweig | 3a4524e | 2020-03-10 15:41:56 -0400 | [diff] [blame] | 97 | int first_tag; |
| 98 | |
| 99 | struct util_dynarray compiled; |
| 100 | |
| 101 | /* For a blend shader using a constant color -- patch point. If |
| 102 | * negative, there's no constant. */ |
| 103 | |
| 104 | int blend_patch_offset; |
| 105 | |
| 106 | /* The number of bytes to allocate per-thread for Thread Local Storage |
| 107 | * (register spilling), or zero if no spilling is used */ |
| 108 | unsigned tls_size; |
| 109 | |
Icecream95 | 75018f6 | 2020-07-06 19:40:05 +1200 | [diff] [blame] | 110 | /* IN: Render target formats for output load/store lowering */ |
| 111 | enum pipe_format rt_formats[8]; |
Alyssa Rosenzweig | 3a4524e | 2020-03-10 15:41:56 -0400 | [diff] [blame] | 112 | } panfrost_program; |
Alyssa Rosenzweig | 9b8cb9f | 2020-03-09 20:19:29 -0400 | [diff] [blame] | 113 | |
Alyssa Rosenzweig | 933e44d | 2020-03-11 13:58:10 -0400 | [diff] [blame] | 114 | typedef struct pan_block { |
| 115 | /* Link to next block. Must be first for mir_get_block */ |
| 116 | struct list_head link; |
| 117 | |
| 118 | /* List of instructions emitted for the current block */ |
| 119 | struct list_head instructions; |
| 120 | |
| 121 | /* Index of the block in source order */ |
| 122 | unsigned name; |
| 123 | |
| 124 | /* Control flow graph */ |
| 125 | struct pan_block *successors[2]; |
Alyssa Rosenzweig | 933e44d | 2020-03-11 13:58:10 -0400 | [diff] [blame] | 126 | struct set *predecessors; |
| 127 | |
| 128 | /* In liveness analysis, these are live masks (per-component) for |
| 129 | * indices for the block. Scalar compilers have the luxury of using |
| 130 | * simple bit fields, but for us, liveness is a vector idea. */ |
| 131 | uint16_t *live_in; |
| 132 | uint16_t *live_out; |
| 133 | } pan_block; |
| 134 | |
| 135 | struct pan_instruction { |
| 136 | struct list_head link; |
| 137 | }; |
| 138 | |
| 139 | #define pan_foreach_instr_in_block_rev(block, v) \ |
| 140 | list_for_each_entry_rev(struct pan_instruction, v, &block->instructions, link) |
| 141 | |
| 142 | #define pan_foreach_successor(blk, v) \ |
| 143 | pan_block *v; \ |
| 144 | pan_block **_v; \ |
| 145 | for (_v = (pan_block **) &blk->successors[0], \ |
| 146 | v = *_v; \ |
| 147 | v != NULL && _v < (pan_block **) &blk->successors[2]; \ |
| 148 | _v++, v = *_v) \ |
| 149 | |
| 150 | #define pan_foreach_predecessor(blk, v) \ |
| 151 | struct set_entry *_entry_##v; \ |
| 152 | struct pan_block *v; \ |
| 153 | for (_entry_##v = _mesa_set_next_entry(blk->predecessors, NULL), \ |
| 154 | v = (struct pan_block *) (_entry_##v ? _entry_##v->key : NULL); \ |
| 155 | _entry_##v != NULL; \ |
| 156 | _entry_##v = _mesa_set_next_entry(blk->predecessors, _entry_##v), \ |
| 157 | v = (struct pan_block *) (_entry_##v ? _entry_##v->key : NULL)) |
| 158 | |
Alyssa Rosenzweig | d429187 | 2020-05-12 13:19:23 -0400 | [diff] [blame] | 159 | static inline pan_block * |
| 160 | pan_exit_block(struct list_head *blocks) |
| 161 | { |
| 162 | pan_block *last = list_last_entry(blocks, pan_block, link); |
| 163 | assert(!last->successors[0] && !last->successors[1]); |
| 164 | return last; |
| 165 | } |
Alyssa Rosenzweig | 933e44d | 2020-03-11 13:58:10 -0400 | [diff] [blame] | 166 | |
| 167 | typedef void (*pan_liveness_update)(uint16_t *, void *, unsigned max); |
| 168 | |
| 169 | void pan_liveness_gen(uint16_t *live, unsigned node, unsigned max, uint16_t mask); |
| 170 | void pan_liveness_kill(uint16_t *live, unsigned node, unsigned max, uint16_t mask); |
| 171 | bool pan_liveness_get(uint16_t *live, unsigned node, uint16_t max); |
| 172 | |
| 173 | void pan_compute_liveness(struct list_head *blocks, |
| 174 | unsigned temp_count, |
| 175 | pan_liveness_update callback); |
| 176 | |
| 177 | void pan_free_liveness(struct list_head *blocks); |
| 178 | |
Alyssa Rosenzweig | 9b8cb9f | 2020-03-09 20:19:29 -0400 | [diff] [blame] | 179 | uint16_t |
| 180 | pan_to_bytemask(unsigned bytes, unsigned mask); |
| 181 | |
Alyssa Rosenzweig | 9b75f41 | 2020-03-11 14:35:38 -0400 | [diff] [blame] | 182 | void pan_block_add_successor(pan_block *block, pan_block *successor); |
| 183 | |
Alyssa Rosenzweig | 5860b18 | 2020-04-27 16:00:38 -0400 | [diff] [blame] | 184 | /* IR indexing */ |
| 185 | #define PAN_IS_REG (1) |
| 186 | |
| 187 | static inline unsigned |
| 188 | pan_ssa_index(nir_ssa_def *ssa) |
| 189 | { |
| 190 | /* Off-by-one ensures BIR_NO_ARG is skipped */ |
| 191 | return ((ssa->index + 1) << 1) | 0; |
| 192 | } |
| 193 | |
| 194 | static inline unsigned |
| 195 | pan_src_index(nir_src *src) |
| 196 | { |
| 197 | if (src->is_ssa) |
| 198 | return pan_ssa_index(src->ssa); |
| 199 | else { |
| 200 | assert(!src->reg.indirect); |
Alyssa Rosenzweig | fbbe3d4 | 2020-04-27 16:04:05 -0400 | [diff] [blame] | 201 | return (src->reg.reg->index << 1) | PAN_IS_REG; |
Alyssa Rosenzweig | 5860b18 | 2020-04-27 16:00:38 -0400 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | static inline unsigned |
| 206 | pan_dest_index(nir_dest *dst) |
| 207 | { |
| 208 | if (dst->is_ssa) |
| 209 | return pan_ssa_index(&dst->ssa); |
| 210 | else { |
| 211 | assert(!dst->reg.indirect); |
Alyssa Rosenzweig | fbbe3d4 | 2020-04-27 16:04:05 -0400 | [diff] [blame] | 212 | return (dst->reg.reg->index << 1) | PAN_IS_REG; |
Alyssa Rosenzweig | 5860b18 | 2020-04-27 16:00:38 -0400 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Alyssa Rosenzweig | b9f7f06 | 2020-04-27 18:43:01 -0400 | [diff] [blame] | 216 | /* IR printing helpers */ |
| 217 | void pan_print_alu_type(nir_alu_type t, FILE *fp); |
| 218 | |
Alyssa Rosenzweig | aeb5518 | 2020-04-29 17:51:03 -0400 | [diff] [blame] | 219 | /* Until it can be upstreamed.. */ |
| 220 | bool pan_has_source_mod(nir_alu_src *src, nir_op op); |
| 221 | bool pan_has_dest_mod(nir_dest **dest, nir_op op); |
| 222 | |
Alyssa Rosenzweig | 9b8cb9f | 2020-03-09 20:19:29 -0400 | [diff] [blame] | 223 | #endif |