aboutsummaryrefslogtreecommitdiffstats
path: root/src/panfrost/midgard/midgard_ra_pipeline.c
blob: afbcf5b64a012460d935a5f58b46375a8efff79b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright (C) 2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
 * Copyright (C) 2019 Collabora, Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "compiler.h"

/* Creates pipeline registers. This is a prepass run before the main register
 * allocator but after scheduling, once bundles are created. It works by
 * iterating the scheduled IR, checking if a value is ever used after the end
 * of the current bundle. If it is not, it is promoted to a bundle-specific
 * pipeline register.
 *
 * Pipeline registers are only written from the first two stages of the
 * pipeline (vmul/sadd) lasting the duration of the bundle only. There are two
 * 128-bit pipeline registers available (r24/r25). The upshot is that no actual
 * register allocation is needed; we can _always_ promote a value to a pipeline
 * register, liveness permitting. This greatly simplifies the logic of this
 * passing, negating the need for a proper RA like work registers.
 */

static bool
mir_pipeline_ins(
        compiler_context *ctx,
        midgard_block *block,
        midgard_bundle *bundle, unsigned i,
        unsigned pipeline_count)
{
        midgard_instruction *ins = bundle->instructions[i];
        unsigned dest = ins->dest;

        /* We could be pipelining a register, so we need to make sure that all
         * of the components read in this bundle are written in this bundle,
         * and that no components are written before this bundle */

        unsigned node = ins->dest;
        unsigned read_mask = 0;

        /* Analyze the bundle for a read mask */

        for (unsigned i = 0; i < bundle->instruction_count; ++i) {
                midgard_instruction *q = bundle->instructions[i];
                read_mask |= mir_mask_of_read_components(q, node);

                /* The fragment colour can't be pipelined (well, it is
                 * pipelined in r0, but this is a delicate dance with
                 * scheduling and RA, not for us to worry about) */

                if (q->compact_branch && q->writeout && mir_has_arg(q, node))
                        return false;
        }

        /* Now analyze for a write mask */
        for (unsigned i = 0; i < bundle->instruction_count; ++i) {
                midgard_instruction *q = bundle->instructions[i];
                if (q->dest != node) continue;

                /* Remove the written mask from the read requirements */
                read_mask &= ~q->mask;
        }

        /* Check for leftovers */
        if (read_mask)
                return false;

        /* Now, check outside the bundle */
        midgard_instruction *start = bundle->instructions[0];

        if (mir_is_written_before(ctx, start, node))
                return false;

        /* We want to know if we live after this bundle, so check if
         * we're live after the last instruction of the bundle */

        midgard_instruction *end = bundle->instructions[
                                    bundle->instruction_count - 1];

        if (mir_is_live_after(ctx, block, end, ins->dest))
                return false;

        /* We're only live in this bundle -- pipeline! */

        mir_rewrite_index(ctx, dest, SSA_FIXED_REGISTER(24 + pipeline_count));

        return true;
}

void
mir_create_pipeline_registers(compiler_context *ctx)
{
        mir_foreach_block(ctx, block) {
                mir_foreach_bundle_in_block(block, bundle) {
                        if (!mir_is_alu_bundle(bundle)) continue;
                        if (bundle->instruction_count < 2) continue;

                        /* Only first 2 instructions could pipeline */
                        bool succ = mir_pipeline_ins(ctx, block, bundle, 0, 0);
                        mir_pipeline_ins(ctx, block, bundle, 1, succ);
                }
        }
}