ir_functor_test.cc 7.36 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12 13 14 15 16 17 18 19
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

tqchen committed
20 21
#include <dmlc/logging.h>
#include <gtest/gtest.h>
22 23
#include <tvm/tir/expr.h>
#include <tvm/tir/op.h>
24
#include <tvm/node/functor.h>
25 26
#include <tvm/tir/expr_functor.h>
#include <tvm/tir/stmt_functor.h>
tqchen committed
27 28 29

TEST(IRF, Basic) {
  using namespace tvm;
30
  using namespace tvm::tir;
tqchen committed
31 32 33
  Var x("x");
  auto z = x + 1;

34
  NodeFunctor<int(const ObjectRef& n, int b)> f;
35
  f.set_dispatch<VarNode>([](const ObjectRef& n, int b) {
tqchen committed
36 37
      return b;
    });
38
  f.set_dispatch<AddNode>([](const ObjectRef& n, int b) {
tqchen committed
39 40 41 42 43 44
      return b + 2;
    });
  CHECK_EQ(f(x, 2),  2);
  CHECK_EQ(f(z, 2),  4);
}

45 46
TEST(IRF, CountVar) {
  using namespace tvm;
47
  using namespace tvm::tir;
48 49 50 51
  int n_var = 0;
  Var x("x"), y;

  auto z = x + 1 + y + y;
52 53
  tir::PostOrderVisit(z, [&n_var](const ObjectRef& n) {
    if (n.as<VarNode>()) ++n_var;
54 55 56 57 58
    });
  CHECK_EQ(n_var, 2);
}


59 60
TEST(IRF, ExprTransform) {
  using namespace tvm;
61
  using namespace tvm::tir;
62 63 64 65
  Var x("x");
  auto z = x + 1;

  class MyExprFunctor
66
      : public tir::ExprFunctor<int(const PrimExpr&, int)> {
67
   public:
68
    int VisitExpr_(const VarNode* op, int b) final {
69 70
      return b;
    }
71
    int VisitExpr_(const IntImmNode* op, int b) final {
72 73
      return op->value;
    }
74
    int VisitExpr_(const AddNode* op, int b) final {
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
      return VisitExpr(op->a, b) + VisitExpr(op->b, b);
    }
  };
  MyExprFunctor f;
  CHECK_EQ(f(x, 2),  2);
  CHECK_EQ(f(z, 2),  3);
  try {
    f(z - 1, 2);
    LOG(FATAL) << "should fail";
  } catch(dmlc::Error) {
  }
}

TEST(IRF, ExprVisit) {
  using namespace tvm;
90
  using namespace tvm::tir;
91 92 93 94
  Var x("x");
  auto z = x + 1;

  class MyVisitor
95 96
      : public tir::ExprFunctor<void(const PrimExpr&)>,
        public tir::StmtFunctor<void(const Stmt&)> {
97 98 99
   public:
    int count = 0;
    // implementation
100
    void VisitExpr_(const VarNode* op) final {
101 102
      ++count;
    }
103
    void VisitExpr_(const IntImmNode* op) final {
104
    }
105
    void VisitExpr_(const AddNode* op) final {
106 107 108
      VisitExpr(op->a);
      VisitExpr(op->b);
    }
109
    void VisitStmt_(const EvaluateNode* op) final {
110 111 112 113
      VisitExpr(op->value);
    }
  };
  MyVisitor v;
114
  v.VisitStmt(EvaluateNode::make(z));
115 116 117
  CHECK_EQ(v.count, 1);
}

118 119 120

TEST(IRF, StmtVisitor) {
  using namespace tvm;
121
  using namespace tvm::tir;
122 123 124 125 126 127
  Var x("x");
  class MyVisitor
      : public StmtExprVisitor {
   public:
    int count = 0;
    // implementation
128
    void VisitExpr_(const VarNode* op) final {
129 130 131 132 133 134
      ++count;
    }
  };
  MyVisitor v;
  auto fmaketest = [&]() {
    auto z = x + 1;
135
    Stmt body = EvaluateNode::make(z);
136
    Var buffer("b", DataType::Handle());
137
    return AllocateNode::make(buffer, DataType::Float(32), {z, z}, const_true(), body);
138 139 140 141 142 143 144
  };
  v(fmaketest());
  CHECK_EQ(v.count, 3);
}

