1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2015-2018 Rob Clark <robclark@freedesktop.org>
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Rob Clark <robclark@freedesktop.org>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#ifndef IR3_CONTEXT_H_
28bf215546Sopenharmony_ci#define IR3_CONTEXT_H_
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "ir3.h"
31bf215546Sopenharmony_ci#include "ir3_compiler.h"
32bf215546Sopenharmony_ci#include "ir3_nir.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci/* for conditionally setting boolean flag(s): */
35bf215546Sopenharmony_ci#define COND(bool, val) ((bool) ? (val) : 0)
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci#define DBG(fmt, ...)                                                          \
38bf215546Sopenharmony_ci   do {                                                                        \
39bf215546Sopenharmony_ci      mesa_logd("%s:%d: " fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__);         \
40bf215546Sopenharmony_ci   } while (0)
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci/**
43bf215546Sopenharmony_ci * The context for compilation of a single shader.
44bf215546Sopenharmony_ci */
45bf215546Sopenharmony_cistruct ir3_context {
46bf215546Sopenharmony_ci   struct ir3_compiler *compiler;
47bf215546Sopenharmony_ci   const struct ir3_context_funcs *funcs;
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci   struct nir_shader *s;
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci   struct nir_instr *cur_instr; /* current instruction, just for debug */
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci   struct ir3 *ir;
54bf215546Sopenharmony_ci   struct ir3_shader_variant *so;
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   /* Tables of scalar inputs/outputs.  Because of the way varying packing
57bf215546Sopenharmony_ci    * works, we could have inputs w/ fractional location, which is a bit
58bf215546Sopenharmony_ci    * awkward to deal with unless we keep track of the split scalar in/
59bf215546Sopenharmony_ci    * out components.
60bf215546Sopenharmony_ci    *
61bf215546Sopenharmony_ci    * These *only* have inputs/outputs that are touched by load_*input and
62bf215546Sopenharmony_ci    * store_output.
63bf215546Sopenharmony_ci    */
64bf215546Sopenharmony_ci   unsigned ninputs, noutputs;
65bf215546Sopenharmony_ci   struct ir3_instruction **inputs;
66bf215546Sopenharmony_ci   struct ir3_instruction **outputs;
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci   struct ir3_block *block;    /* the current block */
69bf215546Sopenharmony_ci   struct ir3_block *in_block; /* block created for shader inputs */
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci   nir_function_impl *impl;
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci   /* For fragment shaders, varyings are not actual shader inputs,
74bf215546Sopenharmony_ci    * instead the hw passes a ij coord which is used with
75bf215546Sopenharmony_ci    * bary.f.
76bf215546Sopenharmony_ci    *
77bf215546Sopenharmony_ci    * But NIR doesn't know that, it still declares varyings as
78bf215546Sopenharmony_ci    * inputs.  So we do all the input tracking normally and fix
79bf215546Sopenharmony_ci    * things up after compile_instructions()
80bf215546Sopenharmony_ci    */
81bf215546Sopenharmony_ci   struct ir3_instruction *ij[IJ_COUNT];
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci   /* for fragment shaders, for gl_FrontFacing and gl_FragCoord: */
84bf215546Sopenharmony_ci   struct ir3_instruction *frag_face, *frag_coord;
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci   /* For vertex shaders, keep track of the system values sources */
87bf215546Sopenharmony_ci   struct ir3_instruction *vertex_id, *basevertex, *instance_id, *base_instance,
88bf215546Sopenharmony_ci      *draw_id, *view_index;
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci   /* For fragment shaders: */
91bf215546Sopenharmony_ci   struct ir3_instruction *samp_id, *samp_mask_in;
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci   /* For geometry shaders: */
94bf215546Sopenharmony_ci   struct ir3_instruction *primitive_id;
95bf215546Sopenharmony_ci   struct ir3_instruction *gs_header;
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci   /* For tessellation shaders: */
98bf215546Sopenharmony_ci   struct ir3_instruction *tcs_header;
99bf215546Sopenharmony_ci   struct ir3_instruction *tess_coord;
100bf215546Sopenharmony_ci   struct ir3_instruction *rel_patch_id;
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci   /* Compute shader inputs: */
103bf215546Sopenharmony_ci   struct ir3_instruction *local_invocation_id, *work_group_id;
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci   /* mapping from nir_register to defining instruction: */
106bf215546Sopenharmony_ci   struct hash_table *def_ht;
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci   unsigned num_arrays;
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci   /* Tracking for max level of flowcontrol (branchstack) needed
111bf215546Sopenharmony_ci    * by a5xx+:
112bf215546Sopenharmony_ci    */
113bf215546Sopenharmony_ci   unsigned stack, max_stack;
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci   unsigned loop_id;
116bf215546Sopenharmony_ci   unsigned loop_depth;
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_ci   /* a common pattern for indirect addressing is to request the
119bf215546Sopenharmony_ci    * same address register multiple times.  To avoid generating
120bf215546Sopenharmony_ci    * duplicate instruction sequences (which our backend does not
121bf215546Sopenharmony_ci    * try to clean up, since that should be done as the NIR stage)
122bf215546Sopenharmony_ci    * we cache the address value generated for a given src value:
123bf215546Sopenharmony_ci    *
124bf215546Sopenharmony_ci    * Note that we have to cache these per alignment, since same
125bf215546Sopenharmony_ci    * src used for an array of vec1 cannot be also used for an
126bf215546Sopenharmony_ci    * array of vec4.
127bf215546Sopenharmony_ci    */
128bf215546Sopenharmony_ci   struct hash_table *addr0_ht[4];
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ci   /* The same for a1.x. We only support immediate values for a1.x, as this
131bf215546Sopenharmony_ci    * is the only use so far.
132bf215546Sopenharmony_ci    */
133bf215546Sopenharmony_ci   struct hash_table_u64 *addr1_ht;
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci   struct hash_table *sel_cond_conversions;
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_ci   /* last dst array, for indirect we need to insert a var-store.
138bf215546Sopenharmony_ci    */
139bf215546Sopenharmony_ci   struct ir3_instruction **last_dst;
140bf215546Sopenharmony_ci   unsigned last_dst_n;
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_ci   /* maps nir_block to ir3_block, mostly for the purposes of
143bf215546Sopenharmony_ci    * figuring out the blocks successors
144bf215546Sopenharmony_ci    */
145bf215546Sopenharmony_ci   struct hash_table *block_ht;
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci   /* maps nir_block at the top of a loop to ir3_block collecting continue
148bf215546Sopenharmony_ci    * edges.
149bf215546Sopenharmony_ci    */
150bf215546Sopenharmony_ci   struct hash_table *continue_block_ht;
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ci   /* on a4xx, bitmask of samplers which need astc+srgb workaround: */
153bf215546Sopenharmony_ci   unsigned astc_srgb;
154bf215546Sopenharmony_ci
155bf215546Sopenharmony_ci   /* on a4xx, per-sampler per-component swizzles, for tg4: */
156bf215546Sopenharmony_ci   uint16_t sampler_swizzles[16];
157bf215546Sopenharmony_ci
158bf215546Sopenharmony_ci   unsigned samples; /* bitmask of x,y sample shifts */
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_ci   unsigned max_texture_index;
161bf215546Sopenharmony_ci
162bf215546Sopenharmony_ci   unsigned prefetch_limit;
163bf215546Sopenharmony_ci
164bf215546Sopenharmony_ci   /* set if we encounter something we can't handle yet, so we
165bf215546Sopenharmony_ci    * can bail cleanly and fallback to TGSI compiler f/e
166bf215546Sopenharmony_ci    */
167bf215546Sopenharmony_ci   bool error;
168bf215546Sopenharmony_ci};
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_cistruct ir3_context_funcs {
171bf215546Sopenharmony_ci   void (*emit_intrinsic_load_ssbo)(struct ir3_context *ctx,
172bf215546Sopenharmony_ci                                    nir_intrinsic_instr *intr,
173bf215546Sopenharmony_ci                                    struct ir3_instruction **dst);
174bf215546Sopenharmony_ci   void (*emit_intrinsic_store_ssbo)(struct ir3_context *ctx,
175bf215546Sopenharmony_ci                                     nir_intrinsic_instr *intr);
176bf215546Sopenharmony_ci   struct ir3_instruction *(*emit_intrinsic_atomic_ssbo)(
177bf215546Sopenharmony_ci      struct ir3_context *ctx, nir_intrinsic_instr *intr);
178bf215546Sopenharmony_ci   void (*emit_intrinsic_load_image)(struct ir3_context *ctx,
179bf215546Sopenharmony_ci                                     nir_intrinsic_instr *intr,
180bf215546Sopenharmony_ci                                     struct ir3_instruction **dst);
181bf215546Sopenharmony_ci   void (*emit_intrinsic_store_image)(struct ir3_context *ctx,
182bf215546Sopenharmony_ci                                      nir_intrinsic_instr *intr);
183bf215546Sopenharmony_ci   struct ir3_instruction *(*emit_intrinsic_atomic_image)(
184bf215546Sopenharmony_ci      struct ir3_context *ctx, nir_intrinsic_instr *intr);
185bf215546Sopenharmony_ci   void (*emit_intrinsic_image_size)(struct ir3_context *ctx,
186bf215546Sopenharmony_ci                                     nir_intrinsic_instr *intr,
187bf215546Sopenharmony_ci                                     struct ir3_instruction **dst);
188bf215546Sopenharmony_ci   void (*emit_intrinsic_load_global_ir3)(struct ir3_context *ctx,
189bf215546Sopenharmony_ci                                          nir_intrinsic_instr *intr,
190bf215546Sopenharmony_ci                                          struct ir3_instruction **dst);
191bf215546Sopenharmony_ci   void (*emit_intrinsic_store_global_ir3)(struct ir3_context *ctx,
192bf215546Sopenharmony_ci                                           nir_intrinsic_instr *intr);
193bf215546Sopenharmony_ci   struct ir3_instruction *(*emit_intrinsic_atomic_global)(
194bf215546Sopenharmony_ci      struct ir3_context *ctx, nir_intrinsic_instr *intr);
195bf215546Sopenharmony_ci};
196bf215546Sopenharmony_ci
197bf215546Sopenharmony_ciextern const struct ir3_context_funcs ir3_a4xx_funcs;
198bf215546Sopenharmony_ciextern const struct ir3_context_funcs ir3_a6xx_funcs;
199bf215546Sopenharmony_ci
200bf215546Sopenharmony_cistruct ir3_context *ir3_context_init(struct ir3_compiler *compiler,
201bf215546Sopenharmony_ci                                     struct ir3_shader *shader,
202bf215546Sopenharmony_ci                                     struct ir3_shader_variant *so);
203bf215546Sopenharmony_civoid ir3_context_free(struct ir3_context *ctx);
204bf215546Sopenharmony_ci
205bf215546Sopenharmony_cistruct ir3_instruction **ir3_get_dst_ssa(struct ir3_context *ctx,
206bf215546Sopenharmony_ci                                         nir_ssa_def *dst, unsigned n);
207bf215546Sopenharmony_cistruct ir3_instruction **ir3_get_dst(struct ir3_context *ctx, nir_dest *dst,
208bf215546Sopenharmony_ci                                     unsigned n);
209bf215546Sopenharmony_cistruct ir3_instruction *const *ir3_get_src(struct ir3_context *ctx,
210bf215546Sopenharmony_ci                                           nir_src *src);
211bf215546Sopenharmony_civoid ir3_put_dst(struct ir3_context *ctx, nir_dest *dst);
212bf215546Sopenharmony_cistruct ir3_instruction *ir3_create_collect(struct ir3_block *block,
213bf215546Sopenharmony_ci                                           struct ir3_instruction *const *arr,
214bf215546Sopenharmony_ci                                           unsigned arrsz);
215bf215546Sopenharmony_civoid ir3_split_dest(struct ir3_block *block, struct ir3_instruction **dst,
216bf215546Sopenharmony_ci                    struct ir3_instruction *src, unsigned base, unsigned n);
217bf215546Sopenharmony_civoid ir3_handle_bindless_cat6(struct ir3_instruction *instr, nir_src rsrc);
218bf215546Sopenharmony_civoid ir3_handle_nonuniform(struct ir3_instruction *instr,
219bf215546Sopenharmony_ci                           nir_intrinsic_instr *intrin);
220bf215546Sopenharmony_civoid emit_intrinsic_image_size_tex(struct ir3_context *ctx,
221bf215546Sopenharmony_ci                                   nir_intrinsic_instr *intr,
222bf215546Sopenharmony_ci                                   struct ir3_instruction **dst);
223bf215546Sopenharmony_ci
224bf215546Sopenharmony_ci#define ir3_collect(block, ...)                                                \
225bf215546Sopenharmony_ci   ({                                                                          \
226bf215546Sopenharmony_ci      struct ir3_instruction *__arr[] = {__VA_ARGS__};                         \
227bf215546Sopenharmony_ci      ir3_create_collect(block, __arr, ARRAY_SIZE(__arr));                     \
228bf215546Sopenharmony_ci   })
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_ciNORETURN void ir3_context_error(struct ir3_context *ctx, const char *format,
231bf215546Sopenharmony_ci                                ...);
232bf215546Sopenharmony_ci
233bf215546Sopenharmony_ci#define compile_assert(ctx, cond)                                              \
234bf215546Sopenharmony_ci   do {                                                                        \
235bf215546Sopenharmony_ci      if (!(cond))                                                             \
236bf215546Sopenharmony_ci         ir3_context_error((ctx), "failed assert: " #cond "\n");               \
237bf215546Sopenharmony_ci   } while (0)
238bf215546Sopenharmony_ci
239bf215546Sopenharmony_cistruct ir3_instruction *ir3_get_addr0(struct ir3_context *ctx,
240bf215546Sopenharmony_ci                                      struct ir3_instruction *src, int align);
241bf215546Sopenharmony_cistruct ir3_instruction *ir3_get_addr1(struct ir3_context *ctx,
242bf215546Sopenharmony_ci                                      unsigned const_val);
243bf215546Sopenharmony_cistruct ir3_instruction *ir3_get_predicate(struct ir3_context *ctx,
244bf215546Sopenharmony_ci                                          struct ir3_instruction *src);
245bf215546Sopenharmony_ci
246bf215546Sopenharmony_civoid ir3_declare_array(struct ir3_context *ctx, nir_register *reg);
247bf215546Sopenharmony_cistruct ir3_array *ir3_get_array(struct ir3_context *ctx, nir_register *reg);
248bf215546Sopenharmony_cistruct ir3_instruction *ir3_create_array_load(struct ir3_context *ctx,
249bf215546Sopenharmony_ci                                              struct ir3_array *arr, int n,
250bf215546Sopenharmony_ci                                              struct ir3_instruction *address);
251bf215546Sopenharmony_civoid ir3_create_array_store(struct ir3_context *ctx, struct ir3_array *arr,
252bf215546Sopenharmony_ci                            int n, struct ir3_instruction *src,
253bf215546Sopenharmony_ci                            struct ir3_instruction *address);
254bf215546Sopenharmony_ci
255bf215546Sopenharmony_cistatic inline type_t
256bf215546Sopenharmony_ciutype_for_size(unsigned bit_size)
257bf215546Sopenharmony_ci{
258bf215546Sopenharmony_ci   switch (bit_size) {
259bf215546Sopenharmony_ci   case 32:
260bf215546Sopenharmony_ci      return TYPE_U32;
261bf215546Sopenharmony_ci   case 16:
262bf215546Sopenharmony_ci      return TYPE_U16;
263bf215546Sopenharmony_ci   case 8:
264bf215546Sopenharmony_ci      return TYPE_U8;
265bf215546Sopenharmony_ci   default:
266bf215546Sopenharmony_ci      unreachable("bad bitsize");
267bf215546Sopenharmony_ci      return ~0;
268bf215546Sopenharmony_ci   }
269bf215546Sopenharmony_ci}
270bf215546Sopenharmony_ci
271bf215546Sopenharmony_cistatic inline type_t
272bf215546Sopenharmony_ciutype_src(nir_src src)
273bf215546Sopenharmony_ci{
274bf215546Sopenharmony_ci   return utype_for_size(nir_src_bit_size(src));
275bf215546Sopenharmony_ci}
276bf215546Sopenharmony_ci
277bf215546Sopenharmony_cistatic inline type_t
278bf215546Sopenharmony_ciutype_dst(nir_dest dst)
279bf215546Sopenharmony_ci{
280bf215546Sopenharmony_ci   return utype_for_size(nir_dest_bit_size(dst));
281bf215546Sopenharmony_ci}
282bf215546Sopenharmony_ci
283bf215546Sopenharmony_ci/**
284bf215546Sopenharmony_ci * Convert nir bitsize to ir3 bitsize, handling the special case of 1b bools
285bf215546Sopenharmony_ci * which can be 16b or 32b depending on gen.
286bf215546Sopenharmony_ci */
287bf215546Sopenharmony_cistatic inline unsigned
288bf215546Sopenharmony_ciir3_bitsize(struct ir3_context *ctx, unsigned nir_bitsize)
289bf215546Sopenharmony_ci{
290bf215546Sopenharmony_ci   if (nir_bitsize == 1)
291bf215546Sopenharmony_ci      return type_size(ctx->compiler->bool_type);
292bf215546Sopenharmony_ci   return nir_bitsize;
293bf215546Sopenharmony_ci}
294bf215546Sopenharmony_ci
295bf215546Sopenharmony_ci#endif /* IR3_CONTEXT_H_ */
296