Add directed test for for-loop-init.

This commit is contained in:
Hans-Kristian Arntzen 2016-12-16 12:43:12 +01:00
parent 51d45511a6
commit a714d424d0
4 changed files with 115 additions and 12 deletions

View File

@ -0,0 +1,52 @@
#version 310 es
precision mediump float;
precision highp int;
layout(location = 0) out mediump int FragColor;
void main()
{
FragColor = 15;
for (mediump int i = 0; i < 25; i = i + 1)
{
FragColor = FragColor + 10;
}
for (mediump int j = 4, i_1 = 1; i_1 < 30; i_1 = i_1 + 1, j = j + 4)
{
FragColor = FragColor + 11;
}
mediump int k = 0;
for (; k < 20; k = k + 1)
{
FragColor = FragColor + 12;
}
k = k + 3;
FragColor = FragColor + k;
mediump int l;
if (k == 40)
{
l = 0;
for (; l < 40; l = l + 1)
{
FragColor = FragColor + 13;
}
return;
}
else
{
l = k;
FragColor = FragColor + l;
}
mediump ivec2 i_2 = ivec2(0);
for (; i_2.x < 10; i_2.x = i_2.x + 1)
{
FragColor = FragColor + i_2.y;
}
mediump int o = k;
for (mediump int m = k; m < 40; m = m + 1)
{
FragColor = FragColor + m;
}
FragColor = FragColor + o;
}

View File

@ -0,0 +1,52 @@
#version 310 es
precision mediump float;
layout(location = 0) out int FragColor;
void main()
{
FragColor = 15;
// Basic loop variable.
for (int i = 0; i < 25; i++)
FragColor += 10;
// Multiple loop variables.
for (int i = 1, j = 4; i < 30; i++, j += 4)
FragColor += 11;
// A potential loop variables, but we access it outside the loop,
// so cannot be one.
int k = 0;
for (; k < 20; k++)
FragColor += 12;
k += 3;
FragColor += k;
// Potential loop variables, but the dominator is not trivial.
int l;
if (k == 40)
{
for (l = 0; l < 40; l++)
FragColor += 13;
return;
}
else
{
l = k;
FragColor += l;
}
// Vectors cannot be loop variables
for (ivec2 i = ivec2(0); i.x < 10; i.x++)
{
FragColor += i.y;
}
// Check that static expressions can be used before the loop header.
int m = 0;
m = k;
int o = m;
for (; m < 40; m++)
FragColor += m;
FragColor += o;
}

View File

@ -2916,13 +2916,14 @@ void Compiler::analyze_variable_scope(SPIRFunction &entry)
{
DominatorBuilder builder(cfg);
auto &blocks = var.second;
auto &type = expression_type(var.first);
// Figure out which block is dominating all accesses of those variables.
for (auto &block : blocks)
{
// If we're accessing a variable inside a continue block, this variable
// might be a loop variable.
if (is_continue(block))
// If we're accessing a variable inside a continue block, this variable might be a loop variable.
// We can only use loop variables with scalars, as we cannot track static expressions for vectors.
if (is_continue(block) && type.vecsize == 1 && type.columns == 1)
{
// The variable is used in multiple continue blocks, this is not a loop
// candidate, signal that by setting block to -1u.

View File

@ -1565,7 +1565,9 @@ string CompilerGLSL::to_expression(uint32_t id)
case TypeVariable:
{
auto &var = get<SPIRVariable>(id);
if (var.statically_assigned)
// If we try to use a loop variable before the loop header, we have to redirect it to the static expression,
// the variable has not been declared yet.
if (var.statically_assigned || (var.loop_variable && !var.loop_variable_enable))
return to_expression(var.static_expression);
else if (var.deferred_declaration)
{
@ -5703,10 +5705,6 @@ bool CompilerGLSL::attempt_emit_loop_header(SPIRBlock &block, SPIRBlock::Method
switch (continue_type)
{
case SPIRBlock::ForLoop:
// If we have loop variables, stop masking out access to the variable now.
for (auto var : block.loop_variables)
get<SPIRVariable>(var).loop_variable_enable = true;
statement("for (", emit_for_loop_initializers(block), "; ", to_expression(block.condition), "; ",
emit_continue_block(block.continue_block), ")");
break;
@ -5754,10 +5752,6 @@ bool CompilerGLSL::attempt_emit_loop_header(SPIRBlock &block, SPIRBlock::Method
switch (continue_type)
{
case SPIRBlock::ForLoop:
// If we have loop variables, stop masking out access to the variable now.
for (auto var : block.loop_variables)
get<SPIRVariable>(var).loop_variable_enable = true;
statement("for (", emit_for_loop_initializers(block), "; ", to_expression(child.condition), "; ",
emit_continue_block(block.continue_block), ")");
break;
@ -5817,6 +5811,10 @@ void CompilerGLSL::emit_block_chain(SPIRBlock &block)
if (block.continue_block)
continue_type = continue_block_type(get<SPIRBlock>(block.continue_block));
// If we have loop variables, stop masking out access to the variable now.
for (auto var : block.loop_variables)
get<SPIRVariable>(var).loop_variable_enable = true;
// This is the older loop behavior in glslang which branches to loop body directly from the loop header.
if (block_is_loop_candidate(block, SPIRBlock::MergeToSelectForLoop))
{