tgsi_sanity.c revision 101f792a2af9c9a19a050afba8b60caa689466a5
1/**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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
36typedef struct {
37   uint file : 28;
38   /* max 2 dimensions */
39   uint dimensions : 4;
40   uint indices[2];
41} scan_register;
42
43struct sanity_check_ctx
44{
45   struct tgsi_iterate_context iter;
46   struct cso_hash *regs_decl;
47   struct cso_hash *regs_used;
48   struct cso_hash *regs_ind_used;
49
50   uint num_imms;
51   uint num_instructions;
52   uint index_of_END;
53
54   uint errors;
55   uint warnings;
56   uint implied_array_size;
57};
58
59static INLINE unsigned
60scan_register_key(const scan_register *reg)
61{
62   unsigned key = reg->file;
63   key |= (reg->indices[0] << 4);
64   key |= (reg->indices[1] << 18);
65
66   return key;
67}
68
69static void
70fill_scan_register1d(scan_register *reg,
71                     uint file, uint index)
72{
73   reg->file = file;
74   reg->dimensions = 1;
75   reg->indices[0] = index;
76   reg->indices[1] = 0;
77}
78
79static void
80fill_scan_register2d(scan_register *reg,
81                     uint file, uint index1, uint index2)
82{
83   reg->file = file;
84   reg->dimensions = 2;
85   reg->indices[0] = index1;
86   reg->indices[1] = index2;
87}
88
89static void
90scan_register_dst(scan_register *reg,
91                  struct tgsi_full_dst_register *dst)
92{
93   if (dst->Register.Dimension) {
94      /*FIXME: right now we don't support indirect
95       * multidimensional addressing */
96      fill_scan_register2d(reg,
97                           dst->Register.File,
98                           dst->Register.Index,
99                           dst->Dimension.Index);
100   } else {
101      fill_scan_register1d(reg,
102                           dst->Register.File,
103                           dst->Register.Index);
104   }
105}
106
107static void
108scan_register_src(scan_register *reg,
109                  struct tgsi_full_src_register *src)
110{
111   if (src->Register.Dimension) {
112      /*FIXME: right now we don't support indirect
113       * multidimensional addressing */
114      fill_scan_register2d(reg,
115                           src->Register.File,
116                           src->Register.Index,
117                           src->Dimension.Index);
118   } else {
119      fill_scan_register1d(reg,
120                           src->Register.File,
121                           src->Register.Index);
122   }
123}
124
125static scan_register *
126create_scan_register_src(struct tgsi_full_src_register *src)
127{
128   scan_register *reg = MALLOC(sizeof(scan_register));
129   scan_register_src(reg, src);
130
131   return reg;
132}
133
134static scan_register *
135create_scan_register_dst(struct tgsi_full_dst_register *dst)
136{
137   scan_register *reg = MALLOC(sizeof(scan_register));
138   scan_register_dst(reg, dst);
139
140   return reg;
141}
142
143static void
144report_error(
145   struct sanity_check_ctx *ctx,
146   const char *format,
147   ... )
148{
149   va_list args;
150
151   debug_printf( "Error  : " );
152   va_start( args, format );
153   _debug_vprintf( format, args );
154   va_end( args );
155   debug_printf( "\n" );
156   ctx->errors++;
157}
158
159static void
160report_warning(
161   struct sanity_check_ctx *ctx,
162   const char *format,
163   ... )
164{
165   va_list args;
166
167   debug_printf( "Warning: " );
168   va_start( args, format );
169   _debug_vprintf( format, args );
170   va_end( args );
171   debug_printf( "\n" );
172   ctx->warnings++;
173}
174
175static boolean
176check_file_name(
177   struct sanity_check_ctx *ctx,
178   uint file )
179{
180   if (file <= TGSI_FILE_NULL || file >= TGSI_FILE_COUNT) {
181      report_error( ctx, "(%u): Invalid register file name", file );
182      return FALSE;
183   }
184   return TRUE;
185}
186
187static boolean
188is_register_declared(
189   struct sanity_check_ctx *ctx,
190   const scan_register *reg)
191{
192   void *data = cso_hash_find_data_from_template(
193      ctx->regs_decl, scan_register_key(reg),
194      (void*)reg, sizeof(scan_register));
195   return  data ? TRUE : FALSE;
196}
197
198static boolean
199is_any_register_declared(
200   struct sanity_check_ctx *ctx,
201   uint file )
202{
203   struct cso_hash_iter iter =
204      cso_hash_first_node(ctx->regs_decl);
205
206   while (!cso_hash_iter_is_null(iter)) {
207      scan_register *reg = (scan_register *)cso_hash_iter_data(iter);
208      if (reg->file == file)
209         return TRUE;
210      iter = cso_hash_iter_next(iter);
211   }
212
213   return FALSE;
214}
215
216static boolean
217is_register_used(
218   struct sanity_check_ctx *ctx,
219   scan_register *reg)
220{
221   void *data = cso_hash_find_data_from_template(
222      ctx->regs_used, scan_register_key(reg),
223      reg, sizeof(scan_register));
224   return  data ? TRUE : FALSE;
225}
226
227
228static boolean
229is_ind_register_used(
230   struct sanity_check_ctx *ctx,
231   scan_register *reg)
232{
233   return cso_hash_contains(ctx->regs_ind_used, reg->file);
234}
235
236static const char *file_names[TGSI_FILE_COUNT] =
237{
238   "NULL",
239   "CONST",
240   "IN",
241   "OUT",
242   "TEMP",
243   "SAMP",
244   "ADDR",
245   "IMM",
246   "PRED",
247   "SV",
248   "IMMX",
249   "TEMPX"
250};
251
252static boolean
253check_register_usage(
254   struct sanity_check_ctx *ctx,
255   scan_register *reg,
256   const char *name,
257   boolean indirect_access )
258{
259   if (!check_file_name( ctx, reg->file )) {
260      FREE(reg);
261      return FALSE;
262   }
263
264   if (indirect_access) {
265      /* Note that 'index' is an offset relative to the value of the
266       * address register.  No range checking done here.*/
267      reg->indices[0] = 0;
268      reg->indices[1] = 0;
269      if (!is_any_register_declared( ctx, reg->file ))
270         report_error( ctx, "%s: Undeclared %s register", file_names[reg->file], name );
271      if (!is_ind_register_used(ctx, reg))
272         cso_hash_insert(ctx->regs_ind_used, reg->file, reg);
273      else
274         FREE(reg);
275   }
276   else {
277      if (!is_register_declared( ctx, reg )) {
278         if (reg->dimensions == 2) {
279            report_error( ctx, "%s[%d][%d]: Undeclared %s register", file_names[reg->file],
280                          reg->indices[0], reg->indices[1], name );
281         }
282         else {
283            report_error( ctx, "%s[%d]: Undeclared %s register", file_names[reg->file],
284                          reg->indices[0], name );
285         }
286      }
287      if (!is_register_used( ctx, reg ))
288         cso_hash_insert(ctx->regs_used, scan_register_key(reg), reg);
289      else
290         FREE(reg);
291   }
292   return TRUE;
293}
294
295static boolean
296iter_instruction(
297   struct tgsi_iterate_context *iter,
298   struct tgsi_full_instruction *inst )
299{
300   struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter;
301   const struct tgsi_opcode_info *info;
302   uint i;
303
304   if (inst->Instruction.Opcode == TGSI_OPCODE_END) {
305      if (ctx->index_of_END != ~0) {
306         report_error( ctx, "Too many END instructions" );
307      }
308      ctx->index_of_END = ctx->num_instructions;
309   }
310
311   info = tgsi_get_opcode_info( inst->Instruction.Opcode );
312   if (info == NULL) {
313      report_error( ctx, "(%u): Invalid instruction opcode", inst->Instruction.Opcode );
314      return TRUE;
315   }
316
317   if (info->num_dst != inst->Instruction.NumDstRegs) {
318      report_error( ctx, "%s: Invalid number of destination operands, should be %u", info->mnemonic, info->num_dst );
319   }
320   if (info->num_src != inst->Instruction.NumSrcRegs) {
321      report_error( ctx, "%s: Invalid number of source operands, should be %u", info->mnemonic, info->num_src );
322   }
323
324   /* Check destination and source registers' validity.
325    * Mark the registers as used.
326    */
327   for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
328      scan_register *reg = create_scan_register_dst(&inst->Dst[i]);
329      check_register_usage(
330         ctx,
331         reg,
332         "destination",
333         FALSE );
334      if (!inst->Dst[i].Register.WriteMask) {
335         report_error(ctx, "Destination register has empty writemask");
336      }
337   }
338   for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
339      scan_register *reg = create_scan_register_src(&inst->Src[i]);
340      check_register_usage(
341         ctx,
342         reg,
343         "source",
344         (boolean)inst->Src[i].Register.Indirect );
345      if (inst->Src[i].Register.Indirect) {
346         scan_register *ind_reg = MALLOC(sizeof(scan_register));
347
348         fill_scan_register1d(ind_reg,
349                              inst->Src[i].Indirect.File,
350                              inst->Src[i].Indirect.Index);
351         check_register_usage(
352            ctx,
353            ind_reg,
354            "indirect",
355            FALSE );
356      }
357   }
358
359   ctx->num_instructions++;
360
361   return TRUE;
362}
363
364static void
365check_and_declare(struct sanity_check_ctx *ctx,
366                  scan_register *reg)
367{
368   if (is_register_declared( ctx, reg))
369      report_error( ctx, "%s[%u]: The same register declared more than once",
370                    file_names[reg->file], reg->indices[0] );
371   cso_hash_insert(ctx->regs_decl,
372                   scan_register_key(reg),
373                   reg);
374}
375
376
377static boolean
378iter_declaration(
379   struct tgsi_iterate_context *iter,
380   struct tgsi_full_declaration *decl )
381{
382   struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter;
383   uint file;
384   uint i;
385
386   /* No declarations allowed after the first instruction.
387    */
388   if (ctx->num_instructions > 0)
389      report_error( ctx, "Instruction expected but declaration found" );
390
391   /* Check registers' validity.
392    * Mark the registers as declared.
393    */
394   file = decl->Declaration.File;
395   if (!check_file_name( ctx, file ))
396      return TRUE;
397   for (i = decl->Range.First; i <= decl->Range.Last; i++) {
398      /* declared TGSI_FILE_INPUT's for geometry processor
399       * have an implied second dimension */
400      if (file == TGSI_FILE_INPUT &&
401          ctx->iter.processor.Processor == TGSI_PROCESSOR_GEOMETRY) {
402         uint vert;
403         for (vert = 0; vert < ctx->implied_array_size; ++vert) {
404            scan_register *reg = MALLOC(sizeof(scan_register));
405            fill_scan_register2d(reg, file, i, vert);
406            check_and_declare(ctx, reg);
407         }
408      } else {
409         scan_register *reg = MALLOC(sizeof(scan_register));
410         if (decl->Declaration.Dimension) {
411            fill_scan_register2d(reg, file, i, decl->Dim.Index2D);
412         } else {
413            fill_scan_register1d(reg, file, i);
414         }
415         check_and_declare(ctx, reg);
416      }
417   }
418
419   return TRUE;
420}
421
422static boolean
423iter_immediate(
424   struct tgsi_iterate_context *iter,
425   struct tgsi_full_immediate *imm )
426{
427   struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter;
428   scan_register *reg;
429
430   /* No immediates allowed after the first instruction.
431    */
432   if (ctx->num_instructions > 0)
433      report_error( ctx, "Instruction expected but immediate found" );
434
435   /* Mark the register as declared.
436    */
437   reg = MALLOC(sizeof(scan_register));
438   fill_scan_register1d(reg, TGSI_FILE_IMMEDIATE, ctx->num_imms);
439   cso_hash_insert(ctx->regs_decl, scan_register_key(reg), reg);
440   ctx->num_imms++;
441
442   /* Check data type validity.
443    */
444   if (imm->Immediate.DataType != TGSI_IMM_FLOAT32 &&
445       imm->Immediate.DataType != TGSI_IMM_UINT32 &&
446       imm->Immediate.DataType != TGSI_IMM_INT32) {
447      report_error( ctx, "(%u): Invalid immediate data type", imm->Immediate.DataType );
448      return TRUE;
449   }
450
451   return TRUE;
452}
453
454
455static boolean
456iter_property(
457   struct tgsi_iterate_context *iter,
458   struct tgsi_full_property *prop )
459{
460   struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter;
461
462   if (iter->processor.Processor == TGSI_PROCESSOR_GEOMETRY &&
463       prop->Property.PropertyName == TGSI_PROPERTY_GS_INPUT_PRIM) {
464      ctx->implied_array_size = u_vertices_per_prim(prop->u[0].Data);
465   }
466   return TRUE;
467}
468
469static boolean
470epilog(
471   struct tgsi_iterate_context *iter )
472{
473   struct sanity_check_ctx *ctx = (struct sanity_check_ctx *) iter;
474
475   /* There must be an END instruction somewhere.
476    */
477   if (ctx->index_of_END == ~0) {
478      report_error( ctx, "Missing END instruction" );
479   }
480
481   /* Check if all declared registers were used.
482    */
483   {
484      struct cso_hash_iter iter =
485         cso_hash_first_node(ctx->regs_decl);
486
487      while (!cso_hash_iter_is_null(iter)) {
488         scan_register *reg = (scan_register *)cso_hash_iter_data(iter);
489         if (!is_register_used(ctx, reg) && !is_ind_register_used(ctx, reg)) {
490            report_warning( ctx, "%s[%u]: Register never used",
491                            file_names[reg->file], reg->indices[0] );
492         }
493         iter = cso_hash_iter_next(iter);
494      }
495   }
496
497   /* Print totals, if any.
498    */
499   if (ctx->errors || ctx->warnings)
500      debug_printf( "%u errors, %u warnings\n", ctx->errors, ctx->warnings );
501
502   return TRUE;
503}
504
505static void
506regs_hash_destroy(struct cso_hash *hash)
507{
508   struct cso_hash_iter iter = cso_hash_first_node(hash);
509   while (!cso_hash_iter_is_null(iter)) {
510      scan_register *reg = (scan_register *)cso_hash_iter_data(iter);
511      iter = cso_hash_erase(hash, iter);
512      assert(reg->file < TGSI_FILE_COUNT);
513      FREE(reg);
514   }
515   cso_hash_delete(hash);
516}
517
518boolean
519tgsi_sanity_check(
520   const struct tgsi_token *tokens )
521{
522   struct sanity_check_ctx ctx;
523
524   ctx.iter.prolog = NULL;
525   ctx.iter.iterate_instruction = iter_instruction;
526   ctx.iter.iterate_declaration = iter_declaration;
527   ctx.iter.iterate_immediate = iter_immediate;
528   ctx.iter.iterate_property = iter_property;
529   ctx.iter.epilog = epilog;
530
531   ctx.regs_decl = cso_hash_create();
532   ctx.regs_used = cso_hash_create();
533   ctx.regs_ind_used = cso_hash_create();
534
535   ctx.num_imms = 0;
536   ctx.num_instructions = 0;
537   ctx.index_of_END = ~0;
538
539   ctx.errors = 0;
540   ctx.warnings = 0;
541   ctx.implied_array_size = 0;
542
543   if (!tgsi_iterate_shader( tokens, &ctx.iter ))
544      return FALSE;
545
546   regs_hash_destroy(ctx.regs_decl);
547   regs_hash_destroy(ctx.regs_used);
548   regs_hash_destroy(ctx.regs_ind_used);
549   return ctx.errors == 0;
550}
551