vkd3d-shader/ir: Record the control flow type in the program.

This commit is contained in:
Giovanni Mascellani 2024-09-10 22:22:24 +02:00 committed by Henri Verbeet
parent dcee148b1a
commit 866f138875
Notes: Henri Verbeet 2024-09-12 18:56:42 +02:00
Approved-by: Giovanni Mascellani (@giomasce)
Approved-by: Henri Verbeet (@hverbeet)
Merge-Request: https://gitlab.winehq.org/wine/vkd3d/-/merge_requests/1061
6 changed files with 25 additions and 30 deletions

View File

@ -1272,7 +1272,8 @@ static enum vkd3d_result shader_sm1_init(struct vkd3d_shader_sm1_parser *sm1, st
sm1->end = &code[token_count]; sm1->end = &code[token_count];
/* Estimate instruction count to avoid reallocation in most shaders. */ /* Estimate instruction count to avoid reallocation in most shaders. */
if (!vsir_program_init(program, compile_info, &version, code_size != ~(size_t)0 ? token_count / 4u + 4 : 16)) if (!vsir_program_init(program, compile_info, &version,
code_size != ~(size_t)0 ? token_count / 4u + 4 : 16, VSIR_CF_STRUCTURED))
return VKD3D_ERROR_OUT_OF_MEMORY; return VKD3D_ERROR_OUT_OF_MEMORY;
vkd3d_shader_parser_init(&sm1->p, program, message_context, compile_info->source_name); vkd3d_shader_parser_init(&sm1->p, program, message_context, compile_info->source_name);

View File

@ -10303,7 +10303,7 @@ static enum vkd3d_result sm6_parser_init(struct sm6_parser *sm6, struct vsir_pro
/* Estimate instruction count to avoid reallocation in most shaders. */ /* Estimate instruction count to avoid reallocation in most shaders. */
count = max(token_count, 400) - 400; count = max(token_count, 400) - 400;
if (!vsir_program_init(program, compile_info, &version, (count + (count >> 2)) / 2u + 10)) if (!vsir_program_init(program, compile_info, &version, (count + (count >> 2)) / 2u + 10, VSIR_CF_BLOCKS))
return VKD3D_ERROR_OUT_OF_MEMORY; return VKD3D_ERROR_OUT_OF_MEMORY;
vkd3d_shader_parser_init(&sm6->p, program, message_context, compile_info->source_name); vkd3d_shader_parser_init(&sm6->p, program, message_context, compile_info->source_name);
sm6->ptr = &sm6->start[1]; sm6->ptr = &sm6->start[1];

View File

@ -7041,7 +7041,7 @@ static void sm1_generate_vsir(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl
version.major = ctx->profile->major_version; version.major = ctx->profile->major_version;
version.minor = ctx->profile->minor_version; version.minor = ctx->profile->minor_version;
version.type = ctx->profile->type; version.type = ctx->profile->type;
if (!vsir_program_init(program, NULL, &version, 0)) if (!vsir_program_init(program, NULL, &version, 0, VSIR_CF_STRUCTURED))
{ {
ctx->result = VKD3D_ERROR_OUT_OF_MEMORY; ctx->result = VKD3D_ERROR_OUT_OF_MEMORY;
return; return;

View File

@ -74,7 +74,7 @@ static int convert_parameter_info(const struct vkd3d_shader_compile_info *compil
} }
bool vsir_program_init(struct vsir_program *program, const struct vkd3d_shader_compile_info *compile_info, bool vsir_program_init(struct vsir_program *program, const struct vkd3d_shader_compile_info *compile_info,
const struct vkd3d_shader_version *version, unsigned int reserve) const struct vkd3d_shader_version *version, unsigned int reserve, enum vsir_control_flow_type cf_type)
{ {
memset(program, 0, sizeof(*program)); memset(program, 0, sizeof(*program));
@ -96,6 +96,7 @@ bool vsir_program_init(struct vsir_program *program, const struct vkd3d_shader_c
} }
program->shader_version = *version; program->shader_version = *version;
program->cf_type = cf_type;
return shader_instruction_array_init(&program->instructions, reserve); return shader_instruction_array_init(&program->instructions, reserve);
} }
@ -2803,6 +2804,8 @@ static enum vkd3d_result vsir_program_flatten_control_flow_constructs(struct vsi
struct cf_flattener flattener = {.program = program}; struct cf_flattener flattener = {.program = program};
enum vkd3d_result result; enum vkd3d_result result;
VKD3D_ASSERT(program->cf_type == VSIR_CF_STRUCTURED);
if ((result = cf_flattener_iterate_instruction_array(&flattener, message_context)) >= 0) if ((result = cf_flattener_iterate_instruction_array(&flattener, message_context)) >= 0)
{ {
vkd3d_free(program->instructions.elements); vkd3d_free(program->instructions.elements);
@ -2810,6 +2813,7 @@ static enum vkd3d_result vsir_program_flatten_control_flow_constructs(struct vsi
program->instructions.capacity = flattener.instruction_capacity; program->instructions.capacity = flattener.instruction_capacity;
program->instructions.count = flattener.instruction_count; program->instructions.count = flattener.instruction_count;
program->block_count = flattener.block_id; program->block_count = flattener.block_id;
program->cf_type = VSIR_CF_BLOCKS;
} }
else else
{ {
@ -2877,6 +2881,8 @@ static enum vkd3d_result vsir_program_lower_switch_to_selection_ladder(struct vs
struct vkd3d_shader_instruction *instructions = NULL; struct vkd3d_shader_instruction *instructions = NULL;
struct lower_switch_to_if_ladder_block_mapping *block_map = NULL; struct lower_switch_to_if_ladder_block_mapping *block_map = NULL;
VKD3D_ASSERT(program->cf_type == VSIR_CF_BLOCKS);
if (!reserve_instructions(&instructions, &ins_capacity, program->instructions.count)) if (!reserve_instructions(&instructions, &ins_capacity, program->instructions.count))
goto fail; goto fail;
@ -3069,6 +3075,8 @@ static enum vkd3d_result vsir_program_materialise_phi_ssas_to_temps(struct vsir_
struct ssas_to_temps_alloc alloc = {0}; struct ssas_to_temps_alloc alloc = {0};
unsigned int current_label = 0; unsigned int current_label = 0;
VKD3D_ASSERT(program->cf_type == VSIR_CF_BLOCKS);
if (!(block_info = vkd3d_calloc(program->block_count, sizeof(*block_info)))) if (!(block_info = vkd3d_calloc(program->block_count, sizeof(*block_info))))
{ {
ERR("Failed to allocate block info array.\n"); ERR("Failed to allocate block info array.\n");
@ -5289,6 +5297,8 @@ static enum vkd3d_result vsir_program_structurize(struct vsir_program *program,
enum vkd3d_result ret; enum vkd3d_result ret;
size_t i; size_t i;
VKD3D_ASSERT(program->cf_type == VSIR_CF_BLOCKS);
target.jump_target_temp_idx = program->temp_count; target.jump_target_temp_idx = program->temp_count;
target.temp_count = program->temp_count + 1; target.temp_count = program->temp_count + 1;
@ -5336,6 +5346,7 @@ static enum vkd3d_result vsir_program_structurize(struct vsir_program *program,
program->instructions.capacity = target.ins_capacity; program->instructions.capacity = target.ins_capacity;
program->instructions.count = target.ins_count; program->instructions.count = target.ins_count;
program->temp_count = target.temp_count; program->temp_count = target.temp_count;
program->cf_type = VSIR_CF_STRUCTURED;
return VKD3D_OK; return VKD3D_OK;
@ -5469,6 +5480,8 @@ static enum vkd3d_result vsir_program_materialize_undominated_ssas_to_temps(stru
enum vkd3d_result ret; enum vkd3d_result ret;
size_t i; size_t i;
VKD3D_ASSERT(program->cf_type == VSIR_CF_BLOCKS);
for (i = 0; i < program->instructions.count;) for (i = 0; i < program->instructions.count;)
{ {
struct vkd3d_shader_instruction *ins = &program->instructions.elements[i]; struct vkd3d_shader_instruction *ins = &program->instructions.elements[i];
@ -5701,7 +5714,6 @@ struct validation_context
enum vkd3d_result status; enum vkd3d_result status;
bool dcl_temps_found; bool dcl_temps_found;
enum vkd3d_shader_opcode phase; enum vkd3d_shader_opcode phase;
enum vsir_control_flow_type cf_type;
bool inside_block; bool inside_block;
struct validation_context_temp_data struct validation_context_temp_data
@ -6130,11 +6142,9 @@ static const char *name_from_cf_type(enum vsir_control_flow_type type)
static void vsir_validate_cf_type(struct validation_context *ctx, static void vsir_validate_cf_type(struct validation_context *ctx,
const struct vkd3d_shader_instruction *instruction, enum vsir_control_flow_type expected_type) const struct vkd3d_shader_instruction *instruction, enum vsir_control_flow_type expected_type)
{ {
VKD3D_ASSERT(ctx->cf_type != VSIR_CF_UNKNOWN); if (ctx->program->cf_type != expected_type)
VKD3D_ASSERT(expected_type != VSIR_CF_UNKNOWN);
if (ctx->cf_type != expected_type)
validator_error(ctx, VKD3D_SHADER_ERROR_VSIR_INVALID_CONTROL_FLOW, "Invalid instruction %#x in %s shader.", validator_error(ctx, VKD3D_SHADER_ERROR_VSIR_INVALID_CONTROL_FLOW, "Invalid instruction %#x in %s shader.",
instruction->opcode, name_from_cf_type(ctx->cf_type)); instruction->opcode, name_from_cf_type(ctx->program->cf_type));
} }
static void vsir_validate_instruction(struct validation_context *ctx) static void vsir_validate_instruction(struct validation_context *ctx)
@ -6249,24 +6259,8 @@ static void vsir_validate_instruction(struct validation_context *ctx)
"Instruction %#x appear before any phase instruction in a hull shader.", "Instruction %#x appear before any phase instruction in a hull shader.",
instruction->opcode); instruction->opcode);
/* We support two different control flow types in shaders: if (ctx->program->cf_type == VSIR_CF_BLOCKS && !vsir_instruction_is_dcl(instruction)
* block-based, like DXIL and SPIR-V, and structured, like D3DBC && instruction->opcode != VKD3DSIH_NOP)
* and TPF. The shader is detected as block-based when its first
* instruction, except for NOP, DCL_* and phases, is a LABEL.
* Currently we mandate that each shader is either purely block-based or
* purely structured. In principle we could allow structured
* constructs in a block, provided they are confined in a single
* block, but need for that hasn't arisen yet, so we don't. */
if (ctx->cf_type == VSIR_CF_UNKNOWN && instruction->opcode != VKD3DSIH_NOP
&& !vsir_instruction_is_dcl(instruction))
{
if (instruction->opcode == VKD3DSIH_LABEL)
ctx->cf_type = VSIR_CF_BLOCKS;
else
ctx->cf_type = VSIR_CF_STRUCTURED;
}
if (ctx->cf_type == VSIR_CF_BLOCKS && !vsir_instruction_is_dcl(instruction))
{ {
switch (instruction->opcode) switch (instruction->opcode)
{ {

View File

@ -2598,7 +2598,7 @@ static bool shader_sm4_init(struct vkd3d_shader_sm4_parser *sm4, struct vsir_pro
version.minor = VKD3D_SM4_VERSION_MINOR(version_token); version.minor = VKD3D_SM4_VERSION_MINOR(version_token);
/* Estimate instruction count to avoid reallocation in most shaders. */ /* Estimate instruction count to avoid reallocation in most shaders. */
if (!vsir_program_init(program, compile_info, &version, token_count / 7u + 20)) if (!vsir_program_init(program, compile_info, &version, token_count / 7u + 20, VSIR_CF_STRUCTURED))
return false; return false;
vkd3d_shader_parser_init(&sm4->p, program, message_context, compile_info->source_name); vkd3d_shader_parser_init(&sm4->p, program, message_context, compile_info->source_name);
sm4->ptr = sm4->start; sm4->ptr = sm4->start;

View File

@ -1366,7 +1366,6 @@ enum vkd3d_shader_config_flags
enum vsir_control_flow_type enum vsir_control_flow_type
{ {
VSIR_CF_UNKNOWN,
VSIR_CF_STRUCTURED, VSIR_CF_STRUCTURED,
VSIR_CF_BLOCKS, VSIR_CF_BLOCKS,
}; };
@ -1390,6 +1389,7 @@ struct vsir_program
unsigned int temp_count; unsigned int temp_count;
unsigned int ssa_count; unsigned int ssa_count;
bool use_vocp; bool use_vocp;
enum vsir_control_flow_type cf_type;
const char **block_names; const char **block_names;
size_t block_name_count; size_t block_name_count;
@ -1402,7 +1402,7 @@ int vsir_program_compile(struct vsir_program *program, uint64_t config_flags,
const struct vkd3d_shader_parameter1 *vsir_program_get_parameter( const struct vkd3d_shader_parameter1 *vsir_program_get_parameter(
const struct vsir_program *program, enum vkd3d_shader_parameter_name name); const struct vsir_program *program, enum vkd3d_shader_parameter_name name);
bool vsir_program_init(struct vsir_program *program, const struct vkd3d_shader_compile_info *compile_info, bool vsir_program_init(struct vsir_program *program, const struct vkd3d_shader_compile_info *compile_info,
const struct vkd3d_shader_version *version, unsigned int reserve); const struct vkd3d_shader_version *version, unsigned int reserve, enum vsir_control_flow_type cf_type);
enum vkd3d_result vsir_program_transform(struct vsir_program *program, uint64_t config_flags, enum vkd3d_result vsir_program_transform(struct vsir_program *program, uint64_t config_flags,
const struct vkd3d_shader_compile_info *compile_info, struct vkd3d_shader_message_context *message_context); const struct vkd3d_shader_compile_info *compile_info, struct vkd3d_shader_message_context *message_context);
enum vkd3d_result vsir_program_validate(struct vsir_program *program, uint64_t config_flags, enum vkd3d_result vsir_program_validate(struct vsir_program *program, uint64_t config_flags,