1 /*
2 * Copyright © 2020 Valve Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "aco_ir.h"
25 #include "aco_builder.h"
26
27 namespace aco {
28 namespace {
29
30 /* there can also be LDS and VALU clauses, but I don't see how those are interesting */
31 enum clause_type
32 {
33 clause_vmem,
34 clause_flat,
35 clause_smem,
36 clause_other,
37 };
38
emit_clause(Builder & bld,unsigned num_instrs,aco_ptr<Instruction> * instrs)39 void emit_clause(Builder& bld, unsigned num_instrs, aco_ptr<Instruction> *instrs)
40 {
41 unsigned start = 0;
42
43 /* skip any stores at the start */
44 for (; (start < num_instrs) && instrs[start]->definitions.empty(); start++)
45 bld.insert(std::move(instrs[start]));
46
47 unsigned end = start;
48 for (; (end < num_instrs) && !instrs[end]->definitions.empty(); end++)
49 ;
50 unsigned clause_size = end - start;
51
52 if (clause_size > 1)
53 bld.sopp(aco_opcode::s_clause, -1, clause_size - 1);
54
55 for (unsigned i = start; i < num_instrs; i++)
56 bld.insert(std::move(instrs[i]));
57 }
58
59 } /* end namespace */
60
form_hard_clauses(Program * program)61 void form_hard_clauses(Program *program)
62 {
63 for (Block& block : program->blocks) {
64 unsigned num_instrs = 0;
65 aco_ptr<Instruction> current_instrs[64];
66 clause_type current_type = clause_other;
67 unsigned current_resource = 0;
68
69 std::vector<aco_ptr<Instruction>> new_instructions;
70 new_instructions.reserve(block.instructions.size());
71 Builder bld(program, &new_instructions);
72
73 for (unsigned i = 0; i < block.instructions.size(); i++) {
74 aco_ptr<Instruction>& instr = block.instructions[i];
75
76 unsigned resource = 0;
77 clause_type type = clause_other;
78 if (instr->isVMEM() && !instr->operands.empty()) {
79 resource = instr->operands[0].tempId();
80 type = clause_vmem;
81 } else if (instr->format == Format::SCRATCH || instr->format == Format::GLOBAL) {
82 type = clause_vmem;
83 } else if (instr->format == Format::FLAT) {
84 type = clause_flat;
85 } else if (instr->format == Format::SMEM && !instr->operands.empty()) {
86 type = clause_smem;
87 if (instr->operands[0].bytes() == 16)
88 resource = instr->operands[0].tempId();
89 }
90
91 if (type != current_type || resource != current_resource || num_instrs == 64) {
92 emit_clause(bld, num_instrs, current_instrs);
93 num_instrs = 0;
94 current_type = type;
95 current_resource = resource;
96 }
97
98 if (type == clause_other) {
99 bld.insert(std::move(instr));
100 continue;
101 }
102
103 current_instrs[num_instrs++] = std::move(instr);
104 }
105
106 emit_clause(bld, num_instrs, current_instrs);
107
108 block.instructions = std::move(new_instructions);
109 }
110 }
111 }
112