| 1 |
/* |
| 2 |
* Copyright (C) 2019 Collabora, Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "compiler.h" |
| 7 |
|
| 8 |
/* Midgard's generic load/store instructions, particularly to implement SSBOs |
| 9 |
* and globals, have support for address arithmetic natively. In particularly, |
| 10 |
* they take two indirect arguments A, B and two immediates #s, #c, calculating |
| 11 |
* the address: |
| 12 |
* |
| 13 |
* A + (zext?(B) << #s) + #c |
| 14 |
* |
| 15 |
* This allows for fast indexing into arrays. This file tries to pattern match |
| 16 |
* the offset in NIR with this form to reduce pressure on the ALU pipe. |
| 17 |
*/ |
| 18 |
|
| 19 |
struct mir_address { |
| 20 |
nir_scalar A; |
| 21 |
nir_scalar B; |
| 22 |
|
| 23 |
midgard_index_address_format type; |
| 24 |
unsigned shift; |
| 25 |
unsigned bias; |
| 26 |
}; |
| 27 |
|
| 28 |
static bool |
| 29 |
mir_args_ssa(nir_scalar s, unsigned count) |
| 30 |
{ |
| 31 |
nir_alu_instr *alu = nir_def_as_alu(s.def); |
| 32 |
|
| 33 |
if (count > nir_op_infos[alu->op].num_inputs) |
| 34 |
return false; |
| 35 |
|
| 36 |
return true; |
| 37 |
} |
| 38 |
|
| 39 |
/* Matches a constant in either slot and moves it to the bias */ |
| 40 |
|
| 41 |
static void |
| 42 |
mir_match_constant(struct mir_address *address) |
| 43 |
{ |
| 44 |
if (address->A.def && nir_scalar_is_const(address->A)) { |
| 45 |
address->bias += nir_scalar_as_uint(address->A); |
| 46 |
address->A.def = NULL; |
| 47 |
} |
| 48 |
|
| 49 |
if (address->B.def && nir_scalar_is_const(address->B)) { |
| 50 |
address->bias += nir_scalar_as_uint(address->B); |
| 51 |
address->B.def = NULL; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/* Matches an iadd when there is a free slot or constant */ |
| 56 |
|
| 57 |
/* The offset field is a 18-bit signed integer */ |
| 58 |
#define MAX_POSITIVE_OFFSET ((1 << 17) - 1) |
| 59 |
|
| 60 |
static void |
| 61 |
mir_match_iadd(struct mir_address *address, bool first_free) |
| 62 |
{ |
| 63 |
if (!address->B.def || !nir_scalar_is_alu(address->B)) |
| 64 |
return; |
| 65 |
|
| 66 |
if (!mir_args_ssa(address->B, 2)) |
| 67 |
return; |
| 68 |
|
| 69 |
nir_op op = nir_scalar_alu_op(address->B); |
| 70 |
|
| 71 |
if (op != nir_op_iadd) |
| 72 |
return; |
| 73 |
|
| 74 |
nir_scalar op1 = nir_scalar_chase_alu_src(address->B, 0); |
| 75 |
nir_scalar op2 = nir_scalar_chase_alu_src(address->B, 1); |
| 76 |
|
| 77 |
if (nir_scalar_is_const(op1) && |
| 78 |
nir_scalar_as_uint(op1) <= MAX_POSITIVE_OFFSET) { |
| 79 |
address->bias += nir_scalar_as_uint(op1); |
| 80 |
address->B = op2; |
| 81 |
} else if (nir_scalar_is_const(op2) && |
| 82 |
nir_scalar_as_uint(op2) <= MAX_POSITIVE_OFFSET) { |
| 83 |
address->bias += nir_scalar_as_uint(op2); |
| 84 |
address->B = op1; |
| 85 |
} else if (!nir_scalar_is_const(op1) && !nir_scalar_is_const(op2) && |
| 86 |
first_free && !address->A.def) { |
| 87 |
address->A = op1; |
| 88 |
address->B = op2; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/* Matches u2u64 and sets type */ |
| 93 |
|
| 94 |
static void |
| 95 |
mir_match_u2u64(struct mir_address *address) |
| 96 |
{ |
| 97 |
if (!address->B.def || !nir_scalar_is_alu(address->B)) |
| 98 |
return; |
| 99 |
|
| 100 |
if (!mir_args_ssa(address->B, 1)) |
| 101 |
return; |
| 102 |
|
| 103 |
nir_op op = nir_scalar_alu_op(address->B); |
| 104 |
if (op != nir_op_u2u64) |
| 105 |
return; |
| 106 |
nir_scalar arg = nir_scalar_chase_alu_src(address->B, 0); |
| 107 |
|
| 108 |
address->B = arg; |
| 109 |
address->type = midgard_index_address_u32; |
| 110 |
} |
| 111 |
|
| 112 |
/* Matches i2i64 and sets type */ |
| 113 |
|
| 114 |
static void |
| 115 |
mir_match_i2i64(struct mir_address *address) |
| 116 |
{ |
| 117 |
if (!address->B.def || !nir_scalar_is_alu(address->B)) |
| 118 |
return; |
| 119 |
|
| 120 |
if (!mir_args_ssa(address->B, 1)) |
| 121 |
return; |
| 122 |
|
| 123 |
nir_op op = nir_scalar_alu_op(address->B); |
| 124 |
if (op != nir_op_i2i64) |
| 125 |
return; |
| 126 |
nir_scalar arg = nir_scalar_chase_alu_src(address->B, 0); |
| 127 |
|
| 128 |
address->B = arg; |
| 129 |
address->type = midgard_index_address_s32; |
| 130 |
} |
| 131 |
|
| 132 |
/* Matches ishl to shift */ |
| 133 |
|
| 134 |
static void |
| 135 |
mir_match_ishl(struct mir_address *address) |
| 136 |
{ |
| 137 |
if (!address->B.def || !nir_scalar_is_alu(address->B)) |
| 138 |
return; |
| 139 |
|
| 140 |
if (!mir_args_ssa(address->B, 2)) |
| 141 |
return; |
| 142 |
|
| 143 |
nir_op op = nir_scalar_alu_op(address->B); |
| 144 |
if (op != nir_op_ishl) |
| 145 |
return; |
| 146 |
nir_scalar op1 = nir_scalar_chase_alu_src(address->B, 0); |
| 147 |
nir_scalar op2 = nir_scalar_chase_alu_src(address->B, 1); |
| 148 |
|
| 149 |
if (!nir_scalar_is_const(op2)) |
| 150 |
return; |
| 151 |
|
| 152 |
unsigned shift = nir_scalar_as_uint(op2); |
| 153 |
if (shift > 0x7) |
| 154 |
return; |
| 155 |
|
| 156 |
address->B = op1; |
| 157 |
address->shift = shift; |
| 158 |
} |
| 159 |
|
| 160 |
/* Strings through mov which can happen from NIR vectorization */ |
| 161 |
|
| 162 |
static void |
| 163 |
mir_match_mov(struct mir_address *address) |
| 164 |
{ |
| 165 |
if (address->A.def && nir_scalar_is_alu(address->A)) { |
| 166 |
nir_op op = nir_scalar_alu_op(address->A); |
| 167 |
|
| 168 |
if (op == nir_op_mov && mir_args_ssa(address->A, 1)) |
| 169 |
address->A = nir_scalar_chase_alu_src(address->A, 0); |
| 170 |
} |
| 171 |
|
| 172 |
if (address->B.def && nir_scalar_is_alu(address->B)) { |
| 173 |
nir_op op = nir_scalar_alu_op(address->B); |
| 174 |
|
| 175 |
if (op == nir_op_mov && mir_args_ssa(address->B, 1)) |
| 176 |
address->B = nir_scalar_chase_alu_src(address->B, 0); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/* Tries to pattern match into mir_address */ |
| 181 |
|
| 182 |
static struct mir_address |
| 183 |
mir_match_offset(nir_def *offset, bool first_free, bool extend) |
| 184 |
{ |
| 185 |
struct mir_address address = { |
| 186 |
.B = {.def = offset}, |
| 187 |
.type = extend ? midgard_index_address_u64 : midgard_index_address_u32, |
| 188 |
}; |
| 189 |
|
| 190 |
mir_match_mov(&address); |
| 191 |
mir_match_constant(&address); |
| 192 |
mir_match_mov(&address); |
| 193 |
mir_match_iadd(&address, first_free); |
| 194 |
mir_match_mov(&address); |
| 195 |
|
| 196 |
if (extend) { |
| 197 |
mir_match_u2u64(&address); |
| 198 |
mir_match_i2i64(&address); |
| 199 |
mir_match_mov(&address); |
| 200 |
} |
| 201 |
|
| 202 |
mir_match_ishl(&address); |
| 203 |
|
| 204 |
return address; |
| 205 |
} |
| 206 |
|
| 207 |
void |
| 208 |
mir_set_offset(compiler_context *ctx, midgard_instruction *ins, nir_src *offset, |
| 209 |
unsigned seg) |
| 210 |
{ |
| 211 |
for (unsigned i = 0; i < 16; ++i) { |
| 212 |
ins->swizzle[1][i] = 0; |
| 213 |
ins->swizzle[2][i] = 0; |
| 214 |
} |
| 215 |
|
| 216 |
/* Sign extend instead of zero extend in case the address is something |
| 217 |
* like `base + offset + 20`, where offset could be negative. */ |
| 218 |
bool force_sext = (nir_src_bit_size(*offset) < 64); |
| 219 |
bool first_free = (seg == LDST_GLOBAL); |
| 220 |
|
| 221 |
struct mir_address match = mir_match_offset(offset->ssa, first_free, true); |
| 222 |
|
| 223 |
if (match.A.def) { |
| 224 |
unsigned bitsize = match.A.def->bit_size; |
| 225 |
assert(bitsize == 32 || bitsize == 64); |
| 226 |
|
| 227 |
ins->src[1] = nir_ssa_index(match.A.def); |
| 228 |
ins->swizzle[1][0] = match.A.comp; |
| 229 |
ins->src_types[1] = nir_type_uint | bitsize; |
| 230 |
ins->load_store.bitsize_toggle = (bitsize == 64); |
| 231 |
} else { |
| 232 |
ins->load_store.bitsize_toggle = true; |
| 233 |
ins->load_store.arg_comp = seg & 0x3; |
| 234 |
ins->load_store.arg_reg = (seg >> 2) & 0x7; |
| 235 |
} |
| 236 |
|
| 237 |
if (match.B.def) { |
| 238 |
ins->src[2] = nir_ssa_index(match.B.def); |
| 239 |
ins->swizzle[2][0] = match.B.comp; |
| 240 |
ins->src_types[2] = nir_type_uint | match.B.def->bit_size; |
| 241 |
} else |
| 242 |
ins->load_store.index_reg = REGISTER_LDST_ZERO; |
| 243 |
|
| 244 |
if (force_sext) |
| 245 |
match.type = midgard_index_address_s32; |
| 246 |
|
| 247 |
ins->load_store.index_format = match.type; |
| 248 |
|
| 249 |
assert(match.shift <= 7); |
| 250 |
ins->load_store.index_shift = match.shift; |
| 251 |
|
| 252 |
ins->constants.u32[0] = match.bias; |
| 253 |
} |
| 254 |
|
| 255 |
void |
| 256 |
mir_set_ubo_offset(midgard_instruction *ins, nir_src *src, unsigned bias) |
| 257 |
{ |
| 258 |
struct mir_address match = mir_match_offset(src->ssa, false, false); |
| 259 |
|
| 260 |
if (match.B.def) { |
| 261 |
ins->src[2] = nir_ssa_index(match.B.def); |
| 262 |
|
| 263 |
for (unsigned i = 0; i < ARRAY_SIZE(ins->swizzle[2]); ++i) |
| 264 |
ins->swizzle[2][i] = match.B.comp; |
| 265 |
} |
| 266 |
|
| 267 |
ins->load_store.index_shift = match.shift; |
| 268 |
ins->constants.u32[0] = match.bias + bias; |
| 269 |
} |