1#!amber
2
3# Copyright 2022 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 coverage-gap found by the GraphicsFuzz project.
19
20# Short description: A fragment shader that covers specific LLVM code paths
21
22# The test passes because the shader always writes red.
23
24# Optimized using spirv-opt with the following arguments:
25# '--copy-propagate-arrays'
26# '--eliminate-local-multi-store'
27# '--if-conversion'
28# '--eliminate-local-single-block'
29# '--eliminate-dead-inserts'
30# '--redundancy-elimination'
31# '--eliminate-local-single-store'
32# '--eliminate-dead-code-aggressive'
33# '--eliminate-dead-code-aggressive'
34# '--redundancy-elimination'
35# '--scalar-replacement=100'
36# '--eliminate-local-multi-store'
37# '--inline-entry-points-exhaustive'
38# '--eliminate-dead-branches'
39# '--merge-return'
40# '--eliminate-dead-branches'
41# '--merge-blocks'
42# '--ccp'
43# '--copy-propagate-arrays'
44# '--combine-access-chains'
45# '--if-conversion'
46# '--private-to-local'
47# '--reduce-load-size'
48# '--scalar-replacement=100'
49# spirv-opt commit hash: a0370efd589be33d5d9a85cfde2f85841b3755af
50
51
52
53SHADER vertex variant_vertex_shader PASSTHROUGH
54
55# variant_fragment_shader is derived from the following GLSL:
56# #version 320 es
57#
58# #define _int_1 _GLF_uniform_int_values[0]
59# #define _int_0 _GLF_uniform_int_values[1]
60#
61# precision highp float;
62# precision highp int;
63#
64# // Contents of _GLF_uniform_int_values: [1, 0]
65# layout(set = 0, binding = 0) uniform buf0
66# {
67#     int _GLF_uniform_int_values[2];
68# };
69#
70# const int _GLF_global_loop_bound = 10;
71# int _GLF_global_loop_count = 0;
72#
73# layout(location = 0) out vec4 _GLF_color;
74#
75# int arr0[10] = int[10](1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
76#
77# int func()
78# {
79#     int a = _int_1;
80#     int b = _int_0;
81#     int arr1[10] = int[10](_int_1, _int_1, _int_1, _int_1, _int_1, _int_1, _int_1, _int_1, _int_1, _int_1);
82#
83#     // Iterates ten times.
84#     while(b >= _int_0 && _GLF_global_loop_count < _GLF_global_loop_bound)
85#     {
86#         _GLF_global_loop_count++;
87#
88#         // Reads zero as an index and then decrements b to -1. Reads value one, so a stays at one.
89#         a = arr1[b--];
90#
91#         // arr0[1] = arr0[0]. No effect.
92#         arr0[_int_1] = arr0[a];
93#
94#         // Always true.
95#         if(a > _int_0)
96#         {
97#             // Increments b to zero and indexes with that. Writes one to arr1[0] but that value already existed.
98#             arr1[++b] = a;
99#         }
100#     }
101#
102#     // Returns zero.
103#     return b;
104# }
105#
106# void main()
107# {
108#     // Always true.
109#     if(func() == _int_0)
110#     {
111#         _GLF_color = vec4(_int_1, _int_0, _int_0, _int_1);
112#     }
113#     else
114#     {
115#         _GLF_color = vec4(_int_0);
116#     }
117# }
118SHADER fragment variant_fragment_shader SPIRV-ASM TARGET_ENV spv1.0
119; SPIR-V
120; Version: 1.0
121; Generator: Khronos Glslang Reference Front End; 10
122; Bound: 157
123; Schema: 0
124               OpCapability Shader
125          %1 = OpExtInstImport "GLSL.std.450"
126               OpMemoryModel Logical GLSL450
127               OpEntryPoint Fragment %4 "main" %104
128               OpExecutionMode %4 OriginUpperLeft
129               OpSource ESSL 320
130               OpName %4 "main"
131               OpName %8 "func("
132               OpName %11 "_GLF_global_loop_count"
133               OpName %17 "arr0"
134               OpName %24 "buf0"
135               OpMemberName %24 0 "_GLF_uniform_int_values"
136               OpName %26 ""
137               OpName %34 "arr1"
138               OpName %104 "_GLF_color"
139               OpDecorate %23 ArrayStride 16
140               OpMemberDecorate %24 0 Offset 0
141               OpDecorate %24 Block
142               OpDecorate %26 DescriptorSet 0
143               OpDecorate %26 Binding 0
144               OpDecorate %104 Location 0
145          %2 = OpTypeVoid
146          %3 = OpTypeFunction %2
147          %6 = OpTypeInt 32 1
148          %7 = OpTypeFunction %6
149         %10 = OpTypePointer Private %6
150         %11 = OpVariable %10 Private
151         %12 = OpConstant %6 0
152         %13 = OpTypeInt 32 0
153         %14 = OpConstant %13 10
154         %15 = OpTypeArray %6 %14
155         %16 = OpTypePointer Private %15
156         %17 = OpVariable %16 Private
157         %18 = OpConstant %6 1
158         %19 = OpConstantComposite %15 %18 %18 %18 %18 %18 %18 %18 %18 %18 %18
159         %20 = OpTypePointer Function %6
160         %22 = OpConstant %13 2
161         %23 = OpTypeArray %6 %22
162         %24 = OpTypeStruct %23
163         %25 = OpTypePointer Uniform %24
164         %26 = OpVariable %25 Uniform
165         %27 = OpTypePointer Uniform %6
166         %33 = OpTypePointer Function %15
167         %64 = OpTypeBool
168         %67 = OpConstant %6 10
169        %101 = OpTypeFloat 32
170        %102 = OpTypeVector %101 4
171        %103 = OpTypePointer Output %102
172        %104 = OpVariable %103 Output
173          %4 = OpFunction %2 None %3
174          %5 = OpLabel
175        %125 = OpVariable %33 Function
176        %126 = OpVariable %20 Function
177               OpStore %11 %12
178               OpStore %17 %19
179        %128 = OpAccessChain %27 %26 %12 %12
180        %129 = OpLoad %6 %128
181        %130 = OpAccessChain %27 %26 %12 %18
182        %131 = OpLoad %6 %130
183        %132 = OpCompositeConstruct %15 %129 %129 %129 %129 %129 %129 %129 %129 %129 %129
184               OpStore %125 %132
185               OpBranch %133
186        %133 = OpLabel
187        %134 = OpPhi %6 %131 %5 %154 %155
188        %136 = OpSGreaterThanEqual %64 %134 %131
189        %137 = OpLoad %6 %11
190        %138 = OpSLessThan %64 %137 %67
191        %139 = OpLogicalAnd %64 %136 %138
192               OpLoopMerge %156 %155 None
193               OpBranchConditional %139 %140 %156
194        %140 = OpLabel
195        %141 = OpLoad %6 %11
196        %142 = OpIAdd %6 %141 %18
197               OpStore %11 %142
198        %143 = OpISub %6 %134 %18
199        %144 = OpAccessChain %20 %125 %134
200        %145 = OpLoad %6 %144
201        %146 = OpAccessChain %10 %17 %145
202        %147 = OpLoad %6 %146
203        %148 = OpAccessChain %10 %17 %129
204               OpStore %148 %147
205        %149 = OpSGreaterThan %64 %145 %131
206               OpSelectionMerge %153 None
207               OpBranchConditional %149 %150 %153
208        %150 = OpLabel
209        %151 = OpIAdd %6 %143 %18
210        %152 = OpAccessChain %20 %125 %151
211               OpStore %152 %145
212               OpBranch %153
213        %153 = OpLabel
214        %154 = OpPhi %6 %143 %140 %151 %150
215               OpBranch %155
216        %155 = OpLabel
217               OpBranch %133
218        %156 = OpLabel
219               OpStore %126 %134
220         %95 = OpLoad %6 %126
221         %96 = OpAccessChain %27 %26 %12 %18
222         %97 = OpLoad %6 %96
223         %98 = OpIEqual %64 %95 %97
224               OpSelectionMerge %100 None
225               OpBranchConditional %98 %99 %118
226         %99 = OpLabel
227        %105 = OpAccessChain %27 %26 %12 %12
228        %106 = OpLoad %6 %105
229        %107 = OpConvertSToF %101 %106
230        %110 = OpConvertSToF %101 %97
231        %117 = OpCompositeConstruct %102 %107 %110 %110 %107
232               OpStore %104 %117
233               OpBranch %100
234        %118 = OpLabel
235        %121 = OpConvertSToF %101 %97
236        %122 = OpCompositeConstruct %102 %121 %121 %121 %121
237               OpStore %104 %122
238               OpBranch %100
239        %100 = OpLabel
240               OpReturn
241               OpFunctionEnd
242          %8 = OpFunction %6 None %7
243          %9 = OpLabel
244         %34 = OpVariable %33 Function
245         %28 = OpAccessChain %27 %26 %12 %12
246         %29 = OpLoad %6 %28
247         %31 = OpAccessChain %27 %26 %12 %18
248         %32 = OpLoad %6 %31
249         %55 = OpCompositeConstruct %15 %29 %29 %29 %29 %29 %29 %29 %29 %29 %29
250               OpStore %34 %55
251               OpBranch %56
252         %56 = OpLabel
253        %123 = OpPhi %6 %32 %9 %124 %59
254               OpLoopMerge %58 %59 None
255               OpBranch %60
256         %60 = OpLabel
257         %65 = OpSGreaterThanEqual %64 %123 %32
258         %66 = OpLoad %6 %11
259         %68 = OpSLessThan %64 %66 %67
260         %69 = OpLogicalAnd %64 %65 %68
261               OpBranchConditional %69 %57 %58
262         %57 = OpLabel
263         %70 = OpLoad %6 %11
264         %71 = OpIAdd %6 %70 %18
265               OpStore %11 %71
266         %73 = OpISub %6 %123 %18
267         %74 = OpAccessChain %20 %34 %123
268         %75 = OpLoad %6 %74
269         %79 = OpAccessChain %10 %17 %75
270         %80 = OpLoad %6 %79
271         %81 = OpAccessChain %10 %17 %29
272               OpStore %81 %80
273         %85 = OpSGreaterThan %64 %75 %32
274               OpSelectionMerge %87 None
275               OpBranchConditional %85 %86 %87
276         %86 = OpLabel
277         %89 = OpIAdd %6 %73 %18
278         %91 = OpAccessChain %20 %34 %89
279               OpStore %91 %75
280               OpBranch %87
281         %87 = OpLabel
282        %124 = OpPhi %6 %73 %57 %89 %86
283               OpBranch %59
284         %59 = OpLabel
285               OpBranch %56
286         %58 = OpLabel
287               OpReturnValue %123
288               OpFunctionEnd
289END
290
291# uniforms for variant
292
293# _GLF_uniform_int_values
294BUFFER variant__GLF_uniform_int_values DATA_TYPE int32[] STD140 DATA
295 1 0
296END
297
298BUFFER variant_framebuffer FORMAT B8G8R8A8_UNORM
299
300PIPELINE graphics variant_pipeline
301  ATTACH variant_vertex_shader
302  ATTACH variant_fragment_shader
303  FRAMEBUFFER_SIZE 32 32
304  BIND BUFFER variant_framebuffer AS color LOCATION 0
305  BIND BUFFER variant__GLF_uniform_int_values AS uniform DESCRIPTOR_SET 0 BINDING 0
306END
307CLEAR_COLOR variant_pipeline 0 0 0 255
308
309CLEAR variant_pipeline
310RUN variant_pipeline DRAW_RECT POS 0 0 SIZE 32 32
311
312EXPECT variant_framebuffer IDX 0 0 SIZE 32 32 EQ_RGBA 255 0 0 255
313