gallium/tgsi: Add a helper for initializing ureg from a shader_info.
[mesa.git] / src / gallium / auxiliary / tgsi / tgsi_sanity.c
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "util/u_debug.h"
29 #include "util/u_memory.h"
30 #include "util/u_prim.h"
31 #include "cso_cache/cso_hash.h"
32 #include "tgsi_sanity.h"
33 #include "tgsi_info.h"
34 #include "tgsi_iterate.h"
35
36
37 DEBUG_GET_ONCE_BOOL_OPTION(print_sanity, "TGSI_PRINT_SANITY", FALSE)
38
39
40 typedef struct {
41 uint file : 28;
42 /* max 2 dimensions */
43 uint dimensions : 4;
44 uint indices[2];
45 } scan_register;
46
47 struct sanity_check_ctx
48 {
49 struct tgsi_iterate_context iter;
50 struct cso_hash regs_decl;
51 struct cso_hash regs_used;
52 struct cso_hash regs_ind_used;
53
54 uint num_imms;
55 uint num_instructions;
56 uint index_of_END;
57
58 uint errors;
59 uint warnings;
60 uint implied_array_size;
61 uint implied_out_array_size;
62
63 boolean print;
64 };
65
66 static inline unsigned
67 scan_register_key(const scan_register *reg)
68 {
69 unsigned key = reg->file;
70 key |= (reg->indices[0] << 4);
71 key |= (reg->indices[1] << 18);
72
73 return key;
74 }
75
76 static void
77 fill_scan_register1d(scan_register *reg,
78 uint file, uint index)
79 {
80 reg->file = file;
81 reg->dimensions = 1;
82 reg->indices[0] = index;
83 reg->indices[1] = 0;
84 }
85
86 static void
87 fill_scan_register2d(scan_register *reg,
88 uint file, uint index1, uint index2)
89 {
90 reg->file = file;
91 reg->dimensions = 2;
92 reg->indices[0] = index1;
93 reg->indices[1] = index2;
94 }
95
96 static void
97 scan_register_dst(scan_register *reg,
98 struct tgsi_full_dst_register *dst)
99 {
100 if (dst->Register.Dimension) {
101 /*FIXME: right now we don't support indirect
102 * multidimensional addressing */
103 fill_scan_register2d(reg,
104 dst->Register.File,
105 dst->Register.Index,
106 dst->Dimension.Index);
107 } else {
108 fill_scan_register1d(reg,
109 dst->Register.File,
110 dst->Register.Index);
111 }
112 }
113
114 static void
115 scan_register_src(scan_register *reg,
116 struct tgsi_full_src_register *src)
117 {
118 if (src->Register.Dimension) {
119 /*FIXME: right now we don't support indirect
120 * multidimensional addressing */
121 fill_scan_register2d(reg,
122 src->Register.File,
123 src->Register.Index,
124 src->Dimension.Index);
125 } else {
126 fill_scan_register1d(reg,
127 src->Register.File,
128 src->Register.Index);
129 }
130 }
131
132 static scan_register *
133 create_scan_register_src(struct tgsi_full_src_register *src)
134 {
135 scan_register *reg = MALLOC(sizeof(scan_register));
136 scan_register_src(reg, src);
137
138 return reg;
139 }
140
141 static scan_register *
142 create_scan_register_dst(struct tgsi_full_dst_register *dst)
143 {
144 scan_register *reg = MALLOC(sizeof(scan_register));
145 scan_register_dst(reg, dst);
146
147 return reg;
148 }
149
150 static void
151 report_error(
152 struct sanity_check_ctx *ctx,
153 const char *format,
154 ... )
155 {
156 va_list args;
157
158 if (!ctx->print)
159 return;
160
161 debug_printf( "Error : " );
162 va_start( args, format );
163 _debug_vprintf( format, args );
164 va_end( args );
165 debug_printf( "\n" );
166 ctx->errors++;
167 }
168
169 static void
170 report_warning(
171 struct sanity_check_ctx *ctx,
172 const char *format,
173 ... )
174 {
175 va_list args;
176
177 if (!ctx->print)
178 return;
179
180 debug_printf( "Warning: " );
181 va_start( args, format );
182 _debug_vprintf( format, args );
183 va_end( args );
184 debug_printf( "\n" );
185 ctx->warnings++;
186 }
187
188 static boolean
189 check_file_name(
190 struct sanity_check_ctx *ctx,
191 uint file )
192 {
193 if (file <= TGSI_FILE_NULL || file >= TGSI_FILE_COUNT) {
194 report_error( ctx, "(%u): Invalid register file name", file );
195 return FALSE;
196 }
197 return TRUE;
198 }
199
200 static boolean
201 is_register_declared(
202 struct sanity_check_ctx *ctx,
203 const scan_register *reg)
204 {
205 void *data = cso_hash_find_data_from_template(
206 &ctx->regs_decl, scan_register_key(reg),
207 (void*)reg, sizeof(scan_register));
208 return data ? TRUE : FALSE;
209 }
210
211 static boolean
212 is_any_register_declared(
213 struct sanity_check_ctx *ctx,
214 uint file )
215 {
216 struct cso_hash_iter iter =
217 cso_hash_first_node(&ctx->regs_decl);
218
219 while (!cso_hash_iter_is_null(iter)) {
220 scan_register *reg = (scan_register *)cso_hash_iter_data(iter);
221 if (reg->file == file)
222 return TRUE;
223 iter = cso_hash_iter_next(iter);
224 }
225
226 return FALSE;
227 }
228
229 static boolean
230 is_register_used(
231 struct sanity_check_ctx *ctx,
232 scan_register *reg)
233 {
234 void *data = cso_hash_find_data_from_template(
235 &ctx->regs_used, scan_register_key(reg),
236 reg, sizeof(scan_register));
237 return data ? TRUE : FALSE;
238 }
239
240
241 static boolean
242 is_ind_register_used(
243 struct sanity_check_ctx *ctx,
244 scan_register *reg)
245 {
246 return cso_hash_contains(&ctx->regs_ind_used, reg->file);
247 }
248
249 static const char *file_names[TGSI_FILE_COUNT] =
250 {
251 "NULL",
252 "CONST",
253 "IN",
254 "OUT",
255 "TEMP",
256 "SAMP",
257 "ADDR",
258 "IMM",
259 "SV",
260 "RES"
261 };
262
263 static boolean
264 check_register_usage(
265 struct sanity_check_ctx *ctx,
266 scan_register *reg,
267 const char *name,
268 boolean indirect_access )
269 {
270 if (!check_file_name( ctx, reg->file )) {
271 FREE(reg);
272 return FALSE;
273 }
274
275 if (indirect_access) {
276 /* Note that 'index' is an offset relative to the value of the
277 * address register. No range checking done here.*/
278 reg->indices[0] = 0;
279 reg->indices[1] = 0;
280 if (!is_any_register_declared( ctx, reg->file ))
281 report_error( ctx, "%s: Undeclared %s register", file_names[reg->file], name );
282 if (!is_ind_register_used(ctx, reg))
283 cso_hash_insert(&ctx->regs_ind_used, reg->file, reg);
284 else
285 FREE(reg);
286 }
287 else {
288 if (!is_register_declared( ctx, reg )) {
289 if (reg->dimensions == 2) {
290 report_error( ctx, "%s[%d][%d]: Undeclared %s register", file_names[reg->file],
291 reg->indices[0], reg->indices[1], name );
292 }
293 else {
294 report_error( ctx, "%s[%d]: Undeclared %s register", file_names[reg->file],
295 reg->indices[0], name );
296 }
297 }
298 if (!is_register_used( ctx, reg ))
299 cso_hash_insert(&ctx->regs_used, scan_register_key(reg), reg);
300 else
301 FREE(reg);
302 }
303 return TRUE;
304 }
305
306 static boolean
307 iter_instruction(
308 struct tgsi_iterate_context *iter,
309 struct tgsi_full_instruction *inst )
310 {
311 struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter;
312 const struct tgsi_opcode_info *info;
313 uint i;
314
315 if (inst->Instruction.Opcode == TGSI_OPCODE_END) {
316 if (ctx->index_of_END != ~0u) {
317 report_error( ctx, "Too many END instructions" );
318 }
319 ctx->index_of_END = ctx->num_instructions;
320 }
321
322 info = tgsi_get_opcode_info( inst->Instruction.Opcode );
323 if (!info) {
324 report_error( ctx, "(%u): Invalid instruction opcode", inst->Instruction.Opcode );
325 return TRUE;
326 }
327
328 if (info->num_dst != inst->Instruction.NumDstRegs) {
329 report_error( ctx, "%s: Invalid number of destination operands, should be %u",
330 tgsi_get_opcode_name(inst->Instruction.Opcode), info->num_dst );
331 }
332 if (info->num_src != inst->Instruction.NumSrcRegs) {
333 report_error( ctx, "%s: Invalid number of source operands, should be %u",
334 tgsi_get_opcode_name(inst->Instruction.Opcode), info->num_src );
335 }
336
337 /* Check destination and source registers' validity.
338 * Mark the registers as used.
339 */
340 for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
341 scan_register *reg = create_scan_register_dst(&inst->Dst[i]);
342 check_register_usage(
343 ctx,
344 reg,
345 "destination",
346 FALSE );
347 if (!inst->Dst[i].Register.WriteMask) {
348 report_error(ctx, "Destination register has empty writemask");
349 }
350 }
351 for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
352 scan_register *reg = create_scan_register_src(&inst->Src[i]);
353 check_register_usage(
354 ctx,
355 reg,
356 "source",
357 (boolean)inst->Src[i].Register.Indirect );
358 if (inst->Src[i].Register.Indirect) {
359 scan_register *ind_reg = MALLOC(sizeof(scan_register));
360
361 fill_scan_register1d(ind_reg,
362 inst->Src[i].Indirect.File,
363 inst->Src[i].Indirect.Index);
364 check_register_usage(
365 ctx,
366 ind_reg,
367 "indirect",
368 FALSE );
369 }
370 }
371
372 ctx->num_instructions++;
373
374 return TRUE;
375 }
376
377 static void
378 check_and_declare(struct sanity_check_ctx *ctx,
379 scan_register *reg)
380 {
381 if (is_register_declared( ctx, reg))
382 report_error( ctx, "%s[%u]: The same register declared more than once",
383 file_names[reg->file], reg->indices[0] );
384 cso_hash_insert(&ctx->regs_decl,
385 scan_register_key(reg),
386 reg);
387 }
388
389
390 static boolean
391 iter_declaration(
392 struct tgsi_iterate_context *iter,
393 struct tgsi_full_declaration *decl )
394 {
395 struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter;
396 uint file;
397 uint i;
398
399 /* No declarations allowed after the first instruction.
400 */
401 if (ctx->num_instructions > 0)
402 report_error( ctx, "Instruction expected but declaration found" );
403
404 /* Check registers' validity.
405 * Mark the registers as declared.
406 */
407 file = decl->Declaration.File;
408 if (!check_file_name( ctx, file ))
409 return TRUE;
410 for (i = decl->Range.First; i <= decl->Range.Last; i++) {
411 /* declared TGSI_FILE_INPUT's for geometry and tessellation
412 * have an implied second dimension */
413 uint processor = ctx->iter.processor.Processor;
414 uint patch = decl->Semantic.Name == TGSI_SEMANTIC_PATCH ||
415 decl->Semantic.Name == TGSI_SEMANTIC_TESSOUTER ||
416 decl->Semantic.Name == TGSI_SEMANTIC_TESSINNER;
417 if (file == TGSI_FILE_INPUT && !patch && (
418 processor == PIPE_SHADER_GEOMETRY ||
419 processor == PIPE_SHADER_TESS_CTRL ||
420 processor == PIPE_SHADER_TESS_EVAL)) {
421 uint vert;
422 for (vert = 0; vert < ctx->implied_array_size; ++vert) {
423 scan_register *reg = MALLOC(sizeof(scan_register));
424 fill_scan_register2d(reg, file, i, vert);
425 check_and_declare(ctx, reg);
426 }
427 } else if (file == TGSI_FILE_OUTPUT && !patch &&
428 processor == PIPE_SHADER_TESS_CTRL) {
429 uint vert;
430 for (vert = 0; vert < ctx->implied_out_array_size; ++vert) {
431 scan_register *reg = MALLOC(sizeof(scan_register));
432 fill_scan_register2d(reg, file, i, vert);
433 check_and_declare(ctx, reg);
434 }
435 } else {
436 scan_register *reg = MALLOC(sizeof(scan_register));
437 if (decl->Declaration.Dimension) {
438 fill_scan_register2d(reg, file, i, decl->Dim.Index2D);
439 } else {
440 fill_scan_register1d(reg, file, i);
441 }
442 check_and_declare(ctx, reg);
443 }
444 }
445
446 return TRUE;
447 }
448
449 static boolean
450 iter_immediate(
451 struct tgsi_iterate_context *iter,
452 struct tgsi_full_immediate *imm )
453 {
454 struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter;
455 scan_register *reg;
456
457 /* No immediates allowed after the first instruction.
458 */
459 if (ctx->num_instructions > 0)
460 report_error( ctx, "Instruction expected but immediate found" );
461
462 /* Mark the register as declared.
463 */
464 reg = MALLOC(sizeof(scan_register));
465 fill_scan_register1d(reg, TGSI_FILE_IMMEDIATE, ctx->num_imms);
466 cso_hash_insert(&ctx->regs_decl, scan_register_key(reg), reg);
467 ctx->num_imms++;
468
469 /* Check data type validity.
470 */
471 if (imm->Immediate.DataType != TGSI_IMM_FLOAT32 &&
472 imm->Immediate.DataType != TGSI_IMM_UINT32 &&
473 imm->Immediate.DataType != TGSI_IMM_INT32) {
474 report_error( ctx, "(%u): Invalid immediate data type", imm->Immediate.DataType );
475 return TRUE;
476 }
477
478 return TRUE;
479 }
480
481
482 static boolean
483 iter_property(
484 struct tgsi_iterate_context *iter,
485 struct tgsi_full_property *prop )
486 {
487 struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter;
488
489 if (iter->processor.Processor == PIPE_SHADER_GEOMETRY &&
490 prop->Property.PropertyName == TGSI_PROPERTY_GS_INPUT_PRIM) {
491 ctx->implied_array_size = u_vertices_per_prim(prop->u[0].Data);
492 }
493 if (iter->processor.Processor == PIPE_SHADER_TESS_CTRL &&
494 prop->Property.PropertyName == TGSI_PROPERTY_TCS_VERTICES_OUT)
495 ctx->implied_out_array_size = prop->u[0].Data;
496 return TRUE;
497 }
498
499 static boolean
500 prolog(struct tgsi_iterate_context *iter)
501 {
502 struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter;
503 if (iter->processor.Processor == PIPE_SHADER_TESS_CTRL ||
504 iter->processor.Processor == PIPE_SHADER_TESS_EVAL)
505 ctx->implied_array_size = 32;
506 return TRUE;
507 }
508
509 static boolean
510 epilog(
511 struct tgsi_iterate_context *iter )
512 {
513 struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter;
514
515 /* There must be an END instruction somewhere.
516 */
517 if (ctx->index_of_END == ~0u) {
518 report_error( ctx, "Missing END instruction" );
519 }
520
521 /* Check if all declared registers were used.
522 */
523 {
524 struct cso_hash_iter iter =
525 cso_hash_first_node(&ctx->regs_decl);
526
527 while (!cso_hash_iter_is_null(iter)) {
528 scan_register *reg = (scan_register *)cso_hash_iter_data(iter);
529 if (!is_register_used(ctx, reg) && !is_ind_register_used(ctx, reg)) {
530 report_warning( ctx, "%s[%u]: Register never used",
531 file_names[reg->file], reg->indices[0] );
532 }
533 iter = cso_hash_iter_next(iter);
534 }
535 }
536
537 /* Print totals, if any.
538 */
539 if (ctx->errors || ctx->warnings)
540 debug_printf( "%u errors, %u warnings\n", ctx->errors, ctx->warnings );
541
542 return TRUE;
543 }
544
545 static void
546 regs_hash_destroy(struct cso_hash *hash)
547 {
548 struct cso_hash_iter iter = cso_hash_first_node(hash);
549 while (!cso_hash_iter_is_null(iter)) {
550 scan_register *reg = (scan_register *)cso_hash_iter_data(iter);
551 iter = cso_hash_erase(hash, iter);
552 assert(reg->file < TGSI_FILE_COUNT);
553 FREE(reg);
554 }
555 cso_hash_deinit(hash);
556 }
557
558 boolean
559 tgsi_sanity_check(
560 const struct tgsi_token *tokens )
561 {
562 struct sanity_check_ctx ctx;
563 boolean retval;
564
565 ctx.iter.prolog = prolog;
566 ctx.iter.iterate_instruction = iter_instruction;
567 ctx.iter.iterate_declaration = iter_declaration;
568 ctx.iter.iterate_immediate = iter_immediate;
569 ctx.iter.iterate_property = iter_property;
570 ctx.iter.epilog = epilog;
571
572 cso_hash_init(&ctx.regs_decl);
573 cso_hash_init(&ctx.regs_used);
574 cso_hash_init(&ctx.regs_ind_used);
575
576 ctx.num_imms = 0;
577 ctx.num_instructions = 0;
578 ctx.index_of_END = ~0;
579
580 ctx.errors = 0;
581 ctx.warnings = 0;
582 ctx.implied_array_size = 0;
583 ctx.print = debug_get_option_print_sanity();
584
585 retval = tgsi_iterate_shader( tokens, &ctx.iter );
586 regs_hash_destroy(&ctx.regs_decl);
587 regs_hash_destroy(&ctx.regs_used);
588 regs_hash_destroy(&ctx.regs_ind_used);
589 if (retval == FALSE)
590 return FALSE;
591
592 return ctx.errors == 0;
593 }