summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/vc4/vc4_qir_lower_uniforms.c
blob: ad19f06d31b98b3ae3134cb603455792ac4e83e5 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Copyright © 2014 Broadcom
 *
 * 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.
 */

/**
 * @file vc4_qir_lower_uniforms.c
 *
 * This is the pre-code-generation pass for fixing up instructions that try to
 * read from multiple uniform values.
 */

#include "vc4_qir.h"
#include "util/hash_table.h"
#include "util/u_math.h"

static inline uint32_t
index_hash(const void *key)
{
        return (uintptr_t)key;
}

static inline bool
index_compare(const void *a, const void *b)
{
        return a == b;
}

static void
add_uniform(struct hash_table *ht, struct qreg reg)
{
        struct hash_entry *entry;
        void *key = (void *)(uintptr_t)(reg.index + 1);

        entry = _mesa_hash_table_search(ht, key);
        if (entry) {
                entry->data++;
        } else {
                _mesa_hash_table_insert(ht, key, (void *)(uintptr_t)1);
        }
}

static void
remove_uniform(struct hash_table *ht, struct qreg reg)
{
        struct hash_entry *entry;
        void *key = (void *)(uintptr_t)(reg.index + 1);

        entry = _mesa_hash_table_search(ht, key);
        assert(entry);
        entry->data = (void *)(((uintptr_t) entry->data) - 1);
        if (entry->data == NULL)
                _mesa_hash_table_remove(ht, entry);
}

static bool
is_lowerable_uniform(struct qinst *inst, int i)
{
        if (inst->src[i].file != QFILE_UNIF)
                return false;
        if (qir_is_tex(inst))
                return i != qir_get_tex_uniform_src(inst);
        return true;
}

/* Returns the number of different uniform values referenced by the
 * instruction.
 */
static uint32_t
qir_get_instruction_uniform_count(struct qinst *inst)
{
        uint32_t count = 0;

        for (int i = 0; i < qir_get_nsrc(inst); i++) {
                if (inst->src[i].file != QFILE_UNIF)
                        continue;

                bool is_duplicate = false;
                for (int j = 0; j < i; j++) {
                        if (inst->src[j].file == QFILE_UNIF &&
                            inst->src[j].index == inst->src[i].index) {
                                is_duplicate = true;
                                break;
                        }
                }
                if (!is_duplicate)
                        count++;
        }

        return count;
}

void
qir_lower_uniforms(struct vc4_compile *c)
{
        struct hash_table *ht =
                _mesa_hash_table_create(c, index_hash, index_compare);

        /* Walk the instruction list, finding which instructions have more
         * than one uniform referenced, and add those uniform values to the
         * ht.
         */
        qir_for_each_inst_inorder(inst, c) {
                uint32_t nsrc = qir_get_nsrc(inst);

                if (qir_get_instruction_uniform_count(inst) <= 1)
                        continue;

                for (int i = 0; i < nsrc; i++) {
                        if (is_lowerable_uniform(inst, i))
                                add_uniform(ht, inst->src[i]);
                }
        }

        while (ht->entries) {
                /* Find the most commonly used uniform in instructions that
                 * need a uniform lowered.
                 */
                uint32_t max_count = 0;
                uint32_t max_index = 0;
                struct hash_entry *entry;
                hash_table_foreach(ht, entry) {
                        uint32_t count = (uintptr_t)entry->data;
                        uint32_t index = (uintptr_t)entry->key - 1;
                        if (count > max_count) {
                                max_count = count;
                                max_index = index;
                        }
                }

                struct qreg unif = qir_reg(QFILE_UNIF, max_index);

                /* Now, find the instructions using this uniform and make them
                 * reference a temp instead.
                 */
                qir_for_each_block(block, c) {
                        struct qinst *mov = NULL;

                        qir_for_each_inst(inst, block) {
                                uint32_t nsrc = qir_get_nsrc(inst);

                                uint32_t count = qir_get_instruction_uniform_count(inst);

                                if (count <= 1)
                                        continue;

                                /* If the block doesn't have a load of hte
                                 * uniform yet, add it.  We could potentially
                                 * do better and CSE MOVs from multiple blocks
                                 * into dominating blocks, except that may
                                 * cause troubles for register allocation.
                                 */
                                if (!mov) {
                                        mov = qir_inst(QOP_MOV, qir_get_temp(c),
                                                       unif, c->undef);
                                        list_add(&mov->link,
                                                 &block->instructions);
                                        c->defs[mov->dst.index] = mov;
                                }

                                bool removed = false;
                                for (int i = 0; i < nsrc; i++) {
                                        if (is_lowerable_uniform(inst, i) &&
                                            inst->src[i].index == max_index) {
                                                inst->src[i] = mov->dst;
                                                remove_uniform(ht, unif);
                                                removed = true;
                                        }
                                }
                                if (removed)
                                        count--;

                                /* If the instruction doesn't need lowering any more,
                                 * then drop it from the list.
                                 */
                                if (count <= 1) {
                                        for (int i = 0; i < nsrc; i++) {
                                                if (is_lowerable_uniform(inst, i))
                                                        remove_uniform(ht, inst->src[i]);
                                        }
                                }
                        }
                }
        }

        _mesa_hash_table_destroy(ht, NULL);
}