FreeTensor
Loading...
Searching...
No Matches
structural_feature.h
Go to the documentation of this file.
1#ifndef FREE_TENSOR_STRUCTURAL_FEATURE_H
2#define FREE_TENSOR_STRUCTURAL_FEATURE_H
3
4#include <memory>
5#include <unordered_map>
6#include <unordered_set>
7
12#include <visitor.h>
13
14namespace freetensor {
15
20 // -1 means unknown
21 std::unordered_map<BaseDataType, int64_t> opCnt_;
22 std::unordered_map<MemType, int64_t> loadCnt_, storeCnt_,
23 accessCnt_; // Memory access count
24 std::unordered_map<MemType, int64_t> loadArea_, storeArea_,
25 accessArea_; // memory footprint
26};
27
38class StructuralFeature : public CompTransientBounds<SymbolTable<Visitor>> {
40
44 struct NodeInfo {
45 std::unordered_map<BaseDataType, int64_t> opCnt_;
46 std::unordered_map<MemType, int64_t> loadCnt_, storeCnt_, accessCnt_;
47
48 std::unordered_map<MemType, int64_t> innerLoadArea_, innerStoreArea_,
49 innerAccessArea_;
50
51 // buffer name -> all accesses of this buffer
52 // If a key does not exist in loads_, stores_ or accesses_, it
53 // means that a node does not access the buffer
54 std::unordered_map<std::string, std::vector<CompAccessBound::Access>>
55 loads_, stores_, accesses_;
56 };
57
59
60 std::unordered_map<ID, NodeFeature> features_; // Node ID -> features
61 std::unordered_map<AST, NodeInfo> info_; // AST -> info
62
63 public:
64 const std::unordered_map<ID, NodeFeature> &features() const {
65 return features_;
66 }
67
68 private:
69 void updCompInfo(const AST &parent, const AST &child, int repeat = 1);
70 void updAccCntInfo(const AST &parent, const AST &child, int repeat = 1);
71 void updAreaInfo(const AST &parent, const AST &child);
72 void updInfo(const AST &parent, const AST &child,
73 int repeat = 1); // repeat = -1 means unknown
74
75 void calcCompFeatures(const Stmt &parent);
76 void calcAccCntFeatures(const Stmt &parent);
77 int64_t calcArea(const std::string &var,
78 const std::vector<CompAccessBound::Access> &accesses);
79 void calcAreaFeatures(const Stmt &parent);
80 void calcFeatures(const Stmt &parent);
81
82 void visitBinOp(const BinaryExpr &op);
83 void visitUnaryOp(const UnaryExpr &op);
84
85 protected:
86 using BaseClass::visit;
87
88 void visitStmt(const Stmt &op) override;
89 void visitExpr(const Expr &op) override;
90
91 void visit(const Load &op) override;
92 void visit(const Store &op) override;
93 void visit(const ReduceTo &op) override;
94
95 void visit(const Cast &op) override;
96 void visit(const IfExpr &op) override;
97
98 void visit(const StmtSeq &op) override;
99 void visit(const If &op) override;
100 void visit(const Assert &op) override;
101 void visit(const For &op) override;
102 void visit(const VarDef &op) override;
103};
104
105inline std::unordered_map<ID, NodeFeature> structuralFeature(const Stmt &op) {
106 StructuralFeature visitor;
107 visitor(op);
108 return visitor.features();
109}
110
111} // namespace freetensor
112
113#endif // FREE_TENSOR_STRUCTURAL_FEATURE_H
Definition: comp_transient_bounds.h:50
BaseClass::StmtRetType visit(const For &op) override
Definition: comp_transient_bounds.h:128
Definition: ref.h:24
Definition: structural_feature.h:38
void visit(const Load &op) override
Definition: structural_feature.cc:205
const std::unordered_map< ID, NodeFeature > & features() const
Definition: structural_feature.h:64
void visitExpr(const Expr &op) override
Definition: structural_feature.cc:195
void visitStmt(const Stmt &op) override
Definition: structural_feature.cc:187
Definition: allocator.h:9
std::unordered_map< ID, NodeFeature > structuralFeature(const Stmt &op)
Definition: structural_feature.h:105
Definition: structural_feature.h:19
std::unordered_map< BaseDataType, int64_t > opCnt_
Definition: structural_feature.h:21
std::unordered_map< MemType, int64_t > loadCnt_
Definition: structural_feature.h:22
std::unordered_map< MemType, int64_t > storeCnt_
Definition: structural_feature.h:22
std::unordered_map< MemType, int64_t > loadArea_
Definition: structural_feature.h:24
std::unordered_map< MemType, int64_t > accessCnt_
Definition: structural_feature.h:23
std::unordered_map< MemType, int64_t > storeArea_
Definition: structural_feature.h:24
std::unordered_map< MemType, int64_t > accessArea_
Definition: structural_feature.h:25