1 /*
2  * Copyright 2017, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "module.h"
18 
19 #include "file_utils.h"
20 #include "instructions.h"
21 #include "test_utils.h"
22 #include "gtest/gtest.h"
23 
24 #include <fstream>
25 #include <memory>
26 
27 namespace android {
28 namespace spirit {
29 
30 class ModuleTest : public ::testing::Test {
31 protected:
SetUp()32   virtual void SetUp() {
33     mWordsGreyscale = readWords("greyscale.spv");
34     mWordsGreyscale2 = readWords("greyscale2.spv");
35     mWordsInvert = readWords("invert.spv");
36   }
37 
38   std::vector<uint32_t> mWordsGreyscale;
39   std::vector<uint32_t> mWordsGreyscale2;
40   std::vector<uint32_t> mWordsInvert;
41 
42 private:
readWords(const char * testFile)43   std::vector<uint32_t> readWords(const char *testFile) {
44     static const std::string testDataPath(
45         "frameworks/rs/rsov/compiler/spirit/test_data/");
46     const std::string &fullPath = getAbsolutePath(testDataPath + testFile);
47     return readFile<uint32_t>(fullPath);
48   }
49 };
50 
TEST_F(ModuleTest,testDeserialization1)51 TEST_F(ModuleTest, testDeserialization1) {
52   auto m = Deserialize<Module>(mWordsGreyscale);
53 
54   ASSERT_NE(nullptr, m);
55 
56   std::unique_ptr<Module> mDeleter(m);
57 
58   int count = 0;
59   std::unique_ptr<IVisitor> v(
60       CreateInstructionVisitor([&count](Instruction *) -> void { count++; }));
61   v->visit(m);
62 
63   ASSERT_EQ(count, 123);
64 
65   // TODO:: checkCountEntity<Instruction>() does not work correctly
66   //  EXPECT_TRUE(checkCountEntity<Instruction>(m, 123));
67   EXPECT_EQ(5, countEntity<AccessChainInst>(m));
68   EXPECT_EQ(2, countEntity<BitcastInst>(m));
69   EXPECT_EQ(1, countEntity<CapabilityInst>(m));
70   EXPECT_EQ(1, countEntity<CompositeConstructInst>(m));
71   EXPECT_EQ(5, countEntity<ConstantInst>(m));
72   EXPECT_EQ(1, countEntity<ConstantCompositeInst>(m));
73   EXPECT_EQ(11, countEntity<DecorateInst>(m));
74   EXPECT_EQ(1, countEntity<DotInst>(m));
75   EXPECT_EQ(1, countEntity<EntryPointInst>(m));
76   EXPECT_EQ(1, countEntity<ExecutionModeInst>(m));
77   EXPECT_EQ(1, countEntity<ExtInstImportInst>(m));
78   EXPECT_EQ(2, countEntity<FunctionInst>(m));
79   EXPECT_EQ(1, countEntity<FunctionCallInst>(m));
80   EXPECT_EQ(2, countEntity<FunctionEndInst>(m));
81   EXPECT_EQ(1, countEntity<FunctionParameterInst>(m));
82   EXPECT_EQ(1, countEntity<IAddInst>(m));
83   EXPECT_EQ(1, countEntity<IMulInst>(m));
84   EXPECT_EQ(1, countEntity<ImageInst>(m));
85   EXPECT_EQ(1, countEntity<ImageFetchInst>(m));
86   EXPECT_EQ(2, countEntity<LabelInst>(m));
87   EXPECT_EQ(11, countEntity<LoadInst>(m));
88   EXPECT_EQ(4, countEntity<MemberDecorateInst>(m));
89   EXPECT_EQ(4, countEntity<MemberNameInst>(m));
90   EXPECT_EQ(1, countEntity<MemoryModelInst>(m));
91   EXPECT_EQ(14, countEntity<NameInst>(m));
92   EXPECT_EQ(1, countEntity<ReturnInst>(m));
93   EXPECT_EQ(1, countEntity<ReturnValueInst>(m));
94   EXPECT_EQ(1, countEntity<SourceInst>(m));
95   EXPECT_EQ(3, countEntity<SourceExtensionInst>(m));
96   EXPECT_EQ(6, countEntity<StoreInst>(m));
97   EXPECT_EQ(1, countEntity<TypeFloatInst>(m));
98   EXPECT_EQ(2, countEntity<TypeFunctionInst>(m));
99   EXPECT_EQ(1, countEntity<TypeImageInst>(m));
100   EXPECT_EQ(2, countEntity<TypeIntInst>(m));
101   EXPECT_EQ(10, countEntity<TypePointerInst>(m));
102   EXPECT_EQ(1, countEntity<TypeRuntimeArrayInst>(m));
103   EXPECT_EQ(1, countEntity<TypeSampledImageInst>(m));
104   EXPECT_EQ(2, countEntity<TypeStructInst>(m));
105   EXPECT_EQ(4, countEntity<TypeVectorInst>(m));
106   EXPECT_EQ(1, countEntity<TypeVoidInst>(m));
107   EXPECT_EQ(9, countEntity<VariableInst>(m));
108   EXPECT_EQ(1, countEntity<VectorShuffleInst>(m));
109   EXPECT_EQ(1, countEntity<EntryPointDefinition>(m));
110   EXPECT_EQ(1, countEntity<DebugInfoSection>(m));
111   EXPECT_EQ(1, countEntity<GlobalSection>(m));
112   EXPECT_EQ(2, countEntity<FunctionDefinition>(m));
113 }
114 
TEST_F(ModuleTest,testDeserialization2)115 TEST_F(ModuleTest, testDeserialization2) {
116   Module *m = Deserialize<Module>(mWordsInvert);
117   ASSERT_NE(nullptr, m);
118 
119   std::unique_ptr<Module> mDeleter(m);
120 
121   auto outwords = Serialize<Module>(m);
122 
123   EXPECT_TRUE(mWordsInvert == outwords);
124 }
125 
TEST_F(ModuleTest,testSerialization1)126 TEST_F(ModuleTest, testSerialization1) {
127   Module *m = Deserialize<Module>(mWordsGreyscale);
128   ASSERT_NE(nullptr, m);
129 
130   std::unique_ptr<Module> mDeleter(m);
131 
132   EXPECT_EQ(2, countEntity<FunctionDefinition>(m));
133 
134   auto outwords = Serialize<Module>(m);
135 
136   EXPECT_TRUE(mWordsGreyscale == outwords);
137 }
138 
TEST_F(ModuleTest,testSerialization2)139 TEST_F(ModuleTest, testSerialization2) {
140   Module *m = Deserialize<Module>(mWordsGreyscale2);
141   ASSERT_NE(nullptr, m);
142 
143   std::unique_ptr<Module> mDeleter(m);
144 
145   EXPECT_EQ(1, countEntity<FunctionDefinition>(m));
146 
147   auto outwords = Serialize<Module>(m);
148 
149   EXPECT_TRUE(mWordsGreyscale2 == outwords);
150 }
151 
TEST_F(ModuleTest,testLookupByName)152 TEST_F(ModuleTest, testLookupByName) {
153   Module *m = Deserialize<Module>(mWordsGreyscale);
154 
155   ASSERT_NE(nullptr, m);
156 
157   std::unique_ptr<Module> mDeleter(m);
158 
159   m->resolveIds();
160 
161   Instruction *mainFunc = m->lookupByName("main");
162 
163   EXPECT_NE(nullptr, mainFunc);
164   EXPECT_STREQ("main", m->lookupNameByInstruction(mainFunc));
165 
166   auto i = static_cast<FunctionInst *>(m->lookupByName("greyscale(vf4;"));
167   ASSERT_NE(nullptr, i);
168 
169   auto kernel = m->getFunctionDefinitionFromInstruction(i);
170   ASSERT_NE(nullptr, kernel);
171 
172   auto pt = static_cast<FunctionInst *>(m->lookupByName("greyscale(vf4;"));
173   EXPECT_NE(nullptr, kernel->getParameter(0));
174   EXPECT_NE(nullptr, kernel->getReturnType());
175 
176   EXPECT_NE(nullptr, m->lookupFunctionDefinitionByName("greyscale(vf4;"));
177 }
178 
TEST_F(ModuleTest,testGetSize)179 TEST_F(ModuleTest, testGetSize) {
180   std::unique_ptr<Module> m(new Module());
181   EXPECT_EQ(4UL, m->getSize(m->getIntType(32)));
182   EXPECT_EQ(4UL, m->getSize(m->getIntType(32, 0)));
183   EXPECT_EQ(4UL, m->getSize(m->getFloatType(32)));
184   EXPECT_EQ(16UL, m->getSize(m->getVectorType(m->getFloatType(32), 4)));
185 }
186 
187 } // namespace spirit
188 } // namespace android
189