xref: /aosp_15_r20/external/mesa3d/src/panfrost/compiler/bi_opt_cse.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright (C) 2021 Collabora, Ltd.
3  * Copyright (C) 2014 Valve Corporation
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 FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 #include "bi_builder.h"
26 #include "compiler.h"
27 
28 #define XXH_INLINE_ALL
29 #include "util/xxhash.h"
30 
31 /* This pass handles CSE'ing repeated expressions created in the process of
32  * translating from NIR. Also, currently this is intra-block only, to make it
33  * work over multiple block we'd need to bring forward dominance calculation.
34  */
35 
36 static inline uint32_t
HASH(uint32_t hash,unsigned data)37 HASH(uint32_t hash, unsigned data)
38 {
39    return XXH32(&data, sizeof(data), hash);
40 }
41 
42 static uint32_t
hash_index(uint32_t hash,bi_index index)43 hash_index(uint32_t hash, bi_index index)
44 {
45    hash = HASH(hash, index.value);
46    hash = HASH(hash, index.abs);
47    hash = HASH(hash, index.neg);
48    hash = HASH(hash, index.swizzle);
49    hash = HASH(hash, index.offset);
50    hash = HASH(hash, index.type);
51    return hash;
52 }
53 
54 /* Hash an ALU instruction. */
55 static uint32_t
hash_instr(const void * data)56 hash_instr(const void *data)
57 {
58    const bi_instr *I = data;
59    uint32_t hash = 0;
60 
61    hash = HASH(hash, I->op);
62    hash = HASH(hash, I->nr_dests);
63    hash = HASH(hash, I->nr_srcs);
64 
65    assert(!I->flow && !I->slot && "CSE must be early");
66 
67    /* Explcitly skip destinations, except for size details */
68    bi_foreach_dest(I, d) {
69       hash = HASH(hash, I->dest[d].swizzle);
70    }
71 
72    bi_foreach_src(I, s) {
73       hash = hash_index(hash, I->src[s]);
74    }
75 
76    /* Explicitly skip branch, regfmt, vecsize, no_spill, tdd, table */
77    hash = HASH(hash, I->dest_mod);
78 
79    /* Explicitly skip other immediates */
80    hash = HASH(hash, I->shift);
81 
82    for (unsigned i = 0; i < ARRAY_SIZE(I->flags); ++i)
83       hash = HASH(hash, I->flags[i]);
84 
85    return hash;
86 }
87 
88 static bool
instrs_equal(const void * _i1,const void * _i2)89 instrs_equal(const void *_i1, const void *_i2)
90 {
91    const bi_instr *i1 = _i1, *i2 = _i2;
92 
93    if (i1->op != i2->op)
94       return false;
95    if (i1->nr_srcs != i2->nr_srcs)
96       return false;
97    if (i1->nr_dests != i2->nr_dests)
98       return false;
99 
100    /* Explicitly skip destinations */
101 
102    bi_foreach_src(i1, s) {
103       bi_index s1 = i1->src[s], s2 = i2->src[s];
104 
105       if (memcmp(&s1, &s2, sizeof(s1)) != 0)
106          return false;
107    }
108 
109    if (i1->dest_mod != i2->dest_mod)
110       return false;
111 
112    if (i1->shift != i2->shift)
113       return false;
114 
115    for (unsigned i = 0; i < ARRAY_SIZE(i1->flags); ++i) {
116       if (i1->flags[i] != i2->flags[i])
117          return false;
118    }
119 
120    return true;
121 }
122 
123 /* Determines what instructions the above routines have to handle */
124 
125 static bool
instr_can_cse(const bi_instr * I)126 instr_can_cse(const bi_instr *I)
127 {
128    switch (I->op) {
129    case BI_OPCODE_DTSEL_IMM:
130    case BI_OPCODE_DISCARD_F32:
131       return false;
132    default:
133       break;
134    }
135 
136    /* Be conservative about which message-passing instructions we CSE,
137     * since most are not pure even within a thread.
138     */
139    if (bi_opcode_props[I->op].message && I->op != BI_OPCODE_LEA_BUF_IMM)
140       return false;
141 
142    if (I->branch_target)
143       return false;
144 
145    return true;
146 }
147 
148 void
bi_opt_cse(bi_context * ctx)149 bi_opt_cse(bi_context *ctx)
150 {
151    struct set *instr_set = _mesa_set_create(NULL, hash_instr, instrs_equal);
152    bi_index *replacement = calloc(sizeof(bi_index), ctx->ssa_alloc);
153 
154    bi_foreach_block(ctx, block) {
155       _mesa_set_clear(instr_set, NULL);
156 
157       bi_foreach_instr_in_block(block, instr) {
158          /* Rewrite before trying to CSE anything so we converge
159           * locally in one iteration */
160          bi_foreach_ssa_src(instr, s) {
161             if (bi_is_staging_src(instr, s))
162                continue;
163 
164             bi_index repl = replacement[instr->src[s].value];
165             if (!bi_is_null(repl))
166                bi_replace_src(instr, s, repl);
167          }
168 
169          if (!instr_can_cse(instr))
170             continue;
171 
172          bool found;
173          struct set_entry *entry =
174             _mesa_set_search_or_add(instr_set, instr, &found);
175          if (found) {
176             const bi_instr *match = entry->key;
177 
178             bi_foreach_dest(instr, d) {
179                replacement[instr->dest[d].value] = match->dest[d];
180             }
181          }
182       }
183    }
184 
185    free(replacement);
186    _mesa_set_destroy(instr_set, NULL);
187 }
188