Merge pull request #806 from KhronosGroup/fix-793

Fix case where OpPhi is used to swap values.
This commit is contained in:
Hans-Kristian Arntzen 2019-01-07 15:36:23 +01:00 committed by GitHub
commit b30d21bafd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 322 additions and 20 deletions

View File

@ -0,0 +1,39 @@
#version 450
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout(binding = 0, std430) buffer _3_4
{
float _m0[];
} _4;
layout(binding = 1, std430) buffer _3_5
{
float _m0[];
} _5;
void main()
{
bool _34;
float _35;
float _35_copy;
float _36;
_34 = true;
_35 = _4._m0[gl_GlobalInvocationID.x];
_36 = 8.5;
for (;;)
{
if (_34)
{
_34 = false;
_35_copy = _35;
_35 = _36;
_36 = _35_copy;
}
else
{
break;
}
}
_5._m0[gl_GlobalInvocationID.x] = _35 - _36;
}

View File

@ -0,0 +1,29 @@
#version 450
layout(binding = 0, std140) uniform UBO
{
int uCount;
int uJ;
int uK;
} _5;
layout(location = 0) out float FragColor;
void main()
{
int _23;
int _23_copy;
int _24;
_23 = _5.uK;
_24 = _5.uJ;
for (int _26 = 0; _26 < _5.uCount; )
{
_23_copy = _23;
_23 = _24;
_24 = _23_copy;
_26++;
continue;
}
FragColor = float(_24 - _23) * float(_5.uJ * _5.uK);
}

View File

@ -0,0 +1,40 @@
#version 450
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout(binding = 0, std430) buffer _3_4
{
float _m0[];
} _4;
layout(binding = 1, std430) buffer _3_5
{
float _m0[];
} _5;
void main()
{
float _26 = 8.5;
bool _34;
float _35;
float _35_copy;
float _36;
_34 = true;
_35 = _4._m0[gl_GlobalInvocationID.x];
_36 = _26;
for (;;)
{
if (_34)
{
_34 = false;
_35_copy = _35;
_35 = _36;
_36 = _35_copy;
}
else
{
break;
}
}
_5._m0[gl_GlobalInvocationID.x] = _35 - _36;
}

View File

@ -0,0 +1,25 @@
#version 450
layout(binding = 0, std140) uniform UBO
{
int uCount;
int uJ;
int uK;
} _5;
layout(location = 0) out float FragColor;
void main()
{
int _23;
int _23_copy;
int _24;
_23 = _5.uK;
_24 = _5.uJ;
for (int _26 = 0; _26 < _5.uCount; _23_copy = _23, _23 = _24, _24 = _23_copy, _26++)
{
continue;
}
FragColor = float(_24 - _23) * float(_5.uJ * _5.uK);
}

View File

@ -0,0 +1,63 @@
; SPIR-V
; Version: 1.0
; Generator: Khronos SPIR-V Tools Assembler; 0
; Bound: 39
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main" %gl_GlobalInvocationID
OpExecutionMode %main LocalSize 1 1 1
OpName %main "main"
OpName %gl_GlobalInvocationID "gl_GlobalInvocationID"
OpDecorate %gl_GlobalInvocationID BuiltIn GlobalInvocationId
OpDecorate %_struct_3 BufferBlock
OpDecorate %4 DescriptorSet 0
OpDecorate %4 Binding 0
OpDecorate %5 DescriptorSet 0
OpDecorate %5 Binding 1
OpDecorate %_runtimearr_float ArrayStride 4
OpMemberDecorate %_struct_3 0 Offset 0
%bool = OpTypeBool
%void = OpTypeVoid
%9 = OpTypeFunction %void
%uint = OpTypeInt 32 0
%int = OpTypeInt 32 1
%float = OpTypeFloat 32
%v3uint = OpTypeVector %uint 3
%v3float = OpTypeVector %float 3
%_ptr_Input_v3uint = OpTypePointer Input %v3uint
%_ptr_Uniform_int = OpTypePointer Uniform %int
%_ptr_Uniform_float = OpTypePointer Uniform %float
%_runtimearr_int = OpTypeRuntimeArray %int
%_runtimearr_float = OpTypeRuntimeArray %float
%_struct_3 = OpTypeStruct %_runtimearr_float
%_ptr_Uniform__struct_3 = OpTypePointer Uniform %_struct_3
%4 = OpVariable %_ptr_Uniform__struct_3 Uniform
%5 = OpVariable %_ptr_Uniform__struct_3 Uniform
%_ptr_Function_float = OpTypePointer Function %float
%gl_GlobalInvocationID = OpVariable %_ptr_Input_v3uint Input
%true = OpConstantTrue %bool
%false = OpConstantFalse %bool
%int_0 = OpConstant %int 0
%float_8_5 = OpConstant %float 8.5
%main = OpFunction %void None %9
%25 = OpLabel
%26 = OpVariable %_ptr_Function_float Function %float_8_5
%27 = OpLoad %v3uint %gl_GlobalInvocationID
%28 = OpCompositeExtract %uint %27 0
%29 = OpAccessChain %_ptr_Uniform_float %4 %int_0 %28
%30 = OpAccessChain %_ptr_Uniform_float %5 %int_0 %28
%31 = OpLoad %float %29
%32 = OpLoad %float %26
OpBranch %33
%33 = OpLabel
%34 = OpPhi %bool %true %25 %false %33
%35 = OpPhi %float %31 %25 %36 %33
%36 = OpPhi %float %32 %25 %35 %33
OpLoopMerge %37 %33 None
OpBranchConditional %34 %33 %37
%37 = OpLabel
%38 = OpFSub %float %35 %36
OpStore %30 %38
OpReturn
OpFunctionEnd

