summaryrefslogtreecommitdiff
path: root/src/amd/compiler/aco_reduce_assign.cpp
diff options
context:
space:
mode:
authorDaniel Schürmann <daniel@schuermann.dev>2019-09-17 13:22:17 +0200
committerDaniel Schürmann <daniel@schuermann.dev>2019-09-19 12:10:00 +0200
commit93c8ebfa780ebd1495095e794731881aef29e7d3 (patch)
tree547268dbeabb0d17f14202d4429b3f6abfdb01c5 /src/amd/compiler/aco_reduce_assign.cpp
parent99cbec0a5f463fef4d9c61f34482d9eb00293704 (diff)
downloadmesa-93c8ebfa780ebd1495095e794731881aef29e7d3.tar.gz
aco: Initial commit of independent AMD compiler
ACO (short for AMD Compiler) is a new compiler backend with the goal to replace LLVM for Radeon hardware for the RADV driver. ACO currently supports only VS, PS and CS on VI and Vega. There are some optimizations missing because of unmerged NIR changes which may decrease performance. Full commit history can be found at https://github.com/daniel-schuermann/mesa/commits/backend Co-authored-by: Daniel Schürmann <daniel@schuermann.dev> Co-authored-by: Rhys Perry <pendingchaos02@gmail.com> Co-authored-by: Bas Nieuwenhuizen <bas@basnieuwenhuizen.nl> Co-authored-by: Connor Abbott <cwabbott0@gmail.com> Co-authored-by: Michael Schellenberger Costa <mschellenbergercosta@googlemail.com> Co-authored-by: Timur Kristóf <timur.kristof@gmail.com> Acked-by: Samuel Pitoiset <samuel.pitoiset@gmail.com> Acked-by: Bas Nieuwenhuizen <bas@basnieuwenhuizen.nl>
Diffstat (limited to 'src/amd/compiler/aco_reduce_assign.cpp')
-rw-r--r--src/amd/compiler/aco_reduce_assign.cpp164
1 files changed, 164 insertions, 0 deletions
diff --git a/src/amd/compiler/aco_reduce_assign.cpp b/src/amd/compiler/aco_reduce_assign.cpp
new file mode 100644
index 00000000000..663a43c539a
--- /dev/null
+++ b/src/amd/compiler/aco_reduce_assign.cpp
@@ -0,0 +1,164 @@
+/*
+ * Copyright © 2018 Valve Corporation
+ * Copyright © 2018 Google
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ */
+
+#include "aco_ir.h"
+#include "aco_builder.h"
+
+/*
+ * Insert p_linear_start instructions right before RA to correctly allocate
+ * temporaries for reductions that have to disrespect EXEC by executing in
+ * WWM.
+ */
+
+namespace aco {
+
+void setup_reduce_temp(Program* program)
+{
+ unsigned last_top_level_block_idx = 0;
+ unsigned maxSize = 0;
+
+ std::vector<bool> hasReductions(program->blocks.size());
+ for (Block& block : program->blocks) {
+ for (aco_ptr<Instruction>& instr : block.instructions) {
+ if (instr->format != Format::PSEUDO_REDUCTION)
+ continue;
+
+ maxSize = MAX2(maxSize, instr->operands[0].size());
+ hasReductions[block.index] = true;
+ }
+ }
+
+ if (maxSize == 0)
+ return;
+
+ assert(maxSize == 1 || maxSize == 2);
+ Temp reduceTmp(0, RegClass(RegType::vgpr, maxSize).as_linear());
+ Temp vtmp(0, RegClass(RegType::vgpr, maxSize).as_linear());
+ int inserted_at = -1;
+ int vtmp_inserted_at = -1;
+ bool reduceTmp_in_loop = false;
+ bool vtmp_in_loop = false;
+
+ for (Block& block : program->blocks) {
+
+ /* insert p_end_linear_vgpr after the outermost loop */
+ if (reduceTmp_in_loop && block.loop_nest_depth == 0) {
+ assert(inserted_at == (int)last_top_level_block_idx);
+
+ aco_ptr<Instruction> end{create_instruction<Instruction>(aco_opcode::p_end_linear_vgpr, Format::PSEUDO, vtmp_in_loop ? 2 : 1, 0)};
+ end->operands[0] = Operand(reduceTmp);
+ if (vtmp_in_loop)
+ end->operands[1] = Operand(vtmp);
+ /* insert after the phis of the loop exit block */
+ std::vector<aco_ptr<Instruction>>::iterator it = block.instructions.begin();
+ while ((*it)->opcode == aco_opcode::p_linear_phi || (*it)->opcode == aco_opcode::p_phi)
+ ++it;
+ block.instructions.insert(it, std::move(end));
+ reduceTmp_in_loop = false;
+ }
+
+ if (block.kind & block_kind_top_level)
+ last_top_level_block_idx = block.index;
+
+ if (!hasReductions[block.index])
+ continue;
+
+ std::vector<aco_ptr<Instruction>>::iterator it;
+ for (it = block.instructions.begin(); it != block.instructions.end(); ++it) {
+ Instruction *instr = (*it).get();
+ if (instr->format != Format::PSEUDO_REDUCTION)
+ continue;
+
+ ReduceOp op = static_cast<Pseudo_reduction_instruction *>(instr)->reduce_op;
+ reduceTmp_in_loop |= block.loop_nest_depth > 0;
+
+ if ((int)last_top_level_block_idx != inserted_at) {
+ reduceTmp = {program->allocateId(), reduceTmp.regClass()};
+ aco_ptr<Pseudo_instruction> create{create_instruction<Pseudo_instruction>(aco_opcode::p_start_linear_vgpr, Format::PSEUDO, 0, 1)};
+ create->definitions[0] = Definition(reduceTmp);
+ /* find the right place to insert this definition */
+ if (last_top_level_block_idx == block.index) {
+ /* insert right before the current instruction */
+ it = block.instructions.insert(it, std::move(create));
+ it++;
+ /* inserted_at is intentionally not updated here, so later blocks
+ * would insert at the end instead of using this one. */
+ } else {
+ assert(last_top_level_block_idx < block.index);
+ /* insert before the branch at last top level block */
+ std::vector<aco_ptr<Instruction>>& instructions = program->blocks[last_top_level_block_idx].instructions;
+ instructions.insert(std::next(instructions.begin(), instructions.size() - 1), std::move(create));
+ inserted_at = last_top_level_block_idx;
+ }
+ }
+
+ /* same as before, except for the vector temporary instead of the reduce temporary */
+ bool need_vtmp = op == imul32 || op == fadd64 || op == fmul64 ||
+ op == fmin64 || op == fmax64;
+
+ need_vtmp |= static_cast<Pseudo_reduction_instruction *>(instr)->cluster_size == 32;
+ vtmp_in_loop |= need_vtmp && block.loop_nest_depth > 0;
+ if (need_vtmp && (int)last_top_level_block_idx != vtmp_inserted_at) {
+ vtmp = {program->allocateId(), vtmp.regClass()};
+ aco_ptr<Pseudo_instruction> create{create_instruction<Pseudo_instruction>(aco_opcode::p_start_linear_vgpr, Format::PSEUDO, 0, 1)};
+ create->definitions[0] = Definition(vtmp);
+ if (last_top_level_block_idx == block.index) {
+ it = block.instructions.insert(it, std::move(create));
+ it++;
+ } else {
+ assert(last_top_level_block_idx < block.index);
+ std::vector<aco_ptr<Instruction>>& instructions = program->blocks[last_top_level_block_idx].instructions;
+ instructions.insert(std::next(instructions.begin(), instructions.size() - 1), std::move(create));
+ vtmp_inserted_at = last_top_level_block_idx;
+ }
+ }
+
+ instr->operands[1] = Operand(reduceTmp);
+ if (need_vtmp)
+ instr->operands[2] = Operand(vtmp);
+
+ /* scalar temporary */
+ Builder bld(program);
+ instr->definitions[1] = bld.def(s2);
+
+ /* scalar identity temporary */
+ if (instr->opcode == aco_opcode::p_exclusive_scan &&
+ (op == imin32 || op == imin64 ||
+ op == imax32 || op == imax64 ||
+ op == fmin32 || op == fmin64 ||
+ op == fmax32 || op == fmax64 ||
+ op == fmul64)) {
+ instr->definitions[2] = bld.def(RegClass(RegType::sgpr, instr->operands[0].size()));
+ }
+
+ /* vcc clobber */
+ if (op == iadd32 && program->chip_class < GFX9)
+ instr->definitions[4] = Definition(vcc, s2);
+ }
+ }
+}
+
+};
+