P4C
The P4 Compiler
Loading...
Searching...
No Matches
simplify_args.h
1
70#ifndef BACKENDS_TOFINO_BF_P4C_MIDEND_SIMPLIFY_ARGS_H_
71#define BACKENDS_TOFINO_BF_P4C_MIDEND_SIMPLIFY_ARGS_H_
72
73#include "backends/tofino/bf-p4c/midend/check_header_alignment.h"
74#include "backends/tofino/bf-p4c/midend/copy_header.h"
75#include "backends/tofino/bf-p4c/midend/type_checker.h"
76#include "frontends/common/resolveReferences/resolveReferences.h"
77#include "frontends/p4/cloner.h"
78#include "frontends/p4/moveDeclarations.h"
79#include "frontends/p4/typeMap.h"
80#include "ir/ir.h"
81#include "midend/flattenHeaders.h"
82
83namespace BFN {
84
166struct InjectTmpVar : public PassManager {
174 class DoInject : public Transform {
175 P4::ReferenceMap *refMap;
176 P4::TypeMap *typeMap;
177
178 bool isNestedStruct(const IR::Expression *expr);
179
180 public:
181 explicit DoInject(P4::ReferenceMap *refMap, P4::TypeMap *typeMap)
182 : refMap(refMap), typeMap(typeMap) {}
183 const IR::Node *postorder(IR::AssignmentStatement *as) override;
184 const IR::Node *postorder(IR::MethodCallStatement *mcs) override;
185 };
186
187 InjectTmpVar(P4::ReferenceMap *refMap, P4::TypeMap *typeMap) {
188 auto typeChecking = new BFN::TypeChecking(refMap, typeMap, true);
189 passes.push_back(typeChecking);
190 passes.push_back(new DoInject(refMap, typeMap));
191 passes.push_back(new P4::ClearTypeMap(typeMap));
192 passes.push_back(typeChecking);
193 passes.push_back(new P4::MoveDeclarations());
194 passes.push_back(new P4::ClearTypeMap(typeMap));
195 passes.push_back(new BFN::CopyHeaders(refMap, typeMap, typeChecking));
196 passes.push_back(new P4::ClearTypeMap(typeMap));
197 passes.push_back(typeChecking);
198 }
199};
200
223class FlattenHeader : public Modifier {
225 const P4::TypeMap *typeMap;
226 IR::Type_Header *flattenedHeader = nullptr;
227 std::vector<cstring> nameSegments{};
228 std::vector<IR::Vector<IR::Annotation>> allAnnotations{};
229 std::vector<Util::SourceInfo> srcInfos{};
230 cstring makeName(std::string_view sep) const;
231 void flattenType(const IR::Type *type);
232 IR::Vector<IR::Annotation> mergeAnnotations() const;
233
234 const IR::Member *flattenedMember;
235 std::vector<cstring> memberSegments{};
236 std::map<cstring, cstring> fieldNameMap;
237 std::map<cstring, std::tuple<const IR::Expression *, cstring>> replacementMap;
238 cstring makeMember(std::string_view sep) const;
239 void flattenMember(const IR::Member *member);
240 const IR::Member *doFlattenMember(const IR::Member *member);
241
242 std::vector<cstring> pathSegments{};
243 cstring makePath(std::string_view sep) const;
244 void flattenStructInitializer(const IR::StructExpression *e,
246 IR::StructExpression *doFlattenStructInitializer(const IR::StructExpression *e);
247 IR::ListExpression *flatten_list(const IR::ListExpression *args);
248 void explode(const IR::Expression *, IR::Vector<IR::Expression> *);
249 int memberDepth(const IR::Member *m);
250 const IR::Member *getTailMembers(const IR::Member *m, int depth);
251 const IR::PathExpression *replaceSrcInfo(const IR::PathExpression *tgt,
252 const IR::PathExpression *src);
253 const IR::Member *replaceSrcInfo(const IR::Member *tgt, const IR::Member *src);
254 const IR::Expression *balancedReplaceSrcInfo(const IR::Expression *tgt, const IR::Member *src);
255
256 std::function<bool(const Context *, const IR::Type_StructLike *)> policy;
257
258 public:
259 explicit FlattenHeader(
260 P4::TypeMap *typeMap,
261 std::function<bool(const Context *, const IR::Type_StructLike *)> policy =
262 [](const Context *, const IR::Type_StructLike *) -> bool { return false; })
263 : typeMap(typeMap), policy(policy) {}
264 bool preorder(IR::Type_Header *header) override;
265 bool preorder(IR::Member *member) override;
266 void postorder(IR::Member *member) override;
267 bool preorder(IR::MethodCallExpression *mc) override;
268};
269
276 P4::ReferenceMap *refMap;
277 P4::TypeMap *typeMap;
278 std::function<bool(const Context *, const IR::Type_StructLike *)> policy;
279
280 public:
282 std::function<bool(const Context *, const IR::Type_StructLike *)> policy)
283 : refMap(refMap), typeMap(typeMap), policy(policy) {
284 setName("EliminateHeaders");
285 }
286 std::map<cstring, IR::IndexedVector<IR::NamedExpression>> rewriteTupleType;
287 std::map<const IR::MethodCallExpression *, const IR::Type *> rewriteOtherType;
288 const IR::Node *preorder(IR::Argument *arg) override;
289 void elimConcat(IR::IndexedVector<IR::NamedExpression> &output, const IR::Concat *expr);
290};
291
296 const EliminateHeaders *eeh;
297
298 public:
299 explicit RewriteTypeArguments(const EliminateHeaders *eeh) : eeh(eeh) {}
300 const IR::Node *preorder(IR::Type_Struct *type_struct) override;
301 const IR::Node *preorder(IR::MethodCallExpression *mc) override;
302};
303
312 public:
314 P4::ReferenceMap *refMap, P4::TypeMap *typeMap,
315 std::function<bool(const Context *, const IR::Type_StructLike *)> policy =
316 [](const Context *, const IR::Type_StructLike *) -> bool { return false; }) {
317 auto eliminateHeaders = new EliminateHeaders(refMap, typeMap, policy);
318 auto rewriteTypeArguments = new RewriteTypeArguments(eliminateHeaders);
319 passes.push_back(new InjectTmpVar(refMap, typeMap));
320 passes.push_back(new FlattenHeader(typeMap));
321 passes.push_back(new P4::ClearTypeMap(typeMap));
322 passes.push_back(new BFN::TypeChecking(refMap, typeMap, true));
323 passes.push_back(eliminateHeaders);
324 passes.push_back(rewriteTypeArguments);
325 // After eliminateHeaders we need to do TypeInference that might
326 // change new ListExpressions to StructExpressions
327 passes.push_back(new P4::ClearTypeMap(typeMap));
328 passes.push_back(new BFN::TypeChecking(refMap, typeMap, true));
329 passes.push_back(new PadFlexibleField(refMap, typeMap)),
330 passes.push_back(new P4::ClearTypeMap(typeMap));
331 passes.push_back(new BFN::TypeChecking(refMap, typeMap, true));
332 }
333};
334
335} // namespace BFN
336
337#endif /* BACKENDS_TOFINO_BF_P4C_MIDEND_SIMPLIFY_ARGS_H_ */
Definition copy_header.h:32
Injects a temporary variable.
Definition simplify_args.h:174
Definition check_header_alignment.h:134
Definition typeChecker.h:32
Definition cloner.h:26
Definition node.h:52
Definition node.h:94
Definition vector.h:59
Definition visitor.h:372
Definition moveDeclarations.h:34
Definition ir/pass_manager.h:40
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition visitor.h:424
Definition typeChecker.h:55
Definition typeMap.h:41
Definition cstring.h:85
Definition simplify_args.h:275
Pass that flattened nested struct within a struct.
Definition simplify_args.h:223
Definition simplify_args.h:295
Top level PassManager that governs simplification of headers and emits.
Definition simplify_args.h:311
PassManager which controls injecting of temporary variables to be used instead of nested structures w...
Definition simplify_args.h:166
The namespace encapsulating Barefoot/Intel-specific stuff.
Definition add_t2na_meta.cpp:21
Definition visitor.h:47