P4C
The P4 Compiler
Loading...
Searching...
No Matches
flattenUnions.h
1/*
2 * Copyright 2022 Intel Corp.
3 * SPDX-FileCopyrightText: 2022 Intel Corp.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef MIDEND_FLATTENUNIONS_H_
9#define MIDEND_FLATTENUNIONS_H_
10
11#include "./frontends/p4/parserControlFlow.h"
12#include "./frontends/p4/simplifyDefUse.h"
13#include "./frontends/p4/unusedDeclarations.h"
14#include "frontends/p4/typeChecking/typeChecker.h"
15#include "ir/ir.h"
16namespace P4 {
17
39class DoFlattenHeaderUnion : public Transform {
40 protected:
41 P4::ReferenceMap *refMap;
42 P4::TypeMap *typeMap;
43 // A field name is only unique within its declaring struct.
44 std::map<std::pair<cstring, cstring>, std::map<cstring, cstring>> structReplacementMap;
45 std::map<cstring, std::map<cstring, cstring>> replacementMap;
46 // Replacement map needed to add element-wise header declaration in right context
47 std::map<IR::Declaration_Variable *, IR::IndexedVector<IR::Declaration>> replaceDVMap;
48
49 public:
50 DoFlattenHeaderUnion(P4::ReferenceMap *refMap, P4::TypeMap *typeMap)
51 : refMap(refMap), typeMap(typeMap) {}
52 const IR::Node *postorder(IR::Type_Struct *sf) override;
53 const IR::Node *postorder(IR::Declaration_Variable *dv) override;
54 const IR::Node *postorder(IR::Member *m) override;
55 const IR::Node *postorder(IR::P4Parser *parser) override;
56 const IR::Node *postorder(IR::P4Control *control) override;
57 const IR::Node *postorder(IR::P4Action *action) override;
58 bool hasHeaderUnionField(IR::Type_Struct *s);
59};
60
87class DoFlattenHeaderUnionStack : public DoFlattenHeaderUnion {
88 std::map<std::pair<cstring, cstring>, std::vector<cstring>> stackFieldMap;
89 std::map<cstring, std::vector<cstring>> stackMap;
90
91 public:
92 DoFlattenHeaderUnionStack(P4::ReferenceMap *refMap, P4::TypeMap *typeMap)
93 : DoFlattenHeaderUnion(refMap, typeMap) {
94 setName("DoFlattenHeaderUnionStack");
95 }
96 const IR::Node *postorder(IR::Type_Struct *sf) override;
97 const IR::Node *postorder(IR::ArrayIndex *e) override;
98 const IR::Node *postorder(IR::Declaration_Variable *dv) override;
99 bool hasHeaderUnionStackField(IR::Type_Struct *s);
100};
101
135class HandleValidityHeaderUnion : public Transform {
136 P4::ReferenceMap *refMap;
137 P4::TypeMap *typeMap;
138 bool flattenStacks;
139 IR::IndexedVector<IR::Declaration> toInsert; // temporaries
140
141 bool isFlattened(const IR::Expression *u) const;
142
143 public:
144 HandleValidityHeaderUnion(P4::ReferenceMap *refMap, P4::TypeMap *typeMap, bool flattenStacks)
145 : refMap(refMap), typeMap(typeMap), flattenStacks(flattenStacks) {
146 setName("HandleValidityHeaderUnion");
147 }
148 const IR::Node *postorder(IR::AssignmentStatement *assn) override;
149 const IR::Node *postorder(IR::IfStatement *a) override;
150 const IR::Node *postorder(IR::SwitchStatement *a) override;
151 const IR::Node *postorder(IR::MethodCallStatement *mcs) override;
152 const IR::Node *postorder(IR::P4Parser *parser) override;
153 const IR::Node *postorder(IR::P4Control *control) override;
154 const IR::Node *postorder(IR::P4Action *action) override;
155 const IR::MethodCallStatement *processValidityForStr(const IR::Statement *s,
156 const IR::Member *m, cstring headerElement,
157 cstring setValid);
158 const IR::Node *setInvalidforRest(const IR::Statement *s, const IR::Member *m,
159 const IR::Type_HeaderUnion *hu, cstring exclude,
160 bool setValidforCurrMem);
161 const IR::Node *expandIsValid(const IR::Statement *a, const IR::MethodCallExpression *mce,
163};
164
170class DoExpandHeaderUnionStackPushPop : public Transform {
171 P4::TypeMap *typeMap;
172
173 public:
174 explicit DoExpandHeaderUnionStackPushPop(P4::TypeMap *typeMap) : typeMap(typeMap) {
175 setName("DoExpandHeaderUnionStackPushPop");
176 }
177 const IR::Node *postorder(IR::MethodCallStatement *mcs) override;
178};
179
180class RemoveUnusedHUDeclarations : public Transform {
181 const UsedDeclSet &used;
182
183 public:
184 explicit RemoveUnusedHUDeclarations(const UsedDeclSet &used) : used(used) {}
185
186 const IR::Node *preorder(IR::Type_HeaderUnion *type) override {
187 if (!used.isUsed(getOriginal<IR::IDeclaration>())) return nullptr;
188 return type;
189 }
190};
191
192class RemoveAllUnusedHUDDeclarations : public PassManager {
193 UsedDeclSet used;
194
195 public:
196 RemoveAllUnusedHUDDeclarations()
197 : PassManager({
198 new CollectUsedDeclarations(used),
200 }) {
201 setName("RemoveAllUnusedHUDDeclarations");
202 setStopOnError(true);
203 }
204};
205
212class FlattenHeaderUnion : public PassManager {
213 public:
214 FlattenHeaderUnion(P4::ReferenceMap *refMap, P4::TypeMap *typeMap, bool loopsUnroll = true) {
215 passes.push_back(new P4::TypeChecking(refMap, typeMap));
216 // Lower push_front/pop_front on header union stacks first; the new nodes need
217 // types before HandleValidityHeaderUnion.
218 if (loopsUnroll) {
219 passes.push_back(new DoExpandHeaderUnionStackPushPop(typeMap));
220 passes.push_back(new P4::ClearTypeMap(typeMap));
221 passes.push_back(new P4::TypeInference(typeMap, false));
222 passes.push_back(new P4::TypeChecking(refMap, typeMap));
223 }
224 passes.push_back(new HandleValidityHeaderUnion(refMap, typeMap, loopsUnroll));
225 // Stack flattening is only applicable if parser loops are unrolled and
226 // header union stack elements are accessed using [] notation. This pass does not handle
227 // .next .last etc accessors for stack elements.
228 if (loopsUnroll) {
229 passes.push_back(new DoFlattenHeaderUnionStack(refMap, typeMap));
231 }
232 passes.push_back(new P4::ClearTypeMap(typeMap));
233 passes.push_back(new P4::TypeInference(typeMap, false));
234 passes.push_back(new P4::TypeChecking(refMap, typeMap));
235 passes.push_back(new DoFlattenHeaderUnion(refMap, typeMap));
237 passes.push_back(new P4::RemoveAllUnusedHUDDeclarations());
238 passes.push_back(new P4::ClearTypeMap(typeMap));
239 passes.push_back(new P4::TypeChecking(nullptr, typeMap));
240 passes.push_back(new P4::RemoveParserIfs(typeMap));
241 }
242};
243} // namespace P4
244
245#endif /* MIDEND_FLATTENUNIONS_H_ */
Definition typeChecker.h:23
Collects all used declarations into @used set.
Definition unusedDeclarations.h:50
Definition flattenUnions.h:170
Definition flattenUnions.h:39
Definition flattenUnions.h:87
Definition flattenUnions.h:135
Definition indexed_vector.h:31
Definition node.h:44
Class used to encode maps from paths to declarations.
Definition referenceMap.h:58
Iterates UnusedDeclarations until convergence.
Definition unusedDeclarations.h:187
Definition flattenUnions.h:192
Definition parserControlFlow.h:105
Definition flattenUnions.h:180
Definition unusedDeclarations.h:39
Definition visitor.h:433
Definition typeChecker.h:46
Definition typeChecker.h:502
Definition typeMap.h:32
Definition unusedDeclarations.h:20
Definition cstring.h:76
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13