| 1 |
// Copyright (c) 2020 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#include "source/fuzz/transformation_replace_opphi_id_from_dead_predecessor.h" |
| 16 |
|
| 17 |
#include "gtest/gtest.h" |
| 18 |
#include "source/fuzz/fuzzer_util.h" |
| 19 |
#include "test/fuzz/fuzz_test_util.h" |
| 20 |
|
| 21 |
namespace spvtools { |
| 22 |
namespace fuzz { |
| 23 |
namespace { |
| 24 |
|
| 25 |
std::string shader = R"( |
| 26 |
OpCapability Shader |
| 27 |
%1 = OpExtInstImport "GLSL.std.450" |
| 28 |
OpMemoryModel Logical GLSL450 |
| 29 |
OpEntryPoint Fragment %2 "main" |
| 30 |
OpExecutionMode %2 OriginUpperLeft |
| 31 |
OpSource ESSL 310 |
| 32 |
OpName %2 "main" |
| 33 |
%3 = OpTypeVoid |
| 34 |
%4 = OpTypeFunction %3 |
| 35 |
%5 = OpTypeBool |
| 36 |
%6 = OpConstantTrue %5 |
| 37 |
%7 = OpConstantFalse %5 |
| 38 |
%8 = OpTypeInt 32 1 |
| 39 |
%9 = OpConstant %8 2 |
| 40 |
%10 = OpConstant %8 3 |
| 41 |
%11 = OpConstant %8 4 |
| 42 |
%12 = OpConstant %8 5 |
| 43 |
%13 = OpConstant %8 6 |
| 44 |
%2 = OpFunction %3 None %4 |
| 45 |
%14 = OpLabel |
| 46 |
OpSelectionMerge %15 None |
| 47 |
OpBranchConditional %6 %16 %17 |
| 48 |
%16 = OpLabel |
| 49 |
%18 = OpCopyObject %8 %9 |
| 50 |
OpSelectionMerge %19 None |
| 51 |
OpBranchConditional %7 %20 %21 |
| 52 |
%20 = OpLabel |
| 53 |
%22 = OpCopyObject %8 %10 |
| 54 |
%23 = OpCopyObject %8 %12 |
| 55 |
OpBranch %19 |
| 56 |
%21 = OpLabel |
| 57 |
%24 = OpCopyObject %8 %9 |
| 58 |
OpBranch %19 |
| 59 |
%19 = OpLabel |
| 60 |
%25 = OpPhi %8 %22 %20 %24 %21 |
| 61 |
OpBranch %15 |
| 62 |
%17 = OpLabel |
| 63 |
%26 = OpCopyObject %8 %12 |
| 64 |
%27 = OpCopyObject %8 %13 |
| 65 |
OpBranch %28 |
| 66 |
%28 = OpLabel |
| 67 |
%29 = OpPhi %8 %27 %17 |
| 68 |
OpBranch %15 |
| 69 |
%15 = OpLabel |
| 70 |
%30 = OpPhi %8 %25 %19 %26 %28 |
| 71 |
OpReturn |
| 72 |
OpFunctionEnd |
| 73 |
)"; |
| 74 |
|
| 75 |
TEST(TransformationReplaceOpPhiIdFromDeadPredecessorTest, Inapplicable) { |
| 76 |
const auto env = SPV_ENV_UNIVERSAL_1_5; |
| 77 |
const auto consumer = nullptr; |
| 78 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 79 |
|
| 80 |
spvtools::ValidatorOptions validator_options; |
| 81 |
TransformationContext transformation_context( |
| 82 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 83 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 84 |
kConsoleMessageConsumer)); |
| 85 |
|
| 86 |
// Record the fact that blocks 20, 17, 28 are dead. |
| 87 |
transformation_context.GetFactManager()->AddFactBlockIsDead(20); |
| 88 |
transformation_context.GetFactManager()->AddFactBlockIsDead(17); |
| 89 |
transformation_context.GetFactManager()->AddFactBlockIsDead(28); |
| 90 |
|
| 91 |
// %26 is not an OpPhi instruction. |
| 92 |
ASSERT_FALSE(TransformationReplaceOpPhiIdFromDeadPredecessor(26, 14, 10) |
| 93 |
.IsApplicable(context.get(), transformation_context)); |
| 94 |
|
| 95 |
// %25 is not a block label. |
| 96 |
ASSERT_FALSE(TransformationReplaceOpPhiIdFromDeadPredecessor(30, 25, 10) |
| 97 |
.IsApplicable(context.get(), transformation_context)); |
| 98 |
|
| 99 |
// %14 is not a predecessor of %28 (which contains %29). |
| 100 |
ASSERT_FALSE(TransformationReplaceOpPhiIdFromDeadPredecessor(29, 14, 10) |
| 101 |
.IsApplicable(context.get(), transformation_context)); |
| 102 |
|
| 103 |
// %19 is not a dead block. |
| 104 |
ASSERT_FALSE(TransformationReplaceOpPhiIdFromDeadPredecessor(30, 19, 10) |
| 105 |
.IsApplicable(context.get(), transformation_context)); |
| 106 |
|
| 107 |
// %7 does not have the same type id as %25. |
| 108 |
ASSERT_FALSE( |
| 109 |
TransformationReplaceOpPhiIdFromDeadPredecessor(25, 20, 7).IsApplicable( |
| 110 |
context.get(), transformation_context)); |
| 111 |
|
| 112 |
// %29 is not available at the end of %20. |
| 113 |
ASSERT_FALSE(TransformationReplaceOpPhiIdFromDeadPredecessor(25, 20, 29) |
| 114 |
.IsApplicable(context.get(), transformation_context)); |
| 115 |
} |
| 116 |
|
| 117 |
TEST(TransformationReplaceOpPhiIdFromDeadPredecessorTest, Apply) { |
| 118 |
const auto env = SPV_ENV_UNIVERSAL_1_5; |
| 119 |
const auto consumer = nullptr; |
| 120 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 121 |
|
| 122 |
spvtools::ValidatorOptions validator_options; |
| 123 |
TransformationContext transformation_context( |
| 124 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 125 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 126 |
kConsoleMessageConsumer)); |
| 127 |
|
| 128 |
// Record the fact that blocks 20, 17, 28 are dead. |
| 129 |
transformation_context.GetFactManager()->AddFactBlockIsDead(20); |
| 130 |
transformation_context.GetFactManager()->AddFactBlockIsDead(17); |
| 131 |
transformation_context.GetFactManager()->AddFactBlockIsDead(28); |
| 132 |
|
| 133 |
auto transformation1 = |
| 134 |
TransformationReplaceOpPhiIdFromDeadPredecessor(25, 20, 18); |
| 135 |
ASSERT_TRUE( |
| 136 |
transformation1.IsApplicable(context.get(), transformation_context)); |
| 137 |
ApplyAndCheckFreshIds(transformation1, context.get(), |
| 138 |
&transformation_context); |
| 139 |
|
| 140 |
auto transformation2 = |
| 141 |
TransformationReplaceOpPhiIdFromDeadPredecessor(30, 28, 29); |
| 142 |
ASSERT_TRUE( |
| 143 |
transformation2.IsApplicable(context.get(), transformation_context)); |
| 144 |
ApplyAndCheckFreshIds(transformation2, context.get(), |
| 145 |
&transformation_context); |
| 146 |
|
| 147 |
auto transformation3 = |
| 148 |
TransformationReplaceOpPhiIdFromDeadPredecessor(29, 17, 10); |
| 149 |
ASSERT_TRUE( |
| 150 |
transformation3.IsApplicable(context.get(), transformation_context)); |
| 151 |
ApplyAndCheckFreshIds(transformation3, context.get(), |
| 152 |
&transformation_context); |
| 153 |
|
| 154 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 155 |
kConsoleMessageConsumer)); |
| 156 |
|
| 157 |
std::string after_transformations = R"( |
| 158 |
OpCapability Shader |
| 159 |
%1 = OpExtInstImport "GLSL.std.450" |
| 160 |
OpMemoryModel Logical GLSL450 |
| 161 |
OpEntryPoint Fragment %2 "main" |
| 162 |
OpExecutionMode %2 OriginUpperLeft |
| 163 |
OpSource ESSL 310 |
| 164 |
OpName %2 "main" |
| 165 |
%3 = OpTypeVoid |
| 166 |
%4 = OpTypeFunction %3 |
| 167 |
%5 = OpTypeBool |
| 168 |
%6 = OpConstantTrue %5 |
| 169 |
%7 = OpConstantFalse %5 |
| 170 |
%8 = OpTypeInt 32 1 |
| 171 |
%9 = OpConstant %8 2 |
| 172 |
%10 = OpConstant %8 3 |
| 173 |
%11 = OpConstant %8 4 |
| 174 |
%12 = OpConstant %8 5 |
| 175 |
%13 = OpConstant %8 6 |
| 176 |
%2 = OpFunction %3 None %4 |
| 177 |
%14 = OpLabel |
| 178 |
OpSelectionMerge %15 None |
| 179 |
OpBranchConditional %6 %16 %17 |
| 180 |
%16 = OpLabel |
| 181 |
%18 = OpCopyObject %8 %9 |
| 182 |
OpSelectionMerge %19 None |
| 183 |
OpBranchConditional %7 %20 %21 |
| 184 |
%20 = OpLabel |
| 185 |
%22 = OpCopyObject %8 %10 |
| 186 |
%23 = OpCopyObject %8 %12 |
| 187 |
OpBranch %19 |
| 188 |
%21 = OpLabel |
| 189 |
%24 = OpCopyObject %8 %9 |
| 190 |
OpBranch %19 |
| 191 |
%19 = OpLabel |
| 192 |
%25 = OpPhi %8 %18 %20 %24 %21 |
| 193 |
OpBranch %15 |
| 194 |
%17 = OpLabel |
| 195 |
%26 = OpCopyObject %8 %12 |
| 196 |
%27 = OpCopyObject %8 %13 |
| 197 |
OpBranch %28 |
| 198 |
%28 = OpLabel |
| 199 |
%29 = OpPhi %8 %10 %17 |
| 200 |
OpBranch %15 |
| 201 |
%15 = OpLabel |
| 202 |
%30 = OpPhi %8 %25 %19 %29 %28 |
| 203 |
OpReturn |
| 204 |
OpFunctionEnd |
| 205 |
)"; |
| 206 |
|
| 207 |
ASSERT_TRUE(IsEqual(env, after_transformations, context.get())); |
| 208 |
} |
| 209 |
|
| 210 |
} // namespace |
| 211 |
} // namespace fuzz |
| 212 |
} // namespace spvtools |