Handle early reads from loop variables with initializers.

Need to consider that static_expression can be 0. Fallback as necessary.
Also need to clear the static expression.
This commit is contained in:
Hans-Kristian Arntzen 2022-04-28 14:36:53 +02:00
parent c52333b984
commit 40b3053b4c
5 changed files with 603 additions and 1 deletions

View File

@ -0,0 +1,107 @@
#version 450
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout(binding = 4, std430) buffer _2_12
{
uint _m0[1];
} _12;
layout(binding = 1, std430) buffer _2_9
{
uint _m0[1];
} _9;
layout(binding = 2, std430) buffer _2_10
{
uint _m0[1];
} _10;
layout(binding = 3, std430) buffer _2_11
{
uint _m0[1];
} _11;
layout(binding = 0, std430) buffer _4_8
{
uint _m0[2];
} _8;
layout(binding = 5, std430) buffer _6_13
{
uint _m0[11];
} _13;
void main()
{
uint _43 = 0u;
uint _44 = 0u;
uint _45 = 0u;
uint _46 = 0u;
uint _47 = 0u;
_13._m0[0u] = 8u;
uint _50 = 0u + 1u;
uint _42 = _50;
for (;;)
{
_13._m0[_42] = 9u;
_42++;
uint _55 = _43;
_43 = _55 + 1u;
if (_8._m0[_55] == 1u)
{
_13._m0[_42] = 12u;
_42++;
return;
}
else
{
_13._m0[_42] = 13u;
_42++;
uint _70 = _44;
_44 = _70 + 1u;
if (_9._m0[_70] == 1u)
{
_13._m0[_42] = 11u;
_42++;
_13._m0[_42] = 14u;
_42++;
_45++;
do
{
_13._m0[_42] = 16u;
_42++;
break;
} while(false);
_13._m0[_42] = 15u;
_42++;
uint _94 = _46;
_46 = _94 + 1u;
if (_11._m0[_94] == 1u)
{
}
else
{
_13._m0[_42] = 19u;
_42++;
}
_13._m0[_42] = 17u;
_42++;
uint _108 = _47;
_47 = _108 + 1u;
if (_12._m0[_108] == 1u)
{
continue;
}
else
{
break;
}
}
else
{
break;
}
}
}
}

View File

@ -0,0 +1,108 @@
#version 450
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout(binding = 4, std430) buffer _2_12
{
uint _m0[1];
} _12;
layout(binding = 1, std430) buffer _2_9
{
uint _m0[1];
} _9;
layout(binding = 2, std430) buffer _2_10
{
uint _m0[1];
} _10;
layout(binding = 3, std430) buffer _2_11
{
uint _m0[1];
} _11;
layout(binding = 0, std430) buffer _4_8
{
uint _m0[2];
} _8;
layout(binding = 5, std430) buffer _6_13
{
uint _m0[11];
} _13;
void main()
{
uint _43 = 0u;
uint _44 = 0u;
uint _45 = 0u;
uint _46 = 0u;
uint _47 = 0u;
uint _115;
_13._m0[_115] = 8u;
uint _50 = _115 + 1u;
uint _42 = _50;
for (;;)
{
_13._m0[_42] = 9u;
_42++;
uint _55 = _43;
_43 = _55 + 1u;
if (_8._m0[_55] == 1u)
{
_13._m0[_42] = 12u;
_42++;
return;
}
else
{
_13._m0[_42] = 13u;
_42++;
uint _70 = _44;
_44 = _70 + 1u;
if (_9._m0[_70] == 1u)
{
_13._m0[_42] = 11u;
_42++;
_13._m0[_42] = 14u;
_42++;
_45++;
do
{
_13._m0[_42] = 16u;
_42++;
break;
} while(false);
_13._m0[_42] = 15u;
_42++;
uint _94 = _46;
_46 = _94 + 1u;
if (_11._m0[_94] == 1u)
{
}
else
{
_13._m0[_42] = 19u;
_42++;
}
_13._m0[_42] = 17u;
_42++;
uint _108 = _47;
_47 = _108 + 1u;
if (_12._m0[_108] == 1u)
{
continue;
}
else
{
break;
}
}
else
{
break;
}
}
}
}

View File

