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