P4C
The P4 Compiler
Loading...
Searching...
No Matches
multiple_apply.h
1
19#ifndef BF_P4C_COMMON_MULTIPLE_APPLY_H_
20#define BF_P4C_COMMON_MULTIPLE_APPLY_H_
21
22#include <set>
23
24#include "backends/tofino/bf-p4c/ir/thread_visitor.h"
25#include "backends/tofino/bf-p4c/lib/union_find.hpp"
26#include "backends/tofino/bf-p4c/mau/mau_visitor.h"
27#include "backends/tofino/bf-p4c/mau/table_mutex.h"
28#include "lib/ordered_map.h"
29#include "lib/ordered_set.h"
30
31using namespace P4;
32
33class BFN_Options;
34
69class MultipleApply : public PassManager {
70 // gtest requirements
71 std::set<cstring> mutex_errors;
72 std::set<cstring> topological_errors;
73 std::set<cstring> default_next_errors;
74
75 const BFN_Options &options;
76
78 bool longBranchDisabled = false;
79
82
84 class MutuallyExclusiveApplies : public MauInspector {
86
89 std::set<cstring> &errors;
90
94
95 Visitor::profile_t init_apply(const IR::Node *root) override;
96 void postorder(const IR::MAU::Table *) override;
97
98 public:
99 explicit MutuallyExclusiveApplies(TablesMutuallyExclusive &m, std::set<cstring> &e)
100 : mutex(m), errors(e) {}
101 };
102
104 UnionFind<const IR::MAU::Table *> duplicate_tables;
105
109 class CheckStaticNextTable : public MauInspector {
110 MultipleApply &self;
111
114
117
119 bool check_equiv(const IR::MAU::Table *table1, const IR::MAU::Table *table2,
120 bool makeUnion = true);
121
124 bool check_equiv(const IR::MAU::TableSeq *seq1, const IR::MAU::TableSeq *seq2,
125 bool makeUnion = true);
126
128 bool equiv_gateway(const IR::Expression *expr1, const IR::Expression *expr2);
129
130 Visitor::profile_t init_apply(const IR::Node *root) override;
131
132 void check_all_gws(const IR::MAU::Table *);
133 void postorder(const IR::MAU::Table *) override;
134
135 public:
136 explicit CheckStaticNextTable(MultipleApply &s) : self(s) {}
137 };
138
140 class DeduplicateTables : public MauTransform {
141 MultipleApply &self;
142
146 std::optional<gress_t> gress;
147
150 std::map<gress_t, std::map<const IR::MAU::Table *, const IR::MAU::Table *>> replacements;
151
153 std::map<gress_t, std::vector<const IR::MAU::TableSeq *>> table_seqs_seen;
154
156 gress_t getGress() const { return gress ? *gress : VisitingThread(this); }
157
158 Visitor::profile_t init_apply(const IR::Node *root) override;
159 const IR::Node *postorder(IR::MAU::Table *tbl) override;
160 const IR::Node *postorder(IR::MAU::TableSeq *seq) override;
161
162 public:
163 explicit DeduplicateTables(MultipleApply &s, std::optional<gress_t> gress = std::nullopt)
164 : self(s), gress(gress) {}
165 };
166
174 class CheckTopologicalTables : public MauInspector {
176 std::set<cstring> &errors;
177
178 bool preorder(const IR::MAU::TableSeq *) override;
179
180 public:
181 explicit CheckTopologicalTables(std::set<cstring> &errors) : errors(errors) {}
182 };
183
184 Visitor::profile_t init_apply(const IR::Node *root) override;
185
186 public:
187 bool mutex_error(cstring name) { return mutex_errors.count(name); }
188
189 unsigned num_mutex_errors() { return mutex_errors.size(); }
190
191 bool topological_error(cstring name) { return topological_errors.count(name); }
192
193 unsigned num_topological_errors() { return topological_errors.size(); }
194
195 bool default_next_error(cstring name) {
196 return default_next_errors.find(name) != default_next_errors.end();
197 }
198
199 unsigned num_default_next_errors() { return default_next_errors.size(); }
200
201 // For gtests, allow doing just the passes for de-duplicating tables and allow specifying which
202 // gress is being visited.
203 MultipleApply(const BFN_Options &options, std::optional<gress_t> gress = std::nullopt,
204 bool dedup_only = false, bool run_default_next = true);
205};
206
207#endif /* BF_P4C_COMMON_MULTIPLE_APPLY_H_ */
Definition bf-p4c-options.h:28
Definition mau_visitor.h:29
Definition mau_visitor.h:55
Definition multiple_apply.h:69
Definition node.h:94
Definition ir/pass_manager.h:40
Definition visitor.h:78
Definition cstring.h:85
Definition ordered_map.h:32
Definition ordered_set.h:32
Definition mau/table_mutex.h:110
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24