FreeTensor
Loading...
Searching...
No Matches
access_type.h
Go to the documentation of this file.
1#ifndef FREE_TENSOR_ACCESS_TYPE_H
2#define FREE_TENSOR_ACCESS_TYPE_H
3
4#include <array>
5#include <iostream>
6#include <string>
7
8#include <container_utils.h>
9#include <except.h>
10#include <serialize/to_string.h>
11
12namespace freetensor {
13
42enum class AccessType : size_t {
43 Input = 0,
44 Bypass,
45 Cache,
46 Output,
48 InOut,
49 // ------
51};
52
53// First deduce array length, then assert, to ensure the length
54constexpr std::array accessTypeNames = {
55 "input", "bypass", "cache", "output", "input-mutable", "inout",
56};
57static_assert(accessTypeNames.size() == (size_t)AccessType::NumTypes);
58
59namespace detail {
60
61template <typename T, T... i>
62constexpr auto createAllAccessTypes(std::integer_sequence<T, i...>) {
63 return std::array{(AccessType)i...};
64}
65
66} // namespace detail
67
69 std::make_index_sequence<(size_t)AccessType::NumTypes>{});
70
71inline std::ostream &operator<<(std::ostream &os, AccessType atype) {
72 return os << accessTypeNames.at((size_t)atype);
73}
74
75inline AccessType parseAType(const std::string &_str) {
76 auto &&str = tolower(_str);
77 for (auto &&[i, s] : views::enumerate(accessTypeNames)) {
78 if (s == str) {
79 return (AccessType)i;
80 }
81 }
82 ERROR(FT_MSG << "Unrecognized access type \"" << _str
83 << "\". Candidates are (case-insensitive): "
84 << (accessTypeNames | join(", ")));
85}
86
87inline bool isWritable(AccessType atype) {
88 switch (atype) {
93 return true;
94 default:
95 return false;
96 }
97}
98
99inline bool isInputting(AccessType atype) {
100 switch (atype) {
105 return true;
106 default:
107 return false;
108 }
109}
110
111inline bool isOutputting(AccessType atype) {
112 switch (atype) {
116 return true;
117 default:
118 return false;
119 }
120}
121
123 switch (atype) {
125 return AccessType::Bypass;
127 return AccessType::Output;
129 return AccessType::InOut;
130 default:
131 return atype;
132 }
133}
134
136 switch (atype) {
138 return AccessType::Input;
140 return AccessType::Cache;
143 default:
144 return atype;
145 }
146}
147
148} // namespace freetensor
149
150#endif // FREE_TENSOR_ACCESS_TYPE_H
#define ERROR(msg)
Definition: except.h:141
#define FT_MSG
Definition: except.h:23
constexpr auto createAllAccessTypes(std::integer_sequence< T, i... >)
Definition: access_type.h:62
Definition: allocator.h:9
AccessType parseAType(const std::string &_str)
Definition: access_type.h:75
constexpr std::array accessTypeNames
Definition: access_type.h:54
std::string tolower(const std::string &s)
Definition: container_utils.h:142
AccessType
Definition: access_type.h:42
std::string join(Container &&c, const std::string &splitter)
Definition: container_utils.h:194
AccessType addOutputting(AccessType atype)
Definition: access_type.h:122
bool isInputting(AccessType atype)
Definition: access_type.h:99
bool isWritable(AccessType atype)
Definition: access_type.h:87
constexpr auto allAccessTypes
Definition: access_type.h:68
std::ostream & operator<<(std::ostream &os, const Dependence &dep)
Definition: deps.cc:1404
AccessType removeOutputting(AccessType atype)
Definition: access_type.h:135
bool isOutputting(AccessType atype)
Definition: access_type.h:111