| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952 |
- // Copyright (c) 2020 Vasyl Teliman
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- #include "source/fuzz/transformation_propagate_instruction_up.h"
- #include "gtest/gtest.h"
- #include "source/fuzz/fuzzer_util.h"
- #include "test/fuzz/fuzz_test_util.h"
- namespace spvtools {
- namespace fuzz {
- namespace {
- TEST(TransformationPropagateInstructionUpTest, BasicTest) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeFloat 32
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 3.5
- %11 = OpConstant %6 3.4000001
- %12 = OpTypeBool
- %17 = OpConstant %6 4
- %20 = OpConstant %6 45
- %27 = OpTypePointer Function %6
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %26 = OpVariable %27 Function
- %13 = OpFOrdEqual %12 %9 %11
- OpSelectionMerge %15 None
- OpBranchConditional %13 %14 %19
- %14 = OpLabel
- %18 = OpFMod %6 %9 %17
- OpBranch %15
- %19 = OpLabel
- %22 = OpFAdd %6 %11 %20
- OpBranch %15
- %15 = OpLabel
- %21 = OpPhi %6 %18 %14 %22 %19
- %23 = OpFMul %6 %21 %21
- %24 = OpFDiv %6 %21 %23
- OpBranch %25
- %25 = OpLabel
- %28 = OpPhi %6 %20 %15
- OpStore %26 %28
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- // |block_id| is invalid.
- ASSERT_FALSE(TransformationPropagateInstructionUp(40, {{}}).IsApplicable(
- context.get(), transformation_context));
- ASSERT_FALSE(TransformationPropagateInstructionUp(26, {{}}).IsApplicable(
- context.get(), transformation_context));
- // |block_id| has no predecessors.
- ASSERT_FALSE(TransformationPropagateInstructionUp(5, {{}}).IsApplicable(
- context.get(), transformation_context));
- // |block_id| has no valid instructions to propagate.
- ASSERT_FALSE(TransformationPropagateInstructionUp(25, {{{15, 40}}})
- .IsApplicable(context.get(), transformation_context));
- // Not all predecessors have fresh ids.
- ASSERT_FALSE(TransformationPropagateInstructionUp(15, {{{19, 40}, {40, 41}}})
- .IsApplicable(context.get(), transformation_context));
- // Not all ids are fresh.
- ASSERT_FALSE(
- TransformationPropagateInstructionUp(15, {{{19, 40}, {14, 14}, {40, 42}}})
- .IsApplicable(context.get(), transformation_context));
- ASSERT_FALSE(
- TransformationPropagateInstructionUp(15, {{{19, 19}, {14, 40}, {40, 42}}})
- .IsApplicable(context.get(), transformation_context));
- // Fresh ids have duplicates.
- ASSERT_FALSE(
- TransformationPropagateInstructionUp(15, {{{19, 40}, {14, 40}, {19, 41}}})
- .IsApplicable(context.get(), transformation_context));
- // Valid transformations.
- {
- TransformationPropagateInstructionUp transformation(14, {{{5, 40}}});
- ASSERT_TRUE(
- transformation.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation, context.get(),
- &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
- context.get(), validator_options, kConsoleMessageConsumer));
- }
- {
- TransformationPropagateInstructionUp transformation(19, {{{5, 41}}});
- ASSERT_TRUE(
- transformation.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation, context.get(),
- &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
- context.get(), validator_options, kConsoleMessageConsumer));
- }
- std::string after_transformation = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeFloat 32
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 3.5
- %11 = OpConstant %6 3.4000001
- %12 = OpTypeBool
- %17 = OpConstant %6 4
- %20 = OpConstant %6 45
- %27 = OpTypePointer Function %6
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %26 = OpVariable %27 Function
- %13 = OpFOrdEqual %12 %9 %11
- %40 = OpFMod %6 %9 %17 ; propagated from %14
- %41 = OpFAdd %6 %11 %20 ; propagated from %19
- OpSelectionMerge %15 None
- OpBranchConditional %13 %14 %19
- %14 = OpLabel
- %18 = OpPhi %6 %40 %5 ; propagated into %5
- OpBranch %15
- %19 = OpLabel
- %22 = OpPhi %6 %41 %5 ; propagated into %5
- OpBranch %15
- %15 = OpLabel
- %21 = OpPhi %6 %18 %14 %22 %19
- %23 = OpFMul %6 %21 %21
- %24 = OpFDiv %6 %21 %23
- OpBranch %25
- %25 = OpLabel
- %28 = OpPhi %6 %20 %15
- OpStore %26 %28
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
- {
- TransformationPropagateInstructionUp transformation(15,
- {{{14, 43}, {19, 44}}});
- ASSERT_TRUE(
- transformation.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation, context.get(),
- &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
- context.get(), validator_options, kConsoleMessageConsumer));
- }
- after_transformation = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeFloat 32
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 3.5
- %11 = OpConstant %6 3.4000001
- %12 = OpTypeBool
- %17 = OpConstant %6 4
- %20 = OpConstant %6 45
- %27 = OpTypePointer Function %6
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %26 = OpVariable %27 Function
- %13 = OpFOrdEqual %12 %9 %11
- %40 = OpFMod %6 %9 %17
- %41 = OpFAdd %6 %11 %20
- OpSelectionMerge %15 None
- OpBranchConditional %13 %14 %19
- %14 = OpLabel
- %18 = OpPhi %6 %40 %5
- %43 = OpFMul %6 %18 %18 ; propagated from %15
- OpBranch %15
- %19 = OpLabel
- %22 = OpPhi %6 %41 %5
- %44 = OpFMul %6 %22 %22 ; propagated from %15
- OpBranch %15
- %15 = OpLabel
- %23 = OpPhi %6 %43 %14 %44 %19 ; propagated into %14 and %19
- %21 = OpPhi %6 %18 %14 %22 %19
- %24 = OpFDiv %6 %21 %23
- OpBranch %25
- %25 = OpLabel
- %28 = OpPhi %6 %20 %15
- OpStore %26 %28
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
- {
- TransformationPropagateInstructionUp transformation(15,
- {{{14, 45}, {19, 46}}});
- ASSERT_TRUE(
- transformation.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation, context.get(),
- &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
- context.get(), validator_options, kConsoleMessageConsumer));
- }
- after_transformation = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeFloat 32
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 3.5
- %11 = OpConstant %6 3.4000001
- %12 = OpTypeBool
- %17 = OpConstant %6 4
- %20 = OpConstant %6 45
- %27 = OpTypePointer Function %6
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %26 = OpVariable %27 Function
- %13 = OpFOrdEqual %12 %9 %11
- %40 = OpFMod %6 %9 %17
- %41 = OpFAdd %6 %11 %20
- OpSelectionMerge %15 None
- OpBranchConditional %13 %14 %19
- %14 = OpLabel
- %18 = OpPhi %6 %40 %5
- %43 = OpFMul %6 %18 %18
- %45 = OpFDiv %6 %18 %43 ; propagated from %15
- OpBranch %15
- %19 = OpLabel
- %22 = OpPhi %6 %41 %5
- %44 = OpFMul %6 %22 %22
- %46 = OpFDiv %6 %22 %44 ; propagated from %15
- OpBranch %15
- %15 = OpLabel
- %24 = OpPhi %6 %45 %14 %46 %19 ; propagated into %14 and %19
- %23 = OpPhi %6 %43 %14 %44 %19
- %21 = OpPhi %6 %18 %14 %22 %19
- OpBranch %25
- %25 = OpLabel
- %28 = OpPhi %6 %20 %15
- OpStore %26 %28
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
- }
- TEST(TransformationPropagateInstructionUpTest, BlockDominatesPredecessor1) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeFloat 32
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 3.5
- %11 = OpConstant %6 3.4000001
- %12 = OpTypeBool
- %17 = OpConstant %6 4
- %20 = OpConstant %6 45
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %13 = OpFOrdEqual %12 %9 %11
- OpSelectionMerge %15 None
- OpBranchConditional %13 %14 %19
- %14 = OpLabel
- %18 = OpFMod %6 %9 %17
- OpBranch %15
- %19 = OpLabel
- %22 = OpFAdd %6 %11 %20
- OpBranch %15
- %15 = OpLabel ; dominates %26
- %21 = OpPhi %6 %18 %14 %22 %19 %28 %26
- %23 = OpFMul %6 %21 %21
- %24 = OpFDiv %6 %21 %23
- OpLoopMerge %27 %26 None
- OpBranch %26
- %26 = OpLabel
- %28 = OpFAdd %6 %24 %23
- OpBranch %15
- %27 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- TransformationPropagateInstructionUp transformation(
- 15, {{{14, 40}, {19, 41}, {26, 42}}});
- ASSERT_TRUE(
- transformation.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- std::string after_transformation = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeFloat 32
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 3.5
- %11 = OpConstant %6 3.4000001
- %12 = OpTypeBool
- %17 = OpConstant %6 4
- %20 = OpConstant %6 45
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %13 = OpFOrdEqual %12 %9 %11
- OpSelectionMerge %15 None
- OpBranchConditional %13 %14 %19
- %14 = OpLabel
- %18 = OpFMod %6 %9 %17
- %40 = OpFMul %6 %18 %18 ; propagated from %15
- OpBranch %15
- %19 = OpLabel
- %22 = OpFAdd %6 %11 %20
- %41 = OpFMul %6 %22 %22 ; propagated from %15
- OpBranch %15
- %15 = OpLabel
- %23 = OpPhi %6 %40 %14 %41 %19 %42 %26 ; propagated into %14, %19, %26
- %21 = OpPhi %6 %18 %14 %22 %19 %28 %26
- %24 = OpFDiv %6 %21 %23
- OpLoopMerge %27 %26 None
- OpBranch %26
- %26 = OpLabel
- %28 = OpFAdd %6 %24 %23
- %42 = OpFMul %6 %28 %28 ; propagated from %15
- OpBranch %15
- %27 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
- }
- TEST(TransformationPropagateInstructionUpTest, BlockDominatesPredecessor2) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeFloat 32
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 3.5
- %11 = OpConstant %6 3.4000001
- %12 = OpTypeBool
- %17 = OpConstant %6 4
- %20 = OpConstant %6 45
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %13 = OpFOrdEqual %12 %9 %11
- OpSelectionMerge %15 None
- OpBranchConditional %13 %14 %19
- %14 = OpLabel
- %18 = OpFMod %6 %9 %17
- OpBranch %15
- %19 = OpLabel
- %22 = OpFAdd %6 %11 %20
- OpBranch %15
- %15 = OpLabel ; doesn't dominate %26
- %21 = OpPhi %6 %18 %14 %22 %19 %20 %26
- %23 = OpFMul %6 %21 %21
- %24 = OpFDiv %6 %21 %23
- OpLoopMerge %27 %26 None
- OpBranch %27
- %26 = OpLabel
- OpBranch %15
- %27 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- TransformationPropagateInstructionUp transformation(
- 15, {{{14, 40}, {19, 41}, {26, 42}}});
- ASSERT_TRUE(
- transformation.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- std::string after_transformation = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeFloat 32
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 3.5
- %11 = OpConstant %6 3.4000001
- %12 = OpTypeBool
- %17 = OpConstant %6 4
- %20 = OpConstant %6 45
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %13 = OpFOrdEqual %12 %9 %11
- OpSelectionMerge %15 None
- OpBranchConditional %13 %14 %19
- %14 = OpLabel
- %18 = OpFMod %6 %9 %17
- %40 = OpFMul %6 %18 %18 ; propagated from %15
- OpBranch %15
- %19 = OpLabel
- %22 = OpFAdd %6 %11 %20
- %41 = OpFMul %6 %22 %22 ; propagated from %15
- OpBranch %15
- %15 = OpLabel
- %23 = OpPhi %6 %40 %14 %41 %19 %42 %26 ; propagated into %14, %19, %26
- %21 = OpPhi %6 %18 %14 %22 %19 %20 %26
- %24 = OpFDiv %6 %21 %23
- OpLoopMerge %27 %26 None
- OpBranch %27
- %26 = OpLabel
- %42 = OpFMul %6 %20 %20 ; propagated from %15
- OpBranch %15
- %27 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
- }
- TEST(TransformationPropagateInstructionUpTest, BlockDominatesPredecessor3) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeFloat 32
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 3.5
- %11 = OpConstant %6 3.4000001
- %12 = OpTypeBool
- %17 = OpConstant %6 4
- %20 = OpConstant %6 45
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %13 = OpFOrdEqual %12 %9 %11
- OpSelectionMerge %15 None
- OpBranchConditional %13 %14 %19
- %14 = OpLabel
- %18 = OpFMod %6 %9 %17
- OpBranch %15
- %19 = OpLabel
- %22 = OpFAdd %6 %11 %20
- OpBranch %15
- %15 = OpLabel ; branches to itself
- %21 = OpPhi %6 %18 %14 %22 %19 %24 %15
- %23 = OpFMul %6 %21 %21
- %24 = OpFDiv %6 %21 %23
- OpLoopMerge %27 %15 None
- OpBranch %15
- %27 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- TransformationPropagateInstructionUp transformation(
- 15, {{{14, 40}, {19, 41}, {15, 42}}});
- ASSERT_TRUE(
- transformation.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- std::string after_transformation = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeFloat 32
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 3.5
- %11 = OpConstant %6 3.4000001
- %12 = OpTypeBool
- %17 = OpConstant %6 4
- %20 = OpConstant %6 45
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %13 = OpFOrdEqual %12 %9 %11
- OpSelectionMerge %15 None
- OpBranchConditional %13 %14 %19
- %14 = OpLabel
- %18 = OpFMod %6 %9 %17
- %40 = OpFMul %6 %18 %18 ; propagated from %15
- OpBranch %15
- %19 = OpLabel
- %22 = OpFAdd %6 %11 %20
- %41 = OpFMul %6 %22 %22 ; propagated from %15
- OpBranch %15
- %15 = OpLabel
- %23 = OpPhi %6 %40 %14 %41 %19 %42 %15 ; propagated into %14, %19, %15
- %21 = OpPhi %6 %18 %14 %22 %19 %24 %15
- %24 = OpFDiv %6 %21 %23
- %42 = OpFMul %6 %24 %24 ; propagated from %15
- OpLoopMerge %27 %15 None
- OpBranch %15
- %27 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
- }
- TEST(TransformationPropagateInstructionUpTest,
- HandlesVariablePointersCapability) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeFloat 32
- %11 = OpConstant %6 23
- %7 = OpTypePointer Function %6
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- OpBranch %9
- %9 = OpLabel
- %10 = OpCopyObject %7 %8
- OpStore %10 %11
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- // Required capabilities haven't yet been specified.
- TransformationPropagateInstructionUp transformation(9, {{{5, 40}}});
- ASSERT_FALSE(
- transformation.IsApplicable(context.get(), transformation_context));
- context->AddCapability(spv::Capability::VariablePointers);
- ASSERT_TRUE(
- transformation.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- std::string after_transformation = R"(
- OpCapability Shader
- OpCapability VariablePointers
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeFloat 32
- %11 = OpConstant %6 23
- %7 = OpTypePointer Function %6
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %40 = OpCopyObject %7 %8 ; propagated from %9
- OpBranch %9
- %9 = OpLabel
- %10 = OpPhi %7 %40 %5 ; propagated into %5
- OpStore %10 %11
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
- }
- TEST(TransformationPropagateInstructionUpTest,
- HandlesVariablePointersStorageBufferCapability) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeFloat 32
- %11 = OpConstant %6 23
- %7 = OpTypePointer Function %6
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- OpBranch %9
- %9 = OpLabel
- %10 = OpCopyObject %7 %8
- OpStore %10 %11
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- // Required capabilities haven't yet been specified
- TransformationPropagateInstructionUp transformation(9, {{{5, 40}}});
- ASSERT_FALSE(
- transformation.IsApplicable(context.get(), transformation_context));
- context->AddCapability(spv::Capability::VariablePointersStorageBuffer);
- ASSERT_TRUE(
- transformation.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- std::string after_transformation = R"(
- OpCapability Shader
- OpCapability VariablePointersStorageBuffer
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeFloat 32
- %11 = OpConstant %6 23
- %7 = OpTypePointer Function %6
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %40 = OpCopyObject %7 %8 ; propagated from %9
- OpBranch %9
- %9 = OpLabel
- %10 = OpPhi %7 %40 %5 ; propagated into %5
- OpStore %10 %11
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
- }
- TEST(TransformationPropagateInstructionUpTest, MultipleIdenticalPredecessors) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeFloat 32
- %11 = OpConstant %6 23
- %12 = OpTypeBool
- %13 = OpConstantTrue %12
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpSelectionMerge %9 None
- OpBranchConditional %13 %9 %9
- %9 = OpLabel
- %14 = OpPhi %6 %11 %5
- %10 = OpCopyObject %6 %14
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- TransformationPropagateInstructionUp transformation(9, {{{5, 40}}});
- ASSERT_TRUE(
- transformation.IsApplicable(context.get(), transformation_context));
- ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- std::string after_transformation = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeFloat 32
- %11 = OpConstant %6 23
- %12 = OpTypeBool
- %13 = OpConstantTrue %12
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %40 = OpCopyObject %6 %11
- OpSelectionMerge %9 None
- OpBranchConditional %13 %9 %9
- %9 = OpLabel
- %10 = OpPhi %6 %40 %5
- %14 = OpPhi %6 %11 %5
- OpReturn
- OpFunctionEnd
- )";
- ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
- }
- TEST(TransformationPropagateInstructionUpTest,
- InapplicableDueToOpTypeSampledImage) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main" %10
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 320
- OpDecorate %10 RelaxedPrecision
- OpDecorate %10 DescriptorSet 0
- OpDecorate %10 Binding 0
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeFloat 32
- %7 = OpTypeImage %6 2D 0 0 0 1 Unknown
- %8 = OpTypeSampledImage %7
- %9 = OpTypePointer UniformConstant %8
- %10 = OpVariable %9 UniformConstant
- %12 = OpTypeVector %6 2
- %13 = OpConstant %6 0
- %14 = OpConstantComposite %12 %13 %13
- %15 = OpTypeVector %6 4
- %30 = OpTypeBool
- %31 = OpConstantTrue %30
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %11 = OpLoad %8 %10
- OpSelectionMerge %20 None
- OpBranchConditional %31 %40 %41
- %40 = OpLabel
- OpBranch %20
- %41 = OpLabel
- OpBranch %20
- %20 = OpLabel
- %50 = OpCopyObject %8 %11
- %16 = OpImageSampleImplicitLod %15 %50 %14
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_5;
- const auto consumer = nullptr;
- const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
- spvtools::ValidatorOptions validator_options;
- ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
- kConsoleMessageConsumer));
- TransformationContext transformation_context(
- MakeUnique<FactManager>(context.get()), validator_options);
- ASSERT_FALSE(
- TransformationPropagateInstructionUp(20, {{{40, 100}, {41, 101}}})
- .IsApplicable(context.get(), transformation_context));
- }
- } // namespace
- } // namespace fuzz
- } // namespace spvtools
|