View File

@ -0,0 +1,69 @@
; SPIR-V
; Version: 1.0
; Generator: Khronos Glslang Reference Front End; 7
; Bound: 55
; Schema: 0
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %FragColor
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 450
OpName %main "main"
OpName %UBO "UBO"
OpMemberName %UBO 0 "uCount"
OpMemberName %UBO 1 "uJ"
OpMemberName %UBO 2 "uK"
OpName %_ ""
OpName %FragColor "FragColor"
OpMemberDecorate %UBO 0 Offset 0
OpMemberDecorate %UBO 1 Offset 4
OpMemberDecorate %UBO 2 Offset 8
OpDecorate %UBO Block
OpDecorate %_ DescriptorSet 0
OpDecorate %_ Binding 0
OpDecorate %FragColor Location 0
%void = OpTypeVoid
%3 = OpTypeFunction %void
%int = OpTypeInt 32 1
%UBO = OpTypeStruct %int %int %int
%_ptr_Uniform_UBO = OpTypePointer Uniform %UBO
%_ = OpVariable %_ptr_Uniform_UBO Uniform
%int_1 = OpConstant %int 1
%_ptr_Uniform_int = OpTypePointer Uniform %int
%int_2 = OpConstant %int 2
%int_0 = OpConstant %int 0
%bool = OpTypeBool
%float = OpTypeFloat 32
%_ptr_Output_float = OpTypePointer Output %float
%FragColor = OpVariable %_ptr_Output_float Output
%main = OpFunction %void None %3
%5 = OpLabel
%14 = OpAccessChain %_ptr_Uniform_int %_ %int_1
%15 = OpLoad %int %14
%18 = OpAccessChain %_ptr_Uniform_int %_ %int_2
%19 = OpLoad %int %18
OpBranch %22
%22 = OpLabel
%54 = OpPhi %int %19 %5 %53 %23
%53 = OpPhi %int %15 %5 %54 %23
%52 = OpPhi %int %int_0 %5 %37 %23
%28 = OpAccessChain %_ptr_Uniform_int %_ %int_0
%29 = OpLoad %int %28
%31 = OpSLessThan %bool %52 %29
OpLoopMerge %24 %23 None
OpBranchConditional %31 %inbetween %24
%inbetween = OpLabel
OpBranch %23
%23 = OpLabel
%37 = OpIAdd %int %52 %int_1
OpBranch %22
%24 = OpLabel
%43 = OpISub %int %53 %54
%44 = OpConvertSToF %float %43
%49 = OpIMul %int %15 %19
%50 = OpConvertSToF %float %49
%51 = OpFMul %float %44 %50
OpStore %FragColor %51
OpReturn
OpFunctionEnd

View File

@ -894,6 +894,10 @@ struct SPIRVariable : IVariant
bool deferred_declaration = false;
bool phi_variable = false;
// Used to deal with Phi variable flushes. See flush_phi().
bool allocate_temporary_copy = false;
bool remapped_variable = false;
uint32_t remapped_components = 0;

View File

