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