Debian Codegraph @

src/asahi/compiler/agx_opcodes.py

raw
1 """
2 Copyright 2021 Alyssa Rosenzweig
3 # SPDX-License-Identifier: MIT
4 """
5
6 opcodes = {}
7 immediates = {}
8 enums = {}
9
10 VARIABLE = ~0
11
12 class Opcode(object):
13 def __init__(self, name, dests, srcs, imms, is_float, can_eliminate,
14 can_reorder, schedule_class, encoding):
15 self.name = name
16 self.dests = dests if dests != VARIABLE else 0
17 self.srcs = srcs if srcs != VARIABLE else 0
18 self.variable_srcs = (srcs == VARIABLE)
19 self.variable_dests = (dests == VARIABLE)
20 self.imms = imms
21 self.is_float = is_float
22 self.can_eliminate = can_eliminate
23 self.can_reorder = can_reorder
24 self.schedule_class = schedule_class
25 self.encoding = encoding
26
27 class Immediate(object):
28 def __init__(self, name, ctype):
29 self.name = name
30 self.ctype = ctype
31
32 class Encoding(object):
33 def __init__(self, description):
34 (exact, length_short, length_long) = description
35
36 # Convenience
37 if length_long is None:
38 length_long = length_short
39
40 self.exact = exact
41 self.length_short = length_short
42 self.extensible = length_short != length_long
43
44 if self.extensible:
45 assert(length_long == length_short + (4 if length_short > 8 else 2))
46
47 def op(name, encoding, dests = 1, srcs = 0, imms = [], is_float = False,
48 can_eliminate = True, can_reorder = True, schedule_class = "none"):
49 encoding = Encoding(encoding) if encoding is not None else None
50
51 opcodes[name] = Opcode(name, dests, srcs, imms, is_float, can_eliminate,
52 can_reorder, schedule_class, encoding)
53
54 def immediate(name, ctype = "uint32_t"):
55 imm = Immediate(name, ctype)
56 immediates[name] = imm
57 return imm
58
59 def enum(name, value_dict):
60 enums[name] = value_dict
61 return immediate(name, "enum agx_" + name)
62
63 L = (1 << 15)
64 _ = None
65
66 FORMAT = immediate("format", "enum agx_format")
67 IMM = immediate("imm", "uint64_t")
68 WRITEOUT = immediate("writeout")
69 INDEX = immediate("index")
70 COMPONENT = immediate("component")
71 CHANNELS = immediate("channels")
72 TRUTH_TABLE = immediate("truth_table")
73 ROUND = immediate("round", "enum agx_round")
74 SHIFT = immediate("shift")
75 MASK = immediate("mask")
76 BFI_MASK = immediate("bfi_mask")
77 LOD_MODE = immediate("lod_mode", "enum agx_lod_mode")
78 PIXEL_OFFSET = immediate("pixel_offset")
79 STACK_SIZE = immediate("stack_size", 'int16_t')
80 EXPLICIT_COORDS = immediate("explicit_coords", "bool")
81
82 DIM = enum("dim", {
83 0: '1d',
84 1: '1d_array',
85 2: '2d',
86 3: '2d_array',
87 4: '2d_ms',
88 5: '3d',
89 6: 'cube',
90 7: 'cube_array',
91 8: '2d_ms_array',
92 })
93
94 GATHER = enum("gather", {
95 0b000: "none",
96 0b001: "r",
97 0b011: "g",
98 0b101: "b",
99 0b111: "a",
100 })
101
102 OFFSET = immediate("offset", "bool")
103 SHADOW = immediate("shadow", "bool")
104 SPARSE = immediate("sparse", "bool")
105 QUERY_LOD = immediate("query_lod", "bool")
106 COHERENT = immediate("coherent", "bool")
107 SCOREBOARD = immediate("scoreboard")
108 ICOND = immediate("icond", "enum agx_icond")
109 FCOND = immediate("fcond", "enum agx_fcond")
110 NEST = immediate("nest")
111 INVERT_COND = immediate("invert_cond")
112 NEST = immediate("nest")
113 TARGET = immediate("target", "agx_block *")
114 ZS = immediate("zs")
115 SR = enum("sr", {
116 0: 'threadgroup_position_in_grid.x',
117 1: 'threadgroup_position_in_grid.y',
118 2: 'threadgroup_position_in_grid.z',
119 4: 'threads_per_threadgroup.x',
120 5: 'threads_per_threadgroup.y',
121 6: 'threads_per_threadgroup.z',
122 8: 'dispatch_threads_per_threadgroup.x',
123 9: 'dispatch_threads_per_threadgroup.y',
124 10: 'dispatch_threads_per_threadgroup.z',
125 14: 'samples_log2',
126 20: 'core_id',
127 21: 'vm_slot',
128 48: 'thread_position_in_threadgroup.x',
129 49: 'thread_position_in_threadgroup.y',
130 50: 'thread_position_in_threadgroup.z',
131 51: 'thread_index_in_threadgroup',
132 52: 'thread_index_in_subgroup',
133 53: 'subgroup_index_in_threadgroup',
134 56: 'active_thread_index_in_quad',
135 57: 'total_active_threads_in_quad',
136 58: 'active_thread_index_in_subgroup',
137 59: 'total_active_threads_in_subgroup',
138 60: 'coverage_mask',
139 62: 'backfacing',
140 63: 'is_active_thread',
141 80: 'thread_position_in_grid.x',
142 81: 'thread_position_in_grid.y',
143 82: 'thread_position_in_grid.z',
144 124: 'input_sample_mask',
145 144: 'helper_op',
146 146: 'helper_arg_l',
147 147: 'helper_arg_h',
148 })
149
150 ATOMIC_OPC = enum("atomic_opc", {
151 0: 'add',
152 1: 'sub',
153 2: 'xchg',
154 3: 'cmpxchg',
155 4: 'umin',
156 5: 'imin',
157 6: 'umax',
158 7: 'imax',
159 8: 'and',
160 9: 'or',
161 10: 'xor',
162 })
163
164 INTERPOLATION = enum("interpolation", {
165 0: 'center',
166 1: 'sample',
167 2: 'centroid',
168 # We translate sample -> sample_register at pack time for simplicity
169 3: 'sample_register',
170 })
171
172 SIMD_OP = enum("simd_op", {
173 0b00000: 'and',
174 0b00001: 'fadd',
175 0b00010: 'or',
176 0b00011: 'fmul',
177 0b00100: 'xor',
178 0b00101: 'fmin',
179 0b00111: 'fmax',
180 0b10000: 'iadd',
181 0b10100: 'smin',
182 0b10110: 'smax',
183 0b11100: 'umin',
184 0b11110: 'umax',
185 })
186
187 FUNOP = lambda x: (x << 28)
188 FUNOP_MASK = FUNOP((1 << 14) - 1)
189
190 def funop(name, opcode, schedule_class = "none"):
191 op(name, (0x0A | (opcode << 28), 4, 6),
192 srcs = 1, is_float = True, schedule_class = schedule_class)
193
194 def iunop(name, opcode):
195 assert(opcode < 4)
196 op(name, (0x3E | (opcode << 26), 6, _), srcs = 1)
197
198 # Listing of opcodes
199 funop("floor", 0b000000)
200 funop("srsqrt", 0b000001)
201 funop("dfdx", 0b000100, schedule_class = "coverage")
202 funop("dfdy", 0b000110, schedule_class = "coverage")
203 funop("rcp", 0b001000)
204 funop("rsqrt", 0b001001)
205 funop("sin_pt_1", 0b001010)
206 funop("log2", 0b001100)
207 funop("exp2", 0b001101)
208 funop("sin_pt_2", 0b001110)
209 funop("ceil", 0b010000)
210 funop("trunc", 0b100000)
211 funop("roundeven", 0b110000)
212
213 iunop("bitrev", 0b01)
214 iunop("popcount", 0b10)
215 iunop("ffs", 0b11)
216
217 op("fadd", encoding = (0x2A, 4, 6), srcs = 2, is_float = True)
218 op("ffma", encoding = (0x3A, 6, 8), srcs = 3, is_float = True)
219 op("fmul", encoding = (0x1A, 4, 6), srcs = 2, is_float = True)
220 op("hadd", encoding = (0x26, 4, 6), srcs = 2, is_float = True)
221 op("hfma", encoding = (0x36, 6, 8), srcs = 3, is_float = True)
222 op("hmul", encoding = (0x16, 4, 6), srcs = 2, is_float = True)
223 op("mov_imm", encoding = (0x62, 6, 8), imms = [IMM])
224 op("iadd", encoding = (0x0E, 8, _), srcs = 2, imms = [SHIFT])
225 op("imad", encoding = (0x1E, 8, _), srcs = 3, imms = [SHIFT])
226 op("bfi", encoding = (0x2E, 8, _), srcs = 3, imms = [BFI_MASK])
227 op("bfeil", encoding = (0x2E | L, 8, _), srcs = 3, imms = [BFI_MASK])
228 op("extr", encoding = (0x2E | (0x1 << 26), 8, _), srcs = 3, imms = [BFI_MASK])
229 op("asr", encoding = (0x2E | L | (0x1 << 26), 8, _), srcs = 2)
230 op("icmpsel", encoding = (0x12, 8, 10), srcs = 4, imms = [ICOND])
231 op("fcmpsel", encoding = (0x02, 8, 10), srcs = 4, imms = [FCOND])
232
233 def subgroup_op(name, opc):
234 exact = 0b01101111 | L | (opc << 29)
235 op(name, encoding = (exact, 6, _), srcs = 1, imms = [SIMD_OP])
236
237 subgroup_op("quad_reduce", 0x0)
238 subgroup_op("simd_reduce", 0x1)
239 subgroup_op("quad_prefix", 0x2)
240 subgroup_op("simd_prefix", 0x3)
241
242 for window, w_bit in [('quad_', 0), ('', 1)]:
243 for s, shuffle in enumerate(['', '_xor', '_up', '_down']):
244 op(f"{window}shuffle{shuffle}",
245 encoding = (0b01101111 | (w_bit << 26) | (s << 38), 6, _),
246 srcs = 2)
247
248 # Pseudo-instruction ballotting a boolean
249 op(f"{window}ballot", _, srcs = 1)
250
251 for T, T_bit, cond in [('f', 0, FCOND), ('i', 1, ICOND)]:
252 op(f"{T}cmp_{window}ballot",
253 encoding = (0b0100010 | (T_bit << 4) | (w_bit << 48), 8, _),
254 srcs = 2, imms = [cond, INVERT_COND])
255
256 # Pseudo-instructions for compares returning 1/0
257 op("icmp", _, srcs = 2, imms = [ICOND, INVERT_COND])
258 op("fcmp", _, srcs = 2, imms = [FCOND, INVERT_COND])
259
260 # sources are coordinates, LOD, texture bindless base (zero for texture state
261 # registers), texture, sampler, shadow/offset
262 op("texture_sample",
263 encoding = (0x31, 8, 10), # XXX WRONG SIZE
264 srcs = 6, imms = [DIM, LOD_MODE, MASK, SCOREBOARD, OFFSET, SHADOW,
265 QUERY_LOD, GATHER, SPARSE])
266 for memory, can_reorder in [("texture", True), ("image", False)]:
267 coherency = [COHERENT] if not can_reorder else []
268 op(f"{memory}_load", encoding = (0x71, 8, 10), # XXX WRONG SIZE
269 srcs = 6, imms = [DIM, LOD_MODE, MASK, SCOREBOARD, OFFSET, SPARSE] + coherency,
270 can_reorder = can_reorder,
271 schedule_class = "none" if can_reorder else "load")
272
273 # sources are base, index
274 op("device_load",
275 encoding = (0x05, 6, 8),
276 srcs = 2, imms = [FORMAT, MASK, SHIFT, SCOREBOARD, COHERENT], can_reorder = False,
277 schedule_class = "load")
278
279 # sources are base (relative to workgroup memory), index
280 op("local_load",
281 encoding = (0b1101001, 6, 8),
282 srcs = 2, imms = [FORMAT, MASK], can_reorder = False,
283 schedule_class = "load")
284
285 # sources are value, base, index
286 # TODO: Consider permitting the short form
287 op("device_store",
288 encoding = (0x45 | (1 << 47), 8, _),
289 dests = 0, srcs = 3, imms = [FORMAT, MASK, SHIFT, SCOREBOARD, COHERENT], can_eliminate = False,
290 schedule_class = "store")
291
292 # sources are value, base, index
293 op("local_store",
294 encoding = (0b0101001, 6, 8),
295 dests = 0, srcs = 3, imms = [FORMAT, MASK],
296 can_eliminate=False, schedule_class = "store")
297
298 # sources are value, index
299 # TODO: Consider permitting the short form
300 op("uniform_store",
301 encoding = ((0b111 << 27) | 0b1000101 | (1 << 47), 8, _),
302 dests = 0, srcs = 2, imms = [MASK], can_eliminate = False)
303
304 # sources are base, offset
305 for kind, bit in [("tex", 1 << 6), ("sampler", 0)]:
306 op(f"{kind}_state_store", encoding = (0b10101101 | bit | (1 << 20), 8, _),
307 dests = 0, srcs = 2, imms = [IMM, SCOREBOARD], can_eliminate = False,
308 schedule_class = "store")
309
310 # sources are value, base, index
311 op("atomic",
312 encoding = (0x15 | (1 << 26) | (1 << 31) | (5 << 44), 8, _),
313 dests = 1, srcs = 3, imms = [ATOMIC_OPC, SCOREBOARD],
314 can_eliminate = False, schedule_class = "atomic")
315
316 # XXX: stop hardcoding the long form
317 op("local_atomic",
318 encoding = (0x19 | (1 << 15) | (1 << 36) | (1 << 47), 10, _),
319 dests = 1, srcs = 3, imms = [ATOMIC_OPC], schedule_class = "atomic",
320 can_eliminate = False)
321
322 op("wait", (0x38, 2, _), dests = 0,
323 can_eliminate = False, imms = [SCOREBOARD], schedule_class = "invalid")
324
325 for (suffix, schedule_class) in [("", "none"), ("_coverage", "coverage"), ("_barrier", "barrier")]:
326 op(f"get_sr{suffix}", (0x72, 4, _), dests = 1, imms = [SR],
327 schedule_class = schedule_class, can_reorder = schedule_class == "none")
328
329 op("sample_mask", (0x7fc1, 6, _), dests = 0, srcs = 2,
330 can_eliminate = False, schedule_class = "coverage")
331
332 # Sources: sample mask, combined depth/stencil
333 op("zs_emit", (0x41, 4, _), dests = 0, srcs = 2,
334 can_eliminate = False, imms = [ZS], schedule_class = "coverage")
335
336 # Sources: sample mask, explicit coords (if present)
337 op("ld_tile", (0x49, 8, _), dests = 1, srcs = 2,
338 imms = [FORMAT, MASK, PIXEL_OFFSET, EXPLICIT_COORDS], can_reorder = False,
339 schedule_class = "coverage")
340
341 # Sources: value, sample mask, explicit coords (if present)
342 op("st_tile", (0x09, 8, _), dests = 0, srcs = 3,
343 can_eliminate = False, imms = [FORMAT, MASK, PIXEL_OFFSET, EXPLICIT_COORDS],
344 schedule_class = "coverage")
345
346 for (name, exact) in [("any", 0xC000), ("none", 0xC020), ("none_after", 0xC020)]:
347 op("jmp_exec_" + name, (exact, 6, _), dests = 0, srcs = 0,
348 can_eliminate = False, schedule_class = "invalid", imms = [TARGET])
349
350 # TODO: model implicit r0l destinations
351 op("pop_exec", (0x52 | (0x3 << 9), 6, _),
352 dests = 0, srcs = 0, can_eliminate = False, schedule_class = "invalid",
353 imms = [NEST])
354
355 for is_float in [False, True]:
356 mod_mask = 0 if is_float else (0x3 << 26) | (0x3 << 38)
357
358 for (cf, cf_op) in [("if", 0), ("else", 1), ("while", 2)]:
359 name = "{}_{}cmp".format(cf, "f" if is_float else "i")
360 exact = 0x42 | (0x0 if is_float else 0x10) | (cf_op << 9)
361 imms = [NEST, FCOND if is_float else ICOND, INVERT_COND, TARGET]
362
363 op(name, (exact, 6, _), dests = 0, srcs = 2, can_eliminate = False,
364 imms = imms, is_float = is_float,
365 schedule_class = "preload" if cf == "else" else "invalid")
366
367 op("bitop", (0x7E, 6, _), srcs = 2, imms = [TRUTH_TABLE])
368 op("intl", (0x3E, 6, _), srcs = 2, imms = [])
369 op("convert", (0x3E | L, 6, _), srcs = 2, imms = [ROUND])
370
371 # Sources are the coeffient register and the sample index (if applicable)
372 op("iter", (0x21, 8, _), srcs = 2, imms = [CHANNELS, INTERPOLATION])
373
374 # Sources are the coeffient register for the varying, the coefficient register
375 # for W, and the sample index (if applicable)
376 op("iterproj", (0x21, 8, _), srcs = 3, imms = [CHANNELS, INTERPOLATION])
377
378 op("ldcf", (0xA1, 8, _), srcs = 1, imms = [CHANNELS])
379 op("st_vary", None, dests = 0, srcs = 2, can_eliminate = False)
380 op("no_varyings", (0x80000051, 4, _), dests = 0, can_eliminate = False)
381 op("stop", (0x88, 2, _), dests = 0, can_eliminate = False,
382 schedule_class = "invalid")
383 op("trap", (0x08, 2, _), dests = 0, can_eliminate = False,
384 schedule_class = "invalid")
385
386 # These are modelled as total barriers since they can guard global memory
387 # access too, and even need to be properly ordered with loads.
388 op("wait_pix", (0x48, 4, _), dests = 0, imms = [WRITEOUT],
389 can_eliminate = False, schedule_class = "barrier")
390 op("signal_pix", (0x58, 4, _), dests = 0, imms = [WRITEOUT],
391 can_eliminate = False, schedule_class = "barrier")
392
393 # Sources are the data vector, the coordinate vector, the LOD, the bindless
394 # table if present (zero for texture state registers), and texture index.
395 op("image_write", (0xF1 | (1 << 23), 6, 8), dests = 0, srcs = 5, imms
396 = [DIM, COHERENT], can_eliminate = False, schedule_class = "store")
397
398 # Sources are the image base, image index, the offset within shared memory, and
399 # the coordinates (or just the layer if implicit).
400 # TODO: Do we need the short encoding?
401 op("block_image_store", (0xB1, 10, _), dests = 0, srcs = 4,
402 imms = [FORMAT, DIM, EXPLICIT_COORDS], can_eliminate = False, schedule_class = "store")
403
404 # Barriers
405 op("threadgroup_barrier", (0x0068, 2, _), dests = 0, srcs = 0,
406 can_eliminate = False, schedule_class = "barrier")
407
408 def memory_barrier(name, a, b, c):
409 op(name, (0xF5 | (a << 10) | (b << 8) | (c << 12), 2, _), dests = 0, srcs = 0,
410 can_eliminate = False, schedule_class = "barrier")
411
412 memory_barrier("memory_barrier", 1, 2, 9)
413
414 # TODO: Not clear what these individually are. Some might be cache flushes?
415 memory_barrier("image_barrier_1", 2, 2, 10)
416 memory_barrier("image_barrier_2", 3, 2, 10)
417 memory_barrier("image_barrier_3", 2, 1, 10)
418 memory_barrier("image_barrier_4", 3, 1, 10)
419
420 memory_barrier("flush_memory_to_texture", 0, 0, 4)
421
422 memory_barrier("memory_barrier_2", 2, 2, 9)
423 memory_barrier("memory_barrier_3", 2, 1, 9)
424 memory_barrier("unknown_barrier_1", 0, 3, 3)
425 memory_barrier("unknown_barrier_2", 0, 3, 0)
426
427 # Seen with device-scope memory barriers. Again not clear what's what.
428 memory_barrier("device_barrier_1", 3, 1, 9)
429 memory_barrier("device_barrier_2", 3, 2, 9)
430
431 op("doorbell", (0x60020 | 0x28 << 32, 6, _), dests = 0,
432 can_eliminate = False, can_reorder = False, imms = [IMM])
433
434 op("stack_unmap", (0x00075, 8, _), dests = 1, srcs = 0, can_eliminate = False, can_reorder = False, imms = [IMM])
435 op("stack_map", (0x10075, 8, _), dests = 0, srcs = 1, can_eliminate = False, can_reorder = False, imms = [IMM])
436
437 op("stack_adjust", encoding = (0x10100b5, 8, _),
438 dests = 0, srcs = 0, can_eliminate = False, can_reorder = False,
439 imms = [STACK_SIZE], schedule_class = "store")
440
441 # source is offset
442 op("stack_load", encoding = (0x35, 6, 8),
443 srcs = 1, imms = [FORMAT, MASK, SCOREBOARD], can_reorder = False,
444 schedule_class = "load")
445
446 # sources are value and offset
447 op("stack_store", encoding = (0xb5, 6, 8),
448 dests = 0, srcs = 2, imms = [FORMAT, MASK, SCOREBOARD],
449 can_eliminate=False, schedule_class = "store")
450
451 # Convenient aliases.
452 op("mov", _, srcs = 1)
453 op("not", _, srcs = 1)
454 op("signext", _, srcs = 1)
455
456 op("collect", _, srcs = VARIABLE)
457 op("split", _, srcs = 1, dests = VARIABLE)
458 op("phi", _, srcs = VARIABLE, schedule_class = "preload")
459
460 # The srcs double as destinations. Only deals in registers. This is generated by
461 # parallel copy lowering and lowered soon after. We need this as a dedicated
462 # instruction only for RA validation.
463 op("swap", _, dests = 0, srcs = 2)
464
465 op("unit_test", _, dests = 0, srcs = 1, can_eliminate = False)
466
467 # Like mov, but takes a register and can only appear at the start. Guaranteed
468 # to be coalesced during RA, rather than lowered to a real move.
469 op("preload", _, srcs = 1, schedule_class = "preload")
470
471 # Opposite of preload. Exports a scalar value to a particular register at the
472 # end of the shader part. Must only appear after the logical end of the exit
473 # block, this avoids special casing the source's liveness. Logically all exports
474 # happen in parallel at the end of the shader part.
475 op("export", _, dests = 0, srcs = 1, imms = [IMM], can_eliminate = False,
476 schedule_class = "invalid")
477
478 # Pseudo-instructions to set the nesting counter. Lowers to r0l writes after RA.
479 op("begin_cf", _, dests = 0, can_eliminate = False)
480 op("break", _, dests = 0, imms = [NEST, TARGET], can_eliminate = False,
481 schedule_class = "invalid")
482
483 for (name, is_float) in [("break_if_icmp", False), ("break_if_fcmp", True)]:
484 op(name, _, dests = 0, srcs = 2,
485 imms = [NEST, INVERT_COND, FCOND if is_float else ICOND, TARGET],
486 can_eliminate = False, schedule_class = "invalid")