P4C
The P4 Compiler
Loading...
Searching...
No Matches
specializeGenericFunctions.h
1/*
2Copyright 2020 VMware, Inc.
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 FRONTENDS_P4_SPECIALIZEGENERICFUNCTIONS_H_
18#define FRONTENDS_P4_SPECIALIZEGENERICFUNCTIONS_H_
19
20#include "frontends/common/resolveReferences/referenceMap.h"
21#include "frontends/common/resolveReferences/resolveReferences.h"
22#include "frontends/p4/typeChecking/typeChecker.h"
23#include "ir/ir.h"
24
25namespace P4 {
26
32 const IR::Function *original;
34 const IR::Function *specialized;
36 const IR::MethodCallExpression *invocation;
39
40 FunctionSpecialization(cstring name, const IR::MethodCallExpression *invocation,
41 const IR::Function *function, const IR::Node *insert)
42 : name(name),
43 original(function),
44 specialized(nullptr),
46 insertBefore(insert) {
47 CHECK_NULL(invocation);
48 CHECK_NULL(invocation);
49 CHECK_NULL(insertBefore);
50 }
51};
52
54 TypeMap *typeMap;
56 // Keep track of the values in the above map which are already
57 // inserted in the program.
58 std::set<FunctionSpecialization *> inserted;
59
60 void add(const IR::MethodCallExpression *mce, const IR::Function *func, const IR::Node *insert,
61 NameGenerator *nameGen) {
62 cstring name = nameGen->newName(func->name.string_view());
63 map.emplace(mce, new FunctionSpecialization(name, mce, func, insert));
64 }
65 FunctionSpecialization *get(const IR::MethodCallExpression *mce) const {
66 return ::P4::get(map, mce);
67 }
68 IR::Vector<IR::Node> *getInsertions(const IR::Node *insertionPoint) {
69 IR::Vector<IR::Node> *result = nullptr;
70 for (auto s : map) {
71 if (inserted.find(s.second) != inserted.end()) continue;
72 if (s.second->insertBefore == insertionPoint) {
73 if (result == nullptr) result = new IR::Vector<IR::Node>();
74 LOG2("Will insert " << dbp(s.second->specialized) << " before "
75 << dbp(insertionPoint));
76 result->push_back(s.second->specialized);
77 inserted.emplace(s.second);
78 }
79 }
80 return result;
81 }
82};
83
90
91 public:
92 explicit FindFunctionSpecializations(FunctionSpecializationMap *specMap) : specMap(specMap) {
93 CHECK_NULL(specMap);
94 setName("FindFunctionSpecializations");
95 }
96
97 bool preorder(const IR::MethodCallExpression *call) override;
98 profile_t init_apply(const IR::Node *node) override;
99};
100
122
123 public:
124 explicit SpecializeFunctions(FunctionSpecializationMap *specMap) : specMap(specMap) {
125 CHECK_NULL(specMap);
126 setName("SpecializeFunctions");
127 }
128 const IR::Node *postorder(IR::Function *function) override;
129 const IR::Node *postorder(IR::MethodCallExpression *) override;
130 const IR::Node *insert(const IR::Node *before);
131 const IR::Node *preorder(IR::P4Control *control) override { return insert(control); }
132 const IR::Node *preorder(IR::P4Parser *parser) override { return insert(parser); }
133 const IR::Node *preorder(IR::Function *function) override { return insert(function); }
134};
135
138
139 public:
140 explicit SpecializeGenericFunctions(TypeMap *typeMap) {
141 passes.emplace_back(new TypeChecking(nullptr, typeMap));
142 passes.emplace_back(new FindFunctionSpecializations(&specMap));
143 passes.emplace_back(new SpecializeFunctions(&specMap));
144 specMap.typeMap = typeMap;
145 setName("SpecializeGenericFunctions");
146 }
147};
148
149} // namespace P4
150
151#endif /* FRONTENDS_P4_SPECIALIZEGENERICFUNCTIONS_H_ */
Definition specializeGenericFunctions.h:87
Definition node.h:95
Definition vector.h:59
Definition visitor.h:400
Definition referenceMap.h:36
Definition referenceMap.h:29
Definition ir/pass_manager.h:40
Visitor mixin for looking up names in enclosing scopes from the Visitor::Context.
Definition resolveReferences.h:35
Specializes each generic function by substituting type parameters.
Definition specializeGenericFunctions.h:120
Definition specializeGenericFunctions.h:136
Definition visitor.h:424
Definition typeChecker.h:55
Definition typeMap.h:41
Definition visitor.h:78
Definition cstring.h:85
Definition ordered_map.h:32
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
Describes how a generic function is specialized.
Definition specializeGenericFunctions.h:28
const IR::Function * specialized
Result of specialization.
Definition specializeGenericFunctions.h:34
const IR::Node * insertBefore
Point in IR tree where to insert the function.
Definition specializeGenericFunctions.h:38
const IR::Function * original
Function that is being specialized.
Definition specializeGenericFunctions.h:32
const IR::MethodCallExpression * invocation
Invocation which causes this specialization.
Definition specializeGenericFunctions.h:36
cstring name
Name to use for specialized function.
Definition specializeGenericFunctions.h:30
Definition specializeGenericFunctions.h:53