P4C
The P4 Compiler
Loading...
Searching...
No Matches
specializeGenericFunctions.h
1/*
2 * Copyright 2020 VMware, Inc.
3 * SPDX-FileCopyrightText: 2020 VMware, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef FRONTENDS_P4_SPECIALIZEGENERICFUNCTIONS_H_
9#define FRONTENDS_P4_SPECIALIZEGENERICFUNCTIONS_H_
10
11#include "frontends/common/resolveReferences/referenceMap.h"
12#include "frontends/common/resolveReferences/resolveReferences.h"
13#include "frontends/p4/typeChecking/typeChecker.h"
14#include "ir/ir.h"
15
16namespace P4 {
17
19struct FunctionSpecialization {
23 const IR::Function *original;
25 const IR::Function *specialized;
27 const IR::MethodCallExpression *invocation;
30
31 FunctionSpecialization(cstring name, const IR::MethodCallExpression *invocation,
32 const IR::Function *function, const IR::Node *insert)
33 : name(name),
34 original(function),
35 specialized(nullptr),
37 insertBefore(insert) {
38 CHECK_NULL(invocation);
39 CHECK_NULL(invocation);
40 CHECK_NULL(insertBefore);
41 }
42};
43
45 TypeMap *typeMap;
47 // Keep track of the values in the above map which are already
48 // inserted in the program.
49 std::set<FunctionSpecialization *> inserted;
50
51 void add(const IR::MethodCallExpression *mce, const IR::Function *func, const IR::Node *insert,
52 NameGenerator *nameGen) {
53 cstring name = nameGen->newName(func->name.string_view());
54 map.emplace(mce, new FunctionSpecialization(name, mce, func, insert));
55 }
56 FunctionSpecialization *get(const IR::MethodCallExpression *mce) const {
57 return ::P4::get(map, mce);
58 }
59 IR::Vector<IR::Node> *getInsertions(const IR::Node *insertionPoint) {
60 IR::Vector<IR::Node> *result = nullptr;
61 for (auto s : map) {
62 if (inserted.find(s.second) != inserted.end()) continue;
63 if (s.second->insertBefore == insertionPoint) {
64 if (result == nullptr) result = new IR::Vector<IR::Node>();
65 LOG2("Will insert " << dbp(s.second->specialized) << " before "
66 << dbp(insertionPoint));
67 result->push_back(s.second->specialized);
68 inserted.emplace(s.second);
69 }
70 }
71 return result;
72 }
73};
74
78class FindFunctionSpecializations : public Inspector, public ResolutionContext {
81
82 public:
83 explicit FindFunctionSpecializations(FunctionSpecializationMap *specMap) : specMap(specMap) {
84 CHECK_NULL(specMap);
85 setName("FindFunctionSpecializations");
86 }
87
88 bool preorder(const IR::MethodCallExpression *call) override;
89 profile_t init_apply(const IR::Node *node) override;
90};
91
111class SpecializeFunctions : public Transform {
113
114 public:
115 explicit SpecializeFunctions(FunctionSpecializationMap *specMap) : specMap(specMap) {
116 CHECK_NULL(specMap);
117 setName("SpecializeFunctions");
118 }
119 const IR::Node *postorder(IR::Function *function) override;
120 const IR::Node *postorder(IR::MethodCallExpression *) override;
121 const IR::Node *insert(const IR::Node *before);
122 const IR::Node *preorder(IR::P4Control *control) override { return insert(control); }
123 const IR::Node *preorder(IR::P4Parser *parser) override { return insert(parser); }
124 const IR::Node *preorder(IR::Function *function) override { return insert(function); }
125};
126
127class SpecializeGenericFunctions : public PassManager {
129
130 public:
131 explicit SpecializeGenericFunctions(TypeMap *typeMap) {
132 passes.emplace_back(new TypeChecking(nullptr, typeMap));
133 passes.emplace_back(new FindFunctionSpecializations(&specMap));
134 passes.emplace_back(new SpecializeFunctions(&specMap));
135 specMap.typeMap = typeMap;
136 setName("SpecializeGenericFunctions");
137 }
138};
139
140} // namespace P4
141
142#endif /* FRONTENDS_P4_SPECIALIZEGENERICFUNCTIONS_H_ */
Definition specializeGenericFunctions.h:78
Definition node.h:53
Definition ir/vector.h:59
Definition visitor.h:418
Definition referenceMap.h:36
Definition referenceMap.h:29
Specializes each generic function by substituting type parameters.
Definition specializeGenericFunctions.h:111
Definition visitor.h:442
Definition typeChecker.h:55
Definition typeMap.h:32
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:13
Describes how a generic function is specialized.
Definition specializeGenericFunctions.h:19
const IR::Function * specialized
Result of specialization.
Definition specializeGenericFunctions.h:25
const IR::Node * insertBefore
Point in IR tree where to insert the function.
Definition specializeGenericFunctions.h:29
const IR::Function * original
Function that is being specialized.
Definition specializeGenericFunctions.h:23
const IR::MethodCallExpression * invocation
Invocation which causes this specialization.
Definition specializeGenericFunctions.h:27
cstring name
Name to use for specialized function.
Definition specializeGenericFunctions.h:21
Definition specializeGenericFunctions.h:44