[Mesa-dev] [PATCH] nir: Use helper macros for dealing with VLAs.

Jose Fonseca jfonseca at vmware.com
Tue Mar 3 05:19:17 PST 2015


v2:
- Single statement, by using memset return value as suggested by Ian
Romanick.
- No internal declaration, as suggested by Jason Ekstrand.
- Move macros to a header.
---
 src/glsl/nir/nir_from_ssa.c          | 23 ++++++---------
 src/glsl/nir/nir_live_variables.c    |  4 +--
 src/glsl/nir/nir_lower_vars_to_ssa.c | 12 +++-----
 src/glsl/nir/nir_vla.h               | 54 ++++++++++++++++++++++++++++++++++++
 4 files changed, 69 insertions(+), 24 deletions(-)
 create mode 100644 src/glsl/nir/nir_vla.h

diff --git a/src/glsl/nir/nir_from_ssa.c b/src/glsl/nir/nir_from_ssa.c
index 66339f3..c3090fb 100644
--- a/src/glsl/nir/nir_from_ssa.c
+++ b/src/glsl/nir/nir_from_ssa.c
@@ -26,7 +26,7 @@
  */
 
 #include "nir.h"
-#include "c99_alloca.h"
+#include "nir_vla.h"
 
 /*
  * This file implements an out-of-SSA pass as described in "Revisiting
@@ -182,7 +182,7 @@ merge_merge_sets(merge_set *a, merge_set *b)
 static bool
 merge_sets_interfere(merge_set *a, merge_set *b)
 {
-   merge_node **dom = alloca((a->size + b->size) * sizeof *dom);
+   NIR_VLA(merge_node *, dom, a->size + b->size);
    int dom_idx = -1;
 
    struct exec_node *an = exec_list_get_head(&a->nodes);
@@ -674,21 +674,16 @@ resolve_parallel_copy(nir_parallel_copy_instr *pcopy,
    }
 
    /* The register/source corresponding to the given index */
-   nir_src *values = alloca(num_copies * 2 * sizeof *values);
-   memset(values, 0, num_copies * 2 * sizeof *values);
+   NIR_VLA_ZERO(nir_src, values, num_copies * 2);
 
-   /* The current location of a given piece of data */
-   int *loc = alloca(num_copies * 2 * sizeof *loc);
+   /* The current location of a given piece of data.  We will use -1 for "null" */
+   NIR_VLA_FILL(int, loc, num_copies * 2, -1);
 
-   /* The piece of data that the given piece of data is to be copied from */
-   int *pred = alloca(num_copies * 2 * sizeof *pred);
-
-   /* Initialize loc and pred.  We will use -1 for "null" */
-   memset(loc, -1, num_copies * 2 * sizeof *loc);
-   memset(pred, -1, num_copies * 2 * sizeof *pred);
+   /* The piece of data that the given piece of data is to be copied from.  We will use -1 for "null" */
+   NIR_VLA_FILL(int, pred, num_copies * 2, -1);
 
    /* The destinations we have yet to properly fill */
-   int *to_do = alloca(num_copies * 2 * sizeof *to_do);
+   NIR_VLA(int, to_do, num_copies * 2);
    int to_do_idx = -1;
 
    /* Now we set everything up:
@@ -738,7 +733,7 @@ resolve_parallel_copy(nir_parallel_copy_instr *pcopy,
    }
 
    /* Currently empty destinations we can go ahead and fill */
