zink/spirv: prepare for control-flow
authorErik Faye-Lund <erik.faye-lund@collabora.com>
Tue, 30 Oct 2018 09:43:53 +0000 (10:43 +0100)
committerErik Faye-Lund <erik.faye-lund@collabora.com>
Mon, 28 Oct 2019 08:51:43 +0000 (08:51 +0000)
Acked-by: Jordan Justen <jordan.l.justen@intel.com>
src/gallium/drivers/zink/nir_to_spirv/nir_to_spirv.c
src/gallium/drivers/zink/zink_compiler.c

index c18a57cd5a5ad275b9485d69cad734b9ffe78672..099f78a12550ae7fd550fbc058c078d40605f129 100644 (file)
@@ -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
index e243166e23d5963aa37b9727ab2f4c40cfddf52b..27a1e6e68f5db0c0a0897c51597e2d1a6128f645 100644 (file)
@@ -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");