P4C
The P4 Compiler
Loading...
Searching...
No Matches
actionsInlining.h
1/*
2Copyright 2013-present Barefoot Networks, 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_ACTIONSINLINING_H_
18#define FRONTENDS_P4_ACTIONSINLINING_H_
19
20#include "commonInlining.h"
21#include "frontends/common/resolveReferences/resolveReferences.h"
22#include "frontends/p4/typeChecking/typeChecker.h"
23#include "frontends/p4/unusedDeclarations.h"
24#include "ir/ir.h"
25
26namespace P4 {
27
28using ActionCallInfo = SimpleCallInfo<IR::P4Action, IR::MethodCallStatement>;
29using AInlineWorkList = SimpleInlineWorkList<ActionCallInfo>;
30using ActionsInlineList = SimpleInlineList<IR::P4Action, ActionCallInfo, AInlineWorkList>;
31
33 ActionsInlineList *toInline; // output
34 P4::TypeMap *typeMap; // input
35 public:
37 : toInline(toInline), typeMap(typeMap) {
38 CHECK_NULL(toInline);
39 CHECK_NULL(typeMap);
40 setName("DiscoverActionsInlining");
41 }
42 bool preorder(const IR::P4Parser *) override { return false; } // skip
43 void postorder(const IR::MethodCallStatement *mcs) override;
44};
45
46// General-purpose actions inliner.
47class ActionsInliner : public AbstractInliner<ActionsInlineList, AInlineWorkList> {
48 std::unique_ptr<MinimalNameGenerator> nameGen;
49 AInlineWorkList::ReplacementMap *replMap;
50
51 public:
52 ActionsInliner() : replMap(nullptr) {}
53 Visitor::profile_t init_apply(const IR::Node *node) override;
54 const IR::Node *preorder(IR::P4Parser *cont) override {
55 prune();
56 return cont;
57 } // skip
58 const IR::Node *preorder(IR::P4Action *action) override;
59 const IR::Node *postorder(IR::P4Action *action) override;
60 const IR::Node *preorder(IR::MethodCallStatement *statement) override;
61};
62
64
65class InlineActions : public PassManager {
66 ActionsInlineList actionsToInline;
67
68 public:
69 InlineActions(ReferenceMap *refMap, TypeMap *typeMap, const RemoveUnusedPolicy &policy) {
70 passes.push_back(new TypeChecking(nullptr, typeMap));
71 passes.push_back(new DiscoverActionsInlining(&actionsToInline, typeMap));
72 passes.push_back(new InlineActionsDriver(&actionsToInline, new ActionsInliner()));
73 passes.push_back(new ResolveReferences(refMap));
74 passes.push_back(new RemoveAllUnusedDeclarations(refMap, policy));
75 setName("InlineActions");
76 }
77};
78
79} // namespace P4
80
81namespace P4::P4_14 {
82
84class InlineActions : public Transform {
85 const IR::V1Program *global;
86 class SubstActionArgs : public Transform {
87 const IR::ActionFunction *function;
88 const IR::Primitive *callsite;
89 const IR::Node *postorder(IR::ActionArg *arg) override {
90 for (unsigned i = 0; i < function->args.size(); ++i)
91 if (function->args[i] == getOriginal()) return callsite->operands[i];
92 BUG("Action arg not argument of action");
93 return arg;
94 }
95
96 public:
97 SubstActionArgs(const IR::ActionFunction *f, const IR::Primitive *c)
98 : function(f), callsite(c) {}
99 };
100 const IR::V1Program *preorder(IR::V1Program *gl) override { return global = gl; }
101 const IR::Node *preorder(IR::Primitive *p) override {
102 if (auto af = global->get<IR::ActionFunction>(p->name)) {
103 SubstActionArgs saa(af, p);
104 saa.setCalledBy(this);
105 return af->action.clone()->apply(saa);
106 }
107 return p;
108 }
109};
110
111} // namespace P4::P4_14
112
113#endif /* FRONTENDS_P4_ACTIONSINLINING_H_ */
Definition commonInlining.h:157
Definition actionsInlining.h:47
Definition actionsInlining.h:32
Definition node.h:95
Definition actionsInlining.h:65
Definition commonInlining.h:178
Definition visitor.h:400
Special inliner which works directly on P4-14 representation.
Definition actionsInlining.h:84
Definition pass_manager.h:40
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Iterates RemoveUnusedDeclarations until convergence.
Definition unusedDeclarations.h:146
Definition unusedDeclarations.h:28
Visitor mixin for looking up names in enclosing scopes from the Visitor::Context.
Definition resolveReferences.h:33
Definition resolveReferences.h:121
Definition visitor.h:424
Definition typeChecker.h:55
Definition typeMap.h:41
Definition visitor.h:78
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24