@ -0,0 +1,185 @@
; SPIR-V
; Version: 1.3
; Generator: Khronos SPIR-V Tools Assembler; 0
; Bound: 114
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %7 "main"
OpExecutionMode %7 LocalSize 1 1 1
OpDecorate %_struct_21 BufferBlock
OpMemberDecorate %_struct_21 0 Offset 0
OpDecorate %_arr_uint_uint_1 ArrayStride 4
OpDecorate %_struct_23 BufferBlock
OpMemberDecorate %_struct_23 0 Offset 0
OpDecorate %_arr_uint_uint_2 ArrayStride 4
OpDecorate %_struct_25 BufferBlock
OpMemberDecorate %_struct_25 0 Offset 0
OpDecorate %_arr_uint_uint_11 ArrayStride 4
OpDecorate %27 DescriptorSet 0
OpDecorate %27 Binding 0
OpDecorate %28 DescriptorSet 0
OpDecorate %28 Binding 1
OpDecorate %29 DescriptorSet 0
OpDecorate %29 Binding 2
OpDecorate %30 DescriptorSet 0
OpDecorate %30 Binding 3
OpDecorate %31 DescriptorSet 0
OpDecorate %31 Binding 4
OpDecorate %32 DescriptorSet 0
OpDecorate %32 Binding 5
%void = OpTypeVoid
%2 = OpTypeFunction %void
%bool = OpTypeBool
%uint = OpTypeInt 32 0
%true = OpConstantTrue %bool
%uint_0 = OpConstant %uint 0
%uint_0_0 = OpConstant %uint 0
%uint_1 = OpConstant %uint 1
%uint_2 = OpConstant %uint 2
%uint_8 = OpConstant %uint 8
%uint_9 = OpConstant %uint 9
%uint_10 = OpConstant %uint 10
%uint_11 = OpConstant %uint 11
%uint_12 = OpConstant %uint 12
%uint_13 = OpConstant %uint 13
%uint_14 = OpConstant %uint 14
%uint_15 = OpConstant %uint 15
%uint_16 = OpConstant %uint 16
%uint_17 = OpConstant %uint 17
%uint_18 = OpConstant %uint 18
%uint_19 = OpConstant %uint 19
%uint_20 = OpConstant %uint 20
%_arr_uint_uint_1 = OpTypeArray %uint %uint_1
%_struct_21 = OpTypeStruct %_arr_uint_uint_1
%_ptr_Uniform__struct_21 = OpTypePointer Uniform %_struct_21
%31 = OpVariable %_ptr_Uniform__struct_21 Uniform
%28 = OpVariable %_ptr_Uniform__struct_21 Uniform
%29 = OpVariable %_ptr_Uniform__struct_21 Uniform
%30 = OpVariable %_ptr_Uniform__struct_21 Uniform
%_arr_uint_uint_2 = OpTypeArray %uint %uint_2
%_struct_23 = OpTypeStruct %_arr_uint_uint_2
%_ptr_Uniform__struct_23 = OpTypePointer Uniform %_struct_23
%27 = OpVariable %_ptr_Uniform__struct_23 Uniform
%_arr_uint_uint_11 = OpTypeArray %uint %uint_11
%_struct_25 = OpTypeStruct %_arr_uint_uint_11
%_ptr_Uniform__struct_25 = OpTypePointer Uniform %_struct_25
%32 = OpVariable %_ptr_Uniform__struct_25 Uniform
%_ptr_Function_uint = OpTypePointer Function %uint
%_ptr_Uniform_uint = OpTypePointer Uniform %uint
%7 = OpFunction %void None %2
%8 = OpLabel
%54 = OpVariable %_ptr_Function_uint Function %uint_0_0
%55 = OpVariable %_ptr_Function_uint Function %uint_0_0
%56 = OpVariable %_ptr_Function_uint Function %uint_0_0
%57 = OpVariable %_ptr_Function_uint Function %uint_0_0
%58 = OpVariable %_ptr_Function_uint Function %uint_0_0
%59 = OpVariable %_ptr_Function_uint Function %uint_0_0
%60 = OpLoad %uint %54
%61 = OpAccessChain %_ptr_Uniform_uint %32 %uint_0_0 %60
OpStore %61 %uint_8
%62 = OpIAdd %uint %60 %uint_1
OpStore %54 %62
OpBranch %9
%9 = OpLabel
%63 = OpLoad %uint %54
%64 = OpAccessChain %_ptr_Uniform_uint %32 %uint_0_0 %63
OpStore %64 %uint_9
%65 = OpIAdd %uint %63 %uint_1
OpStore %54 %65
%66 = OpLoad %uint %55
%67 = OpAccessChain %_ptr_Uniform_uint %27 %uint_0_0 %66
%68 = OpLoad %uint %67
%69 = OpIEqual %bool %68 %uint_1
%70 = OpIAdd %uint %66 %uint_1
OpStore %55 %70
OpLoopMerge %10 %11 None
OpBranchConditional %69 %12 %13
%12 = OpLabel
%71 = OpLoad %uint %54
%72 = OpAccessChain %_ptr_Uniform_uint %32 %uint_0_0 %71
OpStore %72 %uint_12
%73 = OpIAdd %uint %71 %uint_1
OpStore %54 %73
OpReturn
%13 = OpLabel
%74 = OpLoad %uint %54
%75 = OpAccessChain %_ptr_Uniform_uint %32 %uint_0_0 %74
OpStore %75 %uint_13
%76 = OpIAdd %uint %74 %uint_1
OpStore %54 %76
%77 = OpLoad %uint %56
%78 = OpAccessChain %_ptr_Uniform_uint %28 %uint_0_0 %77
%79 = OpLoad %uint %78
%80 = OpIEqual %bool %79 %uint_1
%81 = OpIAdd %uint %77 %uint_1
OpStore %56 %81
OpBranchConditional %80 %11 %10
%11 = OpLabel
%82 = OpLoad %uint %54
%83 = OpAccessChain %_ptr_Uniform_uint %32 %uint_0_0 %82
OpStore %83 %uint_11
%84 = OpIAdd %uint %82 %uint_1
OpStore %54 %84
OpBranch %14
%14 = OpLabel
%85 = OpLoad %uint %54
%86 = OpAccessChain %_ptr_Uniform_uint %32 %uint_0_0 %85
OpStore %86 %uint_14
%87 = OpIAdd %uint %85 %uint_1
OpStore %54 %87
%88 = OpLoad %uint %57
%89 = OpAccessChain %_ptr_Uniform_uint %29 %uint_0_0 %88
%90 = OpLoad %uint %89
%91 = OpIAdd %uint %88 %uint_1
OpStore %57 %91
OpSelectionMerge %15 None
OpSwitch %90 %16
%16 = OpLabel
%92 = OpLoad %uint %54
%93 = OpAccessChain %_ptr_Uniform_uint %32 %uint_0_0 %92
OpStore %93 %uint_16
%94 = OpIAdd %uint %92 %uint_1
OpStore %54 %94
OpBranch %15
%15 = OpLabel
%95 = OpLoad %uint %54
%96 = OpAccessChain %_ptr_Uniform_uint %32 %uint_0_0 %95
OpStore %96 %uint_15
%97 = OpIAdd %uint %95 %uint_1
OpStore %54 %97
%98 = OpLoad %uint %58
%99 = OpAccessChain %_ptr_Uniform_uint %30 %uint_0_0 %98
%100 = OpLoad %uint %99
%101 = OpIEqual %bool %100 %uint_1
%102 = OpIAdd %uint %98 %uint_1
OpStore %58 %102
OpSelectionMerge %17 None
OpBranchConditional %101 %18 %19
%18 = OpLabel
OpBranch %17
%19 = OpLabel
%103 = OpLoad %uint %54
%104 = OpAccessChain %_ptr_Uniform_uint %32 %uint_0_0 %103
OpStore %104 %uint_19
%105 = OpIAdd %uint %103 %uint_1
OpStore %54 %105
OpBranch %17
%17 = OpLabel
%106 = OpLoad %uint %54
%107 = OpAccessChain %_ptr_Uniform_uint %32 %uint_0_0 %106
OpStore %107 %uint_17
%108 = OpIAdd %uint %106 %uint_1
OpStore %54 %108
%109 = OpLoad %uint %59
%110 = OpAccessChain %_ptr_Uniform_uint %31 %uint_0_0 %109
%111 = OpLoad %uint %110
%112 = OpIEqual %bool %111 %uint_1
%113 = OpIAdd %uint %109 %uint_1
OpStore %59 %113
OpBranchConditional %112 %9 %10
%10 = OpLabel
OpBranch %20
%20 = OpLabel
OpReturn
OpFunctionEnd

