1#!amber
2
3# Copyright 2020 Google LLC
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17
18# A test for a bug found by GraphicsFuzz.
19
20# Short description: A fragment shader that compares a float and a coordinate
21
22# The test passes because shader always writes red.
23
24# Optimized using spirv-opt with the following arguments:
25# '--redundancy-elimination'
26# '--redundancy-elimination'
27# '--private-to-local'
28# '--eliminate-dead-inserts'
29# '--eliminate-dead-code-aggressive'
30# '--combine-access-chains'
31# '--reduce-load-size'
32# '--inline-entry-points-exhaustive'
33# '--vector-dce'
34# '--eliminate-dead-branches'
35# '--merge-blocks'
36# '--copy-propagate-arrays'
37# '--vector-dce'
38# '--eliminate-dead-branches'
39# '--merge-blocks'
40# '--eliminate-dead-branches'
41# '--merge-return'
42# '--combine-access-chains'
43# '--eliminate-dead-inserts'
44# '--eliminate-dead-branches'
45# '--if-conversion'
46# '--inline-entry-points-exhaustive'
47# '--simplify-instructions'
48# '--eliminate-local-single-store'
49# '--eliminate-dead-branches'
50# '--eliminate-local-single-block'
51# '--eliminate-local-multi-store'
52# '--convert-local-access-chains'
53# '--ccp'
54# spirv-opt commit hash: ab7ac60f14ae66006bed5c989a2cfd4c4881704c
55
56
57
58SHADER vertex variant_vertex_shader PASSTHROUGH
59
60# variant_fragment_shader is derived from the following GLSL:
61# #version 310 es
62# precision highp float;
63# precision highp int;
64#
65# layout(set = 0, binding = 0) uniform buf0
66# {
67#     vec2 injectionSwitch;
68# };
69# layout(location = 0) out vec4 _GLF_color;
70#
71# // Always returns 5.
72# float func()
73# {
74#     float zero = 0.0;
75#     bool alwaysFalse = gl_FragCoord.x < 0.0;
76#
77#     do
78#     {
79#         if (injectionSwitch.x < injectionSwitch.y) // Always true
80#         {
81#             while (int(zero) <= bitfieldInsert(101, 0, 0, 0))
82#             {
83#                 if (zero <= gl_FragCoord.x)
84#                     return 5.0; // Always returns here
85#
86#                 zero++;
87#             }
88#         }
89#     } while (alwaysFalse);
90#
91#     for (int u = 0; u < 1; u++)
92#     {
93#         if (!alwaysFalse)
94#             return zero;
95#     }
96#
97#     return 0.0;
98# }
99#
100# void main()
101# {
102#     float c = 0.0;
103#     for (int i = 0; i < 1; i++)
104#     {
105#         if (!(gl_FragCoord.x < 0.0)) // Always true
106#             c = func();
107#     }
108#
109#     if (c == 5.0)
110#         _GLF_color = vec4(1, 0, 0, 1);
111#     else
112#         _GLF_color = vec4(0);
113# }
114SHADER fragment variant_fragment_shader SPIRV-ASM TARGET_ENV spv1.0
115; SPIR-V
116; Version: 1.0
117; Generator: Khronos Glslang Reference Front End; 8
118; Bound: 261
119; Schema: 0
120               OpCapability Shader
121          %1 = OpExtInstImport "GLSL.std.450"
122               OpMemoryModel Logical GLSL450
123               OpEntryPoint Fragment %4 "main" %18 %109
124               OpExecutionMode %4 OriginUpperLeft
125               OpSource ESSL 310
126               OpName %4 "main"
127               OpName %8 "func("
128               OpName %11 "zero"
129               OpName %15 "alwaysFalse"
130               OpName %18 "gl_FragCoord"
131               OpName %30 "buf0"
132               OpMemberName %30 0 "injectionSwitch"
133               OpName %32 ""
134               OpName %67 "u"
135               OpName %86 "c"
136               OpName %87 "i"
137               OpName %109 "_GLF_color"
138               OpDecorate %18 BuiltIn FragCoord
139               OpMemberDecorate %30 0 Offset 0
140               OpDecorate %30 Block
141               OpDecorate %32 DescriptorSet 0
142               OpDecorate %32 Binding 0
143               OpDecorate %109 Location 0
144          %2 = OpTypeVoid
145          %3 = OpTypeFunction %2
146          %6 = OpTypeFloat 32
147          %7 = OpTypeFunction %6
148         %10 = OpTypePointer Function %6
149         %12 = OpConstant %6 0
150         %13 = OpTypeBool
151         %14 = OpTypePointer Function %13
152         %16 = OpTypeVector %6 4
153         %17 = OpTypePointer Input %16
154         %18 = OpVariable %17 Input
155         %19 = OpTypeInt 32 0
156         %20 = OpConstant %19 0
157         %21 = OpTypePointer Input %6
158         %29 = OpTypeVector %6 2
159         %30 = OpTypeStruct %29
160         %31 = OpTypePointer Uniform %30
161         %32 = OpVariable %31 Uniform
162         %33 = OpTypeInt 32 1
163         %34 = OpConstant %33 0
164         %35 = OpTypePointer Uniform %6
165         %38 = OpConstant %19 1
166         %51 = OpConstant %33 101
167         %60 = OpConstant %6 5
168         %63 = OpConstant %6 1
169         %66 = OpTypePointer Function %33
170         %74 = OpConstant %33 1
171        %108 = OpTypePointer Output %16
172        %109 = OpVariable %108 Output
173        %110 = OpConstantComposite %16 %63 %12 %12 %63
174        %112 = OpConstantComposite %16 %12 %12 %12 %12
175        %119 = OpConstantFalse %13
176        %121 = OpConstantTrue %13
177        %211 = OpUndef %6
178        %226 = OpUndef %13
179        %228 = OpUndef %33
180        %256 = OpUndef %6
181        %260 = OpUndef %13
182          %4 = OpFunction %2 None %3
183          %5 = OpLabel
184        %128 = OpVariable %14 Function %119
185        %129 = OpVariable %10 Function
186        %130 = OpVariable %10 Function
187        %131 = OpVariable %14 Function
188        %132 = OpVariable %66 Function
189        %133 = OpVariable %10 Function
190         %86 = OpVariable %10 Function
191         %87 = OpVariable %66 Function
192               OpStore %86 %12
193               OpStore %87 %34
194               OpBranch %88
195         %88 = OpLabel
196        %210 = OpPhi %6 %211 %5 %227 %91
197        %188 = OpPhi %6 %12 %5 %225 %91
198        %187 = OpPhi %33 %34 %5 %103 %91
199         %94 = OpSLessThan %13 %187 %74
200               OpLoopMerge %90 %91 None
201               OpBranchConditional %94 %89 %90
202         %89 = OpLabel
203         %95 = OpAccessChain %21 %18 %20
204         %96 = OpLoad %6 %95
205         %97 = OpFOrdLessThan %13 %96 %12
206         %98 = OpLogicalNot %13 %97
207               OpSelectionMerge %100 None
208               OpBranchConditional %98 %99 %100
209         %99 = OpLabel
210               OpStore %128 %119
211               OpBranch %134
212        %134 = OpLabel
213        %221 = OpPhi %33 %187 %99 %228 %136
214        %209 = OpPhi %6 %210 %99 %211 %136
215        %194 = OpPhi %13 %119 %99 %226 %136
216               OpLoopMerge %135 %136 None
217               OpBranch %137
218        %137 = OpLabel
219               OpStore %130 %12
220        %138 = OpAccessChain %21 %18 %20
221        %139 = OpLoad %6 %138
222        %140 = OpFOrdLessThan %13 %139 %12
223               OpStore %131 %140
224               OpBranch %141
225        %141 = OpLabel
226        %193 = OpPhi %13 %119 %137 %196 %143
227        %190 = OpPhi %6 %12 %137 %201 %143
228        %220 = OpPhi %33 %221 %137 %222 %143
229        %208 = OpPhi %6 %209 %137 %212 %143
230               OpLoopMerge %142 %143 None
231               OpBranch %144
232        %144 = OpLabel
233        %145 = OpAccessChain %35 %32 %34 %20
234        %146 = OpLoad %6 %145
235        %147 = OpAccessChain %35 %32 %34 %38
236        %148 = OpLoad %6 %147
237        %149 = OpFOrdLessThan %13 %146 %148
238               OpSelectionMerge %150 None
239               OpBranchConditional %149 %151 %150
240        %151 = OpLabel
241               OpBranch %152
242        %152 = OpLabel
243        %189 = OpPhi %6 %190 %151 %165 %158
244        %154 = OpConvertFToS %33 %189
245        %155 = OpBitFieldInsert %33 %51 %34 %34 %34
246        %156 = OpSLessThanEqual %13 %154 %155
247               OpLoopMerge %157 %158 None
248               OpBranchConditional %156 %159 %157
249        %159 = OpLabel
250        %161 = OpFOrdLessThanEqual %13 %189 %139
251               OpSelectionMerge %162 None
252               OpBranchConditional %161 %163 %162
253        %163 = OpLabel
254               OpStore %128 %121
255               OpStore %129 %60
256               OpBranch %157
257        %162 = OpLabel
258        %165 = OpFAdd %6 %189 %63
259               OpStore %130 %165
260               OpBranch %158
261        %158 = OpLabel
262               OpBranch %152
263        %157 = OpLabel
264        %206 = OpPhi %6 %208 %152 %60 %163
265        %191 = OpPhi %13 %193 %152 %121 %163
266               OpSelectionMerge %167 None
267               OpBranchConditional %191 %142 %167
268        %167 = OpLabel
269               OpBranch %150
270        %150 = OpLabel
271        %222 = OpPhi %33 %220 %144 %220 %167
272        %212 = OpPhi %6 %208 %144 %206 %167
273        %201 = OpPhi %6 %190 %144 %189 %167
274        %196 = OpPhi %13 %193 %144 %191 %167
275               OpBranch %143
276        %143 = OpLabel
277               OpBranchConditional %140 %141 %142
278        %142 = OpLabel
279        %217 = OpPhi %33 %220 %157 %222 %143
280        %205 = OpPhi %6 %206 %157 %212 %143
281        %199 = OpPhi %6 %189 %157 %201 %143
282        %195 = OpPhi %13 %191 %157 %196 %143
283               OpSelectionMerge %170 None
284               OpBranchConditional %195 %135 %170
285        %170 = OpLabel
286               OpStore %132 %34
287               OpBranch %171
288        %171 = OpLabel
289        %197 = OpPhi %33 %34 %170 %183 %175
290        %173 = OpSLessThan %13 %197 %74
291               OpLoopMerge %174 %175 None
292               OpBranchConditional %173 %176 %174
293        %176 = OpLabel
294        %178 = OpLogicalNot %13 %140
295               OpSelectionMerge %179 None
296               OpBranchConditional %178 %180 %179
297        %180 = OpLabel
298               OpStore %128 %121
299               OpStore %129 %199
300               OpBranch %174
301        %179 = OpLabel
302               OpBranch %175
303        %175 = OpLabel
304        %183 = OpIAdd %33 %197 %74
305               OpStore %132 %183
306               OpBranch %171
307        %174 = OpLabel
308        %213 = OpPhi %6 %205 %171 %199 %180
309        %202 = OpPhi %13 %195 %171 %121 %180
310               OpSelectionMerge %185 None
311               OpBranchConditional %202 %135 %185
312        %185 = OpLabel
313               OpStore %128 %121
314               OpStore %129 %12
315               OpBranch %135
316        %136 = OpLabel
317               OpBranch %134
318        %135 = OpLabel
319        %204 = OpPhi %6 %205 %142 %213 %174 %12 %185
320               OpStore %133 %204
321               OpStore %86 %204
322               OpBranch %100
323        %100 = OpLabel
324        %227 = OpPhi %6 %210 %89 %204 %135
325        %225 = OpPhi %6 %188 %89 %204 %135
326        %215 = OpPhi %33 %187 %89 %217 %135
327               OpBranch %91
328         %91 = OpLabel
329        %103 = OpIAdd %33 %215 %74
330               OpStore %87 %103
331               OpBranch %88
332         %90 = OpLabel
333        %105 = OpFOrdEqual %13 %188 %60
334               OpSelectionMerge %107 None
335               OpBranchConditional %105 %106 %111
336        %106 = OpLabel
337               OpStore %109 %110
338               OpBranch %107
339        %111 = OpLabel
340               OpStore %109 %112
341               OpBranch %107
342        %107 = OpLabel
343               OpReturn
344               OpFunctionEnd
345          %8 = OpFunction %6 None %7
346          %9 = OpLabel
347        %120 = OpVariable %14 Function %119
348        %114 = OpVariable %10 Function
349         %11 = OpVariable %10 Function
350         %15 = OpVariable %14 Function
351         %67 = OpVariable %66 Function
352               OpBranch %116
353        %116 = OpLabel
354        %234 = OpPhi %13 %119 %9 %260 %118
355               OpLoopMerge %113 %118 None
356               OpBranch %117
357        %117 = OpLabel
358               OpStore %11 %12
359         %22 = OpAccessChain %21 %18 %20
360         %23 = OpLoad %6 %22
361         %24 = OpFOrdLessThan %13 %23 %12
362               OpStore %15 %24
363               OpBranch %25
364         %25 = OpLabel
365        %233 = OpPhi %13 %234 %117 %240 %28
366        %230 = OpPhi %6 %12 %117 %247 %28
367        %254 = OpPhi %6 %256 %117 %257 %28
368               OpLoopMerge %27 %28 None
369               OpBranch %26
370         %26 = OpLabel
371         %36 = OpAccessChain %35 %32 %34 %20
372         %37 = OpLoad %6 %36
373         %39 = OpAccessChain %35 %32 %34 %38
374         %40 = OpLoad %6 %39
375         %41 = OpFOrdLessThan %13 %37 %40
376               OpSelectionMerge %43 None
377               OpBranchConditional %41 %42 %43
378         %42 = OpLabel
379               OpBranch %44
380         %44 = OpLabel
381        %229 = OpPhi %6 %230 %42 %64 %47
382         %50 = OpConvertFToS %33 %229
383         %52 = OpBitFieldInsert %33 %51 %34 %34 %34
384         %53 = OpSLessThanEqual %13 %50 %52
385               OpLoopMerge %46 %47 None
386               OpBranchConditional %53 %45 %46
387         %45 = OpLabel
388         %57 = OpFOrdLessThanEqual %13 %229 %23
389               OpSelectionMerge %59 None
390               OpBranchConditional %57 %58 %59
391         %58 = OpLabel
392               OpStore %120 %121
393               OpStore %114 %60
394               OpBranch %46
395         %59 = OpLabel
396         %64 = OpFAdd %6 %229 %63
397               OpStore %11 %64
398               OpBranch %47
399         %47 = OpLabel
400               OpBranch %44
401         %46 = OpLabel
402        %252 = OpPhi %6 %254 %44 %60 %58
403        %231 = OpPhi %13 %233 %44 %121 %58
404               OpSelectionMerge %122 None
405               OpBranchConditional %231 %27 %122
406        %122 = OpLabel
407               OpBranch %43
408         %43 = OpLabel
409        %257 = OpPhi %6 %254 %26 %252 %122
410        %247 = OpPhi %6 %230 %26 %229 %122
411        %240 = OpPhi %13 %233 %26 %231 %122
412               OpBranch %28
413         %28 = OpLabel
414               OpBranchConditional %24 %25 %27
415         %27 = OpLabel
416        %251 = OpPhi %6 %252 %46 %257 %28
417        %245 = OpPhi %6 %229 %46 %247 %28
418        %239 = OpPhi %13 %231 %46 %240 %28
419               OpSelectionMerge %124 None
420               OpBranchConditional %239 %113 %124
421        %124 = OpLabel
422               OpStore %67 %34
423               OpBranch %68
424         %68 = OpLabel
425        %241 = OpPhi %33 %34 %124 %83 %71
426         %75 = OpSLessThan %13 %241 %74
427               OpLoopMerge %70 %71 None
428               OpBranchConditional %75 %69 %70
429         %69 = OpLabel
430         %77 = OpLogicalNot %13 %24
431               OpSelectionMerge %79 None
432               OpBranchConditional %77 %78 %79
433         %78 = OpLabel
434               OpStore %120 %121
435               OpStore %114 %245
436               OpBranch %70
437         %79 = OpLabel
438               OpBranch %71
439         %71 = OpLabel
440         %83 = OpIAdd %33 %241 %74
441               OpStore %67 %83
442               OpBranch %68
443         %70 = OpLabel
444        %258 = OpPhi %6 %251 %68 %245 %78
445        %248 = OpPhi %13 %239 %68 %121 %78
446               OpSelectionMerge %126 None
447               OpBranchConditional %248 %113 %126
448        %126 = OpLabel
449               OpStore %120 %121
450               OpStore %114 %12
451               OpBranch %113
452        %118 = OpLabel
453               OpBranch %116
454        %113 = OpLabel
455        %250 = OpPhi %6 %251 %27 %258 %70 %12 %126
456               OpReturnValue %250
457               OpFunctionEnd
458END
459
460# uniforms for variant
461
462# resolution
463BUFFER variant_resolution DATA_TYPE vec2<float> DATA
464 256.0 256.0
465END
466# injectionSwitch
467BUFFER variant_injectionSwitch DATA_TYPE vec2<float> DATA
468 0.0 1.0
469END
470
471BUFFER variant_framebuffer FORMAT B8G8R8A8_UNORM
472
473PIPELINE graphics variant_pipeline
474  ATTACH variant_vertex_shader
475  ATTACH variant_fragment_shader
476  FRAMEBUFFER_SIZE 256 256
477  BIND BUFFER variant_framebuffer AS color LOCATION 0
478  BIND BUFFER variant_resolution AS uniform DESCRIPTOR_SET 0 BINDING 1
479  BIND BUFFER variant_injectionSwitch AS uniform DESCRIPTOR_SET 0 BINDING 0
480END
481CLEAR_COLOR variant_pipeline 0 0 0 255
482
483CLEAR variant_pipeline
484RUN variant_pipeline DRAW_RECT POS 0 0 SIZE 256 256
485
486EXPECT variant_framebuffer IDX 0 0 SIZE 256 256 EQ_RGBA 255 0 0 255
487