2 * Copyright (C) 2018-2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #include "midgard_ops.h"
26 #include "util/u_memory.h"
28 /* Create a mask of accessed components from a swizzle to figure out vector
32 swizzle_to_access_mask(unsigned swizzle
)
34 unsigned component_mask
= 0;
36 for (int i
= 0; i
< 4; ++i
) {
37 unsigned c
= (swizzle
>> (2 * i
)) & 3;
38 component_mask
|= (1 << c
);
41 return component_mask
;
44 /* Does the mask cover more than a scalar? */
47 is_single_component_mask(unsigned mask
)
51 for (int c
= 0; c
< 4; ++c
)
52 if (mask
& (3 << (2 * c
)))
55 return components
== 1;
58 /* Checks for an SSA data hazard between two adjacent instructions, keeping in
59 * mind that we are a vector architecture and we can write to different
60 * components simultaneously */
63 can_run_concurrent_ssa(midgard_instruction
*first
, midgard_instruction
*second
)
65 /* Each instruction reads some registers and writes to a register. See
66 * where the first writes */
68 /* Figure out where exactly we wrote to */
69 int source
= first
->ssa_args
.dest
;
70 int source_mask
= first
->type
== TAG_ALU_4
? squeeze_writemask(first
->alu
.mask
) : 0xF;
72 /* As long as the second doesn't read from the first, we're okay */
73 if (second
->ssa_args
.src0
== source
) {
74 if (first
->type
== TAG_ALU_4
) {
75 /* Figure out which components we just read from */
77 int q
= second
->alu
.src1
;
78 midgard_vector_alu_src
*m
= (midgard_vector_alu_src
*) &q
;
80 /* Check if there are components in common, and fail if so */
81 if (swizzle_to_access_mask(m
->swizzle
) & source_mask
)
88 if (second
->ssa_args
.src1
== source
)
91 /* Otherwise, it's safe in that regard. Another data hazard is both
92 * writing to the same place, of course */
94 if (second
->ssa_args
.dest
== source
) {
95 /* ...but only if the components overlap */
96 int dest_mask
= second
->type
== TAG_ALU_4
? squeeze_writemask(second
->alu
.mask
) : 0xF;
98 if (dest_mask
& source_mask
)
108 midgard_instruction
**segment
, unsigned segment_size
,
109 midgard_instruction
*ains
)
111 for (int s
= 0; s
< segment_size
; ++s
)
112 if (!can_run_concurrent_ssa(segment
[s
], ains
))
120 /* Schedules, but does not emit, a single basic block. After scheduling, the
121 * final tag and size of the block are known, which are necessary for branching
124 static midgard_bundle
125 schedule_bundle(compiler_context
*ctx
, midgard_block
*block
, midgard_instruction
*ins
, int *skip
)
127 int instructions_emitted
= 0, packed_idx
= 0;
128 midgard_bundle bundle
= { 0 };
130 uint8_t tag
= ins
->type
;
132 /* Default to the instruction's tag */
137 uint32_t control
= 0;
138 size_t bytes_emitted
= sizeof(control
);
140 /* TODO: Constant combining */
141 int index
= 0, last_unit
= 0;
143 /* Previous instructions, for the purpose of parallelism */
144 midgard_instruction
*segment
[4] = {0};
145 int segment_size
= 0;
147 instructions_emitted
= -1;
148 midgard_instruction
*pins
= ins
;
150 unsigned constant_count
= 0;
153 midgard_instruction
*ains
= pins
;
155 /* Advance instruction pointer */
157 ains
= mir_next_op(pins
);
161 /* Out-of-work condition */
162 if ((struct list_head
*) ains
== &block
->instructions
)
165 /* Ensure that the chain can continue */
166 if (ains
->type
!= TAG_ALU_4
) break;
168 /* If there's already something in the bundle and we
169 * have weird scheduler constraints, break now */
170 if (ains
->precede_break
&& index
) break;
172 /* According to the presentation "The ARM
173 * Mali-T880 Mobile GPU" from HotChips 27,
174 * there are two pipeline stages. Branching
175 * position determined experimentally. Lines
176 * are executed in parallel:
179 * [ VADD ] [ SMUL ] [ LUT ] [ BRANCH ]
181 * Verify that there are no ordering dependencies here.
183 * TODO: Allow for parallelism!!!
186 /* Pick a unit for it if it doesn't force a particular unit */
188 int unit
= ains
->unit
;
191 int op
= ains
->alu
.op
;
192 int units
= alu_opcode_props
[op
].props
;
194 bool vectorable
= units
& UNITS_ANY_VECTOR
;
195 bool scalarable
= units
& UNITS_SCALAR
;
196 bool could_scalar
= is_single_component_mask(ains
->alu
.mask
);
197 bool vector
= vectorable
&& !(could_scalar
&& scalarable
);
200 assert(units
& UNITS_SCALAR
);
203 if (last_unit
>= UNIT_VADD
) {
204 if (units
& UNIT_VLUT
)
209 if ((units
& UNIT_VMUL
) && !(control
& UNIT_VMUL
))
211 else if ((units
& UNIT_VADD
) && !(control
& UNIT_VADD
))
213 else if (units
& UNIT_VLUT
)
219 if (last_unit
>= UNIT_VADD
) {
220 if ((units
& UNIT_SMUL
) && !(control
& UNIT_SMUL
))
222 else if (units
& UNIT_VLUT
)
227 if ((units
& UNIT_SADD
) && !(control
& UNIT_SADD
) && !midgard_has_hazard(segment
, segment_size
, ains
))
229 else if (units
& UNIT_SMUL
)
230 unit
= ((units
& UNIT_VMUL
) && !(control
& UNIT_VMUL
)) ? UNIT_VMUL
: UNIT_SMUL
;
231 else if ((units
& UNIT_VADD
) && !(control
& UNIT_VADD
))
238 assert(unit
& units
);
241 /* Late unit check, this time for encoding (not parallelism) */
242 if (unit
<= last_unit
) break;
244 /* Clear the segment */
245 if (last_unit
< UNIT_VADD
&& unit
>= UNIT_VADD
)
248 if (midgard_has_hazard(segment
, segment_size
, ains
))
251 /* We're good to go -- emit the instruction */
254 segment
[segment_size
++] = ains
;
256 /* We try to reuse constants if possible, by adjusting
259 if (ains
->has_blend_constant
) {
260 /* Everything conflicts with the blend constant */
261 if (bundle
.has_embedded_constants
)
264 bundle
.has_blend_constant
= 1;
265 bundle
.has_embedded_constants
= 1;
266 } else if (ains
->has_constants
) {
267 /* By definition, blend constants conflict with
268 * everything, so if there are already
269 * constants we break the bundle *now* */
271 if (bundle
.has_blend_constant
)
274 /* For anything but blend constants, we can do
275 * proper analysis, however */
277 /* TODO: Mask by which are used */
278 uint32_t *constants
= (uint32_t *) ains
->constants
;
279 uint32_t *bundles
= (uint32_t *) bundle
.constants
;
281 uint32_t indices
[4] = { 0 };
282 bool break_bundle
= false;
284 for (unsigned i
= 0; i
< 4; ++i
) {
285 uint32_t cons
= constants
[i
];
286 bool constant_found
= false;
288 /* Search for the constant */
289 for (unsigned j
= 0; j
< constant_count
; ++j
) {
290 if (bundles
[j
] != cons
)
293 /* We found it, reuse */
295 constant_found
= true;
302 /* We didn't find it, so allocate it */
303 unsigned idx
= constant_count
++;
306 /* Uh-oh, out of space */
311 /* We have space, copy it in! */
319 /* Cool, we have it in. So use indices as a
322 unsigned swizzle
= SWIZZLE_FROM_ARRAY(indices
);
323 unsigned r_constant
= SSA_FIXED_REGISTER(REGISTER_CONSTANT
);
325 if (ains
->ssa_args
.src0
== r_constant
)
326 ains
->alu
.src1
= vector_alu_apply_swizzle(ains
->alu
.src1
, swizzle
);
328 if (ains
->ssa_args
.src1
== r_constant
)
329 ains
->alu
.src2
= vector_alu_apply_swizzle(ains
->alu
.src2
, swizzle
);
331 bundle
.has_embedded_constants
= true;
334 if (ains
->unit
& UNITS_ANY_VECTOR
) {
335 bytes_emitted
+= sizeof(midgard_reg_info
);
336 bytes_emitted
+= sizeof(midgard_vector_alu
);
337 } else if (ains
->compact_branch
) {
338 /* All of r0 has to be written out along with
339 * the branch writeout */
341 if (ains
->writeout
) {
342 /* The rules for when "bare" writeout
343 * is safe are when all components are
344 * r0 are written out in the final
345 * bundle, earlier than VLUT, where any
346 * register dependencies of r0 are from
347 * an earlier bundle. We can't verify
348 * this before RA, so we don't try. */
354 midgard_instruction ins
= v_mov(0, blank_alu_src
, SSA_FIXED_REGISTER(0));
355 ins
.unit
= UNIT_VMUL
;
358 /* TODO don't leak */
359 midgard_instruction
*move
=
360 mem_dup(&ins
, sizeof(midgard_instruction
));
361 bytes_emitted
+= sizeof(midgard_reg_info
);
362 bytes_emitted
+= sizeof(midgard_vector_alu
);
363 bundle
.instructions
[packed_idx
++] = move
;
366 if (ains
->unit
== ALU_ENAB_BRANCH
) {
367 bytes_emitted
+= sizeof(midgard_branch_extended
);
369 bytes_emitted
+= sizeof(ains
->br_compact
);
372 bytes_emitted
+= sizeof(midgard_reg_info
);
373 bytes_emitted
+= sizeof(midgard_scalar_alu
);
376 /* Defer marking until after writing to allow for break */
377 control
|= ains
->unit
;
378 last_unit
= ains
->unit
;
379 ++instructions_emitted
;
385 /* Pad ALU op to nearest word */
387 if (bytes_emitted
& 15) {
388 padding
= 16 - (bytes_emitted
& 15);
389 bytes_emitted
+= padding
;
392 /* Constants must always be quadwords */
393 if (bundle
.has_embedded_constants
)
396 /* Size ALU instruction for tag */
397 bundle
.tag
= (TAG_ALU_4
) + (bytes_emitted
/ 16) - 1;
398 bundle
.padding
= padding
;
399 bundle
.control
= bundle
.tag
| control
;
404 case TAG_LOAD_STORE_4
: {
405 /* Load store instructions have two words at once. If
406 * we only have one queued up, we need to NOP pad.
407 * Otherwise, we store both in succession to save space
408 * and cycles -- letting them go in parallel -- skip
409 * the next. The usefulness of this optimisation is
410 * greatly dependent on the quality of the instruction
414 midgard_instruction
*next_op
= mir_next_op(ins
);
416 if ((struct list_head
*) next_op
!= &block
->instructions
&& next_op
->type
== TAG_LOAD_STORE_4
) {
417 /* TODO: Concurrency check */
418 instructions_emitted
++;
424 case TAG_TEXTURE_4
: {
425 /* Which tag we use depends on the shader stage */
426 bool in_frag
= ctx
->stage
== MESA_SHADER_FRAGMENT
;
427 bundle
.tag
= in_frag
? TAG_TEXTURE_4
: TAG_TEXTURE_4_VTX
;
432 unreachable("Unknown tag");
436 /* Copy the instructions into the bundle */
437 bundle
.instruction_count
= instructions_emitted
+ 1 + packed_idx
;
439 midgard_instruction
*uins
= ins
;
440 for (; packed_idx
< bundle
.instruction_count
; ++packed_idx
) {
441 bundle
.instructions
[packed_idx
] = uins
;
442 uins
= mir_next_op(uins
);
445 *skip
= instructions_emitted
;
450 /* Schedule a single block by iterating its instruction to create bundles.
451 * While we go, tally about the bundle sizes to compute the block size. */
454 schedule_block(compiler_context
*ctx
, midgard_block
*block
)
456 util_dynarray_init(&block
->bundles
, NULL
);
458 block
->quadword_count
= 0;
460 mir_foreach_instr_in_block(block
, ins
) {
462 midgard_bundle bundle
= schedule_bundle(ctx
, block
, ins
, &skip
);
463 util_dynarray_append(&block
->bundles
, midgard_bundle
, bundle
);
465 if (bundle
.has_blend_constant
) {
466 /* TODO: Multiblock? */
467 int quadwords_within_block
= block
->quadword_count
+ quadword_size(bundle
.tag
) - 1;
468 ctx
->blend_constant_offset
= quadwords_within_block
* 0x10;
472 ins
= mir_next_op(ins
);
474 block
->quadword_count
+= quadword_size(bundle
.tag
);
477 block
->is_scheduled
= true;
481 schedule_program(compiler_context
*ctx
)
483 /* We run RA prior to scheduling */
485 mir_foreach_block(ctx
, block
) {
486 schedule_block(ctx
, block
);
489 /* Pipeline registers creation is a prepass before RA */
490 mir_create_pipeline_registers(ctx
);
492 struct ra_graph
*g
= allocate_registers(ctx
);
493 install_registers(ctx
, g
);