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(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 RemoveAllUnusedDeclarations(policy));
74 setName("InlineActions");
75 }
76};
77
78} // namespace P4
79
80namespace P4::P4_14 {
81
83class InlineActions : public Transform {
84 const IR::V1Program *global;
85 class SubstActionArgs : public Transform {
86 const IR::ActionFunction *function;
87 const IR::Primitive *callsite;
88 const IR::Node *postorder(IR::ActionArg *arg) override {
89 for (unsigned i = 0; i < function->args.size(); ++i)
90 if (function->args[i] == getOriginal()) return callsite->operands[i];
91 BUG("Action arg not argument of action");
92 return arg;
93 }
94
95 public:
96 SubstActionArgs(const IR::ActionFunction *f, const IR::Primitive *c)
97 : function(f), callsite(c) {}
98 };
99 const IR::V1Program *preorder(IR::V1Program *gl) override { return global = gl; }
100 const IR::Node *preorder(IR::Primitive *p) override {
101 if (auto af = global->get<IR::ActionFunction>(p->name)) {
102 SubstActionArgs saa(af, p);
103 saa.setCalledBy(this);
104 return af->action.clone()->apply(saa);
105 }
106 return p;
107 }
108};
109
110} // namespace P4::P4_14
111
112#endif /* FRONTENDS_P4_ACTIONSINLINING_H_ */
Definition commonInlining.h:157
Definition actionsInlining.h:47
Definition actionsInlining.h:32
Definition node.h:94
Definition actionsInlining.h:65
Definition commonInlining.h:178
Definition visitor.h:400
Special inliner which works directly on P4-14 representation.
Definition actionsInlining.h:83
Definition ir/pass_manager.h:40
Iterates RemoveUnusedDeclarations until convergence.
Definition unusedDeclarations.h:189
Definition unusedDeclarations.h:48
Visitor mixin for looking up names in enclosing scopes from the Visitor::Context.
Definition resolveReferences.h:35
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