1fd4e5da5Sopenharmony_ci// Copyright (c) 2022 The Khronos Group Inc.
2fd4e5da5Sopenharmony_ci// Copyright (c) 2022 LunarG Inc.
3fd4e5da5Sopenharmony_ci//
4fd4e5da5Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
5fd4e5da5Sopenharmony_ci// you may not use this file except in compliance with the License.
6fd4e5da5Sopenharmony_ci// You may obtain a copy of the License at
7fd4e5da5Sopenharmony_ci//
8fd4e5da5Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
9fd4e5da5Sopenharmony_ci//
10fd4e5da5Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
11fd4e5da5Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
12fd4e5da5Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13fd4e5da5Sopenharmony_ci// See the License for the specific language governing permissions and
14fd4e5da5Sopenharmony_ci// limitations under the License.
15fd4e5da5Sopenharmony_ci
16fd4e5da5Sopenharmony_ci#include <vector>
17fd4e5da5Sopenharmony_ci
18fd4e5da5Sopenharmony_ci#include "test/opt/pass_fixture.h"
19fd4e5da5Sopenharmony_ci#include "test/opt/pass_utils.h"
20fd4e5da5Sopenharmony_ci
21fd4e5da5Sopenharmony_cinamespace spvtools {
22fd4e5da5Sopenharmony_cinamespace opt {
23fd4e5da5Sopenharmony_cinamespace {
24fd4e5da5Sopenharmony_ci
25fd4e5da5Sopenharmony_ciusing ElimDeadIOComponentsTest = PassTest<::testing::Test>;
26fd4e5da5Sopenharmony_ci
27fd4e5da5Sopenharmony_ciTEST_F(ElimDeadIOComponentsTest, ElimOneConstantIndex) {
28fd4e5da5Sopenharmony_ci  // Should reduce to uv[2]
29fd4e5da5Sopenharmony_ci  //
30fd4e5da5Sopenharmony_ci  // #version 450
31fd4e5da5Sopenharmony_ci  //
32fd4e5da5Sopenharmony_ci  // layout(location = 0) in vec4 uv[8];
33fd4e5da5Sopenharmony_ci  //
34fd4e5da5Sopenharmony_ci  // out gl_PerVertex {
35fd4e5da5Sopenharmony_ci  //     vec4 gl_Position;
36fd4e5da5Sopenharmony_ci  // };
37fd4e5da5Sopenharmony_ci  //
38fd4e5da5Sopenharmony_ci  // void main()
39fd4e5da5Sopenharmony_ci  // {
40fd4e5da5Sopenharmony_ci  //     gl_Position = uv[1];
41fd4e5da5Sopenharmony_ci  // }
42fd4e5da5Sopenharmony_ci  const std::string text = R"(
43fd4e5da5Sopenharmony_ci               OpCapability Shader
44fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
45fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
46fd4e5da5Sopenharmony_ci               OpEntryPoint Vertex %main "main" %_ %uv
47fd4e5da5Sopenharmony_ci               OpSource GLSL 450
48fd4e5da5Sopenharmony_ci               OpName %main "main"
49fd4e5da5Sopenharmony_ci               OpName %gl_PerVertex "gl_PerVertex"
50fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 0 "gl_Position"
51fd4e5da5Sopenharmony_ci               OpName %_ ""
52fd4e5da5Sopenharmony_ci               OpName %uv "uv"
53fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
54fd4e5da5Sopenharmony_ci               OpDecorate %gl_PerVertex Block
55fd4e5da5Sopenharmony_ci               OpDecorate %uv Location 0
56fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
57fd4e5da5Sopenharmony_ci          %3 = OpTypeFunction %void
58fd4e5da5Sopenharmony_ci      %float = OpTypeFloat 32
59fd4e5da5Sopenharmony_ci    %v4float = OpTypeVector %float 4
60fd4e5da5Sopenharmony_ci%gl_PerVertex = OpTypeStruct %v4float
61fd4e5da5Sopenharmony_ci%_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
62fd4e5da5Sopenharmony_ci          %_ = OpVariable %_ptr_Output_gl_PerVertex Output
63fd4e5da5Sopenharmony_ci        %int = OpTypeInt 32 1
64fd4e5da5Sopenharmony_ci      %int_0 = OpConstant %int 0
65fd4e5da5Sopenharmony_ci       %uint = OpTypeInt 32 0
66fd4e5da5Sopenharmony_ci     %uint_8 = OpConstant %uint 8
67fd4e5da5Sopenharmony_ci%_arr_v4float_uint_8 = OpTypeArray %v4float %uint_8
68fd4e5da5Sopenharmony_ci%_ptr_Input__arr_v4float_uint_8 = OpTypePointer Input %_arr_v4float_uint_8
69fd4e5da5Sopenharmony_ci           %uv = OpVariable %_ptr_Input__arr_v4float_uint_8 Input
70fd4e5da5Sopenharmony_ci      %int_1 = OpConstant %int 1
71fd4e5da5Sopenharmony_ci%_ptr_Input_v4float = OpTypePointer Input %v4float
72fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
73fd4e5da5Sopenharmony_ci;CHECK-NOT: %uv = OpVariable %_ptr_Input__arr_v4float_uint_8 Input
74fd4e5da5Sopenharmony_ci;CHECK:     %uv = OpVariable %_ptr_Input__arr_v4float_uint_2 Input
75fd4e5da5Sopenharmony_ci       %main = OpFunction %void None %3
76fd4e5da5Sopenharmony_ci          %5 = OpLabel
77fd4e5da5Sopenharmony_ci         %20 = OpAccessChain %_ptr_Input_v4float %uv %int_1
78fd4e5da5Sopenharmony_ci         %21 = OpLoad %v4float %20
79fd4e5da5Sopenharmony_ci         %23 = OpAccessChain %_ptr_Output_v4float %_ %int_0
80fd4e5da5Sopenharmony_ci               OpStore %23 %21
81fd4e5da5Sopenharmony_ci               OpReturn
82fd4e5da5Sopenharmony_ci               OpFunctionEnd
83fd4e5da5Sopenharmony_ci)";
84fd4e5da5Sopenharmony_ci
85fd4e5da5Sopenharmony_ci  SetTargetEnv(SPV_ENV_VULKAN_1_3);
86fd4e5da5Sopenharmony_ci  SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
87fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<EliminateDeadIOComponentsPass>(
88fd4e5da5Sopenharmony_ci      text, true, spv::StorageClass::Input, false);
89fd4e5da5Sopenharmony_ci}
90fd4e5da5Sopenharmony_ci
91fd4e5da5Sopenharmony_ciTEST_F(ElimDeadIOComponentsTest, ElimOneConstantIndexInBounds) {
92fd4e5da5Sopenharmony_ci  // Same as ElimOneConstantIndex but with OpInBoundsAccessChain
93fd4e5da5Sopenharmony_ci  const std::string text = R"(
94fd4e5da5Sopenharmony_ci               OpCapability Shader
95fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
96fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
97fd4e5da5Sopenharmony_ci               OpEntryPoint Vertex %main "main" %_ %uv
98fd4e5da5Sopenharmony_ci               OpSource GLSL 450
99fd4e5da5Sopenharmony_ci               OpName %main "main"
100fd4e5da5Sopenharmony_ci               OpName %gl_PerVertex "gl_PerVertex"
101fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 0 "gl_Position"
102fd4e5da5Sopenharmony_ci               OpName %_ ""
103fd4e5da5Sopenharmony_ci               OpName %uv "uv"
104fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
105fd4e5da5Sopenharmony_ci               OpDecorate %gl_PerVertex Block
106fd4e5da5Sopenharmony_ci               OpDecorate %uv Location 0
107fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
108fd4e5da5Sopenharmony_ci          %3 = OpTypeFunction %void
109fd4e5da5Sopenharmony_ci      %float = OpTypeFloat 32
110fd4e5da5Sopenharmony_ci    %v4float = OpTypeVector %float 4
111fd4e5da5Sopenharmony_ci%gl_PerVertex = OpTypeStruct %v4float
112fd4e5da5Sopenharmony_ci%_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
113fd4e5da5Sopenharmony_ci          %_ = OpVariable %_ptr_Output_gl_PerVertex Output
114fd4e5da5Sopenharmony_ci        %int = OpTypeInt 32 1
115fd4e5da5Sopenharmony_ci      %int_0 = OpConstant %int 0
116fd4e5da5Sopenharmony_ci       %uint = OpTypeInt 32 0
117fd4e5da5Sopenharmony_ci     %uint_8 = OpConstant %uint 8
118fd4e5da5Sopenharmony_ci%_arr_v4float_uint_8 = OpTypeArray %v4float %uint_8
119fd4e5da5Sopenharmony_ci%_ptr_Input__arr_v4float_uint_8 = OpTypePointer Input %_arr_v4float_uint_8
120fd4e5da5Sopenharmony_ci           %uv = OpVariable %_ptr_Input__arr_v4float_uint_8 Input
121fd4e5da5Sopenharmony_ci      %int_1 = OpConstant %int 1
122fd4e5da5Sopenharmony_ci%_ptr_Input_v4float = OpTypePointer Input %v4float
123fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
124fd4e5da5Sopenharmony_ci;CHECK-NOT: %uv = OpVariable %_ptr_Input__arr_v4float_uint_8 Input
125fd4e5da5Sopenharmony_ci;CHECK:     %uv = OpVariable %_ptr_Input__arr_v4float_uint_2 Input
126fd4e5da5Sopenharmony_ci       %main = OpFunction %void None %3
127fd4e5da5Sopenharmony_ci          %5 = OpLabel
128fd4e5da5Sopenharmony_ci         %20 = OpInBoundsAccessChain %_ptr_Input_v4float %uv %int_1
129fd4e5da5Sopenharmony_ci         %21 = OpLoad %v4float %20
130fd4e5da5Sopenharmony_ci         %23 = OpAccessChain %_ptr_Output_v4float %_ %int_0
131fd4e5da5Sopenharmony_ci               OpStore %23 %21
132fd4e5da5Sopenharmony_ci               OpReturn
133fd4e5da5Sopenharmony_ci               OpFunctionEnd
134fd4e5da5Sopenharmony_ci)";
135fd4e5da5Sopenharmony_ci
136fd4e5da5Sopenharmony_ci  SetTargetEnv(SPV_ENV_VULKAN_1_3);
137fd4e5da5Sopenharmony_ci  SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
138fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<EliminateDeadIOComponentsPass>(
139fd4e5da5Sopenharmony_ci      text, true, spv::StorageClass::Input, false);
140fd4e5da5Sopenharmony_ci}
141fd4e5da5Sopenharmony_ci
142fd4e5da5Sopenharmony_ciTEST_F(ElimDeadIOComponentsTest, ElimTwoConstantIndices) {
143fd4e5da5Sopenharmony_ci  // Should reduce to uv[4]
144fd4e5da5Sopenharmony_ci  //
145fd4e5da5Sopenharmony_ci  // #version 450
146fd4e5da5Sopenharmony_ci  //
147fd4e5da5Sopenharmony_ci  // layout(location = 0) in vec4 uv[8];
148fd4e5da5Sopenharmony_ci  //
149fd4e5da5Sopenharmony_ci  // out gl_PerVertex {
150fd4e5da5Sopenharmony_ci  //     vec4 gl_Position;
151fd4e5da5Sopenharmony_ci  // };
152fd4e5da5Sopenharmony_ci  //
153fd4e5da5Sopenharmony_ci  // void main()
154fd4e5da5Sopenharmony_ci  // {
155fd4e5da5Sopenharmony_ci  //     gl_Position = uv[1] + uv[3];
156fd4e5da5Sopenharmony_ci  // }
157fd4e5da5Sopenharmony_ci  const std::string text = R"(
158fd4e5da5Sopenharmony_ci               OpCapability Shader
159fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
160fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
161fd4e5da5Sopenharmony_ci               OpEntryPoint Vertex %main "main" %_ %uv
162fd4e5da5Sopenharmony_ci               OpSource GLSL 450
163fd4e5da5Sopenharmony_ci               OpName %main "main"
164fd4e5da5Sopenharmony_ci               OpName %gl_PerVertex "gl_PerVertex"
165fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 0 "gl_Position"
166fd4e5da5Sopenharmony_ci               OpName %_ ""
167fd4e5da5Sopenharmony_ci               OpName %uv "uv"
168fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
169fd4e5da5Sopenharmony_ci               OpDecorate %gl_PerVertex Block
170fd4e5da5Sopenharmony_ci               OpDecorate %uv Location 0
171fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
172fd4e5da5Sopenharmony_ci          %3 = OpTypeFunction %void
173fd4e5da5Sopenharmony_ci      %float = OpTypeFloat 32
174fd4e5da5Sopenharmony_ci    %v4float = OpTypeVector %float 4
175fd4e5da5Sopenharmony_ci%gl_PerVertex = OpTypeStruct %v4float
176fd4e5da5Sopenharmony_ci%_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
177fd4e5da5Sopenharmony_ci          %_ = OpVariable %_ptr_Output_gl_PerVertex Output
178fd4e5da5Sopenharmony_ci        %int = OpTypeInt 32 1
179fd4e5da5Sopenharmony_ci      %int_0 = OpConstant %int 0
180fd4e5da5Sopenharmony_ci       %uint = OpTypeInt 32 0
181fd4e5da5Sopenharmony_ci     %uint_8 = OpConstant %uint 8
182fd4e5da5Sopenharmony_ci%_arr_v4float_uint_8 = OpTypeArray %v4float %uint_8
183fd4e5da5Sopenharmony_ci%_ptr_Input__arr_v4float_uint_8 = OpTypePointer Input %_arr_v4float_uint_8
184fd4e5da5Sopenharmony_ci         %uv = OpVariable %_ptr_Input__arr_v4float_uint_8 Input
185fd4e5da5Sopenharmony_ci      %int_1 = OpConstant %int 1
186fd4e5da5Sopenharmony_ci%_ptr_Input_v4float = OpTypePointer Input %v4float
187fd4e5da5Sopenharmony_ci      %int_3 = OpConstant %int 3
188fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
189fd4e5da5Sopenharmony_ci;CHECK-NOT: %uv = OpVariable %_ptr_Input__arr_v4float_uint_8 Input
190fd4e5da5Sopenharmony_ci;CHECK:     %uv = OpVariable %_ptr_Input__arr_v4float_uint_4 Input
191fd4e5da5Sopenharmony_ci       %main = OpFunction %void None %3
192fd4e5da5Sopenharmony_ci          %5 = OpLabel
193fd4e5da5Sopenharmony_ci         %20 = OpAccessChain %_ptr_Input_v4float %uv %int_1
194fd4e5da5Sopenharmony_ci         %21 = OpLoad %v4float %20
195fd4e5da5Sopenharmony_ci         %23 = OpAccessChain %_ptr_Input_v4float %uv %int_3
196fd4e5da5Sopenharmony_ci         %24 = OpLoad %v4float %23
197fd4e5da5Sopenharmony_ci         %25 = OpFAdd %v4float %21 %24
198fd4e5da5Sopenharmony_ci         %27 = OpAccessChain %_ptr_Output_v4float %_ %int_0
199fd4e5da5Sopenharmony_ci               OpStore %27 %25
200fd4e5da5Sopenharmony_ci               OpReturn
201fd4e5da5Sopenharmony_ci               OpFunctionEnd
202fd4e5da5Sopenharmony_ci)";
203fd4e5da5Sopenharmony_ci
204fd4e5da5Sopenharmony_ci  SetTargetEnv(SPV_ENV_VULKAN_1_3);
205fd4e5da5Sopenharmony_ci  SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
206fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<EliminateDeadIOComponentsPass>(
207fd4e5da5Sopenharmony_ci      text, true, spv::StorageClass::Input, false);
208fd4e5da5Sopenharmony_ci}
209fd4e5da5Sopenharmony_ci
210fd4e5da5Sopenharmony_ciTEST_F(ElimDeadIOComponentsTest, NoElimMaxConstantIndex) {
211fd4e5da5Sopenharmony_ci  // Should not reduce uv[8] because of max index of 7
212fd4e5da5Sopenharmony_ci  //
213fd4e5da5Sopenharmony_ci  // #version 450
214fd4e5da5Sopenharmony_ci  //
215fd4e5da5Sopenharmony_ci  // layout(location = 0) in vec4 uv[8];
216fd4e5da5Sopenharmony_ci  //
217fd4e5da5Sopenharmony_ci  // out gl_PerVertex {
218fd4e5da5Sopenharmony_ci  //     vec4 gl_Position;
219fd4e5da5Sopenharmony_ci  // };
220fd4e5da5Sopenharmony_ci  //
221fd4e5da5Sopenharmony_ci  // void main()
222fd4e5da5Sopenharmony_ci  // {
223fd4e5da5Sopenharmony_ci  //     gl_Position = uv[1] + uv[7];
224fd4e5da5Sopenharmony_ci  // }
225fd4e5da5Sopenharmony_ci  const std::string text = R"(
226fd4e5da5Sopenharmony_ci               OpCapability Shader
227fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
228fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
229fd4e5da5Sopenharmony_ci               OpEntryPoint Vertex %main "main" %_ %uv
230fd4e5da5Sopenharmony_ci               OpSource GLSL 450
231fd4e5da5Sopenharmony_ci               OpName %main "main"
232fd4e5da5Sopenharmony_ci               OpName %gl_PerVertex "gl_PerVertex"
233fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 0 "gl_Position"
234fd4e5da5Sopenharmony_ci               OpName %_ ""
235fd4e5da5Sopenharmony_ci               OpName %uv "uv"
236fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
237fd4e5da5Sopenharmony_ci               OpDecorate %gl_PerVertex Block
238fd4e5da5Sopenharmony_ci               OpDecorate %uv Location 0
239fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
240fd4e5da5Sopenharmony_ci          %3 = OpTypeFunction %void
241fd4e5da5Sopenharmony_ci      %float = OpTypeFloat 32
242fd4e5da5Sopenharmony_ci    %v4float = OpTypeVector %float 4
243fd4e5da5Sopenharmony_ci%gl_PerVertex = OpTypeStruct %v4float
244fd4e5da5Sopenharmony_ci%_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
245fd4e5da5Sopenharmony_ci          %_ = OpVariable %_ptr_Output_gl_PerVertex Output
246fd4e5da5Sopenharmony_ci        %int = OpTypeInt 32 1
247fd4e5da5Sopenharmony_ci      %int_0 = OpConstant %int 0
248fd4e5da5Sopenharmony_ci       %uint = OpTypeInt 32 0
249fd4e5da5Sopenharmony_ci     %uint_8 = OpConstant %uint 8
250fd4e5da5Sopenharmony_ci%_arr_v4float_uint_8 = OpTypeArray %v4float %uint_8
251fd4e5da5Sopenharmony_ci%_ptr_Input__arr_v4float_uint_8 = OpTypePointer Input %_arr_v4float_uint_8
252fd4e5da5Sopenharmony_ci         %uv = OpVariable %_ptr_Input__arr_v4float_uint_8 Input
253fd4e5da5Sopenharmony_ci      %int_1 = OpConstant %int 1
254fd4e5da5Sopenharmony_ci%_ptr_Input_v4float = OpTypePointer Input %v4float
255fd4e5da5Sopenharmony_ci      %int_7 = OpConstant %int 7
256fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
257fd4e5da5Sopenharmony_ci;CHECK:     %uv = OpVariable %_ptr_Input__arr_v4float_uint_8 Input
258fd4e5da5Sopenharmony_ci       %main = OpFunction %void None %3
259fd4e5da5Sopenharmony_ci          %5 = OpLabel
260fd4e5da5Sopenharmony_ci         %20 = OpAccessChain %_ptr_Input_v4float %uv %int_1
261fd4e5da5Sopenharmony_ci         %21 = OpLoad %v4float %20
262fd4e5da5Sopenharmony_ci         %23 = OpAccessChain %_ptr_Input_v4float %uv %int_7
263fd4e5da5Sopenharmony_ci         %24 = OpLoad %v4float %23
264fd4e5da5Sopenharmony_ci         %25 = OpFAdd %v4float %21 %24
265fd4e5da5Sopenharmony_ci         %27 = OpAccessChain %_ptr_Output_v4float %_ %int_0
266fd4e5da5Sopenharmony_ci               OpStore %27 %25
267fd4e5da5Sopenharmony_ci               OpReturn
268fd4e5da5Sopenharmony_ci               OpFunctionEnd
269fd4e5da5Sopenharmony_ci)";
270fd4e5da5Sopenharmony_ci
271fd4e5da5Sopenharmony_ci  SetTargetEnv(SPV_ENV_VULKAN_1_3);
272fd4e5da5Sopenharmony_ci  SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
273fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<EliminateDeadIOComponentsPass>(
274fd4e5da5Sopenharmony_ci      text, true, spv::StorageClass::Input, false);
275fd4e5da5Sopenharmony_ci}
276fd4e5da5Sopenharmony_ci
277fd4e5da5Sopenharmony_ciTEST_F(ElimDeadIOComponentsTest, NoElimNonConstantIndex) {
278fd4e5da5Sopenharmony_ci  // Should not reduce uv[8] because of non-constant index of ui
279fd4e5da5Sopenharmony_ci  //
280fd4e5da5Sopenharmony_ci  // #version 450
281fd4e5da5Sopenharmony_ci  //
282fd4e5da5Sopenharmony_ci  // layout(location = 0) in vec4 uv[8];
283fd4e5da5Sopenharmony_ci  //
284fd4e5da5Sopenharmony_ci  // out gl_PerVertex {
285fd4e5da5Sopenharmony_ci  //     vec4 gl_Position;
286fd4e5da5Sopenharmony_ci  // };
287fd4e5da5Sopenharmony_ci  //
288fd4e5da5Sopenharmony_ci  // uniform ubname {
289fd4e5da5Sopenharmony_ci  //     int ui;
290fd4e5da5Sopenharmony_ci  // } ubinst;
291fd4e5da5Sopenharmony_ci  //
292fd4e5da5Sopenharmony_ci  // void main()
293fd4e5da5Sopenharmony_ci  // {
294fd4e5da5Sopenharmony_ci  //     gl_Position = uv[1] + uv[ubinst.ui];
295fd4e5da5Sopenharmony_ci  // }
296fd4e5da5Sopenharmony_ci  const std::string text = R"(
297fd4e5da5Sopenharmony_ci               OpCapability Shader
298fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
299fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
300fd4e5da5Sopenharmony_ci               OpEntryPoint Vertex %main "main" %_ %uv %ubinst
301fd4e5da5Sopenharmony_ci               OpSource GLSL 450
302fd4e5da5Sopenharmony_ci               OpName %main "main"
303fd4e5da5Sopenharmony_ci               OpName %gl_PerVertex "gl_PerVertex"
304fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 0 "gl_Position"
305fd4e5da5Sopenharmony_ci               OpName %_ ""
306fd4e5da5Sopenharmony_ci               OpName %uv "uv"
307fd4e5da5Sopenharmony_ci               OpName %ubname "ubname"
308fd4e5da5Sopenharmony_ci               OpMemberName %ubname 0 "ui"
309fd4e5da5Sopenharmony_ci               OpName %ubinst "ubinst"
310fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
311fd4e5da5Sopenharmony_ci               OpDecorate %gl_PerVertex Block
312fd4e5da5Sopenharmony_ci               OpDecorate %uv Location 0
313fd4e5da5Sopenharmony_ci               OpMemberDecorate %ubname 0 Offset 0
314fd4e5da5Sopenharmony_ci               OpDecorate %ubname Block
315fd4e5da5Sopenharmony_ci               OpDecorate %ubinst DescriptorSet 0
316fd4e5da5Sopenharmony_ci               OpDecorate %ubinst Binding 0
317fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
318fd4e5da5Sopenharmony_ci          %3 = OpTypeFunction %void
319fd4e5da5Sopenharmony_ci      %float = OpTypeFloat 32
320fd4e5da5Sopenharmony_ci    %v4float = OpTypeVector %float 4
321fd4e5da5Sopenharmony_ci%gl_PerVertex = OpTypeStruct %v4float
322fd4e5da5Sopenharmony_ci%_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
323fd4e5da5Sopenharmony_ci          %_ = OpVariable %_ptr_Output_gl_PerVertex Output
324fd4e5da5Sopenharmony_ci        %int = OpTypeInt 32 1
325fd4e5da5Sopenharmony_ci      %int_0 = OpConstant %int 0
326fd4e5da5Sopenharmony_ci       %uint = OpTypeInt 32 0
327fd4e5da5Sopenharmony_ci     %uint_8 = OpConstant %uint 8
328fd4e5da5Sopenharmony_ci%_arr_v4float_uint_8 = OpTypeArray %v4float %uint_8
329fd4e5da5Sopenharmony_ci%_ptr_Input__arr_v4float_uint_8 = OpTypePointer Input %_arr_v4float_uint_8
330fd4e5da5Sopenharmony_ci         %uv = OpVariable %_ptr_Input__arr_v4float_uint_8 Input
331fd4e5da5Sopenharmony_ci      %int_1 = OpConstant %int 1
332fd4e5da5Sopenharmony_ci%_ptr_Input_v4float = OpTypePointer Input %v4float
333fd4e5da5Sopenharmony_ci     %ubname = OpTypeStruct %int
334fd4e5da5Sopenharmony_ci%_ptr_Uniform_ubname = OpTypePointer Uniform %ubname
335fd4e5da5Sopenharmony_ci     %ubinst = OpVariable %_ptr_Uniform_ubname Uniform
336fd4e5da5Sopenharmony_ci%_ptr_Uniform_int = OpTypePointer Uniform %int
337fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
338fd4e5da5Sopenharmony_ci;CHECK:  %uv = OpVariable %_ptr_Input__arr_v4float_uint_8 Input
339fd4e5da5Sopenharmony_ci       %main = OpFunction %void None %3
340fd4e5da5Sopenharmony_ci          %5 = OpLabel
341fd4e5da5Sopenharmony_ci         %20 = OpAccessChain %_ptr_Input_v4float %uv %int_1
342fd4e5da5Sopenharmony_ci         %21 = OpLoad %v4float %20
343fd4e5da5Sopenharmony_ci         %26 = OpAccessChain %_ptr_Uniform_int %ubinst %int_0
344fd4e5da5Sopenharmony_ci         %27 = OpLoad %int %26
345fd4e5da5Sopenharmony_ci         %28 = OpAccessChain %_ptr_Input_v4float %uv %27
346fd4e5da5Sopenharmony_ci         %29 = OpLoad %v4float %28
347fd4e5da5Sopenharmony_ci         %30 = OpFAdd %v4float %21 %29
348fd4e5da5Sopenharmony_ci         %32 = OpAccessChain %_ptr_Output_v4float %_ %int_0
349fd4e5da5Sopenharmony_ci               OpStore %32 %30
350fd4e5da5Sopenharmony_ci               OpReturn
351fd4e5da5Sopenharmony_ci               OpFunctionEnd
352fd4e5da5Sopenharmony_ci)";
353fd4e5da5Sopenharmony_ci
354fd4e5da5Sopenharmony_ci  SetTargetEnv(SPV_ENV_VULKAN_1_3);
355fd4e5da5Sopenharmony_ci  SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
356fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<EliminateDeadIOComponentsPass>(
357fd4e5da5Sopenharmony_ci      text, true, spv::StorageClass::Input, false);
358fd4e5da5Sopenharmony_ci}
359fd4e5da5Sopenharmony_ci
360fd4e5da5Sopenharmony_ciTEST_F(ElimDeadIOComponentsTest, NoElimNonIndexedAccessChain) {
361fd4e5da5Sopenharmony_ci  // Should not change due to non-indexed access chain
362fd4e5da5Sopenharmony_ci  const std::string text = R"(
363fd4e5da5Sopenharmony_ci               OpCapability Shader
364fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
365fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
366fd4e5da5Sopenharmony_ci               OpEntryPoint Vertex %main "main" %_ %uv
367fd4e5da5Sopenharmony_ci               OpSource GLSL 450
368fd4e5da5Sopenharmony_ci               OpName %main "main"
369fd4e5da5Sopenharmony_ci               OpName %gl_PerVertex "gl_PerVertex"
370fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 0 "gl_Position"
371fd4e5da5Sopenharmony_ci               OpName %_ ""
372fd4e5da5Sopenharmony_ci               OpName %uv "uv"
373fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
374fd4e5da5Sopenharmony_ci               OpDecorate %gl_PerVertex Block
375fd4e5da5Sopenharmony_ci               OpDecorate %uv Location 0
376fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
377fd4e5da5Sopenharmony_ci          %3 = OpTypeFunction %void
378fd4e5da5Sopenharmony_ci      %float = OpTypeFloat 32
379fd4e5da5Sopenharmony_ci    %v4float = OpTypeVector %float 4
380fd4e5da5Sopenharmony_ci%gl_PerVertex = OpTypeStruct %v4float
381fd4e5da5Sopenharmony_ci%_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
382fd4e5da5Sopenharmony_ci          %_ = OpVariable %_ptr_Output_gl_PerVertex Output
383fd4e5da5Sopenharmony_ci        %int = OpTypeInt 32 1
384fd4e5da5Sopenharmony_ci      %int_0 = OpConstant %int 0
385fd4e5da5Sopenharmony_ci       %uint = OpTypeInt 32 0
386fd4e5da5Sopenharmony_ci     %uint_8 = OpConstant %uint 8
387fd4e5da5Sopenharmony_ci%_arr_v4float_uint_8 = OpTypeArray %v4float %uint_8
388fd4e5da5Sopenharmony_ci%_ptr_Input__arr_v4float_uint_8 = OpTypePointer Input %_arr_v4float_uint_8
389fd4e5da5Sopenharmony_ci           %uv = OpVariable %_ptr_Input__arr_v4float_uint_8 Input
390fd4e5da5Sopenharmony_ci      %int_1 = OpConstant %int 1
391fd4e5da5Sopenharmony_ci%_ptr_Input_v4float = OpTypePointer Input %v4float
392fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
393fd4e5da5Sopenharmony_ci;CHECK:  %uv = OpVariable %_ptr_Input__arr_v4float_uint_8 Input
394fd4e5da5Sopenharmony_ci       %main = OpFunction %void None %3
395fd4e5da5Sopenharmony_ci          %5 = OpLabel
396fd4e5da5Sopenharmony_ci         %20 = OpAccessChain %_ptr_Input__arr_v4float_uint_8 %uv
397fd4e5da5Sopenharmony_ci               OpReturn
398fd4e5da5Sopenharmony_ci               OpFunctionEnd
399fd4e5da5Sopenharmony_ci)";
400fd4e5da5Sopenharmony_ci
401fd4e5da5Sopenharmony_ci  SetTargetEnv(SPV_ENV_VULKAN_1_3);
402fd4e5da5Sopenharmony_ci  SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
403fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<EliminateDeadIOComponentsPass>(
404fd4e5da5Sopenharmony_ci      text, true, spv::StorageClass::Input, false);
405fd4e5da5Sopenharmony_ci}
406fd4e5da5Sopenharmony_ci
407fd4e5da5Sopenharmony_ciTEST_F(ElimDeadIOComponentsTest, ElimStructMember) {
408fd4e5da5Sopenharmony_ci  // Should eliminate uv
409fd4e5da5Sopenharmony_ci  //
410fd4e5da5Sopenharmony_ci  // #version 450
411fd4e5da5Sopenharmony_ci  //
412fd4e5da5Sopenharmony_ci  // in Vertex {
413fd4e5da5Sopenharmony_ci  //   vec4 Cd;
414fd4e5da5Sopenharmony_ci  //   vec2 uv;
415fd4e5da5Sopenharmony_ci  // } iVert;
416fd4e5da5Sopenharmony_ci  //
417fd4e5da5Sopenharmony_ci  // out vec4 fragColor;
418fd4e5da5Sopenharmony_ci  //
419fd4e5da5Sopenharmony_ci  // void main()
420fd4e5da5Sopenharmony_ci  // {
421fd4e5da5Sopenharmony_ci  //   vec4 color = vec4(iVert.Cd);
422fd4e5da5Sopenharmony_ci  //   fragColor = color;
423fd4e5da5Sopenharmony_ci  // }
424fd4e5da5Sopenharmony_ci  const std::string text = R"(
425fd4e5da5Sopenharmony_ci               OpCapability Shader
426fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
427fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
428fd4e5da5Sopenharmony_ci               OpEntryPoint Fragment %main "main" %iVert %fragColor
429fd4e5da5Sopenharmony_ci               OpExecutionMode %main OriginUpperLeft
430fd4e5da5Sopenharmony_ci               OpSource GLSL 450
431fd4e5da5Sopenharmony_ci               OpName %main "main"
432fd4e5da5Sopenharmony_ci               OpName %Vertex "Vertex"
433fd4e5da5Sopenharmony_ci               OpMemberName %Vertex 0 "Cd"
434fd4e5da5Sopenharmony_ci               OpMemberName %Vertex 1 "uv"
435fd4e5da5Sopenharmony_ci               OpName %iVert "iVert"
436fd4e5da5Sopenharmony_ci               OpName %fragColor "fragColor"
437fd4e5da5Sopenharmony_ci               OpDecorate %Vertex Block
438fd4e5da5Sopenharmony_ci               OpDecorate %iVert Location 0
439fd4e5da5Sopenharmony_ci               OpDecorate %fragColor Location 0
440fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
441fd4e5da5Sopenharmony_ci          %3 = OpTypeFunction %void
442fd4e5da5Sopenharmony_ci      %float = OpTypeFloat 32
443fd4e5da5Sopenharmony_ci    %v4float = OpTypeVector %float 4
444fd4e5da5Sopenharmony_ci    %v2float = OpTypeVector %float 2
445fd4e5da5Sopenharmony_ci     %Vertex = OpTypeStruct %v4float %v2float
446fd4e5da5Sopenharmony_ci; CHECK: %Vertex = OpTypeStruct %v4float %v2float
447fd4e5da5Sopenharmony_ci; CHECK: %Vertex_0 = OpTypeStruct %v4float
448fd4e5da5Sopenharmony_ci%_ptr_Input_Vertex = OpTypePointer Input %Vertex
449fd4e5da5Sopenharmony_ci; CHECK: [[pty:%\w+]] = OpTypePointer Input %Vertex_0
450fd4e5da5Sopenharmony_ci      %iVert = OpVariable %_ptr_Input_Vertex Input
451fd4e5da5Sopenharmony_ci; CHECK: %iVert = OpVariable [[pty]] Input
452fd4e5da5Sopenharmony_ci        %int = OpTypeInt 32 1
453fd4e5da5Sopenharmony_ci      %int_0 = OpConstant %int 0
454fd4e5da5Sopenharmony_ci%_ptr_Input_v4float = OpTypePointer Input %v4float
455fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
456fd4e5da5Sopenharmony_ci  %fragColor = OpVariable %_ptr_Output_v4float Output
457fd4e5da5Sopenharmony_ci       %main = OpFunction %void None %3
458fd4e5da5Sopenharmony_ci          %5 = OpLabel
459fd4e5da5Sopenharmony_ci         %17 = OpAccessChain %_ptr_Input_v4float %iVert %int_0
460fd4e5da5Sopenharmony_ci         %18 = OpLoad %v4float %17
461fd4e5da5Sopenharmony_ci               OpStore %fragColor %18
462fd4e5da5Sopenharmony_ci               OpReturn
463fd4e5da5Sopenharmony_ci               OpFunctionEnd
464fd4e5da5Sopenharmony_ci)";
465fd4e5da5Sopenharmony_ci
466fd4e5da5Sopenharmony_ci  SetTargetEnv(SPV_ENV_VULKAN_1_3);
467fd4e5da5Sopenharmony_ci  SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
468fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<EliminateDeadIOComponentsPass>(
469fd4e5da5Sopenharmony_ci      text, true, spv::StorageClass::Input, false);
470fd4e5da5Sopenharmony_ci}
471fd4e5da5Sopenharmony_ci
472fd4e5da5Sopenharmony_ciTEST_F(ElimDeadIOComponentsTest, ElimOutputStructMember) {
473fd4e5da5Sopenharmony_ci  // Should eliminate uv from Vertex and all but gl_Position from gl_PerVertex
474fd4e5da5Sopenharmony_ci  //
475fd4e5da5Sopenharmony_ci  // #version 450
476fd4e5da5Sopenharmony_ci  //
477fd4e5da5Sopenharmony_ci  // out Vertex {
478fd4e5da5Sopenharmony_ci  //   vec4 Cd;
479fd4e5da5Sopenharmony_ci  //   vec2 uv;
480fd4e5da5Sopenharmony_ci  // } oVert;
481fd4e5da5Sopenharmony_ci  //
482fd4e5da5Sopenharmony_ci  // in vec3 P;
483fd4e5da5Sopenharmony_ci  //
484fd4e5da5Sopenharmony_ci  // void main()
485fd4e5da5Sopenharmony_ci  // {
486fd4e5da5Sopenharmony_ci  //   vec4 worldSpacePos = vec4(P, 1);
487fd4e5da5Sopenharmony_ci  //   oVert.Cd = vec4(1, 0.5, 0, 1);
488fd4e5da5Sopenharmony_ci  //   gl_Position = worldSpacePos;
489fd4e5da5Sopenharmony_ci  // }
490fd4e5da5Sopenharmony_ci
491fd4e5da5Sopenharmony_ci  const std::string text = R"(
492fd4e5da5Sopenharmony_ci               OpCapability Shader
493fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
494fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
495fd4e5da5Sopenharmony_ci               OpEntryPoint Vertex %main "main" %P %oVert %_
496fd4e5da5Sopenharmony_ci               OpSource GLSL 450
497fd4e5da5Sopenharmony_ci               OpName %main "main"
498fd4e5da5Sopenharmony_ci               OpName %P "P"
499fd4e5da5Sopenharmony_ci               OpName %Vertex "Vertex"
500fd4e5da5Sopenharmony_ci               OpMemberName %Vertex 0 "Cd"
501fd4e5da5Sopenharmony_ci               OpMemberName %Vertex 1 "uv"
502fd4e5da5Sopenharmony_ci               OpName %oVert "oVert"
503fd4e5da5Sopenharmony_ci               OpName %gl_PerVertex "gl_PerVertex"
504fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 0 "gl_Position"
505fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 1 "gl_PointSize"
506fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 2 "gl_ClipDistance"
507fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 3 "gl_CullDistance"
508fd4e5da5Sopenharmony_ci               OpName %_ ""
509fd4e5da5Sopenharmony_ci               OpDecorate %P Location 0
510fd4e5da5Sopenharmony_ci               OpDecorate %Vertex Block
511fd4e5da5Sopenharmony_ci               OpDecorate %oVert Location 0
512fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
513fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 1 BuiltIn PointSize
514fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 2 BuiltIn ClipDistance
515fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 3 BuiltIn CullDistance
516fd4e5da5Sopenharmony_ci               OpDecorate %gl_PerVertex Block
517fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
518fd4e5da5Sopenharmony_ci          %3 = OpTypeFunction %void
519fd4e5da5Sopenharmony_ci      %float = OpTypeFloat 32
520fd4e5da5Sopenharmony_ci    %v4float = OpTypeVector %float 4
521fd4e5da5Sopenharmony_ci    %v3float = OpTypeVector %float 3
522fd4e5da5Sopenharmony_ci%_ptr_Input_v3float = OpTypePointer Input %v3float
523fd4e5da5Sopenharmony_ci          %P = OpVariable %_ptr_Input_v3float Input
524fd4e5da5Sopenharmony_ci    %float_1 = OpConstant %float 1
525fd4e5da5Sopenharmony_ci    %v2float = OpTypeVector %float 2
526fd4e5da5Sopenharmony_ci     %Vertex = OpTypeStruct %v4float %v2float
527fd4e5da5Sopenharmony_ci%_ptr_Output_Vertex = OpTypePointer Output %Vertex
528fd4e5da5Sopenharmony_ci      %oVert = OpVariable %_ptr_Output_Vertex Output
529fd4e5da5Sopenharmony_ci        %int = OpTypeInt 32 1
530fd4e5da5Sopenharmony_ci      %int_0 = OpConstant %int 0
531fd4e5da5Sopenharmony_ci  %float_0_5 = OpConstant %float 0.5
532fd4e5da5Sopenharmony_ci    %float_0 = OpConstant %float 0
533fd4e5da5Sopenharmony_ci         %27 = OpConstantComposite %v4float %float_1 %float_0_5 %float_0 %float_1
534fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
535fd4e5da5Sopenharmony_ci       %uint = OpTypeInt 32 0
536fd4e5da5Sopenharmony_ci     %uint_1 = OpConstant %uint 1
537fd4e5da5Sopenharmony_ci%_arr_float_uint_1 = OpTypeArray %float %uint_1
538fd4e5da5Sopenharmony_ci%gl_PerVertex = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
539fd4e5da5Sopenharmony_ci%_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
540fd4e5da5Sopenharmony_ci          %_ = OpVariable %_ptr_Output_gl_PerVertex Output
541fd4e5da5Sopenharmony_ci; CHECK: %Vertex = OpTypeStruct %v4float %v2float
542fd4e5da5Sopenharmony_ci; CHECK: %gl_PerVertex = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
543fd4e5da5Sopenharmony_ci; CHECK: %_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
544fd4e5da5Sopenharmony_ci; CHECK: [[sty:%\w+]] = OpTypeStruct %v4float
545fd4e5da5Sopenharmony_ci; CHECK: [[pty:%\w+]] = OpTypePointer Output [[sty]]
546fd4e5da5Sopenharmony_ci; CHECK: %oVert = OpVariable [[pty]] Output
547fd4e5da5Sopenharmony_ci; CHECK: [[sty2:%\w+]] = OpTypeStruct %v4float
548fd4e5da5Sopenharmony_ci; CHECK: [[pty2:%\w+]] = OpTypePointer Output [[sty2]]
549fd4e5da5Sopenharmony_ci; CHECK: %_ = OpVariable [[pty2]] Output
550fd4e5da5Sopenharmony_ci       %main = OpFunction %void None %3
551fd4e5da5Sopenharmony_ci          %5 = OpLabel
552fd4e5da5Sopenharmony_ci         %13 = OpLoad %v3float %P
553fd4e5da5Sopenharmony_ci         %15 = OpCompositeExtract %float %13 0
554fd4e5da5Sopenharmony_ci         %16 = OpCompositeExtract %float %13 1
555fd4e5da5Sopenharmony_ci         %17 = OpCompositeExtract %float %13 2
556fd4e5da5Sopenharmony_ci         %18 = OpCompositeConstruct %v4float %15 %16 %17 %float_1
557fd4e5da5Sopenharmony_ci         %29 = OpAccessChain %_ptr_Output_v4float %oVert %int_0
558fd4e5da5Sopenharmony_ci               OpStore %29 %27
559fd4e5da5Sopenharmony_ci         %37 = OpAccessChain %_ptr_Output_v4float %_ %int_0
560fd4e5da5Sopenharmony_ci               OpStore %37 %18
561fd4e5da5Sopenharmony_ci               OpReturn
562fd4e5da5Sopenharmony_ci               OpFunctionEnd
563fd4e5da5Sopenharmony_ci)";
564fd4e5da5Sopenharmony_ci
565fd4e5da5Sopenharmony_ci  SetTargetEnv(SPV_ENV_VULKAN_1_3);
566fd4e5da5Sopenharmony_ci  SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
567fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<EliminateDeadIOComponentsPass>(
568fd4e5da5Sopenharmony_ci      text, true, spv::StorageClass::Output, false);
569fd4e5da5Sopenharmony_ci}
570fd4e5da5Sopenharmony_ci
571fd4e5da5Sopenharmony_ciTEST_F(ElimDeadIOComponentsTest, ElimOutputArrayMembers) {
572fd4e5da5Sopenharmony_ci  // Should reduce to uv[2]
573fd4e5da5Sopenharmony_ci  //
574fd4e5da5Sopenharmony_ci  // #version 450
575fd4e5da5Sopenharmony_ci  //
576fd4e5da5Sopenharmony_ci  // layout(location = 0) out vec2 uv[8];
577fd4e5da5Sopenharmony_ci  //
578fd4e5da5Sopenharmony_ci  // void main()
579fd4e5da5Sopenharmony_ci  // {
580fd4e5da5Sopenharmony_ci  //     uv[1] = vec2(1, 0.5);
581fd4e5da5Sopenharmony_ci  // }
582fd4e5da5Sopenharmony_ci
583fd4e5da5Sopenharmony_ci  const std::string text = R"(
584fd4e5da5Sopenharmony_ci               OpCapability Shader
585fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
586fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
587fd4e5da5Sopenharmony_ci               OpEntryPoint Fragment %main "main" %uv
588fd4e5da5Sopenharmony_ci               OpExecutionMode %main OriginUpperLeft
589fd4e5da5Sopenharmony_ci               OpSource GLSL 450
590fd4e5da5Sopenharmony_ci               OpName %main "main"
591fd4e5da5Sopenharmony_ci               OpName %uv "uv"
592fd4e5da5Sopenharmony_ci               OpDecorate %uv Location 0
593fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
594fd4e5da5Sopenharmony_ci          %3 = OpTypeFunction %void
595fd4e5da5Sopenharmony_ci      %float = OpTypeFloat 32
596fd4e5da5Sopenharmony_ci    %v2float = OpTypeVector %float 2
597fd4e5da5Sopenharmony_ci       %uint = OpTypeInt 32 0
598fd4e5da5Sopenharmony_ci     %uint_8 = OpConstant %uint 8
599fd4e5da5Sopenharmony_ci%_arr_v2float_uint_8 = OpTypeArray %v2float %uint_8
600fd4e5da5Sopenharmony_ci%_ptr_Output__arr_v2float_uint_8 = OpTypePointer Output %_arr_v2float_uint_8
601fd4e5da5Sopenharmony_ci         %uv = OpVariable %_ptr_Output__arr_v2float_uint_8 Output
602fd4e5da5Sopenharmony_ci;CHECK-NOT:         %uv = OpVariable %_ptr_Output__arr_v2float_uint_8 Output
603fd4e5da5Sopenharmony_ci;CHECK:             %uv = OpVariable %_ptr_Output__arr_v2float_uint_2 Output
604fd4e5da5Sopenharmony_ci        %int = OpTypeInt 32 1
605fd4e5da5Sopenharmony_ci      %int_1 = OpConstant %int 1
606fd4e5da5Sopenharmony_ci    %float_1 = OpConstant %float 1
607fd4e5da5Sopenharmony_ci  %float_0_5 = OpConstant %float 0.5
608fd4e5da5Sopenharmony_ci         %17 = OpConstantComposite %v2float %float_1 %float_0_5
609fd4e5da5Sopenharmony_ci%_ptr_Output_v2float = OpTypePointer Output %v2float
610fd4e5da5Sopenharmony_ci       %main = OpFunction %void None %3
611fd4e5da5Sopenharmony_ci          %5 = OpLabel
612fd4e5da5Sopenharmony_ci         %19 = OpAccessChain %_ptr_Output_v2float %uv %int_1
613fd4e5da5Sopenharmony_ci               OpStore %19 %17
614fd4e5da5Sopenharmony_ci               OpReturn
615fd4e5da5Sopenharmony_ci               OpFunctionEnd
616fd4e5da5Sopenharmony_ci)";
617fd4e5da5Sopenharmony_ci
618fd4e5da5Sopenharmony_ci  SetTargetEnv(SPV_ENV_VULKAN_1_3);
619fd4e5da5Sopenharmony_ci  SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
620fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<EliminateDeadIOComponentsPass>(
621fd4e5da5Sopenharmony_ci      text, true, spv::StorageClass::Output, false);
622fd4e5da5Sopenharmony_ci}
623fd4e5da5Sopenharmony_ci
624fd4e5da5Sopenharmony_ciTEST_F(ElimDeadIOComponentsTest, VertexOnly) {
625fd4e5da5Sopenharmony_ci  // Should NOT eliminate uv
626fd4e5da5Sopenharmony_ci  //
627fd4e5da5Sopenharmony_ci  // #version 450
628fd4e5da5Sopenharmony_ci  //
629fd4e5da5Sopenharmony_ci  // in Vertex {
630fd4e5da5Sopenharmony_ci  //   vec4 Cd;
631fd4e5da5Sopenharmony_ci  //   vec2 uv;
632fd4e5da5Sopenharmony_ci  // } iVert;
633fd4e5da5Sopenharmony_ci  //
634fd4e5da5Sopenharmony_ci  // out vec4 fragColor;
635fd4e5da5Sopenharmony_ci  //
636fd4e5da5Sopenharmony_ci  // void main()
637fd4e5da5Sopenharmony_ci  // {
638fd4e5da5Sopenharmony_ci  //   vec4 color = vec4(iVert.Cd);
639fd4e5da5Sopenharmony_ci  //   fragColor = color;
640fd4e5da5Sopenharmony_ci  // }
641fd4e5da5Sopenharmony_ci  const std::string text = R"(
642fd4e5da5Sopenharmony_ci               OpCapability Shader
643fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
644fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
645fd4e5da5Sopenharmony_ci               OpEntryPoint Fragment %main "main" %iVert %fragColor
646fd4e5da5Sopenharmony_ci               OpExecutionMode %main OriginUpperLeft
647fd4e5da5Sopenharmony_ci               OpSource GLSL 450
648fd4e5da5Sopenharmony_ci               OpName %main "main"
649fd4e5da5Sopenharmony_ci               OpName %Vertex "Vertex"
650fd4e5da5Sopenharmony_ci               OpMemberName %Vertex 0 "Cd"
651fd4e5da5Sopenharmony_ci               OpMemberName %Vertex 1 "uv"
652fd4e5da5Sopenharmony_ci               OpName %iVert "iVert"
653fd4e5da5Sopenharmony_ci               OpName %fragColor "fragColor"
654fd4e5da5Sopenharmony_ci               OpDecorate %Vertex Block
655fd4e5da5Sopenharmony_ci               OpDecorate %iVert Location 0
656fd4e5da5Sopenharmony_ci               OpDecorate %fragColor Location 0
657fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
658fd4e5da5Sopenharmony_ci          %3 = OpTypeFunction %void
659fd4e5da5Sopenharmony_ci      %float = OpTypeFloat 32
660fd4e5da5Sopenharmony_ci    %v4float = OpTypeVector %float 4
661fd4e5da5Sopenharmony_ci    %v2float = OpTypeVector %float 2
662fd4e5da5Sopenharmony_ci     %Vertex = OpTypeStruct %v4float %v2float
663fd4e5da5Sopenharmony_ci; CHECK: %Vertex = OpTypeStruct %v4float %v2float
664fd4e5da5Sopenharmony_ci%_ptr_Input_Vertex = OpTypePointer Input %Vertex
665fd4e5da5Sopenharmony_ci; CHECK: %_ptr_Input_Vertex = OpTypePointer Input %Vertex
666fd4e5da5Sopenharmony_ci      %iVert = OpVariable %_ptr_Input_Vertex Input
667fd4e5da5Sopenharmony_ci; CHECK: %iVert = OpVariable %_ptr_Input_Vertex Input
668fd4e5da5Sopenharmony_ci        %int = OpTypeInt 32 1
669fd4e5da5Sopenharmony_ci      %int_0 = OpConstant %int 0
670fd4e5da5Sopenharmony_ci%_ptr_Input_v4float = OpTypePointer Input %v4float
671fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
672fd4e5da5Sopenharmony_ci  %fragColor = OpVariable %_ptr_Output_v4float Output
673fd4e5da5Sopenharmony_ci       %main = OpFunction %void None %3
674fd4e5da5Sopenharmony_ci          %5 = OpLabel
675fd4e5da5Sopenharmony_ci         %17 = OpAccessChain %_ptr_Input_v4float %iVert %int_0
676fd4e5da5Sopenharmony_ci         %18 = OpLoad %v4float %17
677fd4e5da5Sopenharmony_ci               OpStore %fragColor %18
678fd4e5da5Sopenharmony_ci               OpReturn
679fd4e5da5Sopenharmony_ci               OpFunctionEnd
680fd4e5da5Sopenharmony_ci)";
681fd4e5da5Sopenharmony_ci
682fd4e5da5Sopenharmony_ci  SetTargetEnv(SPV_ENV_VULKAN_1_3);
683fd4e5da5Sopenharmony_ci  SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
684fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<EliminateDeadIOComponentsPass>(
685fd4e5da5Sopenharmony_ci      text, true, spv::StorageClass::Input, true);
686fd4e5da5Sopenharmony_ci}
687fd4e5da5Sopenharmony_ci
688fd4e5da5Sopenharmony_ciTEST_F(ElimDeadIOComponentsTest, TescInput) {
689fd4e5da5Sopenharmony_ci  // Eliminate PointSize, ClipDistance, CullDistance from gl_in[]
690fd4e5da5Sopenharmony_ci  //
691fd4e5da5Sopenharmony_ci  // #version 450
692fd4e5da5Sopenharmony_ci  //
693fd4e5da5Sopenharmony_ci  // layout (vertices = 4) out;
694fd4e5da5Sopenharmony_ci  //
695fd4e5da5Sopenharmony_ci  // void main()
696fd4e5da5Sopenharmony_ci  // {
697fd4e5da5Sopenharmony_ci  //     vec4 pos = gl_in[gl_InvocationID].gl_Position;
698fd4e5da5Sopenharmony_ci  //     gl_out[gl_InvocationID].gl_Position = pos;
699fd4e5da5Sopenharmony_ci  // }
700fd4e5da5Sopenharmony_ci  const std::string text = R"(
701fd4e5da5Sopenharmony_ci               OpCapability Tessellation
702fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
703fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
704fd4e5da5Sopenharmony_ci               OpEntryPoint TessellationControl %main "main" %gl_in %gl_InvocationID %gl_out
705fd4e5da5Sopenharmony_ci               OpExecutionMode %main OutputVertices 4
706fd4e5da5Sopenharmony_ci               OpSource GLSL 450
707fd4e5da5Sopenharmony_ci               OpName %main "main"
708fd4e5da5Sopenharmony_ci               OpName %pos "pos"
709fd4e5da5Sopenharmony_ci               OpName %gl_PerVertex "gl_PerVertex"
710fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 0 "gl_Position"
711fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 1 "gl_PointSize"
712fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 2 "gl_ClipDistance"
713fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 3 "gl_CullDistance"
714fd4e5da5Sopenharmony_ci               OpName %gl_in "gl_in"
715fd4e5da5Sopenharmony_ci               OpName %gl_InvocationID "gl_InvocationID"
716fd4e5da5Sopenharmony_ci               OpName %gl_PerVertex_0 "gl_PerVertex"
717fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex_0 0 "gl_Position"
718fd4e5da5Sopenharmony_ci               OpName %gl_out "gl_out"
719fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
720fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 1 BuiltIn PointSize
721fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 2 BuiltIn ClipDistance
722fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 3 BuiltIn CullDistance
723fd4e5da5Sopenharmony_ci               OpDecorate %gl_PerVertex Block
724fd4e5da5Sopenharmony_ci               OpDecorate %gl_InvocationID BuiltIn InvocationId
725fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex_0 0 BuiltIn Position
726fd4e5da5Sopenharmony_ci               OpDecorate %gl_PerVertex_0 Block
727fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
728fd4e5da5Sopenharmony_ci          %3 = OpTypeFunction %void
729fd4e5da5Sopenharmony_ci      %float = OpTypeFloat 32
730fd4e5da5Sopenharmony_ci    %v4float = OpTypeVector %float 4
731fd4e5da5Sopenharmony_ci%_ptr_Function_v4float = OpTypePointer Function %v4float
732fd4e5da5Sopenharmony_ci       %uint = OpTypeInt 32 0
733fd4e5da5Sopenharmony_ci     %uint_1 = OpConstant %uint 1
734fd4e5da5Sopenharmony_ci%_arr_float_uint_1 = OpTypeArray %float %uint_1
735fd4e5da5Sopenharmony_ci%gl_PerVertex = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
736fd4e5da5Sopenharmony_ci    %uint_32 = OpConstant %uint 32
737fd4e5da5Sopenharmony_ci%_arr_gl_PerVertex_uint_32 = OpTypeArray %gl_PerVertex %uint_32
738fd4e5da5Sopenharmony_ci%_ptr_Input__arr_gl_PerVertex_uint_32 = OpTypePointer Input %_arr_gl_PerVertex_uint_32
739fd4e5da5Sopenharmony_ci      %gl_in = OpVariable %_ptr_Input__arr_gl_PerVertex_uint_32 Input
740fd4e5da5Sopenharmony_ci        %int = OpTypeInt 32 1
741fd4e5da5Sopenharmony_ci%_ptr_Input_int = OpTypePointer Input %int
742fd4e5da5Sopenharmony_ci%gl_InvocationID = OpVariable %_ptr_Input_int Input
743fd4e5da5Sopenharmony_ci      %int_0 = OpConstant %int 0
744fd4e5da5Sopenharmony_ci%_ptr_Input_v4float = OpTypePointer Input %v4float
745fd4e5da5Sopenharmony_ci%gl_PerVertex_0 = OpTypeStruct %v4float
746fd4e5da5Sopenharmony_ci     %uint_4 = OpConstant %uint 4
747fd4e5da5Sopenharmony_ci%_arr_gl_PerVertex_0_uint_4 = OpTypeArray %gl_PerVertex_0 %uint_4
748fd4e5da5Sopenharmony_ci%_ptr_Output__arr_gl_PerVertex_0_uint_4 = OpTypePointer Output %_arr_gl_PerVertex_0_uint_4
749fd4e5da5Sopenharmony_ci     %gl_out = OpVariable %_ptr_Output__arr_gl_PerVertex_0_uint_4 Output
750fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
751fd4e5da5Sopenharmony_ci; CHECK: %gl_PerVertex = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
752fd4e5da5Sopenharmony_ci; CHECK: [[sty:%\w+]] = OpTypeStruct %v4float
753fd4e5da5Sopenharmony_ci; CHECK: [[asty:%\w+]] = OpTypeArray [[sty]] %uint_32
754fd4e5da5Sopenharmony_ci; CHECK: [[pasty:%\w+]] = OpTypePointer Input [[asty]]
755fd4e5da5Sopenharmony_ci; CHECK: %gl_in = OpVariable [[pasty]] Input
756fd4e5da5Sopenharmony_ci       %main = OpFunction %void None %3
757fd4e5da5Sopenharmony_ci          %5 = OpLabel
758fd4e5da5Sopenharmony_ci        %pos = OpVariable %_ptr_Function_v4float Function
759fd4e5da5Sopenharmony_ci         %21 = OpLoad %int %gl_InvocationID
760fd4e5da5Sopenharmony_ci         %24 = OpAccessChain %_ptr_Input_v4float %gl_in %21 %int_0
761fd4e5da5Sopenharmony_ci         %25 = OpLoad %v4float %24
762fd4e5da5Sopenharmony_ci               OpStore %pos %25
763fd4e5da5Sopenharmony_ci         %31 = OpLoad %int %gl_InvocationID
764fd4e5da5Sopenharmony_ci         %32 = OpLoad %v4float %pos
765fd4e5da5Sopenharmony_ci         %34 = OpAccessChain %_ptr_Output_v4float %gl_out %31 %int_0
766fd4e5da5Sopenharmony_ci               OpStore %34 %32
767fd4e5da5Sopenharmony_ci               OpReturn
768fd4e5da5Sopenharmony_ci               OpFunctionEnd
769fd4e5da5Sopenharmony_ci)";
770fd4e5da5Sopenharmony_ci
771fd4e5da5Sopenharmony_ci  SetTargetEnv(SPV_ENV_VULKAN_1_3);
772fd4e5da5Sopenharmony_ci  SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
773fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<EliminateDeadIOComponentsPass>(
774fd4e5da5Sopenharmony_ci      text, true, spv::StorageClass::Input, false);
775fd4e5da5Sopenharmony_ci}
776fd4e5da5Sopenharmony_ci
777fd4e5da5Sopenharmony_ciTEST_F(ElimDeadIOComponentsTest, TescOutput) {
778fd4e5da5Sopenharmony_ci  // Eliminate PointSize, ClipDistance, CullDistance from gl_out[]
779fd4e5da5Sopenharmony_ci  //
780fd4e5da5Sopenharmony_ci  // #version 450
781fd4e5da5Sopenharmony_ci  //
782fd4e5da5Sopenharmony_ci  // layout (vertices = 4) out;
783fd4e5da5Sopenharmony_ci  //
784fd4e5da5Sopenharmony_ci  // void main()
785fd4e5da5Sopenharmony_ci  // {
786fd4e5da5Sopenharmony_ci  //     vec4 pos = gl_in[gl_InvocationID].gl_Position;
787fd4e5da5Sopenharmony_ci  //     gl_out[gl_InvocationID].gl_Position = pos;
788fd4e5da5Sopenharmony_ci  // }
789fd4e5da5Sopenharmony_ci  const std::string text = R"(
790fd4e5da5Sopenharmony_ci               OpCapability Tessellation
791fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
792fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
793fd4e5da5Sopenharmony_ci               OpEntryPoint TessellationControl %main "main" %gl_in %gl_InvocationID %gl_out
794fd4e5da5Sopenharmony_ci               OpExecutionMode %main OutputVertices 4
795fd4e5da5Sopenharmony_ci               OpSource GLSL 450
796fd4e5da5Sopenharmony_ci               OpName %main "main"
797fd4e5da5Sopenharmony_ci               OpName %pos "pos"
798fd4e5da5Sopenharmony_ci               OpName %gl_PerVertex "gl_PerVertex"
799fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 0 "gl_Position"
800fd4e5da5Sopenharmony_ci               OpName %gl_in "gl_in"
801fd4e5da5Sopenharmony_ci               OpName %gl_InvocationID "gl_InvocationID"
802fd4e5da5Sopenharmony_ci               OpName %gl_PerVertex_0 "gl_PerVertex"
803fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex_0 0 "gl_Position"
804fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex_0 1 "gl_PointSize"
805fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex_0 2 "gl_ClipDistance"
806fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex_0 3 "gl_CullDistance"
807fd4e5da5Sopenharmony_ci               OpName %gl_out "gl_out"
808fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
809fd4e5da5Sopenharmony_ci               OpDecorate %gl_PerVertex Block
810fd4e5da5Sopenharmony_ci               OpDecorate %gl_InvocationID BuiltIn InvocationId
811fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex_0 0 BuiltIn Position
812fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex_0 1 BuiltIn PointSize
813fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex_0 2 BuiltIn ClipDistance
814fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex_0 3 BuiltIn CullDistance
815fd4e5da5Sopenharmony_ci               OpDecorate %gl_PerVertex_0 Block
816fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
817fd4e5da5Sopenharmony_ci          %3 = OpTypeFunction %void
818fd4e5da5Sopenharmony_ci      %float = OpTypeFloat 32
819fd4e5da5Sopenharmony_ci    %v4float = OpTypeVector %float 4
820fd4e5da5Sopenharmony_ci%_ptr_Function_v4float = OpTypePointer Function %v4float
821fd4e5da5Sopenharmony_ci       %uint = OpTypeInt 32 0
822fd4e5da5Sopenharmony_ci     %uint_1 = OpConstant %uint 1
823fd4e5da5Sopenharmony_ci%_arr_float_uint_1 = OpTypeArray %float %uint_1
824fd4e5da5Sopenharmony_ci%gl_PerVertex = OpTypeStruct %v4float
825fd4e5da5Sopenharmony_ci    %uint_32 = OpConstant %uint 32
826fd4e5da5Sopenharmony_ci%_arr_gl_PerVertex_uint_32 = OpTypeArray %gl_PerVertex %uint_32
827fd4e5da5Sopenharmony_ci%_ptr_Input__arr_gl_PerVertex_uint_32 = OpTypePointer Input %_arr_gl_PerVertex_uint_32
828fd4e5da5Sopenharmony_ci      %gl_in = OpVariable %_ptr_Input__arr_gl_PerVertex_uint_32 Input
829fd4e5da5Sopenharmony_ci        %int = OpTypeInt 32 1
830fd4e5da5Sopenharmony_ci%_ptr_Input_int = OpTypePointer Input %int
831fd4e5da5Sopenharmony_ci%gl_InvocationID = OpVariable %_ptr_Input_int Input
832fd4e5da5Sopenharmony_ci      %int_0 = OpConstant %int 0
833fd4e5da5Sopenharmony_ci%_ptr_Input_v4float = OpTypePointer Input %v4float
834fd4e5da5Sopenharmony_ci%gl_PerVertex_0 = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
835fd4e5da5Sopenharmony_ci     %uint_4 = OpConstant %uint 4
836fd4e5da5Sopenharmony_ci%_arr_gl_PerVertex_0_uint_4 = OpTypeArray %gl_PerVertex_0 %uint_4
837fd4e5da5Sopenharmony_ci%_ptr_Output__arr_gl_PerVertex_0_uint_4 = OpTypePointer Output %_arr_gl_PerVertex_0_uint_4
838fd4e5da5Sopenharmony_ci     %gl_out = OpVariable %_ptr_Output__arr_gl_PerVertex_0_uint_4 Output
839fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
840fd4e5da5Sopenharmony_ci; CHECK: [[sty:%\w+]] = OpTypeStruct %v4float
841fd4e5da5Sopenharmony_ci; CHECK: [[asty:%\w+]] = OpTypeArray [[sty]] %uint_4
842fd4e5da5Sopenharmony_ci; CHECK: [[pasty:%\w+]] = OpTypePointer Output [[asty]]
843fd4e5da5Sopenharmony_ci; CHECK: %gl_out = OpVariable [[pasty]] Output
844fd4e5da5Sopenharmony_ci       %main = OpFunction %void None %3
845fd4e5da5Sopenharmony_ci          %5 = OpLabel
846fd4e5da5Sopenharmony_ci        %pos = OpVariable %_ptr_Function_v4float Function
847fd4e5da5Sopenharmony_ci         %21 = OpLoad %int %gl_InvocationID
848fd4e5da5Sopenharmony_ci         %24 = OpAccessChain %_ptr_Input_v4float %gl_in %21 %int_0
849fd4e5da5Sopenharmony_ci         %25 = OpLoad %v4float %24
850fd4e5da5Sopenharmony_ci               OpStore %pos %25
851fd4e5da5Sopenharmony_ci         %31 = OpLoad %int %gl_InvocationID
852fd4e5da5Sopenharmony_ci         %32 = OpLoad %v4float %pos
853fd4e5da5Sopenharmony_ci         %34 = OpAccessChain %_ptr_Output_v4float %gl_out %31 %int_0
854fd4e5da5Sopenharmony_ci               OpStore %34 %32
855fd4e5da5Sopenharmony_ci               OpReturn
856fd4e5da5Sopenharmony_ci               OpFunctionEnd
857fd4e5da5Sopenharmony_ci)";
858fd4e5da5Sopenharmony_ci
859fd4e5da5Sopenharmony_ci  SetTargetEnv(SPV_ENV_VULKAN_1_3);
860fd4e5da5Sopenharmony_ci  SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
861fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<EliminateDeadIOComponentsPass>(
862fd4e5da5Sopenharmony_ci      text, true, spv::StorageClass::Output, false);
863fd4e5da5Sopenharmony_ci}
864fd4e5da5Sopenharmony_ci
865fd4e5da5Sopenharmony_ciTEST_F(ElimDeadIOComponentsTest, TeseInput) {
866fd4e5da5Sopenharmony_ci  // Eliminate PointSize, ClipDistance, CullDistance from gl_in[]
867fd4e5da5Sopenharmony_ci  //
868fd4e5da5Sopenharmony_ci  // #version 450
869fd4e5da5Sopenharmony_ci  //
870fd4e5da5Sopenharmony_ci  // layout(triangles, ccw) in;
871fd4e5da5Sopenharmony_ci  // layout(fractional_odd_spacing) in;
872fd4e5da5Sopenharmony_ci  // layout(point_mode) in;
873fd4e5da5Sopenharmony_ci  //
874fd4e5da5Sopenharmony_ci  // void main()
875fd4e5da5Sopenharmony_ci  // {
876fd4e5da5Sopenharmony_ci  //     vec4 p = gl_in[1].gl_Position;
877fd4e5da5Sopenharmony_ci  //     gl_Position = p;
878fd4e5da5Sopenharmony_ci  // }
879fd4e5da5Sopenharmony_ci  const std::string text = R"(
880fd4e5da5Sopenharmony_ci               OpCapability Tessellation
881fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
882fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
883fd4e5da5Sopenharmony_ci               OpEntryPoint TessellationEvaluation %main "main" %gl_in %_
884fd4e5da5Sopenharmony_ci               OpExecutionMode %main Triangles
885fd4e5da5Sopenharmony_ci               OpExecutionMode %main SpacingFractionalOdd
886fd4e5da5Sopenharmony_ci               OpExecutionMode %main VertexOrderCcw
887fd4e5da5Sopenharmony_ci               OpExecutionMode %main PointMode
888fd4e5da5Sopenharmony_ci               OpSource GLSL 450
889fd4e5da5Sopenharmony_ci               OpName %main "main"
890fd4e5da5Sopenharmony_ci               OpName %p "p"
891fd4e5da5Sopenharmony_ci               OpName %gl_PerVertex "gl_PerVertex"
892fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 0 "gl_Position"
893fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 1 "gl_PointSize"
894fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 2 "gl_ClipDistance"
895fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 3 "gl_CullDistance"
896fd4e5da5Sopenharmony_ci               OpName %gl_in "gl_in"
897fd4e5da5Sopenharmony_ci               OpName %gl_PerVertex_0 "gl_PerVertex"
898fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex_0 0 "gl_Position"
899fd4e5da5Sopenharmony_ci               OpName %_ ""
900fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
901fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 1 BuiltIn PointSize
902fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 2 BuiltIn ClipDistance
903fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 3 BuiltIn CullDistance
904fd4e5da5Sopenharmony_ci               OpDecorate %gl_PerVertex Block
905fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex_0 0 BuiltIn Position
906fd4e5da5Sopenharmony_ci               OpDecorate %gl_PerVertex_0 Block
907fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
908fd4e5da5Sopenharmony_ci          %3 = OpTypeFunction %void
909fd4e5da5Sopenharmony_ci      %float = OpTypeFloat 32
910fd4e5da5Sopenharmony_ci    %v4float = OpTypeVector %float 4
911fd4e5da5Sopenharmony_ci%_ptr_Function_v4float = OpTypePointer Function %v4float
912fd4e5da5Sopenharmony_ci       %uint = OpTypeInt 32 0
913fd4e5da5Sopenharmony_ci     %uint_1 = OpConstant %uint 1
914fd4e5da5Sopenharmony_ci%_arr_float_uint_1 = OpTypeArray %float %uint_1
915fd4e5da5Sopenharmony_ci%gl_PerVertex = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
916fd4e5da5Sopenharmony_ci    %uint_32 = OpConstant %uint 32
917fd4e5da5Sopenharmony_ci%_arr_gl_PerVertex_uint_32 = OpTypeArray %gl_PerVertex %uint_32
918fd4e5da5Sopenharmony_ci%_ptr_Input__arr_gl_PerVertex_uint_32 = OpTypePointer Input %_arr_gl_PerVertex_uint_32
919fd4e5da5Sopenharmony_ci      %gl_in = OpVariable %_ptr_Input__arr_gl_PerVertex_uint_32 Input
920fd4e5da5Sopenharmony_ci        %int = OpTypeInt 32 1
921fd4e5da5Sopenharmony_ci      %int_1 = OpConstant %int 1
922fd4e5da5Sopenharmony_ci      %int_0 = OpConstant %int 0
923fd4e5da5Sopenharmony_ci%_ptr_Input_v4float = OpTypePointer Input %v4float
924fd4e5da5Sopenharmony_ci%gl_PerVertex_0 = OpTypeStruct %v4float
925fd4e5da5Sopenharmony_ci%_ptr_Output_gl_PerVertex_0 = OpTypePointer Output %gl_PerVertex_0
926fd4e5da5Sopenharmony_ci          %_ = OpVariable %_ptr_Output_gl_PerVertex_0 Output
927fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
928fd4e5da5Sopenharmony_ci; CHECK: %gl_PerVertex = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
929fd4e5da5Sopenharmony_ci; CHECK: [[sty:%\w+]] = OpTypeStruct %v4float
930fd4e5da5Sopenharmony_ci; CHECK: [[asty:%\w+]] = OpTypeArray [[sty]] %uint_32
931fd4e5da5Sopenharmony_ci; CHECK: [[pasty:%\w+]] = OpTypePointer Input [[asty]]
932fd4e5da5Sopenharmony_ci; CHECK: %gl_in = OpVariable [[pasty]] Input
933fd4e5da5Sopenharmony_ci       %main = OpFunction %void None %3
934fd4e5da5Sopenharmony_ci          %5 = OpLabel
935fd4e5da5Sopenharmony_ci          %p = OpVariable %_ptr_Function_v4float Function
936fd4e5da5Sopenharmony_ci         %22 = OpAccessChain %_ptr_Input_v4float %gl_in %int_1 %int_0
937fd4e5da5Sopenharmony_ci         %23 = OpLoad %v4float %22
938fd4e5da5Sopenharmony_ci               OpStore %p %23
939fd4e5da5Sopenharmony_ci         %27 = OpLoad %v4float %p
940fd4e5da5Sopenharmony_ci         %29 = OpAccessChain %_ptr_Output_v4float %_ %int_0
941fd4e5da5Sopenharmony_ci               OpStore %29 %27
942fd4e5da5Sopenharmony_ci               OpReturn
943fd4e5da5Sopenharmony_ci               OpFunctionEnd
944fd4e5da5Sopenharmony_ci)";
945fd4e5da5Sopenharmony_ci
946fd4e5da5Sopenharmony_ci  SetTargetEnv(SPV_ENV_VULKAN_1_3);
947fd4e5da5Sopenharmony_ci  SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
948fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<EliminateDeadIOComponentsPass>(
949fd4e5da5Sopenharmony_ci      text, true, spv::StorageClass::Input, false);
950fd4e5da5Sopenharmony_ci}
951fd4e5da5Sopenharmony_ci
952fd4e5da5Sopenharmony_ciTEST_F(ElimDeadIOComponentsTest, TeseOutput) {
953fd4e5da5Sopenharmony_ci  // Eliminate PointSize, ClipDistance, CullDistance from gl_out
954fd4e5da5Sopenharmony_ci  //
955fd4e5da5Sopenharmony_ci  // #version 450
956fd4e5da5Sopenharmony_ci  //
957fd4e5da5Sopenharmony_ci  // layout(triangles, ccw) in;
958fd4e5da5Sopenharmony_ci  // layout(fractional_odd_spacing) in;
959fd4e5da5Sopenharmony_ci  // layout(point_mode) in;
960fd4e5da5Sopenharmony_ci  //
961fd4e5da5Sopenharmony_ci  // void main()
962fd4e5da5Sopenharmony_ci  // {
963fd4e5da5Sopenharmony_ci  //     vec4 p = gl_in[1].gl_Position;
964fd4e5da5Sopenharmony_ci  //     gl_Position = p;
965fd4e5da5Sopenharmony_ci  // }
966fd4e5da5Sopenharmony_ci  const std::string text = R"(
967fd4e5da5Sopenharmony_ci               OpCapability Tessellation
968fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
969fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
970fd4e5da5Sopenharmony_ci               OpEntryPoint TessellationEvaluation %main "main" %gl_in %_
971fd4e5da5Sopenharmony_ci               OpExecutionMode %main Triangles
972fd4e5da5Sopenharmony_ci               OpExecutionMode %main SpacingFractionalOdd
973fd4e5da5Sopenharmony_ci               OpExecutionMode %main VertexOrderCcw
974fd4e5da5Sopenharmony_ci               OpExecutionMode %main PointMode
975fd4e5da5Sopenharmony_ci               OpSource GLSL 450
976fd4e5da5Sopenharmony_ci               OpName %main "main"
977fd4e5da5Sopenharmony_ci               OpName %p "p"
978fd4e5da5Sopenharmony_ci               OpName %gl_PerVertex "gl_PerVertex"
979fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 0 "gl_Position"
980fd4e5da5Sopenharmony_ci               OpName %gl_in "gl_in"
981fd4e5da5Sopenharmony_ci               OpName %gl_PerVertex_0 "gl_PerVertex"
982fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex_0 0 "gl_Position"
983fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex_0 1 "gl_PointSize"
984fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex_0 2 "gl_ClipDistance"
985fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex_0 3 "gl_CullDistance"
986fd4e5da5Sopenharmony_ci               OpName %_ ""
987fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
988fd4e5da5Sopenharmony_ci               OpDecorate %gl_PerVertex Block
989fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex_0 0 BuiltIn Position
990fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex_0 1 BuiltIn PointSize
991fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex_0 2 BuiltIn ClipDistance
992fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex_0 3 BuiltIn CullDistance
993fd4e5da5Sopenharmony_ci               OpDecorate %gl_PerVertex_0 Block
994fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
995fd4e5da5Sopenharmony_ci          %3 = OpTypeFunction %void
996fd4e5da5Sopenharmony_ci      %float = OpTypeFloat 32
997fd4e5da5Sopenharmony_ci    %v4float = OpTypeVector %float 4
998fd4e5da5Sopenharmony_ci%_ptr_Function_v4float = OpTypePointer Function %v4float
999fd4e5da5Sopenharmony_ci       %uint = OpTypeInt 32 0
1000fd4e5da5Sopenharmony_ci     %uint_1 = OpConstant %uint 1
1001fd4e5da5Sopenharmony_ci%_arr_float_uint_1 = OpTypeArray %float %uint_1
1002fd4e5da5Sopenharmony_ci%gl_PerVertex = OpTypeStruct %v4float
1003fd4e5da5Sopenharmony_ci    %uint_32 = OpConstant %uint 32
1004fd4e5da5Sopenharmony_ci%_arr_gl_PerVertex_uint_32 = OpTypeArray %gl_PerVertex %uint_32
1005fd4e5da5Sopenharmony_ci%_ptr_Input__arr_gl_PerVertex_uint_32 = OpTypePointer Input %_arr_gl_PerVertex_uint_32
1006fd4e5da5Sopenharmony_ci      %gl_in = OpVariable %_ptr_Input__arr_gl_PerVertex_uint_32 Input
1007fd4e5da5Sopenharmony_ci        %int = OpTypeInt 32 1
1008fd4e5da5Sopenharmony_ci      %int_1 = OpConstant %int 1
1009fd4e5da5Sopenharmony_ci      %int_0 = OpConstant %int 0
1010fd4e5da5Sopenharmony_ci%_ptr_Input_v4float = OpTypePointer Input %v4float
1011fd4e5da5Sopenharmony_ci%gl_PerVertex_0 = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
1012fd4e5da5Sopenharmony_ci%_ptr_Output_gl_PerVertex_0 = OpTypePointer Output %gl_PerVertex_0
1013fd4e5da5Sopenharmony_ci          %_ = OpVariable %_ptr_Output_gl_PerVertex_0 Output
1014fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
1015fd4e5da5Sopenharmony_ci; CHECK: %_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
1016fd4e5da5Sopenharmony_ci; CHECK: %_ = OpVariable %_ptr_Output_gl_PerVertex Output
1017fd4e5da5Sopenharmony_ci       %main = OpFunction %void None %3
1018fd4e5da5Sopenharmony_ci          %5 = OpLabel
1019fd4e5da5Sopenharmony_ci          %p = OpVariable %_ptr_Function_v4float Function
1020fd4e5da5Sopenharmony_ci         %22 = OpAccessChain %_ptr_Input_v4float %gl_in %int_1 %int_0
1021fd4e5da5Sopenharmony_ci         %23 = OpLoad %v4float %22
1022fd4e5da5Sopenharmony_ci               OpStore %p %23
1023fd4e5da5Sopenharmony_ci         %27 = OpLoad %v4float %p
1024fd4e5da5Sopenharmony_ci         %29 = OpAccessChain %_ptr_Output_v4float %_ %int_0
1025fd4e5da5Sopenharmony_ci               OpStore %29 %27
1026fd4e5da5Sopenharmony_ci               OpReturn
1027fd4e5da5Sopenharmony_ci               OpFunctionEnd
1028fd4e5da5Sopenharmony_ci)";
1029fd4e5da5Sopenharmony_ci
1030fd4e5da5Sopenharmony_ci  SetTargetEnv(SPV_ENV_VULKAN_1_3);
1031fd4e5da5Sopenharmony_ci  SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1032fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<EliminateDeadIOComponentsPass>(
1033fd4e5da5Sopenharmony_ci      text, true, spv::StorageClass::Output, false);
1034fd4e5da5Sopenharmony_ci}
1035fd4e5da5Sopenharmony_ci
1036fd4e5da5Sopenharmony_ciTEST_F(ElimDeadIOComponentsTest, GeomInput) {
1037fd4e5da5Sopenharmony_ci  // Eliminate PointSize, ClipDistance, CullDistance from gl_in[]
1038fd4e5da5Sopenharmony_ci  //
1039fd4e5da5Sopenharmony_ci  // #version 450
1040fd4e5da5Sopenharmony_ci  //
1041fd4e5da5Sopenharmony_ci  // layout(triangle_strip, max_vertices = 3) out;
1042fd4e5da5Sopenharmony_ci  // layout(triangles) in;
1043fd4e5da5Sopenharmony_ci  //
1044fd4e5da5Sopenharmony_ci  // void main()
1045fd4e5da5Sopenharmony_ci  // {
1046fd4e5da5Sopenharmony_ci  //         for (int i = 0; i < 3; i++)
1047fd4e5da5Sopenharmony_ci  //         {
1048fd4e5da5Sopenharmony_ci  //                 gl_Position = gl_in[i].gl_Position;
1049fd4e5da5Sopenharmony_ci  //                 EmitVertex();
1050fd4e5da5Sopenharmony_ci  //         }
1051fd4e5da5Sopenharmony_ci  //         EndPrimitive();
1052fd4e5da5Sopenharmony_ci  // }
1053fd4e5da5Sopenharmony_ci  const std::string text = R"(
1054fd4e5da5Sopenharmony_ci               OpCapability Geometry
1055fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
1056fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
1057fd4e5da5Sopenharmony_ci               OpEntryPoint Geometry %main "main" %_ %gl_in
1058fd4e5da5Sopenharmony_ci               OpExecutionMode %main Triangles
1059fd4e5da5Sopenharmony_ci               OpExecutionMode %main Invocations 1
1060fd4e5da5Sopenharmony_ci               OpExecutionMode %main OutputTriangleStrip
1061fd4e5da5Sopenharmony_ci               OpExecutionMode %main OutputVertices 3
1062fd4e5da5Sopenharmony_ci               OpSource GLSL 460
1063fd4e5da5Sopenharmony_ci               OpName %main "main"
1064fd4e5da5Sopenharmony_ci               OpName %i "i"
1065fd4e5da5Sopenharmony_ci               OpName %gl_PerVertex "gl_PerVertex"
1066fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 0 "gl_Position"
1067fd4e5da5Sopenharmony_ci               OpName %_ ""
1068fd4e5da5Sopenharmony_ci               OpName %gl_PerVertex_0 "gl_PerVertex"
1069fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex_0 0 "gl_Position"
1070fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex_0 1 "gl_PointSize"
1071fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex_0 2 "gl_ClipDistance"
1072fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex_0 3 "gl_CullDistance"
1073fd4e5da5Sopenharmony_ci               OpName %gl_in "gl_in"
1074fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
1075fd4e5da5Sopenharmony_ci               OpDecorate %gl_PerVertex Block
1076fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex_0 0 BuiltIn Position
1077fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex_0 1 BuiltIn PointSize
1078fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex_0 2 BuiltIn ClipDistance
1079fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex_0 3 BuiltIn CullDistance
1080fd4e5da5Sopenharmony_ci               OpDecorate %gl_PerVertex_0 Block
1081fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
1082fd4e5da5Sopenharmony_ci          %3 = OpTypeFunction %void
1083fd4e5da5Sopenharmony_ci        %int = OpTypeInt 32 1
1084fd4e5da5Sopenharmony_ci%_ptr_Function_int = OpTypePointer Function %int
1085fd4e5da5Sopenharmony_ci      %int_0 = OpConstant %int 0
1086fd4e5da5Sopenharmony_ci      %int_3 = OpConstant %int 3
1087fd4e5da5Sopenharmony_ci       %bool = OpTypeBool
1088fd4e5da5Sopenharmony_ci      %float = OpTypeFloat 32
1089fd4e5da5Sopenharmony_ci    %v4float = OpTypeVector %float 4
1090fd4e5da5Sopenharmony_ci       %uint = OpTypeInt 32 0
1091fd4e5da5Sopenharmony_ci     %uint_1 = OpConstant %uint 1
1092fd4e5da5Sopenharmony_ci%_arr_float_uint_1 = OpTypeArray %float %uint_1
1093fd4e5da5Sopenharmony_ci%gl_PerVertex = OpTypeStruct %v4float
1094fd4e5da5Sopenharmony_ci%_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
1095fd4e5da5Sopenharmony_ci          %_ = OpVariable %_ptr_Output_gl_PerVertex Output
1096fd4e5da5Sopenharmony_ci%gl_PerVertex_0 = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
1097fd4e5da5Sopenharmony_ci     %uint_3 = OpConstant %uint 3
1098fd4e5da5Sopenharmony_ci%_arr_gl_PerVertex_0_uint_3 = OpTypeArray %gl_PerVertex_0 %uint_3
1099fd4e5da5Sopenharmony_ci%_ptr_Input__arr_gl_PerVertex_0_uint_3 = OpTypePointer Input %_arr_gl_PerVertex_0_uint_3
1100fd4e5da5Sopenharmony_ci      %gl_in = OpVariable %_ptr_Input__arr_gl_PerVertex_0_uint_3 Input
1101fd4e5da5Sopenharmony_ci%_ptr_Input_v4float = OpTypePointer Input %v4float
1102fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
1103fd4e5da5Sopenharmony_ci      %int_1 = OpConstant %int 1
1104fd4e5da5Sopenharmony_ci; CHECK: [[asty:%\w+]] = OpTypeArray %gl_PerVertex %uint_3
1105fd4e5da5Sopenharmony_ci; CHECK: [[pasty:%\w+]] = OpTypePointer Input [[asty]]
1106fd4e5da5Sopenharmony_ci; CHECK: %gl_in = OpVariable [[pasty]] Input
1107fd4e5da5Sopenharmony_ci       %main = OpFunction %void None %3
1108fd4e5da5Sopenharmony_ci          %5 = OpLabel
1109fd4e5da5Sopenharmony_ci          %i = OpVariable %_ptr_Function_int Function
1110fd4e5da5Sopenharmony_ci               OpStore %i %int_0
1111fd4e5da5Sopenharmony_ci               OpBranch %10
1112fd4e5da5Sopenharmony_ci         %10 = OpLabel
1113fd4e5da5Sopenharmony_ci               OpLoopMerge %12 %13 None
1114fd4e5da5Sopenharmony_ci               OpBranch %14
1115fd4e5da5Sopenharmony_ci         %14 = OpLabel
1116fd4e5da5Sopenharmony_ci         %15 = OpLoad %int %i
1117fd4e5da5Sopenharmony_ci         %18 = OpSLessThan %bool %15 %int_3
1118fd4e5da5Sopenharmony_ci               OpBranchConditional %18 %11 %12
1119fd4e5da5Sopenharmony_ci         %11 = OpLabel
1120fd4e5da5Sopenharmony_ci         %32 = OpLoad %int %i
1121fd4e5da5Sopenharmony_ci         %34 = OpAccessChain %_ptr_Input_v4float %gl_in %32 %int_0
1122fd4e5da5Sopenharmony_ci         %35 = OpLoad %v4float %34
1123fd4e5da5Sopenharmony_ci         %37 = OpAccessChain %_ptr_Output_v4float %_ %int_0
1124fd4e5da5Sopenharmony_ci               OpStore %37 %35
1125fd4e5da5Sopenharmony_ci               OpEmitVertex
1126fd4e5da5Sopenharmony_ci               OpBranch %13
1127fd4e5da5Sopenharmony_ci         %13 = OpLabel
1128fd4e5da5Sopenharmony_ci         %38 = OpLoad %int %i
1129fd4e5da5Sopenharmony_ci         %40 = OpIAdd %int %38 %int_1
1130fd4e5da5Sopenharmony_ci               OpStore %i %40
1131fd4e5da5Sopenharmony_ci               OpBranch %10
1132fd4e5da5Sopenharmony_ci         %12 = OpLabel
1133fd4e5da5Sopenharmony_ci               OpEndPrimitive
1134fd4e5da5Sopenharmony_ci               OpReturn
1135fd4e5da5Sopenharmony_ci               OpFunctionEnd
1136fd4e5da5Sopenharmony_ci)";
1137fd4e5da5Sopenharmony_ci
1138fd4e5da5Sopenharmony_ci  SetTargetEnv(SPV_ENV_VULKAN_1_3);
1139fd4e5da5Sopenharmony_ci  SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1140fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<EliminateDeadIOComponentsPass>(
1141fd4e5da5Sopenharmony_ci      text, true, spv::StorageClass::Input, false);
1142fd4e5da5Sopenharmony_ci}
1143fd4e5da5Sopenharmony_ci
1144fd4e5da5Sopenharmony_ciTEST_F(ElimDeadIOComponentsTest, GeomOutput) {
1145fd4e5da5Sopenharmony_ci  // Eliminate PointSize, ClipDistance, CullDistance from gl_out
1146fd4e5da5Sopenharmony_ci  //
1147fd4e5da5Sopenharmony_ci  // #version 450
1148fd4e5da5Sopenharmony_ci  //
1149fd4e5da5Sopenharmony_ci  // layout(triangle_strip, max_vertices = 3) out;
1150fd4e5da5Sopenharmony_ci  // layout(triangles) in;
1151fd4e5da5Sopenharmony_ci  //
1152fd4e5da5Sopenharmony_ci  // void main()
1153fd4e5da5Sopenharmony_ci  // {
1154fd4e5da5Sopenharmony_ci  //         for (int i = 0; i < 3; i++)
1155fd4e5da5Sopenharmony_ci  //         {
1156fd4e5da5Sopenharmony_ci  //                 gl_Position = gl_in[i].gl_Position;
1157fd4e5da5Sopenharmony_ci  //                 EmitVertex();
1158fd4e5da5Sopenharmony_ci  //         }
1159fd4e5da5Sopenharmony_ci  //         EndPrimitive();
1160fd4e5da5Sopenharmony_ci  // }
1161fd4e5da5Sopenharmony_ci  const std::string text = R"(
1162fd4e5da5Sopenharmony_ci               OpCapability Geometry
1163fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
1164fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
1165fd4e5da5Sopenharmony_ci               OpEntryPoint Geometry %main "main" %_ %gl_in
1166fd4e5da5Sopenharmony_ci               OpExecutionMode %main Triangles
1167fd4e5da5Sopenharmony_ci               OpExecutionMode %main Invocations 1
1168fd4e5da5Sopenharmony_ci               OpExecutionMode %main OutputTriangleStrip
1169fd4e5da5Sopenharmony_ci               OpExecutionMode %main OutputVertices 3
1170fd4e5da5Sopenharmony_ci               OpSource GLSL 460
1171fd4e5da5Sopenharmony_ci               OpName %main "main"
1172fd4e5da5Sopenharmony_ci               OpName %i "i"
1173fd4e5da5Sopenharmony_ci               OpName %gl_PerVertex "gl_PerVertex"
1174fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 0 "gl_Position"
1175fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 1 "gl_PointSize"
1176fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 2 "gl_ClipDistance"
1177fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex 3 "gl_CullDistance"
1178fd4e5da5Sopenharmony_ci               OpName %_ ""
1179fd4e5da5Sopenharmony_ci               OpName %gl_PerVertex_0 "gl_PerVertex"
1180fd4e5da5Sopenharmony_ci               OpMemberName %gl_PerVertex_0 0 "gl_Position"
1181fd4e5da5Sopenharmony_ci               OpName %gl_in "gl_in"
1182fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
1183fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 1 BuiltIn PointSize
1184fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 2 BuiltIn ClipDistance
1185fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex 3 BuiltIn CullDistance
1186fd4e5da5Sopenharmony_ci               OpDecorate %gl_PerVertex Block
1187fd4e5da5Sopenharmony_ci               OpMemberDecorate %gl_PerVertex_0 0 BuiltIn Position
1188fd4e5da5Sopenharmony_ci               OpDecorate %gl_PerVertex_0 Block
1189fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
1190fd4e5da5Sopenharmony_ci          %3 = OpTypeFunction %void
1191fd4e5da5Sopenharmony_ci        %int = OpTypeInt 32 1
1192fd4e5da5Sopenharmony_ci%_ptr_Function_int = OpTypePointer Function %int
1193fd4e5da5Sopenharmony_ci      %int_0 = OpConstant %int 0
1194fd4e5da5Sopenharmony_ci      %int_3 = OpConstant %int 3
1195fd4e5da5Sopenharmony_ci       %bool = OpTypeBool
1196fd4e5da5Sopenharmony_ci      %float = OpTypeFloat 32
1197fd4e5da5Sopenharmony_ci    %v4float = OpTypeVector %float 4
1198fd4e5da5Sopenharmony_ci       %uint = OpTypeInt 32 0
1199fd4e5da5Sopenharmony_ci     %uint_1 = OpConstant %uint 1
1200fd4e5da5Sopenharmony_ci%_arr_float_uint_1 = OpTypeArray %float %uint_1
1201fd4e5da5Sopenharmony_ci%gl_PerVertex = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
1202fd4e5da5Sopenharmony_ci%_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
1203fd4e5da5Sopenharmony_ci          %_ = OpVariable %_ptr_Output_gl_PerVertex Output
1204fd4e5da5Sopenharmony_ci%gl_PerVertex_0 = OpTypeStruct %v4float
1205fd4e5da5Sopenharmony_ci     %uint_3 = OpConstant %uint 3
1206fd4e5da5Sopenharmony_ci%_arr_gl_PerVertex_0_uint_3 = OpTypeArray %gl_PerVertex_0 %uint_3
1207fd4e5da5Sopenharmony_ci%_ptr_Input__arr_gl_PerVertex_0_uint_3 = OpTypePointer Input %_arr_gl_PerVertex_0_uint_3
1208fd4e5da5Sopenharmony_ci      %gl_in = OpVariable %_ptr_Input__arr_gl_PerVertex_0_uint_3 Input
1209fd4e5da5Sopenharmony_ci%_ptr_Input_v4float = OpTypePointer Input %v4float
1210fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
1211fd4e5da5Sopenharmony_ci      %int_1 = OpConstant %int 1
1212fd4e5da5Sopenharmony_ci; CHECK: %_ptr_Output_gl_PerVertex_0 = OpTypePointer Output %gl_PerVertex_0
1213fd4e5da5Sopenharmony_ci; CHECK: %_ = OpVariable %_ptr_Output_gl_PerVertex_0 Output
1214fd4e5da5Sopenharmony_ci       %main = OpFunction %void None %3
1215fd4e5da5Sopenharmony_ci          %5 = OpLabel
1216fd4e5da5Sopenharmony_ci          %i = OpVariable %_ptr_Function_int Function
1217fd4e5da5Sopenharmony_ci               OpStore %i %int_0
1218fd4e5da5Sopenharmony_ci               OpBranch %10
1219fd4e5da5Sopenharmony_ci         %10 = OpLabel
1220fd4e5da5Sopenharmony_ci               OpLoopMerge %12 %13 None
1221fd4e5da5Sopenharmony_ci               OpBranch %14
1222fd4e5da5Sopenharmony_ci         %14 = OpLabel
1223fd4e5da5Sopenharmony_ci         %15 = OpLoad %int %i
1224fd4e5da5Sopenharmony_ci         %18 = OpSLessThan %bool %15 %int_3
1225fd4e5da5Sopenharmony_ci               OpBranchConditional %18 %11 %12
1226fd4e5da5Sopenharmony_ci         %11 = OpLabel
1227fd4e5da5Sopenharmony_ci         %32 = OpLoad %int %i
1228fd4e5da5Sopenharmony_ci         %34 = OpAccessChain %_ptr_Input_v4float %gl_in %32 %int_0
1229fd4e5da5Sopenharmony_ci         %35 = OpLoad %v4float %34
1230fd4e5da5Sopenharmony_ci         %37 = OpAccessChain %_ptr_Output_v4float %_ %int_0
1231fd4e5da5Sopenharmony_ci               OpStore %37 %35
1232fd4e5da5Sopenharmony_ci               OpEmitVertex
1233fd4e5da5Sopenharmony_ci               OpBranch %13
1234fd4e5da5Sopenharmony_ci         %13 = OpLabel
1235fd4e5da5Sopenharmony_ci         %38 = OpLoad %int %i
1236fd4e5da5Sopenharmony_ci         %40 = OpIAdd %int %38 %int_1
1237fd4e5da5Sopenharmony_ci               OpStore %i %40
1238fd4e5da5Sopenharmony_ci               OpBranch %10
1239fd4e5da5Sopenharmony_ci         %12 = OpLabel
1240fd4e5da5Sopenharmony_ci               OpEndPrimitive
1241fd4e5da5Sopenharmony_ci               OpReturn
1242fd4e5da5Sopenharmony_ci               OpFunctionEnd
1243fd4e5da5Sopenharmony_ci)";
1244fd4e5da5Sopenharmony_ci
1245fd4e5da5Sopenharmony_ci  SetTargetEnv(SPV_ENV_VULKAN_1_3);
1246fd4e5da5Sopenharmony_ci  SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1247fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<EliminateDeadIOComponentsPass>(
1248fd4e5da5Sopenharmony_ci      text, true, spv::StorageClass::Output, false);
1249fd4e5da5Sopenharmony_ci}
1250fd4e5da5Sopenharmony_ci
1251fd4e5da5Sopenharmony_ci}  // namespace
1252fd4e5da5Sopenharmony_ci}  // namespace opt
1253fd4e5da5Sopenharmony_ci}  // namespace spvtools
1254