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