[PATCH vkd3d v3 2/3] vkd3d-shader/hlsl: Move fold_constants() to a separate file.

Giovanni Mascellani gmascellani at codeweavers.com
Fri Feb 11 02:32:20 CST 2022


Signed-off-by: Giovanni Mascellani <gmascellani at codeweavers.com>

Il 11/02/22 04:48, Zebediah Figura ha scritto:
> From: Francisco Casas <fcasas at codeweavers.com>
> 
> Also rename it to hlsl_fold_constants().
> 
> Signed-off-by: Francisco Casas <fcasas at codeweavers.com>
> Signed-off-by: Zebediah Figura <zfigura at codeweavers.com>
> ---
>   Makefile.am                           |   1 +
>   libs/vkd3d-shader/hlsl.h              |   2 +
>   libs/vkd3d-shader/hlsl_codegen.c      | 133 +---------------------
>   libs/vkd3d-shader/hlsl_constant_ops.c | 152 ++++++++++++++++++++++++++
>   4 files changed, 156 insertions(+), 132 deletions(-)
>   create mode 100644 libs/vkd3d-shader/hlsl_constant_ops.c
> 
> diff --git a/Makefile.am b/Makefile.am
> index 7a7bd6a7a..c3ba3febc 100644
> --- a/Makefile.am
> +++ b/Makefile.am
> @@ -207,6 +207,7 @@ libvkd3d_shader_la_SOURCES = \
>   	libs/vkd3d-shader/hlsl.c \
>   	libs/vkd3d-shader/hlsl.h \
>   	libs/vkd3d-shader/hlsl_codegen.c \
> +	libs/vkd3d-shader/hlsl_constant_ops.c \
>   	libs/vkd3d-shader/hlsl_sm1.c \
>   	libs/vkd3d-shader/hlsl_sm4.c \
>   	libs/vkd3d-shader/preproc.h \
> diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h
> index 67fe1a8d5..be515e326 100644
> --- a/libs/vkd3d-shader/hlsl.h
> +++ b/libs/vkd3d-shader/hlsl.h
> @@ -795,6 +795,8 @@ unsigned int hlsl_offset_from_deref_safe(struct hlsl_ctx *ctx, const struct hlsl
>   struct hlsl_reg hlsl_reg_from_deref(struct hlsl_ctx *ctx, const struct hlsl_deref *deref,
>           const struct hlsl_type *type);
>   
> +bool hlsl_fold_constants(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context);
> +
>   bool hlsl_sm1_register_from_semantic(struct hlsl_ctx *ctx, const struct hlsl_semantic *semantic,
>           bool output, D3DSHADER_PARAM_REGISTER_TYPE *type, unsigned int *reg);
>   bool hlsl_sm1_usage_from_semantic(const struct hlsl_semantic *semantic, D3DDECLUSAGE *usage, uint32_t *usage_idx);
> diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c
> index 77ddd979a..17a7649fc 100644
> --- a/libs/vkd3d-shader/hlsl_codegen.c
> +++ b/libs/vkd3d-shader/hlsl_codegen.c
> @@ -702,137 +702,6 @@ static bool lower_narrowing_casts(struct hlsl_ctx *ctx, struct hlsl_ir_node *ins
>       return false;
>   }
>   
> -static bool fold_constants(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context)
> -{
> -    struct hlsl_ir_constant *arg1, *arg2 = NULL, *res;
> -    struct hlsl_ir_expr *expr;
> -    unsigned int i, dimx;
> -
> -    if (instr->type != HLSL_IR_EXPR)
> -        return false;
> -    expr = hlsl_ir_expr(instr);
> -
> -    for (i = 0; i < ARRAY_SIZE(expr->operands); ++i)
> -    {
> -        if (expr->operands[i].node && expr->operands[i].node->type != HLSL_IR_CONSTANT)
> -            return false;
> -    }
> -    arg1 = hlsl_ir_constant(expr->operands[0].node);
> -    if (expr->operands[1].node)
> -        arg2 = hlsl_ir_constant(expr->operands[1].node);
> -    dimx = instr->data_type->dimx;
> -
> -    if (!(res = hlsl_alloc(ctx, sizeof(*res))))
> -        return false;
> -    init_node(&res->node, HLSL_IR_CONSTANT, instr->data_type, instr->loc);
> -
> -    switch (instr->data_type->base_type)
> -    {
> -        case HLSL_TYPE_FLOAT:
> -        {
> -            switch (expr->op)
> -            {
> -                case HLSL_OP1_CAST:
> -                    if (instr->data_type->dimx != arg1->node.data_type->dimx
> -                            || instr->data_type->dimy != arg1->node.data_type->dimy)
> -                    {
> -                        FIXME("Cast from %s to %s.\n", debug_hlsl_type(ctx, arg1->node.data_type),
> -                                debug_hlsl_type(ctx, instr->data_type));
> -                        vkd3d_free(res);
> -                        return false;
> -                    }
> -
> -                    switch (arg1->node.data_type->base_type)
> -                    {
> -                        case HLSL_TYPE_INT:
> -                            for (i = 0; i < dimx; ++i)
> -                                res->value[i].f = arg1->value[i].i;
> -                            break;
> -
> -                        case HLSL_TYPE_UINT:
> -                            for (i = 0; i < dimx; ++i)
> -                                res->value[i].f = arg1->value[i].u;
> -                            break;
> -
> -                        default:
> -                            FIXME("Cast from %s to %s.\n", debug_hlsl_type(ctx, arg1->node.data_type),
> -                                    debug_hlsl_type(ctx, instr->data_type));
> -                            vkd3d_free(res);
> -                            return false;
> -                    }
> -                    break;
> -
> -                default:
> -                    FIXME("Fold float op %#x.\n", expr->op);
> -                    vkd3d_free(res);
> -                    return false;
> -            }
> -            break;
> -        }
> -
> -        case HLSL_TYPE_UINT:
> -        {
> -            switch (expr->op)
> -            {
> -                case HLSL_OP1_CAST:
> -                    if (instr->data_type->dimx != arg1->node.data_type->dimx
> -                            || instr->data_type->dimy != arg1->node.data_type->dimy)
> -                    {
> -                        FIXME("Cast from %s to %s.\n", debug_hlsl_type(ctx, arg1->node.data_type),
> -                                debug_hlsl_type(ctx, instr->data_type));
> -                        vkd3d_free(res);
> -                        return false;
> -                    }
> -
> -                    switch (arg1->node.data_type->base_type)
> -                    {
> -                        case HLSL_TYPE_INT:
> -                            for (i = 0; i < dimx; ++i)
> -                                res->value[i].i = arg1->value[i].u;
> -                            break;
> -
> -                        default:
> -                            FIXME("Cast from %s to %s.\n", debug_hlsl_type(ctx, arg1->node.data_type),
> -                                    debug_hlsl_type(ctx, instr->data_type));
> -                            vkd3d_free(res);
> -                            return false;
> -                    }
> -                    break;
> -
> -                case HLSL_OP1_NEG:
> -                    for (i = 0; i < instr->data_type->dimx; ++i)
> -                        res->value[i].u = -arg1->value[i].u;
> -                    break;
> -
> -                case HLSL_OP2_ADD:
> -                    for (i = 0; i < instr->data_type->dimx; ++i)
> -                        res->value[i].u = arg1->value[i].u + arg2->value[i].u;
> -                    break;
> -
> -                case HLSL_OP2_MUL:
> -                    for (i = 0; i < instr->data_type->dimx; ++i)
> -                        res->value[i].u = arg1->value[i].u * arg2->value[i].u;
> -                    break;
> -
> -                default:
> -                    FIXME("Fold uint op %#x.\n", expr->op);
> -                    vkd3d_free(res);
> -                    return false;
> -            }
> -            break;
> -        }
> -
> -        default:
> -            FIXME("Fold type %#x op %#x.\n", instr->data_type->base_type, expr->op);
> -            vkd3d_free(res);
> -            return false;
> -    }
> -
> -    list_add_before(&expr->node.entry, &res->node.entry);
> -    hlsl_replace_node(&expr->node, &res->node);
> -    return true;
> -}
> -
>   static bool remove_trivial_swizzles(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context)
>   {
>       struct hlsl_ir_swizzle *swizzle;
> @@ -1785,7 +1654,7 @@ int hlsl_emit_dxbc(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry_fun
>       transform_ir(ctx, lower_narrowing_casts, body, NULL);
>       do
>       {
> -        progress = transform_ir(ctx, fold_constants, body, NULL);
> +        progress = transform_ir(ctx, hlsl_fold_constants, body, NULL);
>           progress |= copy_propagation_execute(ctx, body);
>       }
>       while (progress);
> diff --git a/libs/vkd3d-shader/hlsl_constant_ops.c b/libs/vkd3d-shader/hlsl_constant_ops.c
> new file mode 100644
> index 000000000..9e19cef4b
> --- /dev/null
> +++ b/libs/vkd3d-shader/hlsl_constant_ops.c
> @@ -0,0 +1,152 @@
> +/*
> + * HLSL constant value operations for constant folding
> + *
> + * Copyright 2022 Francisco Casas for CodeWeavers
> + *
> + * This library is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * This library is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with this library; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
> + */
> +
> +#include "hlsl.h"
> +
> +bool hlsl_fold_constants(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr, void *context)
> +{
> +    struct hlsl_ir_constant *arg1, *arg2 = NULL, *res;
> +    struct hlsl_ir_expr *expr;
> +    unsigned int i, dimx;
> +
> +    if (instr->type != HLSL_IR_EXPR)
> +        return false;
> +    expr = hlsl_ir_expr(instr);
> +
> +    for (i = 0; i < ARRAY_SIZE(expr->operands); ++i)
> +    {
> +        if (expr->operands[i].node && expr->operands[i].node->type != HLSL_IR_CONSTANT)
> +            return false;
> +    }
> +    arg1 = hlsl_ir_constant(expr->operands[0].node);
> +    if (expr->operands[1].node)
> +        arg2 = hlsl_ir_constant(expr->operands[1].node);
> +    dimx = instr->data_type->dimx;
> +
> +    if (!(res = hlsl_alloc(ctx, sizeof(*res))))
> +        return false;
> +    init_node(&res->node, HLSL_IR_CONSTANT, instr->data_type, instr->loc);
> +
> +    switch (instr->data_type->base_type)
> +    {
> +        case HLSL_TYPE_FLOAT:
> +        {
> +            switch (expr->op)
> +            {
> +                case HLSL_OP1_CAST:
> +                    if (instr->data_type->dimx != arg1->node.data_type->dimx
> +                            || instr->data_type->dimy != arg1->node.data_type->dimy)
> +                    {
> +                        FIXME("Cast from %s to %s.\n", debug_hlsl_type(ctx, arg1->node.data_type),
> +                                debug_hlsl_type(ctx, instr->data_type));
> +                        vkd3d_free(res);
> +                        return false;
> +                    }
> +
> +                    switch (arg1->node.data_type->base_type)
> +                    {
> +                        case HLSL_TYPE_INT:
> +                            for (i = 0; i < dimx; ++i)
> +                                res->value[i].f = arg1->value[i].i;
> +                            break;
> +
> +                        case HLSL_TYPE_UINT:
> +                            for (i = 0; i < dimx; ++i)
> +                                res->value[i].f = arg1->value[i].u;
> +                            break;
> +
> +                        default:
> +                            FIXME("Cast from %s to %s.\n", debug_hlsl_type(ctx, arg1->node.data_type),
> +                                    debug_hlsl_type(ctx, instr->data_type));
> +                            vkd3d_free(res);
> +                            return false;
> +                    }
> +                    break;
> +
> +                default:
> +                    FIXME("Fold float op %#x.\n", expr->op);
> +                    vkd3d_free(res);
> +                    return false;
> +            }
> +            break;
> +        }
> +
> +        case HLSL_TYPE_UINT:
> +        {
> +            switch (expr->op)
> +            {
> +                case HLSL_OP1_CAST:
> +                    if (instr->data_type->dimx != arg1->node.data_type->dimx
> +                            || instr->data_type->dimy != arg1->node.data_type->dimy)
> +                    {
> +                        FIXME("Cast from %s to %s.\n", debug_hlsl_type(ctx, arg1->node.data_type),
> +                                debug_hlsl_type(ctx, instr->data_type));
> +                        vkd3d_free(res);
> +                        return false;
> +                    }
> +
> +                    switch (arg1->node.data_type->base_type)
> +                    {
> +                        case HLSL_TYPE_INT:
> +                            for (i = 0; i < dimx; ++i)
> +                                res->value[i].i = arg1->value[i].u;
> +                            break;
> +
> +                        default:
> +                            FIXME("Cast from %s to %s.\n", debug_hlsl_type(ctx, arg1->node.data_type),
> +                                    debug_hlsl_type(ctx, instr->data_type));
> +                            vkd3d_free(res);
> +                            return false;
> +                    }
> +                    break;
> +
> +                case HLSL_OP1_NEG:
> +                    for (i = 0; i < instr->data_type->dimx; ++i)
> +                        res->value[i].u = -arg1->value[i].u;
> +                    break;
> +
> +                case HLSL_OP2_ADD:
> +                    for (i = 0; i < instr->data_type->dimx; ++i)
> +                        res->value[i].u = arg1->value[i].u + arg2->value[i].u;
> +                    break;
> +
> +                case HLSL_OP2_MUL:
> +                    for (i = 0; i < instr->data_type->dimx; ++i)
> +                        res->value[i].u = arg1->value[i].u * arg2->value[i].u;
> +                    break;
> +
> +                default:
> +                    FIXME("Fold uint op %#x.\n", expr->op);
> +                    vkd3d_free(res);
> +                    return false;
> +            }
> +            break;
> +        }
> +
> +        default:
> +            FIXME("Fold type %#x op %#x.\n", instr->data_type->base_type, expr->op);
> +            vkd3d_free(res);
> +            return false;
> +    }
> +
> +    list_add_before(&expr->node.entry, &res->node.entry);
> +    hlsl_replace_node(&expr->node, &res->node);
> +    return true;
> +}



More information about the wine-devel mailing list