| 1 |
/* |
| 2 |
* Copyright (C) 2020 Collabora Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "bi_builder.h" |
| 7 |
#include "bi_swizzles.h" |
| 8 |
#include "compiler.h" |
| 9 |
#include "valhall.h" |
| 10 |
#include "panfrost/lib/pan_props.h" |
| 11 |
|
| 12 |
/* Not all 8-bit and 16-bit instructions support all swizzles on all sources. |
| 13 |
* These passes, intended to run after NIR->BIR but before scheduling/RA, lower |
| 14 |
* away swizzles that cannot be represented. In the future, we should try to |
| 15 |
* recombine swizzles where we can as an optimization. |
| 16 |
*/ |
| 17 |
|
| 18 |
static uint32_t |
| 19 |
va_op_swizzles(enum bi_opcode op, unsigned src) |
| 20 |
{ |
| 21 |
/* This is a bifrost-only instruction that is lowered on valhall */ |
| 22 |
if (!valhall_opcodes[op].exact) |
| 23 |
return bi_op_swizzles[op][src]; |
| 24 |
|
| 25 |
uint32_t swizzles = 0; |
| 26 |
struct va_src_info info = va_src_info(op, src); |
| 27 |
|
| 28 |
if (info.swizzle) { |
| 29 |
assert(info.size == VA_SIZE_16 || info.size == VA_SIZE_32); |
| 30 |
if (info.size == VA_SIZE_16) |
| 31 |
swizzles |= (1 << BI_SWIZZLE_H00) | (1 << BI_SWIZZLE_H10) | |
| 32 |
(1 << BI_SWIZZLE_H01) | (1 << BI_SWIZZLE_H11); |
| 33 |
else if (info.size == VA_SIZE_32) |
| 34 |
swizzles |= (1 << BI_SWIZZLE_H01) | (1 << BI_SWIZZLE_H0) | |
| 35 |
(1 << BI_SWIZZLE_H1); |
| 36 |
} |
| 37 |
|
| 38 |
if (info.lane) { |
| 39 |
if (info.size == VA_SIZE_8) |
| 40 |
swizzles |= (1 << BI_SWIZZLE_B0) | (1 << BI_SWIZZLE_B1) | |
| 41 |
(1 << BI_SWIZZLE_B2) | (1 << BI_SWIZZLE_B3); |
| 42 |
if (info.size == VA_SIZE_16) |
| 43 |
swizzles |= (1 << BI_SWIZZLE_H0) | (1 << BI_SWIZZLE_H1); |
| 44 |
} |
| 45 |
|
| 46 |
if (info.lanes) { |
| 47 |
assert(info.size == VA_SIZE_8); |
| 48 |
swizzles |= (1 << BI_SWIZZLE_B00) | (1 << BI_SWIZZLE_B11) | |
| 49 |
(1 << BI_SWIZZLE_B22) | (1 << BI_SWIZZLE_B33); |
| 50 |
} |
| 51 |
|
| 52 |
if (info.halfswizzle) { |
| 53 |
assert(info.size == VA_SIZE_8); |
| 54 |
swizzles |= (1 << BI_SWIZZLE_B00) | (1 << BI_SWIZZLE_B10) | |
| 55 |
(1 << BI_SWIZZLE_B20) | (1 << BI_SWIZZLE_B30) | |
| 56 |
(1 << BI_SWIZZLE_B01) | (1 << BI_SWIZZLE_B11) | |
| 57 |
(1 << BI_SWIZZLE_B21) | (1 << BI_SWIZZLE_B31) | |
| 58 |
(1 << BI_SWIZZLE_B02) | (1 << BI_SWIZZLE_B12) | |
| 59 |
(1 << BI_SWIZZLE_B22) | (1 << BI_SWIZZLE_B32) | |
| 60 |
(1 << BI_SWIZZLE_B03) | (1 << BI_SWIZZLE_B13) | |
| 61 |
(1 << BI_SWIZZLE_B23) | (1 << BI_SWIZZLE_B33); |
| 62 |
} |
| 63 |
|
| 64 |
if (info.widen) { |
| 65 |
if (info.size == VA_SIZE_8) |
| 66 |
swizzles |= (1 << BI_SWIZZLE_B0123) | (1 << BI_SWIZZLE_B3210) | |
| 67 |
(1 << BI_SWIZZLE_B0101) | (1 << BI_SWIZZLE_B2323) | |
| 68 |
(1 << BI_SWIZZLE_B0000) | (1 << BI_SWIZZLE_B1111) | |
| 69 |
(1 << BI_SWIZZLE_B2222) | (1 << BI_SWIZZLE_B3333) | |
| 70 |
(1 << BI_SWIZZLE_B2301) | (1 << BI_SWIZZLE_B1032) | |
| 71 |
(1 << BI_SWIZZLE_B0011) | (1 << BI_SWIZZLE_B2233); |
| 72 |
else if (info.size == VA_SIZE_16) |
| 73 |
swizzles |= (1 << BI_SWIZZLE_H00) | (1 << BI_SWIZZLE_H10) | |
| 74 |
(1 << BI_SWIZZLE_H01) | (1 << BI_SWIZZLE_H11) | |
| 75 |
(1 << BI_SWIZZLE_B00) | (1 << BI_SWIZZLE_B11) | |
| 76 |
(1 << BI_SWIZZLE_B22) | (1 << BI_SWIZZLE_B33) | |
| 77 |
(1 << BI_SWIZZLE_B01) | (1 << BI_SWIZZLE_B20) | |
| 78 |
(1 << BI_SWIZZLE_B02) | (1 << BI_SWIZZLE_B31) | |
| 79 |
(1 << BI_SWIZZLE_B13) | (1 << BI_SWIZZLE_B23); |
| 80 |
else if (info.size == VA_SIZE_32) |
| 81 |
swizzles |= (1 << BI_SWIZZLE_H01) | (1 << BI_SWIZZLE_H0) | |
| 82 |
(1 << BI_SWIZZLE_H1) | (1 << BI_SWIZZLE_B0) | |
| 83 |
(1 << BI_SWIZZLE_B1) | (1 << BI_SWIZZLE_B2) | |
| 84 |
(1 << BI_SWIZZLE_B3); |
| 85 |
} |
| 86 |
|
| 87 |
if (info.combine) { |
| 88 |
assert(info.size == VA_SIZE_32); |
| 89 |
swizzles |= (1 << BI_SWIZZLE_H01) | (1 << BI_SWIZZLE_H0) | (1 << BI_SWIZZLE_H1); |
| 90 |
} |
| 91 |
|
| 92 |
if (swizzles == 0) |
| 93 |
swizzles = 1 << BI_SWIZZLE_H01; |
| 94 |
|
| 95 |
return swizzles; |
| 96 |
} |
| 97 |
|
| 98 |
bool |
| 99 |
bi_op_supports_swizzle(enum bi_opcode op, unsigned src, |
| 100 |
enum bi_swizzle swizzle, unsigned arch) |
| 101 |
{ |
| 102 |
uint32_t supported_swizzles = arch >= 9 ? |
| 103 |
va_op_swizzles(op, src) : bi_op_swizzles[op][src]; |
| 104 |
return supported_swizzles & BITFIELD_BIT(swizzle); |
| 105 |
} |
| 106 |
|
| 107 |
static void |
| 108 |
lower_swizzle(bi_context *ctx, bi_instr *ins, unsigned src) |
| 109 |
{ |
| 110 |
/* We don't want to deal with reswizzling logic in modifier prop. Move |
| 111 |
* the swizzle outside, it's easier for clamp propagation. */ |
| 112 |
if (ins->op == BI_OPCODE_FCLAMP_V2F16) { |
| 113 |
bi_builder b = bi_init_builder(ctx, bi_after_instr(ins)); |
| 114 |
bi_index dest = ins->dest[0]; |
| 115 |
bi_index tmp = bi_temp(ctx); |
| 116 |
|
| 117 |
bi_index swizzled_src = bi_replace_index(ins->src[0], tmp); |
| 118 |
ins->src[0].swizzle = BI_SWIZZLE_H01; |
| 119 |
ins->dest[0] = tmp; |
| 120 |
bi_swz_v2i16_to(&b, dest, swizzled_src); |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
if (bi_op_supports_swizzle(ins->op, src, ins->src[src].swizzle, ctx->arch)) |
| 125 |
return; |
| 126 |
|
| 127 |
/* First, try to apply a given swizzle to a constant to clear the |
| 128 |
* runtime swizzle. This is less heavy-handed than ignoring the |
| 129 |
* swizzle for scalar destinations, since it maintains |
| 130 |
* replication of the destination. |
| 131 |
*/ |
| 132 |
if (ins->src[src].type == BI_INDEX_CONSTANT) { |
| 133 |
ins->src[src].value = |
| 134 |
bi_apply_swizzle(ins->src[src].value, ins->src[src].swizzle); |
| 135 |
ins->src[src].swizzle = BI_SWIZZLE_H01; |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
/* Even if the source does not replicate, if the consuming instruction |
| 140 |
* produces a 16-bit scalar, we can ignore the other component. |
| 141 |
*/ |
| 142 |
if (ins->dest[0].swizzle == BI_SWIZZLE_H00 && |
| 143 |
ins->src[src].swizzle == BI_SWIZZLE_H00) { |
| 144 |
ins->src[src].swizzle = BI_SWIZZLE_H01; |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
/* Lower it away */ |
| 149 |
bi_builder b = bi_init_builder(ctx, bi_before_instr(ins)); |
| 150 |
|
| 151 |
bi_index orig = ins->src[src]; |
| 152 |
bi_index stripped = bi_replace_index(bi_null(), orig); |
| 153 |
stripped.swizzle = ins->src[src].swizzle; |
| 154 |
|
| 155 |
bi_index swz = bi_swz_v4i8(&b, stripped); |
| 156 |
|
| 157 |
bi_replace_src(ins, src, swz); |
| 158 |
ins->src[src].swizzle = BI_SWIZZLE_H01; |
| 159 |
} |
| 160 |
|
| 161 |
static bool |
| 162 |
bi_instr_replicates(bi_instr *I, BITSET_WORD *replicates_16) |
| 163 |
{ |
| 164 |
switch (I->op) { |
| 165 |
|
| 166 |
/* Instructions that construct vectors have replicated output if their |
| 167 |
* sources are identical. Check this case first. |
| 168 |
*/ |
| 169 |
case BI_OPCODE_MKVEC_V2I16: |
| 170 |
case BI_OPCODE_V2F32_TO_V2F16: |
| 171 |
return bi_is_value_equiv(I->src[0], I->src[1]); |
| 172 |
|
| 173 |
/* 16-bit transcendentals are defined to output zero in their |
| 174 |
* upper half, so they do not replicate |
| 175 |
*/ |
| 176 |
case BI_OPCODE_FRCP_F16: |
| 177 |
case BI_OPCODE_FRSQ_F16: |
| 178 |
return false; |
| 179 |
|
| 180 |
/* Not sure, be conservative, we don't use these.. */ |
| 181 |
case BI_OPCODE_VN_ASST1_F16: |
| 182 |
case BI_OPCODE_FPCLASS_F16: |
| 183 |
case BI_OPCODE_FPOW_SC_DET_F16: |
| 184 |
return false; |
| 185 |
|
| 186 |
default: |
| 187 |
break; |
| 188 |
} |
| 189 |
|
| 190 |
/* Replication analysis only makes sense for ALU instructions */ |
| 191 |
if (bi_get_opcode_props(I)->message != BIFROST_MESSAGE_NONE) |
| 192 |
return false; |
| 193 |
|
| 194 |
/* We only analyze 16-bit instructions for 16-bit replication. We could |
| 195 |
* maybe do better. |
| 196 |
*/ |
| 197 |
if (bi_get_opcode_props(I)->size != BI_SIZE_16) |
| 198 |
return false; |
| 199 |
|
| 200 |
bi_foreach_src(I, s) { |
| 201 |
if (bi_is_null(I->src[s])) |
| 202 |
continue; |
| 203 |
|
| 204 |
/* Replicated swizzles */ |
| 205 |
if (bi_swizzle_replicates_16(I->src[s].swizzle)) |
| 206 |
continue; |
| 207 |
|
| 208 |
/* Replicated values */ |
| 209 |
if (bi_is_ssa(I->src[s]) && BITSET_TEST(replicates_16, I->src[s].value)) |
| 210 |
continue; |
| 211 |
|
| 212 |
/* Replicated constants */ |
| 213 |
if (I->src[s].type == BI_INDEX_CONSTANT && |
| 214 |
(I->src[s].value & 0xFFFF) == (I->src[s].value >> 16)) |
| 215 |
continue; |
| 216 |
|
| 217 |
return false; |
| 218 |
} |
| 219 |
|
| 220 |
return true; |
| 221 |
} |
| 222 |
|
| 223 |
void |
| 224 |
bi_lower_swizzle(bi_context *ctx) |
| 225 |
{ |
| 226 |
bi_foreach_instr_global_safe(ctx, ins) { |
| 227 |
/* bi_lower_mkvec_swz takes care of these and can handle any swizzle */ |
| 228 |
if (ins->op == BI_OPCODE_MKVEC_V2I16 || |
| 229 |
ins->op == BI_OPCODE_MKVEC_V4I8 || |
| 230 |
ins->op == BI_OPCODE_SWZ_V2I16 || |
| 231 |
ins->op == BI_OPCODE_SWZ_V4I8) |
| 232 |
continue; |
| 233 |
|
| 234 |
bi_foreach_src(ins, s) { |
| 235 |
if (bi_is_null(ins->src[s])) |
| 236 |
continue; |
| 237 |
if (ins->src[s].swizzle == BI_SWIZZLE_H01) |
| 238 |
continue; |
| 239 |
|
| 240 |
lower_swizzle(ctx, ins, s); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
/* Now that we've lowered swizzles, clean up the mess */ |
| 245 |
BITSET_WORD *replicates_16 = calloc(sizeof(bi_index), ctx->ssa_alloc); |
| 246 |
|
| 247 |
bi_foreach_instr_global(ctx, ins) { |
| 248 |
if (ins->nr_dests && bi_instr_replicates(ins, replicates_16)) |
| 249 |
BITSET_SET(replicates_16, ins->dest[0].value); |
| 250 |
|
| 251 |
if (ins->op == BI_OPCODE_SWZ_V2I16 && bi_is_ssa(ins->src[0]) && |
| 252 |
BITSET_TEST(replicates_16, ins->src[0].value)) { |
| 253 |
bi_set_opcode(ins, BI_OPCODE_MOV_I32); |
| 254 |
ins->src[0].swizzle = BI_SWIZZLE_H01; |
| 255 |
} |
| 256 |
|
| 257 |
/* On Valhall, if the instruction does some conversion depending on |
| 258 |
* swizzle, we should not touch it. */ |
| 259 |
if (ctx->arch >= 9 && va_op_dest_modifier_does_convert(ins->op)) |
| 260 |
continue; |
| 261 |
|
| 262 |
/* The above passes rely on replicating destinations. For |
| 263 |
* Valhall, we will want to optimize this. For now, default |
| 264 |
* to Bifrost compatible behaviour. |
| 265 |
*/ |
| 266 |
if (ins->nr_dests) |
| 267 |
ins->dest[0].swizzle = BI_SWIZZLE_H01; |
| 268 |
} |
| 269 |
|
| 270 |
free(replicates_16); |
| 271 |
} |