vc4_qir_lower_uniforms.c 7.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * 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.
 */

/**
25
 * @file vc4_qir_lower_uniforms.c
26
 *
27 28
 * This is the pre-code-generation pass for fixing up instructions that try to
 * read from multiple uniform values.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
 */

#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;
51
        void *key = (void *)(uintptr_t)(reg.index + 1);
52 53 54 55 56 57 58 59 60 61 62 63 64

        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;
65
        void *key = (void *)(uintptr_t)(reg.index + 1);
66 67 68

        entry = _mesa_hash_table_search(ht, key);
        assert(entry);
69
        entry->data = (void *)(((uintptr_t) entry->data) - 1);
70 71 72 73 74 75 76 77 78 79
        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))
80
                return i != qir_get_tex_uniform_src(inst);
81 82 83
        return true;
}

84 85 86 87 88 89 90 91
/* 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;

92
        for (int i = 0; i < qir_get_nsrc(inst); i++) {
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
                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;
}

111 112 113 114 115 116 117 118 119 120
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.
         */
121
        qir_for_each_inst_inorder(inst, c) {
122
                uint32_t nsrc = qir_get_nsrc(inst);
123

124
                if (qir_get_instruction_uniform_count(inst) <= 1)
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
                        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;
                hash_table_foreach(ht, entry) {
                        uint32_t count = (uintptr_t)entry->data;
141
                        uint32_t index = (uintptr_t)entry->key - 1;
142 143 144 145 146 147
                        if (count > max_count) {
                                max_count = count;
                                max_index = index;
                        }
                }

148 149
                struct qreg unif = qir_reg(QFILE_UNIF, max_index);

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

                        qir_for_each_inst(inst, block) {
157
                                uint32_t nsrc = qir_get_nsrc(inst);
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

                                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;
176 177
                                }

178
                                bool removed = false;
179
                                for (int i = 0; i < nsrc; i++) {
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
                                        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]);
                                        }
198 199 200 201 202 203 204
                                }
                        }
                }
        }

        _mesa_hash_table_destroy(ht, NULL);
}