P4C
The P4 Compiler
Loading...
Searching...
No Matches
arch/fromv1.0/phase0.h
1
19#ifndef BF_P4C_ARCH_FROMV1_0_PHASE0_H_
20#define BF_P4C_ARCH_FROMV1_0_PHASE0_H_
21
22#include "ir/ir.h"
23#include "ir/pass_manager.h"
24
25namespace P4 {
26class ReferenceMap;
27class TypeMap;
28} // namespace P4
29
30namespace P4 {
31namespace P4V1 {
32class TnaProgramStructure;
33}
34} // namespace P4
35
36namespace BFN {
37
63 P4V1::TnaProgramStructure *s = nullptr);
64};
65
66typedef std::map<const IR::BFN::TnaParser *, const IR::Type_StructLike *> Phase0CallMap;
67
68/* Check if phase0 extern - port_metadata_unpack - is used in the program.
69 * Since we can have multiple ingress parsers, we create a map of parser -
70 * fields to extract for each parser
71 * Fields can be specified as a Type_Header/Type_Struct
72 */
74 public:
76 Phase0CallMap *phase0_calls)
77 : refMap(refMap), typeMap(typeMap), phase0_calls(phase0_calls) {
78 setName("CheckPhaseZeroExtern");
79 }
80
81 private:
82 bool preorder(const IR::MethodCallExpression *expr) override;
83
84 P4::ReferenceMap *refMap;
85 P4::TypeMap *typeMap;
86 Phase0CallMap *phase0_calls;
87};
88
89using Phase0AnnotMap = std::map<cstring, cstring>;
90
92 public:
93 explicit CollectPhase0Annotation(Phase0AnnotMap *name, Phase0AnnotMap *action)
94 : phase0_name_annot(name), phase0_action_annot(action) {
95 CHECK_NULL(name);
96 CHECK_NULL(action);
97 }
98 bool preorder(const IR::ParserState *state) override;
99 Phase0AnnotMap *phase0_name_annot;
100 Phase0AnnotMap *phase0_action_annot;
101};
102
104 public:
105 explicit UpdatePhase0NodeInParser(Phase0CallMap *phase0_calls,
107 Phase0AnnotMap *phase0_name_annot,
108 Phase0AnnotMap *phase0_action_annot)
109 : phase0_calls(phase0_calls),
110 declarations(decls),
111 phase0_name_annot(phase0_name_annot),
112 phase0_action_annot(phase0_action_annot) {
113 setName("UpdatePhase0NodeInParser");
114 }
115
116 private:
117 IR::IndexedVector<IR::StructField> *canPackDataIntoPhase0(
118 const IR::IndexedVector<IR::StructField> *fields, const int);
119 // Populate Phase0 Node in Parser & generate new Phase0 Header type
120 IR::BFN::TnaParser *preorder(IR::BFN::TnaParser *parser) override;
121
122 Phase0CallMap *phase0_calls;
123 IR::IndexedVector<IR::Node> *declarations;
124 Phase0AnnotMap *phase0_name_annot;
125 Phase0AnnotMap *phase0_action_annot;
126 int phase0_count = 0;
127};
128
129// Replace phase0 struct/header declaration to new phase0 header with flexible
130// layout annotation for backend
132 public:
133 explicit UpdatePhase0Header(IR::IndexedVector<IR::Node> *decls) : declarations(decls) {
134 setName("UpdatePhase0Header");
135 }
136
137 private:
138 IR::Node *preorder(IR::Type_Struct *s) override {
139 if (auto *d = declarations->getDeclaration(s->name.toString())) {
140 LOG4("modifying struct " << s << " to header " << d->to<IR::Type_Header>());
141 return d->to<IR::Node>()->clone();
142 }
143 return s;
144 }
145
146 IR::IndexedVector<IR::Node> *declarations;
147};
148
149/* A Phase0 assignment statement in IR is converted into an extract for the
150 * backend Parser
151 * E.g.
152 * Before => ig_md.port_md = port_metadata_unpack<my_port_metadata_t>(pkt);
153 * After => pkt.extract<my_port_metadata_t>(ig_md.port_md);
154 *
155 * A Phase0 method call expression in IR is converted into an extract for the
156 * backend Parser
157 * E.g.
158 * Before => port_metadata_unpack<my_port_metadata_t>(pkt);
159 * After => pkt.advance(64); // for Tofino
160 * After => pkt.advance(192); // for Tofino2
161 * The advance bits are determined by
162 * Device::pardeSpec().bitPhase0Size() + Device::pardeSpec().bitIngressPrePacketPaddingSize()
163 * as specified in bf-p4c/parde/parde_spec.h
164 *
165 * Used when we dont wish to extract the fields but simply advance or skip
166 * through phase0 or port metadata
167 */
169 public:
171 : refMap(refMap), typeMap(typeMap) {
172 setName("ConvertPhase0MceToExtract");
173 }
174
175 private:
176 IR::MethodCallExpression *generate_phase0_extract_method_call(
177 const IR::Expression *lExpr, const IR::MethodCallExpression *rExpr);
178 IR::Node *preorder(IR::MethodCallExpression *expr) override;
179 IR::Node *preorder(IR::AssignmentStatement *stmt) override;
180
181 P4::ReferenceMap *refMap;
182 P4::TypeMap *typeMap;
183};
184
186 public:
187 ConvertPhase0(P4::ReferenceMap *refMap, P4::TypeMap *typeMap) {
188 auto *phase0_calls = new Phase0CallMap();
189 auto *decls = new IR::IndexedVector<IR::Node>();
190 auto *phase0_name_annot = new Phase0AnnotMap();
191 auto *phase0_action_annot = new Phase0AnnotMap();
192 addPasses({
193 new CheckPhaseZeroExtern(refMap, typeMap, phase0_calls),
194 new CollectPhase0Annotation(phase0_name_annot, phase0_action_annot),
195 new UpdatePhase0NodeInParser(phase0_calls, decls, phase0_name_annot,
196 phase0_action_annot),
197 new UpdatePhase0Header(decls),
198 new ConvertPhase0AssignToExtract(refMap, typeMap),
199 });
200 }
201};
202
203cstring getDefaultPhase0TableKeyName();
204
205} // namespace BFN
206
207#endif /* BF_P4C_ARCH_FROMV1_0_PHASE0_H_ */
Definition arch/fromv1.0/phase0.h:73
Definition arch/fromv1.0/phase0.h:91
Definition arch/fromv1.0/phase0.h:168
Definition arch/fromv1.0/phase0.h:185
Definition arch/fromv1.0/phase0.h:131
Definition arch/fromv1.0/phase0.h:103
Definition node.h:52
Definition node.h:94
Definition visitor.h:400
Definition backends/tofino/bf-p4c/arch/fromv1.0/programStructure.h:110
Definition ir/pass_manager.h:40
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition visitor.h:424
Definition typeMap.h:41
Definition cstring.h:85
The namespace encapsulating Barefoot/Intel-specific stuff.
Definition add_t2na_meta.cpp:21
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
Definition arch/fromv1.0/phase0.h:61