-   int *ready = alloca(num_copies * 2 * sizeof *ready);
+   NIR_VLA(int, ready, num_copies * 2);
    int ready_idx = -1;
 
    /* Mark the ones that are ready for copying.  We know an index is a
diff --git a/src/glsl/nir/nir_live_variables.c b/src/glsl/nir/nir_live_variables.c
index b57ca3a..1c96dcf 100644
--- a/src/glsl/nir/nir_live_variables.c
+++ b/src/glsl/nir/nir_live_variables.c
@@ -26,7 +26,7 @@
 
 #include "nir.h"
 #include "nir_worklist.h"
-#include "c99_alloca.h"
+#include "nir_vla.h"
 
 /*
  * Basic liveness analysis.  This works only in SSA form.
@@ -131,7 +131,7 @@ static bool
 propagate_across_edge(nir_block *pred, nir_block *succ,
                       struct live_variables_state *state)
 {
-   BITSET_WORD *live = alloca(state->bitset_words * sizeof *live);
+   NIR_VLA(BITSET_WORD, live, state->bitset_words);
    memcpy(live, succ->live_in, state->bitset_words * sizeof *live);
 
    nir_foreach_instr(succ, instr) {
diff --git a/src/glsl/nir/nir_lower_vars_to_ssa.c b/src/glsl/nir/nir_lower_vars_to_ssa.c
index f54d1b7..9e9a418 100644
--- a/src/glsl/nir/nir_lower_vars_to_ssa.c
+++ b/src/glsl/nir/nir_lower_vars_to_ssa.c
@@ -26,8 +26,7 @@
  */
 
 #include "nir.h"
-
-#include "c99_alloca.h"
+#include "nir_vla.h"
 
 
 struct deref_node {
@@ -902,8 +901,8 @@ rename_variables_block(nir_block *block, struct lower_variables_state *state)
 static void
 insert_phi_nodes(struct lower_variables_state *state)
 {
-   unsigned *work = alloca(state->impl->num_blocks * sizeof *work);
-   unsigned *has_already = alloca(state->impl->num_blocks * sizeof *has_already);
+   NIR_VLA_ZERO(unsigned, work, state->impl->num_blocks);
+   NIR_VLA_ZERO(unsigned, has_already, state->impl->num_blocks);
 
    /*
     * Since the work flags already prevent us from inserting a node that has
@@ -913,10 +912,7 @@ insert_phi_nodes(struct lower_variables_state *state)
     * function. So all we need to handle W is an array and a pointer to the
     * next element to be inserted and the next element to be removed.
     */
-   nir_block **W = alloca(state->impl->num_blocks * sizeof *W);
-
-   memset(work, 0, state->impl->num_blocks * sizeof *work);
-   memset(has_already, 0, state->impl->num_blocks * sizeof *has_already);
+   NIR_VLA(nir_block *, W, state->impl->num_blocks);
 
    unsigned w_start, w_end;
    unsigned iter_count = 0;
diff --git a/src/glsl/nir/nir_vla.h b/src/glsl/nir/nir_vla.h
new file mode 100644
index 0000000..7537833
--- /dev/null
+++ b/src/glsl/nir/nir_vla.h
@@ -0,0 +1,54 @@
+/**************************************************************************
+ *
+ * Copyright 2015 VMware, Inc.
+ * All Rights Reserved.
+ *
+ * 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, sub license, 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 NON-INFRINGEMENT.
+ * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS 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.
+ *
+ **************************************************************************/
+
+#pragma once
+
+
+#include "c99_alloca.h"
+
+
+/* Declare a variable length array, with no initialization */
+#define NIR_VLA(_type, _name, _length) \
+   _type *_name = alloca((_length) * sizeof *_name)
+
+
+/* Declare a variable length array, and initialize it with the given byte.
+ *
+ * _length is evaluated twice, so expressions with side-effects must be
+ * avoided.
+ */
+#define NIR_VLA_FILL(_type, _name, _length, _byte) \
+   _type *_name = memset(alloca((_length) * sizeof *_name), _byte, (_length) * sizeof *_name)
+
+
+/* Declare a variable length array, and zero it.
+ *
+ * Just like NIR_VLA_FILL, _length is evaluated twice, so expressions with
+ * side-effects must be avoided.
+ */
+#define NIR_VLA_ZERO(_type, _name, _length) \
+   NIR_VLA_FILL(_type, _name, _length, 0)
-- 
2.1.0



More information about the mesa-dev mailing list