| 1 |
/* |
| 2 |
* Copyright (c) 2018-2019 Alyssa Rosenzweig |
| 3 |
* Copyright (C) 2019-2020 Collabora, Ltd. |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
#ifndef __MDG_HELPERS_H |
| 8 |
#define __MDG_HELPERS_H |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <string.h> |
| 12 |
#include "util/macros.h" |
| 13 |
#include "midgard.h" |
| 14 |
|
| 15 |
#define OP_IS_LOAD_VARY_F(op) \ |
| 16 |
(op == midgard_op_ld_vary_16 || op == midgard_op_ld_vary_32) |
| 17 |
|
| 18 |
#define OP_IS_PROJECTION(op) \ |
| 19 |
(op == midgard_op_ldst_perspective_div_y || \ |
| 20 |
op == midgard_op_ldst_perspective_div_z || \ |
| 21 |
op == midgard_op_ldst_perspective_div_w) |
| 22 |
|
| 23 |
#define OP_IS_VEC4_ONLY(op) \ |
| 24 |
(OP_IS_PROJECTION(op) || op == midgard_op_ld_cubemap_coords) |
| 25 |
|
| 26 |
#define OP_IS_MOVE(op) \ |
| 27 |
((op >= midgard_alu_op_fmov && op <= midgard_alu_op_fmov_rtp) || \ |
| 28 |
op == midgard_alu_op_imov) |
| 29 |
|
| 30 |
#define OP_IS_UBO_READ(op) \ |
| 31 |
(op >= midgard_op_ld_ubo_u8 && op <= midgard_op_ld_ubo_128_bswap8) |
| 32 |
|
| 33 |
#define OP_IS_CSEL_V(op) \ |
| 34 |
(op == midgard_alu_op_icsel_v || op == midgard_alu_op_fcsel_v) |
| 35 |
|
| 36 |
#define OP_IS_CSEL(op) \ |
| 37 |
(OP_IS_CSEL_V(op) || op == midgard_alu_op_icsel || \ |
| 38 |
op == midgard_alu_op_fcsel) |
| 39 |
|
| 40 |
#define OP_IS_UNSIGNED_CMP(op) \ |
| 41 |
(op == midgard_alu_op_ult || op == midgard_alu_op_ule) |
| 42 |
|
| 43 |
#define OP_IS_INTEGER_CMP(op) \ |
| 44 |
(op == midgard_alu_op_ieq || op == midgard_alu_op_ine || \ |
| 45 |
op == midgard_alu_op_ilt || op == midgard_alu_op_ile || \ |
| 46 |
OP_IS_UNSIGNED_CMP(op)) |
| 47 |
|
| 48 |
#define OP_IS_COMMON_STORE(op) \ |
| 49 |
(op >= midgard_op_st_u8 && op <= midgard_op_st_128_bswap8) |
| 50 |
|
| 51 |
#define OP_IS_IMAGE(op) \ |
| 52 |
((op >= midgard_op_ld_image_32f && op <= midgard_op_ld_image_32i) || \ |
| 53 |
(op >= midgard_op_st_image_32f && op <= midgard_op_st_image_32i) || \ |
| 54 |
op == midgard_op_lea_image) |
| 55 |
|
| 56 |
#define OP_IS_SPECIAL(op) \ |
| 57 |
((op >= midgard_op_ld_special_32f && op <= midgard_op_ld_special_32i) || \ |
| 58 |
(op >= midgard_op_st_special_32f && op <= midgard_op_st_special_32i)) |
| 59 |
|
| 60 |
#define OP_IS_PACK_COLOUR(op) \ |
| 61 |
((op >= midgard_op_pack_colour_f32 && op <= midgard_op_pack_colour_s32)) |
| 62 |
|
| 63 |
#define OP_IS_UNPACK_COLOUR(op) \ |
| 64 |
((op >= midgard_op_unpack_colour_f32 && op <= midgard_op_unpack_colour_s32)) |
| 65 |
|
| 66 |
/* Instructions that are on the load/store unit but don't access memory */ |
| 67 |
#define OP_IS_REG2REG_LDST(op) \ |
| 68 |
(op >= midgard_op_unpack_colour_f32 && \ |
| 69 |
op <= midgard_op_ldst_perspective_div_w) |
| 70 |
|
| 71 |
/* ALU control words are single bit fields with a lot of space */ |
| 72 |
|
| 73 |
#define ALU_ENAB_VEC_MUL (1 << 17) |
| 74 |
#define ALU_ENAB_SCAL_ADD (1 << 19) |
| 75 |
#define ALU_ENAB_VEC_ADD (1 << 21) |
| 76 |
#define ALU_ENAB_SCAL_MUL (1 << 23) |
| 77 |
#define ALU_ENAB_VEC_LUT (1 << 25) |
| 78 |
#define ALU_ENAB_BR_COMPACT (1 << 26) |
| 79 |
#define ALU_ENAB_BRANCH (1 << 27) |
| 80 |
|
| 81 |
/* Other opcode properties that don't conflict with the ALU_ENABs, non-ISA */ |
| 82 |
|
| 83 |
/* Denotes an opcode that takes a vector input with a fixed-number of |
| 84 |
* channels, but outputs to only a single output channel, like dot products. |
| 85 |
* For these, to determine the effective mask, this quirk can be set. We have |
| 86 |
* an intentional off-by-one, since 0-channel makes no sense but we need to fit |
| 87 |
* 4 channels in 2-bits. Similarly, 1-channel doesn't make sense (since then why |
| 88 |
* are we quirked?), so that corresponds to "no count set". |
| 89 |
*/ |
| 90 |
#define OP_CHANNEL_COUNT(c) ((c - 1) << 0) |
| 91 |
#define GET_CHANNEL_COUNT(c) ((c & (0x3 << 0)) ? ((c & (0x3 << 0)) + 1) : 0) |
| 92 |
|
| 93 |
/* For instructions that take a single argument, normally the first argument |
| 94 |
* slot is used for the argument and the second slot is a dummy #0 constant. |
| 95 |
* However, there are exceptions: instructions like fmov store their argument |
| 96 |
* in the _second_ slot and store a dummy r24 in the first slot, designated by |
| 97 |
* QUIRK_FLIPPED_R24 */ |
| 98 |
|
| 99 |
#define QUIRK_FLIPPED_R24 (1 << 2) |
| 100 |
|
| 101 |
/* Is the op commutative? */ |
| 102 |
#define OP_COMMUTES (1 << 3) |
| 103 |
|
| 104 |
/* Does the op convert types between int- and float- space (i2f/f2u/etc) */ |
| 105 |
#define OP_TYPE_CONVERT (1 << 4) |
| 106 |
|
| 107 |
/* Is this opcode the first in a f2x (rte, rtz, rtn, rtp) sequence? If so, |
| 108 |
* takes a roundmode argument in the IR. This has the semantic of rounding the |
| 109 |
* source (it's all fused in), which is why it doesn't necessarily make sense |
| 110 |
* for i2f (though folding there might be necessary for OpenCL reasons). Comes |
| 111 |
* up in format conversion, i.e. f2u_rte */ |
| 112 |
#define MIDGARD_ROUNDS (1 << 5) |
| 113 |
|
| 114 |
/* Vector-independant shorthands for the above; these numbers are arbitrary and |
| 115 |
* not from the ISA. Convert to the above with unit_enum_to_midgard */ |
| 116 |
|
| 117 |
#define UNIT_MUL 0 |
| 118 |
#define UNIT_ADD 1 |
| 119 |
#define UNIT_LUT 2 |
| 120 |
|
| 121 |
#define IS_ALU(tag) (tag >= TAG_ALU_4) |
| 122 |
|
| 123 |
/* Special register aliases */ |
| 124 |
|
| 125 |
#define MAX_WORK_REGISTERS 16 |
| 126 |
|
| 127 |
/* Uniforms are begin at (REGISTER_UNIFORMS - uniform_count) */ |
| 128 |
#define REGISTER_UNIFORMS 24 |
| 129 |
|
| 130 |
/* r24 and r25 are special registers that only exist during the pipeline, |
| 131 |
* by using them when we don't care about the register we skip a roundtrip |
| 132 |
* to the register file. */ |
| 133 |
#define REGISTER_UNUSED 24 |
| 134 |
#define REGISTER_CONSTANT 26 |
| 135 |
#define REGISTER_LDST_BASE 26 |
| 136 |
#define REGISTER_TEXTURE_BASE 28 |
| 137 |
#define REGISTER_SELECT 31 |
| 138 |
|
| 139 |
/* The following registers are read-only */ |
| 140 |
|
| 141 |
/* XY is Program Counter, ZW is Stack Pointer */ |
| 142 |
#define REGISTER_LDST_PC_SP 2 |
| 143 |
|
| 144 |
/* XY is Thread Local Storage pointer, ZW is Workgroup Local Storage pointer */ |
| 145 |
#define REGISTER_LDST_LOCAL_STORAGE_PTR 3 |
| 146 |
|
| 147 |
#define REGISTER_LDST_LOCAL_THREAD_ID 4 |
| 148 |
#define REGISTER_LDST_GROUP_ID 5 |
| 149 |
#define REGISTER_LDST_GLOBAL_THREAD_ID 6 |
| 150 |
|
| 151 |
/* This register is always zeroed when read. */ |
| 152 |
#define REGISTER_LDST_ZERO 7 |
| 153 |
|
| 154 |
/* SSA helper aliases to mimic the registers. */ |
| 155 |
|
| 156 |
#define SSA_FIXED_SHIFT 24 |
| 157 |
#define SSA_FIXED_REGISTER(reg) (((1 + (reg)) << SSA_FIXED_SHIFT) | 1) |
| 158 |
#define SSA_REG_FROM_FIXED(reg) ((((reg) & ~1) >> SSA_FIXED_SHIFT) - 1) |
| 159 |
#define SSA_FIXED_MINIMUM SSA_FIXED_REGISTER(0) |
| 160 |
|
| 161 |
#define COMPONENT_X 0x0 |
| 162 |
#define COMPONENT_Y 0x1 |
| 163 |
#define COMPONENT_Z 0x2 |
| 164 |
#define COMPONENT_W 0x3 |
| 165 |
|
| 166 |
#define SWIZZLE_IDENTITY \ |
| 167 |
{ \ |
| 168 |
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, \ |
| 169 |
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, \ |
| 170 |
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, \ |
| 171 |
{ \ |
| 172 |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 \ |
| 173 |
} \ |
| 174 |
} |
| 175 |
|
| 176 |
#define SWIZZLE_IDENTITY_4 \ |
| 177 |
{ \ |
| 178 |
{0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, \ |
| 179 |
{0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, \ |
| 180 |
{0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, \ |
| 181 |
{0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, \ |
| 182 |
} |
| 183 |
|
| 184 |
static inline unsigned |
| 185 |
mask_of(unsigned nr_comp) |
| 186 |
{ |
| 187 |
return (1 << nr_comp) - 1; |
| 188 |
} |
| 189 |
|
| 190 |
/* See ISA notes */ |
| 191 |
|
| 192 |
#define LDST_NOP (3) |
| 193 |
|
| 194 |
/* There are five ALU units: VMUL, VADD, SMUL, SADD, LUT. A given opcode is |
| 195 |
* implemented on some subset of these units (or occassionally all of them). |
| 196 |
* This table encodes a bit mask of valid units for each opcode, so the |
| 197 |
* scheduler can figure where to plonk the instruction. */ |
| 198 |
|
| 199 |
/* Shorthands for each unit */ |
| 200 |
#define UNIT_VMUL ALU_ENAB_VEC_MUL |
| 201 |
#define UNIT_SADD ALU_ENAB_SCAL_ADD |
| 202 |
#define UNIT_VADD ALU_ENAB_VEC_ADD |
| 203 |
#define UNIT_SMUL ALU_ENAB_SCAL_MUL |
| 204 |
#define UNIT_VLUT ALU_ENAB_VEC_LUT |
| 205 |
|
| 206 |
/* Shorthands for usual combinations of units */ |
| 207 |
|
| 208 |
#define UNITS_MUL (UNIT_VMUL | UNIT_SMUL) |
| 209 |
#define UNITS_ADD (UNIT_VADD | UNIT_SADD) |
| 210 |
#define UNITS_MOST (UNITS_MUL | UNITS_ADD) |
| 211 |
#define UNITS_ALL (UNITS_MOST | UNIT_VLUT) |
| 212 |
#define UNITS_SCALAR (UNIT_SADD | UNIT_SMUL) |
| 213 |
#define UNITS_VECTOR (UNIT_VMUL | UNIT_VADD) |
| 214 |
#define UNITS_ANY_VECTOR (UNITS_VECTOR | UNIT_VLUT) |
| 215 |
|
| 216 |
struct mir_op_props { |
| 217 |
const char *name; |
| 218 |
unsigned props; |
| 219 |
}; |
| 220 |
|
| 221 |
/* For load/store */ |
| 222 |
|
| 223 |
struct mir_ldst_op_props { |
| 224 |
const char *name; |
| 225 |
unsigned props; |
| 226 |
}; |
| 227 |
|
| 228 |
struct mir_tex_op_props { |
| 229 |
const char *name; |
| 230 |
unsigned props; |
| 231 |
}; |
| 232 |
|
| 233 |
struct mir_tag_props { |
| 234 |
const char *name; |
| 235 |
unsigned size; |
| 236 |
}; |
| 237 |
|
| 238 |
/* Lower 2-bits are a midgard_reg_mode */ |
| 239 |
#define GET_LDST_SIZE(c) (c & 3) |
| 240 |
|
| 241 |
/* Store (so the primary register is a source, not a destination */ |
| 242 |
#define LDST_STORE (1 << 2) |
| 243 |
|
| 244 |
/* Mask has special meaning and should not be manipulated directly */ |
| 245 |
#define LDST_SPECIAL_MASK (1 << 3) |
| 246 |
|
| 247 |
/* Non-store operation has side effects and should not be eliminated even if |
| 248 |
* its mask is 0 */ |
| 249 |
#define LDST_SIDE_FX (1 << 4) |
| 250 |
|
| 251 |
/* Computes an address according to indirects/zext/shift/etc */ |
| 252 |
#define LDST_ADDRESS (1 << 5) |
| 253 |
|
| 254 |
/* Some fields such swizzle and address have special meanings */ |
| 255 |
#define LDST_ATOMIC (1 << 6) |
| 256 |
|
| 257 |
/* Operates on attributes/varyings (including images) */ |
| 258 |
#define LDST_ATTRIB (1 << 7) |
| 259 |
|
| 260 |
/* This file is common, so don't define the tables themselves. #include |
| 261 |
* midgard_op.h if you need that, or edit midgard_ops.c directly */ |
| 262 |
|
| 263 |
/* Duplicate bits to convert a per-component to duplicated 8-bit format, |
| 264 |
* which is used for vector units */ |
| 265 |
|
| 266 |
static inline unsigned |
| 267 |
expand_writemask(unsigned mask, unsigned log2_channels) |
| 268 |
{ |
| 269 |
unsigned o = 0; |
| 270 |
unsigned factor = 8 >> log2_channels; |
| 271 |
unsigned expanded = (1 << factor) - 1; |
| 272 |
|
| 273 |
for (unsigned i = 0; i < (1 << log2_channels); ++i) |
| 274 |
if (mask & (1 << i)) |
| 275 |
o |= (expanded << (factor * i)); |
| 276 |
|
| 277 |
return o; |
| 278 |
} |
| 279 |
|
| 280 |
/* Coerce structs to integer */ |
| 281 |
|
| 282 |
static inline unsigned |
| 283 |
vector_alu_srco_unsigned(midgard_vector_alu_src src) |
| 284 |
{ |
| 285 |
unsigned u; |
| 286 |
memcpy(&u, &src, sizeof(src)); |
| 287 |
return u; |
| 288 |
} |
| 289 |
|
| 290 |
static inline midgard_vector_alu_src |
| 291 |
vector_alu_from_unsigned(unsigned u) |
| 292 |
{ |
| 293 |
midgard_vector_alu_src s; |
| 294 |
memcpy(&s, &u, sizeof(s)); |
| 295 |
return s; |
| 296 |
} |
| 297 |
|
| 298 |
static inline void |
| 299 |
mir_compose_swizzle(unsigned *left, unsigned *right, unsigned *final_out) |
| 300 |
{ |
| 301 |
unsigned out[16]; |
| 302 |
|
| 303 |
for (unsigned c = 0; c < 16; ++c) |
| 304 |
out[c] = right[left[c]]; |
| 305 |
|
| 306 |
memcpy(final_out, out, sizeof(out)); |
| 307 |
} |
| 308 |
|
| 309 |
/* Checks for an xyzw.. swizzle, given a mask */ |
| 310 |
|
| 311 |
static inline bool |
| 312 |
mir_is_simple_swizzle(unsigned *swizzle, unsigned mask) |
| 313 |
{ |
| 314 |
for (unsigned i = 0; i < 16; ++i) { |
| 315 |
if (!(mask & (1 << i))) |
| 316 |
continue; |
| 317 |
|
| 318 |
if (swizzle[i] != i) |
| 319 |
return false; |
| 320 |
} |
| 321 |
|
| 322 |
return true; |
| 323 |
} |
| 324 |
|
| 325 |
/* Packs a load/store argument */ |
| 326 |
|
| 327 |
static inline uint8_t |
| 328 |
midgard_ldst_comp(unsigned reg, unsigned component, unsigned size) |
| 329 |
{ |
| 330 |
assert((reg & ~1) == 0); |
| 331 |
assert(size == 16 || size == 32 || size == 64); |
| 332 |
|
| 333 |
/* Shift so everything is in terms of 32-bit units */ |
| 334 |
if (size == 64) { |
| 335 |
assert(component < 2); |
| 336 |
component <<= 1; |
| 337 |
} else if (size == 16) { |
| 338 |
assert((component & 1) == 0); |
| 339 |
component >>= 1; |
| 340 |
} |
| 341 |
|
| 342 |
return component; |
| 343 |
} |
| 344 |
|
| 345 |
/* Packs/unpacks a ubo index immediate. The unpack must be defined here so it |
| 346 |
* can be used with the disassembler, which need not be linked with the main |
| 347 |
* compiler. |
| 348 |
*/ |
| 349 |
|
| 350 |
void midgard_pack_ubo_index_imm(midgard_load_store_word *word, unsigned index); |
| 351 |
|
| 352 |
static inline unsigned |
| 353 |
midgard_unpack_ubo_index_imm(midgard_load_store_word word) |
| 354 |
{ |
| 355 |
unsigned ubo = word.arg_comp | (word.arg_reg << 2) | |
| 356 |
(word.bitsize_toggle << 5) | (word.index_format << 6); |
| 357 |
|
| 358 |
return ubo; |
| 359 |
} |
| 360 |
|
| 361 |
/* Packs/unpacks varying parameters. |
| 362 |
* FIXME: IMPORTANT: We currently handle varying mode weirdly, by passing all |
| 363 |
* parameters via an offset and using REGISTER_LDST_ZERO as base. This works |
| 364 |
* for most parameters, but does not allow us to encode/decode direct sample |
| 365 |
* position. */ |
| 366 |
void midgard_pack_varying_params(midgard_load_store_word *word, |
| 367 |
midgard_varying_params p); |
| 368 |
midgard_varying_params |
| 369 |
midgard_unpack_varying_params(midgard_load_store_word word); |
| 370 |
|
| 371 |
/* Load/store ops' displacement helpers. |
| 372 |
* This is useful because different types of load/store ops have different |
| 373 |
* displacement bitsize. */ |
| 374 |
|
| 375 |
#define UNPACK_LDST_ATTRIB_OFS(a) ((a) >> 9) |
| 376 |
#define UNPACK_LDST_VERTEX_OFS(a) util_sign_extend((a) & 0x1FF, 9) |
| 377 |
#define UNPACK_LDST_SELECTOR_OFS(a) ((a) >> 9) |
| 378 |
#define UNPACK_LDST_UBO_OFS(a) ((a) >> 2) |
| 379 |
#define UNPACK_LDST_MEM_OFS(a) ((a)) |
| 380 |
|
| 381 |
#define PACK_LDST_ATTRIB_OFS(a) ((a) << 9) |
| 382 |
#define PACK_LDST_VERTEX_OFS(a) ((a) & 0x1FF) |
| 383 |
#define PACK_LDST_SELECTOR_OFS(a) ((a) << 9) |
| 384 |
#define PACK_LDST_UBO_OFS(a) ((a) << 2) |
| 385 |
#define PACK_LDST_MEM_OFS(a) ((a)) |
| 386 |
|
| 387 |
static inline bool |
| 388 |
midgard_is_branch_unit(unsigned unit) |
| 389 |
{ |
| 390 |
return (unit == ALU_ENAB_BRANCH) || (unit == ALU_ENAB_BR_COMPACT); |
| 391 |
} |
| 392 |
|
| 393 |
/* Packs ALU mod argument */ |
| 394 |
struct midgard_instruction; |
| 395 |
unsigned mir_pack_mod(const struct midgard_instruction *ins, unsigned i, |
| 396 |
bool scalar); |
| 397 |
|
| 398 |
void mir_print_constant_component(FILE *fp, const midgard_constants *consts, |
| 399 |
unsigned c, midgard_reg_mode reg_mode, |
| 400 |
bool half, unsigned mod, midgard_alu_op op); |
| 401 |
|
| 402 |
void mir_print_outmod(FILE *fp, unsigned outmod, bool is_int); |
| 403 |
|
| 404 |
#endif |