e9c2d66d8233818922f820610563f09b7ff1b5ff
[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 switch (op) {
46 case iadd32: return chip >= GFX9 ? aco_opcode::v_add_u32 : aco_opcode::v_add_co_u32;
47 case imul32: return aco_opcode::v_mul_lo_u32;
48 case fadd32: return aco_opcode::v_add_f32;
49 case fmul32: return aco_opcode::v_mul_f32;
50 case imax32: return aco_opcode::v_max_i32;
51 case imin32: return aco_opcode::v_min_i32;
52 case umin32: return aco_opcode::v_min_u32;
53 case umax32: return aco_opcode::v_max_u32;
54 case fmin32: return aco_opcode::v_min_f32;
55 case fmax32: return aco_opcode::v_max_f32;
56 case iand32: return aco_opcode::v_and_b32;
57 case ixor32: return aco_opcode::v_xor_b32;
58 case ior32: return aco_opcode::v_or_b32;
59 case iadd64: return aco_opcode::num_opcodes;
60 case imul64: return aco_opcode::num_opcodes;
61 case fadd64: return aco_opcode::v_add_f64;
62 case fmul64: return aco_opcode::v_mul_f64;
63 case imin64: return aco_opcode::num_opcodes;
64 case imax64: return aco_opcode::num_opcodes;
65 case umin64: return aco_opcode::num_opcodes;
66 case umax64: return aco_opcode::num_opcodes;
67 case fmin64: return aco_opcode::v_min_f64;
68 case fmax64: return aco_opcode::v_max_f64;
69 case iand64: return aco_opcode::num_opcodes;
70 case ior64: return aco_opcode::num_opcodes;
71 case ixor64: return aco_opcode::num_opcodes;
72 default: return aco_opcode::num_opcodes;
73 }
74 }
75
76 void emit_vadd32(Builder& bld, Definition def, Operand src0, Operand src1)
77 {
78 Instruction *instr = bld.vadd32(def, src0, src1, false, Operand(s2), true);
79 if (instr->definitions.size() >= 2) {
80 assert(instr->definitions[1].regClass() == bld.lm);
81 instr->definitions[1].setFixed(vcc);
82 }
83 }
84
85 void emit_int64_dpp_op(lower_context *ctx, PhysReg dst_reg, PhysReg src0_reg, PhysReg src1_reg,
86 PhysReg vtmp_reg, ReduceOp op,
87 unsigned dpp_ctrl, unsigned row_mask, unsigned bank_mask, bool bound_ctrl,
88 Operand *identity=NULL)
89 {
90 Builder bld(ctx->program, &ctx->instructions);
91 Definition dst[] = {Definition(dst_reg, v1), Definition(PhysReg{dst_reg+1}, v1)};
92 Definition vtmp_def[] = {Definition(vtmp_reg, v1), Definition(PhysReg{vtmp_reg+1}, v1)};
93 Operand src0[] = {Operand(src0_reg, v1), Operand(PhysReg{src0_reg+1}, v1)};
94 Operand src1[] = {Operand(src1_reg, v1), Operand(PhysReg{src1_reg+1}, v1)};
95 Operand src1_64 = Operand(src1_reg, v2);
96 Operand vtmp_op[] = {Operand(vtmp_reg, v1), Operand(PhysReg{vtmp_reg+1}, v1)};
97 Operand vtmp_op64 = Operand(vtmp_reg, v2);
98 if (op == iadd64) {
99 if (ctx->program->chip_class >= GFX10) {
100 if (identity)
101 bld.vop1(aco_opcode::v_mov_b32, vtmp_def[0], identity[0]);
102 bld.vop1_dpp(aco_opcode::v_mov_b32, vtmp_def[0], src0[0],
103 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
104 bld.vop3(aco_opcode::v_add_co_u32_e64, dst[0], bld.def(bld.lm, vcc), vtmp_op[0], src1[0]);
105 } else {
106 bld.vop2_dpp(aco_opcode::v_add_co_u32, dst[0], bld.def(bld.lm, vcc), src0[0], src1[0],
107 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
108 }
109 bld.vop2_dpp(aco_opcode::v_addc_co_u32, dst[1], bld.def(bld.lm, vcc), src0[1], src1[1], Operand(vcc, bld.lm),
110 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
111 } else if (op == iand64) {
112 bld.vop2_dpp(aco_opcode::v_and_b32, dst[0], src0[0], src1[0],
113 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
114 bld.vop2_dpp(aco_opcode::v_and_b32, dst[1], src0[1], src1[1],
115 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
116 } else if (op == ior64) {
117 bld.vop2_dpp(aco_opcode::v_or_b32, dst[0], src0[0], src1[0],
118 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
119 bld.vop2_dpp(aco_opcode::v_or_b32, dst[1], src0[1], src1[1],
120 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
121 } else if (op == ixor64) {
122 bld.vop2_dpp(aco_opcode::v_xor_b32, dst[0], src0[0], src1[0],
123 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
124 bld.vop2_dpp(aco_opcode::v_xor_b32, dst[1], src0[1], src1[1],
125 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
126 } else if (op == umin64 || op == umax64 || op == imin64 || op == imax64) {
127 aco_opcode cmp = aco_opcode::num_opcodes;
128 switch (op) {
129 case umin64:
130 cmp = aco_opcode::v_cmp_gt_u64;
131 break;
132 case umax64:
133 cmp = aco_opcode::v_cmp_lt_u64;
134 break;
135 case imin64:
136 cmp = aco_opcode::v_cmp_gt_i64;
137 break;
138 case imax64:
139 cmp = aco_opcode::v_cmp_lt_i64;
140 break;
141 default:
142 break;
143 }
144
145 if (identity) {
146 bld.vop1(aco_opcode::v_mov_b32, vtmp_def[0], identity[0]);
147 bld.vop1(aco_opcode::v_mov_b32, vtmp_def[1], identity[1]);
148 }
149 bld.vop1_dpp(aco_opcode::v_mov_b32, vtmp_def[0], src0[0],
150 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
151 bld.vop1_dpp(aco_opcode::v_mov_b32, vtmp_def[1], src0[1],
152 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
153
154 bld.vopc(cmp, bld.def(bld.lm, vcc), vtmp_op64, src1_64);
155 bld.vop2(aco_opcode::v_cndmask_b32, dst[0], vtmp_op[0], src1[0], Operand(vcc, bld.lm));
156 bld.vop2(aco_opcode::v_cndmask_b32, dst[1], vtmp_op[1], src1[1], Operand(vcc, bld.lm));
157 } else if (op == imul64) {
158 /* t4 = dpp(x_hi)
159 * t1 = umul_lo(t4, y_lo)
160 * t3 = dpp(x_lo)
161 * t0 = umul_lo(t3, y_hi)
162 * t2 = iadd(t0, t1)
163 * t5 = umul_hi(t3, y_lo)
164 * res_hi = iadd(t2, t5)
165 * res_lo = umul_lo(t3, y_lo)
166 * Requires that res_hi != src0[0] and res_hi != src1[0]
167 * and that vtmp[0] != res_hi.
168 */
169 if (identity)
170 bld.vop1(aco_opcode::v_mov_b32, vtmp_def[0], identity[1]);
171 bld.vop1_dpp(aco_opcode::v_mov_b32, vtmp_def[0], src0[1],
172 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
173 bld.vop3(aco_opcode::v_mul_lo_u32, vtmp_def[1], vtmp_op[0], src1[0]);
174 if (identity)
175 bld.vop1(aco_opcode::v_mov_b32, vtmp_def[0], identity[0]);
176 bld.vop1_dpp(aco_opcode::v_mov_b32, vtmp_def[0], src0[0],
177 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
178 bld.vop3(aco_opcode::v_mul_lo_u32, vtmp_def[0], vtmp_op[0], src1[1]);
179 emit_vadd32(bld, vtmp_def[1], vtmp_op[0], vtmp_op[1]);
180 if (identity)
181 bld.vop1(aco_opcode::v_mov_b32, vtmp_def[0], identity[0]);
182 bld.vop1_dpp(aco_opcode::v_mov_b32, vtmp_def[0], src0[0],
183 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
184 bld.vop3(aco_opcode::v_mul_hi_u32, vtmp_def[0], vtmp_op[0], src1[0]);
185 emit_vadd32(bld, dst[1], vtmp_op[1], vtmp_op[0]);
186 if (identity)
187 bld.vop1(aco_opcode::v_mov_b32, vtmp_def[0], identity[0]);
188 bld.vop1_dpp(aco_opcode::v_mov_b32, vtmp_def[0], src0[0],
189 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
190 bld.vop3(aco_opcode::v_mul_lo_u32, dst[0], vtmp_op[0], src1[0]);
191 }
192 }
193
194 void emit_int64_op(lower_context *ctx, PhysReg dst_reg, PhysReg src0_reg, PhysReg src1_reg, PhysReg vtmp, ReduceOp op)
195 {
196 Builder bld(ctx->program, &ctx->instructions);
197 Definition dst[] = {Definition(dst_reg, v1), Definition(PhysReg{dst_reg+1}, v1)};
198 RegClass src0_rc = src0_reg.reg >= 256 ? v1 : s1;
199 Operand src0[] = {Operand(src0_reg, src0_rc), Operand(PhysReg{src0_reg+1}, src0_rc)};
200 Operand src1[] = {Operand(src1_reg, v1), Operand(PhysReg{src1_reg+1}, v1)};
201 Operand src0_64 = Operand(src0_reg, src0_reg.reg >= 256 ? v2 : s2);
202 Operand src1_64 = Operand(src1_reg, v2);
203
204 if (src0_rc == s1 &&
205 (op == imul64 || op == umin64 || op == umax64 || op == imin64 || op == imax64)) {
206 assert(vtmp.reg != 0);
207 bld.vop1(aco_opcode::v_mov_b32, Definition(vtmp, v1), src0[0]);
208 bld.vop1(aco_opcode::v_mov_b32, Definition(PhysReg{vtmp+1}, v1), src0[1]);
209 src0_reg = vtmp;
210 src0[0] = Operand(vtmp, v1);
211 src0[1] = Operand(PhysReg{vtmp+1}, v1);
212 src0_64 = Operand(vtmp, v2);
213 } else if (src0_rc == s1 && op == iadd64) {
214 assert(vtmp.reg != 0);
215 bld.vop1(aco_opcode::v_mov_b32, Definition(PhysReg{vtmp+1}, v1), src0[1]);
216 src0[1] = Operand(PhysReg{vtmp+1}, v1);
217 }
218
219 if (op == iadd64) {
220 if (ctx->program->chip_class >= GFX10) {
221 bld.vop3(aco_opcode::v_add_co_u32_e64, dst[0], bld.def(bld.lm, vcc), src0[0], src1[0]);
222 } else {
223 bld.vop2(aco_opcode::v_add_co_u32, dst[0], bld.def(bld.lm, vcc), src0[0], src1[0]);
224 }
225 bld.vop2(aco_opcode::v_addc_co_u32, dst[1], bld.def(bld.lm, vcc), src0[1], src1[1], Operand(vcc, bld.lm));
226 } else if (op == iand64) {
227 bld.vop2(aco_opcode::v_and_b32, dst[0], src0[0], src1[0]);
228 bld.vop2(aco_opcode::v_and_b32, dst[1], src0[1], src1[1]);
229 } else if (op == ior64) {
230 bld.vop2(aco_opcode::v_or_b32, dst[0], src0[0], src1[0]);
231 bld.vop2(aco_opcode::v_or_b32, dst[1], src0[1], src1[1]);
232 } else if (op == ixor64) {
233 bld.vop2(aco_opcode::v_xor_b32, dst[0], src0[0], src1[0]);
234 bld.vop2(aco_opcode::v_xor_b32, dst[1], src0[1], src1[1]);
235 } else if (op == umin64 || op == umax64 || op == imin64 || op == imax64) {
236 aco_opcode cmp = aco_opcode::num_opcodes;
237 switch (op) {
238 case umin64:
239 cmp = aco_opcode::v_cmp_gt_u64;
240 break;
241 case umax64:
242 cmp = aco_opcode::v_cmp_lt_u64;
243 break;
244 case imin64:
245 cmp = aco_opcode::v_cmp_gt_i64;
246 break;
247 case imax64:
248 cmp = aco_opcode::v_cmp_lt_i64;
249 break;
250 default:
251 break;
252 }
253
254 bld.vopc(cmp, bld.def(bld.lm, vcc), src0_64, src1_64);
255 bld.vop2(aco_opcode::v_cndmask_b32, dst[0], src0[0], src1[0], Operand(vcc, bld.lm));
256 bld.vop2(aco_opcode::v_cndmask_b32, dst[1], src0[1], src1[1], Operand(vcc, bld.lm));
257 } else if (op == imul64) {
258 if (src1_reg == dst_reg) {
259 /* it's fine if src0==dst but not if src1==dst */
260 std::swap(src0_reg, src1_reg);
261 std::swap(src0[0], src1[0]);
262 std::swap(src0[1], src1[1]);
263 std::swap(src0_64, src1_64);
264 }
265 assert(!(src0_reg == src1_reg));
266 /* t1 = umul_lo(x_hi, y_lo)
267 * t0 = umul_lo(x_lo, y_hi)
268 * t2 = iadd(t0, t1)
269 * t5 = umul_hi(x_lo, y_lo)
270 * res_hi = iadd(t2, t5)
271 * res_lo = umul_lo(x_lo, y_lo)
272 * assumes that it's ok to modify x_hi/y_hi, since we might not have vtmp
273 */
274 Definition tmp0_def(PhysReg{src0_reg+1}, v1);
275 Definition tmp1_def(PhysReg{src1_reg+1}, v1);
276 Operand tmp0_op = src0[1];
277 Operand tmp1_op = src1[1];
278 bld.vop3(aco_opcode::v_mul_lo_u32, tmp0_def, src0[1], src1[0]);
279 bld.vop3(aco_opcode::v_mul_lo_u32, tmp1_def, src0[0], src1[1]);
280 emit_vadd32(bld, tmp0_def, tmp1_op, tmp0_op);
281 bld.vop3(aco_opcode::v_mul_hi_u32, tmp1_def, src0[0], src1[0]);
282 emit_vadd32(bld, dst[1], tmp0_op, tmp1_op);
283 bld.vop3(aco_opcode::v_mul_lo_u32, dst[0], src0[0], src1[0]);
284 }
285 }
286
287 void emit_dpp_op(lower_context *ctx, PhysReg dst_reg, PhysReg src0_reg, PhysReg src1_reg,
288 PhysReg vtmp, ReduceOp op, unsigned size,
289 unsigned dpp_ctrl, unsigned row_mask, unsigned bank_mask, bool bound_ctrl,
290 Operand *identity=NULL) /* for VOP3 with sparse writes */
291 {
292 Builder bld(ctx->program, &ctx->instructions);
293 RegClass rc = RegClass(RegType::vgpr, size);
294 Definition dst(dst_reg, rc);
295 Operand src0(src0_reg, rc);
296 Operand src1(src1_reg, rc);
297
298 aco_opcode opcode = get_reduce_opcode(ctx->program->chip_class, op);
299 bool vop3 = op == imul32 || size == 2;
300
301 if (!vop3) {
302 if (opcode == aco_opcode::v_add_co_u32)
303 bld.vop2_dpp(opcode, dst, bld.def(bld.lm, vcc), src0, src1, dpp_ctrl, row_mask, bank_mask, bound_ctrl);
304 else
305 bld.vop2_dpp(opcode, dst, src0, src1, dpp_ctrl, row_mask, bank_mask, bound_ctrl);
306 return;
307 }
308
309 if (opcode == aco_opcode::num_opcodes) {
310 emit_int64_dpp_op(ctx, dst_reg ,src0_reg, src1_reg, vtmp, op,
311 dpp_ctrl, row_mask, bank_mask, bound_ctrl, identity);
312 return;
313 }
314
315 if (identity)
316 bld.vop1(aco_opcode::v_mov_b32, Definition(vtmp, v1), identity[0]);
317 if (identity && size >= 2)
318 bld.vop1(aco_opcode::v_mov_b32, Definition(PhysReg{vtmp+1}, v1), identity[1]);
319
320 for (unsigned i = 0; i < size; i++)
321 bld.vop1_dpp(aco_opcode::v_mov_b32, Definition(PhysReg{vtmp+i}, v1), Operand(PhysReg{src0_reg+i}, v1),
322 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
323
324 bld.vop3(opcode, dst, Operand(vtmp, rc), src1);
325 }
326
327 void emit_op(lower_context *ctx, PhysReg dst_reg, PhysReg src0_reg, PhysReg src1_reg,
328 PhysReg vtmp, ReduceOp op, unsigned size)
329 {
330 Builder bld(ctx->program, &ctx->instructions);
331 RegClass rc = RegClass(RegType::vgpr, size);
332 Definition dst(dst_reg, rc);
333 Operand src0(src0_reg, RegClass(src0_reg.reg >= 256 ? RegType::vgpr : RegType::sgpr, size));
334 Operand src1(src1_reg, rc);
335
336 aco_opcode opcode = get_reduce_opcode(ctx->program->chip_class, op);
337 bool vop3 = op == imul32 || size == 2;
338
339 if (opcode == aco_opcode::num_opcodes) {
340 emit_int64_op(ctx, dst_reg, src0_reg, src1_reg, vtmp, op);
341 return;
342 }
343
344 if (vop3) {
345 bld.vop3(opcode, dst, src0, src1);
346 } else if (opcode == aco_opcode::v_add_co_u32) {
347 bld.vop2(opcode, dst, bld.def(bld.lm, vcc), src0, src1);
348 } else {
349 bld.vop2(opcode, dst, src0, src1);
350 }
351 }
352
353 void emit_dpp_mov(lower_context *ctx, PhysReg dst, PhysReg src0, unsigned size,
354 unsigned dpp_ctrl, unsigned row_mask, unsigned bank_mask, bool bound_ctrl)
355 {
356 Builder bld(ctx->program, &ctx->instructions);
357 for (unsigned i = 0; i < size; i++) {
358 bld.vop1_dpp(aco_opcode::v_mov_b32, Definition(PhysReg{dst+i}, v1), Operand(PhysReg{src0+i}, v1),
359 dpp_ctrl, row_mask, bank_mask, bound_ctrl);
360 }
361 }
362
363 uint32_t get_reduction_identity(ReduceOp op, unsigned idx)
364 {
365 switch (op) {
366 case iadd32:
367 case iadd64:
368 case fadd32:
369 case fadd64:
370 case ior32:
371 case ior64:
372 case ixor32:
373 case ixor64:
374 case umax32:
375 case umax64:
376 return 0;
377 case imul32:
378 case imul64:
379 return idx ? 0 : 1;
380 case fmul32:
381 return 0x3f800000u; /* 1.0 */
382 case fmul64:
383 return idx ? 0x3ff00000u : 0u; /* 1.0 */
384 case imin32:
385 return INT32_MAX;
386 case imin64:
387 return idx ? 0x7fffffffu : 0xffffffffu;
388 case imax32:
389 return INT32_MIN;
390 case imax64:
391 return idx ? 0x80000000u : 0;
392 case umin32:
393 case umin64:
394 case iand32:
395 case iand64:
396 return 0xffffffffu;
397 case fmin32:
398 return 0x7f800000u; /* infinity */
399 case fmin64:
400 return idx ? 0x7ff00000u : 0u; /* infinity */
401 case fmax32:
402 return 0xff800000u; /* negative infinity */
403 case fmax64:
404 return idx ? 0xfff00000u : 0u; /* negative infinity */
405 default:
406 unreachable("Invalid reduction operation");
407 break;
408 }
409 return 0;
410 }
411
412 void emit_reduction(lower_context *ctx, aco_opcode op, ReduceOp reduce_op, unsigned cluster_size, PhysReg tmp,
413 PhysReg stmp, PhysReg vtmp, PhysReg sitmp, Operand src, Definition dst)
414 {
415 assert(cluster_size == 64 || op == aco_opcode::p_reduce);
416
417 Builder bld(ctx->program, &ctx->instructions);
418
419 Operand identity[2];
420 identity[0] = Operand(get_reduction_identity(reduce_op, 0));
421 identity[1] = Operand(get_reduction_identity(reduce_op, 1));
422 Operand vcndmask_identity[2] = {identity[0], identity[1]};
423
424 /* First, copy the source to tmp and set inactive lanes to the identity */
425 bld.sop1(Builder::s_or_saveexec, Definition(stmp, bld.lm), Definition(scc, s1), Definition(exec, bld.lm), Operand(UINT64_MAX), Operand(exec, bld.lm));
426
427 for (unsigned i = 0; i < src.size(); i++) {
428 /* p_exclusive_scan needs it to be a sgpr or inline constant for the v_writelane_b32
429 * except on GFX10, where v_writelane_b32 can take a literal. */
430 if (identity[i].isLiteral() && op == aco_opcode::p_exclusive_scan && ctx->program->chip_class < GFX10) {
431 bld.sop1(aco_opcode::s_mov_b32, Definition(PhysReg{sitmp+i}, s1), identity[i]);
432 identity[i] = Operand(PhysReg{sitmp+i}, s1);
433
434 bld.vop1(aco_opcode::v_mov_b32, Definition(PhysReg{tmp+i}, v1), identity[i]);
435 vcndmask_identity[i] = Operand(PhysReg{tmp+i}, v1);
436 } else if (identity[i].isLiteral()) {
437 bld.vop1(aco_opcode::v_mov_b32, Definition(PhysReg{tmp+i}, v1), identity[i]);
438 vcndmask_identity[i] = Operand(PhysReg{tmp+i}, v1);
439 }
440 }
441
442 for (unsigned i = 0; i < src.size(); i++) {
443 bld.vop2_e64(aco_opcode::v_cndmask_b32, Definition(PhysReg{tmp + i}, v1),
444 vcndmask_identity[i], Operand(PhysReg{src.physReg() + i}, v1),
445 Operand(stmp, bld.lm));
446 }
447
448 bool exec_restored = false;
449 bool dst_written = false;
450 switch (op) {
451 case aco_opcode::p_reduce:
452 if (cluster_size == 1) break;
453 emit_dpp_op(ctx, tmp, tmp, tmp, vtmp, reduce_op, src.size(),
454 dpp_quad_perm(1, 0, 3, 2), 0xf, 0xf, false);
455 if (cluster_size == 2) break;
456 emit_dpp_op(ctx, tmp, tmp, tmp, vtmp, reduce_op, src.size(),
457 dpp_quad_perm(2, 3, 0, 1), 0xf, 0xf, false);
458 if (cluster_size == 4) break;
459 emit_dpp_op(ctx, tmp, tmp, tmp, vtmp, reduce_op, src.size(),
460 dpp_row_half_mirror, 0xf, 0xf, false);
461 if (cluster_size == 8) break;
462 emit_dpp_op(ctx, tmp, tmp, tmp, vtmp, reduce_op, src.size(),
463 dpp_row_mirror, 0xf, 0xf, false);
464 if (cluster_size == 16) break;
465 if (cluster_size == 32) {
466 for (unsigned i = 0; i < src.size(); i++)
467 bld.ds(aco_opcode::ds_swizzle_b32, Definition(PhysReg{vtmp+i}, v1), Operand(PhysReg{tmp+i}, s1), ds_pattern_bitmode(0x1f, 0, 0x10));
468 bld.sop1(Builder::s_mov, Definition(exec, bld.lm), Operand(stmp, bld.lm));
469 exec_restored = true;
470 emit_op(ctx, dst.physReg(), vtmp, tmp, PhysReg{0}, reduce_op, src.size());
471 dst_written = true;
472 } else if (ctx->program->chip_class >= GFX10) {
473 assert(cluster_size == 64);
474 /* GFX10+ doesn't support row_bcast15 and row_bcast31 */
475 for (unsigned i = 0; i < src.size(); i++)
476 bld.vop3(aco_opcode::v_permlanex16_b32, Definition(PhysReg{vtmp+i}, v1), Operand(PhysReg{tmp+i}, v1), Operand(0u), Operand(0u));
477 emit_op(ctx, tmp, tmp, vtmp, PhysReg{0}, reduce_op, src.size());
478
479 for (unsigned i = 0; i < src.size(); i++)
480 bld.vop3(aco_opcode::v_readlane_b32, Definition(PhysReg{sitmp+i}, s1), Operand(PhysReg{tmp+i}, v1), Operand(31u));
481 emit_op(ctx, tmp, sitmp, tmp, vtmp, reduce_op, src.size());
482 } else {
483 assert(cluster_size == 64);
484 emit_dpp_op(ctx, tmp, tmp, tmp, vtmp, reduce_op, src.size(),
485 dpp_row_bcast15, 0xa, 0xf, false);
486 emit_dpp_op(ctx, tmp, tmp, tmp, vtmp, reduce_op, src.size(),
487 dpp_row_bcast31, 0xc, 0xf, false);
488 }
489 break;
490 case aco_opcode::p_exclusive_scan:
491 if (ctx->program->chip_class >= GFX10) { /* gfx10 doesn't support wf_sr1, so emulate it */
492 /* shift rows right */
493 emit_dpp_mov(ctx, vtmp, tmp, src.size(), dpp_row_sr(1), 0xf, 0xf, true);
494
495 /* fill in the gaps in rows 1 and 3 */
496 bld.sop1(aco_opcode::s_mov_b32, Definition(exec_lo, s1), Operand(0x10000u));
497 bld.sop1(aco_opcode::s_mov_b32, Definition(exec_hi, s1), Operand(0x10000u));
498 for (unsigned i = 0; i < src.size(); i++) {
499 Instruction *perm = bld.vop3(aco_opcode::v_permlanex16_b32,
500 Definition(PhysReg{vtmp+i}, v1),
501 Operand(PhysReg{tmp+i}, v1),
502 Operand(0xffffffffu), Operand(0xffffffffu)).instr;
503 static_cast<VOP3A_instruction*>(perm)->opsel[0] = true; /* FI (Fetch Inactive) */
504 }
505 bld.sop1(Builder::s_mov, Definition(exec, bld.lm), Operand(UINT64_MAX));
506
507 /* fill in the gap in row 2 */
508 for (unsigned i = 0; i < src.size(); i++) {
509 bld.vop3(aco_opcode::v_readlane_b32, Definition(PhysReg{sitmp+i}, s1), Operand(PhysReg{tmp+i}, v1), Operand(31u));
510 bld.vop3(aco_opcode::v_writelane_b32, Definition(PhysReg{vtmp+i}, v1), Operand(PhysReg{sitmp+i}, s1), Operand(32u));
511 }
512 std::swap(tmp, vtmp);
513 } else {
514 emit_dpp_mov(ctx, tmp, tmp, src.size(), dpp_wf_sr1, 0xf, 0xf, true);
515 }
516 for (unsigned i = 0; i < src.size(); i++) {
517 if (!identity[i].isConstant() || identity[i].constantValue()) { /* bound_ctrl should take case of this overwise */
518 if (ctx->program->chip_class < GFX10)
519 assert((identity[i].isConstant() && !identity[i].isLiteral()) || identity[i].physReg() == PhysReg{sitmp+i});
520 bld.vop3(aco_opcode::v_writelane_b32, Definition(PhysReg{tmp+i}, v1),
521 identity[i], Operand(0u));
522 }
523 }
524 /* fall through */
525 case aco_opcode::p_inclusive_scan:
526 assert(cluster_size == 64);
527 emit_dpp_op(ctx, tmp, tmp, tmp, vtmp, reduce_op, src.size(),
528 dpp_row_sr(1), 0xf, 0xf, false, identity);
529 emit_dpp_op(ctx, tmp, tmp, tmp, vtmp, reduce_op, src.size(),
530 dpp_row_sr(2), 0xf, 0xf, false, identity);
531 emit_dpp_op(ctx, tmp, tmp, tmp, vtmp, reduce_op, src.size(),
532 dpp_row_sr(4), 0xf, 0xf, false, identity);
533 emit_dpp_op(ctx, tmp, tmp, tmp, vtmp, reduce_op, src.size(),
534 dpp_row_sr(8), 0xf, 0xf, false, identity);
535 if (ctx->program->chip_class >= GFX10) {
536 bld.sop1(aco_opcode::s_mov_b32, Definition(exec_lo, s1), Operand(0xffff0000u));
537 bld.sop1(aco_opcode::s_mov_b32, Definition(exec_hi, s1), Operand(0xffff0000u));
538 for (unsigned i = 0; i < src.size(); i++) {
539 Instruction *perm = bld.vop3(aco_opcode::v_permlanex16_b32,
540 Definition(PhysReg{vtmp+i}, v1),
541 Operand(PhysReg{tmp+i}, v1),
542 Operand(0xffffffffu), Operand(0xffffffffu)).instr;
543 static_cast<VOP3A_instruction*>(perm)->opsel[0] = true; /* FI (Fetch Inactive) */
544 }
545 emit_op(ctx, tmp, tmp, vtmp, PhysReg{0}, reduce_op, src.size());
546
547 bld.sop1(aco_opcode::s_mov_b32, Definition(exec_lo, s1), Operand(0u));
548 bld.sop1(aco_opcode::s_mov_b32, Definition(exec_hi, s1), Operand(0xffffffffu));
549 for (unsigned i = 0; i < src.size(); i++)
550 bld.vop3(aco_opcode::v_readlane_b32, Definition(PhysReg{sitmp+i}, s1), Operand(PhysReg{tmp+i}, v1), Operand(31u));
551 emit_op(ctx, tmp, sitmp, tmp, vtmp, reduce_op, src.size());
552 } else {
553 emit_dpp_op(ctx, tmp, tmp, tmp, vtmp, reduce_op, src.size(),
554 dpp_row_bcast15, 0xa, 0xf, false, identity);
555 emit_dpp_op(ctx, tmp, tmp, tmp, vtmp, reduce_op, src.size(),
556 dpp_row_bcast31, 0xc, 0xf, false, identity);
557 }
558 break;
559 default:
560 unreachable("Invalid reduction mode");
561 }
562
563 if (!exec_restored)
564 bld.sop1(Builder::s_mov, Definition(exec, bld.lm), Operand(stmp, bld.lm));
565
566 if (op == aco_opcode::p_reduce && cluster_size == 64) {
567 for (unsigned k = 0; k < src.size(); k++) {
568 bld.vop3(aco_opcode::v_readlane_b32, Definition(PhysReg{dst.physReg() + k}, s1),
569 Operand(PhysReg{tmp + k}, v1), Operand(63u));
570 }
571 } else if (!(dst.physReg() == tmp) && !dst_written) {
572 for (unsigned k = 0; k < src.size(); k++) {
573 bld.vop1(aco_opcode::v_mov_b32, Definition(PhysReg{dst.physReg() + k}, s1),
574 Operand(PhysReg{tmp + k}, v1));
575 }
576 }
577 }
578
579 struct copy_operation {
580 Operand op;
581 Definition def;
582 unsigned uses;
583 unsigned size;
584 };
585
586 void handle_operands(std::map<PhysReg, copy_operation>& copy_map, lower_context* ctx, chip_class chip_class, Pseudo_instruction *pi)
587 {
588 Builder bld(ctx->program, &ctx->instructions);
589 aco_ptr<Instruction> mov;
590 std::map<PhysReg, copy_operation>::iterator it = copy_map.begin();
591 std::map<PhysReg, copy_operation>::iterator target;
592 bool writes_scc = false;
593
594 /* count the number of uses for each dst reg */
595 while (it != copy_map.end()) {
596 if (it->second.op.isConstant()) {
597 ++it;
598 continue;
599 }
600
601 if (it->second.def.physReg() == scc)
602 writes_scc = true;
603
604 assert(!pi->tmp_in_scc || !(it->second.def.physReg() == pi->scratch_sgpr));
605
606 /* if src and dst reg are the same, remove operation */
607 if (it->first == it->second.op.physReg()) {
608 it = copy_map.erase(it);
609 continue;
610 }
611 /* check if the operand reg may be overwritten by another copy operation */
612 target = copy_map.find(it->second.op.physReg());
613 if (target != copy_map.end()) {
614 target->second.uses++;
615 }
616
617 ++it;
618 }
619
620 /* first, handle paths in the location transfer graph */
621 bool preserve_scc = pi->tmp_in_scc && !writes_scc;
622 it = copy_map.begin();
623 while (it != copy_map.end()) {
624
625 /* the target reg is not used as operand for any other copy */
626 if (it->second.uses == 0) {
627
628 /* try to coalesce 32-bit sgpr copies to 64-bit copies */
629 if (it->second.def.getTemp().type() == RegType::sgpr && it->second.size == 1 &&
630 !it->second.op.isConstant() && it->first % 2 == it->second.op.physReg() % 2) {
631
632 PhysReg other_def_reg = PhysReg{it->first % 2 ? it->first - 1 : it->first + 1};
633 PhysReg other_op_reg = PhysReg{it->first % 2 ? it->second.op.physReg() - 1 : it->second.op.physReg() + 1};
634 std::map<PhysReg, copy_operation>::iterator other = copy_map.find(other_def_reg);
635
636 if (other != copy_map.end() && !other->second.uses && other->second.size == 1 &&
637 other->second.op.physReg() == other_op_reg && !other->second.op.isConstant()) {
638 std::map<PhysReg, copy_operation>::iterator to_erase = it->first % 2 ? it : other;
639 it = it->first % 2 ? other : it;
640 copy_map.erase(to_erase);
641 it->second.size = 2;
642 }
643 }
644
645 if (it->second.def.physReg() == scc) {
646 bld.sopc(aco_opcode::s_cmp_lg_i32, it->second.def, it->second.op, Operand(0u));
647 preserve_scc = true;
648 } else if (it->second.size == 2 && it->second.def.getTemp().type() == RegType::sgpr) {
649 bld.sop1(aco_opcode::s_mov_b64, it->second.def, Operand(it->second.op.physReg(), s2));
650 } else {
651 bld.copy(it->second.def, it->second.op);
652 }
653
654 /* reduce the number of uses of the operand reg by one */
655 if (!it->second.op.isConstant()) {
656 for (unsigned i = 0; i < it->second.size; i++) {
657 target = copy_map.find(PhysReg{it->second.op.physReg() + i});
658 if (target != copy_map.end())
659 target->second.uses--;
660 }
661 }
662
663 copy_map.erase(it);
664 it = copy_map.begin();
665 continue;
666 } else {
667 /* the target reg is used as operand, check the next entry */
668 ++it;
669 }
670 }
671
672 if (copy_map.empty())
673 return;
674
675 /* all target regs are needed as operand somewhere which means, all entries are part of a cycle */
676 bool constants = false;
677 for (it = copy_map.begin(); it != copy_map.end(); ++it) {
678 assert(it->second.op.isFixed());
679 if (it->first == it->second.op.physReg())
680 continue;
681 /* do constants later */
682 if (it->second.op.isConstant()) {
683 constants = true;
684 continue;
685 }
686
687 if (preserve_scc && it->second.def.getTemp().type() == RegType::sgpr)
688 assert(!(it->second.def.physReg() == pi->scratch_sgpr));
689
690 /* to resolve the cycle, we have to swap the src reg with the dst reg */
691 copy_operation swap = it->second;
692 assert(swap.op.regClass() == swap.def.regClass());
693 Operand def_as_op = Operand(swap.def.physReg(), swap.def.regClass());
694 Definition op_as_def = Definition(swap.op.physReg(), swap.op.regClass());
695 if (chip_class >= GFX9 && swap.def.getTemp().type() == RegType::vgpr) {
696 bld.vop1(aco_opcode::v_swap_b32, swap.def, op_as_def, swap.op, def_as_op);
697 } else if (swap.op.physReg() == scc || swap.def.physReg() == scc) {
698 /* we need to swap scc and another sgpr */
699 assert(!preserve_scc);
700
701 PhysReg other = swap.op.physReg() == scc ? swap.def.physReg() : swap.op.physReg();
702
703 bld.sop1(aco_opcode::s_mov_b32, Definition(pi->scratch_sgpr, s1), Operand(scc, s1));
704 bld.sopc(aco_opcode::s_cmp_lg_i32, Definition(scc, s1), Operand(other, s1), Operand(0u));
705 bld.sop1(aco_opcode::s_mov_b32, Definition(other, s1), Operand(pi->scratch_sgpr, s1));
706 } else if (swap.def.getTemp().type() == RegType::sgpr) {
707 if (preserve_scc) {
708 bld.sop1(aco_opcode::s_mov_b32, Definition(pi->scratch_sgpr, s1), swap.op);
709 bld.sop1(aco_opcode::s_mov_b32, op_as_def, def_as_op);
710 bld.sop1(aco_opcode::s_mov_b32, swap.def, Operand(pi->scratch_sgpr, s1));
711 } else {
712 bld.sop2(aco_opcode::s_xor_b32, op_as_def, Definition(scc, s1), swap.op, def_as_op);
713 bld.sop2(aco_opcode::s_xor_b32, swap.def, Definition(scc, s1), swap.op, def_as_op);
714 bld.sop2(aco_opcode::s_xor_b32, op_as_def, Definition(scc, s1), swap.op, def_as_op);
715 }
716 } else {
717 bld.vop2(aco_opcode::v_xor_b32, op_as_def, swap.op, def_as_op);
718 bld.vop2(aco_opcode::v_xor_b32, swap.def, swap.op, def_as_op);
719 bld.vop2(aco_opcode::v_xor_b32, op_as_def, swap.op, def_as_op);
720 }
721
722 /* change the operand reg of the target's use */
723 assert(swap.uses == 1);
724 target = it;
725 for (++target; target != copy_map.end(); ++target) {
726 if (target->second.op.physReg() == it->first) {
727 target->second.op.setFixed(swap.op.physReg());
728 break;
729 }
730 }
731 }
732
733 /* copy constants into a registers which were operands */
734 if (constants) {
735 for (it = copy_map.begin(); it != copy_map.end(); ++it) {
736 if (!it->second.op.isConstant())
737 continue;
738 if (it->second.def.physReg() == scc) {
739 bld.sopc(aco_opcode::s_cmp_lg_i32, Definition(scc, s1), Operand(0u), Operand(it->second.op.constantValue() ? 1u : 0u));
740 } else {
741 bld.copy(it->second.def, it->second.op);
742 }
743 }
744 }
745 }
746
747 void lower_to_hw_instr(Program* program)
748 {
749 Block *discard_block = NULL;
750
751 for (size_t i = 0; i < program->blocks.size(); i++)
752 {
753 Block *block = &program->blocks[i];
754 lower_context ctx;
755 ctx.program = program;
756 Builder bld(program, &ctx.instructions);
757
758 bool set_mode = i == 0 && block->fp_mode.val != program->config->float_mode;
759 for (unsigned pred : block->linear_preds) {
760 if (program->blocks[pred].fp_mode.val != block->fp_mode.val) {
761 set_mode = true;
762 break;
763 }
764 }
765 if (set_mode) {
766 /* only allow changing modes at top-level blocks so this doesn't break
767 * the "jump over empty blocks" optimization */
768 assert(block->kind & block_kind_top_level);
769 uint32_t mode = block->fp_mode.val;
770 /* "((size - 1) << 11) | register" (MODE is encoded as register 1) */
771 bld.sopk(aco_opcode::s_setreg_imm32_b32, Operand(mode), (7 << 11) | 1);
772 }
773
774 for (size_t j = 0; j < block->instructions.size(); j++) {
775 aco_ptr<Instruction>& instr = block->instructions[j];
776 aco_ptr<Instruction> mov;
777 if (instr->format == Format::PSEUDO) {
778 Pseudo_instruction *pi = (Pseudo_instruction*)instr.get();
779
780 switch (instr->opcode)
781 {
782 case aco_opcode::p_extract_vector:
783 {
784 unsigned reg = instr->operands[0].physReg() + instr->operands[1].constantValue() * instr->definitions[0].size();
785 RegClass rc = RegClass(instr->operands[0].getTemp().type(), 1);
786 RegClass rc_def = RegClass(instr->definitions[0].getTemp().type(), 1);
787 if (reg == instr->definitions[0].physReg())
788 break;
789
790 std::map<PhysReg, copy_operation> copy_operations;
791 for (unsigned i = 0; i < instr->definitions[0].size(); i++) {
792 Definition def = Definition(PhysReg{instr->definitions[0].physReg() + i}, rc_def);
793 copy_operations[def.physReg()] = {Operand(PhysReg{reg + i}, rc), def, 0, 1};
794 }
795 handle_operands(copy_operations, &ctx, program->chip_class, pi);
796 break;
797 }
798 case aco_opcode::p_create_vector:
799 {
800 std::map<PhysReg, copy_operation> copy_operations;
801 RegClass rc_def = RegClass(instr->definitions[0].getTemp().type(), 1);
802 unsigned reg_idx = 0;
803 for (const Operand& op : instr->operands) {
804 if (op.isConstant()) {
805 const PhysReg reg = PhysReg{instr->definitions[0].physReg() + reg_idx};
806 const Definition def = Definition(reg, rc_def);
807 copy_operations[reg] = {op, def, 0, 1};
808 reg_idx++;
809 continue;
810 }
811
812 RegClass rc_op = RegClass(op.getTemp().type(), 1);
813 for (unsigned j = 0; j < op.size(); j++)
814 {
815 const Operand copy_op = Operand(PhysReg{op.physReg() + j}, rc_op);
816 const Definition def = Definition(PhysReg{instr->definitions[0].physReg() + reg_idx}, rc_def);
817 copy_operations[def.physReg()] = {copy_op, def, 0, 1};
818 reg_idx++;
819 }
820 }
821 handle_operands(copy_operations, &ctx, program->chip_class, pi);
822 break;
823 }
824 case aco_opcode::p_split_vector:
825 {
826 std::map<PhysReg, copy_operation> copy_operations;
827 RegClass rc_op = instr->operands[0].isConstant() ? s1 : RegClass(instr->operands[0].regClass().type(), 1);
828 for (unsigned i = 0; i < instr->definitions.size(); i++) {
829 unsigned k = instr->definitions[i].size();
830 RegClass rc_def = RegClass(instr->definitions[i].getTemp().type(), 1);
831 for (unsigned j = 0; j < k; j++) {
832 Operand op = Operand(PhysReg{instr->operands[0].physReg() + (i*k+j)}, rc_op);
833 Definition def = Definition(PhysReg{instr->definitions[i].physReg() + j}, rc_def);
834 copy_operations[def.physReg()] = {op, def, 0, 1};
835 }
836 }
837 handle_operands(copy_operations, &ctx, program->chip_class, pi);
838 break;
839 }
840 case aco_opcode::p_parallelcopy:
841 case aco_opcode::p_wqm:
842 {
843 std::map<PhysReg, copy_operation> copy_operations;
844 for (unsigned i = 0; i < instr->operands.size(); i++)
845 {
846 Operand operand = instr->operands[i];
847 if (operand.isConstant() || operand.size() == 1) {
848 assert(instr->definitions[i].size() == 1);
849 copy_operations[instr->definitions[i].physReg()] = {operand, instr->definitions[i], 0, 1};
850 } else {
851 RegClass def_rc = RegClass(instr->definitions[i].regClass().type(), 1);
852 RegClass op_rc = RegClass(operand.getTemp().type(), 1);
853 for (unsigned j = 0; j < operand.size(); j++)
854 {
855 Operand op = Operand(PhysReg{instr->operands[i].physReg() + j}, op_rc);
856 Definition def = Definition(PhysReg{instr->definitions[i].physReg() + j}, def_rc);
857 copy_operations[def.physReg()] = {op, def, 0, 1};
858 }
859 }
860 }
861 handle_operands(copy_operations, &ctx, program->chip_class, pi);
862 break;
863 }
864 case aco_opcode::p_exit_early_if:
865 {
866 /* don't bother with an early exit at the end of the program */
867 if (block->instructions[j + 1]->opcode == aco_opcode::p_logical_end &&
868 block->instructions[j + 2]->opcode == aco_opcode::s_endpgm) {
869 break;
870 }
871
872 if (!discard_block) {
873 discard_block = program->create_and_insert_block();
874 block = &program->blocks[i];
875
876 bld.reset(discard_block);
877 bld.exp(aco_opcode::exp, Operand(v1), Operand(v1), Operand(v1), Operand(v1),
878 0, V_008DFC_SQ_EXP_NULL, false, true, true);
879 if (program->wb_smem_l1_on_end)
880 bld.smem(aco_opcode::s_dcache_wb);
881 bld.sopp(aco_opcode::s_endpgm);
882
883 bld.reset(&ctx.instructions);
884 }
885
886 //TODO: exec can be zero here with block_kind_discard
887
888 assert(instr->operands[0].physReg() == scc);
889 bld.sopp(aco_opcode::s_cbranch_scc0, instr->operands[0], discard_block->index);
890
891 discard_block->linear_preds.push_back(block->index);
892 block->linear_succs.push_back(discard_block->index);
893 break;
894 }
895 case aco_opcode::p_spill:
896 {
897 assert(instr->operands[0].regClass() == v1.as_linear());
898 for (unsigned i = 0; i < instr->operands[2].size(); i++) {
899 bld.vop3(aco_opcode::v_writelane_b32, bld.def(v1, instr->operands[0].physReg()),
900 Operand(PhysReg{instr->operands[2].physReg() + i}, s1),
901 Operand(instr->operands[1].constantValue() + i));
902 }
903 break;
904 }
905 case aco_opcode::p_reload:
906 {
907 assert(instr->operands[0].regClass() == v1.as_linear());
908 for (unsigned i = 0; i < instr->definitions[0].size(); i++) {
909 bld.vop3(aco_opcode::v_readlane_b32,
910 bld.def(s1, PhysReg{instr->definitions[0].physReg() + i}),
911 instr->operands[0], Operand(instr->operands[1].constantValue() + i));
912 }
913 break;
914 }
915 case aco_opcode::p_as_uniform:
916 {
917 if (instr->operands[0].isConstant() || instr->operands[0].regClass().type() == RegType::sgpr) {
918 std::map<PhysReg, copy_operation> copy_operations;
919 Operand operand = instr->operands[0];
920 if (operand.isConstant() || operand.size() == 1) {
921 assert(instr->definitions[0].size() == 1);
922 copy_operations[instr->definitions[0].physReg()] = {operand, instr->definitions[0], 0, 1};
923 } else {
924 for (unsigned i = 0; i < operand.size(); i++)
925 {
926 Operand op = Operand(PhysReg{operand.physReg() + i}, s1);
927 Definition def = Definition(PhysReg{instr->definitions[0].physReg() + i}, s1);
928 copy_operations[def.physReg()] = {op, def, 0, 1};
929 }
930 }
931
932 handle_operands(copy_operations, &ctx, program->chip_class, pi);
933 } else {
934 assert(instr->operands[0].regClass().type() == RegType::vgpr);
935 assert(instr->definitions[0].regClass().type() == RegType::sgpr);
936 assert(instr->operands[0].size() == instr->definitions[0].size());
937 for (unsigned i = 0; i < instr->definitions[0].size(); i++) {
938 bld.vop1(aco_opcode::v_readfirstlane_b32,
939 bld.def(s1, PhysReg{instr->definitions[0].physReg() + i}),
940 Operand(PhysReg{instr->operands[0].physReg() + i}, v1));
941 }
942 }
943 break;
944 }
945 default:
946 break;
947 }
948 } else if (instr->format == Format::PSEUDO_BRANCH) {
949 Pseudo_branch_instruction* branch = static_cast<Pseudo_branch_instruction*>(instr.get());
950 /* check if all blocks from current to target are empty */
951 bool can_remove = block->index < branch->target[0];
952 for (unsigned i = block->index + 1; can_remove && i < branch->target[0]; i++) {
953 if (program->blocks[i].instructions.size())
954 can_remove = false;
955 }
956 if (can_remove)
957 continue;
958
959 switch (instr->opcode) {
960 case aco_opcode::p_branch:
961 assert(block->linear_succs[0] == branch->target[0]);
962 bld.sopp(aco_opcode::s_branch, branch->target[0]);
963 break;
964 case aco_opcode::p_cbranch_nz:
965 assert(block->linear_succs[1] == branch->target[0]);
966 if (branch->operands[0].physReg() == exec)
967 bld.sopp(aco_opcode::s_cbranch_execnz, branch->target[0]);
968 else if (branch->operands[0].physReg() == vcc)
969 bld.sopp(aco_opcode::s_cbranch_vccnz, branch->target[0]);
970 else {
971 assert(branch->operands[0].physReg() == scc);
972 bld.sopp(aco_opcode::s_cbranch_scc1, branch->target[0]);
973 }
974 break;
975 case aco_opcode::p_cbranch_z:
976 assert(block->linear_succs[1] == branch->target[0]);
977 if (branch->operands[0].physReg() == exec)
978 bld.sopp(aco_opcode::s_cbranch_execz, branch->target[0]);
979 else if (branch->operands[0].physReg() == vcc)
980 bld.sopp(aco_opcode::s_cbranch_vccz, branch->target[0]);
981 else {
982 assert(branch->operands[0].physReg() == scc);
983 bld.sopp(aco_opcode::s_cbranch_scc0, branch->target[0]);
984 }
985 break;
986 default:
987 unreachable("Unknown Pseudo branch instruction!");
988 }
989
990 } else if (instr->format == Format::PSEUDO_REDUCTION) {
991 Pseudo_reduction_instruction* reduce = static_cast<Pseudo_reduction_instruction*>(instr.get());
992 if (reduce->reduce_op == gfx10_wave64_bpermute) {
993 /* Only makes sense on GFX10 wave64 */
994 assert(program->chip_class >= GFX10);
995 assert(program->info->wave_size == 64);
996 assert(instr->definitions[0].regClass() == v1); /* Destination */
997 assert(instr->definitions[1].regClass() == s2); /* Temp EXEC */
998 assert(instr->definitions[1].physReg() != vcc);
999 assert(instr->definitions[2].physReg() == scc); /* SCC clobber */
1000 assert(instr->operands[0].physReg() == vcc); /* Compare */
1001 assert(instr->operands[1].regClass() == v2.as_linear()); /* Temp VGPR pair */
1002 assert(instr->operands[2].regClass() == v1); /* Indices x4 */
1003 assert(instr->operands[3].regClass() == v1); /* Input data */
1004
1005 PhysReg shared_vgpr_reg_lo = PhysReg(align(program->config->num_vgprs, 4) + 256);
1006 PhysReg shared_vgpr_reg_hi = PhysReg(shared_vgpr_reg_lo + 1);
1007 Operand compare = instr->operands[0];
1008 Operand tmp1(instr->operands[1].physReg(), v1);
1009 Operand tmp2(PhysReg(instr->operands[1].physReg() + 1), v1);
1010 Operand index_x4 = instr->operands[2];
1011 Operand input_data = instr->operands[3];
1012 Definition shared_vgpr_lo(shared_vgpr_reg_lo, v1);
1013 Definition shared_vgpr_hi(shared_vgpr_reg_hi, v1);
1014 Definition def_temp1(tmp1.physReg(), v1);
1015 Definition def_temp2(tmp2.physReg(), v1);
1016
1017 /* Save EXEC and set it for all lanes */
1018 bld.sop1(aco_opcode::s_or_saveexec_b64, instr->definitions[1], instr->definitions[2],
1019 Definition(exec, s2), Operand((uint64_t)-1), Operand(exec, s2));
1020
1021 /* HI: Copy data from high lanes 32-63 to shared vgpr */
1022 bld.vop1_dpp(aco_opcode::v_mov_b32, shared_vgpr_hi, input_data, dpp_quad_perm(0, 1, 2, 3), 0xc, 0xf, false);
1023
1024 /* LO: Copy data from low lanes 0-31 to shared vgpr */
1025 bld.vop1_dpp(aco_opcode::v_mov_b32, shared_vgpr_lo, input_data, dpp_quad_perm(0, 1, 2, 3), 0x3, 0xf, false);
1026 /* LO: Copy shared vgpr (high lanes' data) to output vgpr */
1027 bld.vop1_dpp(aco_opcode::v_mov_b32, def_temp1, Operand(shared_vgpr_reg_hi, v1), dpp_quad_perm(0, 1, 2, 3), 0x3, 0xf, false);
1028
1029 /* HI: Copy shared vgpr (low lanes' data) to output vgpr */
1030 bld.vop1_dpp(aco_opcode::v_mov_b32, def_temp1, Operand(shared_vgpr_reg_lo, v1), dpp_quad_perm(0, 1, 2, 3), 0xc, 0xf, false);
1031
1032 /* Permute the original input */
1033 bld.ds(aco_opcode::ds_bpermute_b32, def_temp2, index_x4, input_data);
1034 /* Permute the swapped input */
1035 bld.ds(aco_opcode::ds_bpermute_b32, def_temp1, index_x4, tmp1);
1036
1037 /* Restore saved EXEC */
1038 bld.sop1(aco_opcode::s_mov_b64, Definition(exec, s2), Operand(instr->definitions[1].physReg(), s2));
1039 /* Choose whether to use the original or swapped */
1040 bld.vop2(aco_opcode::v_cndmask_b32, instr->definitions[0], tmp1, tmp2, compare);
1041 } else {
1042 emit_reduction(&ctx, reduce->opcode, reduce->reduce_op, reduce->cluster_size,
1043 reduce->operands[1].physReg(), // tmp
1044 reduce->definitions[1].physReg(), // stmp
1045 reduce->operands[2].physReg(), // vtmp
1046 reduce->definitions[2].physReg(), // sitmp
1047 reduce->operands[0], reduce->definitions[0]);
1048 }
1049 } else {
1050 ctx.instructions.emplace_back(std::move(instr));
1051 }
1052
1053 }
1054 block->instructions.swap(ctx.instructions);
1055 }
1056 }
1057
1058 }