gallivm: Fix a long standing bug with nested if-then-else emission.

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.
This commit is contained in:
José Fonseca 2010-10-10 18:45:14 +01:00
parent e2acc7be26
commit 124adf253c
1 changed files with 6 additions and 17 deletions

View 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);
}