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_PROJECTION(op) ( \
34 op == midgard_op_ldst_perspective_division_z || \
35 op == midgard_op_ldst_perspective_division_w \
38 #define OP_IS_VEC4_ONLY(op) ( \
39 OP_IS_PROJECTION(op) || \
40 op == midgard_op_ld_cubemap_coords \
43 #define OP_IS_MOVE(op) ( \
44 op == midgard_alu_op_fmov || \
45 op == midgard_alu_op_imov \
48 #define OP_IS_UBO_READ(op) ( \
49 op == midgard_op_ld_ubo_char || \
50 op == midgard_op_ld_ubo_char2 || \
51 op == midgard_op_ld_ubo_char4 || \
52 op == midgard_op_ld_ubo_short4 || \
53 op == midgard_op_ld_ubo_int4 \
56 #define OP_IS_CSEL_V(op) ( \
57 op == midgard_alu_op_icsel_v || \
58 op == midgard_alu_op_fcsel_v \
61 #define OP_IS_CSEL(op) ( \
63 op == midgard_alu_op_icsel || \
64 op == midgard_alu_op_fcsel \
67 #define OP_IS_DERIVATIVE(op) ( \
68 op == TEXTURE_OP_DFDX || \
69 op == TEXTURE_OP_DFDY \
72 /* ALU control words are single bit fields with a lot of space */
74 #define ALU_ENAB_VEC_MUL (1 << 17)
75 #define ALU_ENAB_SCAL_ADD (1 << 19)
76 #define ALU_ENAB_VEC_ADD (1 << 21)
77 #define ALU_ENAB_SCAL_MUL (1 << 23)
78 #define ALU_ENAB_VEC_LUT (1 << 25)
79 #define ALU_ENAB_BR_COMPACT (1 << 26)
80 #define ALU_ENAB_BRANCH (1 << 27)
82 /* Other opcode properties that don't conflict with the ALU_ENABs, non-ISA */
84 /* Denotes an opcode that takes a vector input with a fixed-number of
85 * channels, but outputs to only a single output channel, like dot products.
86 * For these, to determine the effective mask, this quirk can be set. We have
87 * an intentional off-by-one (a la MALI_POSITIVE), since 0-channel makes no
88 * sense but we need to fit 4 channels in 2-bits. Similarly, 1-channel doesn't
89 * make sense (since then why are we quirked?), so that corresponds to "no
92 #define OP_CHANNEL_COUNT(c) ((c - 1) << 0)
93 #define GET_CHANNEL_COUNT(c) ((c & (0x3 << 0)) ? ((c & (0x3 << 0)) + 1) : 0)
95 /* For instructions that take a single argument, normally the first argument
96 * slot is used for the argument and the second slot is a dummy #0 constant.
97 * However, there are exceptions: instructions like fmov store their argument
98 * in the _second_ slot and store a dummy r24 in the first slot, designated by
99 * QUIRK_FLIPPED_R24 */
101 #define QUIRK_FLIPPED_R24 (1 << 2)
103 /* Is the op commutative? */
104 #define OP_COMMUTES (1 << 3)
106 /* Does the op convert types between int- and float- space (i2f/f2u/etc) */
107 #define OP_TYPE_CONVERT (1 << 4)
109 /* Vector-independant shorthands for the above; these numbers are arbitrary and
110 * not from the ISA. Convert to the above with unit_enum_to_midgard */
116 /* 4-bit type tags */
118 #define TAG_TEXTURE_4_VTX 0x2
119 #define TAG_TEXTURE_4 0x3
120 #define TAG_LOAD_STORE_4 0x5
121 #define TAG_ALU_4 0x8
122 #define TAG_ALU_8 0x9
123 #define TAG_ALU_12 0xA
124 #define TAG_ALU_16 0xB
127 quadword_size(int tag
)
131 case TAG_LOAD_STORE_4
:
133 case TAG_TEXTURE_4_VTX
:
142 unreachable("Unknown tag");
146 #define IS_ALU(tag) (tag == TAG_ALU_4 || tag == TAG_ALU_8 || \
147 tag == TAG_ALU_12 || tag == TAG_ALU_16)
149 /* Special register aliases */
151 #define MAX_WORK_REGISTERS 16
153 /* Uniforms are begin at (REGISTER_UNIFORMS - uniform_count) */
154 #define REGISTER_UNIFORMS 24
156 #define REGISTER_UNUSED 24
157 #define REGISTER_CONSTANT 26
158 #define REGISTER_LDST_BASE 26
159 #define REGISTER_TEXTURE_BASE 28
160 #define REGISTER_SELECT 31
162 /* SSA helper aliases to mimic the registers. */
164 #define SSA_UNUSED ~0
165 #define SSA_FIXED_SHIFT 24
166 #define SSA_FIXED_REGISTER(reg) (((1 + (reg)) << SSA_FIXED_SHIFT) | 1)
167 #define SSA_REG_FROM_FIXED(reg) ((((reg) & ~1) >> SSA_FIXED_SHIFT) - 1)
168 #define SSA_FIXED_MINIMUM SSA_FIXED_REGISTER(0)
170 #define COMPONENT_X 0x0
171 #define COMPONENT_Y 0x1
172 #define COMPONENT_Z 0x2
173 #define COMPONENT_W 0x3
175 #define SWIZZLE_IDENTITY { \
176 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, \
177 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, \
178 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } \
181 #define SWIZZLE_IDENTITY_4 { \
182 { 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
183 { 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
184 { 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
187 static inline unsigned
188 mask_of(unsigned nr_comp
)
190 return (1 << nr_comp
) - 1;
197 /* There are five ALU units: VMUL, VADD, SMUL, SADD, LUT. A given opcode is
198 * implemented on some subset of these units (or occassionally all of them).
199 * This table encodes a bit mask of valid units for each opcode, so the
200 * scheduler can figure where to plonk the instruction. */
202 /* Shorthands for each unit */
203 #define UNIT_VMUL ALU_ENAB_VEC_MUL
204 #define UNIT_SADD ALU_ENAB_SCAL_ADD
205 #define UNIT_VADD ALU_ENAB_VEC_ADD
206 #define UNIT_SMUL ALU_ENAB_SCAL_MUL
207 #define UNIT_VLUT ALU_ENAB_VEC_LUT
209 /* Shorthands for usual combinations of units */
211 #define UNITS_MUL (UNIT_VMUL | UNIT_SMUL)
212 #define UNITS_ADD (UNIT_VADD | UNIT_SADD)
213 #define UNITS_MOST (UNITS_MUL | UNITS_ADD)
214 #define UNITS_ALL (UNITS_MOST | UNIT_VLUT)
215 #define UNITS_SCALAR (UNIT_SADD | UNIT_SMUL)
216 #define UNITS_VECTOR (UNIT_VMUL | UNIT_VADD)
217 #define UNITS_ANY_VECTOR (UNITS_VECTOR | UNIT_VLUT)
219 struct mir_op_props
{
226 struct mir_ldst_op_props
{
231 /* Lower 2-bits are a midgard_reg_mode */
232 #define GET_LDST_SIZE(c) (c & 3)
234 /* Store (so the primary register is a source, not a destination */
235 #define LDST_STORE (1 << 2)
237 /* Mask has special meaning and should not be manipulated directly */
238 #define LDST_SPECIAL_MASK (1 << 3)
240 /* Non-store operation has side effects and should not be eliminated even if
242 #define LDST_SIDE_FX (1 << 4)
244 /* This file is common, so don't define the tables themselves. #include
245 * midgard_op.h if you need that, or edit midgard_ops.c directly */
247 /* Duplicate bits to convert a 4-bit writemask to duplicated 8-bit format,
248 * which is used for 32-bit vector units */
250 static inline unsigned
251 expand_writemask_32(unsigned mask
)
255 for (int i
= 0; i
< 4; ++i
)
262 /* Coerce structs to integer */
264 static inline unsigned
265 vector_alu_srco_unsigned(midgard_vector_alu_src src
)
268 memcpy(&u
, &src
, sizeof(src
));
272 static inline midgard_vector_alu_src
273 vector_alu_from_unsigned(unsigned u
)
275 midgard_vector_alu_src s
;
276 memcpy(&s
, &u
, sizeof(s
));
281 mir_compose_swizzle(unsigned *left
, unsigned *right
, unsigned *final_out
)
285 for (unsigned c
= 0; c
< 16; ++c
)
286 out
[c
] = right
[left
[c
]];
288 memcpy(final_out
, out
, sizeof(out
));
291 /* Checks for an xyzw.. swizzle, given a mask */
294 mir_is_simple_swizzle(unsigned *swizzle
, unsigned mask
)
296 for (unsigned i
= 0; i
< 16; ++i
) {
297 if (!(mask
& (1 << i
))) continue;
306 /* Packs a load/store argument */
308 static inline uint8_t
309 midgard_ldst_reg(unsigned reg
, unsigned component
)
311 assert((reg
== REGISTER_LDST_BASE
) || (reg
== REGISTER_LDST_BASE
+ 1));
313 midgard_ldst_register_select sel
= {
314 .component
= component
,
319 memcpy(&packed
, &sel
, sizeof(packed
));
324 /* Unpacks a load/store argument */
326 static inline midgard_ldst_register_select
327 midgard_ldst_select(uint8_t u
)
329 midgard_ldst_register_select sel
;
330 memcpy(&sel
, &u
, sizeof(u
));
334 static inline uint8_t
335 midgard_ldst_pack(midgard_ldst_register_select sel
)
338 memcpy(&packed
, &sel
, sizeof(packed
));