blob: d0a5208d388e607cdd8004306ab2acae17802e9c [file] [log] [blame]
Alyssa Rosenzweig9b8cb9f2020-03-09 20:19:29 -04001/*
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 Rosenzweige6102672020-03-10 16:06:30 -040028#include "compiler/nir/nir.h"
Alyssa Rosenzweig3a4524e2020-03-10 15:41:56 -040029#include "util/u_dynarray.h"
Alyssa Rosenzweige6102672020-03-10 16:06:30 -040030#include "util/hash_table.h"
Alyssa Rosenzweig3a4524e2020-03-10 15:41:56 -040031
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
46enum {
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
65enum {
66 PAN_VERTEX_ID = 16,
67 PAN_INSTANCE_ID = 17,
68 PAN_MAX_ATTRIBUTE
69};
70
Alyssa Rosenzweige6102672020-03-10 16:06:30 -040071struct 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
78void
Alyssa Rosenzweig680fb052020-08-18 08:31:42 -040079panfrost_nir_assign_sysvals(struct panfrost_sysvals *ctx, void *memctx, nir_shader *shader);
Alyssa Rosenzweige6102672020-03-10 16:06:30 -040080
81int
82panfrost_sysval_for_instr(nir_instr *instr, nir_dest *dest);
83
Alyssa Rosenzweig86b2b4e2020-10-15 08:45:24 -040084bool
85nir_undef_to_zero(nir_shader *shader);
86
Alyssa Rosenzweig3a4524e2020-03-10 15:41:56 -040087typedef struct {
88 int work_register_count;
Alyssa Rosenzweig3a4524e2020-03-10 15:41:56 -040089 int uniform_cutoff;
90
Alyssa Rosenzweig1a8f1a32020-04-23 19:26:01 -040091 /* For Bifrost - output type for each RT */
Alyssa Rosenzweig5a3088e2020-08-05 18:10:41 -040092 nir_alu_type blend_types[8];
Alyssa Rosenzweig1a8f1a32020-04-23 19:26:01 -040093
Boris Brezillon2f3f5da2020-10-13 12:26:11 +020094 /* For Bifrost - return address for blend instructions */
95 uint32_t blend_ret_offsets[8];
96
Alyssa Rosenzweig3a4524e2020-03-10 15:41:56 -040097 /* Prepended before uniforms, mapping to SYSVAL_ names for the
98 * sysval */
99
100 unsigned sysval_count;
101 unsigned sysvals[MAX_SYSVAL_COUNT];
102
Alyssa Rosenzweig3a4524e2020-03-10 15:41:56 -0400103 int first_tag;
104
105 struct util_dynarray compiled;
106
Alyssa Rosenzweig3a4524e2020-03-10 15:41:56 -0400107 /* The number of bytes to allocate per-thread for Thread Local Storage
108 * (register spilling), or zero if no spilling is used */
109 unsigned tls_size;
110
Alyssa Rosenzweig3a4524e2020-03-10 15:41:56 -0400111} panfrost_program;
Alyssa Rosenzweig9b8cb9f2020-03-09 20:19:29 -0400112
Boris Brezillon0a74a042020-10-08 10:09:56 +0200113struct panfrost_compile_inputs {
114 unsigned gpu_id;
115 bool is_blend;
116 struct {
117 unsigned rt;
Boris Brezillona5005c32020-10-08 10:58:53 +0200118 float constants[4];
Boris Brezillon6c61f0b2020-10-12 14:56:45 +0200119 uint64_t bifrost_blend_desc;
Boris Brezillon0a74a042020-10-08 10:09:56 +0200120 } blend;
121 bool shaderdb;
122
123 enum pipe_format rt_formats[8];
124};
125
Alyssa Rosenzweig933e44d2020-03-11 13:58:10 -0400126typedef struct pan_block {
127 /* Link to next block. Must be first for mir_get_block */
128 struct list_head link;
129
130 /* List of instructions emitted for the current block */
131 struct list_head instructions;
132
133 /* Index of the block in source order */
134 unsigned name;
135
136 /* Control flow graph */
137 struct pan_block *successors[2];
Alyssa Rosenzweig933e44d2020-03-11 13:58:10 -0400138 struct set *predecessors;
Alyssa Rosenzweigc8b9a052020-10-02 13:06:54 -0400139 bool unconditional_jumps;
Alyssa Rosenzweig933e44d2020-03-11 13:58:10 -0400140
141 /* In liveness analysis, these are live masks (per-component) for
142 * indices for the block. Scalar compilers have the luxury of using
143 * simple bit fields, but for us, liveness is a vector idea. */
144 uint16_t *live_in;
145 uint16_t *live_out;
146} pan_block;
147
148struct pan_instruction {
149 struct list_head link;
150};
151
152#define pan_foreach_instr_in_block_rev(block, v) \
153 list_for_each_entry_rev(struct pan_instruction, v, &block->instructions, link)
154
155#define pan_foreach_successor(blk, v) \
156 pan_block *v; \
157 pan_block **_v; \
158 for (_v = (pan_block **) &blk->successors[0], \
159 v = *_v; \
160 v != NULL && _v < (pan_block **) &blk->successors[2]; \
161 _v++, v = *_v) \
162
163#define pan_foreach_predecessor(blk, v) \
164 struct set_entry *_entry_##v; \
165 struct pan_block *v; \
166 for (_entry_##v = _mesa_set_next_entry(blk->predecessors, NULL), \
167 v = (struct pan_block *) (_entry_##v ? _entry_##v->key : NULL); \
168 _entry_##v != NULL; \
169 _entry_##v = _mesa_set_next_entry(blk->predecessors, _entry_##v), \
170 v = (struct pan_block *) (_entry_##v ? _entry_##v->key : NULL))
171
Alyssa Rosenzweigd4291872020-05-12 13:19:23 -0400172static inline pan_block *
173pan_exit_block(struct list_head *blocks)
174{
175 pan_block *last = list_last_entry(blocks, pan_block, link);
176 assert(!last->successors[0] && !last->successors[1]);
177 return last;
178}
Alyssa Rosenzweig933e44d2020-03-11 13:58:10 -0400179
180typedef void (*pan_liveness_update)(uint16_t *, void *, unsigned max);
181
182void pan_liveness_gen(uint16_t *live, unsigned node, unsigned max, uint16_t mask);
183void pan_liveness_kill(uint16_t *live, unsigned node, unsigned max, uint16_t mask);
184bool pan_liveness_get(uint16_t *live, unsigned node, uint16_t max);
185
186void pan_compute_liveness(struct list_head *blocks,
187 unsigned temp_count,
188 pan_liveness_update callback);
189
190void pan_free_liveness(struct list_head *blocks);
191
Alyssa Rosenzweig9b8cb9f2020-03-09 20:19:29 -0400192uint16_t
193pan_to_bytemask(unsigned bytes, unsigned mask);
194
Alyssa Rosenzweig9b75f412020-03-11 14:35:38 -0400195void pan_block_add_successor(pan_block *block, pan_block *successor);
196
Alyssa Rosenzweig5860b182020-04-27 16:00:38 -0400197/* IR indexing */
198#define PAN_IS_REG (1)
199
200static inline unsigned
201pan_ssa_index(nir_ssa_def *ssa)
202{
203 /* Off-by-one ensures BIR_NO_ARG is skipped */
204 return ((ssa->index + 1) << 1) | 0;
205}
206
207static inline unsigned
208pan_src_index(nir_src *src)
209{
210 if (src->is_ssa)
211 return pan_ssa_index(src->ssa);
212 else {
213 assert(!src->reg.indirect);
Alyssa Rosenzweigfbbe3d42020-04-27 16:04:05 -0400214 return (src->reg.reg->index << 1) | PAN_IS_REG;
Alyssa Rosenzweig5860b182020-04-27 16:00:38 -0400215 }
216}
217
218static inline unsigned
219pan_dest_index(nir_dest *dst)
220{
221 if (dst->is_ssa)
222 return pan_ssa_index(&dst->ssa);
223 else {
224 assert(!dst->reg.indirect);
Alyssa Rosenzweigfbbe3d42020-04-27 16:04:05 -0400225 return (dst->reg.reg->index << 1) | PAN_IS_REG;
Alyssa Rosenzweig5860b182020-04-27 16:00:38 -0400226 }
227}
228
Alyssa Rosenzweigb9f7f062020-04-27 18:43:01 -0400229/* IR printing helpers */
230void pan_print_alu_type(nir_alu_type t, FILE *fp);
231
Alyssa Rosenzweigaeb55182020-04-29 17:51:03 -0400232/* Until it can be upstreamed.. */
233bool pan_has_source_mod(nir_alu_src *src, nir_op op);
234bool pan_has_dest_mod(nir_dest **dest, nir_op op);
235
Alyssa Rosenzweig42319c52020-11-04 08:37:55 -0500236/* NIR passes to do some backend-specific lowering */
237
238#define PAN_WRITEOUT_C 1
239#define PAN_WRITEOUT_Z 2
240#define PAN_WRITEOUT_S 4
241
242bool pan_nir_reorder_writeout(nir_shader *nir);
243bool pan_nir_lower_zs_store(nir_shader *nir);
244
Alyssa Rosenzweig9b8cb9f2020-03-09 20:19:29 -0400245#endif