From d5fd309ef8b3ed10a0f862a1acb1e6c1681a7ca1 Mon Sep 17 00:00:00 2001 From: Giovanni Mascellani Date: Wed, 31 Aug 2022 13:25:24 +0200 Subject: [PATCH] vkd3d: Add a macro to mark unreachable code. This should silence warnings about some branches non returning any value without requiring additional "return 0" statement or similar. Also, in theory this might enable to compiler to optimize the program a little bit more, though that's unlikely to have any measurable effect. --- include/private/vkd3d_common.h | 14 +++++++++ libs/vkd3d-shader/hlsl.c | 14 ++++----- libs/vkd3d-shader/hlsl.h | 3 +- libs/vkd3d-shader/hlsl.y | 12 ++++---- libs/vkd3d-shader/hlsl_codegen.c | 9 ++---- libs/vkd3d-shader/hlsl_constant_ops.c | 12 ++------ libs/vkd3d-shader/hlsl_sm1.c | 9 +++--- libs/vkd3d-shader/hlsl_sm4.c | 42 +++++++++------------------ libs/vkd3d-shader/vkd3d_shader_main.c | 2 +- tests/shader_runner_d3d9.c | 6 ++-- 10 files changed, 52 insertions(+), 71 deletions(-) diff --git a/include/private/vkd3d_common.h b/include/private/vkd3d_common.h index c041b52d..9a2de265 100644 --- a/include/private/vkd3d_common.h +++ b/include/private/vkd3d_common.h @@ -56,12 +56,26 @@ static inline size_t align(size_t addr, size_t alignment) # define VKD3D_NORETURN __attribute__((noreturn)) # define VKD3D_PRINTF_FUNC(fmt, args) __attribute__((format(printf, fmt, args))) # define VKD3D_UNUSED __attribute__((unused)) +# define VKD3D_UNREACHABLE __builtin_unreachable() #else # define VKD3D_NORETURN # define VKD3D_PRINTF_FUNC(fmt, args) # define VKD3D_UNUSED +# define VKD3D_UNREACHABLE (void)0 #endif /* __GNUC__ */ +VKD3D_NORETURN static inline void vkd3d_unreachable_(const char *filename, unsigned int line) +{ + fprintf(stderr, "%s:%u: Aborting, reached unreachable code.\n", filename, line); + abort(); +} + +#ifdef NDEBUG +#define vkd3d_unreachable() VKD3D_UNREACHABLE +#else +#define vkd3d_unreachable() vkd3d_unreachable_(__FILE__, __LINE__) +#endif + static inline unsigned int vkd3d_popcount(unsigned int v) { #ifdef _MSC_VER diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c index 00b54b76..ae8ab185 100644 --- a/libs/vkd3d-shader/hlsl.c +++ b/libs/vkd3d-shader/hlsl.c @@ -333,13 +333,11 @@ static unsigned int traverse_path_from_component_index(struct hlsl_ctx *ctx, } index -= field_comp_count; } - assert(0); - return 0; + vkd3d_unreachable(); } default: - assert(0); - return 0; + vkd3d_unreachable(); } } @@ -466,8 +464,7 @@ struct hlsl_type *hlsl_get_element_type_from_path_index(struct hlsl_ctx *ctx, co } default: - assert(0); - return NULL; + vkd3d_unreachable(); } } @@ -586,8 +583,7 @@ unsigned int hlsl_type_component_count(const struct hlsl_type *type) return 1; default: - assert(0); - return 0; + vkd3d_unreachable(); } } @@ -1590,7 +1586,7 @@ static void dump_ir_constant(struct vkd3d_string_buffer *buffer, const struct hl break; default: - assert(0); + vkd3d_unreachable(); } } if (type->dimx != 1) diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h index 794749aa..7d382488 100644 --- a/libs/vkd3d-shader/hlsl.h +++ b/libs/vkd3d-shader/hlsl.h @@ -701,8 +701,7 @@ static inline unsigned int hlsl_sampler_dim_count(enum hlsl_sampler_dim dim) case HLSL_SAMPLER_DIM_CUBEARRAY: return 4; default: - assert(0); - return 0; + vkd3d_unreachable(); } } diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y index 624481d8..b9e7b262 100644 --- a/libs/vkd3d-shader/hlsl.y +++ b/libs/vkd3d-shader/hlsl.y @@ -1086,8 +1086,7 @@ static unsigned int evaluate_array_dimension(struct hlsl_ir_node *node) case HLSL_TYPE_BOOL: return !!value->u; default: - assert(0); - return 0; + vkd3d_unreachable(); } } @@ -1104,10 +1103,10 @@ static unsigned int evaluate_array_dimension(struct hlsl_ir_node *node) case HLSL_IR_STORE: WARN("Invalid node type %s.\n", hlsl_node_type_to_string(node->type)); return 0; - } - assert(0); - return 0; + default: + vkd3d_unreachable(); + } } static bool expr_compatible_data_types(struct hlsl_type *t1, struct hlsl_type *t2) @@ -2510,8 +2509,7 @@ static unsigned int hlsl_offset_dim_count(enum hlsl_sampler_dim dim) /* Offset parameters not supported for these types. */ return 0; default: - assert(0); - return 0; + vkd3d_unreachable(); } } diff --git a/libs/vkd3d-shader/hlsl_codegen.c b/libs/vkd3d-shader/hlsl_codegen.c index f919a30e..289c5a11 100644 --- a/libs/vkd3d-shader/hlsl_codegen.c +++ b/libs/vkd3d-shader/hlsl_codegen.c @@ -80,8 +80,7 @@ static struct hlsl_ir_node *new_offset_from_path_index(struct hlsl_ctx *ctx, str } default: - assert(0); - return NULL; + vkd3d_unreachable(); } if (offset) @@ -1763,8 +1762,7 @@ static void allocate_const_registers_recurse(struct hlsl_ctx *ctx, struct hlsl_b return; default: - assert(0); - return; + vkd3d_unreachable(); } defs->values[constant->reg.id + y].f[x] = f; } @@ -2149,8 +2147,7 @@ bool hlsl_component_index_range_from_deref(struct hlsl_ctx *ctx, const struct hl break; default: - assert(0); - break; + vkd3d_unreachable(); } type = hlsl_get_element_type_from_path_index(ctx, type, path_node); diff --git a/libs/vkd3d-shader/hlsl_constant_ops.c b/libs/vkd3d-shader/hlsl_constant_ops.c index 20384180..7ca63a3e 100644 --- a/libs/vkd3d-shader/hlsl_constant_ops.c +++ b/libs/vkd3d-shader/hlsl_constant_ops.c @@ -79,8 +79,7 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct break; default: - assert(0); - return false; + vkd3d_unreachable(); } switch (dst->node.data_type->base_type) @@ -104,12 +103,8 @@ static bool fold_cast(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, struct case HLSL_TYPE_BOOL: /* Casts to bool should have already been lowered. */ - assert(0); - break; - default: - assert(0); - return false; + vkd3d_unreachable(); } } return true; @@ -248,8 +243,7 @@ static bool fold_nequal(struct hlsl_ctx *ctx, struct hlsl_ir_constant *dst, break; default: - assert(0); - return false; + vkd3d_unreachable(); } dst->value[k].u *= ~0u; diff --git a/libs/vkd3d-shader/hlsl_sm1.c b/libs/vkd3d-shader/hlsl_sm1.c index 06313a42..2219ef56 100644 --- a/libs/vkd3d-shader/hlsl_sm1.c +++ b/libs/vkd3d-shader/hlsl_sm1.c @@ -159,8 +159,7 @@ static D3DXPARAMETER_CLASS sm1_class(const struct hlsl_type *type) return D3DXPC_VECTOR; default: ERR("Invalid class %#x.\n", type->type); - assert(0); - return 0; + vkd3d_unreachable(); } } @@ -193,6 +192,7 @@ static D3DXPARAMETER_TYPE sm1_base_type(const struct hlsl_type *type) return D3DXPT_SAMPLER; default: ERR("Invalid dimension %#x.\n", type->sampler_dim); + vkd3d_unreachable(); } break; case HLSL_TYPE_STRING: @@ -212,6 +212,7 @@ static D3DXPARAMETER_TYPE sm1_base_type(const struct hlsl_type *type) return D3DXPT_TEXTURE; default: ERR("Invalid dimension %#x.\n", type->sampler_dim); + vkd3d_unreachable(); } break; case HLSL_TYPE_VERTEXSHADER: @@ -219,10 +220,8 @@ static D3DXPARAMETER_TYPE sm1_base_type(const struct hlsl_type *type) case HLSL_TYPE_VOID: return D3DXPT_VOID; default: - assert(0); + vkd3d_unreachable(); } - assert(0); - return 0; } static const struct hlsl_type *get_array_type(const struct hlsl_type *type) diff --git a/libs/vkd3d-shader/hlsl_sm4.c b/libs/vkd3d-shader/hlsl_sm4.c index f056571a..5d763c56 100644 --- a/libs/vkd3d-shader/hlsl_sm4.c +++ b/libs/vkd3d-shader/hlsl_sm4.c @@ -279,8 +279,7 @@ static D3D_SHADER_VARIABLE_CLASS sm4_class(const struct hlsl_type *type) return D3D_SVC_VECTOR; default: ERR("Invalid class %#x.\n", type->type); - assert(0); - return 0; + vkd3d_unreachable(); } } @@ -313,7 +312,7 @@ static D3D_SHADER_VARIABLE_TYPE sm4_base_type(const struct hlsl_type *type) case HLSL_SAMPLER_DIM_GENERIC: return D3D_SVT_SAMPLER; default: - assert(0); + vkd3d_unreachable(); } break; case HLSL_TYPE_STRING: @@ -332,7 +331,7 @@ static D3D_SHADER_VARIABLE_TYPE sm4_base_type(const struct hlsl_type *type) case HLSL_SAMPLER_DIM_GENERIC: return D3D_SVT_TEXTURE; default: - assert(0); + vkd3d_unreachable(); } break; case HLSL_TYPE_UINT: @@ -342,10 +341,8 @@ static D3D_SHADER_VARIABLE_TYPE sm4_base_type(const struct hlsl_type *type) case HLSL_TYPE_VOID: return D3D_SVT_VOID; default: - assert(0); + vkd3d_unreachable(); } - assert(0); - return 0; } static void write_sm4_type(struct hlsl_ctx *ctx, struct vkd3d_bytecode_buffer *buffer, struct hlsl_type *type) @@ -414,8 +411,7 @@ static D3D_SHADER_INPUT_TYPE sm4_resource_type(const struct hlsl_type *type) case HLSL_TYPE_TEXTURE: return D3D_SIT_TEXTURE; default: - assert(0); - return 0; + vkd3d_unreachable(); } } @@ -439,8 +435,7 @@ static D3D_RESOURCE_RETURN_TYPE sm4_resource_format(const struct hlsl_type *type return D3D_RETURN_TYPE_UINT; default: - assert(0); - return 0; + vkd3d_unreachable(); } } @@ -467,8 +462,7 @@ static D3D_SRV_DIMENSION sm4_rdef_resource_dimension(const struct hlsl_type *typ case HLSL_SAMPLER_DIM_CUBEARRAY: return D3D_SRV_DIMENSION_TEXTURECUBEARRAY; default: - assert(0); - return D3D_SRV_DIMENSION_UNKNOWN; + vkd3d_unreachable(); } } @@ -764,8 +758,7 @@ static enum vkd3d_sm4_resource_type sm4_resource_dimension(const struct hlsl_typ case HLSL_SAMPLER_DIM_CUBEARRAY: return VKD3D_SM4_RESOURCE_TEXTURE_CUBEARRAY; default: - assert(0); - return 0; + vkd3d_unreachable(); } } @@ -800,8 +793,7 @@ static uint32_t sm4_encode_instruction_modifier(const struct sm4_instruction_mod break; default: - assert(0); - break; + vkd3d_unreachable(); } return word; @@ -1528,7 +1520,7 @@ static void write_sm4_cast(struct hlsl_ctx *ctx, break; default: - assert(0); + vkd3d_unreachable(); } break; @@ -1554,7 +1546,7 @@ static void write_sm4_cast(struct hlsl_ctx *ctx, break; default: - assert(0); + vkd3d_unreachable(); } break; @@ -1580,7 +1572,7 @@ static void write_sm4_cast(struct hlsl_ctx *ctx, break; default: - assert(0); + vkd3d_unreachable(); } break; @@ -1594,11 +1586,8 @@ static void write_sm4_cast(struct hlsl_ctx *ctx, case HLSL_TYPE_BOOL: /* Casts to bool should have already been lowered. */ - assert(0); - break; - default: - assert(0); + vkd3d_unreachable(); } } @@ -1754,11 +1743,8 @@ static void write_sm4_expr(struct hlsl_ctx *ctx, break; case 1: - assert(0); - break; - default: - assert(0); + vkd3d_unreachable(); } break; diff --git a/libs/vkd3d-shader/vkd3d_shader_main.c b/libs/vkd3d-shader/vkd3d_shader_main.c index 7d0157fc..1575a004 100644 --- a/libs/vkd3d-shader/vkd3d_shader_main.c +++ b/libs/vkd3d-shader/vkd3d_shader_main.c @@ -1314,7 +1314,7 @@ int vkd3d_shader_compile(const struct vkd3d_shader_compile_info *compile_info, break; default: - assert(0); + vkd3d_unreachable(); } vkd3d_shader_message_context_trace_messages(&message_context); diff --git a/tests/shader_runner_d3d9.c b/tests/shader_runner_d3d9.c index 583b7ca7..a56f39be 100644 --- a/tests/shader_runner_d3d9.c +++ b/tests/shader_runner_d3d9.c @@ -183,8 +183,7 @@ static D3DTEXTUREADDRESS sampler_address_to_d3d9(D3D12_TEXTURE_ADDRESS_MODE addr return D3DTADDRESS_MIRRORONCE; } - assert(0); - return 0; + vkd3d_unreachable(); } static bool d3d9_runner_check_requirements(struct shader_runner *r) @@ -369,8 +368,7 @@ static bool d3d9_runner_draw(struct shader_runner *r, break; case RESOURCE_TYPE_UAV: - assert(0); - break; + vkd3d_unreachable(); case RESOURCE_TYPE_VERTEX_BUFFER: for (j = 0; j < runner->r.input_element_count; ++j)