Home | History | Annotate | Download | only in compiler
      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 "Wrapper.h"
     18 #include "bcinfo/MetadataExtractor.h"
     19 #include "builder.h"
     20 #include "file_utils.h"
     21 #include "instructions.h"
     22 #include "module.h"
     23 #include "test_utils.h"
     24 #include "gtest/gtest.h"
     25 
     26 namespace android {
     27 namespace spirit {
     28 
     29 class WrapperTest : public ::testing::Test {
     30 protected:
     31   virtual void SetUp() {
     32     mWordsGreyscale = readWords("greyscale.spv");
     33     mWordsGreyscale2 = readWords("greyscale2.spv");
     34     mWordsInvert = readWords("invert.spv");
     35   }
     36 
     37   std::vector<uint32_t> mWordsGreyscale;
     38   std::vector<uint32_t> mWordsGreyscale2;
     39   std::vector<uint32_t> mWordsInvert;
     40 
     41 private:
     42   std::vector<uint32_t> readWords(const char *testFile) {
     43     static const std::string testDataPath(
     44         "frameworks/rs/rsov/compiler/spirit/test_data/");
     45     const std::string &fullPath = getAbsolutePath(testDataPath + testFile);
     46     return readFile<uint32_t>(fullPath);
     47   }
     48 };
     49 
     50 TEST_F(WrapperTest, testAddBuffer) {
     51   Builder b;
     52   Module m(&b);
     53   auto elemType = m.getIntType(32);
     54   VariableInst *buffer = AddBuffer(elemType, 2, b, &m);
     55   ASSERT_NE(nullptr, buffer);
     56 
     57   GlobalSection *gs = m.getGlobalSection();
     58 
     59   EXPECT_EQ(1, countEntity<TypeRuntimeArrayInst>(gs));
     60   EXPECT_EQ(1, countEntity<TypeStructInst>(gs));
     61   EXPECT_EQ(1, countEntity<VariableInst>(gs));
     62 }
     63 
     64 TEST_F(WrapperTest, testAddWrapper1) {
     65   std::unique_ptr<Module> m(Deserialize<Module>(mWordsGreyscale));
     66 
     67   ASSERT_NE(nullptr, m);
     68 
     69   m->resolveIds();
     70 
     71   Builder b;
     72   m->setBuilder(&b);
     73 
     74   constexpr uint32_t sig =
     75       bcinfo::MD_SIG_Kernel | bcinfo::MD_SIG_In | bcinfo::MD_SIG_Out;
     76 
     77   EXPECT_FALSE(AddWrapper("foo", sig, 1, b, m.get()));
     78 
     79   EXPECT_TRUE(AddWrapper("greyscale(vf4;", sig, 1, b, m.get()));
     80 
     81   // The input already has an entry point
     82   EXPECT_EQ(2, countEntity<EntryPointDefinition>(m.get()));
     83 }
     84 
     85 TEST_F(WrapperTest, testAddWrapper2) {
     86   std::unique_ptr<Module> m(Deserialize<Module>(mWordsInvert));
     87 
     88   ASSERT_NE(nullptr, m);
     89 
     90   m->resolveIds();
     91 
     92   Builder b;
     93   m->setBuilder(&b);
     94 
     95   uint32_t sig = bcinfo::MD_SIG_Kernel | bcinfo::MD_SIG_In | bcinfo::MD_SIG_Out;
     96 
     97   EXPECT_FALSE(AddWrapper("foo", sig, 1, b, m.get()));
     98 
     99   ASSERT_TRUE(AddWrapper("invert", sig, 1, b, m.get()));
    100 
    101   EXPECT_EQ(1, countEntity<EntryPointDefinition>(m.get()));
    102 }
    103 
    104 TEST_F(WrapperTest, testAddWrapperForRoot) {
    105   std::unique_ptr<Module> m(Deserialize<Module>(mWordsInvert));
    106 
    107   ASSERT_NE(nullptr, m);
    108 
    109   Builder b;
    110   m->setBuilder(&b);
    111 
    112   bool success = AddWrapper("root", 0, 1, b, m.get());
    113   ASSERT_TRUE(success);
    114 }
    115 
    116 } // namespace spirit
    117 } // namespace android
    118