xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/r300/compiler/radeon_program_print.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright 2009 Nicolai Hähnle <[email protected]>
3  * SPDX-License-Identifier: MIT
4  */
5 
6 #include "radeon_program.h"
7 #include "radeon_compiler_util.h"
8 
9 #include <stdio.h>
10 
textarget_to_string(rc_texture_target target)11 static const char * textarget_to_string(rc_texture_target target)
12 {
13 	switch(target) {
14 	case RC_TEXTURE_2D_ARRAY: return "2D_ARRAY";
15 	case RC_TEXTURE_1D_ARRAY: return "1D_ARRAY";
16 	case RC_TEXTURE_CUBE: return "CUBE";
17 	case RC_TEXTURE_3D: return "3D";
18 	case RC_TEXTURE_RECT: return "RECT";
19 	case RC_TEXTURE_2D: return "2D";
20 	case RC_TEXTURE_1D: return "1D";
21 	default: return "BAD_TEXTURE_TARGET";
22 	}
23 }
24 
presubtract_op_to_string(rc_presubtract_op op)25 static const char * presubtract_op_to_string(rc_presubtract_op op)
26 {
27 	switch(op) {
28 	case RC_PRESUB_NONE:
29 		return "NONE";
30 	case RC_PRESUB_BIAS:
31 		return "(1 - 2 * src0)";
32 	case RC_PRESUB_SUB:
33 		return "(src1 - src0)";
34 	case RC_PRESUB_ADD:
35 		return "(src1 + src0)";
36 	case RC_PRESUB_INV:
37 		return "(1 - src0)";
38 	default:
39 		return "BAD_PRESUBTRACT_OP";
40 	}
41 }
42 
print_omod_op(FILE * f,rc_omod_op op)43 static void print_omod_op(FILE * f, rc_omod_op op)
44 {
45 	const char * omod_str;
46 
47 	switch(op) {
48 	case RC_OMOD_MUL_1:
49 	case RC_OMOD_DISABLE:
50 		return;
51 	case RC_OMOD_MUL_2:
52 		omod_str = "* 2";
53 		break;
54 	case RC_OMOD_MUL_4:
55 		omod_str = "* 4";
56 		break;
57 	case RC_OMOD_MUL_8:
58 		omod_str = "* 8";
59 		break;
60 	case RC_OMOD_DIV_2:
61 		omod_str = "/ 2";
62 		break;
63 	case RC_OMOD_DIV_4:
64 		omod_str = "/ 4";
65 		break;
66 	case RC_OMOD_DIV_8:
67 		omod_str = "/ 8";
68 		break;
69 	default:
70 		return;
71 	}
72 	fprintf(f, " %s", omod_str);
73 }
74 
rc_print_comparefunc(FILE * f,const char * lhs,rc_compare_func func,const char * rhs)75 static void rc_print_comparefunc(FILE * f, const char * lhs, rc_compare_func func, const char * rhs)
76 {
77 	if (func == RC_COMPARE_FUNC_NEVER) {
78 		fprintf(f, "false");
79 	} else if (func == RC_COMPARE_FUNC_ALWAYS) {
80 		fprintf(f, "true");
81 	} else {
82 		const char * op;
83 		switch(func) {
84 		case RC_COMPARE_FUNC_LESS: op = "<"; break;
85 		case RC_COMPARE_FUNC_EQUAL: op = "=="; break;
86 		case RC_COMPARE_FUNC_LEQUAL: op = "<="; break;
87 		case RC_COMPARE_FUNC_GREATER: op = ">"; break;
88 		case RC_COMPARE_FUNC_NOTEQUAL: op = "!="; break;
89 		case RC_COMPARE_FUNC_GEQUAL: op = ">="; break;
90 		default: op = "???"; break;
91 		}
92 		fprintf(f, "%s %s %s", lhs, op, rhs);
93 	}
94 }
95 
rc_print_inline_float(FILE * f,int index)96 static void rc_print_inline_float(FILE * f, int index)
97 {
98 	fprintf(f, "%f (0x%x)", rc_inline_to_float(index), index);
99 }
100 
rc_print_register(FILE * f,rc_register_file file,int index,unsigned int reladdr)101 static void rc_print_register(FILE * f, rc_register_file file, int index, unsigned int reladdr)
102 {
103 	if (file == RC_FILE_NONE) {
104 		fprintf(f, "none");
105 	} else if (file == RC_FILE_SPECIAL) {
106 		switch(index) {
107 		case RC_SPECIAL_ALU_RESULT: fprintf(f, "aluresult"); break;
108 		default: fprintf(f, "special[%i]", index); break;
109 		}
110 	} else if (file == RC_FILE_INLINE) {
111 		rc_print_inline_float(f, index);
112 	} else {
113 		const char * filename;
114 		switch(file) {
115 		case RC_FILE_TEMPORARY: filename = "temp"; break;
116 		case RC_FILE_INPUT: filename = "input"; break;
117 		case RC_FILE_OUTPUT: filename = "output"; break;
118 		case RC_FILE_ADDRESS: filename = "addr"; break;
119 		case RC_FILE_CONSTANT: filename = "const"; break;
120 		default: filename = "BAD FILE"; break;
121 		}
122 		fprintf(f, "%s[%i%s]", filename, index, reladdr ? " + addr[0]" : "");
123 	}
124 }
125 
rc_print_mask(FILE * f,unsigned int mask)126 static void rc_print_mask(FILE * f, unsigned int mask)
127 {
128 	if (mask & RC_MASK_X) fprintf(f, "x");
129 	if (mask & RC_MASK_Y) fprintf(f, "y");
130 	if (mask & RC_MASK_Z) fprintf(f, "z");
131 	if (mask & RC_MASK_W) fprintf(f, "w");
132 }
133 
rc_print_dst_register(FILE * f,struct rc_dst_register dst)134 static void rc_print_dst_register(FILE * f, struct rc_dst_register dst)
135 {
136 	rc_print_register(f, dst.File, dst.Index, 0);
137 	if (dst.WriteMask != RC_MASK_XYZW) {
138 		fprintf(f, ".");
139 		rc_print_mask(f, dst.WriteMask);
140 	}
141 }
142 
rc_swizzle_char(unsigned int swz)143 static char rc_swizzle_char(unsigned int swz)
144 {
145 	switch(swz) {
146 	case RC_SWIZZLE_X: return 'x';
147 	case RC_SWIZZLE_Y: return 'y';
148 	case RC_SWIZZLE_Z: return 'z';
149 	case RC_SWIZZLE_W: return 'w';
150 	case RC_SWIZZLE_ZERO: return '0';
151 	case RC_SWIZZLE_ONE: return '1';
152 	case RC_SWIZZLE_HALF: return 'H';
153 	case RC_SWIZZLE_UNUSED: return '_';
154 	}
155 	fprintf(stderr, "bad swz: %u\n", swz);
156 	return '?';
157 }
158 
rc_print_swizzle(FILE * f,unsigned int swizzle,unsigned int negate)159 static void rc_print_swizzle(FILE * f, unsigned int swizzle, unsigned int negate)
160 {
161 	unsigned int comp;
162 	for(comp = 0; comp < 4; ++comp) {
163 		rc_swizzle swz = GET_SWZ(swizzle, comp);
164 		if (GET_BIT(negate, comp))
165 			fprintf(f, "-");
166 		fprintf(f, "%c", rc_swizzle_char(swz));
167 	}
168 }
169 
rc_print_presub_instruction(FILE * f,struct rc_presub_instruction inst)170 static void rc_print_presub_instruction(FILE * f,
171 					struct rc_presub_instruction inst)
172 {
173 	fprintf(f,"(");
174 	switch(inst.Opcode){
175 	case RC_PRESUB_BIAS:
176 		fprintf(f, "1 - 2 * ");
177 		rc_print_register(f, inst.SrcReg[0].File,
178 				inst.SrcReg[0].Index,inst.SrcReg[0].RelAddr);
179 		break;
180 	case RC_PRESUB_SUB:
181 		rc_print_register(f, inst.SrcReg[1].File,
182 				inst.SrcReg[1].Index,inst.SrcReg[1].RelAddr);
183 		fprintf(f, " - ");
184 		rc_print_register(f, inst.SrcReg[0].File,
185 				inst.SrcReg[0].Index,inst.SrcReg[0].RelAddr);
186 		break;
187 	case RC_PRESUB_ADD:
188 		rc_print_register(f, inst.SrcReg[1].File,
189 				inst.SrcReg[1].Index,inst.SrcReg[1].RelAddr);
190 		fprintf(f, " + ");
191 		rc_print_register(f, inst.SrcReg[0].File,
192 				inst.SrcReg[0].Index,inst.SrcReg[0].RelAddr);
193 		break;
194 	case RC_PRESUB_INV:
195 		fprintf(f, "1 - ");
196 		rc_print_register(f, inst.SrcReg[0].File,
197 				inst.SrcReg[0].Index,inst.SrcReg[0].RelAddr);
198 		break;
199 	default:
200 		break;
201 	}
202 	fprintf(f, ")");
203 }
204 
rc_print_src_register(FILE * f,struct rc_instruction * inst,struct rc_src_register src)205 static void rc_print_src_register(FILE * f, struct rc_instruction * inst,
206 						struct rc_src_register src)
207 {
208 	int trivial_negate = (src.Negate == RC_MASK_NONE || src.Negate == RC_MASK_XYZW);
209 
210 	if (src.Negate == RC_MASK_XYZW)
211 		fprintf(f, "-");
212 	if (src.Abs)
213 		fprintf(f, "|");
214 
215 	if(src.File == RC_FILE_PRESUB)
216 		rc_print_presub_instruction(f, inst->U.I.PreSub);
217 	else
218 		rc_print_register(f, src.File, src.Index, src.RelAddr);
219 
220 	if (src.Abs && !trivial_negate)
221 		fprintf(f, "|");
222 
223 	if (src.Swizzle != RC_SWIZZLE_XYZW || !trivial_negate) {
224 		fprintf(f, ".");
225 		rc_print_swizzle(f, src.Swizzle, trivial_negate ? 0 : src.Negate);
226 	}
227 
228 	if (src.Abs && trivial_negate)
229 		fprintf(f, "|");
230 }
231 
update_branch_depth(rc_opcode opcode,unsigned * branch_depth)232 static unsigned update_branch_depth(rc_opcode opcode, unsigned *branch_depth)
233 {
234 	switch (opcode) {
235 	case RC_OPCODE_IF:
236 	case RC_OPCODE_BGNLOOP:
237 		return (*branch_depth)++ * 2;
238 
239 	case RC_OPCODE_ENDIF:
240 	case RC_OPCODE_ENDLOOP:
241 		assert(*branch_depth > 0);
242 		return --(*branch_depth) * 2;
243 
244 	case RC_OPCODE_ELSE:
245 		assert(*branch_depth > 0);
246 		return (*branch_depth - 1) * 2;
247 
248 	default:
249 		return *branch_depth * 2;
250 	}
251 }
252 
rc_print_normal_instruction(FILE * f,struct rc_instruction * inst,unsigned * branch_depth)253 static void rc_print_normal_instruction(FILE * f, struct rc_instruction * inst, unsigned *branch_depth)
254 {
255 	const struct rc_opcode_info * opcode = rc_get_opcode_info(inst->U.I.Opcode);
256 	unsigned int reg;
257 	unsigned spaces = update_branch_depth(inst->U.I.Opcode, branch_depth);
258 
259 	for (unsigned i = 0; i < spaces; i++)
260 		fprintf(f, " ");
261 
262 	fprintf(f, "%s", opcode->Name);
263 
264 	switch(inst->U.I.SaturateMode) {
265 	case RC_SATURATE_NONE: break;
266 	case RC_SATURATE_ZERO_ONE: fprintf(f, "_SAT"); break;
267 	case RC_SATURATE_MINUS_PLUS_ONE: fprintf(f, "_SAT2"); break;
268 	default: fprintf(f, "_BAD_SAT"); break;
269 	}
270 
271 	if (opcode->HasDstReg) {
272 		fprintf(f, " ");
273 		rc_print_dst_register(f, inst->U.I.DstReg);
274 		print_omod_op(f, inst->U.I.Omod);
275 		if (opcode->NumSrcRegs)
276 			fprintf(f, ",");
277 	}
278 
279 	for(reg = 0; reg < opcode->NumSrcRegs; ++reg) {
280 		if (reg > 0)
281 			fprintf(f, ",");
282 		fprintf(f, " ");
283 		rc_print_src_register(f, inst, inst->U.I.SrcReg[reg]);
284 	}
285 
286 	if (opcode->HasTexture) {
287 		fprintf(f, ", %s%s[%u]%s%s",
288 			textarget_to_string(inst->U.I.TexSrcTarget),
289 			inst->U.I.TexShadow ? "SHADOW" : "",
290 			inst->U.I.TexSrcUnit,
291 			inst->U.I.TexSemWait ? " SEM_WAIT" : "",
292 			inst->U.I.TexSemAcquire ? " SEM_ACQUIRE" : "");
293 	}
294 
295 	fprintf(f, ";");
296 
297 	if (inst->U.I.WriteALUResult) {
298 		fprintf(f, " [aluresult = (");
299 		rc_print_comparefunc(f,
300 			(inst->U.I.WriteALUResult == RC_ALURESULT_X) ? "x" : "w",
301 			inst->U.I.ALUResultCompare, "0");
302 		fprintf(f, ")]");
303 	}
304 
305 	if (inst->U.I.DstReg.Pred == RC_PRED_SET) {
306 		fprintf(f, " PRED_SET");
307 	} else if (inst->U.I.DstReg.Pred == RC_PRED_INV) {
308 		fprintf(f, " PRED_INV");
309 	}
310 
311 	fprintf(f, "\n");
312 }
313 
rc_print_pair_instruction(FILE * f,struct rc_instruction * fullinst,unsigned * branch_depth)314 static void rc_print_pair_instruction(FILE * f, struct rc_instruction * fullinst, unsigned *branch_depth)
315 {
316 	struct rc_pair_instruction * inst = &fullinst->U.P;
317 	int printedsrc = 0;
318 	unsigned spaces = update_branch_depth(inst->RGB.Opcode != RC_OPCODE_NOP ?
319 					      inst->RGB.Opcode : inst->Alpha.Opcode, branch_depth);
320 
321 	for (unsigned i = 0; i < spaces; i++)
322 		fprintf(f, " ");
323 
324 	for(unsigned int src = 0; src < 3; ++src) {
325 		if (inst->RGB.Src[src].Used) {
326 			if (printedsrc)
327 				fprintf(f, ", ");
328 			fprintf(f, "src%i.xyz = ", src);
329 			rc_print_register(f, inst->RGB.Src[src].File, inst->RGB.Src[src].Index, 0);
330 			printedsrc = 1;
331 		}
332 		if (inst->Alpha.Src[src].Used) {
333 			if (printedsrc)
334 				fprintf(f, ", ");
335 			fprintf(f, "src%i.w = ", src);
336 			rc_print_register(f, inst->Alpha.Src[src].File, inst->Alpha.Src[src].Index, 0);
337 			printedsrc = 1;
338 		}
339 	}
340 	if(inst->RGB.Src[RC_PAIR_PRESUB_SRC].Used) {
341 		fprintf(f, ", srcp.xyz = %s",
342 			presubtract_op_to_string(
343 					inst->RGB.Src[RC_PAIR_PRESUB_SRC].Index));
344 	}
345 	if(inst->Alpha.Src[RC_PAIR_PRESUB_SRC].Used) {
346 		fprintf(f, ", srcp.w = %s",
347 			presubtract_op_to_string(
348 					inst->Alpha.Src[RC_PAIR_PRESUB_SRC].Index));
349 	}
350 	if (inst->SemWait) {
351 		fprintf(f, " SEM_WAIT");
352 	}
353 	fprintf(f, "\n");
354 
355 	if (inst->RGB.Opcode != RC_OPCODE_NOP) {
356 		const struct rc_opcode_info * opcode = rc_get_opcode_info(inst->RGB.Opcode);
357 
358 		for (unsigned i = 0; i < spaces; i++)
359 			fprintf(f, " ");
360 
361 		fprintf(f, "     %s%s", opcode->Name, inst->RGB.Saturate ? "_SAT" : "");
362 		if (inst->RGB.WriteMask)
363 			fprintf(f, " temp[%i].%s%s%s", inst->RGB.DestIndex,
364 				(inst->RGB.WriteMask & 1) ? "x" : "",
365 				(inst->RGB.WriteMask & 2) ? "y" : "",
366 				(inst->RGB.WriteMask & 4) ? "z" : "");
367 		if (inst->RGB.OutputWriteMask)
368 			fprintf(f, " color[%i].%s%s%s", inst->RGB.Target,
369 				(inst->RGB.OutputWriteMask & 1) ? "x" : "",
370 				(inst->RGB.OutputWriteMask & 2) ? "y" : "",
371 				(inst->RGB.OutputWriteMask & 4) ? "z" : "");
372 		if (inst->WriteALUResult == RC_ALURESULT_X)
373 			fprintf(f, " aluresult");
374 
375 		print_omod_op(f, inst->RGB.Omod);
376 
377 		for(unsigned int arg = 0; arg < opcode->NumSrcRegs; ++arg) {
378 			const char* abs = inst->RGB.Arg[arg].Abs ? "|" : "";
379 			const char* neg = inst->RGB.Arg[arg].Negate ? "-" : "";
380 			fprintf(f, ", %s%ssrc", neg, abs);
381 			if(inst->RGB.Arg[arg].Source == RC_PAIR_PRESUB_SRC)
382 				fprintf(f,"p");
383 			else
384 				fprintf(f,"%d", inst->RGB.Arg[arg].Source);
385 			fprintf(f,".%c%c%c%s",
386 				rc_swizzle_char(GET_SWZ(inst->RGB.Arg[arg].Swizzle, 0)),
387 				rc_swizzle_char(GET_SWZ(inst->RGB.Arg[arg].Swizzle, 1)),
388 				rc_swizzle_char(GET_SWZ(inst->RGB.Arg[arg].Swizzle, 2)),
389 				abs);
390 		}
391 		fprintf(f, "\n");
392 	}
393 
394 	if (inst->Alpha.Opcode != RC_OPCODE_NOP) {
395 		const struct rc_opcode_info * opcode = rc_get_opcode_info(inst->Alpha.Opcode);
396 
397 		for (unsigned i = 0; i < spaces; i++)
398 			fprintf(f, " ");
399 
400 		fprintf(f, "     %s%s", opcode->Name, inst->Alpha.Saturate ? "_SAT" : "");
401 		if (inst->Alpha.WriteMask)
402 			fprintf(f, " temp[%i].w", inst->Alpha.DestIndex);
403 		if (inst->Alpha.OutputWriteMask)
404 			fprintf(f, " color[%i].w", inst->Alpha.Target);
405 		if (inst->Alpha.DepthWriteMask)
406 			fprintf(f, " depth.w");
407 		if (inst->WriteALUResult == RC_ALURESULT_W)
408 			fprintf(f, " aluresult");
409 
410 		print_omod_op(f, inst->Alpha.Omod);
411 
412 		for(unsigned int arg = 0; arg < opcode->NumSrcRegs; ++arg) {
413 			const char* abs = inst->Alpha.Arg[arg].Abs ? "|" : "";
414 			const char* neg = inst->Alpha.Arg[arg].Negate ? "-" : "";
415 			fprintf(f, ", %s%ssrc", neg, abs);
416 			if(inst->Alpha.Arg[arg].Source == RC_PAIR_PRESUB_SRC)
417 				fprintf(f,"p");
418 			else
419 				fprintf(f,"%d", inst->Alpha.Arg[arg].Source);
420 			fprintf(f,".%c%s",
421 				rc_swizzle_char(GET_SWZ(inst->Alpha.Arg[arg].Swizzle, 0)), abs);
422 		}
423 		fprintf(f, "\n");
424 	}
425 
426 	if (inst->WriteALUResult) {
427 		for (unsigned i = 0; i < spaces; i++)
428 			fprintf(f, " ");
429 
430 		fprintf(f, "      [aluresult = (");
431 		rc_print_comparefunc(f, "result", inst->ALUResultCompare, "0");
432 		fprintf(f, ")]\n");
433 	}
434 }
435 
436 /**
437  * Print program to stderr, default options.
438  */
rc_print_program(const struct rc_program * prog)439 void rc_print_program(const struct rc_program *prog)
440 {
441 	unsigned int linenum = 0;
442 	unsigned branch_depth = 0;
443 	struct rc_instruction *inst;
444 
445 	fprintf(stderr, "# Radeon Compiler Program\n");
446 
447 	for(inst = prog->Instructions.Next; inst != &prog->Instructions; inst = inst->Next) {
448 		fprintf(stderr, "%3d: ", linenum);
449 
450 		if (inst->Type == RC_INSTRUCTION_PAIR)
451 			rc_print_pair_instruction(stderr, inst, &branch_depth);
452 		else
453 			rc_print_normal_instruction(stderr, inst, &branch_depth);
454 
455 		linenum++;
456 	}
457 }
458