[PATCH vkd3d 3/3] vkd3d-shader/hlsl: Propagate constant loads in copy propagation.

Giovanni Mascellani gmascellani at codeweavers.com
Wed Jan 19 06:53:44 CST 2022


Signed-off-by: Matteo Bruni <mbruni at codeweavers.com>
Signed-off-by: Giovanni Mascellani <gmascellani at codeweavers.com>
---
Control flow reworked so that copy_propagation_analyze_load() is
easier to read.
---
 libs/vkd3d-shader/hlsl_codegen.c | 71 +++++++++++++++++++++++++++-----
 1 file changed, 61 insertions(+), 10 deletions(-)

diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c
index e358bb14..882a546e 100644
--- a/libs/vkd3d-shader/hlsl_codegen.c
+++ b/libs/vkd3d-shader/hlsl_codegen.c
@@ -479,6 +479,48 @@ static struct hlsl_ir_node *copy_propagation_compute_replacement(struct copy_pro
     return node;
 }
 
+static struct hlsl_ir_node *copy_propagation_compute_constant(struct hlsl_ctx *ctx,
+        struct copy_propagation_var_def *variable, unsigned int offset, unsigned int count,
+        const struct vkd3d_shader_location *loc)
+{
+    enum hlsl_base_type type = HLSL_TYPE_VOID;
+    struct hlsl_ir_constant *constant = NULL;
+    unsigned int i;
+
+    assert(offset + count <= variable->var->data_type->reg_size);
+
+    for (i = 0; i < count; ++i)
+    {
+        struct hlsl_ir_node *store_node = variable->values[offset + i].node;
+        enum hlsl_base_type store_type;
+
+        if (!store_node)
+            return NULL;
+        if (store_node->type != HLSL_IR_CONSTANT)
+            return NULL;
+
+        store_type = store_node->data_type->base_type;
+
+        if (type == HLSL_TYPE_VOID)
+            type = store_type;
+        else if (type != store_type)
+            return NULL;
+    }
+
+    if (!(constant = hlsl_new_uint_constant(ctx, 0, *loc)))
+        return NULL;
+    constant->node.data_type = hlsl_get_vector_type(ctx, type, count);
+
+    for (i = 0; i < count; ++i)
+    {
+        struct hlsl_ir_node *store_node = variable->values[offset + i].node;
+
+        constant->value[i] = hlsl_ir_constant(store_node)->value[variable->values[offset + i].component];
+    }
+
+    return &constant->node;
+}
+
 static bool copy_propagation_analyze_load(struct hlsl_ctx *ctx, struct hlsl_ir_load *load,
         struct copy_propagation_state *state)
 {
@@ -499,19 +541,28 @@ static bool copy_propagation_analyze_load(struct hlsl_ctx *ctx, struct hlsl_ir_l
     if (!(var_def = copy_propagation_get_var_def(state, var)))
         return false;
 
-    if (!(new_node = copy_propagation_compute_replacement(var_def, offset, type->dimx, &swizzle)))
+    if ((new_node = copy_propagation_compute_constant(ctx, var_def, offset, type->dimx, &load->node.loc)))
     {
-        TRACE("No single source for propagating load from %s[%u-%u].\n", var->name, offset, offset + type->dimx);
-        return false;
+        TRACE("Load from %s[%d-%d] reconstructed as constant value.\n",
+                var->name, offset, offset + type->dimx);
+        list_add_before(&node->entry, &new_node->entry);
+        replace_node(node, new_node);
+        return true;
     }
 
-    TRACE("Load from %s[%u-%u] propagated as instruction %p%s.\n",
-            var->name, offset, offset + type->dimx, new_node, debug_hlsl_swizzle(swizzle, 4));
-    if (!(swizzle_node = hlsl_new_swizzle(ctx, swizzle, type->dimx, new_node, &node->loc)))
-        return false;
-    list_add_before(&node->entry, &swizzle_node->node.entry);
-    replace_node(node, &swizzle_node->node);
-    return true;
+    if ((new_node = copy_propagation_compute_replacement(var_def, offset, type->dimx, &swizzle)))
+    {
+        TRACE("Load from %s[%u-%u] propagated as instruction %p%s.\n",
+                var->name, offset, offset + type->dimx, new_node, debug_hlsl_swizzle(swizzle, 4));
+        if (!(swizzle_node = hlsl_new_swizzle(ctx, swizzle, type->dimx, new_node, &node->loc)))
+            return false;
+        list_add_before(&node->entry, &swizzle_node->node.entry);
+        replace_node(node, &swizzle_node->node);
+        return true;
+    }
+
+    TRACE("No single source for propagating load from %s[%u-%u].\n", var->name, offset, offset + type->dimx);
+    return false;
 }
 
 static void copy_propagation_record_store(struct hlsl_ctx *ctx, struct hlsl_ir_store *store,
-- 
2.34.1




More information about the wine-devel mailing list