xref: /aosp_15_r20/external/mesa3d/src/panfrost/compiler/bi_layout.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright (C) 2020 Collabora, Ltd.
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 FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #include "compiler.h"
25 
26 /* The scheduler packs multiple instructions into a clause (grouped as tuple),
27  * and the packing code takes in a clause and emits it to the wire. During
28  * scheduling, we need to lay out the instructions (tuples) and constants
29  * within the clause so constraints can be resolved during scheduling instead
30  * of failing packing. These routines will help building clauses from
31  * instructions so the scheduler can focus on the high-level algorithm, and
32  * manipulating clause layouts.
33  */
34 
35 /* Is embedded constant 0 packed for free in a clause with this many tuples? */
36 
37 bool
bi_ec0_packed(unsigned tuple_count)38 bi_ec0_packed(unsigned tuple_count)
39 {
40    return (tuple_count == 3) || (tuple_count == 5) || (tuple_count == 6) ||
41           (tuple_count == 8);
42 }
43 
44 /* Helper to calculate the number of quadwords in a clause. This is a function
45  * of the number of instructions and constants; it doesn't require actually
46  * packing, which is useful for branch offsets.
47  *
48  * Table of instruction count to instruction quadwords, per the packing
49  * algorithm, where * indicates a constant is packed for free:
50  *
51  *   X | Y
52  *  ---|---
53  *   1 | 1
54  *   2 | 2
55  *   3 | 3*
56  *   4 | 3
57  *   5 | 4*
58  *   6 | 5*
59  *   7 | 5
60  *   8 | 6*
61  *
62  * Y = { X      if X <= 3
63  *     { X - 1  if 4 <= X <= 6
64  *     { X - 2  if 7 <= X <= 8
65  *
66  * and there is a constant for free if X is in {3, 5, 6, 8}. The remaining
67  * constants are packed two-by-two as constant quadwords.
68  */
69 
70 static unsigned
bi_clause_quadwords(bi_clause * clause)71 bi_clause_quadwords(bi_clause *clause)
72 {
73    unsigned X = clause->tuple_count;
74    unsigned Y = X - ((X >= 7) ? 2 : (X >= 4) ? 1 : 0);
75 
76    unsigned constants = clause->constant_count;
77 
78    if ((X != 4) && (X != 7) && (X >= 3) && constants)
79       constants--;
80 
81    return Y + DIV_ROUND_UP(constants, 2);
82 }
83 
84 /* Measures the number of quadwords a branch jumps. Bifrost relative offsets
85  * are from the beginning of a clause so to jump forward we count the current
86  * clause length, but to jump backwards we do not. */
87 
88 signed
bi_block_offset(bi_context * ctx,bi_clause * start,bi_block * target)89 bi_block_offset(bi_context *ctx, bi_clause *start, bi_block *target)
90 {
91    /* Signed since we might jump backwards */
92    signed ret = 0;
93 
94    /* Determine if the block we're branching to is strictly greater in
95     * source order */
96    bool forwards = target->index > start->block->index;
97 
98    if (forwards) {
99       /* We have to jump through this block from the start of this
100        * clause to the end */
101       bi_foreach_clause_in_block_from(start->block, clause, start) {
102          ret += bi_clause_quadwords(clause);
103       }
104 
105       /* We then need to jump through every clause of every following
106        * block until the target */
107       bi_foreach_block_from(ctx, start->block, blk) {
108          /* Don't double-count the first block */
109          if (blk == start->block)
110             continue;
111 
112          /* End just before the target */
113          if (blk == target)
114             break;
115 
116          /* Count every clause in the block */
117          bi_foreach_clause_in_block(blk, clause) {
118             ret += bi_clause_quadwords(clause);
119          }
120       }
121    } else {
122       /* We start at the beginning of the clause but have to jump
123        * through the clauses before us in the block */
124       bi_foreach_clause_in_block_from_rev(start->block, clause, start) {
125          if (clause == start)
126             continue;
127 
128          ret -= bi_clause_quadwords(clause);
129       }
130 
131       /* And jump back every clause of preceding blocks up through
132        * and including the target to get to the beginning of the
133        * target */
134       bi_foreach_block_from_rev(ctx, start->block, blk) {
135          if (blk == start->block)
136             continue;
137 
138          bi_foreach_clause_in_block(blk, clause) {
139             ret -= bi_clause_quadwords(clause);
140          }
141 
142          /* End just after the target */
143          if (blk == target)
144             break;
145       }
146    }
147 
148    return ret;
149 }
150