View File

@ -0,0 +1,185 @@
; SPIR-V
; Version: 1.3
; Generator: Khronos SPIR-V Tools Assembler; 0
; Bound: 114
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %7 "main"
OpExecutionMode %7 LocalSize 1 1 1
OpDecorate %_struct_21 BufferBlock
OpMemberDecorate %_struct_21 0 Offset 0
OpDecorate %_arr_uint_uint_1 ArrayStride 4
OpDecorate %_struct_23 BufferBlock
OpMemberDecorate %_struct_23 0 Offset 0
OpDecorate %_arr_uint_uint_2 ArrayStride 4
OpDecorate %_struct_25 BufferBlock
OpMemberDecorate %_struct_25 0 Offset 0
OpDecorate %_arr_uint_uint_11 ArrayStride 4
OpDecorate %27 DescriptorSet 0
OpDecorate %27 Binding 0
OpDecorate %28 DescriptorSet 0
OpDecorate %28 Binding 1
OpDecorate %29 DescriptorSet 0
OpDecorate %29 Binding 2
OpDecorate %30 DescriptorSet 0
OpDecorate %30 Binding 3
OpDecorate %31 DescriptorSet 0
OpDecorate %31 Binding 4
OpDecorate %32 DescriptorSet 0
OpDecorate %32 Binding 5
%void = OpTypeVoid
%2 = OpTypeFunction %void
%bool = OpTypeBool
%uint = OpTypeInt 32 0
%true = OpConstantTrue %bool
%uint_0 = OpConstant %uint 0
%uint_0_0 = OpConstant %uint 0
%uint_1 = OpConstant %uint 1
%uint_2 = OpConstant %uint 2
%uint_8 = OpConstant %uint 8
%uint_9 = OpConstant %uint 9
%uint_10 = OpConstant %uint 10
%uint_11 = OpConstant %uint 11
%uint_12 = OpConstant %uint 12
%uint_13 = OpConstant %uint 13
%uint_14 = OpConstant %uint 14
%uint_15 = OpConstant %uint 15
%uint_16 = OpConstant %uint 16
%uint_17 = OpConstant %uint 17
%uint_18 = OpConstant %uint 18
%uint_19 = OpConstant %uint 19
%uint_20 = OpConstant %uint 20
%_arr_uint_uint_1 = OpTypeArray %uint %uint_1
%_struct_21 = OpTypeStruct %_arr_uint_uint_1
%_ptr_Uniform__struct_21 = OpTypePointer Uniform %_struct_21
%31 = OpVariable %_ptr_Uniform__struct_21 Uniform
%28 = OpVariable %_ptr_Uniform__struct_21 Uniform
%29 = OpVariable %_ptr_Uniform__struct_21 Uniform
%30 = OpVariable %_ptr_Uniform__struct_21 Uniform
%_arr_uint_uint_2 = OpTypeArray %uint %uint_2
%_struct_23 = OpTypeStruct %_arr_uint_uint_2
%_ptr_Uniform__struct_23 = OpTypePointer Uniform %_struct_23
%27 = OpVariable %_ptr_Uniform__struct_23 Uniform
%_arr_uint_uint_11 = OpTypeArray %uint %uint_11
%_struct_25 = OpTypeStruct %_arr_uint_uint_11
%_ptr_Uniform__struct_25 = OpTypePointer Uniform %_struct_25
%32 = OpVariable %_ptr_Uniform__struct_25 Uniform
%_ptr_Function_uint = OpTypePointer Function %uint
%_ptr_Uniform_uint = OpTypePointer Uniform %uint
%7 = OpFunction %void None %2
%8 = OpLabel
%54 = OpVariable %_ptr_Function_uint Function
%55 = OpVariable %_ptr_Function_uint Function %uint_0_0
%56 = OpVariable %_ptr_Function_uint Function %uint_0_0
%57 = OpVariable %_ptr_Function_uint Function %uint_0_0
%58 = OpVariable %_ptr_Function_uint Function %uint_0_0
%59 = OpVariable %_ptr_Function_uint Function %uint_0_0
%60 = OpLoad %uint %54
%61 = OpAccessChain %_ptr_Uniform_uint %32 %uint_0_0 %60
OpStore %61 %uint_8
%62 = OpIAdd %uint %60 %uint_1
OpStore %54 %62
OpBranch %9
%9 = OpLabel
%63 = OpLoad %uint %54
%64 = OpAccessChain %_ptr_Uniform_uint %32 %uint_0_0 %63
OpStore %64 %uint_9
%65 = OpIAdd %uint %63 %uint_1
OpStore %54 %65
%66 = OpLoad %uint %55
%67 = OpAccessChain %_ptr_Uniform_uint %27 %uint_0_0 %66
%68 = OpLoad %uint %67
%69 = OpIEqual %bool %68 %uint_1
%70 = OpIAdd %uint %66 %uint_1
OpStore %55 %70
OpLoopMerge %10 %11 None
OpBranchConditional %69 %12 %13
%12 = OpLabel
%71 = OpLoad %uint %54
%72 = OpAccessChain %_ptr_Uniform_uint %32 %uint_0_0 %71
OpStore %72 %uint_12
%73 = OpIAdd %uint %71 %uint_1
OpStore %54 %73
OpReturn
%13 = OpLabel
%74 = OpLoad %uint %54
%75 = OpAccessChain %_ptr_Uniform_uint %32 %uint_0_0 %74
OpStore %75 %uint_13
%76 = OpIAdd %uint %74 %uint_1
OpStore %54 %76
%77 = OpLoad %uint %56
%78 = OpAccessChain %_ptr_Uniform_uint %28 %uint_0_0 %77
%79 = OpLoad %uint %78
%80 = OpIEqual %bool %79 %uint_1
%81 = OpIAdd %uint %77 %uint_1
OpStore %56 %81
OpBranchConditional %80 %11 %10
%11 = OpLabel
%82 = OpLoad %uint %54
%83 = OpAccessChain %_ptr_Uniform_uint %32 %uint_0_0 %82
OpStore %83 %uint_11
%84 = OpIAdd %uint %82 %uint_1
OpStore %54 %84
OpBranch %14
%14 = OpLabel
%85 = OpLoad %uint %54
%86 = OpAccessChain %_ptr_Uniform_uint %32 %uint_0_0 %85
OpStore %86 %uint_14
%87 = OpIAdd %uint %85 %uint_1
OpStore %54 %87
%88 = OpLoad %uint %57
%89 = OpAccessChain %_ptr_Uniform_uint %29 %uint_0_0 %88
%90 = OpLoad %uint %89
%91 = OpIAdd %uint %88 %uint_1
OpStore %57 %91
OpSelectionMerge %15 None
OpSwitch %90 %16
%16 = OpLabel
%92 = OpLoad %uint %54
%93 = OpAccessChain %_ptr_Uniform_uint %32 %uint_0_0 %92
OpStore %93 %uint_16
%94 = OpIAdd %uint %92 %uint_1
OpStore %54 %94
OpBranch %15
%15 = OpLabel
%95 = OpLoad %uint %54
%96 = OpAccessChain %_ptr_Uniform_uint %32 %uint_0_0 %95
OpStore %96 %uint_15
%97 = OpIAdd %uint %95 %uint_1
OpStore %54 %97
%98 = OpLoad %uint %58
%99 = OpAccessChain %_ptr_Uniform_uint %30 %uint_0_0 %98
%100 = OpLoad %uint %99
%101 = OpIEqual %bool %100 %uint_1
%102 = OpIAdd %uint %98 %uint_1
OpStore %58 %102
OpSelectionMerge %17 None
OpBranchConditional %101 %18 %19
%18 = OpLabel
OpBranch %17
%19 = OpLabel
%103 = OpLoad %uint %54
%104 = OpAccessChain %_ptr_Uniform_uint %32 %uint_0_0 %103
OpStore %104 %uint_19
%105 = OpIAdd %uint %103 %uint_1
OpStore %54 %105
OpBranch %17
%17 = OpLabel
%106 = OpLoad %uint %54
%107 = OpAccessChain %_ptr_Uniform_uint %32 %uint_0_0 %106
OpStore %107 %uint_17
%108 = OpIAdd %uint %106 %uint_1
OpStore %54 %108
%109 = OpLoad %uint %59
%110 = OpAccessChain %_ptr_Uniform_uint %31 %uint_0_0 %109
%111 = OpLoad %uint %110
%112 = OpIEqual %bool %111 %uint_1
%113 = OpIAdd %uint %109 %uint_1
OpStore %59 %113
OpBranchConditional %112 %9 %10
%10 = OpLabel
OpBranch %20
%20 = OpLabel
OpReturn
OpFunctionEnd

View File

@ -4710,7 +4710,20 @@ string CompilerGLSL::to_expression(uint32_t id, bool register_expression_read)
// 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);
{
// We might try to load from a loop variable before it has been initialized.
// Prefer static expression and fallback to initializer.
if (var.static_expression)
return to_expression(var.static_expression);
else if (var.initializer)
return to_expression(var.initializer);
else
{
// We cannot declare the variable yet, so have to fake it.
uint32_t undef_id = ir.increase_bound_by(1);
return emit_uninitialized_temporary_expression(get_variable_data_type_id(var), undef_id).expression;
}
}
else if (var.deferred_declaration)
{
var.deferred_declaration = false;
@ -14127,7 +14140,11 @@ void CompilerGLSL::emit_function(SPIRFunction &func, const Bitset &return_flags)
// Loop variables are never declared outside their for-loop, so block any implicit declaration.
if (var.loop_variable)
{
var.deferred_declaration = false;
// Need to reset the static expression so we can fallback to initializer if need be.
var.static_expression = 0;
}
}
// Enforce declaration order for regression testing purposes.