1 /*
2 * Copyright © 2018 Valve Corporation
3 * Copyright © 2018 Google
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 *
24 */
25
26 #include "aco_builder.h"
27 #include "aco_ir.h"
28
29 #include <vector>
30
31 /*
32 * Insert p_linear_start instructions right before RA to correctly allocate
33 * temporaries for reductions that have to disrespect EXEC by executing in
34 * WWM.
35 */
36
37 namespace aco {
38
39 void
setup_reduce_temp(Program * program)40 setup_reduce_temp(Program* program)
41 {
42 unsigned last_top_level_block_idx = 0;
43 unsigned maxSize = 0;
44
45 std::vector<bool> hasReductions(program->blocks.size());
46 for (Block& block : program->blocks) {
47 for (aco_ptr<Instruction>& instr : block.instructions) {
48 if (instr->format != Format::PSEUDO_REDUCTION)
49 continue;
50
51 maxSize = MAX2(maxSize, instr->operands[0].size());
52 hasReductions[block.index] = true;
53 }
54 }
55
56 if (maxSize == 0)
57 return;
58
59 assert(maxSize == 1 || maxSize == 2);
60 Temp reduceTmp(0, RegClass(RegType::vgpr, maxSize).as_linear());
61 Temp vtmp(0, RegClass(RegType::vgpr, maxSize).as_linear());
62 int inserted_at = -1;
63 int vtmp_inserted_at = -1;
64 bool reduceTmp_in_loop = false;
65 bool vtmp_in_loop = false;
66
67 for (Block& block : program->blocks) {
68
69 /* insert p_end_linear_vgpr after the outermost loop */
70 if (reduceTmp_in_loop && block.loop_nest_depth == 0) {
71 assert(inserted_at == (int)last_top_level_block_idx);
72
73 aco_ptr<Instruction> end{create_instruction<Instruction>(
74 aco_opcode::p_end_linear_vgpr, Format::PSEUDO, vtmp_in_loop ? 2 : 1, 0)};
75 end->operands[0] = Operand(reduceTmp);
76 if (vtmp_in_loop)
77 end->operands[1] = Operand(vtmp);
78 /* insert after the phis of the loop exit block */
79 std::vector<aco_ptr<Instruction>>::iterator it = block.instructions.begin();
80 while ((*it)->opcode == aco_opcode::p_linear_phi || (*it)->opcode == aco_opcode::p_phi)
81 ++it;
82 block.instructions.insert(it, std::move(end));
83 reduceTmp_in_loop = false;
84 }
85
86 if (block.kind & block_kind_top_level)
87 last_top_level_block_idx = block.index;
88
89 if (!hasReductions[block.index])
90 continue;
91
92 std::vector<aco_ptr<Instruction>>::iterator it;
93 for (it = block.instructions.begin(); it != block.instructions.end(); ++it) {
94 Instruction* instr = (*it).get();
95 if (instr->format != Format::PSEUDO_REDUCTION)
96 continue;
97
98 ReduceOp op = instr->reduction().reduce_op;
99 reduceTmp_in_loop |= block.loop_nest_depth > 0;
100
101 if ((int)last_top_level_block_idx != inserted_at) {
102 reduceTmp = program->allocateTmp(reduceTmp.regClass());
103 aco_ptr<Pseudo_instruction> create{create_instruction<Pseudo_instruction>(
104 aco_opcode::p_start_linear_vgpr, Format::PSEUDO, 0, 1)};
105 create->definitions[0] = Definition(reduceTmp);
106 /* find the right place to insert this definition */
107 if (last_top_level_block_idx == block.index) {
108 /* insert right before the current instruction */
109 it = block.instructions.insert(it, std::move(create));
110 it++;
111 /* inserted_at is intentionally not updated here, so later blocks
112 * would insert at the end instead of using this one. */
113 } else {
114 assert(last_top_level_block_idx < block.index);
115 /* insert before the branch at last top level block */
116 std::vector<aco_ptr<Instruction>>& instructions =
117 program->blocks[last_top_level_block_idx].instructions;
118 instructions.insert(std::next(instructions.begin(), instructions.size() - 1),
119 std::move(create));
120 inserted_at = last_top_level_block_idx;
121 }
122 }
123
124 /* same as before, except for the vector temporary instead of the reduce temporary */
125 unsigned cluster_size = instr->reduction().cluster_size;
126 bool need_vtmp = op == imul32 || op == fadd64 || op == fmul64 || op == fmin64 ||
127 op == fmax64 || op == umin64 || op == umax64 || op == imin64 ||
128 op == imax64 || op == imul64;
129 bool gfx10_need_vtmp = op == imul8 || op == imax8 || op == imin8 || op == umin8 ||
130 op == imul16 || op == imax16 || op == imin16 || op == umin16 ||
131 op == iadd64;
132
133 if (program->chip_class >= GFX10 && cluster_size == 64)
134 need_vtmp = true;
135 if (program->chip_class >= GFX10 && gfx10_need_vtmp)
136 need_vtmp = true;
137 if (program->chip_class <= GFX7)
138 need_vtmp = true;
139
140 need_vtmp |= cluster_size == 32;
141
142 vtmp_in_loop |= need_vtmp && block.loop_nest_depth > 0;
143 if (need_vtmp && (int)last_top_level_block_idx != vtmp_inserted_at) {
144 vtmp = program->allocateTmp(vtmp.regClass());
145 aco_ptr<Pseudo_instruction> create{create_instruction<Pseudo_instruction>(
146 aco_opcode::p_start_linear_vgpr, Format::PSEUDO, 0, 1)};
147 create->definitions[0] = Definition(vtmp);
148 if (last_top_level_block_idx == block.index) {
149 it = block.instructions.insert(it, std::move(create));
150 it++;
151 } else {
152 assert(last_top_level_block_idx < block.index);
153 std::vector<aco_ptr<Instruction>>& instructions =
154 program->blocks[last_top_level_block_idx].instructions;
155 instructions.insert(std::next(instructions.begin(), instructions.size() - 1),
156 std::move(create));
157 vtmp_inserted_at = last_top_level_block_idx;
158 }
159 }
160
161 instr->operands[1] = Operand(reduceTmp);
162 if (need_vtmp)
163 instr->operands[2] = Operand(vtmp);
164 }
165 }
166 }
167
168 }; // namespace aco
169