Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/asahi/compiler/agx3_funop_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,33 @@ agx3_funop_encode_impl(enum agx_isa_encoding enc, enum agx3_funop op, uint16_t d
(op == AGX3_FUNOP_SQRT_STEP && (dest_16 || src_neg || saturate))))
return false;

/* Native scalar half registers may be odd; float registers are pairs. */
if (dest >= 128 || src >= 128 || (!dest_16 && (dest & 1)) ||
/* Native scalar half registers may be odd; float registers are pairs,
* so a 32-bit h254 operand encodes as h254:h255 and a 32-bit h255 is
* refused.
*
* The runtime entry point (agx3_funop_encode_runtime() /
* agx3_funop_encode_g17g()) admits the full eight-bit half-index
* namespace (h0-h255) that the AGX3C packers, including
* agx3_alu_unary_m5(), already use. The encoder helper itself supports
* all five G17-class scalar register unary forms -- f32rint (including
* AGX3_FUNOP_RINT), f32log2, f32exp2, f32rsqrt and f32sqrtstep -- each
* of which deposits all of the destination and source index bits
* identically in the AGX3C and AGX3D (G18G) tables. In production,
* agx3_emit_funop() routes only G18G floor/ceil/trunc (keeping
* AGX3_FUNOP_RINT off this route so roundeven falls through to
* agx3_alu_unary_m5()), f32log2, f32exp2, f32rsqrt and f32sqrtstep here
* (with SRSQRT and unsaturated RSQRT caught earlier in
* agx3_pack_binary.c by separate ALU helpers). Encodable is not
* allocatable: RA caps AGX3C and AGX3D at 252 halves (AGX3C_NUM_REGS,
* AGX3D_NUM_REGS), so h252-h255 only occur in encoder tests.
*
* The legacy entry point, agx3_funop_encode_source_lifetime(), keeps its
* 128-half limit. It serves the fixed AGX3B/AGX3C replay forms and the
* remaining qualified production forms, and RA constrains the SSA webs of
* those forms to the low bank (texture_gpr in agx_register_allocate.c), so
* 128 is the contract those forms are allocated against. */
const unsigned nr_regs = runtime ? 256 : 128;
if (dest >= nr_regs || src >= nr_regs || (!dest_16 && (dest & 1)) ||
(!src_16 && (src & 1)))
return false;

Expand Down
52 changes: 52 additions & 0 deletions src/asahi/compiler/test/test-agx3-funop-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

#include "agx3_funop_core.h"
#include "agx3_compose/agx3_decode.h"

#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -314,6 +315,57 @@ main(void)
agx3_funop_encode(AGX_ISA_ENCODING_AGX3B, AGX3_FUNOP_FLOOR,
128, 0, false, false, false, 0, b, &len));

/* The runtime path (G17G, G18G) takes every half register. Decode each
* word with the G18G table and check the registers it names, so a field
* bit the AGX3D encoding places differently from AGX3C fails here. */
static const struct {
enum agx3_funop op;
enum agx3_mnemonic mnemonic;
bool half;
} high_ops[] = {
{AGX3_FUNOP_FLOOR, AGX3_MN_f32rint, false},
{AGX3_FUNOP_CEIL, AGX3_MN_f32rint, false},
{AGX3_FUNOP_TRUNC, AGX3_MN_f32rint, false},
{AGX3_FUNOP_LOG2, AGX3_MN_f32log2, false},
{AGX3_FUNOP_EXP2, AGX3_MN_f32exp2, false},
{AGX3_FUNOP_LOG2, AGX3_MN_f32log2, true},
{AGX3_FUNOP_EXP2, AGX3_MN_f32exp2, true},
{AGX3_FUNOP_RSQRT, AGX3_MN_f32rsqrt, false},
{AGX3_FUNOP_RSQRT, AGX3_MN_f32rsqrt, true},
{AGX3_FUNOP_SQRT_STEP, AGX3_MN_f32sqrtstep, false},
};
for (unsigned i = 0; i < sizeof(high_ops) / sizeof(high_ops[0]); ++i) {
const unsigned step = high_ops[i].half ? 1 : 2;
for (unsigned reg = 0; reg < 256; reg += step) {
const uint16_t dest = reg, src = (reg + 130 * step) % 256;
const struct agx3_funop_runtime runtime = {
.src_cache = reg & 4,
.dest_16 = high_ops[i].half,
.src_16 = high_ops[i].half,
};
checks++;
uint64_t fields[AGX3_MAX_FIELDS];
if (!agx3_funop_encode_g17g(high_ops[i].op, dest, src, false, false,
false, reg & 1 ? 0x80 : 0, &runtime, b,
&len) ||
agx3_decode(&agx3d_table, b, len, high_ops[i].mnemonic,
fields) < 0 ||
fields[2] != dest || fields[18] != src) {
fprintf(stderr, "%s dest %u src %u: not encoded for G18G\n",
agx3_mnemonic_name(high_ops[i].mnemonic), dest, src);
failures++;
}
}
}
expect_refused("runtime register out of range",
agx3_funop_encode_g17g(AGX3_FUNOP_FLOOR, 256, 0, false,
false, false, 0, &(struct agx3_funop_runtime){0}, b,
&len));
expect_refused("runtime 32-bit register pair past h255",
agx3_funop_encode_g17g(AGX3_FUNOP_FLOOR, 255, 0, false,
false, false, 0, &(struct agx3_funop_runtime){0}, b,
&len));

if (failures) {
fprintf(stderr, "agx3_funop_core: %u/%u checks failed\n", failures,
checks);
Expand Down