vc4: Split optimizing VPM writes from VPM reads.
authorEric Anholt <eric@anholt.net>
Tue, 15 Nov 2016 22:24:25 +0000 (14:24 -0800)
committerEric Anholt <eric@anholt.net>
Tue, 29 Nov 2016 16:38:59 +0000 (08:38 -0800)
The VPM write logic will be basically the same as the texture coordinate
write logic we need, and it's not really related to the VPM read logic
other than the reuse of the use_count array.

src/gallium/drivers/vc4/Makefile.sources
src/gallium/drivers/vc4/vc4_opt_coalesce_ff_writes.c [new file with mode: 0644]
src/gallium/drivers/vc4/vc4_opt_vpm.c
src/gallium/drivers/vc4/vc4_qir.c
src/gallium/drivers/vc4/vc4_qir.h

index e1496d1012af69c131ffd65ee1674d1aaa6a04f5..f09dac24848c3d95c0c80e522fed0001f415ba9d 100644 (file)
@@ -28,6 +28,7 @@ C_SOURCES := \
        vc4_opt_peephole_sf.c \
        vc4_opt_small_immediates.c \
        vc4_opt_vpm.c \
+       vc4_opt_coalesce_ff_writes.c \
        vc4_program.c \
        vc4_qir.c \
        vc4_qir_emit_uniform_stream_resets.c \
diff --git a/src/gallium/drivers/vc4/vc4_opt_coalesce_ff_writes.c b/src/gallium/drivers/vc4/vc4_opt_coalesce_ff_writes.c
new file mode 100644 (file)
index 0000000..4e5b119
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * 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_opt_coalesce_ff_writes.c
+ *
+ * This modifies instructions that generate the value consumed by a VPM write
+ * to write directly into the VPM.
+ */
+
+#include "vc4_qir.h"
+
+bool
+qir_opt_coalesce_ff_writes(struct vc4_compile *c)
+{
+        if (c->stage == QSTAGE_FRAG)
+                return false;
+
+        /* For now, only do this pass when we don't have control flow. */
+        struct qblock *block = qir_entry_block(c);
+        if (block != qir_exit_block(c))
+                return false;
+
+        bool progress = false;
+        struct qinst *vpm_writes[64] = { 0 };
+        uint32_t use_count[c->num_temps];
+        uint32_t vpm_write_count = 0;
+        memset(&use_count, 0, sizeof(use_count));
+
+        qir_for_each_inst_inorder(inst, c) {
+                switch (inst->dst.file) {
+                case QFILE_VPM:
+                        vpm_writes[vpm_write_count++] = inst;
+                        break;
+                default:
+                        break;
+                }
+
+                for (int i = 0; i < qir_get_nsrc(inst); i++) {
+                        if (inst->src[i].file == QFILE_TEMP) {
+                                uint32_t temp = inst->src[i].index;
+                                use_count[temp]++;
+                        }
+                }
+        }
+
+        for (int i = 0; i < vpm_write_count; i++) {
+                if (!qir_is_raw_mov(vpm_writes[i]) ||
+                    vpm_writes[i]->src[0].file != QFILE_TEMP) {
+                        continue;
+                }
+
+                uint32_t temp = vpm_writes[i]->src[0].index;
+                if (use_count[temp] != 1)
+                        continue;
+
+                struct qinst *inst = c->defs[temp];
+                if (!inst)
+                        continue;
+
+                if (qir_depends_on_flags(inst) || inst->sf)
+                        continue;
+
+                if (qir_has_side_effects(c, inst) ||
+                    qir_has_side_effect_reads(c, inst)) {
+                        continue;
+                }
+
+                /* Move the generating instruction to the end of the program
+                 * to maintain the order of the VPM writes.
+                 */
+                assert(!vpm_writes[i]->sf);
+                list_del(&inst->link);
+                list_addtail(&inst->link, &vpm_writes[i]->link);
+                qir_remove_instruction(c, vpm_writes[i]);
+
+                c->defs[inst->dst.index] = NULL;
+                inst->dst.file = QFILE_VPM;
+                inst->dst.index = 0;
+
+                progress = true;
+        }
+
+        return progress;
+}
index b3bef27225423a968145b3d0a29d81ed18f48a51..6f196e7d1b9922cfda0efa1393756a5e15326029 100644 (file)
 /**
  * @file vc4_opt_vpm.c
  *
- * This modifies instructions that:
- * 1. exclusively consume a value read from the VPM to directly read the VPM if
- *    other operands allow it.
- * 2. generate the value consumed by a VPM write to write directly into the VPM.
+ * This modifies instructions that exclusively consume a value read from the
+ * VPM to directly read the VPM if other operands allow it.
  */
 
 #include "vc4_qir.h"
