stmt_simplify.cc 3.64 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 stmt_simplify.cc
 * \brief Statement simplifier based on analyzer
 */
24 25
#include <tvm/tir/expr.h>
#include <tvm/tir/ir_pass.h>
26
#include <tvm/arith/analyzer.h>
27
#include <tvm/tir/op.h>
28
#include <tvm/arith/analyzer.h>
29
#include "ir_mutator_with_analyzer.h"
30 31 32

namespace tvm {
namespace arith {
33

34
using namespace tir;
35

36
class StmtSimplifier : public IRMutatorWithAnalyzer {
37
 public:
38 39 40 41
  explicit StmtSimplifier(Analyzer* analyzer)
      : IRMutatorWithAnalyzer(analyzer) {}

  using Parent = IRMutatorWithAnalyzer;
42 43
  using Parent::VisitStmt;
  using Parent::VisitStmt_;
44

45
  PrimExpr VisitExpr(const PrimExpr& expr) final {
46
    return analyzer_->Simplify(expr);
47 48
  }

49
  Stmt Simplify(Stmt stmt) {
50
    return operator()(std::move(stmt));
51 52
  }

53
  Stmt VisitStmt_(const ForNode* op) final {
54 55 56
    analyzer_->Bind(op->loop_var, Range::make_by_min_extent(op->min, op->extent));
    With<ConstraintContext> ctx1(analyzer_, op->loop_var >= op->min);
    With<ConstraintContext> ctx2(analyzer_, op->loop_var < op->min + op->extent);
57
    return Parent::VisitStmt_(op);
58 59
  }

60
  Stmt VisitStmt_(const LetStmtNode* op) {
61
    PrimExpr value = this->VisitExpr(op->value);
62
    if (!tir::HasSideEffect(value)) {
63 64 65
      // it is fine to discard the let binding
      // because the call to simplify will always inline the var.
      analyzer_->Bind(op->var, value);
66
      return this->VisitStmt(op->body);
67
    }
68
    Stmt body = this->VisitStmt(op->body);
69 70
    if (value.same_as(op->value) &&
        body.same_as(op->body)) {
71
      return GetRef<Stmt>(op);
72
    } else {
73 74 75 76
      auto n = this->CopyOnWrite(op);
      n->value = std::move(value);
      n->body = std::move(body);
      return Stmt(n);
77 78 79
    }
  }

80
  // eliminate useless stores
81
  Stmt VisitStmt_(const StoreNode* op) final {
82
    Stmt stmt = Parent::VisitStmt_(op);
83 84
    op = stmt.as<StoreNode>();
    if (const LoadNode* load = op->value.as<LoadNode>()) {
85 86
      if (load->buffer_var.same_as(op->buffer_var) &&
          Equal(load->index, op->index)) {
87
        return EvaluateNode::make(0);
88 89
      }
    }
90
    return GetRef<Stmt>(op);
91
  }
92 93 94 95
};

}  // namespace arith

96
namespace tir {
97 98

Stmt CanonicalSimplify(Stmt stmt, Map<Var, Range> vrange) {
99 100 101 102
  arith::Analyzer analyzer;
  for (auto kv : vrange) {
    analyzer.Bind(kv.first, kv.second);
  }
103
  return arith::StmtSimplifier(&analyzer).Simplify(std::move(stmt));
104 105
}

106
PrimExpr CanonicalSimplify(PrimExpr expr, Map<Var, Range> vrange) {
107 108 109 110 111 112 113
  arith::Analyzer analyzer;
  for (auto kv : vrange) {
    analyzer.Bind(kv.first, kv.second);
  }
  return analyzer.canonical_simplify(expr);
}

114
PrimExpr Simplify(PrimExpr expr, Map<Var, Range> vrange) {
115
  arith::Analyzer analyzer;
116
  for (auto kv : vrange) {
117
    analyzer.Bind(kv.first, kv.second);
118
  }
119 120
  expr = analyzer.Simplify(expr);
  return expr;
121 122
}

123
Stmt Simplify(Stmt stmt, Map<Var, Range> vrange) {
124
  return CanonicalSimplify(std::move(stmt), vrange);
125
}
126
}  // namespace tir
127
}  // namespace tvm