rewrite_simplify.h 4.5 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 24 25 26 27 28 29 30 31 32
/*!
 * \file rewrite_simplify.h
 * \brief Rewrite-rule based simplification.
 */
#ifndef TVM_ARITHMETIC_REWRITE_SIMPLIFY_H_
#define TVM_ARITHMETIC_REWRITE_SIMPLIFY_H_

#include <tvm/arithmetic.h>
#include <tvm/expr_operator.h>
#include <tvm/ir_mutator.h>
#include <unordered_map>
#include "const_fold.h"
#include "pattern_match.h"
33
#include "ir_mutator_with_analyzer.h"
34 35 36 37 38 39 40 41 42 43 44

namespace tvm {
namespace arith {

using namespace ir;

/*!
 * \brief Rewrite-based simplifier.
 *
 * This class can be inheritated for other simplifiers.
 */
45
class RewriteSimplifier::Impl : public IRMutatorWithAnalyzer {
46
 public:
47 48
  using IRMutatorWithAnalyzer::Mutate_;

49
  explicit Impl(Analyzer* parent)
50
      : IRMutatorWithAnalyzer(parent) {}
51 52 53 54 55 56 57

  void Update(const Var& var, const Expr& info, bool override);
  Expr Mutate_(const Add* op, const Expr& self) override;
  Expr Mutate_(const Sub* op, const Expr& self) override;
  Expr Mutate_(const Mul* op, const Expr& self) override;
  Expr Mutate_(const Div* op, const Expr& self) override;
  Expr Mutate_(const Mod* op, const Expr& self) override;
58 59
  Expr Mutate_(const FloorDiv* op, const Expr& self) override;
  Expr Mutate_(const FloorMod* op, const Expr& self) override;
60 61 62 63 64 65 66 67 68 69 70 71 72
  Expr Mutate_(const Min* op, const Expr& self) override;
  Expr Mutate_(const Max* op, const Expr& self) override;
  Expr Mutate_(const EQ* op, const Expr& self) override;
  Expr Mutate_(const NE* op, const Expr& self) override;
  Expr Mutate_(const LT* op, const Expr& self) override;
  Expr Mutate_(const LE* op, const Expr& self) override;
  Expr Mutate_(const GT* op, const Expr& self) override;
  Expr Mutate_(const GE* op, const Expr& self) override;
  Expr Mutate_(const And* op, const Expr& self) override;
  Expr Mutate_(const Or* op, const Expr& self) override;
  Expr Mutate_(const Not* op, const Expr& self) override;
  Expr Mutate_(const Select* op, const Expr& self) override;
  Expr Mutate_(const Call* op, const Expr& self) override;
73
  Expr Mutate_(const Variable* op, const Expr& self) override;
74
  Expr Mutate_(const Cast* op, const Expr& self) override;
75
  Expr Mutate_(const Let* op, const Expr& self) override;
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

 protected:
  /*! \brief internal structure for comparison. */
  enum CompareResult {
    kUnknown,
    kEQ,
    kGT,
    kGE,
    kLT,
    kLE,
    kNE
  };
  // counter to record recursive rewrite depth.
  int recur_depth_{0};
  // internal variable map
  std::unordered_map<Var, Expr, ExprHash, ExprEqual> var_map_;
  // maximum number of recursion allowed during a single pass.
  static const constexpr int kMaxRecurDepth = 5;

  /*!
   * \brief try to compare x against val.
   * \param x The expression to be evaluated.
   * \param val The constant value.
   * \return comparison result.
   */
  CompareResult TryCompare(const Expr& x, int64_t val);

 private:
  // Whether x >= val
  bool CanProveGreaterEqual(const Expr& x, int64_t val) {
106
    return analyzer_->CanProveGreaterEqual(x, val);
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  }
  // Whether x == val
  bool CanProveEqual(const Expr& x, int64_t val) {
    // TODO(tqchen) refer back to super-analyzer.
    return TryCompare(x, val) == kEQ;
  }

  // Recursive rewrite x
  // we limit maximum depth of recursive rewrite allowed to
  // avoid infinite loop
  Expr RecursiveRewrite(const Expr& x) {
    if (recur_depth_ >= kMaxRecurDepth) return x;
    ++recur_depth_;
    Expr res = Mutate(x);
    --recur_depth_;
    return res;
  }

  template<typename TA>
  PConstWithTypeLike<TA> ZeroWithTypeLike(const Pattern<TA>& pattern) {
    return PConstWithTypeLike<TA>(pattern.derived(), 0);
  }
129 130 131 132 133

  template<typename TA>
  PConstWithTypeLike<TA> OneWithTypeLike(const Pattern<TA>& pattern) {
    return PConstWithTypeLike<TA>(pattern.derived(), 1);
  }
134 135 136 137 138 139
};


}  // namespace arith
}  // namespace tvm
#endif  // TVM_ARITHMETIC_REWRITE_SIMPLIFY_H_