Home | History | Annotate | Download | only in nir
      1 /*
      2  * Copyright  2014 Intel Corporation
      3  *
      4  * Permission is hereby granted, free of charge, to any person obtaining a
      5  * copy of this software and associated documentation files (the "Software"),
      6  * to deal in the Software without restriction, including without limitation
      7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      8  * and/or sell copies of the Software, and to permit persons to whom the
      9  * Software is furnished to do so, subject to the following conditions:
     10  *
     11  * The above copyright notice and this permission notice (including the next
     12  * paragraph) shall be included in all copies or substantial portions of the
     13  * Software.
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     21  * IN THE SOFTWARE.
     22  *
     23  * Authors:
     24  *    Connor Abbott (cwabbott0 (at) gmail.com)
     25  *
     26  */
     27 
     28 #include "nir.h"
     29 #include "compiler/shader_enums.h"
     30 #include <stdio.h>
     31 #include <stdlib.h>
     32 #include <inttypes.h> /* for PRIx64 macro */
     33 
     34 #if defined(_WIN32) && !defined(snprintf)
     35 #define snprintf _snprintf
     36 #endif
     37 
     38 static void
     39 print_tabs(unsigned num_tabs, FILE *fp)
     40 {
     41    for (unsigned i = 0; i < num_tabs; i++)
     42       fprintf(fp, "\t");
     43 }
     44 
     45 typedef struct {
     46    FILE *fp;
     47    nir_shader *shader;
     48    /** map from nir_variable -> printable name */
     49    struct hash_table *ht;
     50 
     51    /** set of names used so far for nir_variables */
     52    struct set *syms;
     53 
     54    /* an index used to make new non-conflicting names */
     55    unsigned index;
     56 
     57    /**
     58     * Optional table of annotations mapping nir object
     59     * (such as instr or var) to message to print.
     60     */
     61    struct hash_table *annotations;
     62 } print_state;
     63 
     64 static void
     65 print_annotation(print_state *state, void *obj)
     66 {
     67    if (!state->annotations)
     68       return;
     69 
     70    struct hash_entry *entry = _mesa_hash_table_search(state->annotations, obj);
     71    if (!entry)
     72       return;
     73 
     74    const char *note = entry->data;
     75    _mesa_hash_table_remove(state->annotations, entry);
     76 
     77    fprintf(stderr, "%s\n\n", note);
     78 }
     79 
     80 static void
     81 print_register(nir_register *reg, print_state *state)
     82 {
     83    FILE *fp = state->fp;
     84    if (reg->name != NULL)
     85       fprintf(fp, "/* %s */ ", reg->name);
     86    if (reg->is_global)
     87       fprintf(fp, "gr%u", reg->index);
     88    else
     89       fprintf(fp, "r%u", reg->index);
     90 }
     91 
     92 static const char *sizes[] = { "error", "vec1", "vec2", "vec3", "vec4" };
     93 
     94 static void
     95 print_register_decl(nir_register *reg, print_state *state)
     96 {
     97    FILE *fp = state->fp;
     98    fprintf(fp, "decl_reg %s %u ", sizes[reg->num_components], reg->bit_size);
     99    if (reg->is_packed)
    100       fprintf(fp, "(packed) ");
    101    print_register(reg, state);
    102    if (reg->num_array_elems != 0)
    103       fprintf(fp, "[%u]", reg->num_array_elems);
    104    fprintf(fp, "\n");
    105 }
    106 
    107 static void
    108 print_ssa_def(nir_ssa_def *def, print_state *state)
    109 {
    110    FILE *fp = state->fp;
    111    if (def->name != NULL)
    112       fprintf(fp, "/* %s */ ", def->name);
    113    fprintf(fp, "%s %u ssa_%u", sizes[def->num_components], def->bit_size,
    114            def->index);
    115 }
    116 
    117 static void
    118 print_ssa_use(nir_ssa_def *def, print_state *state)
    119 {
    120    FILE *fp = state->fp;
    121    if (def->name != NULL)
    122       fprintf(fp, "/* %s */ ", def->name);
    123    fprintf(fp, "ssa_%u", def->index);
    124 }
    125 
    126 static void print_src(nir_src *src, print_state *state);
    127 
    128 static void
    129 print_reg_src(nir_reg_src *src, print_state *state)
    130 {
    131    FILE *fp = state->fp;
    132    print_register(src->reg, state);
    133    if (src->reg->num_array_elems != 0) {
    134       fprintf(fp, "[%u", src->base_offset);
    135       if (src->indirect != NULL) {
    136          fprintf(fp, " + ");
    137          print_src(src->indirect, state);
    138       }
    139       fprintf(fp, "]");
    140    }
    141 }
    142 
    143 static void
    144 print_reg_dest(nir_reg_dest *dest, print_state *state)
    145 {
    146    FILE *fp = state->fp;
    147    print_register(dest->reg, state);
    148    if (dest->reg->num_array_elems != 0) {
    149       fprintf(fp, "[%u", dest->base_offset);
    150       if (dest->indirect != NULL) {
    151          fprintf(fp, " + ");
    152          print_src(dest->indirect, state);
    153       }
    154       fprintf(fp, "]");
    155    }
    156 }
    157 
    158 static void
    159 print_src(nir_src *src, print_state *state)
    160 {
    161    if (src->is_ssa)
    162       print_ssa_use(src->ssa, state);
    163    else
    164       print_reg_src(&src->reg, state);
    165 }
    166 
    167 static void
    168 print_dest(nir_dest *dest, print_state *state)
    169 {
    170    if (dest->is_ssa)
    171       print_ssa_def(&dest->ssa, state);
    172    else
    173       print_reg_dest(&dest->reg, state);
    174 }
    175 
    176 static void
    177 print_alu_src(nir_alu_instr *instr, unsigned src, print_state *state)
    178 {
    179    FILE *fp = state->fp;
    180 
    181    if (instr->src[src].negate)
    182       fprintf(fp, "-");
    183    if (instr->src[src].abs)
    184       fprintf(fp, "abs(");
    185 
    186    print_src(&instr->src[src].src, state);
    187 
    188    bool print_swizzle = false;
    189    unsigned used_channels = 0;
    190 
    191    for (unsigned i = 0; i < 4; i++) {
    192       if (!nir_alu_instr_channel_used(instr, src, i))
    193          continue;
    194 
    195       used_channels++;
    196 
    197       if (instr->src[src].swizzle[i] != i) {
    198          print_swizzle = true;
    199          break;
    200       }
    201    }
    202 
    203    unsigned live_channels = instr->src[src].src.is_ssa
    204       ? instr->src[src].src.ssa->num_components
    205       : instr->src[src].src.reg.reg->num_components;
    206 
    207    if (print_swizzle || used_channels != live_channels) {
    208       fprintf(fp, ".");
    209       for (unsigned i = 0; i < 4; i++) {
    210          if (!nir_alu_instr_channel_used(instr, src, i))
    211             continue;
    212 
    213          fprintf(fp, "%c", "xyzw"[instr->src[src].swizzle[i]]);
    214       }
    215    }
    216 
    217    if (instr->src[src].abs)
    218       fprintf(fp, ")");
    219 }
    220 
    221 static void
    222 print_alu_dest(nir_alu_dest *dest, print_state *state)
    223 {
    224    FILE *fp = state->fp;
    225    /* we're going to print the saturate modifier later, after the opcode */
    226 
    227    print_dest(&dest->dest, state);
    228 
    229    if (!dest->dest.is_ssa &&
    230        dest->write_mask != (1 << dest->dest.reg.reg->num_components) - 1) {
    231       fprintf(fp, ".");
    232       for (unsigned i = 0; i < 4; i++)
    233          if ((dest->write_mask >> i) & 1)
    234             fprintf(fp, "%c", "xyzw"[i]);
    235    }
    236 }
    237 
    238 static void
    239 print_alu_instr(nir_alu_instr *instr, print_state *state)
    240 {
    241    FILE *fp = state->fp;
    242 
    243    print_alu_dest(&instr->dest, state);
    244 
    245    fprintf(fp, " = %s", nir_op_infos[instr->op].name);
    246    if (instr->exact)
    247       fprintf(fp, "!");
    248    if (instr->dest.saturate)
    249       fprintf(fp, ".sat");
    250    fprintf(fp, " ");
    251 
    252    for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
    253       if (i != 0)
    254          fprintf(fp, ", ");
    255 
    256       print_alu_src(instr, i, state);
    257    }
    258 }
    259 
    260 static const char *
    261 get_var_name(nir_variable *var, print_state *state)
    262 {
    263    if (state->ht == NULL)
    264       return var->name;
    265 
    266    assert(state->syms);
    267 
    268    struct hash_entry *entry = _mesa_hash_table_search(state->ht, var);
    269    if (entry)
    270       return entry->data;
    271 
    272    char *name;
    273    if (var->name == NULL) {
    274       name = ralloc_asprintf(state->syms, "@%u", state->index++);
    275    } else {
    276       struct set_entry *set_entry = _mesa_set_search(state->syms, var->name);
    277       if (set_entry != NULL) {
    278          /* we have a collision with another name, append an @ + a unique
    279           * index */
    280          name = ralloc_asprintf(state->syms, "%s@%u", var->name,
    281                                 state->index++);
    282       } else {
    283          /* Mark this one as seen */
    284          _mesa_set_add(state->syms, var->name);
    285          name = var->name;
    286       }
    287    }
    288 
    289    _mesa_hash_table_insert(state->ht, var, name);
    290 
    291    return name;
    292 }
    293 
    294 static void
    295 print_constant(nir_constant *c, const struct glsl_type *type, print_state *state)
    296 {
    297    FILE *fp = state->fp;
    298    const unsigned rows = glsl_get_vector_elements(type);
    299    const unsigned cols = glsl_get_matrix_columns(type);
    300    unsigned i, j;
    301 
    302    switch (glsl_get_base_type(type)) {
    303    case GLSL_TYPE_UINT:
    304    case GLSL_TYPE_INT:
    305    case GLSL_TYPE_BOOL:
    306       /* Only float base types can be matrices. */
    307       assert(cols == 1);
    308 
    309       for (i = 0; i < rows; i++) {
    310          if (i > 0) fprintf(fp, ", ");
    311          fprintf(fp, "0x%08x", c->values[0].u32[i]);
    312       }
    313       break;
    314 
    315    case GLSL_TYPE_FLOAT:
    316       for (i = 0; i < cols; i++) {
    317          for (j = 0; j < rows; j++) {
    318             if (i + j > 0) fprintf(fp, ", ");
    319             fprintf(fp, "%f", c->values[i].f32[j]);
    320          }
    321       }
    322       break;
    323 
    324    case GLSL_TYPE_DOUBLE:
    325       for (i = 0; i < cols; i++) {
    326          for (j = 0; j < rows; j++) {
    327             if (i + j > 0) fprintf(fp, ", ");
    328             fprintf(fp, "%f", c->values[i].f64[j]);
    329          }
    330       }
    331       break;
    332 
    333    case GLSL_TYPE_STRUCT:
    334       for (i = 0; i < c->num_elements; i++) {
    335          if (i > 0) fprintf(fp, ", ");
    336          fprintf(fp, "{ ");
    337          print_constant(c->elements[i], glsl_get_struct_field(type, i), state);
    338          fprintf(fp, " }");
    339       }
    340       break;
    341 
    342    case GLSL_TYPE_ARRAY:
    343       for (i = 0; i < c->num_elements; i++) {
    344          if (i > 0) fprintf(fp, ", ");
    345          fprintf(fp, "{ ");
    346          print_constant(c->elements[i], glsl_get_array_element(type), state);
    347          fprintf(fp, " }");
    348       }
    349       break;
    350 
    351    default:
    352       unreachable("not reached");
    353    }
    354 }
    355 
    356 static const char *
    357 get_variable_mode_str(nir_variable_mode mode)
    358 {
    359    switch (mode) {
    360    case nir_var_shader_in:
    361       return "shader_in";
    362    case nir_var_shader_out:
    363       return "shader_out";
    364    case nir_var_uniform:
    365       return "uniform";
    366    case nir_var_shader_storage:
    367       return "shader_storage";
    368    case nir_var_system_value:
    369       return "system";
    370    case nir_var_shared:
    371       return "shared";
    372    case nir_var_param:
    373    case nir_var_global:
    374    case nir_var_local:
    375    default:
    376       return "";
    377    }
    378 }
    379 
    380 static void
    381 print_var_decl(nir_variable *var, print_state *state)
    382 {
    383    FILE *fp = state->fp;
    384 
    385    fprintf(fp, "decl_var ");
    386 
    387    const char *const cent = (var->data.centroid) ? "centroid " : "";
    388    const char *const samp = (var->data.sample) ? "sample " : "";
    389    const char *const patch = (var->data.patch) ? "patch " : "";
    390    const char *const inv = (var->data.invariant) ? "invariant " : "";
    391    fprintf(fp, "%s%s%s%s%s %s ",
    392            cent, samp, patch, inv, get_variable_mode_str(var->data.mode),
    393            glsl_interp_mode_name(var->data.interpolation));
    394 
    395    const char *const coher = (var->data.image.coherent) ? "coherent " : "";
    396    const char *const volat = (var->data.image._volatile) ? "volatile " : "";
    397    const char *const restr = (var->data.image.restrict_flag) ? "restrict " : "";
    398    const char *const ronly = (var->data.image.read_only) ? "readonly " : "";
    399    const char *const wonly = (var->data.image.write_only) ? "writeonly " : "";
    400    fprintf(fp, "%s%s%s%s%s", coher, volat, restr, ronly, wonly);
    401 
    402    fprintf(fp, "%s %s", glsl_get_type_name(var->type),
    403            get_var_name(var, state));
    404 
    405    if (var->data.mode == nir_var_shader_in ||
    406        var->data.mode == nir_var_shader_out ||
    407        var->data.mode == nir_var_uniform ||
    408        var->data.mode == nir_var_shader_storage) {
    409       const char *loc = NULL;
    410       char buf[4];
    411 
    412       switch (state->shader->stage) {
    413       case MESA_SHADER_VERTEX:
    414          if (var->data.mode == nir_var_shader_in)
    415             loc = gl_vert_attrib_name(var->data.location);
    416          else if (var->data.mode == nir_var_shader_out)
    417             loc = gl_varying_slot_name(var->data.location);
    418          break;
    419       case MESA_SHADER_GEOMETRY:
    420          if ((var->data.mode == nir_var_shader_in) ||
    421              (var->data.mode == nir_var_shader_out))
    422             loc = gl_varying_slot_name(var->data.location);
    423          break;
    424       case MESA_SHADER_FRAGMENT:
    425          if (var->data.mode == nir_var_shader_in)
    426             loc = gl_varying_slot_name(var->data.location);
    427          else if (var->data.mode == nir_var_shader_out)
    428             loc = gl_frag_result_name(var->data.location);
    429          break;
    430       case MESA_SHADER_TESS_CTRL:
    431       case MESA_SHADER_TESS_EVAL:
    432       case MESA_SHADER_COMPUTE:
    433       default:
    434          /* TODO */
    435          break;
    436       }
    437 
    438       if (!loc) {
    439          snprintf(buf, sizeof(buf), "%u", var->data.location);
    440          loc = buf;
    441       }
    442 
    443       fprintf(fp, " (%s, %u, %u)%s", loc, var->data.driver_location, var->data.binding,
    444               var->data.compact ? " compact" : "");
    445    }
    446 
    447    if (var->constant_initializer) {
    448       fprintf(fp, " = { ");
    449       print_constant(var->constant_initializer, var->type, state);
    450       fprintf(fp, " }");
    451    }
    452 
    453    fprintf(fp, "\n");
    454    print_annotation(state, var);
    455 }
    456 
    457 static void
    458 print_var(nir_variable *var, print_state *state)
    459 {
    460    FILE *fp = state->fp;
    461    fprintf(fp, "%s", get_var_name(var, state));
    462 }
    463 
    464 static void
    465 print_arg(nir_variable *var, print_state *state)
    466 {
    467    FILE *fp = state->fp;
    468    fprintf(fp, "%s %s", glsl_get_type_name(var->type),
    469            get_var_name(var, state));
    470 }
    471 
    472 static void
    473 print_deref_var(nir_deref_var *deref, print_state *state)
    474 {
    475    print_var(deref->var, state);
    476 }
    477 
    478 static void
    479 print_deref_array(nir_deref_array *deref, print_state *state)
    480 {
    481    FILE *fp = state->fp;
    482    fprintf(fp, "[");
    483    switch (deref->deref_array_type) {
    484    case nir_deref_array_type_direct:
    485       fprintf(fp, "%u", deref->base_offset);
    486       break;
    487    case nir_deref_array_type_indirect:
    488       if (deref->base_offset != 0)
    489          fprintf(fp, "%u + ", deref->base_offset);
    490       print_src(&deref->indirect, state);
    491       break;
    492    case nir_deref_array_type_wildcard:
    493       fprintf(fp, "*");
    494       break;
    495    }
    496    fprintf(fp, "]");
    497 }
    498 
    499 static void
    500 print_deref_struct(nir_deref_struct *deref, const struct glsl_type *parent_type,
    501                    print_state *state)
    502 {
    503    FILE *fp = state->fp;
    504    fprintf(fp, ".%s", glsl_get_struct_elem_name(parent_type, deref->index));
    505 }
    506 
    507 static void
    508 print_deref(nir_deref_var *deref, print_state *state)
    509 {
    510    nir_deref *tail = &deref->deref;
    511    nir_deref *pretail = NULL;
    512    while (tail != NULL) {
    513       switch (tail->deref_type) {
    514       case nir_deref_type_var:
    515          assert(pretail == NULL);
    516          assert(tail == &deref->deref);
    517          print_deref_var(deref, state);
    518          break;
    519 
    520       case nir_deref_type_array:
    521          assert(pretail != NULL);
    522          print_deref_array(nir_deref_as_array(tail), state);
    523          break;
    524 
    525       case nir_deref_type_struct:
    526          assert(pretail != NULL);
    527          print_deref_struct(nir_deref_as_struct(tail),
    528                             pretail->type, state);
    529          break;
    530 
    531       default:
    532          unreachable("Invalid deref type");
    533       }
    534 
    535       pretail = tail;
    536       tail = pretail->child;
    537    }
    538 }
    539 
    540 static void
    541 print_intrinsic_instr(nir_intrinsic_instr *instr, print_state *state)
    542 {
    543    const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
    544    unsigned num_srcs = info->num_srcs;
    545    FILE *fp = state->fp;
    546 
    547    if (info->has_dest) {
    548       print_dest(&instr->dest, state);
    549       fprintf(fp, " = ");
    550    }
    551 
    552    fprintf(fp, "intrinsic %s (", info->name);
    553 
    554    for (unsigned i = 0; i < num_srcs; i++) {
    555       if (i != 0)
    556          fprintf(fp, ", ");
    557 
    558       print_src(&instr->src[i], state);
    559    }
    560 
    561    fprintf(fp, ") (");
    562 
    563    for (unsigned i = 0; i < info->num_variables; i++) {
    564       if (i != 0)
    565          fprintf(fp, ", ");
    566 
    567       print_deref(instr->variables[i], state);
    568    }
    569 
    570    fprintf(fp, ") (");
    571 
    572    for (unsigned i = 0; i < info->num_indices; i++) {
    573       if (i != 0)
    574          fprintf(fp, ", ");
    575 
    576       fprintf(fp, "%d", instr->const_index[i]);
    577    }
    578 
    579    fprintf(fp, ")");
    580 
    581    static const char *index_name[NIR_INTRINSIC_NUM_INDEX_FLAGS] = {
    582       [NIR_INTRINSIC_BASE] = "base",
    583       [NIR_INTRINSIC_WRMASK] = "wrmask",
    584       [NIR_INTRINSIC_STREAM_ID] = "stream-id",
    585       [NIR_INTRINSIC_UCP_ID] = "ucp-id",
    586       [NIR_INTRINSIC_RANGE] = "range",
    587       [NIR_INTRINSIC_DESC_SET] = "desc-set",
    588       [NIR_INTRINSIC_BINDING] = "binding",
    589       [NIR_INTRINSIC_COMPONENT] = "component",
    590       [NIR_INTRINSIC_INTERP_MODE] = "interp_mode",
    591    };
    592    for (unsigned idx = 1; idx < NIR_INTRINSIC_NUM_INDEX_FLAGS; idx++) {
    593       if (!info->index_map[idx])
    594          continue;
    595       fprintf(fp, " /*");
    596       if (idx == NIR_INTRINSIC_WRMASK) {
    597          /* special case wrmask to show it as a writemask.. */
    598          unsigned wrmask = nir_intrinsic_write_mask(instr);
    599          fprintf(fp, " wrmask=");
    600          for (unsigned i = 0; i < 4; i++)
    601             if ((wrmask >> i) & 1)
    602                fprintf(fp, "%c", "xyzw"[i]);
    603       } else {
    604          unsigned off = info->index_map[idx] - 1;
    605          assert(index_name[idx]);  /* forgot to update index_name table? */
    606          fprintf(fp, " %s=%d", index_name[idx], instr->const_index[off]);
    607       }
    608       fprintf(fp, " */");
    609    }
    610 
    611    if (!state->shader)
    612       return;
    613 
    614    struct exec_list *var_list = NULL;
    615 
    616    switch (instr->intrinsic) {
    617    case nir_intrinsic_load_uniform:
    618       var_list = &state->shader->uniforms;
    619       break;
    620    case nir_intrinsic_load_input:
    621    case nir_intrinsic_load_per_vertex_input:
    622       var_list = &state->shader->inputs;
    623       break;
    624    case nir_intrinsic_load_output:
    625    case nir_intrinsic_store_output:
    626    case nir_intrinsic_store_per_vertex_output:
    627       var_list = &state->shader->outputs;
    628       break;
    629    default:
    630       return;
    631    }
    632 
    633    nir_foreach_variable(var, var_list) {
    634       if ((var->data.driver_location == nir_intrinsic_base(instr)) &&
    635           (instr->intrinsic == nir_intrinsic_load_uniform ||
    636            var->data.location_frac == nir_intrinsic_component(instr)) &&
    637           var->name) {
    638          fprintf(fp, "\t/* %s */", var->name);
    639          break;
    640       }
    641    }
    642 }
    643 
    644 static void
    645 print_tex_instr(nir_tex_instr *instr, print_state *state)
    646 {
    647    FILE *fp = state->fp;
    648 
    649    print_dest(&instr->dest, state);
    650 
    651    fprintf(fp, " = ");
    652 
    653    switch (instr->op) {
    654    case nir_texop_tex:
    655       fprintf(fp, "tex ");
    656       break;
    657    case nir_texop_txb:
    658       fprintf(fp, "txb ");
    659       break;
    660    case nir_texop_txl:
    661       fprintf(fp, "txl ");
    662       break;
    663    case nir_texop_txd:
    664       fprintf(fp, "txd ");
    665       break;
    666    case nir_texop_txf:
    667       fprintf(fp, "txf ");
    668       break;
    669    case nir_texop_txf_ms:
    670       fprintf(fp, "txf_ms ");
    671       break;
    672    case nir_texop_txf_ms_mcs:
    673       fprintf(fp, "txf_ms_mcs ");
    674       break;
    675    case nir_texop_txs:
    676       fprintf(fp, "txs ");
    677       break;
    678    case nir_texop_lod:
    679       fprintf(fp, "lod ");
    680       break;
    681    case nir_texop_tg4:
    682       fprintf(fp, "tg4 ");
    683       break;
    684    case nir_texop_query_levels:
    685       fprintf(fp, "query_levels ");
    686       break;
    687    case nir_texop_texture_samples:
    688       fprintf(fp, "texture_samples ");
    689       break;
    690    case nir_texop_samples_identical:
    691       fprintf(fp, "samples_identical ");
    692       break;
    693    default:
    694       unreachable("Invalid texture operation");
    695       break;
    696    }
    697 
    698    for (unsigned i = 0; i < instr->num_srcs; i++) {
    699       print_src(&instr->src[i].src, state);
    700 
    701       fprintf(fp, " ");
    702 
    703       switch(instr->src[i].src_type) {
    704       case nir_tex_src_coord:
    705          fprintf(fp, "(coord)");
    706          break;
    707       case nir_tex_src_projector:
    708          fprintf(fp, "(projector)");
    709          break;
    710       case nir_tex_src_comparator:
    711          fprintf(fp, "(comparator)");
    712          break;
    713       case nir_tex_src_offset:
    714          fprintf(fp, "(offset)");
    715          break;
    716       case nir_tex_src_bias:
    717          fprintf(fp, "(bias)");
    718          break;
    719       case nir_tex_src_lod:
    720          fprintf(fp, "(lod)");
    721          break;
    722       case nir_tex_src_ms_index:
    723          fprintf(fp, "(ms_index)");
    724          break;
    725       case nir_tex_src_ms_mcs:
    726          fprintf(fp, "(ms_mcs)");
    727          break;
    728       case nir_tex_src_ddx:
    729          fprintf(fp, "(ddx)");
    730          break;
    731       case nir_tex_src_ddy:
    732          fprintf(fp, "(ddy)");
    733          break;
    734       case nir_tex_src_texture_offset:
    735          fprintf(fp, "(texture_offset)");
    736          break;
    737       case nir_tex_src_sampler_offset:
    738          fprintf(fp, "(sampler_offset)");
    739          break;
    740       case nir_tex_src_plane:
    741          fprintf(fp, "(plane)");
    742          break;
    743 
    744       default:
    745          unreachable("Invalid texture source type");
    746          break;
    747       }
    748 
    749       fprintf(fp, ", ");
    750    }
    751 
    752    if (instr->op == nir_texop_tg4) {
    753       fprintf(fp, "%u (gather_component), ", instr->component);
    754    }
    755 
    756    if (instr->texture) {
    757       print_deref(instr->texture, state);
    758       fprintf(fp, " (texture)");
    759       if (instr->sampler) {
    760          print_deref(instr->sampler, state);
    761          fprintf(fp, " (sampler)");
    762       }
    763    } else {
    764       assert(instr->sampler == NULL);
    765       fprintf(fp, "%u (texture) %u (sampler)",
    766               instr->texture_index, instr->sampler_index);
    767    }
    768 }
    769 
    770 static void
    771 print_call_instr(nir_call_instr *instr, print_state *state)
    772 {
    773    FILE *fp = state->fp;
    774 
    775    fprintf(fp, "call %s ", instr->callee->name);
    776 
    777    for (unsigned i = 0; i < instr->num_params; i++) {
    778       if (i != 0)
    779          fprintf(fp, ", ");
    780 
    781       print_deref(instr->params[i], state);
    782    }
    783 
    784    if (instr->return_deref != NULL) {
    785       if (instr->num_params != 0)
    786          fprintf(fp, ", ");
    787       fprintf(fp, "returning ");
    788       print_deref(instr->return_deref, state);
    789    }
    790 }
    791 
    792 static void
    793 print_load_const_instr(nir_load_const_instr *instr, print_state *state)
    794 {
    795    FILE *fp = state->fp;
    796 
    797    print_ssa_def(&instr->def, state);
    798 
    799    fprintf(fp, " = load_const (");
    800 
    801    for (unsigned i = 0; i < instr->def.num_components; i++) {
    802       if (i != 0)
    803          fprintf(fp, ", ");
    804 
    805       /*
    806        * we don't really know the type of the constant (if it will be used as a
    807        * float or an int), so just print the raw constant in hex for fidelity
    808        * and then print the float in a comment for readability.
    809        */
    810 
    811       if (instr->def.bit_size == 64)
    812          fprintf(fp, "0x%16" PRIx64 " /* %f */", instr->value.u64[i],
    813                  instr->value.f64[i]);
    814       else
    815          fprintf(fp, "0x%08x /* %f */", instr->value.u32[i], instr->value.f32[i]);
    816    }
    817 
    818    fprintf(fp, ")");
    819 }
    820 
    821 static void
    822 print_jump_instr(nir_jump_instr *instr, print_state *state)
    823 {
    824    FILE *fp = state->fp;
    825 
    826    switch (instr->type) {
    827    case nir_jump_break:
    828       fprintf(fp, "break");
    829       break;
    830 
    831    case nir_jump_continue:
    832       fprintf(fp, "continue");
    833       break;
    834 
    835    case nir_jump_return:
    836       fprintf(fp, "return");
    837       break;
    838    }
    839 }
    840 
    841 static void
    842 print_ssa_undef_instr(nir_ssa_undef_instr* instr, print_state *state)
    843 {
    844    FILE *fp = state->fp;
    845    print_ssa_def(&instr->def, state);
    846    fprintf(fp, " = undefined");
    847 }
    848 
    849 static void
    850 print_phi_instr(nir_phi_instr *instr, print_state *state)
    851 {
    852    FILE *fp = state->fp;
    853    print_dest(&instr->dest, state);
    854    fprintf(fp, " = phi ");
    855    nir_foreach_phi_src(src, instr) {
    856       if (&src->node != exec_list_get_head(&instr->srcs))
    857          fprintf(fp, ", ");
    858 
    859       fprintf(fp, "block_%u: ", src->pred->index);
    860       print_src(&src->src, state);
    861    }
    862 }
    863 
    864 static void
    865 print_parallel_copy_instr(nir_parallel_copy_instr *instr, print_state *state)
    866 {
    867    FILE *fp = state->fp;
    868    nir_foreach_parallel_copy_entry(entry, instr) {
    869       if (&entry->node != exec_list_get_head(&instr->entries))
    870          fprintf(fp, "; ");
    871 
    872       print_dest(&entry->dest, state);
    873       fprintf(fp, " = ");
    874       print_src(&entry->src, state);
    875    }
    876 }
    877 
    878 static void
    879 print_instr(const nir_instr *instr, print_state *state, unsigned tabs)
    880 {
    881    FILE *fp = state->fp;
    882    print_tabs(tabs, fp);
    883 
    884    switch (instr->type) {
    885    case nir_instr_type_alu:
    886       print_alu_instr(nir_instr_as_alu(instr), state);
    887       break;
    888 
    889    case nir_instr_type_call:
    890       print_call_instr(nir_instr_as_call(instr), state);
    891       break;
    892 
    893    case nir_instr_type_intrinsic:
    894       print_intrinsic_instr(nir_instr_as_intrinsic(instr), state);
    895       break;
    896 
    897    case nir_instr_type_tex:
    898       print_tex_instr(nir_instr_as_tex(instr), state);
    899       break;
    900 
    901    case nir_instr_type_load_const:
    902       print_load_const_instr(nir_instr_as_load_const(instr), state);
    903       break;
    904 
    905    case nir_instr_type_jump:
    906       print_jump_instr(nir_instr_as_jump(instr), state);
    907       break;
    908 
    909    case nir_instr_type_ssa_undef:
    910       print_ssa_undef_instr(nir_instr_as_ssa_undef(instr), state);
    911       break;
    912 
    913    case nir_instr_type_phi:
    914       print_phi_instr(nir_instr_as_phi(instr), state);
    915       break;
    916 
    917    case nir_instr_type_parallel_copy:
    918       print_parallel_copy_instr(nir_instr_as_parallel_copy(instr), state);
    919       break;
    920 
    921    default:
    922       unreachable("Invalid instruction type");
    923       break;
    924    }
    925 }
    926 
    927 static int
    928 compare_block_index(const void *p1, const void *p2)
    929 {
    930    const nir_block *block1 = *((const nir_block **) p1);
    931    const nir_block *block2 = *((const nir_block **) p2);
    932 
    933    return (int) block1->index - (int) block2->index;
    934 }
    935 
    936 static void print_cf_node(nir_cf_node *node, print_state *state,
    937                           unsigned tabs);
    938 
    939 static void
    940 print_block(nir_block *block, print_state *state, unsigned tabs)
    941 {
    942    FILE *fp = state->fp;
    943 
    944    print_tabs(tabs, fp);
    945    fprintf(fp, "block block_%u:\n", block->index);
    946 
    947    /* sort the predecessors by index so we consistently print the same thing */
    948 
    949    nir_block **preds =
    950       malloc(block->predecessors->entries * sizeof(nir_block *));
    951 
    952    struct set_entry *entry;
    953    unsigned i = 0;
    954    set_foreach(block->predecessors, entry) {
    955       preds[i++] = (nir_block *) entry->key;
    956    }
    957 
    958    qsort(preds, block->predecessors->entries, sizeof(nir_block *),
    959          compare_block_index);
    960 
    961    print_tabs(tabs, fp);
    962    fprintf(fp, "/* preds: ");
    963    for (unsigned i = 0; i < block->predecessors->entries; i++) {
    964       fprintf(fp, "block_%u ", preds[i]->index);
    965    }
    966    fprintf(fp, "*/\n");
    967 
    968    free(preds);
    969 
    970    nir_foreach_instr(instr, block) {
    971       print_instr(instr, state, tabs);
    972       fprintf(fp, "\n");
    973       print_annotation(state, instr);
    974    }
    975 
    976    print_tabs(tabs, fp);
    977    fprintf(fp, "/* succs: ");
    978    for (unsigned i = 0; i < 2; i++)
    979       if (block->successors[i]) {
    980          fprintf(fp, "block_%u ", block->successors[i]->index);
    981       }
    982    fprintf(fp, "*/\n");
    983 }
    984 
    985 static void
    986 print_if(nir_if *if_stmt, print_state *state, unsigned tabs)
    987 {
    988    FILE *fp = state->fp;
    989 
    990    print_tabs(tabs, fp);
    991    fprintf(fp, "if ");
    992    print_src(&if_stmt->condition, state);
    993    fprintf(fp, " {\n");
    994    foreach_list_typed(nir_cf_node, node, node, &if_stmt->then_list) {
    995       print_cf_node(node, state, tabs + 1);
    996    }
    997    print_tabs(tabs, fp);
    998    fprintf(fp, "} else {\n");
    999    foreach_list_typed(nir_cf_node, node, node, &if_stmt->else_list) {
   1000       print_cf_node(node, state, tabs + 1);
   1001    }
   1002    print_tabs(tabs, fp);
   1003    fprintf(fp, "}\n");
   1004 }
   1005 
   1006 static void
   1007 print_loop(nir_loop *loop, print_state *state, unsigned tabs)
   1008 {
   1009    FILE *fp = state->fp;
   1010 
   1011    print_tabs(tabs, fp);
   1012    fprintf(fp, "loop {\n");
   1013    foreach_list_typed(nir_cf_node, node, node, &loop->body) {
   1014       print_cf_node(node, state, tabs + 1);
   1015    }
   1016    print_tabs(tabs, fp);
   1017    fprintf(fp, "}\n");
   1018 }
   1019 
   1020 static void
   1021 print_cf_node(nir_cf_node *node, print_state *state, unsigned int tabs)
   1022 {
   1023    switch (node->type) {
   1024    case nir_cf_node_block:
   1025       print_block(nir_cf_node_as_block(node), state, tabs);
   1026       break;
   1027 
   1028    case nir_cf_node_if:
   1029       print_if(nir_cf_node_as_if(node), state, tabs);
   1030       break;
   1031 
   1032    case nir_cf_node_loop:
   1033       print_loop(nir_cf_node_as_loop(node), state, tabs);
   1034       break;
   1035 
   1036    default:
   1037       unreachable("Invalid CFG node type");
   1038    }
   1039 }
   1040 
   1041 static void
   1042 print_function_impl(nir_function_impl *impl, print_state *state)
   1043 {
   1044    FILE *fp = state->fp;
   1045 
   1046    fprintf(fp, "\nimpl %s ", impl->function->name);
   1047 
   1048    for (unsigned i = 0; i < impl->num_params; i++) {
   1049       if (i != 0)
   1050          fprintf(fp, ", ");
   1051 
   1052       print_arg(impl->params[i], state);
   1053    }
   1054 
   1055    if (impl->return_var != NULL) {
   1056       if (impl->num_params != 0)
   1057          fprintf(fp, ", ");
   1058       fprintf(fp, "returning ");
   1059       print_arg(impl->return_var, state);
   1060    }
   1061 
   1062    fprintf(fp, "{\n");
   1063 
   1064    nir_foreach_variable(var, &impl->locals) {
   1065       fprintf(fp, "\t");
   1066       print_var_decl(var, state);
   1067    }
   1068 
   1069    foreach_list_typed(nir_register, reg, node, &impl->registers) {
   1070       fprintf(fp, "\t");
   1071       print_register_decl(reg, state);
   1072    }
   1073 
   1074    nir_index_blocks(impl);
   1075 
   1076    foreach_list_typed(nir_cf_node, node, node, &impl->body) {
   1077       print_cf_node(node, state, 1);
   1078    }
   1079 
   1080    fprintf(fp, "\tblock block_%u:\n}\n\n", impl->end_block->index);
   1081 }
   1082 
   1083 static void
   1084 print_function(nir_function *function, print_state *state)
   1085 {
   1086    FILE *fp = state->fp;
   1087 
   1088    fprintf(fp, "decl_function %s ", function->name);
   1089 
   1090    for (unsigned i = 0; i < function->num_params; i++) {
   1091       if (i != 0)
   1092          fprintf(fp, ", ");
   1093 
   1094       switch (function->params[i].param_type) {
   1095       case nir_parameter_in:
   1096          fprintf(fp, "in ");
   1097          break;
   1098       case nir_parameter_out:
   1099          fprintf(fp, "out ");
   1100          break;
   1101       case nir_parameter_inout:
   1102          fprintf(fp, "inout ");
   1103          break;
   1104       default:
   1105          unreachable("Invalid parameter type");
   1106       }
   1107 
   1108       fprintf(fp, "%s", glsl_get_type_name(function->params[i].type));
   1109    }
   1110 
   1111    if (function->return_type != NULL) {
   1112       if (function->num_params != 0)
   1113          fprintf(fp, ", ");
   1114       fprintf(fp, "returning %s", glsl_get_type_name(function->return_type));
   1115    }
   1116 
   1117    fprintf(fp, "\n");
   1118 
   1119    if (function->impl != NULL) {
   1120       print_function_impl(function->impl, state);
   1121       return;
   1122    }
   1123 }
   1124 
   1125 static void
   1126 init_print_state(print_state *state, nir_shader *shader, FILE *fp)
   1127 {
   1128    state->fp = fp;
   1129    state->shader = shader;
   1130    state->ht = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
   1131                                        _mesa_key_pointer_equal);
   1132    state->syms = _mesa_set_create(NULL, _mesa_key_hash_string,
   1133                                   _mesa_key_string_equal);
   1134    state->index = 0;
   1135 }
   1136 
   1137 static void
   1138 destroy_print_state(print_state *state)
   1139 {
   1140    _mesa_hash_table_destroy(state->ht, NULL);
   1141    _mesa_set_destroy(state->syms, NULL);
   1142 }
   1143 
   1144 void
   1145 nir_print_shader_annotated(nir_shader *shader, FILE *fp,
   1146                            struct hash_table *annotations)
   1147 {
   1148    print_state state;
   1149    init_print_state(&state, shader, fp);
   1150 
   1151    state.annotations = annotations;
   1152 
   1153    fprintf(fp, "shader: %s\n", gl_shader_stage_name(shader->stage));
   1154 
   1155    if (shader->info->name)
   1156       fprintf(fp, "name: %s\n", shader->info->name);
   1157 
   1158    if (shader->info->label)
   1159       fprintf(fp, "label: %s\n", shader->info->label);
   1160 
   1161    fprintf(fp, "inputs: %u\n", shader->num_inputs);
   1162    fprintf(fp, "outputs: %u\n", shader->num_outputs);
   1163    fprintf(fp, "uniforms: %u\n", shader->num_uniforms);
   1164    fprintf(fp, "shared: %u\n", shader->num_shared);
   1165 
   1166    nir_foreach_variable(var, &shader->uniforms) {
   1167       print_var_decl(var, &state);
   1168    }
   1169 
   1170    nir_foreach_variable(var, &shader->inputs) {
   1171       print_var_decl(var, &state);
   1172    }
   1173 
   1174    nir_foreach_variable(var, &shader->outputs) {
   1175       print_var_decl(var, &state);
   1176    }
   1177 
   1178    nir_foreach_variable(var, &shader->shared) {
   1179       print_var_decl(var, &state);
   1180    }
   1181 
   1182    nir_foreach_variable(var, &shader->globals) {
   1183       print_var_decl(var, &state);
   1184    }
   1185 
   1186    nir_foreach_variable(var, &shader->system_values) {
   1187       print_var_decl(var, &state);
   1188    }
   1189 
   1190    foreach_list_typed(nir_register, reg, node, &shader->registers) {
   1191       print_register_decl(reg, &state);
   1192    }
   1193 
   1194    foreach_list_typed(nir_function, func, node, &shader->functions) {
   1195       print_function(func, &state);
   1196    }
   1197 
   1198    destroy_print_state(&state);
   1199 }
   1200 
   1201 void
   1202 nir_print_shader(nir_shader *shader, FILE *fp)
   1203 {
   1204    nir_print_shader_annotated(shader, fp, NULL);
   1205 }
   1206 
   1207 void
   1208 nir_print_instr(const nir_instr *instr, FILE *fp)
   1209 {
   1210    print_state state = {
   1211       .fp = fp,
   1212    };
   1213    print_instr(instr, &state, 0);
   1214 
   1215 }
   1216