1 // Copyright (c) 2018 Google LLC.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gmock/gmock.h>
19 #include "../pass_fixture.h"
20 #include "opt/licm_pass.h"
24 using namespace spvtools;
25 using ::testing::UnorderedElementsAre;
27 using PassClassTest = PassTest<::testing::Test>;
30 A simple test for the LICM pass
32 Generated from the following GLSL fragment shader
33 --eliminate-local-multi-store has also been run on the spv binary
39 for (int i = 0; i < 10; i++) {
45 TEST_F(PassClassTest, SimpleHoist) {
46 const std::string before_hoist = R"(OpCapability Shader
47 %1 = OpExtInstImport "GLSL.std.450"
48 OpMemoryModel Logical GLSL450
49 OpEntryPoint Fragment %main "main"
50 OpExecutionMode %main OriginUpperLeft
54 %4 = OpTypeFunction %void
56 %_ptr_Function_int = OpTypePointer Function %int
57 %int_1 = OpConstant %int 1
58 %int_2 = OpConstant %int 2
59 %int_0 = OpConstant %int 0
60 %int_10 = OpConstant %int 10
62 %main = OpFunction %void None %4
66 %14 = OpPhi %int %int_0 %12 %15 %16
67 %17 = OpPhi %int %int_0 %12 %18 %16
68 OpLoopMerge %19 %16 None
71 %21 = OpSLessThan %bool %17 %int_10
72 OpBranchConditional %21 %22 %19
74 %15 = OpIAdd %int %int_1 %int_2
77 %18 = OpIAdd %int %17 %int_1
84 const std::string after_hoist = R"(OpCapability Shader
85 %1 = OpExtInstImport "GLSL.std.450"
86 OpMemoryModel Logical GLSL450
87 OpEntryPoint Fragment %main "main"
88 OpExecutionMode %main OriginUpperLeft
92 %4 = OpTypeFunction %void
94 %_ptr_Function_int = OpTypePointer Function %int
95 %int_1 = OpConstant %int 1
96 %int_2 = OpConstant %int 2
97 %int_0 = OpConstant %int 0
98 %int_10 = OpConstant %int 10
100 %main = OpFunction %void None %4
102 %15 = OpIAdd %int %int_1 %int_2
105 %14 = OpPhi %int %int_0 %12 %15 %16
106 %17 = OpPhi %int %int_0 %12 %18 %16
107 OpLoopMerge %19 %16 None
110 %21 = OpSLessThan %bool %17 %int_10
111 OpBranchConditional %21 %22 %19
115 %18 = OpIAdd %int %17 %int_1
122 SinglePassRunAndCheck<opt::LICMPass>(before_hoist, after_hoist, true);