1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2014 Intel Corporation
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
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Connor Abbott (cwabbott0@gmail.com)
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci */
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include "nir.h"
29bf215546Sopenharmony_ci#include "compiler/shader_enums.h"
30bf215546Sopenharmony_ci#include "util/half_float.h"
31bf215546Sopenharmony_ci#include "util/memstream.h"
32bf215546Sopenharmony_ci#include "util/mesa-sha1.h"
33bf215546Sopenharmony_ci#include "vulkan/vulkan_core.h"
34bf215546Sopenharmony_ci#include <stdio.h>
35bf215546Sopenharmony_ci#include <stdlib.h>
36bf215546Sopenharmony_ci#include <inttypes.h> /* for PRIx64 macro */
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_cistatic void
39bf215546Sopenharmony_ciprint_tabs(unsigned num_tabs, FILE *fp)
40bf215546Sopenharmony_ci{
41bf215546Sopenharmony_ci   for (unsigned i = 0; i < num_tabs; i++)
42bf215546Sopenharmony_ci      fprintf(fp, "\t");
43bf215546Sopenharmony_ci}
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_citypedef struct {
46bf215546Sopenharmony_ci   FILE *fp;
47bf215546Sopenharmony_ci   nir_shader *shader;
48bf215546Sopenharmony_ci   /** map from nir_variable -> printable name */
49bf215546Sopenharmony_ci   struct hash_table *ht;
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci   /** set of names used so far for nir_variables */
52bf215546Sopenharmony_ci   struct set *syms;
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci   /* an index used to make new non-conflicting names */
55bf215546Sopenharmony_ci   unsigned index;
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci   /**
58bf215546Sopenharmony_ci    * Optional table of annotations mapping nir object
59bf215546Sopenharmony_ci    * (such as instr or var) to message to print.
60bf215546Sopenharmony_ci    */
61bf215546Sopenharmony_ci   struct hash_table *annotations;
62bf215546Sopenharmony_ci} print_state;
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_cistatic void
65bf215546Sopenharmony_ciprint_annotation(print_state *state, void *obj)
66bf215546Sopenharmony_ci{
67bf215546Sopenharmony_ci   FILE *fp = state->fp;
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_ci   if (!state->annotations)
70bf215546Sopenharmony_ci      return;
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci   struct hash_entry *entry = _mesa_hash_table_search(state->annotations, obj);
73bf215546Sopenharmony_ci   if (!entry)
74bf215546Sopenharmony_ci      return;
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci   const char *note = entry->data;
77bf215546Sopenharmony_ci   _mesa_hash_table_remove(state->annotations, entry);
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_ci   fprintf(fp, "%s\n\n", note);
80bf215546Sopenharmony_ci}
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_cistatic void
83bf215546Sopenharmony_ciprint_register(nir_register *reg, print_state *state)
84bf215546Sopenharmony_ci{
85bf215546Sopenharmony_ci   FILE *fp = state->fp;
86bf215546Sopenharmony_ci   fprintf(fp, "r%u", reg->index);
87bf215546Sopenharmony_ci}
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_cistatic const char *sizes[] = { "error", "vec1", "vec2", "vec3", "vec4",
90bf215546Sopenharmony_ci                               "vec5", "error", "error", "vec8",
91bf215546Sopenharmony_ci                               "error", "error", "error", "error",
92bf215546Sopenharmony_ci                               "error", "error", "error", "vec16"};
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_cistatic void
95bf215546Sopenharmony_ciprint_register_decl(nir_register *reg, print_state *state)
96bf215546Sopenharmony_ci{
97bf215546Sopenharmony_ci   FILE *fp = state->fp;
98bf215546Sopenharmony_ci   fprintf(fp, "decl_reg %s %u ", sizes[reg->num_components], reg->bit_size);
99bf215546Sopenharmony_ci   print_register(reg, state);
100bf215546Sopenharmony_ci   if (reg->num_array_elems != 0)
101bf215546Sopenharmony_ci      fprintf(fp, "[%u]", reg->num_array_elems);
102bf215546Sopenharmony_ci   fprintf(fp, "\n");
103bf215546Sopenharmony_ci}
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_cistatic void
106bf215546Sopenharmony_ciprint_ssa_def(nir_ssa_def *def, print_state *state)
107bf215546Sopenharmony_ci{
108bf215546Sopenharmony_ci   FILE *fp = state->fp;
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci   const char *divergence = "";
111bf215546Sopenharmony_ci   if (state->shader->info.divergence_analysis_run)
112bf215546Sopenharmony_ci      divergence = def->divergent ? "div " : "con ";
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci   fprintf(fp, "%s %2u %sssa_%u", sizes[def->num_components], def->bit_size,
115bf215546Sopenharmony_ci           divergence, def->index);
116bf215546Sopenharmony_ci}
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_cistatic void
119bf215546Sopenharmony_ciprint_const_from_load(nir_load_const_instr *instr, print_state *state)
120bf215546Sopenharmony_ci{
121bf215546Sopenharmony_ci   FILE *fp = state->fp;
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_ci   /*
124bf215546Sopenharmony_ci    * we don't really know the type of the constant (if it will be used as a
125bf215546Sopenharmony_ci    * float or an int), so just print the raw constant in hex for fidelity
126bf215546Sopenharmony_ci    * and then print in float again for readability.
127bf215546Sopenharmony_ci    */
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci   fprintf(fp, "(");
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ci   for (unsigned i = 0; i < instr->def.num_components; i++) {
132bf215546Sopenharmony_ci      if (i != 0)
133bf215546Sopenharmony_ci         fprintf(fp, ", ");
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci      switch (instr->def.bit_size) {
136bf215546Sopenharmony_ci      case 64:
137bf215546Sopenharmony_ci         fprintf(fp, "0x%016" PRIx64, instr->value[i].u64);
138bf215546Sopenharmony_ci         break;
139bf215546Sopenharmony_ci      case 32:
140bf215546Sopenharmony_ci         fprintf(fp, "0x%08x", instr->value[i].u32);
141bf215546Sopenharmony_ci         break;
142bf215546Sopenharmony_ci      case 16:
143bf215546Sopenharmony_ci         fprintf(fp, "0x%04x", instr->value[i].u16);
144bf215546Sopenharmony_ci         break;
145bf215546Sopenharmony_ci      case 8:
146bf215546Sopenharmony_ci         fprintf(fp, "0x%02x", instr->value[i].u8);
147bf215546Sopenharmony_ci         break;
148bf215546Sopenharmony_ci      case 1:
149bf215546Sopenharmony_ci         fprintf(fp, "%s", instr->value[i].b ? "true" : "false");
150bf215546Sopenharmony_ci         break;
151bf215546Sopenharmony_ci      }
152bf215546Sopenharmony_ci   }
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ci   if (instr->def.bit_size > 8) {
155bf215546Sopenharmony_ci      if (instr->def.num_components > 1)
156bf215546Sopenharmony_ci         fprintf(fp, ") = (");
157bf215546Sopenharmony_ci      else
158bf215546Sopenharmony_ci         fprintf(fp, " = ");
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_ci      for (unsigned i = 0; i < instr->def.num_components; i++) {
161bf215546Sopenharmony_ci         if (i != 0)
162bf215546Sopenharmony_ci            fprintf(fp, ", ");
163bf215546Sopenharmony_ci
164bf215546Sopenharmony_ci         switch (instr->def.bit_size) {
165bf215546Sopenharmony_ci         case 64:
166bf215546Sopenharmony_ci            fprintf(fp, "%f", instr->value[i].f64);
167bf215546Sopenharmony_ci            break;
168bf215546Sopenharmony_ci         case 32:
169bf215546Sopenharmony_ci            fprintf(fp, "%f", instr->value[i].f32);
170bf215546Sopenharmony_ci            break;
171bf215546Sopenharmony_ci         case 16:
172bf215546Sopenharmony_ci            fprintf(fp, "%f", _mesa_half_to_float(instr->value[i].u16));
173bf215546Sopenharmony_ci            break;
174bf215546Sopenharmony_ci         default:
175bf215546Sopenharmony_ci            unreachable("unhandled bit size");
176bf215546Sopenharmony_ci         }
177bf215546Sopenharmony_ci      }
178bf215546Sopenharmony_ci   }
179bf215546Sopenharmony_ci
180bf215546Sopenharmony_ci   fprintf(fp, ")");
181bf215546Sopenharmony_ci}
182bf215546Sopenharmony_ci
183bf215546Sopenharmony_cistatic void
184bf215546Sopenharmony_ciprint_load_const_instr(nir_load_const_instr *instr, print_state *state)
185bf215546Sopenharmony_ci{
186bf215546Sopenharmony_ci   FILE *fp = state->fp;
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_ci   print_ssa_def(&instr->def, state);
189bf215546Sopenharmony_ci
190bf215546Sopenharmony_ci   fprintf(fp, " = load_const ");
191bf215546Sopenharmony_ci
192bf215546Sopenharmony_ci   print_const_from_load(instr, state);
193bf215546Sopenharmony_ci}
194bf215546Sopenharmony_ci
195bf215546Sopenharmony_cistatic void
196bf215546Sopenharmony_ciprint_ssa_use(nir_ssa_def *def, print_state *state)
197bf215546Sopenharmony_ci{
198bf215546Sopenharmony_ci   FILE *fp = state->fp;
199bf215546Sopenharmony_ci   fprintf(fp, "ssa_%u", def->index);
200bf215546Sopenharmony_ci   nir_instr *instr = def->parent_instr;
201bf215546Sopenharmony_ci   if (instr->type == nir_instr_type_load_const && NIR_DEBUG(PRINT_CONSTS)) {
202bf215546Sopenharmony_ci      fprintf(fp, " /*");
203bf215546Sopenharmony_ci      print_const_from_load(nir_instr_as_load_const(instr), state);
204bf215546Sopenharmony_ci      fprintf(fp, "*/");
205bf215546Sopenharmony_ci   }
206bf215546Sopenharmony_ci}
207bf215546Sopenharmony_ci
208bf215546Sopenharmony_cistatic void print_src(const nir_src *src, print_state *state);
209bf215546Sopenharmony_ci
210bf215546Sopenharmony_cistatic void
211bf215546Sopenharmony_ciprint_reg_src(const nir_reg_src *src, print_state *state)
212bf215546Sopenharmony_ci{
213bf215546Sopenharmony_ci   FILE *fp = state->fp;
214bf215546Sopenharmony_ci   print_register(src->reg, state);
215bf215546Sopenharmony_ci   if (src->reg->num_array_elems != 0) {
216bf215546Sopenharmony_ci      fprintf(fp, "[%u", src->base_offset);
217bf215546Sopenharmony_ci      if (src->indirect != NULL) {
218bf215546Sopenharmony_ci         fprintf(fp, " + ");
219bf215546Sopenharmony_ci         print_src(src->indirect, state);
220bf215546Sopenharmony_ci      }
221bf215546Sopenharmony_ci      fprintf(fp, "]");
222bf215546Sopenharmony_ci   }
223bf215546Sopenharmony_ci}
224bf215546Sopenharmony_ci
225bf215546Sopenharmony_cistatic void
226bf215546Sopenharmony_ciprint_reg_dest(nir_reg_dest *dest, print_state *state)
227bf215546Sopenharmony_ci{
228bf215546Sopenharmony_ci   FILE *fp = state->fp;
229bf215546Sopenharmony_ci   print_register(dest->reg, state);
230bf215546Sopenharmony_ci   if (dest->reg->num_array_elems != 0) {
231bf215546Sopenharmony_ci      fprintf(fp, "[%u", dest->base_offset);
232bf215546Sopenharmony_ci      if (dest->indirect != NULL) {
233bf215546Sopenharmony_ci         fprintf(fp, " + ");
234bf215546Sopenharmony_ci         print_src(dest->indirect, state);
235bf215546Sopenharmony_ci      }
236bf215546Sopenharmony_ci      fprintf(fp, "]");
237bf215546Sopenharmony_ci   }
238bf215546Sopenharmony_ci}
239bf215546Sopenharmony_ci
240bf215546Sopenharmony_cistatic void
241bf215546Sopenharmony_ciprint_src(const nir_src *src, print_state *state)
242bf215546Sopenharmony_ci{
243bf215546Sopenharmony_ci   if (src->is_ssa)
244bf215546Sopenharmony_ci      print_ssa_use(src->ssa, state);
245bf215546Sopenharmony_ci   else
246bf215546Sopenharmony_ci      print_reg_src(&src->reg, state);
247bf215546Sopenharmony_ci}
248bf215546Sopenharmony_ci
249bf215546Sopenharmony_cistatic void
250bf215546Sopenharmony_ciprint_dest(nir_dest *dest, print_state *state)
251bf215546Sopenharmony_ci{
252bf215546Sopenharmony_ci   if (dest->is_ssa)
253bf215546Sopenharmony_ci      print_ssa_def(&dest->ssa, state);
254bf215546Sopenharmony_ci   else
255bf215546Sopenharmony_ci      print_reg_dest(&dest->reg, state);
256bf215546Sopenharmony_ci}
257bf215546Sopenharmony_ci
258bf215546Sopenharmony_cistatic const char *
259bf215546Sopenharmony_cicomp_mask_string(unsigned num_components)
260bf215546Sopenharmony_ci{
261bf215546Sopenharmony_ci   return (num_components > 4) ? "abcdefghijklmnop" : "xyzw";
262bf215546Sopenharmony_ci}
263bf215546Sopenharmony_ci
264bf215546Sopenharmony_cistatic void
265bf215546Sopenharmony_ciprint_alu_src(nir_alu_instr *instr, unsigned src, print_state *state)
266bf215546Sopenharmony_ci{
267bf215546Sopenharmony_ci   FILE *fp = state->fp;
268bf215546Sopenharmony_ci
269bf215546Sopenharmony_ci   if (instr->src[src].negate)
270bf215546Sopenharmony_ci      fprintf(fp, "-");
271bf215546Sopenharmony_ci   if (instr->src[src].abs)
272bf215546Sopenharmony_ci      fprintf(fp, "abs(");
273bf215546Sopenharmony_ci
274bf215546Sopenharmony_ci   print_src(&instr->src[src].src, state);
275bf215546Sopenharmony_ci
276bf215546Sopenharmony_ci   bool print_swizzle = false;
277bf215546Sopenharmony_ci   nir_component_mask_t used_channels = 0;
278bf215546Sopenharmony_ci
279bf215546Sopenharmony_ci   for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
280bf215546Sopenharmony_ci      if (!nir_alu_instr_channel_used(instr, src, i))
281bf215546Sopenharmony_ci         continue;
282bf215546Sopenharmony_ci
283bf215546Sopenharmony_ci      used_channels++;
284bf215546Sopenharmony_ci
285bf215546Sopenharmony_ci      if (instr->src[src].swizzle[i] != i) {
286bf215546Sopenharmony_ci         print_swizzle = true;
287bf215546Sopenharmony_ci         break;
288bf215546Sopenharmony_ci      }
289bf215546Sopenharmony_ci   }
290bf215546Sopenharmony_ci
291bf215546Sopenharmony_ci   unsigned live_channels = nir_src_num_components(instr->src[src].src);
292bf215546Sopenharmony_ci
293bf215546Sopenharmony_ci   if (print_swizzle || used_channels != live_channels) {
294bf215546Sopenharmony_ci      fprintf(fp, ".");
295bf215546Sopenharmony_ci      for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
296bf215546Sopenharmony_ci         if (!nir_alu_instr_channel_used(instr, src, i))
297bf215546Sopenharmony_ci            continue;
298bf215546Sopenharmony_ci
299bf215546Sopenharmony_ci         fprintf(fp, "%c", comp_mask_string(live_channels)[instr->src[src].swizzle[i]]);
300bf215546Sopenharmony_ci      }
301bf215546Sopenharmony_ci   }
302bf215546Sopenharmony_ci
303bf215546Sopenharmony_ci   if (instr->src[src].abs)
304bf215546Sopenharmony_ci      fprintf(fp, ")");
305bf215546Sopenharmony_ci}
306bf215546Sopenharmony_ci
307bf215546Sopenharmony_cistatic void
308bf215546Sopenharmony_ciprint_alu_dest(nir_alu_dest *dest, print_state *state)
309bf215546Sopenharmony_ci{
310bf215546Sopenharmony_ci   FILE *fp = state->fp;
311bf215546Sopenharmony_ci   /* we're going to print the saturate modifier later, after the opcode */
312bf215546Sopenharmony_ci
313bf215546Sopenharmony_ci   print_dest(&dest->dest, state);
314bf215546Sopenharmony_ci
315bf215546Sopenharmony_ci   if (!dest->dest.is_ssa &&
316bf215546Sopenharmony_ci       dest->write_mask != (1 << dest->dest.reg.reg->num_components) - 1) {
317bf215546Sopenharmony_ci      unsigned live_channels = dest->dest.reg.reg->num_components;
318bf215546Sopenharmony_ci      fprintf(fp, ".");
319bf215546Sopenharmony_ci      for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
320bf215546Sopenharmony_ci         if ((dest->write_mask >> i) & 1)
321bf215546Sopenharmony_ci            fprintf(fp, "%c", comp_mask_string(live_channels)[i]);
322bf215546Sopenharmony_ci   }
323bf215546Sopenharmony_ci}
324bf215546Sopenharmony_ci
325bf215546Sopenharmony_cistatic void
326bf215546Sopenharmony_ciprint_alu_instr(nir_alu_instr *instr, print_state *state)
327bf215546Sopenharmony_ci{
328bf215546Sopenharmony_ci   FILE *fp = state->fp;
329bf215546Sopenharmony_ci
330bf215546Sopenharmony_ci   print_alu_dest(&instr->dest, state);
331bf215546Sopenharmony_ci
332bf215546Sopenharmony_ci   fprintf(fp, " = %s", nir_op_infos[instr->op].name);
333bf215546Sopenharmony_ci   if (instr->exact)
334bf215546Sopenharmony_ci      fprintf(fp, "!");
335bf215546Sopenharmony_ci   if (instr->dest.saturate)
336bf215546Sopenharmony_ci      fprintf(fp, ".sat");
337bf215546Sopenharmony_ci   if (instr->no_signed_wrap)
338bf215546Sopenharmony_ci      fprintf(fp, ".nsw");
339bf215546Sopenharmony_ci   if (instr->no_unsigned_wrap)
340bf215546Sopenharmony_ci      fprintf(fp, ".nuw");
341bf215546Sopenharmony_ci   fprintf(fp, " ");
342bf215546Sopenharmony_ci
343bf215546Sopenharmony_ci   for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
344bf215546Sopenharmony_ci      if (i != 0)
345bf215546Sopenharmony_ci         fprintf(fp, ", ");
346bf215546Sopenharmony_ci
347bf215546Sopenharmony_ci      print_alu_src(instr, i, state);
348bf215546Sopenharmony_ci   }
349bf215546Sopenharmony_ci}
350bf215546Sopenharmony_ci
351bf215546Sopenharmony_cistatic const char *
352bf215546Sopenharmony_ciget_var_name(nir_variable *var, print_state *state)
353bf215546Sopenharmony_ci{
354bf215546Sopenharmony_ci   if (state->ht == NULL)
355bf215546Sopenharmony_ci      return var->name ? var->name : "unnamed";
356bf215546Sopenharmony_ci
357bf215546Sopenharmony_ci   assert(state->syms);
358bf215546Sopenharmony_ci
359bf215546Sopenharmony_ci   struct hash_entry *entry = _mesa_hash_table_search(state->ht, var);
360bf215546Sopenharmony_ci   if (entry)
361bf215546Sopenharmony_ci      return entry->data;
362bf215546Sopenharmony_ci
363bf215546Sopenharmony_ci   char *name;
364bf215546Sopenharmony_ci   if (var->name == NULL) {
365bf215546Sopenharmony_ci      name = ralloc_asprintf(state->syms, "@%u", state->index++);
366bf215546Sopenharmony_ci   } else {
367bf215546Sopenharmony_ci      struct set_entry *set_entry = _mesa_set_search(state->syms, var->name);
368bf215546Sopenharmony_ci      if (set_entry != NULL) {
369bf215546Sopenharmony_ci         /* we have a collision with another name, append an @ + a unique
370bf215546Sopenharmony_ci          * index */
371bf215546Sopenharmony_ci         name = ralloc_asprintf(state->syms, "%s@%u", var->name,
372bf215546Sopenharmony_ci                                state->index++);
373bf215546Sopenharmony_ci      } else {
374bf215546Sopenharmony_ci         /* Mark this one as seen */
375bf215546Sopenharmony_ci         _mesa_set_add(state->syms, var->name);
376bf215546Sopenharmony_ci         name = var->name;
377bf215546Sopenharmony_ci      }
378bf215546Sopenharmony_ci   }
379bf215546Sopenharmony_ci
380bf215546Sopenharmony_ci   _mesa_hash_table_insert(state->ht, var, name);
381bf215546Sopenharmony_ci
382bf215546Sopenharmony_ci   return name;
383bf215546Sopenharmony_ci}
384bf215546Sopenharmony_ci
385bf215546Sopenharmony_cistatic const char *
386bf215546Sopenharmony_ciget_constant_sampler_addressing_mode(enum cl_sampler_addressing_mode mode)
387bf215546Sopenharmony_ci{
388bf215546Sopenharmony_ci   switch (mode) {
389bf215546Sopenharmony_ci   case SAMPLER_ADDRESSING_MODE_NONE: return "none";
390bf215546Sopenharmony_ci   case SAMPLER_ADDRESSING_MODE_CLAMP_TO_EDGE: return "clamp_to_edge";
391bf215546Sopenharmony_ci   case SAMPLER_ADDRESSING_MODE_CLAMP: return "clamp";
392bf215546Sopenharmony_ci   case SAMPLER_ADDRESSING_MODE_REPEAT: return "repeat";
393bf215546Sopenharmony_ci   case SAMPLER_ADDRESSING_MODE_REPEAT_MIRRORED: return "repeat_mirrored";
394bf215546Sopenharmony_ci   default: unreachable("Invalid addressing mode");
395bf215546Sopenharmony_ci   }
396bf215546Sopenharmony_ci}
397bf215546Sopenharmony_ci
398bf215546Sopenharmony_cistatic const char *
399bf215546Sopenharmony_ciget_constant_sampler_filter_mode(enum cl_sampler_filter_mode mode)
400bf215546Sopenharmony_ci{
401bf215546Sopenharmony_ci   switch (mode) {
402bf215546Sopenharmony_ci   case SAMPLER_FILTER_MODE_NEAREST: return "nearest";
403bf215546Sopenharmony_ci   case SAMPLER_FILTER_MODE_LINEAR: return "linear";
404bf215546Sopenharmony_ci   default: unreachable("Invalid filter mode");
405bf215546Sopenharmony_ci   }
406bf215546Sopenharmony_ci}
407bf215546Sopenharmony_ci
408bf215546Sopenharmony_cistatic void
409bf215546Sopenharmony_ciprint_constant(nir_constant *c, const struct glsl_type *type, print_state *state)
410bf215546Sopenharmony_ci{
411bf215546Sopenharmony_ci   FILE *fp = state->fp;
412bf215546Sopenharmony_ci   const unsigned rows = glsl_get_vector_elements(type);
413bf215546Sopenharmony_ci   const unsigned cols = glsl_get_matrix_columns(type);
414bf215546Sopenharmony_ci   unsigned i;
415bf215546Sopenharmony_ci
416bf215546Sopenharmony_ci   switch (glsl_get_base_type(type)) {
417bf215546Sopenharmony_ci   case GLSL_TYPE_BOOL:
418bf215546Sopenharmony_ci      /* Only float base types can be matrices. */
419bf215546Sopenharmony_ci      assert(cols == 1);
420bf215546Sopenharmony_ci
421bf215546Sopenharmony_ci      for (i = 0; i < rows; i++) {
422bf215546Sopenharmony_ci         if (i > 0) fprintf(fp, ", ");
423bf215546Sopenharmony_ci         fprintf(fp, "%s", c->values[i].b ? "true" : "false");
424bf215546Sopenharmony_ci      }
425bf215546Sopenharmony_ci      break;
426bf215546Sopenharmony_ci
427bf215546Sopenharmony_ci   case GLSL_TYPE_UINT8:
428bf215546Sopenharmony_ci   case GLSL_TYPE_INT8:
429bf215546Sopenharmony_ci      /* Only float base types can be matrices. */
430bf215546Sopenharmony_ci      assert(cols == 1);
431bf215546Sopenharmony_ci
432bf215546Sopenharmony_ci      for (i = 0; i < rows; i++) {
433bf215546Sopenharmony_ci         if (i > 0) fprintf(fp, ", ");
434bf215546Sopenharmony_ci         fprintf(fp, "0x%02x", c->values[i].u8);
435bf215546Sopenharmony_ci      }
436bf215546Sopenharmony_ci      break;
437bf215546Sopenharmony_ci
438bf215546Sopenharmony_ci   case GLSL_TYPE_UINT16:
439bf215546Sopenharmony_ci   case GLSL_TYPE_INT16:
440bf215546Sopenharmony_ci      /* Only float base types can be matrices. */
441bf215546Sopenharmony_ci      assert(cols == 1);
442bf215546Sopenharmony_ci
443bf215546Sopenharmony_ci      for (i = 0; i < rows; i++) {
444bf215546Sopenharmony_ci         if (i > 0) fprintf(fp, ", ");
445bf215546Sopenharmony_ci         fprintf(fp, "0x%04x", c->values[i].u16);
446bf215546Sopenharmony_ci      }
447bf215546Sopenharmony_ci      break;
448bf215546Sopenharmony_ci
449bf215546Sopenharmony_ci   case GLSL_TYPE_UINT:
450bf215546Sopenharmony_ci   case GLSL_TYPE_INT:
451bf215546Sopenharmony_ci      /* Only float base types can be matrices. */
452bf215546Sopenharmony_ci      assert(cols == 1);
453bf215546Sopenharmony_ci
454bf215546Sopenharmony_ci      for (i = 0; i < rows; i++) {
455bf215546Sopenharmony_ci         if (i > 0) fprintf(fp, ", ");
456bf215546Sopenharmony_ci         fprintf(fp, "0x%08x", c->values[i].u32);
457bf215546Sopenharmony_ci      }
458bf215546Sopenharmony_ci      break;
459bf215546Sopenharmony_ci
460bf215546Sopenharmony_ci   case GLSL_TYPE_FLOAT16:
461bf215546Sopenharmony_ci   case GLSL_TYPE_FLOAT:
462bf215546Sopenharmony_ci   case GLSL_TYPE_DOUBLE:
463bf215546Sopenharmony_ci      if (cols > 1) {
464bf215546Sopenharmony_ci         for (i = 0; i < cols; i++) {
465bf215546Sopenharmony_ci            if (i > 0) fprintf(fp, ", ");
466bf215546Sopenharmony_ci            print_constant(c->elements[i], glsl_get_column_type(type), state);
467bf215546Sopenharmony_ci         }
468bf215546Sopenharmony_ci      } else {
469bf215546Sopenharmony_ci         switch (glsl_get_base_type(type)) {
470bf215546Sopenharmony_ci         case GLSL_TYPE_FLOAT16:
471bf215546Sopenharmony_ci            for (i = 0; i < rows; i++) {
472bf215546Sopenharmony_ci               if (i > 0) fprintf(fp, ", ");
473bf215546Sopenharmony_ci               fprintf(fp, "%f", _mesa_half_to_float(c->values[i].u16));
474bf215546Sopenharmony_ci            }
475bf215546Sopenharmony_ci            break;
476bf215546Sopenharmony_ci
477bf215546Sopenharmony_ci         case GLSL_TYPE_FLOAT:
478bf215546Sopenharmony_ci            for (i = 0; i < rows; i++) {
479bf215546Sopenharmony_ci               if (i > 0) fprintf(fp, ", ");
480bf215546Sopenharmony_ci               fprintf(fp, "%f", c->values[i].f32);
481bf215546Sopenharmony_ci            }
482bf215546Sopenharmony_ci            break;
483bf215546Sopenharmony_ci
484bf215546Sopenharmony_ci         case GLSL_TYPE_DOUBLE:
485bf215546Sopenharmony_ci            for (i = 0; i < rows; i++) {
486bf215546Sopenharmony_ci               if (i > 0) fprintf(fp, ", ");
487bf215546Sopenharmony_ci               fprintf(fp, "%f", c->values[i].f64);
488bf215546Sopenharmony_ci            }
489bf215546Sopenharmony_ci            break;
490bf215546Sopenharmony_ci
491bf215546Sopenharmony_ci         default:
492bf215546Sopenharmony_ci            unreachable("Cannot get here from the first level switch");
493bf215546Sopenharmony_ci         }
494bf215546Sopenharmony_ci      }
495bf215546Sopenharmony_ci      break;
496bf215546Sopenharmony_ci
497bf215546Sopenharmony_ci   case GLSL_TYPE_UINT64:
498bf215546Sopenharmony_ci   case GLSL_TYPE_INT64:
499bf215546Sopenharmony_ci      /* Only float base types can be matrices. */
500bf215546Sopenharmony_ci      assert(cols == 1);
501bf215546Sopenharmony_ci
502bf215546Sopenharmony_ci      for (i = 0; i < cols; i++) {
503bf215546Sopenharmony_ci         if (i > 0) fprintf(fp, ", ");
504bf215546Sopenharmony_ci         fprintf(fp, "0x%08" PRIx64, c->values[i].u64);
505bf215546Sopenharmony_ci      }
506bf215546Sopenharmony_ci      break;
507bf215546Sopenharmony_ci
508bf215546Sopenharmony_ci   case GLSL_TYPE_STRUCT:
509bf215546Sopenharmony_ci   case GLSL_TYPE_INTERFACE:
510bf215546Sopenharmony_ci      for (i = 0; i < c->num_elements; i++) {
511bf215546Sopenharmony_ci         if (i > 0) fprintf(fp, ", ");
512bf215546Sopenharmony_ci         fprintf(fp, "{ ");
513bf215546Sopenharmony_ci         print_constant(c->elements[i], glsl_get_struct_field(type, i), state);
514bf215546Sopenharmony_ci         fprintf(fp, " }");
515bf215546Sopenharmony_ci      }
516bf215546Sopenharmony_ci      break;
517bf215546Sopenharmony_ci
518bf215546Sopenharmony_ci   case GLSL_TYPE_ARRAY:
519bf215546Sopenharmony_ci      for (i = 0; i < c->num_elements; i++) {
520bf215546Sopenharmony_ci         if (i > 0) fprintf(fp, ", ");
521bf215546Sopenharmony_ci         fprintf(fp, "{ ");
522bf215546Sopenharmony_ci         print_constant(c->elements[i], glsl_get_array_element(type), state);
523bf215546Sopenharmony_ci         fprintf(fp, " }");
524bf215546Sopenharmony_ci      }
525bf215546Sopenharmony_ci      break;
526bf215546Sopenharmony_ci
527bf215546Sopenharmony_ci   default:
528bf215546Sopenharmony_ci      unreachable("not reached");
529bf215546Sopenharmony_ci   }
530bf215546Sopenharmony_ci}
531bf215546Sopenharmony_ci
532bf215546Sopenharmony_cistatic const char *
533bf215546Sopenharmony_ciget_variable_mode_str(nir_variable_mode mode, bool want_local_global_mode)
534bf215546Sopenharmony_ci{
535bf215546Sopenharmony_ci   switch (mode) {
536bf215546Sopenharmony_ci   case nir_var_shader_in:
537bf215546Sopenharmony_ci      return "shader_in";
538bf215546Sopenharmony_ci   case nir_var_shader_out:
539bf215546Sopenharmony_ci      return "shader_out";
540bf215546Sopenharmony_ci   case nir_var_uniform:
541bf215546Sopenharmony_ci      return "uniform";
542bf215546Sopenharmony_ci   case nir_var_mem_ubo:
543bf215546Sopenharmony_ci      return "ubo";
544bf215546Sopenharmony_ci   case nir_var_system_value:
545bf215546Sopenharmony_ci      return "system";
546bf215546Sopenharmony_ci   case nir_var_mem_ssbo:
547bf215546Sopenharmony_ci      return "ssbo";
548bf215546Sopenharmony_ci   case nir_var_mem_shared:
549bf215546Sopenharmony_ci      return "shared";
550bf215546Sopenharmony_ci   case nir_var_mem_global:
551bf215546Sopenharmony_ci      return "global";
552bf215546Sopenharmony_ci   case nir_var_mem_push_const:
553bf215546Sopenharmony_ci      return "push_const";
554bf215546Sopenharmony_ci   case nir_var_mem_constant:
555bf215546Sopenharmony_ci      return "constant";
556bf215546Sopenharmony_ci   case nir_var_image:
557bf215546Sopenharmony_ci      return "image";
558bf215546Sopenharmony_ci   case nir_var_shader_temp:
559bf215546Sopenharmony_ci      return want_local_global_mode ? "shader_temp" : "";
560bf215546Sopenharmony_ci   case nir_var_function_temp:
561bf215546Sopenharmony_ci      return want_local_global_mode ? "function_temp" : "";
562bf215546Sopenharmony_ci   case nir_var_shader_call_data:
563bf215546Sopenharmony_ci      return "shader_call_data";
564bf215546Sopenharmony_ci   case nir_var_ray_hit_attrib:
565bf215546Sopenharmony_ci      return "ray_hit_attrib";
566bf215546Sopenharmony_ci   case nir_var_mem_task_payload:
567bf215546Sopenharmony_ci      return "task_payload";
568bf215546Sopenharmony_ci   default:
569bf215546Sopenharmony_ci      if (mode && (mode & nir_var_mem_generic) == mode)
570bf215546Sopenharmony_ci         return "generic";
571bf215546Sopenharmony_ci      return "";
572bf215546Sopenharmony_ci   }
573bf215546Sopenharmony_ci}
574bf215546Sopenharmony_ci
575bf215546Sopenharmony_cistatic void
576bf215546Sopenharmony_ciprint_var_decl(nir_variable *var, print_state *state)
577bf215546Sopenharmony_ci{
578bf215546Sopenharmony_ci   FILE *fp = state->fp;
579bf215546Sopenharmony_ci
580bf215546Sopenharmony_ci   fprintf(fp, "decl_var ");
581bf215546Sopenharmony_ci
582bf215546Sopenharmony_ci   const char *const bindless = (var->data.bindless) ? "bindless " : "";
583bf215546Sopenharmony_ci   const char *const cent = (var->data.centroid) ? "centroid " : "";
584bf215546Sopenharmony_ci   const char *const samp = (var->data.sample) ? "sample " : "";
585bf215546Sopenharmony_ci   const char *const patch = (var->data.patch) ? "patch " : "";
586bf215546Sopenharmony_ci   const char *const inv = (var->data.invariant) ? "invariant " : "";
587bf215546Sopenharmony_ci   const char *const per_view = (var->data.per_view) ? "per_view " : "";
588bf215546Sopenharmony_ci   const char *const per_primitive = (var->data.per_primitive) ? "per_primitive " : "";
589bf215546Sopenharmony_ci   const char *const ray_query = (var->data.ray_query) ? "ray_query " : "";
590bf215546Sopenharmony_ci   fprintf(fp, "%s%s%s%s%s%s%s%s%s %s ",
591bf215546Sopenharmony_ci           bindless, cent, samp, patch, inv, per_view, per_primitive, ray_query,
592bf215546Sopenharmony_ci           get_variable_mode_str(var->data.mode, false),
593bf215546Sopenharmony_ci           glsl_interp_mode_name(var->data.interpolation));
594bf215546Sopenharmony_ci
595bf215546Sopenharmony_ci   enum gl_access_qualifier access = var->data.access;
596bf215546Sopenharmony_ci   const char *const coher = (access & ACCESS_COHERENT) ? "coherent " : "";
597bf215546Sopenharmony_ci   const char *const volat = (access & ACCESS_VOLATILE) ? "volatile " : "";
598bf215546Sopenharmony_ci   const char *const restr = (access & ACCESS_RESTRICT) ? "restrict " : "";
599bf215546Sopenharmony_ci   const char *const ronly = (access & ACCESS_NON_WRITEABLE) ? "readonly " : "";
600bf215546Sopenharmony_ci   const char *const wonly = (access & ACCESS_NON_READABLE) ? "writeonly " : "";
601bf215546Sopenharmony_ci   const char *const reorder = (access & ACCESS_CAN_REORDER) ? "reorderable " : "";
602bf215546Sopenharmony_ci   const char *const stream_cache_policy = (access & ACCESS_STREAM_CACHE_POLICY) ?
603bf215546Sopenharmony_ci                                           "stream-cache-policy " : "";
604bf215546Sopenharmony_ci   const char *const include_helpers = (access & ACCESS_INCLUDE_HELPERS) ?
605bf215546Sopenharmony_ci                                       "include-helpers " : "";
606bf215546Sopenharmony_ci   fprintf(fp, "%s%s%s%s%s%s%s%s", coher, volat, restr, ronly, wonly, reorder,
607bf215546Sopenharmony_ci           stream_cache_policy, include_helpers);
608bf215546Sopenharmony_ci
609bf215546Sopenharmony_ci   if (glsl_get_base_type(glsl_without_array(var->type)) == GLSL_TYPE_IMAGE) {
610bf215546Sopenharmony_ci      fprintf(fp, "%s ", util_format_short_name(var->data.image.format));
611bf215546Sopenharmony_ci   }
612bf215546Sopenharmony_ci
613bf215546Sopenharmony_ci   if (var->data.precision) {
614bf215546Sopenharmony_ci      const char *precisions[] = {
615bf215546Sopenharmony_ci         "",
616bf215546Sopenharmony_ci         "highp",
617bf215546Sopenharmony_ci         "mediump",
618bf215546Sopenharmony_ci         "lowp",
619bf215546Sopenharmony_ci      };
620bf215546Sopenharmony_ci      fprintf(fp, "%s ", precisions[var->data.precision]);
621bf215546Sopenharmony_ci   }
622bf215546Sopenharmony_ci
623bf215546Sopenharmony_ci   fprintf(fp, "%s %s", glsl_get_type_name(var->type),
624bf215546Sopenharmony_ci           get_var_name(var, state));
625bf215546Sopenharmony_ci
626bf215546Sopenharmony_ci   if (var->data.mode & (nir_var_shader_in |
627bf215546Sopenharmony_ci                         nir_var_shader_out |
628bf215546Sopenharmony_ci                         nir_var_uniform |
629bf215546Sopenharmony_ci                         nir_var_mem_ubo |
630bf215546Sopenharmony_ci                         nir_var_mem_ssbo |
631bf215546Sopenharmony_ci                         nir_var_image)) {
632bf215546Sopenharmony_ci      const char *loc = NULL;
633bf215546Sopenharmony_ci      char buf[4];
634bf215546Sopenharmony_ci
635bf215546Sopenharmony_ci      switch (state->shader->info.stage) {
636bf215546Sopenharmony_ci      case MESA_SHADER_VERTEX:
637bf215546Sopenharmony_ci         if (var->data.mode == nir_var_shader_in)
638bf215546Sopenharmony_ci            loc = gl_vert_attrib_name(var->data.location);
639bf215546Sopenharmony_ci         else if (var->data.mode == nir_var_shader_out)
640bf215546Sopenharmony_ci            loc = gl_varying_slot_name_for_stage(var->data.location,
641bf215546Sopenharmony_ci                                                 state->shader->info.stage);
642bf215546Sopenharmony_ci         break;
643bf215546Sopenharmony_ci      case MESA_SHADER_TASK:
644bf215546Sopenharmony_ci      case MESA_SHADER_MESH:
645bf215546Sopenharmony_ci      case MESA_SHADER_GEOMETRY:
646bf215546Sopenharmony_ci         if ((var->data.mode == nir_var_shader_in) ||
647bf215546Sopenharmony_ci             (var->data.mode == nir_var_shader_out)) {
648bf215546Sopenharmony_ci            loc = gl_varying_slot_name_for_stage(var->data.location,
649bf215546Sopenharmony_ci                                                 state->shader->info.stage);
650bf215546Sopenharmony_ci         }
651bf215546Sopenharmony_ci         break;
652bf215546Sopenharmony_ci      case MESA_SHADER_FRAGMENT:
653bf215546Sopenharmony_ci         if (var->data.mode == nir_var_shader_in) {
654bf215546Sopenharmony_ci            loc = gl_varying_slot_name_for_stage(var->data.location,
655bf215546Sopenharmony_ci                                                 state->shader->info.stage);
656bf215546Sopenharmony_ci         } else if (var->data.mode == nir_var_shader_out) {
657bf215546Sopenharmony_ci            loc = gl_frag_result_name(var->data.location);
658bf215546Sopenharmony_ci         }
659bf215546Sopenharmony_ci         break;
660bf215546Sopenharmony_ci      case MESA_SHADER_TESS_CTRL:
661bf215546Sopenharmony_ci      case MESA_SHADER_TESS_EVAL:
662bf215546Sopenharmony_ci      case MESA_SHADER_COMPUTE:
663bf215546Sopenharmony_ci      case MESA_SHADER_KERNEL:
664bf215546Sopenharmony_ci      default:
665bf215546Sopenharmony_ci         /* TODO */
666bf215546Sopenharmony_ci         break;
667bf215546Sopenharmony_ci      }
668bf215546Sopenharmony_ci
669bf215546Sopenharmony_ci      if (!loc) {
670bf215546Sopenharmony_ci         if (var->data.location == ~0) {
671bf215546Sopenharmony_ci            loc = "~0";
672bf215546Sopenharmony_ci         } else {
673bf215546Sopenharmony_ci            snprintf(buf, sizeof(buf), "%u", var->data.location);
674bf215546Sopenharmony_ci            loc = buf;
675bf215546Sopenharmony_ci         }
676bf215546Sopenharmony_ci      }
677bf215546Sopenharmony_ci
678bf215546Sopenharmony_ci      /* For shader I/O vars that have been split to components or packed,
679bf215546Sopenharmony_ci       * print the fractional location within the input/output.
680bf215546Sopenharmony_ci       */
681bf215546Sopenharmony_ci      unsigned int num_components =
682bf215546Sopenharmony_ci         glsl_get_components(glsl_without_array(var->type));
683bf215546Sopenharmony_ci      const char *components = NULL;
684bf215546Sopenharmony_ci      char components_local[18] = {'.' /* the rest is 0-filled */};
685bf215546Sopenharmony_ci      switch (var->data.mode) {
686bf215546Sopenharmony_ci      case nir_var_shader_in:
687bf215546Sopenharmony_ci      case nir_var_shader_out:
688bf215546Sopenharmony_ci         if (num_components < 16 && num_components != 0) {
689bf215546Sopenharmony_ci            const char *xyzw = comp_mask_string(num_components);
690bf215546Sopenharmony_ci            for (int i = 0; i < num_components; i++)
691bf215546Sopenharmony_ci               components_local[i + 1] = xyzw[i + var->data.location_frac];
692bf215546Sopenharmony_ci
693bf215546Sopenharmony_ci            components = components_local;
694bf215546Sopenharmony_ci         }
695bf215546Sopenharmony_ci         break;
696bf215546Sopenharmony_ci      default:
697bf215546Sopenharmony_ci         break;
698bf215546Sopenharmony_ci      }
699bf215546Sopenharmony_ci
700bf215546Sopenharmony_ci      fprintf(fp, " (%s%s, %u, %u)%s", loc,
701bf215546Sopenharmony_ci              components ? components : "",
702bf215546Sopenharmony_ci              var->data.driver_location, var->data.binding,
703bf215546Sopenharmony_ci              var->data.compact ? " compact" : "");
704bf215546Sopenharmony_ci   }
705bf215546Sopenharmony_ci
706bf215546Sopenharmony_ci   if (var->constant_initializer) {
707bf215546Sopenharmony_ci      fprintf(fp, " = { ");
708bf215546Sopenharmony_ci      print_constant(var->constant_initializer, var->type, state);
709bf215546Sopenharmony_ci      fprintf(fp, " }");
710bf215546Sopenharmony_ci   }
711bf215546Sopenharmony_ci   if (glsl_type_is_sampler(var->type) && var->data.sampler.is_inline_sampler) {
712bf215546Sopenharmony_ci      fprintf(fp, " = { %s, %s, %s }",
713bf215546Sopenharmony_ci              get_constant_sampler_addressing_mode(var->data.sampler.addressing_mode),
714bf215546Sopenharmony_ci              var->data.sampler.normalized_coordinates ? "true" : "false",
715bf215546Sopenharmony_ci              get_constant_sampler_filter_mode(var->data.sampler.filter_mode));
716bf215546Sopenharmony_ci   }
717bf215546Sopenharmony_ci   if (var->pointer_initializer)
718bf215546Sopenharmony_ci      fprintf(fp, " = &%s", get_var_name(var->pointer_initializer, state));
719bf215546Sopenharmony_ci
720bf215546Sopenharmony_ci   fprintf(fp, "\n");
721bf215546Sopenharmony_ci   print_annotation(state, var);
722bf215546Sopenharmony_ci}
723bf215546Sopenharmony_ci
724bf215546Sopenharmony_cistatic void
725bf215546Sopenharmony_ciprint_deref_link(const nir_deref_instr *instr, bool whole_chain, print_state *state)
726bf215546Sopenharmony_ci{
727bf215546Sopenharmony_ci   FILE *fp = state->fp;
728bf215546Sopenharmony_ci
729bf215546Sopenharmony_ci   if (instr->deref_type == nir_deref_type_var) {
730bf215546Sopenharmony_ci      fprintf(fp, "%s", get_var_name(instr->var, state));
731bf215546Sopenharmony_ci      return;
732bf215546Sopenharmony_ci   } else if (instr->deref_type == nir_deref_type_cast) {
733bf215546Sopenharmony_ci      fprintf(fp, "(%s *)", glsl_get_type_name(instr->type));
734bf215546Sopenharmony_ci      print_src(&instr->parent, state);
735bf215546Sopenharmony_ci      return;
736bf215546Sopenharmony_ci   }
737bf215546Sopenharmony_ci
738bf215546Sopenharmony_ci   assert(instr->parent.is_ssa);
739bf215546Sopenharmony_ci   nir_deref_instr *parent =
740bf215546Sopenharmony_ci      nir_instr_as_deref(instr->parent.ssa->parent_instr);
741bf215546Sopenharmony_ci
742bf215546Sopenharmony_ci   /* Is the parent we're going to print a bare cast? */
743bf215546Sopenharmony_ci   const bool is_parent_cast =
744bf215546Sopenharmony_ci      whole_chain && parent->deref_type == nir_deref_type_cast;
745bf215546Sopenharmony_ci
746bf215546Sopenharmony_ci   /* If we're not printing the whole chain, the parent we print will be a SSA
747bf215546Sopenharmony_ci    * value that represents a pointer.  The only deref type that naturally
748bf215546Sopenharmony_ci    * gives a pointer is a cast.
749bf215546Sopenharmony_ci    */
750bf215546Sopenharmony_ci   const bool is_parent_pointer =
751bf215546Sopenharmony_ci      !whole_chain || parent->deref_type == nir_deref_type_cast;
752bf215546Sopenharmony_ci
753bf215546Sopenharmony_ci   /* Struct derefs have a nice syntax that works on pointers, arrays derefs
754bf215546Sopenharmony_ci    * do not.
755bf215546Sopenharmony_ci    */
756bf215546Sopenharmony_ci   const bool need_deref =
757bf215546Sopenharmony_ci      is_parent_pointer && instr->deref_type != nir_deref_type_struct;
758bf215546Sopenharmony_ci
759bf215546Sopenharmony_ci   /* Cast need extra parens and so * dereferences */
760bf215546Sopenharmony_ci   if (is_parent_cast || need_deref)
761bf215546Sopenharmony_ci      fprintf(fp, "(");
762bf215546Sopenharmony_ci
763bf215546Sopenharmony_ci   if (need_deref)
764bf215546Sopenharmony_ci      fprintf(fp, "*");
765bf215546Sopenharmony_ci
766bf215546Sopenharmony_ci   if (whole_chain) {
767bf215546Sopenharmony_ci      print_deref_link(parent, whole_chain, state);
768bf215546Sopenharmony_ci   } else {
769bf215546Sopenharmony_ci      print_src(&instr->parent, state);
770bf215546Sopenharmony_ci   }
771bf215546Sopenharmony_ci
772bf215546Sopenharmony_ci   if (is_parent_cast || need_deref)
773bf215546Sopenharmony_ci      fprintf(fp, ")");
774bf215546Sopenharmony_ci
775bf215546Sopenharmony_ci   switch (instr->deref_type) {
776bf215546Sopenharmony_ci   case nir_deref_type_struct:
777bf215546Sopenharmony_ci      fprintf(fp, "%s%s", is_parent_pointer ? "->" : ".",
778bf215546Sopenharmony_ci              glsl_get_struct_elem_name(parent->type, instr->strct.index));
779bf215546Sopenharmony_ci      break;
780bf215546Sopenharmony_ci
781bf215546Sopenharmony_ci   case nir_deref_type_array:
782bf215546Sopenharmony_ci   case nir_deref_type_ptr_as_array: {
783bf215546Sopenharmony_ci      if (nir_src_is_const(instr->arr.index)) {
784bf215546Sopenharmony_ci         fprintf(fp, "[%"PRId64"]", nir_src_as_int(instr->arr.index));
785bf215546Sopenharmony_ci      } else {
786bf215546Sopenharmony_ci         fprintf(fp, "[");
787bf215546Sopenharmony_ci         print_src(&instr->arr.index, state);
788bf215546Sopenharmony_ci         fprintf(fp, "]");
789bf215546Sopenharmony_ci      }
790bf215546Sopenharmony_ci      break;
791bf215546Sopenharmony_ci   }
792bf215546Sopenharmony_ci
793bf215546Sopenharmony_ci   case nir_deref_type_array_wildcard:
794bf215546Sopenharmony_ci      fprintf(fp, "[*]");
795bf215546Sopenharmony_ci      break;
796bf215546Sopenharmony_ci
797bf215546Sopenharmony_ci   default:
798bf215546Sopenharmony_ci      unreachable("Invalid deref instruction type");
799bf215546Sopenharmony_ci   }
800bf215546Sopenharmony_ci}
801bf215546Sopenharmony_ci
802bf215546Sopenharmony_cistatic void
803bf215546Sopenharmony_ciprint_deref_instr(nir_deref_instr *instr, print_state *state)
804bf215546Sopenharmony_ci{
805bf215546Sopenharmony_ci   FILE *fp = state->fp;
806bf215546Sopenharmony_ci
807bf215546Sopenharmony_ci   print_dest(&instr->dest, state);
808bf215546Sopenharmony_ci
809bf215546Sopenharmony_ci   switch (instr->deref_type) {
810bf215546Sopenharmony_ci   case nir_deref_type_var:
811bf215546Sopenharmony_ci      fprintf(fp, " = deref_var ");
812bf215546Sopenharmony_ci      break;
813bf215546Sopenharmony_ci   case nir_deref_type_array:
814bf215546Sopenharmony_ci   case nir_deref_type_array_wildcard:
815bf215546Sopenharmony_ci      fprintf(fp, " = deref_array ");
816bf215546Sopenharmony_ci      break;
817bf215546Sopenharmony_ci   case nir_deref_type_struct:
818bf215546Sopenharmony_ci      fprintf(fp, " = deref_struct ");
819bf215546Sopenharmony_ci      break;
820bf215546Sopenharmony_ci   case nir_deref_type_cast:
821bf215546Sopenharmony_ci      fprintf(fp, " = deref_cast ");
822bf215546Sopenharmony_ci      break;
823bf215546Sopenharmony_ci   case nir_deref_type_ptr_as_array:
824bf215546Sopenharmony_ci      fprintf(fp, " = deref_ptr_as_array ");
825bf215546Sopenharmony_ci      break;
826bf215546Sopenharmony_ci   default:
827bf215546Sopenharmony_ci      unreachable("Invalid deref instruction type");
828bf215546Sopenharmony_ci   }
829bf215546Sopenharmony_ci
830bf215546Sopenharmony_ci   /* Only casts naturally return a pointer type */
831bf215546Sopenharmony_ci   if (instr->deref_type != nir_deref_type_cast)
832bf215546Sopenharmony_ci      fprintf(fp, "&");
833bf215546Sopenharmony_ci
834bf215546Sopenharmony_ci   print_deref_link(instr, false, state);
835bf215546Sopenharmony_ci
836bf215546Sopenharmony_ci   fprintf(fp, " (");
837bf215546Sopenharmony_ci   unsigned modes = instr->modes;
838bf215546Sopenharmony_ci   while (modes) {
839bf215546Sopenharmony_ci      int m = u_bit_scan(&modes);
840bf215546Sopenharmony_ci      fprintf(fp, "%s%s", get_variable_mode_str(1 << m, true),
841bf215546Sopenharmony_ci                          modes ? "|" : "");
842bf215546Sopenharmony_ci   }
843bf215546Sopenharmony_ci   fprintf(fp, " %s) ", glsl_get_type_name(instr->type));
844bf215546Sopenharmony_ci
845bf215546Sopenharmony_ci   if (instr->deref_type != nir_deref_type_var &&
846bf215546Sopenharmony_ci       instr->deref_type != nir_deref_type_cast) {
847bf215546Sopenharmony_ci      /* Print the entire chain as a comment */
848bf215546Sopenharmony_ci      fprintf(fp, "/* &");
849bf215546Sopenharmony_ci      print_deref_link(instr, true, state);
850bf215546Sopenharmony_ci      fprintf(fp, " */");
851bf215546Sopenharmony_ci   }
852bf215546Sopenharmony_ci
853bf215546Sopenharmony_ci   if (instr->deref_type == nir_deref_type_cast) {
854bf215546Sopenharmony_ci      fprintf(fp, " /* ptr_stride=%u, align_mul=%u, align_offset=%u */",
855bf215546Sopenharmony_ci              instr->cast.ptr_stride,
856bf215546Sopenharmony_ci              instr->cast.align_mul, instr->cast.align_offset);
857bf215546Sopenharmony_ci   }
858bf215546Sopenharmony_ci}
859bf215546Sopenharmony_ci
860bf215546Sopenharmony_cistatic const char *
861bf215546Sopenharmony_civulkan_descriptor_type_name(VkDescriptorType type)
862bf215546Sopenharmony_ci{
863bf215546Sopenharmony_ci   switch (type) {
864bf215546Sopenharmony_ci   case VK_DESCRIPTOR_TYPE_SAMPLER: return "sampler";
865bf215546Sopenharmony_ci   case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: return "texture+sampler";
866bf215546Sopenharmony_ci   case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: return "texture";
867bf215546Sopenharmony_ci   case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: return "image";
868bf215546Sopenharmony_ci   case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: return "texture-buffer";
869bf215546Sopenharmony_ci   case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: return "image-buffer";
870bf215546Sopenharmony_ci   case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: return "UBO";
871bf215546Sopenharmony_ci   case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: return "SSBO";
872bf215546Sopenharmony_ci   case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: return "UBO";
873bf215546Sopenharmony_ci   case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: return "SSBO";
874bf215546Sopenharmony_ci   case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: return "input-att";
875bf215546Sopenharmony_ci   case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK: return "inline-UBO";
876bf215546Sopenharmony_ci   case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR: return "accel-struct";
877bf215546Sopenharmony_ci   default: return "unknown";
878bf215546Sopenharmony_ci   }
879bf215546Sopenharmony_ci}
880bf215546Sopenharmony_ci
881bf215546Sopenharmony_cistatic void
882bf215546Sopenharmony_ciprint_alu_type(nir_alu_type type, print_state *state)
883bf215546Sopenharmony_ci{
884bf215546Sopenharmony_ci   FILE *fp = state->fp;
885bf215546Sopenharmony_ci   unsigned size = nir_alu_type_get_type_size(type);
886bf215546Sopenharmony_ci   const char *name;
887bf215546Sopenharmony_ci
888bf215546Sopenharmony_ci   switch (nir_alu_type_get_base_type(type)) {
889bf215546Sopenharmony_ci   case nir_type_int: name = "int"; break;
890bf215546Sopenharmony_ci   case nir_type_uint: name = "uint"; break;
891bf215546Sopenharmony_ci   case nir_type_bool: name = "bool"; break;
892bf215546Sopenharmony_ci   case nir_type_float: name = "float"; break;
893bf215546Sopenharmony_ci   default: name = "invalid";
894bf215546Sopenharmony_ci   }
895bf215546Sopenharmony_ci   if (size)
896bf215546Sopenharmony_ci      fprintf(fp, "%s%u", name, size);
897bf215546Sopenharmony_ci   else
898bf215546Sopenharmony_ci      fprintf(fp, "%s", name);
899bf215546Sopenharmony_ci}
900bf215546Sopenharmony_ci
901bf215546Sopenharmony_cistatic void
902bf215546Sopenharmony_ciprint_intrinsic_instr(nir_intrinsic_instr *instr, print_state *state)
903bf215546Sopenharmony_ci{
904bf215546Sopenharmony_ci   const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
905bf215546Sopenharmony_ci   unsigned num_srcs = info->num_srcs;
906bf215546Sopenharmony_ci   FILE *fp = state->fp;
907bf215546Sopenharmony_ci
908bf215546Sopenharmony_ci   if (info->has_dest) {
909bf215546Sopenharmony_ci      print_dest(&instr->dest, state);
910bf215546Sopenharmony_ci      fprintf(fp, " = ");
911bf215546Sopenharmony_ci   }
912bf215546Sopenharmony_ci
913bf215546Sopenharmony_ci   fprintf(fp, "intrinsic %s (", info->name);
914bf215546Sopenharmony_ci
915bf215546Sopenharmony_ci   for (unsigned i = 0; i < num_srcs; i++) {
916bf215546Sopenharmony_ci      if (i != 0)
917bf215546Sopenharmony_ci         fprintf(fp, ", ");
918bf215546Sopenharmony_ci
919bf215546Sopenharmony_ci      print_src(&instr->src[i], state);
920bf215546Sopenharmony_ci   }
921bf215546Sopenharmony_ci
922bf215546Sopenharmony_ci   fprintf(fp, ") (");
923bf215546Sopenharmony_ci
924bf215546Sopenharmony_ci   for (unsigned i = 0; i < info->num_indices; i++) {
925bf215546Sopenharmony_ci      unsigned idx = info->indices[i];
926bf215546Sopenharmony_ci      bool print_raw = true;
927bf215546Sopenharmony_ci      if (i != 0)
928bf215546Sopenharmony_ci         fprintf(fp, ", ");
929bf215546Sopenharmony_ci      switch (idx) {
930bf215546Sopenharmony_ci      case NIR_INTRINSIC_WRITE_MASK: {
931bf215546Sopenharmony_ci         /* special case wrmask to show it as a writemask.. */
932bf215546Sopenharmony_ci         unsigned wrmask = nir_intrinsic_write_mask(instr);
933bf215546Sopenharmony_ci         fprintf(fp, "wrmask=");
934bf215546Sopenharmony_ci         for (unsigned i = 0; i < instr->num_components; i++)
935bf215546Sopenharmony_ci            if ((wrmask >> i) & 1)
936bf215546Sopenharmony_ci               fprintf(fp, "%c", comp_mask_string(instr->num_components)[i]);
937bf215546Sopenharmony_ci         break;
938bf215546Sopenharmony_ci      }
939bf215546Sopenharmony_ci
940bf215546Sopenharmony_ci      case NIR_INTRINSIC_REDUCTION_OP: {
941bf215546Sopenharmony_ci         nir_op reduction_op = nir_intrinsic_reduction_op(instr);
942bf215546Sopenharmony_ci         fprintf(fp, "reduction_op=%s", nir_op_infos[reduction_op].name);
943bf215546Sopenharmony_ci         break;
944bf215546Sopenharmony_ci      }
945bf215546Sopenharmony_ci
946bf215546Sopenharmony_ci      case NIR_INTRINSIC_IMAGE_DIM: {
947bf215546Sopenharmony_ci         static const char *dim_name[] = {
948bf215546Sopenharmony_ci            [GLSL_SAMPLER_DIM_1D] = "1D",
949bf215546Sopenharmony_ci            [GLSL_SAMPLER_DIM_2D] = "2D",
950bf215546Sopenharmony_ci            [GLSL_SAMPLER_DIM_3D] = "3D",
951bf215546Sopenharmony_ci            [GLSL_SAMPLER_DIM_CUBE] = "Cube",
952bf215546Sopenharmony_ci            [GLSL_SAMPLER_DIM_RECT] = "Rect",
953bf215546Sopenharmony_ci            [GLSL_SAMPLER_DIM_BUF] = "Buf",
954bf215546Sopenharmony_ci            [GLSL_SAMPLER_DIM_MS] = "2D-MSAA",
955bf215546Sopenharmony_ci            [GLSL_SAMPLER_DIM_SUBPASS] = "Subpass",
956bf215546Sopenharmony_ci            [GLSL_SAMPLER_DIM_SUBPASS_MS] = "Subpass-MSAA",
957bf215546Sopenharmony_ci         };
958bf215546Sopenharmony_ci         enum glsl_sampler_dim dim = nir_intrinsic_image_dim(instr);
959bf215546Sopenharmony_ci         assert(dim < ARRAY_SIZE(dim_name) && dim_name[dim]);
960bf215546Sopenharmony_ci         fprintf(fp, "image_dim=%s", dim_name[dim]);
961bf215546Sopenharmony_ci         break;
962bf215546Sopenharmony_ci      }
963bf215546Sopenharmony_ci
964bf215546Sopenharmony_ci      case NIR_INTRINSIC_IMAGE_ARRAY: {
965bf215546Sopenharmony_ci         bool array = nir_intrinsic_image_array(instr);
966bf215546Sopenharmony_ci         fprintf(fp, "image_array=%s", array ? "true" : "false");
967bf215546Sopenharmony_ci         break;
968bf215546Sopenharmony_ci      }
969bf215546Sopenharmony_ci
970bf215546Sopenharmony_ci      case NIR_INTRINSIC_FORMAT: {
971bf215546Sopenharmony_ci         enum pipe_format format = nir_intrinsic_format(instr);
972bf215546Sopenharmony_ci         fprintf(fp, "format=%s", util_format_short_name(format));
973bf215546Sopenharmony_ci         break;
974bf215546Sopenharmony_ci      }
975bf215546Sopenharmony_ci
976bf215546Sopenharmony_ci      case NIR_INTRINSIC_DESC_TYPE: {
977bf215546Sopenharmony_ci         VkDescriptorType desc_type = nir_intrinsic_desc_type(instr);
978bf215546Sopenharmony_ci         fprintf(fp, "desc_type=%s", vulkan_descriptor_type_name(desc_type));
979bf215546Sopenharmony_ci         break;
980bf215546Sopenharmony_ci      }
981bf215546Sopenharmony_ci
982bf215546Sopenharmony_ci      case NIR_INTRINSIC_SRC_TYPE: {
983bf215546Sopenharmony_ci         fprintf(fp, "src_type=");
984bf215546Sopenharmony_ci         print_alu_type(nir_intrinsic_src_type(instr), state);
985bf215546Sopenharmony_ci         break;
986bf215546Sopenharmony_ci      }
987bf215546Sopenharmony_ci
988bf215546Sopenharmony_ci      case NIR_INTRINSIC_DEST_TYPE: {
989bf215546Sopenharmony_ci         fprintf(fp, "dest_type=");
990bf215546Sopenharmony_ci         print_alu_type(nir_intrinsic_dest_type(instr), state);
991bf215546Sopenharmony_ci         break;
992bf215546Sopenharmony_ci      }
993bf215546Sopenharmony_ci
994bf215546Sopenharmony_ci      case NIR_INTRINSIC_SWIZZLE_MASK: {
995bf215546Sopenharmony_ci         fprintf(fp, "swizzle_mask=");
996bf215546Sopenharmony_ci         unsigned mask = nir_intrinsic_swizzle_mask(instr);
997bf215546Sopenharmony_ci         if (instr->intrinsic == nir_intrinsic_quad_swizzle_amd) {
998bf215546Sopenharmony_ci            for (unsigned i = 0; i < 4; i++)
999bf215546Sopenharmony_ci               fprintf(fp, "%d", (mask >> (i * 2) & 3));
1000bf215546Sopenharmony_ci         } else if (instr->intrinsic == nir_intrinsic_masked_swizzle_amd) {
1001bf215546Sopenharmony_ci            fprintf(fp, "((id & %d) | %d) ^ %d", mask & 0x1F,
1002bf215546Sopenharmony_ci                                                (mask >> 5) & 0x1F,
1003bf215546Sopenharmony_ci                                                (mask >> 10) & 0x1F);
1004bf215546Sopenharmony_ci         } else {
1005bf215546Sopenharmony_ci            fprintf(fp, "%d", mask);
1006bf215546Sopenharmony_ci         }
1007bf215546Sopenharmony_ci         break;
1008bf215546Sopenharmony_ci      }
1009bf215546Sopenharmony_ci
1010bf215546Sopenharmony_ci      case NIR_INTRINSIC_MEMORY_SEMANTICS: {
1011bf215546Sopenharmony_ci         nir_memory_semantics semantics = nir_intrinsic_memory_semantics(instr);
1012bf215546Sopenharmony_ci         fprintf(fp, "mem_semantics=");
1013bf215546Sopenharmony_ci         switch (semantics & (NIR_MEMORY_ACQUIRE | NIR_MEMORY_RELEASE)) {
1014bf215546Sopenharmony_ci         case 0:                  fprintf(fp, "NONE");    break;
1015bf215546Sopenharmony_ci         case NIR_MEMORY_ACQUIRE: fprintf(fp, "ACQ");     break;
1016bf215546Sopenharmony_ci         case NIR_MEMORY_RELEASE: fprintf(fp, "REL");     break;
1017bf215546Sopenharmony_ci         default:                 fprintf(fp, "ACQ|REL"); break;
1018bf215546Sopenharmony_ci         }
1019bf215546Sopenharmony_ci         if (semantics & (NIR_MEMORY_MAKE_AVAILABLE)) fprintf(fp, "|AVAILABLE");
1020bf215546Sopenharmony_ci         if (semantics & (NIR_MEMORY_MAKE_VISIBLE))   fprintf(fp, "|VISIBLE");
1021bf215546Sopenharmony_ci         break;
1022bf215546Sopenharmony_ci      }
1023bf215546Sopenharmony_ci
1024bf215546Sopenharmony_ci      case NIR_INTRINSIC_MEMORY_MODES: {
1025bf215546Sopenharmony_ci         fprintf(fp, "mem_modes=");
1026bf215546Sopenharmony_ci         unsigned int modes = nir_intrinsic_memory_modes(instr);
1027bf215546Sopenharmony_ci         while (modes) {
1028bf215546Sopenharmony_ci            nir_variable_mode m = u_bit_scan(&modes);
1029bf215546Sopenharmony_ci            fprintf(fp, "%s%s", get_variable_mode_str(1 << m, true), modes ? "|" : "");
1030bf215546Sopenharmony_ci         }
1031bf215546Sopenharmony_ci         break;
1032bf215546Sopenharmony_ci      }
1033bf215546Sopenharmony_ci
1034bf215546Sopenharmony_ci      case NIR_INTRINSIC_EXECUTION_SCOPE:
1035bf215546Sopenharmony_ci      case NIR_INTRINSIC_MEMORY_SCOPE: {
1036bf215546Sopenharmony_ci         fprintf(fp, "%s=", nir_intrinsic_index_names[idx]);
1037bf215546Sopenharmony_ci         nir_scope scope =
1038bf215546Sopenharmony_ci            idx == NIR_INTRINSIC_MEMORY_SCOPE ? nir_intrinsic_memory_scope(instr)
1039bf215546Sopenharmony_ci                                              : nir_intrinsic_execution_scope(instr);
1040bf215546Sopenharmony_ci         switch (scope) {
1041bf215546Sopenharmony_ci         case NIR_SCOPE_NONE:         fprintf(fp, "NONE");         break;
1042bf215546Sopenharmony_ci         case NIR_SCOPE_DEVICE:       fprintf(fp, "DEVICE");       break;
1043bf215546Sopenharmony_ci         case NIR_SCOPE_QUEUE_FAMILY: fprintf(fp, "QUEUE_FAMILY"); break;
1044bf215546Sopenharmony_ci         case NIR_SCOPE_WORKGROUP:    fprintf(fp, "WORKGROUP");    break;
1045bf215546Sopenharmony_ci         case NIR_SCOPE_SHADER_CALL:  fprintf(fp, "SHADER_CALL");  break;
1046bf215546Sopenharmony_ci         case NIR_SCOPE_SUBGROUP:     fprintf(fp, "SUBGROUP");     break;
1047bf215546Sopenharmony_ci         case NIR_SCOPE_INVOCATION:   fprintf(fp, "INVOCATION");   break;
1048bf215546Sopenharmony_ci         }
1049bf215546Sopenharmony_ci         break;
1050bf215546Sopenharmony_ci      }
1051bf215546Sopenharmony_ci
1052bf215546Sopenharmony_ci      case NIR_INTRINSIC_IO_SEMANTICS: {
1053bf215546Sopenharmony_ci         struct nir_io_semantics io = nir_intrinsic_io_semantics(instr);
1054bf215546Sopenharmony_ci         fprintf(fp, "io location=%u slots=%u", io.location, io.num_slots);
1055bf215546Sopenharmony_ci
1056bf215546Sopenharmony_ci         if (io.dual_source_blend_index)
1057bf215546Sopenharmony_ci            fprintf(fp, " dualsrc");
1058bf215546Sopenharmony_ci
1059bf215546Sopenharmony_ci         if (io.fb_fetch_output)
1060bf215546Sopenharmony_ci            fprintf(fp, " fbfetch");
1061bf215546Sopenharmony_ci
1062bf215546Sopenharmony_ci         if (io.per_view)
1063bf215546Sopenharmony_ci            fprintf(fp, " perview");
1064bf215546Sopenharmony_ci
1065bf215546Sopenharmony_ci         if (io.medium_precision)
1066bf215546Sopenharmony_ci            fprintf(fp, " mediump");
1067bf215546Sopenharmony_ci
1068bf215546Sopenharmony_ci         if (io.high_16bits)
1069bf215546Sopenharmony_ci            fprintf(fp, " high_16bits");
1070bf215546Sopenharmony_ci
1071bf215546Sopenharmony_ci         if (io.no_varying)
1072bf215546Sopenharmony_ci            fprintf(fp, " no_varying");
1073bf215546Sopenharmony_ci
1074bf215546Sopenharmony_ci         if (io.no_sysval_output)
1075bf215546Sopenharmony_ci            fprintf(fp, " no_sysval_output");
1076bf215546Sopenharmony_ci
1077bf215546Sopenharmony_ci         if (state->shader &&
1078bf215546Sopenharmony_ci               state->shader->info.stage == MESA_SHADER_GEOMETRY &&
1079bf215546Sopenharmony_ci               (instr->intrinsic == nir_intrinsic_store_output ||
1080bf215546Sopenharmony_ci                instr->intrinsic == nir_intrinsic_store_per_primitive_output ||
1081bf215546Sopenharmony_ci                instr->intrinsic == nir_intrinsic_store_per_vertex_output)) {
1082bf215546Sopenharmony_ci            unsigned gs_streams = io.gs_streams;
1083bf215546Sopenharmony_ci            fprintf(fp, " gs_streams(");
1084bf215546Sopenharmony_ci            for (unsigned i = 0; i < 4; i++) {
1085bf215546Sopenharmony_ci               fprintf(fp, "%s%c=%u", i ? " " : "", "xyzw"[i],
1086bf215546Sopenharmony_ci                       (gs_streams >> (i * 2)) & 0x3);
1087bf215546Sopenharmony_ci            }
1088bf215546Sopenharmony_ci            fprintf(fp, ")");
1089bf215546Sopenharmony_ci         }
1090bf215546Sopenharmony_ci
1091bf215546Sopenharmony_ci         break;
1092bf215546Sopenharmony_ci      }
1093bf215546Sopenharmony_ci
1094bf215546Sopenharmony_ci      case NIR_INTRINSIC_IO_XFB:
1095bf215546Sopenharmony_ci      case NIR_INTRINSIC_IO_XFB2: {
1096bf215546Sopenharmony_ci         /* This prints both IO_XFB and IO_XFB2. */
1097bf215546Sopenharmony_ci         fprintf(fp, "xfb%s(", idx == NIR_INTRINSIC_IO_XFB ? "" : "2");
1098bf215546Sopenharmony_ci         bool first = true;
1099bf215546Sopenharmony_ci         for (unsigned i = 0; i < 2; i++) {
1100bf215546Sopenharmony_ci            unsigned start_comp = (idx == NIR_INTRINSIC_IO_XFB ? 0 : 2) + i;
1101bf215546Sopenharmony_ci            nir_io_xfb xfb = start_comp < 2 ? nir_intrinsic_io_xfb(instr) :
1102bf215546Sopenharmony_ci                                              nir_intrinsic_io_xfb2(instr);
1103bf215546Sopenharmony_ci
1104bf215546Sopenharmony_ci            if (!xfb.out[i].num_components)
1105bf215546Sopenharmony_ci               continue;
1106bf215546Sopenharmony_ci
1107bf215546Sopenharmony_ci            if (!first)
1108bf215546Sopenharmony_ci               fprintf(fp, ", ");
1109bf215546Sopenharmony_ci            first = false;
1110bf215546Sopenharmony_ci
1111bf215546Sopenharmony_ci            if (xfb.out[i].num_components > 1) {
1112bf215546Sopenharmony_ci               fprintf(fp, "components=%u..%u",
1113bf215546Sopenharmony_ci                       start_comp, start_comp + xfb.out[i].num_components - 1);
1114bf215546Sopenharmony_ci            } else {
1115bf215546Sopenharmony_ci               fprintf(fp, "component=%u", start_comp);
1116bf215546Sopenharmony_ci            }
1117bf215546Sopenharmony_ci            fprintf(fp, " buffer=%u offset=%u",
1118bf215546Sopenharmony_ci                    xfb.out[i].buffer, (uint32_t)xfb.out[i].offset * 4);
1119bf215546Sopenharmony_ci         }
1120bf215546Sopenharmony_ci         fprintf(fp, ")");
1121bf215546Sopenharmony_ci         break;
1122bf215546Sopenharmony_ci      }
1123bf215546Sopenharmony_ci
1124bf215546Sopenharmony_ci      case NIR_INTRINSIC_ROUNDING_MODE: {
1125bf215546Sopenharmony_ci         fprintf(fp, "rounding_mode=");
1126bf215546Sopenharmony_ci         switch (nir_intrinsic_rounding_mode(instr)) {
1127bf215546Sopenharmony_ci         case nir_rounding_mode_undef: fprintf(fp, "undef");   break;
1128bf215546Sopenharmony_ci         case nir_rounding_mode_rtne:  fprintf(fp, "rtne");    break;
1129bf215546Sopenharmony_ci         case nir_rounding_mode_ru:    fprintf(fp, "ru");      break;
1130bf215546Sopenharmony_ci         case nir_rounding_mode_rd:    fprintf(fp, "rd");      break;
1131bf215546Sopenharmony_ci         case nir_rounding_mode_rtz:   fprintf(fp, "rtz");     break;
1132bf215546Sopenharmony_ci         default:                      fprintf(fp, "unkown");  break;
1133bf215546Sopenharmony_ci         }
1134bf215546Sopenharmony_ci         break;
1135bf215546Sopenharmony_ci      }
1136bf215546Sopenharmony_ci
1137bf215546Sopenharmony_ci      default: {
1138bf215546Sopenharmony_ci         unsigned off = info->index_map[idx] - 1;
1139bf215546Sopenharmony_ci         fprintf(fp, "%s=%d", nir_intrinsic_index_names[idx], instr->const_index[off]);
1140bf215546Sopenharmony_ci         print_raw = false;
1141bf215546Sopenharmony_ci         break;
1142bf215546Sopenharmony_ci      }
1143bf215546Sopenharmony_ci      }
1144bf215546Sopenharmony_ci      if (print_raw)
1145bf215546Sopenharmony_ci         fprintf(fp, " /*%d*/", instr->const_index[i]);
1146bf215546Sopenharmony_ci   }
1147bf215546Sopenharmony_ci   fprintf(fp, ")");
1148bf215546Sopenharmony_ci
1149bf215546Sopenharmony_ci   if (!state->shader)
1150bf215546Sopenharmony_ci      return;
1151bf215546Sopenharmony_ci
1152bf215546Sopenharmony_ci   nir_variable_mode var_mode;
1153bf215546Sopenharmony_ci   switch (instr->intrinsic) {
1154bf215546Sopenharmony_ci   case nir_intrinsic_load_uniform:
1155bf215546Sopenharmony_ci      var_mode = nir_var_uniform;
1156bf215546Sopenharmony_ci      break;
1157bf215546Sopenharmony_ci   case nir_intrinsic_load_input:
1158bf215546Sopenharmony_ci   case nir_intrinsic_load_interpolated_input:
1159bf215546Sopenharmony_ci   case nir_intrinsic_load_per_vertex_input:
1160bf215546Sopenharmony_ci      var_mode = nir_var_shader_in;
1161bf215546Sopenharmony_ci      break;
1162bf215546Sopenharmony_ci   case nir_intrinsic_load_output:
1163bf215546Sopenharmony_ci   case nir_intrinsic_store_output:
1164bf215546Sopenharmony_ci   case nir_intrinsic_store_per_vertex_output:
1165bf215546Sopenharmony_ci      var_mode = nir_var_shader_out;
1166bf215546Sopenharmony_ci      break;
1167bf215546Sopenharmony_ci   default:
1168bf215546Sopenharmony_ci      return;
1169bf215546Sopenharmony_ci   }
1170bf215546Sopenharmony_ci
1171bf215546Sopenharmony_ci   nir_foreach_variable_with_modes(var, state->shader, var_mode) {
1172bf215546Sopenharmony_ci      if ((var->data.driver_location == nir_intrinsic_base(instr)) &&
1173bf215546Sopenharmony_ci          (instr->intrinsic == nir_intrinsic_load_uniform ||
1174bf215546Sopenharmony_ci           (nir_intrinsic_component(instr) >= var->data.location_frac  &&
1175bf215546Sopenharmony_ci            nir_intrinsic_component(instr) <
1176bf215546Sopenharmony_ci            (var->data.location_frac + glsl_get_components(var->type)))) &&
1177bf215546Sopenharmony_ci           var->name) {
1178bf215546Sopenharmony_ci         fprintf(fp, "\t/* %s */", var->name);
1179bf215546Sopenharmony_ci         break;
1180bf215546Sopenharmony_ci      }
1181bf215546Sopenharmony_ci   }
1182bf215546Sopenharmony_ci}
1183bf215546Sopenharmony_ci
1184bf215546Sopenharmony_cistatic void
1185bf215546Sopenharmony_ciprint_tex_instr(nir_tex_instr *instr, print_state *state)
1186bf215546Sopenharmony_ci{
1187bf215546Sopenharmony_ci   FILE *fp = state->fp;
1188bf215546Sopenharmony_ci
1189bf215546Sopenharmony_ci   print_dest(&instr->dest, state);
1190bf215546Sopenharmony_ci
1191bf215546Sopenharmony_ci   fprintf(fp, " = (");
1192bf215546Sopenharmony_ci   print_alu_type(instr->dest_type, state);
1193bf215546Sopenharmony_ci   fprintf(fp, ")");
1194bf215546Sopenharmony_ci
1195bf215546Sopenharmony_ci   switch (instr->op) {
1196bf215546Sopenharmony_ci   case nir_texop_tex:
1197bf215546Sopenharmony_ci      fprintf(fp, "tex ");
1198bf215546Sopenharmony_ci      break;
1199bf215546Sopenharmony_ci   case nir_texop_txb:
1200bf215546Sopenharmony_ci      fprintf(fp, "txb ");
1201bf215546Sopenharmony_ci      break;
1202bf215546Sopenharmony_ci   case nir_texop_txl:
1203bf215546Sopenharmony_ci      fprintf(fp, "txl ");
1204bf215546Sopenharmony_ci      break;
1205bf215546Sopenharmony_ci   case nir_texop_txd:
1206bf215546Sopenharmony_ci      fprintf(fp, "txd ");
1207bf215546Sopenharmony_ci      break;
1208bf215546Sopenharmony_ci   case nir_texop_txf:
1209bf215546Sopenharmony_ci      fprintf(fp, "txf ");
1210bf215546Sopenharmony_ci      break;
1211bf215546Sopenharmony_ci   case nir_texop_txf_ms:
1212bf215546Sopenharmony_ci      fprintf(fp, "txf_ms ");
1213bf215546Sopenharmony_ci      break;
1214bf215546Sopenharmony_ci   case nir_texop_txf_ms_fb:
1215bf215546Sopenharmony_ci      fprintf(fp, "txf_ms_fb ");
1216bf215546Sopenharmony_ci      break;
1217bf215546Sopenharmony_ci   case nir_texop_txf_ms_mcs_intel:
1218bf215546Sopenharmony_ci      fprintf(fp, "txf_ms_mcs_intel ");
1219bf215546Sopenharmony_ci      break;
1220bf215546Sopenharmony_ci   case nir_texop_txs:
1221bf215546Sopenharmony_ci      fprintf(fp, "txs ");
1222bf215546Sopenharmony_ci      break;
1223bf215546Sopenharmony_ci   case nir_texop_lod:
1224bf215546Sopenharmony_ci      fprintf(fp, "lod ");
1225bf215546Sopenharmony_ci      break;
1226bf215546Sopenharmony_ci   case nir_texop_tg4:
1227bf215546Sopenharmony_ci      fprintf(fp, "tg4 ");
1228bf215546Sopenharmony_ci      break;
1229bf215546Sopenharmony_ci   case nir_texop_query_levels:
1230bf215546Sopenharmony_ci      fprintf(fp, "query_levels ");
1231bf215546Sopenharmony_ci      break;
1232bf215546Sopenharmony_ci   case nir_texop_texture_samples:
1233bf215546Sopenharmony_ci      fprintf(fp, "texture_samples ");
1234bf215546Sopenharmony_ci      break;
1235bf215546Sopenharmony_ci   case nir_texop_samples_identical:
1236bf215546Sopenharmony_ci      fprintf(fp, "samples_identical ");
1237bf215546Sopenharmony_ci      break;
1238bf215546Sopenharmony_ci   case nir_texop_tex_prefetch:
1239bf215546Sopenharmony_ci      fprintf(fp, "tex (pre-dispatchable) ");
1240bf215546Sopenharmony_ci      break;
1241bf215546Sopenharmony_ci   case nir_texop_fragment_fetch_amd:
1242bf215546Sopenharmony_ci      fprintf(fp, "fragment_fetch_amd ");
1243bf215546Sopenharmony_ci      break;
1244bf215546Sopenharmony_ci   case nir_texop_fragment_mask_fetch_amd:
1245bf215546Sopenharmony_ci      fprintf(fp, "fragment_mask_fetch_amd ");
1246bf215546Sopenharmony_ci      break;
1247bf215546Sopenharmony_ci   default:
1248bf215546Sopenharmony_ci      unreachable("Invalid texture operation");
1249bf215546Sopenharmony_ci      break;
1250bf215546Sopenharmony_ci   }
1251bf215546Sopenharmony_ci
1252bf215546Sopenharmony_ci   bool has_texture_deref = false, has_sampler_deref = false;
1253bf215546Sopenharmony_ci   for (unsigned i = 0; i < instr->num_srcs; i++) {
1254bf215546Sopenharmony_ci      if (i > 0) {
1255bf215546Sopenharmony_ci         fprintf(fp, ", ");
1256bf215546Sopenharmony_ci      }
1257bf215546Sopenharmony_ci
1258bf215546Sopenharmony_ci      print_src(&instr->src[i].src, state);
1259bf215546Sopenharmony_ci      fprintf(fp, " ");
1260bf215546Sopenharmony_ci
1261bf215546Sopenharmony_ci      switch(instr->src[i].src_type) {
1262bf215546Sopenharmony_ci      case nir_tex_src_backend1:
1263bf215546Sopenharmony_ci         fprintf(fp, "(backend1)");
1264bf215546Sopenharmony_ci         break;
1265bf215546Sopenharmony_ci      case nir_tex_src_backend2:
1266bf215546Sopenharmony_ci         fprintf(fp, "(backend2)");
1267bf215546Sopenharmony_ci         break;
1268bf215546Sopenharmony_ci      case nir_tex_src_coord:
1269bf215546Sopenharmony_ci         fprintf(fp, "(coord)");
1270bf215546Sopenharmony_ci         break;
1271bf215546Sopenharmony_ci      case nir_tex_src_projector:
1272bf215546Sopenharmony_ci         fprintf(fp, "(projector)");
1273bf215546Sopenharmony_ci         break;
1274bf215546Sopenharmony_ci      case nir_tex_src_comparator:
1275bf215546Sopenharmony_ci         fprintf(fp, "(comparator)");
1276bf215546Sopenharmony_ci         break;
1277bf215546Sopenharmony_ci      case nir_tex_src_offset:
1278bf215546Sopenharmony_ci         fprintf(fp, "(offset)");
1279bf215546Sopenharmony_ci         break;
1280bf215546Sopenharmony_ci      case nir_tex_src_bias:
1281bf215546Sopenharmony_ci         fprintf(fp, "(bias)");
1282bf215546Sopenharmony_ci         break;
1283bf215546Sopenharmony_ci      case nir_tex_src_lod:
1284bf215546Sopenharmony_ci         fprintf(fp, "(lod)");
1285bf215546Sopenharmony_ci         break;
1286bf215546Sopenharmony_ci      case nir_tex_src_min_lod:
1287bf215546Sopenharmony_ci         fprintf(fp, "(min_lod)");
1288bf215546Sopenharmony_ci         break;
1289bf215546Sopenharmony_ci      case nir_tex_src_ms_index:
1290bf215546Sopenharmony_ci         fprintf(fp, "(ms_index)");
1291bf215546Sopenharmony_ci         break;
1292bf215546Sopenharmony_ci      case nir_tex_src_ms_mcs_intel:
1293bf215546Sopenharmony_ci         fprintf(fp, "(ms_mcs_intel)");
1294bf215546Sopenharmony_ci         break;
1295bf215546Sopenharmony_ci      case nir_tex_src_ddx:
1296bf215546Sopenharmony_ci         fprintf(fp, "(ddx)");
1297bf215546Sopenharmony_ci         break;
1298bf215546Sopenharmony_ci      case nir_tex_src_ddy:
1299bf215546Sopenharmony_ci         fprintf(fp, "(ddy)");
1300bf215546Sopenharmony_ci         break;
1301bf215546Sopenharmony_ci      case nir_tex_src_texture_deref:
1302bf215546Sopenharmony_ci         has_texture_deref = true;
1303bf215546Sopenharmony_ci         fprintf(fp, "(texture_deref)");
1304bf215546Sopenharmony_ci         break;
1305bf215546Sopenharmony_ci      case nir_tex_src_sampler_deref:
1306bf215546Sopenharmony_ci         has_sampler_deref = true;
1307bf215546Sopenharmony_ci         fprintf(fp, "(sampler_deref)");
1308bf215546Sopenharmony_ci         break;
1309bf215546Sopenharmony_ci      case nir_tex_src_texture_offset:
1310bf215546Sopenharmony_ci         fprintf(fp, "(texture_offset)");
1311bf215546Sopenharmony_ci         break;
1312bf215546Sopenharmony_ci      case nir_tex_src_sampler_offset:
1313bf215546Sopenharmony_ci         fprintf(fp, "(sampler_offset)");
1314bf215546Sopenharmony_ci         break;
1315bf215546Sopenharmony_ci      case nir_tex_src_texture_handle:
1316bf215546Sopenharmony_ci         fprintf(fp, "(texture_handle)");
1317bf215546Sopenharmony_ci         break;
1318bf215546Sopenharmony_ci      case nir_tex_src_sampler_handle:
1319bf215546Sopenharmony_ci         fprintf(fp, "(sampler_handle)");
1320bf215546Sopenharmony_ci         break;
1321bf215546Sopenharmony_ci      case nir_tex_src_plane:
1322bf215546Sopenharmony_ci         fprintf(fp, "(plane)");
1323bf215546Sopenharmony_ci         break;
1324bf215546Sopenharmony_ci
1325bf215546Sopenharmony_ci      default:
1326bf215546Sopenharmony_ci         unreachable("Invalid texture source type");
1327bf215546Sopenharmony_ci         break;
1328bf215546Sopenharmony_ci      }
1329bf215546Sopenharmony_ci   }
1330bf215546Sopenharmony_ci
1331bf215546Sopenharmony_ci   if (instr->op == nir_texop_tg4) {
1332bf215546Sopenharmony_ci      fprintf(fp, ", %u (gather_component)", instr->component);
1333bf215546Sopenharmony_ci   }
1334bf215546Sopenharmony_ci
1335bf215546Sopenharmony_ci   if (nir_tex_instr_has_explicit_tg4_offsets(instr)) {
1336bf215546Sopenharmony_ci      fprintf(fp, ", { (%i, %i)", instr->tg4_offsets[0][0], instr->tg4_offsets[0][1]);
1337bf215546Sopenharmony_ci      for (unsigned i = 1; i < 4; ++i)
1338bf215546Sopenharmony_ci         fprintf(fp, ", (%i, %i)", instr->tg4_offsets[i][0],
1339bf215546Sopenharmony_ci                 instr->tg4_offsets[i][1]);
1340bf215546Sopenharmony_ci      fprintf(fp, " } (offsets)");
1341bf215546Sopenharmony_ci   }
1342bf215546Sopenharmony_ci
1343bf215546Sopenharmony_ci   if (instr->op != nir_texop_txf_ms_fb) {
1344bf215546Sopenharmony_ci      if (!has_texture_deref) {
1345bf215546Sopenharmony_ci         fprintf(fp, ", %u (texture)", instr->texture_index);
1346bf215546Sopenharmony_ci      }
1347bf215546Sopenharmony_ci
1348bf215546Sopenharmony_ci      if (!has_sampler_deref) {
1349bf215546Sopenharmony_ci         fprintf(fp, ", %u (sampler)", instr->sampler_index);
1350bf215546Sopenharmony_ci      }
1351bf215546Sopenharmony_ci   }
1352bf215546Sopenharmony_ci
1353bf215546Sopenharmony_ci   if (instr->texture_non_uniform) {
1354bf215546Sopenharmony_ci      fprintf(fp, ", texture non-uniform");
1355bf215546Sopenharmony_ci   }
1356bf215546Sopenharmony_ci
1357bf215546Sopenharmony_ci   if (instr->sampler_non_uniform) {
1358bf215546Sopenharmony_ci      fprintf(fp, ", sampler non-uniform");
1359bf215546Sopenharmony_ci   }
1360bf215546Sopenharmony_ci
1361bf215546Sopenharmony_ci   if (instr->is_sparse) {
1362bf215546Sopenharmony_ci      fprintf(fp, ", sparse");
1363bf215546Sopenharmony_ci   }
1364bf215546Sopenharmony_ci}
1365bf215546Sopenharmony_ci
1366bf215546Sopenharmony_cistatic void
1367bf215546Sopenharmony_ciprint_call_instr(nir_call_instr *instr, print_state *state)
1368bf215546Sopenharmony_ci{
1369bf215546Sopenharmony_ci   FILE *fp = state->fp;
1370bf215546Sopenharmony_ci
1371bf215546Sopenharmony_ci   fprintf(fp, "call %s ", instr->callee->name);
1372bf215546Sopenharmony_ci
1373bf215546Sopenharmony_ci   for (unsigned i = 0; i < instr->num_params; i++) {
1374bf215546Sopenharmony_ci      if (i != 0)
1375bf215546Sopenharmony_ci         fprintf(fp, ", ");
1376bf215546Sopenharmony_ci
1377bf215546Sopenharmony_ci      print_src(&instr->params[i], state);
1378bf215546Sopenharmony_ci   }
1379bf215546Sopenharmony_ci}
1380bf215546Sopenharmony_ci
1381bf215546Sopenharmony_cistatic void
1382bf215546Sopenharmony_ciprint_jump_instr(nir_jump_instr *instr, print_state *state)
1383bf215546Sopenharmony_ci{
1384bf215546Sopenharmony_ci   FILE *fp = state->fp;
1385bf215546Sopenharmony_ci
1386bf215546Sopenharmony_ci   switch (instr->type) {
1387bf215546Sopenharmony_ci   case nir_jump_break:
1388bf215546Sopenharmony_ci      fprintf(fp, "break");
1389bf215546Sopenharmony_ci      break;
1390bf215546Sopenharmony_ci
1391bf215546Sopenharmony_ci   case nir_jump_continue:
1392bf215546Sopenharmony_ci      fprintf(fp, "continue");
1393bf215546Sopenharmony_ci      break;
1394bf215546Sopenharmony_ci
1395bf215546Sopenharmony_ci   case nir_jump_return:
1396bf215546Sopenharmony_ci      fprintf(fp, "return");
1397bf215546Sopenharmony_ci      break;
1398bf215546Sopenharmony_ci
1399bf215546Sopenharmony_ci   case nir_jump_halt:
1400bf215546Sopenharmony_ci      fprintf(fp, "halt");
1401bf215546Sopenharmony_ci      break;
1402bf215546Sopenharmony_ci
1403bf215546Sopenharmony_ci   case nir_jump_goto:
1404bf215546Sopenharmony_ci      fprintf(fp, "goto block_%u",
1405bf215546Sopenharmony_ci              instr->target ? instr->target->index : -1);
1406bf215546Sopenharmony_ci      break;
1407bf215546Sopenharmony_ci
1408bf215546Sopenharmony_ci   case nir_jump_goto_if:
1409bf215546Sopenharmony_ci      fprintf(fp, "goto block_%u if ",
1410bf215546Sopenharmony_ci              instr->target ? instr->target->index : -1);
1411bf215546Sopenharmony_ci      print_src(&instr->condition, state);
1412bf215546Sopenharmony_ci      fprintf(fp, " else block_%u",
1413bf215546Sopenharmony_ci              instr->else_target ? instr->else_target->index : -1);
1414bf215546Sopenharmony_ci      break;
1415bf215546Sopenharmony_ci   }
1416bf215546Sopenharmony_ci}
1417bf215546Sopenharmony_ci
1418bf215546Sopenharmony_cistatic void
1419bf215546Sopenharmony_ciprint_ssa_undef_instr(nir_ssa_undef_instr* instr, print_state *state)
1420bf215546Sopenharmony_ci{
1421bf215546Sopenharmony_ci   FILE *fp = state->fp;
1422bf215546Sopenharmony_ci   print_ssa_def(&instr->def, state);
1423bf215546Sopenharmony_ci   fprintf(fp, " = undefined");
1424bf215546Sopenharmony_ci}
1425bf215546Sopenharmony_ci
1426bf215546Sopenharmony_cistatic void
1427bf215546Sopenharmony_ciprint_phi_instr(nir_phi_instr *instr, print_state *state)
1428bf215546Sopenharmony_ci{
1429bf215546Sopenharmony_ci   FILE *fp = state->fp;
1430bf215546Sopenharmony_ci   print_dest(&instr->dest, state);
1431bf215546Sopenharmony_ci   fprintf(fp, " = phi ");
1432bf215546Sopenharmony_ci   nir_foreach_phi_src(src, instr) {
1433bf215546Sopenharmony_ci      if (&src->node != exec_list_get_head(&instr->srcs))
1434bf215546Sopenharmony_ci         fprintf(fp, ", ");
1435bf215546Sopenharmony_ci
1436bf215546Sopenharmony_ci      fprintf(fp, "block_%u: ", src->pred->index);
1437bf215546Sopenharmony_ci      print_src(&src->src, state);
1438bf215546Sopenharmony_ci   }
1439bf215546Sopenharmony_ci}
1440bf215546Sopenharmony_ci
1441bf215546Sopenharmony_cistatic void
1442bf215546Sopenharmony_ciprint_parallel_copy_instr(nir_parallel_copy_instr *instr, print_state *state)
1443bf215546Sopenharmony_ci{
1444bf215546Sopenharmony_ci   FILE *fp = state->fp;
1445bf215546Sopenharmony_ci   nir_foreach_parallel_copy_entry(entry, instr) {
1446bf215546Sopenharmony_ci      if (&entry->node != exec_list_get_head(&instr->entries))
1447bf215546Sopenharmony_ci         fprintf(fp, "; ");
1448bf215546Sopenharmony_ci
1449bf215546Sopenharmony_ci      print_dest(&entry->dest, state);
1450bf215546Sopenharmony_ci      fprintf(fp, " = ");
1451bf215546Sopenharmony_ci      print_src(&entry->src, state);
1452bf215546Sopenharmony_ci   }
1453bf215546Sopenharmony_ci}
1454bf215546Sopenharmony_ci
1455bf215546Sopenharmony_cistatic void
1456bf215546Sopenharmony_ciprint_instr(const nir_instr *instr, print_state *state, unsigned tabs)
1457bf215546Sopenharmony_ci{
1458bf215546Sopenharmony_ci   FILE *fp = state->fp;
1459bf215546Sopenharmony_ci   print_tabs(tabs, fp);
1460bf215546Sopenharmony_ci
1461bf215546Sopenharmony_ci   switch (instr->type) {
1462bf215546Sopenharmony_ci   case nir_instr_type_alu:
1463bf215546Sopenharmony_ci      print_alu_instr(nir_instr_as_alu(instr), state);
1464bf215546Sopenharmony_ci      break;
1465bf215546Sopenharmony_ci
1466bf215546Sopenharmony_ci   case nir_instr_type_deref:
1467bf215546Sopenharmony_ci      print_deref_instr(nir_instr_as_deref(instr), state);
1468bf215546Sopenharmony_ci      break;
1469bf215546Sopenharmony_ci
1470bf215546Sopenharmony_ci   case nir_instr_type_call:
1471bf215546Sopenharmony_ci      print_call_instr(nir_instr_as_call(instr), state);
1472bf215546Sopenharmony_ci      break;
1473bf215546Sopenharmony_ci
1474bf215546Sopenharmony_ci   case nir_instr_type_intrinsic:
1475bf215546Sopenharmony_ci      print_intrinsic_instr(nir_instr_as_intrinsic(instr), state);
1476bf215546Sopenharmony_ci      break;
1477bf215546Sopenharmony_ci
1478bf215546Sopenharmony_ci   case nir_instr_type_tex:
1479bf215546Sopenharmony_ci      print_tex_instr(nir_instr_as_tex(instr), state);
1480bf215546Sopenharmony_ci      break;
1481bf215546Sopenharmony_ci
1482bf215546Sopenharmony_ci   case nir_instr_type_load_const:
1483bf215546Sopenharmony_ci      print_load_const_instr(nir_instr_as_load_const(instr), state);
1484bf215546Sopenharmony_ci      break;
1485bf215546Sopenharmony_ci
1486bf215546Sopenharmony_ci   case nir_instr_type_jump:
1487bf215546Sopenharmony_ci      print_jump_instr(nir_instr_as_jump(instr), state);
1488bf215546Sopenharmony_ci      break;
1489bf215546Sopenharmony_ci
1490bf215546Sopenharmony_ci   case nir_instr_type_ssa_undef:
1491bf215546Sopenharmony_ci      print_ssa_undef_instr(nir_instr_as_ssa_undef(instr), state);
1492bf215546Sopenharmony_ci      break;
1493bf215546Sopenharmony_ci
1494bf215546Sopenharmony_ci   case nir_instr_type_phi:
1495bf215546Sopenharmony_ci      print_phi_instr(nir_instr_as_phi(instr), state);
1496bf215546Sopenharmony_ci      break;
1497bf215546Sopenharmony_ci
1498bf215546Sopenharmony_ci   case nir_instr_type_parallel_copy:
1499bf215546Sopenharmony_ci      print_parallel_copy_instr(nir_instr_as_parallel_copy(instr), state);
1500bf215546Sopenharmony_ci      break;
1501bf215546Sopenharmony_ci
1502bf215546Sopenharmony_ci   default:
1503bf215546Sopenharmony_ci      unreachable("Invalid instruction type");
1504bf215546Sopenharmony_ci      break;
1505bf215546Sopenharmony_ci   }
1506bf215546Sopenharmony_ci}
1507bf215546Sopenharmony_ci
1508bf215546Sopenharmony_cistatic void print_cf_node(nir_cf_node *node, print_state *state,
1509bf215546Sopenharmony_ci                          unsigned tabs);
1510bf215546Sopenharmony_ci
1511bf215546Sopenharmony_cistatic void
1512bf215546Sopenharmony_ciprint_block(nir_block *block, print_state *state, unsigned tabs)
1513bf215546Sopenharmony_ci{
1514bf215546Sopenharmony_ci   FILE *fp = state->fp;
1515bf215546Sopenharmony_ci
1516bf215546Sopenharmony_ci   print_tabs(tabs, fp);
1517bf215546Sopenharmony_ci   fprintf(fp, "block block_%u:\n", block->index);
1518bf215546Sopenharmony_ci
1519bf215546Sopenharmony_ci   nir_block **preds = nir_block_get_predecessors_sorted(block, NULL);
1520bf215546Sopenharmony_ci
1521bf215546Sopenharmony_ci   print_tabs(tabs, fp);
1522bf215546Sopenharmony_ci   fprintf(fp, "/* preds: ");
1523bf215546Sopenharmony_ci   for (unsigned i = 0; i < block->predecessors->entries; i++) {
1524bf215546Sopenharmony_ci      fprintf(fp, "block_%u ", preds[i]->index);
1525bf215546Sopenharmony_ci   }
1526bf215546Sopenharmony_ci   fprintf(fp, "*/\n");
1527bf215546Sopenharmony_ci
1528bf215546Sopenharmony_ci   ralloc_free(preds);
1529bf215546Sopenharmony_ci
1530bf215546Sopenharmony_ci   nir_foreach_instr(instr, block) {
1531bf215546Sopenharmony_ci      print_instr(instr, state, tabs);
1532bf215546Sopenharmony_ci      fprintf(fp, "\n");
1533bf215546Sopenharmony_ci      print_annotation(state, instr);
1534bf215546Sopenharmony_ci   }
1535bf215546Sopenharmony_ci
1536bf215546Sopenharmony_ci   print_tabs(tabs, fp);
1537bf215546Sopenharmony_ci   fprintf(fp, "/* succs: ");
1538bf215546Sopenharmony_ci   for (unsigned i = 0; i < 2; i++)
1539bf215546Sopenharmony_ci      if (block->successors[i]) {
1540bf215546Sopenharmony_ci         fprintf(fp, "block_%u ", block->successors[i]->index);
1541bf215546Sopenharmony_ci      }
1542bf215546Sopenharmony_ci   fprintf(fp, "*/\n");
1543bf215546Sopenharmony_ci}
1544bf215546Sopenharmony_ci
1545bf215546Sopenharmony_cistatic void
1546bf215546Sopenharmony_ciprint_if(nir_if *if_stmt, print_state *state, unsigned tabs)
1547bf215546Sopenharmony_ci{
1548bf215546Sopenharmony_ci   FILE *fp = state->fp;
1549bf215546Sopenharmony_ci
1550bf215546Sopenharmony_ci   print_tabs(tabs, fp);
1551bf215546Sopenharmony_ci   fprintf(fp, "if ");
1552bf215546Sopenharmony_ci   print_src(&if_stmt->condition, state);
1553bf215546Sopenharmony_ci   fprintf(fp, " {\n");
1554bf215546Sopenharmony_ci   foreach_list_typed(nir_cf_node, node, node, &if_stmt->then_list) {
1555bf215546Sopenharmony_ci      print_cf_node(node, state, tabs + 1);
1556bf215546Sopenharmony_ci   }
1557bf215546Sopenharmony_ci   print_tabs(tabs, fp);
1558bf215546Sopenharmony_ci   fprintf(fp, "} else {\n");
1559bf215546Sopenharmony_ci   foreach_list_typed(nir_cf_node, node, node, &if_stmt->else_list) {
1560bf215546Sopenharmony_ci      print_cf_node(node, state, tabs + 1);
1561bf215546Sopenharmony_ci   }
1562bf215546Sopenharmony_ci   print_tabs(tabs, fp);
1563bf215546Sopenharmony_ci   fprintf(fp, "}\n");
1564bf215546Sopenharmony_ci}
1565bf215546Sopenharmony_ci
1566bf215546Sopenharmony_cistatic void
1567bf215546Sopenharmony_ciprint_loop(nir_loop *loop, print_state *state, unsigned tabs)
1568bf215546Sopenharmony_ci{
1569bf215546Sopenharmony_ci   FILE *fp = state->fp;
1570bf215546Sopenharmony_ci
1571bf215546Sopenharmony_ci   print_tabs(tabs, fp);
1572bf215546Sopenharmony_ci   fprintf(fp, "loop {\n");
1573bf215546Sopenharmony_ci   foreach_list_typed(nir_cf_node, node, node, &loop->body) {
1574bf215546Sopenharmony_ci      print_cf_node(node, state, tabs + 1);
1575bf215546Sopenharmony_ci   }
1576bf215546Sopenharmony_ci   print_tabs(tabs, fp);
1577bf215546Sopenharmony_ci   fprintf(fp, "}\n");
1578bf215546Sopenharmony_ci}
1579bf215546Sopenharmony_ci
1580bf215546Sopenharmony_cistatic void
1581bf215546Sopenharmony_ciprint_cf_node(nir_cf_node *node, print_state *state, unsigned int tabs)
1582bf215546Sopenharmony_ci{
1583bf215546Sopenharmony_ci   switch (node->type) {
1584bf215546Sopenharmony_ci   case nir_cf_node_block:
1585bf215546Sopenharmony_ci      print_block(nir_cf_node_as_block(node), state, tabs);
1586bf215546Sopenharmony_ci      break;
1587bf215546Sopenharmony_ci
1588bf215546Sopenharmony_ci   case nir_cf_node_if:
1589bf215546Sopenharmony_ci      print_if(nir_cf_node_as_if(node), state, tabs);
1590bf215546Sopenharmony_ci      break;
1591bf215546Sopenharmony_ci
1592bf215546Sopenharmony_ci   case nir_cf_node_loop:
1593bf215546Sopenharmony_ci      print_loop(nir_cf_node_as_loop(node), state, tabs);
1594bf215546Sopenharmony_ci      break;
1595bf215546Sopenharmony_ci
1596bf215546Sopenharmony_ci   default:
1597bf215546Sopenharmony_ci      unreachable("Invalid CFG node type");
1598bf215546Sopenharmony_ci   }
1599bf215546Sopenharmony_ci}
1600bf215546Sopenharmony_ci
1601bf215546Sopenharmony_cistatic void
1602bf215546Sopenharmony_ciprint_function_impl(nir_function_impl *impl, print_state *state)
1603bf215546Sopenharmony_ci{
1604bf215546Sopenharmony_ci   FILE *fp = state->fp;
1605bf215546Sopenharmony_ci
1606bf215546Sopenharmony_ci   fprintf(fp, "\nimpl %s ", impl->function->name);
1607bf215546Sopenharmony_ci
1608bf215546Sopenharmony_ci   fprintf(fp, "{\n");
1609bf215546Sopenharmony_ci
1610bf215546Sopenharmony_ci   if (impl->preamble) {
1611bf215546Sopenharmony_ci      fprintf(fp, "\tpreamble %s\n", impl->preamble->name);
1612bf215546Sopenharmony_ci   }
1613bf215546Sopenharmony_ci
1614bf215546Sopenharmony_ci   nir_foreach_function_temp_variable(var, impl) {
1615bf215546Sopenharmony_ci      fprintf(fp, "\t");
1616bf215546Sopenharmony_ci      print_var_decl(var, state);
1617bf215546Sopenharmony_ci   }
1618bf215546Sopenharmony_ci
1619bf215546Sopenharmony_ci   foreach_list_typed(nir_register, reg, node, &impl->registers) {
1620bf215546Sopenharmony_ci      fprintf(fp, "\t");
1621bf215546Sopenharmony_ci      print_register_decl(reg, state);
1622bf215546Sopenharmony_ci   }
1623bf215546Sopenharmony_ci
1624bf215546Sopenharmony_ci   nir_index_blocks(impl);
1625bf215546Sopenharmony_ci
1626bf215546Sopenharmony_ci   foreach_list_typed(nir_cf_node, node, node, &impl->body) {
1627bf215546Sopenharmony_ci      print_cf_node(node, state, 1);
1628bf215546Sopenharmony_ci   }
1629bf215546Sopenharmony_ci
1630bf215546Sopenharmony_ci   fprintf(fp, "\tblock block_%u:\n}\n\n", impl->end_block->index);
1631bf215546Sopenharmony_ci}
1632bf215546Sopenharmony_ci
1633bf215546Sopenharmony_cistatic void
1634bf215546Sopenharmony_ciprint_function(nir_function *function, print_state *state)
1635bf215546Sopenharmony_ci{
1636bf215546Sopenharmony_ci   FILE *fp = state->fp;
1637bf215546Sopenharmony_ci
1638bf215546Sopenharmony_ci   fprintf(fp, "decl_function %s (%d params)", function->name,
1639bf215546Sopenharmony_ci           function->num_params);
1640bf215546Sopenharmony_ci
1641bf215546Sopenharmony_ci   fprintf(fp, "\n");
1642bf215546Sopenharmony_ci
1643bf215546Sopenharmony_ci   if (function->impl != NULL) {
1644bf215546Sopenharmony_ci      print_function_impl(function->impl, state);
1645bf215546Sopenharmony_ci      return;
1646bf215546Sopenharmony_ci   }
1647bf215546Sopenharmony_ci}
1648bf215546Sopenharmony_ci
1649bf215546Sopenharmony_cistatic void
1650bf215546Sopenharmony_ciinit_print_state(print_state *state, nir_shader *shader, FILE *fp)
1651bf215546Sopenharmony_ci{
1652bf215546Sopenharmony_ci   state->fp = fp;
1653bf215546Sopenharmony_ci   state->shader = shader;
1654bf215546Sopenharmony_ci   state->ht = _mesa_pointer_hash_table_create(NULL);
1655bf215546Sopenharmony_ci   state->syms = _mesa_set_create(NULL, _mesa_hash_string,
1656bf215546Sopenharmony_ci                                  _mesa_key_string_equal);
1657bf215546Sopenharmony_ci   state->index = 0;
1658bf215546Sopenharmony_ci}
1659bf215546Sopenharmony_ci
1660bf215546Sopenharmony_cistatic void
1661bf215546Sopenharmony_cidestroy_print_state(print_state *state)
1662bf215546Sopenharmony_ci{
1663bf215546Sopenharmony_ci   _mesa_hash_table_destroy(state->ht, NULL);
1664bf215546Sopenharmony_ci   _mesa_set_destroy(state->syms, NULL);
1665bf215546Sopenharmony_ci}
1666bf215546Sopenharmony_ci
1667bf215546Sopenharmony_cistatic const char *
1668bf215546Sopenharmony_ciprimitive_name(unsigned primitive)
1669bf215546Sopenharmony_ci{
1670bf215546Sopenharmony_ci#define PRIM(X) case SHADER_PRIM_ ## X : return #X
1671bf215546Sopenharmony_ci   switch (primitive) {
1672bf215546Sopenharmony_ci   PRIM(POINTS);
1673bf215546Sopenharmony_ci   PRIM(LINES);
1674bf215546Sopenharmony_ci   PRIM(LINE_LOOP);
1675bf215546Sopenharmony_ci   PRIM(LINE_STRIP);
1676bf215546Sopenharmony_ci   PRIM(TRIANGLES);
1677bf215546Sopenharmony_ci   PRIM(TRIANGLE_STRIP);
1678bf215546Sopenharmony_ci   PRIM(TRIANGLE_FAN);
1679bf215546Sopenharmony_ci   PRIM(QUADS);
1680bf215546Sopenharmony_ci   PRIM(QUAD_STRIP);
1681bf215546Sopenharmony_ci   PRIM(POLYGON);
1682bf215546Sopenharmony_ci   default:
1683bf215546Sopenharmony_ci      return "UNKNOWN";
1684bf215546Sopenharmony_ci   }
1685bf215546Sopenharmony_ci}
1686bf215546Sopenharmony_ci
1687bf215546Sopenharmony_ci
1688bf215546Sopenharmony_civoid
1689bf215546Sopenharmony_cinir_print_shader_annotated(nir_shader *shader, FILE *fp,
1690bf215546Sopenharmony_ci                           struct hash_table *annotations)
1691bf215546Sopenharmony_ci{
1692bf215546Sopenharmony_ci   print_state state;
1693bf215546Sopenharmony_ci   init_print_state(&state, shader, fp);
1694bf215546Sopenharmony_ci
1695bf215546Sopenharmony_ci   state.annotations = annotations;
1696bf215546Sopenharmony_ci
1697bf215546Sopenharmony_ci   fprintf(fp, "shader: %s\n", gl_shader_stage_name(shader->info.stage));
1698bf215546Sopenharmony_ci
1699bf215546Sopenharmony_ci   fprintf(fp, "source_sha1: {");
1700bf215546Sopenharmony_ci   _mesa_sha1_print(fp, shader->info.source_sha1);
1701bf215546Sopenharmony_ci   fprintf(fp, "}\n");
1702bf215546Sopenharmony_ci
1703bf215546Sopenharmony_ci   if (shader->info.name)
1704bf215546Sopenharmony_ci      fprintf(fp, "name: %s\n", shader->info.name);
1705bf215546Sopenharmony_ci
1706bf215546Sopenharmony_ci   if (shader->info.label)
1707bf215546Sopenharmony_ci      fprintf(fp, "label: %s\n", shader->info.label);
1708bf215546Sopenharmony_ci
1709bf215546Sopenharmony_ci   if (gl_shader_stage_uses_workgroup(shader->info.stage)) {
1710bf215546Sopenharmony_ci      fprintf(fp, "workgroup-size: %u, %u, %u%s\n",
1711bf215546Sopenharmony_ci              shader->info.workgroup_size[0],
1712bf215546Sopenharmony_ci              shader->info.workgroup_size[1],
1713bf215546Sopenharmony_ci              shader->info.workgroup_size[2],
1714bf215546Sopenharmony_ci              shader->info.workgroup_size_variable ? " (variable)" : "");
1715bf215546Sopenharmony_ci      fprintf(fp, "shared-size: %u\n", shader->info.shared_size);
1716bf215546Sopenharmony_ci   }
1717bf215546Sopenharmony_ci   if (shader->info.stage == MESA_SHADER_MESH ||
1718bf215546Sopenharmony_ci       shader->info.stage == MESA_SHADER_TASK) {
1719bf215546Sopenharmony_ci      fprintf(fp, "task_payload-size: %u\n", shader->info.task_payload_size);
1720bf215546Sopenharmony_ci   }
1721bf215546Sopenharmony_ci
1722bf215546Sopenharmony_ci   fprintf(fp, "inputs: %u\n", shader->num_inputs);
1723bf215546Sopenharmony_ci   fprintf(fp, "outputs: %u\n", shader->num_outputs);
1724bf215546Sopenharmony_ci   fprintf(fp, "uniforms: %u\n", shader->num_uniforms);
1725bf215546Sopenharmony_ci   if (shader->info.num_ubos)
1726bf215546Sopenharmony_ci      fprintf(fp, "ubos: %u\n", shader->info.num_ubos);
1727bf215546Sopenharmony_ci   fprintf(fp, "shared: %u\n", shader->info.shared_size);
1728bf215546Sopenharmony_ci   fprintf(fp, "ray queries: %u\n", shader->info.ray_queries);
1729bf215546Sopenharmony_ci   if (shader->scratch_size)
1730bf215546Sopenharmony_ci      fprintf(fp, "scratch: %u\n", shader->scratch_size);
1731bf215546Sopenharmony_ci   if (shader->constant_data_size)
1732bf215546Sopenharmony_ci      fprintf(fp, "constants: %u\n", shader->constant_data_size);
1733bf215546Sopenharmony_ci
1734bf215546Sopenharmony_ci   if (shader->info.stage == MESA_SHADER_GEOMETRY) {
1735bf215546Sopenharmony_ci      fprintf(fp, "invocations: %u\n", shader->info.gs.invocations);
1736bf215546Sopenharmony_ci      fprintf(fp, "vertices in: %u\n", shader->info.gs.vertices_in);
1737bf215546Sopenharmony_ci      fprintf(fp, "vertices out: %u\n", shader->info.gs.vertices_out);
1738bf215546Sopenharmony_ci      fprintf(fp, "input primitive: %s\n", primitive_name(shader->info.gs.input_primitive));
1739bf215546Sopenharmony_ci      fprintf(fp, "output primitive: %s\n", primitive_name(shader->info.gs.output_primitive));
1740bf215546Sopenharmony_ci      fprintf(fp, "active_stream_mask: 0x%x\n", shader->info.gs.active_stream_mask);
1741bf215546Sopenharmony_ci      fprintf(fp, "uses_end_primitive: %u\n", shader->info.gs.uses_end_primitive);
1742bf215546Sopenharmony_ci   } else if (shader->info.stage == MESA_SHADER_MESH) {
1743bf215546Sopenharmony_ci      fprintf(fp, "output primitive: %s\n", primitive_name(shader->info.mesh.primitive_type));
1744bf215546Sopenharmony_ci      fprintf(fp, "max primitives out: %u\n", shader->info.mesh.max_primitives_out);
1745bf215546Sopenharmony_ci      fprintf(fp, "max vertices out: %u\n", shader->info.mesh.max_vertices_out);
1746bf215546Sopenharmony_ci   }
1747bf215546Sopenharmony_ci
1748bf215546Sopenharmony_ci   nir_foreach_variable_in_shader(var, shader)
1749bf215546Sopenharmony_ci      print_var_decl(var, &state);
1750bf215546Sopenharmony_ci
1751bf215546Sopenharmony_ci   foreach_list_typed(nir_function, func, node, &shader->functions) {
1752bf215546Sopenharmony_ci      print_function(func, &state);
1753bf215546Sopenharmony_ci   }
1754bf215546Sopenharmony_ci
1755bf215546Sopenharmony_ci   destroy_print_state(&state);
1756bf215546Sopenharmony_ci}
1757bf215546Sopenharmony_ci
1758bf215546Sopenharmony_civoid
1759bf215546Sopenharmony_cinir_print_shader(nir_shader *shader, FILE *fp)
1760bf215546Sopenharmony_ci{
1761bf215546Sopenharmony_ci   nir_print_shader_annotated(shader, fp, NULL);
1762bf215546Sopenharmony_ci   fflush(fp);
1763bf215546Sopenharmony_ci}
1764bf215546Sopenharmony_ci
1765bf215546Sopenharmony_cichar *
1766bf215546Sopenharmony_cinir_shader_as_str_annotated(nir_shader *nir, struct hash_table *annotations, void *mem_ctx)
1767bf215546Sopenharmony_ci{
1768bf215546Sopenharmony_ci   char *stream_data = NULL;
1769bf215546Sopenharmony_ci   size_t stream_size = 0;
1770bf215546Sopenharmony_ci   struct u_memstream mem;
1771bf215546Sopenharmony_ci   if (u_memstream_open(&mem, &stream_data, &stream_size)) {
1772bf215546Sopenharmony_ci      FILE *const stream = u_memstream_get(&mem);
1773bf215546Sopenharmony_ci      nir_print_shader_annotated(nir, stream, annotations);
1774bf215546Sopenharmony_ci      u_memstream_close(&mem);
1775bf215546Sopenharmony_ci   }
1776bf215546Sopenharmony_ci
1777bf215546Sopenharmony_ci   char *str = ralloc_size(mem_ctx, stream_size + 1);
1778bf215546Sopenharmony_ci   memcpy(str, stream_data, stream_size);
1779bf215546Sopenharmony_ci   str[stream_size] = '\0';
1780bf215546Sopenharmony_ci
1781bf215546Sopenharmony_ci   free(stream_data);
1782bf215546Sopenharmony_ci
1783bf215546Sopenharmony_ci   return str;
1784bf215546Sopenharmony_ci}
1785bf215546Sopenharmony_ci
1786bf215546Sopenharmony_cichar *
1787bf215546Sopenharmony_cinir_shader_as_str(nir_shader *nir, void *mem_ctx)
1788bf215546Sopenharmony_ci{
1789bf215546Sopenharmony_ci   return nir_shader_as_str_annotated(nir, NULL, mem_ctx);
1790bf215546Sopenharmony_ci}
1791bf215546Sopenharmony_ci
1792bf215546Sopenharmony_civoid
1793bf215546Sopenharmony_cinir_print_instr(const nir_instr *instr, FILE *fp)
1794bf215546Sopenharmony_ci{
1795bf215546Sopenharmony_ci   print_state state = {
1796bf215546Sopenharmony_ci      .fp = fp,
1797bf215546Sopenharmony_ci   };
1798bf215546Sopenharmony_ci   if (instr->block) {
1799bf215546Sopenharmony_ci      nir_function_impl *impl = nir_cf_node_get_function(&instr->block->cf_node);
1800bf215546Sopenharmony_ci      state.shader = impl->function->shader;
1801bf215546Sopenharmony_ci   }
1802bf215546Sopenharmony_ci
1803bf215546Sopenharmony_ci   print_instr(instr, &state, 0);
1804bf215546Sopenharmony_ci
1805bf215546Sopenharmony_ci}
1806bf215546Sopenharmony_ci
1807bf215546Sopenharmony_civoid
1808bf215546Sopenharmony_cinir_print_deref(const nir_deref_instr *deref, FILE *fp)
1809bf215546Sopenharmony_ci{
1810bf215546Sopenharmony_ci   print_state state = {
1811bf215546Sopenharmony_ci      .fp = fp,
1812bf215546Sopenharmony_ci   };
1813bf215546Sopenharmony_ci   print_deref_link(deref, true, &state);
1814bf215546Sopenharmony_ci}
1815bf215546Sopenharmony_ci
1816bf215546Sopenharmony_civoid nir_log_shader_annotated_tagged(enum mesa_log_level level, const char *tag,
1817bf215546Sopenharmony_ci                                     nir_shader *shader, struct hash_table *annotations)
1818bf215546Sopenharmony_ci{
1819bf215546Sopenharmony_ci   char *str = nir_shader_as_str_annotated(shader, annotations, NULL);
1820bf215546Sopenharmony_ci   _mesa_log_multiline(level, tag, str);
1821bf215546Sopenharmony_ci   ralloc_free(str);
1822bf215546Sopenharmony_ci}
1823