@ -1283,7 +1283,7 @@ string CompilerGLSL::layout_for_variable(const SPIRVariable &var)
auto &dec = ir.meta[var.self].decoration;
auto &type = get<SPIRType>(var.basetype);
auto flags = dec.decoration_flags;
auto &flags = dec.decoration_flags;
auto typeflags = ir.meta[type.self].decoration.decoration_flags;
if (options.vulkan_semantics && var.storage == StorageClassPushConstant)
@ -3459,7 +3459,7 @@ string CompilerGLSL::constant_expression_vector(const SPIRConstant &c, uint32_t
string CompilerGLSL::declare_temporary(uint32_t result_type, uint32_t result_id)
{
auto &type = get<SPIRType>(result_type);
auto flags = ir.meta[result_id].decoration.decoration_flags;
auto &flags = ir.meta[result_id].decoration.decoration_flags;
// If we're declaring temporaries inside continue blocks,
// we must declare the temporary in the loop header so that the continue block can avoid declaring new variables.
@ -4568,7 +4568,7 @@ void CompilerGLSL::emit_glsl_op(uint32_t result_type, uint32_t id, uint32_t eop,
{
forced_temporaries.insert(id);
auto &type = get<SPIRType>(result_type);
auto flags = ir.meta[id].decoration.decoration_flags;
auto &flags = ir.meta[id].decoration.decoration_flags;
statement(flags_to_precision_qualifiers_glsl(type, flags), variable_decl(type, to_name(id)), ";");
set<SPIRExpression>(id, to_name(id), result_type, true);
@ -4691,7 +4691,7 @@ void CompilerGLSL::emit_glsl_op(uint32_t result_type, uint32_t id, uint32_t eop,
{
forced_temporaries.insert(id);
auto &type = get<SPIRType>(result_type);
auto flags = ir.meta[id].decoration.decoration_flags;
auto &flags = ir.meta[id].decoration.decoration_flags;
statement(flags_to_precision_qualifiers_glsl(type, flags), variable_decl(type, to_name(id)), ";");
set<SPIRExpression>(id, to_name(id), result_type, true);
@ -6219,6 +6219,13 @@ void CompilerGLSL::flush_variable_declaration(uint32_t id)
if (var && var->deferred_declaration)
{
statement(variable_decl_function_local(*var), ";");
if (var->allocate_temporary_copy)
{
auto &type = get<SPIRType>(var->basetype);
auto &flags = ir.meta[id].decoration.decoration_flags;
statement(flags_to_precision_qualifiers_glsl(type, flags), variable_decl(type, join("_", id, "_copy")),
";");
}
var->deferred_declaration = false;
}
}
@ -6840,7 +6847,7 @@ void CompilerGLSL::emit_instruction(const Instruction &instruction)
// We cannot construct array of arrays because we cannot treat the inputs
// as value types. Need to declare the array-of-arrays, and copy in elements one by one.
forced_temporaries.insert(id);
auto flags = ir.meta[id].decoration.decoration_flags;
auto &flags = ir.meta[id].decoration.decoration_flags;
statement(flags_to_precision_qualifiers_glsl(out_type, flags), variable_decl(out_type, to_name(id)), ";");
set<SPIRExpression>(id, to_name(id), result_type, true);
for (uint32_t i = 0; i < length; i++)
@ -7225,7 +7232,7 @@ void CompilerGLSL::emit_instruction(const Instruction &instruction)
uint32_t op1 = ops[3];
forced_temporaries.insert(result_id);
auto &type = get<SPIRType>(result_type);
auto flags = ir.meta[result_id].decoration.decoration_flags;
auto &flags = ir.meta[result_id].decoration.decoration_flags;
statement(flags_to_precision_qualifiers_glsl(type, flags), variable_decl(type, to_name(result_id)), ";");
set<SPIRExpression>(result_id, to_name(result_id), result_type, true);
@ -7250,7 +7257,7 @@ void CompilerGLSL::emit_instruction(const Instruction &instruction)
uint32_t op1 = ops[3];
forced_temporaries.insert(result_id);
auto &type = get<SPIRType>(result_type);
auto flags = ir.meta[result_id].decoration.decoration_flags;
auto &flags = ir.meta[result_id].decoration.decoration_flags;
statement(flags_to_precision_qualifiers_glsl(type, flags), variable_decl(type, to_name(result_id)), ";");
set<SPIRExpression>(result_id, to_name(result_id), result_type, true);
@ -8791,7 +8798,7 @@ const char *CompilerGLSL::to_precision_qualifiers_glsl(uint32_t id)
string CompilerGLSL::to_qualifiers_glsl(uint32_t id)
{
auto flags = ir.meta[id].decoration.decoration_flags;
auto &flags = ir.meta[id].decoration.decoration_flags;
string res;
auto *var = maybe_get<SPIRVariable>(id);
@ -8870,7 +8877,7 @@ string CompilerGLSL::variable_decl(const SPIRVariable &variable)
const char *CompilerGLSL::to_pls_qualifiers_glsl(const SPIRVariable &variable)
{
auto flags = ir.meta[variable.self].decoration.decoration_flags;
auto &flags = ir.meta[variable.self].decoration.decoration_flags;
if (flags.get(DecorationRelaxedPrecision))
return "mediump ";
else
@ -9301,7 +9308,7 @@ void CompilerGLSL::flatten_buffer_block(uint32_t id)
auto &var = get<SPIRVariable>(id);
auto &type = get<SPIRType>(var.basetype);
auto name = to_name(type.self, false);
auto flags = ir.meta.at(type.self).decoration.decoration_flags;
auto &flags = ir.meta.at(type.self).decoration.decoration_flags;
if (!type.array.empty())
SPIRV_CROSS_THROW(name + " is an array of UBOs.");
@ -9598,8 +9605,12 @@ void CompilerGLSL::flush_phi(uint32_t from, uint32_t to)
{
auto &child = get<SPIRBlock>(to);
for (auto &phi : child.phi_variables)
unordered_set<uint32_t> temporary_phi_variables;
for (auto itr = begin(child.phi_variables); itr != end(child.phi_variables); ++itr)
{
auto &phi = *itr;
if (phi.parent == from)
{
auto &var = get<SPIRVariable>(phi.function_variable);
@ -9611,10 +9622,38 @@ void CompilerGLSL::flush_phi(uint32_t from, uint32_t to)
{
flush_variable_declaration(phi.function_variable);
// Check if we are going to write to a Phi variable that another statement will read from
// as part of another Phi node in our target block.
// For this case, we will need to copy phi.function_variable to a temporary, and use that for future reads.
// This is judged to be extremely rare, so deal with it here using a simple, but suboptimal algorithm.
bool need_saved_temporary =
find_if(itr + 1, end(child.phi_variables), [&](const SPIRBlock::Phi &future_phi) -> bool {
return future_phi.local_variable == phi.function_variable && future_phi.parent == from;
}) != end(child.phi_variables);
if (need_saved_temporary)
{
// Need to make sure we declare the phi variable with a copy at the right scope.
// We cannot safely declare a temporary here since we might be inside a continue block.
if (!var.allocate_temporary_copy)
{
var.allocate_temporary_copy = true;
force_recompile = true;
}
statement("_", phi.function_variable, "_copy", " = ", to_name(phi.function_variable), ";");
temporary_phi_variables.insert(phi.function_variable);
}
// This might be called in continue block, so make sure we
// use this to emit ESSL 1.0 compliant increments/decrements.
auto lhs = to_expression(phi.function_variable);
auto rhs = to_expression(phi.local_variable);
string rhs;
if (temporary_phi_variables.count(phi.local_variable))
rhs = join("_", phi.local_variable, "_copy");
else
rhs = to_expression(phi.local_variable);
if (!optimize_read_modify_write(get<SPIRType>(var.basetype), lhs, rhs))
statement(lhs, " = ", rhs, ";");
}
@ -10109,14 +10148,8 @@ void CompilerGLSL::flush_undeclared_variables(SPIRBlock &block)
{
// Enforce declaration order for regression testing purposes.
sort(begin(block.dominated_variables), end(block.dominated_variables));
for (auto &v : block.dominated_variables)
{
auto &var = get<SPIRVariable>(v);
if (var.deferred_declaration)
statement(variable_decl(var), ";");
var.deferred_declaration = false;
}
flush_variable_declaration(v);
}
void CompilerGLSL::emit_hoisted_temporaries(vector<pair<uint32_t, uint32_t>> &temporaries)
@ -10129,7 +10162,7 @@ void CompilerGLSL::emit_hoisted_temporaries(vector<pair<uint32_t, uint32_t>> &te
for (auto &tmp : temporaries)
{
add_local_variable_name(tmp.second);
auto flags = ir.meta[tmp.second].decoration.decoration_flags;
auto &flags = ir.meta[tmp.second].decoration.decoration_flags;
auto &type = get<SPIRType>(tmp.first);
statement(flags_to_precision_qualifiers_glsl(type, flags), variable_decl(type, to_name(tmp.second)), ";");