P4C
The P4 Compiler
Loading...
Searching...
No Matches
dpdkProgram.h
1/*
2Copyright 2020 Intel Corp.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef BACKENDS_DPDK_DPDKPROGRAM_H_
18#define BACKENDS_DPDK_DPDKPROGRAM_H_
19
20#include "dpdkArch.h"
21#include "dpdkProgramStructure.h"
22#include "frontends/common/constantFolding.h"
23#include "frontends/common/resolveReferences/referenceMap.h"
24#include "frontends/p4/coreLibrary.h"
25#include "frontends/p4/enumInstance.h"
26#include "frontends/p4/evaluator/evaluator.h"
27#include "frontends/p4/methodInstance.h"
28#include "frontends/p4/simplify.h"
29#include "frontends/p4/typeMap.h"
30#include "frontends/p4/unusedDeclarations.h"
31#include "ir/ir.h"
32#include "lib/big_int_util.h"
33#include "lib/json.h"
34#include "options.h"
35
36namespace P4::DPDK {
37
38using namespace P4::literals;
39
41 P4::TypeMap *typemap;
42 P4::ReferenceMap *refmap;
43 DpdkProgramStructure *structure;
44 DpdkOptions &options;
45 const IR::DpdkAsmProgram *dpdk_program;
46
47 public:
49 DpdkProgramStructure *structure, DpdkOptions &options)
50 : typemap(typemap), refmap(refmap), structure(structure), options(options) {}
51
52 const IR::DpdkAsmProgram *create(IR::P4Program *prog);
53 IR::IndexedVector<IR::DpdkAsmStatement> create_pna_preamble();
54 IR::IndexedVector<IR::DpdkAsmStatement> create_psa_preamble();
55 IR::IndexedVector<IR::DpdkAsmStatement> create_pna_postamble();
56 IR::IndexedVector<IR::DpdkAsmStatement> create_psa_postamble();
57 const IR::Node *preorder(IR::P4Program *p) override;
58 const IR::DpdkAsmProgram *getDpdkProgram() { return dpdk_program; }
60 IR::Type_Struct *metadata);
61};
62
65 P4::ReferenceMap *refmap;
66 P4::TypeMap *typemap;
67 DpdkProgramStructure *structure;
68 IR::Type_Struct *metadataStruct;
69
70 public:
72 DpdkProgramStructure *structure, IR::Type_Struct *metadataStruct)
73 : refmap(refmap), typemap(typemap), structure(structure), metadataStruct(metadataStruct) {}
74 IR::IndexedVector<IR::DpdkAsmStatement> getInstructions() { return instructions; }
75
76 bool preorder(const IR::P4Parser *a) override;
77 bool preorder(const IR::ParserState *s) override;
78 void add_instr(const IR::DpdkAsmStatement *s) { instructions.push_back(s); }
79 cstring append_parser_name(const IR::P4Parser *p, cstring);
80 IR::Declaration_Variable *addNewTmpVarToMetadata(cstring name, const IR::Type *type);
81 void handleTupleExpression(const IR::ListExpression *cl, const IR::ListExpression *input,
82 int inputSize, cstring trueLabel, cstring falseLabel);
83 void getCondVars(const IR::Expression *sv, const IR::Expression *ce, IR::Expression **leftExpr,
84 IR::Expression **rightExpr);
85};
86
88 P4::TypeMap *typemap;
89 P4::ReferenceMap *refmap;
90 DpdkProgramStructure *structure;
91 IR::Type_Struct *metadataStruct;
97 std::set<cstring> unique_actions;
98 bool deparser;
99
100 public:
102 DpdkProgramStructure *structure, IR::Type_Struct *metadataStruct,
103 bool deparser = false)
104 : typemap(typemap),
105 refmap(refmap),
106 structure(structure),
107 metadataStruct(metadataStruct),
108 deparser(deparser) {}
109
110 IR::IndexedVector<IR::DpdkTable> &getTables() { return tables; }
111 IR::IndexedVector<IR::DpdkSelector> &getSelectors() { return selectors; }
112 IR::IndexedVector<IR::DpdkLearner> &getLearners() { return learners; }
113 IR::IndexedVector<IR::DpdkAction> &getActions() { return actions; }
114 IR::IndexedVector<IR::DpdkAsmStatement> &getInstructions() { return instructions; }
115
116 bool preorder(const IR::P4Action *a) override;
117 bool preorder(const IR::P4Table *a) override;
118 bool preorder(const IR::P4Control *) override;
119 bool checkTableValid(const IR::P4Table *a);
120
121 void add_inst(const IR::DpdkAsmStatement *s) { instructions.push_back(s); }
122 void add_table(const IR::DpdkTable *t) { tables.push_back(t); }
123 void add_table(const IR::DpdkSelector *s) { selectors.push_back(s); }
124 void add_table(const IR::DpdkLearner *s) { learners.push_back(s); }
125 void add_action(const IR::DpdkAction *a) { actions.push_back(a); }
126
127 std::optional<const IR::Member *> getMemExprFromProperty(const IR::P4Table *, cstring);
128 std::optional<int> getNumberFromProperty(const IR::P4Table *, cstring);
129};
130
132 ordered_set<cstring> &actions;
133
134 public:
135 explicit CollectActionUses(ordered_set<cstring> &a) : actions(a) {}
136 bool preorder(const IR::ActionListElement *ale) {
137 if (auto mce = ale->expression->to<IR::MethodCallExpression>()) {
138 if (auto path = mce->method->to<IR::PathExpression>()) {
139 if (path->path->name.originalName == "NoAction")
140 actions.insert("NoAction"_cs);
141 else
142 actions.insert(path->path->name.name);
143 }
144 }
145 return false;
146 }
147};
148
150 const ordered_set<cstring> &used_actions;
151 std::set<cstring> kept_actions;
152
153 public:
154 explicit ElimUnusedActions(const ordered_set<cstring> &a) : used_actions(a) {}
155 const IR::Node *postorder(IR::DpdkAction *a) override {
156 if (kept_actions.count(a->name.name) != 0) return nullptr;
157 if (used_actions.find(a->name.name) != used_actions.end()) {
158 kept_actions.insert(a->name.name);
159 return a;
160 }
161 return nullptr;
162 }
163};
164
169 ordered_set<cstring> actions;
170
171 public:
173 addPasses({new CollectActionUses(actions), new ElimUnusedActions(actions)});
174 }
175};
176
177} // namespace P4::DPDK
178#endif /* BACKENDS_DPDK_DPDKPROGRAM_H_ */
Definition dpdkProgram.h:131
Definition dpdkProgram.h:87
bool checkTableValid(const IR::P4Table *a)
Definition dpdkProgram.cpp:518
Definition dpdkProgram.h:63
void getCondVars(const IR::Expression *sv, const IR::Expression *ce, IR::Expression **leftExpr, IR::Expression **rightExpr)
Definition dpdkProgram.cpp:276
Definition dpdkProgram.h:40
IR::IndexedVector< IR::DpdkStructType > UpdateHeaderMetadata(IR::P4Program *prog, IR::Type_Struct *metadata)
Definition dpdkProgram.cpp:30
Definition backends/dpdk/options.h:24
Definition dpdkProgram.h:149
Definition dpdkProgram.h:168
Definition node.h:52
Definition node.h:95
Definition visitor.h:400
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
Definition ordered_set.h:32
Definition dpdk/backend.cpp:37
Definition cstring.h:80
Collect information related to P4 programs targeting dpdk.
Definition dpdkProgramStructure.h:16