aco: use v_bfe_u32 for unsigned reductions sign-extension on GFX6-GFX7
[mesa.git] / src / amd / compiler / aco_lower_to_hw_instr.cpp
1 /*
2 * Copyright © 2018 Valve Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Daniel Schürmann (daniel.schuermann@campus.tu-berlin.de)
25 *
26 */
27
28 #include <map>
29
30 #include "aco_ir.h"
31 #include "aco_builder.h"
32 #include "util/u_math.h"
33 #include "sid.h"
34 #include "vulkan/radv_shader.h"
35
36
37 namespace aco {
38
39 struct lower_context {
40 Program *program;
41 std::vector<aco_ptr<Instruction>> instructions;
42 };
43
44 aco_opcode get_reduce_opcode(chip_class chip, ReduceOp op) {
45 /* Because some 16-bit instructions are already VOP3 on GFX10, we use the
46 * 32-bit opcodes (VOP2) which allows to remove the tempory VGPR and to use
47 * DPP with the arithmetic instructions. This requires to sign-extend.
48 */
49 switch (op) {
50 case iadd8:
51 case iadd16:
52 if (chip >= GFX10) {
53 return aco_opcode::v_add_u32;
54 } else if (chip >= GFX8) {
55 return aco_opcode::v_add_u16;
56 } else {
57 return aco_opcode::v_add_co_u32;
58 }
59 break;
60 case imul8:
61 case imul16:
62 if (chip >= GFX10) {
63 return aco_opcode::v_mul_lo_u16_e64;
64 } else if (chip >= GFX8) {
65 return aco_opcode::v_mul_lo_u16;
66 } else {
67 return aco_opcode::v_mul_u32_u24;
68 }
69 break;
70 case fadd16: return aco_opcode::v_add_f16;
71 case fmul16: return aco_opcode::v_mul_f16;
72 case imax8:
73 case imax16:
74 if (chip >= GFX10) {
75 return aco_opcode::v_max_i32;
76 } else if (chip >= GFX8) {
77 return aco_opcode::v_max_i16;
78 } else {
79 return aco_opcode::v_max_i32;
80 }
81 break;
82 case imin8:
83 case imin16:
84 if (chip >= GFX10) {
85 return aco_opcode::v_min_i32;
86 } else if (chip >= GFX8) {
87 return aco_opcode::v_min_i16;
88 } else {
89 return aco_opcode::v_min_i32;
90 }
91 break;
92 case umin8:
93 case umin16:
94 if (chip >= GFX10) {
95 return aco_opcode::v_min_u32;
96 } else if (chip >= GFX8) {
97 return aco_opcode::v_min_u16;
98 } else {
99 return aco_opcode::v_min_u32;
100 }
101 break;
102 case umax8:
103 case umax16:
104 if (chip >= GFX10) {
105 return aco_opcode::v_max_u32;
106 } else if (chip >= GFX8) {
107 return aco_opcode::v_max_u16;
108 } else {
109 return aco_opcode::v_max_u32;
110 }
111 break;
112 case fmin16: return aco_opcode::v_min_f16;
113 case fmax16: return aco_opcode::v_max_f16;
114 case iadd32: return chip >= GFX9 ? aco_opcode::v_add_u32 : aco_opcode::v_add_co_u32;
115 case imul32: return aco_opcode::v_mul_lo_u32;
116 case fadd32: return aco_opcode::v_add_f32;
117 case fmul32: return aco_opcode::v_mul_f32;
118 case imax32: return aco_opcode::v_max_i32;
119 case imin32: return aco_opcode::v_min_i32;
120 case umin32: return aco_opcode::v_min_u32;
121 case umax32: return aco_opcode::v_max_u32;
122 case fmin32: return aco_opcode::v_min_f32;
123 case fmax32: return aco_opcode::v_max_f32;
124 case iand8:
125 case iand16:
126 case iand32: return aco_opcode::v_and_b32;
127 case ixor8:
128 case ixor16:
129 case ixor32: return aco_opcode::v_xor_b32;
130 case ior8:
131 case ior16:
132 case ior32: return aco_opcode::v_or_b32;
133 case iadd64: return aco_opcode::num_opcodes;
134 case imul64: return aco_opcode::num_opcodes;
135 case fadd64: return aco_opcode::v_add_f64;
136 case fmul64: return aco_opcode::v_mul_f64;
137 case imin64: return aco_opcode::num_opcodes;
138 case imax64: return aco_opcode::num_opcodes;
139 case umin64: return aco_opcode::num_opcodes;
140 case umax64: return aco_opcode::num_opcodes;
141 case fmin64: return aco_opcode::v_min_f64;
142 case fmax64: return aco_opcode::v_max_f64;
143 case iand64: return aco_opcode::num_opcodes;
144 case ior64: return aco_opcode::num_opcodes;
145 case ixor64: return aco_opcode::num_opcodes;
146 default: return aco_opcode::num_opcodes;
147 }
148 }
149
150 bool is_vop3_reduce_opcode(aco_opcode opcode)
151 {
152 /* 64-bit reductions are VOP3. */
153 if (opcode == aco_opcode::num_opcodes)
154 return true;
155
156 return instr_info.format[(int)opcode] == Format::VOP3;
157 }
158
159 void emit_vadd32(Builder& bld, Definition def, Operand src0, Operand src1)
160 {
161 Instruction *instr = bld.vadd32(def, src0, src1, false, Operand(s2), true);
162 if (instr->definitions.size() >= 2) {
163 assert(instr->definitions[1].regClass() == bld.lm);
164 instr->definitions[1].setFixed(vcc);
165 }
166 }
167
168 void emit_int64_dpp_op(lower_context *ctx, PhysReg dst_reg, PhysReg src0_reg, PhysReg src1_reg,
169 PhysReg vtmp_reg, ReduceOp op,
170 unsigned dpp_ctrl, unsigned row_mask, unsigned bank_mask, bool bound_ctrl,
171 Operand *identity=NULL)
172 {
173 Builder bld(ctx->program, &ctx->instructions);
174 Definition dst[] = {Definition(dst_reg, v1), Definition(PhysReg{dst_reg+1}, v1)};
175 Definition vtmp_def[] = {Definition(vtmp_reg, v1), Definition(PhysReg{vtmp_reg+1}, v1)};
176 Operand src0[] = {Operand(src0_reg, v1), Operand(PhysReg{src0_reg+1}, v1)};
177 Operand src1[] = {Operand(src1_reg, v1), Operand(PhysReg{src1_reg+1}, v1)};
178 Operand src1_64 = Operand(src1_reg, v2);
179 Operand vtmp_op[] = {Operand(vtmp_reg, v1), Operand(PhysReg{vtmp_reg+1}, v1)};
180 Operand vtmp_op64 = Operand(vtmp_reg, v2);
181 if (op == iadd64) {
182 if (ctx->program->chip_class >= GFX10) {
183 if (identity)
184 bld.vop1(aco_opcode::v_mov_b32, vtmp_def[0], identity[0]);
185 bld.vop1_dpp(aco_opcode::v_mov_b32, vtmp_def[0], src0[0],
186 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
187 bld.vop3(aco_opcode::v_add_co_u32_e64, dst[0], bld.def(bld.lm, vcc), vtmp_op[0], src1[0]);
188 } else {
189 bld.vop2_dpp(aco_opcode::v_add_co_u32, dst[0], bld.def(bld.lm, vcc), src0[0], src1[0],
190 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
191 }
192 bld.vop2_dpp(aco_opcode::v_addc_co_u32, dst[1], bld.def(bld.lm, vcc), src0[1], src1[1], Operand(vcc, bld.lm),
193 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
194 } else if (op == iand64) {
195 bld.vop2_dpp(aco_opcode::v_and_b32, dst[0], src0[0], src1[0],
196 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
197 bld.vop2_dpp(aco_opcode::v_and_b32, dst[1], src0[1], src1[1],
198 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
199 } else if (op == ior64) {
200 bld.vop2_dpp(aco_opcode::v_or_b32, dst[0], src0[0], src1[0],
201 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
202 bld.vop2_dpp(aco_opcode::v_or_b32, dst[1], src0[1], src1[1],
203 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
204 } else if (op == ixor64) {
205 bld.vop2_dpp(aco_opcode::v_xor_b32, dst[0], src0[0], src1[0],
206 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
207 bld.vop2_dpp(aco_opcode::v_xor_b32, dst[1], src0[1], src1[1],
208 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
209 } else if (op == umin64 || op == umax64 || op == imin64 || op == imax64) {
210 aco_opcode cmp = aco_opcode::num_opcodes;
211 switch (op) {
212 case umin64:
213 cmp = aco_opcode::v_cmp_gt_u64;
214 break;
215 case umax64:
216 cmp = aco_opcode::v_cmp_lt_u64;
217 break;
218 case imin64:
219 cmp = aco_opcode::v_cmp_gt_i64;
220 break;
221 case imax64:
222 cmp = aco_opcode::v_cmp_lt_i64;
223 break;
224 default:
225 break;
226 }
227
228 if (identity) {
229 bld.vop1(aco_opcode::v_mov_b32, vtmp_def[0], identity[0]);
230 bld.vop1(aco_opcode::v_mov_b32, vtmp_def[1], identity[1]);
231 }
232 bld.vop1_dpp(aco_opcode::v_mov_b32, vtmp_def[0], src0[0],
233 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
234 bld.vop1_dpp(aco_opcode::v_mov_b32, vtmp_def[1], src0[1],
235 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
236
237 bld.vopc(cmp, bld.def(bld.lm, vcc), vtmp_op64, src1_64);
238 bld.vop2(aco_opcode::v_cndmask_b32, dst[0], vtmp_op[0], src1[0], Operand(vcc, bld.lm));
239 bld.vop2(aco_opcode::v_cndmask_b32, dst[1], vtmp_op[1], src1[1], Operand(vcc, bld.lm));
240 } else if (op == imul64) {
241 /* t4 = dpp(x_hi)
242 * t1 = umul_lo(t4, y_lo)
243 * t3 = dpp(x_lo)
244 * t0 = umul_lo(t3, y_hi)
245 * t2 = iadd(t0, t1)
246 * t5 = umul_hi(t3, y_lo)
247 * res_hi = iadd(t2, t5)
248 * res_lo = umul_lo(t3, y_lo)
249 * Requires that res_hi != src0[0] and res_hi != src1[0]
250 * and that vtmp[0] != res_hi.
251 */
252 if (identity)
253 bld.vop1(aco_opcode::v_mov_b32, vtmp_def[0], identity[1]);
254 bld.vop1_dpp(aco_opcode::v_mov_b32, vtmp_def[0], src0[1],
255 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
256 bld.vop3(aco_opcode::v_mul_lo_u32, vtmp_def[1], vtmp_op[0], src1[0]);
257 if (identity)
258 bld.vop1(aco_opcode::v_mov_b32, vtmp_def[0], identity[0]);
259 bld.vop1_dpp(aco_opcode::v_mov_b32, vtmp_def[0], src0[0],
260 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
261 bld.vop3(aco_opcode::v_mul_lo_u32, vtmp_def[0], vtmp_op[0], src1[1]);
262 emit_vadd32(bld, vtmp_def[1], vtmp_op[0], vtmp_op[1]);
263 if (identity)
264 bld.vop1(aco_opcode::v_mov_b32, vtmp_def[0], identity[0]);
265 bld.vop1_dpp(aco_opcode::v_mov_b32, vtmp_def[0], src0[0],
266 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
267 bld.vop3(aco_opcode::v_mul_hi_u32, vtmp_def[0], vtmp_op[0], src1[0]);
268 emit_vadd32(bld, dst[1], vtmp_op[1], vtmp_op[0]);
269 if (identity)
270 bld.vop1(aco_opcode::v_mov_b32, vtmp_def[0], identity[0]);
271 bld.vop1_dpp(aco_opcode::v_mov_b32, vtmp_def[0], src0[0],
272 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
273 bld.vop3(aco_opcode::v_mul_lo_u32, dst[0], vtmp_op[0], src1[0]);
274 }
275 }
276
277 void emit_int64_op(lower_context *ctx, PhysReg dst_reg, PhysReg src0_reg, PhysReg src1_reg, PhysReg vtmp, ReduceOp op)
278 {
279 Builder bld(ctx->program, &ctx->instructions);
280 Definition dst[] = {Definition(dst_reg, v1), Definition(PhysReg{dst_reg+1}, v1)};
281 RegClass src0_rc = src0_reg.reg() >= 256 ? v1 : s1;
282 Operand src0[] = {Operand(src0_reg, src0_rc), Operand(PhysReg{src0_reg+1}, src0_rc)};
283 Operand src1[] = {Operand(src1_reg, v1), Operand(PhysReg{src1_reg+1}, v1)};
284 Operand src0_64 = Operand(src0_reg, src0_reg.reg() >= 256 ? v2 : s2);
285 Operand src1_64 = Operand(src1_reg, v2);
286
287 if (src0_rc == s1 &&
288 (op == imul64 || op == umin64 || op == umax64 || op == imin64 || op == imax64)) {
289 assert(vtmp.reg() != 0);
290 bld.vop1(aco_opcode::v_mov_b32, Definition(vtmp, v1), src0[0]);
291 bld.vop1(aco_opcode::v_mov_b32, Definition(PhysReg{vtmp+1}, v1), src0[1]);
292 src0_reg = vtmp;
293 src0[0] = Operand(vtmp, v1);
294 src0[1] = Operand(PhysReg{vtmp+1}, v1);
295 src0_64 = Operand(vtmp, v2);
296 } else if (src0_rc == s1 && op == iadd64) {
297 assert(vtmp.reg() != 0);
298 bld.vop1(aco_opcode::v_mov_b32, Definition(PhysReg{vtmp+1}, v1), src0[1]);
299 src0[1] = Operand(PhysReg{vtmp+1}, v1);
300 }
301
302 if (op == iadd64) {
303 if (ctx->program->chip_class >= GFX10) {
304 bld.vop3(aco_opcode::v_add_co_u32_e64, dst[0], bld.def(bld.lm, vcc), src0[0], src1[0]);
305 } else {
306 bld.vop2(aco_opcode::v_add_co_u32, dst[0], bld.def(bld.lm, vcc), src0[0], src1[0]);
307 }
308 bld.vop2(aco_opcode::v_addc_co_u32, dst[1], bld.def(bld.lm, vcc), src0[1], src1[1], Operand(vcc, bld.lm));
309 } else if (op == iand64) {
310 bld.vop2(aco_opcode::v_and_b32, dst[0], src0[0], src1[0]);
311 bld.vop2(aco_opcode::v_and_b32, dst[1], src0[1], src1[1]);
312 } else if (op == ior64) {
313 bld.vop2(aco_opcode::v_or_b32, dst[0], src0[0], src1[0]);
314 bld.vop2(aco_opcode::v_or_b32, dst[1], src0[1], src1[1]);
315 } else if (op == ixor64) {
316 bld.vop2(aco_opcode::v_xor_b32, dst[0], src0[0], src1[0]);
317 bld.vop2(aco_opcode::v_xor_b32, dst[1], src0[1], src1[1]);
318 } else if (op == umin64 || op == umax64 || op == imin64 || op == imax64) {
319 aco_opcode cmp = aco_opcode::num_opcodes;
320 switch (op) {
321 case umin64:
322 cmp = aco_opcode::v_cmp_gt_u64;
323 break;
324 case umax64:
325 cmp = aco_opcode::v_cmp_lt_u64;
326 break;
327 case imin64:
328 cmp = aco_opcode::v_cmp_gt_i64;
329 break;
330 case imax64:
331 cmp = aco_opcode::v_cmp_lt_i64;
332 break;
333 default:
334 break;
335 }
336
337 bld.vopc(cmp, bld.def(bld.lm, vcc), src0_64, src1_64);
338 bld.vop2(aco_opcode::v_cndmask_b32, dst[0], src0[0], src1[0], Operand(vcc, bld.lm));
339 bld.vop2(aco_opcode::v_cndmask_b32, dst[1], src0[1], src1[1], Operand(vcc, bld.lm));
340 } else if (op == imul64) {
341 if (src1_reg == dst_reg) {
342 /* it's fine if src0==dst but not if src1==dst */
343 std::swap(src0_reg, src1_reg);
344 std::swap(src0[0], src1[0]);
345 std::swap(src0[1], src1[1]);
346 std::swap(src0_64, src1_64);
347 }
348 assert(!(src0_reg == src1_reg));
349 /* t1 = umul_lo(x_hi, y_lo)
350 * t0 = umul_lo(x_lo, y_hi)
351 * t2 = iadd(t0, t1)
352 * t5 = umul_hi(x_lo, y_lo)
353 * res_hi = iadd(t2, t5)
354 * res_lo = umul_lo(x_lo, y_lo)
355 * assumes that it's ok to modify x_hi/y_hi, since we might not have vtmp
356 */
357 Definition tmp0_def(PhysReg{src0_reg+1}, v1);
358 Definition tmp1_def(PhysReg{src1_reg+1}, v1);
359 Operand tmp0_op = src0[1];
360 Operand tmp1_op = src1[1];
361 bld.vop3(aco_opcode::v_mul_lo_u32, tmp0_def, src0[1], src1[0]);
362 bld.vop3(aco_opcode::v_mul_lo_u32, tmp1_def, src0[0], src1[1]);
363 emit_vadd32(bld, tmp0_def, tmp1_op, tmp0_op);
364 bld.vop3(aco_opcode::v_mul_hi_u32, tmp1_def, src0[0], src1[0]);
365 emit_vadd32(bld, dst[1], tmp0_op, tmp1_op);
366 bld.vop3(aco_opcode::v_mul_lo_u32, dst[0], src0[0], src1[0]);
367 }
368 }
369
370 void emit_dpp_op(lower_context *ctx, PhysReg dst_reg, PhysReg src0_reg, PhysReg src1_reg,
371 PhysReg vtmp, ReduceOp op, unsigned size,
372 unsigned dpp_ctrl, unsigned row_mask, unsigned bank_mask, bool bound_ctrl,
373 Operand *identity=NULL) /* for VOP3 with sparse writes */
374 {
375 Builder bld(ctx->program, &ctx->instructions);
376 RegClass rc = RegClass(RegType::vgpr, size);
377 Definition dst(dst_reg, rc);
378 Operand src0(src0_reg, rc);
379 Operand src1(src1_reg, rc);
380
381 aco_opcode opcode = get_reduce_opcode(ctx->program->chip_class, op);
382 bool vop3 = is_vop3_reduce_opcode(opcode);
383
384 if (!vop3) {
385 if (opcode == aco_opcode::v_add_co_u32)
386 bld.vop2_dpp(opcode, dst, bld.def(bld.lm, vcc), src0, src1, dpp_ctrl, row_mask, bank_mask, bound_ctrl);
387 else
388 bld.vop2_dpp(opcode, dst, src0, src1, dpp_ctrl, row_mask, bank_mask, bound_ctrl);
389 return;
390 }
391
392 if (opcode == aco_opcode::num_opcodes) {
393 emit_int64_dpp_op(ctx, dst_reg ,src0_reg, src1_reg, vtmp, op,
394 dpp_ctrl, row_mask, bank_mask, bound_ctrl, identity);
395 return;
396 }
397
398 if (identity)
399 bld.vop1(aco_opcode::v_mov_b32, Definition(vtmp, v1), identity[0]);
400 if (identity && size >= 2)
401 bld.vop1(aco_opcode::v_mov_b32, Definition(PhysReg{vtmp+1}, v1), identity[1]);
402
403 for (unsigned i = 0; i < size; i++)
404 bld.vop1_dpp(aco_opcode::v_mov_b32, Definition(PhysReg{vtmp+i}, v1), Operand(PhysReg{src0_reg+i}, v1),
405 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
406
407 bld.vop3(opcode, dst, Operand(vtmp, rc), src1);
408 }
409
410 void emit_op(lower_context *ctx, PhysReg dst_reg, PhysReg src0_reg, PhysReg src1_reg,
411 PhysReg vtmp, ReduceOp op, unsigned size)
412 {
413 Builder bld(ctx->program, &ctx->instructions);
414 RegClass rc = RegClass(RegType::vgpr, size);
415 Definition dst(dst_reg, rc);
416 Operand src0(src0_reg, RegClass(src0_reg.reg() >= 256 ? RegType::vgpr : RegType::sgpr, size));
417 Operand src1(src1_reg, rc);
418
419 aco_opcode opcode = get_reduce_opcode(ctx->program->chip_class, op);
420 bool vop3 = is_vop3_reduce_opcode(opcode);
421
422 if (opcode == aco_opcode::num_opcodes) {
423 emit_int64_op(ctx, dst_reg, src0_reg, src1_reg, vtmp, op);
424 return;
425 }
426
427 if (vop3) {
428 bld.vop3(opcode, dst, src0, src1);
429 } else if (opcode == aco_opcode::v_add_co_u32) {
430 bld.vop2(opcode, dst, bld.def(bld.lm, vcc), src0, src1);
431 } else {
432 bld.vop2(opcode, dst, src0, src1);
433 }
434 }
435
436 void emit_dpp_mov(lower_context *ctx, PhysReg dst, PhysReg src0, unsigned size,
437 unsigned dpp_ctrl, unsigned row_mask, unsigned bank_mask, bool bound_ctrl)
438 {
439 Builder bld(ctx->program, &ctx->instructions);
440 for (unsigned i = 0; i < size; i++) {
441 bld.vop1_dpp(aco_opcode::v_mov_b32, Definition(PhysReg{dst+i}, v1), Operand(PhysReg{src0+i}, v1),
442 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
443 }
444 }
445
446 uint32_t get_reduction_identity(ReduceOp op, unsigned idx)
447 {
448 switch (op) {
449 case iadd8:
450 case iadd16:
451 case iadd32:
452 case iadd64:
453 case fadd16:
454 case fadd32:
455 case fadd64:
456 case ior8:
457 case ior16:
458 case ior32:
459 case ior64:
460 case ixor8:
461 case ixor16:
462 case ixor32:
463 case ixor64:
464 case umax8:
465 case umax16:
466 case umax32:
467 case umax64:
468 return 0;
469 case imul8:
470 case imul16:
471 case imul32:
472 case imul64:
473 return idx ? 0 : 1;
474 case fmul16:
475 return 0x3c00u; /* 1.0 */
476 case fmul32:
477 return 0x3f800000u; /* 1.0 */
478 case fmul64:
479 return idx ? 0x3ff00000u : 0u; /* 1.0 */
480 case imin8:
481 return INT8_MAX;
482 case imin16:
483 return INT16_MAX;
484 case imin32:
485 return INT32_MAX;
486 case imin64:
487 return idx ? 0x7fffffffu : 0xffffffffu;
488 case imax8:
489 return INT8_MIN;
490 case imax16:
491 return INT16_MIN;
492 case imax32:
493 return INT32_MIN;
494 case imax64:
495 return idx ? 0x80000000u : 0;
496 case umin8:
497 case umin16:
498 case iand8:
499 case iand16:
500 return 0xffffffffu;
501 case umin32:
502 case umin64:
503 case iand32:
504 case iand64:
505 return 0xffffffffu;
506 case fmin16:
507 return 0x7c00u; /* infinity */
508 case fmin32:
509 return 0x7f800000u; /* infinity */
510 case fmin64:
511 return idx ? 0x7ff00000u : 0u; /* infinity */
512 case fmax16:
513 return 0xfc00u; /* negative infinity */
514 case fmax32:
515 return 0xff800000u; /* negative infinity */
516 case fmax64:
517 return idx ? 0xfff00000u : 0u; /* negative infinity */
518 default:
519 unreachable("Invalid reduction operation");
520 break;
521 }
522 return 0;
523 }
524
525 void emit_ds_swizzle(Builder bld, PhysReg dst, PhysReg src, unsigned size, unsigned ds_pattern)
526 {
527 for (unsigned i = 0; i < size; i++) {
528 bld.ds(aco_opcode::ds_swizzle_b32, Definition(PhysReg{dst+i}, v1),
529 Operand(PhysReg{src+i}, v1), ds_pattern);
530 }
531 }
532
533 void emit_reduction(lower_context *ctx, aco_opcode op, ReduceOp reduce_op, unsigned cluster_size, PhysReg tmp,
534 PhysReg stmp, PhysReg vtmp, PhysReg sitmp, Operand src, Definition dst)
535 {
536 assert(cluster_size == ctx->program->wave_size || op == aco_opcode::p_reduce);
537 assert(cluster_size <= ctx->program->wave_size);
538
539 Builder bld(ctx->program, &ctx->instructions);
540
541 Operand identity[2];
542 identity[0] = Operand(get_reduction_identity(reduce_op, 0));
543 identity[1] = Operand(get_reduction_identity(reduce_op, 1));
544 Operand vcndmask_identity[2] = {identity[0], identity[1]};
545
546 /* First, copy the source to tmp and set inactive lanes to the identity */
547 bld.sop1(Builder::s_or_saveexec, Definition(stmp, bld.lm), Definition(scc, s1), Definition(exec, bld.lm), Operand(UINT64_MAX), Operand(exec, bld.lm));
548
549 for (unsigned i = 0; i < src.size(); i++) {
550 /* p_exclusive_scan needs it to be a sgpr or inline constant for the v_writelane_b32
551 * except on GFX10, where v_writelane_b32 can take a literal. */
552 if (identity[i].isLiteral() && op == aco_opcode::p_exclusive_scan && ctx->program->chip_class < GFX10) {
553 bld.sop1(aco_opcode::s_mov_b32, Definition(PhysReg{sitmp+i}, s1), identity[i]);
554 identity[i] = Operand(PhysReg{sitmp+i}, s1);
555
556 bld.vop1(aco_opcode::v_mov_b32, Definition(PhysReg{tmp+i}, v1), identity[i]);
557 vcndmask_identity[i] = Operand(PhysReg{tmp+i}, v1);
558 } else if (identity[i].isLiteral()) {
559 bld.vop1(aco_opcode::v_mov_b32, Definition(PhysReg{tmp+i}, v1), identity[i]);
560 vcndmask_identity[i] = Operand(PhysReg{tmp+i}, v1);
561 }
562 }
563
564 for (unsigned i = 0; i < src.size(); i++) {
565 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(PhysReg{tmp + i}, v1),
566 vcndmask_identity[i], Operand(PhysReg{src.physReg() + i}, v1),
567 Operand(stmp, bld.lm));
568 }
569
570 if (src.regClass() == v1b) {
571 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
572 sdwa->operands[0] = Operand(PhysReg{tmp}, v1);
573 sdwa->definitions[0] = Definition(PhysReg{tmp}, v1);
574 if (reduce_op == imin8 || reduce_op == imax8)
575 sdwa->sel[0] = sdwa_sbyte;
576 else
577 sdwa->sel[0] = sdwa_ubyte;
578 sdwa->dst_sel = sdwa_udword;
579 bld.insert(std::move(sdwa));
580 } else if (src.regClass() == v2b) {
581 if (ctx->program->chip_class >= GFX10 &&
582 (reduce_op == iadd16 || reduce_op == imax16 ||
583 reduce_op == imin16 || reduce_op == umin16 || reduce_op == umax16)) {
584 aco_ptr<SDWA_instruction> sdwa{create_instruction<SDWA_instruction>(aco_opcode::v_mov_b32, asSDWA(Format::VOP1), 1, 1)};
585 sdwa->operands[0] = Operand(PhysReg{tmp}, v1);
586 sdwa->definitions[0] = Definition(PhysReg{tmp}, v1);
587 if (reduce_op == imin16 || reduce_op == imax16 || reduce_op == iadd16)
588 sdwa->sel[0] = sdwa_sword;
589 else
590 sdwa->sel[0] = sdwa_uword;
591 sdwa->dst_sel = sdwa_udword;
592 bld.insert(std::move(sdwa));
593 } else if (ctx->program->chip_class == GFX6 || ctx->program->chip_class == GFX7) {
594 aco_opcode opcode;
595
596 if (reduce_op == imin16 || reduce_op == imax16 || reduce_op == iadd16)
597 opcode = aco_opcode::v_bfe_i32;
598 else
599 opcode = aco_opcode::v_bfe_u32;
600
601 bld.vop3(opcode, Definition(PhysReg{tmp}, v1),
602 Operand(PhysReg{tmp}, v1), Operand(0u), Operand(16u));
603 }
604 }
605
606 bool reduction_needs_last_op = false;
607 switch (op) {
608 case aco_opcode::p_reduce:
609 if (cluster_size == 1) break;
610
611 if (ctx->program->chip_class <= GFX7) {
612 reduction_needs_last_op = true;
613 emit_ds_swizzle(bld, vtmp, tmp, src.size(), (1 << 15) | dpp_quad_perm(1, 0, 3, 2));
614 if (cluster_size == 2) break;
615 emit_op(ctx, tmp, vtmp, tmp, PhysReg{0}, reduce_op, src.size());
616 emit_ds_swizzle(bld, vtmp, tmp, src.size(), (1 << 15) | dpp_quad_perm(2, 3, 0, 1));
617 if (cluster_size == 4) break;
618 emit_op(ctx, tmp, vtmp, tmp, PhysReg{0}, reduce_op, src.size());
619 emit_ds_swizzle(bld, vtmp, tmp, src.size(), ds_pattern_bitmode(0x1f, 0, 0x04));
620 if (cluster_size == 8) break;
621 emit_op(ctx, tmp, vtmp, tmp, PhysReg{0}, reduce_op, src.size());
622 emit_ds_swizzle(bld, vtmp, tmp, src.size(), ds_pattern_bitmode(0x1f, 0, 0x08));
623 if (cluster_size == 16) break;
624 emit_op(ctx, tmp, vtmp, tmp, PhysReg{0}, reduce_op, src.size());
625 emit_ds_swizzle(bld, vtmp, tmp, src.size(), ds_pattern_bitmode(0x1f, 0, 0x10));
626 if (cluster_size == 32) break;
627 emit_op(ctx, tmp, vtmp, tmp, PhysReg{0}, reduce_op, src.size());
628 for (unsigned i = 0; i < src.size(); i++)
629 bld.readlane(Definition(PhysReg{dst.physReg() + i}, s1), Operand(PhysReg{tmp + i}, v1), Operand(0u));
630 // TODO: it would be more effective to do the last reduction step on SALU
631 emit_op(ctx, tmp, dst.physReg(), tmp, vtmp, reduce_op, src.size());
632 reduction_needs_last_op = false;
633 break;
634 }
635
636 emit_dpp_op(ctx, tmp, tmp, tmp, vtmp, reduce_op, src.size(), dpp_quad_perm(1, 0, 3, 2), 0xf, 0xf, false);
637 if (cluster_size == 2) break;
638 emit_dpp_op(ctx, tmp, tmp, tmp, vtmp, reduce_op, src.size(), dpp_quad_perm(2, 3, 0, 1), 0xf, 0xf, false);
639 if (cluster_size == 4) break;
640 emit_dpp_op(ctx, tmp, tmp, tmp, vtmp, reduce_op, src.size(), dpp_row_half_mirror, 0xf, 0xf, false);
641 if (cluster_size == 8) break;
642 emit_dpp_op(ctx, tmp, tmp, tmp, vtmp, reduce_op, src.size(), dpp_row_mirror, 0xf, 0xf, false);
643 if (cluster_size == 16) break;
644
645 if (ctx->program->chip_class >= GFX10) {
646 /* GFX10+ doesn't support row_bcast15 and row_bcast31 */
647 for (unsigned i = 0; i < src.size(); i++)
648 bld.vop3(aco_opcode::v_permlanex16_b32, Definition(PhysReg{vtmp+i}, v1), Operand(PhysReg{tmp+i}, v1), Operand(0u), Operand(0u));
649
650 if (cluster_size == 32) {
651 reduction_needs_last_op = true;
652 break;
653 }
654
655 emit_op(ctx, tmp, tmp, vtmp, PhysReg{0}, reduce_op, src.size());
656 for (unsigned i = 0; i < src.size(); i++)
657 bld.readlane(Definition(PhysReg{dst.physReg() + i}, s1), Operand(PhysReg{tmp+i}, v1), Operand(0u));
658 // TODO: it would be more effective to do the last reduction step on SALU
659 emit_op(ctx, tmp, dst.physReg(), tmp, vtmp, reduce_op, src.size());
660 break;
661 }
662
663 if (cluster_size == 32) {
664 emit_ds_swizzle(bld, vtmp, tmp, src.size(), ds_pattern_bitmode(0x1f, 0, 0x10));
665 reduction_needs_last_op = true;
666 break;
667 }
668 assert(cluster_size == 64);
669 emit_dpp_op(ctx, tmp, tmp, tmp, vtmp, reduce_op, src.size(), dpp_row_bcast15, 0xa, 0xf, false);
670 emit_dpp_op(ctx, tmp, tmp, tmp, vtmp, reduce_op, src.size(), dpp_row_bcast31, 0xc, 0xf, false);
671 break;
672 case aco_opcode::p_exclusive_scan:
673 if (ctx->program->chip_class >= GFX10) { /* gfx10 doesn't support wf_sr1, so emulate it */
674 /* shift rows right */
675 emit_dpp_mov(ctx, vtmp, tmp, src.size(), dpp_row_sr(1), 0xf, 0xf, true);
676
677 /* fill in the gaps in rows 1 and 3 */
678 bld.sop1(aco_opcode::s_mov_b32, Definition(exec_lo, s1), Operand(0x10000u));
679 bld.sop1(aco_opcode::s_mov_b32, Definition(exec_hi, s1), Operand(0x10000u));
680 for (unsigned i = 0; i < src.size(); i++) {
681 Instruction *perm = bld.vop3(aco_opcode::v_permlanex16_b32,
682 Definition(PhysReg{vtmp+i}, v1),
683 Operand(PhysReg{tmp+i}, v1),
684 Operand(0xffffffffu), Operand(0xffffffffu)).instr;
685 static_cast<VOP3A_instruction*>(perm)->opsel = 1; /* FI (Fetch Inactive) */
686 }
687 bld.sop1(Builder::s_mov, Definition(exec, bld.lm), Operand(UINT64_MAX));
688
689 if (ctx->program->wave_size == 64) {
690 /* fill in the gap in row 2 */
691 for (unsigned i = 0; i < src.size(); i++) {
692 bld.readlane(Definition(PhysReg{sitmp+i}, s1), Operand(PhysReg{tmp+i}, v1), Operand(31u));
693 bld.writelane(Definition(PhysReg{vtmp+i}, v1), Operand(PhysReg{sitmp+i}, s1), Operand(32u), Operand(PhysReg{vtmp+i}, v1));
694 }
695 }
696 std::swap(tmp, vtmp);
697 } else if (ctx->program->chip_class >= GFX8) {
698 emit_dpp_mov(ctx, tmp, tmp, src.size(), dpp_wf_sr1, 0xf, 0xf, true);
699 } else {
700 // TODO: use LDS on CS with a single write and shifted read
701 /* wavefront shift_right by 1 on SI/CI */
702 emit_ds_swizzle(bld, vtmp, tmp, src.size(), (1 << 15) | dpp_quad_perm(0, 0, 1, 2));
703 emit_ds_swizzle(bld, tmp, tmp, src.size(), ds_pattern_bitmode(0x1F, 0x00, 0x07)); /* mirror(8) */
704 bld.sop1(aco_opcode::s_mov_b32, Definition(exec_lo, s1), Operand(0x10101010u));
705 bld.sop1(aco_opcode::s_mov_b32, Definition(exec_hi, s1), Operand(exec_lo, s1));
706 for (unsigned i = 0; i < src.size(); i++)
707 bld.vop1(aco_opcode::v_mov_b32, Definition(PhysReg{vtmp+i}, v1), Operand(PhysReg{tmp+i}, v1));
708
709 bld.sop1(aco_opcode::s_mov_b64, Definition(exec, s2), Operand(UINT64_MAX));
710 emit_ds_swizzle(bld, tmp, tmp, src.size(), ds_pattern_bitmode(0x1F, 0x00, 0x08)); /* swap(8) */
711 bld.sop1(aco_opcode::s_mov_b32, Definition(exec_lo, s1), Operand(0x01000100u));
712 bld.sop1(aco_opcode::s_mov_b32, Definition(exec_hi, s1), Operand(exec_lo, s1));
713 for (unsigned i = 0; i < src.size(); i++)
714 bld.vop1(aco_opcode::v_mov_b32, Definition(PhysReg{vtmp+i}, v1), Operand(PhysReg{tmp+i}, v1));
715
716 bld.sop1(aco_opcode::s_mov_b64, Definition(exec, s2), Operand(UINT64_MAX));
717 emit_ds_swizzle(bld, tmp, tmp, src.size(), ds_pattern_bitmode(0x1F, 0x00, 0x10)); /* swap(16) */
718 bld.sop2(aco_opcode::s_bfm_b32, Definition(exec_lo, s1), Operand(1u), Operand(16u));
719 bld.sop2(aco_opcode::s_bfm_b32, Definition(exec_hi, s1), Operand(1u), Operand(16u));
720 for (unsigned i = 0; i < src.size(); i++)
721 bld.vop1(aco_opcode::v_mov_b32, Definition(PhysReg{vtmp+i}, v1), Operand(PhysReg{tmp+i}, v1));
722
723 bld.sop1(aco_opcode::s_mov_b64, Definition(exec, s2), Operand(UINT64_MAX));
724 for (unsigned i = 0; i < src.size(); i++) {
725 bld.writelane(Definition(PhysReg{vtmp+i}, v1), identity[i], Operand(0u), Operand(PhysReg{vtmp+i}, v1));
726 bld.readlane(Definition(PhysReg{sitmp+i}, s1), Operand(PhysReg{tmp+i}, v1), Operand(0u));
727 bld.writelane(Definition(PhysReg{vtmp+i}, v1), Operand(PhysReg{sitmp+i}, s1), Operand(32u), Operand(PhysReg{vtmp+i}, v1));
728 identity[i] = Operand(0u); /* prevent further uses of identity */
729 }
730 std::swap(tmp, vtmp);
731 }
732
733 for (unsigned i = 0; i < src.size(); i++) {
734 if (!identity[i].isConstant() || identity[i].constantValue()) { /* bound_ctrl should take care of this overwise */
735 if (ctx->program->chip_class < GFX10)
736 assert((identity[i].isConstant() && !identity[i].isLiteral()) || identity[i].physReg() == PhysReg{sitmp+i});
737 bld.writelane(Definition(PhysReg{tmp+i}, v1), identity[i], Operand(0u), Operand(PhysReg{tmp+i}, v1));
738 }
739 }
740 /* fall through */
741 case aco_opcode::p_inclusive_scan:
742 assert(cluster_size == ctx->program->wave_size);
743 if (ctx->program->chip_class <= GFX7) {
744 emit_ds_swizzle(bld, vtmp, tmp, src.size(), ds_pattern_bitmode(0x1e, 0x00, 0x00));
745 bld.sop1(aco_opcode::s_mov_b32, Definition(exec_lo, s1), Operand(0xAAAAAAAAu));
746 bld.sop1(aco_opcode::s_mov_b32, Definition(exec_hi, s1), Operand(exec_lo, s1));
747 emit_op(ctx, tmp, tmp, vtmp, PhysReg{0}, reduce_op, src.size());
748
749 bld.sop1(aco_opcode::s_mov_b64, Definition(exec, s2), Operand(UINT64_MAX));
750 emit_ds_swizzle(bld, vtmp, tmp, src.size(), ds_pattern_bitmode(0x1c, 0x01, 0x00));
751 bld.sop1(aco_opcode::s_mov_b32, Definition(exec_lo, s1), Operand(0xCCCCCCCCu));
752 bld.sop1(aco_opcode::s_mov_b32, Definition(exec_hi, s1), Operand(exec_lo, s1));
753 emit_op(ctx, tmp, tmp, vtmp, PhysReg{0}, reduce_op, src.size());
754
755 bld.sop1(aco_opcode::s_mov_b64, Definition(exec, s2), Operand(UINT64_MAX));
756 emit_ds_swizzle(bld, vtmp, tmp, src.size(), ds_pattern_bitmode(0x18, 0x03, 0x00));
757 bld.sop1(aco_opcode::s_mov_b32, Definition(exec_lo, s1), Operand(0xF0F0F0F0u));
758 bld.sop1(aco_opcode::s_mov_b32, Definition(exec_hi, s1), Operand(exec_lo, s1));
759 emit_op(ctx, tmp, tmp, vtmp, PhysReg{0}, reduce_op, src.size());
760
761 bld.sop1(aco_opcode::s_mov_b64, Definition(exec, s2), Operand(UINT64_MAX));
762 emit_ds_swizzle(bld, vtmp, tmp, src.size(), ds_pattern_bitmode(0x10, 0x07, 0x00));
763 bld.sop1(aco_opcode::s_mov_b32, Definition(exec_lo, s1), Operand(0xFF00FF00u));
764 bld.sop1(aco_opcode::s_mov_b32, Definition(exec_hi, s1), Operand(exec_lo, s1));
765 emit_op(ctx, tmp, tmp, vtmp, PhysReg{0}, reduce_op, src.size());
766
767 bld.sop1(aco_opcode::s_mov_b64, Definition(exec, s2), Operand(UINT64_MAX));
768 emit_ds_swizzle(bld, vtmp, tmp, src.size(), ds_pattern_bitmode(0x00, 0x0f, 0x00));
769 bld.sop2(aco_opcode::s_bfm_b32, Definition(exec_lo, s1), Operand(16u), Operand(16u));
770 bld.sop2(aco_opcode::s_bfm_b32, Definition(exec_hi, s1), Operand(16u), Operand(16u));
771 emit_op(ctx, tmp, tmp, vtmp, PhysReg{0}, reduce_op, src.size());
772
773 for (unsigned i = 0; i < src.size(); i++)
774 bld.readlane(Definition(PhysReg{sitmp+i}, s1), Operand(PhysReg{tmp+i}, v1), Operand(31u));
775 bld.sop2(aco_opcode::s_bfm_b64, Definition(exec, s2), Operand(32u), Operand(32u));
776 emit_op(ctx, tmp, sitmp, tmp, vtmp, reduce_op, src.size());
777 break;
778 }
779
780 emit_dpp_op(ctx, tmp, tmp, tmp, vtmp, reduce_op, src.size(),
781 dpp_row_sr(1), 0xf, 0xf, false, identity);
782 emit_dpp_op(ctx, tmp, tmp, tmp, vtmp, reduce_op, src.size(),
783 dpp_row_sr(2), 0xf, 0xf, false, identity);
784 emit_dpp_op(ctx, tmp, tmp, tmp, vtmp, reduce_op, src.size(),
785 dpp_row_sr(4), 0xf, 0xf, false, identity);
786 emit_dpp_op(ctx, tmp, tmp, tmp, vtmp, reduce_op, src.size(),
787 dpp_row_sr(8), 0xf, 0xf, false, identity);
788 if (ctx->program->chip_class >= GFX10) {
789 bld.sop2(aco_opcode::s_bfm_b32, Definition(exec_lo, s1), Operand(16u), Operand(16u));
790 bld.sop2(aco_opcode::s_bfm_b32, Definition(exec_hi, s1), Operand(16u), Operand(16u));
791 for (unsigned i = 0; i < src.size(); i++) {
792 Instruction *perm = bld.vop3(aco_opcode::v_permlanex16_b32,
793 Definition(PhysReg{vtmp+i}, v1),
794 Operand(PhysReg{tmp+i}, v1),
795 Operand(0xffffffffu), Operand(0xffffffffu)).instr;
796 static_cast<VOP3A_instruction*>(perm)->opsel = 1; /* FI (Fetch Inactive) */
797 }
798 emit_op(ctx, tmp, tmp, vtmp, PhysReg{0}, reduce_op, src.size());
799
800 if (ctx->program->wave_size == 64) {
801 bld.sop2(aco_opcode::s_bfm_b64, Definition(exec, s2), Operand(32u), Operand(32u));
802 for (unsigned i = 0; i < src.size(); i++)
803 bld.readlane(Definition(PhysReg{sitmp+i}, s1), Operand(PhysReg{tmp+i}, v1), Operand(31u));
804 emit_op(ctx, tmp, sitmp, tmp, vtmp, reduce_op, src.size());
805 }
806 } else {
807 emit_dpp_op(ctx, tmp, tmp, tmp, vtmp, reduce_op, src.size(),
808 dpp_row_bcast15, 0xa, 0xf, false, identity);
809 emit_dpp_op(ctx, tmp, tmp, tmp, vtmp, reduce_op, src.size(),
810 dpp_row_bcast31, 0xc, 0xf, false, identity);
811 }
812 break;
813 default:
814 unreachable("Invalid reduction mode");
815 }
816
817
818 if (op == aco_opcode::p_reduce) {
819 if (reduction_needs_last_op && dst.regClass().type() == RegType::vgpr) {
820 bld.sop1(Builder::s_mov, Definition(exec, bld.lm), Operand(stmp, bld.lm));
821 emit_op(ctx, dst.physReg(), tmp, vtmp, PhysReg{0}, reduce_op, src.size());
822 return;
823 }
824
825 if (reduction_needs_last_op)
826 emit_op(ctx, tmp, vtmp, tmp, PhysReg{0}, reduce_op, src.size());
827 }
828
829 /* restore exec */
830 bld.sop1(Builder::s_mov, Definition(exec, bld.lm), Operand(stmp, bld.lm));
831
832 if (dst.regClass().type() == RegType::sgpr) {
833 for (unsigned k = 0; k < src.size(); k++) {
834 bld.readlane(Definition(PhysReg{dst.physReg() + k}, s1),
835 Operand(PhysReg{tmp + k}, v1), Operand(ctx->program->wave_size - 1));
836 }
837 } else if (dst.physReg() != tmp) {
838 for (unsigned k = 0; k < src.size(); k++) {
839 bld.vop1(aco_opcode::v_mov_b32, Definition(PhysReg{dst.physReg() + k}, v1),
840 Operand(PhysReg{tmp + k}, v1));
841 }
842 }
843 }
844
845 void emit_gfx10_wave64_bpermute(Program *program, aco_ptr<Instruction> &instr, Builder &bld)
846 {
847 /* Emulates proper bpermute on GFX10 in wave64 mode.
848 *
849 * This is necessary because on GFX10 the bpermute instruction only works
850 * on half waves (you can think of it as having a cluster size of 32), so we
851 * manually swap the data between the two halves using two shared VGPRs.
852 */
853
854 assert(program->chip_class >= GFX10);
855 assert(program->info->wave_size == 64);
856
857 unsigned shared_vgpr_reg_0 = align(program->config->num_vgprs, 4) + 256;
858 Definition dst = instr->definitions[0];
859 Definition tmp_exec = instr->definitions[1];
860 Definition clobber_scc = instr->definitions[2];
861 Operand index_x4 = instr->operands[0];
862 Operand input_data = instr->operands[1];
863 Operand same_half = instr->operands[2];
864
865 assert(dst.regClass() == v1);
866 assert(tmp_exec.regClass() == bld.lm);
867 assert(clobber_scc.isFixed() && clobber_scc.physReg() == scc);
868 assert(same_half.regClass() == bld.lm);
869 assert(index_x4.regClass() == v1);
870 assert(input_data.regClass().type() == RegType::vgpr);
871 assert(input_data.bytes() <= 4);
872 assert(dst.physReg() != index_x4.physReg());
873 assert(dst.physReg() != input_data.physReg());
874 assert(tmp_exec.physReg() != same_half.physReg());
875
876 PhysReg shared_vgpr_lo(shared_vgpr_reg_0);
877 PhysReg shared_vgpr_hi(shared_vgpr_reg_0 + 1);
878
879 /* Permute the input within the same half-wave */
880 bld.ds(aco_opcode::ds_bpermute_b32, dst, index_x4, input_data);
881
882 /* HI: Copy data from high lanes 32-63 to shared vgpr */
883 bld.vop1_dpp(aco_opcode::v_mov_b32, Definition(shared_vgpr_hi, v1), input_data, dpp_quad_perm(0, 1, 2, 3), 0xc, 0xf, false);
884 /* Save EXEC */
885 bld.sop1(aco_opcode::s_mov_b64, tmp_exec, Operand(exec, s2));
886 /* Set EXEC to enable LO lanes only */
887 bld.sop2(aco_opcode::s_bfm_b64, Definition(exec, s2), Operand(32u), Operand(0u));
888 /* LO: Copy data from low lanes 0-31 to shared vgpr */
889 bld.vop1(aco_opcode::v_mov_b32, Definition(shared_vgpr_lo, v1), input_data);
890 /* LO: bpermute shared vgpr (high lanes' data) */
891 bld.ds(aco_opcode::ds_bpermute_b32, Definition(shared_vgpr_hi, v1), index_x4, Operand(shared_vgpr_hi, v1));
892 /* Set EXEC to enable HI lanes only */
893 bld.sop2(aco_opcode::s_bfm_b64, Definition(exec, s2), Operand(32u), Operand(32u));
894 /* HI: bpermute shared vgpr (low lanes' data) */
895 bld.ds(aco_opcode::ds_bpermute_b32, Definition(shared_vgpr_lo, v1), index_x4, Operand(shared_vgpr_lo, v1));
896
897 /* Only enable lanes which use the other half's data */
898 bld.sop2(aco_opcode::s_andn2_b64, Definition(exec, s2), clobber_scc, Operand(tmp_exec.physReg(), s2), same_half);
899 /* LO: Copy shared vgpr (high lanes' bpermuted data) to output vgpr */
900 bld.vop1_dpp(aco_opcode::v_mov_b32, dst, Operand(shared_vgpr_hi, v1), dpp_quad_perm(0, 1, 2, 3), 0x3, 0xf, false);
901 /* HI: Copy shared vgpr (low lanes' bpermuted data) to output vgpr */
902 bld.vop1_dpp(aco_opcode::v_mov_b32, dst, Operand(shared_vgpr_lo, v1), dpp_quad_perm(0, 1, 2, 3), 0xc, 0xf, false);
903
904 /* Restore saved EXEC */
905 bld.sop1(aco_opcode::s_mov_b64, Definition(exec, s2), Operand(tmp_exec.physReg(), s2));
906
907 /* RA assumes that the result is always in the low part of the register, so we have to shift, if it's not there already */
908 if (input_data.physReg().byte()) {
909 unsigned right_shift = input_data.physReg().byte() * 8;
910 bld.vop2(aco_opcode::v_lshrrev_b32, dst, Operand(right_shift), Operand(dst.physReg(), v1));
911 }
912 }
913
914 void emit_gfx6_bpermute(Program *program, aco_ptr<Instruction> &instr, Builder &bld)
915 {
916 /* Emulates bpermute using readlane instructions */
917
918 Operand index = instr->operands[0];
919 Operand input = instr->operands[1];
920 Definition dst = instr->definitions[0];
921 Definition temp_exec = instr->definitions[1];
922 Definition clobber_vcc = instr->definitions[2];
923
924 assert(dst.regClass() == v1);
925 assert(temp_exec.regClass() == bld.lm);
926 assert(clobber_vcc.regClass() == bld.lm);
927 assert(clobber_vcc.physReg() == vcc);
928 assert(index.regClass() == v1);
929 assert(index.physReg() != dst.physReg());
930 assert(input.regClass().type() == RegType::vgpr);
931 assert(input.bytes() <= 4);
932 assert(input.physReg() != dst.physReg());
933
934 /* Save original EXEC */
935 bld.sop1(aco_opcode::s_mov_b64, temp_exec, Operand(exec, s2));
936
937 /* An "unrolled loop" that is executed per each lane.
938 * This takes only a few instructions per lane, as opposed to a "real" loop
939 * with branching, where the branch instruction alone would take 16+ cycles.
940 */
941 for (unsigned n = 0; n < program->wave_size; ++n) {
942 /* Activate the lane which has N for its source index */
943 bld.vopc(aco_opcode::v_cmpx_eq_u32, Definition(exec, bld.lm), clobber_vcc, Operand(n), index);
944 /* Read the data from lane N */
945 bld.readlane(Definition(vcc, s1), input, Operand(n));
946 /* On the active lane, move the data we read from lane N to the destination VGPR */
947 bld.vop1(aco_opcode::v_mov_b32, dst, Operand(vcc, s1));
948 /* Restore original EXEC */
949 bld.sop1(aco_opcode::s_mov_b64, Definition(exec, s2), Operand(temp_exec.physReg(), s2));
950 }
951 }
952
953 struct copy_operation {
954 Operand op;
955 Definition def;
956 unsigned bytes;
957 union {
958 uint8_t uses[8];
959 uint64_t is_used = 0;
960 };
961 };
962
963 void split_copy(unsigned offset, Definition *def, Operand *op, const copy_operation& src, bool ignore_uses, unsigned max_size)
964 {
965 PhysReg def_reg = src.def.physReg();
966 PhysReg op_reg = src.op.physReg();
967 def_reg.reg_b += offset;
968 op_reg.reg_b += offset;
969
970 max_size = MIN2(max_size, src.def.regClass().type() == RegType::vgpr ? 4 : 8);
971
972 /* make sure the size is a power of two and reg % bytes == 0 */
973 unsigned bytes = 1;
974 for (; bytes <= max_size; bytes *= 2) {
975 unsigned next = bytes * 2u;
976 bool can_increase = def_reg.reg_b % next == 0 &&
977 offset + next <= src.bytes && next <= max_size;
978 if (!src.op.isConstant() && can_increase)
979 can_increase = op_reg.reg_b % next == 0;
980 for (unsigned i = 0; !ignore_uses && can_increase && (i < bytes); i++)
981 can_increase = (src.uses[offset + bytes + i] == 0) == (src.uses[offset] == 0);
982 if (!can_increase)
983 break;
984 }
985
986 RegClass def_cls = bytes % 4 == 0 ? RegClass(src.def.regClass().type(), bytes / 4u) :
987 RegClass(src.def.regClass().type(), bytes).as_subdword();
988 *def = Definition(src.def.tempId(), def_reg, def_cls);
989 if (src.op.isConstant()) {
990 assert(offset == 0 || (offset == 4 && src.op.bytes() == 8));
991 if (src.op.bytes() == 8 && bytes == 4)
992 *op = Operand(uint32_t(src.op.constantValue64() >> (offset * 8u)));
993 else
994 *op = src.op;
995 } else {
996 RegClass op_cls = bytes % 4 == 0 ? RegClass(src.op.regClass().type(), bytes / 4u) :
997 RegClass(src.op.regClass().type(), bytes).as_subdword();
998 *op = Operand(op_reg, op_cls);
999 op->setTemp(Temp(src.op.tempId(), op_cls));
1000 }
1001 }
1002
1003 uint32_t get_intersection_mask(int a_start, int a_size,
1004 int b_start, int b_size)
1005 {
1006 int intersection_start = MAX2(b_start - a_start, 0);
1007 int intersection_end = MAX2(b_start + b_size - a_start, 0);
1008 if (intersection_start >= a_size || intersection_end == 0)
1009 return 0;
1010
1011 uint32_t mask = u_bit_consecutive(0, a_size);
1012 return u_bit_consecutive(intersection_start, intersection_end - intersection_start) & mask;
1013 }
1014
1015 bool do_copy(lower_context* ctx, Builder& bld, const copy_operation& copy, bool *preserve_scc)
1016 {
1017 bool did_copy = false;
1018 for (unsigned offset = 0; offset < copy.bytes;) {
1019 if (copy.uses[offset]) {
1020 offset++;
1021 continue;
1022 }
1023
1024 Definition def;
1025 Operand op;
1026 split_copy(offset, &def, &op, copy, false, 8);
1027
1028 if (def.physReg() == scc) {
1029 bld.sopc(aco_opcode::s_cmp_lg_i32, def, op, Operand(0u));
1030 *preserve_scc = true;
1031 } else if (def.bytes() == 8 && def.getTemp().type() == RegType::sgpr) {
1032 bld.sop1(aco_opcode::s_mov_b64, def, Operand(op.physReg(), s2));
1033 } else {
1034 bld.copy(def, op);
1035 }
1036
1037 ctx->program->statistics[statistic_copies]++;
1038
1039 did_copy = true;
1040 offset += def.bytes();
1041 }
1042 return did_copy;
1043 }
1044
1045 void do_swap(lower_context *ctx, Builder& bld, const copy_operation& copy, bool preserve_scc, Pseudo_instruction *pi)
1046 {
1047 unsigned offset = 0;
1048
1049 if (copy.bytes == 3 && (copy.def.physReg().reg_b % 4 <= 1) &&
1050 (copy.def.physReg().reg_b % 4) == (copy.op.physReg().reg_b % 4)) {
1051 /* instead of doing a 2-byte and 1-byte swap, do a 4-byte swap and then fixup with a 1-byte swap */
1052 PhysReg op = copy.op.physReg();
1053 PhysReg def = copy.def.physReg();
1054 op.reg_b &= ~0x3;
1055 def.reg_b &= ~0x3;
1056
1057 copy_operation tmp;
1058 tmp.op = Operand(op, v1);
1059 tmp.def = Definition(def, v1);
1060 tmp.bytes = 4;
1061 memset(tmp.uses, 1, 4);
1062 do_swap(ctx, bld, tmp, preserve_scc, pi);
1063
1064 op.reg_b += copy.def.physReg().reg_b % 4 == 0 ? 3 : 0;
1065 def.reg_b += copy.def.physReg().reg_b % 4 == 0 ? 3 : 0;
1066 tmp.op = Operand(op, v1b);
1067 tmp.def = Definition(def, v1b);
1068 tmp.bytes = 1;
1069 tmp.uses[0] = 1;
1070 do_swap(ctx, bld, tmp, preserve_scc, pi);
1071
1072 offset = copy.bytes;
1073 }
1074
1075 for (; offset < copy.bytes;) {
1076 Definition def;
1077 Operand op;
1078 split_copy(offset, &def, &op, copy, true, 8);
1079
1080 assert(op.regClass() == def.regClass());
1081 Operand def_as_op = Operand(def.physReg(), def.regClass());
1082 Definition op_as_def = Definition(op.physReg(), op.regClass());
1083 if (ctx->program->chip_class >= GFX9 && def.regClass() == v1) {
1084 bld.vop1(aco_opcode::v_swap_b32, def, op_as_def, op, def_as_op);
1085 ctx->program->statistics[statistic_copies]++;
1086 } else if (def.regClass() == v1) {
1087 bld.vop2(aco_opcode::v_xor_b32, op_as_def, op, def_as_op);
1088 bld.vop2(aco_opcode::v_xor_b32, def, op, def_as_op);
1089 bld.vop2(aco_opcode::v_xor_b32, op_as_def, op, def_as_op);
1090 ctx->program->statistics[statistic_copies] += 3;
1091 } else if (op.physReg() == scc || def.physReg() == scc) {
1092 /* we need to swap scc and another sgpr */
1093 assert(!preserve_scc);
1094
1095 PhysReg other = op.physReg() == scc ? def.physReg() : op.physReg();
1096
1097 bld.sop1(aco_opcode::s_mov_b32, Definition(pi->scratch_sgpr, s1), Operand(scc, s1));
1098 bld.sopc(aco_opcode::s_cmp_lg_i32, Definition(scc, s1), Operand(other, s1), Operand(0u));
1099 bld.sop1(aco_opcode::s_mov_b32, Definition(other, s1), Operand(pi->scratch_sgpr, s1));
1100 ctx->program->statistics[statistic_copies] += 3;
1101 } else if (def.regClass() == s1) {
1102 if (preserve_scc) {
1103 bld.sop1(aco_opcode::s_mov_b32, Definition(pi->scratch_sgpr, s1), op);
1104 bld.sop1(aco_opcode::s_mov_b32, op_as_def, def_as_op);
1105 bld.sop1(aco_opcode::s_mov_b32, def, Operand(pi->scratch_sgpr, s1));
1106 } else {
1107 bld.sop2(aco_opcode::s_xor_b32, op_as_def, Definition(scc, s1), op, def_as_op);
1108 bld.sop2(aco_opcode::s_xor_b32, def, Definition(scc, s1), op, def_as_op);
1109 bld.sop2(aco_opcode::s_xor_b32, op_as_def, Definition(scc, s1), op, def_as_op);
1110 }
1111 ctx->program->statistics[statistic_copies] += 3;
1112 } else if (def.regClass() == s2) {
1113 if (preserve_scc)
1114 bld.sop1(aco_opcode::s_mov_b32, Definition(pi->scratch_sgpr, s1), Operand(scc, s1));
1115 bld.sop2(aco_opcode::s_xor_b64, op_as_def, Definition(scc, s1), op, def_as_op);
1116 bld.sop2(aco_opcode::s_xor_b64, def, Definition(scc, s1), op, def_as_op);
1117 bld.sop2(aco_opcode::s_xor_b64, op_as_def, Definition(scc, s1), op, def_as_op);
1118 if (preserve_scc)
1119 bld.sopc(aco_opcode::s_cmp_lg_i32, Definition(scc, s1), Operand(pi->scratch_sgpr, s1), Operand(0u));
1120 ctx->program->statistics[statistic_copies] += 3;
1121 } else if (ctx->program->chip_class >= GFX9 && def.bytes() == 2 && def.physReg().reg() == op.physReg().reg()) {
1122 aco_ptr<VOP3P_instruction> vop3p{create_instruction<VOP3P_instruction>(aco_opcode::v_pk_add_u16, Format::VOP3P, 2, 1)};
1123 vop3p->operands[0] = Operand(PhysReg{op.physReg().reg()}, v1);
1124 vop3p->operands[1] = Operand(0u);
1125 vop3p->definitions[0] = Definition(PhysReg{op.physReg().reg()}, v1);
1126 vop3p->opsel_lo = 0x1;
1127 vop3p->opsel_hi = 0x2;
1128 bld.insert(std::move(vop3p));
1129 } else {
1130 assert(def.regClass().is_subdword());
1131 bld.vop2_sdwa(aco_opcode::v_xor_b32, op_as_def, op, def_as_op);
1132 bld.vop2_sdwa(aco_opcode::v_xor_b32, def, op, def_as_op);
1133 bld.vop2_sdwa(aco_opcode::v_xor_b32, op_as_def, op, def_as_op);
1134 ctx->program->statistics[statistic_copies] += 3;
1135 }
1136
1137 offset += def.bytes();
1138 }
1139
1140 /* fixup in case we swapped bytes we shouldn't have */
1141 copy_operation tmp_copy = copy;
1142 tmp_copy.op.setFixed(copy.def.physReg());
1143 tmp_copy.def.setFixed(copy.op.physReg());
1144 do_copy(ctx, bld, tmp_copy, &preserve_scc);
1145 }
1146
1147 void handle_operands(std::map<PhysReg, copy_operation>& copy_map, lower_context* ctx, chip_class chip_class, Pseudo_instruction *pi)
1148 {
1149 Builder bld(ctx->program, &ctx->instructions);
1150 aco_ptr<Instruction> mov;
1151 std::map<PhysReg, copy_operation>::iterator it = copy_map.begin();
1152 std::map<PhysReg, copy_operation>::iterator target;
1153 bool writes_scc = false;
1154
1155 /* count the number of uses for each dst reg */
1156 while (it != copy_map.end()) {
1157
1158 if (it->second.def.physReg() == scc)
1159 writes_scc = true;
1160
1161 assert(!pi->tmp_in_scc || !(it->second.def.physReg() == pi->scratch_sgpr));
1162
1163 /* if src and dst reg are the same, remove operation */
1164 if (it->first == it->second.op.physReg()) {
1165 it = copy_map.erase(it);
1166 continue;
1167 }
1168
1169 /* split large copies */
1170 if (it->second.bytes > 8) {
1171 assert(!it->second.op.isConstant());
1172 assert(!it->second.def.regClass().is_subdword());
1173 RegClass rc = RegClass(it->second.def.regClass().type(), it->second.def.size() - 2);
1174 Definition hi_def = Definition(PhysReg{it->first + 2}, rc);
1175 rc = RegClass(it->second.op.regClass().type(), it->second.op.size() - 2);
1176 Operand hi_op = Operand(PhysReg{it->second.op.physReg() + 2}, rc);
1177 copy_operation copy = {hi_op, hi_def, it->second.bytes - 8};
1178 copy_map[hi_def.physReg()] = copy;
1179 assert(it->second.op.physReg().byte() == 0 && it->second.def.physReg().byte() == 0);
1180 it->second.op = Operand(it->second.op.physReg(), it->second.op.regClass().type() == RegType::sgpr ? s2 : v2);
1181 it->second.def = Definition(it->second.def.physReg(), it->second.def.regClass().type() == RegType::sgpr ? s2 : v2);
1182 it->second.bytes = 8;
1183 }
1184
1185 /* check if the definition reg is used by another copy operation */
1186 for (std::pair<const PhysReg, copy_operation>& copy : copy_map) {
1187 if (copy.second.op.isConstant())
1188 continue;
1189 for (uint16_t i = 0; i < it->second.bytes; i++) {
1190 /* distance might underflow */
1191 unsigned distance = it->first.reg_b + i - copy.second.op.physReg().reg_b;
1192 if (distance < copy.second.bytes)
1193 it->second.uses[i] += 1;
1194 }
1195 }
1196
1197 ++it;
1198 }
1199
1200 /* first, handle paths in the location transfer graph */
1201 bool preserve_scc = pi->tmp_in_scc && !writes_scc;
1202 it = copy_map.begin();
1203 while (it != copy_map.end()) {
1204
1205 /* try to coalesce 32-bit sgpr copies to 64-bit copies */
1206 if (it->second.is_used == 0 &&
1207 it->second.def.getTemp().type() == RegType::sgpr && it->second.bytes == 4 &&
1208 !it->second.op.isConstant() && it->first % 2 == it->second.op.physReg() % 2) {
1209
1210 PhysReg other_def_reg = PhysReg{it->first % 2 ? it->first - 1 : it->first + 1};
1211 PhysReg other_op_reg = PhysReg{it->first % 2 ? it->second.op.physReg() - 1 : it->second.op.physReg() + 1};
1212 std::map<PhysReg, copy_operation>::iterator other = copy_map.find(other_def_reg);
1213
1214 if (other != copy_map.end() && !other->second.is_used && other->second.bytes == 4 &&
1215 other->second.op.physReg() == other_op_reg && !other->second.op.isConstant()) {
1216 std::map<PhysReg, copy_operation>::iterator to_erase = it->first % 2 ? it : other;
1217 it = it->first % 2 ? other : it;
1218 copy_map.erase(to_erase);
1219 it->second.bytes = 8;
1220 }
1221 }
1222 // TODO: try to coalesce subdword copies
1223
1224 /* find portions where the target reg is not used as operand for any other copy */
1225 if (it->second.is_used) {
1226 if (it->second.op.isConstant()) {
1227 /* we have to skip constants until is_used=0 */
1228 ++it;
1229 continue;
1230 }
1231
1232 unsigned has_zero_use_bytes = 0;
1233 for (unsigned i = 0; i < it->second.bytes; i++)
1234 has_zero_use_bytes |= (it->second.uses[i] == 0) << i;
1235
1236 if (has_zero_use_bytes) {
1237 /* Skipping partial copying and doing a v_swap_b32 and then fixup
1238 * copies is usually beneficial for sub-dword copies, but if doing
1239 * a partial copy allows further copies, it should be done instead. */
1240 bool partial_copy = (has_zero_use_bytes == 0xf) || (has_zero_use_bytes == 0xf0);
1241 for (std::pair<const PhysReg, copy_operation>& copy : copy_map) {
1242 if (partial_copy)
1243 break;
1244 for (uint16_t i = 0; i < copy.second.bytes; i++) {
1245 /* distance might underflow */
1246 unsigned distance = copy.first.reg_b + i - it->second.op.physReg().reg_b;
1247 if (distance < it->second.bytes && copy.second.uses[i] == 1 &&
1248 !it->second.uses[distance])
1249 partial_copy = true;
1250 }
1251 }
1252
1253 if (!partial_copy) {
1254 ++it;
1255 continue;
1256 }
1257 } else {
1258 /* full target reg is used: register swapping needed */
1259 ++it;
1260 continue;
1261 }
1262 }
1263
1264 bool did_copy = do_copy(ctx, bld, it->second, &preserve_scc);
1265
1266 std::pair<PhysReg, copy_operation> copy = *it;
1267
1268 if (it->second.is_used == 0) {
1269 /* the target reg is not used as operand for any other copy, so we
1270 * copied to all of it */
1271 copy_map.erase(it);
1272 it = copy_map.begin();
1273 } else {
1274 /* we only performed some portions of this copy, so split it to only
1275 * leave the portions that still need to be done */
1276 copy_operation original = it->second; /* the map insertion below can overwrite this */
1277 copy_map.erase(it);
1278 for (unsigned offset = 0; offset < original.bytes;) {
1279 if (original.uses[offset] == 0) {
1280 offset++;
1281 continue;
1282 }
1283 Definition def;
1284 Operand op;
1285 split_copy(offset, &def, &op, original, false, 8);
1286
1287 copy_operation copy = {op, def, def.bytes()};
1288 for (unsigned i = 0; i < copy.bytes; i++)
1289 copy.uses[i] = original.uses[i + offset];
1290 copy_map[def.physReg()] = copy;
1291
1292 offset += def.bytes();
1293 }
1294
1295 it = copy_map.begin();
1296 }
1297
1298 /* Reduce the number of uses of the operand reg by one. Do this after
1299 * splitting the copy or removing it in case the copy writes to it's own
1300 * operand (for example, v[7:8] = v[8:9]) */
1301 if (did_copy && !copy.second.op.isConstant()) {
1302 for (std::pair<const PhysReg, copy_operation>& other : copy_map) {
1303 for (uint16_t i = 0; i < other.second.bytes; i++) {
1304 /* distance might underflow */
1305 unsigned distance = other.first.reg_b + i - copy.second.op.physReg().reg_b;
1306 if (distance < copy.second.bytes && !copy.second.uses[distance])
1307 other.second.uses[i] -= 1;
1308 }
1309 }
1310 }
1311 }
1312
1313 if (copy_map.empty())
1314 return;
1315
1316 /* all target regs are needed as operand somewhere which means, all entries are part of a cycle */
1317 unsigned largest = 0;
1318 for (const std::pair<const PhysReg, copy_operation>& op : copy_map)
1319 largest = MAX2(largest, op.second.bytes);
1320
1321 while (!copy_map.empty()) {
1322
1323 /* Perform larger swaps first, because larger swaps swaps can make other
1324 * swaps unnecessary. */
1325 auto it = copy_map.begin();
1326 for (auto it2 = copy_map.begin(); it2 != copy_map.end(); ++it2) {
1327 if (it2->second.bytes > it->second.bytes) {
1328 it = it2;
1329 if (it->second.bytes == largest)
1330 break;
1331 }
1332 }
1333
1334 /* should already be done */
1335 assert(!it->second.op.isConstant());
1336
1337 assert(it->second.op.isFixed());
1338 assert(it->second.def.regClass() == it->second.op.regClass());
1339
1340 if (it->first == it->second.op.physReg()) {
1341 copy_map.erase(it);
1342 continue;
1343 }
1344
1345 if (preserve_scc && it->second.def.getTemp().type() == RegType::sgpr)
1346 assert(!(it->second.def.physReg() == pi->scratch_sgpr));
1347
1348 /* to resolve the cycle, we have to swap the src reg with the dst reg */
1349 copy_operation swap = it->second;
1350
1351 /* if this is self-intersecting, we have to split it because
1352 * self-intersecting swaps don't make sense */
1353 PhysReg lower = swap.def.physReg();
1354 PhysReg higher = swap.op.physReg();
1355 if (lower.reg_b > higher.reg_b)
1356 std::swap(lower, higher);
1357 if (higher.reg_b - lower.reg_b < (int)swap.bytes) {
1358 unsigned offset = higher.reg_b - lower.reg_b;
1359 RegType type = swap.def.regClass().type();
1360
1361 copy_operation middle;
1362 lower.reg_b += offset;
1363 higher.reg_b += offset;
1364 middle.bytes = swap.bytes - offset * 2;
1365 memcpy(middle.uses, swap.uses + offset, middle.bytes);
1366 middle.op = Operand(lower, RegClass::get(type, middle.bytes));
1367 middle.def = Definition(higher, RegClass::get(type, middle.bytes));
1368 copy_map[higher] = middle;
1369
1370 copy_operation end;
1371 lower.reg_b += middle.bytes;
1372 higher.reg_b += middle.bytes;
1373 end.bytes = swap.bytes - (offset + middle.bytes);
1374 memcpy(end.uses, swap.uses + offset + middle.bytes, end.bytes);
1375 end.op = Operand(lower, RegClass::get(type, end.bytes));
1376 end.def = Definition(higher, RegClass::get(type, end.bytes));
1377 copy_map[higher] = end;
1378
1379 memset(swap.uses + offset, 0, swap.bytes - offset);
1380 swap.bytes = offset;
1381 }
1382
1383 do_swap(ctx, bld, swap, preserve_scc, pi);
1384
1385 /* remove from map */
1386 copy_map.erase(it);
1387
1388 /* change the operand reg of the target's uses and split uses if needed */
1389 target = copy_map.begin();
1390 uint32_t bytes_left = u_bit_consecutive(0, swap.bytes);
1391 for (; target != copy_map.end(); ++target) {
1392 if (target->second.op.physReg() == swap.def.physReg() && swap.bytes == target->second.bytes) {
1393 target->second.op.setFixed(swap.op.physReg());
1394 break;
1395 }
1396
1397 uint32_t imask = get_intersection_mask(swap.def.physReg().reg_b, swap.bytes,
1398 target->second.op.physReg().reg_b, target->second.bytes);
1399
1400 if (!imask)
1401 continue;
1402
1403 assert(target->second.bytes < swap.bytes);
1404
1405 int offset = (int)target->second.op.physReg().reg_b - (int)swap.def.physReg().reg_b;
1406
1407 /* split and update the middle (the portion that reads the swap's
1408 * definition) to read the swap's operand instead */
1409 int target_op_end = target->second.op.physReg().reg_b + target->second.bytes;
1410 int swap_def_end = swap.def.physReg().reg_b + swap.bytes;
1411 int before_bytes = MAX2(-offset, 0);
1412 int after_bytes = MAX2(target_op_end - swap_def_end, 0);
1413 int middle_bytes = target->second.bytes - before_bytes - after_bytes;
1414
1415 if (after_bytes) {
1416 unsigned after_offset = before_bytes + middle_bytes;
1417 assert(after_offset > 0);
1418 copy_operation copy;
1419 copy.bytes = after_bytes;
1420 memcpy(copy.uses, target->second.uses + after_offset, copy.bytes);
1421 RegClass rc = RegClass::get(target->second.op.regClass().type(), after_bytes);
1422 copy.op = Operand(target->second.op.physReg().advance(after_offset), rc);
1423 copy.def = Definition(target->second.def.physReg().advance(after_offset), rc);
1424 copy_map[copy.def.physReg()] = copy;
1425 }
1426
1427 if (middle_bytes) {
1428 copy_operation copy;
1429 copy.bytes = middle_bytes;
1430 memcpy(copy.uses, target->second.uses + before_bytes, copy.bytes);
1431 RegClass rc = RegClass::get(target->second.op.regClass().type(), middle_bytes);
1432 copy.op = Operand(swap.op.physReg().advance(MAX2(offset, 0)), rc);
1433 copy.def = Definition(target->second.def.physReg().advance(before_bytes), rc);
1434 copy_map[copy.def.physReg()] = copy;
1435 }
1436
1437 if (before_bytes) {
1438 copy_operation copy;
1439 target->second.bytes = before_bytes;
1440 RegClass rc = RegClass::get(target->second.op.regClass().type(), before_bytes);
1441 target->second.op = Operand(target->second.op.physReg(), rc);
1442 target->second.def = Definition(target->second.def.physReg(), rc);
1443 memset(target->second.uses + target->second.bytes, 0, 8 - target->second.bytes);
1444 }
1445
1446 /* break early since we know each byte of the swap's definition is used
1447 * at most once */
1448 bytes_left &= ~imask;
1449 if (!bytes_left)
1450 break;
1451 }
1452 }
1453 }
1454
1455 void lower_to_hw_instr(Program* program)
1456 {
1457 Block *discard_block = NULL;
1458
1459 for (size_t i = 0; i < program->blocks.size(); i++)
1460 {
1461 Block *block = &program->blocks[i];
1462 lower_context ctx;
1463 ctx.program = program;
1464 Builder bld(program, &ctx.instructions);
1465
1466 bool set_mode = i == 0 && block->fp_mode.val != program->config->float_mode;
1467 for (unsigned pred : block->linear_preds) {
1468 if (program->blocks[pred].fp_mode.val != block->fp_mode.val) {
1469 set_mode = true;
1470 break;
1471 }
1472 }
1473 if (set_mode) {
1474 /* only allow changing modes at top-level blocks so this doesn't break
1475 * the "jump over empty blocks" optimization */
1476 assert(block->kind & block_kind_top_level);
1477 uint32_t mode = block->fp_mode.val;
1478 /* "((size - 1) << 11) | register" (MODE is encoded as register 1) */
1479 bld.sopk(aco_opcode::s_setreg_imm32_b32, Operand(mode), (7 << 11) | 1);
1480 }
1481
1482 for (size_t j = 0; j < block->instructions.size(); j++) {
1483 aco_ptr<Instruction>& instr = block->instructions[j];
1484 aco_ptr<Instruction> mov;
1485 if (instr->format == Format::PSEUDO) {
1486 Pseudo_instruction *pi = (Pseudo_instruction*)instr.get();
1487
1488 switch (instr->opcode)
1489 {
1490 case aco_opcode::p_extract_vector:
1491 {
1492 PhysReg reg = instr->operands[0].physReg();
1493 Definition& def = instr->definitions[0];
1494 reg.reg_b += instr->operands[1].constantValue() * def.bytes();
1495
1496 if (reg == def.physReg())
1497 break;
1498
1499 RegClass op_rc = def.regClass().is_subdword() ? def.regClass() :
1500 RegClass(instr->operands[0].getTemp().type(), def.size());
1501 std::map<PhysReg, copy_operation> copy_operations;
1502 copy_operations[def.physReg()] = {Operand(reg, op_rc), def, def.bytes()};
1503 handle_operands(copy_operations, &ctx, program->chip_class, pi);
1504 break;
1505 }
1506 case aco_opcode::p_create_vector:
1507 {
1508 std::map<PhysReg, copy_operation> copy_operations;
1509 PhysReg reg = instr->definitions[0].physReg();
1510
1511 for (const Operand& op : instr->operands) {
1512 if (op.isConstant()) {
1513 const Definition def = Definition(reg, RegClass(instr->definitions[0].getTemp().type(), op.size()));
1514 copy_operations[reg] = {op, def, op.bytes()};
1515 reg.reg_b += op.bytes();
1516 continue;
1517 }
1518 if (op.isUndefined()) {
1519 // TODO: coalesce subdword copies if dst byte is 0
1520 reg.reg_b += op.bytes();
1521 continue;
1522 }
1523
1524 RegClass rc_def = op.regClass().is_subdword() ? op.regClass() :
1525 RegClass(instr->definitions[0].getTemp().type(), op.size());
1526 const Definition def = Definition(reg, rc_def);
1527 copy_operations[def.physReg()] = {op, def, op.bytes()};
1528 reg.reg_b += op.bytes();
1529 }
1530 handle_operands(copy_operations, &ctx, program->chip_class, pi);
1531 break;
1532 }
1533 case aco_opcode::p_split_vector:
1534 {
1535 std::map<PhysReg, copy_operation> copy_operations;
1536 PhysReg reg = instr->operands[0].physReg();
1537
1538 for (const Definition& def : instr->definitions) {
1539 RegClass rc_op = def.regClass().is_subdword() ? def.regClass() :
1540 RegClass(instr->operands[0].getTemp().type(), def.size());
1541 const Operand op = Operand(reg, rc_op);
1542 copy_operations[def.physReg()] = {op, def, def.bytes()};
1543 reg.reg_b += def.bytes();
1544 }
1545 handle_operands(copy_operations, &ctx, program->chip_class, pi);
1546 break;
1547 }
1548 case aco_opcode::p_parallelcopy:
1549 case aco_opcode::p_wqm:
1550 {
1551 std::map<PhysReg, copy_operation> copy_operations;
1552 for (unsigned i = 0; i < instr->operands.size(); i++) {
1553 assert(instr->definitions[i].bytes() == instr->operands[i].bytes());
1554 copy_operations[instr->definitions[i].physReg()] = {instr->operands[i], instr->definitions[i], instr->operands[i].bytes()};
1555 }
1556 handle_operands(copy_operations, &ctx, program->chip_class, pi);
1557 break;
1558 }
1559 case aco_opcode::p_exit_early_if:
1560 {
1561 /* don't bother with an early exit near the end of the program */
1562 if ((block->instructions.size() - 1 - j) <= 4 &&
1563 block->instructions.back()->opcode == aco_opcode::s_endpgm) {
1564 unsigned null_exp_dest = (ctx.program->stage & hw_fs) ? 9 /* NULL */ : V_008DFC_SQ_EXP_POS;
1565 bool ignore_early_exit = true;
1566
1567 for (unsigned k = j + 1; k < block->instructions.size(); ++k) {
1568 const aco_ptr<Instruction> &instr = block->instructions[k];
1569 if (instr->opcode == aco_opcode::s_endpgm ||
1570 instr->opcode == aco_opcode::p_logical_end)
1571 continue;
1572 else if (instr->opcode == aco_opcode::exp &&
1573 static_cast<Export_instruction *>(instr.get())->dest == null_exp_dest)
1574 continue;
1575 else if (instr->opcode == aco_opcode::p_parallelcopy &&
1576 instr->definitions[0].isFixed() &&
1577 instr->definitions[0].physReg() == exec)
1578 continue;
1579
1580 ignore_early_exit = false;
1581 }
1582
1583 if (ignore_early_exit)
1584 break;
1585 }
1586
1587 if (!discard_block) {
1588 discard_block = program->create_and_insert_block();
1589 block = &program->blocks[i];
1590
1591 bld.reset(discard_block);
1592 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
1593 0, V_008DFC_SQ_EXP_NULL, false, true, true);
1594 if (program->wb_smem_l1_on_end)
1595 bld.smem(aco_opcode::s_dcache_wb);
1596 bld.sopp(aco_opcode::s_endpgm);
1597
1598 bld.reset(&ctx.instructions);
1599 }
1600
1601 //TODO: exec can be zero here with block_kind_discard
1602
1603 assert(instr->operands[0].physReg() == scc);
1604 bld.sopp(aco_opcode::s_cbranch_scc0, instr->operands[0], discard_block->index);
1605
1606 discard_block->linear_preds.push_back(block->index);
1607 block->linear_succs.push_back(discard_block->index);
1608 break;
1609 }
1610 case aco_opcode::p_spill:
1611 {
1612 assert(instr->operands[0].regClass() == v1.as_linear());
1613 for (unsigned i = 0; i < instr->operands[2].size(); i++)
1614 bld.writelane(bld.def(v1, instr->operands[0].physReg()),
1615 Operand(PhysReg{instr->operands[2].physReg() + i}, s1),
1616 Operand(instr->operands[1].constantValue() + i),
1617 instr->operands[0]);
1618 break;
1619 }
1620 case aco_opcode::p_reload:
1621 {
1622 assert(instr->operands[0].regClass() == v1.as_linear());
1623 for (unsigned i = 0; i < instr->definitions[0].size(); i++)
1624 bld.readlane(bld.def(s1, PhysReg{instr->definitions[0].physReg() + i}),
1625 instr->operands[0],
1626 Operand(instr->operands[1].constantValue() + i));
1627 break;
1628 }
1629 case aco_opcode::p_as_uniform:
1630 {
1631 if (instr->operands[0].isConstant() || instr->operands[0].regClass().type() == RegType::sgpr) {
1632 std::map<PhysReg, copy_operation> copy_operations;
1633 copy_operations[instr->definitions[0].physReg()] = {instr->operands[0], instr->definitions[0], instr->definitions[0].bytes()};
1634 handle_operands(copy_operations, &ctx, program->chip_class, pi);
1635 } else {
1636 assert(instr->operands[0].regClass().type() == RegType::vgpr);
1637 assert(instr->definitions[0].regClass().type() == RegType::sgpr);
1638 assert(instr->operands[0].size() == instr->definitions[0].size());
1639 for (unsigned i = 0; i < instr->definitions[0].size(); i++) {
1640 bld.vop1(aco_opcode::v_readfirstlane_b32,
1641 bld.def(s1, PhysReg{instr->definitions[0].physReg() + i}),
1642 Operand(PhysReg{instr->operands[0].physReg() + i}, v1));
1643 }
1644 }
1645 break;
1646 }
1647 case aco_opcode::p_bpermute:
1648 {
1649 if (ctx.program->chip_class <= GFX7)
1650 emit_gfx6_bpermute(program, instr, bld);
1651 else if (ctx.program->chip_class == GFX10 && ctx.program->wave_size == 64)
1652 emit_gfx10_wave64_bpermute(program, instr, bld);
1653 else
1654 unreachable("Current hardware supports ds_bpermute, don't emit p_bpermute.");
1655 }
1656 default:
1657 break;
1658 }
1659 } else if (instr->format == Format::PSEUDO_BRANCH) {
1660 Pseudo_branch_instruction* branch = static_cast<Pseudo_branch_instruction*>(instr.get());
1661 /* check if all blocks from current to target are empty */
1662 bool can_remove = block->index < branch->target[0];
1663 for (unsigned i = block->index + 1; can_remove && i < branch->target[0]; i++) {
1664 if (program->blocks[i].instructions.size())
1665 can_remove = false;
1666 }
1667 if (can_remove)
1668 continue;
1669
1670 switch (instr->opcode) {
1671 case aco_opcode::p_branch:
1672 assert(block->linear_succs[0] == branch->target[0]);
1673 bld.sopp(aco_opcode::s_branch, branch->target[0]);
1674 break;
1675 case aco_opcode::p_cbranch_nz:
1676 assert(block->linear_succs[1] == branch->target[0]);
1677 if (branch->operands[0].physReg() == exec)
1678 bld.sopp(aco_opcode::s_cbranch_execnz, branch->target[0]);
1679 else if (branch->operands[0].physReg() == vcc)
1680 bld.sopp(aco_opcode::s_cbranch_vccnz, branch->target[0]);
1681 else {
1682 assert(branch->operands[0].physReg() == scc);
1683 bld.sopp(aco_opcode::s_cbranch_scc1, branch->target[0]);
1684 }
1685 break;
1686 case aco_opcode::p_cbranch_z:
1687 assert(block->linear_succs[1] == branch->target[0]);
1688 if (branch->operands[0].physReg() == exec)
1689 bld.sopp(aco_opcode::s_cbranch_execz, branch->target[0]);
1690 else if (branch->operands[0].physReg() == vcc)
1691 bld.sopp(aco_opcode::s_cbranch_vccz, branch->target[0]);
1692 else {
1693 assert(branch->operands[0].physReg() == scc);
1694 bld.sopp(aco_opcode::s_cbranch_scc0, branch->target[0]);
1695 }
1696 break;
1697 default:
1698 unreachable("Unknown Pseudo branch instruction!");
1699 }
1700
1701 } else if (instr->format == Format::PSEUDO_REDUCTION) {
1702 Pseudo_reduction_instruction* reduce = static_cast<Pseudo_reduction_instruction*>(instr.get());
1703 emit_reduction(&ctx, reduce->opcode, reduce->reduce_op, reduce->cluster_size,
1704 reduce->operands[1].physReg(), // tmp
1705 reduce->definitions[1].physReg(), // stmp
1706 reduce->operands[2].physReg(), // vtmp
1707 reduce->definitions[2].physReg(), // sitmp
1708 reduce->operands[0], reduce->definitions[0]);
1709 } else {
1710 ctx.instructions.emplace_back(std::move(instr));
1711 }
1712
1713 }
1714 block->instructions.swap(ctx.instructions);
1715 }
1716 }
1717
1718 }