Debian Codegraph @

src/panfrost/compiler/bifrost/valhall/va_pack.c

raw
1 /*
2 * Copyright (C) 2021 Collabora, Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "bi_builder.h"
7 #include "va_compiler.h"
8 #include "valhall.h"
9 #include "valhall_enums.h"
10
11 /* This file contains the final passes of the compiler. Running after
12 * scheduling and RA, the IR is now finalized, so we need to emit it to actual
13 * bits on the wire (as well as fixup branches)
14 */
15
16 /*
17 * Unreachable for encoding failures, when hitting an invalid instruction.
18 * Prints the (first) failing instruction to aid debugging.
19 */
20 NORETURN static void PRINTFLIKE(2, 3)
21 invalid_instruction(const bi_instr *I, const char *cause, ...)
22 {
23 fputs("\nInvalid ", stderr);
24
25 va_list ap;
26 va_start(ap, cause);
27 vfprintf(stderr, cause, ap);
28 va_end(ap);
29
30 fputs(":\n\t", stderr);
31 bi_print_instr(I, stderr);
32 fprintf(stderr, "\n");
33
34 UNREACHABLE("Invalid instruction");
35 }
36
37 /*
38 * Like assert, but prints the instruction if the assertion fails to aid
39 * debugging invalid inputs to the packing module.
40 */
41 #define pack_assert(I, cond) \
42 if (!(cond)) \
43 invalid_instruction(I, "invariant " #cond);
44
45 /*
46 * Validate that two adjacent 32-bit sources form an aligned 64-bit register
47 * pair. This is a compiler invariant, required on Valhall but not on Bifrost.
48 */
49 static void
50 va_validate_register_pair(const bi_instr *I, unsigned s)
51 {
52 ASSERTED bi_index lo = I->src[s], hi = I->src[s + 1];
53
54 pack_assert(I, lo.type == hi.type);
55
56 if (lo.type == BI_INDEX_REGISTER) {
57 pack_assert(I, hi.value & 1);
58 pack_assert(I, hi.value == lo.value + 1);
59 } else if (lo.type == BI_INDEX_FAU && lo.value & BIR_FAU_IMMEDIATE) {
60 /* Small constants are zero extended, so the top word encode zero */
61 pack_assert(I, hi.value == (BIR_FAU_IMMEDIATE | 0));
62 } else {
63 pack_assert(I, hi.offset & 1);
64 pack_assert(I, hi.offset == lo.offset + 1);
65 }
66 }
67
68 static unsigned
69 va_pack_reg(const bi_instr *I, bi_index idx)
70 {
71 pack_assert(I, idx.type == BI_INDEX_REGISTER);
72 pack_assert(I, idx.value < 64);
73
74 return idx.value;
75 }
76
77 static unsigned
78 va_pack_fau_special(const bi_instr *I, enum bir_fau fau)
79 {
80 switch (fau) {
81 case BIR_FAU_WARP_ID:
82 return VA_FAU_SPECIAL_PAGE_0_WARP_ID;
83 case BIR_FAU_ATEST_PARAM:
84 return VA_FAU_SPECIAL_PAGE_0_ATEST_DATUM;
85 case BIR_FAU_TLS_PTR:
86 return VA_FAU_SPECIAL_PAGE_1_THREAD_LOCAL_POINTER;
87 case BIR_FAU_WLS_PTR:
88 return VA_FAU_SPECIAL_PAGE_1_WORKGROUP_LOCAL_POINTER;
89 case BIR_FAU_LANE_ID:
90 return VA_FAU_SPECIAL_PAGE_3_LANE_ID;
91 case BIR_FAU_CORE_ID:
92 return VA_FAU_SPECIAL_PAGE_3_CORE_ID;
93 case BIR_FAU_SHADER_OUTPUT:
94 return VA_FAU_SPECIAL_PAGE_3_SHADER_OUTPUT;
95 case BIR_FAU_PROGRAM_COUNTER:
96 return VA_FAU_SPECIAL_PAGE_3_PROGRAM_COUNTER;
97 case BIR_FAU_SAMPLE_POS_ARRAY:
98 return VA_FAU_SPECIAL_PAGE_0_SAMPLE;
99
100 case BIR_FAU_BLEND_0 ...(BIR_FAU_BLEND_0 + 7):
101 return VA_FAU_SPECIAL_PAGE_0_BLEND_DESCRIPTOR_0 + (fau - BIR_FAU_BLEND_0);
102
103 default:
104 invalid_instruction(I, "FAU");
105 }
106 }
107
108 /*
109 * Encode a 64-bit FAU source. The offset is ignored, so this function can be
110 * used to encode a 32-bit FAU source by or'ing in the appropriate offset.
111 */
112 static unsigned
113 va_pack_fau_64(const bi_instr *I, bi_index idx)
114 {
115 pack_assert(I, idx.type == BI_INDEX_FAU);
116
117 unsigned val = (idx.value & BITFIELD_MASK(5));
118
119 if (idx.value & BIR_FAU_IMMEDIATE)
120 return (0x3 << 6) | (val << 1);
121 else if (idx.value & BIR_FAU_UNIFORM)
122 return (0x2 << 6) | (val << 1);
123 else
124 return (0x7 << 5) | (va_pack_fau_special(I, idx.value) << 1);
125 }
126
127 static unsigned
128 va_pack_src(const bi_instr *I, unsigned s)
129 {
130 bi_index idx = I->src[s];
131
132 if (idx.type == BI_INDEX_REGISTER) {
133 unsigned value = va_pack_reg(I, idx);
134 if (idx.discard)
135 value |= (1 << 6);
136 return value;
137 } else if (idx.type == BI_INDEX_FAU) {
138 pack_assert(I, idx.offset <= 1);
139 return va_pack_fau_64(I, idx) | idx.offset;
140 }
141
142 invalid_instruction(I, "type of source %u", s);
143 }
144
145 static unsigned
146 va_pack_wrmask(const bi_instr *I)
147 {
148 switch (I->dest[0].swizzle) {
149 case BI_SWIZZLE_H00:
150 return 0x1;
151 case BI_SWIZZLE_H11:
152 return 0x2;
153 case BI_SWIZZLE_H01:
154 return 0x3;
155 default:
156 invalid_instruction(I, "write mask");
157 }
158 }
159
160 static enum va_atomic_operation
161 va_pack_atom_opc(const bi_instr *I)
162 {
163 switch (I->atom_opc) {
164 case BI_ATOM_OPC_AADD:
165 return VA_ATOMIC_OPERATION_AADD;
166 case BI_ATOM_OPC_ASMIN:
167 return VA_ATOMIC_OPERATION_ASMIN;
168 case BI_ATOM_OPC_ASMAX:
169 return VA_ATOMIC_OPERATION_ASMAX;
170 case BI_ATOM_OPC_AUMIN:
171 return VA_ATOMIC_OPERATION_AUMIN;
172 case BI_ATOM_OPC_AUMAX:
173 return VA_ATOMIC_OPERATION_AUMAX;
174 case BI_ATOM_OPC_AAND:
175 return VA_ATOMIC_OPERATION_AAND;
176 case BI_ATOM_OPC_AOR:
177 return VA_ATOMIC_OPERATION_AOR;
178 case BI_ATOM_OPC_AXOR:
179 return VA_ATOMIC_OPERATION_AXOR;
180 case BI_ATOM_OPC_ACMPXCHG:
181 case BI_ATOM_OPC_AXCHG:
182 return VA_ATOMIC_OPERATION_AXCHG;
183 default:
184 invalid_instruction(I, "atomic opcode");
185 }
186 }
187
188 static enum va_atomic_operation_with_1
189 va_pack_atom_opc_1(const bi_instr *I)
190 {
191 switch (I->atom_opc) {
192 case BI_ATOM_OPC_AINC:
193 return VA_ATOMIC_OPERATION_WITH_1_AINC;
194 case BI_ATOM_OPC_ADEC:
195 return VA_ATOMIC_OPERATION_WITH_1_ADEC;
196 case BI_ATOM_OPC_AUMAX1:
197 return VA_ATOMIC_OPERATION_WITH_1_AUMAX1;
198 case BI_ATOM_OPC_ASMAX1:
199 return VA_ATOMIC_OPERATION_WITH_1_ASMAX1;
200 case BI_ATOM_OPC_AOR1:
201 return VA_ATOMIC_OPERATION_WITH_1_AOR1;
202 default:
203 invalid_instruction(I, "atomic opcode with implicit 1");
204 }
205 }
206
207 static unsigned
208 va_pack_dest(const bi_instr *I)
209 {
210 assert(I->nr_dests);
211 return va_pack_reg(I, I->dest[0]) | (va_pack_wrmask(I) << 6);
212 }
213
214 static enum va_widen
215 va_pack_widen_f32(const bi_instr *I, enum bi_swizzle swz)
216 {
217 switch (swz) {
218 case BI_SWIZZLE_H01:
219 return VA_WIDEN_NONE;
220 case BI_SWIZZLE_H0:
221 return VA_WIDEN_H0;
222 case BI_SWIZZLE_H1:
223 return VA_WIDEN_H1;
224 default:
225 invalid_instruction(I, "widen");
226 }
227 }
228
229 static enum va_swizzles_16_bit
230 va_pack_swizzle_f16(const bi_instr *I, enum bi_swizzle swz)
231 {
232 switch (swz) {
233 case BI_SWIZZLE_H00:
234 return VA_SWIZZLES_16_BIT_H00;
235 case BI_SWIZZLE_H10:
236 return VA_SWIZZLES_16_BIT_H10;
237 case BI_SWIZZLE_H01:
238 return VA_SWIZZLES_16_BIT_H01;
239 case BI_SWIZZLE_H11:
240 return VA_SWIZZLES_16_BIT_H11;
241 default:
242 invalid_instruction(I, "16-bit swizzle");
243 }
244 }
245
246 static unsigned
247 va_pack_widen(const bi_instr *I, enum bi_swizzle swz, enum va_size size)
248 {
249 if (size == VA_SIZE_8) {
250 switch (swz) {
251 case BI_SWIZZLE_B0123:
252 return VA_SWIZZLES_8_BIT_B0123;
253 case BI_SWIZZLE_B3210:
254 return VA_SWIZZLES_8_BIT_B3210;
255 case BI_SWIZZLE_B0101:
256 return VA_SWIZZLES_8_BIT_B0101;
257 case BI_SWIZZLE_B2323:
258 return VA_SWIZZLES_8_BIT_B2323;
259 case BI_SWIZZLE_B0000:
260 return VA_SWIZZLES_8_BIT_B0000;
261 case BI_SWIZZLE_B1111:
262 return VA_SWIZZLES_8_BIT_B1111;
263 case BI_SWIZZLE_B2222:
264 return VA_SWIZZLES_8_BIT_B2222;
265 case BI_SWIZZLE_B3333:
266 return VA_SWIZZLES_8_BIT_B3333;
267 case BI_SWIZZLE_B0011:
268 return VA_SWIZZLES_8_BIT_B0011;
269 case BI_SWIZZLE_B2301:
270 return VA_SWIZZLES_8_BIT_B2301;
271 case BI_SWIZZLE_B1032:
272 return VA_SWIZZLES_8_BIT_B1032;
273 case BI_SWIZZLE_B2233:
274 return VA_SWIZZLES_8_BIT_B2233;
275 default:
276 invalid_instruction(I, "8-bit widen");
277 }
278 } else if (size == VA_SIZE_16) {
279 switch (swz) {
280 case BI_SWIZZLE_H00:
281 return VA_SWIZZLES_16_BIT_H00;
282 case BI_SWIZZLE_H10:
283 return VA_SWIZZLES_16_BIT_H10;
284 case BI_SWIZZLE_H01:
285 return VA_SWIZZLES_16_BIT_H01;
286 case BI_SWIZZLE_H11:
287 return VA_SWIZZLES_16_BIT_H11;
288 case BI_SWIZZLE_B00:
289 return VA_SWIZZLES_16_BIT_B00;
290 case BI_SWIZZLE_B11:
291 return VA_SWIZZLES_16_BIT_B11;
292 case BI_SWIZZLE_B22:
293 return VA_SWIZZLES_16_BIT_B22;
294 case BI_SWIZZLE_B33:
295 return VA_SWIZZLES_16_BIT_B33;
296 case BI_SWIZZLE_B01:
297 return VA_SWIZZLES_16_BIT_B01;
298 case BI_SWIZZLE_B20:
299 return VA_SWIZZLES_16_BIT_B20;
300 case BI_SWIZZLE_B02:
301 return VA_SWIZZLES_16_BIT_B02;
302 case BI_SWIZZLE_B31:
303 return VA_SWIZZLES_16_BIT_B31;
304 case BI_SWIZZLE_B13:
305 return VA_SWIZZLES_16_BIT_B13;
306 case BI_SWIZZLE_B23:
307 return VA_SWIZZLES_16_BIT_B23;
308 default:
309 invalid_instruction(I, "16-bit widen");
310 }
311 } else if (size == VA_SIZE_32) {
312 switch (swz) {
313 case BI_SWIZZLE_H01:
314 return VA_SWIZZLES_32_BIT_NONE;
315 case BI_SWIZZLE_H0:
316 return VA_SWIZZLES_32_BIT_H0;
317 case BI_SWIZZLE_H1:
318 return VA_SWIZZLES_32_BIT_H1;
319 case BI_SWIZZLE_B0:
320 return VA_SWIZZLES_32_BIT_B0;
321 case BI_SWIZZLE_B1:
322 return VA_SWIZZLES_32_BIT_B1;
323 case BI_SWIZZLE_B2:
324 return VA_SWIZZLES_32_BIT_B2;
325 case BI_SWIZZLE_B3:
326 return VA_SWIZZLES_32_BIT_B3;
327 default:
328 invalid_instruction(I, "32-bit widen");
329 }
330 } else if (size == VA_SIZE_64) {
331 switch (swz) {
332 case BI_SWIZZLE_H01:
333 return VA_SWIZZLES_64_BIT_NONE;
334 case BI_SWIZZLE_H0:
335 return VA_SWIZZLES_64_BIT_H0;
336 case BI_SWIZZLE_H1:
337 return VA_SWIZZLES_64_BIT_H1;
338 case BI_SWIZZLE_B0:
339 return VA_SWIZZLES_64_BIT_B0;
340 case BI_SWIZZLE_B1:
341 return VA_SWIZZLES_64_BIT_B1;
342 case BI_SWIZZLE_B2:
343 return VA_SWIZZLES_64_BIT_B2;
344 case BI_SWIZZLE_B3:
345 return VA_SWIZZLES_64_BIT_B3;
346 default:
347 invalid_instruction(I, "64-bit widen");
348 }
349 } else {
350 invalid_instruction(I, "type size for widen");
351 }
352 }
353
354 static enum va_half_swizzles_8_bit
355 va_pack_halfswizzle(const bi_instr *I, enum bi_swizzle swz)
356 {
357 switch (swz) {
358 case BI_SWIZZLE_B00:
359 return VA_HALF_SWIZZLES_8_BIT_B00;
360 case BI_SWIZZLE_B10:
361 return VA_HALF_SWIZZLES_8_BIT_B10;
362 case BI_SWIZZLE_B20:
363 return VA_HALF_SWIZZLES_8_BIT_B20;
364 case BI_SWIZZLE_B30:
365 return VA_HALF_SWIZZLES_8_BIT_B30;
366 case BI_SWIZZLE_B01:
367 return VA_HALF_SWIZZLES_8_BIT_B01;
368 case BI_SWIZZLE_B11:
369 return VA_HALF_SWIZZLES_8_BIT_B11;
370 case BI_SWIZZLE_B21:
371 return VA_HALF_SWIZZLES_8_BIT_B21;
372 case BI_SWIZZLE_B31:
373 return VA_HALF_SWIZZLES_8_BIT_B31;
374 case BI_SWIZZLE_B02:
375 return VA_HALF_SWIZZLES_8_BIT_B02;
376 case BI_SWIZZLE_B12:
377 return VA_HALF_SWIZZLES_8_BIT_B12;
378 case BI_SWIZZLE_B22:
379 return VA_HALF_SWIZZLES_8_BIT_B22;
380 case BI_SWIZZLE_B32:
381 return VA_HALF_SWIZZLES_8_BIT_B32;
382 case BI_SWIZZLE_B03:
383 return VA_HALF_SWIZZLES_8_BIT_B03;
384 case BI_SWIZZLE_B13:
385 return VA_HALF_SWIZZLES_8_BIT_B13;
386 case BI_SWIZZLE_B23:
387 return VA_HALF_SWIZZLES_8_BIT_B23;
388 case BI_SWIZZLE_B33:
389 return VA_HALF_SWIZZLES_8_BIT_B33;
390 default:
391 invalid_instruction(I, "v2u8 swizzle");
392 }
393 }
394
395 static enum va_lanes_8_bit
396 va_pack_shift_lanes(const bi_instr *I, enum bi_swizzle swz)
397 {
398 switch (swz) {
399 case BI_SWIZZLE_B00:
400 return VA_LANES_8_BIT_B00;
401 case BI_SWIZZLE_B11:
402 return VA_LANES_8_BIT_B11;
403 case BI_SWIZZLE_B22:
404 return VA_LANES_8_BIT_B22;
405 case BI_SWIZZLE_B33:
406 return VA_LANES_8_BIT_B33;
407 default:
408 invalid_instruction(I, "lane shift");
409 }
410 }
411
412 static enum va_combine
413 va_pack_combine(const bi_instr *I, enum bi_swizzle swz)
414 {
415 switch (swz) {
416 case BI_SWIZZLE_H01:
417 return VA_COMBINE_NONE;
418 case BI_SWIZZLE_H0:
419 return VA_COMBINE_H0;
420 case BI_SWIZZLE_H1:
421 return VA_COMBINE_H1;
422 default:
423 invalid_instruction(I, "branch lane");
424 }
425 }
426
427 static enum va_source_format
428 va_pack_source_format(const bi_instr *I)
429 {
430 switch (I->source_format) {
431 case BI_SOURCE_FORMAT_FLAT32:
432 return VA_SOURCE_FORMAT_SRC_FLAT32;
433 case BI_SOURCE_FORMAT_FLAT16:
434 return VA_SOURCE_FORMAT_SRC_FLAT16;
435 case BI_SOURCE_FORMAT_F32:
436 return VA_SOURCE_FORMAT_SRC_F32;
437 case BI_SOURCE_FORMAT_F16:
438 return VA_SOURCE_FORMAT_SRC_F16;
439 }
440
441 invalid_instruction(I, "source format");
442 }
443
444 static uint64_t
445 va_pack_rhadd(const bi_instr *I)
446 {
447 switch (I->round) {
448 case BI_ROUND_RTN:
449 return 0; /* hadd */
450 case BI_ROUND_RTP:
451 return BITFIELD_BIT(30); /* rhadd */
452 default:
453 UNREACHABLE("Invalid round for HADD");
454 }
455 }
456
457 static uint64_t
458 va_pack_alu(const bi_instr *I, unsigned arch)
459 {
460 struct va_opcode_info info = valhall_opcodes[I->op];
461 uint64_t hex = 0;
462
463 switch (I->op) {
464 /* Add FREXP flags */
465 case BI_OPCODE_FREXPE_F32:
466 case BI_OPCODE_FREXPE_V2F16:
467 case BI_OPCODE_FREXPM_F32:
468 case BI_OPCODE_FREXPM_V2F16:
469 if (I->sqrt)
470 hex |= 1ull << 24;
471 if (I->log)
472 hex |= 1ull << 25;
473 break;
474
475 case BI_OPCODE_FLUSH_F32:
476 case BI_OPCODE_FLUSH_V2F16:
477 hex |= I->nan_mode << 8;
478 if (I->ftz)
479 hex |= 1ull << 10;
480 if (I->flush_inf)
481 hex |= 1ull << 11;
482 break;
483
484 /* Add mux type */
485 case BI_OPCODE_MUX_I32:
486 case BI_OPCODE_MUX_V2I16:
487 case BI_OPCODE_MUX_V4I8:
488 hex |= (uint64_t)I->mux << 32;
489 break;
490
491 /* Add .eq flag */
492 case BI_OPCODE_BRANCHZ_I16:
493 case BI_OPCODE_BRANCHZI:
494 pack_assert(I, I->cmpf == BI_CMPF_EQ || I->cmpf == BI_CMPF_NE);
495
496 if (I->cmpf == BI_CMPF_EQ)
497 hex |= (1ull << 36);
498
499 if (I->op == BI_OPCODE_BRANCHZI)
500 hex |= (0x1ull << 40); /* Absolute */
501 else
502 hex |= ((uint64_t)I->branch_offset & BITFIELD_MASK(27)) << 8;
503
504 break;
505
506 /* Add arithmetic flag */
507 case BI_OPCODE_RSHIFT_AND_I32:
508 case BI_OPCODE_RSHIFT_AND_V2I16:
509 case BI_OPCODE_RSHIFT_AND_V4I8:
510 case BI_OPCODE_RSHIFT_OR_I32:
511 case BI_OPCODE_RSHIFT_OR_V2I16:
512 case BI_OPCODE_RSHIFT_OR_V4I8:
513 case BI_OPCODE_RSHIFT_XOR_I32:
514 case BI_OPCODE_RSHIFT_XOR_V2I16:
515 case BI_OPCODE_RSHIFT_XOR_V4I8:
516 hex |= (uint64_t)I->arithmetic << 34;
517 break;
518
519 case BI_OPCODE_LEA_BUF_IMM:
520 hex |= ((uint64_t)I->table) << 8;
521 hex |= ((uint64_t)I->index) << 12;
522 break;
523
524 case BI_OPCODE_LEA_ATTR_IMM:
525 hex |= ((uint64_t)I->table) << 16;
526 hex |= ((uint64_t)I->attribute_index) << 20;
527 break;
528
529 case BI_OPCODE_IADD_IMM_I32:
530 case BI_OPCODE_IADD_IMM_V2I16:
531 case BI_OPCODE_IADD_IMM_V4I8:
532 case BI_OPCODE_FADD_IMM_F32:
533 case BI_OPCODE_FADD_IMM_V2F16:
534 hex |= ((uint64_t)I->index) << 8;
535 break;
536
537 case BI_OPCODE_CLPER_I32:
538 hex |= ((uint64_t)I->inactive_result) << 22;
539 hex |= ((uint64_t)I->lane_op) << 32;
540 hex |= ((uint64_t)I->subgroup) << 36;
541 break;
542
543 case BI_OPCODE_LD_VAR:
544 case BI_OPCODE_LD_VAR_FLAT:
545 case BI_OPCODE_LD_VAR_IMM:
546 case BI_OPCODE_LD_VAR_FLAT_IMM:
547 case BI_OPCODE_LD_VAR_BUF_F16:
548 case BI_OPCODE_LD_VAR_BUF_F32:
549 case BI_OPCODE_LD_VAR_BUF_IMM_F16:
550 case BI_OPCODE_LD_VAR_BUF_IMM_F32:
551 case BI_OPCODE_LD_VAR_SPECIAL:
552 if (I->op == BI_OPCODE_LD_VAR_SPECIAL)
553 hex |= ((uint64_t)I->varying_name) << 12; /* instead of index */
554 else if (I->op == BI_OPCODE_LD_VAR_BUF_IMM_F16 ||
555 I->op == BI_OPCODE_LD_VAR_BUF_IMM_F32) {
556 if (arch >= 11)
557 hex |= ((uint64_t)I->index) << 8;
558 else
559 hex |= ((uint64_t)I->index) << 16;
560 } else if (I->op == BI_OPCODE_LD_VAR_IMM ||
561 I->op == BI_OPCODE_LD_VAR_FLAT_IMM) {
562 hex |= ((uint64_t)I->table) << 8;
563 hex |= ((uint64_t)I->index) << 12;
564 }
565
566 hex |= ((uint64_t)va_pack_source_format(I)) << 24;
567 hex |= ((uint64_t)I->update) << 36;
568 hex |= ((uint64_t)I->sample) << 38;
569 break;
570
571 case BI_OPCODE_LD_ATTR_IMM:
572 hex |= ((uint64_t)I->table) << 16;
573 hex |= ((uint64_t)I->attribute_index) << 20;
574 break;
575
576 case BI_OPCODE_LD_TEX_IMM:
577 case BI_OPCODE_LEA_TEX_IMM:
578 hex |= ((uint64_t)I->table) << 16;
579 hex |= ((uint64_t)I->texture_index) << 20;
580 break;
581
582 case BI_OPCODE_WMASK:
583 hex |= ((uint64_t)I->subgroup) << 36;
584 break;
585
586 case BI_OPCODE_ZS_EMIT:
587 if (I->stencil)
588 hex |= (1 << 24);
589 if (I->z)
590 hex |= (1 << 25);
591 break;
592
593 case BI_OPCODE_LD_TILE:
594 if (I->z_stencil)
595 hex |= (1ull << 36);
596 break;
597
598 default:
599 break;
600 }
601
602 /* FMA_RSCALE.f32 special modes treated as extra opcodes */
603 if (I->op == BI_OPCODE_FMA_RSCALE_F32) {
604 pack_assert(I, I->special < 4);
605 hex |= ((uint64_t)I->special) << 48;
606 }
607
608 /* Add the normal destination or a placeholder. Staging destinations are
609 * added elsewhere, as they require special handling for control fields.
610 */
611 if (info.has_dest && info.nr_staging_dests == 0) {
612 hex |= (uint64_t)va_pack_dest(I) << 40;
613 } else if (info.nr_staging_dests == 0 && info.nr_staging_srcs == 0) {
614 pack_assert(I, I->nr_dests == 0);
615 hex |= 0xC0ull << 40; /* Placeholder */
616 }
617
618 bool swap12 = va_swap_12(I->op);
619
620 /* First src is staging if we read, skip it when packing sources */
621 unsigned src_offset = bi_get_opcode_props(I)->sr_read ? 1 : 0;
622
623 for (unsigned i = 0; i < info.nr_srcs; ++i) {
624 unsigned logical_i = (swap12 && i == 1) ? 2 : (swap12 && i == 2) ? 1 : i;
625
626 struct va_src_info src_info = info.srcs[i];
627 enum va_size size = src_info.size;
628
629 bi_index src = I->src[logical_i + src_offset];
630 hex |= (uint64_t)va_pack_src(I, logical_i + src_offset) << (8 * i);
631
632 if (src_info.notted) {
633 if (src.neg)
634 hex |= (1ull << 35);
635 } else if (src_info.absneg) {
636 unsigned neg_offs = 32 + 2 + ((2 - i) * 2);
637 unsigned abs_offs = 33 + 2 + ((2 - i) * 2);
638
639 if (src.neg)
640 hex |= 1ull << neg_offs;
641 if (src.abs)
642 hex |= 1ull << abs_offs;
643 } else {
644 if (src.neg)
645 invalid_instruction(I, "negate");
646 if (src.abs)
647 invalid_instruction(I, "absolute value");
648 }
649
650 if (src_info.swizzle) {
651 unsigned offs = 24 + ((2 - i) * 2);
652 unsigned S = src.swizzle;
653 pack_assert(I, size == VA_SIZE_16 || size == VA_SIZE_32);
654
655 uint64_t v = (size == VA_SIZE_32 ? va_pack_widen_f32(I, S)
656 : va_pack_swizzle_f16(I, S));
657 hex |= v << offs;
658 } else if (src_info.widen) {
659 unsigned offs = (i == 1) ? 26 : 36;
660 hex |= (uint64_t)va_pack_widen(I, src.swizzle, src_info.size) << offs;
661 } else if (src_info.lane) {
662 unsigned offs = (I->op == BI_OPCODE_MKVEC_V2I8) ?
663 ((i == 0) ? 38 : 36) : ((i == 0) ? 28 : 26);
664
665 if (src_info.size == VA_SIZE_16) {
666 hex |= (src.swizzle == BI_SWIZZLE_H1 ? 1 : 0) << offs;
667 } else {
668 pack_assert(I, src_info.size == VA_SIZE_8);
669 unsigned comp = src.swizzle - BI_SWIZZLE_B0;
670 pack_assert(I, comp < 4);
671 hex |= (uint64_t)comp << offs;
672 }
673 } else if (src_info.lanes) {
674 pack_assert(I, src_info.size == VA_SIZE_8);
675 pack_assert(I, i == 1);
676 hex |= (uint64_t)va_pack_shift_lanes(I, src.swizzle) << 26;
677 } else if (src_info.combine) {
678 /* Treat as swizzle, subgroup ops not yet supported */
679 pack_assert(I, src_info.size == VA_SIZE_32);
680 pack_assert(I, i == 0);
681 hex |= (uint64_t)va_pack_widen_f32(I, src.swizzle) << 37;
682 } else if (src_info.halfswizzle) {
683 pack_assert(I, src_info.size == VA_SIZE_8);
684 pack_assert(I, i == 0);
685 hex |= (uint64_t)va_pack_halfswizzle(I, src.swizzle) << 36;
686 } else if (src.swizzle != BI_SWIZZLE_H01) {
687 invalid_instruction(I, "swizzle");
688 }
689 }
690
691 if (info.saturate)
692 hex |= (uint64_t)I->saturate << 30;
693 if (info.rhadd)
694 hex |= va_pack_rhadd(I);
695 if (info.clamp)
696 hex |= (uint64_t)I->clamp << 32;
697 if (info.round_mode)
698 hex |= (uint64_t)I->round << 30;
699 if (info.condition)
700 hex |= (uint64_t)I->cmpf << 32;
701 if (info.result_type)
702 hex |= (uint64_t)I->result_type << 30;
703
704 return hex;
705 }
706
707 static uint64_t
708 va_pack_byte_offset(const bi_instr *I)
709 {
710 int16_t offset = I->byte_offset;
711 if (offset != I->byte_offset)
712 invalid_instruction(I, "byte offset");
713
714 uint16_t offset_as_u16 = offset;
715 return ((uint64_t)offset_as_u16) << 8;
716 }
717
718 static uint64_t
719 va_pack_byte_offset_8(const bi_instr *I)
720 {
721 uint8_t offset = I->byte_offset;
722 if (offset != I->byte_offset)
723 invalid_instruction(I, "byte offset");
724
725 return ((uint64_t)offset) << 8;
726 }
727
728 static uint64_t
729 va_pack_gclk(const bi_instr *I)
730 {
731 switch (I->source) {
732 case BI_SOURCE_CYCLE_COUNTER:
733 return VA_SOURCE_CYCLE_COUNTER;
734 case BI_SOURCE_SYSTEM_TIMESTAMP:
735 return VA_SOURCE_SYSTEM_TIMESTAMP;
736 }
737
738 invalid_instruction(I, "source");
739 }
740
741 static uint64_t
742 va_pack_load(const bi_instr *I, bool buffer_descriptor)
743 {
744 const uint8_t load_lane_identity[8] = {
745 VA_LOAD_LANE_8_BIT_B0, VA_LOAD_LANE_16_BIT_H0,
746 VA_LOAD_LANE_24_BIT_IDENTITY, VA_LOAD_LANE_32_BIT_W0,
747 VA_LOAD_LANE_48_BIT_IDENTITY, VA_LOAD_LANE_64_BIT_IDENTITY,
748 VA_LOAD_LANE_96_BIT_IDENTITY, VA_LOAD_LANE_128_BIT_IDENTITY,
749 };
750
751 unsigned memory_size = (valhall_opcodes[I->op].exact >> 27) & 0x7;
752 uint64_t hex = (uint64_t)load_lane_identity[memory_size] << 36;
753
754 // unsigned
755 hex |= (1ull << 39);
756
757 if (!buffer_descriptor)
758 hex |= va_pack_byte_offset(I);
759
760 hex |= (uint64_t)va_pack_src(I, 0) << 0;
761 hex |= (uint64_t)I->mem_access << 24;
762
763 if (buffer_descriptor)
764 hex |= (uint64_t)va_pack_src(I, 1) << 8;
765
766 return hex;
767 }
768 static uint64_t
769 va_pack_store(const bi_instr *I)
770 {
771 uint64_t hex = 0;
772
773 va_validate_register_pair(I, 1);
774 hex |= (uint64_t)va_pack_src(I, 1) << 0;
775 hex |= I->mem_access << 24;
776
777 hex |= va_pack_byte_offset(I);
778
779 return hex;
780 }
781
782 static enum va_lod_mode
783 va_pack_lod_mode(const bi_instr *I)
784 {
785 switch (I->va_lod_mode) {
786 case BI_VA_LOD_MODE_ZERO_LOD:
787 return VA_LOD_MODE_ZERO;
788 case BI_VA_LOD_MODE_COMPUTED_LOD:
789 return VA_LOD_MODE_COMPUTED;
790 case BI_VA_LOD_MODE_EXPLICIT:
791 return VA_LOD_MODE_EXPLICIT;
792 case BI_VA_LOD_MODE_COMPUTED_BIAS:
793 return VA_LOD_MODE_COMPUTED_BIAS;
794 case BI_VA_LOD_MODE_GRDESC:
795 return VA_LOD_MODE_GRDESC;
796 }
797
798 invalid_instruction(I, "LOD mode");
799 }
800
801 static enum va_register_type
802 va_pack_register_type(const bi_instr *I)
803 {
804 switch (I->register_format) {
805 case BI_REGISTER_FORMAT_F16:
806 case BI_REGISTER_FORMAT_F32:
807 return VA_REGISTER_TYPE_F;
808
809 case BI_REGISTER_FORMAT_U16:
810 case BI_REGISTER_FORMAT_U32:
811 return VA_REGISTER_TYPE_U;
812
813 case BI_REGISTER_FORMAT_S16:
814 case BI_REGISTER_FORMAT_S32:
815 return VA_REGISTER_TYPE_S;
816
817 default:
818 invalid_instruction(I, "register type");
819 }
820 }
821
822 static enum va_register_format
823 va_pack_register_format(const bi_instr *I)
824 {
825 switch (I->register_format) {
826 case BI_REGISTER_FORMAT_AUTO:
827 return VA_REGISTER_FORMAT_AUTO;
828 case BI_REGISTER_FORMAT_F32:
829 return VA_REGISTER_FORMAT_F32;
830 case BI_REGISTER_FORMAT_F16:
831 return VA_REGISTER_FORMAT_F16;
832 case BI_REGISTER_FORMAT_S32:
833 return VA_REGISTER_FORMAT_S32;
834 case BI_REGISTER_FORMAT_S16:
835 return VA_REGISTER_FORMAT_S16;
836 case BI_REGISTER_FORMAT_U32:
837 return VA_REGISTER_FORMAT_U32;
838 case BI_REGISTER_FORMAT_U16:
839 return VA_REGISTER_FORMAT_U16;
840 default:
841 invalid_instruction(I, "register format");
842 }
843 }
844
845 uint64_t
846 va_pack_instr(const bi_instr *I, unsigned arch)
847 {
848 struct va_opcode_info info = valhall_opcodes[I->op];
849
850 uint64_t hex = info.exact | (((uint64_t)I->flow) << 59);
851 hex |= ((uint64_t)va_select_fau_page(I)) << 57;
852
853 if (info.slot)
854 hex |= ((uint64_t)I->slot << 30);
855
856 if (info.sr_count) {
857 bool read = bi_get_opcode_props(I)->sr_read;
858 bi_index sr = read ? I->src[0] : I->dest[0];
859
860 unsigned count =
861 read ? bi_count_read_registers(I, 0) : bi_count_write_registers(I, 0);
862
863 hex |= ((uint64_t)count << 33);
864 hex |= (uint64_t)va_pack_reg(I, sr) << 40;
865 hex |= ((uint64_t)info.sr_control << 46);
866 }
867
868 if (info.sr_write_count) {
869 hex |= ((uint64_t)bi_count_write_registers(I, 0) - 1) << 36;
870 hex |= ((uint64_t)va_pack_reg(I, I->dest[0])) << 16;
871 }
872
873 if (info.vecsize)
874 hex |= ((uint64_t)I->vecsize << 28);
875
876 if (info.register_format)
877 hex |= ((uint64_t)va_pack_register_format(I)) << 24;
878
879 switch (I->op) {
880 case BI_OPCODE_LOAD_I8:
881 case BI_OPCODE_LOAD_I16:
882 case BI_OPCODE_LOAD_I24:
883 case BI_OPCODE_LOAD_I32:
884 case BI_OPCODE_LOAD_I48:
885 case BI_OPCODE_LOAD_I64:
886 case BI_OPCODE_LOAD_I96:
887 case BI_OPCODE_LOAD_I128:
888 hex |= va_pack_load(I, false);
889 break;
890
891 case BI_OPCODE_LD_PKA_I8:
892 case BI_OPCODE_LD_PKA_I16:
893 case BI_OPCODE_LD_PKA_I24:
894 case BI_OPCODE_LD_PKA_I32:
895 case BI_OPCODE_LD_PKA_I48:
896 case BI_OPCODE_LD_PKA_I64:
897 case BI_OPCODE_LD_PKA_I96:
898 case BI_OPCODE_LD_PKA_I128:
899 hex |= va_pack_load(I, true);
900 break;
901
902 case BI_OPCODE_STORE_I8:
903 case BI_OPCODE_STORE_I16:
904 case BI_OPCODE_STORE_I24:
905 case BI_OPCODE_STORE_I32:
906 case BI_OPCODE_STORE_I48:
907 case BI_OPCODE_STORE_I64:
908 case BI_OPCODE_STORE_I96:
909 case BI_OPCODE_STORE_I128:
910 hex |= va_pack_store(I);
911 break;
912
913 case BI_OPCODE_ATOM1_RETURN_I64:
914 /* Permit omitting the destination for plain ATOM1 */
915 if (!bi_count_write_registers(I, 0)) {
916 hex |= (0x40ull << 40); // fake read
917 }
918
919 /* 64-bit source */
920 va_validate_register_pair(I, 0);
921 hex |= (uint64_t)va_pack_src(I, 0) << 0;
922 hex |= va_pack_byte_offset_8(I);
923 hex |= ((uint64_t)va_pack_atom_opc_1(I)) << 22;
924 break;
925
926 case BI_OPCODE_ACMPXCHG_I64:
927 case BI_OPCODE_AXCHG_I64:
928 case BI_OPCODE_ATOM_I64:
929 case BI_OPCODE_ATOM_RETURN_I64:
930 /* 64-bit source */
931 va_validate_register_pair(I, 1);
932 hex |= (uint64_t)va_pack_src(I, 1) << 0;
933 hex |= va_pack_byte_offset_8(I);
934 hex |= ((uint64_t)va_pack_atom_opc(I)) << 22;
935
936 if (I->op == BI_OPCODE_ATOM_RETURN_I64)
937 hex |= (0xc0ull << 40); // flags
938
939 if (I->atom_opc == BI_ATOM_OPC_ACMPXCHG)
940 hex |= (1 << 26); /* .compare */
941
942 break;
943
944 case BI_OPCODE_ATOM1_RETURN_I32:
945 /* Permit omitting the destination for plain ATOM1 */
946 if (!bi_count_write_registers(I, 0)) {
947 hex |= (0x40ull << 40); // fake read
948 }
949
950 /* 64-bit source */
951 va_validate_register_pair(I, 0);
952 hex |= (uint64_t)va_pack_src(I, 0) << 0;
953 hex |= va_pack_byte_offset_8(I);
954 hex |= ((uint64_t)va_pack_atom_opc_1(I)) << 22;
955 break;
956
957 case BI_OPCODE_ACMPXCHG_I32:
958 case BI_OPCODE_AXCHG_I32:
959 case BI_OPCODE_ATOM_I32:
960 case BI_OPCODE_ATOM_RETURN_I32:
961 /* 64-bit source */
962 va_validate_register_pair(I, 1);
963 hex |= (uint64_t)va_pack_src(I, 1) << 0;
964 hex |= va_pack_byte_offset_8(I);
965 hex |= ((uint64_t)va_pack_atom_opc(I)) << 22;
966
967 if (I->op == BI_OPCODE_ATOM_RETURN_I32)
968 hex |= (0xc0ull << 40); // flags
969
970 if (I->atom_opc == BI_ATOM_OPC_ACMPXCHG)
971 hex |= (1 << 26); /* .compare */
972
973 break;
974
975 case BI_OPCODE_LD_CVT:
976 hex |= (uint64_t)va_pack_src(I, 0);
977 hex |= va_pack_byte_offset(I);
978
979 /* Conversion descriptor */
980 hex |= (uint64_t)va_pack_src(I, 2) << 16;
981 hex |= (uint64_t)I->mem_access << 37;
982 break;
983
984 case BI_OPCODE_ST_CVT:
985 /* Staging read */
986 va_validate_register_pair(I, 1);
987 hex |= (uint64_t)va_pack_src(I, 1) << 0;
988 hex |= va_pack_byte_offset(I);
989
990 /* Conversion descriptor */
991 hex |= (uint64_t)va_pack_src(I, 3) << 16;
992 hex |= (uint64_t)I->mem_access << 37;
993 break;
994
995 case BI_OPCODE_BLEND: {
996 /* Source 0 - Blend descriptor (64-bit) */
997 hex |= ((uint64_t)va_pack_src(I, 2)) << 0;
998 va_validate_register_pair(I, 2);
999
1000 /* Target */
1001 if (I->branch_offset & 0x7)
1002 invalid_instruction(I, "unaligned branch");
1003 if (I->branch_offset > 2040)
1004 invalid_instruction(I, "too large branch-offset");
1005 hex |= ((I->branch_offset >> 3) << 8);
1006
1007 /* Source 2 - coverage mask */
1008 hex |= ((uint64_t)va_pack_reg(I, I->src[1])) << 16;
1009
1010 /* Vector size */
1011 unsigned vecsize = 4;
1012 hex |= ((uint64_t)(vecsize - 1) << 28);
1013
1014 break;
1015 }
1016
1017 case BI_OPCODE_LD_GCLK_U64:
1018 hex |= va_pack_gclk(I);
1019 break;
1020
1021 case BI_OPCODE_TEX_GRADIENT:
1022 case BI_OPCODE_TEX_SINGLE:
1023 case BI_OPCODE_TEX_FETCH:
1024 case BI_OPCODE_TEX_GATHER: {
1025 /* Image to read from */
1026 hex |= ((uint64_t)va_pack_src(I, 1)) << 0;
1027
1028 if ((I->op == BI_OPCODE_TEX_FETCH || I->op == BI_OPCODE_TEX_GRADIENT) &&
1029 I->shadow)
1030 invalid_instruction(I, "texture instruction does not support .shadow");
1031
1032 if (I->wide_indices)
1033 hex |= (1ull << 8);
1034 if (I->array_enable)
1035 hex |= (1ull << 10);
1036 if (I->texel_offset)
1037 hex |= (1ull << 11);
1038 if (I->shadow)
1039 hex |= (1ull << 12);
1040 if (I->skip)
1041 hex |= (1ull << 39);
1042 if (!bi_is_regfmt_16(I->register_format))
1043 hex |= (1ull << 46);
1044
1045 if (I->op == BI_OPCODE_TEX_GRADIENT) {
1046 if (I->force_delta_enable)
1047 hex |= (1ull << 12);
1048 if (I->lod_bias_disable)
1049 hex |= (1ull << 13);
1050 if (I->lod_clamp_disable)
1051 hex |= (1ull << 14);
1052 if (I->derivative_enable)
1053 hex |= (1ull << 15);
1054 }
1055
1056 if (I->op == BI_OPCODE_TEX_SINGLE)
1057 hex |= ((uint64_t)va_pack_lod_mode(I)) << 13;
1058
1059 if (I->op == BI_OPCODE_TEX_GATHER) {
1060 if (I->integer_coordinates)
1061 hex |= (1 << 13);
1062 hex |= ((uint64_t)I->fetch_component) << 14;
1063 }
1064
1065 hex |= (I->write_mask << 22);
1066 hex |= ((uint64_t)I->dimension) << 28;
1067
1068 break;
1069 }
1070
1071 default:
1072 if (!info.exact && I->op != BI_OPCODE_NOP)
1073 invalid_instruction(I, "opcode");
1074
1075 hex |= va_pack_alu(I, arch);
1076 break;
1077 }
1078
1079 return hex;
1080 }
1081
1082 static unsigned
1083 va_instructions_in_block(bi_block *block)
1084 {
1085 unsigned offset = 0;
1086
1087 bi_foreach_instr_in_block(block, _) {
1088 offset++;
1089 }
1090
1091 return offset;
1092 }
1093
1094 /* Calculate branch_offset from a branch_target for a direct relative branch */
1095
1096 static void
1097 va_lower_branch_target(bi_context *ctx, bi_block *start, bi_instr *I)
1098 {
1099 /* Precondition: unlowered relative branch */
1100 bi_block *target = I->branch_target;
1101 assert(target != NULL);
1102
1103 /* Signed since we might jump backwards */
1104 signed offset = 0;
1105
1106 /* Determine if the target block is strictly greater in source order */
1107 bool forwards = target->index > start->index;
1108
1109 if (forwards) {
1110 /* We have to jump through this block */
1111 bi_foreach_instr_in_block_from(start, _, I) {
1112 offset++;
1113 }
1114
1115 /* We then need to jump over every following block until the target */
1116 bi_foreach_block_from(ctx, start, blk) {
1117 /* End just before the target */
1118 if (blk == target)
1119 break;
1120
1121 /* Count other blocks */
1122 if (blk != start)
1123 offset += va_instructions_in_block(blk);
1124 }
1125 } else {
1126 /* Jump through the beginning of this block */
1127 bi_foreach_instr_in_block_from_rev(start, ins, I) {
1128 if (ins != I)
1129 offset--;
1130 }
1131
1132 /* Jump over preceding blocks up to and including the target to get to
1133 * the beginning of the target */
1134 bi_foreach_block_from_rev(ctx, start, blk) {
1135 if (blk == start)
1136 continue;
1137
1138 offset -= va_instructions_in_block(blk);
1139
1140 /* End just after the target */
1141 if (blk == target)
1142 break;
1143 }
1144 }
1145
1146 /* Offset is relative to the next instruction, so bias */
1147 offset--;
1148
1149 /* Update the instruction */
1150 I->branch_offset = offset;
1151 }
1152
1153 /*
1154 * Late lowering to insert blend shader calls after BLEND instructions. Required
1155 * to support blend shaders, so this pass may be omitted if it is known that
1156 * blend shaders are never used.
1157 *
1158 * This lowering runs late because it introduces control flow changes without
1159 * modifying the control flow graph. It hardcodes registers, meaning running
1160 * after RA makes sense. Finally, it hardcodes a manually sized instruction
1161 * sequence, requiring it to run after scheduling.
1162 *
1163 * As it is Valhall specific, running it as a pre-pack lowering is sensible.
1164 */
1165 static void
1166 va_lower_blend(bi_context *ctx)
1167 {
1168 /* Program counter for *next* instruction */
1169 bi_index pc = bi_fau(BIR_FAU_PROGRAM_COUNTER, false);
1170
1171 bi_foreach_instr_global_safe(ctx, I) {
1172 if (I->op != BI_OPCODE_BLEND)
1173 continue;
1174
1175 bi_builder b = bi_init_builder(ctx, bi_after_instr(I));
1176
1177 unsigned prolog_length = 2 * 8;
1178
1179 /* By ABI, the preload blend link register is shared with blend
1180 * shaders */
1181 assert(bi_is_equiv(I->dest[0], bi_register(bi_preload_reg(
1182 BI_PRELOAD_BLEND_LINK, ctx->arch))));
1183
1184 if (I->flow == VA_FLOW_END)
1185 bi_iadd_imm_i32_to(&b, I->dest[0], va_zero_lut(), 0);
1186 else
1187 bi_iadd_imm_i32_to(&b, I->dest[0], pc, prolog_length - 8);
1188
1189 bi_branchzi(&b, va_zero_lut(), I->src[3], BI_CMPF_EQ);
1190
1191 /* For fixed function: skip the prologue, or return */
1192 if (I->flow != VA_FLOW_END)
1193 I->branch_offset = prolog_length;
1194 }
1195 }
1196
1197 void
1198 bi_pack_valhall(bi_context *ctx, struct util_dynarray *emission)
1199 {
1200 unsigned orig_size = emission->size;
1201
1202 va_validate(stderr, ctx);
1203
1204 /* Late lowering */
1205 if (ctx->stage == MESA_SHADER_FRAGMENT && !ctx->inputs->is_blend)
1206 va_lower_blend(ctx);
1207
1208 bi_foreach_block(ctx, block) {
1209 bi_foreach_instr_in_block(block, I) {
1210 if (I->op == BI_OPCODE_BRANCHZ_I16)
1211 va_lower_branch_target(ctx, block, I);
1212
1213 uint64_t hex = va_pack_instr(I, ctx->arch);
1214 util_dynarray_append(emission, hex);
1215 }
1216 }
1217
1218 /* Pad with zeroes, but keep empty programs empty so they may be omitted
1219 * altogether. Failing to do this would result in a program containing only
1220 * zeroes, which is invalid and will raise an encoding fault.
1221 *
1222 * Pad an extra 16 byte (one instruction) to separate primary and secondary
1223 * shader disassembles. This is not strictly necessary, but it's a good
1224 * practice. 128 bytes is the optimal program alignment on Trym, so pad
1225 * secondary shaders up to 128 bytes. This may help the instruction cache.
1226 */
1227 if (orig_size != emission->size) {
1228 unsigned aligned = ALIGN_POT(emission->size + 16, 128);
1229 unsigned count = aligned - emission->size;
1230
1231 memset(util_dynarray_grow(emission, uint8_t, count), 0, count);
1232 }
1233 }