P4C
The P4 Compiler
Loading...
Searching...
No Matches
constraints.h
1
19#ifndef BF_P4C_PHV_CONSTRAINTS_CONSTRAINTS_H_
20#define BF_P4C_PHV_CONSTRAINTS_CONSTRAINTS_H_
21
22#include <cstdint>
23#include <ostream>
24
25#include "lib/cstring.h"
26#include "lib/ordered_set.h"
27
30
31namespace PHV {
32
33class Field;
34
35}
36
37namespace Constraints {
38
39using namespace P4;
40
45 protected:
46 unsigned reason = 0;
47
48 public:
49 virtual bool hasConstraint() const = 0;
50 virtual void addConstraint(uint32_t reason) = 0;
51};
52
57 public:
58 // Define reasons for constraints here as enum classes.
59 enum SolitaryReason {
60 NONE = 0, // represents absence of solitary constraint
61 ALU = (1 << 0), // solitary constraint due to ALU operation
62 CHECKSUM = (1 << 1), // solitary constraint due to use in checksum
63 ARCH = (1 << 2), // solitary constraint required by the hardware
64 DIGEST = (1 << 3), // solitary constraint due to use in digest
65 PRAGMA_SOLITARY = (1 << 4), // solitary constraint due to pa_solitary pragma
66 PRAGMA_CONTAINER_SIZE = (1 << 5), // solitary constraint due to pa_container_size
67 CONFLICT_ALIGNMENT = (1 << 6), // solitary constraint due to conflicting alignment
68 // in bridge packing
69 CLEAR_ON_WRITE = (1 << 7) // solitary constraint due to the field being
70 // cleared-on-write
71 };
72
73 bool hasConstraint() const { return (reason != 0); }
74 void addConstraint(uint32_t r) { reason |= r; }
75
76 bool isALU() const { return reason & ALU; }
77 bool isChecksum() const { return reason & CHECKSUM; }
78 bool isArch() const { return reason & ARCH; }
79 bool isDigest() const { return reason & DIGEST; }
80 bool isPragmaSolitary() const { return reason & PRAGMA_SOLITARY; }
81 bool isPragmaContainerSize() const { return reason & PRAGMA_CONTAINER_SIZE; }
82 bool isClearOnWrite() const { return reason & CLEAR_ON_WRITE; }
83 bool isOnlyClearOnWrite() const { return reason == CLEAR_ON_WRITE; }
84};
85
86std::ostream &operator<<(std::ostream &out, const SolitaryConstraint &cons);
87
91 public:
92 // Define type of digest in which the field is used.
93 enum DigestType {
94 NONE = 0, // Field is not used in a digest
95 MIRROR = (1 << 0), // used in mirror digest
96 LEARNING = (1 << 1), // used in learning digest
97 RESUBMIT = (1 << 2), // used in resubmit
98 PKTGEN = (1 << 3) // used in pktgen
99 };
100
101 bool hasConstraint() const { return (reason != 0); }
102 void addConstraint(uint32_t r) { reason |= r; }
103
104 bool isMirror() const { return reason & MIRROR; }
105 bool isLearning() const { return reason & LEARNING; }
106 bool isResubmit() const { return reason & RESUBMIT; }
107 bool isPktGen() const { return reason & PKTGEN; }
108};
109
111 public:
112 enum DeparsedToTMReason {
113 NONE = 0, // Field is not deparsed to TM
114 DEPARSE = 1 << 0, // Field is deparsed to TM
115 };
116
117 bool hasConstraint() const { return (reason != 0); }
118 void addConstraint(uint32_t r) { reason |= r; }
119
120 bool isDeparse() const { return reason & DEPARSE; }
121};
122
124 public:
125 enum NoSplitReason {
126 NONE = 0, // Field is not deparsed to TM
127 NO_SPLIT = 1 << 0, // Field is deparsed to TM
128 };
129
130 bool hasConstraint() const { return (reason != 0); }
131 void addConstraint(uint32_t r) { reason |= r; }
132
133 bool isNoSplit() const { return reason & NO_SPLIT; }
134};
135
137 protected:
138 unsigned reason = 0;
139 unsigned value = 0;
140
141 public:
142 virtual ~IntegerConstraint() {}
143 virtual bool hasConstraint() const = 0;
144 virtual void addConstraint(unsigned r, unsigned v) = 0;
145};
146
150 protected:
151 // used by bridged packing to insert extra padding to ease phv allocation
152 // see IMPL_NOTE(0) in bridged_packing.h
153 // DO NOT OUTPUT THIS CONSTRAINT TO USER LOG
154 unsigned container_size = 0;
155
156 public:
157 // Define the cause of alignment constraint
158 enum AlignmentReason {
159 NONE = 0,
160 BRIDGE = (1 << 0),
161 PARSER = (1 << 1),
162 DEPARSER = (1 << 2),
163 TERNARY_MATCH = (1 << 3),
164 DIGEST = (1 << 4),
165 INTRINSIC = (1 << 5),
166 PA_BYTE_PACK = (1 << 6),
167 };
168
170 bool hasConstraint() const { return (reason != 0); }
171 void addConstraint(unsigned source, unsigned v) {
172 reason |= source;
173 value = v;
174 }
175
176 void updateConstraint(unsigned source) { reason |= source; }
177 void eraseConstraint() { reason = 0; }
178 unsigned getAlignment() const { return value; }
179 unsigned getReason() const { return reason; }
180
181 void setContainerSize(unsigned size) { container_size = size; }
182 unsigned getContainerSize() const { return container_size; }
183
184 bool isBridged() const { return reason & BRIDGE; }
185 bool isParser() const { return reason & PARSER; }
186 bool isDeparser() const { return reason & DEPARSER; }
187 bool isTernaryMatch() const { return reason & TERNARY_MATCH; }
188 bool isDigest() const { return reason & DIGEST; }
189 bool isIntrinsic() const { return reason & INTRINSIC; }
190
191 bool operator==(const AlignmentConstraint &a) const {
192 return reason == a.reason && value == a.value;
193 }
194 bool operator<(AlignmentConstraint const &a) const {
195 if (value < a.value)
196 return true;
197 else if (reason < a.reason)
198 return true;
199 return false;
200 }
201};
202
204 public:
205 enum ContainerSizeReason { NONE = 0, PRAGMA = 1 };
206
208 bool hasConstraint() const { return (reason != 0); }
209 void addConstraint(unsigned source, unsigned v) {
210 reason = source;
211 value = v;
212 }
213 unsigned getContainerSize() const { return value; }
214};
215
217 protected:
218 unsigned reason = 0;
219 /* fields that share the same group constraint */
221
222 public:
223 virtual ~GroupConstraint() {}
224 virtual bool hasConstraint() const = 0;
225 virtual void addConstraint(uint32_t reason) = 0;
226};
227
229 protected:
230 unsigned reason = 0;
231 // represents the pair of fields have the mutual constraint
232 // e.g. A pair of field that must be packed to the same byte.
233 // A pair of field that must be be aligned to the same bit offset.
234 std::pair<cstring, cstring> fields;
235
236 public:
238 if (f1 < f2) {
239 fields.first = f1;
240 fields.second = f2;
241 } else {
242 fields.first = f2;
243 fields.second = f1;
244 }
245 }
247 fields.first = p.fields.first;
248 fields.second = p.fields.second;
249 }
250 ~PairConstraint() {}
251 virtual bool hasConstraint() const = 0;
252 virtual void addConstraint(uint32_t reason) = 0;
253 bool operator==(const PairConstraint &a) const {
254 return reason == a.reason && fields == a.fields;
255 }
256 bool operator<(PairConstraint const &a) const {
257 if (reason < a.reason)
258 return true;
259 else if (fields < a.fields)
260 return true;
261 return false;
262 }
263};
264
266 public:
268 bool hasConstraint() const { return (reason != 0); }
269 void addConstraint(unsigned r) { reason |= r; }
270};
271
273 public:
275 bool hasConstraint() const { return (reason != 0); }
276 void addConstraint(unsigned r) { reason |= r; }
277};
278
280 public:
282 bool hasConstraint() const { return (reason != 0); }
283 void addConstraint(unsigned r) { reason |= r; }
284};
285
287 public:
289 bool hasConstraint() const { return (reason != 0); }
290 void addConstraint(unsigned r) { reason |= r; }
291};
292
293} // namespace Constraints
294
295#endif /* BF_P4C_PHV_CONSTRAINTS_CONSTRAINTS_H_ */
Definition constraints.h:149
Definition constraints.h:44
Definition constraints.h:203
Definition constraints.h:265
Definition constraints.h:110
Definition constraints.h:90
Definition constraints.h:216
Definition constraints.h:136
Definition constraints.h:286
Definition constraints.h:272
Definition constraints.h:279
Definition constraints.h:123
Definition constraints.h:228
Definition constraints.h:56
Definition cstring.h:85
Definition ordered_set.h:32
Definition tofino/bf-p4c/phv/constraints/constraints.cpp:21
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
The namespace encapsulating PHV-related stuff.
Definition gateway.h:32