aboutsummaryrefslogtreecommitdiffstats
path: root/src/mesa/drivers/dri/i965/brw_eu_validate.c
blob: 2de2ea1babc3ef26004cadd4bc667a275993e5fb (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
/*
 * Copyright © 2015 Intel Corporation
 *
 * 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 (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 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.
 */

/** @file brw_eu_validate.c
 *
 * This file implements a pass that validates shader assembly.
 */

#include "brw_eu.h"

/* We're going to do lots of string concatenation, so this should help. */
struct string {
   char *str;
   size_t len;
};

static void
cat(struct string *dest, const struct string src)
{
   dest->str = realloc(dest->str, dest->len + src.len + 1);
   memcpy(dest->str + dest->len, src.str, src.len);
   dest->str[dest->len + src.len] = '\0';
   dest->len = dest->len + src.len;
}
#define CAT(dest, src) cat(&dest, (struct string){src, strlen(src)})

#define error(str) "\tERROR: " str "\n"

#define ERROR_IF(cond, msg)          \
   do {                              \
      if (cond) {                    \
         CAT(error_msg, error(msg)); \
         valid = false;              \
      }                              \
   } while(0)

static bool
src0_is_null(const struct brw_device_info *devinfo, const brw_inst *inst)
{
   return brw_inst_src0_reg_file(devinfo, inst) == BRW_ARCHITECTURE_REGISTER_FILE &&
          brw_inst_src0_da_reg_nr(devinfo, inst) == BRW_ARF_NULL;
}

static bool
src1_is_null(const struct brw_device_info *devinfo, const brw_inst *inst)
{
   return brw_inst_src1_reg_file(devinfo, inst) == BRW_ARCHITECTURE_REGISTER_FILE &&
          brw_inst_src1_da_reg_nr(devinfo, inst) == BRW_ARF_NULL;
}

enum gen {
   GEN4  = (1 << 0),
   GEN45 = (1 << 1),
   GEN5  = (1 << 2),
   GEN6  = (1 << 3),
   GEN7  = (1 << 4),
   GEN75 = (1 << 5),
   GEN8  = (1 << 6),
   GEN9  = (1 << 7),
   GEN_ALL = ~0
};

#define GEN_GE(gen) (~((gen) - 1) | gen)
#define GEN_LE(gen) (((gen) - 1) | gen)

struct inst_info {
   enum gen gen;
};

static const struct inst_info inst_info[128] = {
   [BRW_OPCODE_ILLEGAL] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_MOV] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_SEL] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_MOVI] = {
      .gen = GEN_GE(GEN45),
   },
   [BRW_OPCODE_NOT] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_AND] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_OR] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_XOR] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_SHR] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_SHL] = {
      .gen = GEN_ALL,
   },
   /* BRW_OPCODE_DIM / BRW_OPCODE_SMOV */
   /* Reserved - 11 */
   [BRW_OPCODE_ASR] = {
      .gen = GEN_ALL,
   },
   /* Reserved - 13-15 */
   [BRW_OPCODE_CMP] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_CMPN] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_CSEL] = {
      .gen = GEN_GE(GEN8),
   },
   [BRW_OPCODE_F32TO16] = {
      .gen = GEN7 | GEN75,
   },
   [BRW_OPCODE_F16TO32] = {
      .gen = GEN7 | GEN75,
   },
   /* Reserved - 21-22 */
   [BRW_OPCODE_BFREV] = {
      .gen = GEN_GE(GEN7),
   },
   [BRW_OPCODE_BFE] = {
      .gen = GEN_GE(GEN7),
   },
   [BRW_OPCODE_BFI1] = {
      .gen = GEN_GE(GEN7),
   },
   [BRW_OPCODE_BFI2] = {
      .gen = GEN_GE(GEN7),
   },
   /* Reserved - 27-31 */
   [BRW_OPCODE_JMPI] = {
      .gen = GEN_ALL,
   },
   /* BRW_OPCODE_BRD */
   [BRW_OPCODE_IF] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_IFF] = { /* also BRW_OPCODE_BRC */
      .gen = GEN_LE(GEN5),
   },
   [BRW_OPCODE_ELSE] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_ENDIF] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_DO] = { /* also BRW_OPCODE_CASE */
      .gen = GEN_LE(GEN5),
   },
   [BRW_OPCODE_WHILE] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_BREAK] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_CONTINUE] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_HALT] = {
      .gen = GEN_ALL,
   },
   /* BRW_OPCODE_CALLA */
   /* BRW_OPCODE_MSAVE / BRW_OPCODE_CALL */
   /* BRW_OPCODE_MREST / BRW_OPCODE_RET */
   /* BRW_OPCODE_PUSH / BRW_OPCODE_FORK / BRW_OPCODE_GOTO */
   /* BRW_OPCODE_POP */
   [BRW_OPCODE_WAIT] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_SEND] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_SENDC] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_SENDS] = {
      .gen = GEN_GE(GEN9),
   },
   [BRW_OPCODE_SENDSC] = {
      .gen = GEN_GE(GEN9),
   },
   /* Reserved 53-55 */
   [BRW_OPCODE_MATH] = {
      .gen = GEN_GE(GEN6),
   },
   /* Reserved 57-63 */
   [BRW_OPCODE_ADD] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_MUL] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_AVG] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_FRC] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_RNDU] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_RNDD] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_RNDE] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_RNDZ] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_MAC] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_MACH] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_LZD] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_FBH] = {
      .gen = GEN_GE(GEN7),
   },
   [BRW_OPCODE_FBL] = {
      .gen = GEN_GE(GEN7),
   },
   [BRW_OPCODE_CBIT] = {
      .gen = GEN_GE(GEN7),
   },
   [BRW_OPCODE_ADDC] = {
      .gen = GEN_GE(GEN7),
   },
   [BRW_OPCODE_SUBB] = {
      .gen = GEN_GE(GEN7),
   },
   [BRW_OPCODE_SAD2] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_SADA2] = {
      .gen = GEN_ALL,
   },
   /* Reserved 82-83 */
   [BRW_OPCODE_DP4] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_DPH] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_DP3] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_DP2] = {
      .gen = GEN_ALL,
   },
   /* Reserved 88 */
   [BRW_OPCODE_LINE] = {
      .gen = GEN_ALL,
   },
   [BRW_OPCODE_PLN] = {
      .gen = GEN_GE(GEN45),
   },
   [BRW_OPCODE_MAD] = {
      .gen = GEN_GE(GEN6),
   },
   [BRW_OPCODE_LRP] = {
      .gen = GEN_GE(GEN6),
   },
   /* Reserved 93-124 */
   /* BRW_OPCODE_NENOP */
   [BRW_OPCODE_NOP] = {
      .gen = GEN_ALL,
   },
};

