[PATCH vkd3d 1/5] vkd3d-shader: Allocate temporary registers for variables.

Zebediah Figura zfigura at codeweavers.com
Tue Mar 30 17:03:11 CDT 2021


Signed-off-by: Zebediah Figura <zfigura at codeweavers.com>
---
 libs/vkd3d-shader/hlsl.c         |   4 +-
 libs/vkd3d-shader/hlsl.h         |   9 ++
 libs/vkd3d-shader/hlsl_codegen.c | 184 +++++++++++++++++++++++++++++++
 3 files changed, 195 insertions(+), 2 deletions(-)

diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c
index 53b9db36..67ca2f2c 100644
--- a/libs/vkd3d-shader/hlsl.c
+++ b/libs/vkd3d-shader/hlsl.c
@@ -890,7 +890,7 @@ static void dump_deref(struct vkd3d_string_buffer *buffer, const struct hlsl_der
     }
 }
 
-static const char *debug_writemask(DWORD writemask)
+const char *debug_hlsl_writemask(unsigned int writemask)
 {
     static const char components[] = {'x', 'y', 'z', 'w'};
     char string[5];
@@ -914,7 +914,7 @@ static void dump_ir_assignment(struct vkd3d_string_buffer *buffer, const struct
     vkd3d_string_buffer_printf(buffer, "= (");
     dump_deref(buffer, &assign->lhs);
     if (assign->writemask != VKD3DSP_WRITEMASK_ALL)
-        vkd3d_string_buffer_printf(buffer, "%s", debug_writemask(assign->writemask));
+        vkd3d_string_buffer_printf(buffer, "%s", debug_hlsl_writemask(assign->writemask));
     vkd3d_string_buffer_printf(buffer, " ");
     dump_src(buffer, &assign->rhs);
     vkd3d_string_buffer_printf(buffer, ")");
diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h
index cf12f63f..fa7709ef 100644
--- a/libs/vkd3d-shader/hlsl.h
+++ b/libs/vkd3d-shader/hlsl.h
@@ -137,6 +137,13 @@ struct hlsl_struct_field
     unsigned int reg_offset;
 };
 
+struct hlsl_reg
+{
+    uint32_t id;
+    unsigned char writemask;
+    bool allocated;
+};
+
 enum hlsl_ir_node_type
 {
     HLSL_IR_ASSIGNMENT = 0,
@@ -208,6 +215,7 @@ struct hlsl_ir_var
     struct list scope_entry, param_entry;
 
     unsigned int first_write, last_read;
+    struct hlsl_reg reg;
 
     uint32_t is_input_varying : 1;
     uint32_t is_output_varying : 1;
@@ -493,6 +501,7 @@ static inline void hlsl_src_remove(struct hlsl_src *src)
 }
 
 const char *debug_hlsl_type(const struct hlsl_type *type) DECLSPEC_HIDDEN;
+const char *debug_hlsl_writemask(unsigned int writemask) DECLSPEC_HIDDEN;
 
 struct vkd3d_string_buffer *hlsl_type_to_string(struct vkd3d_string_buffer_cache *string_buffers,
         const struct hlsl_type *type) DECLSPEC_HIDDEN;
diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c
index dbd591af..420e720f 100644
--- a/libs/vkd3d-shader/hlsl_codegen.c
+++ b/libs/vkd3d-shader/hlsl_codegen.c
@@ -612,6 +612,188 @@ static void compute_liveness(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl
     compute_liveness_recurse(entry_func->body, 0, 0);
 }
 
+struct liveness
+{
+    size_t size;
+    struct
+    {
+        /* 0 if not live yet. */
+        unsigned int last_read;
+    } *regs;
+};
+
+static unsigned char get_available_writemask(struct liveness *liveness,
+        unsigned int first_write, unsigned int index, unsigned int components)
+{
+    unsigned char i, writemask = 0, count = 0;
+
+    for (i = 0; i < 4; ++i)
+    {
+        if (liveness->regs[index + i].last_read <= first_write)
+        {
+            writemask |= 1 << i;
+            if (++count == components)
+                return writemask;
+        }
+    }
+
+    return 0;
+}
+
+static bool resize_liveness(struct liveness *liveness, size_t new_count)
+{
+    size_t old_capacity = liveness->size;
+
+    if (!vkd3d_array_reserve((void **)&liveness->regs, &liveness->size, new_count, sizeof(*liveness->regs)))
+        return false;
+
+    if (liveness->size > old_capacity)
+        memset(liveness->regs + old_capacity, 0, (liveness->size - old_capacity) * sizeof(*liveness->regs));
+    return true;
+}
+
+static struct hlsl_reg allocate_register(struct liveness *liveness,
+        unsigned int first_write, unsigned int last_read, unsigned char components)
+{
+    struct hlsl_reg ret = {.allocated = true};
+    unsigned char writemask, i;
+    unsigned int regnum;
+
+    for (regnum = 0; regnum < liveness->size; regnum += 4)
+    {
+        if ((writemask = get_available_writemask(liveness, first_write, regnum, components)))
+            break;
+    }
+    if (regnum == liveness->size)
+    {
+        if (!resize_liveness(liveness, regnum + 4))
+            return ret;
+        writemask = (1 << components) - 1;
+    }
+    for (i = 0; i < 4; ++i)
+    {
+        if (writemask & (1 << i))
+            liveness->regs[regnum + i].last_read = last_read;
+    }
+    ret.id = regnum / 4;
+    ret.writemask = writemask;
+    return ret;
+}
+
+static bool is_range_available(struct liveness *liveness, unsigned int first_write,
+        unsigned int index, unsigned int elements)
+{
+    unsigned int i;
+
+    for (i = 0; i < elements; i += 4)
+    {
+        if (!get_available_writemask(liveness, first_write, index + i, 4))
+            return false;
+    }
+    return true;
+}
+
+/* "elements" is the total number of consecutive whole registers needed. */
+static struct hlsl_reg allocate_range(struct liveness *liveness,
+        unsigned int first_write, unsigned int last_read, unsigned int elements)
+{
+    const unsigned int components = elements * 4;
+    struct hlsl_reg ret = {.allocated = true};
+    unsigned int i, regnum;
+
+    for (regnum = 0; regnum < liveness->size; regnum += 4)
+    {
+        if (is_range_available(liveness, first_write, regnum, min(components, liveness->size - regnum)))
+            break;
+    }
+    if (!resize_liveness(liveness, regnum + components))
+        return ret;
+
+    for (i = 0; i < components; ++i)
+        liveness->regs[regnum + i].last_read = last_read;
+    ret.id = regnum / 4;
+    return ret;
+}
+
+static const char *debug_register(char class, struct hlsl_reg reg, const struct hlsl_type *type)
+{
+    if (type->reg_size > 4)
+        return vkd3d_dbg_sprintf("%c%u-%c%u", class, reg.id, class,
+                reg.id + type->reg_size - 1);
+    return vkd3d_dbg_sprintf("%c%u%s", class, reg.id, debug_hlsl_writemask(reg.writemask));
+}
+
+static void allocate_variable_temp_register(struct hlsl_ir_var *var, struct liveness *liveness)
+{
+    if (var->is_input_varying || var->is_output_varying || var->is_uniform)
+        return;
+
+    if (!var->reg.allocated && var->last_read)
+    {
+        if (var->data_type->reg_size > 1)
+            var->reg = allocate_range(liveness, var->first_write,
+                    var->last_read, var->data_type->reg_size);
+        else
+            var->reg = allocate_register(liveness, var->first_write,
+                    var->last_read, var->data_type->dimx);
+        TRACE("Allocated %s to %s (liveness %u-%u).\n", debug_register('r', var->reg, var->data_type),
+                var->name, var->first_write, var->last_read);
+    }
+}
+
+static void allocate_temp_registers_recurse(struct list *instrs, struct liveness *liveness)
+{
+    struct hlsl_ir_node *instr;
+
+    LIST_FOR_EACH_ENTRY(instr, instrs, struct hlsl_ir_node, entry)
+    {
+        switch (instr->type)
+        {
+            case HLSL_IR_ASSIGNMENT:
+            {
+                struct hlsl_ir_assignment *assignment = hlsl_ir_assignment(instr);
+                allocate_variable_temp_register(assignment->lhs.var, liveness);
+                break;
+            }
+
+            case HLSL_IR_IF:
+            {
+                struct hlsl_ir_if *iff = hlsl_ir_if(instr);
+                allocate_temp_registers_recurse(&iff->then_instrs, liveness);
+                allocate_temp_registers_recurse(&iff->else_instrs, liveness);
+                break;
+            }
+
+            case HLSL_IR_LOAD:
+            {
+                struct hlsl_ir_load *load = hlsl_ir_load(instr);
+                allocate_variable_temp_register(load->src.var, liveness);
+                break;
+            }
+
+            case HLSL_IR_LOOP:
+            {
+                struct hlsl_ir_loop *loop = hlsl_ir_loop(instr);
+                allocate_temp_registers_recurse(&loop->body, liveness);
+                break;
+            }
+
+            default:
+                break;
+        }
+    }
+}
+
+/* Simple greedy temporary register allocation pass that just assigns a unique
+ * index to all (simultaneously live) variables or intermediate values. Agnostic
+ * as to how many registers are actually available for the current backend, and
+ * does not handle constants. */
+static void allocate_temp_registers(struct hlsl_ir_function_decl *entry_func)
+{
+    struct liveness liveness = {0};
+    allocate_temp_registers_recurse(entry_func->body, &liveness);
+}
+
 int hlsl_emit_dxbc(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry_func)
 {
     struct hlsl_ir_var *var;
@@ -649,6 +831,8 @@ int hlsl_emit_dxbc(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry_fun
     if (TRACE_ON())
         rb_for_each_entry(&ctx->functions, dump_function, NULL);
 
+    allocate_temp_registers(entry_func);
+
     if (ctx->failed)
         return VKD3D_ERROR_INVALID_SHADER;
     return VKD3D_ERROR_NOT_IMPLEMENTED;
-- 
2.31.0




More information about the wine-devel mailing list