Debian Codegraph @

src/nouveau/compiler/nak/builder.rs

raw
1 // Copyright © 2022 Collabora, Ltd.
2 // SPDX-License-Identifier: MIT
3
4 use crate::ir::*;
5
6 pub trait Builder {
7 fn push_instr(&mut self, instr: Instr) -> &mut Instr;
8
9 fn sm(&self) -> u8;
10
11 fn push_op(&mut self, op: impl Into<Op>) -> &mut Instr {
12 self.push_instr(Instr::new(op))
13 }
14
15 fn predicate(&mut self, pred: Pred) -> PredicatedBuilder<'_, Self>
16 where
17 Self: Sized,
18 {
19 PredicatedBuilder {
20 b: self,
21 pred: pred,
22 }
23 }
24
25 fn lop2_to(&mut self, dst: Dst, op: LogicOp2, mut x: Src, mut y: Src) {
26 let is_predicate = match &dst {
27 Dst::None => panic!("No LOP destination"),
28 Dst::SSA(ssa) => ssa.is_predicate(),
29 Dst::Reg(reg) => reg.is_predicate(),
30 };
31 assert!(x.is_predicate() == is_predicate);
32 assert!(y.is_predicate() == is_predicate);
33
34 if self.sm() >= 70 {
35 let mut op = op.to_lut();
36 if x.src_mod.is_bnot() {
37 op = LogicOp3::new_lut(&|x, y, _| op.eval(!x, y, 0));
38 x.src_mod = SrcMod::None;
39 }
40 if y.src_mod.is_bnot() {
41 op = LogicOp3::new_lut(&|x, y, _| op.eval(x, !y, 0));
42 y.src_mod = SrcMod::None;
43 }
44 if is_predicate {
45 self.push_op(OpPLop3 {
46 dsts: [dst, Dst::None],
47 srcs: [x, y, true.into()],
48 ops: [op, LogicOp3::new_const(false)],
49 });
50 } else {
51 self.push_op(OpLop3 {
52 dst: dst,
53 srcs: [x, y, 0.into()],
54 op: op,
55 });
56 }
57 } else {
58 if is_predicate {
59 let mut x = x;
60 let cmp_op = match op {
61 LogicOp2::And => PredSetOp::And,
62 LogicOp2::Or => PredSetOp::Or,
63 LogicOp2::Xor => PredSetOp::Xor,
64 LogicOp2::PassB => {
65 // Pass through B by AND with PT
66 x = true.into();
67 PredSetOp::And
68 }
69 };
70 self.push_op(OpPSetP {
71 dsts: [dst, Dst::None],
72 ops: [cmp_op, PredSetOp::And],
73 srcs: [x, y, true.into()],
74 });
75 } else {
76 self.push_op(OpLop2 {
77 dst: dst,
78 srcs: [x, y],
79 op: op,
80 });
81 }
82 }
83 }
84
85 fn prmt_to(&mut self, dst: Dst, x: Src, y: Src, sel: [u8; 4]) {
86 if sel == [0, 1, 2, 3] {
87 self.copy_to(dst, x);
88 } else if sel == [4, 5, 6, 7] {
89 self.copy_to(dst, y);
90 } else {
91 let mut sel_u32 = 0;
92 for i in 0..4 {
93 assert!(sel[i] < 16);
94 sel_u32 |= u32::from(sel[i]) << (i * 4);
95 }
96
97 self.push_op(OpPrmt {
98 dst: dst,
99 srcs: [x, y],
100 sel: sel_u32.into(),
101 mode: PrmtMode::Index,
102 });
103 }
104 }
105
106 fn copy_to(&mut self, dst: Dst, src: Src) {
107 self.push_op(OpCopy { dst: dst, src: src });
108 }
109
110 fn swap(&mut self, x: RegRef, y: RegRef) {
111 assert!(x.file() == y.file());
112 self.push_op(OpSwap {
113 dsts: [x.into(), y.into()],
114 srcs: [y.into(), x.into()],
115 });
116 }
117 }
118
119 pub trait SSABuilder: Builder {
120 fn alloc_ssa(&mut self, file: RegFile) -> SSAValue;
121 fn alloc_ssa_vec(&mut self, file: RegFile, comps: u8) -> SSARef;
122
123 fn shl(&mut self, x: Src, shift: Src) -> SSAValue {
124 let dst = self.alloc_ssa(RegFile::GPR);
125 if self.sm() >= 70 {
126 self.push_op(OpShf {
127 dst: dst.into(),
128 low: x,
129 high: 0.into(),
130 shift: shift,
131 right: false,
132 wrap: true,
133 data_type: IntType::I32,
134 dst_high: false,
135 });
136 } else {
137 self.push_op(OpShl {
138 dst: dst.into(),
139 src: x,
140 shift: shift,
141 wrap: true,
142 });
143 }
144 dst
145 }
146
147 fn shl64(&mut self, x: Src, shift: Src) -> SSARef {
148 let x = x.as_ssa().unwrap();
149 debug_assert!(shift.is_unmodified());
150
151 let dst = self.alloc_ssa_vec(RegFile::GPR, 2);
152 if self.sm() >= 70 {
153 self.push_op(OpShf {
154 dst: dst[0].into(),
155 low: x[0].into(),
156 high: 0.into(),
157 shift: shift.clone(),
158 right: false,
159 wrap: true,
160 data_type: IntType::U64,
161 dst_high: false,
162 });
163 } else {
164 // On Maxwell and earlier, shf.l doesn't work without .high so we
165 // have to use only the high parts, hard-coding the lower parts
166 // to rZ
167 self.push_op(OpShf {
168 dst: dst[0].into(),
169 low: 0.into(),
170 high: x[0].into(),
171 shift: shift.clone(),
172 right: false,
173 wrap: true,
174 data_type: IntType::U64,
175 dst_high: true,
176 });
177 }
178 self.push_op(OpShf {
179 dst: dst[1].into(),
180 low: x[0].into(),
181 high: x[1].into(),
182 shift,
183 right: false,
184 wrap: true,
185 data_type: IntType::U64,
186 dst_high: true,
187 });
188 dst
189 }
190
191 fn shr(&mut self, x: Src, shift: Src, signed: bool) -> SSAValue {
192 let dst = self.alloc_ssa(RegFile::GPR);
193 if self.sm() >= 70 {
194 self.push_op(OpShf {
195 dst: dst.into(),
196 low: 0.into(),
197 high: x,
198 shift: shift,
199 right: true,
200 wrap: true,
201 data_type: if signed { IntType::I32 } else { IntType::U32 },
202 dst_high: true,
203 });
204 } else {
205 self.push_op(OpShr {
206 dst: dst.into(),
207 src: x,
208 shift: shift,
209 wrap: true,
210 signed,
211 });
212 }
213 dst
214 }
215
216 fn shr64(&mut self, x: Src, shift: Src, signed: bool) -> SSARef {
217 let x = x.as_ssa().unwrap();
218 debug_assert!(shift.is_unmodified());
219
220 let dst = self.alloc_ssa_vec(RegFile::GPR, 2);
221 self.push_op(OpShf {
222 dst: dst[0].into(),
223 low: x[0].into(),
224 high: x[1].into(),
225 shift: shift.clone(),
226 right: true,
227 wrap: true,
228 data_type: if signed { IntType::I64 } else { IntType::U64 },
229 dst_high: false,
230 });
231 self.push_op(OpShf {
232 dst: dst[1].into(),
233 low: 0.into(),
234 high: x[1].into(),
235 shift,
236 right: true,
237 wrap: true,
238 data_type: if signed { IntType::I64 } else { IntType::U64 },
239 dst_high: true,
240 });
241 dst
242 }
243
244 fn fadd(&mut self, x: Src, y: Src) -> SSAValue {
245 let dst = self.alloc_ssa(RegFile::GPR);
246 self.push_op(OpFAdd {
247 dst: dst.into(),
248 srcs: [x, y],
249 saturate: false,
250 rnd_mode: FRndMode::NearestEven,
251 ftz: false,
252 });
253 dst
254 }
255
256 fn fmul(&mut self, x: Src, y: Src) -> SSAValue {
257 let dst = self.alloc_ssa(RegFile::GPR);
258 self.push_op(OpFMul {
259 dst: dst.into(),
260 srcs: [x, y],
261 saturate: false,
262 rnd_mode: FRndMode::NearestEven,
263 ftz: false,
264 dnz: false,
265 });
266 dst
267 }
268
269 fn fset(&mut self, cmp_op: FloatCmpOp, x: Src, y: Src) -> SSAValue {
270 let dst = self.alloc_ssa(RegFile::GPR);
271 self.push_op(OpFSet {
272 dst: dst.into(),
273 cmp_op: cmp_op,
274 srcs: [x, y],
275 ftz: false,
276 });
277 dst
278 }
279
280 fn fsetp(&mut self, cmp_op: FloatCmpOp, x: Src, y: Src) -> SSAValue {
281 let dst = self.alloc_ssa(RegFile::Pred);
282 self.push_op(OpFSetP {
283 dst: dst.into(),
284 set_op: PredSetOp::And,
285 cmp_op: cmp_op,
286 srcs: [x, y],
287 accum: SrcRef::True.into(),
288 ftz: false,
289 });
290 dst
291 }
292
293 fn hadd2(&mut self, x: Src, y: Src) -> SSAValue {
294 let dst = self.alloc_ssa(RegFile::GPR);
295 self.push_op(OpHAdd2 {
296 dst: dst.into(),
297 srcs: [x, y],
298 saturate: false,
299 ftz: false,
300 f32: false,
301 });
302 dst
303 }
304
305 fn hset2(&mut self, cmp_op: FloatCmpOp, x: Src, y: Src) -> SSARef {
306 let dst = self.alloc_ssa(RegFile::GPR);
307 self.push_op(OpHSet2 {
308 dst: dst.into(),
309 set_op: PredSetOp::And,
310 cmp_op: cmp_op,
311 srcs: [x, y],
312 ftz: false,
313 accum: SrcRef::True.into(),
314 });
315 dst.into()
316 }
317
318 fn dsetp(&mut self, cmp_op: FloatCmpOp, x: Src, y: Src) -> SSAValue {
319 let dst = self.alloc_ssa(RegFile::Pred);
320 self.push_op(OpDSetP {
321 dst: dst.into(),
322 set_op: PredSetOp::And,
323 cmp_op: cmp_op,
324 srcs: [x, y],
325 accum: SrcRef::True.into(),
326 });
327 dst
328 }
329
330 fn iabs(&mut self, i: Src) -> SSAValue {
331 let dst = self.alloc_ssa(RegFile::GPR);
332 if self.sm() >= 70 {
333 self.push_op(OpIAbs {
334 dst: dst.into(),
335 src: i,
336 });
337 } else {
338 self.push_op(OpI2I {
339 dst: dst.into(),
340 src: i,
341 src_type: IntType::I32,
342 dst_type: IntType::I32,
343 saturate: false,
344 abs: true,
345 neg: false,
346 });
347 }
348 dst
349 }
350
351 fn iadd(&mut self, x: Src, y: Src, z: Src) -> SSAValue {
352 let dst = self.alloc_ssa(RegFile::GPR);
353 if self.sm() >= 70 {
354 self.push_op(OpIAdd3 {
355 dst: dst.into(),
356 srcs: [x, y, z],
357 overflow: [Dst::None, Dst::None],
358 });
359 } else {
360 assert!(z.is_zero());
361 self.push_op(OpIAdd2 {
362 dst: dst.into(),
363 srcs: [x, y],
364 carry_out: Dst::None,
365 });
366 }
367 dst
368 }
369
370 fn iadd64(&mut self, x: Src, y: Src, z: Src) -> SSARef {
371 fn split_iadd64_src(src: Src) -> [Src; 2] {
372 match src.src_ref {
373 SrcRef::Zero => [0.into(), 0.into()],
374 SrcRef::SSA(ssa) => {
375 if src.src_mod.is_ineg() {
376 [Src::from(ssa[0]).ineg(), Src::from(ssa[1]).bnot()]
377 } else {
378 [Src::from(ssa[0]), Src::from(ssa[1])]
379 }
380 }
381 _ => panic!("Unsupported iadd64 source"),
382 }
383 }
384
385 let is_3src = !x.is_zero() && !y.is_zero() && !z.is_zero();
386
387 let [x0, x1] = split_iadd64_src(x);
388 let [y0, y1] = split_iadd64_src(y);
389 let dst = self.alloc_ssa_vec(RegFile::GPR, 2);
390 if self.sm() >= 70 {
391 let carry1 = self.alloc_ssa(RegFile::Pred);
392 let (carry2_dst, carry2_src) = if is_3src {
393 let carry2 = self.alloc_ssa(RegFile::Pred);
394 (carry2.into(), carry2.into())
395 } else {
396 // If one of the sources is known to be zero, we only need one
397 // carry predicate.
398 (Dst::None, false.into())
399 };
400
401 let [z0, z1] = split_iadd64_src(z);
402 self.push_op(OpIAdd3 {
403 dst: dst[0].into(),
404 overflow: [carry1.into(), carry2_dst],
405 srcs: [x0, y0, z0],
406 });
407 self.push_op(OpIAdd3X {
408 dst: dst[1].into(),
409 overflow: [Dst::None, Dst::None],
410 srcs: [x1, y1, z1],
411 carry: [carry1.into(), carry2_src],
412 });
413 } else {
414 assert!(z.is_zero());
415 let carry = self.alloc_ssa(RegFile::Carry);
416 self.push_op(OpIAdd2 {
417 dst: dst[0].into(),
418 srcs: [x0, y0],
419 carry_out: carry.into(),
420 });
421 self.push_op(OpIAdd2X {
422 dst: dst[1].into(),
423 srcs: [x1, y1],
424 carry_out: Dst::None,
425 carry_in: carry.into(),
426 });
427 }
428 dst
429 }
430
431 fn imnmx(&mut self, tp: IntCmpType, x: Src, y: Src, min: Src) -> SSAValue {
432 let dst = self.alloc_ssa(RegFile::GPR);
433 self.push_op(OpIMnMx {
434 dst: dst.into(),
435 cmp_type: tp,
436 srcs: [x, y],
437 min: min,
438 });
439 dst
440 }
441
442 fn imul(&mut self, x: Src, y: Src) -> SSAValue {
443 let dst = self.alloc_ssa(RegFile::GPR);
444 if self.sm() >= 70 {
445 self.push_op(OpIMad {
446 dst: dst.into(),
447 srcs: [x, y, 0.into()],
448 signed: false,
449 });
450 } else {
451 self.push_op(OpIMul {
452 dst: dst.into(),
453 srcs: [x, y],
454 signed: [false; 2],
455 high: false,
456 });
457 }
458 dst
459 }
460
461 fn imul_2x32_64(&mut self, x: Src, y: Src, signed: bool) -> SSARef {
462 let dst = self.alloc_ssa_vec(RegFile::GPR, 2);
463 if self.sm() >= 70 {
464 self.push_op(OpIMad64 {
465 dst: dst.clone().into(),
466 srcs: [x, y, 0.into()],
467 signed,
468 });
469 } else {
470 self.push_op(OpIMul {
471 dst: dst[0].into(),
472 srcs: [x.clone(), y.clone()],
473 signed: [signed; 2],
474 high: false,
475 });
476 self.push_op(OpIMul {
477 dst: dst[1].into(),
478 srcs: [x, y],
479 signed: [signed; 2],
480 high: true,
481 });
482 }
483 dst
484 }
485
486 fn ineg(&mut self, i: Src) -> SSAValue {
487 let dst = self.alloc_ssa(RegFile::GPR);
488 if self.sm() >= 70 {
489 self.push_op(OpIAdd3 {
490 dst: dst.into(),
491 overflow: [Dst::None, Dst::None],
492 srcs: [0.into(), i.ineg(), 0.into()],
493 });
494 } else {
495 self.push_op(OpIAdd2 {
496 dst: dst.into(),
497 srcs: [0.into(), i.ineg()],
498 carry_out: Dst::None,
499 });
500 }
501 dst
502 }
503
504 fn ineg64(&mut self, x: Src) -> SSARef {
505 self.iadd64(0.into(), x.ineg(), 0.into())
506 }
507
508 fn isetp(
509 &mut self,
510 cmp_type: IntCmpType,
511 cmp_op: IntCmpOp,
512 x: Src,
513 y: Src,
514 ) -> SSAValue {
515 let dst = self.alloc_ssa(RegFile::Pred);
516 self.push_op(OpISetP {
517 dst: dst.into(),
518 set_op: PredSetOp::And,
519 cmp_op: cmp_op,
520 cmp_type: cmp_type,
521 ex: false,
522 srcs: [x, y],
523 accum: true.into(),
524 low_cmp: true.into(),
525 });
526 dst
527 }
528
529 fn isetp64(
530 &mut self,
531 cmp_type: IntCmpType,
532 cmp_op: IntCmpOp,
533 x: Src,
534 y: Src,
535 ) -> SSARef {
536 let x = x.as_ssa().unwrap();
537 let y = y.as_ssa().unwrap();
538
539 // Low bits are always an unsigned comparison
540 let low = self.isetp(IntCmpType::U32, cmp_op, x[0].into(), y[0].into());
541
542 let dst = self.alloc_ssa(RegFile::Pred);
543 match cmp_op {
544 IntCmpOp::False | IntCmpOp::True => {
545 panic!("These don't make sense for the builder helper");
546 }
547 IntCmpOp::Eq | IntCmpOp::Ne => {
548 self.push_op(OpISetP {
549 dst: dst.into(),
550 set_op: match cmp_op {
551 IntCmpOp::Eq => PredSetOp::And,
552 IntCmpOp::Ne => PredSetOp::Or,
553 _ => panic!("Not an integer equality"),
554 },
555 cmp_op: cmp_op,
556 cmp_type: IntCmpType::U32,
557 ex: false,
558 srcs: [x[1].into(), y[1].into()],
559 accum: low.into(),
560 low_cmp: true.into(),
561 });
562 }
563 IntCmpOp::Ge | IntCmpOp::Gt | IntCmpOp::Le | IntCmpOp::Lt => {
564 if self.sm() >= 70 {
565 self.push_op(OpISetP {
566 dst: dst.into(),
567 set_op: PredSetOp::And,
568 cmp_op,
569 cmp_type,
570 ex: true,
571 srcs: [x[1].into(), y[1].into()],
572 accum: true.into(),
573 low_cmp: low.into(),
574 });
575 } else {
576 // On Maxwell, iset.ex doesn't do what we want so we need to
577 // do it with 3 comparisons. Fortunately, we can chain them
578 // together and don't need the extra logic that the NIR
579 // lowering would emit.
580 let low_and_high_eq = self.alloc_ssa(RegFile::Pred);
581 self.push_op(OpISetP {
582 dst: low_and_high_eq.into(),
583 set_op: PredSetOp::And,
584 cmp_op: IntCmpOp::Eq,
585 cmp_type: IntCmpType::U32,
586 ex: false,
587 srcs: [x[1].into(), y[1].into()],
588 accum: low.into(),
589 low_cmp: true.into(),
590 });
591 self.push_op(OpISetP {
592 dst: dst.into(),
593 set_op: PredSetOp::Or,
594 // We always want a strict inequality for the high part
595 // so it's false when the two are equal and safe to OR
596 // with the low part.
597 cmp_op: match cmp_op {
598 IntCmpOp::Lt | IntCmpOp::Le => IntCmpOp::Lt,
599 IntCmpOp::Gt | IntCmpOp::Ge => IntCmpOp::Gt,
600 _ => panic!("Not an integer inequality"),
601 },
602 cmp_type,
603 ex: false,
604 srcs: [x[1].into(), y[1].into()],
605 accum: low_and_high_eq.into(),
606 low_cmp: true.into(),
607 });
608 }
609 }
610 }
611 dst.into()
612 }
613
614 fn lea(&mut self, a: Src, b: Src, shift: u8) -> SSAValue {
615 let dst = self.alloc_ssa(RegFile::GPR);
616 assert!(self.sm() >= 70);
617
618 self.push_op(OpLea {
619 dst: dst.into(),
620 overflow: Dst::None,
621 a: a,
622 b: b,
623 a_high: 0.into(),
624 dst_high: false,
625 shift: shift % 32,
626 intermediate_mod: SrcMod::None,
627 });
628
629 dst
630 }
631
632 fn lea64(&mut self, a: Src, b: Src, shift: u8) -> SSARef {
633 assert!(self.sm() >= 70);
634 assert!(a.is_unmodified());
635 assert!(b.is_unmodified());
636
637 let a = a.as_ssa().unwrap();
638 let b = b.as_ssa().unwrap();
639 let dst = self.alloc_ssa_vec(RegFile::GPR, 2);
640 let shift = shift % 64;
641 if shift >= 32 {
642 self.copy_to(dst[0].into(), b[0].into());
643 self.push_op(OpLea {
644 dst: dst[1].into(),
645 overflow: Dst::None,
646 a: a[0].into(),
647 b: b[1].into(),
648 a_high: 0.into(),
649 dst_high: false,
650 shift: shift - 32,
651 intermediate_mod: SrcMod::None,
652 });
653 } else {
654 let carry = self.alloc_ssa(RegFile::Pred);
655 self.push_op(OpLea {
656 dst: dst[0].into(),
657 overflow: carry.into(),
658 a: a[0].into(),
659 b: b[0].into(),
660 a_high: 0.into(),
661 dst_high: false,
662 shift: shift,
663 intermediate_mod: SrcMod::None,
664 });
665 self.push_op(OpLeaX {
666 dst: dst[1].into(),
667 overflow: Dst::None,
668 a: a[0].into(),
669 b: b[1].into(),
670 a_high: a[1].into(),
671 carry: carry.into(),
672 dst_high: true,
673 shift: shift,
674 intermediate_mod: SrcMod::None,
675 });
676 }
677 dst
678 }
679
680 fn lop2(&mut self, op: LogicOp2, x: Src, y: Src) -> SSAValue {
681 let dst = if x.is_predicate() {
682 self.alloc_ssa(RegFile::Pred)
683 } else {
684 self.alloc_ssa(RegFile::GPR)
685 };
686 self.lop2_to(dst.into(), op, x, y);
687 dst
688 }
689
690 fn brev(&mut self, x: Src) -> SSAValue {
691 let dst = self.alloc_ssa(RegFile::GPR);
692 if self.sm() >= 70 {
693 self.push_op(OpBRev {
694 dst: dst.into(),
695 src: x,
696 });
697 } else {
698 // No BREV in Maxwell
699 self.push_op(OpBfe {
700 dst: dst.into(),
701 base: x,
702 signed: false,
703 range: Src::new_imm_u32(0x2000),
704 reverse: true,
705 });
706 }
707 dst
708 }
709
710 fn mufu(&mut self, op: MuFuOp, src: Src) -> SSAValue {
711 let dst = self.alloc_ssa(RegFile::GPR);
712 self.push_op(OpMuFu {
713 dst: dst.into(),
714 op: op,
715 src: src,
716 });
717 dst
718 }
719
720 fn fsin(&mut self, src: Src) -> SSAValue {
721 let tmp = if self.sm() >= 70 {
722 let frac_1_2pi = 1.0 / (2.0 * std::f32::consts::PI);
723 self.fmul(src, frac_1_2pi.into())
724 } else {
725 let tmp = self.alloc_ssa(RegFile::GPR);
726 self.push_op(OpRro {
727 dst: tmp.into(),
728 op: RroOp::SinCos,
729 src,
730 });
731 tmp
732 };
733 self.mufu(MuFuOp::Sin, tmp.into())
734 }
735
736 fn fcos(&mut self, src: Src) -> SSAValue {
737 let tmp = if self.sm() >= 70 {
738 let frac_1_2pi = 1.0 / (2.0 * std::f32::consts::PI);
739 self.fmul(src, frac_1_2pi.into())
740 } else {
741 let tmp = self.alloc_ssa(RegFile::GPR);
742 self.push_op(OpRro {
743 dst: tmp.into(),
744 op: RroOp::SinCos,
745 src,
746 });
747 tmp
748 };
749 self.mufu(MuFuOp::Cos, tmp.into())
750 }
751
752 fn fexp2(&mut self, src: Src) -> SSAValue {
753 let tmp = if self.sm() >= 70 {
754 src
755 } else {
756 let tmp = self.alloc_ssa(RegFile::GPR);
757 self.push_op(OpRro {
758 dst: tmp.into(),
759 op: RroOp::Exp2,
760 src,
761 });
762 tmp.into()
763 };
764 self.mufu(MuFuOp::Exp2, tmp)
765 }
766
767 fn prmt(&mut self, x: Src, y: Src, sel: [u8; 4]) -> SSAValue {
768 let dst = self.alloc_ssa(RegFile::GPR);
769 self.prmt_to(dst.into(), x, y, sel);
770 dst
771 }
772
773 fn prmt4(&mut self, src: [Src; 4], sel: [u8; 4]) -> SSAValue {
774 let max_sel = *sel.iter().max().unwrap();
775 let [src0, src1, src2, src3] = src;
776 if max_sel < 8 {
777 self.prmt(src0, src1, sel)
778 } else if max_sel < 12 {
779 let mut sel_a = [0_u8; 4];
780 let mut sel_b = [0_u8; 4];
781 for i in 0..4_u8 {
782 if sel[usize::from(i)] < 8 {
783 sel_a[usize::from(i)] = sel[usize::from(i)];
784 sel_b[usize::from(i)] = i;
785 } else {
786 sel_b[usize::from(i)] = (sel[usize::from(i)] - 8) + 4;
787 }
788 }
789 let a = self.prmt(src0, src1, sel_a);
790 self.prmt(a.into(), src2, sel_b)
791 } else if max_sel < 16 {
792 let mut sel_a = [0_u8; 4];
793 let mut sel_b = [0_u8; 4];
794 let mut sel_c = [0_u8; 4];
795 for i in 0..4_u8 {
796 if sel[usize::from(i)] < 8 {
797 sel_a[usize::from(i)] = sel[usize::from(i)];
798 sel_c[usize::from(i)] = i;
799 } else {
800 sel_b[usize::from(i)] = sel[usize::from(i)] - 8;
801 sel_c[usize::from(i)] = 4 + i;
802 }
803 }
804 let a = self.prmt(src0, src1, sel_a);
805 let b = self.prmt(src2, src3, sel_b);
806 self.prmt(a.into(), b.into(), sel_c)
807 } else {
808 panic!("Invalid permute value: {max_sel}");
809 }
810 }
811
812 fn sel(&mut self, cond: Src, x: Src, y: Src) -> SSAValue {
813 assert!(cond.src_ref.is_predicate());
814 assert!(x.is_predicate() == y.is_predicate());
815 if x.is_predicate() {
816 let dst = self.alloc_ssa(RegFile::Pred);
817 if self.sm() >= 70 {
818 self.push_op(OpPLop3 {
819 dsts: [dst.into(), Dst::None],
820 srcs: [cond, x, y],
821 ops: [
822 LogicOp3::new_lut(&|c, x, y| (c & x) | (!c & y)),
823 LogicOp3::new_const(false),
824 ],
825 });
826 } else {
827 let tmp = self.alloc_ssa(RegFile::Pred);
828 self.push_op(OpPSetP {
829 dsts: [tmp.into(), Dst::None],
830 ops: [PredSetOp::And, PredSetOp::And],
831 srcs: [cond.clone(), x, true.into()],
832 });
833 self.push_op(OpPSetP {
834 dsts: [dst.into(), Dst::None],
835 ops: [PredSetOp::And, PredSetOp::Or],
836 srcs: [cond.bnot(), y, tmp.into()],
837 });
838 }
839 dst
840 } else {
841 let dst = self.alloc_ssa(RegFile::GPR);
842 self.push_op(OpSel {
843 dst: dst.into(),
844 cond: cond,
845 srcs: [x, y],
846 });
847 dst
848 }
849 }
850
851 fn undef(&mut self) -> SSAValue {
852 let dst = self.alloc_ssa(RegFile::GPR);
853 self.push_op(OpUndef { dst: dst.into() });
854 dst
855 }
856
857 fn copy(&mut self, src: Src) -> SSAValue {
858 let dst = if src.is_predicate() {
859 self.alloc_ssa(RegFile::Pred)
860 } else {
861 self.alloc_ssa(RegFile::GPR)
862 };
863 self.copy_to(dst.into(), src);
864 dst
865 }
866
867 fn bmov_to_bar(&mut self, src: Src) -> SSAValue {
868 assert!(src.src_ref.as_ssa().unwrap().file() == RegFile::GPR);
869 let dst = self.alloc_ssa(RegFile::Bar);
870 self.push_op(OpBMov {
871 dst: dst.into(),
872 src: src,
873 clear: false,
874 });
875 dst
876 }
877
878 fn bmov_to_gpr(&mut self, src: Src) -> SSAValue {
879 assert!(src.src_ref.as_ssa().unwrap().file() == RegFile::Bar);
880 let dst = self.alloc_ssa(RegFile::GPR);
881 self.push_op(OpBMov {
882 dst: dst.into(),
883 src: src,
884 clear: false,
885 });
886 dst
887 }
888 }
889
890 pub struct InstrBuilder<'a> {
891 instrs: MappedInstrs,
892 sm: &'a ShaderModelInfo,
893 }
894
895 impl<'a> InstrBuilder<'a> {
896 pub fn new(sm: &'a ShaderModelInfo) -> Self {
897 Self {
898 instrs: MappedInstrs::None,
899 sm,
900 }
901 }
902 }
903
904 impl InstrBuilder<'_> {
905 pub fn into_vec(self) -> Vec<Instr> {
906 match self.instrs {
907 MappedInstrs::None => Vec::new(),
908 MappedInstrs::One(i) => vec![i],
909 MappedInstrs::Many(v) => v,
910 }
911 }
912
913 pub fn into_mapped_instrs(self) -> MappedInstrs {
914 self.instrs
915 }
916 }
917
918 impl Builder for InstrBuilder<'_> {
919 fn push_instr(&mut self, instr: Instr) -> &mut Instr {
920 self.instrs.push(instr);
921 self.instrs.last_mut().unwrap()
922 }
923
924 fn sm(&self) -> u8 {
925 self.sm.sm()
926 }
927 }
928
929 pub struct SSAInstrBuilder<'a> {
930 b: InstrBuilder<'a>,
931 alloc: &'a mut SSAValueAllocator,
932 }
933
934 impl<'a> SSAInstrBuilder<'a> {
935 pub fn new(
936 sm: &'a ShaderModelInfo,
937 alloc: &'a mut SSAValueAllocator,
938 ) -> Self {
939 Self {
940 b: InstrBuilder::new(sm),
941 alloc: alloc,
942 }
943 }
944 }
945
946 impl SSAInstrBuilder<'_> {
947 pub fn into_vec(self) -> Vec<Instr> {
948 self.b.into_vec()
949 }
950
951 pub fn into_mapped_instrs(self) -> MappedInstrs {
952 self.b.into_mapped_instrs()
953 }
954 }
955
956 impl Builder for SSAInstrBuilder<'_> {
957 fn push_instr(&mut self, instr: Instr) -> &mut Instr {
958 self.b.push_instr(instr)
959 }
960
961 fn sm(&self) -> u8 {
962 self.b.sm()
963 }
964 }
965
966 impl SSABuilder for SSAInstrBuilder<'_> {
967 fn alloc_ssa(&mut self, file: RegFile) -> SSAValue {
968 self.alloc.alloc(file)
969 }
970
971 fn alloc_ssa_vec(&mut self, file: RegFile, comps: u8) -> SSARef {
972 self.alloc.alloc_vec(file, comps)
973 }
974 }
975
976 pub struct PredicatedBuilder<'a, T: Builder> {
977 b: &'a mut T,
978 pred: Pred,
979 }
980
981 impl<T: Builder> Builder for PredicatedBuilder<'_, T> {
982 fn push_instr(&mut self, instr: Instr) -> &mut Instr {
983 let mut instr = instr;
984 assert!(instr.pred.is_true());
985 instr.pred = self.pred;
986 self.b.push_instr(instr)
987 }
988
989 fn sm(&self) -> u8 {
990 self.b.sm()
991 }
992 }
993
994 impl<T: SSABuilder> SSABuilder for PredicatedBuilder<'_, T> {
995 fn alloc_ssa(&mut self, file: RegFile) -> SSAValue {
996 self.b.alloc_ssa(file)
997 }
998
999 fn alloc_ssa_vec(&mut self, file: RegFile, comps: u8) -> SSARef {
1000 self.b.alloc_ssa_vec(file, comps)
1001 }
1002 }
1003
1004 pub struct UniformBuilder<'a, T: Builder> {
1005 b: &'a mut T,
1006 uniform: bool,
1007 }
1008
1009 impl<'a, T: Builder> UniformBuilder<'a, T> {
1010 pub fn new(b: &'a mut T, uniform: bool) -> Self {
1011 Self { b, uniform }
1012 }
1013 }
1014
1015 impl<T: Builder> Builder for UniformBuilder<'_, T> {
1016 fn push_instr(&mut self, instr: Instr) -> &mut Instr {
1017 self.b.push_instr(instr)
1018 }
1019
1020 fn sm(&self) -> u8 {
1021 self.b.sm()
1022 }
1023 }
1024
1025 impl<T: SSABuilder> SSABuilder for UniformBuilder<'_, T> {
1026 fn alloc_ssa(&mut self, file: RegFile) -> SSAValue {
1027 let file = if self.uniform {
1028 file.to_uniform().unwrap()
1029 } else {
1030 file
1031 };
1032 self.b.alloc_ssa(file)
1033 }
1034
1035 fn alloc_ssa_vec(&mut self, file: RegFile, comps: u8) -> SSARef {
1036 let file = if self.uniform {
1037 file.to_uniform().unwrap()
1038 } else {
1039 file
1040 };
1041 self.b.alloc_ssa_vec(file, comps)
1042 }
1043 }