1 // Copyright (c) 2017 Google Inc.
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
16 #include <memory>
17 #include <string>
18 #include <vector>
19
20 #include "gmock/gmock.h"
21 #include "source/opt/dominator_analysis.h"
22 #include "source/opt/pass.h"
23 #include "test/opt/assembly_builder.h"
24 #include "test/opt/function_utils.h"
25 #include "test/opt/pass_fixture.h"
26 #include "test/opt/pass_utils.h"
27
28 namespace spvtools {
29 namespace opt {
30 namespace {
31
32 using ::testing::UnorderedElementsAre;
33 using PassClassTest = PassTest<::testing::Test>;
34
35 /*
36 Generated from the following GLSL
37 #version 330 core
38 layout(location = 0) out vec4 v;
39 void main(){
40 if (true) {
41 if (true) {
42 v = vec4(1,1,1,1);
43 } else {
44 v = vec4(2,2,2,2);
45 }
46 } else {
47 if (true) {
48 v = vec4(3,3,3,3);
49 } else {
50 v = vec4(4,4,4,4);
51 }
52 }
53 }
54 */
TEST_F(PassClassTest,UnreachableNestedIfs)55 TEST_F(PassClassTest, UnreachableNestedIfs) {
56 const std::string text = R"(
57 OpCapability Shader
58 %1 = OpExtInstImport "GLSL.std.450"
59 OpMemoryModel Logical GLSL450
60 OpEntryPoint Fragment %4 "main" %15
61 OpExecutionMode %4 OriginUpperLeft
62 OpSource GLSL 330
63 OpName %4 "main"
64 OpName %15 "v"
65 OpDecorate %15 Location 0
66 %2 = OpTypeVoid
67 %3 = OpTypeFunction %2
68 %6 = OpTypeBool
69 %7 = OpConstantTrue %6
70 %12 = OpTypeFloat 32
71 %13 = OpTypeVector %12 4
72 %14 = OpTypePointer Output %13
73 %15 = OpVariable %14 Output
74 %16 = OpConstant %12 1
75 %17 = OpConstantComposite %13 %16 %16 %16 %16
76 %19 = OpConstant %12 2
77 %20 = OpConstantComposite %13 %19 %19 %19 %19
78 %24 = OpConstant %12 3
79 %25 = OpConstantComposite %13 %24 %24 %24 %24
80 %27 = OpConstant %12 4
81 %28 = OpConstantComposite %13 %27 %27 %27 %27
82 %4 = OpFunction %2 None %3
83 %5 = OpLabel
84 OpSelectionMerge %9 None
85 OpBranchConditional %7 %8 %21
86 %8 = OpLabel
87 OpSelectionMerge %11 None
88 OpBranchConditional %7 %10 %18
89 %10 = OpLabel
90 OpStore %15 %17
91 OpBranch %11
92 %18 = OpLabel
93 OpStore %15 %20
94 OpBranch %11
95 %11 = OpLabel
96 OpBranch %9
97 %21 = OpLabel
98 OpSelectionMerge %23 None
99 OpBranchConditional %7 %22 %26
100 %22 = OpLabel
101 OpStore %15 %25
102 OpBranch %23
103 %26 = OpLabel
104 OpStore %15 %28
105 OpBranch %23
106 %23 = OpLabel
107 OpBranch %9
108 %9 = OpLabel
109 OpReturn
110 OpFunctionEnd
111 )";
112 // clang-format on
113 std::unique_ptr<IRContext> context =
114 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
115 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
116 Module* module = context->module();
117 EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
118 << text << std::endl;
119
120 const Function* f = spvtest::GetFunction(module, 4);
121
122 DominatorAnalysis* analysis = context->GetDominatorAnalysis(f);
123
124 EXPECT_TRUE(analysis->Dominates(5, 8));
125 EXPECT_TRUE(analysis->Dominates(5, 9));
126 EXPECT_TRUE(analysis->Dominates(5, 21));
127 EXPECT_TRUE(analysis->Dominates(5, 18));
128 EXPECT_TRUE(analysis->Dominates(5, 10));
129 EXPECT_TRUE(analysis->Dominates(5, 11));
130 EXPECT_TRUE(analysis->Dominates(5, 23));
131 EXPECT_TRUE(analysis->Dominates(5, 22));
132 EXPECT_TRUE(analysis->Dominates(5, 26));
133 EXPECT_TRUE(analysis->Dominates(8, 18));
134 EXPECT_TRUE(analysis->Dominates(8, 10));
135 EXPECT_TRUE(analysis->Dominates(8, 11));
136 EXPECT_TRUE(analysis->Dominates(21, 23));
137 EXPECT_TRUE(analysis->Dominates(21, 22));
138 EXPECT_TRUE(analysis->Dominates(21, 26));
139
140 EXPECT_TRUE(analysis->StrictlyDominates(5, 8));
141 EXPECT_TRUE(analysis->StrictlyDominates(5, 9));
142 EXPECT_TRUE(analysis->StrictlyDominates(5, 21));
143 EXPECT_TRUE(analysis->StrictlyDominates(8, 18));
144 EXPECT_TRUE(analysis->StrictlyDominates(8, 10));
145 EXPECT_TRUE(analysis->StrictlyDominates(8, 11));
146 EXPECT_TRUE(analysis->StrictlyDominates(21, 23));
147 EXPECT_TRUE(analysis->StrictlyDominates(21, 22));
148 EXPECT_TRUE(analysis->StrictlyDominates(21, 26));
149 }
150
151 } // namespace
152 } // namespace opt
153 } // namespace spvtools
154