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 27namespace android { 28namespace spirit { 29 30class ModuleTest : public ::testing::Test { 31protected: 32 virtual void SetUp() { 33 mWordsGlobal = readWords("global.spv"); 34 mWordsGreyscale = readWords("greyscale.spv"); 35 mWordsGreyscale2 = readWords("greyscale2.spv"); 36 mWordsInvert = readWords("invert.spv"); 37 } 38 39 std::vector<uint32_t> mWordsGlobal; 40 std::vector<uint32_t> mWordsGreyscale; 41 std::vector<uint32_t> mWordsGreyscale2; 42 std::vector<uint32_t> mWordsInvert; 43 44private: 45 std::vector<uint32_t> readWords(const char *testFile) { 46 static const std::string testDataPath( 47 "frameworks/rs/rsov/compiler/spirit/test_data/"); 48 const std::string &fullPath = getAbsolutePath(testDataPath + testFile); 49 return readFile<uint32_t>(fullPath); 50 } 51}; 52 53TEST_F(ModuleTest, testDeserialization1) { 54 auto m = Deserialize<Module>(mWordsGreyscale); 55 56 ASSERT_NE(nullptr, m); 57 58 std::unique_ptr<Module> mDeleter(m); 59 60 int count = 0; 61 std::unique_ptr<IVisitor> v( 62 CreateInstructionVisitor([&count](Instruction *) -> void { count++; })); 63 v->visit(m); 64 65 ASSERT_EQ(count, 123); 66 67 // TODO:: checkCountEntity<Instruction>() does not work correctly 68 // EXPECT_TRUE(checkCountEntity<Instruction>(m, 123)); 69 EXPECT_EQ(5, countEntity<AccessChainInst>(m)); 70 EXPECT_EQ(2, countEntity<BitcastInst>(m)); 71 EXPECT_EQ(1, countEntity<CapabilityInst>(m)); 72 EXPECT_EQ(1, countEntity<CompositeConstructInst>(m)); 73 EXPECT_EQ(5, countEntity<ConstantInst>(m)); 74 EXPECT_EQ(1, countEntity<ConstantCompositeInst>(m)); 75 EXPECT_EQ(11, countEntity<DecorateInst>(m)); 76 EXPECT_EQ(1, countEntity<DotInst>(m)); 77 EXPECT_EQ(1, countEntity<EntryPointInst>(m)); 78 EXPECT_EQ(1, countEntity<ExecutionModeInst>(m)); 79 EXPECT_EQ(1, countEntity<ExtInstImportInst>(m)); 80 EXPECT_EQ(2, countEntity<FunctionInst>(m)); 81 EXPECT_EQ(1, countEntity<FunctionCallInst>(m)); 82 EXPECT_EQ(2, countEntity<FunctionEndInst>(m)); 83 EXPECT_EQ(1, countEntity<FunctionParameterInst>(m)); 84 EXPECT_EQ(1, countEntity<IAddInst>(m)); 85 EXPECT_EQ(1, countEntity<IMulInst>(m)); 86 EXPECT_EQ(1, countEntity<ImageInst>(m)); 87 EXPECT_EQ(1, countEntity<ImageFetchInst>(m)); 88 EXPECT_EQ(2, countEntity<LabelInst>(m)); 89 EXPECT_EQ(11, countEntity<LoadInst>(m)); 90 EXPECT_EQ(4, countEntity<MemberDecorateInst>(m)); 91 EXPECT_EQ(4, countEntity<MemberNameInst>(m)); 92 EXPECT_EQ(1, countEntity<MemoryModelInst>(m)); 93 EXPECT_EQ(14, countEntity<NameInst>(m)); 94 EXPECT_EQ(1, countEntity<ReturnInst>(m)); 95 EXPECT_EQ(1, countEntity<ReturnValueInst>(m)); 96 EXPECT_EQ(1, countEntity<SourceInst>(m)); 97 EXPECT_EQ(3, countEntity<SourceExtensionInst>(m)); 98 EXPECT_EQ(6, countEntity<StoreInst>(m)); 99 EXPECT_EQ(1, countEntity<TypeFloatInst>(m)); 100 EXPECT_EQ(2, countEntity<TypeFunctionInst>(m)); 101 EXPECT_EQ(1, countEntity<TypeImageInst>(m)); 102 EXPECT_EQ(2, countEntity<TypeIntInst>(m)); 103 EXPECT_EQ(10, countEntity<TypePointerInst>(m)); 104 EXPECT_EQ(1, countEntity<TypeRuntimeArrayInst>(m)); 105 EXPECT_EQ(1, countEntity<TypeSampledImageInst>(m)); 106 EXPECT_EQ(2, countEntity<TypeStructInst>(m)); 107 EXPECT_EQ(4, countEntity<TypeVectorInst>(m)); 108 EXPECT_EQ(1, countEntity<TypeVoidInst>(m)); 109 EXPECT_EQ(9, countEntity<VariableInst>(m)); 110 EXPECT_EQ(1, countEntity<VectorShuffleInst>(m)); 111 EXPECT_EQ(1, countEntity<EntryPointDefinition>(m)); 112 EXPECT_EQ(1, countEntity<DebugInfoSection>(m)); 113 EXPECT_EQ(1, countEntity<GlobalSection>(m)); 114 EXPECT_EQ(2, countEntity<FunctionDefinition>(m)); 115} 116 117TEST_F(ModuleTest, testDeserialization2) { 118 Module *m = Deserialize<Module>(mWordsInvert); 119 ASSERT_NE(nullptr, m); 120 121 std::unique_ptr<Module> mDeleter(m); 122 123 auto outwords = Serialize<Module>(m); 124 125 EXPECT_TRUE(mWordsInvert == outwords); 126} 127 128TEST_F(ModuleTest, testSerialization1) { 129 Module *m = Deserialize<Module>(mWordsGreyscale); 130 ASSERT_NE(nullptr, m); 131 132 std::unique_ptr<Module> mDeleter(m); 133 134 EXPECT_EQ(2, countEntity<FunctionDefinition>(m)); 135 136 auto outwords = Serialize<Module>(m); 137 138 EXPECT_TRUE(mWordsGreyscale == outwords); 139} 140 141TEST_F(ModuleTest, testSerialization2) { 142 Module *m = Deserialize<Module>(mWordsGreyscale2); 143 ASSERT_NE(nullptr, m); 144 145 std::unique_ptr<Module> mDeleter(m); 146 147 EXPECT_EQ(1, countEntity<FunctionDefinition>(m)); 148 149 auto outwords = Serialize<Module>(m); 150 151 EXPECT_TRUE(mWordsGreyscale2 == outwords); 152} 153 154TEST_F(ModuleTest, testLookupByName) { 155 Module *m = Deserialize<Module>(mWordsGreyscale); 156 157 ASSERT_NE(nullptr, m); 158 159 std::unique_ptr<Module> mDeleter(m); 160 161 m->resolveIds(); 162 163 Instruction *mainFunc = m->lookupByName("main"); 164 165 EXPECT_NE(nullptr, mainFunc); 166 EXPECT_STREQ("main", m->lookupNameByInstruction(mainFunc)); 167 168 auto i = static_cast<FunctionInst *>(m->lookupByName("greyscale(vf4;")); 169 ASSERT_NE(nullptr, i); 170 171 auto kernel = m->getFunctionDefinitionFromInstruction(i); 172 ASSERT_NE(nullptr, kernel); 173 174 auto pt = static_cast<FunctionInst *>(m->lookupByName("greyscale(vf4;")); 175 EXPECT_NE(nullptr, kernel->getParameter(0)); 176 EXPECT_NE(nullptr, kernel->getReturnType()); 177 178 EXPECT_NE(nullptr, m->lookupFunctionDefinitionByName("greyscale(vf4;")); 179} 180 181TEST_F(ModuleTest, testGetSize) { 182 std::unique_ptr<Module> m(new Module()); 183 EXPECT_EQ(4UL, m->getSize(m->getIntType(32))); 184 EXPECT_EQ(4UL, m->getSize(m->getIntType(32, 0))); 185 EXPECT_EQ(4UL, m->getSize(m->getFloatType(32))); 186 EXPECT_EQ(16UL, m->getSize(m->getVectorType(m->getFloatType(32), 4))); 187} 188 189TEST_F(ModuleTest, testFindStringOfPrefix) { 190 Module *m = Deserialize<Module>(mWordsGlobal); 191 192 ASSERT_NE(nullptr, m); 193 194 std::unique_ptr<Module> mDeleter(m); 195 ASSERT_STREQ(".rsov.ExportedVars:0;", 196 m->findStringOfPrefix(".rsov.ExportedVars:").c_str()); 197 198} 199 200} // namespace spirit 201} // namespace android 202