ir_visitor_with_analyzer.h 2.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * 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
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 */

/*!
 * \file tvm/arithmetic/ir_visitor_with_analyzer.h
 * \brief IR visitor class with an analyzer context.
 */

25 26
#ifndef TVM_ARITH_IR_VISITOR_WITH_ANALYZER_H_
#define TVM_ARITH_IR_VISITOR_WITH_ANALYZER_H_
27

28
#include <tvm/arith/analyzer.h>
29 30
#include <tvm/tir/expr.h>
#include <tvm/tir/stmt_functor.h>
31 32

namespace tvm {
33
namespace tir {
34

35
class IRVisitorWithAnalyzer final : public StmtExprVisitor {
36
 public:
37
  PrimExpr Simplify(const PrimExpr& expr) {
38 39 40
    return analyzer_.Simplify(expr);
  }

41
  void VisitStmt_(const ForNode* op) {
42 43
    analyzer_.Bind(op->loop_var,
                   Range::make_by_min_extent(op->min, op->extent));
44
    return StmtExprVisitor::VisitStmt_(op);
45 46
  }

47
  void VisitStmt_(const AttrStmtNode* op) {
48 49
    if (op->attr_key == attr::thread_extent ||
        op->attr_key == attr::virtual_thread) {
50
      IterVar iv = Downcast<IterVar>(op->node);
51 52 53
      CHECK_NE(iv->thread_tag.length(), 0U);
      analyzer_.Bind(iv->var,
                      Range::make_by_min_extent(0, op->value));
54
      StmtExprVisitor::VisitStmt_(op);
55
    } else {
56
      StmtExprVisitor::VisitStmt_(op);
57 58 59
    }
  }

60
  void VisitExpr_(const ReduceNode* op) {
61 62 63 64 65
    // Setup the domain information before simplification.
    for (const IterVar& iv : op->axis) {
      analyzer_.Bind(iv->var, iv->dom);
    }
    // Recursively call simplification when necessary.
66
    StmtExprVisitor::VisitExpr_(op);
67 68 69 70 71 72 73
  }

 protected:
  /*! \brief internal analyzer field. */
  arith::Analyzer analyzer_;
};

74
}  // namespace tir
75
}  // namespace tvm
76
#endif  // TVM_ARITH_IR_VISITOR_WITH_ANALYZER_H_