/* * HLSL optimization and code generation * * Copyright 2019-2020 Zebediah Figura 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" /* Allocate a unique, ordered index to each instruction, which will be used for * computing liveness ranges. */ static unsigned int index_instructions(struct list *instrs, unsigned int index) { struct hlsl_ir_node *instr; LIST_FOR_EACH_ENTRY(instr, instrs, struct hlsl_ir_node, entry) { instr->index = index++; if (instr->type == HLSL_IR_IF) { struct hlsl_ir_if *iff = hlsl_ir_if(instr); index = index_instructions(&iff->then_instrs, index); index = index_instructions(&iff->else_instrs, index); } else if (instr->type == HLSL_IR_LOOP) { index = index_instructions(&hlsl_ir_loop(instr)->body, index); hlsl_ir_loop(instr)->next_index = index; } } return index; } static void dump_function_decl(struct rb_entry *entry, void *context) { struct hlsl_ir_function_decl *func = RB_ENTRY_VALUE(entry, struct hlsl_ir_function_decl, entry); if (func->body) hlsl_dump_function(func); } static void dump_function(struct rb_entry *entry, void *context) { struct hlsl_ir_function *func = RB_ENTRY_VALUE(entry, struct hlsl_ir_function, entry); rb_for_each_entry(&func->overloads, dump_function_decl, NULL); } /* Compute the earliest and latest liveness for each variable. In the case that * a variable is accessed inside of a loop, we promote its liveness to extend * to at least the range of the entire loop. Note that we don't need to do this * for anonymous nodes, since there's currently no way to use a node which was * calculated in an earlier iteration of the loop. */ static void compute_liveness_recurse(struct list *instrs, unsigned int loop_first, unsigned int loop_last) { struct hlsl_ir_node *instr; struct hlsl_ir_var *var; 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); var = assignment->lhs.var; if (!var->first_write) var->first_write = loop_first ? min(instr->index, loop_first) : instr->index; assignment->rhs.node->last_read = instr->index; if (assignment->lhs.offset.node) assignment->lhs.offset.node->last_read = instr->index; break; } case HLSL_IR_EXPR: { struct hlsl_ir_expr *expr = hlsl_ir_expr(instr); unsigned int i; for (i = 0; i < ARRAY_SIZE(expr->operands) && expr->operands[i].node; ++i) expr->operands[i].node->last_read = instr->index; break; } case HLSL_IR_IF: { struct hlsl_ir_if *iff = hlsl_ir_if(instr); compute_liveness_recurse(&iff->then_instrs, loop_first, loop_last); compute_liveness_recurse(&iff->else_instrs, loop_first, loop_last); iff->condition.node->last_read = instr->index; break; } case HLSL_IR_LOAD: { struct hlsl_ir_load *load = hlsl_ir_load(instr); var = load->src.var; var->last_read = loop_last ? max(instr->index, loop_last) : instr->index; if (load->src.offset.node) load->src.offset.node->last_read = instr->index; break; } case HLSL_IR_LOOP: { struct hlsl_ir_loop *loop = hlsl_ir_loop(instr); compute_liveness_recurse(&loop->body, loop_first ? loop_first : instr->index, loop_last ? loop_last : loop->next_index); break; } case HLSL_IR_SWIZZLE: { struct hlsl_ir_swizzle *swizzle = hlsl_ir_swizzle(instr); swizzle->val.node->last_read = instr->index; break; } case HLSL_IR_CONSTANT: case HLSL_IR_JUMP: break; } } } static void compute_liveness(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry_func) { struct hlsl_ir_var *var; LIST_FOR_EACH_ENTRY(var, &ctx->globals->vars, struct hlsl_ir_var, scope_entry) { var->first_write = 1; } LIST_FOR_EACH_ENTRY(var, entry_func->parameters, struct hlsl_ir_var, param_entry) { if (var->modifiers & HLSL_STORAGE_IN) var->first_write = 1; if (var->modifiers & HLSL_STORAGE_OUT) var->last_read = UINT_MAX; } if (entry_func->return_var) entry_func->return_var->last_read = UINT_MAX; compute_liveness_recurse(entry_func->body, 0, 0); } int hlsl_emit_dxbc(struct hlsl_ctx *ctx, struct hlsl_ir_function_decl *entry_func) { list_move_head(entry_func->body, &ctx->static_initializers); /* Index 0 means unused; index 1 means function entry, so start at 2. */ index_instructions(entry_func->body, 2); if (TRACE_ON()) rb_for_each_entry(&ctx->functions, dump_function, NULL); compute_liveness(ctx, entry_func); if (ctx->failed) return VKD3D_ERROR_INVALID_SHADER; return VKD3D_ERROR_NOT_IMPLEMENTED; }