spirv: Add cast and loop helpers for vtn_cf_node
authorJason Ekstrand <jason@jlekstrand.net>
Wed, 12 Feb 2020 21:00:30 +0000 (15:00 -0600)
committerMarge Bot <eric+marge@anholt.net>
Fri, 3 Apr 2020 20:54:00 +0000 (20:54 +0000)
Reviewed-by: Caio Marcelo de Oliveira Filho <caio.oliveira@intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/3820>

src/compiler/spirv/vtn_cfg.c
src/compiler/spirv/vtn_private.h

index e07ccf70e306e50f9fcc22d50bb9d7df2aea6069..107755d96ac28a17ba084d76fab8de267e56ce3b 100644 (file)
@@ -918,10 +918,10 @@ vtn_emit_cf_list(struct vtn_builder *b, struct list_head *cf_list,
                  nir_variable *switch_fall_var, bool *has_switch_break,
                  vtn_instruction_handler handler)
 {
-   list_for_each_entry(struct vtn_cf_node, node, cf_list, link) {
+   vtn_foreach_cf_node(node, cf_list) {
       switch (node->type) {
       case vtn_cf_node_type_block: {
-         struct vtn_block *block = (struct vtn_block *)node;
+         struct vtn_block *block = vtn_cf_node_as_block(node);
 
          const uint32_t *block_start = block->label;
          const uint32_t *block_end = block->merge ? block->merge :
@@ -959,7 +959,7 @@ vtn_emit_cf_list(struct vtn_builder *b, struct list_head *cf_list,
       }
 
       case vtn_cf_node_type_if: {
-         struct vtn_if *vtn_if = (struct vtn_if *)node;
+         struct vtn_if *vtn_if = vtn_cf_node_as_if(node);
          bool sw_break = false;
 
          nir_if *nif =
@@ -998,7 +998,7 @@ vtn_emit_cf_list(struct vtn_builder *b, struct list_head *cf_list,
       }
 
       case vtn_cf_node_type_loop: {
-         struct vtn_loop *vtn_loop = (struct vtn_loop *)node;
+         struct vtn_loop *vtn_loop = vtn_cf_node_as_loop(node);
 
          nir_loop *loop = nir_push_loop(&b->nb);
          loop->control = vtn_loop_control(b, vtn_loop);
@@ -1035,7 +1035,7 @@ vtn_emit_cf_list(struct vtn_builder *b, struct list_head *cf_list,
       }
 
       case vtn_cf_node_type_switch: {
-         struct vtn_switch *vtn_switch = (struct vtn_switch *)node;
+         struct vtn_switch *vtn_switch = vtn_cf_node_as_switch(node);
 
          /* First, we create a variable to keep track of whether or not the
           * switch is still going at any given point.  Any switch breaks
index 4b2d28978d9267bb0b0a5b32698c8a8f36e35240..a042afe4bbe971dcff5675ae9a0b0755db8151d3 100644 (file)
@@ -242,6 +242,22 @@ struct vtn_function {
    SpvFunctionControlMask control;
 };
 
+#define VTN_DECL_CF_NODE_CAST(_type)               \
+static inline struct vtn_##_type *                 \
+vtn_cf_node_as_##_type(struct vtn_cf_node *node)   \
+{                                                  \
+   assert(node->type == vtn_cf_node_type_##_type); \
+   return (struct vtn_##_type *)node;              \
+}
+
+VTN_DECL_CF_NODE_CAST(block)
+VTN_DECL_CF_NODE_CAST(loop)
+VTN_DECL_CF_NODE_CAST(if)
+VTN_DECL_CF_NODE_CAST(switch)
+
+#define vtn_foreach_cf_node(node, cf_list) \
+   list_for_each_entry(struct vtn_cf_node, node, cf_list, link)
+
 typedef bool (*vtn_instruction_handler)(struct vtn_builder *, SpvOp,
                                         const uint32_t *, unsigned);