Alyssa Rosenzweig | eceaea4 | 2020-03-02 19:47:11 -0500 | [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 | * Authors (Collabora): |
| 24 | * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com> |
| 25 | */ |
| 26 | |
| 27 | #ifndef __BIFROST_COMPILER_H |
| 28 | #define __BIFROST_COMPILER_H |
| 29 | |
Alyssa Rosenzweig | 29acd7b | 2020-03-02 20:40:52 -0500 | [diff] [blame] | 30 | #include "bifrost.h" |
Alyssa Rosenzweig | 7ac6212 | 2020-03-02 20:38:26 -0500 | [diff] [blame] | 31 | #include "compiler/nir/nir.h" |
Alyssa Rosenzweig | 9b8cb9f | 2020-03-09 20:19:29 -0400 | [diff] [blame] | 32 | #include "panfrost/util/pan_ir.h" |
Alyssa Rosenzweig | 7ac6212 | 2020-03-02 20:38:26 -0500 | [diff] [blame] | 33 | |
Alyssa Rosenzweig | e7dc2a7 | 2020-03-02 20:06:34 -0500 | [diff] [blame] | 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. |
| 44 | * |
| 45 | * Please keep this list alphabetized. Please use a dictionary if you |
| 46 | * don't know how to do that. |
| 47 | */ |
| 48 | |
| 49 | enum bi_class { |
| 50 | BI_ADD, |
| 51 | BI_ATEST, |
| 52 | BI_BRANCH, |
| 53 | BI_CMP, |
| 54 | BI_BLEND, |
| 55 | BI_BITWISE, |
| 56 | BI_CONVERT, |
| 57 | BI_CSEL, |
| 58 | BI_DISCARD, |
| 59 | BI_FMA, |
| 60 | BI_FREXP, |
| 61 | BI_LOAD, |
Alyssa Rosenzweig | 1ead0d3 | 2020-03-06 09:52:09 -0500 | [diff] [blame] | 62 | BI_LOAD_UNIFORM, |
Alyssa Rosenzweig | e7dc2a7 | 2020-03-02 20:06:34 -0500 | [diff] [blame] | 63 | BI_LOAD_ATTR, |
| 64 | BI_LOAD_VAR, |
| 65 | BI_LOAD_VAR_ADDRESS, |
| 66 | BI_MINMAX, |
| 67 | BI_MOV, |
| 68 | BI_SHIFT, |
| 69 | BI_STORE, |
| 70 | BI_STORE_VAR, |
| 71 | BI_SPECIAL, /* _FAST, _TABLE on supported GPUs */ |
Alyssa Rosenzweig | 5896db9 | 2020-03-03 08:35:51 -0500 | [diff] [blame] | 72 | BI_SWIZZLE, |
Alyssa Rosenzweig | e7dc2a7 | 2020-03-02 20:06:34 -0500 | [diff] [blame] | 73 | BI_TEX, |
| 74 | BI_ROUND, |
Alyssa Rosenzweig | 7ac6212 | 2020-03-02 20:38:26 -0500 | [diff] [blame] | 75 | BI_NUM_CLASSES |
Alyssa Rosenzweig | e7dc2a7 | 2020-03-02 20:06:34 -0500 | [diff] [blame] | 76 | }; |
| 77 | |
Alyssa Rosenzweig | 7ac6212 | 2020-03-02 20:38:26 -0500 | [diff] [blame] | 78 | /* Properties of a class... */ |
| 79 | extern unsigned bi_class_props[BI_NUM_CLASSES]; |
| 80 | |
| 81 | /* abs/neg/outmod valid for a float op */ |
| 82 | #define BI_MODS (1 << 0) |
| 83 | |
Alyssa Rosenzweig | 34165c7 | 2020-03-02 20:46:37 -0500 | [diff] [blame] | 84 | /* Generic enough that little class-specific information is required. In other |
| 85 | * words, it acts as a "normal" ALU op, even if the encoding ends up being |
| 86 | * irregular enough to warrant a separate class */ |
| 87 | #define BI_GENERIC (1 << 1) |
| 88 | |
Alyssa Rosenzweig | d69bf8d | 2020-03-02 20:52:36 -0500 | [diff] [blame] | 89 | /* Accepts a bifrost_roundmode */ |
| 90 | #define BI_ROUNDMODE (1 << 2) |
| 91 | |
Alyssa Rosenzweig | 99f3c1f | 2020-03-02 21:53:13 -0500 | [diff] [blame] | 92 | /* Can be scheduled to FMA */ |
| 93 | #define BI_SCHED_FMA (1 << 3) |
| 94 | |
| 95 | /* Can be scheduled to ADD */ |
| 96 | #define BI_SCHED_ADD (1 << 4) |
| 97 | |
| 98 | /* Most ALU ops can do either, actually */ |
| 99 | #define BI_SCHED_ALL (BI_SCHED_FMA | BI_SCHED_ADD) |
| 100 | |
Alyssa Rosenzweig | c70a198 | 2020-03-03 08:16:50 -0500 | [diff] [blame] | 101 | /* Along with setting BI_SCHED_ADD, eats up the entire cycle, so FMA must be |
| 102 | * nopped out. Used for _FAST operations. */ |
| 103 | #define BI_SCHED_SLOW (1 << 5) |
| 104 | |
Alyssa Rosenzweig | 5896db9 | 2020-03-03 08:35:51 -0500 | [diff] [blame] | 105 | /* Swizzling allowed for the 8/16-bit source */ |
| 106 | #define BI_SWIZZLABLE (1 << 6) |
| 107 | |
Alyssa Rosenzweig | 07228a6 | 2020-03-03 13:55:33 -0500 | [diff] [blame] | 108 | /* For scheduling purposes this is a high latency instruction and must be at |
| 109 | * the end of a clause. Implies ADD */ |
| 110 | #define BI_SCHED_HI_LATENCY ((1 << 7) | BI_SCHED_ADD) |
| 111 | |
Alyssa Rosenzweig | 230be61 | 2020-03-02 20:24:03 -0500 | [diff] [blame] | 112 | /* It can't get any worse than csel4... can it? */ |
| 113 | #define BIR_SRC_COUNT 4 |
| 114 | |
Alyssa Rosenzweig | 1ead0d3 | 2020-03-06 09:52:09 -0500 | [diff] [blame] | 115 | /* Class-specific data for BI_LOAD, BI_LD_ATTR, BI_LD_VAR_ADDR */ |
Alyssa Rosenzweig | aa2f12d | 2020-03-02 21:19:16 -0500 | [diff] [blame] | 116 | struct bi_load { |
Alyssa Rosenzweig | 1ead0d3 | 2020-03-06 09:52:09 -0500 | [diff] [blame] | 117 | /* Note: LD_ATTR does not support indirects */ |
Alyssa Rosenzweig | aa2f12d | 2020-03-02 21:19:16 -0500 | [diff] [blame] | 118 | unsigned location; |
| 119 | |
Alyssa Rosenzweig | 1ead0d3 | 2020-03-06 09:52:09 -0500 | [diff] [blame] | 120 | /* Number of vector channels */ |
Alyssa Rosenzweig | aa2f12d | 2020-03-02 21:19:16 -0500 | [diff] [blame] | 121 | unsigned channels; |
| 122 | }; |
| 123 | |
Alyssa Rosenzweig | 9643b9d | 2020-03-02 21:48:51 -0500 | [diff] [blame] | 124 | /* BI_LD_VARY */ |
| 125 | struct bi_load_vary { |
| 126 | /* All parameters used here. Indirect location specified in |
| 127 | * src1 and ignoring location, if present. */ |
| 128 | struct bi_load load; |
| 129 | |
| 130 | enum bifrost_interp_mode interp_mode; |
| 131 | bool reuse; |
| 132 | bool flat; |
| 133 | }; |
| 134 | |
Alyssa Rosenzweig | 47451bb | 2020-03-03 13:48:13 -0500 | [diff] [blame] | 135 | /* BI_BRANCH encoding the details of the branch itself as well as a pointer to |
| 136 | * the target. We forward declare bi_block since this is mildly circular (not |
| 137 | * strictly, but this order of the file makes more sense I think) |
| 138 | * |
| 139 | * We define our own enum of conditions since the conditions in the hardware |
| 140 | * packed in crazy ways that would make manipulation unweildly (meaning changes |
| 141 | * based on port swapping, etc), so we defer dealing with that until emit time. |
| 142 | * Likewise, we expose NIR types instead of the crazy branch types, although |
| 143 | * the restrictions do eventually apply of course. */ |
| 144 | |
| 145 | struct bi_block; |
| 146 | |
| 147 | enum bi_cond { |
| 148 | BI_COND_ALWAYS, |
| 149 | BI_COND_LT, |
| 150 | BI_COND_LE, |
| 151 | BI_COND_GE, |
| 152 | BI_COND_GT, |
| 153 | BI_COND_EQ, |
| 154 | BI_COND_NE, |
| 155 | }; |
| 156 | |
| 157 | struct bi_branch { |
| 158 | /* Types are specified in src_types and must be compatible (either both |
| 159 | * int, or both float, 16/32, and same size or 32/16 if float. Types |
| 160 | * ignored if BI_COND_ALWAYS is set for an unconditional branch. */ |
| 161 | |
| 162 | enum bi_cond cond; |
| 163 | struct bi_block *target; |
| 164 | }; |
| 165 | |
Alyssa Rosenzweig | 44ebc27 | 2020-03-03 07:58:05 -0500 | [diff] [blame] | 166 | /* Opcodes within a class */ |
| 167 | enum bi_minmax_op { |
| 168 | BI_MINMAX_MIN, |
| 169 | BI_MINMAX_MAX |
| 170 | }; |
| 171 | |
| 172 | enum bi_bitwise_op { |
| 173 | BI_BITWISE_AND, |
| 174 | BI_BITWISE_OR, |
| 175 | BI_BITWISE_XOR |
| 176 | }; |
| 177 | |
| 178 | enum bi_round_op { |
| 179 | BI_ROUND_MODE, /* use round mode */ |
| 180 | BI_ROUND_ROUND /* i.e.: fround() */ |
| 181 | }; |
| 182 | |
Alyssa Rosenzweig | e7dc2a7 | 2020-03-02 20:06:34 -0500 | [diff] [blame] | 183 | typedef struct { |
| 184 | struct list_head link; /* Must be first */ |
| 185 | enum bi_class type; |
Alyssa Rosenzweig | 230be61 | 2020-03-02 20:24:03 -0500 | [diff] [blame] | 186 | |
| 187 | /* Indices, see bir_ssa_index etc. Note zero is special cased |
| 188 | * to "no argument" */ |
| 189 | unsigned dest; |
| 190 | unsigned src[BIR_SRC_COUNT]; |
Alyssa Rosenzweig | 29acd7b | 2020-03-02 20:40:52 -0500 | [diff] [blame] | 191 | |
Alyssa Rosenzweig | 795646d | 2020-03-09 14:09:04 -0400 | [diff] [blame] | 192 | /* If one of the sources has BIR_INDEX_CONSTANT */ |
Alyssa Rosenzweig | b5bdd89 | 2020-03-03 07:47:29 -0500 | [diff] [blame] | 193 | union { |
| 194 | uint64_t u64; |
| 195 | uint32_t u32; |
| 196 | uint16_t u16[2]; |
| 197 | uint8_t u8[4]; |
| 198 | } constant; |
| 199 | |
Alyssa Rosenzweig | 29acd7b | 2020-03-02 20:40:52 -0500 | [diff] [blame] | 200 | /* Floating-point modifiers, type/class permitting. If not |
| 201 | * allowed for the type/class, these are ignored. */ |
| 202 | enum bifrost_outmod outmod; |
| 203 | bool src_abs[BIR_SRC_COUNT]; |
| 204 | bool src_neg[BIR_SRC_COUNT]; |
Alyssa Rosenzweig | d69bf8d | 2020-03-02 20:52:36 -0500 | [diff] [blame] | 205 | |
| 206 | /* Round mode (requires BI_ROUNDMODE) */ |
| 207 | enum bifrost_roundmode roundmode; |
Alyssa Rosenzweig | b93aec6 | 2020-03-02 20:53:47 -0500 | [diff] [blame] | 208 | |
Alyssa Rosenzweig | e9d480c | 2020-03-09 14:25:00 -0400 | [diff] [blame^] | 209 | /* Writemask (bit for each affected byte). This is quite restricted -- |
| 210 | * ALU ops can only write to a single channel (exception: <32 in which |
| 211 | * you can write to 32/N contiguous aligned channels). Load/store can |
| 212 | * only write to all channels at once, in a sense. But it's still |
| 213 | * better to use this generic form than have synthetic ops flying |
| 214 | * about, since we're not essentially vector for RA purposes. */ |
| 215 | uint16_t writemask; |
| 216 | |
Alyssa Rosenzweig | c42002d | 2020-03-02 22:03:05 -0500 | [diff] [blame] | 217 | /* Destination type. Usually the type of the instruction |
| 218 | * itself, but if sources and destination have different |
| 219 | * types, the type of the destination wins (so f2i would be |
| 220 | * int). Zero if there is no destination. Bitsize included */ |
| 221 | nir_alu_type dest_type; |
| 222 | |
Alyssa Rosenzweig | 8929fe0 | 2020-03-03 08:37:15 -0500 | [diff] [blame] | 223 | /* Source types if required by the class */ |
| 224 | nir_alu_type src_types[BIR_SRC_COUNT]; |
| 225 | |
Alyssa Rosenzweig | 795646d | 2020-03-09 14:09:04 -0400 | [diff] [blame] | 226 | /* If the source type is 8-bit or 16-bit such that SIMD is possible, |
| 227 | * and the class has BI_SWIZZLABLE, this is a swizzle in the usual |
| 228 | * sense. On non-SIMD instructions, it can be used for component |
| 229 | * selection, so we don't have to special case extraction. */ |
| 230 | uint8_t swizzle[BIR_SRC_COUNT][NIR_MAX_VEC_COMPONENTS]; |
Alyssa Rosenzweig | 5896db9 | 2020-03-03 08:35:51 -0500 | [diff] [blame] | 231 | |
Alyssa Rosenzweig | 44ebc27 | 2020-03-03 07:58:05 -0500 | [diff] [blame] | 232 | /* A class-specific op from which the actual opcode can be derived |
| 233 | * (along with the above information) */ |
| 234 | |
| 235 | union { |
| 236 | enum bi_minmax_op minmax; |
| 237 | enum bi_bitwise_op bitwise; |
| 238 | enum bi_round_op round; |
| 239 | } op; |
| 240 | |
Alyssa Rosenzweig | b93aec6 | 2020-03-02 20:53:47 -0500 | [diff] [blame] | 241 | /* Union for class-specific information */ |
| 242 | union { |
| 243 | enum bifrost_minmax_mode minmax; |
Alyssa Rosenzweig | aa2f12d | 2020-03-02 21:19:16 -0500 | [diff] [blame] | 244 | struct bi_load load; |
Alyssa Rosenzweig | 9643b9d | 2020-03-02 21:48:51 -0500 | [diff] [blame] | 245 | struct bi_load_vary load_vary; |
Alyssa Rosenzweig | 47451bb | 2020-03-03 13:48:13 -0500 | [diff] [blame] | 246 | struct bi_branch branch; |
Alyssa Rosenzweig | 546c301 | 2020-03-05 07:46:00 -0500 | [diff] [blame] | 247 | |
| 248 | /* For CSEL, the comparison op. BI_COND_ALWAYS doesn't make |
| 249 | * sense here but you can always just use a move for that */ |
| 250 | enum bi_cond csel_cond; |
Alyssa Rosenzweig | 92a4f26 | 2020-03-06 09:25:58 -0500 | [diff] [blame] | 251 | |
| 252 | /* For BLEND -- the location 0-7 */ |
| 253 | unsigned blend_location; |
Alyssa Rosenzweig | b93aec6 | 2020-03-02 20:53:47 -0500 | [diff] [blame] | 254 | }; |
Alyssa Rosenzweig | e7dc2a7 | 2020-03-02 20:06:34 -0500 | [diff] [blame] | 255 | } bi_instruction; |
| 256 | |
Alyssa Rosenzweig | a35854c | 2020-03-02 22:00:07 -0500 | [diff] [blame] | 257 | /* Scheduling takes place in two steps. Step 1 groups instructions within a |
| 258 | * block into distinct clauses (bi_clause). Step 2 schedules instructions |
| 259 | * within a clause into FMA/ADD pairs (bi_bundle). |
| 260 | * |
| 261 | * A bi_bundle contains two paired instruction pointers. If a slot is unfilled, |
| 262 | * leave it NULL; the emitter will fill in a nop. |
| 263 | */ |
| 264 | |
Alyssa Rosenzweig | e7dc2a7 | 2020-03-02 20:06:34 -0500 | [diff] [blame] | 265 | typedef struct { |
Alyssa Rosenzweig | a35854c | 2020-03-02 22:00:07 -0500 | [diff] [blame] | 266 | bi_instruction *fma; |
| 267 | bi_instruction *add; |
| 268 | } bi_bundle; |
| 269 | |
| 270 | typedef struct { |
| 271 | struct list_head link; |
| 272 | |
| 273 | /* A clause can have 8 instructions in bundled FMA/ADD sense, so there |
| 274 | * can be 8 bundles. But each bundle can have both an FMA and an ADD, |
| 275 | * so a clause can have up to 16 bi_instructions. Whether bundles or |
| 276 | * instructions are used depends on where in scheduling we are. */ |
| 277 | |
| 278 | unsigned instruction_count; |
| 279 | unsigned bundle_count; |
| 280 | |
| 281 | union { |
| 282 | bi_instruction *instructions[16]; |
| 283 | bi_bundle bundles[8]; |
| 284 | }; |
Alyssa Rosenzweig | fba1d12 | 2020-03-03 08:09:18 -0500 | [diff] [blame] | 285 | |
| 286 | /* For scoreboarding -- the clause ID (this is not globally unique!) |
| 287 | * and its dependencies in terms of other clauses, computed during |
| 288 | * scheduling and used when emitting code. Dependencies expressed as a |
| 289 | * bitfield matching the hardware, except shifted by a clause (the |
| 290 | * shift back to the ISA's off-by-one encoding is worked out when |
| 291 | * emitting clauses) */ |
| 292 | unsigned scoreboard_id; |
| 293 | uint8_t dependencies; |
| 294 | |
| 295 | /* Back-to-back corresponds directly to the back-to-back bit. Branch |
| 296 | * conditional corresponds to the branch conditional bit except that in |
| 297 | * the emitted code it's always set if back-to-bit is, whereas we use |
| 298 | * the actual value (without back-to-back so to speak) internally */ |
| 299 | bool back_to_back; |
| 300 | bool branch_conditional; |
| 301 | |
| 302 | /* Corresponds to the usual bit but shifted by a clause */ |
| 303 | bool data_register_write_barrier; |
Alyssa Rosenzweig | d3370bd | 2020-03-03 13:01:41 -0500 | [diff] [blame] | 304 | |
| 305 | /* Constants read by this clause. ISA limit. */ |
| 306 | uint64_t constants[8]; |
| 307 | unsigned constant_count; |
Alyssa Rosenzweig | a35854c | 2020-03-02 22:00:07 -0500 | [diff] [blame] | 308 | } bi_clause; |
| 309 | |
| 310 | typedef struct bi_block { |
Alyssa Rosenzweig | e7dc2a7 | 2020-03-02 20:06:34 -0500 | [diff] [blame] | 311 | struct list_head link; /* must be first */ |
Alyssa Rosenzweig | a35854c | 2020-03-02 22:00:07 -0500 | [diff] [blame] | 312 | unsigned name; /* Just for pretty-printing */ |
| 313 | |
| 314 | /* If true, uses clauses; if false, uses instructions */ |
| 315 | bool scheduled; |
Alyssa Rosenzweig | b329f8c | 2020-03-06 19:25:00 -0500 | [diff] [blame] | 316 | struct list_head instructions; /* pre-schedule, list of bi_instructions */ |
| 317 | struct list_head clauses; /* list of bi_clause */ |
Alyssa Rosenzweig | 2afddc4 | 2020-03-03 13:47:13 -0500 | [diff] [blame] | 318 | |
| 319 | /* Control flow graph */ |
| 320 | struct set *predecessors; |
| 321 | struct bi_block *successors[2]; |
Alyssa Rosenzweig | e7dc2a7 | 2020-03-02 20:06:34 -0500 | [diff] [blame] | 322 | } bi_block; |
| 323 | |
Alyssa Rosenzweig | eceaea4 | 2020-03-02 19:47:11 -0500 | [diff] [blame] | 324 | typedef struct { |
| 325 | nir_shader *nir; |
Alyssa Rosenzweig | 0d29184 | 2020-03-05 10:11:39 -0500 | [diff] [blame] | 326 | gl_shader_stage stage; |
Alyssa Rosenzweig | e7dc2a7 | 2020-03-02 20:06:34 -0500 | [diff] [blame] | 327 | struct list_head blocks; /* list of bi_block */ |
Alyssa Rosenzweig | 0b26cb1 | 2020-03-03 14:27:05 -0500 | [diff] [blame] | 328 | uint32_t quirks; |
Alyssa Rosenzweig | 83c4562 | 2020-03-05 10:25:19 -0500 | [diff] [blame] | 329 | |
| 330 | /* During NIR->BIR */ |
Alyssa Rosenzweig | d86659c | 2020-03-06 09:43:43 -0500 | [diff] [blame] | 331 | nir_function_impl *impl; |
Alyssa Rosenzweig | 83c4562 | 2020-03-05 10:25:19 -0500 | [diff] [blame] | 332 | bi_block *current_block; |
| 333 | unsigned block_name_count; |
Alyssa Rosenzweig | 55dab92 | 2020-03-05 16:44:49 -0500 | [diff] [blame] | 334 | bi_block *after_block; |
Alyssa Rosenzweig | 987aea1 | 2020-03-05 17:03:53 -0500 | [diff] [blame] | 335 | bi_block *break_block; |
| 336 | bi_block *continue_block; |
Alyssa Rosenzweig | dabb6c6 | 2020-03-06 09:26:44 -0500 | [diff] [blame] | 337 | bool emitted_atest; |
Alyssa Rosenzweig | 83c4562 | 2020-03-05 10:25:19 -0500 | [diff] [blame] | 338 | |
Alyssa Rosenzweig | d86659c | 2020-03-06 09:43:43 -0500 | [diff] [blame] | 339 | /* For creating temporaries */ |
| 340 | unsigned temp_alloc; |
| 341 | |
Alyssa Rosenzweig | 83c4562 | 2020-03-05 10:25:19 -0500 | [diff] [blame] | 342 | /* Stats for shader-db */ |
| 343 | unsigned instruction_count; |
Alyssa Rosenzweig | 987aea1 | 2020-03-05 17:03:53 -0500 | [diff] [blame] | 344 | unsigned loop_count; |
Alyssa Rosenzweig | 55dab92 | 2020-03-05 16:44:49 -0500 | [diff] [blame] | 345 | } bi_context; |
| 346 | |
| 347 | static inline bi_instruction * |
| 348 | bi_emit(bi_context *ctx, bi_instruction ins) |
| 349 | { |
| 350 | bi_instruction *u = rzalloc(ctx, bi_instruction); |
| 351 | memcpy(u, &ins, sizeof(ins)); |
| 352 | list_addtail(&u->link, &ctx->current_block->instructions); |
| 353 | return u; |
| 354 | } |
| 355 | |
| 356 | static inline void |
| 357 | bi_remove_instruction(bi_instruction *ins) |
| 358 | { |
| 359 | list_del(&ins->link); |
| 360 | } |
Alyssa Rosenzweig | eceaea4 | 2020-03-02 19:47:11 -0500 | [diff] [blame] | 361 | |
Alyssa Rosenzweig | 230be61 | 2020-03-02 20:24:03 -0500 | [diff] [blame] | 362 | /* So we can distinguish between SSA/reg/sentinel quickly */ |
| 363 | #define BIR_NO_ARG (0) |
| 364 | #define BIR_IS_REG (1) |
| 365 | |
Alyssa Rosenzweig | a2c1265 | 2020-03-03 07:45:33 -0500 | [diff] [blame] | 366 | /* If high bits are set, instead of SSA/registers, we have specials indexed by |
| 367 | * the low bits if necessary. |
| 368 | * |
| 369 | * Fixed register: do not allocate register, do not collect $200. |
| 370 | * Uniform: access a uniform register given by low bits. |
| 371 | * Constant: access the specified constant |
| 372 | * Zero: special cased to avoid wasting a constant |
| 373 | */ |
| 374 | |
| 375 | #define BIR_INDEX_REGISTER (1 << 31) |
| 376 | #define BIR_INDEX_UNIFORM (1 << 30) |
| 377 | #define BIR_INDEX_CONSTANT (1 << 29) |
| 378 | #define BIR_INDEX_ZERO (1 << 28) |
| 379 | |
| 380 | /* Keep me synced please so we can check src & BIR_SPECIAL */ |
| 381 | |
| 382 | #define BIR_SPECIAL ((BIR_INDEX_REGISTER | BIR_INDEX_UNIFORM) | \ |
| 383 | (BIR_INDEX_CONSTANT | BIR_INDEX_ZERO) |
| 384 | |
Alyssa Rosenzweig | 230be61 | 2020-03-02 20:24:03 -0500 | [diff] [blame] | 385 | static inline unsigned |
Alyssa Rosenzweig | d86659c | 2020-03-06 09:43:43 -0500 | [diff] [blame] | 386 | bi_make_temp(bi_context *ctx) |
| 387 | { |
| 388 | return (ctx->impl->ssa_alloc + 1 + ctx->temp_alloc++) << 1; |
| 389 | } |
| 390 | |
| 391 | static inline unsigned |
| 392 | bi_make_temp_reg(bi_context *ctx) |
| 393 | { |
| 394 | return ((ctx->impl->reg_alloc + ctx->temp_alloc++) << 1) | BIR_IS_REG; |
| 395 | } |
| 396 | |
| 397 | static inline unsigned |
Alyssa Rosenzweig | 230be61 | 2020-03-02 20:24:03 -0500 | [diff] [blame] | 398 | bir_ssa_index(nir_ssa_def *ssa) |
| 399 | { |
| 400 | /* Off-by-one ensures BIR_NO_ARG is skipped */ |
| 401 | return ((ssa->index + 1) << 1) | 0; |
| 402 | } |
| 403 | |
| 404 | static inline unsigned |
| 405 | bir_src_index(nir_src *src) |
| 406 | { |
| 407 | if (src->is_ssa) |
| 408 | return bir_ssa_index(src->ssa); |
| 409 | else { |
| 410 | assert(!src->reg.indirect); |
| 411 | return (src->reg.reg->index << 1) | BIR_IS_REG; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | static inline unsigned |
| 416 | bir_dest_index(nir_dest *dst) |
| 417 | { |
| 418 | if (dst->is_ssa) |
| 419 | return bir_ssa_index(&dst->ssa); |
| 420 | else { |
| 421 | assert(!dst->reg.indirect); |
| 422 | return (dst->reg.reg->index << 1) | BIR_IS_REG; |
| 423 | } |
| 424 | } |
| 425 | |
Alyssa Rosenzweig | 8ec6718 | 2020-03-03 14:32:28 -0500 | [diff] [blame] | 426 | /* Iterators for Bifrost IR */ |
| 427 | |
| 428 | #define bi_foreach_block(ctx, v) \ |
| 429 | list_for_each_entry(bi_block, v, &ctx->blocks, link) |
| 430 | |
| 431 | #define bi_foreach_block_from(ctx, from, v) \ |
| 432 | list_for_each_entry_from(bi_block, v, from, &ctx->blocks, link) |
| 433 | |
| 434 | #define bi_foreach_instr_in_block(block, v) \ |
| 435 | list_for_each_entry(bi_instruction, v, &block->instructions, link) |
| 436 | |
| 437 | #define bi_foreach_instr_in_block_rev(block, v) \ |
| 438 | list_for_each_entry_rev(bi_instruction, v, &block->instructions, link) |
| 439 | |
| 440 | #define bi_foreach_instr_in_block_safe(block, v) \ |
| 441 | list_for_each_entry_safe(bi_instruction, v, &block->instructions, link) |
| 442 | |
| 443 | #define bi_foreach_instr_in_block_safe_rev(block, v) \ |
| 444 | list_for_each_entry_safe_rev(bi_instruction, v, &block->instructions, link) |
| 445 | |
| 446 | #define bi_foreach_instr_in_block_from(block, v, from) \ |
| 447 | list_for_each_entry_from(bi_instruction, v, from, &block->instructions, link) |
| 448 | |
| 449 | #define bi_foreach_instr_in_block_from_rev(block, v, from) \ |
| 450 | list_for_each_entry_from_rev(bi_instruction, v, from, &block->instructions, link) |
| 451 | |
| 452 | #define bi_foreach_clause_in_block(block, v) \ |
| 453 | list_for_each_entry(bi_clause, v, &block->clauses, link) |
| 454 | |
| 455 | #define bi_foreach_instr_global(ctx, v) \ |
| 456 | bi_foreach_block(ctx, v_block) \ |
| 457 | bi_foreach_instr_in_block(v_block, v) |
| 458 | |
| 459 | #define bi_foreach_instr_global_safe(ctx, v) \ |
| 460 | bi_foreach_block(ctx, v_block) \ |
| 461 | bi_foreach_instr_in_block_safe(v_block, v) |
| 462 | |
| 463 | #define bi_foreach_successor(blk, v) \ |
| 464 | bi_block *v; \ |
| 465 | bi_block **_v; \ |
| 466 | for (_v = &blk->successors[0], \ |
| 467 | v = *_v; \ |
| 468 | v != NULL && _v < &blk->successors[2]; \ |
| 469 | _v++, v = *_v) \ |
| 470 | |
| 471 | /* Based on set_foreach, expanded with automatic type casts */ |
| 472 | |
| 473 | #define bi_foreach_predecessor(blk, v) \ |
| 474 | struct set_entry *_entry_##v; \ |
| 475 | bi_block *v; \ |
| 476 | for (_entry_##v = _mesa_set_next_entry(blk->predecessors, NULL), \ |
| 477 | v = (bi_block *) (_entry_##v ? _entry_##v->key : NULL); \ |
| 478 | _entry_##v != NULL; \ |
| 479 | _entry_##v = _mesa_set_next_entry(blk->predecessors, _entry_##v), \ |
| 480 | v = (bi_block *) (_entry_##v ? _entry_##v->key : NULL)) |
| 481 | |
| 482 | #define bi_foreach_src(ins, v) \ |
| 483 | for (unsigned v = 0; v < ARRAY_SIZE(ins->src); ++v) |
| 484 | |
Alyssa Rosenzweig | 5d16a81 | 2020-03-04 09:19:06 -0500 | [diff] [blame] | 485 | /* BIR manipulation */ |
| 486 | |
| 487 | bool bi_has_outmod(bi_instruction *ins); |
| 488 | bool bi_has_source_mods(bi_instruction *ins); |
| 489 | bool bi_is_src_swizzled(bi_instruction *ins, unsigned s); |
| 490 | |
Alyssa Rosenzweig | b329f8c | 2020-03-06 19:25:00 -0500 | [diff] [blame] | 491 | /* BIR passes */ |
| 492 | |
| 493 | void bi_schedule(bi_context *ctx); |
| 494 | |
Alyssa Rosenzweig | eceaea4 | 2020-03-02 19:47:11 -0500 | [diff] [blame] | 495 | #endif |