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

Matteo Bruni mbruni at codeweavers.com
Wed Dec 1 10:14:53 CST 2021


From: Giovanni Mascellani <gmascellani at codeweavers.com>

Signed-off-by: Matteo Bruni <mbruni at codeweavers.com>
---
changelog: Try constant propagation first.

 libs/vkd3d-shader/hlsl_codegen.c | 51 ++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c
index 2ef51f78..6773ab81 100644
--- a/libs/vkd3d-shader/hlsl_codegen.c
+++ b/libs/vkd3d-shader/hlsl_codegen.c
@@ -438,6 +438,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)
 {
@@ -458,6 +500,15 @@ 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_constant(ctx, var_def, offset, type->dimx, &load->node.loc)))
+    {
+        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;
+    }
+
     if (!(new_node = copy_propagation_compute_replacement(var_def, offset, type->dimx, &swizzle)))
     {
         TRACE("No single source for propagating load from %s[%u-%u].\n", var->name, offset, offset + type->dimx);
-- 
2.26.3




More information about the wine-devel mailing list