summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/r600/sb/sb_bc_dump.cpp
blob: 0fc73c419a68139f108d0920a847303a59d7ed09 (plain)
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
/*
 * Copyright 2013 Vadim Girlin <vadimgirlin@gmail.com>
 *
 * 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
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, 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 NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS 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:
 *      Vadim Girlin
 */

#include "sb_bc.h"
#include "sb_shader.h"
#include "sb_pass.h"

namespace r600_sb {

static const char* chans = "xyzw01?_";

static const char* vec_bs[] = {
		"VEC_012", "VEC_021", "VEC_120", "VEC_102", "VEC_201", "VEC_210"
};

static const char* scl_bs[] = {
		"SCL_210", "SCL_122", "SCL_212", "SCL_221"
};


bool bc_dump::visit(cf_node& n, bool enter) {
	if (enter) {

		id = n.bc.id << 1;

		if ((n.bc.op_ptr->flags & CF_ALU) && n.bc.is_alu_extended()) {
			dump_dw(id, 2);
			id += 2;
			sblog << "\n";
		}

		dump_dw(id, 2);
		dump(n);

		if (n.bc.op_ptr->flags & CF_CLAUSE) {
			id = n.bc.addr << 1;
			new_group = 1;
		}
	}
	return true;
}

bool bc_dump::visit(alu_node& n, bool enter) {
	if (enter) {
		sblog << " ";
		dump_dw(id, 2);

		if (new_group) {
			sblog.print_w(++group_index, 5);
			sblog << " ";
		} else
			sblog << "      ";

		dump(n);
		id += 2;

		new_group = n.bc.last;
	} else {
		if (n.bc.last) {
			alu_group_node *g =
					static_cast<alu_group_node*>(n.get_alu_group_node());
			assert(g);
			for (unsigned k = 0; k < g->literals.size(); ++k) {
				sblog << " ";
				dump_dw(id, 1);
				id += 1;
				sblog << "\n";
			}

			id = (id + 1) & ~1u;
		}
	}

	return false;
}

bool bc_dump::visit(fetch_node& n, bool enter) {
	if (enter) {
		sblog << " ";
		dump_dw(id, 3);
		dump(n);
		id += 4;
	}
	return false;
}

static void fill_to(sb_ostringstream &s, int pos) {
	int l = s.str().length();
	if (l < pos)
		s << std::string(pos-l, ' ');
}

void bc_dump::dump(cf_node& n) {
	sb_ostringstream s;
	s << n.bc.op_ptr->name;

	if (n.bc.op_ptr->flags & CF_EXP) {
		static const char *exp_type[] = {"PIXEL", "POS  ", "PARAM"};

		fill_to(s, 18);
		s << " " << exp_type[n.bc.type] << " ";

		if (n.bc.burst_count) {
			sb_ostringstream s2;
			s2 << n.bc.array_base << "-" << n.bc.array_base + n.bc.burst_count;
			s.print_wl(s2.str(), 5);
			s << " R" << n.bc.rw_gpr << "-" <<
					n.bc.rw_gpr + n.bc.burst_count << ".";
		} else {
			s.print_wl(n.bc.array_base, 5);
			s << " R" << n.bc.rw_gpr << ".";
		}

		for (int k = 0; k < 4; ++k)
			s << chans[n.bc.sel[k]];

	} else if (n.bc.op_ptr->flags & CF_MEM) {
		static const char *exp_type[] = {"WRITE", "WRITE_IND", "WRITE_ACK",
				"WRITE_IND_ACK"};
		fill_to(s, 18);
		s << " " << exp_type[n.bc.type] << " ";
		s.print_wl(n.bc.array_base, 5);
		s << " R" << n.bc.rw_gpr << ".";
		for (int k = 0; k < 4; ++k)
			s << ((n.bc.comp_mask & (1 << k)) ? chans[k] : '_');

		if ((n.bc.op_ptr->flags & CF_RAT) && (n.bc.type & 1)) {
			s << ", @R" << n.bc.index_gpr << ".xyz";
		}
		if ((n.bc.op_ptr->flags & CF_MEM) && (n.bc.type & 1)) {
			s << ", @R" << n.bc.index_gpr << ".x";
		}

		s << "  ES:" << n.bc.elem_size;

	} else {

		if (n.bc.op_ptr->flags & CF_CLAUSE) {
			s << " " << n.bc.count+1;
		}

		s << " @" << (n.bc.addr << 1);

		if (n.bc.op_ptr->flags & CF_ALU) {
			static const char *index_mode[] = {"", " CF_INDEX_0", " CF_INDEX_1"};

			for (int k = 0; k < 4; ++k) {
				bc_kcache &kc = n.bc.kc[k];
				if (kc.mode) {
					s << " KC" << k << "[CB" << kc.bank << ":" <<
							(kc.addr << 4) << "-" <<
							(((kc.addr + kc.mode) << 4) - 1) << index_mode[kc.index_mode] << "]";
				}
			}
		}

		if (n.bc.cond)
			s << " CND:" << n.bc.cond;

		if (n.bc.pop_count)
			s << " POP:" << n.bc.pop_count;

		if (n.bc.count && (n.bc.op_ptr->flags & CF_EMIT))
			s << " STREAM" << n.bc.count;
	}

	if (!n.bc.barrier)
		s << "  NO_BARRIER";

	if (n.bc.valid_pixel_mode)
		s << "  VPM";

	if (n.bc.whole_quad_mode)
		s << "  WQM";

	if (n.bc.end_of_program)
		s << "  EOP";

	sblog << s.str() << "\n";
}


static void print_sel(sb_ostream &s, int sel, int rel, int index_mode,
                      int need_brackets) {
	if (rel && index_mode >= 5 && sel < 128)
		s << "G";
	if (rel || need_brackets) {
		s << "[";
	}
	s << sel;
	if (rel) {
		if (index_mode == 0 || index_mode == 6)
			s << "+AR";
		else if (index_mode == 4)
			s << "+AL";
	}
	if (rel || need_brackets) {
		s << "]";
	}
}

static void print_dst(sb_ostream &s, bc_alu &alu)
{
	unsigned sel = alu.dst_gpr;
	char reg_char = 'R';
	if (sel >= 128 - 4) { // clause temporary gpr
		sel -= 128 - 4;
		reg_char = 'T';
	}

	if (alu.write_mask || alu.op_ptr->src_count == 3) {
		s << reg_char;
		print_sel(s, sel, alu.dst_rel, alu.index_mode, 0);
	} else {
		s << "__";
	}
	s << ".";
	s << chans[alu.dst_chan];
}

static void print_src(sb_ostream &s, bc_alu &alu, unsigned idx)
{
	bc_alu_src *src = &alu.src[idx];
	unsigned sel = src->sel, need_sel = 1, need_chan = 1, need_brackets = 0;

	if (src->neg)
		s <<"-";
	if (src->abs)
		s <<"|";

	if (sel < 128 - 4) {
		s << "R";
	} else if (sel < 128) {
		s << "T";
		sel -= 128 - 4;
	} else if (sel < 160) {
		s << "KC0";
		need_brackets = 1;
		sel -= 128;
	} else if (sel < 192) {
		s << "KC1";
		need_brackets = 1;
		sel -= 160;
	} else if (sel >= 448) {
		s << "Param";
		sel -= 448;
	} else if (sel >= 288) {
		s << "KC3";
		need_brackets = 1;
		sel -= 288;
	} else if (sel >= 256) {
		s << "KC2";
		need_brackets = 1;
		sel -= 256;
	} else {
		need_sel = 0;
		need_chan = 0;
		switch (sel) {
		case ALU_SRC_PS:
			s << "PS";
			break;
		case ALU_SRC_PV:
			s << "PV";
			need_chan = 1;
			break;
		case ALU_SRC_LITERAL:
			s << "[0x";
			s.print_zw_hex(src->value.u, 8);
			s << " " << src->value.f << "]";
			need_chan = 1;
			break;
		case ALU_SRC_0_5:
			s << "0.5";
			break;
		case ALU_SRC_M_1_INT:
			s << "-1";
			break;
		case ALU_SRC_1_INT:
			s << "1";
			break;
		case ALU_SRC_1:
			s << "1.0";
			break;
		case ALU_SRC_0:
			s << "0";
			break;
		default:
			s << "??IMM_" <<  sel;
			break;
		}
	}

	if (need_sel)
		print_sel(s, sel, src->rel, alu.index_mode, need_brackets);

	if (need_chan) {
		s << "." << chans[src->chan];
	}

	if (src->abs)
		s << "|";
}
void bc_dump::dump(alu_node& n) {
	sb_ostringstream s;
	static const char *omod_str[] = {"","*2","*4","/2"};
	static const char *slots = "xyzwt";

	s << (n.bc.update_exec_mask ? "M" : " ");
	s << (n.bc.update_pred ? "P" : " ");
	s << " ";
	s << (n.bc.pred_sel>=2 ? (n.bc.pred_sel == 2 ? "0" : "1") : " ");
	s << " ";

	s << slots[n.bc.slot] << ": ";

	s << n.bc.op_ptr->name << omod_str[n.bc.omod] << (n.bc.clamp ? "_sat" : "");
	fill_to(s, 26);
	s << " ";

	print_dst(s, n.bc);
	for (int k = 0; k < n.bc.op_ptr->src_count; ++k) {
		s << (k ? ", " : ",  ");
		print_src(s, n.bc, k);
	}

	if (n.bc.bank_swizzle) {
		fill_to(s, 55);
		if (n.bc.slot == SLOT_TRANS)
			s << "  " << scl_bs[n.bc.bank_swizzle];
		else
			s << "  " << vec_bs[n.bc.bank_swizzle];
	}

	sblog << s.str() << "\n";
}

int bc_dump::init() {
	sb_ostringstream s;
	s << "===== SHADER #" << sh.id;

	if (sh.optimized)
		s << " OPT";

	s << " ";

	std::string target = std::string(" ") +
			sh.get_full_target_name() + " =====";

	while (s.str().length() + target.length() < 80)
		s << "=";

	s << target;

	sblog << "\n" << s.str() << "\n";

	s.clear();

	if (bc_data) {
		s << "===== " << ndw << " dw ===== " << sh.ngpr
				<< " gprs ===== " << sh.nstack << " stack ";
	}

	while (s.str().length() < 80)
		s << "=";

	sblog << s.str() << "\n";

	return 0;
}

int bc_dump::done() {
	sb_ostringstream s;
	s << "===== SHADER_END ";

	while (s.str().length() < 80)
		s << "=";

	sblog << s.str() << "\n\n";

	return 0;
}

bc_dump::bc_dump(shader& s, bytecode* bc)  :
	vpass(s), bc_data(), ndw(), id(),
	new_group(), group_index() {

	if (bc) {
		bc_data = bc->data();
		ndw = bc->ndw();
	}
}

void bc_dump::dump(fetch_node& n) {
	sb_ostringstream s;
	static const char * fetch_type[] = {"VERTEX", "INSTANCE", ""};

	s << n.bc.op_ptr->name;
	fill_to(s, 20);

	s << "R";
	print_sel(s, n.bc.dst_gpr, n.bc.dst_rel, INDEX_LOOP, 0);
	s << ".";
	for (int k = 0; k < 4; ++k)
		s << chans[n.bc.dst_sel[k]];
	s << ", ";

	s << "R";
	print_sel(s, n.bc.src_gpr, n.bc.src_rel, INDEX_LOOP, 0);
	s << ".";

	unsigned vtx = n.bc.op_ptr->flags & FF_VTX;
	unsigned num_src_comp = vtx ? ctx.is_cayman() ? 2 : 1 : 4;

	for (unsigned k = 0; k < num_src_comp; ++k)
		s << chans[n.bc.src_sel[k]];

	if (vtx && n.bc.offset[0]) {
		s << " + " << n.bc.offset[0] << "b ";
	}

	s << ",   RID:" << n.bc.resource_id;

	if (vtx) {
		s << "  " << fetch_type[n.bc.fetch_type];
		if (!ctx.is_cayman() && n.bc.mega_fetch_count)
			s << " MFC:" << n.bc.mega_fetch_count;
		if (n.bc.fetch_whole_quad)
			s << " FWQ";
		if (ctx.is_egcm() && n.bc.resource_index_mode)
			s << " RIM:SQ_CF_INDEX_" << n.bc.resource_index_mode;
		if (ctx.is_egcm() && n.bc.sampler_index_mode)
			s << " SID:SQ_CF_INDEX_" << n.bc.sampler_index_mode;

		s << " UCF:" << n.bc.use_const_fields
				<< " FMT(DTA:" << n.bc.data_format
				<< " NUM:" << n.bc.num_format_all
				<< " COMP:" << n.bc.format_comp_all
				<< " MODE:" << n.bc.srf_mode_all << ")";
	} else {
		s << ", SID:" << n.bc.sampler_id;
		if (n.bc.lod_bias)
			s << " LB:" << n.bc.lod_bias;
		s << " CT:";
		for (unsigned k = 0; k < 4; ++k)
			s << (n.bc.coord_type[k] ? "N" : "U");
		for (unsigned k = 0; k < 3; ++k)
			if (n.bc.offset[k])
				s << " O" << chans[k] << ":" << n.bc.offset[k];
		if (ctx.is_egcm() && n.bc.resource_index_mode)
			s << " RIM:SQ_CF_INDEX_" << n.bc.resource_index_mode;
		if (ctx.is_egcm() && n.bc.sampler_index_mode)
			s << " SID:SQ_CF_INDEX_" << n.bc.sampler_index_mode;
	}

	sblog << s.str() << "\n";
}

void bc_dump::dump_dw(unsigned dw_id, unsigned count) {
	if (!bc_data)
		return;

	assert(dw_id + count <= ndw);

	sblog.print_zw(dw_id, 4);
	sblog << "  ";
	while (count--) {
		sblog.print_zw_hex(bc_data[dw_id++], 8);
		sblog << " ";
	}
}

} // namespace r600_sb