P4C
The P4 Compiler
Loading...
Searching...
No Matches
eliminateActionRun.h
1/*
2 * SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef MIDEND_ELIMINATEACTIONRUN_H_
19#define MIDEND_ELIMINATEACTIONRUN_H_
20
21#include "frontends/common/resolveReferences/resolveReferences.h"
22#include "ir/ir.h"
23
24namespace P4 {
25
47class ElimActionRun : public PassManager {
49
50 struct atinfo_t {
51 const IR::P4Table *tbl;
52 const IR::Type_Enum *action_tags;
53 const IR::Declaration_Variable *action_run;
54 std::map<cstring, cstring> actions;
55 explicit atinfo_t(const IR::P4Table *t) : tbl(t) {}
56 };
57
58 std::map<cstring, atinfo_t> actionRunTables;
59 std::map<cstring, atinfo_t *> actionsToModify;
60 std::map<const IR::Statement *, IR::Vector<IR::StatOrDecl> *> prepend_statement;
61 IR::Vector<IR::Declaration> add_to_control;
62
63 class ActionTableUse : public Inspector, public ResolutionContext {
64 ElimActionRun &self;
65
66 public:
67 explicit ActionTableUse(ElimActionRun &s) : self(s) {}
68
69 bool preorder(const IR::P4Parser *) override { return false; }
70 bool preorder(const IR::P4Action *) override { return false; }
71 bool preorder(const IR::Function *) override { return false; }
72
73 void postorder(const IR::Member *) override;
74 } actionTableUse;
75
76 class RewriteActionRun : public Transform, public ResolutionContext {
77 ElimActionRun &self;
78
79 public:
80 explicit RewriteActionRun(ElimActionRun &s) : self(s) {}
81
82 const IR::P4Parser *preorder(IR::P4Parser *p) override {
83 prune();
84 return p;
85 }
86 const IR::Function *preorder(IR::Function *f) override {
87 prune();
88 return f;
89 }
90
91 const IR::Expression *postorder(IR::Member *) override;
92 const IR::P4Action *preorder(IR::P4Action *) override;
93 const IR::SwitchCase *postorder(IR::SwitchCase *) override;
94 const IR::Node *postorder(IR::Statement *) override;
95 const IR::P4Control *postorder(IR::P4Control *) override;
96 const IR::P4Program *postorder(IR::P4Program *) override;
97 };
98
99 public:
100 ElimActionRun() : actionTableUse(*this) {
101 addPasses({&actionTableUse, new PassIf([this]() { return !actionRunTables.empty(); },
102 {
103 &nameGen,
104 new RewriteActionRun(*this),
105 [this]() { actionRunTables.clear(); },
106 })});
107 }
108};
109
110} // namespace P4
111
112#endif /* MIDEND_ELIMINATEACTIONRUN_H_ */
Definition node.h:94
Definition vector.h:59
Definition visitor.h:413
Definition referenceMap.h:36
Definition ir/pass_manager.h:172
Visitor mixin for looking up names in enclosing scopes from the Visitor::Context.
Definition resolveReferences.h:35
Definition visitor.h:437
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24