2 * Copyright © 2012 Intel Corporation
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:
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
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
27 #include "glsl/ralloc.h"
28 #include "brw_context.h"
32 test_compact_instruction(struct brw_compile
*p
, struct brw_instruction src
)
34 struct brw_context
*brw
= p
->brw
;
36 struct brw_compact_instruction dst
;
37 memset(&dst
, 0xd0, sizeof(dst
));
39 if (brw_try_compact_instruction(p
, &dst
, &src
)) {
40 struct brw_instruction uncompacted
;
42 brw_uncompact_instruction(brw
, &uncompacted
, &dst
);
43 if (memcmp(&uncompacted
, &src
, sizeof(src
))) {
44 brw_debug_compact_uncompact(brw
, &src
, &uncompacted
);
48 struct brw_compact_instruction unchanged
;
49 memset(&unchanged
, 0xd0, sizeof(unchanged
));
50 /* It's not supposed to change dst unless it compacted. */
51 if (memcmp(&unchanged
, &dst
, sizeof(dst
))) {
52 fprintf(stderr
, "Failed to compact, but dst changed\n");
53 fprintf(stderr
, " Instruction: ");
54 brw_disassemble_inst(stderr
, &src
, brw
->gen
, false);
63 * When doing fuzz testing, pad bits won't round-trip.
65 * This sort of a superset of skip_bit, which is testing for changing bits that
66 * aren't worth testing for fuzzing. We also just want to clear bits that
67 * become meaningless once fuzzing twiddles a related bit.
70 clear_pad_bits(const struct brw_context
*brw
, struct brw_instruction
*inst
)
72 if (brw_inst_opcode(brw
, inst
) != BRW_OPCODE_SEND
&&
73 brw_inst_opcode(brw
, inst
) != BRW_OPCODE_SENDC
&&
74 brw_inst_opcode(brw
, inst
) != BRW_OPCODE_BREAK
&&
75 brw_inst_opcode(brw
, inst
) != BRW_OPCODE_CONTINUE
&&
76 brw_inst_src0_reg_file(brw
, inst
) != BRW_IMMEDIATE_VALUE
&&
77 brw_inst_src1_reg_file(brw
, inst
) != BRW_IMMEDIATE_VALUE
) {
78 brw_inst_set_bits(inst
, 127, 111, 0);
83 skip_bit(const struct brw_context
*brw
, struct brw_instruction
*src
, int bit
)
89 /* The compact bit -- uncompacted can't have it set. */
98 if (bit
>= 90 && bit
<= 95)
101 /* sometimes these are pad bits. */
102 if (brw_inst_opcode(brw
, src
) != BRW_OPCODE_SEND
&&
103 brw_inst_opcode(brw
, src
) != BRW_OPCODE_SENDC
&&
104 brw_inst_opcode(brw
, src
) != BRW_OPCODE_BREAK
&&
105 brw_inst_opcode(brw
, src
) != BRW_OPCODE_CONTINUE
&&
106 brw_inst_src0_reg_file(brw
, src
) != BRW_IMMEDIATE_VALUE
&&
107 brw_inst_src1_reg_file(brw
, src
) != BRW_IMMEDIATE_VALUE
&&
116 test_fuzz_compact_instruction(struct brw_compile
*p
,
117 struct brw_instruction src
)
119 for (int bit0
= 0; bit0
< 128; bit0
++) {
120 if (skip_bit(p
->brw
, &src
, bit0
))
123 for (int bit1
= 0; bit1
< 128; bit1
++) {
124 struct brw_instruction instr
= src
;
125 uint32_t *bits
= (uint32_t *)&instr
;
127 if (skip_bit(p
->brw
, &src
, bit1
))
130 bits
[bit0
/ 32] ^= (1 << (bit0
& 31));
131 bits
[bit1
/ 32] ^= (1 << (bit1
& 31));
133 clear_pad_bits(p
->brw
, &instr
);
135 if (!test_compact_instruction(p
, instr
)) {
136 printf(" twiddled bits for fuzzing %d, %d\n", bit0
, bit1
);
146 gen_ADD_GRF_GRF_GRF(struct brw_compile
*p
)
148 struct brw_reg g0
= brw_vec8_grf(0, 0);
149 struct brw_reg g2
= brw_vec8_grf(2, 0);
150 struct brw_reg g4
= brw_vec8_grf(4, 0);
152 brw_ADD(p
, g0
, g2
, g4
);
156 gen_ADD_GRF_GRF_IMM(struct brw_compile
*p
)
158 struct brw_reg g0
= brw_vec8_grf(0, 0);
159 struct brw_reg g2
= brw_vec8_grf(2, 0);
161 brw_ADD(p
, g0
, g2
, brw_imm_f(1.0));
165 gen_ADD_GRF_GRF_IMM_d(struct brw_compile
*p
)
167 struct brw_reg g0
= retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_D
);
168 struct brw_reg g2
= retype(brw_vec8_grf(2, 0), BRW_REGISTER_TYPE_D
);
170 brw_ADD(p
, g0
, g2
, brw_imm_d(1));
174 gen_MOV_GRF_GRF(struct brw_compile
*p
)
176 struct brw_reg g0
= brw_vec8_grf(0, 0);
177 struct brw_reg g2
= brw_vec8_grf(2, 0);
183 gen_ADD_MRF_GRF_GRF(struct brw_compile
*p
)
185 struct brw_reg m6
= brw_vec8_reg(BRW_MESSAGE_REGISTER_FILE
, 6, 0);
186 struct brw_reg g2
= brw_vec8_grf(2, 0);
187 struct brw_reg g4
= brw_vec8_grf(4, 0);
189 brw_ADD(p
, m6
, g2
, g4
);
193 gen_ADD_vec1_GRF_GRF_GRF(struct brw_compile
*p
)
195 struct brw_reg g0
= brw_vec1_grf(0, 0);
196 struct brw_reg g2
= brw_vec1_grf(2, 0);
197 struct brw_reg g4
= brw_vec1_grf(4, 0);
199 brw_ADD(p
, g0
, g2
, g4
);
203 gen_PLN_MRF_GRF_GRF(struct brw_compile
*p
)
205 struct brw_reg m6
= brw_vec8_reg(BRW_MESSAGE_REGISTER_FILE
, 6, 0);
206 struct brw_reg interp
= brw_vec1_grf(2, 0);
207 struct brw_reg g4
= brw_vec8_grf(4, 0);
209 brw_PLN(p
, m6
, interp
, g4
);
213 gen_f0_0_MOV_GRF_GRF(struct brw_compile
*p
)
215 struct brw_reg g0
= brw_vec8_grf(0, 0);
216 struct brw_reg g2
= brw_vec8_grf(2, 0);
218 brw_push_insn_state(p
);
219 brw_set_default_predicate_control(p
, true);
221 brw_pop_insn_state(p
);
224 /* The handling of f0.1 vs f0.0 changes between gen6 and gen7. Explicitly test
225 * it, so that we run the fuzzing can run over all the other bits that might
229 gen_f0_1_MOV_GRF_GRF(struct brw_compile
*p
)
231 struct brw_reg g0
= brw_vec8_grf(0, 0);
232 struct brw_reg g2
= brw_vec8_grf(2, 0);
234 brw_push_insn_state(p
);
235 brw_set_default_predicate_control(p
, true);
236 struct brw_instruction
*mov
= brw_MOV(p
, g0
, g2
);
237 brw_inst_set_flag_subreg_nr(p
->brw
, mov
, 1);
238 brw_pop_insn_state(p
);
242 void (*func
)(struct brw_compile
*p
);
245 { gen_ADD_GRF_GRF_GRF
},
246 { gen_ADD_GRF_GRF_IMM
},
247 { gen_ADD_GRF_GRF_IMM_d
},
248 { gen_ADD_MRF_GRF_GRF
},
249 { gen_ADD_vec1_GRF_GRF_GRF
},
250 { gen_PLN_MRF_GRF_GRF
},
251 { gen_f0_0_MOV_GRF_GRF
},
252 { gen_f0_1_MOV_GRF_GRF
},
256 run_tests(struct brw_context
*brw
)
260 for (int i
= 0; i
< ARRAY_SIZE(tests
); i
++) {
261 for (int align_16
= 0; align_16
<= 1; align_16
++) {
262 struct brw_compile
*p
= rzalloc(NULL
, struct brw_compile
);
263 brw_init_compile(brw
, p
, p
);
265 brw_set_default_predicate_control(p
, BRW_PREDICATE_NONE
);
267 brw_set_default_access_mode(p
, BRW_ALIGN_16
);
269 brw_set_default_access_mode(p
, BRW_ALIGN_1
);
272 assert(p
->nr_insn
== 1);
274 if (!test_compact_instruction(p
, p
->store
[0])) {
279 if (!test_fuzz_compact_instruction(p
, p
->store
[0])) {
292 main(int argc
, char **argv
)
294 struct brw_context
*brw
= calloc(1, sizeof(*brw
));
298 for (brw
->gen
= 6; brw
->gen
<= 7; brw
->gen
++) {
299 fail
|= run_tests(brw
);