P4C
The P4 Compiler
Loading...
Searching...
No Matches
parser_critical_path.h
1
19#ifndef BF_P4C_PHV_ANALYSIS_PARSER_CRITICAL_PATH_H_
20#define BF_P4C_PHV_ANALYSIS_PARSER_CRITICAL_PATH_H_
21
22#include "bf-p4c/ir/control_flow_visitor.h"
23#include "bf-p4c/parde/parde_visitor.h"
24#include "bf-p4c/phv/phv_fields.h"
25#include "ir/ir.h"
26#include "lib/cstring.h"
27
32 std::vector<std::pair<const IR::BFN::ParserState *, int>> path;
33 int length;
34
35 ParserCriticalPathResult() : path(), length(0) {}
36
37 // Clear the state stored in the ParserCriticalPathResult struct.
38 void clear() {
39 path.clear();
40 length = 0;
41 }
42};
43
53 private:
54 profile_t init_apply(const IR::Node *root) override {
55 profile_t rv = Inspector::init_apply(root);
56 result.clear();
57 return rv;
58 }
59
60 bool preorder(const IR::BFN::AbstractParser *parser) override {
61 return gress_ == parser->gress;
62 };
63
64 bool preorder(const IR::BFN::ParserPrimitive *) override { return false; };
65
66 bool preorder(const IR::BFN::ParserState *state) override;
67
68 void flow_merge(Visitor &) override;
69 void flow_copy(::ControlFlowVisitor &) override;
70 void end_apply() override;
71 gress_t gress_;
72 ParserCriticalPathResult &final_result;
73
74 public:
76 : gress_(gress), final_result(rst) {
77 joinFlows = true;
78 visitDagOnce = false;
79 }
80
81 ParserCriticalPath *clone() const override { return new ParserCriticalPath(*this); }
82
83 public:
85};
86
88 gress_t gress;
90
91 bool preorder(const IR::BFN::ParserState *state) override {
92 if (state->gress != gress) return false;
93
94 for (const auto *p4State : state->p4States) {
95 for (auto annot : p4State->annotations->annotations) {
96 if (annot->name.name == "critical") {
97 auto &exprs = annot->expr;
98 if (exprs.size() == 1) {
99 auto gress = exprs[0]->to<IR::StringLiteral>();
100 if (!gress) {
101 error(
102 "Invalid use of %1%, correct usage is: "
103 "@pragma critical [ingress/egress]",
104 annot);
105 }
106
107 if (gress && gress->value == toString(state->gress)) {
108 LOG3("@critical specified on " << state->name);
109 critical_states.insert(state);
110 return true;
111 }
112 } else if (exprs.size() == 0) {
113 LOG3("@critical specified on " << state->name);
114 critical_states.insert(state);
115 return true;
116 }
117 }
118 }
119 }
120
121 return true;
122 }
123
124 public:
126 : gress(gr), critical_states(cs) {}
127};
128
132 public:
133 explicit CalcParserCriticalPath(const PhvInfo &phv) : phv(phv) {
134 addPasses({new CollectUserSpecifiedCriticalStates(INGRESS, ingress_user_critical_states),
135 new CollectUserSpecifiedCriticalStates(EGRESS, egress_user_critical_states),
136 new ParserCriticalPath(INGRESS, ingress_result),
137 new ParserCriticalPath(EGRESS, egress_result)});
138 }
139
140 ordered_set<const PHV::Field *> calc_all_critical_fields() const;
141
142 const ParserCriticalPathResult &get_ingress_result() const { return ingress_result; }
143
144 const ParserCriticalPathResult &get_egress_result() const { return egress_result; }
145
146 const ordered_set<const IR::BFN::ParserState *> &get_ingress_user_critical_states() const {
147 return ingress_user_critical_states;
148 }
149
150 const ordered_set<const IR::BFN::ParserState *> &get_egress_user_critical_states() const {
151 return egress_user_critical_states;
152 }
153
154 bool is_on_critical_path(const IR::BFN::ParserState *state) const;
155 bool is_user_specified_critical_state(const IR::BFN::ParserState *state) const;
156
157 private:
158 static bool is_on_critical_path(const IR::BFN::ParserState *state,
159 const ParserCriticalPathResult &result);
160
161 static bool is_user_specified_critical_state(
162 const IR::BFN::ParserState *state, const ordered_set<const IR::BFN::ParserState *> &result);
163
164 ordered_set<const PHV::Field *> calc_critical_fields(
165 const ParserCriticalPathResult &critical_path) const;
166
167 private:
168 const PhvInfo &phv;
169
170 ParserCriticalPathResult ingress_result;
171 ParserCriticalPathResult egress_result;
172
173 ordered_set<const IR::BFN::ParserState *> ingress_user_critical_states;
174 ordered_set<const IR::BFN::ParserState *> egress_user_critical_states;
175};
176
177std::ostream &operator<<(std::ostream &out, const ParserCriticalPathResult &rst);
178
179#endif /* BF_P4C_PHV_ANALYSIS_PARSER_CRITICAL_PATH_H_ */
Definition parser_critical_path.h:131
Definition parser_critical_path.h:87
Definition visitor.h:463
Definition node.h:95
Definition visitor.h:400
Definition ir/pass_manager.h:40
Definition visitor.h:78
Definition visitor.h:75
Definition ordered_set.h:32
Definition parser_critical_path.h:52
Definition phv_fields.h:1095
Definition parde_visitor.h:66
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:51
Definition parser_critical_path.h:28
std::vector< std::pair< const IR::BFN::ParserState *, int > > path
Definition parser_critical_path.h:32