FreeTensor
Loading...
Searching...
No Matches
cache.h
Go to the documentation of this file.
1#ifndef FREE_TENSOR_CACHE_H
2#define FREE_TENSOR_CACHE_H
3
6#include <mutator.h>
7
8namespace freetensor {
9
10class MakeCacheVar : public Mutator {
11 ID stmt_;
12 std::string oldVar_, newVar_;
13 ID oldDef_, newDef_;
14 MemType mtype_;
15 VarDef def_;
16 bool inStmt_ = false;
17
18 public:
19 MakeCacheVar(const ID &stmt, const std::string &oldVar, MemType mtype,
20 bool isReduction)
21 : stmt_(stmt), oldVar_(oldVar), mtype_(mtype) {
22 newVar_ = oldVar_ + (isReduction ? ".r" : ".c");
23 switch (mtype) {
25 newVar_ += ".local";
26 break;
28 newVar_ += ".shared";
29 break;
32 newVar_ += ".global";
33 break;
35 newVar_ += ".warp";
36 default:;
37 }
38 }
39
40 const std::string &newVar() const { return newVar_; }
41 const ID &oldDef() const { return oldDef_; }
42 const ID &newDef() const { return newDef_; }
43
44 protected:
45 Stmt visitStmt(const Stmt &op) override;
46 Stmt visit(const VarDef &op) override;
47 Expr visit(const Load &op) override;
48 Stmt visit(const Store &op) override;
49 Stmt visit(const ReduceTo &op) override;
50};
51
52class MakeFillAndFlush : public SymbolTable<Mutator> {
54
55 ID stmt_;
56 std::string oldVar_, newVar_;
57 ID oldDef_, fillStmt_, flushStmt_;
58 const AccessBound &rwRange_, &wRange_;
59 VarDef def_;
60
61 public:
62 MakeFillAndFlush(const ID &stmt, const std::string &oldVar,
63 const std::string &newVar, const ID &oldDef,
64 const AccessBound &rwRange, const AccessBound &wRange)
65 : stmt_(stmt), oldVar_(oldVar), newVar_(newVar), oldDef_(oldDef),
66 rwRange_(rwRange), wRange_(wRange) {}
67
68 const ID &fillStmt() const { return fillStmt_; }
69 const ID &flushStmt() const { return flushStmt_; }
70
71 protected:
72 Stmt visitStmt(const Stmt &op) override;
73 Stmt visit(const VarDef &op) override;
74};
75
76class MakeInitAndReduce : public SymbolTable<Mutator> {
78
79 ID stmt_;
80 std::string oldVar_, newVar_;
81 ID oldDef_, newDef_, initStmt_, reduceStmt_;
82 const AccessBound &range_;
83 VarDef def_;
84 ReduceTo reduce_;
85 bool inNewVar_ = false;
86
87 public:
88 MakeInitAndReduce(const ID &stmt, const std::string &oldVar,
89 const std::string &newVar, const ID &oldDef,
90 const ID &newDef, const AccessBound &range)
91 : stmt_(stmt), oldVar_(oldVar), newVar_(newVar), oldDef_(oldDef),
92 newDef_(newDef), range_(range) {}
93
94 const ID &initStmt() const { return initStmt_; }
95 const ID &reduceStmt() const { return reduceStmt_; }
96
97 protected:
98 using BaseClass::visit;
99 Stmt visitStmt(const Stmt &op) override;
100 Stmt visit(const VarDef &op) override;
101 Stmt visit(const ReduceTo &op) override;
102 Stmt visit(const Store &op) override;
103 Expr visit(const Load &op) override;
104};
105
106std::pair<Stmt, std::tuple<ID, ID, std::string, ID>>
107cache(const Stmt &ast, const ID &stmt, const std::string &var, MemType mtype);
108
109std::pair<Stmt, std::tuple<ID, ID, std::string, ID>>
110cacheReduction(const Stmt &ast, const ID &stmt, const std::string &var,
111 MemType mtype);
112
113} // namespace freetensor
114
115#endif // FREE_TENSOR_CACHE_H
Definition: id.h:18
Definition: cache.h:10
MakeCacheVar(const ID &stmt, const std::string &oldVar, MemType mtype, bool isReduction)
Definition: cache.h:19
const std::string & newVar() const
Definition: cache.h:40
Stmt visit(const VarDef &op) override
Definition: cache.cc:36
const ID & newDef() const
Definition: cache.h:42
const ID & oldDef() const
Definition: cache.h:41
Stmt visitStmt(const Stmt &op) override
Definition: cache.cc:16
Definition: cache.h:52
Stmt visitStmt(const Stmt &op) override
Definition: cache.cc:81
MakeFillAndFlush(const ID &stmt, const std::string &oldVar, const std::string &newVar, const ID &oldDef, const AccessBound &rwRange, const AccessBound &wRange)
Definition: cache.h:62
Stmt visit(const VarDef &op) override
Definition: cache.cc:144
const ID & fillStmt() const
Definition: cache.h:68
const ID & flushStmt() const
Definition: cache.h:69
Definition: cache.h:76
Stmt visit(const VarDef &op) override
Definition: cache.cc:216
const ID & initStmt() const
Definition: cache.h:94
const ID & reduceStmt() const
Definition: cache.h:95
MakeInitAndReduce(const ID &stmt, const std::string &oldVar, const std::string &newVar, const ID &oldDef, const ID &newDef, const AccessBound &range)
Definition: cache.h:88
Stmt visitStmt(const Stmt &op) override
Definition: cache.cc:155
Definition: mutator.h:11
Definition: symbol_table.h:122
BaseClass::StmtRetType visit(const VarDef &op) override
Definition: symbol_table.h:167
Definition: allocator.h:9
std::pair< Stmt, std::tuple< ID, ID, std::string, ID > > cache(const Stmt &ast, const ID &stmt, const std::string &var, MemType mtype)
Definition: cache.cc:264
Ref< StmtNode > Stmt
Definition: ast.h:152
std::pair< Stmt, std::tuple< ID, ID, std::string, ID > > cacheReduction(const Stmt &ast, const ID &stmt, const std::string &var, MemType mtype)
Definition: cache.cc:295
PBSet range(T &&map)
Definition: presburger.h:1054
MemType
Definition: mem_type.h:14
Definition: comp_access_bound.h:15