parallel.h 4.23 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
/*!
 * \file tl/op/parallel.h
 * \brief Infer layout from ops and parallel for
 */

#ifndef TVM_TL_OP_PARALLEL_H_
#define TVM_TL_OP_PARALLEL_H_

#include <tvm/target/target.h>
#include <tvm/tir/stmt_functor.h>

#include "../layout/layout.h"
13
14
#include "../transform/layout_reducer.h"
#include "./operator.h"
15
16
17
18
19
20

namespace tvm {
namespace tl {

using namespace tir;

21
22
23
24
25
26
27
28
29
30
31
32
33
34
class LayoutConflictException : public std::exception {
public:
  const char *what() const noexcept override { return msg_.c_str(); }
  LayoutConflictException(const std::string &msg) : msg_(msg) {}

private:
  std::string msg_;
};

bool ProveFragmentContains(Fragment small_frag, Fragment large_frag,
                           Array<PrimExpr> small_frag_indices,
                           Array<PrimExpr> large_frag_indices,
                           arith::Analyzer &analyzer_);

35
class ParallelOpNode;
36
37

class ParallelLoopNestVisitor : public StmtExprVisitor {
38
private:
39
40
41
42
  ParallelLoopNestVisitor(ParallelOpNode *op) : p(op){};
  void VisitStmt_(const ForNode *op) override;
  void VisitStmt_(const BufferStoreNode *op) override;
  void VisitExpr_(const BufferLoadNode *op) override;
43

44
  ParallelOpNode *p;
45

46
  friend class ParallelOpNode;
47
48
};

49
50
51
52
// ParallelOpNode represents a parallel for loop operator in TileLang.
// It is responsible for inferring layouts, holding loop structure, and managing
// predicates.
class ParallelOpNode : public TileOperatorNode {
53
public:
54
55
56
57
58
  // The inferred layout for the loop, mutable to allow lazy inference.
  mutable Fragment loop_layout_;
  // The predicate expression for the loop, if any, mutable for lazy
  // construction.
  mutable Optional<PrimExpr> predicate_;
59

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  // Type key for TVM object system.
  static constexpr const char *_type_key = "tl.ParallelOp";
  TVM_DECLARE_FINAL_OBJECT_INFO(ParallelOpNode, TileOperatorNode);

  // Construct from a root For loop.
  ParallelOpNode(For root);

  // Lower the operator to a TIR statement.
  Stmt Lower(const LowerArgs &T, arith::Analyzer *analyzer) const override;

  // Infer the layout for this parallel operator.
  LayoutMap InferLayout(const LayoutInferArgs &T,
                        InferLevel level) const override;

  // Copy constructor for ParallelOpNode.
  ParallelOpNode(const ParallelOpNode &other) : ParallelOpNode(other.root_) {
76
77
78
79
    loop_layout_ = other.loop_layout_;
    predicate_ = other.predicate_;
  }

80
  // Get the inferred loop layout.
81
  Fragment GetLoopLayout() const { return loop_layout_; }
82
  // Get the root For loop.
83
  For GetRoot() const { return root_; }
84
  // Get the mapping from buffer to access indices.
85
  Map<Buffer, Array<PrimExpr>> GetIndiceMap() const { return indice_map_; }
86
  // Get the predicate for a given thread variable.
87
88
  Optional<PrimExpr> GetPredicate(Var thread_var) const;

89
90
91
  // Clone this operator.
  TileOperator Clone() const;

92
private:
93
94
95
  // Complete the fragment layout for a given buffer.
  Fragment CompleteBufferFragment(const Buffer &buffer) const;
  // Check if the buffer is accessed with common indices (i.e., loop variables).
96
  bool IsCommonAccessIndice(const Buffer &buffer) const;
97
98
  // Add a predicate to the current predicate expression.
  void AddPredicate(PrimExpr expr) const {
99
100
    predicate_ = predicate_.defined() ? And(expr, predicate_.value()) : expr;
  }
101
102
  // Allow ParallelLoopNestVisitor to access private members.
  friend class ParallelLoopNestVisitor;
103

104
  // The root For loop node.
105
  For root_;
106
  // Visitor for collecting loop nest information.
107
  ParallelLoopNestVisitor V;
108
  // Mapping from buffer to their access indices in the loop.
109
  Map<Buffer, Array<PrimExpr>> indice_map_;
110
  // Set of buffers that are written to in the loop.
111
  std::unordered_set<Buffer, ObjectPtrHash, ObjectPtrEqual> buffer_is_write_;
112
  // The loop variables for the parallel loop nest.
113
  Array<IterVar> loop_vars_;
114
  // Analyzer for simplifying and analyzing expressions, mutable for lazy use.
115
  mutable arith::Analyzer analyzer_;
116
117
  // Mapping from buffer to reducer info.
  Map<Var, ReducerInfo> reducer_info_map_;
118
};
119

120
121
122
123
124
125
126
127
class ParallelOp : public TileOperator {
public:
  TVM_DEFINE_OBJECT_REF_METHODS(ParallelOp, TileOperator, ParallelOpNode);

  ParallelOp(For root) {
    auto op = make_object<ParallelOpNode>(root);
    data_ = std::move(op);
  }
128
129
};

130
131
} // namespace tl
} // namespace tvm
132

133
#endif // TVM_TL_OP_PARALLEL_H_