TEST(IRF, StmtMutator) {
  using namespace tvm;
145
  using namespace tvm::tir;
146 147 148
  Var x("x");

  class MyVisitor
149 150
      : public tir::StmtMutator,
        public tir::ExprMutator {
151 152 153 154 155 156
   public:
    using StmtMutator::operator();
    using ExprMutator::operator();

   protected:
    // implementation
157
    PrimExpr VisitExpr_(const AddNode* op) final {
158 159
      return op->a;
    }
160 161 162
    Stmt VisitStmt_(const SeqStmtNode* op) final {
      return StmtMutator::VisitSeqStmt_(op, true);
    }
163
    PrimExpr VisitExpr(const PrimExpr& expr) final {
164 165 166
      return ExprMutator::VisitExpr(expr);
    }
  };
167
  auto fmakealloc = [&]() {
168
    auto z = x + 1;
169
    Stmt body = EvaluateNode::make(z);
170
    Var buffer("b", DataType::Handle());
171
    return AllocateNode::make(buffer, DataType::Float(32), {1, z}, const_true(), body);
172 173
  };

174 175
  auto fmakeif = [&]() {
    auto z = x + 1;
176 177
    Stmt body = EvaluateNode::make(z);
    return IfThenElseNode::make(x, EvaluateNode::make(0), body);
178 179
  };

180 181
  MyVisitor v;
  {
182
    auto body = fmakealloc();
183 184 185
    Stmt body2 = EvaluateNode::make(1);
    Stmt bref = body.as<AllocateNode>()->body;
    auto* extentptr = body.as<AllocateNode>()->extents.get();
186 187 188 189 190
    Array<Stmt> arr{std::move(body), body2, body2};
    auto* arrptr = arr.get();
    arr.MutateByApply([&](Stmt s) { return v(std::move(s)); });
    CHECK(arr.get() == arrptr);
    // inplace update body
191 192
    CHECK(arr[0].as<AllocateNode>()->extents[1].same_as(x));
    CHECK(arr[0].as<AllocateNode>()->extents.get() == extentptr);
193
    // copy because there is additional refs
194 195 196
    CHECK(!arr[0].as<AllocateNode>()->body.same_as(bref));
    CHECK(arr[0].as<AllocateNode>()->body.as<EvaluateNode>()->value.same_as(x));
    CHECK(bref.as<EvaluateNode>()->value.as<AddNode>());
197 198
  }
  {
199
    Array<Stmt> arr{fmakealloc()};
200 201 202 203 204
    // mutate array get reference by another one, triiger copy.
    Array<Stmt> arr2 = arr;
    auto* arrptr = arr.get();
    arr.MutateByApply([&](Stmt s) { return v(std::move(s)); });
    CHECK(arr.get() != arrptr);
205 206
    CHECK(arr[0].as<AllocateNode>()->extents[1].same_as(x));
    CHECK(!arr2[0].as<AllocateNode>()->extents[1].same_as(x));
207 208 209 210 211 212
    // mutate but no content change.
    arr2 = arr;
    arr.MutateByApply([&](Stmt s) { return v(std::move(s)); });
    CHECK(arr2.get() == arr.get());
  }
  {
213 214
    Array<Stmt> arr{fmakeif()};
    arr.MutateByApply([&](Stmt s) { return v(std::move(s)); });
215
    CHECK(arr[0].as<IfThenElseNode>()->else_case.as<EvaluateNode>()->value.same_as(x));
216 217 218 219 220 221 222
    // mutate but no content change.
    auto arr2 = arr;
    arr.MutateByApply([&](Stmt s) { return v(std::move(s)); });
    CHECK(arr2.get() == arr.get());
  }

  {
223
    auto body = EvaluateNode::make(CallNode::make(DataType::Int(32), "xyz", {x + 1}, CallNode::Extern));
224
    auto res = v(std::move(body));
225
    CHECK(res.as<EvaluateNode>()->value.as<CallNode>()->args[0].same_as(x));
226
  }
227 228
  {
    auto body = fmakealloc();
229
    Stmt body2 = EvaluateNode::make(1);
230
    auto* ref2 = body2.get();
231
    auto* extentptr = body.as<AllocateNode>()->extents.get();
232 233 234 235 236 237 238
    // construct a recursive SeqStmt.
    body = SeqStmt({body});
    body = SeqStmt({body, body2});
    body = SeqStmt({body, body2});
    body = v(std::move(body));
    // the seq get flattened
    CHECK(body.as<SeqStmtNode>()->size() == 3);
239
    CHECK(body.as<SeqStmtNode>()->seq[0].as<AllocateNode>()->extents.get() == extentptr);
240 241 242 243 244 245
    CHECK(body.as<SeqStmtNode>()->seq[1].get() == ref2);
  }

  {
    // Cannot cow because of bref
    auto body = fmakealloc();
246 247
    Stmt body2 = EvaluateNode::make(1);
    auto* extentptr = body.as<AllocateNode>()->extents.get();
248 249 250 251 252 253
    // construct a recursive SeqStmt.
    body = SeqStmt({body});
    auto bref = body;
    body = SeqStmt({body, body2});
    body = v(std::move(body));
    // the seq get flattened
254
    CHECK(body.as<SeqStmtNode>()->seq[0].as<AllocateNode>()->extents.get() != extentptr);
255
  }
256 257
}

tqchen committed
258 259 260 261 262
int main(int argc, char ** argv) {
  testing::InitGoogleTest(&argc, argv);
  testing::FLAGS_gtest_death_test_style = "threadsafe";
  return RUN_ALL_TESTS();
}