diff --git a/libs/vkd3d-shader/dxil.c b/libs/vkd3d-shader/dxil.c index 19b4ffabf..7c484c071 100644 --- a/libs/vkd3d-shader/dxil.c +++ b/libs/vkd3d-shader/dxil.c @@ -748,16 +748,8 @@ struct sm6_symbol struct incoming_value { - const struct sm6_block *block; - struct vkd3d_shader_register reg; -}; - -struct sm6_phi -{ - struct sm6_value value; - struct incoming_value *incoming; - size_t incoming_capacity; - size_t incoming_count; + unsigned int block_idx; + const struct sm6_value *src; }; struct sm6_block @@ -768,10 +760,6 @@ struct sm6_block /* A nonzero id. */ unsigned int id; - - struct sm6_phi *phi; - size_t phi_capacity; - size_t phi_count; }; struct sm6_function @@ -3117,6 +3105,37 @@ static size_t sm6_parser_get_value_idx_by_ref(struct sm6_parser *sm6, const stru return operand; } +static uint64_t decode_rotated_signed_value(uint64_t value) +{ + if (value != 1) + { + bool neg = value & 1; + value >>= 1; + return neg ? -value : value; + } + + return value << 63; +} + +static const struct sm6_value *sm6_parser_get_value_by_rotated_signed_idx(struct sm6_parser *dxil, + uint64_t idx, const struct sm6_type *fwd_type) +{ + int64_t rotated_idx; + size_t operand; + + rotated_idx = decode_rotated_signed_value(idx); + if (rotated_idx > INT32_MAX || rotated_idx < INT32_MIN) + vkd3d_shader_parser_warning(&dxil->p, VKD3D_SHADER_WARNING_DXIL_IGNORING_OPERANDS, + "Ignoring upper 32 bits of DXIL SSA value signed relative index %"PRIx64".", rotated_idx); + + if ((operand = sm6_parser_get_value_index(dxil, rotated_idx)) == SIZE_MAX) + return NULL; + + sm6_parser_pre_init_or_validate_referenced_value(dxil, operand, fwd_type); + + return &dxil->values[operand]; +} + static const struct sm6_value *sm6_parser_get_value_by_ref(struct sm6_parser *sm6, const struct dxil_record *record, const struct sm6_type *type, unsigned int *rec_idx) { @@ -3188,17 +3207,6 @@ static bool sm6_parser_declare_function(struct sm6_parser *sm6, const struct dxi return true; } -static inline uint64_t decode_rotated_signed_value(uint64_t value) -{ - if (value != 1) - { - bool neg = value & 1; - value >>= 1; - return neg ? -value : value; - } - return value << 63; -} - static struct sm6_index *sm6_get_value_index(struct sm6_parser *sm6, struct sm6_value *value) { switch (value->value_type) @@ -4283,26 +4291,6 @@ static struct sm6_block *sm6_block_create() return block; } -static struct sm6_phi *sm6_block_phi_require_space(struct sm6_block *block, struct sm6_parser *sm6) -{ - struct sm6_phi *phi; - - if (!vkd3d_array_reserve((void **)&block->phi, &block->phi_capacity, block->phi_count + 1, sizeof(*block->phi))) - { - ERR("Failed to allocate phi array.\n"); - vkd3d_shader_parser_error(&sm6->p, VKD3D_SHADER_ERROR_DXIL_OUT_OF_MEMORY, - "Out of memory allocating a phi instruction."); - return NULL; - } - phi = &block->phi[block->phi_count++]; - - phi->incoming = NULL; - phi->incoming_capacity = 0; - phi->incoming_count = 0; - - return phi; -} - struct function_emission_state { struct sm6_block *code_block; @@ -4736,15 +4724,6 @@ static bool sm6_function_validate_block_index(const struct sm6_function *functio return true; } -static const struct sm6_block *sm6_function_get_block(const struct sm6_function *function, - uint64_t index, struct sm6_parser *dxil) -{ - if (!sm6_function_validate_block_index(function, index, dxil)) - return NULL; - - return function->blocks[index]; -} - static void sm6_parser_emit_br(struct sm6_parser *dxil, const struct dxil_record *record, struct sm6_function *function, struct vkd3d_shader_instruction *ins) { @@ -7653,95 +7632,105 @@ static int phi_incoming_compare(const void *a, const void *b) { const struct incoming_value *incoming_a = a, *incoming_b = b; - return (incoming_a->block > incoming_b->block) - (incoming_a->block < incoming_b->block); + return vkd3d_u32_compare(incoming_a->block_idx, incoming_b->block_idx); } -static void sm6_parser_emit_phi(struct sm6_parser *sm6, const struct dxil_record *record, - struct sm6_function *function, struct sm6_block *code_block, struct vkd3d_shader_instruction *ins, - struct sm6_value *dst) +static void sm6_parser_emit_phi(struct sm6_parser *dxil, const struct dxil_record *record, + struct sm6_function *function, struct vkd3d_shader_instruction *ins, struct sm6_value *dst) { + struct vkd3d_shader_src_param *src_params; + unsigned int i, j, incoming_count; struct incoming_value *incoming; + const struct sm6_value *src; const struct sm6_type *type; - struct sm6_phi *phi; - unsigned int i, j; - uint64_t src_idx; if (!(record->operand_count & 1)) { - WARN("Invalid operand count %u.\n", record->operand_count); - vkd3d_shader_parser_error(&sm6->p, VKD3D_SHADER_ERROR_DXIL_INVALID_OPERAND_COUNT, + vkd3d_shader_parser_error(&dxil->p, VKD3D_SHADER_ERROR_DXIL_INVALID_OPERAND_COUNT, "Invalid operand count %u for phi instruction.", record->operand_count); return; } - if (!(type = sm6_parser_get_type(sm6, record->operands[0]))) + if (!(type = sm6_parser_get_type(dxil, record->operands[0]))) return; if (!sm6_type_is_numeric(type)) { /* dxc doesn't seem to use buffer/resource read return types here. */ - FIXME("Only scalar numeric types are supported.\n"); - vkd3d_shader_parser_error(&sm6->p, VKD3D_SHADER_ERROR_DXIL_INVALID_OPERAND_COUNT, + vkd3d_shader_parser_error(&dxil->p, VKD3D_SHADER_ERROR_DXIL_INVALID_OPERAND_COUNT, "Result type class %u of a phi instruction is not scalar numeric.", type->class); return; } dst->type = type; - sm6_parser_init_ssa_value(sm6, dst); - if (!(phi = sm6_block_phi_require_space(code_block, sm6))) - return; - phi->incoming_count = record->operand_count / 2u; - - if (!vkd3d_array_reserve((void **)&phi->incoming, &phi->incoming_capacity, phi->incoming_count, - sizeof(*phi->incoming))) + incoming_count = record->operand_count / 2u; + if (!(incoming = vkd3d_calloc(incoming_count, sizeof(*incoming)))) { - ERR("Failed to allocate phi incoming array.\n"); - vkd3d_shader_parser_error(&sm6->p, VKD3D_SHADER_ERROR_DXIL_OUT_OF_MEMORY, + vkd3d_shader_parser_error(&dxil->p, VKD3D_SHADER_ERROR_DXIL_OUT_OF_MEMORY, "Out of memory allocating a phi incoming array."); return; } - incoming = phi->incoming; for (i = 1; i < record->operand_count; i += 2) { - src_idx = sm6->value_count - decode_rotated_signed_value(record->operands[i]); - /* May be a forward reference. */ - if (src_idx >= sm6->cur_max_value) + /* Phi forward references are handled by the same mechanism as all + * others. Constant and undefined values are never forward references, + * and the only other valid incoming is an SSA value, which will be + * initialised if necessary. */ + if (!(src = sm6_parser_get_value_by_rotated_signed_idx(dxil, record->operands[i], type))) + goto done; + + if (!sm6_value_is_constant(src) && !sm6_value_is_undef(src) && !sm6_value_is_ssa(src)) { - WARN("Invalid value index %"PRIu64".\n", src_idx); - vkd3d_shader_parser_error(&sm6->p, VKD3D_SHADER_ERROR_DXIL_INVALID_OPERAND, - "Invalid value index %"PRIu64" for a phi incoming value.", src_idx); - return; + vkd3d_shader_parser_error(&dxil->p, VKD3D_SHADER_ERROR_DXIL_INVALID_OPERAND, + "A PHI incoming value is not a constant or SSA register."); + goto done; } + if (src->type != type) + vkd3d_shader_parser_warning(&dxil->p, VKD3D_SHADER_WARNING_DXIL_TYPE_MISMATCH, + "The type of a phi incoming value does not match the result type."); + + if (!sm6_function_validate_block_index(function, record->operands[i + 1], dxil)) + goto done; + j = i / 2u; - /* Store the value index in the register for later resolution. */ - incoming[j].reg.idx[0].offset = src_idx; - incoming[j].block = sm6_function_get_block(function, record->operands[i + 1], sm6); + incoming[j].src = src; + incoming[j].block_idx = record->operands[i + 1]; } - ins->opcode = VSIR_OP_NOP; + qsort(incoming, incoming_count, sizeof(*incoming), phi_incoming_compare); - qsort(incoming, phi->incoming_count, sizeof(*incoming), phi_incoming_compare); - - for (i = 1, j = 1; i < phi->incoming_count; ++i) + /* Deduplicate incomings. DXIL phi instructions can contain duplicates. */ + for (i = 1, j = 1; i < incoming_count; ++i) { - if (incoming[i].block != incoming[i - 1].block) + if (incoming[i].block_idx != incoming[i - 1].block_idx) { incoming[j++] = incoming[i]; continue; } - if (incoming[i].reg.idx[0].offset != incoming[i - 1].reg.idx[0].offset) - { - WARN("PHI conflict.\n"); - vkd3d_shader_parser_error(&sm6->p, VKD3D_SHADER_ERROR_DXIL_INVALID_OPERAND, + if (incoming[i].src != incoming[i - 1].src) + vkd3d_shader_parser_error(&dxil->p, VKD3D_SHADER_ERROR_DXIL_INVALID_OPERAND, "Two phi incomings have the same block but different values."); - } } - /* if (j == 1) we should be able to set dst->u.reg to incoming[0].reg, but structurisation - * may potentially add new incomings. */ - phi->incoming_count = j; + incoming_count = j; + + vsir_instruction_init(ins, &dxil->p.location, VSIR_OP_PHI); + if (!(src_params = instruction_src_params_alloc(ins, incoming_count * 2u, dxil))) + goto done; + + for (i = 0; i < incoming_count; ++i) + { + j = i * 2u; + src_param_init_from_value(&src_params[j], incoming[i].src, 0, dxil); + vsir_src_param_init_label(&src_params[j + 1], incoming[i].block_idx + 1); + } + + instruction_dst_param_init_ssa_scalar(ins, 0, dxil); + +done: + vkd3d_free(incoming); } static void sm6_parser_emit_ret(struct sm6_parser *dxil, @@ -8274,45 +8263,6 @@ static struct sm6_block *sm6_function_create_block(struct sm6_function *function return block; } -static enum vkd3d_result sm6_function_resolve_phi_incomings(const struct sm6_function *function, - struct sm6_parser *sm6) -{ - const struct sm6_block *block; - size_t i, j, block_idx; - - for (block_idx = 0; block_idx < function->block_count; ++block_idx) - { - block = function->blocks[block_idx]; - - for (i = 0; i < block->phi_count; ++i) - { - struct sm6_phi *phi = &block->phi[i]; - const struct sm6_value *src; - - for (j = 0; j < phi->incoming_count; ++j) - { - src = &sm6->values[phi->incoming[j].reg.idx[0].offset]; - if (!sm6_value_is_constant(src) && !sm6_value_is_undef(src) && !sm6_value_is_ssa(src)) - { - FIXME("PHI incoming value is not a constant or SSA register.\n"); - vkd3d_shader_parser_error(&sm6->p, VKD3D_SHADER_ERROR_DXIL_INVALID_OPERAND, - "A PHI incoming value is not a constant or SSA register."); - return VKD3D_ERROR_INVALID_SHADER; - } - if (src->type != phi->value.type) - { - WARN("Type mismatch.\n"); - vkd3d_shader_parser_warning(&sm6->p, VKD3D_SHADER_WARNING_DXIL_TYPE_MISMATCH, - "The type of a phi incoming value does not match the result type."); - } - vsir_register_from_dxil_value(&phi->incoming[j].reg, src, 0, sm6); - } - } - } - - return VKD3D_OK; -} - static enum vkd3d_result sm6_parser_function_init(struct sm6_parser *sm6, const struct dxil_block *block, struct sm6_function *function) { @@ -8443,7 +8393,7 @@ static enum vkd3d_result sm6_parser_function_init(struct sm6_parser *sm6, const sm6_parser_emit_load(sm6, record, ins, dst); break; case FUNC_CODE_INST_PHI: - sm6_parser_emit_phi(sm6, record, function, code_block, ins, dst); + sm6_parser_emit_phi(sm6, record, function, ins, dst); break; case FUNC_CODE_INST_RET: sm6_parser_emit_ret(sm6, record, ins); @@ -8471,10 +8421,6 @@ static enum vkd3d_result sm6_parser_function_init(struct sm6_parser *sm6, const if (record->attachment) metadata_attachment_record_apply(record->attachment, record->code, ins, dst, sm6); - /* This is specific for PHI nodes, but must happen after attachments have been applied. */ - if (record->code == FUNC_CODE_INST_PHI) - code_block->phi[code_block->phi_count - 1].value = *dst; - /* A vsir instruction can be emitted for a block terminator, so handle * a possible instruction count increment before moving to the next * code block. */ @@ -8503,52 +8449,7 @@ static enum vkd3d_result sm6_parser_function_init(struct sm6_parser *sm6, const return VKD3D_ERROR_INVALID_SHADER; } - return sm6_function_resolve_phi_incomings(function, sm6); -} - -static void sm6_block_emit_phi(const struct sm6_block *block, struct sm6_parser *sm6) -{ - struct vkd3d_shader_instruction *ins; - unsigned int i, j, incoming_count; - const struct sm6_phi *src_phi; - - for (i = 0; i < block->phi_count; ++i) - { - struct vkd3d_shader_src_param *src_params; - struct vkd3d_shader_dst_param *dst_param; - - src_phi = &block->phi[i]; - incoming_count = src_phi->incoming_count; - - if (!(ins = sm6_parser_add_instruction(sm6, VSIR_OP_PHI))) - return; - if (!(src_params = instruction_src_params_alloc(ins, incoming_count * 2u, sm6))) - { - vkd3d_shader_instruction_make_nop(ins); - return; - } - if (!(dst_param = instruction_dst_params_alloc(ins, 1, sm6))) - { - vkd3d_shader_instruction_make_nop(ins); - return; - } - - for (j = 0; j < incoming_count; ++j) - { - const struct sm6_block *incoming_block = src_phi->incoming[j].block; - unsigned int index = j * 2; - - src_param_init(&src_params[index]); - src_params[index].reg = src_phi->incoming[j].reg; - if (incoming_block) - vsir_src_param_init_label(&src_params[index + 1], incoming_block->id); - else - VKD3D_ASSERT(sm6->p.status < 0); - } - - dst_param_init(dst_param); - vsir_register_from_dxil_value(&dst_param->reg, &src_phi->value, 0, sm6); - } + return VKD3D_OK; } static enum vkd3d_result sm6_parser_module_init(struct sm6_parser *sm6, const struct dxil_block *block, @@ -8636,7 +8537,6 @@ static enum vkd3d_result sm6_function_emit_blocks(const struct sm6_function *fun const struct sm6_block *block = function->blocks[i]; sm6_parser_emit_label(sm6, block->id); - sm6_block_emit_phi(block, sm6); for (j = 0; j < block->instruction_count; ++j) { @@ -10629,19 +10529,9 @@ static void sm6_symtab_cleanup(struct sm6_symbol *symbols, size_t count) vkd3d_free(symbols); } -static void sm6_phi_destroy(struct sm6_phi *phi) -{ - vkd3d_free(phi->incoming); -} - static void sm6_block_destroy(struct sm6_block *block) { - unsigned int i; - vkd3d_free(block->instructions); - for (i = 0; i < block->phi_count; ++i) - sm6_phi_destroy(&block->phi[i]); - vkd3d_free(block->phi); vkd3d_free(block); }