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