summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/ilo/shader/toy_compiler.c
blob: a4c2a2c4e2005cda2720958dccc368824537c029 (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
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
/*
 * Mesa 3-D graphics library
 *
 * Copyright (C) 2012-2013 LunarG, Inc.
 *
 * 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 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:
 *    Chia-I Wu <olv@lunarg.com>
 */

#include "toy_compiler.h"

/**
 * Dump an operand.
 */
static void
tc_dump_operand(struct toy_compiler *tc,
                enum toy_file file, enum toy_type type, enum toy_rect rect,
                bool indirect, unsigned indirect_subreg, uint32_t val32,
                bool is_dst)
{
   static const char *toy_file_names[TOY_FILE_COUNT] = {
      [TOY_FILE_VRF]        = "v",
      [TOY_FILE_ARF]        = "NOT USED",
      [TOY_FILE_GRF]        = "r",
      [TOY_FILE_MRF]        = "m",
      [TOY_FILE_IMM]        = "NOT USED",
   };
   const char *name = toy_file_names[file];
   int reg, subreg;

   if (file != TOY_FILE_IMM) {
      reg = val32 / TOY_REG_WIDTH;
      subreg = (val32 % TOY_REG_WIDTH) / toy_type_size(type);
   }

   switch (file) {
   case TOY_FILE_GRF:
      if (indirect) {
         const int addr_subreg = indirect_subreg / toy_type_size(TOY_TYPE_UW);

         ilo_printf("%s[a0.%d", name, addr_subreg);
         if (val32)
            ilo_printf("%+d", (int) val32);
         ilo_printf("]");
         break;
      }
      /* fall through */
   case TOY_FILE_VRF:
   case TOY_FILE_MRF:
      ilo_printf("%s%d", name, reg);
      if (subreg)
         ilo_printf(".%d", subreg);
      break;
   case TOY_FILE_ARF:
      switch (reg) {
      case GEN6_ARF_NULL:
         ilo_printf("null");
         break;
      case GEN6_ARF_A0:
         ilo_printf("a0.%d", subreg);
         break;
      case GEN6_ARF_ACC0:
      case GEN6_ARF_ACC0 + 1:
         ilo_printf("acc%d.%d", (reg & 1), subreg);
         break;
      case GEN6_ARF_F0:
         ilo_printf("f0.%d", subreg);
         break;
      case GEN6_ARF_SR0:
         ilo_printf("sr0.%d", subreg);
         break;
      case GEN6_ARF_CR0:
         ilo_printf("cr0.%d", subreg);
         break;
      case GEN6_ARF_N0:
      case GEN6_ARF_N0 + 1:
         ilo_printf("n%d.%d", (reg & 1), subreg);
         break;
      case GEN6_ARF_IP:
         ilo_printf("ip");
         break;
      }
      break;
   case TOY_FILE_IMM:
      switch (type) {
      case TOY_TYPE_F:
         {
            union fi fi = { .ui = val32 };
            ilo_printf("%f", fi.f);
         }
         break;
      case TOY_TYPE_D:
         ilo_printf("%d", (int32_t) val32);
         break;
      case TOY_TYPE_UD:
         ilo_printf("%u", val32);
         break;
      case TOY_TYPE_W:
         ilo_printf("%d", (int16_t) (val32 & 0xffff));
         break;
      case TOY_TYPE_UW:
         ilo_printf("%u", val32 & 0xffff);
         break;
      case TOY_TYPE_V:
         ilo_printf("0x%08x", val32);
         break;
      default:
         assert(!"unknown imm type");
         break;
      }
      break;
   default:
      assert(!"unexpected file");
      break;
   }

   /* dump the region parameter */
   if (file != TOY_FILE_IMM) {
      int vert_stride, width, horz_stride;

      switch (rect) {
      case TOY_RECT_LINEAR:
         vert_stride = tc->rect_linear_width;
         width = tc->rect_linear_width;
         horz_stride = 1;
         break;
      case TOY_RECT_041:
         vert_stride = 0;
         width = 4;
         horz_stride = 1;
         break;
      case TOY_RECT_010:
         vert_stride = 0;
         width = 1;
         horz_stride = 0;
         break;
      case TOY_RECT_220:
         vert_stride = 2;
         width = 2;
         horz_stride = 0;
         break;
      case TOY_RECT_440:
         vert_stride = 4;
         width = 4;
         horz_stride = 0;
         break;
      case TOY_RECT_240:
         vert_stride = 2;
         width = 4;
         horz_stride = 0;
         break;
      default:
         assert(!"unknown rect parameter");
         vert_stride = 0;
         width = 0;
         horz_stride = 0;
         break;
      }

      if (is_dst)
         ilo_printf("<%d>", horz_stride);
      else
         ilo_printf("<%d;%d,%d>", vert_stride, width, horz_stride);
   }

   switch (type) {
   case TOY_TYPE_F:
      ilo_printf(":f");
      break;
   case TOY_TYPE_D:
      ilo_printf(":d");
      break;
   case TOY_TYPE_UD:
      ilo_printf(":ud");
      break;
   case TOY_TYPE_W:
      ilo_printf(":w");
      break;
   case TOY_TYPE_UW:
      ilo_printf(":uw");
      break;
   case TOY_TYPE_V:
      ilo_printf(":v");
      break;
   default:
      assert(!"unexpected type");
      break;
   }
}

/**
 * Dump a source operand.
 */
static void
tc_dump_src(struct toy_compiler *tc, struct toy_src src)
{
   if (src.negate)
      ilo_printf("-");
   if (src.absolute)
      ilo_printf("|");

   tc_dump_operand(tc, src.file, src.type, src.rect,
         src.indirect, src.indirect_subreg, src.val32, false);

   if (tsrc_is_swizzled(src)) {
      const char xyzw[] = "xyzw";
      ilo_printf(".%c%c%c%c",
            xyzw[src.swizzle_x],
            xyzw[src.swizzle_y],
            xyzw[src.swizzle_z],
            xyzw[src.swizzle_w]);
   }

   if (src.absolute)
      ilo_printf("|");
}

/**
 * Dump a destination operand.
 */
static void
tc_dump_dst(struct toy_compiler *tc, struct toy_dst dst)
{
   tc_dump_operand(tc, dst.file, dst.type, dst.rect,
         dst.indirect, dst.indirect_subreg, dst.val32, true);

   if (dst.writemask != TOY_WRITEMASK_XYZW) {
      ilo_printf(".");
      if (dst.writemask & TOY_WRITEMASK_X)
         ilo_printf("x");
      if (dst.writemask & TOY_WRITEMASK_Y)
         ilo_printf("y");
      if (dst.writemask & TOY_WRITEMASK_Z)
         ilo_printf("z");
      if (dst.writemask & TOY_WRITEMASK_W)
         ilo_printf("w");
   }
}

static const char *
get_opcode_name(unsigned opcode)
{
   switch (opcode) {
   case GEN6_OPCODE_MOV:                   return "mov";
   case GEN6_OPCODE_SEL:                   return "sel";
   case GEN6_OPCODE_NOT:                   return "not";
   case GEN6_OPCODE_AND:                   return "and";
   case GEN6_OPCODE_OR:                    return "or";
   case GEN6_OPCODE_XOR:                   return "xor";
   case GEN6_OPCODE_SHR:                   return "shr";
   case GEN6_OPCODE_SHL:                   return "shl";
   case 0xa:                   return "rsr";
   case 0xb:                   return "rsl";
   case GEN6_OPCODE_ASR:                   return "asr";
   case GEN6_OPCODE_CMP:                   return "cmp";
   case GEN6_OPCODE_CMPN:                  return "cmpn";
   case GEN6_OPCODE_JMPI:                  return "jmpi";
   case GEN6_OPCODE_IF:                    return "if";
   case 0x23:                   return "iff";
   case GEN6_OPCODE_ELSE:                  return "else";
   case GEN6_OPCODE_ENDIF:                 return "endif";
   case 0x26:                    return "do";
   case GEN6_OPCODE_WHILE:                 return "while";
   case GEN6_OPCODE_BREAK:                 return "break";
   case GEN6_OPCODE_CONT:              return "continue";
   case GEN6_OPCODE_HALT:                  return "halt";
   case 0x2c:                 return "msave";
   case 0x2d:              return "mrestore";
   case 0x2e:                  return "push";
   case 0x2f:                   return "pop";
   case GEN6_OPCODE_WAIT:                  return "wait";
   case GEN6_OPCODE_SEND:                  return "send";
   case GEN6_OPCODE_SENDC:                 return "sendc";
   case GEN6_OPCODE_MATH:                  return "math";
   case GEN6_OPCODE_ADD:                   return "add";
   case GEN6_OPCODE_MUL:                   return "mul";
   case GEN6_OPCODE_AVG:                   return "avg";
   case GEN6_OPCODE_FRC:                   return "frc";
   case GEN6_OPCODE_RNDU:                  return "rndu";
   case GEN6_OPCODE_RNDD:                  return "rndd";
   case GEN6_OPCODE_RNDE:                  return "rnde";
   case GEN6_OPCODE_RNDZ:                  return "rndz";
   case GEN6_OPCODE_MAC:                   return "mac";
   case GEN6_OPCODE_MACH:                  return "mach";
   case GEN6_OPCODE_LZD:                   return "lzd";
   case GEN6_OPCODE_SAD2:                  return "sad2";
   case GEN6_OPCODE_SADA2:                 return "sada2";
   case GEN6_OPCODE_DP4:                   return "dp4";
   case GEN6_OPCODE_DPH:                   return "dph";
   case GEN6_OPCODE_DP3:                   return "dp3";
   case GEN6_OPCODE_DP2:                   return "dp2";
   case 0x58:                  return "dpa2";
   case GEN6_OPCODE_LINE:                  return "line";
   case GEN6_OPCODE_PLN:                   return "pln";
   case GEN6_OPCODE_MAD:                   return "mad";
   case GEN6_OPCODE_NOP:                   return "nop";
   case TOY_OPCODE_DO:                    return "do";
   /* TGSI */
   case TOY_OPCODE_TGSI_IN:               return "tgsi.in";
   case TOY_OPCODE_TGSI_CONST:            return "tgsi.const";
   case TOY_OPCODE_TGSI_SV:               return "tgsi.sv";
   case TOY_OPCODE_TGSI_IMM:              return "tgsi.imm";
   case TOY_OPCODE_TGSI_INDIRECT_FETCH:   return "tgsi.indirect_fetch";
   case TOY_OPCODE_TGSI_INDIRECT_STORE:   return "tgsi.indirect_store";
   case TOY_OPCODE_TGSI_TEX:              return "tgsi.tex";
   case TOY_OPCODE_TGSI_TXB:              return "tgsi.txb";
   case TOY_OPCODE_TGSI_TXD:              return "tgsi.txd";
   case TOY_OPCODE_TGSI_TXL:              return "tgsi.txl";
   case TOY_OPCODE_TGSI_TXP:              return "tgsi.txp";
   case TOY_OPCODE_TGSI_TXF:              return "tgsi.txf";
   case TOY_OPCODE_TGSI_TXQ:              return "tgsi.txq";
   case TOY_OPCODE_TGSI_TXQ_LZ:           return "tgsi.txq_lz";
   case TOY_OPCODE_TGSI_TEX2:             return "tgsi.tex2";
   case TOY_OPCODE_TGSI_TXB2:             return "tgsi.txb2";
   case TOY_OPCODE_TGSI_TXL2:             return "tgsi.txl2";
   case TOY_OPCODE_TGSI_SAMPLE:           return "tgsi.sample";
   case TOY_OPCODE_TGSI_SAMPLE_I:         return "tgsi.sample_i";
   case TOY_OPCODE_TGSI_SAMPLE_I_MS:      return "tgsi.sample_i_ms";
   case TOY_OPCODE_TGSI_SAMPLE_B:         return "tgsi.sample_b";
   case TOY_OPCODE_TGSI_SAMPLE_C:         return "tgsi.sample_c";
   case TOY_OPCODE_TGSI_SAMPLE_C_LZ:      return "tgsi.sample_c_lz";
   case TOY_OPCODE_TGSI_SAMPLE_D:         return "tgsi.sample_d";
   case TOY_OPCODE_TGSI_SAMPLE_L:         return "tgsi.sample_l";
   case TOY_OPCODE_TGSI_GATHER4:          return "tgsi.gather4";
   case TOY_OPCODE_TGSI_SVIEWINFO:        return "tgsi.sviewinfo";
   case TOY_OPCODE_TGSI_SAMPLE_POS:       return "tgsi.sample_pos";
   case TOY_OPCODE_TGSI_SAMPLE_INFO:      return "tgsi.sample_info";
   /* math */
   case TOY_OPCODE_INV:                   return "math.inv";
   case TOY_OPCODE_LOG:                   return "math.log";
   case TOY_OPCODE_EXP:                   return "math.exp";
   case TOY_OPCODE_SQRT:                  return "math.sqrt";
   case TOY_OPCODE_RSQ:                   return "math.rsq";
   case TOY_OPCODE_SIN:                   return "math.sin";
   case TOY_OPCODE_COS:                   return "math.cos";
   case TOY_OPCODE_FDIV:                  return "math.fdiv";
   case TOY_OPCODE_POW:                   return "math.pow";
   case TOY_OPCODE_INT_DIV_QUOTIENT:      return "math.int_div_quotient";
   case TOY_OPCODE_INT_DIV_REMAINDER:     return "math.int_div_remainer";
   /* urb */
   case TOY_OPCODE_URB_WRITE:             return "urb.urb_write";
   /* gs */
   case TOY_OPCODE_EMIT:                  return "gs.emit";
   case TOY_OPCODE_ENDPRIM:               return "gs.endprim";
   /* fs */
   case TOY_OPCODE_DDX:                   return "fs.ddx";
   case TOY_OPCODE_DDY:                   return "fs.ddy";
   case TOY_OPCODE_FB_WRITE:              return "fs.fb_write";
   case TOY_OPCODE_KIL:                   return "fs.kil";
   default:                               return "unk";
   }
}

static const char *
get_cond_modifier_name(unsigned opcode, unsigned cond_modifier)
{
   switch (opcode) {
   case GEN6_OPCODE_SEND:
   case GEN6_OPCODE_SENDC:
      /* SFID */
      switch (cond_modifier) {
      case GEN6_SFID_NULL:                       return "Null";
      case GEN6_SFID_SAMPLER:                    return "Sampling Engine";
      case GEN6_SFID_GATEWAY:            return "Message Gateway";
      case GEN6_SFID_DP_SAMPLER:    return "Data Port Sampler Cache";
      case GEN6_SFID_DP_RC:     return "Data Port Render Cache";
      case GEN6_SFID_URB:                        return "URB";
      case GEN6_SFID_SPAWNER:             return "Thread Spawner";
      case GEN6_SFID_DP_CC:   return "Constant Cache";
      default:                                  return "Unknown";
      }
      break;
   case GEN6_OPCODE_MATH:
      /* FC */
      switch (cond_modifier) {
      case GEN6_MATH_INV:               return "INV";
      case GEN6_MATH_LOG:               return "LOG";
      case GEN6_MATH_EXP:               return "EXP";
      case GEN6_MATH_SQRT:              return "SQRT";
      case GEN6_MATH_RSQ:               return "RSQ";
      case GEN6_MATH_SIN:               return "SIN";
      case GEN6_MATH_COS:               return "COS";
      case GEN6_MATH_FDIV:              return "FDIV";
      case GEN6_MATH_POW:               return "POW";
      case GEN6_MATH_INT_DIV_QUOTIENT:  return "INT DIV (quotient)";
      case GEN6_MATH_INT_DIV_REMAINDER: return "INT DIV (remainder)";
      default:                                  return "UNK";
      }
      break;
   default:
      switch (cond_modifier) {
      case GEN6_COND_NONE:                return NULL;
      case GEN6_COND_Z:                   return "z";
      case GEN6_COND_NZ:                  return "nz";
      case GEN6_COND_G:                   return "g";
      case GEN6_COND_GE:                  return "ge";
      case GEN6_COND_L:                   return "l";
      case GEN6_COND_LE:                  return "le";
      default:                                  return "unk";
      }
      break;
   }
}

/**
 * Dump an instruction.
 */
static void
tc_dump_inst(struct toy_compiler *tc, const struct toy_inst *inst)
{
   const char *name;
   int i;

   name = get_opcode_name(inst->opcode);

   ilo_printf("  %s", name);

   if (inst->opcode == GEN6_OPCODE_NOP) {
      ilo_printf("\n");
      return;
   }

   if (inst->saturate)
      ilo_printf(".sat");

   name = get_cond_modifier_name(inst->opcode, inst->cond_modifier);
   if (name)
      ilo_printf(".%s", name);

   ilo_printf(" ");

   tc_dump_dst(tc, inst->dst);

   for (i = 0; i < Elements(inst->src); i++) {
      if (tsrc_is_null(inst->src[i]))
         break;

      ilo_printf(", ");
      tc_dump_src(tc, inst->src[i]);
   }

   ilo_printf("\n");
}

/**
 * Dump the instructions added to the compiler.
 */
void
toy_compiler_dump(struct toy_compiler *tc)
{
   struct toy_inst *inst;
   int pc;

   pc = 0;
   tc_head(tc);
   while ((inst = tc_next_no_skip(tc)) != NULL) {
      /* we do not generate code for markers */
      if (inst->marker)
         ilo_printf("marker:");
      else
         ilo_printf("%6d:", pc++);

      tc_dump_inst(tc, inst);
   }
}

/**
 * Clean up the toy compiler.
 */
void
toy_compiler_cleanup(struct toy_compiler *tc)
{
   struct toy_inst *inst, *next;

   LIST_FOR_EACH_ENTRY_SAFE(inst, next, &tc->instructions, list)
      util_slab_free(&tc->mempool, inst);

   util_slab_destroy(&tc->mempool);
}

/**
 * Initialize the instruction template, from which tc_add() initializes the
 * newly added instructions.
 */
static void
tc_init_inst_templ(struct toy_compiler *tc)
{
   struct toy_inst *templ = &tc->templ;
   int i;

   templ->opcode = GEN6_OPCODE_NOP;
   templ->access_mode = GEN6_ALIGN_1;
   templ->mask_ctrl = GEN6_MASKCTRL_NORMAL;
   templ->dep_ctrl = GEN6_DEPCTRL_NORMAL;
   templ->qtr_ctrl = GEN6_QTRCTRL_1Q;
   templ->thread_ctrl = GEN6_THREADCTRL_NORMAL;
   templ->pred_ctrl = GEN6_PREDCTRL_NONE;
   templ->pred_inv = false;
   templ->exec_size = GEN6_EXECSIZE_1;
   templ->cond_modifier = GEN6_COND_NONE;
   templ->acc_wr_ctrl = false;
   templ->saturate = false;

   templ->marker = false;

   templ->dst = tdst_null();
   for (i = 0; i < Elements(templ->src); i++)
      templ->src[i] = tsrc_null();

   for (i = 0; i < Elements(templ->tex.offsets); i++)
      templ->tex.offsets[i] = tsrc_null();

   list_inithead(&templ->list);
}

/**
 * Initialize the toy compiler.
 */
void
toy_compiler_init(struct toy_compiler *tc, const struct ilo_dev *dev)
{
   memset(tc, 0, sizeof(*tc));

   tc->dev = dev;

   tc_init_inst_templ(tc);

   util_slab_create(&tc->mempool, sizeof(struct toy_inst),
         64, UTIL_SLAB_SINGLETHREADED);

   list_inithead(&tc->instructions);
   /* instructions are added to the tail */
   tc_tail(tc);

   tc->rect_linear_width = 1;

   /* skip 0 so that util_hash_table_get() never returns NULL */
   tc->next_vrf = 1;
}