--- /dev/null
+# Copyright 2019 Google LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# A test for a bug found by GraphicsFuzz.
+
+# Short description: A fragment shader with early return from loop in function
+
+# The test passes because the fragment shader writes a red pixel, and then
+# terminates without further output manipulation (the other write of red
+# is not dynamically reachable)
+
+# Optimized using spirv-opt with the following arguments:
+# '--merge-blocks'
+# spirv-opt commit hash: 4a00a80c40484a6f6f72f48c9d34943cf8f180d4
+
+# Derived from the following GLSL.
+
+# Fragment shader GLSL:
+# #version 310 es
+#
+# precision highp float;
+#
+# layout(location = 0) out vec4 _GLF_color;
+#
+# float f()
+# {
+# for(
+# int i = 1;
+# i < 10;
+# i ++
+# )
+# {
+# if(float(i) >= 1.0)
+# {
+# return 1.0;
+# }
+# }
+# return 1.0;
+# }
+# void main()
+# {
+# vec4 c = vec4(1.0, 0.0, 0.0, 1.0);
+# for(
+# int i = 0;
+# i < 1;
+# i ++
+# )
+# {
+# c.x = f();
+# }
+# _GLF_color = c;
+# }
+
+[require]
+fbsize 256 256
+
+[vertex shader passthrough]
+
+[fragment shader spirv]
+; SPIR-V
+; Version: 1.0
+; Generator: Khronos Glslang Reference Front End; 7
+; Bound: 58
+; Schema: 0
+ OpCapability Shader
+ %1 = OpExtInstImport "GLSL.std.450"
+ OpMemoryModel Logical GLSL450
+ OpEntryPoint Fragment %4 "main" %56
+ OpExecutionMode %4 OriginUpperLeft
+ OpSource ESSL 310
+ OpName %4 "main"
+ OpName %8 "f("
+ OpName %12 "i"
+ OpName %36 "c"
+ OpName %39 "i"
+ OpName %56 "_GLF_color"
+ OpDecorate %12 RelaxedPrecision
+ OpDecorate %19 RelaxedPrecision
+ OpDecorate %23 RelaxedPrecision
+ OpDecorate %30 RelaxedPrecision
+ OpDecorate %31 RelaxedPrecision
+ OpDecorate %39 RelaxedPrecision
+ OpDecorate %46 RelaxedPrecision
+ OpDecorate %53 RelaxedPrecision
+ OpDecorate %54 RelaxedPrecision
+ OpDecorate %56 Location 0
+ %2 = OpTypeVoid
+ %3 = OpTypeFunction %2
+ %6 = OpTypeFloat 32
+ %7 = OpTypeFunction %6
+ %10 = OpTypeInt 32 1
+ %11 = OpTypePointer Function %10
+ %13 = OpConstant %10 1
+ %20 = OpConstant %10 10
+ %21 = OpTypeBool
+ %25 = OpConstant %6 1
+ %34 = OpTypeVector %6 4
+ %35 = OpTypePointer Function %34
+ %37 = OpConstant %6 0
+ %38 = OpConstantComposite %34 %25 %37 %37 %25
+ %40 = OpConstant %10 0
+ %49 = OpTypeInt 32 0
+ %50 = OpConstant %49 0
+ %51 = OpTypePointer Function %6
+ %55 = OpTypePointer Output %34
+ %56 = OpVariable %55 Output
+ %4 = OpFunction %2 None %3
+ %5 = OpLabel
+ %36 = OpVariable %35 Function
+ %39 = OpVariable %11 Function
+ OpStore %36 %38
+ OpStore %39 %40
+ OpBranch %41
+ %41 = OpLabel
+ %46 = OpLoad %10 %39
+ %47 = OpSLessThan %21 %46 %13
+ OpLoopMerge %43 %42 None
+ OpBranchConditional %47 %42 %43
+ %42 = OpLabel
+ %48 = OpFunctionCall %6 %8
+ %52 = OpAccessChain %51 %36 %50
+ OpStore %52 %48
+ %53 = OpLoad %10 %39
+ %54 = OpIAdd %10 %53 %13
+ OpStore %39 %54
+ OpBranch %41
+ %43 = OpLabel
+ %57 = OpLoad %34 %36
+ OpStore %56 %57
+ OpReturn
+ OpFunctionEnd
+ %8 = OpFunction %6 None %7
+ %9 = OpLabel
+ %12 = OpVariable %11 Function
+ OpStore %12 %13
+ OpBranch %14
+ %14 = OpLabel
+ %19 = OpLoad %10 %12
+ %22 = OpSLessThan %21 %19 %20
+ OpLoopMerge %16 %28 None
+ OpBranchConditional %22 %15 %16
+ %15 = OpLabel
+ %23 = OpLoad %10 %12
+ %24 = OpConvertSToF %6 %23
+ %26 = OpFOrdGreaterThanEqual %21 %24 %25
+ OpSelectionMerge %28 None
+ OpBranchConditional %26 %27 %28
+ %27 = OpLabel
+ OpReturnValue %25
+ %28 = OpLabel
+ %30 = OpLoad %10 %12
+ %31 = OpIAdd %10 %30 %13
+ OpStore %12 %31
+ OpBranch %14
+ %16 = OpLabel
+ OpReturnValue %25
+ OpFunctionEnd
+
+
+[test]
+draw rect -1 -1 2 2
+probe rgba (0, 0) (1, 0, 0, 1)
{ "mat-array-distance.amber", "mat-array-distance", "A fragment shader that uses an array of matrices and distance" },
{ "matrices-and-return-in-loop.amber", "matrices-and-return-in-loop", "A fragment shader with matrices and a return in a loop" },
{ "pow-vec4.amber", "pow-vec4", "A fragment shader that uses pow" },
+ { "return-in-loop-in-function.amber", "return-in-loop-in-function", "A fragment shader with early return from loop in function" },
{ "swizzle-struct-init-min.amber", "swizzle-struct-init-min", "A fragment shader that uses vector swizzles, struct initializers, and min" },
{ "while-inside-switch.amber", "while-inside-switch", "A fragment shader that uses a while loop inside a switch" },
};