From: Erik Faye-Lund Date: Tue, 30 Oct 2018 09:43:53 +0000 (+0100) Subject: zink/spirv: prepare for control-flow X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=8bbf86e7bce6d9f0297b84bbfc5c11fdd104f025;p=mesa.git zink/spirv: prepare for control-flow Acked-by: Jordan Justen --- diff --git a/src/gallium/drivers/zink/nir_to_spirv/nir_to_spirv.c b/src/gallium/drivers/zink/nir_to_spirv/nir_to_spirv.c index c18a57cd5a5..099f78a1255 100644 --- a/src/gallium/drivers/zink/nir_to_spirv/nir_to_spirv.c +++ b/src/gallium/drivers/zink/nir_to_spirv/nir_to_spirv.c @@ -50,6 +50,10 @@ struct ntv_context { size_t num_defs; struct hash_table *vars; + + const SpvId *block_ids; + size_t num_blocks; + bool block_started; }; static SpvId @@ -83,6 +87,13 @@ get_bvec_type(struct ntv_context *ctx, int num_components) return bool_type; } +static SpvId +block_label(struct ntv_context *ctx, nir_block *block) +{ + assert(block->index < ctx->num_blocks); + return ctx->block_ids[block->index]; +} + static SpvId get_fvec_type(struct ntv_context *ctx, unsigned bit_size, unsigned num_components) { @@ -1128,9 +1139,20 @@ emit_tex(struct ntv_context *ctx, nir_tex_instr *tex) store_dest(ctx, &tex->dest, result, tex->dest_type); } +static void +start_block(struct ntv_context *ctx, SpvId label) +{ + assert(!ctx->block_started); + + /* start new block */ + spirv_builder_label(&ctx->builder, label); + ctx->block_started = true; +} + static void emit_block(struct ntv_context *ctx, struct nir_block *block) { + start_block(ctx, block_label(ctx, block)); nir_foreach_instr(instr, block) { switch (instr->type) { case nir_instr_type_alu: @@ -1253,7 +1275,6 @@ nir_to_spirv(struct nir_shader *s) SpvId type_main = spirv_builder_type_function(&ctx.builder, type_void, NULL, 0); SpvId entry_point = spirv_builder_new_id(&ctx.builder); - SpvId label = spirv_builder_new_id(&ctx.builder); spirv_builder_emit_name(&ctx.builder, entry_point, "main"); nir_foreach_variable(var, &s->inputs) @@ -1276,9 +1297,9 @@ nir_to_spirv(struct nir_shader *s) spirv_builder_function(&ctx.builder, entry_point, type_void, SpvFunctionControlMaskNone, type_main); - spirv_builder_label(&ctx.builder, label); nir_function_impl *entry = nir_shader_get_entrypoint(s); + nir_metadata_require(entry, nir_metadata_block_index); ctx.defs = (SpvId *)malloc(sizeof(SpvId) * entry->ssa_alloc); if (!ctx.defs) @@ -1290,7 +1311,18 @@ nir_to_spirv(struct nir_shader *s) if (!ctx.vars) goto fail; + SpvId *block_ids = (SpvId *)malloc(sizeof(SpvId) * entry->num_blocks); + if (!block_ids) + goto fail; + + for (int i = 0; i < entry->num_blocks; ++i) + block_ids[i] = spirv_builder_new_id(&ctx.builder); + + ctx.block_ids = block_ids; + ctx.num_blocks = entry->num_blocks; + emit_cf_list(&ctx, &entry->body); + free(ctx.defs); spirv_builder_return(&ctx.builder); // doesn't belong here, but whatevz diff --git a/src/gallium/drivers/zink/zink_compiler.c b/src/gallium/drivers/zink/zink_compiler.c index e243166e23d..27a1e6e68f5 100644 --- a/src/gallium/drivers/zink/zink_compiler.c +++ b/src/gallium/drivers/zink/zink_compiler.c @@ -303,6 +303,7 @@ zink_compile_nir(struct zink_screen *screen, struct nir_shader *nir) optimize_nir(nir); NIR_PASS_V(nir, nir_remove_dead_variables, nir_var_function_temp); NIR_PASS_V(nir, lower_discard_if); + NIR_PASS_V(nir, nir_convert_from_ssa, true); if (zink_debug & ZINK_DEBUG_NIR) { fprintf(stderr, "NIR shader:\n---8<---\n");