| 1 |
/* |
| 2 |
* Copyright (C) 2019 Alyssa Rosenzweig |
| 3 |
* Copyright (C) 2019 Collabora, Ltd. |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "compiler.h" |
| 8 |
|
| 9 |
/* Creates pipeline registers. This is a prepass run before the main register |
| 10 |
* allocator but after scheduling, once bundles are created. It works by |
| 11 |
* iterating the scheduled IR, checking if a value is ever used after the end |
| 12 |
* of the current bundle. If it is not, it is promoted to a bundle-specific |
| 13 |
* pipeline register. |
| 14 |
* |
| 15 |
* Pipeline registers are only written from the first two stages of the |
| 16 |
* pipeline (vmul/sadd) lasting the duration of the bundle only. There are two |
| 17 |
* 128-bit pipeline registers available (r24/r25). The upshot is that no actual |
| 18 |
* register allocation is needed; we can _always_ promote a value to a pipeline |
| 19 |
* register, liveness permitting. This greatly simplifies the logic of this |
| 20 |
* passing, negating the need for a proper RA like work registers. |
| 21 |
*/ |
| 22 |
|
| 23 |
static bool |
| 24 |
mir_pipeline_ins(compiler_context *ctx, midgard_block *block, |
| 25 |
midgard_bundle *bundle, unsigned i, unsigned pipeline_count) |
| 26 |
{ |
| 27 |
midgard_instruction *ins = bundle->instructions[i]; |
| 28 |
|
| 29 |
/* Our goal is to create a pipeline register. Pipeline registers are |
| 30 |
* created at the start of the bundle and are destroyed at the end. So |
| 31 |
* we conservatively require: |
| 32 |
* |
| 33 |
* 1. Each component read in the second stage is written in the first stage. |
| 34 |
* 2. The index is not live after the bundle. |
| 35 |
* 3. We're not a special index (writeout, conditionals, ..) |
| 36 |
* |
| 37 |
* Rationale: #1 ensures that there is no need to go before the |
| 38 |
* creation of the bundle, so the pipeline register can exist. #2 is |
| 39 |
* since the pipeline register will be destroyed at the end. This |
| 40 |
* ensures that nothing will try to read/write the pipeline register |
| 41 |
* once it is not live, and that there's no need to go earlier. */ |
| 42 |
|
| 43 |
unsigned node = ins->dest; |
| 44 |
unsigned read_mask = 0; |
| 45 |
|
| 46 |
if (node >= SSA_FIXED_MINIMUM) |
| 47 |
return false; |
| 48 |
|
| 49 |
if (node == ctx->blend_src1) |
| 50 |
return false; |
| 51 |
|
| 52 |
/* Analyze the bundle for a per-byte read mask */ |
| 53 |
|
| 54 |
for (unsigned j = 0; j < bundle->instruction_count; ++j) { |
| 55 |
midgard_instruction *q = bundle->instructions[j]; |
| 56 |
|
| 57 |
/* The fragment colour can't be pipelined (well, it is |
| 58 |
* pipelined in r0, but this is a delicate dance with |
| 59 |
* scheduling and RA, not for us to worry about) */ |
| 60 |
|
| 61 |
if (q->compact_branch && q->writeout && mir_has_arg(q, node)) |
| 62 |
return false; |
| 63 |
|
| 64 |
if (q->unit < UNIT_VADD) |
| 65 |
continue; |
| 66 |
read_mask |= mir_bytemask_of_read_components(q, node); |
| 67 |
} |
| 68 |
|
| 69 |
/* Now check what's written in the beginning stage */ |
| 70 |
for (unsigned j = 0; j < bundle->instruction_count; ++j) { |
| 71 |
midgard_instruction *q = bundle->instructions[j]; |
| 72 |
if (q->unit >= UNIT_VADD) |
| 73 |
break; |
| 74 |
if (q->dest != node) |
| 75 |
continue; |
| 76 |
|
| 77 |
/* Remove the written mask from the read requirements */ |
| 78 |
read_mask &= ~mir_bytemask(q); |
| 79 |
} |
| 80 |
|
| 81 |
/* Check for leftovers */ |
| 82 |
if (read_mask) |
| 83 |
return false; |
| 84 |
|
| 85 |
/* We want to know if we live after this bundle, so check if |
| 86 |
* we're live after the last instruction of the bundle */ |
| 87 |
|
| 88 |
midgard_instruction *end = |
| 89 |
bundle->instructions[bundle->instruction_count - 1]; |
| 90 |
|
| 91 |
if (mir_is_live_after(ctx, block, end, ins->dest)) |
| 92 |
return false; |
| 93 |
|
| 94 |
/* We're only live in this bundle -- pipeline! */ |
| 95 |
unsigned preg = SSA_FIXED_REGISTER(24 + pipeline_count); |
| 96 |
|
| 97 |
for (unsigned j = 0; j < bundle->instruction_count; ++j) { |
| 98 |
midgard_instruction *q = bundle->instructions[j]; |
| 99 |
|
| 100 |
if (q->unit >= UNIT_VADD) |
| 101 |
mir_rewrite_index_src_single(q, node, preg); |
| 102 |
else |
| 103 |
mir_rewrite_index_dst_single(q, node, preg); |
| 104 |
} |
| 105 |
|
| 106 |
return true; |
| 107 |
} |
| 108 |
|
| 109 |
void |
| 110 |
mir_create_pipeline_registers(compiler_context *ctx) |
| 111 |
{ |
| 112 |
mir_invalidate_liveness(ctx); |
| 113 |
|
| 114 |
mir_foreach_block(ctx, _block) { |
| 115 |
midgard_block *block = (midgard_block *)_block; |
| 116 |
|
| 117 |
mir_foreach_bundle_in_block(block, bundle) { |
| 118 |
if (!mir_is_alu_bundle(bundle)) |
| 119 |
continue; |
| 120 |
if (bundle->instruction_count < 2) |
| 121 |
continue; |
| 122 |
|
| 123 |
/* Only first 2 instructions could pipeline */ |
| 124 |
bool succ = mir_pipeline_ins(ctx, block, bundle, 0, 0); |
| 125 |
mir_pipeline_ins(ctx, block, bundle, 1, succ); |
| 126 |
} |
| 127 |
} |
| 128 |
} |