well_formed.cc 3.46 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.
 */

20 21 22 23
/*!
 * \file well_formed.cc
 * \brief check that expression is well formed.
 */
Zhi committed
24
#include <tvm/relay/analysis.h>
25
#include <tvm/relay/expr_functor.h>
26
#include <tvm/relay/pattern_functor.h>
27 28 29 30 31 32
#include <unordered_set>

namespace tvm {
namespace relay {


33
//! brief make sure each Var is bound at most once in a scope.
34
class WellFormedChecker : private ExprVisitor, PatternVisitor {
35 36
  bool well_formed = true;

37 38 39 40
  std::vector<std::unordered_set<Var, ObjectHash, ObjectEqual>> scope;
  std::unordered_set<Var, ObjectHash, ObjectEqual> current_bound;
  std::unordered_set<Var, ObjectHash, ObjectEqual> total_bound;
  std::unordered_set<Var, ObjectHash, ObjectEqual> free;
41

42 43 44
  struct Scope {
    WellFormedChecker* wfc;
    explicit Scope(WellFormedChecker* wfc) : wfc(wfc) {
45
      wfc->scope.push_back({{}});
46 47 48 49 50 51 52 53 54 55 56 57 58
    }
    ~Scope() {
      CHECK_GE(wfc->scope.size(), 0);
      for (const Var& v : wfc->scope.back()) {
        CHECK_GE(wfc->current_bound.count(v), 0);
        wfc->current_bound.erase(v);
      }
      wfc->scope.pop_back();
    }
  };

  void Bound(const Var& v) {
    if (current_bound.count(v) != 0 || total_bound.count(v) != 0 || free.count(v) != 0) {
59 60
      well_formed = false;
    }
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    CHECK_GE(scope.size(), 0);
    scope.back().insert(v);
    current_bound.insert(v);
    total_bound.insert(v);
  }

  void VisitExpr_(const VarNode* op) final {
    Var v = GetRef<Var>(op);
    if (current_bound.count(v) == 0) {
      if (total_bound.count(v) != 0) {
        well_formed = false;
      } else {
        free.insert(v);
      }
    }
76 77
  }

78
  void VisitExpr_(const LetNode* l) final {
79
    Scope s(this);
80 81
    // we do letrec only for FunctionNode,
    // but shadowing let in let binding is likely programming error, and we should forbidden it.
82
    Bound(l->var);
83 84 85 86
    CheckWellFormed(l->value);
    CheckWellFormed(l->body);
  }

87
  void VisitExpr_(const FunctionNode* f) final {
88
    Scope s(this);
89
    for (const Var& param : f->params) {
90
      Bound(param);
91 92 93 94
    }
    CheckWellFormed(f->body);
  }

95 96 97 98 99 100
  void VisitClause(const Clause& c) final {
    Scope s(this);
    VisitPattern(c->lhs);
    VisitExpr(c->rhs);
  }

101 102 103 104 105
  void VisitPattern(const Pattern& p) final {
    PatternVisitor::VisitPattern(p);
  }

  void VisitVar(const Var& v) final {
106 107 108 109 110 111 112 113 114
    Bound(v);
  }

  void VisitExpr(const Expr& e) final {
    if (auto v = e.as<VarNode>()) {
      VisitExpr_(v);
    } else {
      ExprVisitor::VisitExpr(e);
    }
115 116
  }

117
 public:
118
  bool CheckWellFormed(const Expr& e) {
119 120 121 122 123
    this->VisitExpr(e);
    return well_formed;
  }
};

124
bool WellFormed(const Expr& e) {
125 126 127
  return WellFormedChecker().CheckWellFormed(e);
}

128
TVM_REGISTER_GLOBAL("relay._analysis.well_formed")
129
.set_body_typed(WellFormed);
130 131 132

}  // namespace relay
}  // namespace tvm