nir_print.c revision 01e04c3f
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@gmail.com) 25 * 26 */ 27 28#include "nir.h" 29#include "compiler/shader_enums.h" 30#include "util/half_float.h" 31#include <stdio.h> 32#include <stdlib.h> 33#include <inttypes.h> /* for PRIx64 macro */ 34 35static void 36print_tabs(unsigned num_tabs, FILE *fp) 37{ 38 for (unsigned i = 0; i < num_tabs; i++) 39 fprintf(fp, "\t"); 40} 41 42typedef struct { 43 FILE *fp; 44 nir_shader *shader; 45 /** map from nir_variable -> printable name */ 46 struct hash_table *ht; 47 48 /** set of names used so far for nir_variables */ 49 struct set *syms; 50 51 /* an index used to make new non-conflicting names */ 52 unsigned index; 53 54 /** 55 * Optional table of annotations mapping nir object 56 * (such as instr or var) to message to print. 57 */ 58 struct hash_table *annotations; 59} print_state; 60 61static void 62print_annotation(print_state *state, void *obj) 63{ 64 if (!state->annotations) 65 return; 66 67 struct hash_entry *entry = _mesa_hash_table_search(state->annotations, obj); 68 if (!entry) 69 return; 70 71 const char *note = entry->data; 72 _mesa_hash_table_remove(state->annotations, entry); 73 74 fprintf(stderr, "%s\n\n", note); 75} 76 77static void 78print_register(nir_register *reg, print_state *state) 79{ 80 FILE *fp = state->fp; 81 if (reg->name != NULL) 82 fprintf(fp, "/* %s */ ", reg->name); 83 if (reg->is_global) 84 fprintf(fp, "gr%u", reg->index); 85 else 86 fprintf(fp, "r%u", reg->index); 87} 88 89static const char *sizes[] = { "error", "vec1", "vec2", "vec3", "vec4", 90 "error", "error", "error", "vec8", 91 "error", "error", "error", "error", 92 "error", "error", "error", "vec16"}; 93 94static void 95print_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 107static void 108print_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 117static void 118print_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 126static void print_src(nir_src *src, print_state *state); 127 128static void 129print_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 143static void 144print_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 158static void 159print_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 167static void 168print_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 176static void 177print_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 nir_component_mask_t used_channels = 0; 190 191 for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; 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 = nir_src_num_components(instr->src[src].src); 204 205 if (print_swizzle || used_channels != live_channels) { 206 fprintf(fp, "."); 207 for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) { 208 if (!nir_alu_instr_channel_used(instr, src, i)) 209 continue; 210 211 fprintf(fp, "%c", "xyzw"[instr->src[src].swizzle[i]]); 212 } 213 } 214 215 if (instr->src[src].abs) 216 fprintf(fp, ")"); 217} 218 219static void 220print_alu_dest(nir_alu_dest *dest, print_state *state) 221{ 222 FILE *fp = state->fp; 223 /* we're going to print the saturate modifier later, after the opcode */ 224 225 print_dest(&dest->dest, state); 226 227 if (!dest->dest.is_ssa && 228 dest->write_mask != (1 << dest->dest.reg.reg->num_components) - 1) { 229 fprintf(fp, "."); 230 for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) 231 if ((dest->write_mask >> i) & 1) 232 fprintf(fp, "%c", "xyzw"[i]); 233 } 234} 235 236static void 237print_alu_instr(nir_alu_instr *instr, print_state *state) 238{ 239 FILE *fp = state->fp; 240 241 print_alu_dest(&instr->dest, state); 242 243 fprintf(fp, " = %s", nir_op_infos[instr->op].name); 244 if (instr->exact) 245 fprintf(fp, "!"); 246 if (instr->dest.saturate) 247 fprintf(fp, ".sat"); 248 fprintf(fp, " "); 249 250 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) { 251 if (i != 0) 252 fprintf(fp, ", "); 253 254 print_alu_src(instr, i, state); 255 } 256} 257 258static const char * 259get_var_name(nir_variable *var, print_state *state) 260{ 261 if (state->ht == NULL) 262 return var->name ? var->name : "unnamed"; 263 264 assert(state->syms); 265 266 struct hash_entry *entry = _mesa_hash_table_search(state->ht, var); 267 if (entry) 268 return entry->data; 269 270 char *name; 271 if (var->name == NULL) { 272 name = ralloc_asprintf(state->syms, "@%u", state->index++); 273 } else { 274 struct set_entry *set_entry = _mesa_set_search(state->syms, var->name); 275 if (set_entry != NULL) { 276 /* we have a collision with another name, append an @ + a unique 277 * index */ 278 name = ralloc_asprintf(state->syms, "%s@%u", var->name, 279 state->index++); 280 } else { 281 /* Mark this one as seen */ 282 _mesa_set_add(state->syms, var->name); 283 name = var->name; 284 } 285 } 286 287 _mesa_hash_table_insert(state->ht, var, name); 288 289 return name; 290} 291 292static void 293print_constant(nir_constant *c, const struct glsl_type *type, print_state *state) 294{ 295 FILE *fp = state->fp; 296 const unsigned rows = glsl_get_vector_elements(type); 297 const unsigned cols = glsl_get_matrix_columns(type); 298 unsigned i, j; 299 300 switch (glsl_get_base_type(type)) { 301 case GLSL_TYPE_UINT8: 302 case GLSL_TYPE_INT8: 303 /* Only float base types can be matrices. */ 304 assert(cols == 1); 305 306 for (i = 0; i < rows; i++) { 307 if (i > 0) fprintf(fp, ", "); 308 fprintf(fp, "0x%02x", c->values[0].u8[i]); 309 } 310 break; 311 312 case GLSL_TYPE_UINT16: 313 case GLSL_TYPE_INT16: 314 /* Only float base types can be matrices. */ 315 assert(cols == 1); 316 317 for (i = 0; i < rows; i++) { 318 if (i > 0) fprintf(fp, ", "); 319 fprintf(fp, "0x%04x", c->values[0].u16[i]); 320 } 321 break; 322 323 case GLSL_TYPE_UINT: 324 case GLSL_TYPE_INT: 325 case GLSL_TYPE_BOOL: 326 /* Only float base types can be matrices. */ 327 assert(cols == 1); 328 329 for (i = 0; i < rows; i++) { 330 if (i > 0) fprintf(fp, ", "); 331 fprintf(fp, "0x%08x", c->values[0].u32[i]); 332 } 333 break; 334 335 case GLSL_TYPE_FLOAT16: 336 for (i = 0; i < cols; i++) { 337 for (j = 0; j < rows; j++) { 338 if (i + j > 0) fprintf(fp, ", "); 339 fprintf(fp, "%f", _mesa_half_to_float(c->values[i].u16[j])); 340 } 341 } 342 break; 343 344 case GLSL_TYPE_FLOAT: 345 for (i = 0; i < cols; i++) { 346 for (j = 0; j < rows; j++) { 347 if (i + j > 0) fprintf(fp, ", "); 348 fprintf(fp, "%f", c->values[i].f32[j]); 349 } 350 } 351 break; 352 353 case GLSL_TYPE_DOUBLE: 354 for (i = 0; i < cols; i++) { 355 for (j = 0; j < rows; j++) { 356 if (i + j > 0) fprintf(fp, ", "); 357 fprintf(fp, "%f", c->values[i].f64[j]); 358 } 359 } 360 break; 361 362 case GLSL_TYPE_UINT64: 363 case GLSL_TYPE_INT64: 364 /* Only float base types can be matrices. */ 365 assert(cols == 1); 366 367 for (i = 0; i < cols; i++) { 368 if (i > 0) fprintf(fp, ", "); 369 fprintf(fp, "0x%08" PRIx64, c->values[0].u64[i]); 370 } 371 break; 372 373 case GLSL_TYPE_STRUCT: 374 for (i = 0; i < c->num_elements; i++) { 375 if (i > 0) fprintf(fp, ", "); 376 fprintf(fp, "{ "); 377 print_constant(c->elements[i], glsl_get_struct_field(type, i), state); 378 fprintf(fp, " }"); 379 } 380 break; 381 382 case GLSL_TYPE_ARRAY: 383 for (i = 0; i < c->num_elements; i++) { 384 if (i > 0) fprintf(fp, ", "); 385 fprintf(fp, "{ "); 386 print_constant(c->elements[i], glsl_get_array_element(type), state); 387 fprintf(fp, " }"); 388 } 389 break; 390 391 default: 392 unreachable("not reached"); 393 } 394} 395 396static const char * 397get_variable_mode_str(nir_variable_mode mode, bool want_local_global_mode) 398{ 399 switch (mode) { 400 case nir_var_shader_in: 401 return "shader_in"; 402 case nir_var_shader_out: 403 return "shader_out"; 404 case nir_var_uniform: 405 return "uniform"; 406 case nir_var_shader_storage: 407 return "shader_storage"; 408 case nir_var_system_value: 409 return "system"; 410 case nir_var_shared: 411 return "shared"; 412 case nir_var_global: 413 return want_local_global_mode ? "global" : ""; 414 case nir_var_local: 415 return want_local_global_mode ? "local" : ""; 416 default: 417 return ""; 418 } 419} 420 421static void 422print_var_decl(nir_variable *var, print_state *state) 423{ 424 FILE *fp = state->fp; 425 426 fprintf(fp, "decl_var "); 427 428 const char *const cent = (var->data.centroid) ? "centroid " : ""; 429 const char *const samp = (var->data.sample) ? "sample " : ""; 430 const char *const patch = (var->data.patch) ? "patch " : ""; 431 const char *const inv = (var->data.invariant) ? "invariant " : ""; 432 fprintf(fp, "%s%s%s%s%s %s ", 433 cent, samp, patch, inv, get_variable_mode_str(var->data.mode, false), 434 glsl_interp_mode_name(var->data.interpolation)); 435 436 enum gl_access_qualifier access = var->data.image.access; 437 const char *const coher = (access & ACCESS_COHERENT) ? "coherent " : ""; 438 const char *const volat = (access & ACCESS_VOLATILE) ? "volatile " : ""; 439 const char *const restr = (access & ACCESS_RESTRICT) ? "restrict " : ""; 440 const char *const ronly = (access & ACCESS_NON_WRITEABLE) ? "readonly " : ""; 441 const char *const wonly = (access & ACCESS_NON_READABLE) ? "writeonly " : ""; 442 fprintf(fp, "%s%s%s%s%s", coher, volat, restr, ronly, wonly); 443 444 fprintf(fp, "%s %s", glsl_get_type_name(var->type), 445 get_var_name(var, state)); 446 447 if (var->data.mode == nir_var_shader_in || 448 var->data.mode == nir_var_shader_out || 449 var->data.mode == nir_var_uniform || 450 var->data.mode == nir_var_shader_storage) { 451 const char *loc = NULL; 452 char buf[4]; 453 454 switch (state->shader->info.stage) { 455 case MESA_SHADER_VERTEX: 456 if (var->data.mode == nir_var_shader_in) 457 loc = gl_vert_attrib_name(var->data.location); 458 else if (var->data.mode == nir_var_shader_out) 459 loc = gl_varying_slot_name(var->data.location); 460 break; 461 case MESA_SHADER_GEOMETRY: 462 if ((var->data.mode == nir_var_shader_in) || 463 (var->data.mode == nir_var_shader_out)) 464 loc = gl_varying_slot_name(var->data.location); 465 break; 466 case MESA_SHADER_FRAGMENT: 467 if (var->data.mode == nir_var_shader_in) 468 loc = gl_varying_slot_name(var->data.location); 469 else if (var->data.mode == nir_var_shader_out) 470 loc = gl_frag_result_name(var->data.location); 471 break; 472 case MESA_SHADER_TESS_CTRL: 473 case MESA_SHADER_TESS_EVAL: 474 case MESA_SHADER_COMPUTE: 475 default: 476 /* TODO */ 477 break; 478 } 479 480 if (!loc) { 481 snprintf(buf, sizeof(buf), "%u", var->data.location); 482 loc = buf; 483 } 484 485 /* For shader I/O vars that have been split to components or packed, 486 * print the fractional location within the input/output. 487 */ 488 unsigned int num_components = 489 glsl_get_components(glsl_without_array(var->type)); 490 const char *components = NULL; 491 char components_local[6] = {'.' /* the rest is 0-filled */}; 492 switch (var->data.mode) { 493 case nir_var_shader_in: 494 case nir_var_shader_out: 495 if (num_components < 4 && num_components != 0) { 496 const char *xyzw = "xyzw"; 497 for (int i = 0; i < num_components; i++) 498 components_local[i + 1] = xyzw[i + var->data.location_frac]; 499 500 components = components_local; 501 } 502 break; 503 default: 504 break; 505 } 506 507 fprintf(fp, " (%s%s, %u, %u)%s", loc, 508 components ? components : "", 509 var->data.driver_location, var->data.binding, 510 var->data.compact ? " compact" : ""); 511 } 512 513 if (var->constant_initializer) { 514 fprintf(fp, " = { "); 515 print_constant(var->constant_initializer, var->type, state); 516 fprintf(fp, " }"); 517 } 518 519 fprintf(fp, "\n"); 520 print_annotation(state, var); 521} 522 523static void 524print_deref_link(nir_deref_instr *instr, bool whole_chain, print_state *state) 525{ 526 FILE *fp = state->fp; 527 528 if (instr->deref_type == nir_deref_type_var) { 529 fprintf(fp, "%s", get_var_name(instr->var, state)); 530 return; 531 } else if (instr->deref_type == nir_deref_type_cast) { 532 fprintf(fp, "(%s *)", glsl_get_type_name(instr->type)); 533 print_src(&instr->parent, state); 534 return; 535 } 536 537 assert(instr->parent.is_ssa); 538 nir_deref_instr *parent = 539 nir_instr_as_deref(instr->parent.ssa->parent_instr); 540 541 /* Is the parent we're going to print a bare cast? */ 542 const bool is_parent_cast = 543 whole_chain && parent->deref_type == nir_deref_type_cast; 544 545 /* If we're not printing the whole chain, the parent we print will be a SSA 546 * value that represents a pointer. The only deref type that naturally 547 * gives a pointer is a cast. 548 */ 549 const bool is_parent_pointer = 550 !whole_chain || parent->deref_type == nir_deref_type_cast; 551 552 /* Struct derefs have a nice syntax that works on pointers, arrays derefs 553 * do not. 554 */ 555 const bool need_deref = 556 is_parent_pointer && instr->deref_type != nir_deref_type_struct; 557 558 /* Cast need extra parens and so * dereferences */ 559 if (is_parent_cast || need_deref) 560 fprintf(fp, "("); 561 562 if (need_deref) 563 fprintf(fp, "*"); 564 565 if (whole_chain) { 566 print_deref_link(parent, whole_chain, state); 567 } else { 568 print_src(&instr->parent, state); 569 } 570 571 if (is_parent_cast || need_deref) 572 fprintf(fp, ")"); 573 574 switch (instr->deref_type) { 575 case nir_deref_type_struct: 576 fprintf(fp, "%s%s", is_parent_pointer ? "->" : ".", 577 glsl_get_struct_elem_name(parent->type, instr->strct.index)); 578 break; 579 580 case nir_deref_type_array: { 581 nir_const_value *const_index = nir_src_as_const_value(instr->arr.index); 582 if (const_index) { 583 fprintf(fp, "[%u]", const_index->u32[0]); 584 } else { 585 fprintf(fp, "["); 586 print_src(&instr->arr.index, state); 587 fprintf(fp, "]"); 588 } 589 break; 590 } 591 592 case nir_deref_type_array_wildcard: 593 fprintf(fp, "[*]"); 594 break; 595 596 default: 597 unreachable("Invalid deref instruction type"); 598 } 599} 600 601static void 602print_deref_instr(nir_deref_instr *instr, print_state *state) 603{ 604 FILE *fp = state->fp; 605 606 print_dest(&instr->dest, state); 607 608 switch (instr->deref_type) { 609 case nir_deref_type_var: 610 fprintf(fp, " = deref_var "); 611 break; 612 case nir_deref_type_array: 613 case nir_deref_type_array_wildcard: 614 fprintf(fp, " = deref_array "); 615 break; 616 case nir_deref_type_struct: 617 fprintf(fp, " = deref_struct "); 618 break; 619 case nir_deref_type_cast: 620 fprintf(fp, " = deref_cast "); 621 break; 622 default: 623 unreachable("Invalid deref instruction type"); 624 } 625 626 /* Only casts naturally return a pointer type */ 627 if (instr->deref_type != nir_deref_type_cast) 628 fprintf(fp, "&"); 629 630 print_deref_link(instr, false, state); 631 632 fprintf(fp, " (%s %s) ", 633 get_variable_mode_str(instr->mode, true), 634 glsl_get_type_name(instr->type)); 635 636 if (instr->deref_type != nir_deref_type_var && 637 instr->deref_type != nir_deref_type_cast) { 638 /* Print the entire chain as a comment */ 639 fprintf(fp, "/* &"); 640 print_deref_link(instr, true, state); 641 fprintf(fp, " */"); 642 } 643} 644 645static void 646print_intrinsic_instr(nir_intrinsic_instr *instr, print_state *state) 647{ 648 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic]; 649 unsigned num_srcs = info->num_srcs; 650 FILE *fp = state->fp; 651 652 if (info->has_dest) { 653 print_dest(&instr->dest, state); 654 fprintf(fp, " = "); 655 } 656 657 fprintf(fp, "intrinsic %s (", info->name); 658 659 for (unsigned i = 0; i < num_srcs; i++) { 660 if (i != 0) 661 fprintf(fp, ", "); 662 663 print_src(&instr->src[i], state); 664 } 665 666 fprintf(fp, ") ("); 667 668 for (unsigned i = 0; i < info->num_indices; i++) { 669 if (i != 0) 670 fprintf(fp, ", "); 671 672 fprintf(fp, "%d", instr->const_index[i]); 673 } 674 675 fprintf(fp, ")"); 676 677 static const char *index_name[NIR_INTRINSIC_NUM_INDEX_FLAGS] = { 678 [NIR_INTRINSIC_BASE] = "base", 679 [NIR_INTRINSIC_WRMASK] = "wrmask", 680 [NIR_INTRINSIC_STREAM_ID] = "stream-id", 681 [NIR_INTRINSIC_UCP_ID] = "ucp-id", 682 [NIR_INTRINSIC_RANGE] = "range", 683 [NIR_INTRINSIC_DESC_SET] = "desc-set", 684 [NIR_INTRINSIC_BINDING] = "binding", 685 [NIR_INTRINSIC_COMPONENT] = "component", 686 [NIR_INTRINSIC_INTERP_MODE] = "interp_mode", 687 [NIR_INTRINSIC_REDUCTION_OP] = "reduction_op", 688 [NIR_INTRINSIC_CLUSTER_SIZE] = "cluster_size", 689 [NIR_INTRINSIC_PARAM_IDX] = "param_idx", 690 [NIR_INTRINSIC_IMAGE_DIM] = "image_dim", 691 [NIR_INTRINSIC_IMAGE_ARRAY] = "image_array", 692 [NIR_INTRINSIC_ACCESS] = "access", 693 [NIR_INTRINSIC_FORMAT] = "format", 694 }; 695 for (unsigned idx = 1; idx < NIR_INTRINSIC_NUM_INDEX_FLAGS; idx++) { 696 if (!info->index_map[idx]) 697 continue; 698 fprintf(fp, " /*"); 699 if (idx == NIR_INTRINSIC_WRMASK) { 700 /* special case wrmask to show it as a writemask.. */ 701 unsigned wrmask = nir_intrinsic_write_mask(instr); 702 fprintf(fp, " wrmask="); 703 for (unsigned i = 0; i < 4; i++) 704 if ((wrmask >> i) & 1) 705 fprintf(fp, "%c", "xyzw"[i]); 706 } else if (idx == NIR_INTRINSIC_REDUCTION_OP) { 707 nir_op reduction_op = nir_intrinsic_reduction_op(instr); 708 fprintf(fp, " reduction_op=%s", nir_op_infos[reduction_op].name); 709 } else if (idx == NIR_INTRINSIC_IMAGE_DIM) { 710 static const char *dim_name[] = { 711 [GLSL_SAMPLER_DIM_1D] = "1D", 712 [GLSL_SAMPLER_DIM_2D] = "2D", 713 [GLSL_SAMPLER_DIM_3D] = "3D", 714 [GLSL_SAMPLER_DIM_CUBE] = "Cube", 715 [GLSL_SAMPLER_DIM_RECT] = "Rect", 716 [GLSL_SAMPLER_DIM_BUF] = "Buf", 717 [GLSL_SAMPLER_DIM_MS] = "2D-MSAA", 718 [GLSL_SAMPLER_DIM_SUBPASS] = "Subpass", 719 [GLSL_SAMPLER_DIM_SUBPASS_MS] = "Subpass-MSAA", 720 }; 721 enum glsl_sampler_dim dim = nir_intrinsic_image_dim(instr); 722 assert(dim < ARRAY_SIZE(dim_name) && dim_name[idx]); 723 fprintf(fp, " image_dim=%s", dim_name[dim]); 724 } else if (idx == NIR_INTRINSIC_IMAGE_ARRAY) { 725 bool array = nir_intrinsic_image_dim(instr); 726 fprintf(fp, " image_dim=%s", array ? "true" : "false"); 727 } else { 728 unsigned off = info->index_map[idx] - 1; 729 assert(index_name[idx]); /* forgot to update index_name table? */ 730 fprintf(fp, " %s=%d", index_name[idx], instr->const_index[off]); 731 } 732 fprintf(fp, " */"); 733 } 734 735 if (!state->shader) 736 return; 737 738 struct exec_list *var_list = NULL; 739 740 switch (instr->intrinsic) { 741 case nir_intrinsic_load_uniform: 742 var_list = &state->shader->uniforms; 743 break; 744 case nir_intrinsic_load_input: 745 case nir_intrinsic_load_per_vertex_input: 746 var_list = &state->shader->inputs; 747 break; 748 case nir_intrinsic_load_output: 749 case nir_intrinsic_store_output: 750 case nir_intrinsic_store_per_vertex_output: 751 var_list = &state->shader->outputs; 752 break; 753 default: 754 return; 755 } 756 757 nir_foreach_variable(var, var_list) { 758 if ((var->data.driver_location == nir_intrinsic_base(instr)) && 759 (instr->intrinsic == nir_intrinsic_load_uniform || 760 var->data.location_frac == nir_intrinsic_component(instr)) && 761 var->name) { 762 fprintf(fp, "\t/* %s */", var->name); 763 break; 764 } 765 } 766} 767 768static void 769print_tex_instr(nir_tex_instr *instr, print_state *state) 770{ 771 FILE *fp = state->fp; 772 773 print_dest(&instr->dest, state); 774 775 fprintf(fp, " = "); 776 777 switch (instr->op) { 778 case nir_texop_tex: 779 fprintf(fp, "tex "); 780 break; 781 case nir_texop_txb: 782 fprintf(fp, "txb "); 783 break; 784 case nir_texop_txl: 785 fprintf(fp, "txl "); 786 break; 787 case nir_texop_txd: 788 fprintf(fp, "txd "); 789 break; 790 case nir_texop_txf: 791 fprintf(fp, "txf "); 792 break; 793 case nir_texop_txf_ms: 794 fprintf(fp, "txf_ms "); 795 break; 796 case nir_texop_txf_ms_mcs: 797 fprintf(fp, "txf_ms_mcs "); 798 break; 799 case nir_texop_txs: 800 fprintf(fp, "txs "); 801 break; 802 case nir_texop_lod: 803 fprintf(fp, "lod "); 804 break; 805 case nir_texop_tg4: 806 fprintf(fp, "tg4 "); 807 break; 808 case nir_texop_query_levels: 809 fprintf(fp, "query_levels "); 810 break; 811 case nir_texop_texture_samples: 812 fprintf(fp, "texture_samples "); 813 break; 814 case nir_texop_samples_identical: 815 fprintf(fp, "samples_identical "); 816 break; 817 default: 818 unreachable("Invalid texture operation"); 819 break; 820 } 821 822 bool has_texture_deref = false, has_sampler_deref = false; 823 for (unsigned i = 0; i < instr->num_srcs; i++) { 824 print_src(&instr->src[i].src, state); 825 826 fprintf(fp, " "); 827 828 switch(instr->src[i].src_type) { 829 case nir_tex_src_coord: 830 fprintf(fp, "(coord)"); 831 break; 832 case nir_tex_src_projector: 833 fprintf(fp, "(projector)"); 834 break; 835 case nir_tex_src_comparator: 836 fprintf(fp, "(comparator)"); 837 break; 838 case nir_tex_src_offset: 839 fprintf(fp, "(offset)"); 840 break; 841 case nir_tex_src_bias: 842 fprintf(fp, "(bias)"); 843 break; 844 case nir_tex_src_lod: 845 fprintf(fp, "(lod)"); 846 break; 847 case nir_tex_src_ms_index: 848 fprintf(fp, "(ms_index)"); 849 break; 850 case nir_tex_src_ms_mcs: 851 fprintf(fp, "(ms_mcs)"); 852 break; 853 case nir_tex_src_ddx: 854 fprintf(fp, "(ddx)"); 855 break; 856 case nir_tex_src_ddy: 857 fprintf(fp, "(ddy)"); 858 break; 859 case nir_tex_src_texture_deref: 860 has_texture_deref = true; 861 fprintf(fp, "(texture_deref)"); 862 break; 863 case nir_tex_src_sampler_deref: 864 has_sampler_deref = true; 865 fprintf(fp, "(sampler_deref)"); 866 break; 867 case nir_tex_src_texture_offset: 868 fprintf(fp, "(texture_offset)"); 869 break; 870 case nir_tex_src_sampler_offset: 871 fprintf(fp, "(sampler_offset)"); 872 break; 873 case nir_tex_src_plane: 874 fprintf(fp, "(plane)"); 875 break; 876 877 default: 878 unreachable("Invalid texture source type"); 879 break; 880 } 881 882 fprintf(fp, ", "); 883 } 884 885 if (instr->op == nir_texop_tg4) { 886 fprintf(fp, "%u (gather_component), ", instr->component); 887 } 888 889 if (!has_texture_deref) { 890 fprintf(fp, "%u (texture), ", instr->texture_index); 891 } 892 893 if (!has_sampler_deref) { 894 fprintf(fp, "%u (sampler), ", instr->sampler_index); 895 } 896} 897 898static void 899print_call_instr(nir_call_instr *instr, print_state *state) 900{ 901 FILE *fp = state->fp; 902 903 fprintf(fp, "call %s ", instr->callee->name); 904 905 for (unsigned i = 0; i < instr->num_params; i++) { 906 if (i != 0) 907 fprintf(fp, ", "); 908 909 print_src(&instr->params[i], state); 910 } 911} 912 913static void 914print_load_const_instr(nir_load_const_instr *instr, print_state *state) 915{ 916 FILE *fp = state->fp; 917 918 print_ssa_def(&instr->def, state); 919 920 fprintf(fp, " = load_const ("); 921 922 for (unsigned i = 0; i < instr->def.num_components; i++) { 923 if (i != 0) 924 fprintf(fp, ", "); 925 926 /* 927 * we don't really know the type of the constant (if it will be used as a 928 * float or an int), so just print the raw constant in hex for fidelity 929 * and then print the float in a comment for readability. 930 */ 931 932 switch (instr->def.bit_size) { 933 case 64: 934 fprintf(fp, "0x%16" PRIx64 " /* %f */", instr->value.u64[i], 935 instr->value.f64[i]); 936 break; 937 case 32: 938 fprintf(fp, "0x%08x /* %f */", instr->value.u32[i], instr->value.f32[i]); 939 break; 940 case 16: 941 fprintf(fp, "0x%04x /* %f */", instr->value.u16[i], 942 _mesa_half_to_float(instr->value.u16[i])); 943 break; 944 case 8: 945 fprintf(fp, "0x%02x", instr->value.u8[i]); 946 break; 947 } 948 } 949 950 fprintf(fp, ")"); 951} 952 953static void 954print_jump_instr(nir_jump_instr *instr, print_state *state) 955{ 956 FILE *fp = state->fp; 957 958 switch (instr->type) { 959 case nir_jump_break: 960 fprintf(fp, "break"); 961 break; 962 963 case nir_jump_continue: 964 fprintf(fp, "continue"); 965 break; 966 967 case nir_jump_return: 968 fprintf(fp, "return"); 969 break; 970 } 971} 972 973static void 974print_ssa_undef_instr(nir_ssa_undef_instr* instr, print_state *state) 975{ 976 FILE *fp = state->fp; 977 print_ssa_def(&instr->def, state); 978 fprintf(fp, " = undefined"); 979} 980 981static void 982print_phi_instr(nir_phi_instr *instr, print_state *state) 983{ 984 FILE *fp = state->fp; 985 print_dest(&instr->dest, state); 986 fprintf(fp, " = phi "); 987 nir_foreach_phi_src(src, instr) { 988 if (&src->node != exec_list_get_head(&instr->srcs)) 989 fprintf(fp, ", "); 990 991 fprintf(fp, "block_%u: ", src->pred->index); 992 print_src(&src->src, state); 993 } 994} 995 996static void 997print_parallel_copy_instr(nir_parallel_copy_instr *instr, print_state *state) 998{ 999 FILE *fp = state->fp; 1000 nir_foreach_parallel_copy_entry(entry, instr) { 1001 if (&entry->node != exec_list_get_head(&instr->entries)) 1002 fprintf(fp, "; "); 1003 1004 print_dest(&entry->dest, state); 1005 fprintf(fp, " = "); 1006 print_src(&entry->src, state); 1007 } 1008} 1009 1010static void 1011print_instr(const nir_instr *instr, print_state *state, unsigned tabs) 1012{ 1013 FILE *fp = state->fp; 1014 print_tabs(tabs, fp); 1015 1016 switch (instr->type) { 1017 case nir_instr_type_alu: 1018 print_alu_instr(nir_instr_as_alu(instr), state); 1019 break; 1020 1021 case nir_instr_type_deref: 1022 print_deref_instr(nir_instr_as_deref(instr), state); 1023 break; 1024 1025 case nir_instr_type_call: 1026 print_call_instr(nir_instr_as_call(instr), state); 1027 break; 1028 1029 case nir_instr_type_intrinsic: 1030 print_intrinsic_instr(nir_instr_as_intrinsic(instr), state); 1031 break; 1032 1033 case nir_instr_type_tex: 1034 print_tex_instr(nir_instr_as_tex(instr), state); 1035 break; 1036 1037 case nir_instr_type_load_const: 1038 print_load_const_instr(nir_instr_as_load_const(instr), state); 1039 break; 1040 1041 case nir_instr_type_jump: 1042 print_jump_instr(nir_instr_as_jump(instr), state); 1043 break; 1044 1045 case nir_instr_type_ssa_undef: 1046 print_ssa_undef_instr(nir_instr_as_ssa_undef(instr), state); 1047 break; 1048 1049 case nir_instr_type_phi: 1050 print_phi_instr(nir_instr_as_phi(instr), state); 1051 break; 1052 1053 case nir_instr_type_parallel_copy: 1054 print_parallel_copy_instr(nir_instr_as_parallel_copy(instr), state); 1055 break; 1056 1057 default: 1058 unreachable("Invalid instruction type"); 1059 break; 1060 } 1061} 1062 1063static int 1064compare_block_index(const void *p1, const void *p2) 1065{ 1066 const nir_block *block1 = *((const nir_block **) p1); 1067 const nir_block *block2 = *((const nir_block **) p2); 1068 1069 return (int) block1->index - (int) block2->index; 1070} 1071 1072static void print_cf_node(nir_cf_node *node, print_state *state, 1073 unsigned tabs); 1074 1075static void 1076print_block(nir_block *block, print_state *state, unsigned tabs) 1077{ 1078 FILE *fp = state->fp; 1079 1080 print_tabs(tabs, fp); 1081 fprintf(fp, "block block_%u:\n", block->index); 1082 1083 /* sort the predecessors by index so we consistently print the same thing */ 1084 1085 nir_block **preds = 1086 malloc(block->predecessors->entries * sizeof(nir_block *)); 1087 1088 unsigned i = 0; 1089 set_foreach(block->predecessors, entry) { 1090 preds[i++] = (nir_block *) entry->key; 1091 } 1092 1093 qsort(preds, block->predecessors->entries, sizeof(nir_block *), 1094 compare_block_index); 1095 1096 print_tabs(tabs, fp); 1097 fprintf(fp, "/* preds: "); 1098 for (unsigned i = 0; i < block->predecessors->entries; i++) { 1099 fprintf(fp, "block_%u ", preds[i]->index); 1100 } 1101 fprintf(fp, "*/\n"); 1102 1103 free(preds); 1104 1105 nir_foreach_instr(instr, block) { 1106 print_instr(instr, state, tabs); 1107 fprintf(fp, "\n"); 1108 print_annotation(state, instr); 1109 } 1110 1111 print_tabs(tabs, fp); 1112 fprintf(fp, "/* succs: "); 1113 for (unsigned i = 0; i < 2; i++) 1114 if (block->successors[i]) { 1115 fprintf(fp, "block_%u ", block->successors[i]->index); 1116 } 1117 fprintf(fp, "*/\n"); 1118} 1119 1120static void 1121print_if(nir_if *if_stmt, print_state *state, unsigned tabs) 1122{ 1123 FILE *fp = state->fp; 1124 1125 print_tabs(tabs, fp); 1126 fprintf(fp, "if "); 1127 print_src(&if_stmt->condition, state); 1128 fprintf(fp, " {\n"); 1129 foreach_list_typed(nir_cf_node, node, node, &if_stmt->then_list) { 1130 print_cf_node(node, state, tabs + 1); 1131 } 1132 print_tabs(tabs, fp); 1133 fprintf(fp, "} else {\n"); 1134 foreach_list_typed(nir_cf_node, node, node, &if_stmt->else_list) { 1135 print_cf_node(node, state, tabs + 1); 1136 } 1137 print_tabs(tabs, fp); 1138 fprintf(fp, "}\n"); 1139} 1140 1141static void 1142print_loop(nir_loop *loop, print_state *state, unsigned tabs) 1143{ 1144 FILE *fp = state->fp; 1145 1146 print_tabs(tabs, fp); 1147 fprintf(fp, "loop {\n"); 1148 foreach_list_typed(nir_cf_node, node, node, &loop->body) { 1149 print_cf_node(node, state, tabs + 1); 1150 } 1151 print_tabs(tabs, fp); 1152 fprintf(fp, "}\n"); 1153} 1154 1155static void 1156print_cf_node(nir_cf_node *node, print_state *state, unsigned int tabs) 1157{ 1158 switch (node->type) { 1159 case nir_cf_node_block: 1160 print_block(nir_cf_node_as_block(node), state, tabs); 1161 break; 1162 1163 case nir_cf_node_if: 1164 print_if(nir_cf_node_as_if(node), state, tabs); 1165 break; 1166 1167 case nir_cf_node_loop: 1168 print_loop(nir_cf_node_as_loop(node), state, tabs); 1169 break; 1170 1171 default: 1172 unreachable("Invalid CFG node type"); 1173 } 1174} 1175 1176static void 1177print_function_impl(nir_function_impl *impl, print_state *state) 1178{ 1179 FILE *fp = state->fp; 1180 1181 fprintf(fp, "\nimpl %s ", impl->function->name); 1182 1183 fprintf(fp, "{\n"); 1184 1185 nir_foreach_variable(var, &impl->locals) { 1186 fprintf(fp, "\t"); 1187 print_var_decl(var, state); 1188 } 1189 1190 foreach_list_typed(nir_register, reg, node, &impl->registers) { 1191 fprintf(fp, "\t"); 1192 print_register_decl(reg, state); 1193 } 1194 1195 nir_index_blocks(impl); 1196 1197 foreach_list_typed(nir_cf_node, node, node, &impl->body) { 1198 print_cf_node(node, state, 1); 1199 } 1200 1201 fprintf(fp, "\tblock block_%u:\n}\n\n", impl->end_block->index); 1202} 1203 1204static void 1205print_function(nir_function *function, print_state *state) 1206{ 1207 FILE *fp = state->fp; 1208 1209 fprintf(fp, "decl_function %s (%d params)", function->name, 1210 function->num_params); 1211 1212 fprintf(fp, "\n"); 1213 1214 if (function->impl != NULL) { 1215 print_function_impl(function->impl, state); 1216 return; 1217 } 1218} 1219 1220static void 1221init_print_state(print_state *state, nir_shader *shader, FILE *fp) 1222{ 1223 state->fp = fp; 1224 state->shader = shader; 1225 state->ht = _mesa_hash_table_create(NULL, _mesa_hash_pointer, 1226 _mesa_key_pointer_equal); 1227 state->syms = _mesa_set_create(NULL, _mesa_key_hash_string, 1228 _mesa_key_string_equal); 1229 state->index = 0; 1230} 1231 1232static void 1233destroy_print_state(print_state *state) 1234{ 1235 _mesa_hash_table_destroy(state->ht, NULL); 1236 _mesa_set_destroy(state->syms, NULL); 1237} 1238 1239void 1240nir_print_shader_annotated(nir_shader *shader, FILE *fp, 1241 struct hash_table *annotations) 1242{ 1243 print_state state; 1244 init_print_state(&state, shader, fp); 1245 1246 state.annotations = annotations; 1247 1248 fprintf(fp, "shader: %s\n", gl_shader_stage_name(shader->info.stage)); 1249 1250 if (shader->info.name) 1251 fprintf(fp, "name: %s\n", shader->info.name); 1252 1253 if (shader->info.label) 1254 fprintf(fp, "label: %s\n", shader->info.label); 1255 1256 switch (shader->info.stage) { 1257 case MESA_SHADER_COMPUTE: 1258 fprintf(fp, "local-size: %u, %u, %u%s\n", 1259 shader->info.cs.local_size[0], 1260 shader->info.cs.local_size[1], 1261 shader->info.cs.local_size[2], 1262 shader->info.cs.local_size_variable ? " (variable)" : ""); 1263 fprintf(fp, "shared-size: %u\n", shader->info.cs.shared_size); 1264 break; 1265 default: 1266 break; 1267 } 1268 1269 fprintf(fp, "inputs: %u\n", shader->num_inputs); 1270 fprintf(fp, "outputs: %u\n", shader->num_outputs); 1271 fprintf(fp, "uniforms: %u\n", shader->num_uniforms); 1272 fprintf(fp, "shared: %u\n", shader->num_shared); 1273 1274 nir_foreach_variable(var, &shader->uniforms) { 1275 print_var_decl(var, &state); 1276 } 1277 1278 nir_foreach_variable(var, &shader->inputs) { 1279 print_var_decl(var, &state); 1280 } 1281 1282 nir_foreach_variable(var, &shader->outputs) { 1283 print_var_decl(var, &state); 1284 } 1285 1286 nir_foreach_variable(var, &shader->shared) { 1287 print_var_decl(var, &state); 1288 } 1289 1290 nir_foreach_variable(var, &shader->globals) { 1291 print_var_decl(var, &state); 1292 } 1293 1294 nir_foreach_variable(var, &shader->system_values) { 1295 print_var_decl(var, &state); 1296 } 1297 1298 foreach_list_typed(nir_register, reg, node, &shader->registers) { 1299 print_register_decl(reg, &state); 1300 } 1301 1302 foreach_list_typed(nir_function, func, node, &shader->functions) { 1303 print_function(func, &state); 1304 } 1305 1306 destroy_print_state(&state); 1307} 1308 1309void 1310nir_print_shader(nir_shader *shader, FILE *fp) 1311{ 1312 nir_print_shader_annotated(shader, fp, NULL); 1313} 1314 1315void 1316nir_print_instr(const nir_instr *instr, FILE *fp) 1317{ 1318 print_state state = { 1319 .fp = fp, 1320 }; 1321 print_instr(instr, &state, 0); 1322 1323} 1324