| 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_flatten_conditional_branch.h" |
| 16 |
|
| 17 |
#include "gtest/gtest.h" |
| 18 |
#include "source/fuzz/counter_overflow_id_source.h" |
| 19 |
#include "source/fuzz/fuzzer_util.h" |
| 20 |
#include "source/fuzz/instruction_descriptor.h" |
| 21 |
#include "test/fuzz/fuzz_test_util.h" |
| 22 |
|
| 23 |
namespace spvtools { |
| 24 |
namespace fuzz { |
| 25 |
namespace { |
| 26 |
|
| 27 |
protobufs::SideEffectWrapperInfo MakeSideEffectWrapperInfo( |
| 28 |
const protobufs::InstructionDescriptor& instruction, |
| 29 |
uint32_t merge_block_id, uint32_t execute_block_id, |
| 30 |
uint32_t actual_result_id, uint32_t alternative_block_id, |
| 31 |
uint32_t placeholder_result_id, uint32_t value_to_copy_id) { |
| 32 |
protobufs::SideEffectWrapperInfo result; |
| 33 |
*result.mutable_instruction() = instruction; |
| 34 |
result.set_merge_block_id(merge_block_id); |
| 35 |
result.set_execute_block_id(execute_block_id); |
| 36 |
result.set_actual_result_id(actual_result_id); |
| 37 |
result.set_alternative_block_id(alternative_block_id); |
| 38 |
result.set_placeholder_result_id(placeholder_result_id); |
| 39 |
result.set_value_to_copy_id(value_to_copy_id); |
| 40 |
return result; |
| 41 |
} |
| 42 |
|
| 43 |
protobufs::SideEffectWrapperInfo MakeSideEffectWrapperInfo( |
| 44 |
const protobufs::InstructionDescriptor& instruction, |
| 45 |
uint32_t merge_block_id, uint32_t execute_block_id) { |
| 46 |
return MakeSideEffectWrapperInfo(instruction, merge_block_id, |
| 47 |
execute_block_id, 0, 0, 0, 0); |
| 48 |
} |
| 49 |
|
| 50 |
TEST(TransformationFlattenConditionalBranchTest, Inapplicable) { |
| 51 |
std::string shader = R"( |
| 52 |
OpCapability Shader |
| 53 |
%1 = OpExtInstImport "GLSL.std.450" |
| 54 |
OpMemoryModel Logical GLSL450 |
| 55 |
OpEntryPoint Fragment %2 "main" %3 |
| 56 |
OpExecutionMode %2 OriginUpperLeft |
| 57 |
OpSource ESSL 310 |
| 58 |
OpName %2 "main" |
| 59 |
%4 = OpTypeVoid |
| 60 |
%5 = OpTypeFunction %4 |
| 61 |
%6 = OpTypeInt 32 1 |
| 62 |
%7 = OpTypeInt 32 0 |
| 63 |
%8 = OpConstant %7 0 |
| 64 |
%9 = OpTypeBool |
| 65 |
%10 = OpConstantTrue %9 |
| 66 |
%11 = OpTypePointer Function %6 |
| 67 |
%12 = OpTypePointer Workgroup %6 |
| 68 |
%3 = OpVariable %12 Workgroup |
| 69 |
%13 = OpConstant %6 2 |
| 70 |
%2 = OpFunction %4 None %5 |
| 71 |
%14 = OpLabel |
| 72 |
OpBranch %15 |
| 73 |
%15 = OpLabel |
| 74 |
OpSelectionMerge %16 None |
| 75 |
OpSwitch %13 %17 2 %18 |
| 76 |
%17 = OpLabel |
| 77 |
OpBranch %16 |
| 78 |
%18 = OpLabel |
| 79 |
OpBranch %16 |
| 80 |
%16 = OpLabel |
| 81 |
OpLoopMerge %19 %16 None |
| 82 |
OpBranchConditional %10 %16 %19 |
| 83 |
%19 = OpLabel |
| 84 |
OpSelectionMerge %20 None |
| 85 |
OpBranchConditional %10 %21 %20 |
| 86 |
%21 = OpLabel |
| 87 |
OpReturn |
| 88 |
%20 = OpLabel |
| 89 |
OpSelectionMerge %22 None |
| 90 |
OpBranchConditional %10 %23 %22 |
| 91 |
%23 = OpLabel |
| 92 |
OpSelectionMerge %24 None |
| 93 |
OpBranchConditional %10 %25 %24 |
| 94 |
%25 = OpLabel |
| 95 |
OpBranch %24 |
| 96 |
%24 = OpLabel |
| 97 |
OpBranch %22 |
| 98 |
%22 = OpLabel |
| 99 |
OpSelectionMerge %26 None |
| 100 |
OpBranchConditional %10 %26 %27 |
| 101 |
%27 = OpLabel |
| 102 |
OpBranch %28 |
| 103 |
%28 = OpLabel |
| 104 |
OpLoopMerge %29 %28 None |
| 105 |
OpBranchConditional %10 %28 %29 |
| 106 |
%29 = OpLabel |
| 107 |
OpBranch %26 |
| 108 |
%26 = OpLabel |
| 109 |
OpSelectionMerge %30 None |
| 110 |
OpBranchConditional %10 %30 %31 |
| 111 |
%31 = OpLabel |
| 112 |
OpBranch %32 |
| 113 |
%32 = OpLabel |
| 114 |
%33 = OpAtomicLoad %6 %3 %8 %8 |
| 115 |
OpBranch %30 |
| 116 |
%30 = OpLabel |
| 117 |
OpSelectionMerge %34 None |
| 118 |
OpBranchConditional %10 %35 %34 |
| 119 |
%35 = OpLabel |
| 120 |
OpMemoryBarrier %8 %8 |
| 121 |
OpBranch %34 |
| 122 |
%34 = OpLabel |
| 123 |
OpLoopMerge %40 %39 None |
| 124 |
OpBranchConditional %10 %36 %40 |
| 125 |
%36 = OpLabel |
| 126 |
OpSelectionMerge %38 None |
| 127 |
OpBranchConditional %10 %37 %38 |
| 128 |
%37 = OpLabel |
| 129 |
OpBranch %40 |
| 130 |
%38 = OpLabel |
| 131 |
OpBranch %39 |
| 132 |
%39 = OpLabel |
| 133 |
OpBranch %34 |
| 134 |
%40 = OpLabel |
| 135 |
OpReturn |
| 136 |
OpFunctionEnd |
| 137 |
)"; |
| 138 |
|
| 139 |
const auto env = SPV_ENV_UNIVERSAL_1_5; |
| 140 |
const auto consumer = nullptr; |
| 141 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 142 |
spvtools::ValidatorOptions validator_options; |
| 143 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 144 |
kConsoleMessageConsumer)); |
| 145 |
TransformationContext transformation_context( |
| 146 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 147 |
// Block %15 does not end with OpBranchConditional. |
| 148 |
ASSERT_FALSE(TransformationFlattenConditionalBranch(15, true, 0, 0, 0, {}) |
| 149 |
.IsApplicable(context.get(), transformation_context)); |
| 150 |
|
| 151 |
// Block %17 is not a selection header. |
| 152 |
ASSERT_FALSE(TransformationFlattenConditionalBranch(17, true, 0, 0, 0, {}) |
| 153 |
.IsApplicable(context.get(), transformation_context)); |
| 154 |
|
| 155 |
// Block %16 is a loop header, not a selection header. |
| 156 |
ASSERT_FALSE(TransformationFlattenConditionalBranch(16, true, 0, 0, 0, {}) |
| 157 |
.IsApplicable(context.get(), transformation_context)); |
| 158 |
|
| 159 |
// Block %19 and the corresponding merge block do not describe a single-entry, |
| 160 |
// single-exit region, because there is a return instruction in %21. |
| 161 |
ASSERT_FALSE(TransformationFlattenConditionalBranch(19, true, 0, 0, 0, {}) |
| 162 |
.IsApplicable(context.get(), transformation_context)); |
| 163 |
|
| 164 |
// Block %20 is the header of a construct containing an inner selection |
| 165 |
// construct. |
| 166 |
ASSERT_FALSE(TransformationFlattenConditionalBranch(20, true, 0, 0, 0, {}) |
| 167 |
.IsApplicable(context.get(), transformation_context)); |
| 168 |
|
| 169 |
// Block %22 is the header of a construct containing an inner loop. |
| 170 |
ASSERT_FALSE(TransformationFlattenConditionalBranch(22, true, 0, 0, 0, {}) |
| 171 |
.IsApplicable(context.get(), transformation_context)); |
| 172 |
|
| 173 |
// Block %30 is the header of a construct containing a barrier instruction. |
| 174 |
ASSERT_FALSE(TransformationFlattenConditionalBranch(30, true, 0, 0, 0, {}) |
| 175 |
.IsApplicable(context.get(), transformation_context)); |
| 176 |
|
| 177 |
// %33 is not a block. |
| 178 |
ASSERT_FALSE(TransformationFlattenConditionalBranch(33, true, 0, 0, 0, {}) |
| 179 |
.IsApplicable(context.get(), transformation_context)); |
| 180 |
|
| 181 |
// Block %36 and the corresponding merge block do not describe a single-entry, |
| 182 |
// single-exit region, because block %37 breaks out of the outer loop. |
| 183 |
ASSERT_FALSE(TransformationFlattenConditionalBranch(36, true, 0, 0, 0, {}) |
| 184 |
.IsApplicable(context.get(), transformation_context)); |
| 185 |
} |
| 186 |
|
| 187 |
TEST(TransformationFlattenConditionalBranchTest, Simple) { |
| 188 |
std::string shader = R"( |
| 189 |
OpCapability Shader |
| 190 |
%1 = OpExtInstImport "GLSL.std.450" |
| 191 |
OpMemoryModel Logical GLSL450 |
| 192 |
OpEntryPoint Fragment %2 "main" |
| 193 |
OpExecutionMode %2 OriginUpperLeft |
| 194 |
OpSource ESSL 310 |
| 195 |
OpName %2 "main" |
| 196 |
%3 = OpTypeBool |
| 197 |
%4 = OpConstantTrue %3 |
| 198 |
%5 = OpTypeVoid |
| 199 |
%6 = OpTypeFunction %5 |
| 200 |
%2 = OpFunction %5 None %6 |
| 201 |
%7 = OpLabel |
| 202 |
OpSelectionMerge %8 None |
| 203 |
OpBranchConditional %4 %9 %10 |
| 204 |
%10 = OpLabel |
| 205 |
%26 = OpPhi %3 %4 %7 |
| 206 |
OpBranch %8 |
| 207 |
%9 = OpLabel |
| 208 |
%27 = OpPhi %3 %4 %7 |
| 209 |
%11 = OpCopyObject %3 %4 |
| 210 |
OpBranch %8 |
| 211 |
%8 = OpLabel |
| 212 |
%12 = OpPhi %3 %11 %9 %4 %10 |
| 213 |
%23 = OpPhi %3 %4 %9 %4 %10 |
| 214 |
OpBranch %13 |
| 215 |
%13 = OpLabel |
| 216 |
%14 = OpCopyObject %3 %4 |
| 217 |
OpSelectionMerge %15 None |
| 218 |
OpBranchConditional %4 %16 %17 |
| 219 |
%16 = OpLabel |
| 220 |
%28 = OpPhi %3 %4 %13 |
| 221 |
OpBranch %18 |
| 222 |
%18 = OpLabel |
| 223 |
OpBranch %19 |
| 224 |
%17 = OpLabel |
| 225 |
%29 = OpPhi %3 %4 %13 |
| 226 |
%20 = OpCopyObject %3 %4 |
| 227 |
OpBranch %19 |
| 228 |
%19 = OpLabel |
| 229 |
%21 = OpPhi %3 %4 %18 %20 %17 |
| 230 |
OpBranch %15 |
| 231 |
%15 = OpLabel |
| 232 |
OpSelectionMerge %22 None |
| 233 |
OpBranchConditional %4 %22 %22 |
| 234 |
%22 = OpLabel |
| 235 |
%30 = OpPhi %3 %4 %15 |
| 236 |
OpSelectionMerge %25 None |
| 237 |
OpBranchConditional %4 %24 %24 |
| 238 |
%24 = OpLabel |
| 239 |
OpBranch %25 |
| 240 |
%25 = OpLabel |
| 241 |
OpReturn |
| 242 |
OpFunctionEnd |
| 243 |
)"; |
| 244 |
|
| 245 |
const auto env = SPV_ENV_UNIVERSAL_1_5; |
| 246 |
const auto consumer = nullptr; |
| 247 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 248 |
spvtools::ValidatorOptions validator_options; |
| 249 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 250 |
kConsoleMessageConsumer)); |
| 251 |
TransformationContext transformation_context( |
| 252 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 253 |
auto transformation1 = |
| 254 |
TransformationFlattenConditionalBranch(7, true, 0, 0, 0, {}); |
| 255 |
ASSERT_TRUE( |
| 256 |
transformation1.IsApplicable(context.get(), transformation_context)); |
| 257 |
ApplyAndCheckFreshIds(transformation1, context.get(), |
| 258 |
&transformation_context); |
| 259 |
|
| 260 |
auto transformation2 = |
| 261 |
TransformationFlattenConditionalBranch(13, false, 0, 0, 0, {}); |
| 262 |
ASSERT_TRUE( |
| 263 |
transformation2.IsApplicable(context.get(), transformation_context)); |
| 264 |
ApplyAndCheckFreshIds(transformation2, context.get(), |
| 265 |
&transformation_context); |
| 266 |
|
| 267 |
auto transformation3 = |
| 268 |
TransformationFlattenConditionalBranch(15, true, 0, 0, 0, {}); |
| 269 |
ASSERT_TRUE( |
| 270 |
transformation3.IsApplicable(context.get(), transformation_context)); |
| 271 |
ApplyAndCheckFreshIds(transformation3, context.get(), |
| 272 |
&transformation_context); |
| 273 |
|
| 274 |
auto transformation4 = |
| 275 |
TransformationFlattenConditionalBranch(22, false, 0, 0, 0, {}); |
| 276 |
ASSERT_TRUE( |
| 277 |
transformation4.IsApplicable(context.get(), transformation_context)); |
| 278 |
ApplyAndCheckFreshIds(transformation4, context.get(), |
| 279 |
&transformation_context); |
| 280 |
|
| 281 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 282 |
kConsoleMessageConsumer)); |
| 283 |
|
| 284 |
std::string after_transformations = R"( |
| 285 |
OpCapability Shader |
| 286 |
%1 = OpExtInstImport "GLSL.std.450" |
| 287 |
OpMemoryModel Logical GLSL450 |
| 288 |
OpEntryPoint Fragment %2 "main" |
| 289 |
OpExecutionMode %2 OriginUpperLeft |
| 290 |
OpSource ESSL 310 |
| 291 |
OpName %2 "main" |
| 292 |
%3 = OpTypeBool |
| 293 |
%4 = OpConstantTrue %3 |
| 294 |
%5 = OpTypeVoid |
| 295 |
%6 = OpTypeFunction %5 |
| 296 |
%2 = OpFunction %5 None %6 |
| 297 |
%7 = OpLabel |
| 298 |
OpBranch %9 |
| 299 |
%9 = OpLabel |
| 300 |
%27 = OpPhi %3 %4 %7 |
| 301 |
%11 = OpCopyObject %3 %4 |
| 302 |
OpBranch %10 |
| 303 |
%10 = OpLabel |
| 304 |
%26 = OpPhi %3 %4 %9 |
| 305 |
OpBranch %8 |
| 306 |
%8 = OpLabel |
| 307 |
%12 = OpSelect %3 %4 %11 %4 |
| 308 |
%23 = OpSelect %3 %4 %4 %4 |
| 309 |
OpBranch %13 |
| 310 |
%13 = OpLabel |
| 311 |
%14 = OpCopyObject %3 %4 |
| 312 |
OpBranch %17 |
| 313 |
%17 = OpLabel |
| 314 |
%29 = OpPhi %3 %4 %13 |
| 315 |
%20 = OpCopyObject %3 %4 |
| 316 |
OpBranch %16 |
| 317 |
%16 = OpLabel |
| 318 |
%28 = OpPhi %3 %4 %17 |
| 319 |
OpBranch %18 |
| 320 |
%18 = OpLabel |
| 321 |
OpBranch %19 |
| 322 |
%19 = OpLabel |
| 323 |
%21 = OpSelect %3 %4 %4 %20 |
| 324 |
OpBranch %15 |
| 325 |
%15 = OpLabel |
| 326 |
OpBranch %22 |
| 327 |
%22 = OpLabel |
| 328 |
%30 = OpPhi %3 %4 %15 |
| 329 |
OpBranch %24 |
| 330 |
%24 = OpLabel |
| 331 |
OpBranch %25 |
| 332 |
%25 = OpLabel |
| 333 |
OpReturn |
| 334 |
OpFunctionEnd |
| 335 |
)"; |
| 336 |
|
| 337 |
ASSERT_TRUE(IsEqual(env, after_transformations, context.get())); |
| 338 |
} |
| 339 |
|
| 340 |
TEST(TransformationFlattenConditionalBranchTest, LoadStoreFunctionCall) { |
| 341 |
std::string shader = R"( |
| 342 |
OpCapability Shader |
| 343 |
%1 = OpExtInstImport "GLSL.std.450" |
| 344 |
OpMemoryModel Logical GLSL450 |
| 345 |
OpEntryPoint Fragment %2 "main" |
| 346 |
OpExecutionMode %2 OriginUpperLeft |
| 347 |
OpSource ESSL 310 |
| 348 |
%9 = OpTypeVoid |
| 349 |
%10 = OpTypeFunction %9 |
| 350 |
%11 = OpTypeInt 32 1 |
| 351 |
%12 = OpTypeVector %11 4 |
| 352 |
%13 = OpTypeFunction %11 |
| 353 |
%70 = OpConstant %11 0 |
| 354 |
%14 = OpConstant %11 1 |
| 355 |
%15 = OpTypeFloat 32 |
| 356 |
%16 = OpTypeVector %15 2 |
| 357 |
%17 = OpConstant %15 1 |
| 358 |
%18 = OpConstantComposite %16 %17 %17 |
| 359 |
%19 = OpTypeBool |
| 360 |
%20 = OpConstantTrue %19 |
| 361 |
%21 = OpTypePointer Function %11 |
| 362 |
%22 = OpTypeSampler |
| 363 |
%23 = OpTypeImage %9 2D 2 0 0 1 Unknown |
| 364 |
%24 = OpTypeSampledImage %23 |
| 365 |
%25 = OpTypePointer Function %23 |
| 366 |
%26 = OpTypePointer Function %22 |
| 367 |
%27 = OpTypeInt 32 0 |
| 368 |
%28 = OpConstant %27 2 |
| 369 |
%29 = OpTypeArray %11 %28 |
| 370 |
%30 = OpTypePointer Function %29 |
| 371 |
%2 = OpFunction %9 None %10 |
| 372 |
%31 = OpLabel |
| 373 |
%4 = OpVariable %21 Function |
| 374 |
%5 = OpVariable %30 Function |
| 375 |
%32 = OpVariable %25 Function |
| 376 |
%33 = OpVariable %26 Function |
| 377 |
%34 = OpLoad %23 %32 |
| 378 |
%35 = OpLoad %22 %33 |
| 379 |
OpSelectionMerge %36 None |
| 380 |
OpBranchConditional %20 %37 %36 |
| 381 |
%37 = OpLabel |
| 382 |
%6 = OpLoad %11 %4 |
| 383 |
%7 = OpIAdd %11 %6 %14 |
| 384 |
OpStore %4 %7 |
| 385 |
OpBranch %36 |
| 386 |
%36 = OpLabel |
| 387 |
%42 = OpPhi %11 %14 %37 %14 %31 |
| 388 |
OpSelectionMerge %43 None |
| 389 |
OpBranchConditional %20 %44 %45 |
| 390 |
%44 = OpLabel |
| 391 |
%8 = OpFunctionCall %11 %3 |
| 392 |
OpStore %4 %8 |
| 393 |
OpBranch %46 |
| 394 |
%45 = OpLabel |
| 395 |
%47 = OpAccessChain %21 %5 %14 |
| 396 |
OpStore %47 %14 |
| 397 |
OpBranch %46 |
| 398 |
%46 = OpLabel |
| 399 |
OpStore %4 %14 |
| 400 |
OpBranch %43 |
| 401 |
%43 = OpLabel |
| 402 |
OpStore %4 %14 |
| 403 |
OpSelectionMerge %48 None |
| 404 |
OpBranchConditional %20 %49 %48 |
| 405 |
%49 = OpLabel |
| 406 |
OpBranch %48 |
| 407 |
%48 = OpLabel |
| 408 |
OpSelectionMerge %50 None |
| 409 |
OpBranchConditional %20 %51 %50 |
| 410 |
%51 = OpLabel |
| 411 |
%52 = OpSampledImage %24 %34 %35 |
| 412 |
%53 = OpLoad %11 %4 |
| 413 |
%54 = OpImageSampleImplicitLod %12 %52 %18 |
| 414 |
OpBranch %50 |
| 415 |
%50 = OpLabel |
| 416 |
OpReturn |
| 417 |
OpFunctionEnd |
| 418 |
%3 = OpFunction %11 None %13 |
| 419 |
%55 = OpLabel |
| 420 |
OpReturnValue %14 |
| 421 |
OpFunctionEnd |
| 422 |
)"; |
| 423 |
|
| 424 |
const auto env = SPV_ENV_UNIVERSAL_1_5; |
| 425 |
const auto consumer = nullptr; |
| 426 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 427 |
spvtools::ValidatorOptions validator_options; |
| 428 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 429 |
kConsoleMessageConsumer)); |
| 430 |
TransformationContext transformation_context( |
| 431 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 432 |
#ifndef NDEBUG |
| 433 |
// The following checks lead to assertion failures, since some entries |
| 434 |
// requiring fresh ids are not present in the map, and the transformation |
| 435 |
// context does not have a source overflow ids. |
| 436 |
|
| 437 |
ASSERT_DEATH(TransformationFlattenConditionalBranch(31, true, 0, 0, 0, {}) |
| 438 |
.IsApplicable(context.get(), transformation_context), |
| 439 |
"Bad attempt to query whether overflow ids are available."); |
| 440 |
|
| 441 |
ASSERT_DEATH(TransformationFlattenConditionalBranch( |
| 442 |
31, true, 0, 0, 0, |
| 443 |
{{MakeSideEffectWrapperInfo( |
| 444 |
MakeInstructionDescriptor(6, SpvOpLoad, 0), 100, 101, |
| 445 |
102, 103, 104, 14)}}) |
| 446 |
.IsApplicable(context.get(), transformation_context), |
| 447 |
"Bad attempt to query whether overflow ids are available."); |
| 448 |
#endif |
| 449 |
|
| 450 |
// The map maps from an instruction to a list with not enough fresh ids. |
| 451 |
ASSERT_FALSE(TransformationFlattenConditionalBranch( |
| 452 |
31, true, 0, 0, 0, |
| 453 |
{{MakeSideEffectWrapperInfo( |
| 454 |
MakeInstructionDescriptor(6, SpvOpLoad, 0), 100, 101, |
| 455 |
102, 103, 0, 0)}}) |
| 456 |
.IsApplicable(context.get(), transformation_context)); |
| 457 |
|
| 458 |
// Not all fresh ids given are distinct. |
| 459 |
ASSERT_FALSE(TransformationFlattenConditionalBranch( |
| 460 |
31, true, 0, 0, 0, |
| 461 |
{{MakeSideEffectWrapperInfo( |
| 462 |
MakeInstructionDescriptor(6, SpvOpLoad, 0), 100, 100, |
| 463 |
102, 103, 104, 0)}}) |
| 464 |
.IsApplicable(context.get(), transformation_context)); |
| 465 |
|
| 466 |
// %48 heads a construct containing an OpSampledImage instruction. |
| 467 |
ASSERT_FALSE(TransformationFlattenConditionalBranch( |
| 468 |
48, true, 0, 0, 0, |
| 469 |
{{MakeSideEffectWrapperInfo( |
| 470 |
MakeInstructionDescriptor(53, SpvOpLoad, 0), 100, 101, |
| 471 |
102, 103, 104, 0)}}) |
| 472 |
.IsApplicable(context.get(), transformation_context)); |
| 473 |
|
| 474 |
// %0 is not a valid id. |
| 475 |
ASSERT_FALSE( |
| 476 |
TransformationFlattenConditionalBranch( |
| 477 |
31, true, 0, 0, 0, |
| 478 |
{MakeSideEffectWrapperInfo(MakeInstructionDescriptor(6, SpvOpLoad, 0), |
| 479 |
104, 100, 101, 102, 103, 0), |
| 480 |
MakeSideEffectWrapperInfo( |
| 481 |
MakeInstructionDescriptor(6, SpvOpStore, 0), 106, 105)}) |
| 482 |
.IsApplicable(context.get(), transformation_context)); |
| 483 |
|
| 484 |
// %17 is a float constant, while %6 has int type. |
| 485 |
ASSERT_FALSE( |
| 486 |
TransformationFlattenConditionalBranch( |
| 487 |
31, true, 0, 0, 0, |
| 488 |
{MakeSideEffectWrapperInfo(MakeInstructionDescriptor(6, SpvOpLoad, 0), |
| 489 |
104, 100, 101, 102, 103, 17), |
| 490 |
MakeSideEffectWrapperInfo( |
| 491 |
MakeInstructionDescriptor(6, SpvOpStore, 0), 106, 105)}) |
| 492 |
.IsApplicable(context.get(), transformation_context)); |
| 493 |
|
| 494 |
auto transformation1 = TransformationFlattenConditionalBranch( |
| 495 |
31, true, 0, 0, 0, |
| 496 |
{MakeSideEffectWrapperInfo(MakeInstructionDescriptor(6, SpvOpLoad, 0), |
| 497 |
104, 100, 101, 102, 103, 70), |
| 498 |
MakeSideEffectWrapperInfo(MakeInstructionDescriptor(6, SpvOpStore, 0), |
| 499 |
106, 105)}); |
| 500 |
ASSERT_TRUE( |
| 501 |
transformation1.IsApplicable(context.get(), transformation_context)); |
| 502 |
ApplyAndCheckFreshIds(transformation1, context.get(), |
| 503 |
&transformation_context); |
| 504 |
|
| 505 |
// Check that the placeholder id was marked as irrelevant. |
| 506 |
ASSERT_TRUE(transformation_context.GetFactManager()->IdIsIrrelevant(103)); |
| 507 |
|
| 508 |
// Make a new transformation context with a source of overflow ids. |
| 509 |
auto overflow_ids_unique_ptr = MakeUnique<CounterOverflowIdSource>(1000); |
| 510 |
auto overflow_ids_ptr = overflow_ids_unique_ptr.get(); |
| 511 |
TransformationContext new_transformation_context( |
| 512 |
MakeUnique<FactManager>(context.get()), validator_options, |
| 513 |
std::move(overflow_ids_unique_ptr)); |
| 514 |
|
| 515 |
auto transformation2 = TransformationFlattenConditionalBranch( |
| 516 |
36, false, 0, 0, 0, |
| 517 |
{MakeSideEffectWrapperInfo(MakeInstructionDescriptor(8, SpvOpStore, 0), |
| 518 |
114, 113)}); |
| 519 |
ASSERT_TRUE( |
| 520 |
transformation2.IsApplicable(context.get(), new_transformation_context)); |
| 521 |
ApplyAndCheckFreshIds(transformation2, context.get(), |
| 522 |
&new_transformation_context, |
| 523 |
overflow_ids_ptr->GetIssuedOverflowIds()); |
| 524 |
|
| 525 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 526 |
kConsoleMessageConsumer)); |
| 527 |
|
| 528 |
std::string after_transformations = R"( |
| 529 |
OpCapability Shader |
| 530 |
%1 = OpExtInstImport "GLSL.std.450" |
| 531 |
OpMemoryModel Logical GLSL450 |
| 532 |
OpEntryPoint Fragment %2 "main" |
| 533 |
OpExecutionMode %2 OriginUpperLeft |
| 534 |
OpSource ESSL 310 |
| 535 |
%9 = OpTypeVoid |
| 536 |
%10 = OpTypeFunction %9 |
| 537 |
%11 = OpTypeInt 32 1 |
| 538 |
%12 = OpTypeVector %11 4 |
| 539 |
%13 = OpTypeFunction %11 |
| 540 |
%70 = OpConstant %11 0 |
| 541 |
%14 = OpConstant %11 1 |
| 542 |
%15 = OpTypeFloat 32 |
| 543 |
%16 = OpTypeVector %15 2 |
| 544 |
%17 = OpConstant %15 1 |
| 545 |
%18 = OpConstantComposite %16 %17 %17 |
| 546 |
%19 = OpTypeBool |
| 547 |
%20 = OpConstantTrue %19 |
| 548 |
%21 = OpTypePointer Function %11 |
| 549 |
%22 = OpTypeSampler |
| 550 |
%23 = OpTypeImage %9 2D 2 0 0 1 Unknown |
| 551 |
%24 = OpTypeSampledImage %23 |
| 552 |
%25 = OpTypePointer Function %23 |
| 553 |
%26 = OpTypePointer Function %22 |
| 554 |
%27 = OpTypeInt 32 0 |
| 555 |
%28 = OpConstant %27 2 |
| 556 |
%29 = OpTypeArray %11 %28 |
| 557 |
%30 = OpTypePointer Function %29 |
| 558 |
%2 = OpFunction %9 None %10 |
| 559 |
%31 = OpLabel |
| 560 |
%4 = OpVariable %21 Function |
| 561 |
%5 = OpVariable %30 Function |
| 562 |
%32 = OpVariable %25 Function |
| 563 |
%33 = OpVariable %26 Function |
| 564 |
%34 = OpLoad %23 %32 |
| 565 |
%35 = OpLoad %22 %33 |
| 566 |
OpBranch %37 |
| 567 |
%37 = OpLabel |
| 568 |
OpSelectionMerge %104 None |
| 569 |
OpBranchConditional %20 %100 %102 |
| 570 |
%100 = OpLabel |
| 571 |
%101 = OpLoad %11 %4 |
| 572 |
OpBranch %104 |
| 573 |
%102 = OpLabel |
| 574 |
%103 = OpCopyObject %11 %70 |
| 575 |
OpBranch %104 |
| 576 |
%104 = OpLabel |
| 577 |
%6 = OpPhi %11 %101 %100 %103 %102 |
| 578 |
%7 = OpIAdd %11 %6 %14 |
| 579 |
OpSelectionMerge %106 None |
| 580 |
OpBranchConditional %20 %105 %106 |
| 581 |
%105 = OpLabel |
| 582 |
OpStore %4 %7 |
| 583 |
OpBranch %106 |
| 584 |
%106 = OpLabel |
| 585 |
OpBranch %36 |
| 586 |
%36 = OpLabel |
| 587 |
%42 = OpSelect %11 %20 %14 %14 |
| 588 |
OpBranch %45 |
| 589 |
%45 = OpLabel |
| 590 |
%47 = OpAccessChain %21 %5 %14 |
| 591 |
OpSelectionMerge %1005 None |
| 592 |
OpBranchConditional %20 %1005 %1006 |
| 593 |
%1006 = OpLabel |
| 594 |
OpStore %47 %14 |
| 595 |
OpBranch %1005 |
| 596 |
%1005 = OpLabel |
| 597 |
OpBranch %44 |
| 598 |
%44 = OpLabel |
| 599 |
OpSelectionMerge %1000 None |
| 600 |
OpBranchConditional %20 %1001 %1003 |
| 601 |
%1001 = OpLabel |
| 602 |
%1002 = OpFunctionCall %11 %3 |
| 603 |
OpBranch %1000 |
| 604 |
%1003 = OpLabel |
| 605 |
%1004 = OpCopyObject %11 %70 |
| 606 |
OpBranch %1000 |
| 607 |
%1000 = OpLabel |
| 608 |
%8 = OpPhi %11 %1002 %1001 %1004 %1003 |
| 609 |
OpSelectionMerge %114 None |
| 610 |
OpBranchConditional %20 %113 %114 |
| 611 |
%113 = OpLabel |
| 612 |
OpStore %4 %8 |
| 613 |
OpBranch %114 |
| 614 |
%114 = OpLabel |
| 615 |
OpBranch %46 |
| 616 |
%46 = OpLabel |
| 617 |
OpStore %4 %14 |
| 618 |
OpBranch %43 |
| 619 |
%43 = OpLabel |
| 620 |
OpStore %4 %14 |
| 621 |
OpSelectionMerge %48 None |
| 622 |
OpBranchConditional %20 %49 %48 |
| 623 |
%49 = OpLabel |
| 624 |
OpBranch %48 |
| 625 |
%48 = OpLabel |
| 626 |
OpSelectionMerge %50 None |
| 627 |
OpBranchConditional %20 %51 %50 |
| 628 |
%51 = OpLabel |
| 629 |
%52 = OpSampledImage %24 %34 %35 |
| 630 |
%53 = OpLoad %11 %4 |
| 631 |
%54 = OpImageSampleImplicitLod %12 %52 %18 |
| 632 |
OpBranch %50 |
| 633 |
%50 = OpLabel |
| 634 |
OpReturn |
| 635 |
OpFunctionEnd |
| 636 |
%3 = OpFunction %11 None %13 |
| 637 |
%55 = OpLabel |
| 638 |
OpReturnValue %14 |
| 639 |
OpFunctionEnd |
| 640 |
)"; |
| 641 |
|
| 642 |
ASSERT_TRUE(IsEqual(env, after_transformations, context.get())); |
| 643 |
} // namespace |
| 644 |
|
| 645 |
TEST(TransformationFlattenConditionalBranchTest, EdgeCases) { |
| 646 |
std::string shader = R"( |
| 647 |
OpCapability Shader |
| 648 |
%1 = OpExtInstImport "GLSL.std.450" |
| 649 |
OpMemoryModel Logical GLSL450 |
| 650 |
OpEntryPoint Fragment %2 "main" |
| 651 |
OpExecutionMode %2 OriginUpperLeft |
| 652 |
OpSource ESSL 310 |
| 653 |
%3 = OpTypeVoid |
| 654 |
%4 = OpTypeBool |
| 655 |
%5 = OpConstantTrue %4 |
| 656 |
%6 = OpTypeFunction %3 |
| 657 |
%2 = OpFunction %3 None %6 |
| 658 |
%7 = OpLabel |
| 659 |
OpSelectionMerge %8 None |
| 660 |
OpBranchConditional %5 %9 %8 |
| 661 |
%9 = OpLabel |
| 662 |
%10 = OpFunctionCall %3 %11 |
| 663 |
OpBranch %8 |
| 664 |
%8 = OpLabel |
| 665 |
OpSelectionMerge %12 None |
| 666 |
OpBranchConditional %5 %13 %12 |
| 667 |
%13 = OpLabel |
| 668 |
%14 = OpFunctionCall %3 %11 |
| 669 |
%15 = OpCopyObject %3 %14 |
| 670 |
OpBranch %12 |
| 671 |
%12 = OpLabel |
| 672 |
OpReturn |
| 673 |
%16 = OpLabel |
| 674 |
OpSelectionMerge %17 None |
| 675 |
OpBranchConditional %5 %18 %17 |
| 676 |
%18 = OpLabel |
| 677 |
OpBranch %17 |
| 678 |
%17 = OpLabel |
| 679 |
OpReturn |
| 680 |
OpFunctionEnd |
| 681 |
%11 = OpFunction %3 None %6 |
| 682 |
%19 = OpLabel |
| 683 |
OpBranch %20 |
| 684 |
%20 = OpLabel |
| 685 |
OpSelectionMerge %25 None |
| 686 |
OpBranchConditional %5 %21 %22 |
| 687 |
%21 = OpLabel |
| 688 |
OpBranch %22 |
| 689 |
%22 = OpLabel |
| 690 |
OpSelectionMerge %24 None |
| 691 |
OpBranchConditional %5 %24 %23 |
| 692 |
%23 = OpLabel |
| 693 |
OpBranch %24 |
| 694 |
%24 = OpLabel |
| 695 |
OpBranch %25 |
| 696 |
%25 = OpLabel |
| 697 |
OpReturn |
| 698 |
OpFunctionEnd |
| 699 |
)"; |
| 700 |
|
| 701 |
const auto env = SPV_ENV_UNIVERSAL_1_5; |
| 702 |
const auto consumer = nullptr; |
| 703 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 704 |
spvtools::ValidatorOptions validator_options; |
| 705 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 706 |
kConsoleMessageConsumer)); |
| 707 |
TransformationContext transformation_context( |
| 708 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 709 |
#ifndef NDEBUG |
| 710 |
// The selection construct headed by %7 requires fresh ids because it contains |
| 711 |
// a function call. This causes an assertion failure because transformation |
| 712 |
// context does not have a source of overflow ids. |
| 713 |
ASSERT_DEATH(TransformationFlattenConditionalBranch(7, true, 0, 0, 0, {}) |
| 714 |
.IsApplicable(context.get(), transformation_context), |
| 715 |
"Bad attempt to query whether overflow ids are available."); |
| 716 |
#endif |
| 717 |
|
| 718 |
auto transformation1 = TransformationFlattenConditionalBranch( |
| 719 |
7, true, 0, 0, 0, |
| 720 |
{{MakeSideEffectWrapperInfo( |
| 721 |
MakeInstructionDescriptor(10, SpvOpFunctionCall, 0), 100, 101)}}); |
| 722 |
ASSERT_TRUE( |
| 723 |
transformation1.IsApplicable(context.get(), transformation_context)); |
| 724 |
ApplyAndCheckFreshIds(transformation1, context.get(), |
| 725 |
&transformation_context); |
| 726 |
|
| 727 |
// The selection construct headed by %8 cannot be flattened because it |
| 728 |
// contains a function call returning void, whose result id is used. |
| 729 |
ASSERT_FALSE( |
| 730 |
TransformationFlattenConditionalBranch( |
| 731 |
7, true, 0, 0, 0, |
| 732 |
{{MakeSideEffectWrapperInfo( |
| 733 |
MakeInstructionDescriptor(14, SpvOpFunctionCall, 0), 102, 103)}}) |
| 734 |
.IsApplicable(context.get(), transformation_context)); |
| 735 |
|
| 736 |
// Block %16 is unreachable. |
| 737 |
ASSERT_FALSE(TransformationFlattenConditionalBranch(16, true, 0, 0, 0, {}) |
| 738 |
.IsApplicable(context.get(), transformation_context)); |
| 739 |
|
| 740 |
auto transformation2 = |
| 741 |
TransformationFlattenConditionalBranch(20, false, 0, 0, 0, {}); |
| 742 |
ASSERT_TRUE( |
| 743 |
transformation2.IsApplicable(context.get(), transformation_context)); |
| 744 |
ApplyAndCheckFreshIds(transformation2, context.get(), |
| 745 |
&transformation_context); |
| 746 |
|
| 747 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 748 |
kConsoleMessageConsumer)); |
| 749 |
|
| 750 |
std::string after_transformation = R"( |
| 751 |
OpCapability Shader |
| 752 |
%1 = OpExtInstImport "GLSL.std.450" |
| 753 |
OpMemoryModel Logical GLSL450 |
| 754 |
OpEntryPoint Fragment %2 "main" |
| 755 |
OpExecutionMode %2 OriginUpperLeft |
| 756 |
OpSource ESSL 310 |
| 757 |
%3 = OpTypeVoid |
| 758 |
%4 = OpTypeBool |
| 759 |
%5 = OpConstantTrue %4 |
| 760 |
%6 = OpTypeFunction %3 |
| 761 |
%2 = OpFunction %3 None %6 |
| 762 |
%7 = OpLabel |
| 763 |
OpBranch %9 |
| 764 |
%9 = OpLabel |
| 765 |
OpSelectionMerge %100 None |
| 766 |
OpBranchConditional %5 %101 %100 |
| 767 |
%101 = OpLabel |
| 768 |
%10 = OpFunctionCall %3 %11 |
| 769 |
OpBranch %100 |
| 770 |
%100 = OpLabel |
| 771 |
OpBranch %8 |
| 772 |
%8 = OpLabel |
| 773 |
OpSelectionMerge %12 None |
| 774 |
OpBranchConditional %5 %13 %12 |
| 775 |
%13 = OpLabel |
| 776 |
%14 = OpFunctionCall %3 %11 |
| 777 |
%15 = OpCopyObject %3 %14 |
| 778 |
OpBranch %12 |
| 779 |
%12 = OpLabel |
| 780 |
OpReturn |
| 781 |
%16 = OpLabel |
| 782 |
OpSelectionMerge %17 None |
| 783 |
OpBranchConditional %5 %18 %17 |
| 784 |
%18 = OpLabel |
| 785 |
OpBranch %17 |
| 786 |
%17 = OpLabel |
| 787 |
OpReturn |
| 788 |
OpFunctionEnd |
| 789 |
%11 = OpFunction %3 None %6 |
| 790 |
%19 = OpLabel |
| 791 |
OpBranch %20 |
| 792 |
%20 = OpLabel |
| 793 |
OpBranch %21 |
| 794 |
%21 = OpLabel |
| 795 |
OpBranch %22 |
| 796 |
%22 = OpLabel |
| 797 |
OpSelectionMerge %24 None |
| 798 |
OpBranchConditional %5 %24 %23 |
| 799 |
%23 = OpLabel |
| 800 |
OpBranch %24 |
| 801 |
%24 = OpLabel |
| 802 |
OpBranch %25 |
| 803 |
%25 = OpLabel |
| 804 |
OpReturn |
| 805 |
OpFunctionEnd |
| 806 |
)"; |
| 807 |
|
| 808 |
ASSERT_TRUE(IsEqual(env, after_transformation, context.get())); |
| 809 |
} |
| 810 |
|
| 811 |
TEST(TransformationFlattenConditionalBranchTest, PhiToSelect1) { |
| 812 |
std::string shader = R"( |
| 813 |
OpCapability Shader |
| 814 |
%1 = OpExtInstImport "GLSL.std.450" |
| 815 |
OpMemoryModel Logical GLSL450 |
| 816 |
OpEntryPoint Fragment %2 "main" |
| 817 |
OpExecutionMode %2 OriginUpperLeft |
| 818 |
OpSource ESSL 310 |
| 819 |
%3 = OpTypeVoid |
| 820 |
%4 = OpTypeBool |
| 821 |
%5 = OpConstantTrue %4 |
| 822 |
%10 = OpConstantFalse %4 |
| 823 |
%6 = OpTypeFunction %3 |
| 824 |
%2 = OpFunction %3 None %6 |
| 825 |
%7 = OpLabel |
| 826 |
OpSelectionMerge %8 None |
| 827 |
OpBranchConditional %5 %9 %8 |
| 828 |
%9 = OpLabel |
| 829 |
OpBranch %8 |
| 830 |
%8 = OpLabel |
| 831 |
%11 = OpPhi %4 %5 %9 %10 %7 |
| 832 |
OpReturn |
| 833 |
OpFunctionEnd |
| 834 |
)"; |
| 835 |
|
| 836 |
const auto env = SPV_ENV_UNIVERSAL_1_5; |
| 837 |
const auto consumer = nullptr; |
| 838 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 839 |
spvtools::ValidatorOptions validator_options; |
| 840 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 841 |
kConsoleMessageConsumer)); |
| 842 |
TransformationContext transformation_context( |
| 843 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 844 |
|
| 845 |
auto transformation = |
| 846 |
TransformationFlattenConditionalBranch(7, true, 0, 0, 0, {}); |
| 847 |
ASSERT_TRUE( |
| 848 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 849 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 850 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 851 |
kConsoleMessageConsumer)); |
| 852 |
|
| 853 |
std::string after_transformation = R"( |
| 854 |
OpCapability Shader |
| 855 |
%1 = OpExtInstImport "GLSL.std.450" |
| 856 |
OpMemoryModel Logical GLSL450 |
| 857 |
OpEntryPoint Fragment %2 "main" |
| 858 |
OpExecutionMode %2 OriginUpperLeft |
| 859 |
OpSource ESSL 310 |
| 860 |
%3 = OpTypeVoid |
| 861 |
%4 = OpTypeBool |
| 862 |
%5 = OpConstantTrue %4 |
| 863 |
%10 = OpConstantFalse %4 |
| 864 |
%6 = OpTypeFunction %3 |
| 865 |
%2 = OpFunction %3 None %6 |
| 866 |
%7 = OpLabel |
| 867 |
OpBranch %9 |
| 868 |
%9 = OpLabel |
| 869 |
OpBranch %8 |
| 870 |
%8 = OpLabel |
| 871 |
%11 = OpSelect %4 %5 %5 %10 |
| 872 |
OpReturn |
| 873 |
OpFunctionEnd |
| 874 |
)"; |
| 875 |
ASSERT_TRUE(IsEqual(env, after_transformation, context.get())); |
| 876 |
} |
| 877 |
|
| 878 |
TEST(TransformationFlattenConditionalBranchTest, PhiToSelect2) { |
| 879 |
std::string shader = R"( |
| 880 |
OpCapability Shader |
| 881 |
%1 = OpExtInstImport "GLSL.std.450" |
| 882 |
OpMemoryModel Logical GLSL450 |
| 883 |
OpEntryPoint Fragment %2 "main" |
| 884 |
OpExecutionMode %2 OriginUpperLeft |
| 885 |
OpSource ESSL 310 |
| 886 |
%3 = OpTypeVoid |
| 887 |
%4 = OpTypeBool |
| 888 |
%5 = OpConstantTrue %4 |
| 889 |
%10 = OpConstantFalse %4 |
| 890 |
%6 = OpTypeFunction %3 |
| 891 |
%2 = OpFunction %3 None %6 |
| 892 |
%7 = OpLabel |
| 893 |
OpSelectionMerge %8 None |
| 894 |
OpBranchConditional %5 %9 %8 |
| 895 |
%9 = OpLabel |
| 896 |
OpBranch %8 |
| 897 |
%8 = OpLabel |
| 898 |
%11 = OpPhi %4 %10 %7 %5 %9 |
| 899 |
OpReturn |
| 900 |
OpFunctionEnd |
| 901 |
)"; |
| 902 |
|
| 903 |
const auto env = SPV_ENV_UNIVERSAL_1_5; |
| 904 |
const auto consumer = nullptr; |
| 905 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 906 |
spvtools::ValidatorOptions validator_options; |
| 907 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 908 |
kConsoleMessageConsumer)); |
| 909 |
TransformationContext transformation_context( |
| 910 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 911 |
|
| 912 |
auto transformation = |
| 913 |
TransformationFlattenConditionalBranch(7, true, 0, 0, 0, {}); |
| 914 |
ASSERT_TRUE( |
| 915 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 916 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 917 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 918 |
kConsoleMessageConsumer)); |
| 919 |
|
| 920 |
std::string after_transformation = R"( |
| 921 |
OpCapability Shader |
| 922 |
%1 = OpExtInstImport "GLSL.std.450" |
| 923 |
OpMemoryModel Logical GLSL450 |
| 924 |
OpEntryPoint Fragment %2 "main" |
| 925 |
OpExecutionMode %2 OriginUpperLeft |
| 926 |
OpSource ESSL 310 |
| 927 |
%3 = OpTypeVoid |
| 928 |
%4 = OpTypeBool |
| 929 |
%5 = OpConstantTrue %4 |
| 930 |
%10 = OpConstantFalse %4 |
| 931 |
%6 = OpTypeFunction %3 |
| 932 |
%2 = OpFunction %3 None %6 |
| 933 |
%7 = OpLabel |
| 934 |
OpBranch %9 |
| 935 |
%9 = OpLabel |
| 936 |
OpBranch %8 |
| 937 |
%8 = OpLabel |
| 938 |
%11 = OpSelect %4 %5 %5 %10 |
| 939 |
OpReturn |
| 940 |
OpFunctionEnd |
| 941 |
)"; |
| 942 |
ASSERT_TRUE(IsEqual(env, after_transformation, context.get())); |
| 943 |
} |
| 944 |
|
| 945 |
TEST(TransformationFlattenConditionalBranchTest, PhiToSelect3) { |
| 946 |
std::string shader = R"( |
| 947 |
OpCapability Shader |
| 948 |
%1 = OpExtInstImport "GLSL.std.450" |
| 949 |
OpMemoryModel Logical GLSL450 |
| 950 |
OpEntryPoint Fragment %2 "main" |
| 951 |
OpExecutionMode %2 OriginUpperLeft |
| 952 |
OpSource ESSL 310 |
| 953 |
%3 = OpTypeVoid |
| 954 |
%4 = OpTypeBool |
| 955 |
%5 = OpConstantTrue %4 |
| 956 |
%10 = OpConstantFalse %4 |
| 957 |
%6 = OpTypeFunction %3 |
| 958 |
%2 = OpFunction %3 None %6 |
| 959 |
%7 = OpLabel |
| 960 |
OpSelectionMerge %8 None |
| 961 |
OpBranchConditional %5 %9 %12 |
| 962 |
%9 = OpLabel |
| 963 |
OpBranch %8 |
| 964 |
%12 = OpLabel |
| 965 |
OpBranch %8 |
| 966 |
%8 = OpLabel |
| 967 |
%11 = OpPhi %4 %10 %12 %5 %9 |
| 968 |
OpReturn |
| 969 |
OpFunctionEnd |
| 970 |
)"; |
| 971 |
|
| 972 |
const auto env = SPV_ENV_UNIVERSAL_1_5; |
| 973 |
const auto consumer = nullptr; |
| 974 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 975 |
spvtools::ValidatorOptions validator_options; |
| 976 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 977 |
kConsoleMessageConsumer)); |
| 978 |
TransformationContext transformation_context( |
| 979 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 980 |
|
| 981 |
auto transformation = |
| 982 |
TransformationFlattenConditionalBranch(7, true, 0, 0, 0, {}); |
| 983 |
ASSERT_TRUE( |
| 984 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 985 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 986 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 987 |
kConsoleMessageConsumer)); |
| 988 |
|
| 989 |
std::string after_transformation = R"( |
| 990 |
OpCapability Shader |
| 991 |
%1 = OpExtInstImport "GLSL.std.450" |
| 992 |
OpMemoryModel Logical GLSL450 |
| 993 |
OpEntryPoint Fragment %2 "main" |
| 994 |
OpExecutionMode %2 OriginUpperLeft |
| 995 |
OpSource ESSL 310 |
| 996 |
%3 = OpTypeVoid |
| 997 |
%4 = OpTypeBool |
| 998 |
%5 = OpConstantTrue %4 |
| 999 |
%10 = OpConstantFalse %4 |
| 1000 |
%6 = OpTypeFunction %3 |
| 1001 |
%2 = OpFunction %3 None %6 |
| 1002 |
%7 = OpLabel |
| 1003 |
OpBranch %9 |
| 1004 |
%9 = OpLabel |
| 1005 |
OpBranch %12 |
| 1006 |
%12 = OpLabel |
| 1007 |
OpBranch %8 |
| 1008 |
%8 = OpLabel |
| 1009 |
%11 = OpSelect %4 %5 %5 %10 |
| 1010 |
OpReturn |
| 1011 |
OpFunctionEnd |
| 1012 |
)"; |
| 1013 |
ASSERT_TRUE(IsEqual(env, after_transformation, context.get())); |
| 1014 |
} |
| 1015 |
|
| 1016 |
TEST(TransformationFlattenConditionalBranchTest, PhiToSelect4) { |
| 1017 |
std::string shader = R"( |
| 1018 |
OpCapability Shader |
| 1019 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1020 |
OpMemoryModel Logical GLSL450 |
| 1021 |
OpEntryPoint Fragment %2 "main" |
| 1022 |
OpExecutionMode %2 OriginUpperLeft |
| 1023 |
OpSource ESSL 310 |
| 1024 |
%3 = OpTypeVoid |
| 1025 |
%4 = OpTypeBool |
| 1026 |
%5 = OpConstantTrue %4 |
| 1027 |
%10 = OpConstantFalse %4 |
| 1028 |
%6 = OpTypeFunction %3 |
| 1029 |
%2 = OpFunction %3 None %6 |
| 1030 |
%7 = OpLabel |
| 1031 |
OpSelectionMerge %8 None |
| 1032 |
OpBranchConditional %5 %9 %12 |
| 1033 |
%9 = OpLabel |
| 1034 |
OpBranch %8 |
| 1035 |
%12 = OpLabel |
| 1036 |
OpBranch %8 |
| 1037 |
%8 = OpLabel |
| 1038 |
%11 = OpPhi %4 %5 %9 %10 %12 |
| 1039 |
OpReturn |
| 1040 |
OpFunctionEnd |
| 1041 |
)"; |
| 1042 |
|
| 1043 |
const auto env = SPV_ENV_UNIVERSAL_1_5; |
| 1044 |
const auto consumer = nullptr; |
| 1045 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 1046 |
spvtools::ValidatorOptions validator_options; |
| 1047 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 1048 |
kConsoleMessageConsumer)); |
| 1049 |
TransformationContext transformation_context( |
| 1050 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 1051 |
|
| 1052 |
auto transformation = |
| 1053 |
TransformationFlattenConditionalBranch(7, true, 0, 0, 0, {}); |
| 1054 |
ASSERT_TRUE( |
| 1055 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 1056 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 1057 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 1058 |
kConsoleMessageConsumer)); |
| 1059 |
|
| 1060 |
std::string after_transformation = R"( |
| 1061 |
OpCapability Shader |
| 1062 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1063 |
OpMemoryModel Logical GLSL450 |
| 1064 |
OpEntryPoint Fragment %2 "main" |
| 1065 |
OpExecutionMode %2 OriginUpperLeft |
| 1066 |
OpSource ESSL 310 |
| 1067 |
%3 = OpTypeVoid |
| 1068 |
%4 = OpTypeBool |
| 1069 |
%5 = OpConstantTrue %4 |
| 1070 |
%10 = OpConstantFalse %4 |
| 1071 |
%6 = OpTypeFunction %3 |
| 1072 |
%2 = OpFunction %3 None %6 |
| 1073 |
%7 = OpLabel |
| 1074 |
OpBranch %9 |
| 1075 |
%9 = OpLabel |
| 1076 |
OpBranch %12 |
| 1077 |
%12 = OpLabel |
| 1078 |
OpBranch %8 |
| 1079 |
%8 = OpLabel |
| 1080 |
%11 = OpSelect %4 %5 %5 %10 |
| 1081 |
OpReturn |
| 1082 |
OpFunctionEnd |
| 1083 |
)"; |
| 1084 |
ASSERT_TRUE(IsEqual(env, after_transformation, context.get())); |
| 1085 |
} |
| 1086 |
|
| 1087 |
TEST(TransformationFlattenConditionalBranchTest, PhiToSelect5) { |
| 1088 |
std::string shader = R"( |
| 1089 |
OpCapability Shader |
| 1090 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1091 |
OpMemoryModel Logical GLSL450 |
| 1092 |
OpEntryPoint Fragment %2 "main" |
| 1093 |
OpExecutionMode %2 OriginUpperLeft |
| 1094 |
OpSource ESSL 310 |
| 1095 |
%3 = OpTypeVoid |
| 1096 |
%4 = OpTypeBool |
| 1097 |
%5 = OpConstantTrue %4 |
| 1098 |
%10 = OpConstantFalse %4 |
| 1099 |
%6 = OpTypeFunction %3 |
| 1100 |
%100 = OpTypePointer Function %4 |
| 1101 |
%2 = OpFunction %3 None %6 |
| 1102 |
%7 = OpLabel |
| 1103 |
%101 = OpVariable %100 Function |
| 1104 |
%102 = OpVariable %100 Function |
| 1105 |
OpSelectionMerge %470 None |
| 1106 |
OpBranchConditional %5 %454 %462 |
| 1107 |
%454 = OpLabel |
| 1108 |
%522 = OpLoad %4 %101 |
| 1109 |
OpBranch %470 |
| 1110 |
%462 = OpLabel |
| 1111 |
%466 = OpLoad %4 %102 |
| 1112 |
OpBranch %470 |
| 1113 |
%470 = OpLabel |
| 1114 |
%534 = OpPhi %4 %522 %454 %466 %462 |
| 1115 |
OpReturn |
| 1116 |
OpFunctionEnd |
| 1117 |
)"; |
| 1118 |
|
| 1119 |
const auto env = SPV_ENV_UNIVERSAL_1_5; |
| 1120 |
const auto consumer = nullptr; |
| 1121 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 1122 |
spvtools::ValidatorOptions validator_options; |
| 1123 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 1124 |
kConsoleMessageConsumer)); |
| 1125 |
|
| 1126 |
TransformationContext transformation_context( |
| 1127 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 1128 |
|
| 1129 |
auto transformation = TransformationFlattenConditionalBranch( |
| 1130 |
7, true, 0, 0, 0, |
| 1131 |
{MakeSideEffectWrapperInfo(MakeInstructionDescriptor(522, SpvOpLoad, 0), |
| 1132 |
200, 201, 202, 203, 204, 5), |
| 1133 |
MakeSideEffectWrapperInfo(MakeInstructionDescriptor(466, SpvOpLoad, 0), |
| 1134 |
300, 301, 302, 303, 304, 5)}); |
| 1135 |
ASSERT_TRUE( |
| 1136 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 1137 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 1138 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 1139 |
kConsoleMessageConsumer)); |
| 1140 |
|
| 1141 |
std::string after_transformation = R"( |
| 1142 |
OpCapability Shader |
| 1143 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1144 |
OpMemoryModel Logical GLSL450 |
| 1145 |
OpEntryPoint Fragment %2 "main" |
| 1146 |
OpExecutionMode %2 OriginUpperLeft |
| 1147 |
OpSource ESSL 310 |
| 1148 |
%3 = OpTypeVoid |
| 1149 |
%4 = OpTypeBool |
| 1150 |
%5 = OpConstantTrue %4 |
| 1151 |
%10 = OpConstantFalse %4 |
| 1152 |
%6 = OpTypeFunction %3 |
| 1153 |
%100 = OpTypePointer Function %4 |
| 1154 |
%2 = OpFunction %3 None %6 |
| 1155 |
%7 = OpLabel |
| 1156 |
%101 = OpVariable %100 Function |
| 1157 |
%102 = OpVariable %100 Function |
| 1158 |
OpBranch %454 |
| 1159 |
%454 = OpLabel |
| 1160 |
OpSelectionMerge %200 None |
| 1161 |
OpBranchConditional %5 %201 %203 |
| 1162 |
%201 = OpLabel |
| 1163 |
%202 = OpLoad %4 %101 |
| 1164 |
OpBranch %200 |
| 1165 |
%203 = OpLabel |
| 1166 |
%204 = OpCopyObject %4 %5 |
| 1167 |
OpBranch %200 |
| 1168 |
%200 = OpLabel |
| 1169 |
%522 = OpPhi %4 %202 %201 %204 %203 |
| 1170 |
OpBranch %462 |
| 1171 |
%462 = OpLabel |
| 1172 |
OpSelectionMerge %300 None |
| 1173 |
OpBranchConditional %5 %303 %301 |
| 1174 |
%301 = OpLabel |
| 1175 |
%302 = OpLoad %4 %102 |
| 1176 |
OpBranch %300 |
| 1177 |
%303 = OpLabel |
| 1178 |
%304 = OpCopyObject %4 %5 |
| 1179 |
OpBranch %300 |
| 1180 |
%300 = OpLabel |
| 1181 |
%466 = OpPhi %4 %302 %301 %304 %303 |
| 1182 |
OpBranch %470 |
| 1183 |
%470 = OpLabel |
| 1184 |
%534 = OpSelect %4 %5 %522 %466 |
| 1185 |
OpReturn |
| 1186 |
OpFunctionEnd |
| 1187 |
)"; |
| 1188 |
ASSERT_TRUE(IsEqual(env, after_transformation, context.get())); |
| 1189 |
} |
| 1190 |
|
| 1191 |
TEST(TransformationFlattenConditionalBranchTest, |
| 1192 |
LoadFromBufferBlockDecoratedStruct) { |
| 1193 |
std::string shader = R"( |
| 1194 |
OpCapability Shader |
| 1195 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1196 |
OpMemoryModel Logical GLSL450 |
| 1197 |
OpEntryPoint Fragment %4 "main" |
| 1198 |
OpExecutionMode %4 OriginUpperLeft |
| 1199 |
OpSource ESSL 320 |
| 1200 |
OpMemberDecorate %11 0 Offset 0 |
| 1201 |
OpDecorate %11 BufferBlock |
| 1202 |
OpDecorate %13 DescriptorSet 0 |
| 1203 |
OpDecorate %13 Binding 0 |
| 1204 |
%2 = OpTypeVoid |
| 1205 |
%3 = OpTypeFunction %2 |
| 1206 |
%6 = OpTypeBool |
| 1207 |
%7 = OpConstantTrue %6 |
| 1208 |
%10 = OpTypeInt 32 1 |
| 1209 |
%11 = OpTypeStruct %10 |
| 1210 |
%12 = OpTypePointer Uniform %11 |
| 1211 |
%13 = OpVariable %12 Uniform |
| 1212 |
%21 = OpUndef %11 |
| 1213 |
%4 = OpFunction %2 None %3 |
| 1214 |
%5 = OpLabel |
| 1215 |
OpSelectionMerge %9 None |
| 1216 |
OpBranchConditional %7 %8 %9 |
| 1217 |
%8 = OpLabel |
| 1218 |
%20 = OpLoad %11 %13 |
| 1219 |
OpBranch %9 |
| 1220 |
%9 = OpLabel |
| 1221 |
OpReturn |
| 1222 |
OpFunctionEnd |
| 1223 |
)"; |
| 1224 |
|
| 1225 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 1226 |
const auto consumer = nullptr; |
| 1227 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 1228 |
spvtools::ValidatorOptions validator_options; |
| 1229 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 1230 |
kConsoleMessageConsumer)); |
| 1231 |
TransformationContext transformation_context( |
| 1232 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 1233 |
|
| 1234 |
auto transformation = TransformationFlattenConditionalBranch( |
| 1235 |
5, true, 0, 0, 0, |
| 1236 |
{MakeSideEffectWrapperInfo(MakeInstructionDescriptor(20, SpvOpLoad, 0), |
| 1237 |
100, 101, 102, 103, 104, 21)}); |
| 1238 |
ASSERT_TRUE( |
| 1239 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 1240 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 1241 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 1242 |
kConsoleMessageConsumer)); |
| 1243 |
} |
| 1244 |
|
| 1245 |
TEST(TransformationFlattenConditionalBranchTest, InapplicableSampledImageLoad) { |
| 1246 |
std::string shader = R"( |
| 1247 |
OpCapability Shader |
| 1248 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1249 |
OpMemoryModel Logical GLSL450 |
| 1250 |
OpEntryPoint Fragment %4 "main" %12 %96 |
| 1251 |
OpExecutionMode %4 OriginUpperLeft |
| 1252 |
OpSource ESSL 320 |
| 1253 |
OpDecorate %12 BuiltIn FragCoord |
| 1254 |
OpDecorate %91 DescriptorSet 0 |
| 1255 |
OpDecorate %91 Binding 0 |
| 1256 |
OpDecorate %96 Location 0 |
| 1257 |
%2 = OpTypeVoid |
| 1258 |
%3 = OpTypeFunction %2 |
| 1259 |
%6 = OpTypeFloat 32 |
| 1260 |
%7 = OpTypeVector %6 2 |
| 1261 |
%10 = OpTypeVector %6 4 |
| 1262 |
%11 = OpTypePointer Input %10 |
| 1263 |
%12 = OpVariable %11 Input |
| 1264 |
%21 = OpConstant %6 2 |
| 1265 |
%24 = OpTypeInt 32 1 |
| 1266 |
%33 = OpTypeBool |
| 1267 |
%35 = OpConstantTrue %33 |
| 1268 |
%88 = OpTypeImage %6 2D 0 0 0 1 Unknown |
| 1269 |
%89 = OpTypeSampledImage %88 |
| 1270 |
%90 = OpTypePointer UniformConstant %89 |
| 1271 |
%91 = OpVariable %90 UniformConstant |
| 1272 |
%95 = OpTypePointer Output %10 |
| 1273 |
%96 = OpVariable %95 Output |
| 1274 |
%200 = OpUndef %89 |
| 1275 |
%4 = OpFunction %2 None %3 |
| 1276 |
%5 = OpLabel |
| 1277 |
OpBranch %28 |
| 1278 |
%28 = OpLabel |
| 1279 |
OpSelectionMerge %38 None |
| 1280 |
OpBranchConditional %35 %32 %37 |
| 1281 |
%32 = OpLabel |
| 1282 |
%40 = OpLoad %89 %91 |
| 1283 |
OpBranch %38 |
| 1284 |
%37 = OpLabel |
| 1285 |
OpBranch %38 |
| 1286 |
%38 = OpLabel |
| 1287 |
OpReturn |
| 1288 |
OpFunctionEnd |
| 1289 |
)"; |
| 1290 |
|
| 1291 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 1292 |
const auto consumer = nullptr; |
| 1293 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 1294 |
spvtools::ValidatorOptions validator_options; |
| 1295 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 1296 |
kConsoleMessageConsumer)); |
| 1297 |
|
| 1298 |
TransformationContext transformation_context( |
| 1299 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 1300 |
|
| 1301 |
ASSERT_FALSE(TransformationFlattenConditionalBranch( |
| 1302 |
28, true, 0, 0, 0, |
| 1303 |
{MakeSideEffectWrapperInfo( |
| 1304 |
MakeInstructionDescriptor(40, SpvOpLoad, 0), 100, 101, |
| 1305 |
102, 103, 104, 200)}) |
| 1306 |
.IsApplicable(context.get(), transformation_context)); |
| 1307 |
} |
| 1308 |
|
| 1309 |
TEST(TransformationFlattenConditionalBranchTest, |
| 1310 |
InapplicablePhiToSelectVector) { |
| 1311 |
std::string shader = R"( |
| 1312 |
OpCapability Shader |
| 1313 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1314 |
OpMemoryModel Logical GLSL450 |
| 1315 |
OpEntryPoint Fragment %4 "main" |
| 1316 |
OpExecutionMode %4 OriginUpperLeft |
| 1317 |
OpSource ESSL 320 |
| 1318 |
%2 = OpTypeVoid |
| 1319 |
%3 = OpTypeFunction %2 |
| 1320 |
%6 = OpTypeBool |
| 1321 |
%7 = OpConstantTrue %6 |
| 1322 |
%10 = OpTypeInt 32 1 |
| 1323 |
%11 = OpTypeVector %10 3 |
| 1324 |
%12 = OpUndef %11 |
| 1325 |
%4 = OpFunction %2 None %3 |
| 1326 |
%5 = OpLabel |
| 1327 |
OpSelectionMerge %20 None |
| 1328 |
OpBranchConditional %7 %8 %9 |
| 1329 |
%8 = OpLabel |
| 1330 |
OpBranch %20 |
| 1331 |
%9 = OpLabel |
| 1332 |
OpBranch %20 |
| 1333 |
%20 = OpLabel |
| 1334 |
%21 = OpPhi %11 %12 %8 %12 %9 |
| 1335 |
OpReturn |
| 1336 |
OpFunctionEnd |
| 1337 |
)"; |
| 1338 |
|
| 1339 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 1340 |
const auto consumer = nullptr; |
| 1341 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 1342 |
spvtools::ValidatorOptions validator_options; |
| 1343 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 1344 |
kConsoleMessageConsumer)); |
| 1345 |
|
| 1346 |
TransformationContext transformation_context( |
| 1347 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 1348 |
|
| 1349 |
auto transformation = |
| 1350 |
TransformationFlattenConditionalBranch(5, true, 0, 0, 0, {}); |
| 1351 |
ASSERT_FALSE( |
| 1352 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 1353 |
} |
| 1354 |
|
| 1355 |
TEST(TransformationFlattenConditionalBranchTest, |
| 1356 |
InapplicablePhiToSelectVector2) { |
| 1357 |
std::string shader = R"( |
| 1358 |
OpCapability Shader |
| 1359 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1360 |
OpMemoryModel Logical GLSL450 |
| 1361 |
OpEntryPoint Fragment %4 "main" |
| 1362 |
OpExecutionMode %4 OriginUpperLeft |
| 1363 |
OpSource ESSL 320 |
| 1364 |
%2 = OpTypeVoid |
| 1365 |
%3 = OpTypeFunction %2 |
| 1366 |
%6 = OpTypeBool |
| 1367 |
%30 = OpTypeVector %6 3 |
| 1368 |
%31 = OpTypeVector %6 2 |
| 1369 |
%7 = OpConstantTrue %6 |
| 1370 |
%10 = OpTypeInt 32 1 |
| 1371 |
%11 = OpTypeVector %10 3 |
| 1372 |
%40 = OpTypeFloat 32 |
| 1373 |
%41 = OpTypeVector %40 4 |
| 1374 |
%12 = OpUndef %11 |
| 1375 |
%60 = OpUndef %41 |
| 1376 |
%61 = OpConstantComposite %31 %7 %7 |
| 1377 |
%4 = OpFunction %2 None %3 |
| 1378 |
%5 = OpLabel |
| 1379 |
OpSelectionMerge %20 None |
| 1380 |
OpBranchConditional %7 %8 %9 |
| 1381 |
%8 = OpLabel |
| 1382 |
OpBranch %20 |
| 1383 |
%9 = OpLabel |
| 1384 |
OpBranch %20 |
| 1385 |
%20 = OpLabel |
| 1386 |
%21 = OpPhi %11 %12 %8 %12 %9 |
| 1387 |
%22 = OpPhi %11 %12 %8 %12 %9 |
| 1388 |
%23 = OpPhi %41 %60 %8 %60 %9 |
| 1389 |
%24 = OpPhi %31 %61 %8 %61 %9 |
| 1390 |
%25 = OpPhi %41 %60 %8 %60 %9 |
| 1391 |
OpReturn |
| 1392 |
OpFunctionEnd |
| 1393 |
)"; |
| 1394 |
|
| 1395 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 1396 |
const auto consumer = nullptr; |
| 1397 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 1398 |
spvtools::ValidatorOptions validator_options; |
| 1399 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 1400 |
kConsoleMessageConsumer)); |
| 1401 |
|
| 1402 |
TransformationContext transformation_context( |
| 1403 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 1404 |
|
| 1405 |
auto transformation = |
| 1406 |
TransformationFlattenConditionalBranch(5, true, 101, 102, 103, {}); |
| 1407 |
|
| 1408 |
// bvec4 is not present in the module. |
| 1409 |
ASSERT_FALSE( |
| 1410 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 1411 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 1412 |
} |
| 1413 |
|
| 1414 |
TEST(TransformationFlattenConditionalBranchTest, |
| 1415 |
InapplicablePhiToSelectMatrix) { |
| 1416 |
std::string shader = R"( |
| 1417 |
OpCapability Shader |
| 1418 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1419 |
OpMemoryModel Logical GLSL450 |
| 1420 |
OpEntryPoint Fragment %4 "main" |
| 1421 |
OpExecutionMode %4 OriginUpperLeft |
| 1422 |
OpSource ESSL 320 |
| 1423 |
%2 = OpTypeVoid |
| 1424 |
%3 = OpTypeFunction %2 |
| 1425 |
%6 = OpTypeBool |
| 1426 |
%7 = OpConstantTrue %6 |
| 1427 |
%10 = OpTypeFloat 32 |
| 1428 |
%30 = OpTypeVector %10 3 |
| 1429 |
%11 = OpTypeMatrix %30 3 |
| 1430 |
%12 = OpUndef %11 |
| 1431 |
%4 = OpFunction %2 None %3 |
| 1432 |
%5 = OpLabel |
| 1433 |
OpSelectionMerge %20 None |
| 1434 |
OpBranchConditional %7 %8 %9 |
| 1435 |
%8 = OpLabel |
| 1436 |
OpBranch %20 |
| 1437 |
%9 = OpLabel |
| 1438 |
OpBranch %20 |
| 1439 |
%20 = OpLabel |
| 1440 |
%21 = OpPhi %11 %12 %8 %12 %9 |
| 1441 |
OpReturn |
| 1442 |
OpFunctionEnd |
| 1443 |
)"; |
| 1444 |
|
| 1445 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 1446 |
const auto consumer = nullptr; |
| 1447 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 1448 |
spvtools::ValidatorOptions validator_options; |
| 1449 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 1450 |
kConsoleMessageConsumer)); |
| 1451 |
|
| 1452 |
TransformationContext transformation_context( |
| 1453 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 1454 |
|
| 1455 |
auto transformation = |
| 1456 |
TransformationFlattenConditionalBranch(5, true, 0, 0, 0, {}); |
| 1457 |
ASSERT_FALSE( |
| 1458 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 1459 |
} |
| 1460 |
|
| 1461 |
TEST(TransformationFlattenConditionalBranchTest, ApplicablePhiToSelectVector) { |
| 1462 |
std::string shader = R"( |
| 1463 |
OpCapability Shader |
| 1464 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1465 |
OpMemoryModel Logical GLSL450 |
| 1466 |
OpEntryPoint Fragment %4 "main" |
| 1467 |
OpExecutionMode %4 OriginUpperLeft |
| 1468 |
OpSource ESSL 320 |
| 1469 |
%2 = OpTypeVoid |
| 1470 |
%3 = OpTypeFunction %2 |
| 1471 |
%6 = OpTypeBool |
| 1472 |
%7 = OpConstantTrue %6 |
| 1473 |
%10 = OpTypeInt 32 1 |
| 1474 |
%11 = OpTypeVector %10 3 |
| 1475 |
%12 = OpUndef %11 |
| 1476 |
%4 = OpFunction %2 None %3 |
| 1477 |
%5 = OpLabel |
| 1478 |
OpSelectionMerge %20 None |
| 1479 |
OpBranchConditional %7 %8 %9 |
| 1480 |
%8 = OpLabel |
| 1481 |
OpBranch %20 |
| 1482 |
%9 = OpLabel |
| 1483 |
OpBranch %20 |
| 1484 |
%20 = OpLabel |
| 1485 |
%21 = OpPhi %11 %12 %8 %12 %9 |
| 1486 |
OpReturn |
| 1487 |
OpFunctionEnd |
| 1488 |
)"; |
| 1489 |
|
| 1490 |
const auto env = SPV_ENV_UNIVERSAL_1_5; |
| 1491 |
const auto consumer = nullptr; |
| 1492 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 1493 |
spvtools::ValidatorOptions validator_options; |
| 1494 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 1495 |
kConsoleMessageConsumer)); |
| 1496 |
|
| 1497 |
TransformationContext transformation_context( |
| 1498 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 1499 |
|
| 1500 |
auto transformation = |
| 1501 |
TransformationFlattenConditionalBranch(5, true, 0, 0, 0, {}); |
| 1502 |
ASSERT_TRUE( |
| 1503 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 1504 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 1505 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 1506 |
kConsoleMessageConsumer)); |
| 1507 |
|
| 1508 |
std::string expected_shader = R"( |
| 1509 |
OpCapability Shader |
| 1510 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1511 |
OpMemoryModel Logical GLSL450 |
| 1512 |
OpEntryPoint Fragment %4 "main" |
| 1513 |
OpExecutionMode %4 OriginUpperLeft |
| 1514 |
OpSource ESSL 320 |
| 1515 |
%2 = OpTypeVoid |
| 1516 |
%3 = OpTypeFunction %2 |
| 1517 |
%6 = OpTypeBool |
| 1518 |
%7 = OpConstantTrue %6 |
| 1519 |
%10 = OpTypeInt 32 1 |
| 1520 |
%11 = OpTypeVector %10 3 |
| 1521 |
%12 = OpUndef %11 |
| 1522 |
%4 = OpFunction %2 None %3 |
| 1523 |
%5 = OpLabel |
| 1524 |
OpBranch %8 |
| 1525 |
%8 = OpLabel |
| 1526 |
OpBranch %9 |
| 1527 |
%9 = OpLabel |
| 1528 |
OpBranch %20 |
| 1529 |
%20 = OpLabel |
| 1530 |
%21 = OpSelect %11 %7 %12 %12 |
| 1531 |
OpReturn |
| 1532 |
OpFunctionEnd |
| 1533 |
)"; |
| 1534 |
ASSERT_TRUE(IsEqual(env, expected_shader, context.get())); |
| 1535 |
} |
| 1536 |
|
| 1537 |
TEST(TransformationFlattenConditionalBranchTest, ApplicablePhiToSelectVector2) { |
| 1538 |
std::string shader = R"( |
| 1539 |
OpCapability Shader |
| 1540 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1541 |
OpMemoryModel Logical GLSL450 |
| 1542 |
OpEntryPoint Fragment %4 "main" |
| 1543 |
OpExecutionMode %4 OriginUpperLeft |
| 1544 |
OpSource ESSL 320 |
| 1545 |
%2 = OpTypeVoid |
| 1546 |
%3 = OpTypeFunction %2 |
| 1547 |
%6 = OpTypeBool |
| 1548 |
%30 = OpTypeVector %6 3 |
| 1549 |
%31 = OpTypeVector %6 2 |
| 1550 |
%32 = OpTypeVector %6 4 |
| 1551 |
%7 = OpConstantTrue %6 |
| 1552 |
%10 = OpTypeInt 32 1 |
| 1553 |
%11 = OpTypeVector %10 3 |
| 1554 |
%40 = OpTypeFloat 32 |
| 1555 |
%41 = OpTypeVector %40 4 |
| 1556 |
%12 = OpUndef %11 |
| 1557 |
%60 = OpUndef %41 |
| 1558 |
%61 = OpConstantComposite %31 %7 %7 |
| 1559 |
%4 = OpFunction %2 None %3 |
| 1560 |
%5 = OpLabel |
| 1561 |
OpSelectionMerge %20 None |
| 1562 |
OpBranchConditional %7 %8 %9 |
| 1563 |
%8 = OpLabel |
| 1564 |
OpBranch %20 |
| 1565 |
%9 = OpLabel |
| 1566 |
OpBranch %20 |
| 1567 |
%20 = OpLabel |
| 1568 |
%21 = OpPhi %11 %12 %8 %12 %9 |
| 1569 |
%22 = OpPhi %11 %12 %8 %12 %9 |
| 1570 |
%23 = OpPhi %41 %60 %8 %60 %9 |
| 1571 |
%24 = OpPhi %31 %61 %8 %61 %9 |
| 1572 |
%25 = OpPhi %41 %60 %8 %60 %9 |
| 1573 |
OpReturn |
| 1574 |
OpFunctionEnd |
| 1575 |
)"; |
| 1576 |
|
| 1577 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 1578 |
const auto consumer = nullptr; |
| 1579 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 1580 |
spvtools::ValidatorOptions validator_options; |
| 1581 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 1582 |
kConsoleMessageConsumer)); |
| 1583 |
|
| 1584 |
TransformationContext transformation_context( |
| 1585 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 1586 |
|
| 1587 |
// No id for the 2D vector case is provided. |
| 1588 |
ASSERT_FALSE(TransformationFlattenConditionalBranch(5, true, 0, 102, 103, {}) |
| 1589 |
.IsApplicable(context.get(), transformation_context)); |
| 1590 |
|
| 1591 |
// No id for the 3D vector case is provided. |
| 1592 |
ASSERT_FALSE(TransformationFlattenConditionalBranch(5, true, 101, 0, 103, {}) |
| 1593 |
.IsApplicable(context.get(), transformation_context)); |
| 1594 |
|
| 1595 |
// No id for the 4D vector case is provided. |
| 1596 |
ASSERT_FALSE(TransformationFlattenConditionalBranch(5, true, 101, 102, 0, {}) |
| 1597 |
.IsApplicable(context.get(), transformation_context)); |
| 1598 |
|
| 1599 |
// %10 is not fresh |
| 1600 |
ASSERT_FALSE(TransformationFlattenConditionalBranch(5, true, 10, 102, 103, {}) |
| 1601 |
.IsApplicable(context.get(), transformation_context)); |
| 1602 |
|
| 1603 |
// %10 is not fresh |
| 1604 |
ASSERT_FALSE(TransformationFlattenConditionalBranch(5, true, 101, 10, 103, {}) |
| 1605 |
.IsApplicable(context.get(), transformation_context)); |
| 1606 |
|
| 1607 |
// %10 is not fresh |
| 1608 |
ASSERT_FALSE(TransformationFlattenConditionalBranch(5, true, 101, 102, 10, {}) |
| 1609 |
.IsApplicable(context.get(), transformation_context)); |
| 1610 |
|
| 1611 |
// Duplicate "fresh" ids used for boolean vector constructors |
| 1612 |
ASSERT_FALSE( |
| 1613 |
TransformationFlattenConditionalBranch(5, true, 101, 102, 102, {}) |
| 1614 |
.IsApplicable(context.get(), transformation_context)); |
| 1615 |
|
| 1616 |
auto transformation = |
| 1617 |
TransformationFlattenConditionalBranch(5, true, 101, 102, 103, {}); |
| 1618 |
|
| 1619 |
ASSERT_TRUE( |
| 1620 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 1621 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 1622 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 1623 |
kConsoleMessageConsumer)); |
| 1624 |
|
| 1625 |
std::string expected_shader = R"( |
| 1626 |
OpCapability Shader |
| 1627 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1628 |
OpMemoryModel Logical GLSL450 |
| 1629 |
OpEntryPoint Fragment %4 "main" |
| 1630 |
OpExecutionMode %4 OriginUpperLeft |
| 1631 |
OpSource ESSL 320 |
| 1632 |
%2 = OpTypeVoid |
| 1633 |
%3 = OpTypeFunction %2 |
| 1634 |
%6 = OpTypeBool |
| 1635 |
%30 = OpTypeVector %6 3 |
| 1636 |
%31 = OpTypeVector %6 2 |
| 1637 |
%32 = OpTypeVector %6 4 |
| 1638 |
%7 = OpConstantTrue %6 |
| 1639 |
%10 = OpTypeInt 32 1 |
| 1640 |
%11 = OpTypeVector %10 3 |
| 1641 |
%40 = OpTypeFloat 32 |
| 1642 |
%41 = OpTypeVector %40 4 |
| 1643 |
%12 = OpUndef %11 |
| 1644 |
%60 = OpUndef %41 |
| 1645 |
%61 = OpConstantComposite %31 %7 %7 |
| 1646 |
%4 = OpFunction %2 None %3 |
| 1647 |
%5 = OpLabel |
| 1648 |
OpBranch %8 |
| 1649 |
%8 = OpLabel |
| 1650 |
OpBranch %9 |
| 1651 |
%9 = OpLabel |
| 1652 |
OpBranch %20 |
| 1653 |
%20 = OpLabel |
| 1654 |
%103 = OpCompositeConstruct %32 %7 %7 %7 %7 |
| 1655 |
%102 = OpCompositeConstruct %30 %7 %7 %7 |
| 1656 |
%101 = OpCompositeConstruct %31 %7 %7 |
| 1657 |
%21 = OpSelect %11 %102 %12 %12 |
| 1658 |
%22 = OpSelect %11 %102 %12 %12 |
| 1659 |
%23 = OpSelect %41 %103 %60 %60 |
| 1660 |
%24 = OpSelect %31 %101 %61 %61 |
| 1661 |
%25 = OpSelect %41 %103 %60 %60 |
| 1662 |
OpReturn |
| 1663 |
OpFunctionEnd |
| 1664 |
)"; |
| 1665 |
|
| 1666 |
ASSERT_TRUE(IsEqual(env, expected_shader, context.get())); |
| 1667 |
} |
| 1668 |
|
| 1669 |
TEST(TransformationFlattenConditionalBranchTest, ApplicablePhiToSelectVector3) { |
| 1670 |
std::string shader = R"( |
| 1671 |
OpCapability Shader |
| 1672 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1673 |
OpMemoryModel Logical GLSL450 |
| 1674 |
OpEntryPoint Fragment %4 "main" |
| 1675 |
OpExecutionMode %4 OriginUpperLeft |
| 1676 |
OpSource ESSL 320 |
| 1677 |
%2 = OpTypeVoid |
| 1678 |
%3 = OpTypeFunction %2 |
| 1679 |
%6 = OpTypeBool |
| 1680 |
%30 = OpTypeVector %6 3 |
| 1681 |
%31 = OpTypeVector %6 2 |
| 1682 |
%32 = OpTypeVector %6 4 |
| 1683 |
%7 = OpConstantTrue %6 |
| 1684 |
%10 = OpTypeInt 32 1 |
| 1685 |
%11 = OpTypeVector %10 3 |
| 1686 |
%40 = OpTypeFloat 32 |
| 1687 |
%41 = OpTypeVector %40 4 |
| 1688 |
%12 = OpUndef %11 |
| 1689 |
%60 = OpUndef %41 |
| 1690 |
%61 = OpConstantComposite %31 %7 %7 |
| 1691 |
%4 = OpFunction %2 None %3 |
| 1692 |
%5 = OpLabel |
| 1693 |
OpSelectionMerge %20 None |
| 1694 |
OpBranchConditional %7 %8 %9 |
| 1695 |
%8 = OpLabel |
| 1696 |
OpBranch %20 |
| 1697 |
%9 = OpLabel |
| 1698 |
OpBranch %20 |
| 1699 |
%20 = OpLabel |
| 1700 |
%21 = OpPhi %11 %12 %8 %12 %9 |
| 1701 |
%22 = OpPhi %11 %12 %8 %12 %9 |
| 1702 |
%23 = OpPhi %41 %60 %8 %60 %9 |
| 1703 |
%24 = OpPhi %31 %61 %8 %61 %9 |
| 1704 |
%25 = OpPhi %41 %60 %8 %60 %9 |
| 1705 |
OpReturn |
| 1706 |
OpFunctionEnd |
| 1707 |
)"; |
| 1708 |
|
| 1709 |
const auto env = SPV_ENV_UNIVERSAL_1_5; |
| 1710 |
const auto consumer = nullptr; |
| 1711 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 1712 |
spvtools::ValidatorOptions validator_options; |
| 1713 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 1714 |
kConsoleMessageConsumer)); |
| 1715 |
|
| 1716 |
TransformationContext transformation_context( |
| 1717 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 1718 |
|
| 1719 |
auto transformation = |
| 1720 |
TransformationFlattenConditionalBranch(5, true, 101, 0, 103, {}); |
| 1721 |
|
| 1722 |
ASSERT_TRUE( |
| 1723 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 1724 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 1725 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 1726 |
kConsoleMessageConsumer)); |
| 1727 |
|
| 1728 |
// Check that the in operands of any OpSelect instructions all have the |
| 1729 |
// appropriate operand type. |
| 1730 |
context->module()->ForEachInst([](opt::Instruction* inst) { |
| 1731 |
if (inst->opcode() == SpvOpSelect) { |
| 1732 |
ASSERT_EQ(SPV_OPERAND_TYPE_ID, inst->GetInOperand(0).type); |
| 1733 |
ASSERT_EQ(SPV_OPERAND_TYPE_ID, inst->GetInOperand(1).type); |
| 1734 |
ASSERT_EQ(SPV_OPERAND_TYPE_ID, inst->GetInOperand(2).type); |
| 1735 |
} |
| 1736 |
}); |
| 1737 |
|
| 1738 |
std::string expected_shader = R"( |
| 1739 |
OpCapability Shader |
| 1740 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1741 |
OpMemoryModel Logical GLSL450 |
| 1742 |
OpEntryPoint Fragment %4 "main" |
| 1743 |
OpExecutionMode %4 OriginUpperLeft |
| 1744 |
OpSource ESSL 320 |
| 1745 |
%2 = OpTypeVoid |
| 1746 |
%3 = OpTypeFunction %2 |
| 1747 |
%6 = OpTypeBool |
| 1748 |
%30 = OpTypeVector %6 3 |
| 1749 |
%31 = OpTypeVector %6 2 |
| 1750 |
%32 = OpTypeVector %6 4 |
| 1751 |
%7 = OpConstantTrue %6 |
| 1752 |
%10 = OpTypeInt 32 1 |
| 1753 |
%11 = OpTypeVector %10 3 |
| 1754 |
%40 = OpTypeFloat 32 |
| 1755 |
%41 = OpTypeVector %40 4 |
| 1756 |
%12 = OpUndef %11 |
| 1757 |
%60 = OpUndef %41 |
| 1758 |
%61 = OpConstantComposite %31 %7 %7 |
| 1759 |
%4 = OpFunction %2 None %3 |
| 1760 |
%5 = OpLabel |
| 1761 |
OpBranch %8 |
| 1762 |
%8 = OpLabel |
| 1763 |
OpBranch %9 |
| 1764 |
%9 = OpLabel |
| 1765 |
OpBranch %20 |
| 1766 |
%20 = OpLabel |
| 1767 |
%103 = OpCompositeConstruct %32 %7 %7 %7 %7 |
| 1768 |
%101 = OpCompositeConstruct %31 %7 %7 |
| 1769 |
%21 = OpSelect %11 %7 %12 %12 |
| 1770 |
%22 = OpSelect %11 %7 %12 %12 |
| 1771 |
%23 = OpSelect %41 %103 %60 %60 |
| 1772 |
%24 = OpSelect %31 %101 %61 %61 |
| 1773 |
%25 = OpSelect %41 %103 %60 %60 |
| 1774 |
OpReturn |
| 1775 |
OpFunctionEnd |
| 1776 |
)"; |
| 1777 |
|
| 1778 |
ASSERT_TRUE(IsEqual(env, expected_shader, context.get())); |
| 1779 |
} |
| 1780 |
|
| 1781 |
TEST(TransformationFlattenConditionalBranchTest, ApplicablePhiToSelectMatrix) { |
| 1782 |
std::string shader = R"( |
| 1783 |
OpCapability Shader |
| 1784 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1785 |
OpMemoryModel Logical GLSL450 |
| 1786 |
OpEntryPoint Fragment %4 "main" |
| 1787 |
OpExecutionMode %4 OriginUpperLeft |
| 1788 |
OpSource ESSL 320 |
| 1789 |
%2 = OpTypeVoid |
| 1790 |
%3 = OpTypeFunction %2 |
| 1791 |
%6 = OpTypeBool |
| 1792 |
%7 = OpConstantTrue %6 |
| 1793 |
%10 = OpTypeFloat 32 |
| 1794 |
%30 = OpTypeVector %10 3 |
| 1795 |
%11 = OpTypeMatrix %30 3 |
| 1796 |
%12 = OpUndef %11 |
| 1797 |
%4 = OpFunction %2 None %3 |
| 1798 |
%5 = OpLabel |
| 1799 |
OpSelectionMerge %20 None |
| 1800 |
OpBranchConditional %7 %8 %9 |
| 1801 |
%8 = OpLabel |
| 1802 |
OpBranch %20 |
| 1803 |
%9 = OpLabel |
| 1804 |
OpBranch %20 |
| 1805 |
%20 = OpLabel |
| 1806 |
%21 = OpPhi %11 %12 %8 %12 %9 |
| 1807 |
OpReturn |
| 1808 |
OpFunctionEnd |
| 1809 |
)"; |
| 1810 |
|
| 1811 |
const auto env = SPV_ENV_UNIVERSAL_1_5; |
| 1812 |
const auto consumer = nullptr; |
| 1813 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 1814 |
spvtools::ValidatorOptions validator_options; |
| 1815 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 1816 |
kConsoleMessageConsumer)); |
| 1817 |
|
| 1818 |
TransformationContext transformation_context( |
| 1819 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 1820 |
|
| 1821 |
auto transformation = |
| 1822 |
TransformationFlattenConditionalBranch(5, true, 0, 0, 0, {}); |
| 1823 |
ASSERT_TRUE( |
| 1824 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 1825 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 1826 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 1827 |
kConsoleMessageConsumer)); |
| 1828 |
|
| 1829 |
std::string expected_shader = R"( |
| 1830 |
OpCapability Shader |
| 1831 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1832 |
OpMemoryModel Logical GLSL450 |
| 1833 |
OpEntryPoint Fragment %4 "main" |
| 1834 |
OpExecutionMode %4 OriginUpperLeft |
| 1835 |
OpSource ESSL 320 |
| 1836 |
%2 = OpTypeVoid |
| 1837 |
%3 = OpTypeFunction %2 |
| 1838 |
%6 = OpTypeBool |
| 1839 |
%7 = OpConstantTrue %6 |
| 1840 |
%10 = OpTypeFloat 32 |
| 1841 |
%30 = OpTypeVector %10 3 |
| 1842 |
%11 = OpTypeMatrix %30 3 |
| 1843 |
%12 = OpUndef %11 |
| 1844 |
%4 = OpFunction %2 None %3 |
| 1845 |
%5 = OpLabel |
| 1846 |
OpBranch %8 |
| 1847 |
%8 = OpLabel |
| 1848 |
OpBranch %9 |
| 1849 |
%9 = OpLabel |
| 1850 |
OpBranch %20 |
| 1851 |
%20 = OpLabel |
| 1852 |
%21 = OpSelect %11 %7 %12 %12 |
| 1853 |
OpReturn |
| 1854 |
OpFunctionEnd |
| 1855 |
)"; |
| 1856 |
ASSERT_TRUE(IsEqual(env, expected_shader, context.get())); |
| 1857 |
} |
| 1858 |
|
| 1859 |
TEST(TransformationFlattenConditionalBranchTest, |
| 1860 |
InapplicableConditionIsIrrelevant) { |
| 1861 |
std::string shader = R"( |
| 1862 |
OpCapability Shader |
| 1863 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1864 |
OpMemoryModel Logical GLSL450 |
| 1865 |
OpEntryPoint Fragment %4 "main" |
| 1866 |
OpExecutionMode %4 OriginUpperLeft |
| 1867 |
OpSource ESSL 320 |
| 1868 |
%2 = OpTypeVoid |
| 1869 |
%3 = OpTypeFunction %2 |
| 1870 |
%6 = OpTypeBool |
| 1871 |
%7 = OpConstantTrue %6 |
| 1872 |
%10 = OpTypeInt 32 1 |
| 1873 |
%4 = OpFunction %2 None %3 |
| 1874 |
%5 = OpLabel |
| 1875 |
OpSelectionMerge %9 None |
| 1876 |
OpBranchConditional %7 %8 %9 |
| 1877 |
%8 = OpLabel |
| 1878 |
OpBranch %9 |
| 1879 |
%9 = OpLabel |
| 1880 |
OpReturn |
| 1881 |
OpFunctionEnd |
| 1882 |
)"; |
| 1883 |
|
| 1884 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 1885 |
const auto consumer = nullptr; |
| 1886 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 1887 |
spvtools::ValidatorOptions validator_options; |
| 1888 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 1889 |
kConsoleMessageConsumer)); |
| 1890 |
TransformationContext transformation_context( |
| 1891 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 1892 |
|
| 1893 |
transformation_context.GetFactManager()->AddFactIdIsIrrelevant(7); |
| 1894 |
|
| 1895 |
// Inapplicable because the branch condition, %7, is irrelevant. |
| 1896 |
ASSERT_FALSE(TransformationFlattenConditionalBranch(5, true, 0, 0, 0, {}) |
| 1897 |
.IsApplicable(context.get(), transformation_context)); |
| 1898 |
} |
| 1899 |
|
| 1900 |
TEST(TransformationFlattenConditionalBranchTest, |
| 1901 |
OpPhiWhenTrueBranchIsConvergenceBlock) { |
| 1902 |
std::string shader = R"( |
| 1903 |
OpCapability Shader |
| 1904 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1905 |
OpMemoryModel Logical GLSL450 |
| 1906 |
OpEntryPoint Fragment %4 "main" |
| 1907 |
OpExecutionMode %4 OriginUpperLeft |
| 1908 |
OpSource ESSL 320 |
| 1909 |
OpName %4 "main" |
| 1910 |
%2 = OpTypeVoid |
| 1911 |
%3 = OpTypeFunction %2 |
| 1912 |
%6 = OpTypeBool |
| 1913 |
%7 = OpConstantTrue %6 |
| 1914 |
%4 = OpFunction %2 None %3 |
| 1915 |
%5 = OpLabel |
| 1916 |
OpSelectionMerge %9 None |
| 1917 |
OpBranchConditional %7 %9 %8 |
| 1918 |
%8 = OpLabel |
| 1919 |
%10 = OpCopyObject %6 %7 |
| 1920 |
OpBranch %9 |
| 1921 |
%9 = OpLabel |
| 1922 |
%11 = OpPhi %6 %10 %8 %7 %5 |
| 1923 |
%12 = OpPhi %6 %7 %5 %10 %8 |
| 1924 |
OpReturn |
| 1925 |
OpFunctionEnd |
| 1926 |
)"; |
| 1927 |
|
| 1928 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 1929 |
const auto consumer = nullptr; |
| 1930 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 1931 |
spvtools::ValidatorOptions validator_options; |
| 1932 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 1933 |
kConsoleMessageConsumer)); |
| 1934 |
TransformationContext transformation_context( |
| 1935 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 1936 |
|
| 1937 |
TransformationFlattenConditionalBranch transformation(5, true, 0, 0, 0, {}); |
| 1938 |
ASSERT_TRUE( |
| 1939 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 1940 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 1941 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 1942 |
kConsoleMessageConsumer)); |
| 1943 |
|
| 1944 |
std::string expected = R"( |
| 1945 |
OpCapability Shader |
| 1946 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1947 |
OpMemoryModel Logical GLSL450 |
| 1948 |
OpEntryPoint Fragment %4 "main" |
| 1949 |
OpExecutionMode %4 OriginUpperLeft |
| 1950 |
OpSource ESSL 320 |
| 1951 |
OpName %4 "main" |
| 1952 |
%2 = OpTypeVoid |
| 1953 |
%3 = OpTypeFunction %2 |
| 1954 |
%6 = OpTypeBool |
| 1955 |
%7 = OpConstantTrue %6 |
| 1956 |
%4 = OpFunction %2 None %3 |
| 1957 |
%5 = OpLabel |
| 1958 |
OpBranch %8 |
| 1959 |
%8 = OpLabel |
| 1960 |
%10 = OpCopyObject %6 %7 |
| 1961 |
OpBranch %9 |
| 1962 |
%9 = OpLabel |
| 1963 |
%11 = OpSelect %6 %7 %7 %10 |
| 1964 |
%12 = OpSelect %6 %7 %7 %10 |
| 1965 |
OpReturn |
| 1966 |
OpFunctionEnd |
| 1967 |
)"; |
| 1968 |
|
| 1969 |
ASSERT_TRUE(IsEqual(env, expected, context.get())); |
| 1970 |
} |
| 1971 |
|
| 1972 |
TEST(TransformationFlattenConditionalBranchTest, |
| 1973 |
OpPhiWhenFalseBranchIsConvergenceBlock) { |
| 1974 |
std::string shader = R"( |
| 1975 |
OpCapability Shader |
| 1976 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1977 |
OpMemoryModel Logical GLSL450 |
| 1978 |
OpEntryPoint Fragment %4 "main" |
| 1979 |
OpExecutionMode %4 OriginUpperLeft |
| 1980 |
OpSource ESSL 320 |
| 1981 |
OpName %4 "main" |
| 1982 |
%2 = OpTypeVoid |
| 1983 |
%3 = OpTypeFunction %2 |
| 1984 |
%6 = OpTypeBool |
| 1985 |
%7 = OpConstantTrue %6 |
| 1986 |
%4 = OpFunction %2 None %3 |
| 1987 |
%5 = OpLabel |
| 1988 |
OpSelectionMerge %9 None |
| 1989 |
OpBranchConditional %7 %8 %9 |
| 1990 |
%8 = OpLabel |
| 1991 |
%10 = OpCopyObject %6 %7 |
| 1992 |
OpBranch %9 |
| 1993 |
%9 = OpLabel |
| 1994 |
%11 = OpPhi %6 %10 %8 %7 %5 |
| 1995 |
%12 = OpPhi %6 %7 %5 %10 %8 |
| 1996 |
OpReturn |
| 1997 |
OpFunctionEnd |
| 1998 |
)"; |
| 1999 |
|
| 2000 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 2001 |
const auto consumer = nullptr; |
| 2002 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 2003 |
spvtools::ValidatorOptions validator_options; |
| 2004 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 2005 |
kConsoleMessageConsumer)); |
| 2006 |
TransformationContext transformation_context( |
| 2007 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 2008 |
|
| 2009 |
TransformationFlattenConditionalBranch transformation(5, true, 0, 0, 0, {}); |
| 2010 |
ASSERT_TRUE( |
| 2011 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 2012 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 2013 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 2014 |
kConsoleMessageConsumer)); |
| 2015 |
|
| 2016 |
std::string expected = R"( |
| 2017 |
OpCapability Shader |
| 2018 |
%1 = OpExtInstImport "GLSL.std.450" |
| 2019 |
OpMemoryModel Logical GLSL450 |
| 2020 |
OpEntryPoint Fragment %4 "main" |
| 2021 |
OpExecutionMode %4 OriginUpperLeft |
| 2022 |
OpSource ESSL 320 |
| 2023 |
OpName %4 "main" |
| 2024 |
%2 = OpTypeVoid |
| 2025 |
%3 = OpTypeFunction %2 |
| 2026 |
%6 = OpTypeBool |
| 2027 |
%7 = OpConstantTrue %6 |
| 2028 |
%4 = OpFunction %2 None %3 |
| 2029 |
%5 = OpLabel |
| 2030 |
OpBranch %8 |
| 2031 |
%8 = OpLabel |
| 2032 |
%10 = OpCopyObject %6 %7 |
| 2033 |
OpBranch %9 |
| 2034 |
%9 = OpLabel |
| 2035 |
%11 = OpSelect %6 %7 %10 %7 |
| 2036 |
%12 = OpSelect %6 %7 %10 %7 |
| 2037 |
OpReturn |
| 2038 |
OpFunctionEnd |
| 2039 |
)"; |
| 2040 |
|
| 2041 |
ASSERT_TRUE(IsEqual(env, expected, context.get())); |
| 2042 |
} |
| 2043 |
|
| 2044 |
TEST(TransformationFlattenConditionalBranchTest, ContainsDeadBlocksTest) { |
| 2045 |
std::string shader = R"( |
| 2046 |
OpCapability Shader |
| 2047 |
%1 = OpExtInstImport "GLSL.std.450" |
| 2048 |
OpMemoryModel Logical GLSL450 |
| 2049 |
OpEntryPoint Fragment %4 "main" |
| 2050 |
OpExecutionMode %4 OriginUpperLeft |
| 2051 |
OpSource ESSL 320 |
| 2052 |
OpName %4 "main" |
| 2053 |
%2 = OpTypeVoid |
| 2054 |
%3 = OpTypeFunction %2 |
| 2055 |
%6 = OpTypeBool |
| 2056 |
%7 = OpConstantFalse %6 |
| 2057 |
%4 = OpFunction %2 None %3 |
| 2058 |
%5 = OpLabel |
| 2059 |
OpSelectionMerge %9 None |
| 2060 |
OpBranchConditional %7 %8 %9 |
| 2061 |
%8 = OpLabel |
| 2062 |
%10 = OpCopyObject %6 %7 |
| 2063 |
OpBranch %9 |
| 2064 |
%9 = OpLabel |
| 2065 |
%11 = OpPhi %6 %10 %8 %7 %5 |
| 2066 |
%12 = OpPhi %6 %7 %5 %10 %8 |
| 2067 |
OpReturn |
| 2068 |
OpFunctionEnd |
| 2069 |
)"; |
| 2070 |
|
| 2071 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 2072 |
const auto consumer = nullptr; |
| 2073 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 2074 |
spvtools::ValidatorOptions validator_options; |
| 2075 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 2076 |
kConsoleMessageConsumer)); |
| 2077 |
TransformationContext transformation_context( |
| 2078 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 2079 |
|
| 2080 |
TransformationFlattenConditionalBranch transformation(5, true, 0, 0, 0, {}); |
| 2081 |
ASSERT_TRUE( |
| 2082 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 2083 |
|
| 2084 |
transformation_context.GetFactManager()->AddFactBlockIsDead(8); |
| 2085 |
|
| 2086 |
ASSERT_FALSE( |
| 2087 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 2088 |
} |
| 2089 |
|
| 2090 |
TEST(TransformationFlattenConditionalBranchTest, ContainsContinueBlockTest) { |
| 2091 |
std::string shader = R"( |
| 2092 |
OpCapability Shader |
| 2093 |
%1 = OpExtInstImport "GLSL.std.450" |
| 2094 |
OpMemoryModel Logical GLSL450 |
| 2095 |
OpEntryPoint Fragment %4 "main" |
| 2096 |
OpExecutionMode %4 OriginUpperLeft |
| 2097 |
OpSource ESSL 320 |
| 2098 |
OpName %4 "main" |
| 2099 |
%2 = OpTypeVoid |
| 2100 |
%3 = OpTypeFunction %2 |
| 2101 |
%6 = OpTypeBool |
| 2102 |
%7 = OpConstantFalse %6 |
| 2103 |
%4 = OpFunction %2 None %3 |
| 2104 |
%12 = OpLabel |
| 2105 |
OpBranch %13 |
| 2106 |
%13 = OpLabel |
| 2107 |
OpLoopMerge %15 %14 None |
| 2108 |
OpBranchConditional %7 %5 %15 |
| 2109 |
%5 = OpLabel |
| 2110 |
OpSelectionMerge %11 None |
| 2111 |
OpBranchConditional %7 %9 %10 |
| 2112 |
%9 = OpLabel |
| 2113 |
OpBranch %11 |
| 2114 |
%10 = OpLabel |
| 2115 |
OpBranch %14 |
| 2116 |
%11 = OpLabel |
| 2117 |
OpBranch %14 |
| 2118 |
%14 = OpLabel |
| 2119 |
OpBranch %13 |
| 2120 |
%15 = OpLabel |
| 2121 |
OpReturn |
| 2122 |
OpFunctionEnd |
| 2123 |
)"; |
| 2124 |
|
| 2125 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 2126 |
const auto consumer = nullptr; |
| 2127 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 2128 |
spvtools::ValidatorOptions validator_options; |
| 2129 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 2130 |
kConsoleMessageConsumer)); |
| 2131 |
TransformationContext transformation_context( |
| 2132 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 2133 |
|
| 2134 |
ASSERT_FALSE(TransformationFlattenConditionalBranch(5, true, 0, 0, 0, {}) |
| 2135 |
.IsApplicable(context.get(), transformation_context)); |
| 2136 |
} |
| 2137 |
|
| 2138 |
} // namespace |
| 2139 |
} // namespace fuzz |
| 2140 |
} // namespace spvtools |