Debian Codegraph @

test/fuzz/transformation_replace_opselect_with_conditional_branch_test.cpp

raw
1 // Copyright (c) 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "source/fuzz/transformation_replace_opselect_with_conditional_branch.h"
16
17 #include "gtest/gtest.h"
18 #include "source/fuzz/fuzzer_util.h"
19 #include "test/fuzz/fuzz_test_util.h"
20
21 namespace spvtools {
22 namespace fuzz {
23 namespace {
24
25 TEST(TransformationReplaceOpSelectWithConditionalBranchTest, Inapplicable) {
26 std::string shader = R"(
27 OpCapability Shader
28 %1 = OpExtInstImport "GLSL.std.450"
29 OpMemoryModel Logical GLSL450
30 OpEntryPoint Fragment %2 "main"
31 OpExecutionMode %2 OriginUpperLeft
32 OpSource ESSL 310
33 %3 = OpTypeVoid
34 %4 = OpTypeFunction %3
35 %5 = OpTypeInt 32 1
36 %6 = OpConstant %5 1
37 %7 = OpConstant %5 2
38 %8 = OpTypeVector %5 4
39 %9 = OpConstantNull %8
40 %10 = OpConstantComposite %8 %6 %6 %7 %7
41 %11 = OpTypeBool
42 %12 = OpTypeVector %11 4
43 %13 = OpConstantTrue %11
44 %14 = OpConstantFalse %11
45 %15 = OpConstantComposite %12 %13 %14 %14 %13
46 %2 = OpFunction %3 None %4
47 %16 = OpLabel
48 %17 = OpCopyObject %5 %6
49 %18 = OpCopyObject %5 %7
50 OpBranch %19
51 %19 = OpLabel
52 %20 = OpCopyObject %5 %17
53 %21 = OpSelect %5 %13 %17 %18
54 OpBranch %22
55 %22 = OpLabel
56 %23 = OpSelect %8 %15 %9 %10
57 OpBranch %24
58 %24 = OpLabel
59 OpSelectionMerge %25 None
60 OpBranchConditional %13 %26 %27
61 %26 = OpLabel
62 %28 = OpSelect %5 %13 %17 %18
63 OpBranch %27
64 %27 = OpLabel
65 %29 = OpSelect %5 %13 %17 %18
66 OpBranch %25
67 %25 = OpLabel
68 %30 = OpSelect %5 %13 %17 %18
69 OpBranch %31
70 %31 = OpLabel
71 OpLoopMerge %32 %33 None
72 OpBranch %33
73 %33 = OpLabel
74 %34 = OpSelect %5 %13 %17 %18
75 OpBranchConditional %13 %31 %32
76 %32 = OpLabel
77 %35 = OpSelect %5 %13 %17 %18
78 OpBranch %36
79 %36 = OpLabel
80 %37 = OpSelect %5 %13 %17 %18
81 OpReturn
82 OpFunctionEnd
83 )";
84 const auto env = SPV_ENV_UNIVERSAL_1_5;
85 const auto consumer = nullptr;
86 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
87 spvtools::ValidatorOptions validator_options;
88 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
89 kConsoleMessageConsumer));
90 TransformationContext transformation_context(
91 MakeUnique<FactManager>(context.get()), validator_options);
92 // %20 is not an OpSelect instruction.
93 ASSERT_FALSE(TransformationReplaceOpSelectWithConditionalBranch(20, 100, 101)
94 .IsApplicable(context.get(), transformation_context));
95
96 // %21 is not the first instruction in its block.
97 ASSERT_FALSE(TransformationReplaceOpSelectWithConditionalBranch(21, 100, 101)
98 .IsApplicable(context.get(), transformation_context));
99
100 // The condition for %23 is not a scalar, but a vector of booleans.
101 ASSERT_FALSE(TransformationReplaceOpSelectWithConditionalBranch(23, 100, 101)
102 .IsApplicable(context.get(), transformation_context));
103
104 // The predecessor (%24) of the block containing %28 is the header of a
105 // selection construct and does not branch unconditionally.
106 ASSERT_FALSE(TransformationReplaceOpSelectWithConditionalBranch(24, 100, 101)
107 .IsApplicable(context.get(), transformation_context));
108
109 // The block containing %29 has two predecessors (%24 and %26).
110 ASSERT_FALSE(TransformationReplaceOpSelectWithConditionalBranch(29, 100, 101)
111 .IsApplicable(context.get(), transformation_context));
112
113 // The block containing %30 is the merge block for a selection construct.
114 ASSERT_FALSE(TransformationReplaceOpSelectWithConditionalBranch(30, 100, 101)
115 .IsApplicable(context.get(), transformation_context));
116
117 // The predecessor (%31) of the block containing %34 is a loop header.
118 ASSERT_FALSE(TransformationReplaceOpSelectWithConditionalBranch(31, 100, 101)
119 .IsApplicable(context.get(), transformation_context));
120
121 // The block containing %35 is the merge block for a loop construct.
122 ASSERT_FALSE(TransformationReplaceOpSelectWithConditionalBranch(35, 100, 101)
123 .IsApplicable(context.get(), transformation_context));
124
125 #ifndef NDEBUG
126 // |true_block_id| and |false_block_id| are both 0.
127 ASSERT_DEATH(
128 TransformationReplaceOpSelectWithConditionalBranch(37, 0, 0).IsApplicable(
129 context.get(), transformation_context),
130 "At least one of the ids must be non-zero.");
131 #endif
132
133 // The fresh ids are not distinct.
134 ASSERT_FALSE(TransformationReplaceOpSelectWithConditionalBranch(37, 100, 100)
135 .IsApplicable(context.get(), transformation_context));
136
137 // One of the ids is not fresh.
138 ASSERT_FALSE(TransformationReplaceOpSelectWithConditionalBranch(37, 100, 10)
139 .IsApplicable(context.get(), transformation_context));
140 }
141
142 TEST(TransformationReplaceOpSelectWithConditionalBranchTest, Simple) {
143 std::string shader = R"(
144 OpCapability Shader
145 %1 = OpExtInstImport "GLSL.std.450"
146 OpMemoryModel Logical GLSL450
147 OpEntryPoint Fragment %2 "main"
148 OpExecutionMode %2 OriginUpperLeft
149 OpSource ESSL 310
150 OpName %2 "main"
151 %3 = OpTypeVoid
152 %4 = OpTypeFunction %3
153 %5 = OpTypeInt 32 1
154 %6 = OpConstant %5 1
155 %7 = OpConstant %5 2
156 %8 = OpTypeVector %5 4
157 %9 = OpConstantNull %8
158 %10 = OpConstantComposite %8 %6 %6 %7 %7
159 %11 = OpTypeBool
160 %12 = OpTypeVector %11 4
161 %13 = OpConstantTrue %11
162 %14 = OpConstantFalse %11
163 %15 = OpConstantComposite %12 %13 %14 %14 %13
164 %2 = OpFunction %3 None %4
165 %16 = OpLabel
166 %17 = OpCopyObject %5 %6
167 %18 = OpCopyObject %5 %7
168 OpBranch %19
169 %19 = OpLabel
170 %20 = OpSelect %5 %13 %17 %18
171 OpSelectionMerge %21 None
172 OpBranchConditional %13 %22 %21
173 %22 = OpLabel
174 OpBranch %23
175 %23 = OpLabel
176 %24 = OpSelect %8 %13 %9 %10
177 OpBranch %21
178 %21 = OpLabel
179 OpBranch %25
180 %25 = OpLabel
181 %26 = OpSelect %5 %13 %17 %18
182 OpReturn
183 OpFunctionEnd
184 )";
185
186 const auto env = SPV_ENV_UNIVERSAL_1_5;
187 const auto consumer = nullptr;
188 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
189 spvtools::ValidatorOptions validator_options;
190 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
191 kConsoleMessageConsumer));
192 TransformationContext transformation_context(
193 MakeUnique<FactManager>(context.get()), validator_options);
194 auto transformation =
195 TransformationReplaceOpSelectWithConditionalBranch(20, 100, 101);
196 ASSERT_TRUE(
197 transformation.IsApplicable(context.get(), transformation_context));
198 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
199
200 auto transformation2 =
201 TransformationReplaceOpSelectWithConditionalBranch(24, 0, 102);
202 ASSERT_TRUE(
203 transformation2.IsApplicable(context.get(), transformation_context));
204 ApplyAndCheckFreshIds(transformation2, context.get(),
205 &transformation_context);
206
207 auto transformation3 =
208 TransformationReplaceOpSelectWithConditionalBranch(26, 103, 0);
209 ASSERT_TRUE(
210 transformation3.IsApplicable(context.get(), transformation_context));
211 ApplyAndCheckFreshIds(transformation3, context.get(),
212 &transformation_context);
213
214 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
215 kConsoleMessageConsumer));
216
217 std::string after_transformation = R"(
218 OpCapability Shader
219 %1 = OpExtInstImport "GLSL.std.450"
220 OpMemoryModel Logical GLSL450
221 OpEntryPoint Fragment %2 "main"
222 OpExecutionMode %2 OriginUpperLeft
223 OpSource ESSL 310
224 OpName %2 "main"
225 %3 = OpTypeVoid
226 %4 = OpTypeFunction %3
227 %5 = OpTypeInt 32 1
228 %6 = OpConstant %5 1
229 %7 = OpConstant %5 2
230 %8 = OpTypeVector %5 4
231 %9 = OpConstantNull %8
232 %10 = OpConstantComposite %8 %6 %6 %7 %7
233 %11 = OpTypeBool
234 %12 = OpTypeVector %11 4
235 %13 = OpConstantTrue %11
236 %14 = OpConstantFalse %11
237 %15 = OpConstantComposite %12 %13 %14 %14 %13
238 %2 = OpFunction %3 None %4
239 %16 = OpLabel
240 %17 = OpCopyObject %5 %6
241 %18 = OpCopyObject %5 %7
242 OpSelectionMerge %19 None
243 OpBranchConditional %13 %100 %101
244 %100 = OpLabel
245 OpBranch %19
246 %101 = OpLabel
247 OpBranch %19
248 %19 = OpLabel
249 %20 = OpPhi %5 %17 %100 %18 %101
250 OpSelectionMerge %21 None
251 OpBranchConditional %13 %22 %21
252 %22 = OpLabel
253 OpSelectionMerge %23 None
254 OpBranchConditional %13 %23 %102
255 %102 = OpLabel
256 OpBranch %23
257 %23 = OpLabel
258 %24 = OpPhi %8 %9 %22 %10 %102
259 OpBranch %21
260 %21 = OpLabel
261 OpSelectionMerge %25 None
262 OpBranchConditional %13 %103 %25
263 %103 = OpLabel
264 OpBranch %25
265 %25 = OpLabel
266 %26 = OpPhi %5 %17 %103 %18 %21
267 OpReturn
268 OpFunctionEnd
269 )";
270
271 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
272 }
273
274 } // namespace
275 } // namespace fuzz
276 } // namespace spvtools