aco/gfx10: Add notes about some GFX10 hazards.
[mesa.git] / src / amd / compiler / aco_assembler.cpp
1 #include <vector>
2 #include <algorithm>
3
4 #include "aco_ir.h"
5 #include "common/sid.h"
6 #include "ac_shader_util.h"
7 #include "util/u_math.h"
8
9 namespace aco {
10
11 struct asm_context {
12 Program *program;
13 enum chip_class chip_class;
14 std::vector<std::pair<int, SOPP_instruction*>> branches;
15 std::vector<unsigned> constaddrs;
16 const int16_t* opcode;
17 // TODO: keep track of branch instructions referring blocks
18 // and, when emitting the block, correct the offset in instr
19 asm_context(Program* program) : program(program), chip_class(program->chip_class) {
20 if (chip_class <= GFX9)
21 opcode = &instr_info.opcode_gfx9[0];
22 else if (chip_class == GFX10)
23 opcode = &instr_info.opcode_gfx10[0];
24 }
25
26 int subvector_begin_pos = -1;
27 };
28
29 void emit_instruction(asm_context& ctx, std::vector<uint32_t>& out, Instruction* instr)
30 {
31 uint32_t instr_offset = out.size() * 4u;
32
33 /* lower remaining pseudo-instructions */
34 if (instr->opcode == aco_opcode::p_constaddr) {
35 unsigned dest = instr->definitions[0].physReg();
36 unsigned offset = instr->operands[0].constantValue();
37
38 /* s_getpc_b64 dest[0:1] */
39 uint32_t encoding = (0b101111101 << 23);
40 uint32_t opcode = ctx.opcode[(int)aco_opcode::s_getpc_b64];
41 if (opcode >= 55 && ctx.chip_class <= GFX9) {
42 assert(ctx.chip_class == GFX9 && opcode < 60);
43 opcode = opcode - 4;
44 }
45 encoding |= dest << 16;
46 encoding |= opcode << 8;
47 out.push_back(encoding);
48
49 /* s_add_u32 dest[0], dest[0], ... */
50 encoding = (0b10 << 30);
51 encoding |= ctx.opcode[(int)aco_opcode::s_add_u32] << 23;
52 encoding |= dest << 16;
53 encoding |= dest;
54 encoding |= 255 << 8;
55 out.push_back(encoding);
56 ctx.constaddrs.push_back(out.size());
57 out.push_back(-(instr_offset + 4) + offset);
58
59 /* s_addc_u32 dest[1], dest[1], 0 */
60 encoding = (0b10 << 30);
61 encoding |= ctx.opcode[(int)aco_opcode::s_addc_u32] << 23;
62 encoding |= (dest + 1) << 16;
63 encoding |= dest + 1;
64 encoding |= 128 << 8;
65 out.push_back(encoding);
66 return;
67 }
68
69 uint32_t opcode = ctx.opcode[(int)instr->opcode];
70 if (opcode == (uint32_t)-1) {
71 fprintf(stderr, "Unsupported opcode: ");
72 aco_print_instr(instr, stderr);
73 abort();
74 }
75
76 switch (instr->format) {
77 case Format::SOP2: {
78 uint32_t encoding = (0b10 << 30);
79 encoding |= opcode << 23;
80 encoding |= !instr->definitions.empty() ? instr->definitions[0].physReg() << 16 : 0;
81 encoding |= instr->operands.size() >= 2 ? instr->operands[1].physReg() << 8 : 0;
82 encoding |= !instr->operands.empty() ? instr->operands[0].physReg() : 0;
83 out.push_back(encoding);
84 break;
85 }
86 case Format::SOPK: {
87 SOPK_instruction *sopk = static_cast<SOPK_instruction*>(instr);
88
89 if (instr->opcode == aco_opcode::s_subvector_loop_begin) {
90 assert(ctx.chip_class >= GFX10);
91 assert(ctx.subvector_begin_pos == -1);
92 ctx.subvector_begin_pos = out.size();
93 } else if (instr->opcode == aco_opcode::s_subvector_loop_end) {
94 assert(ctx.chip_class >= GFX10);
95 assert(ctx.subvector_begin_pos != -1);
96 /* Adjust s_subvector_loop_begin instruction to the address after the end */
97 out[ctx.subvector_begin_pos] |= (out.size() - ctx.subvector_begin_pos);
98 /* Adjust s_subvector_loop_end instruction to the address after the beginning */
99 sopk->imm = (uint16_t)(ctx.subvector_begin_pos - (int)out.size());
100 ctx.subvector_begin_pos = -1;
101 }
102
103 uint32_t encoding = (0b1011 << 28);
104 encoding |= opcode << 23;
105 encoding |=
106 !instr->definitions.empty() && !(instr->definitions[0].physReg() == scc) ?
107 instr->definitions[0].physReg() << 16 :
108 !instr->operands.empty() && !(instr->operands[0].physReg() == scc) ?
109 instr->operands[0].physReg() << 16 : 0;
110 encoding |= sopk->imm;
111 out.push_back(encoding);
112 break;
113 }
114 case Format::SOP1: {
115 uint32_t encoding = (0b101111101 << 23);
116 if (opcode >= 55 && ctx.chip_class <= GFX9) {
117 assert(ctx.chip_class == GFX9 && opcode < 60);
118 opcode = opcode - 4;
119 }
120 encoding |= !instr->definitions.empty() ? instr->definitions[0].physReg() << 16 : 0;
121 encoding |= opcode << 8;
122 encoding |= !instr->operands.empty() ? instr->operands[0].physReg() : 0;
123 out.push_back(encoding);
124 break;
125 }
126 case Format::SOPC: {
127 uint32_t encoding = (0b101111110 << 23);
128 encoding |= opcode << 16;
129 encoding |= instr->operands.size() == 2 ? instr->operands[1].physReg() << 8 : 0;
130 encoding |= !instr->operands.empty() ? instr->operands[0].physReg() : 0;
131 out.push_back(encoding);
132 break;
133 }
134 case Format::SOPP: {
135 SOPP_instruction* sopp = static_cast<SOPP_instruction*>(instr);
136 uint32_t encoding = (0b101111111 << 23);
137 encoding |= opcode << 16;
138 encoding |= (uint16_t) sopp->imm;
139 if (sopp->block != -1)
140 ctx.branches.emplace_back(out.size(), sopp);
141 out.push_back(encoding);
142 break;
143 }
144 case Format::SMEM: {
145 SMEM_instruction* smem = static_cast<SMEM_instruction*>(instr);
146 bool soe = instr->operands.size() >= (!instr->definitions.empty() ? 3 : 4);
147 bool is_load = !instr->definitions.empty();
148
149 uint32_t encoding = 0;
150
151 if (ctx.chip_class <= GFX9) {
152 encoding = (0b110000 << 26);
153 assert(!smem->dlc); /* Device-level coherent is not supported on GFX9 and lower */
154 encoding |= smem->nv ? 1 << 15 : 0;
155 } else {
156 encoding = (0b111101 << 26);
157 assert(!smem->nv); /* Non-volatile is not supported on GFX10 */
158 encoding |= smem->dlc ? 1 << 14 : 0;
159 }
160
161 encoding |= opcode << 18;
162 encoding |= smem->glc ? 1 << 16 : 0;
163
164 if (ctx.chip_class <= GFX9) {
165 if (instr->operands.size() >= 2)
166 encoding |= instr->operands[1].isConstant() ? 1 << 17 : 0; /* IMM - immediate enable */
167 }
168 if (ctx.chip_class == GFX9) {
169 encoding |= soe ? 1 << 14 : 0;
170 }
171
172 if (is_load || instr->operands.size() >= 3) { /* SDATA */
173 encoding |= (is_load ? instr->definitions[0].physReg() : instr->operands[2].physReg()) << 6;
174 }
175 if (instr->operands.size() >= 1) { /* SBASE */
176 encoding |= instr->operands[0].physReg() >> 1;
177 }
178
179 out.push_back(encoding);
180 encoding = 0;
181
182 int32_t offset = 0;
183 uint32_t soffset = ctx.chip_class >= GFX10
184 ? sgpr_null /* On GFX10 this is disabled by specifying SGPR_NULL */
185 : 0; /* On GFX9, it is disabled by the SOE bit (and it's not present on GFX8 and below) */
186 if (instr->operands.size() >= 2) {
187 const Operand &op_off1 = instr->operands[1];
188 if (ctx.chip_class <= GFX9) {
189 offset = op_off1.isConstant() ? op_off1.constantValue() : op_off1.physReg();
190 } else {
191 /* GFX10 only supports constants in OFFSET, so put the operand in SOFFSET if it's an SGPR */
192 if (op_off1.isConstant()) {
193 offset = op_off1.constantValue();
194 } else {
195 soffset = op_off1.physReg();
196 assert(!soe); /* There is no place to put the other SGPR offset, if any */
197 }
198 }
199
200 if (soe) {
201 const Operand &op_off2 = instr->operands.back();
202 assert(ctx.chip_class >= GFX9); /* GFX8 and below don't support specifying a constant and an SGPR at the same time */
203 assert(!op_off2.isConstant());
204 soffset = op_off2.physReg();
205 }
206 }
207 encoding |= offset;
208 encoding |= soffset << 25;
209
210 out.push_back(encoding);
211 return;
212 }
213 case Format::VOP2: {
214 uint32_t encoding = 0;
215 encoding |= opcode << 25;
216 encoding |= (0xFF & instr->definitions[0].physReg()) << 17;
217 encoding |= (0xFF & instr->operands[1].physReg()) << 9;
218 encoding |= instr->operands[0].physReg();
219 out.push_back(encoding);
220 break;
221 }
222 case Format::VOP1: {
223 uint32_t encoding = (0b0111111 << 25);
224 if (!instr->definitions.empty())
225 encoding |= (0xFF & instr->definitions[0].physReg()) << 17;
226 encoding |= opcode << 9;
227 if (!instr->operands.empty())
228 encoding |= instr->operands[0].physReg();
229 out.push_back(encoding);
230 break;
231 }
232 case Format::VOPC: {
233 uint32_t encoding = (0b0111110 << 25);
234 encoding |= opcode << 17;
235 encoding |= (0xFF & instr->operands[1].physReg()) << 9;
236 encoding |= instr->operands[0].physReg();
237 out.push_back(encoding);
238 break;
239 }
240 case Format::VINTRP: {
241 Interp_instruction* interp = static_cast<Interp_instruction*>(instr);
242 uint32_t encoding = 0;
243
244 if (ctx.chip_class == GFX8 || ctx.chip_class == GFX9) {
245 encoding = (0b110101 << 26); /* Vega ISA doc says 110010 but it's wrong */
246 } else {
247 encoding = (0b110010 << 26);
248 }
249
250 assert(encoding);
251 encoding |= (0xFF & instr->definitions[0].physReg()) << 18;
252 encoding |= opcode << 16;
253 encoding |= interp->attribute << 10;
254 encoding |= interp->component << 8;
255 if (instr->opcode == aco_opcode::v_interp_mov_f32)
256 encoding |= (0x3 & instr->operands[0].constantValue());
257 else
258 encoding |= (0xFF & instr->operands[0].physReg());
259 out.push_back(encoding);
260 break;
261 }
262 case Format::DS: {
263 DS_instruction* ds = static_cast<DS_instruction*>(instr);
264 uint32_t encoding = (0b110110 << 26);
265 if (ctx.chip_class == GFX8 || ctx.chip_class == GFX9) {
266 encoding |= opcode << 17;
267 encoding |= (ds->gds ? 1 : 0) << 16;
268 } else {
269 encoding |= opcode << 18;
270 encoding |= (ds->gds ? 1 : 0) << 17;
271 }
272 encoding |= ((0xFF & ds->offset1) << 8);
273 encoding |= (0xFFFF & ds->offset0);
274 out.push_back(encoding);
275 encoding = 0;
276 unsigned reg = !instr->definitions.empty() ? instr->definitions[0].physReg() : 0;
277 encoding |= (0xFF & reg) << 24;
278 reg = instr->operands.size() >= 3 && !(instr->operands[2].physReg() == m0) ? instr->operands[2].physReg() : 0;
279 encoding |= (0xFF & reg) << 16;
280 reg = instr->operands.size() >= 2 && !(instr->operands[1].physReg() == m0) ? instr->operands[1].physReg() : 0;
281 encoding |= (0xFF & reg) << 8;
282 encoding |= (0xFF & instr->operands[0].physReg());
283 out.push_back(encoding);
284 break;
285 }
286 case Format::MUBUF: {
287 MUBUF_instruction* mubuf = static_cast<MUBUF_instruction*>(instr);
288 uint32_t encoding = (0b111000 << 26);
289 encoding |= opcode << 18;
290 encoding |= (mubuf->lds ? 1 : 0) << 16;
291 encoding |= (mubuf->glc ? 1 : 0) << 14;
292 encoding |= (mubuf->idxen ? 1 : 0) << 13;
293 encoding |= (mubuf->offen ? 1 : 0) << 12;
294 if (ctx.chip_class <= GFX9) {
295 assert(!mubuf->dlc); /* Device-level coherent is not supported on GFX9 and lower */
296 encoding |= (mubuf->slc ? 1 : 0) << 17;
297 } else if (ctx.chip_class >= GFX10) {
298 encoding |= (mubuf->dlc ? 1 : 0) << 15;
299 }
300 encoding |= 0x0FFF & mubuf->offset;
301 out.push_back(encoding);
302 encoding = 0;
303 if (ctx.chip_class >= GFX10) {
304 encoding |= (mubuf->slc ? 1 : 0) << 22;
305 }
306 encoding |= instr->operands[2].physReg() << 24;
307 encoding |= (mubuf->tfe ? 1 : 0) << 23;
308 encoding |= (instr->operands[1].physReg() >> 2) << 16;
309 unsigned reg = instr->operands.size() > 3 ? instr->operands[3].physReg() : instr->definitions[0].physReg();
310 encoding |= (0xFF & reg) << 8;
311 encoding |= (0xFF & instr->operands[0].physReg());
312 out.push_back(encoding);
313 break;
314 }
315 case Format::MTBUF: {
316 MTBUF_instruction* mtbuf = static_cast<MTBUF_instruction*>(instr);
317
318 uint32_t img_format = ac_get_tbuffer_format(ctx.chip_class, mtbuf->dfmt, mtbuf->nfmt);
319 uint32_t encoding = (0b111010 << 26);
320 assert(!mtbuf->dlc || ctx.chip_class >= GFX10);
321 encoding |= (mtbuf->dlc ? 1 : 0) << 15; /* DLC bit replaces one bit of the OPCODE on GFX10 */
322 encoding |= (mtbuf->glc ? 1 : 0) << 14;
323 encoding |= (mtbuf->idxen ? 1 : 0) << 13;
324 encoding |= (mtbuf->offen ? 1 : 0) << 12;
325 encoding |= 0x0FFF & mtbuf->offset;
326 encoding |= (img_format << 19); /* Handles both the GFX10 FORMAT and the old NFMT+DFMT */
327
328 if (ctx.chip_class <= GFX9) {
329 encoding |= opcode << 15;
330 } else {
331 encoding |= (opcode & 0x07) << 16; /* 3 LSBs of 4-bit OPCODE */
332 }
333
334 out.push_back(encoding);
335 encoding = 0;
336
337 encoding |= instr->operands[2].physReg() << 24;
338 encoding |= (mtbuf->tfe ? 1 : 0) << 23;
339 encoding |= (mtbuf->slc ? 1 : 0) << 22;
340 encoding |= (instr->operands[1].physReg() >> 2) << 16;
341 unsigned reg = instr->operands.size() > 3 ? instr->operands[3].physReg() : instr->definitions[0].physReg();
342 encoding |= (0xFF & reg) << 8;
343 encoding |= (0xFF & instr->operands[0].physReg());
344
345 if (ctx.chip_class >= GFX10) {
346 encoding |= (((opcode & 0x08) >> 4) << 21); /* MSB of 4-bit OPCODE */
347 }
348
349 out.push_back(encoding);
350 break;
351 }
352 case Format::MIMG: {
353 MIMG_instruction* mimg = static_cast<MIMG_instruction*>(instr);
354 uint32_t encoding = (0b111100 << 26);
355 encoding |= mimg->slc ? 1 << 25 : 0;
356 encoding |= opcode << 18;
357 encoding |= mimg->lwe ? 1 << 17 : 0;
358 encoding |= mimg->tfe ? 1 << 16 : 0;
359 encoding |= mimg->glc ? 1 << 13 : 0;
360 encoding |= mimg->unrm ? 1 << 12 : 0;
361 if (ctx.chip_class <= GFX9) {
362 assert(!mimg->dlc); /* Device-level coherent is not supported on GFX9 and lower */
363 assert(!mimg->r128);
364 encoding |= mimg->a16 ? 1 << 15 : 0;
365 encoding |= mimg->da ? 1 << 14 : 0;
366 } else {
367 encoding |= mimg->r128 ? 1 << 15 : 0; /* GFX10: A16 moved to 2nd word, R128 replaces it in 1st word */
368 encoding |= mimg->dim << 3; /* GFX10: dimensionality instead of declare array */
369 encoding |= mimg->dlc ? 1 << 7 : 0;
370 }
371 encoding |= (0xF & mimg->dmask) << 8;
372 out.push_back(encoding);
373 encoding = (0xFF & instr->operands[0].physReg()); /* VADDR */
374 if (!instr->definitions.empty()) {
375 encoding |= (0xFF & instr->definitions[0].physReg()) << 8; /* VDATA */
376 } else if (instr->operands.size() == 4) {
377 encoding |= (0xFF & instr->operands[3].physReg()) << 8; /* VDATA */
378 }
379 encoding |= (0x1F & (instr->operands[1].physReg() >> 2)) << 16; /* T# (resource) */
380 if (instr->operands.size() > 2)
381 encoding |= (0x1F & (instr->operands[2].physReg() >> 2)) << 21; /* sampler */
382
383 assert(!mimg->d16 || ctx.chip_class >= GFX9);
384 encoding |= mimg->d16 ? 1 << 15 : 0;
385 if (ctx.chip_class >= GFX10) {
386 encoding |= mimg->a16 ? 1 << 14 : 0; /* GFX10: A16 still exists, but is in a different place */
387 }
388
389 out.push_back(encoding);
390 break;
391 }
392 case Format::FLAT:
393 case Format::SCRATCH:
394 case Format::GLOBAL: {
395 FLAT_instruction *flat = static_cast<FLAT_instruction*>(instr);
396 uint32_t encoding = (0b110111 << 26);
397 encoding |= opcode << 18;
398 if (ctx.chip_class <= GFX9) {
399 assert(flat->offset <= 0x1fff);
400 encoding |= flat->offset & 0x1fff;
401 } else {
402 /* GFX10 has a 12-bit immediate OFFSET field,
403 * but it has a hw bug: it ignores the offset, called FlatSegmentOffsetBug
404 */
405 assert(flat->offset == 0);
406 }
407 if (instr->format == Format::SCRATCH)
408 encoding |= 1 << 14;
409 else if (instr->format == Format::GLOBAL)
410 encoding |= 2 << 14;
411 encoding |= flat->lds ? 1 << 13 : 0;
412 encoding |= flat->glc ? 1 << 16 : 0;
413 encoding |= flat->slc ? 1 << 17 : 0;
414 if (ctx.chip_class >= GFX10) {
415 assert(!flat->nv);
416 encoding |= flat->dlc ? 1 << 12 : 0;
417 } else {
418 assert(!flat->dlc);
419 }
420 out.push_back(encoding);
421 encoding = (0xFF & instr->operands[0].physReg());
422 if (!instr->definitions.empty())
423 encoding |= (0xFF & instr->definitions[0].physReg()) << 24;
424 else
425 encoding |= (0xFF & instr->operands[2].physReg()) << 8;
426 if (!instr->operands[1].isUndefined()) {
427 assert(ctx.chip_class >= GFX10 || instr->operands[1].physReg() != 0x7F);
428 assert(instr->format != Format::FLAT);
429 encoding |= instr->operands[1].physReg() << 16;
430 } else if (instr->format != Format::FLAT) {
431 if (ctx.chip_class <= GFX9)
432 encoding |= 0x7F << 16;
433 else
434 encoding |= sgpr_null << 16;
435 }
436 encoding |= flat->nv ? 1 << 23 : 0;
437 out.push_back(encoding);
438 break;
439 }
440 case Format::EXP: {
441 Export_instruction* exp = static_cast<Export_instruction*>(instr);
442 uint32_t encoding;
443 if (ctx.chip_class <= GFX9) {
444 encoding = (0b110001 << 26);
445 } else if (ctx.chip_class >= GFX10) {
446 encoding = (0b111110 << 26);
447 }
448
449 encoding |= exp->valid_mask ? 0b1 << 12 : 0;
450 encoding |= exp->done ? 0b1 << 11 : 0;
451 encoding |= exp->compressed ? 0b1 << 10 : 0;
452 encoding |= exp->dest << 4;
453 encoding |= exp->enabled_mask;
454 out.push_back(encoding);
455 encoding = 0xFF & exp->operands[0].physReg();
456 encoding |= (0xFF & exp->operands[1].physReg()) << 8;
457 encoding |= (0xFF & exp->operands[2].physReg()) << 16;
458 encoding |= (0xFF & exp->operands[3].physReg()) << 24;
459 out.push_back(encoding);
460 break;
461 }
462 case Format::PSEUDO:
463 case Format::PSEUDO_BARRIER:
464 unreachable("Pseudo instructions should be lowered before assembly.");
465 default:
466 if ((uint16_t) instr->format & (uint16_t) Format::VOP3A) {
467 VOP3A_instruction* vop3 = static_cast<VOP3A_instruction*>(instr);
468
469 if ((uint16_t) instr->format & (uint16_t) Format::VOP2) {
470 opcode = opcode + 0x100;
471 } else if ((uint16_t) instr->format & (uint16_t) Format::VOP1) {
472 if (ctx.chip_class <= GFX9) {
473 opcode = opcode + 0x140;
474 } else {
475 /* RDNA ISA doc says this is 0x140, but that doesn't work */
476 opcode = opcode + 0x180;
477 }
478 } else if ((uint16_t) instr->format & (uint16_t) Format::VOPC) {
479 opcode = opcode + 0x0;
480 } else if ((uint16_t) instr->format & (uint16_t) Format::VINTRP) {
481 opcode = opcode + 0x270;
482 }
483
484 uint32_t encoding;
485 if (ctx.chip_class <= GFX9) {
486 encoding = (0b110100 << 26);
487 } else if (ctx.chip_class == GFX10) {
488 encoding = (0b110101 << 26);
489 }
490
491 encoding |= opcode << 16;
492 encoding |= (vop3->clamp ? 1 : 0) << 15;
493 for (unsigned i = 0; i < 3; i++)
494 encoding |= vop3->abs[i] << (8+i);
495 for (unsigned i = 0; i < 4; i++)
496 encoding |= vop3->opsel[i] << (11+i);
497 if (instr->definitions.size() == 2)
498 encoding |= instr->definitions[1].physReg() << 8;
499 encoding |= (0xFF & instr->definitions[0].physReg());
500 out.push_back(encoding);
501 encoding = 0;
502 if (instr->opcode == aco_opcode::v_interp_mov_f32) {
503 encoding = 0x3 & instr->operands[0].constantValue();
504 } else {
505 for (unsigned i = 0; i < instr->operands.size(); i++)
506 encoding |= instr->operands[i].physReg() << (i * 9);
507 }
508 encoding |= vop3->omod << 27;
509 for (unsigned i = 0; i < 3; i++)
510 encoding |= vop3->neg[i] << (29+i);
511 out.push_back(encoding);
512
513 } else if (instr->isDPP()){
514 /* first emit the instruction without the DPP operand */
515 Operand dpp_op = instr->operands[0];
516 instr->operands[0] = Operand(PhysReg{250}, v1);
517 instr->format = (Format) ((uint32_t) instr->format & ~(1 << 14));
518 emit_instruction(ctx, out, instr);
519 DPP_instruction* dpp = static_cast<DPP_instruction*>(instr);
520 uint32_t encoding = (0xF & dpp->row_mask) << 28;
521 encoding |= (0xF & dpp->bank_mask) << 24;
522 encoding |= dpp->abs[1] << 23;
523 encoding |= dpp->neg[1] << 22;
524 encoding |= dpp->abs[0] << 21;
525 encoding |= dpp->neg[0] << 20;
526 encoding |= dpp->bound_ctrl << 19;
527 encoding |= dpp->dpp_ctrl << 8;
528 encoding |= (0xFF) & dpp_op.physReg();
529 out.push_back(encoding);
530 return;
531 } else {
532 unreachable("unimplemented instruction format");
533 }
534 break;
535 }
536
537 /* append literal dword */
538 for (const Operand& op : instr->operands) {
539 if (op.isLiteral()) {
540 out.push_back(op.constantValue());
541 break;
542 }
543 }
544 }
545
546 void emit_block(asm_context& ctx, std::vector<uint32_t>& out, Block& block)
547 {
548 for (aco_ptr<Instruction>& instr : block.instructions) {
549 #if 0
550 int start_idx = out.size();
551 std::cerr << "Encoding:\t" << std::endl;
552 aco_print_instr(&*instr, stderr);
553 std::cerr << std::endl;
554 #endif
555 emit_instruction(ctx, out, instr.get());
556 #if 0
557 for (int i = start_idx; i < out.size(); i++)
558 std::cerr << "encoding: " << "0x" << std::setfill('0') << std::setw(8) << std::hex << out[i] << std::endl;
559 #endif
560 }
561 }
562
563 void fix_exports(asm_context& ctx, std::vector<uint32_t>& out, Program* program)
564 {
565 for (int idx = program->blocks.size() - 1; idx >= 0; idx--) {
566 Block& block = program->blocks[idx];
567 std::vector<aco_ptr<Instruction>>::reverse_iterator it = block.instructions.rbegin();
568 bool endBlock = false;
569 bool exported = false;
570 while ( it != block.instructions.rend())
571 {
572 if ((*it)->format == Format::EXP && endBlock) {
573 Export_instruction* exp = static_cast<Export_instruction*>((*it).get());
574 if (program->stage & hw_vs) {
575 if (exp->dest >= V_008DFC_SQ_EXP_POS && exp->dest <= (V_008DFC_SQ_EXP_POS + 3)) {
576 exp->done = true;
577 exported = true;
578 break;
579 }
580 } else {
581 exp->done = true;
582 exp->valid_mask = true;
583 exported = true;
584 break;
585 }
586 } else if ((*it)->definitions.size() && (*it)->definitions[0].physReg() == exec)
587 break;
588 else if ((*it)->opcode == aco_opcode::s_endpgm) {
589 if (endBlock)
590 break;
591 endBlock = true;
592 }
593 ++it;
594 }
595 if (!endBlock || exported)
596 continue;
597 /* we didn't find an Export instruction and have to insert a null export */
598 aco_ptr<Export_instruction> exp{create_instruction<Export_instruction>(aco_opcode::exp, Format::EXP, 4, 0)};
599 for (unsigned i = 0; i < 4; i++)
600 exp->operands[i] = Operand(v1);
601 exp->enabled_mask = 0;
602 exp->compressed = false;
603 exp->done = true;
604 exp->valid_mask = program->stage & hw_fs;
605 if (program->stage & hw_fs)
606 exp->dest = 9; /* NULL */
607 else
608 exp->dest = V_008DFC_SQ_EXP_POS;
609 /* insert the null export 1 instruction before endpgm */
610 block.instructions.insert(block.instructions.end() - 1, std::move(exp));
611 }
612 }
613
614 static void fix_branches_gfx10(asm_context& ctx, std::vector<uint32_t>& out)
615 {
616 /* Branches with an offset of 0x3f are buggy on GFX10, we workaround by inserting NOPs if needed. */
617 bool gfx10_3f_bug = false;
618
619 do {
620 auto buggy_branch_it = std::find_if(ctx.branches.begin(), ctx.branches.end(), [&ctx](const auto &branch) -> bool {
621 return ((int)ctx.program->blocks[branch.second->block].offset - branch.first - 1) == 0x3f;
622 });
623
624 gfx10_3f_bug = buggy_branch_it != ctx.branches.end();
625
626 if (gfx10_3f_bug) {
627 /* Insert an s_nop after the branch */
628 constexpr uint32_t s_nop_0 = 0xbf800000u;
629 int s_nop_pos = buggy_branch_it->first + 1;
630 auto out_pos = std::next(out.begin(), s_nop_pos);
631 out.insert(out_pos, s_nop_0);
632
633 /* Update the offset of each affected block */
634 for (Block& block : ctx.program->blocks) {
635 if (block.offset > (unsigned)buggy_branch_it->first)
636 block.offset++;
637 }
638
639 /* Update the branches following the current one */
640 for (auto branch_it = std::next(buggy_branch_it); branch_it != ctx.branches.end(); ++branch_it)
641 branch_it->first++;
642
643 /* Find first constant address after the inserted instruction */
644 auto caddr_it = std::find_if(ctx.constaddrs.begin(), ctx.constaddrs.end(), [s_nop_pos](const int &caddr_pos) -> bool {
645 return caddr_pos >= s_nop_pos;
646 });
647
648 /* Update the locations of constant addresses */
649 for (; caddr_it != ctx.constaddrs.end(); ++caddr_it)
650 (*caddr_it)++;
651
652 }
653 } while (gfx10_3f_bug);
654 }
655
656 void fix_branches(asm_context& ctx, std::vector<uint32_t>& out)
657 {
658 if (ctx.chip_class >= GFX10)
659 fix_branches_gfx10(ctx, out);
660
661 for (std::pair<int, SOPP_instruction*> &branch : ctx.branches) {
662 int offset = (int)ctx.program->blocks[branch.second->block].offset - branch.first - 1;
663 out[branch.first] |= (uint16_t) offset;
664 }
665 }
666
667 void fix_constaddrs(asm_context& ctx, std::vector<uint32_t>& out)
668 {
669 for (unsigned addr : ctx.constaddrs)
670 out[addr] += out.size() * 4u;
671 }
672
673 unsigned emit_program(Program* program,
674 std::vector<uint32_t>& code)
675 {
676 asm_context ctx(program);
677
678 if (program->stage & (hw_vs | hw_fs))
679 fix_exports(ctx, code, program);
680
681 for (Block& block : program->blocks) {
682 block.offset = code.size();
683 emit_block(ctx, code, block);
684 }
685
686 fix_branches(ctx, code);
687
688 unsigned exec_size = code.size() * sizeof(uint32_t);
689
690 if (program->chip_class >= GFX10) {
691 /* Pad output with s_code_end so instruction prefetching doesn't cause
692 * page faults */
693 unsigned final_size = align(code.size() + 3 * 16, 16);
694 while (code.size() < final_size)
695 code.push_back(0xbf9f0000u);
696 }
697
698 fix_constaddrs(ctx, code);
699
700 while (program->constant_data.size() % 4u)
701 program->constant_data.push_back(0);
702 /* Copy constant data */
703 code.insert(code.end(), (uint32_t*)program->constant_data.data(),
704 (uint32_t*)(program->constant_data.data() + program->constant_data.size()));
705
706 return exec_size;
707 }
708
709 }