summaryrefslogtreecommitdiff
path: root/src/freedreno/ir3/tests/disasm.c
blob: ec203c90d11561899ac5d7dea37116d05198294b (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
/*
 * Copyright © 2016 Broadcom
 * Copyright © 2020 Google LLC
 *
 * 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.
 */

/* Unit test for disassembly of instructions.
 *
 * The goal is to take instructions we've seen the blob produce, and test that
 * we can disassemble them correctly.  For the next person investigating the
 * behavior of this instruction, please include the testcase it was generated
 * from, and the qcom disassembly as a comment if it differs from what we
 * produce.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util/macros.h"
#include "disasm.h"

#include "ir3.h"
#include "ir3_assembler.h"
#include "ir3_shader.h"

#define INSTR_5XX(i, d) { .gpu_id = 540, .instr = #i, .expected = d }
#define INSTR_6XX(i, d) { .gpu_id = 630, .instr = #i, .expected = d }

static const struct test {
	int gpu_id;
	const char *instr;
	const char *expected;
} tests[] = {
	/* cat0 */
	INSTR_6XX(00000000_00000000, "nop"),
	INSTR_6XX(00000200_00000000, "(rpt2)nop"),
	INSTR_6XX(03000000_00000000, "end"),
	INSTR_6XX(00800000_00000004, "br p0.x, #4"),
	INSTR_6XX(00900000_00000003, "br !p0.x, #3"),
	INSTR_6XX(03820000_00000015, "shps #21"), /* emit */
	INSTR_6XX(04021000_00000000, "(ss)shpe"), /* cut */
	INSTR_6XX(02820000_00000014, "getone #20"), /* kill p0.x */
	INSTR_6XX(00906020_00000007, "brao !p0.x, !p0.y, #7"),
	INSTR_6XX(00804040_00000003, "braa p0.x, p0.y, #3"),
	INSTR_6XX(07820000_00000000, "prede"),
	INSTR_6XX(00800063_0000001e, "brac.3 #30"),
	INSTR_6XX(06820000_00000000, "predt p0.x"),
	INSTR_6XX(07020000_00000000, "predf p0.x"),
	INSTR_6XX(07820000_00000000, "prede"),

	/* cat1 */
	INSTR_6XX(20244000_00000020, "mov.f32f32 r0.x, c8.x"),
	INSTR_6XX(20200000_00000020, "mov.f16f16 hr0.x, hc8.x"),
	INSTR_6XX(20150000_00000000, "cov.s32s16 hr0.x, r0.x"),
	INSTR_6XX(20156004_00000c11, "(ul)mov.s32s32 r1.x, c<a0.x + 17>"),
	INSTR_6XX(201100f4_00000000, "mova a0.x, hr0.x"),
	INSTR_6XX(20244905_00000410, "(rpt1)mov.f32f32 r1.y, (r)c260.x"),
	INSTR_6XX(20174004_00000008, "mov.s32s32 r<a0.x + 4>, r2.x"),
	INSTR_6XX(20130000_00000005, "mov.s16s16 hr<a0.x>, hr1.y"),
	INSTR_6XX(20110004_00000800, "mov.s16s16 hr1.x, hr<a0.x>"),
	/* dEQP-VK.subgroups.ballot.compute.compute */
	INSTR_6XX(260cc3c0_00000000, "movmsk.w128 r48.x"), /* movmsk.w128 sr48.x */

	INSTR_6XX(20510005_0000ffff, "mov.s16s16 hr1.y, -1"),
	INSTR_6XX(20400005_00003900, "mov.f16f16 hr1.y, h(0.625000)"),
	INSTR_6XX(20400006_00003800, "mov.f16f16 hr1.z, h(0.500000)"),
	INSTR_6XX(204880f5_00000000, "mova1 a1.x, 0"),

	/* cat2 */
	INSTR_6XX(40104002_0c210001, "add.f hr0.z, r0.y, c<a0.x + 33>"),
	INSTR_6XX(40b80804_10408004, "(nop3) cmps.f.lt r1.x, (abs)r1.x, c16.x"),
	INSTR_6XX(47308a02_00002000, "(rpt2)bary.f (ei)r0.z, (r)0, r0.x"),
	INSTR_6XX(43480801_00008001, "(nop3) absneg.s hr0.y, (abs)hr0.y"),
	INSTR_6XX(50600004_2c010004, "(sy)mul.f hr1.x, hr1.x, h(0.5)"),
	INSTR_6XX(42280807_27ff0000, "(nop3) add.s hr1.w, hr0.x, h(-1)"),
	INSTR_6XX(40a500f8_2c000004, "cmps.f.ne p0.x, hr1.x, h(0.0)"),
	INSTR_6XX(438000f8_20010009, "and.b p0.x, hr2.y, h(1)"),
	INSTR_6XX(438000f9_00020001, "and.b p0.y, hr0.y, hr0.z"),
	INSTR_6XX(40080902_50200006, "(rpt1)add.f hr0.z, (r)hr1.z, (neg)(r)hc8.x"),
	INSTR_6XX(42380c01_00040001, "(sat)(nop3) add.s r0.y, r0.y, r1.x"),
	INSTR_6XX(42480000_48801086, "(nop2) sub.u hr0.x, hc33.z, (neg)hr<a0.x + 128>"),
	INSTR_6XX(46b00001_00001020, "clz.b r0.y, c8.x"),
	INSTR_6XX(46700009_00000009, "bfrev.b r2.y, r2.y"),

	/* cat3 */
	INSTR_6XX(66000000_10421041, "sel.f16 hr0.x, hc16.y, hr0.x, hc16.z"),
	INSTR_6XX(64848109_109a9099, "(rpt1)sel.b32 r2.y, c38.y, (r)r2.y, c38.z"),
	INSTR_6XX(64810904_30521036, "(rpt1)sel.b32 r1.x, (r)c13.z, r0.z, (r)c20.z"),
	INSTR_6XX(64818902_20041032, "(rpt1)sel.b32 r0.z, (r)c12.z, r0.w, (r)r1.x"),
	INSTR_6XX(63820005_10315030, "mad.f32 r1.y, (neg)c12.x, r1.x, c12.y"),
	INSTR_6XX(62050009_00091000, "mad.u24 r2.y, c0.x, r2.z, r2.y"),
	INSTR_6XX(61828008_00081033, "madsh.m16 r2.x, c12.w, r1.y, r2.x"),

	/* cat4 */
	INSTR_6XX(8010000a_00000003, "rcp r2.z, r0.w"),

	/* cat5 */
	/* dEQP-VK.glsl.derivate.dfdx.uniform_if.float_mediump */
	INSTR_6XX(a3801102_00000001, "dsx (f32)(x)r0.z, r0.x"), /* dsx (f32)(xOOO)r0.z, r0.x */
	/* dEQP-VK.glsl.derivate.dfdy.uniform_if.float_mediump */
	INSTR_6XX(a3c01102_00000001, "dsy (f32)(x)r0.z, r0.x"), /* dsy (f32)(xOOO)r0.z, r0.x */
	/* dEQP-VK.glsl.derivate.dfdxfine.uniform_loop.float_highp */
	INSTR_6XX(a6001105_00000001, "dsxpp.1 (x)r1.y, r0.x"), /* dsxpp.1 (xOOO)r1.y, r0.x */
	INSTR_6XX(a6201105_00000001, "dsxpp.1.p (x)r1.y, r0.x"), /* dsxpp.1 (xOOO)r1.y, r0.x */

	INSTR_6XX(a2802f00_00000001, "getsize (u16)(xyzw)hr0.x, r0.x, t#0"),
	INSTR_6XX(a0c89f04_c4600005, "sam.base1 (f32)(xyzw)r1.x, r0.z, s#3, t#2"),  /* sam.s2en.mode6.base1 (f32)(xyzw)r1.x, r0.z, 35 */
	INSTR_6XX(a1c85f00_c0200005, "getlod.base0 (s32)(xyzw)r0.x, r0.z, s#1, t#0"),  /* getlod.s2en.mode6.base0 (s32)(xyzw)r0.x, r0.z, 1 */
	INSTR_6XX(a1000f00_00000004, "samb (f16)(xyzw)hr0.x, hr0.z, hr0.x, s#0, t#0"),
	INSTR_6XX(a1000f00_00000003, "samb (f16)(xyzw)hr0.x, r0.y, r0.x, s#0, t#0"),
	INSTR_6XX(a0c00f00_04400002, "sam (f16)(xyzw)hr0.x, hr0.y, s#2, t#2"),
	INSTR_6XX(a6c02f00_00000000, "rgetinfo (u16)(xyzw)hr0.x"),
	INSTR_6XX(a3482f08_c0000000, "getinfo.base0 (u16)(xyzw)hr2.x, t#0"),


	/* cat6 */

	INSTR_6XX(c0c00000_00000000, "stg.f16 g[hr0.x], hr0.x, hr0.x"),
	/* dEQP-GLES31.functional.tessellation.invariance.outer_edge_symmetry.isolines_equal_spacing_ccw */
	INSTR_6XX(c0d20906_02800004, "stg.f32 g[r1.x+r1.z], r0.z, 2"), /* stg.a.f32 g[r1.x+(r1.z<<2)], r0.z, 2 */

	/* TODO: We don't support disasm of stc yet and produce a stgb instead
	 * (same as their disasm does for other families.  They're used as part
	 * uniforms setup, followed by a shpe and then a load of the constant that
	 * was stored in the dynamic part of the shader.
	 */
	/* dEQP-GLES3.functional.ubo.random.basic_arrays.0 */
	/* INSTR_6XX(c7020020_01800000, "stc c[32], r0.x, 1"), */
	/* dEQP-VK.image.image_size.cube_array.readonly_writeonly_1x1x12 */
	/* INSTR_6XX(c7060020_03800000, "stc c[32], r0.x, 3"), */

	/* dEQP-VK.image.image_size.cube_array.readonly_writeonly_1x1x12 */
	INSTR_6XX(c0260200_03676100, "stib.b.untyped.1d.u32.3.imm.base0 r0.x, r0.w, 1"), /* stib.untyped.u32.1d.3.mode4.base0 r0.x, r0.w, 1 */
	/* dEQP-VK.texture.filtering.cube.formats.a8b8g8r8_srgb_nearest_mipmap_nearest.txt */
	INSTR_6XX(c0220200_0361b801, "ldib.b.typed.1d.f32.4.imm r0.x, r0.w, 1"), /* ldib.f32.1d.4.mode0.base0 r0.x, r0.w, 1 */

	/* dEQP-GLES31.functional.tessellation.invariance.outer_edge_symmetry.isolines_equal_spacing_ccw */
	INSTR_6XX(c2c21100_04800006, "stlw.f32 l[r2.x], r0.w, 4"),
	INSTR_6XX(c2c20f00_01800004, "stlw.f32 l[r1.w], r0.z, 1"),
	INSTR_6XX(c2860003_02808011, "ldlw.u32 r0.w, l[r0.z+8], 2"),

	/* dEQP-VK.compute.basic.shared_var_single_group */
	INSTR_6XX(c1060500_01800008, "stl.u32 l[r0.z], r1.x, 1"),
	INSTR_6XX(c0460001_01804001, "ldl.u32 r0.y, l[r0.y], 1"),

	INSTR_6XX(c0860018_03820001, "ldp.u32 r6.x, p[r2.x], 3"),
	INSTR_6XX(c0420002_01808019, "ldl.f32 r0.z, l[r0.z+12], 1"),
	INSTR_6XX(c1021710_04800000, "stl.f32 l[r2.w+16], r0.x, 4"),
	INSTR_6XX(d7c60011_03c00000, "(sy)ldlv.u32 r4.y, l[0], 3"),

	/* resinfo */
	INSTR_6XX(c0260000_0063c200, "resinfo.b.untyped.2d.u32.1.imm r0.x, 0"), /* resinfo.u32.2d.mode0.base0 r0.x, 0 */
	/* dEQP-GLES31.functional.image_load_store.buffer.image_size.writeonly_7.txt */
	INSTR_6XX(c0260000_0063c000, "resinfo.b.untyped.1d.u32.1.imm r0.x, 0"), /* resinfo.u32.1d.mode0.base0 r0.x, 0 */
	/* dEQP-VK.image.image_size.2d.readonly_12x34.txt */
	INSTR_6XX(c0260000_0063c300, "resinfo.b.untyped.2d.u32.1.imm.base0 r0.x, 0"), /* resinfo.u32.2d.mode4.base0 r0.x, 0 */
	/* dEQP-GLES31.functional.image_load_store.buffer.image_size.readonly_writeonly_7 */
	INSTR_5XX(c3e60000_00000e00, "resinfo.4d r0.x, g[0]"), /* resinfo.u32.1dtype r0.x, 0 */
	/* dEQP-GLES31.functional.image_load_store.2d.image_size.readonly_writeonly_32x32.txt */
	INSTR_5XX(c3e60000_00000200, "resinfo.2d r0.x, g[0]"), /* resinfo.u32.2d r0.x, 0 */
	/* dEQP-GLES31.functional.image_load_store.3d.image_size.readonly_writeonly_12x34x56 */
	INSTR_5XX(c3e60000_00000c00, "resinfo.3d r0.x, g[0]"), /* resinfo.u32.3d r0.x, 0 */

	/* ldgb */
	/* dEQP-GLES31.functional.ssbo.layout.single_basic_type.packed.mediump_vec4 */
	INSTR_5XX(c6e20000_06003600, "ldgb.untyped.4d.f32.4 r0.x, g[0], r0.x, r1.z"), /* ldgb.a.untyped.1dtype.f32.4 r0.x, g[r0.x], r1.z, 0 */
	/* dEQP-GLES31.functional.ssbo.layout.single_basic_type.packed.mediump_ivec4 */
	INSTR_5XX(c6ea0000_06003600, "ldgb.untyped.4d.s32.4 r0.x, g[0], r0.x, r1.z"), /* ldgb.a.untyped.1dtype.s32.4 r0.x, g[r0.x], r1.z, 0 */
	/* dEQP-GLES31.functional.ssbo.layout.single_basic_type.packed.mediump_float */
	INSTR_5XX(c6e20000_02000600, "ldgb.untyped.4d.f32.1 r0.x, g[0], r0.x, r0.z"), /* ldgb.a.untyped.1dtype.f32.1 r0.x, g[r0.x], r0.z, 0 */
	/* dEQP-GLES31.functional.ssbo.layout.random.vector_types.0 */
	INSTR_5XX(c6ea0008_14002600, "ldgb.untyped.4d.s32.3 r2.x, g[0], r0.x, r5.x"), /* ldgb.a.untyped.1dtype.s32.3 r2.x, g[r0.x], r5.x, 0 */
	INSTR_5XX(c6ea0204_1401a600, "ldgb.untyped.4d.s32.3 r1.x, g[1], r1.z, r5.x"), /* ldgb.a.untyped.1dtype.s32.3 r1.x, g[r1.z], r5.x, 1 */

	/* discard stuff */
	INSTR_6XX(42b400f8_20010004, "cmps.s.eq p0.x, r1.x, 1"),
	INSTR_6XX(02800000_00000000, "kill p0.x"),

	/* Immediates */
	INSTR_6XX(40100007_68000008, "add.f r1.w, r2.x, (neg)(0.0)"),
	INSTR_6XX(40100007_68010008, "add.f r1.w, r2.x, (neg)(0.5)"),
	INSTR_6XX(40100007_68020008, "add.f r1.w, r2.x, (neg)(1.0)"),
	INSTR_6XX(40100007_68030008, "add.f r1.w, r2.x, (neg)(2.0)"),
	INSTR_6XX(40100007_68040008, "add.f r1.w, r2.x, (neg)(e)"),
	INSTR_6XX(40100007_68050008, "add.f r1.w, r2.x, (neg)(pi)"),
	INSTR_6XX(40100007_68060008, "add.f r1.w, r2.x, (neg)(1/pi)"),
	INSTR_6XX(40100007_68070008, "add.f r1.w, r2.x, (neg)(1/log2(e))"),
	INSTR_6XX(40100007_68080008, "add.f r1.w, r2.x, (neg)(log2(e))"),
	INSTR_6XX(40100007_68090008, "add.f r1.w, r2.x, (neg)(1/log2(10))"),
	INSTR_6XX(40100007_680a0008, "add.f r1.w, r2.x, (neg)(log2(10))"),
	INSTR_6XX(40100007_680b0008, "add.f r1.w, r2.x, (neg)(4.0)"),

	/* LDC.  Our disasm differs greatly from qcom here, and we've got some
	 * important info they lack(?!), but same goes the other way.
	 */
	/* dEQP-GLES31.functional.shaders.opaque_type_indexing.ubo.uniform_fragment */
	INSTR_6XX(c0260000_00c78040, "ldc.offset0.1.uniform r0.x, r0.x, r0.x"), /* ldc.1.mode1.base0 r0.x, 0, r0.x */
	INSTR_6XX(c0260201_00c78040, "ldc.offset0.1.uniform r0.y, r0.x, r0.y"), /* ldc.1.mode1.base0 r0.y, 0, r0.y */
	/* dEQP-GLES31.functional.shaders.opaque_type_indexing.ubo.dynamically_uniform_fragment  */
	INSTR_6XX(c0260000_00c78080, "ldc.offset0.1.nonuniform r0.x, r0.x, r0.x"), /* ldc.1.mode2.base0 r0.x, 0, r0.x */
	INSTR_6XX(c0260201_00c78080, "ldc.offset0.1.nonuniform r0.y, r0.x, r0.y"), /* ldc.1.mode2.base0 r0.y, 0, r0.y */
	/* custom shaders, loading .x, .y, .z, .w from an array of vec4 in block 0 */
	INSTR_6XX(c0260000_00478000, "ldc.offset0.1.imm r0.x, r0.x, 0"), /* ldc.1.mode0.base0 r0.x, r0.x, 0 */
	INSTR_6XX(c0260000_00478200, "ldc.offset1.1.imm r0.x, r0.x, 0"), /* ldc.1.mode0.base0 r0.x, r0.x, 0 */
	INSTR_6XX(c0260000_00478400, "ldc.offset2.1.imm r0.x, r0.x, 0"), /* ldc.1.mode0.base0 r0.x, r0.x, 0 */
	INSTR_6XX(c0260000_00478600, "ldc.offset3.1.imm r0.x, r0.x, 0"), /* ldc.1.mode0.base0 r0.x, r0.x, 0 */

	/* dEQP-VK.glsl.struct.local.nested_struct_array_dynamic_index_fragment */
	INSTR_6XX(c1425b50_01803e02, "stp.f32 p[r11.y-176], r0.y, 1"),
	INSTR_6XX(c1425b98_02803e14, "stp.f32 p[r11.y-104], r2.z, 2"),
	INSTR_6XX(c1465ba0_01803e2a, "stp.u32 p[r11.y-96], r5.y, 1"),
	INSTR_6XX(c0860008_01860001, "ldp.u32 r2.x, p[r6.x], 1"),
	/* Custom stp based on above to catch a disasm bug. */
	INSTR_6XX(c1465b00_0180022a, "stp.u32 p[r11.y+256], r5.y, 1"),

	/* Atomic: */
	INSTR_5XX(c4d60002_00008001, "atomic.inc.untyped.1d.u32.1.g r0.z, g[0], r0.z, r0.x, r0.x"),
	INSTR_5XX(c4160205_03000001, "atomic.add.untyped.1d.u32.1.g r1.y, g[1], r0.x, r0.w, r0.x"),
	INSTR_6XX(d5c60003_03008001, "(sy)atomic.max.untyped.1d.u32.1.l r0.w, l[r0.z], r0.w"),

	/* Bindless atomic: */
	INSTR_6XX(c03a0003_01640001, "atomic.add.b.untyped.1d.s32.1.imm r0.w, r0.y, 0"), /* atomic.b.add.g.s32.1d.mode0.base0 r0.w,r0.y,0 */
	INSTR_6XX(c03a0003_01660001, "atomic.and.b.untyped.1d.s32.1.imm r0.w, r0.y, 0"), /* atomic.b.and.g.s32.1d.mode0.base0 r0.w,r0.y,0 */
	INSTR_6XX(c0360000_0365c801, "atomic.max.b.typed.1d.u32.1.imm r0.x, r0.w, 0"),   /* atomic.b.max.g.u32.1d.mode0.base0 r0.x,r0.w,0 */

	/* dEQP-GLES31.functional.shaders.opaque_type_indexing.sampler.const_literal.fragment.sampler2d */
	INSTR_6XX(a0c01f04_0cc00005, "sam (f32)(xyzw)r1.x, r0.z, s#6, t#6"),
	/* dEQP-GLES31.functional.shaders.opaque_type_indexing.sampler.uniform.fragment.sampler2d (looks like maybe the compiler didn't figure out */
	INSTR_6XX(a0c81f07_0100000b, "sam.s2en (f32)(xyzw)r1.w, r1.y, hr2.x"), /* sam.s2en.mode0 (f32)(xyzw)r1.w, r1.y, hr2.x */
	/* dEQP-GLES31.functional.shaders.opaque_type_indexing.sampler.dynamically_uniform.fragment.sampler2d */
	INSTR_6XX(a0c81f07_8100000b, "sam.s2en.uniform (f32)(xyzw)r1.w, r1.y, hr2.x"), /* sam.s2en.mode4 (f32)(xyzw)r1.w, r1.y, hr2.x */

	/* Custom test since we've never seen the blob emit these. */
	INSTR_6XX(c0260004_00490000, "getspid.u32 r1.x"),
	INSTR_6XX(c0260005_00494000, "getwid.u32 r1.y"),
};

