docc 0.8.3 (0d44999b)
C++ API Reference
Loading...
Searching...
No Matches
recorder.h
Go to the documentation of this file.
1#pragma once
2
9
10#include <concepts>
11#include <nlohmann/json.hpp>
13
14namespace sdfg {
15namespace transformations {
16
20template<typename T>
21concept transformation_concept = std::derived_from<T, sdfg::transformations::Transformation>;
22
32class Recorder {
33private:
34 nlohmann::json history_;
35
36protected:
44 virtual void
45 enrich(nlohmann::json& desc, builder::StructuredSDFGBuilder& builder, analysis::AnalysisManager& analysis_manager) {
46 auto& loop_analysis = analysis_manager.get<analysis::LoopAnalysis>();
47 for (auto& [key, value] : desc["subgraph"].items()) {
48 if (!value.contains("element_id")) {
49 continue;
50 }
51 auto element_id = value["element_id"].template get<size_t>();
52 auto* elem = builder.find_element_by_id(element_id);
54 continue;
55 }
56 auto* loop = static_cast<structured_control_flow::ControlFlowNode*>(elem);
57 auto loop_info = loop_analysis.loop_info(loop);
58 value["loop_info"] = loop_info_to_json(loop_info);
59 }
60 }
61
62public:
67
68 virtual ~Recorder() = default;
69
81 bool record(
82 Transformation& transformation,
84 analysis::AnalysisManager& analysis_manager,
85 bool skip_if_not_applicable = false
86 ) {
87 if (!transformation.can_be_applied(builder, analysis_manager)) {
88 if (!skip_if_not_applicable) {
89 throw transformations::
90 InvalidTransformationException("Transformation " + transformation.name() + " cannot be applied.");
91 }
92 return false;
93 }
94
95 nlohmann::json desc;
96 transformation.to_json(desc);
97
98#ifndef NDEBUG
99 std::string schema_error;
100 if (!validate_transformation_schema(desc, schema_error)) {
102 "Transformation '" + transformation.name() + "' produced an invalid description: " + schema_error
103 );
104 }
105#endif
106
107 this->enrich(desc, builder, analysis_manager);
108 history_.push_back(desc);
109 transformation.apply(builder, analysis_manager);
110 return true;
111 }
112
123 template<typename T, typename... Args>
125 void apply(
127 analysis::AnalysisManager& analysis_manager,
128 bool skip_if_not_applicable,
129 Args&&... args
130 ) {
131 T transformation(std::forward<Args>(args)...);
132 this->record(transformation, builder, analysis_manager, skip_if_not_applicable);
133 };
134
139 void save(std::filesystem::path path) const;
140
145 nlohmann::json get_history() const {
146 return history_;
147 }
148
153 nlohmann::json& history() {
154 return history_;
155 }
156};
157
158} // namespace transformations
159} // namespace sdfg
Definition analysis.h:53
T & get()
Definition analysis.h:83
Definition loop_analysis.h:97
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
Base class for all structured control flow nodes in a StructuredSDFG.
Definition control_flow_node.h:44
Exception thrown when a transformation description is invalid.
Definition transformation.h:110
void save(std::filesystem::path path) const
Save recorded transformation history to a file.
void apply(builder::StructuredSDFGBuilder &builder, analysis::AnalysisManager &analysis_manager, bool skip_if_not_applicable, Args &&... args)
Construct a transformation from args and record it.
Definition recorder.h:125
Recorder()
Construct an empty recorder.
virtual void enrich(nlohmann::json &desc, builder::StructuredSDFGBuilder &builder, analysis::AnalysisManager &analysis_manager)
Enrich a transformation descriptor after to_json and before it is recorded and applied.
Definition recorder.h:45
nlohmann::json & history()
Get mutable reference to transformation history.
Definition recorder.h:153
nlohmann::json get_history() const
Get the transformation history.
Definition recorder.h:145
bool record(Transformation &transformation, builder::StructuredSDFGBuilder &builder, analysis::AnalysisManager &analysis_manager, bool skip_if_not_applicable=false)
Apply an already-constructed transformation and record it.
Definition recorder.h:81
Base class for all SDFG transformations.
Definition transformation.h:25
virtual void to_json(nlohmann::json &j) const =0
Serialize this transformation to JSON.
virtual bool can_be_applied(builder::StructuredSDFGBuilder &builder, analysis::AnalysisManager &analysis_manager)=0
Check if this transformation can be safely applied.
virtual std::string name() const =0
Get the name of this transformation.
virtual void apply(builder::StructuredSDFGBuilder &builder, analysis::AnalysisManager &analysis_manager)=0
Apply this transformation to the SDFG.
Concept for types that are transformations.
Definition recorder.h:21
Definition function_builder.h:10
Definition loop_parallelization.h:8
bool validate_transformation_schema(const nlohmann::json &j, std::string &error_out)
Validate that a transformation description respects the serialization schema.
Definition transformation_schema.h:45
Definition analysis.h:7
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