i965: Structure code so unsupported inst will not generate more errors.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_eu_validate.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 /** @file brw_eu_validate.c
25 *
26 * This file implements a pass that validates shader assembly.
27 */
28
29 #include "brw_eu.h"
30
31 /* We're going to do lots of string concatenation, so this should help. */
32 struct string {
33 char *str;
34 size_t len;
35 };
36
37 static void
38 cat(struct string *dest, const struct string src)
39 {
40 dest->str = realloc(dest->str, dest->len + src.len + 1);
41 memcpy(dest->str + dest->len, src.str, src.len);
42 dest->str[dest->len + src.len] = '\0';
43 dest->len = dest->len + src.len;
44 }
45 #define CAT(dest, src) cat(&dest, (struct string){src, strlen(src)})
46
47 #define error(str) "\tERROR: " str "\n"
48
49 #define ERROR(msg) ERROR_IF(true, msg)
50 #define ERROR_IF(cond, msg) \
51 do { \
52 if (cond) { \
53 CAT(error_msg, error(msg)); \
54 } \
55 } while(0)
56
57 #define CHECK(func, args...) \
58 do { \
59 struct string __msg = func(devinfo, inst, ##args); \
60 if (__msg.str) { \
61 cat(&error_msg, __msg); \
62 free(__msg.str); \
63 } \
64 } while (0)
65
66 static bool
67 src0_is_null(const struct gen_device_info *devinfo, const brw_inst *inst)
68 {
69 return brw_inst_src0_reg_file(devinfo, inst) == BRW_ARCHITECTURE_REGISTER_FILE &&
70 brw_inst_src0_da_reg_nr(devinfo, inst) == BRW_ARF_NULL;
71 }
72
73 static bool
74 src1_is_null(const struct gen_device_info *devinfo, const brw_inst *inst)
75 {
76 return brw_inst_src1_reg_file(devinfo, inst) == BRW_ARCHITECTURE_REGISTER_FILE &&
77 brw_inst_src1_da_reg_nr(devinfo, inst) == BRW_ARF_NULL;
78 }
79
80 static bool
81 src0_is_grf(const struct gen_device_info *devinfo, const brw_inst *inst)
82 {
83 return brw_inst_src0_reg_file(devinfo, inst) == BRW_GENERAL_REGISTER_FILE;
84 }
85
86 static unsigned
87 num_sources_from_inst(const struct gen_device_info *devinfo,
88 const brw_inst *inst)
89 {
90 const struct opcode_desc *desc =
91 brw_opcode_desc(devinfo, brw_inst_opcode(devinfo, inst));
92 unsigned math_function;
93
94 if (brw_inst_opcode(devinfo, inst) == BRW_OPCODE_MATH) {
95 math_function = brw_inst_math_function(devinfo, inst);
96 } else if (devinfo->gen < 6 &&
97 brw_inst_opcode(devinfo, inst) == BRW_OPCODE_SEND) {
98 if (brw_inst_sfid(devinfo, inst) == BRW_SFID_MATH) {
99 math_function = brw_inst_math_msg_function(devinfo, inst);
100 } else {
101 /* Send instructions are allowed to have null sources since they use
102 * the base_mrf field to specify which message register source.
103 */
104 return 0;
105 }
106 } else if (desc) {
107 return desc->nsrc;
108 } else {
109 return 0;
110 }
111
112 switch (math_function) {
113 case BRW_MATH_FUNCTION_INV:
114 case BRW_MATH_FUNCTION_LOG:
115 case BRW_MATH_FUNCTION_EXP:
116 case BRW_MATH_FUNCTION_SQRT:
117 case BRW_MATH_FUNCTION_RSQ:
118 case BRW_MATH_FUNCTION_SIN:
119 case BRW_MATH_FUNCTION_COS:
120 case BRW_MATH_FUNCTION_SINCOS:
121 case GEN8_MATH_FUNCTION_INVM:
122 case GEN8_MATH_FUNCTION_RSQRTM:
123 return 1;
124 case BRW_MATH_FUNCTION_FDIV:
125 case BRW_MATH_FUNCTION_POW:
126 case BRW_MATH_FUNCTION_INT_DIV_QUOTIENT_AND_REMAINDER:
127 case BRW_MATH_FUNCTION_INT_DIV_QUOTIENT:
128 case BRW_MATH_FUNCTION_INT_DIV_REMAINDER:
129 return 2;
130 default:
131 unreachable("not reached");
132 }
133 }
134
135 static bool
136 is_unsupported_inst(const struct gen_device_info *devinfo,
137 const brw_inst *inst)
138 {
139 return brw_opcode_desc(devinfo, brw_inst_opcode(devinfo, inst)) == NULL;
140 }
141
142 bool
143 brw_validate_instructions(const struct brw_codegen *p, int start_offset,
144 struct annotation_info *annotation)
145 {
146 const struct gen_device_info *devinfo = p->devinfo;
147 const void *store = p->store;
148 bool valid = true;
149
150 for (int src_offset = start_offset; src_offset < p->next_insn_offset;
151 src_offset += sizeof(brw_inst)) {
152 struct string error_msg = { .str = NULL, .len = 0 };
153 const brw_inst *inst = store + src_offset;
154
155 switch (num_sources_from_inst(devinfo, inst)) {
156 case 3:
157 /* Nothing to test. 3-src instructions can only have GRF sources, and
158 * there's no bit to control the file.
159 */
160 break;
161 case 2:
162 ERROR_IF(src1_is_null(devinfo, inst), "src1 is null");
163 /* fallthrough */
164 case 1:
165 ERROR_IF(src0_is_null(devinfo, inst), "src0 is null");
166 break;
167 case 0:
168 default:
169 break;
170 }
171
172 if (is_unsupported_inst(devinfo, inst)) {
173 ERROR("Instruction not supported on this Gen");
174 } else {
175 }
176
177 if (brw_inst_opcode(devinfo, inst) == BRW_OPCODE_SEND) {
178 ERROR_IF(brw_inst_src0_address_mode(devinfo, inst) !=
179 BRW_ADDRESS_DIRECT, "send must use direct addressing");
180
181 if (devinfo->gen >= 7) {
182 ERROR_IF(!src0_is_grf(devinfo, inst), "send from non-GRF");
183 ERROR_IF(brw_inst_eot(devinfo, inst) &&
184 brw_inst_src0_da_reg_nr(devinfo, inst) < 112,
185 "send with EOT must use g112-g127");
186 }
187 }
188
189 if (error_msg.str && annotation) {
190 annotation_insert_error(annotation, src_offset, error_msg.str);
191 }
192 valid = valid && error_msg.len == 0;
193 free(error_msg.str);
194 }
195
196 return valid;
197 }