static void
trim(char *string)
{
	for (int len = strlen(string); len > 0 && string[len - 1] == '\n'; len--)
		string[len - 1] = 0;
}

int
main(int argc, char **argv)
{
	int retval = 0;
	int decode_fails = 0, asm_fails = 0, encode_fails = 0;
	const int output_size = 4096;
	char *disasm_output = malloc(output_size);
	FILE *fdisasm = fmemopen(disasm_output, output_size, "w+");
	if (!fdisasm) {
		fprintf(stderr, "failed to fmemopen\n");
		return 1;
	}

	struct ir3_compiler *compilers[10] = {};

	for (int i = 0; i < ARRAY_SIZE(tests); i++) {
		const struct test *test = &tests[i];
		printf("Testing a%d %s: \"%s\"...\n",
				test->gpu_id, test->instr, test->expected);

		rewind(fdisasm);
		memset(disasm_output, 0, output_size);

		/*
		 * Test disassembly:
		 */

		uint32_t code[2] = {
			strtoll(&test->instr[9], NULL, 16),
			strtoll(&test->instr[0], NULL, 16),
		};
		disasm_a3xx(code, ARRAY_SIZE(code), 0, fdisasm, test->gpu_id);
		fflush(fdisasm);

		trim(disasm_output);

		if (strcmp(disasm_output, test->expected) != 0) {
			printf("FAIL: disasm\n");
			printf("  Expected: \"%s\"\n", test->expected);
			printf("  Got:      \"%s\"\n", disasm_output);
			retval = 1;
			decode_fails++;
			continue;
		}

		/*
		 * Test assembly, which should result in the identical binary:
		 */

		unsigned gen = test->gpu_id / 100;
		if (!compilers[gen]) {
			compilers[gen] = ir3_compiler_create(NULL, test->gpu_id);
		}

		FILE *fasm = fmemopen((void *)test->expected, strlen(test->expected), "r");

		struct ir3_kernel_info info = {};
		struct ir3_shader *shader = ir3_parse_asm(compilers[gen], &info, fasm);
		fclose(fasm);
		if (!shader) {
			printf("FAIL: assembler failed\n");
			asm_fails++;
			/* Until the assembler is more complete, don't count this as
			 * a failure, but skip checking that assembled binary matches.
			 */
			continue;
		}

		struct ir3_shader_variant *v = shader->variants;
		if (memcmp(v->bin, code, sizeof(code))) {
			printf("FAIL: assembler\n");
			printf("  Expected: %08x_%08x\n", code[1], code[0]);
			printf("  Got:      %08x_%08x\n", v->bin[1], v->bin[0]);
			retval = 1;
			encode_fails++;
		}

		ir3_shader_destroy(shader);
	}

	if (decode_fails)
		printf("%d/%d decode fails\n", decode_fails, (int)ARRAY_SIZE(tests));
	if (asm_fails)
		printf("%d/%d assembler fails\n", asm_fails, (int)ARRAY_SIZE(tests));
	if (encode_fails)
		printf("%d/%d encode fails\n", encode_fails, (int)ARRAY_SIZE(tests));

	for (unsigned i = 0; i < ARRAY_SIZE(compilers); i++) {
		if (!compilers[i])
			continue;
		ir3_compiler_destroy(compilers[i]);
	}

	fclose(fdisasm);
	free(disasm_output);

	return retval;
}