docc 0.8.3 (0d44999b)
C++ API Reference
Loading...
Searching...
No Matches
structured_loop.h
Go to the documentation of this file.
1#pragma once
2
3#include <optional>
4#include <string>
5#include <vector>
6
10
11namespace sdfg {
12
13namespace builder {
15}
17
23
32private:
33 std::unordered_map<std::string, std::string> properties_;
34 std::string value_;
35 ScheduleTypeCategory category_;
36
37public:
38 ScheduleType(std::string value, ScheduleTypeCategory category) : value_(value), category_(category) {
39 }
40
45 const std::string& value() const {
46 return value_;
47 }
48
53 const std::unordered_map<std::string, std::string>& properties() const {
54 return properties_;
55 }
56
62 return category_;
63 }
64
70 void set_property(const std::string& key, const std::string& value) {
71 if (properties_.find(key) == properties_.end()) {
72 properties_.insert({key, value});
73 return;
74 }
75 properties_.at(key) = value;
76 }
77
82 void remove_property(const std::string& key) {
83 properties_.erase(key);
84 }
85
86 void operator=(const ScheduleType& rhs) {
87 value_ = rhs.value_;
88 properties_.clear();
89 for (const auto& entry : rhs.properties_) {
90 properties_.insert(entry);
91 }
92 category_ = rhs.category_;
93 }
94};
95
102public:
103 static const std::string value() {
104 return "SEQUENTIAL";
105 }
109};
110
119public:
121 static void set(ScheduleType& schedule, bool full = true) {
122 schedule.set_property("unroll", full ? "full" : "none");
123 }
124
126 static bool is_set(const ScheduleType& schedule) {
127 auto it = schedule.properties().find("unroll");
128 return it != schedule.properties().end() && it->second == "full";
129 }
130};
131
139public:
140 static void set(ScheduleType& schedule, bool no_fusion = true) {
141 schedule.set_property("fusion", no_fusion ? "none" : "default");
142 }
143
144 static bool is_set(const ScheduleType& schedule) {
145 auto it = schedule.properties().find("fusion");
146 return it != schedule.properties().end() && it->second == "none";
147 }
148};
149
159public:
160 static constexpr const char* KEY = "init";
161
162 static void set(ScheduleType& schedule, bool zero_init = true) {
163 schedule.set_property(KEY, zero_init ? "zero" : "none");
164 }
165
166 static bool is_set(const ScheduleType& schedule) {
167 auto it = schedule.properties().find(KEY);
168 return it != schedule.properties().end() && it->second == "zero";
169 }
170};
171
179public:
180 static const std::vector<std::string>& keys() {
181 static const std::vector<std::string> keys{"fusion", ScheduleType_ZeroInit::KEY};
182 return keys;
183 }
184
186 static void copy(const ScheduleType& from, ScheduleType& to) {
187 for (const auto& key : keys()) {
188 auto it = from.properties().find(key);
189 if (it != from.properties().end()) {
190 to.set_property(key, it->second);
191 }
192 }
193 }
194
196 static void inherit(const ScheduleType& from, ScheduleType& to) {
197 for (const auto& key : keys()) {
198 auto it = from.properties().find(key);
199 if (it != from.properties().end() && !to.properties().contains(key)) {
200 to.set_property(key, it->second);
201 }
202 }
203 }
204
206 static void strip(ScheduleType& schedule) {
207 for (const auto& key : keys()) {
208 schedule.remove_property(key);
209 }
210 }
211};
212
213
246
247protected:
252
253 std::unique_ptr<Sequence> root_;
254
256
257 // Memoized header analyses; every writer of indvar/init/update/condition must reset them.
258 mutable std::optional<symbolic::Integer> stride_cache_;
259 mutable std::optional<symbolic::Expression> canonical_bound_cache_;
260 mutable std::optional<symbolic::Expression> num_iterations_cache_;
261
263
265
267
269
271 size_t element_id,
272 const DebugInfo& debug_info,
273 ControlFlowNode* parent,
279 );
280
281 static constexpr size_t REQUIRED_ELEMENT_IDS = 2;
282
283public:
286
288 static bool classof(const Element& element) {
289 return is_a(element.type_id(), TypeGroup);
290 }
291
292 virtual ~StructuredLoop() = default;
293
294 StructuredLoop(const StructuredLoop& node) = delete;
296
297 void validate(const Function& function) const override;
298
304
310
316
322
327 Sequence& root() const;
328
334
340 void replace(const symbolic::Expression old_expression, const symbolic::Expression new_expression) override;
341
342 void replace(const symbolic::ExpressionMapping& replacements) override;
343
350
356 bool is_contiguous() const;
357
363 bool is_monotonic() const;
364
373
385
396
403
422
432};
433
434} // namespace structured_control_flow
435} // namespace sdfg
Definition element.h:30
Element(size_t element_id, const DebugInfo &debug_info)
size_t element_id() const
const DebugInfo & debug_info() const
virtual ElementType type_id() const =0
Returns the type of the element.
Definition function.h:43
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
ControlFlowNode(size_t element_id, const DebugInfo &debug_info, ControlFlowNode *parent)
Protected constructor for control flow nodes with parent.
Annotations describing a whole loop nest rather than a single loop's execution strategy.
Definition structured_loop.h:178
static void inherit(const ScheduleType &from, ScheduleType &to)
Copy nest annotations present on from onto to, unless to already sets them.
Definition structured_loop.h:196
static void copy(const ScheduleType &from, ScheduleType &to)
Copy all nest annotations present on from onto to (overwriting).
Definition structured_loop.h:186
static const std::vector< std::string > & keys()
Definition structured_loop.h:180
static void strip(ScheduleType &schedule)
Remove all nest annotations from schedule.
Definition structured_loop.h:206
No-fusion annotation carried as a generic property on any ScheduleType.
Definition structured_loop.h:138
static void set(ScheduleType &schedule, bool no_fusion=true)
Definition structured_loop.h:140
static bool is_set(const ScheduleType &schedule)
Definition structured_loop.h:144
Sequential schedule type for Map nodes.
Definition structured_loop.h:101
static const std::string value()
Definition structured_loop.h:103
static ScheduleType create()
Definition structured_loop.h:106
Unroll annotation carried as a generic property on any ScheduleType.
Definition structured_loop.h:118
static void set(ScheduleType &schedule, bool full=true)
Mark (or clear) the schedule for full unrolling.
Definition structured_loop.h:121
static bool is_set(const ScheduleType &schedule)
True if the schedule is marked for full unrolling.
Definition structured_loop.h:126
Zero-initialization annotation carried as a generic property on any ScheduleType.
Definition structured_loop.h:158
static void set(ScheduleType &schedule, bool zero_init=true)
Definition structured_loop.h:162
static constexpr const char * KEY
Definition structured_loop.h:160
static bool is_set(const ScheduleType &schedule)
Definition structured_loop.h:166
Represents a schedule type for Map nodes.
Definition structured_loop.h:31
void operator=(const ScheduleType &rhs)
Definition structured_loop.h:86
const std::unordered_map< std::string, std::string > & properties() const
Get all schedule properties.
Definition structured_loop.h:53
void set_property(const std::string &key, const std::string &value)
Set a schedule property.
Definition structured_loop.h:70
void remove_property(const std::string &key)
Remove a schedule property if present.
Definition structured_loop.h:82
ScheduleTypeCategory category() const
Get the schedule type category.
Definition structured_loop.h:61
ScheduleType(std::string value, ScheduleTypeCategory category)
Definition structured_loop.h:38
const std::string & value() const
Get the schedule type identifier.
Definition structured_loop.h:45
A sequential container of control flow nodes.
Definition sequence.h:52
bool is_loop_normal_form() const
Checks if the loop is in a normal form.
symbolic::Expression compute_num_iterations() const
static bool classof(const Element &element)
LLVM-style RTTI predicate: true if element is a StructuredLoop.
Definition structured_loop.h:288
const symbolic::Expression update() const
Get the update expression for the induction variable.
std::optional< symbolic::Expression > num_iterations_cache_
Definition structured_loop.h:260
symbolic::Expression num_iterations() const
Describes the number of iterations of a loop as a closed-form expression.
void replace(const symbolic::ExpressionMapping &replacements) override
symbolic::Expression init_
Definition structured_loop.h:249
std::optional< symbolic::Expression > canonical_bound_cache_
Definition structured_loop.h:259
symbolic::Expression num_iterations_approx() const
Overapproximated (upper-bound) number of iterations.
symbolic::Condition condition_
Definition structured_loop.h:251
const symbolic::Symbol indvar() const
Get the induction variable (loop counter) symbol.
const ScheduleType & schedule_type() const
Get the scheduling strategy for this Map.
bool is_monotonic() const
Checks if the loop is monotonic (i.e., stride is a positive integer).
symbolic::Symbol indvar_
Definition structured_loop.h:248
const symbolic::Expression init() const
Get the initialization expression for the induction variable.
static constexpr size_t REQUIRED_ELEMENT_IDS
Definition structured_loop.h:281
StructuredLoop(size_t element_id, const DebugInfo &debug_info, ControlFlowNode *parent, symbolic::Symbol indvar, symbolic::Expression init, symbolic::Expression update, symbolic::Condition condition, const ScheduleType &schedule_type=ScheduleType_Sequential::create())
symbolic::Expression update_
Definition structured_loop.h:250
static constexpr ElementType TypeGroup
Bitmask of all concrete ElementTypes that are StructuredLoops.
Definition structured_loop.h:285
symbolic::Expression canonical_bound_upper() const
Describes the upper bound of a loop (for positive stride).
const symbolic::Condition condition() const
Get the loop continuation condition.
void validate(const Function &function) const override
Validates the element.
symbolic::Expression canonical_bound() const
Describes the bound of a loop as a closed-form expression.
Sequence & root() const
Access the loop body sequence.
std::unique_ptr< Sequence > root_
Definition structured_loop.h:253
StructuredLoop(const StructuredLoop &node)=delete
StructuredLoop & operator=(const StructuredLoop &)=delete
std::optional< symbolic::Integer > stride_cache_
Definition structured_loop.h:258
void replace(const symbolic::Expression old_expression, const symbolic::Expression new_expression) override
Replace occurrences of an expression in loop parameters and body.
symbolic::Expression canonical_bound_lower() const
Describes the lower bound of a loop (for negative stride).
symbolic::Integer stride() const
Describes the stride of a loop's update as a constant.
bool is_contiguous() const
Checks if the loop has a positive unit stride (i.e., update is indvar + 1).
ScheduleType schedule_type_
Definition structured_loop.h:255
symbolic::Expression compute_canonical_bound() const
SymEngine::RCP< const SymEngine::Basic > Expression
Reference-counted pointer to a symbolic expression (SymEngine Basic).
Definition symbolic.h:83
SymEngine::RCP< const SymEngine::Symbol > Symbol
Reference-counted pointer to a SymEngine Symbol.
Definition symbolic.h:71
SymEngine::RCP< const SymEngine::Boolean > Condition
Reference-counted pointer to a boolean condition expression.
Definition symbolic.h:86
SymEngine::RCP< const SymEngine::Integer > Integer
Reference-counted pointer to a SymEngine Integer constant.
Definition symbolic.h:77
SymEngine::map_basic_basic ExpressionMapping
Definition symbolic.h:109
Definition function_builder.h:10
Definition language_extension.h:18
ScheduleTypeCategory
Categories of schedule types for Map nodes Defines the high-level classification of scheduling strate...
Definition structured_loop.h:22
@ Parallelizer
Definition structured_loop.h:22
@ Vectorizer
Definition structured_loop.h:22
@ Offloader
Definition structured_loop.h:22
Definition analysis.h:7
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
ElementType
Definition element.h:72
@ For
Definition element.h:88
@ Map
Definition element.h:89
@ Reduce
Definition element.h:90
Symbolic system for integer-based symbol analysis.