gallivm: Fix a long standing bug with nested if-then-else emission.
authorJosé Fonseca <jfonseca@vmware.com>
Sun, 10 Oct 2010 17:45:14 +0000 (18:45 +0100)
committerJosé Fonseca <jfonseca@vmware.com>
Sun, 10 Oct 2010 17:48:02 +0000 (18:48 +0100)
We can't patch true-block at end-if time, as there is no guarantee that
the block at the beginning of the true stanza is the same at the end of
the true stanza -- other control flow elements may have been emitted half
way the true stanza.

Although this bug surfaced recently with the commit to skip mip filtering
when lod is an integer the bug was always there, although probably it
was avoided until now: e.g., cubemap selection nests if-then-else on the
else stanza, which does not suffer from the same problem.

src/gallium/auxiliary/gallivm/lp_bld_flow.c

index 99a49df317c05583da7cc6eae9daf2942c3f6900..9d1a74f5d16c0cde4839da5b625735da9d5758ea 100644 (file)
@@ -320,7 +320,6 @@ lp_build_if(struct lp_build_if_state *ifthen,
 
    /* create endif/merge basic block for the phi functions */
    ifthen->merge_block = lp_build_insert_new_block(builder, "endif-block");
-   LLVMPositionBuilderAtEnd(builder, ifthen->merge_block);
 
    /* create/insert true_block before merge_block */
    ifthen->true_block = LLVMInsertBasicBlock(ifthen->merge_block, "if-true-block");
@@ -336,6 +335,9 @@ lp_build_if(struct lp_build_if_state *ifthen,
 void
 lp_build_else(struct lp_build_if_state *ifthen)
 {
+   /* Append an unconditional Br(anch) instruction on the true_block */
+   LLVMBuildBr(ifthen->builder, ifthen->merge_block);
+
    /* create/insert false_block before the merge block */
    ifthen->false_block = LLVMInsertBasicBlock(ifthen->merge_block, "if-false-block");
 
@@ -353,9 +355,9 @@ lp_build_endif(struct lp_build_if_state *ifthen)
    /* Insert branch to the merge block from current block */
    LLVMBuildBr(ifthen->builder, ifthen->merge_block);
 
-   /***
-    *** Now patch in the various branch instructions.
-    ***/
+   /*
+    * Now patch in the various branch instructions.
+    */
 
    /* Insert the conditional branch instruction at the end of entry_block */
    LLVMPositionBuilderAtEnd(ifthen->builder, ifthen->entry_block);
@@ -370,19 +372,6 @@ lp_build_endif(struct lp_build_if_state *ifthen)
                       ifthen->true_block, ifthen->merge_block);
    }
 
-   /* Insert branch from end of true_block to merge_block */
-   if (ifthen->false_block) {
-      /* Append an unconditional Br(anch) instruction on the true_block */
-      LLVMPositionBuilderAtEnd(ifthen->builder, ifthen->true_block);
-      LLVMBuildBr(ifthen->builder, ifthen->merge_block);
-   }
-   else {
-      /* No else clause.
-       * Note that we've already inserted the branch at the end of
-       * true_block.  See the very first LLVMBuildBr() call in this function.
-       */
-   }
-
    /* Resume building code at end of the ifthen->merge_block */
    LLVMPositionBuilderAtEnd(ifthen->builder, ifthen->merge_block);
 }