P4C
The P4 Compiler
Loading...
Searching...
No Matches
ping_pong_generation.h
1
18
19#ifndef BACKENDS_TOFINO_BF_P4C_MIDEND_PING_PONG_GENERATION_H_
20#define BACKENDS_TOFINO_BF_P4C_MIDEND_PING_PONG_GENERATION_H_
21
22#include "backends/tofino/bf-p4c/ir/gress.h"
23#include "backends/tofino/bf-p4c/midend/collect_pipelines.h"
24#include "backends/tofino/bf-p4c/midend/type_checker.h"
25#include "frontends/common/resolveReferences/referenceMap.h"
26#include "frontends/p4/methodInstance.h"
27#include "frontends/p4/typeMap.h"
28#include "ir/ir.h"
29
30namespace BFN {
31
46class PingPongGeneration : public PassManager {
47 // CONSTANTS ----------------------------------------------------------------------------------
51 static const cstring ID_PING_PONG_SUFFIX;
55 static const cstring GMD_STRUCTURE_NAME;
59 static const cstring PING_PONG_FIELD_NAME;
60
61 // VARIABLES ----------------------------------------------------------------------------------
62 // These variables serve as a state/storage between different passes
66 const IR::Type_Header *ghost_meta_struct = nullptr;
67
68 // Basic maps
69 P4::ReferenceMap *refMap;
70 P4::TypeMap *typeMap;
71
72 // Local maps for storing information
73 using RegisterSet = std::set<const IR::Declaration_Instance *>;
74 using TnaControlLess = ByNameLess<const IR::BFN::TnaControl>;
75
76 template <typename Key>
78
79 template <typename Key, typename Value>
80 using TnaControlPairMap = std::map<std::pair<const IR::BFN::TnaControl *, const Key>, Value,
81 TnaControlPairLess<const Key>>;
82
83 template <typename Value>
84 using TnaControlMap = std::map<const IR::BFN::TnaControl *, Value, TnaControlLess>;
85
86 using RegisterToRegisterActionMap =
87 TnaControlPairMap<const IR::Declaration_Instance *, RegisterSet>;
88 using RegisterToP4ActionMap =
89 TnaControlPairMap<const IR::Declaration_Instance *, std::set<const IR::P4Action *>>;
90 using P4ActionToRegisterMap =
91 TnaControlPairMap<const IR::P4Action *, const IR::Declaration_Instance *>;
92 using P4ActionToP4TableMap = TnaControlPairMap<const IR::P4Action *, const IR::P4Table *>;
93 using P4TableToRegisterMap =
94 TnaControlPairMap<const IR::P4Table *, const IR::Declaration_Instance *>;
95 using P4TableToP4TableMap = std::unordered_map<const IR::P4Table *, const IR::P4Table *>;
96 using ControlToRegisterMap = TnaControlMap<RegisterSet>;
100 TnaControlMap<cstring> ghost_meta_name;
101
102 RegisterToRegisterActionMap registerToRegisterAction;
103 RegisterToP4ActionMap registerToP4Action;
104 P4ActionToRegisterMap p4ActionToRegister;
105 P4ActionToP4TableMap p4ActionToP4Table;
106 P4TableToRegisterMap p4TableToRegister;
107 ControlToRegisterMap ppIfCoveredRegisters;
108 ControlToRegisterMap controlToRegister;
109 P4TableToP4TableMap p4TableToDuplicateTable;
110
112
113 // HELPER FUNCTIONS ---------------------------------------------------------------------------
114 inline IR::Path *appendPingPongSuffix(IR::Path *, std::set<cstring> &);
115 inline void duplicateNodeDeclaration(const IR::Declaration *, IR::BFN::TnaControl *,
116 std::set<cstring> &);
117
120 std::set<const IR::Declaration_Instance *> allRegisters() const;
121 bool shouldTransform(); // FIXME const;
122
125 bool isPingPongValid(const IR::Declaration_Instance *reg);
126
130 bool isPingPongValid(const IR::BFN::TnaControl *gress, const IR::Declaration_Instance *reg,
131 bool calculatingCovered = false); // FIXME const;
132
135 bool isPingPongValid(const CollectPipelines::Pipe &pipe, const IR::Declaration_Instance *reg,
136 bool calculatingCovered = false); // FIXME const;
137
138 // HELPER VISITORS ----------------------------------------------------------------------------
139 // Finds a ghost_metadata.ping_pong field reference in a subtree
141 // Base class for chaning declarations
142 class DeclarationChanger;
143 // This visitor changes specific references in new cloned register action
144 class RegActionChanger;
145 // This visitor changes specific references in new cloned P4 action
146 class P4ActionChanger;
147 // This visitor changes specific references in new cloned P4 table
148 class P4TableChanger;
149 // This visitor changes P4 table references in cloned MethodCallStatement
150 class ApplyMCSChanger;
151
152 // MAIN VISITORS ------------------------------------------------------------------------------
156 class GetAllRegisters : public Inspector {
157 PingPongGeneration &self;
158
159 bool preorder(const IR::MethodCallExpression *) override;
160
161 public:
162 explicit GetAllRegisters(PingPongGeneration &self) : self(self) {}
163 };
164
168 class AddAllTables : public Inspector {
169 PingPongGeneration &self;
170
171 bool preorder(const IR::ActionListElement *) override;
172
173 public:
174 explicit AddAllTables(PingPongGeneration &self) : self(self) {}
175 };
176
182 class CheckPingPongTables : public Inspector {
183 PingPongGeneration &self;
184
185 // Finds ghost_metadata structure presence
186 bool preorder(const IR::Type_Header *) override;
187 // Finds tables applied under ping_pong
188 bool preorder(const IR::PathExpression *) override;
189 // Gets the identifier of ghost metadata
190 bool preorder(const IR::Parameter *) override;
191
192 public:
193 explicit CheckPingPongTables(PingPongGeneration &self) : self(self) {}
194 };
195
199 class GeneratePingPongMechanismDeclarations : public Transform {
200 PingPongGeneration &self;
201
202 IR::Node *preorder(IR::P4Program *);
203 IR::Node *preorder(IR::BFN::TnaControl *);
204
205 public:
206 explicit GeneratePingPongMechanismDeclarations(PingPongGeneration &self) : self(self) {}
207 };
208
212 class GeneratePingPongMechanism : public Transform {
213 PingPongGeneration &self;
214
215 IR::Node *postorder(IR::MethodCallStatement *);
216
217 public:
218 explicit GeneratePingPongMechanism(PingPongGeneration &self) : self(self) {}
219 };
220
221 public:
226 : refMap(refMap), typeMap(typeMap) {
227 setName("Automatic ping-pong generation");
228 addPasses({new CollectPipelines(refMap, &pipelines),
229 new TypeChecking(refMap, typeMap, true), new GetAllRegisters(*this),
230 new AddAllTables(*this), new CheckPingPongTables(*this),
231 new PassIf([this]() { return shouldTransform(); },
232 {new GeneratePingPongMechanismDeclarations(*this),
233 // Update ref and type map, bacause the IR might have changed
234 new TypeChecking(refMap, typeMap, true),
235 new GeneratePingPongMechanism(*this)})});
236 }
237};
238
239} // namespace BFN
240
241#endif // BACKENDS_TOFINO_BF_P4C_MIDEND_PING_PONG_GENERATION_H_
Definition collect_pipelines.h:58
Definition collect_pipelines.h:80
This visitor changes P4 table references in cloned MethodCallStatement.
Definition ping_pong_generation.cpp:143
Base visitor class for other classes that change declarations.
Definition ping_pong_generation.cpp:42
This visitor changes specific references in new cloned P4 action.
Definition ping_pong_generation.cpp:93
This visitor changes specific references in new cloned P4 table.
Definition ping_pong_generation.cpp:115
Finds a ghost_metadata.ping_pong field reference in a subtree.
Definition ping_pong_generation.cpp:202
This visitor changes specific references in new cloned register action.
Definition ping_pong_generation.cpp:72
Definition cmp.h:97
Definition node.h:94
Definition visitor.h:413
Definition ir/pass_manager.h:172
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition visitor.h:437
Definition typeChecker.h:55
Definition typeMap.h:41
Definition cstring.h:85
Definition cmp.h:113
Definition decaf.h:117
PingPongGeneration(P4::ReferenceMap *refMap, P4::TypeMap *typeMap)
Definition ping_pong_generation.h:225
Inspector pass that collects information about all pipelines declared in the Switch....
Definition collect_pipelines.h:42
The namespace encapsulating Barefoot/Intel-specific stuff.
Definition add_t2na_meta.cpp:21