• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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->opcode == aco_opcode::p_interp_gfx11 ||
49              instr->opcode == aco_opcode::p_bpermute_permlane) {
50             maxSize = MAX2(maxSize, 1);
51             hasReductions[block.index] = true;
52          } else if (instr->format == Format::PSEUDO_REDUCTION) {
53             maxSize = MAX2(maxSize, instr->operands[0].size());
54             hasReductions[block.index] = true;
55          }
56       }
57    }
58 
59    if (maxSize == 0)
60       return;
61 
62    assert(maxSize == 1 || maxSize == 2);
63    Temp reduceTmp(0, RegClass(RegType::vgpr, maxSize).as_linear());
64    Temp vtmp(0, RegClass(RegType::vgpr, maxSize).as_linear());
65    int inserted_at = -1;
66    int vtmp_inserted_at = -1;
67 
68    for (Block& block : program->blocks) {
69 
70       if (block.kind & block_kind_top_level) {
71          last_top_level_block_idx = block.index;
72 
73          /* TODO: this could be improved in this case:
74           *    start_linear_vgpr
75           *    if (...) {
76           *       use_linear_vgpr
77           *    }
78           *    end_linear_vgpr
79           * Here, the linear vgpr is used before any phi copies, so this isn't necessary.
80           */
81          if (inserted_at >= 0) {
82             aco_ptr<Instruction> end{create_instruction<Instruction>(
83                aco_opcode::p_end_linear_vgpr, Format::PSEUDO, vtmp_inserted_at >= 0 ? 2 : 1, 0)};
84             end->operands[0] = Operand(reduceTmp);
85             if (vtmp_inserted_at >= 0)
86                end->operands[1] = Operand(vtmp);
87             /* insert after the phis of the block */
88             std::vector<aco_ptr<Instruction>>::iterator it = block.instructions.begin();
89             while ((*it)->opcode == aco_opcode::p_linear_phi || (*it)->opcode == aco_opcode::p_phi)
90                ++it;
91             block.instructions.insert(it, std::move(end));
92             inserted_at = vtmp_inserted_at = -1;
93          }
94       }
95 
96       if (!hasReductions[block.index])
97          continue;
98 
99       std::vector<aco_ptr<Instruction>>::iterator it;
100       for (it = block.instructions.begin(); it != block.instructions.end(); ++it) {
101          Instruction* instr = (*it).get();
102          if (instr->format != Format::PSEUDO_REDUCTION &&
103              instr->opcode != aco_opcode::p_interp_gfx11 &&
104              instr->opcode != aco_opcode::p_bpermute_permlane)
105             continue;
106 
107          if ((int)last_top_level_block_idx != inserted_at) {
108             reduceTmp = program->allocateTmp(reduceTmp.regClass());
109             aco_ptr<Pseudo_instruction> create{create_instruction<Pseudo_instruction>(
110                aco_opcode::p_start_linear_vgpr, Format::PSEUDO, 0, 1)};
111             create->definitions[0] = Definition(reduceTmp);
112             /* find the right place to insert this definition */
113             if (last_top_level_block_idx == block.index) {
114                /* insert right before the current instruction */
115                it = block.instructions.insert(it, std::move(create));
116                it++;
117                /* inserted_at is intentionally not updated here, so later blocks
118                 * would insert at the end instead of using this one. */
119             } else {
120                assert(last_top_level_block_idx < block.index);
121                /* insert before the branch at last top level block */
122                std::vector<aco_ptr<Instruction>>& instructions =
123                   program->blocks[last_top_level_block_idx].instructions;
124                instructions.insert(std::next(instructions.begin(), instructions.size() - 1),
125                                    std::move(create));
126                inserted_at = last_top_level_block_idx;
127             }
128          }
129 
130          /* same as before, except for the vector temporary instead of the reduce temporary */
131          bool need_vtmp = false;
132          if (instr->isReduction()) {
133             ReduceOp op = instr->reduction().reduce_op;
134             unsigned cluster_size = instr->reduction().cluster_size;
135             need_vtmp = op == imul32 || op == fadd64 || op == fmul64 || op == fmin64 ||
136                         op == fmax64 || op == umin64 || op == umax64 || op == imin64 ||
137                         op == imax64 || op == imul64;
138             bool gfx10_need_vtmp = op == imul8 || op == imax8 || op == imin8 || op == umin8 ||
139                                    op == imul16 || op == imax16 || op == imin16 || op == umin16 ||
140                                    op == iadd64;
141 
142             if (program->gfx_level >= GFX10 && cluster_size == 64)
143                need_vtmp = true;
144             if (program->gfx_level >= GFX10 && gfx10_need_vtmp)
145                need_vtmp = true;
146             if (program->gfx_level <= GFX7)
147                need_vtmp = true;
148 
149             need_vtmp |= cluster_size == 32;
150          }
151 
152          if (need_vtmp && (int)last_top_level_block_idx != vtmp_inserted_at) {
153             vtmp = program->allocateTmp(vtmp.regClass());
154             aco_ptr<Pseudo_instruction> create{create_instruction<Pseudo_instruction>(
155                aco_opcode::p_start_linear_vgpr, Format::PSEUDO, 0, 1)};
156             create->definitions[0] = Definition(vtmp);
157             if (last_top_level_block_idx == block.index) {
158                it = block.instructions.insert(it, std::move(create));
159                it++;
160             } else {
161                assert(last_top_level_block_idx < block.index);
162                std::vector<aco_ptr<Instruction>>& instructions =
163                   program->blocks[last_top_level_block_idx].instructions;
164                instructions.insert(std::next(instructions.begin(), instructions.size() - 1),
165                                    std::move(create));
166                vtmp_inserted_at = last_top_level_block_idx;
167             }
168          }
169 
170          if (instr->isReduction()) {
171             instr->operands[1] = Operand(reduceTmp);
172             if (need_vtmp)
173                instr->operands[2] = Operand(vtmp);
174          } else {
175             assert(instr->opcode == aco_opcode::p_interp_gfx11 ||
176                    instr->opcode == aco_opcode::p_bpermute_permlane);
177             instr->operands[0] = Operand(reduceTmp);
178             instr->operands[0].setLateKill(true);
179          }
180       }
181    }
182 }
183 
184 }; // namespace aco
185