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 std::map<cstring, std::map<cstring, cstring>> replacementMap;
44 // Replacement map needed to add element-wise header declaration in right context
45 std::map<IR::Declaration_Variable *, IR::IndexedVector<IR::Declaration>> replaceDVMap;
46
47 public:
48 DoFlattenHeaderUnion(P4::ReferenceMap *refMap, P4::TypeMap *typeMap)
49 : refMap(refMap), typeMap(typeMap) {}
50 const IR::Node *postorder(IR::Type_Struct *sf) override;
51 const IR::Node *postorder(IR::Declaration_Variable *dv) override;
52 const IR::Node *postorder(IR::Member *m) override;
53 const IR::Node *postorder(IR::P4Parser *parser) override;
54 const IR::Node *postorder(IR::P4Control *control) override;
55 const IR::Node *postorder(IR::P4Action *action) override;
56 bool hasHeaderUnionField(IR::Type_Struct *s);
57};
58
85class DoFlattenHeaderUnionStack : public DoFlattenHeaderUnion {
86 std::map<cstring, std::vector<cstring>> stackMap;
87
88 public:
89 DoFlattenHeaderUnionStack(P4::ReferenceMap *refMap, P4::TypeMap *typeMap)
90 : DoFlattenHeaderUnion(refMap, typeMap) {
91 setName("DoFlattenHeaderUnionStack");
92 }
93 const IR::Node *postorder(IR::Type_Struct *sf) override;
94 const IR::Node *postorder(IR::ArrayIndex *e) override;
95 const IR::Node *postorder(IR::Declaration_Variable *dv) override;
96 bool hasHeaderUnionStackField(IR::Type_Struct *s);
97};
98
131class HandleValidityHeaderUnion : public Transform {
132 P4::ReferenceMap *refMap;
133 P4::TypeMap *typeMap;
134 IR::IndexedVector<IR::Declaration> toInsert; // temporaries
135
136 public:
137 HandleValidityHeaderUnion(P4::ReferenceMap *refMap, P4::TypeMap *typeMap)
138 : refMap(refMap), typeMap(typeMap) {
139 setName("HandleValidityHeaderUnion");
140 }
141 const IR::Node *postorder(IR::AssignmentStatement *assn) override;
142 const IR::Node *postorder(IR::IfStatement *a) override;
143 const IR::Node *postorder(IR::SwitchStatement *a) override;
144 const IR::Node *postorder(IR::MethodCallStatement *mcs) override;
145 const IR::Node *postorder(IR::P4Parser *parser) override;
146 const IR::Node *postorder(IR::P4Control *control) override;
147 const IR::Node *postorder(IR::P4Action *action) override;
148 const IR::MethodCallStatement *processValidityForStr(const IR::Statement *s,
149 const IR::Member *m, cstring headerElement,
150 cstring setValid);
151 const IR::Node *setInvalidforRest(const IR::Statement *s, const IR::Member *m,
152 const IR::Type_HeaderUnion *hu, cstring exclude,
153 bool setValidforCurrMem);
154 const IR::Node *expandIsValid(const IR::Statement *a, const IR::MethodCallExpression *mce,
156};
157
158class RemoveUnusedHUDeclarations : public Transform {
159 const UsedDeclSet &used;
160
161 public:
162 explicit RemoveUnusedHUDeclarations(const UsedDeclSet &used) : used(used) {}
163
164 const IR::Node *preorder(IR::Type_HeaderUnion *type) override {
165 if (!used.isUsed(getOriginal<IR::IDeclaration>())) return nullptr;
166 return type;
167 }
168};
169
170class RemoveAllUnusedHUDDeclarations : public PassManager {
171 UsedDeclSet used;
172
173 public:
174 RemoveAllUnusedHUDDeclarations()
175 : PassManager({
176 new CollectUsedDeclarations(used),
178 }) {
179 setName("RemoveAllUnusedHUDDeclarations");
180 setStopOnError(true);
181 }
182};
183
190class FlattenHeaderUnion : public PassManager {
191 public:
192 FlattenHeaderUnion(P4::ReferenceMap *refMap, P4::TypeMap *typeMap, bool loopsUnroll = true) {
193 passes.push_back(new P4::TypeChecking(refMap, typeMap));
194 passes.push_back(new HandleValidityHeaderUnion(refMap, typeMap));
195 // Stack flattening is only applicable if parser loops are unrolled and
196 // header union stack elements are accessed using [] notation. This pass does not handle
197 // .next .last etc accessors for stack elements.
198 if (loopsUnroll) {
199 passes.push_back(new DoFlattenHeaderUnionStack(refMap, typeMap));
201 }
202 passes.push_back(new P4::ClearTypeMap(typeMap));
203 passes.push_back(new P4::TypeInference(typeMap, false));
204 passes.push_back(new P4::TypeChecking(refMap, typeMap));
205 passes.push_back(new DoFlattenHeaderUnion(refMap, typeMap));
207 passes.push_back(new P4::RemoveAllUnusedHUDDeclarations());
208 passes.push_back(new P4::ClearTypeMap(typeMap));
209 passes.push_back(new P4::TypeChecking(nullptr, typeMap));
210 passes.push_back(new P4::RemoveParserIfs(typeMap));
211 }
212};
213} // namespace P4
214
215#endif /* MIDEND_FLATTENUNIONS_H_ */
Definition typeChecker.h:32
Collects all used declarations into @used set.
Definition unusedDeclarations.h:59
Definition flattenUnions.h:39
Definition flattenUnions.h:85
Definition flattenUnions.h:131
Definition indexed_vector.h:31
Definition node.h:53
Class used to encode maps from paths to declarations.
Definition referenceMap.h:67
Iterates UnusedDeclarations until convergence.
Definition unusedDeclarations.h:196
Definition flattenUnions.h:170
Definition parserControlFlow.h:105
Definition flattenUnions.h:158
Definition unusedDeclarations.h:48
Definition visitor.h:442
Definition typeChecker.h:55
Definition typeChecker.h:491
Definition typeMap.h:32
Definition unusedDeclarations.h:29
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13