docc 0.8.3 (0d44999b)
C++ API Reference
Loading...
Searching...
No Matches
element.h
Go to the documentation of this file.
1#pragma once
2
3#include <cassert>
4#include <cstdint>
5#include <string>
6#include <string_view>
7#include <type_traits>
8#include <unordered_map>
9
10#include "sdfg/exceptions.h"
12
13namespace sdfg {
14
15namespace builder {
16class SDFGBuilder;
18} // namespace builder
19
20namespace deepcopy {
22} // namespace deepcopy
23
24namespace serializer {
25class JSONSerializer;
26} // namespace serializer
27
28class Function;
29
30class DebugInfo {
31private:
32 std::string filename_;
33 std::string function_;
34 size_t start_line_;
35 size_t start_column_;
36 size_t end_line_;
37 size_t end_column_;
38
39 bool has_;
40
41public:
43
44 DebugInfo(std::string filename, size_t start_line, size_t start_column, size_t end_line, size_t end_column);
45
47 std::string filename,
48 std::string function,
49 size_t start_line,
50 size_t start_column,
51 size_t end_line,
52 size_t end_column
53 );
54
55 bool has() const;
56
57 std::string filename() const;
58
59 std::string function() const;
60
61 size_t start_line() const;
62
63 size_t start_column() const;
64
65 size_t end_line() const;
66
67 size_t end_column() const;
68
69 static DebugInfo merge(const DebugInfo& left, const DebugInfo& right);
70};
71
72enum class ElementType : uint64_t {
73 State = 1LL,
74 InterstateEdge = 1LL << 1,
75 AccessNode = 1LL << 2,
76 ConstantNode = 1LL << 3,
77 Tasklet = 1LL << 4,
78 LibraryNode = 1LL << 5,
79 Memlet = 1LL << 6,
80 Return = 1LL << 7,
81 Block = 1LL << 8,
82 Sequence = 1LL << 9,
83 AssignmentBlock = 1LL << 10,
84 IfElse = 1LL << 11,
85 While = 1LL << 12,
86 Continue = 1LL << 13,
87 Break = 1LL << 14,
88 For = 1LL << 15,
89 Map = 1LL << 16,
90 Reduce = 1LL << 17,
91};
92
95 return static_cast<ElementType>(static_cast<uint64_t>(lhs) | static_cast<uint64_t>(rhs));
96}
97
99 return static_cast<ElementType>(static_cast<uint64_t>(lhs) & static_cast<uint64_t>(rhs));
100}
101
103 return static_cast<ElementType>(~static_cast<uint64_t>(type));
104}
105
107constexpr bool is_a(ElementType type, ElementType mask) {
108 return (static_cast<uint64_t>(type) & static_cast<uint64_t>(mask)) != 0;
109}
110
112constexpr std::string_view element_type_name(ElementType type) {
113 switch (type) {
115 return "state";
117 return "interstate_edge";
119 return "access_node";
121 return "constant_node";
123 return "tasklet";
125 return "library_node";
127 return "memlet";
129 return "return";
131 return "block";
133 return "sequence";
135 return "assignment_block";
137 return "if_else";
139 return "while";
141 return "continue";
143 return "break";
144 case ElementType::For:
145 return "for";
146 case ElementType::Map:
147 return "map";
149 return "reduce";
150 }
151 throw InvalidSDFGException("Element: Unknown element type");
152}
153
154inline std::ostream& operator<<(std::ostream& os, ElementType type) {
155 os << element_type_name(type);
156 return os;
157}
158
159class Element {
164
165protected:
168 std::unordered_map<std::string, std::string> metadata_;
169
170public:
172
173 virtual ~Element() = default;
174
175 size_t element_id() const;
176
177 const DebugInfo& debug_info() const;
178
180
181 void add_metadata(const std::string& key, const std::string& value);
182
183 void remove_metadata(const std::string& key);
184
185 const std::string& metadata(const std::string& key) const;
186
187 const std::string* metadata_if_exists(const std::string& key) const;
188
189 const std::unordered_map<std::string, std::string>& metadata() const;
190
194 virtual ElementType type_id() const = 0;
195
199 std::string_view element_type() const {
200 return element_type_name(this->type_id());
201 }
202
208 virtual void validate(const Function& function) const = 0;
209
210 virtual void replace(const symbolic::Expression old_expression, const symbolic::Expression new_expression) = 0;
211
212 virtual void replace(const symbolic::ExpressionMapping& replacements);
213};
214
215typedef size_t ElementId;
216
221template<typename T>
222using element_cast_target_t = std::remove_pointer_t<std::remove_reference_t<T>>;
223
234template<typename T>
235bool isa(const Element& element) {
236 return element_cast_target_t<T>::classof(element);
237}
238
239template<typename T>
240bool isa(const Element* element) {
241 return element != nullptr && element_cast_target_t<T>::classof(*element);
242}
243
245template<typename T>
247 using Target = element_cast_target_t<T>;
248 return (element != nullptr && Target::classof(*element)) ? static_cast<Target*>(element) : nullptr;
249}
250
251template<typename T>
253 using Target = element_cast_target_t<T>;
254 return (element != nullptr && Target::classof(*element)) ? static_cast<const Target*>(element) : nullptr;
255}
256
258template<typename T>
260 using Target = element_cast_target_t<T>;
261 if (!Target::classof(element)) {
262 throw InvalidSDFGException("dyn_cast: element is not of the requested type");
263 }
264 return static_cast<Target&>(element);
265}
266
267template<typename T>
269 using Target = element_cast_target_t<T>;
270 if (!Target::classof(element)) {
271 throw InvalidSDFGException("dyn_cast: element is not of the requested type");
272 }
273 return static_cast<const Target&>(element);
274}
275
276} // namespace sdfg
Definition element.h:30
std::string filename() const
bool has() const
size_t end_column() const
std::string function() const
size_t end_line() const
static DebugInfo merge(const DebugInfo &left, const DebugInfo &right)
DebugInfo(std::string filename, size_t start_line, size_t start_column, size_t end_line, size_t end_column)
size_t start_column() const
DebugInfo(std::string filename, std::string function, size_t start_line, size_t start_column, size_t end_line, size_t end_column)
size_t start_line() const
Definition element.h:159
void remove_metadata(const std::string &key)
void set_debug_info(const DebugInfo &debug_info)
size_t element_id_
Definition element.h:166
const std::string * metadata_if_exists(const std::string &key) const
const std::unordered_map< std::string, std::string > & metadata() const
virtual void replace(const symbolic::Expression old_expression, const symbolic::Expression new_expression)=0
void add_metadata(const std::string &key, const std::string &value)
Element(size_t element_id, const DebugInfo &debug_info)
size_t element_id() const
const std::string & metadata(const std::string &key) const
const DebugInfo & debug_info() const
virtual void replace(const symbolic::ExpressionMapping &replacements)
DebugInfo debug_info_
Definition element.h:167
std::string_view element_type() const
Returns the type of the element as a human-readable string.
Definition element.h:199
virtual ~Element()=default
std::unordered_map< std::string, std::string > metadata_
Definition element.h:168
virtual void validate(const Function &function) const =0
Validates the element.
virtual ElementType type_id() const =0
Returns the type of the element.
Definition function.h:43
Definition exceptions.h:8
Definition sdfg_builder.h:13
Note: Even though the class references unique_ptr, it will never delete an SDFG it has a reference to...
Definition structured_sdfg_builder.h:44
Definition structured_sdfg_deep_copy.h:8
Definition json_serializer.h:22
SymEngine::RCP< const SymEngine::Basic > Expression
Reference-counted pointer to a symbolic expression (SymEngine Basic).
Definition symbolic.h:83
SymEngine::map_basic_basic ExpressionMapping
Definition symbolic.h:109
Definition function_builder.h:10
Definition structured_sdfg_deep_copy.h:6
Definition cutout_serializer.h:13
Definition analysis.h:7
constexpr ElementType operator~(ElementType type)
Definition element.h:102
constexpr bool is_a(ElementType type, ElementType mask)
Tests whether type is contained in the category mask (efficient, branch-free type check).
Definition element.h:107
constexpr ElementType operator|(ElementType lhs, ElementType rhs)
Bitwise combination of element types, enabling category masks.
Definition element.h:94
constexpr std::string_view element_type_name(ElementType type)
Returns the type of an element as a human-readable string (no allocation).
Definition element.h:112
std::remove_pointer_t< std::remove_reference_t< T > > element_cast_target_t
Resolves the concrete target type for isa/dyn_cast, accepting the type itself (Block),...
Definition element.h:222
ElementType
Definition element.h:72
@ AccessNode
Definition element.h:75
@ IfElse
Definition element.h:84
@ For
Definition element.h:88
@ While
Definition element.h:85
@ Sequence
Definition element.h:82
@ State
Definition element.h:73
@ Map
Definition element.h:89
@ Tasklet
Definition element.h:77
@ AssignmentBlock
Definition element.h:83
@ LibraryNode
Definition element.h:78
@ Memlet
Definition element.h:79
@ Return
Definition element.h:80
@ Continue
Definition element.h:86
@ ConstantNode
Definition element.h:76
@ Block
Definition element.h:81
@ Break
Definition element.h:87
@ Reduce
Definition element.h:90
@ InterstateEdge
Definition element.h:74
element_cast_target_t< T > * dyn_cast(Element *element)
Efficient down-cast to T, or nullptr if element is not a T.
Definition element.h:246
bool isa(const Element &element)
LLVM-style RTTI check: true if element belongs to type category T.
Definition element.h:235
std::ostream & operator<<(std::ostream &os, ElementType type)
Definition element.h:154
constexpr ElementType operator&(ElementType lhs, ElementType rhs)
Definition element.h:98
size_t ElementId
Definition element.h:215
Symbolic system for integer-based symbol analysis.