lima/ppir: change offset type to int
[mesa.git] / src / gallium / drivers / lima / ir / pp / ppir.h
1 /*
2 * Copyright (c) 2017 Lima Project
3 * Copyright (c) 2013 Connor Abbott
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the 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 THE
18 * 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
21 * THE SOFTWARE.
22 *
23 */
24
25 #ifndef LIMA_IR_PP_PPIR_H
26 #define LIMA_IR_PP_PPIR_H
27
28 #include "util/u_math.h"
29 #include "util/list.h"
30
31 #include "ir/lima_ir.h"
32
33 typedef enum {
34 ppir_op_mov,
35 ppir_op_add,
36
37 ppir_op_ddx,
38 ppir_op_ddy,
39
40 ppir_op_mul,
41 ppir_op_rcp,
42
43 ppir_op_sin_lut,
44 ppir_op_cos_lut,
45
46 ppir_op_sum3,
47 ppir_op_sum4,
48
49 ppir_op_normalize2,
50 ppir_op_normalize3,
51 ppir_op_normalize4,
52
53 ppir_op_select,
54
55 ppir_op_sin,
56 ppir_op_cos,
57 ppir_op_tan,
58 ppir_op_asin,
59 ppir_op_acos,
60
61 ppir_op_atan,
62 ppir_op_atan2,
63 ppir_op_atan_pt1,
64 ppir_op_atan2_pt1,
65 ppir_op_atan_pt2,
66
67 ppir_op_exp,
68 ppir_op_log,
69 ppir_op_exp2,
70 ppir_op_log2,
71 ppir_op_sqrt,
72 ppir_op_rsqrt,
73
74 ppir_op_sign,
75 ppir_op_floor,
76 ppir_op_ceil,
77 ppir_op_fract,
78 ppir_op_mod,
79 ppir_op_min,
80 ppir_op_max,
81 ppir_op_trunc,
82
83 ppir_op_dot2,
84 ppir_op_dot3,
85 ppir_op_dot4,
86
87 ppir_op_and,
88 ppir_op_or,
89 ppir_op_xor,
90
91 ppir_op_lt,
92 ppir_op_gt,
93 ppir_op_le,
94 ppir_op_ge,
95 ppir_op_eq,
96 ppir_op_ne,
97 ppir_op_not,
98
99 ppir_op_load_uniform,
100 ppir_op_load_varying,
101 ppir_op_load_coords,
102 ppir_op_load_fragcoord,
103 ppir_op_load_texture,
104 ppir_op_load_temp,
105
106 ppir_op_store_temp,
107 ppir_op_store_color,
108
109 ppir_op_const,
110
111 ppir_op_discard,
112 ppir_op_branch,
113
114 ppir_op_num,
115 } ppir_op;
116
117 typedef enum {
118 ppir_node_type_alu,
119 ppir_node_type_const,
120 ppir_node_type_load,
121 ppir_node_type_store,
122 ppir_node_type_load_texture,
123 ppir_node_type_discard,
124 ppir_node_type_branch,
125 } ppir_node_type;
126
127 typedef struct {
128 char *name;
129 ppir_node_type type;
130 int *slots;
131 } ppir_op_info;
132
133 extern const ppir_op_info ppir_op_infos[];
134
135 typedef struct {
136 void *pred, *succ;
137 struct list_head pred_link;
138 struct list_head succ_link;
139 } ppir_dep;
140
141 typedef struct ppir_node {
142 struct list_head list;
143 ppir_op op;
144 ppir_node_type type;
145 int index;
146 char name[16];
147 bool printed;
148 struct ppir_instr *instr;
149 int instr_pos;
150 struct ppir_block *block;
151
152 /* for scheduler */
153 struct list_head succ_list;
154 struct list_head pred_list;
155 } ppir_node;
156
157 typedef enum {
158 ppir_pipeline_reg_const0,
159 ppir_pipeline_reg_const1,
160 ppir_pipeline_reg_sampler,
161 ppir_pipeline_reg_uniform,
162 ppir_pipeline_reg_vmul,
163 ppir_pipeline_reg_fmul,
164 ppir_pipeline_reg_discard, /* varying load */
165 } ppir_pipeline;
166
167 typedef struct ppir_reg {
168 struct list_head list;
169 int index;
170 int num_components;
171 /* whether this reg has to start from the x component
172 * of a full physical reg, this is true for reg used
173 * in load/store instr which has no swizzle field
174 */
175 bool is_head;
176 /* instr live range */
177 int live_in, live_out;
178 bool spilled;
179 } ppir_reg;
180
181 typedef enum {
182 ppir_target_ssa,
183 ppir_target_pipeline,
184 ppir_target_register,
185 } ppir_target;
186
187 typedef struct ppir_src {
188 ppir_target type;
189
190 union {
191 ppir_reg *ssa;
192 ppir_reg *reg;
193 ppir_pipeline pipeline;
194 };
195
196 uint8_t swizzle[4];
197 bool absolute, negate;
198 } ppir_src;
199
200 typedef enum {
201 ppir_outmod_none,
202 ppir_outmod_clamp_fraction,
203 ppir_outmod_clamp_positive,
204 ppir_outmod_round,
205 } ppir_outmod;
206
207 typedef struct ppir_dest {
208 ppir_target type;
209
210 union {
211 ppir_reg ssa;
212 ppir_reg *reg;
213 ppir_pipeline pipeline;
214 };
215
216 ppir_outmod modifier;
217 unsigned write_mask : 4;
218 } ppir_dest;
219
220 typedef struct {
221 ppir_node node;
222 ppir_dest dest;
223 ppir_src src[3];
224 int num_src;
225 int shift : 3; /* Only used for ppir_op_mul */
226 } ppir_alu_node;
227
228 typedef struct ppir_const {
229 union fi value[4];
230 int num;
231 } ppir_const;
232
233 typedef struct {
234 ppir_node node;
235 ppir_const constant;
236 ppir_dest dest;
237 } ppir_const_node;
238
239 typedef struct {
240 ppir_node node;
241 int index;
242 int num_components;
243 ppir_dest dest;
244 ppir_src src;
245 } ppir_load_node;
246
247 typedef struct {
248 ppir_node node;
249 int index;
250 int num_components;
251 ppir_src src;
252 } ppir_store_node;
253
254 typedef struct {
255 ppir_node node;
256 ppir_dest dest;
257 ppir_src src_coords;
258 int sampler;
259 int sampler_dim;
260 } ppir_load_texture_node;
261
262 typedef struct {
263 ppir_node node;
264 } ppir_discard_node;
265
266 enum ppir_instr_slot {
267 PPIR_INSTR_SLOT_VARYING,
268 PPIR_INSTR_SLOT_TEXLD,
269 PPIR_INSTR_SLOT_UNIFORM,
270 PPIR_INSTR_SLOT_ALU_VEC_MUL,
271 PPIR_INSTR_SLOT_ALU_SCL_MUL,
272 PPIR_INSTR_SLOT_ALU_VEC_ADD,
273 PPIR_INSTR_SLOT_ALU_SCL_ADD,
274 PPIR_INSTR_SLOT_ALU_COMBINE,
275 PPIR_INSTR_SLOT_STORE_TEMP,
276 PPIR_INSTR_SLOT_BRANCH,
277 PPIR_INSTR_SLOT_NUM,
278 PPIR_INSTR_SLOT_END,
279 PPIR_INSTR_SLOT_ALU_START = PPIR_INSTR_SLOT_ALU_VEC_MUL,
280 PPIR_INSTR_SLOT_ALU_END = PPIR_INSTR_SLOT_ALU_COMBINE,
281 };
282
283 typedef struct ppir_instr {
284 struct list_head list;
285 int index;
286 bool printed;
287 int seq; /* command sequence after schedule */
288
289 ppir_node *slots[PPIR_INSTR_SLOT_NUM];
290 ppir_const constant[2];
291 bool is_end;
292
293 /* for scheduler */
294 struct list_head succ_list;
295 struct list_head pred_list;
296 float reg_pressure;
297 int est; /* earliest start time */
298 int parent_index;
299 bool scheduled;
300 int offset;
301 } ppir_instr;
302
303 typedef struct ppir_block {
304 struct list_head list;
305 struct list_head node_list;
306 struct list_head instr_list;
307 struct ppir_compiler *comp;
308
309 /* for scheduler */
310 int sched_instr_index;
311 int sched_instr_base;
312 } ppir_block;
313
314 typedef struct {
315 ppir_node node;
316 ppir_src src[2];
317 bool cond_gt;
318 bool cond_eq;
319 bool cond_lt;
320 ppir_block *target;
321 } ppir_branch_node;
322
323 struct ra_regs;
324 struct lima_fs_shader_state;
325
326 typedef struct ppir_compiler {
327 struct list_head block_list;
328 int cur_index;
329 int cur_instr_index;
330
331 struct list_head reg_list;
332
333 /* array for searching ssa/reg node */
334 ppir_node **var_nodes;
335 unsigned reg_base;
336
337 struct ra_regs *ra;
338 struct lima_fs_shader_state *prog;
339
340 /* for scheduler */
341 int sched_instr_base;
342
343 /* for regalloc spilling debug */
344 int force_spilling;
345
346 ppir_block *discard_block;
347 } ppir_compiler;
348
349 void *ppir_node_create(ppir_block *block, ppir_op op, int index, unsigned mask);
350 void ppir_node_add_dep(ppir_node *succ, ppir_node *pred);
351 void ppir_node_remove_dep(ppir_dep *dep);
352 void ppir_node_delete(ppir_node *node);
353 void ppir_node_print_prog(ppir_compiler *comp);
354 void ppir_node_replace_child(ppir_node *parent, ppir_node *old_child, ppir_node *new_child);
355 void ppir_node_replace_all_succ(ppir_node *dst, ppir_node *src);
356 void ppir_node_replace_pred(ppir_dep *dep, ppir_node *new_pred);
357
358 static inline bool ppir_node_is_root(ppir_node *node)
359 {
360 return list_empty(&node->succ_list);
361 }
362
363 static inline bool ppir_node_is_leaf(ppir_node *node)
364 {
365 return list_empty(&node->pred_list);
366 }
367
368 static inline bool ppir_node_has_single_succ(ppir_node *node)
369 {
370 return list_is_singular(&node->succ_list);
371 }
372
373 static inline ppir_node *ppir_node_first_succ(ppir_node *node)
374 {
375 return list_first_entry(&node->succ_list, ppir_dep, succ_link)->succ;
376 }
377
378 static inline bool ppir_node_has_single_pred(ppir_node *node)
379 {
380 return list_is_singular(&node->pred_list);
381 }
382
383 static inline ppir_node *ppir_node_first_pred(ppir_node *node)
384 {
385 return list_first_entry(&node->pred_list, ppir_dep, pred_link)->pred;
386 }
387
388 #define ppir_node_foreach_succ(node, dep) \
389 list_for_each_entry(ppir_dep, dep, &node->succ_list, succ_link)
390 #define ppir_node_foreach_succ_safe(node, dep) \
391 list_for_each_entry_safe(ppir_dep, dep, &node->succ_list, succ_link)
392 #define ppir_node_foreach_pred(node, dep) \
393 list_for_each_entry(ppir_dep, dep, &node->pred_list, pred_link)
394 #define ppir_node_foreach_pred_safe(node, dep) \
395 list_for_each_entry_safe(ppir_dep, dep, &node->pred_list, pred_link)
396
397 #define ppir_node_to_alu(node) ((ppir_alu_node *)(node))
398 #define ppir_node_to_const(node) ((ppir_const_node *)(node))
399 #define ppir_node_to_load(node) ((ppir_load_node *)(node))
400 #define ppir_node_to_store(node) ((ppir_store_node *)(node))
401 #define ppir_node_to_load_texture(node) ((ppir_load_texture_node *)(node))
402 #define ppir_node_to_discard(node) ((ppir_discard_node *)(node))
403 #define ppir_node_to_branch(node) ((ppir_branch_node *)(node))
404
405 static inline ppir_dest *ppir_node_get_dest(ppir_node *node)
406 {
407 switch (node->type) {
408 case ppir_node_type_alu:
409 return &ppir_node_to_alu(node)->dest;
410 case ppir_node_type_load:
411 return &ppir_node_to_load(node)->dest;
412 case ppir_node_type_const:
413 return &ppir_node_to_const(node)->dest;
414 case ppir_node_type_load_texture:
415 return &ppir_node_to_load_texture(node)->dest;
416 default:
417 return NULL;
418 }
419 }
420
421 static inline void ppir_node_target_assign(ppir_src *src, ppir_dest *dest)
422 {
423 src->type = dest->type;
424 switch (src->type) {
425 case ppir_target_ssa:
426 src->ssa = &dest->ssa;
427 break;
428 case ppir_target_register:
429 src->reg = dest->reg;
430 break;
431 case ppir_target_pipeline:
432 src->pipeline = dest->pipeline;
433 break;
434 }
435 }
436
437 static inline bool ppir_node_target_equal(ppir_src *src, ppir_dest *dest)
438 {
439 if (src->type != dest->type ||
440 (src->type == ppir_target_ssa && src->ssa != &dest->ssa) ||
441 (src->type == ppir_target_register && src->reg != dest->reg) ||
442 (src->type == ppir_target_pipeline && src->pipeline != dest->pipeline))
443 return false;
444
445 return true;
446 }
447
448 static inline int ppir_target_get_src_reg_index(ppir_src *src)
449 {
450 switch (src->type) {
451 case ppir_target_ssa:
452 return src->ssa->index;
453 case ppir_target_register:
454 return src->reg->index;
455 case ppir_target_pipeline:
456 if (src->pipeline == ppir_pipeline_reg_discard)
457 return 15 * 4;
458 return (src->pipeline + 12) * 4;
459 }
460
461 return -1;
462 }
463
464 static inline int ppir_target_get_dest_reg_index(ppir_dest *dest)
465 {
466 switch (dest->type) {
467 case ppir_target_ssa:
468 return dest->ssa.index;
469 case ppir_target_register:
470 return dest->reg->index;
471 case ppir_target_pipeline:
472 if (dest->pipeline == ppir_pipeline_reg_discard)
473 return 15 * 4;
474 return (dest->pipeline + 12) * 4;
475 }
476
477 return -1;
478 }
479
480 static inline bool ppir_target_is_scaler(ppir_dest *dest)
481 {
482 switch (dest->type) {
483 case ppir_target_ssa:
484 return dest->ssa.num_components == 1;
485 case ppir_target_register:
486 /* only one bit in mask is set */
487 if ((dest->write_mask & 0x3) == 0x3 ||
488 (dest->write_mask & 0x5) == 0x5 ||
489 (dest->write_mask & 0x9) == 0x9 ||
490 (dest->write_mask & 0x6) == 0x6 ||
491 (dest->write_mask & 0xa) == 0xa ||
492 (dest->write_mask & 0xc) == 0xc)
493 return false;
494 else
495 return true;
496 case ppir_target_pipeline:
497 if (dest->pipeline == ppir_pipeline_reg_fmul)
498 return true;
499 else
500 return false;
501 default:
502 return false;
503 }
504 }
505
506 ppir_instr *ppir_instr_create(ppir_block *block);
507 bool ppir_instr_insert_node(ppir_instr *instr, ppir_node *node);
508 void ppir_instr_add_dep(ppir_instr *succ, ppir_instr *pred);
509 void ppir_instr_print_list(ppir_compiler *comp);
510 void ppir_instr_print_dep(ppir_compiler *comp);
511 void ppir_instr_insert_mul_node(ppir_node *add, ppir_node *mul);
512
513 #define ppir_instr_foreach_succ(instr, dep) \
514 list_for_each_entry(ppir_dep, dep, &instr->succ_list, succ_link)
515 #define ppir_instr_foreach_succ_safe(instr, dep) \
516 list_for_each_entry_safe(ppir_dep, dep, &instr->succ_list, succ_link)
517 #define ppir_instr_foreach_pred(instr, dep) \
518 list_for_each_entry(ppir_dep, dep, &instr->pred_list, pred_link)
519 #define ppir_instr_foreach_pred_safe(instr, dep) \
520 list_for_each_entry_safe(ppir_dep, dep, &instr->pred_list, pred_link)
521
522 static inline bool ppir_instr_is_root(ppir_instr *instr)
523 {
524 return list_empty(&instr->succ_list);
525 }
526
527 static inline bool ppir_instr_is_leaf(ppir_instr *instr)
528 {
529 return list_empty(&instr->pred_list);
530 }
531
532 bool ppir_lower_prog(ppir_compiler *comp);
533 bool ppir_node_to_instr(ppir_compiler *comp);
534 bool ppir_schedule_prog(ppir_compiler *comp);
535 bool ppir_regalloc_prog(ppir_compiler *comp);
536 bool ppir_codegen_prog(ppir_compiler *comp);
537
538 #endif