static unsigned
num_sources_from_inst(const struct brw_device_info *devinfo,
                      const brw_inst *inst)
{
   unsigned math_function;

   if (brw_inst_opcode(devinfo, inst) == BRW_OPCODE_MATH) {
      math_function = brw_inst_math_function(devinfo, inst);
   } else if (devinfo->gen < 6 &&
              brw_inst_opcode(devinfo, inst) == BRW_OPCODE_SEND) {
      if (brw_inst_sfid(devinfo, inst) == BRW_SFID_MATH) {
         math_function = brw_inst_math_msg_function(devinfo, inst);
      } else {
         /* Send instructions are allowed to have null sources since they use
          * the base_mrf field to specify which message register source.
          */
         return 0;
      }
   } else {
      return opcode_descs[brw_inst_opcode(devinfo, inst)].nsrc;
   }

   switch (math_function) {
   case BRW_MATH_FUNCTION_INV:
   case BRW_MATH_FUNCTION_LOG:
   case BRW_MATH_FUNCTION_EXP:
   case BRW_MATH_FUNCTION_SQRT:
   case BRW_MATH_FUNCTION_RSQ:
   case BRW_MATH_FUNCTION_SIN:
   case BRW_MATH_FUNCTION_COS:
   case BRW_MATH_FUNCTION_SINCOS:
   case GEN8_MATH_FUNCTION_INVM:
   case GEN8_MATH_FUNCTION_RSQRTM:
      return 1;
   case BRW_MATH_FUNCTION_FDIV:
   case BRW_MATH_FUNCTION_POW:
   case BRW_MATH_FUNCTION_INT_DIV_QUOTIENT_AND_REMAINDER:
   case BRW_MATH_FUNCTION_INT_DIV_QUOTIENT:
   case BRW_MATH_FUNCTION_INT_DIV_REMAINDER:
      return 2;
   default:
      unreachable("not reached");
   }
}

static enum gen
gen_from_devinfo(const struct brw_device_info *devinfo)
{
   switch (devinfo->gen) {
   case 4: return devinfo->is_g4x ? GEN45 : GEN4;
   case 5: return GEN5;
   case 6: return GEN6;
   case 7: return devinfo->is_haswell ? GEN75 : GEN7;
   case 8: return GEN8;
   case 9: return GEN9;
   default:
      unreachable("not reached");
   }
}

static bool
is_unsupported_inst(const struct brw_device_info *devinfo,
                    const brw_inst *inst)
{
   enum gen gen = gen_from_devinfo(devinfo);
   return (inst_info[brw_inst_opcode(devinfo, inst)].gen & gen) == 0;
}

bool
brw_validate_instructions(const struct brw_codegen *p, int start_offset,
                          struct annotation_info *annotation)
{
   const struct brw_device_info *devinfo = p->devinfo;
   const void *store = p->store + start_offset / 16;
   bool valid = true;

   for (int src_offset = 0; src_offset < p->next_insn_offset - start_offset;
        src_offset += sizeof(brw_inst)) {
      struct string error_msg = { .str = NULL, .len = 0 };
      const brw_inst *inst = store + src_offset;

      switch (num_sources_from_inst(devinfo, inst)) {
      case 3:
         /* Nothing to test. 3-src instructions can only have GRF sources, and
          * there's no bit to control the file.
          */
         break;
      case 2:
         ERROR_IF(src1_is_null(devinfo, inst), "src1 is null");
         /* fallthrough */
      case 1:
         ERROR_IF(src0_is_null(devinfo, inst), "src0 is null");
         break;
      case 0:
      default:
         break;
      }

      ERROR_IF(is_unsupported_inst(devinfo, inst),
               "Instruction not supported on this Gen");

      if (error_msg.str && annotation) {
         annotation_insert_error(annotation, src_offset, error_msg.str);
      }
      free(error_msg.str);
   }

   return valid;
}