@@ -44,20 +42,10 @@ qir_opt_vpm(struct vc4_compile *c)
                 return false;
 
         bool progress = false;
-        struct qinst *vpm_writes[64] = { 0 };
         uint32_t use_count[c->num_temps];
-        uint32_t vpm_write_count = 0;
         memset(&use_count, 0, sizeof(use_count));
 
         qir_for_each_inst_inorder(inst, c) {
-                switch (inst->dst.file) {
-                case QFILE_VPM:
-                        vpm_writes[vpm_write_count++] = inst;
-                        break;
-                default:
-                        break;
-                }
-
                 for (int i = 0; i < qir_get_nsrc(inst); i++) {
                         if (inst->src[i].file == QFILE_TEMP) {
                                 uint32_t temp = inst->src[i].index;
@@ -127,42 +115,5 @@ qir_opt_vpm(struct vc4_compile *c)
                 }
         }
 
-        for (int i = 0; i < vpm_write_count; i++) {
-                if (!qir_is_raw_mov(vpm_writes[i]) ||
-                    vpm_writes[i]->src[0].file != QFILE_TEMP) {
-                        continue;
-                }
-
-                uint32_t temp = vpm_writes[i]->src[0].index;
-                if (use_count[temp] != 1)
-                        continue;
-
-                struct qinst *inst = c->defs[temp];
-                if (!inst)
-                        continue;
-
-                if (qir_depends_on_flags(inst) || inst->sf)
-                        continue;
-
-                if (qir_has_side_effects(c, inst) ||
-                    qir_has_side_effect_reads(c, inst)) {
-                        continue;
-                }
-
-                /* Move the generating instruction to the end of the program
-                 * to maintain the order of the VPM writes.
-                 */
-                assert(!vpm_writes[i]->sf);
-                list_del(&inst->link);
-                list_addtail(&inst->link, &vpm_writes[i]->link);
-                qir_remove_instruction(c, vpm_writes[i]);
-
-                c->defs[inst->dst.index] = NULL;
-                inst->dst.file = QFILE_VPM;
-                inst->dst.index = 0;
-
-                progress = true;
-        }
-
         return progress;
 }
index 7c556a98ea2b1fd884b7b0f5411bdfbb7797924f..a082c41dfe0afef22fa2acfcb1ca98a60bb0aee8 100644 (file)
@@ -784,6 +784,7 @@ qir_optimize(struct vc4_compile *c)
                 OPTPASS(qir_opt_dead_code);
                 OPTPASS(qir_opt_small_immediates);
                 OPTPASS(qir_opt_vpm);
+                OPTPASS(qir_opt_coalesce_ff_writes);
 
                 if (!progress)
                         break;
index 99cc957853a52c0be2501b1b92280f14da0af419..a8f90cfe7af29c7422343c99f269fc42daf70e55 100644 (file)
@@ -595,6 +595,7 @@ void qir_validate(struct vc4_compile *c);
 
 void qir_optimize(struct vc4_compile *c);
 bool qir_opt_algebraic(struct vc4_compile *c);
+bool qir_opt_coalesce_ff_writes(struct vc4_compile *c);
 bool qir_opt_constant_folding(struct vc4_compile *c);
 bool qir_opt_copy_propagation(struct vc4_compile *c);
 bool qir_opt_dead_code(struct vc4_compile *c);