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