|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include <gtest/gtest.h> |
| 21 | +#include <tvm/ir/global_var_supply.h> |
| 22 | +#include <tvm/ir/module.h> |
| 23 | +#include <tvm/ir/name_supply.h> |
| 24 | +#include <tvm/relay/expr.h> |
| 25 | +#include <tvm/relay/function.h> |
| 26 | + |
| 27 | +using namespace tvm; |
| 28 | + |
| 29 | +NameSupply preambleNameSupply() { |
| 30 | + NameSupply name_supply = NameSupply("prefix"); |
| 31 | + name_supply->FreshName("test"); |
| 32 | + return name_supply; |
| 33 | +} |
| 34 | + |
| 35 | +TEST(NameSupply, FreshName) { |
| 36 | + NameSupply name_supply = preambleNameSupply(); |
| 37 | + String fresh = name_supply->FreshName("test"); |
| 38 | + |
| 39 | + EXPECT_EQ(fresh.compare("prefix_test1"), 0); |
| 40 | +} |
| 41 | + |
| 42 | +TEST(NameSupply, ContainsName) { |
| 43 | + NameSupply name_supply = preambleNameSupply(); |
| 44 | + |
| 45 | + EXPECT_TRUE(name_supply->ContainsName("test")); |
| 46 | + EXPECT_FALSE(name_supply->ContainsName("test1")); |
| 47 | +} |
| 48 | + |
| 49 | +TEST(NameSupply, ReserveName) { |
| 50 | + NameSupply name_supply = preambleNameSupply(); |
| 51 | + name_supply->ReserveName("otherTest", false); |
| 52 | + |
| 53 | + EXPECT_TRUE(name_supply->ContainsName("otherTest", false)); |
| 54 | + EXPECT_FALSE(name_supply->ContainsName("otherTest")); |
| 55 | +} |
| 56 | + |
| 57 | +GlobalVarSupply preambleVarSupply() { |
| 58 | + GlobalVarSupply global_var_supply = GlobalVarSupply(); |
| 59 | + global_var_supply->FreshGlobal("test"); |
| 60 | + return global_var_supply; |
| 61 | +} |
| 62 | + |
| 63 | +TEST(GlobalVarSupply, FreshGlobal) { |
| 64 | + GlobalVarSupply global_var_supply = preambleVarSupply(); |
| 65 | + GlobalVar first_var = global_var_supply->FreshGlobal("test"); |
| 66 | + GlobalVar second_var = global_var_supply->FreshGlobal("test"); |
| 67 | + |
| 68 | + EXPECT_FALSE(tvm::StructuralEqual()(first_var, second_var)); |
| 69 | + EXPECT_EQ(first_var->name_hint.compare("test1"), 0); |
| 70 | + EXPECT_EQ(second_var->name_hint.compare("test2"), 0); |
| 71 | +} |
| 72 | + |
| 73 | +TEST(GlobalVarSupply, UniqueGlobalFor) { |
| 74 | + GlobalVarSupply global_var_supply = preambleVarSupply(); |
| 75 | + GlobalVar first_var = global_var_supply->UniqueGlobalFor("someName"); |
| 76 | + GlobalVar second_var = global_var_supply->UniqueGlobalFor("someName"); |
| 77 | + |
| 78 | + EXPECT_TRUE(tvm::StructuralEqual()(first_var, second_var)); |
| 79 | + EXPECT_EQ(first_var->name_hint.compare("someName"), 0); |
| 80 | + EXPECT_EQ(second_var->name_hint.compare("someName"), 0); |
| 81 | +} |
| 82 | + |
| 83 | +TEST(GlobalVarSupply, ReserveGlobal) { |
| 84 | + GlobalVarSupply global_var_supply = preambleVarSupply(); |
| 85 | + GlobalVar var = GlobalVar("someName"); |
| 86 | + global_var_supply->ReserveGlobalVar(var); |
| 87 | + GlobalVar second_var = global_var_supply->UniqueGlobalFor("someName"); |
| 88 | + GlobalVar third_var = global_var_supply->FreshGlobal("someName"); |
| 89 | + |
| 90 | + EXPECT_TRUE(tvm::StructuralEqual()(var, second_var)); |
| 91 | + EXPECT_FALSE(tvm::StructuralEqual()(var, third_var)); |
| 92 | + EXPECT_EQ(second_var->name_hint.compare("someName"), 0); |
| 93 | + EXPECT_EQ(third_var->name_hint.compare("someName1"), 0); |
| 94 | +} |
| 95 | + |
| 96 | +TEST(GlobalVarSupply, BuildIRModule) { |
| 97 | + auto x = relay::Var("x", relay::Type()); |
| 98 | + auto f = relay::Function(tvm::Array<relay::Var>{x}, x, relay::Type(), {}); |
| 99 | + GlobalVar var = GlobalVar("test"); |
| 100 | + IRModule module = IRModule({{var, f}}); |
| 101 | + |
| 102 | + GlobalVarSupply global_var_supply = GlobalVarSupply(module); |
| 103 | + GlobalVar second_var = global_var_supply->UniqueGlobalFor("test", false); |
| 104 | + GlobalVar third_var = global_var_supply->FreshGlobal("test", false); |
| 105 | + |
| 106 | + EXPECT_TRUE(tvm::StructuralEqual()(var, second_var)); |
| 107 | + EXPECT_FALSE(tvm::StructuralEqual()(var, third_var)); |
| 108 | + EXPECT_EQ(second_var->name_hint.compare("test"), 0); |
| 109 | + EXPECT_EQ(third_var->name_hint.compare("test1"), 0); |
| 110 | +} |
0 commit comments