P4C
The P4 Compiler
Loading...
Searching...
No Matches
localizeActions.h
1/*
2 * SPDX-FileCopyrightText: 2013 Barefoot Networks, Inc.
3 * Copyright 2013-present Barefoot Networks, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef FRONTENDS_P4_LOCALIZEACTIONS_H_
9#define FRONTENDS_P4_LOCALIZEACTIONS_H_
10
11#include "frontends/common/resolveReferences/referenceMap.h"
12#include "frontends/common/resolveReferences/resolveReferences.h"
13#include "frontends/p4/callGraph.h"
14#include "frontends/p4/typeChecking/typeChecker.h"
15#include "frontends/p4/unusedDeclarations.h"
16#include "ir/ir.h"
17#include "lib/ordered_set.h"
18
19namespace P4 {
20
22 public:
23 // For each control that uses an action and for each each global action
24 // we create a replacement.
25 std::map<const IR::P4Control *, ordered_map<const IR::P4Action *, const IR::P4Action *> *> repl;
26
27 const IR::P4Action *getReplacement(const IR::P4Action *action,
28 const IR::P4Control *control) const {
29 auto map = ::P4::get(repl, control);
30 if (map == nullptr) return nullptr;
31 if (map->find(action) != map->end()) return (*map)[action];
32 return nullptr;
33 }
34 void addReplacement(const IR::P4Action *action, const IR::P4Control *control,
35 const IR::P4Action *replacement) {
36 LOG1("Cloning global " << dbp(action) << " into " << dbp(replacement) << " for "
37 << dbp(control));
38 if (repl.find(control) == repl.end())
40 (*repl[control])[action] = replacement;
41 }
42};
43
44// Find global (i.e., declared at toplevel) actions and who uses them.
45class FindGlobalActionUses : public Inspector, public ResolutionContext {
48 std::set<const IR::P4Action *> globalActions;
49
50 public:
51 explicit FindGlobalActionUses(GlobalActionReplacements *repl) : repl(repl) {
52 CHECK_NULL(repl);
53 setName("FindGlobalActionUses");
54 }
55 bool preorder(const IR::PathExpression *path) override;
56 bool preorder(const IR::P4Action *action) override;
57 profile_t init_apply(const IR::Node *node) override;
58};
59
60// Global actions are cloned into actions local to the
61// control using them. One action can produce many copies.
62class LocalizeActions : public Transform, public ResolutionContext {
64
65 public:
66 explicit LocalizeActions(GlobalActionReplacements *repl) : repl(repl) {
67 visitDagOnce = false;
68 CHECK_NULL(repl);
69 setName("LocalizeActions");
70 }
71 const IR::Node *postorder(IR::P4Control *control) override;
72 const IR::Node *postorder(IR::PathExpression *expression) override;
73};
74
76 public:
77 // For each action and each user the replacement action to use.
78 // Node is either a P4Table or MethodCallExpression.
79 std::map<const IR::P4Action *, ordered_map<const IR::Node *, const IR::P4Action *> *> toInsert;
80 std::map<const IR::PathExpression *, const IR::P4Action *> repl;
81 // For each action all replacements to insert
82
83 const IR::P4Action *getActionUser(const IR::P4Action *action, const IR::Node *user) {
84 if (toInsert.find(action) == toInsert.end()) return nullptr;
85 auto map = toInsert[action];
86 CHECK_NULL(map);
87 if (map->find(user) == map->end()) return nullptr;
88 return (*map)[user];
89 }
90 void createReplacement(const IR::P4Action *original, const IR::Node *user,
91 const IR::P4Action *replacement) {
92 auto map = toInsert[original];
93 if (map == nullptr) {
95 toInsert[original] = map;
96 }
97 (*map)[user] = replacement;
98 }
99 // In the specified path replace the original with the replacement
100 void setRefReplacement(const IR::PathExpression *path, const IR::P4Action *replacement) {
101 LOG1("Adding replacement " << dbp(replacement) << " used by " << dbp(path));
102 repl[path] = replacement;
103 }
104};
105
106// Find actions that are invoked in multiple places; create a new
107// copy for each invocation and store it in the repl map. Ignores
108// actions that are not in a control.
109class FindRepeatedActionUses : public Inspector, public ResolutionContext {
110 MinimalNameGenerator nameGen;
111 ActionReplacement *repl;
112
113 public:
114 explicit FindRepeatedActionUses(ActionReplacement *repl) : repl(repl) {
115 CHECK_NULL(repl);
116 setName("FindRepeatedActionUses");
117 }
118 bool preorder(const IR::PathExpression *expression) override;
119 profile_t init_apply(const IR::Node *node) override;
120};
121
122// Replicates actions for each different user.
123// Should be run after LocalizeActions.
124class DuplicateActions : public Transform {
125 ActionReplacement *repl;
126
127 public:
128 explicit DuplicateActions(ActionReplacement *repl) : repl(repl) {
129 visitDagOnce = false;
130 CHECK_NULL(repl);
131 setName("DuplicateActions");
132 }
133 const IR::Node *postorder(IR::PathExpression *expression) override;
134 const IR::Node *postorder(IR::P4Control *control) override;
135};
136
137// Add a @name annotation on each global action that does not have one
138class TagGlobalActions : public Transform {
139 public:
140 TagGlobalActions() { setName("TagGlobalActions"); }
141 const IR::Node *preorder(IR::P4Action *action) override;
142 const IR::Node *preorder(IR::P4Parser *parser) override {
143 prune();
144 return parser;
145 }
146 const IR::Node *preorder(IR::P4Control *control) override {
147 prune();
148 return control;
149 }
150};
151
157class LocalizeAllActions : public PassManager {
158 GlobalActionReplacements globalReplacements;
159 ActionReplacement localReplacements;
160
161 public:
162 explicit LocalizeAllActions(const RemoveUnusedPolicy &policy) {
163 passes.emplace_back(new TagGlobalActions());
164 passes.emplace_back(new PassRepeated{
165 new FindGlobalActionUses(&globalReplacements),
166 new LocalizeActions(&globalReplacements),
167 });
168 passes.emplace_back(new FindRepeatedActionUses(&localReplacements));
169 passes.emplace_back(new DuplicateActions(&localReplacements));
170 passes.emplace_back(new RemoveAllUnusedDeclarations(policy));
171 setName("LocalizeAllActions");
172 }
173};
174
175} // namespace P4
176
177#endif /* FRONTENDS_P4_LOCALIZEACTIONS_H_ */
Definition localizeActions.h:75
Definition localizeActions.h:124
Definition localizeActions.h:45
Definition localizeActions.h:109
Definition localizeActions.h:21
Definition node.h:53
Definition visitor.h:418
Definition localizeActions.h:62
Definition referenceMap.h:36
Definition ir/pass_manager.h:146
Iterates UnusedDeclarations until convergence.
Definition unusedDeclarations.h:196
Definition unusedDeclarations.h:48
Definition localizeActions.h:138
Definition visitor.h:442
Definition visitor.h:78
Definition ordered_map.h:32
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13