| 1 |
/* |
| 2 |
* Copyright 2021 Alyssa Rosenzweig |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "agx_compiler.h" |
| 7 |
#include "agx_opcodes.h" |
| 8 |
|
| 9 |
/* Binary patches needed for branch offsets */ |
| 10 |
struct agx_branch_fixup { |
| 11 |
/* Offset into the binary to patch */ |
| 12 |
off_t offset; |
| 13 |
|
| 14 |
/* Value to patch with will be block->offset */ |
| 15 |
agx_block *block; |
| 16 |
|
| 17 |
/* If true, skips to the last instruction of the target block */ |
| 18 |
bool skip_to_end; |
| 19 |
}; |
| 20 |
|
| 21 |
static void |
| 22 |
pack_assert_internal(const agx_instr *I, bool condition, const char *msg) |
| 23 |
{ |
| 24 |
if (!condition) { |
| 25 |
fprintf(stderr, "Packing assertion failed for instruction:\n\n"); |
| 26 |
agx_print_instr(I, stderr); |
| 27 |
fprintf(stderr, "\n%s\n", msg); |
| 28 |
abort(); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
#define pack_assert_msg(I, cond, msg) \ |
| 33 |
pack_assert_internal(I, cond, msg ": " #cond) |
| 34 |
|
| 35 |
#define pack_assert(I, cond) pack_assert_internal(I, cond, #cond) |
| 36 |
|
| 37 |
static void |
| 38 |
assert_register_is_aligned(const agx_instr *I, agx_index reg) |
| 39 |
{ |
| 40 |
pack_assert_msg(I, reg.type == AGX_INDEX_REGISTER, "expecting a register"); |
| 41 |
|
| 42 |
switch (reg.size) { |
| 43 |
case AGX_SIZE_16: |
| 44 |
return; |
| 45 |
case AGX_SIZE_32: |
| 46 |
pack_assert_msg(I, (reg.value & 1) == 0, "unaligned reg"); |
| 47 |
return; |
| 48 |
case AGX_SIZE_64: |
| 49 |
pack_assert_msg(I, (reg.value & 3) == 0, "unaligned reg"); |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
UNREACHABLE("Invalid register size"); |
| 54 |
} |
| 55 |
|
| 56 |
/* Texturing has its own operands */ |
| 57 |
static unsigned |
| 58 |
agx_pack_sample_coords(const agx_instr *I, agx_index index, bool *flag, |
| 59 |
bool *is_16) |
| 60 |
{ |
| 61 |
pack_assert_msg(I, index.size <= AGX_SIZE_32, "32-bit coordinates"); |
| 62 |
pack_assert_msg(I, index.value < 0x100, "coordinate register bound"); |
| 63 |
|
| 64 |
*is_16 = index.size == AGX_SIZE_16; |
| 65 |
*flag = index.discard; |
| 66 |
return index.value; |
| 67 |
} |
| 68 |
|
| 69 |
static unsigned |
| 70 |
agx_pack_texture(const agx_instr *I, agx_index base, agx_index index, |
| 71 |
unsigned *packed_base, unsigned *flag) |
| 72 |
{ |
| 73 |
if (base.type == AGX_INDEX_IMMEDIATE) { |
| 74 |
pack_assert(I, base.value == 0); |
| 75 |
|
| 76 |
/* Texture state registers */ |
| 77 |
*packed_base = 0; |
| 78 |
|
| 79 |
if (index.type == AGX_INDEX_REGISTER) { |
| 80 |
pack_assert(I, index.size == AGX_SIZE_16); |
| 81 |
*flag = 1; |
| 82 |
} else { |
| 83 |
pack_assert(I, index.type == AGX_INDEX_IMMEDIATE); |
| 84 |
*flag = 0; |
| 85 |
} |
| 86 |
} else { |
| 87 |
pack_assert(I, base.type == AGX_INDEX_UNIFORM); |
| 88 |
pack_assert(I, base.size == AGX_SIZE_64); |
| 89 |
pack_assert(I, (base.value & 3) == 0); |
| 90 |
pack_assert(I, index.size <= AGX_SIZE_32); |
| 91 |
|
| 92 |
/* Bindless */ |
| 93 |
*packed_base = base.value >> 2; |
| 94 |
*flag = 3; |
| 95 |
} |
| 96 |
|
| 97 |
return index.value; |
| 98 |
} |
| 99 |
|
| 100 |
static unsigned |
| 101 |
agx_pack_sampler(const agx_instr *I, agx_index index, bool *flag) |
| 102 |
{ |
| 103 |
if (index.type == AGX_INDEX_REGISTER) { |
| 104 |
pack_assert(I, index.size == AGX_SIZE_16); |
| 105 |
*flag = 1; |
| 106 |
} else { |
| 107 |
pack_assert(I, index.type == AGX_INDEX_IMMEDIATE); |
| 108 |
*flag = 0; |
| 109 |
} |
| 110 |
|
| 111 |
return index.value; |
| 112 |
} |
| 113 |
|
| 114 |
static unsigned |
| 115 |
agx_pack_sample_compare_offset(const agx_instr *I, agx_index index) |
| 116 |
{ |
| 117 |
if (index.type == AGX_INDEX_NULL) |
| 118 |
return 0; |
| 119 |
|
| 120 |
pack_assert(I, index.size == AGX_SIZE_32); |
| 121 |
pack_assert(I, index.value < 0x100); |
| 122 |
assert_register_is_aligned(I, index); |
| 123 |
return index.value; |
| 124 |
} |
| 125 |
|
| 126 |
static unsigned |
| 127 |
agx_pack_lod(const agx_instr *I, agx_index index, unsigned *lod_mode) |
| 128 |
{ |
| 129 |
/* For automatic LOD, the LOD field is unused. Assert as much. */ |
| 130 |
if ((*lod_mode) == AGX_LOD_MODE_AUTO_LOD) { |
| 131 |
pack_assert(I, index.type == AGX_INDEX_IMMEDIATE); |
| 132 |
pack_assert(I, index.value == 0); |
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
if (index.type == AGX_INDEX_UNIFORM) { |
| 137 |
/* Translate LOD mode from register mode to uniform mode */ |
| 138 |
pack_assert(I, |
| 139 |
((*lod_mode) & BITFIELD_BIT(2)) && "must start as reg mode"); |
| 140 |
*lod_mode = (*lod_mode) & ~BITFIELD_BIT(2); |
| 141 |
pack_assert(I, index.value < 0x200); |
| 142 |
} else { |
| 143 |
/* Otherwise must be registers */ |
| 144 |
pack_assert(I, index.type == AGX_INDEX_REGISTER); |
| 145 |
pack_assert(I, index.value < 0x100); |
| 146 |
} |
| 147 |
|
| 148 |
return index.value; |
| 149 |
} |
| 150 |
|
| 151 |
static unsigned |
| 152 |
agx_pack_pbe_source(const agx_instr *I, agx_index index, bool *flag) |
| 153 |
{ |
| 154 |
pack_assert(I, index.size == AGX_SIZE_16 || index.size == AGX_SIZE_32); |
| 155 |
assert_register_is_aligned(I, index); |
| 156 |
|
| 157 |
*flag = (index.size == AGX_SIZE_32); |
| 158 |
return index.value; |
| 159 |
} |
| 160 |
|
| 161 |
static unsigned |
| 162 |
agx_pack_pbe_lod(const agx_instr *I, agx_index index, bool *flag) |
| 163 |
{ |
| 164 |
pack_assert(I, index.size == AGX_SIZE_16); |
| 165 |
|
| 166 |
if (index.type == AGX_INDEX_IMMEDIATE) |
| 167 |
*flag = true; |
| 168 |
else if (index.type == AGX_INDEX_REGISTER) |
| 169 |
*flag = false; |
| 170 |
else |
| 171 |
UNREACHABLE("Invalid PBE LOD type"); |
| 172 |
|
| 173 |
return index.value; |
| 174 |
} |
| 175 |
|
| 176 |
/* Load/stores have their own operands */ |
| 177 |
|
| 178 |
static unsigned |
| 179 |
agx_pack_memory_reg(const agx_instr *I, agx_index index, bool *flag) |
| 180 |
{ |
| 181 |
assert_register_is_aligned(I, index); |
| 182 |
|
| 183 |
*flag = (index.size >= AGX_SIZE_32); |
| 184 |
return index.value; |
| 185 |
} |
| 186 |
|
| 187 |
static unsigned |
| 188 |
agx_pack_memory_base(const agx_instr *I, agx_index index, bool *flag) |
| 189 |
{ |
| 190 |
pack_assert(I, index.size == AGX_SIZE_64); |
| 191 |
pack_assert(I, (index.value & 1) == 0); |
| 192 |
|
| 193 |
/* Can't seem to access high uniforms from memory instructions */ |
| 194 |
pack_assert(I, index.value < 0x100); |
| 195 |
|
| 196 |
if (index.type == AGX_INDEX_UNIFORM) { |
| 197 |
*flag = 1; |
| 198 |
} else { |
| 199 |
pack_assert(I, index.type == AGX_INDEX_REGISTER); |
| 200 |
*flag = 0; |
| 201 |
} |
| 202 |
|
| 203 |
return index.value; |
| 204 |
} |
| 205 |
|
| 206 |
static unsigned |
| 207 |
agx_pack_memory_index(const agx_instr *I, agx_index index, bool *flag) |
| 208 |
{ |
| 209 |
if (index.type == AGX_INDEX_IMMEDIATE) { |
| 210 |
pack_assert(I, index.value < 0x10000); |
| 211 |
*flag = 1; |
| 212 |
|
| 213 |
return index.value; |
| 214 |
} else { |
| 215 |
pack_assert(I, index.type == AGX_INDEX_REGISTER); |
| 216 |
pack_assert(I, index.size == AGX_SIZE_32); |
| 217 |
pack_assert(I, (index.value & 1) == 0); |
| 218 |
pack_assert(I, index.value < 0x100); |
| 219 |
|
| 220 |
*flag = 0; |
| 221 |
return index.value; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
static uint16_t |
| 226 |
agx_pack_local_base(const agx_instr *I, agx_index index, unsigned *flags) |
| 227 |
{ |
| 228 |
pack_assert(I, index.size == AGX_SIZE_16); |
| 229 |
|
| 230 |
if (index.type == AGX_INDEX_IMMEDIATE) { |
| 231 |
pack_assert(I, index.value == 0); |
| 232 |
*flags = 2; |
| 233 |
return 0; |
| 234 |
} else if (index.type == AGX_INDEX_UNIFORM) { |
| 235 |
*flags = 1 | ((index.value >> 8) << 1); |
| 236 |
return index.value & BITFIELD_MASK(8); |
| 237 |
} else { |
| 238 |
assert_register_is_aligned(I, index); |
| 239 |
*flags = 0; |
| 240 |
return index.value; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
static uint16_t |
| 245 |
agx_pack_local_index(const agx_instr *I, agx_index index, bool *flag) |
| 246 |
{ |
| 247 |
pack_assert(I, index.size == AGX_SIZE_16); |
| 248 |
|
| 249 |
if (index.type == AGX_INDEX_IMMEDIATE) { |
| 250 |
pack_assert(I, index.value < 0x10000); |
| 251 |
*flag = 1; |
| 252 |
return index.value; |
| 253 |
} else { |
| 254 |
assert_register_is_aligned(I, index); |
| 255 |
*flag = 0; |
| 256 |
return index.value; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
static unsigned |
| 261 |
agx_pack_atomic_source(const agx_instr *I, agx_index index) |
| 262 |
{ |
| 263 |
pack_assert_msg(I, index.size == AGX_SIZE_32, "no 64-bit atomics yet"); |
| 264 |
assert_register_is_aligned(I, index); |
| 265 |
return index.value; |
| 266 |
} |
| 267 |
|
| 268 |
static unsigned |
| 269 |
agx_pack_atomic_dest(const agx_instr *I, agx_index index, bool *flag) |
| 270 |
{ |
| 271 |
/* Atomic destinstions are optional (e.g. for update with no return) */ |
| 272 |
if (index.type == AGX_INDEX_NULL) { |
| 273 |
*flag = 0; |
| 274 |
return 0; |
| 275 |
} |
| 276 |
|
| 277 |
/* But are otherwise registers */ |
| 278 |
pack_assert_msg(I, index.size == AGX_SIZE_32, "no 64-bit atomics yet"); |
| 279 |
assert_register_is_aligned(I, index); |
| 280 |
*flag = 1; |
| 281 |
return index.value; |
| 282 |
} |
| 283 |
|
| 284 |
/* ALU goes through a common path */ |
| 285 |
|
| 286 |
static unsigned |
| 287 |
agx_pack_alu_dst(const agx_instr *I, agx_index dest) |
| 288 |
{ |
| 289 |
assert_register_is_aligned(I, dest); |
| 290 |
unsigned reg = dest.value; |
| 291 |
enum agx_size size = dest.size; |
| 292 |
pack_assert(I, reg < 0x100); |
| 293 |
|
| 294 |
return (dest.cache ? (1 << 0) : 0) | ((size >= AGX_SIZE_32) ? (1 << 1) : 0) | |
| 295 |
((size == AGX_SIZE_64) ? (1 << 2) : 0) | ((reg << 2)); |
| 296 |
} |
| 297 |
|
| 298 |
static unsigned |
| 299 |
agx_pack_alu_src(const agx_instr *I, agx_index src) |
| 300 |
{ |
| 301 |
unsigned value = src.value; |
| 302 |
enum agx_size size = src.size; |
| 303 |
|
| 304 |
if (src.type == AGX_INDEX_IMMEDIATE) { |
| 305 |
/* Flags 0 for an 8-bit immediate */ |
| 306 |
pack_assert(I, value < 0x100); |
| 307 |
|
| 308 |
return (value & BITFIELD_MASK(6)) | ((value >> 6) << 10); |
| 309 |
} else if (src.type == AGX_INDEX_UNIFORM) { |
| 310 |
pack_assert(I, size == AGX_SIZE_16 || size == AGX_SIZE_32); |
| 311 |
pack_assert(I, value < AGX_NUM_UNIFORMS); |
| 312 |
|
| 313 |
return (value & BITFIELD_MASK(6)) | |
| 314 |
((value & BITFIELD_BIT(8)) ? (1 << 6) : 0) | |
| 315 |
((size == AGX_SIZE_32) ? (1 << 7) : 0) | (0x1 << 8) | |
| 316 |
(((value >> 6) & BITFIELD_MASK(2)) << 10); |
| 317 |
} else { |
| 318 |
assert_register_is_aligned(I, src); |
| 319 |
pack_assert(I, !(src.cache && src.discard)); |
| 320 |
|
| 321 |
unsigned hint = src.discard ? 0x3 : src.cache ? 0x2 : 0x1; |
| 322 |
unsigned size_flag = (size == AGX_SIZE_64) ? 0x3 |
| 323 |
: (size == AGX_SIZE_32) ? 0x2 |
| 324 |
: (size == AGX_SIZE_16) ? 0x0 |
| 325 |
: 0x0; |
| 326 |
|
| 327 |
return (value & BITFIELD_MASK(6)) | (hint << 6) | (size_flag << 8) | |
| 328 |
(((value >> 6) & BITFIELD_MASK(2)) << 10); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
static unsigned |
| 333 |
agx_pack_cmpsel_src(const agx_instr *I, agx_index src, enum agx_size dest_size) |
| 334 |
{ |
| 335 |
unsigned value = src.value; |
| 336 |
ASSERTED enum agx_size size = src.size; |
| 337 |
|
| 338 |
if (src.type == AGX_INDEX_IMMEDIATE) { |
| 339 |
/* Flags 0x4 for an 8-bit immediate */ |
| 340 |
pack_assert(I, value < 0x100); |
| 341 |
|
| 342 |
return (value & BITFIELD_MASK(6)) | (0x4 << 6) | ((value >> 6) << 10); |
| 343 |
} else if (src.type == AGX_INDEX_UNIFORM) { |
| 344 |
pack_assert(I, size == AGX_SIZE_16 || size == AGX_SIZE_32); |
| 345 |
pack_assert(I, size == dest_size); |
| 346 |
pack_assert(I, value < 0x200); |
| 347 |
|
| 348 |
return (value & BITFIELD_MASK(6)) | ((value >> 8) << 6) | (0x3 << 7) | |
| 349 |
(((value >> 6) & BITFIELD_MASK(2)) << 10); |
| 350 |
} else { |
| 351 |
pack_assert(I, src.type == AGX_INDEX_REGISTER); |
| 352 |
pack_assert(I, !(src.cache && src.discard)); |
| 353 |
pack_assert(I, size == AGX_SIZE_16 || size == AGX_SIZE_32); |
| 354 |
pack_assert(I, size == dest_size); |
| 355 |
assert_register_is_aligned(I, src); |
| 356 |
|
| 357 |
unsigned hint = src.discard ? 0x3 : src.cache ? 0x2 : 0x1; |
| 358 |
|
| 359 |
return (value & BITFIELD_MASK(6)) | (hint << 6) | |
| 360 |
(((value >> 6) & BITFIELD_MASK(2)) << 10); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
static unsigned |
| 365 |
agx_pack_sample_mask_src(const agx_instr *I, agx_index src) |
| 366 |
{ |
| 367 |
unsigned value = src.value; |
| 368 |
unsigned packed_value = |
| 369 |
(value & BITFIELD_MASK(6)) | (((value >> 6) & BITFIELD_MASK(2)) << 8); |
| 370 |
|
| 371 |
if (src.type == AGX_INDEX_IMMEDIATE) { |
| 372 |
pack_assert(I, value < 0x100); |
| 373 |
return packed_value | (1 << 7); |
| 374 |
} else { |
| 375 |
pack_assert(I, src.type == AGX_INDEX_REGISTER); |
| 376 |
assert_register_is_aligned(I, src); |
| 377 |
pack_assert(I, !(src.cache && src.discard)); |
| 378 |
|
| 379 |
return packed_value; |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
static unsigned |
| 384 |
agx_pack_float_mod(agx_index src) |
| 385 |
{ |
| 386 |
return (src.abs ? (1 << 0) : 0) | (src.neg ? (1 << 1) : 0); |
| 387 |
} |
| 388 |
|
| 389 |
static bool |
| 390 |
agx_all_16(agx_instr *I) |
| 391 |
{ |
| 392 |
agx_foreach_dest(I, d) { |
| 393 |
if (!agx_is_null(I->dest[d]) && I->dest[d].size != AGX_SIZE_16) |
| 394 |
return false; |
| 395 |
} |
| 396 |
|
| 397 |
agx_foreach_src(I, s) { |
| 398 |
if (!agx_is_null(I->src[s]) && I->src[s].size != AGX_SIZE_16) |
| 399 |
return false; |
| 400 |
} |
| 401 |
|
| 402 |
return true; |
| 403 |
} |
| 404 |
|
| 405 |
/* Generic pack for ALU instructions, which are quite regular */ |
| 406 |
|
| 407 |
static void |
| 408 |
agx_pack_alu(struct util_dynarray *emission, agx_instr *I) |
| 409 |
{ |
| 410 |
struct agx_opcode_info info = agx_opcodes_info[I->op]; |
| 411 |
struct agx_encoding encoding = info.encoding; |
| 412 |
|
| 413 |
bool is_f16 = (I->op == AGX_OPCODE_HMUL || I->op == AGX_OPCODE_HFMA || |
| 414 |
I->op == AGX_OPCODE_HADD); |
| 415 |
|
| 416 |
pack_assert_msg(I, encoding.exact, "invalid encoding"); |
| 417 |
|
| 418 |
uint64_t raw = encoding.exact; |
| 419 |
uint16_t extend = 0; |
| 420 |
|
| 421 |
// TODO: assert saturable |
| 422 |
if (I->saturate) |
| 423 |
raw |= (1 << 6); |
| 424 |
|
| 425 |
if (info.nr_dests) { |
| 426 |
pack_assert(I, info.nr_dests == 1); |
| 427 |
unsigned D = agx_pack_alu_dst(I, I->dest[0]); |
| 428 |
unsigned extend_offset = (sizeof(extend) * 8) - 4; |
| 429 |
|
| 430 |
raw |= (D & BITFIELD_MASK(8)) << 7; |
| 431 |
extend |= ((D >> 8) << extend_offset); |
| 432 |
|
| 433 |
if (info.immediates & AGX_IMMEDIATE_INVERT_COND) { |
| 434 |
raw |= (uint64_t)(I->invert_cond) << 47; |
| 435 |
} |
| 436 |
} else if (info.immediates & AGX_IMMEDIATE_NEST) { |
| 437 |
raw |= (I->invert_cond << 8); |
| 438 |
raw |= (I->nest << 11); |
| 439 |
raw |= (I->icond << 13); |
| 440 |
} |
| 441 |
|
| 442 |
for (unsigned s = 0; s < info.nr_srcs; ++s) { |
| 443 |
bool is_cmpsel = (s >= 2) && (I->op == AGX_OPCODE_ICMPSEL || |
| 444 |
I->op == AGX_OPCODE_FCMPSEL); |
| 445 |
|
| 446 |
unsigned src = is_cmpsel |
| 447 |
? agx_pack_cmpsel_src(I, I->src[s], I->dest[0].size) |
| 448 |
: agx_pack_alu_src(I, I->src[s]); |
| 449 |
|
| 450 |
unsigned src_short = (src & BITFIELD_MASK(10)); |
| 451 |
unsigned src_extend = (src >> 10); |
| 452 |
|
| 453 |
/* Size bit always zero and so omitted for 16-bit */ |
| 454 |
if (is_f16 && !is_cmpsel) |
| 455 |
pack_assert(I, (src_short & (1 << 9)) == 0); |
| 456 |
|
| 457 |
if (info.is_float || (I->op == AGX_OPCODE_FCMPSEL && !is_cmpsel)) { |
| 458 |
unsigned fmod = agx_pack_float_mod(I->src[s]); |
| 459 |
unsigned fmod_offset = is_f16 ? 9 : 10; |
| 460 |
src_short |= (fmod << fmod_offset); |
| 461 |
} else if (I->op == AGX_OPCODE_IMAD || I->op == AGX_OPCODE_IADD) { |
| 462 |
/* Force unsigned for immediates so uadd_sat works properly */ |
| 463 |
bool zext = I->src[s].abs || I->src[s].type == AGX_INDEX_IMMEDIATE; |
| 464 |
bool extends = I->src[s].size < AGX_SIZE_64; |
| 465 |
|
| 466 |
unsigned sxt = (extends && !zext) ? (1 << 10) : 0; |
| 467 |
|
| 468 |
unsigned negate_src = (I->op == AGX_OPCODE_IMAD) ? 2 : 1; |
| 469 |
pack_assert(I, !I->src[s].neg || s == negate_src); |
| 470 |
src_short |= sxt; |
| 471 |
} |
| 472 |
|
| 473 |
/* Sources come at predictable offsets */ |
| 474 |
unsigned offset = 16 + (12 * s); |
| 475 |
raw |= (((uint64_t)src_short) << offset); |
| 476 |
|
| 477 |
/* Destination and each source get extended in reverse order */ |
| 478 |
unsigned extend_offset = (sizeof(extend) * 8) - ((s + 3) * 2); |
| 479 |
extend |= (src_extend << extend_offset); |
| 480 |
} |
| 481 |
|
| 482 |
if ((I->op == AGX_OPCODE_IMAD && I->src[2].neg) || |
| 483 |
(I->op == AGX_OPCODE_IADD && I->src[1].neg)) |
| 484 |
raw |= (1 << 27); |
| 485 |
|
| 486 |
if (info.immediates & AGX_IMMEDIATE_TRUTH_TABLE) { |
| 487 |
raw |= (I->truth_table & 0x3) << 26; |
| 488 |
raw |= (uint64_t)(I->truth_table >> 2) << 38; |
| 489 |
} else if (info.immediates & AGX_IMMEDIATE_SHIFT) { |
| 490 |
pack_assert(I, I->shift <= 4); |
| 491 |
raw |= (uint64_t)(I->shift & 1) << 39; |
| 492 |
raw |= (uint64_t)(I->shift >> 1) << 52; |
| 493 |
} else if (info.immediates & AGX_IMMEDIATE_BFI_MASK) { |
| 494 |
raw |= (uint64_t)(I->bfi_mask & 0x3) << 38; |
| 495 |
raw |= (uint64_t)((I->bfi_mask >> 2) & 0x3) << 50; |
| 496 |
raw |= (uint64_t)((I->bfi_mask >> 4) & 0x1) << 63; |
| 497 |
} else if (info.immediates & AGX_IMMEDIATE_SIMD_OP) { |
| 498 |
raw |= (uint64_t)(I->simd_op & 0x1) << 28; |
| 499 |
raw |= (uint64_t)((I->simd_op >> 1) & 0x7) << 38; |
| 500 |
raw |= (uint64_t)((I->simd_op >> 4) & 0x1) << 47; |
| 501 |
} else if (info.immediates & AGX_IMMEDIATE_SR) { |
| 502 |
raw |= (uint64_t)(I->sr & 0x3F) << 16; |
| 503 |
raw |= (uint64_t)(I->sr >> 6) << 26; |
| 504 |
} else if (info.immediates & AGX_IMMEDIATE_WRITEOUT) |
| 505 |
raw |= (uint64_t)(I->imm) << 8; |
| 506 |
else if (info.immediates & AGX_IMMEDIATE_IMM) |
| 507 |
raw |= (uint64_t)(I->imm) << 16; |
| 508 |
else if (info.immediates & AGX_IMMEDIATE_ROUND) |
| 509 |
raw |= (uint64_t)(I->imm) << 26; |
| 510 |
else if (info.immediates & (AGX_IMMEDIATE_FCOND | AGX_IMMEDIATE_ICOND)) |
| 511 |
raw |= (uint64_t)(I->fcond) << 61; |
| 512 |
|
| 513 |
/* Determine length bit */ |
| 514 |
unsigned length = encoding.length_short; |
| 515 |
if (I->op == AGX_OPCODE_MOV_IMM && I->dest[0].size == AGX_SIZE_16) |
| 516 |
length -= 2; |
| 517 |
|
| 518 |
uint64_t short_mask = BITFIELD64_MASK(8 * length); |
| 519 |
bool length_bit = (extend || (raw & ~short_mask)); |
| 520 |
|
| 521 |
if (encoding.extensible && length_bit) { |
| 522 |
raw |= (1 << 15); |
| 523 |
length += (length > 8) ? 4 : 2; |
| 524 |
} |
| 525 |
|
| 526 |
/* Pack! */ |
| 527 |
if (length <= sizeof(uint64_t)) { |
| 528 |
unsigned extend_offset = ((length - sizeof(extend)) * 8); |
| 529 |
|
| 530 |
/* XXX: Encode these special cases better */ |
| 531 |
switch (I->op) { |
| 532 |
case AGX_OPCODE_IADD: |
| 533 |
case AGX_OPCODE_ICMP_BALLOT: |
| 534 |
case AGX_OPCODE_ICMP_QUAD_BALLOT: |
| 535 |
case AGX_OPCODE_FCMP_BALLOT: |
| 536 |
case AGX_OPCODE_FCMP_QUAD_BALLOT: |
| 537 |
extend_offset -= 16; |
| 538 |
break; |
| 539 |
default: |
| 540 |
break; |
| 541 |
} |
| 542 |
|
| 543 |
raw |= (uint64_t)extend << extend_offset; |
| 544 |
memcpy(util_dynarray_grow_bytes(emission, 1, length), &raw, length); |
| 545 |
} else { |
| 546 |
/* So far, >8 byte ALU is only to store the extend bits */ |
| 547 |
unsigned extend_offset = (((length - sizeof(extend)) * 8) - 64); |
| 548 |
unsigned hi = ((uint64_t)extend) << extend_offset; |
| 549 |
|
| 550 |
memcpy(util_dynarray_grow_bytes(emission, 1, 8), &raw, 8); |
| 551 |
memcpy(util_dynarray_grow_bytes(emission, 1, length - 8), &hi, |
| 552 |
length - 8); |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
static void |
| 557 |
agx_pack_instr(struct util_dynarray *emission, struct util_dynarray *fixups, |
| 558 |
agx_instr *I, enum u_tristate needs_g13x_coherency) |
| 559 |
{ |
| 560 |
switch (I->op) { |
| 561 |
case AGX_OPCODE_LD_TILE: |
| 562 |
case AGX_OPCODE_ST_TILE: { |
| 563 |
bool load = (I->op == AGX_OPCODE_LD_TILE); |
| 564 |
unsigned D = agx_pack_alu_dst(I, load ? I->dest[0] : I->src[0]); |
| 565 |
pack_assert(I, I->mask < 0x10); |
| 566 |
pack_assert(I, I->pixel_offset < 0x200); |
| 567 |
|
| 568 |
agx_index sample_index = load ? I->src[0] : I->src[1]; |
| 569 |
agx_index coords = load ? I->src[1] : I->src[2]; |
| 570 |
pack_assert(I, sample_index.type == AGX_INDEX_REGISTER || |
| 571 |
sample_index.type == AGX_INDEX_IMMEDIATE); |
| 572 |
pack_assert(I, sample_index.size == AGX_SIZE_16); |
| 573 |
unsigned St = (sample_index.type == AGX_INDEX_REGISTER) ? 1 : 0; |
| 574 |
unsigned S = sample_index.value; |
| 575 |
pack_assert(I, S < 0x100); |
| 576 |
|
| 577 |
pack_assert(I, I->explicit_coords == (coords.type == AGX_INDEX_REGISTER)); |
| 578 |
unsigned C = I->explicit_coords ? coords.value : 0; |
| 579 |
|
| 580 |
uint64_t raw = agx_opcodes_info[I->op].encoding.exact | |
| 581 |
((uint64_t)((D >> 1) & BITFIELD_MASK(7)) << 8) | |
| 582 |
(St << 22) | ((uint64_t)(I->format) << 24) | |
| 583 |
((uint64_t)(C & BITFIELD_MASK(6)) << 16) | |
| 584 |
((uint64_t)(I->pixel_offset & BITFIELD_MASK(7)) << 28) | |
| 585 |
(load || I->explicit_coords ? (1ull << 35) : 0) | |
| 586 |
((uint64_t)(I->mask) << 36) | |
| 587 |
((uint64_t)(I->pixel_offset >> 7) << 40) | |
| 588 |
((uint64_t)(S & BITFIELD_MASK(6)) << 42) | |
| 589 |
(I->explicit_coords ? (1ull << 55) : 0) | |
| 590 |
((uint64_t)(S >> 6) << 56) | ((uint64_t)(C >> 6) << 58) | |
| 591 |
(((uint64_t)(D >> 8)) << 60); |
| 592 |
|
| 593 |
unsigned size = 8; |
| 594 |
memcpy(util_dynarray_grow_bytes(emission, 1, size), &raw, size); |
| 595 |
break; |
| 596 |
} |
| 597 |
|
| 598 |
case AGX_OPCODE_SAMPLE_MASK: { |
| 599 |
unsigned S = agx_pack_sample_mask_src(I, I->src[1]); |
| 600 |
unsigned T = I->src[0].value; |
| 601 |
bool Tt = I->src[0].type == AGX_INDEX_IMMEDIATE; |
| 602 |
pack_assert(I, Tt || I->src[0].type == AGX_INDEX_REGISTER); |
| 603 |
pack_assert(I, S < (1 << 10)); |
| 604 |
uint32_t raw = 0xc1 | (Tt ? BITFIELD_BIT(8) : 0) | |
| 605 |
((T & BITFIELD_MASK(6)) << 9) | ((S & 0xff) << 16) | |
| 606 |
((T >> 6) << 24) | ((S >> 8) << 26); |
| 607 |
|
| 608 |
unsigned size = 4; |
| 609 |
memcpy(util_dynarray_grow_bytes(emission, 1, size), &raw, size); |
| 610 |
break; |
| 611 |
} |
| 612 |
|
| 613 |
case AGX_OPCODE_WAIT: { |
| 614 |
uint64_t raw = |
| 615 |
agx_opcodes_info[I->op].encoding.exact | (I->scoreboard << 8); |
| 616 |
|
| 617 |
unsigned size = 2; |
| 618 |
memcpy(util_dynarray_grow_bytes(emission, 1, size), &raw, size); |
| 619 |
break; |
| 620 |
} |
| 621 |
|
| 622 |
case AGX_OPCODE_ITER: |
| 623 |
case AGX_OPCODE_ITERPROJ: |
| 624 |
case AGX_OPCODE_LDCF: { |
| 625 |
bool flat = (I->op == AGX_OPCODE_LDCF); |
| 626 |
bool perspective = (I->op == AGX_OPCODE_ITERPROJ); |
| 627 |
unsigned D = agx_pack_alu_dst(I, I->dest[0]); |
| 628 |
unsigned channels = (I->channels & 0x3); |
| 629 |
|
| 630 |
/* Destination cache not supported, mask off */ |
| 631 |
D &= ~1; |
| 632 |
|
| 633 |
agx_index src_I = I->src[0]; |
| 634 |
pack_assert(I, src_I.type == AGX_INDEX_IMMEDIATE || |
| 635 |
src_I.type == AGX_INDEX_REGISTER); |
| 636 |
|
| 637 |
unsigned cf_I = src_I.value; |
| 638 |
unsigned cf_J = 0; |
| 639 |
|
| 640 |
if (perspective) { |
| 641 |
agx_index src_J = I->src[1]; |
| 642 |
pack_assert(I, src_J.type == AGX_INDEX_IMMEDIATE); |
| 643 |
cf_J = src_J.value; |
| 644 |
} |
| 645 |
|
| 646 |
pack_assert(I, cf_I < 0x100); |
| 647 |
pack_assert(I, cf_J < 0x100); |
| 648 |
|
| 649 |
enum agx_interpolation interp = I->interpolation; |
| 650 |
agx_index sample_index = flat ? agx_null() : I->src[perspective ? 2 : 1]; |
| 651 |
|
| 652 |
/* Fix up the interpolation enum to distinguish the sample index source */ |
| 653 |
if (interp == AGX_INTERPOLATION_SAMPLE) { |
| 654 |
if (sample_index.type == AGX_INDEX_REGISTER) |
| 655 |
interp = AGX_INTERPOLATION_SAMPLE_REGISTER; |
| 656 |
else |
| 657 |
pack_assert(I, sample_index.type == AGX_INDEX_IMMEDIATE); |
| 658 |
} else { |
| 659 |
sample_index = agx_zero(); |
| 660 |
} |
| 661 |
|
| 662 |
bool kill = false; // TODO: optimize |
| 663 |
bool forward = false; // TODO: optimize |
| 664 |
|
| 665 |
uint64_t raw = |
| 666 |
0x21 | (flat ? (1 << 7) : 0) | (perspective ? (1 << 6) : 0) | |
| 667 |
((D & 0xFF) << 7) | (1ull << 15) | /* XXX */ |
| 668 |
((cf_I & BITFIELD_MASK(6)) << 16) | |
| 669 |
((src_I.type == AGX_INDEX_REGISTER) ? (1 << 23) : 0) | |
| 670 |
((cf_J & BITFIELD_MASK(6)) << 24) | (((uint64_t)channels) << 30) | |
| 671 |
(((uint64_t)sample_index.value) << 32) | (forward ? (1ull << 46) : 0) | |
| 672 |
(((uint64_t)interp) << 48) | (kill ? (1ull << 52) : 0) | |
| 673 |
(((uint64_t)(D >> 8)) << 56) | ((uint64_t)(cf_I >> 6) << 58) | |
| 674 |
((uint64_t)(cf_J >> 6) << 60); |
| 675 |
|
| 676 |
unsigned size = 8; |
| 677 |
memcpy(util_dynarray_grow_bytes(emission, 1, size), &raw, size); |
| 678 |
break; |
| 679 |
} |
| 680 |
|
| 681 |
case AGX_OPCODE_ST_VARY: { |
| 682 |
agx_index index_src = I->src[0]; |
| 683 |
agx_index value = I->src[1]; |
| 684 |
|
| 685 |
pack_assert(I, index_src.type == AGX_INDEX_IMMEDIATE || |
| 686 |
index_src.type == AGX_INDEX_REGISTER); |
| 687 |
pack_assert(I, index_src.value < BITFIELD_MASK(8)); |
| 688 |
pack_assert(I, value.type == AGX_INDEX_REGISTER); |
| 689 |
pack_assert(I, value.size == AGX_SIZE_32); |
| 690 |
|
| 691 |
uint64_t raw = 0x11 | (I->last ? (1 << 7) : 0) | |
| 692 |
((value.value & 0x3F) << 9) | |
| 693 |
(((uint64_t)(index_src.value & 0x3F)) << 16) | |
| 694 |
(index_src.type == AGX_INDEX_IMMEDIATE ? (1 << 23) : 0) | |
| 695 |
((value.value >> 6) << 24) | |
| 696 |
((index_src.value >> 6) << 26) | (0x8u << 28); /* XXX */ |
| 697 |
|
| 698 |
unsigned size = 4; |
| 699 |
memcpy(util_dynarray_grow_bytes(emission, 1, size), &raw, size); |
| 700 |
break; |
| 701 |
} |
| 702 |
|
| 703 |
case AGX_OPCODE_DEVICE_LOAD: |
| 704 |
case AGX_OPCODE_DEVICE_STORE: |
| 705 |
case AGX_OPCODE_UNIFORM_STORE: { |
| 706 |
bool is_device_store = I->op == AGX_OPCODE_DEVICE_STORE; |
| 707 |
bool is_uniform_store = I->op == AGX_OPCODE_UNIFORM_STORE; |
| 708 |
bool is_store = is_device_store || is_uniform_store; |
| 709 |
bool has_base = !is_uniform_store; |
| 710 |
|
| 711 |
/* Uniform stores are required to be 16-bit. The encoding that should be |
| 712 |
* 32-bit annoyingly doesn't work. Fix up the format and size so we can |
| 713 |
* use scalar 32-bit values in the IR and avoid special casing earlier in |
| 714 |
* the compiler. |
| 715 |
*/ |
| 716 |
enum agx_format format = is_uniform_store ? AGX_FORMAT_I16 : I->format; |
| 717 |
agx_index reg = is_store ? I->src[0] : I->dest[0]; |
| 718 |
unsigned mask = I->mask; |
| 719 |
|
| 720 |
if (is_uniform_store && reg.size != AGX_SIZE_16) { |
| 721 |
if (reg.size == AGX_SIZE_64) { |
| 722 |
assert(mask == 1); |
| 723 |
mask = BITFIELD_MASK(4); |
| 724 |
} else { |
| 725 |
assert(reg.size == AGX_SIZE_32); |
| 726 |
assert(mask == 1 || mask == 3); |
| 727 |
mask = BITFIELD_MASK(mask == 3 ? 4 : 2); |
| 728 |
} |
| 729 |
|
| 730 |
reg.size = AGX_SIZE_16; |
| 731 |
} |
| 732 |
|
| 733 |
unsigned offset_src = (has_base ? 1 : 0) + (is_store ? 1 : 0); |
| 734 |
|
| 735 |
bool Rt, At = false, Ot; |
| 736 |
unsigned R = agx_pack_memory_reg(I, reg, &Rt); |
| 737 |
unsigned A = |
| 738 |
has_base ? agx_pack_memory_base(I, I->src[is_store ? 1 : 0], &At) : 0; |
| 739 |
unsigned O = agx_pack_memory_index(I, I->src[offset_src], &Ot); |
| 740 |
unsigned u1 = is_uniform_store ? 0 : 1; // XXX |
| 741 |
unsigned u3 = 0; |
| 742 |
unsigned u4 = is_uniform_store ? 0 : I->coherent ? 7 : 4; |
| 743 |
unsigned u5 = 0; |
| 744 |
bool L = true; /* TODO: when would you want short? */ |
| 745 |
|
| 746 |
pack_assert(I, mask != 0); |
| 747 |
pack_assert(I, format <= 0x10); |
| 748 |
|
| 749 |
uint64_t raw = |
| 750 |
agx_opcodes_info[I->op].encoding.exact | |
| 751 |
((format & BITFIELD_MASK(3)) << 7) | ((R & BITFIELD_MASK(6)) << 10) | |
| 752 |
((A & BITFIELD_MASK(4)) << 16) | ((O & BITFIELD_MASK(4)) << 20) | |
| 753 |
(Ot ? (1 << 24) : 0) | (I->src[offset_src].abs ? (1 << 25) : 0) | |
| 754 |
(is_uniform_store ? (2 << 25) : 0) | (u1 << 26) | (At << 27) | |
| 755 |
(u3 << 28) | (I->scoreboard << 30) | |
| 756 |
(((uint64_t)((O >> 4) & BITFIELD_MASK(4))) << 32) | |
| 757 |
(((uint64_t)((A >> 4) & BITFIELD_MASK(4))) << 36) | |
| 758 |
(((uint64_t)((R >> 6) & BITFIELD_MASK(2))) << 40) | |
| 759 |
(((uint64_t)I->shift) << 42) | (((uint64_t)u4) << 44) | |
| 760 |
(L ? (1ull << 47) : 0) | (((uint64_t)(format >> 3)) << 48) | |
| 761 |
(((uint64_t)Rt) << 49) | (((uint64_t)u5) << 50) | |
| 762 |
(((uint64_t)mask) << 52) | (((uint64_t)(O >> 8)) << 56); |
| 763 |
|
| 764 |
unsigned size = L ? 8 : 6; |
| 765 |
memcpy(util_dynarray_grow_bytes(emission, 1, size), &raw, size); |
| 766 |
break; |
| 767 |
} |
| 768 |
|
| 769 |
case AGX_OPCODE_LOCAL_LOAD: |
| 770 |
case AGX_OPCODE_LOCAL_STORE: { |
| 771 |
bool is_load = I->op == AGX_OPCODE_LOCAL_LOAD; |
| 772 |
bool L = true; /* TODO: when would you want short? */ |
| 773 |
unsigned At; |
| 774 |
bool Rt, Ot; |
| 775 |
|
| 776 |
unsigned R = |
| 777 |
agx_pack_memory_reg(I, is_load ? I->dest[0] : I->src[0], &Rt); |
| 778 |
unsigned A = agx_pack_local_base(I, is_load ? I->src[0] : I->src[1], &At); |
| 779 |
unsigned O = |
| 780 |
agx_pack_local_index(I, is_load ? I->src[1] : I->src[2], &Ot); |
| 781 |
|
| 782 |
uint64_t raw = |
| 783 |
agx_opcodes_info[I->op].encoding.exact | (Rt ? BITFIELD64_BIT(8) : 0) | |
| 784 |
((R & BITFIELD_MASK(6)) << 9) | (L ? BITFIELD64_BIT(15) : 0) | |
| 785 |
((A & BITFIELD_MASK(6)) << 16) | (At << 22) | (I->format << 24) | |
| 786 |
((O & BITFIELD64_MASK(6)) << 28) | (Ot ? BITFIELD64_BIT(34) : 0) | |
| 787 |
(((uint64_t)I->mask) << 36) | (((uint64_t)(O >> 6)) << 48) | |
| 788 |
(((uint64_t)(A >> 6)) << 58) | (((uint64_t)(R >> 6)) << 60); |
| 789 |
|
| 790 |
unsigned size = L ? 8 : 6; |
| 791 |
memcpy(util_dynarray_grow_bytes(emission, 1, size), &raw, size); |
| 792 |
break; |
| 793 |
} |
| 794 |
|
| 795 |
case AGX_OPCODE_ATOMIC: { |
| 796 |
bool At, Ot, Rt; |
| 797 |
unsigned A = agx_pack_memory_base(I, I->src[1], &At); |
| 798 |
unsigned O = agx_pack_memory_index(I, I->src[2], &Ot); |
| 799 |
unsigned R = agx_pack_atomic_dest(I, I->dest[0], &Rt); |
| 800 |
unsigned S = agx_pack_atomic_source(I, I->src[0]); |
| 801 |
|
| 802 |
/* Due to a hardware quirk, there is a bit in the atomic instruction that |
| 803 |
* differs based on the target GPU. So, if we're packing an atomic, the |
| 804 |
* shader must be keyed to a particular GPU (either needs_g13x_coherency |
| 805 |
* or not needs_g13x_coherency). Assert that here. |
| 806 |
* |
| 807 |
* needs_g13x_coherency == U_TRISTATE_UNSET is only allowed for shaders |
| 808 |
* that do not use atomics and are therefore portable across devices. |
| 809 |
*/ |
| 810 |
assert(needs_g13x_coherency != U_TRISTATE_UNSET); |
| 811 |
|
| 812 |
uint64_t raw = |
| 813 |
agx_opcodes_info[I->op].encoding.exact | |
| 814 |
(((uint64_t)I->atomic_opc) << 6) | ((R & BITFIELD_MASK(6)) << 10) | |
| 815 |
((A & BITFIELD_MASK(4)) << 16) | ((O & BITFIELD_MASK(4)) << 20) | |
| 816 |
(Ot ? (1 << 24) : 0) | (I->src[2].abs ? (1 << 25) : 0) | (At << 27) | |
| 817 |
(I->scoreboard << 30) | |
| 818 |
(((uint64_t)((O >> 4) & BITFIELD_MASK(4))) << 32) | |
| 819 |
(((uint64_t)((A >> 4) & BITFIELD_MASK(4))) << 36) | |
| 820 |
(((uint64_t)(R >> 6)) << 40) | |
| 821 |
(needs_g13x_coherency == U_TRISTATE_YES ? BITFIELD64_BIT(45) : 0) | |
| 822 |
(Rt ? BITFIELD64_BIT(47) : 0) | (((uint64_t)S) << 48) | |
| 823 |
(((uint64_t)(O >> 8)) << 56); |
| 824 |
|
| 825 |
memcpy(util_dynarray_grow_bytes(emission, 1, 8), &raw, 8); |
| 826 |
break; |
| 827 |
} |
| 828 |
|
| 829 |
case AGX_OPCODE_LOCAL_ATOMIC: { |
| 830 |
bool L = true; /* TODO: Don't force */ |
| 831 |
|
| 832 |
unsigned At; |
| 833 |
bool Rt = false, Ot; |
| 834 |
|
| 835 |
bool Ra = I->dest[0].type != AGX_INDEX_NULL; |
| 836 |
unsigned R = Ra ? agx_pack_memory_reg(I, I->dest[0], &Rt) : 0; |
| 837 |
unsigned S = agx_pack_atomic_source(I, I->src[0]); |
| 838 |
unsigned A = agx_pack_local_base(I, I->src[1], &At); |
| 839 |
unsigned O = agx_pack_local_index(I, I->src[2], &Ot); |
| 840 |
|
| 841 |
uint64_t raw = |
| 842 |
agx_opcodes_info[I->op].encoding.exact | (Rt ? BITFIELD64_BIT(8) : 0) | |
| 843 |
((R & BITFIELD_MASK(6)) << 9) | (L ? BITFIELD64_BIT(15) : 0) | |
| 844 |
((A & BITFIELD_MASK(6)) << 16) | (At << 22) | |
| 845 |
(((uint64_t)I->atomic_opc) << 24) | ((O & BITFIELD64_MASK(6)) << 28) | |
| 846 |
(Ot ? BITFIELD64_BIT(34) : 0) | (Ra ? BITFIELD64_BIT(38) : 0) | |
| 847 |
(((uint64_t)(O >> 6)) << 48) | (((uint64_t)(A >> 6)) << 58) | |
| 848 |
(((uint64_t)(R >> 6)) << 60); |
| 849 |
|
| 850 |
uint64_t raw2 = S; |
| 851 |
|
| 852 |
memcpy(util_dynarray_grow_bytes(emission, 1, 8), &raw, 8); |
| 853 |
memcpy(util_dynarray_grow_bytes(emission, 1, 2), &raw2, 2); |
| 854 |
break; |
| 855 |
} |
| 856 |
|
| 857 |
case AGX_OPCODE_TEXTURE_LOAD: |
| 858 |
case AGX_OPCODE_IMAGE_LOAD: |
| 859 |
case AGX_OPCODE_TEXTURE_SAMPLE: { |
| 860 |
pack_assert(I, I->mask != 0); |
| 861 |
pack_assert(I, I->format <= 0x10); |
| 862 |
|
| 863 |
bool Rt, Ct, St, Cs; |
| 864 |
unsigned Tt; |
| 865 |
unsigned U; |
| 866 |
enum agx_lod_mode lod_mode = I->lod_mode; |
| 867 |
|
| 868 |
unsigned R = agx_pack_memory_reg(I, I->dest[0], &Rt); |
| 869 |
unsigned C = agx_pack_sample_coords(I, I->src[0], &Ct, &Cs); |
| 870 |
unsigned T = agx_pack_texture(I, I->src[2], I->src[3], &U, &Tt); |
| 871 |
unsigned S = agx_pack_sampler(I, I->src[4], &St); |
| 872 |
unsigned O = agx_pack_sample_compare_offset(I, I->src[5]); |
| 873 |
unsigned D = agx_pack_lod(I, I->src[1], &lod_mode); |
| 874 |
|
| 875 |
unsigned q1 = I->shadow; |
| 876 |
unsigned q2 = I->query_lod ? 2 : 0; |
| 877 |
unsigned q3 = 0xc; // XXX |
| 878 |
unsigned kill = 0; // helper invocation kill bit |
| 879 |
|
| 880 |
/* Set bit 43 for image loads. This seems to makes sure that image loads |
| 881 |
* get the value written by the latest image store, not some other image |
| 882 |
* store that was already in flight, fixing |
| 883 |
* |
| 884 |
* KHR-GLES31.core.shader_image_load_store.basic-glsl-misc-fs |
| 885 |
* |
| 886 |
* Apple seems to set this bit unconditionally for read/write image loads |
| 887 |
* and never for readonly image loads. Some sort of cache control. |
| 888 |
*/ |
| 889 |
if (I->op == AGX_OPCODE_IMAGE_LOAD) { |
| 890 |
q3 |= 1; |
| 891 |
|
| 892 |
/* Cache bypass for multidie coherency */ |
| 893 |
if (I->coherent) { |
| 894 |
q3 |= 2; |
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
uint32_t extend = ((U & BITFIELD_MASK(5)) << 0) | (kill << 5) | |
| 899 |
(I->sparse ? (1 << 6) : 0) | ((I->dim >> 3) << 7) | |
| 900 |
((R >> 6) << 8) | ((C >> 6) << 10) | ((D >> 6) << 12) | |
| 901 |
((T >> 6) << 14) | ((O & BITFIELD_MASK(6)) << 16) | |
| 902 |
(I->gather << 23) | (I->offset << 27) | |
| 903 |
((S >> 6) << 28) | ((O >> 6) << 30); |
| 904 |
|
| 905 |
bool L = (extend != 0); |
| 906 |
|
| 907 |
uint64_t raw = |
| 908 |
0x31 | ((I->op != AGX_OPCODE_TEXTURE_SAMPLE) ? (1 << 6) : 0) | |
| 909 |
(Rt ? (1 << 8) : 0) | ((R & BITFIELD_MASK(6)) << 9) | |
| 910 |
(L ? (1 << 15) : 0) | ((C & BITFIELD_MASK(6)) << 16) | |
| 911 |
(Ct ? (1 << 22) : 0) | (q1 << 23) | ((D & BITFIELD_MASK(6)) << 24) | |
| 912 |
(q2 << 30) | (((uint64_t)(T & BITFIELD_MASK(6))) << 32) | |
| 913 |
(((uint64_t)Tt) << 38) | |
| 914 |
(((uint64_t)(I->dim & BITFIELD_MASK(3))) << 40) | |
| 915 |
(((uint64_t)q3) << 43) | (Cs ? BITFIELD64_BIT(47) : 0) | |
| 916 |
(((uint64_t)I->mask) << 48) | (((uint64_t)lod_mode) << 52) | |
| 917 |
(((uint64_t)(S & BITFIELD_MASK(6))) << 56) | (((uint64_t)St) << 62) | |
| 918 |
(((uint64_t)I->scoreboard) << 63); |
| 919 |
|
| 920 |
memcpy(util_dynarray_grow_bytes(emission, 1, 8), &raw, 8); |
| 921 |
if (L) |
| 922 |
memcpy(util_dynarray_grow_bytes(emission, 1, 4), &extend, 4); |
| 923 |
|
| 924 |
break; |
| 925 |
} |
| 926 |
|
| 927 |
case AGX_OPCODE_TEX_STATE_STORE: |
| 928 |
case AGX_OPCODE_SAMPLER_STATE_STORE: { |
| 929 |
unsigned U, Tt, T = agx_pack_texture(I, I->src[0], I->src[1], &U, &Tt); |
| 930 |
|
| 931 |
pack_assert(I, U < (1 << 4)); |
| 932 |
pack_assert(I, (T & 1) == 0); |
| 933 |
pack_assert(I, T < (1 << 24)); |
| 934 |
pack_assert(I, I->imm < (1 << 7)); |
| 935 |
|
| 936 |
uint64_t raw = |
| 937 |
agx_opcodes_info[I->op].encoding.exact | (I->imm << 8) | |
| 938 |
(I->scoreboard << 16) | ((uint64_t)T << 26) | |
| 939 |
((I->src[1].type == AGX_INDEX_IMMEDIATE) ? (1ul << 58) : 0) | |
| 940 |
((uint64_t)U << 59); |
| 941 |
|
| 942 |
memcpy(util_dynarray_grow_bytes(emission, 1, 8), &raw, 8); |
| 943 |
break; |
| 944 |
} |
| 945 |
|
| 946 |
case AGX_OPCODE_IMAGE_WRITE: { |
| 947 |
bool Ct, Dt, Rt, Cs; |
| 948 |
unsigned Tt; |
| 949 |
unsigned U; |
| 950 |
|
| 951 |
unsigned R = agx_pack_pbe_source(I, I->src[0], &Rt); |
| 952 |
unsigned C = agx_pack_sample_coords(I, I->src[1], &Ct, &Cs); |
| 953 |
unsigned D = agx_pack_pbe_lod(I, I->src[2], &Dt); |
| 954 |
unsigned T = agx_pack_texture(I, I->src[3], I->src[4], &U, &Tt); |
| 955 |
bool rtz = false; |
| 956 |
|
| 957 |
pack_assert(I, U < (1 << 5)); |
| 958 |
pack_assert(I, D < (1 << 8)); |
| 959 |
pack_assert(I, R < (1 << 8)); |
| 960 |
pack_assert(I, C < (1 << 8)); |
| 961 |
pack_assert(I, T < (1 << 8)); |
| 962 |
pack_assert(I, Tt < (1 << 2)); |
| 963 |
|
| 964 |
unsigned coherency = I->coherent ? 0xf : 0x9; |
| 965 |
|
| 966 |
uint64_t raw = agx_opcodes_info[I->op].encoding.exact | |
| 967 |
(Rt ? (1 << 8) : 0) | ((R & BITFIELD_MASK(6)) << 9) | |
| 968 |
((C & BITFIELD_MASK(6)) << 16) | (Ct ? (1 << 22) : 0) | |
| 969 |
((D & BITFIELD_MASK(6)) << 24) | (Dt ? (1u << 31) : 0) | |
| 970 |
(((uint64_t)(T & BITFIELD_MASK(6))) << 32) | |
| 971 |
(((uint64_t)Tt) << 38) | |
| 972 |
(((uint64_t)I->dim & BITFIELD_MASK(3)) << 40) | |
| 973 |
(((uint64_t)coherency) << 43) | (Cs ? (1ull << 47) : 0) | |
| 974 |
(((uint64_t)U) << 48) | (rtz ? (1ull << 53) : 0) | |
| 975 |
((I->dim & BITFIELD_BIT(4)) ? (1ull << 55) : 0) | |
| 976 |
(((uint64_t)R >> 6) << 56) | (((uint64_t)C >> 6) << 58) | |
| 977 |
(((uint64_t)D >> 6) << 60) | (((uint64_t)T >> 6) << 62); |
| 978 |
|
| 979 |
if (raw >> 48) { |
| 980 |
raw |= BITFIELD_BIT(15); |
| 981 |
memcpy(util_dynarray_grow_bytes(emission, 1, 8), &raw, 8); |
| 982 |
} else { |
| 983 |
memcpy(util_dynarray_grow_bytes(emission, 1, 6), &raw, 6); |
| 984 |
} |
| 985 |
|
| 986 |
break; |
| 987 |
} |
| 988 |
|
| 989 |
case AGX_OPCODE_BLOCK_IMAGE_STORE: { |
| 990 |
enum agx_format F = I->format; |
| 991 |
pack_assert(I, F < 0x10); |
| 992 |
|
| 993 |
unsigned Tt = 0; |
| 994 |
pack_assert(I, Tt < 0x4); |
| 995 |
|
| 996 |
unsigned U; |
| 997 |
unsigned T = agx_pack_texture(I, I->src[0], I->src[1], &U, &Tt); |
| 998 |
pack_assert(I, T < 0x100); |
| 999 |
pack_assert(I, U < (1 << 5)); |
| 1000 |
|
| 1001 |
bool Cs = false; |
| 1002 |
bool Ct = I->src[3].discard; |
| 1003 |
unsigned C = I->src[3].value; |
| 1004 |
|
| 1005 |
agx_index offset = I->src[2]; |
| 1006 |
pack_assert(I, offset.size == AGX_SIZE_32); |
| 1007 |
assert_register_is_aligned(I, offset); |
| 1008 |
unsigned R = offset.value; |
| 1009 |
|
| 1010 |
bool unk1 = true; |
| 1011 |
|
| 1012 |
/* This bit has weird behaviour with the interaction of the texture state |
| 1013 |
* index and the tilebuffer offset. Probably best not to use it for now. |
| 1014 |
*/ |
| 1015 |
unsigned unk3 = 1; |
| 1016 |
|
| 1017 |
uint32_t word0 = agx_opcodes_info[I->op].encoding.exact | |
| 1018 |
(1 << 15) /* we always set length bit for now */ | |
| 1019 |
((F & 1) << 8) | ((R & BITFIELD_MASK(6)) << 9) | |
| 1020 |
((C & BITFIELD_MASK(6)) << 16) | (Ct ? (1 << 22) : 0) | |
| 1021 |
(I->explicit_coords ? (1 << 23) : 0) | |
| 1022 |
(unk1 ? (1u << 31) : 0); |
| 1023 |
|
| 1024 |
uint32_t word1 = (T & BITFIELD_MASK(6)) | (Tt << 6) | |
| 1025 |
((I->dim & BITFIELD_MASK(3)) << 8) | (9 << 11) | |
| 1026 |
(Cs ? (1 << 15) : 0) | (((uint64_t)U) << 16) | |
| 1027 |
((I->dim & BITFIELD_BIT(3)) ? (1u << 23) : 0) | |
| 1028 |
((R >> 6) << 24) | ((C >> 6) << 26); |
| 1029 |
|
| 1030 |
uint32_t word2 = (F >> 1) | (unk3 ? (1 << 3) : 0) | ((T >> 6) << 14); |
| 1031 |
|
| 1032 |
memcpy(util_dynarray_grow_bytes(emission, 1, 4), &word0, 4); |
| 1033 |
memcpy(util_dynarray_grow_bytes(emission, 1, 4), &word1, 4); |
| 1034 |
memcpy(util_dynarray_grow_bytes(emission, 1, 2), &word2, 2); |
| 1035 |
break; |
| 1036 |
} |
| 1037 |
|
| 1038 |
case AGX_OPCODE_ZS_EMIT: { |
| 1039 |
agx_index S = I->src[0]; |
| 1040 |
if (S.type == AGX_INDEX_IMMEDIATE) |
| 1041 |
pack_assert(I, S.value < BITFIELD_BIT(8)); |
| 1042 |
else |
| 1043 |
assert_register_is_aligned(I, S); |
| 1044 |
|
| 1045 |
agx_index T = I->src[1]; |
| 1046 |
assert_register_is_aligned(I, T); |
| 1047 |
|
| 1048 |
pack_assert(I, I->zs >= 1 && I->zs <= 3); |
| 1049 |
|
| 1050 |
uint32_t word0 = agx_opcodes_info[I->op].encoding.exact | |
| 1051 |
((S.type == AGX_INDEX_IMMEDIATE) ? (1 << 8) : 0) | |
| 1052 |
((S.value & BITFIELD_MASK(6)) << 9) | |
| 1053 |
((T.value & BITFIELD_MASK(6)) << 16) | |
| 1054 |
((T.value >> 6) << 26) | ((S.value >> 6) << 24) | |
| 1055 |
(I->zs << 29); |
| 1056 |
|
| 1057 |
memcpy(util_dynarray_grow_bytes(emission, 1, 4), &word0, 4); |
| 1058 |
break; |
| 1059 |
} |
| 1060 |
|
| 1061 |
case AGX_OPCODE_JMP_EXEC_ANY: |
| 1062 |
case AGX_OPCODE_JMP_EXEC_NONE: |
| 1063 |
case AGX_OPCODE_JMP_EXEC_NONE_AFTER: { |
| 1064 |
/* We don't implement indirect branches */ |
| 1065 |
pack_assert(I, I->target != NULL); |
| 1066 |
|
| 1067 |
/* We'll fix the offset later. */ |
| 1068 |
struct agx_branch_fixup fixup = { |
| 1069 |
.block = I->target, |
| 1070 |
.offset = emission->size, |
| 1071 |
.skip_to_end = I->op == AGX_OPCODE_JMP_EXEC_NONE_AFTER, |
| 1072 |
}; |
| 1073 |
|
| 1074 |
util_dynarray_append(fixups, fixup); |
| 1075 |
|
| 1076 |
/* The rest of the instruction is fixed */ |
| 1077 |
struct agx_opcode_info info = agx_opcodes_info[I->op]; |
| 1078 |
uint64_t raw = info.encoding.exact; |
| 1079 |
memcpy(util_dynarray_grow_bytes(emission, 1, 6), &raw, 6); |
| 1080 |
break; |
| 1081 |
} |
| 1082 |
|
| 1083 |
case AGX_OPCODE_DOORBELL: { |
| 1084 |
pack_assert(I, I->imm < BITFIELD_MASK(8)); |
| 1085 |
struct agx_opcode_info info = agx_opcodes_info[I->op]; |
| 1086 |
uint64_t raw = info.encoding.exact | (I->imm << 40); |
| 1087 |
memcpy(util_dynarray_grow_bytes(emission, 1, 6), &raw, 6); |
| 1088 |
break; |
| 1089 |
} |
| 1090 |
|
| 1091 |
case AGX_OPCODE_STACK_UNMAP: |
| 1092 |
case AGX_OPCODE_STACK_MAP: { |
| 1093 |
agx_index value = I->op == AGX_OPCODE_STACK_MAP ? I->src[0] : I->dest[0]; |
| 1094 |
|
| 1095 |
pack_assert(I, value.type == AGX_INDEX_REGISTER); |
| 1096 |
pack_assert(I, value.size == AGX_SIZE_32); |
| 1097 |
pack_assert(I, I->imm < BITFIELD_MASK(16)); |
| 1098 |
|
| 1099 |
unsigned q1 = 0; // XXX |
| 1100 |
unsigned q2 = 0; // XXX |
| 1101 |
unsigned q3 = 0; // XXX |
| 1102 |
unsigned q4 = 16; // XXX |
| 1103 |
unsigned q5 = 16; // XXX |
| 1104 |
|
| 1105 |
struct agx_opcode_info info = agx_opcodes_info[I->op]; |
| 1106 |
uint64_t raw = |
| 1107 |
info.encoding.exact | (q1 << 8) | ((value.value & 0x3F) << 10) | |
| 1108 |
((I->imm & 0xF) << 20) | (1ull << 24) | // XXX |
| 1109 |
(1ull << 26) | // XXX |
| 1110 |
(q2 << 30) | ((uint64_t)((I->imm >> 4) & 0xF) << 32) | |
| 1111 |
((uint64_t)q3 << 37) | ((uint64_t)(value.value >> 6) << 40) | |
| 1112 |
((uint64_t)q4 << 42) | (1ull << 47) | // XXX |
| 1113 |
((uint64_t)q5 << 48) | ((uint64_t)(I->imm >> 8) << 56); |
| 1114 |
|
| 1115 |
memcpy(util_dynarray_grow_bytes(emission, 1, 8), &raw, 8); |
| 1116 |
break; |
| 1117 |
} |
| 1118 |
|
| 1119 |
case AGX_OPCODE_STACK_LOAD: |
| 1120 |
case AGX_OPCODE_STACK_STORE: { |
| 1121 |
enum agx_format format = I->format; |
| 1122 |
unsigned mask = I->mask; |
| 1123 |
|
| 1124 |
bool is_load = I->op == AGX_OPCODE_STACK_LOAD; |
| 1125 |
bool L = true; /* TODO: when would you want short? */ |
| 1126 |
|
| 1127 |
pack_assert(I, mask != 0); |
| 1128 |
pack_assert(I, format <= 0x10); |
| 1129 |
|
| 1130 |
bool Rt, Ot; |
| 1131 |
unsigned R = |
| 1132 |
agx_pack_memory_reg(I, is_load ? I->dest[0] : I->src[0], &Rt); |
| 1133 |
unsigned O = |
| 1134 |
agx_pack_memory_index(I, is_load ? I->src[0] : I->src[1], &Ot); |
| 1135 |
|
| 1136 |
unsigned i1 = 1; // XXX |
| 1137 |
unsigned i2 = 0; // XXX |
| 1138 |
unsigned i5 = 4; // XXX |
| 1139 |
|
| 1140 |
uint64_t raw = |
| 1141 |
agx_opcodes_info[I->op].encoding.exact | |
| 1142 |
((format & BITFIELD_MASK(2)) << 8) | ((R & BITFIELD_MASK(6)) << 10) | |
| 1143 |
((O & BITFIELD_MASK(4)) << 20) | (Ot ? (1 << 24) : 0) | |
| 1144 |
((uint64_t)i1 << 26) | ((uint64_t)I->scoreboard << 30) | |
| 1145 |
(((uint64_t)((O >> 4) & BITFIELD_MASK(4))) << 32) | |
| 1146 |
((uint64_t)i2 << 36) | |
| 1147 |
(((uint64_t)((R >> 6) & BITFIELD_MASK(2))) << 40) | |
| 1148 |
((uint64_t)i5 << 44) | (L ? (1ull << 47) : 0) | |
| 1149 |
(((uint64_t)(format >> 2)) << 50) | (((uint64_t)Rt) << 49) | |
| 1150 |
(((uint64_t)mask) << 52) | (((uint64_t)(O >> 8)) << 56); |
| 1151 |
|
| 1152 |
unsigned size = L ? 8 : 6; |
| 1153 |
memcpy(util_dynarray_grow_bytes(emission, 1, size), &raw, size); |
| 1154 |
break; |
| 1155 |
} |
| 1156 |
case AGX_OPCODE_STACK_ADJUST: { |
| 1157 |
struct agx_opcode_info info = agx_opcodes_info[I->op]; |
| 1158 |
|
| 1159 |
unsigned i0 = 0; // XXX |
| 1160 |
unsigned i1 = 1; // XXX |
| 1161 |
unsigned i2 = 2; // XXX |
| 1162 |
unsigned i3 = 0; // XXX |
| 1163 |
unsigned i4 = 0; // XXX |
| 1164 |
|
| 1165 |
uint64_t raw = |
| 1166 |
info.encoding.exact | ((uint64_t)i0 << 8) | ((uint64_t)i1 << 26) | |
| 1167 |
((uint64_t)i2 << 36) | ((uint64_t)i3 << 44) | ((uint64_t)i4 << 50) | |
| 1168 |
((I->stack_size & 0xF) << 20) | |
| 1169 |
((uint64_t)((I->stack_size >> 4) & 0xF) << 32) | (1ull << 47) | // XXX |
| 1170 |
((uint64_t)(I->stack_size >> 8) << 56); |
| 1171 |
|
| 1172 |
memcpy(util_dynarray_grow_bytes(emission, 1, 8), &raw, 8); |
| 1173 |
break; |
| 1174 |
} |
| 1175 |
|
| 1176 |
case AGX_OPCODE_EXPORT: |
| 1177 |
/* Zero-byte pseudo-op. This remains until late for the benefit of |
| 1178 |
* register cache optimizations. |
| 1179 |
*/ |
| 1180 |
return; |
| 1181 |
|
| 1182 |
default: |
| 1183 |
agx_pack_alu(emission, I); |
| 1184 |
return; |
| 1185 |
} |
| 1186 |
} |
| 1187 |
|
| 1188 |
/* Relative branches may be emitted before their targets, so we patch the |
| 1189 |
* binary to fix up the branch offsets after the main emit */ |
| 1190 |
|
| 1191 |
static void |
| 1192 |
agx_fixup_branch(struct util_dynarray *emission, struct agx_branch_fixup fix) |
| 1193 |
{ |
| 1194 |
/* Branch offset is 2 bytes into the jump instruction */ |
| 1195 |
uint8_t *location = ((uint8_t *)emission->data) + fix.offset + 2; |
| 1196 |
|
| 1197 |
off_t target = fix.skip_to_end ? fix.block->last_offset : fix.block->offset; |
| 1198 |
|
| 1199 |
/* Offsets are relative to the jump instruction */ |
| 1200 |
int32_t patch = (int32_t)target - (int32_t)fix.offset; |
| 1201 |
|
| 1202 |
/* Patch the binary */ |
| 1203 |
memcpy(location, &patch, sizeof(patch)); |
| 1204 |
} |
| 1205 |
|
| 1206 |
void |
| 1207 |
agx_pack_binary(agx_context *ctx, struct util_dynarray *emission) |
| 1208 |
{ |
| 1209 |
struct util_dynarray fixups; |
| 1210 |
util_dynarray_init(&fixups, ctx); |
| 1211 |
|
| 1212 |
agx_foreach_block(ctx, block) { |
| 1213 |
/* Relative to the start of the binary, the block begins at the current |
| 1214 |
* number of bytes emitted */ |
| 1215 |
block->offset = emission->size; |
| 1216 |
|
| 1217 |
agx_foreach_instr_in_block(block, ins) { |
| 1218 |
block->last_offset = emission->size; |
| 1219 |
agx_pack_instr(emission, &fixups, ins, |
| 1220 |
ctx->key->dev.needs_g13x_coherency); |
| 1221 |
} |
| 1222 |
} |
| 1223 |
|
| 1224 |
util_dynarray_foreach(&fixups, struct agx_branch_fixup, fixup) |
| 1225 |
agx_fixup_branch(emission, *fixup); |
| 1226 |
|
| 1227 |
util_dynarray_fini(&fixups); |
| 1228 |
|
| 1229 |
/* Dougall calls the instruction in this footer "trap". Match the blob. */ |
| 1230 |
if (!ctx->key->no_stop || ctx->is_preamble) { |
| 1231 |
for (unsigned i = 0; i < 8; ++i) { |
| 1232 |
uint16_t trap = agx_opcodes_info[AGX_OPCODE_TRAP].encoding.exact; |
| 1233 |
util_dynarray_append(emission, trap); |
| 1234 |
} |
| 1235 |
} |
| 1236 |
} |