• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2018 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 "reduce_test_util.h"
16 #include "source/opt/build_module.h"
17 #include "source/reduce/operand_to_const_reduction_pass.h"
18 
19 namespace spvtools {
20 namespace reduce {
21 namespace {
22 
TEST(OperandToConstantReductionPassTest,BasicCheck)23 TEST(OperandToConstantReductionPassTest, BasicCheck) {
24   std::string prologue = R"(
25                OpCapability Shader
26           %1 = OpExtInstImport "GLSL.std.450"
27                OpMemoryModel Logical GLSL450
28                OpEntryPoint Fragment %4 "main" %37
29                OpExecutionMode %4 OriginUpperLeft
30                OpSource ESSL 310
31                OpName %4 "main"
32                OpName %9 "buf1"
33                OpMemberName %9 0 "f"
34                OpName %11 ""
35                OpName %24 "buf2"
36                OpMemberName %24 0 "i"
37                OpName %26 ""
38                OpName %37 "_GLF_color"
39                OpMemberDecorate %9 0 Offset 0
40                OpDecorate %9 Block
41                OpDecorate %11 DescriptorSet 0
42                OpDecorate %11 Binding 1
43                OpMemberDecorate %24 0 Offset 0
44                OpDecorate %24 Block
45                OpDecorate %26 DescriptorSet 0
46                OpDecorate %26 Binding 2
47                OpDecorate %37 Location 0
48           %2 = OpTypeVoid
49           %3 = OpTypeFunction %2
50           %6 = OpTypeFloat 32
51           %9 = OpTypeStruct %6
52          %10 = OpTypePointer Uniform %9
53          %11 = OpVariable %10 Uniform
54          %12 = OpTypeInt 32 1
55          %13 = OpConstant %12 0
56          %14 = OpTypePointer Uniform %6
57          %20 = OpConstant %6 2
58          %24 = OpTypeStruct %12
59          %25 = OpTypePointer Uniform %24
60          %26 = OpVariable %25 Uniform
61          %27 = OpTypePointer Uniform %12
62          %33 = OpConstant %12 3
63          %35 = OpTypeVector %6 4
64          %36 = OpTypePointer Output %35
65          %37 = OpVariable %36 Output
66           %4 = OpFunction %2 None %3
67           %5 = OpLabel
68          %15 = OpAccessChain %14 %11 %13
69          %16 = OpLoad %6 %15
70          %19 = OpFAdd %6 %16 %16
71          %21 = OpFAdd %6 %19 %20
72          %28 = OpAccessChain %27 %26 %13
73          %29 = OpLoad %12 %28
74   )";
75 
76   std::string epilogue = R"(
77          %45 = OpConvertSToF %6 %34
78          %46 = OpCompositeConstruct %35 %16 %21 %43 %45
79                OpStore %37 %46
80                OpReturn
81                OpFunctionEnd
82   )";
83 
84   std::string original = prologue + R"(
85          %32 = OpIAdd %12 %29 %29
86          %34 = OpIAdd %12 %32 %33
87          %43 = OpConvertSToF %6 %29
88   )" + epilogue;
89 
90   std::string expected = prologue + R"(
91          %32 = OpIAdd %12 %13 %13 ; %29 -> %13 x 2
92          %34 = OpIAdd %12 %13 %33 ; %32 -> %13
93          %43 = OpConvertSToF %6 %13 ; %29 -> %13
94   )" + epilogue;
95 
96   const auto env = SPV_ENV_UNIVERSAL_1_3;
97   const auto consumer = nullptr;
98   const auto context =
99       BuildModule(env, consumer, original, kReduceAssembleOption);
100   const auto pass = TestSubclass<OperandToConstReductionPass>(env);
101   const auto ops = pass.WrapGetAvailableOpportunities(context.get());
102   ASSERT_EQ(17, ops.size());
103   ASSERT_TRUE(ops[0]->PreconditionHolds());
104   ops[0]->TryToApply();
105   ASSERT_TRUE(ops[1]->PreconditionHolds());
106   ops[1]->TryToApply();
107   ASSERT_TRUE(ops[2]->PreconditionHolds());
108   ops[2]->TryToApply();
109   ASSERT_TRUE(ops[3]->PreconditionHolds());
110   ops[3]->TryToApply();
111 
112   CheckEqual(env, expected, context.get());
113 }
114 
TEST(OperandToConstantReductionPassTest,WithCalledFunction)115 TEST(OperandToConstantReductionPassTest, WithCalledFunction) {
116   std::string shader = R"(
117                OpCapability Shader
118           %1 = OpExtInstImport "GLSL.std.450"
119                OpMemoryModel Logical GLSL450
120                OpEntryPoint Fragment %4 "main" %10 %12
121                OpExecutionMode %4 OriginUpperLeft
122                OpSource ESSL 310
123           %2 = OpTypeVoid
124           %3 = OpTypeFunction %2
125           %6 = OpTypeFloat 32
126           %7 = OpTypeVector %6 4
127           %8 = OpTypeFunction %7
128           %9 = OpTypePointer Output %7
129          %10 = OpVariable %9 Output
130          %11 = OpTypePointer Input %7
131          %12 = OpVariable %11 Input
132          %13 = OpConstant %6 0
133          %14 = OpConstantComposite %7 %13 %13 %13 %13
134           %4 = OpFunction %2 None %3
135           %5 = OpLabel
136          %15 = OpFunctionCall %7 %16
137                OpReturn
138                OpFunctionEnd
139          %16 = OpFunction %7 None %8
140          %17 = OpLabel
141                OpReturnValue %14
142                OpFunctionEnd
143   )";
144 
145   const auto env = SPV_ENV_UNIVERSAL_1_3;
146   const auto consumer = nullptr;
147   const auto context =
148       BuildModule(env, consumer, shader, kReduceAssembleOption);
149   const auto pass = TestSubclass<OperandToConstReductionPass>(env);
150   const auto ops = pass.WrapGetAvailableOpportunities(context.get());
151   ASSERT_EQ(0, ops.size());
152 }
153 
154 }  // namespace
155 }  // namespace reduce
156 }  // namespace spvtools
157