1 /* Copyright (c) 2018-2019 Alyssa Rosenzweig (alyssa@rosenzweig.io)
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 #ifndef __MDG_HELPERS_H
23 #define __MDG_HELPERS_H
25 #include "util/macros.h"
28 #define OP_IS_LOAD_VARY_F(op) (\
29 op == midgard_op_ld_vary_16 || \
30 op == midgard_op_ld_vary_32 \
33 #define OP_IS_STORE_VARY(op) (\
34 op == midgard_op_st_vary_16 || \
35 op == midgard_op_st_vary_32 || \
36 op == midgard_op_st_vary_32u || \
37 op == midgard_op_st_vary_32i \
40 #define OP_IS_PROJECTION(op) ( \
41 op == midgard_op_ldst_perspective_division_z || \
42 op == midgard_op_ldst_perspective_division_w \
45 #define OP_IS_VEC4_ONLY(op) ( \
46 OP_IS_PROJECTION(op) || \
47 op == midgard_op_ld_cubemap_coords \
50 #define OP_IS_MOVE(op) ( \
51 op == midgard_alu_op_fmov || \
52 op == midgard_alu_op_imov \
55 #define OP_IS_UBO_READ(op) ( \
56 op == midgard_op_ld_ubo_char || \
57 op == midgard_op_ld_ubo_char2 || \
58 op == midgard_op_ld_ubo_char4 || \
59 op == midgard_op_ld_ubo_short4 || \
60 op == midgard_op_ld_ubo_int4 \
63 #define OP_IS_CSEL_V(op) ( \
64 op == midgard_alu_op_icsel_v || \
65 op == midgard_alu_op_fcsel_v \
68 #define OP_IS_CSEL(op) ( \
70 op == midgard_alu_op_icsel || \
71 op == midgard_alu_op_fcsel \
74 #define OP_IS_DERIVATIVE(op) ( \
75 op == TEXTURE_OP_DFDX || \
76 op == TEXTURE_OP_DFDY \
79 /* ALU control words are single bit fields with a lot of space */
81 #define ALU_ENAB_VEC_MUL (1 << 17)
82 #define ALU_ENAB_SCAL_ADD (1 << 19)
83 #define ALU_ENAB_VEC_ADD (1 << 21)
84 #define ALU_ENAB_SCAL_MUL (1 << 23)
85 #define ALU_ENAB_VEC_LUT (1 << 25)
86 #define ALU_ENAB_BR_COMPACT (1 << 26)
87 #define ALU_ENAB_BRANCH (1 << 27)
89 /* Other opcode properties that don't conflict with the ALU_ENABs, non-ISA */
91 /* Denotes an opcode that takes a vector input with a fixed-number of
92 * channels, but outputs to only a single output channel, like dot products.
93 * For these, to determine the effective mask, this quirk can be set. We have
94 * an intentional off-by-one (a la MALI_POSITIVE), since 0-channel makes no
95 * sense but we need to fit 4 channels in 2-bits. Similarly, 1-channel doesn't
96 * make sense (since then why are we quirked?), so that corresponds to "no
99 #define OP_CHANNEL_COUNT(c) ((c - 1) << 0)
100 #define GET_CHANNEL_COUNT(c) ((c & (0x3 << 0)) ? ((c & (0x3 << 0)) + 1) : 0)
102 /* For instructions that take a single argument, normally the first argument
103 * slot is used for the argument and the second slot is a dummy #0 constant.
104 * However, there are exceptions: instructions like fmov store their argument
105 * in the _second_ slot and store a dummy r24 in the first slot, designated by
106 * QUIRK_FLIPPED_R24 */
108 #define QUIRK_FLIPPED_R24 (1 << 2)
110 /* Is the op commutative? */
111 #define OP_COMMUTES (1 << 3)
113 /* Does the op convert types between int- and float- space (i2f/f2u/etc) */
114 #define OP_TYPE_CONVERT (1 << 4)
116 /* Vector-independant shorthands for the above; these numbers are arbitrary and
117 * not from the ISA. Convert to the above with unit_enum_to_midgard */
123 /* 4-bit type tags */
125 #define TAG_TEXTURE_4_VTX 0x2
126 #define TAG_TEXTURE_4 0x3
127 #define TAG_LOAD_STORE_4 0x5
128 #define TAG_ALU_4 0x8
129 #define TAG_ALU_8 0x9
130 #define TAG_ALU_12 0xA
131 #define TAG_ALU_16 0xB
134 quadword_size(int tag
)
138 case TAG_LOAD_STORE_4
:
140 case TAG_TEXTURE_4_VTX
:
149 unreachable("Unknown tag");
153 #define IS_ALU(tag) (tag == TAG_ALU_4 || tag == TAG_ALU_8 || \
154 tag == TAG_ALU_12 || tag == TAG_ALU_16)
156 /* Special register aliases */
158 #define MAX_WORK_REGISTERS 16
160 /* Uniforms are begin at (REGISTER_UNIFORMS - uniform_count) */
161 #define REGISTER_UNIFORMS 24
163 #define REGISTER_UNUSED 24
164 #define REGISTER_CONSTANT 26
165 #define REGISTER_LDST_BASE 26
166 #define REGISTER_TEXTURE_BASE 28
167 #define REGISTER_SELECT 31
169 /* SSA helper aliases to mimic the registers. */
171 #define SSA_UNUSED ~0
172 #define SSA_FIXED_SHIFT 24
173 #define SSA_FIXED_REGISTER(reg) (((1 + (reg)) << SSA_FIXED_SHIFT) | 1)
174 #define SSA_REG_FROM_FIXED(reg) ((((reg) & ~1) >> SSA_FIXED_SHIFT) - 1)
175 #define SSA_FIXED_MINIMUM SSA_FIXED_REGISTER(0)
177 /* Swizzle support */
179 #define SWIZZLE(A, B, C, D) (((D) << 6) | ((C) << 4) | ((B) << 2) | ((A) << 0))
180 #define SWIZZLE_FROM_ARRAY(r) SWIZZLE(r[0], r[1], r[2], r[3])
181 #define COMPONENT_X 0x0
182 #define COMPONENT_Y 0x1
183 #define COMPONENT_Z 0x2
184 #define COMPONENT_W 0x3
186 #define SWIZZLE_XXXX SWIZZLE(COMPONENT_X, COMPONENT_X, COMPONENT_X, COMPONENT_X)
187 #define SWIZZLE_XYXX SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_X, COMPONENT_X)
188 #define SWIZZLE_XYZX SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_Z, COMPONENT_X)
189 #define SWIZZLE_XYZW SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_Z, COMPONENT_W)
190 #define SWIZZLE_XYXZ SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_X, COMPONENT_Z)
191 #define SWIZZLE_XYZZ SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_Z, COMPONENT_Z)
192 #define SWIZZLE_XXXY SWIZZLE(COMPONENT_X, COMPONENT_X, COMPONENT_X, COMPONENT_Y)
193 #define SWIZZLE_ZZZW SWIZZLE(COMPONENT_Z, COMPONENT_Z, COMPONENT_Z, COMPONENT_W)
194 #define SWIZZLE_ZWWW SWIZZLE(COMPONENT_Z, COMPONENT_W, COMPONENT_W, COMPONENT_W)
195 #define SWIZZLE_WWWW SWIZZLE(COMPONENT_W, COMPONENT_W, COMPONENT_W, COMPONENT_W)
197 static inline unsigned
198 swizzle_of(unsigned comp
)
210 unreachable("Invalid component count");
214 static inline unsigned
215 mask_of(unsigned nr_comp
)
217 return (1 << nr_comp
) - 1;
225 /* There are five ALU units: VMUL, VADD, SMUL, SADD, LUT. A given opcode is
226 * implemented on some subset of these units (or occassionally all of them).
227 * This table encodes a bit mask of valid units for each opcode, so the
228 * scheduler can figure where to plonk the instruction. */
230 /* Shorthands for each unit */
231 #define UNIT_VMUL ALU_ENAB_VEC_MUL
232 #define UNIT_SADD ALU_ENAB_SCAL_ADD
233 #define UNIT_VADD ALU_ENAB_VEC_ADD
234 #define UNIT_SMUL ALU_ENAB_SCAL_MUL
235 #define UNIT_VLUT ALU_ENAB_VEC_LUT
237 /* Shorthands for usual combinations of units */
239 #define UNITS_MUL (UNIT_VMUL | UNIT_SMUL)
240 #define UNITS_ADD (UNIT_VADD | UNIT_SADD)
241 #define UNITS_MOST (UNITS_MUL | UNITS_ADD)
242 #define UNITS_ALL (UNITS_MOST | UNIT_VLUT)
243 #define UNITS_SCALAR (UNIT_SADD | UNIT_SMUL)
244 #define UNITS_VECTOR (UNIT_VMUL | UNIT_VADD)
245 #define UNITS_ANY_VECTOR (UNITS_VECTOR | UNIT_VLUT)
247 struct mir_op_props
{
254 struct mir_ldst_op_props
{
259 /* Lower 2-bits are a midgard_reg_mode */
260 #define GET_LDST_SIZE(c) (c & 3)
262 /* Store (so the primary register is a source, not a destination */
263 #define LDST_STORE (1 << 2)
265 /* Mask has special meaning and should not be manipulated directly */
266 #define LDST_SPECIAL_MASK (1 << 3)
268 /* Non-store operation has side effects and should not be eliminated even if
270 #define LDST_SIDE_FX (1 << 4)
272 /* This file is common, so don't define the tables themselves. #include
273 * midgard_op.h if you need that, or edit midgard_ops.c directly */
275 /* Duplicate bits to convert a 4-bit writemask to duplicated 8-bit format,
276 * which is used for 32-bit vector units */
278 static inline unsigned
279 expand_writemask_32(unsigned mask
)
283 for (int i
= 0; i
< 4; ++i
)
290 /* Coerce structs to integer */
292 static inline unsigned
293 vector_alu_srco_unsigned(midgard_vector_alu_src src
)
296 memcpy(&u
, &src
, sizeof(src
));
300 static inline midgard_vector_alu_src
301 vector_alu_from_unsigned(unsigned u
)
303 midgard_vector_alu_src s
;
304 memcpy(&s
, &u
, sizeof(s
));
308 /* Composes two swizzles */
309 static inline unsigned
310 pan_compose_swizzle(unsigned left
, unsigned right
)
314 for (unsigned c
= 0; c
< 4; ++c
) {
315 unsigned s
= (left
>> (2*c
)) & 0x3;
316 unsigned q
= (right
>> (2*s
)) & 0x3;
324 /* Applies a swizzle to an ALU source */
326 static inline unsigned
327 vector_alu_apply_swizzle(unsigned src
, unsigned swizzle
)
329 midgard_vector_alu_src s
=
330 vector_alu_from_unsigned(src
);
332 s
.swizzle
= pan_compose_swizzle(s
.swizzle
, swizzle
);
334 return vector_alu_srco_unsigned(s
);
337 /* Checks for an xyzw.. swizzle, given a mask */
340 mir_is_simple_swizzle(unsigned swizzle
, unsigned mask
)
342 for (unsigned i
= 0; i
< 16; ++i
) {
343 if (!(mask
& (1 << i
))) continue;
345 if (((swizzle
>> (2 * i
)) & 0x3) != i
)
352 /* Packs a load/store argument */
354 static inline uint8_t
355 midgard_ldst_reg(unsigned reg
, unsigned component
)
357 assert((reg
== REGISTER_LDST_BASE
) || (reg
== REGISTER_LDST_BASE
+ 1));
359 midgard_ldst_register_select sel
= {
360 .component
= component
,
365 memcpy(&packed
, &sel
, sizeof(packed
));
370 /* Unpacks a load/store argument */
372 static inline midgard_ldst_register_select
373 midgard_ldst_select(uint8_t u
)
375 midgard_ldst_register_select sel
;
376 memcpy(&sel
, &u
, sizeof(u
));
380 static inline uint8_t
381 midgard_ldst_pack(midgard_ldst_register_select sel
)
384 memcpy(&packed
, &sel
, sizeof(packed
));
388 /* Gets a swizzle like yyyy and returns y */
390 static inline unsigned
391 swizzle_to_component(unsigned swizzle
)
393 unsigned c
= swizzle
& 3;
394 assert(((swizzle
>> 2) & 3) == c
);
395 assert(((swizzle
>> 4) & 3) == c
);
396 assert(((swizzle
>> 6) & 3) == c
);
401 static inline unsigned
402 component_to_swizzle(unsigned c
, unsigned count
)
406 return SWIZZLE(c
, c
, c
, c
);
408 return SWIZZLE(c
, c
+ 1, c
+ 1, c
+ 1);
410 return SWIZZLE(c
, c
+ 1, c
+ 2, c
+ 2);
412 return SWIZZLE(c
, c
+ 1, c
+ 2, c
+ 3);
414 unreachable("Invalid component count");