pan/bi: Use safe helpers to remove srcs/dests
authorAlyssa Rosenzweig <alyssa@collabora.com>
Fri, 22 Jul 2022 15:46:51 +0000 (11:46 -0400)
committerMarge Bot <emma+marge@anholt.net>
Fri, 2 Sep 2022 16:03:23 +0000 (16:03 +0000)
Changing I->nr_srcs or I->nr_dests directly is generally unsafe, but the special
case of removing sources/destinations at the end is safe. Add and use helpers to
wrap this operation simplifying the remaining code audit before we can
dynamically allocate sources/destinations.

At this point in the series, nothing modifies I->nr_dests after allocation
except these helpers, so destinations should be safe to make dynamic. There's a
bit more work needed for sources.

Signed-off-by: Alyssa Rosenzweig <alyssa@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/17794>

src/panfrost/bifrost/bi_pack.c
src/panfrost/bifrost/bi_schedule.c
src/panfrost/bifrost/compiler.h
src/panfrost/bifrost/valhall/va_optimize.c

index b3b6cbf..b497d28 100644 (file)
@@ -744,8 +744,7 @@ bi_lower_texc_dual(bi_context *ctx)
                 if (I->op == BI_OPCODE_TEXC_DUAL) {
                         /* In hardware, TEXC has 1 destination */
                         I->op = BI_OPCODE_TEXC;
-                        I->dest[1] = bi_null();
-                        I->nr_dests = 1;
+                        bi_drop_dests(I, 1);
                 }
         }
 }
index a06b416..f183fd2 100644 (file)
@@ -328,13 +328,10 @@ bi_lower_cubeface(bi_context *ctx,
 
         pinstr->op = BI_OPCODE_CUBEFACE2;
         pinstr->dest[0] = pinstr->dest[1];
-        pinstr->dest[1] = bi_null();
-        pinstr->nr_dests = 1;
+        bi_drop_dests(pinstr, 1);
 
         pinstr->src[0] = cubeface1->dest[0];
-        pinstr->src[1] = bi_null();
-        pinstr->src[2] = bi_null();
-        pinstr->nr_srcs = 1;
+        bi_drop_srcs(pinstr, 1);
 
         return cubeface1;
 }
@@ -397,8 +394,7 @@ bi_lower_seg_add(bi_context *ctx,
 
         pinstr->op = BI_OPCODE_SEG_ADD;
         pinstr->src[0] = pinstr->src[1];
-        pinstr->src[1] = bi_null();
-        pinstr->nr_srcs = 1;
+        bi_drop_srcs(pinstr, 1);
 
         assert(pinstr->dest[0].type == BI_INDEX_REGISTER);
         pinstr->dest[0].value += 1;
index ceb7e9d..6fbdfa5 100644 (file)
@@ -547,6 +547,33 @@ bi_is_staging_src(const bi_instr *I, unsigned s)
         return (s == 0 || s == 4) && bi_opcode_props[I->op].sr_read;
 }
 
+/*
+ * Safe helpers to remove destinations/sources at the end of the
+ * destination/source array when changing opcodes. Unlike adding
+ * sources/destinations, this does not require reallocation.
+ */
+static inline void
+bi_drop_dests(bi_instr *I, unsigned new_count)
+{
+        assert(new_count < I->nr_dests);
+
+        for (unsigned i = new_count; i < I->nr_dests; ++i)
+                I->dest[i] = bi_null();
+
+        I->nr_dests = new_count;
+}
+
+static inline void
+bi_drop_srcs(bi_instr *I, unsigned new_count)
+{
+        assert(new_count < I->nr_srcs);
+
+        for (unsigned i = new_count; i < I->nr_srcs; ++i)
+                I->src[i] = bi_null();
+
+        I->nr_srcs = new_count;
+}
+
 /* Represents the assignment of slots for a given bi_tuple */
 
 typedef struct {
index 0671187..a50c424 100644 (file)
@@ -105,8 +105,7 @@ va_fuse_add_imm(bi_instr *I)
    }
 
    I->src[0] = I->src[1 - s];
-   I->src[1] = bi_null();
-   I->nr_srcs = 1;
+   bi_drop_srcs(I, 1);
 }
 
 void