P4C
The P4 Compiler
Loading...
Searching...
No Matches
action_constraint_solver.h
1
19#ifndef BF_P4C_PHV_SOLVER_ACTION_CONSTRAINT_SOLVER_H_
20#define BF_P4C_PHV_SOLVER_ACTION_CONSTRAINT_SOLVER_H_
21
22#include <optional>
23#include <vector>
24
25#include "bf-p4c/ir/bitrange.h"
26#include "bf-p4c/phv/solver/symbolic_bitvec.h"
27#include "lib/bitvec.h"
28#include "lib/cstring.h"
29#include "lib/ordered_map.h"
30
31namespace solver {
32
33using namespace P4::literals;
34using namespace P4;
35
39
46 public:
47 virtual cstring name() const = 0;
48 virtual cstring to_cstring() const = 0;
49};
50
56class DepositField : public Instruction {
57 public:
58 ContainerID dest;
59 ContainerID src1;
60 int left_rotate;
61 bitvec mask;
62 ContainerID src2;
63 DepositField(ContainerID dest, ContainerID src1, int left_rotate, bitvec mask, ContainerID src2)
64 : dest(dest), src1(src1), left_rotate(left_rotate), mask(mask), src2(src2) {}
65 cstring name() const override { return "deposit-field"_cs; };
66 cstring to_cstring() const override;
67};
68
71class BitmaskedSet : public Instruction {
72 public:
73 ContainerID dest;
74 ContainerID src1;
75 ContainerID src2;
76 bitvec mask;
78 : dest(dest), src1(src1), src2(src2), mask(mask) {}
79 cstring name() const override { return "bitmasked-set"_cs; };
80 cstring to_cstring() const override;
81};
82
86 public:
87 ContainerID dest;
88 ContainerID src1;
89 int shift1;
90 ContainerID src2;
91 int shift2;
92 bitvec mask;
93 ByteRotateMerge(ContainerID dest, ContainerID src1, int shift1, ContainerID src2, int shift2,
94 bitvec mask)
95 : dest(dest), src1(src1), shift1(shift1), src2(src2), shift2(shift2), mask(mask) {}
96 cstring name() const override { return "byte-rotate-merge"_cs; };
97 cstring to_cstring() const override;
98};
99
103class ContainerSet : public Instruction {
104 public:
105 ContainerID dest;
106 ContainerID src;
107 ContainerSet(ContainerID dest, ContainerID src) : dest(dest), src(src) {}
108 cstring name() const override { return "set"_cs; };
109 cstring to_cstring() const override;
110};
111
112enum class ErrorCode {
113 unsat = 1,
114 invalid_input = 2,
115 too_many_container_sources = 3,
116 too_many_unaligned_sources = 4,
117 invalid_for_deposit_field = 5,
118 smt_sovler_unknown = 6,
119 deposit_src2_must_be_dest = 7,
120 non_rot_aligned_and_non_byte_shiftable = 8,
121 invalid_whole_container_write = 9,
122 dark_container_ad_or_const_source = 10,
123};
124
126struct Error {
127 ErrorCode code;
128 cstring msg;
129 Error(ErrorCode code, cstring msg) : code(code), msg(msg){};
130};
131
134 int size;
137};
138
140struct Result {
141 std::optional<Error> err;
142 std::vector<const Instruction *> instructions;
143 Result() {}
144 explicit Result(const Error &err) : err(err) {}
145 explicit Result(const Instruction *inst) : instructions({inst}) {}
146 explicit Result(const std::vector<const Instruction *> &instructions)
147 : instructions(instructions) {}
148 bool ok() { return !err; }
149};
150
152struct Operand {
153 bool is_ad_or_const;
154 ContainerID container;
155 le_bitrange range;
156 bool operator==(const Operand &b) const {
157 return is_ad_or_const == b.is_ad_or_const && container == b.container && range == b.range;
158 }
159 bool operator!=(const Operand &b) const { return !(*this == b); }
160};
161std::ostream &operator<<(std::ostream &s, const Operand &);
162
165Operand make_container_operand(ContainerID c, le_bitrange r);
166
168struct Assign {
169 Operand dst;
170 Operand src;
171};
172std::ostream &operator<<(std::ostream &s, const Assign &);
173std::ostream &operator<<(std::ostream &s, const std::vector<Assign> &);
174
176using RotateClassifiedAssigns = std::map<int, std::vector<Assign>>;
177
180 protected:
186 bool enable_bitmasked_set_i = true;
187
188 protected:
191 std::optional<Error> validate_input() const;
192
200 const RotateClassifiedAssigns &offset_assigns) const;
201
205 std::optional<Error> check_whole_container_set_with_none_source_allocated() const;
206
207 public:
209 void enable_bitmasked_set(bool enable) { enable_bitmasked_set_i = enable; }
210
212 virtual void add_assign(const Operand &dst, Operand src);
213
223 virtual void add_src_unallocated_assign(const ContainerID &c, const le_bitrange &range);
224
226 virtual void set_container_spec(ContainerID id, int size, bitvec live);
227
229 virtual Result solve() = 0;
230
232 virtual void clear() {
233 dest_assigns_i.clear();
234 specs_i.clear();
235 src_unallocated_bits.clear();
236 enable_bitmasked_set_i = true;
237 }
238};
239
260 private:
261 // check whether @p bv_dest satifies all assignments.
262 std::optional<Error> dest_meet_expectation(const ContainerID dest,
263 const std::vector<Assign> &src1,
264 const std::vector<Assign> &src2,
265 const symbolic_bitvec::BitVec &bv_dest,
266 const symbolic_bitvec::BitVec &bv_src1,
267 const symbolic_bitvec::BitVec &bv_src2) const;
268
271 Result run_deposit_field_symbolic_bitvec_solver(const ContainerID dest,
272 const std::vector<Assign> &src1,
273 const std::vector<Assign> &src2) const;
274
276 Result run_deposit_field_solver(const ContainerID dest, const std::vector<Assign> &src1,
277 const std::vector<Assign> &src2) const;
278
280 Result try_deposit_field(const ContainerID dest, const RotateClassifiedAssigns &assigns) const;
281
284 Result run_byte_rotate_merge_symbolic_bitvec_solver(const ContainerID dest,
285 const std::vector<Assign> &src1,
286 const std::vector<Assign> &src2) const;
287
290 Result run_byte_rotate_merge_solver(const ContainerID dest, const std::vector<Assign> &src1,
291 const std::vector<Assign> &src2) const;
292
295 Result try_byte_rotate_merge(const ContainerID dest,
296 const RotateClassifiedAssigns &assigns) const;
297
303 Result try_bitmasked_set(const ContainerID dest, const RotateClassifiedAssigns &assigns) const;
304
328 const RotateClassifiedAssigns *apply_unallocated_src_optimization(
329 const ContainerID dest, const RotateClassifiedAssigns &offset_assigns);
330
331 public:
332 Result solve() override;
333};
334
342 public:
344 Result solve() override;
345};
346
353 public:
355 Result solve() override;
356};
357
358} // namespace solver
359
360#endif /* BF_P4C_PHV_SOLVER_ACTION_CONSTRAINT_SOLVER_H_ */
Definition bitvec.h:120
Definition cstring.h:85
Definition ordered_map.h:32
Definition action_constraint_solver.h:352
Result solve() override
Definition phv/solver/action_constraint_solver.cpp:736
Definition action_constraint_solver.h:341
Result solve() override
solve checks that either
Definition phv/solver/action_constraint_solver.cpp:715
Definition action_constraint_solver.h:259
Result solve() override
return solve result.
Definition phv/solver/action_constraint_solver.cpp:663
ActionSolverBase contains basic methods and states for all solvers.
Definition action_constraint_solver.h:179
virtual void clear()
clear all states and will reset enable_bitmasked_set to true.
Definition action_constraint_solver.h:232
virtual Result solve()=0
return solve result.
Result try_container_set(const ContainerID dest, const RotateClassifiedAssigns &offset_assigns) const
Definition phv/solver/action_constraint_solver.cpp:186
ordered_map< ContainerID, RotateClassifiedAssigns > dest_assigns_i
destination-clustered assignments.
Definition action_constraint_solver.h:182
void enable_bitmasked_set(bool enable)
set true to enable bitmasked_set set, otherwise disable. default: enable.
Definition action_constraint_solver.h:209
std::optional< Error > check_whole_container_set_with_none_source_allocated() const
Definition phv/solver/action_constraint_solver.cpp:215
std::optional< Error > validate_input() const
Definition phv/solver/action_constraint_solver.cpp:273
ordered_map< ContainerID, bitvec > src_unallocated_bits
bits that will be written in this action but their sources have not been allcoated.
Definition action_constraint_solver.h:185
virtual void add_src_unallocated_assign(const ContainerID &c, const le_bitrange &range)
Definition phv/solver/action_constraint_solver.cpp:251
virtual void add_assign(const Operand &dst, Operand src)
add an assignment from the action.
Definition phv/solver/action_constraint_solver.cpp:235
virtual void set_container_spec(ContainerID id, int size, bitvec live)
set_container_spec will update the container spec.
Definition phv/solver/action_constraint_solver.cpp:263
Definition action_constraint_solver.h:71
Definition action_constraint_solver.h:85
Definition action_constraint_solver.h:103
Definition action_constraint_solver.h:56
Definition action_constraint_solver.h:45
Definition symbolic_bitvec.h:60
Definition cstring.h:80
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
@ Error
Print a warning and continue compilation.
Definition phv/solver/action_constraint_solver.cpp:33
Operand make_ad_or_const_operand()
Constructor helper functions for operand.
Definition phv/solver/action_constraint_solver.cpp:182
std::map< int, std::vector< Assign > > RotateClassifiedAssigns
right-rotate-offset-indexed assignments.
Definition action_constraint_solver.h:176
bitvec live
bits that will have live fieldslices allocated, after action.
Definition action_constraint_solver.h:136
Assign is a set instruction that move operand src bits to dst.
Definition action_constraint_solver.h:168
ContainerSpec container specification.
Definition action_constraint_solver.h:133
Definition common/field_defuse.cpp:590
Error type for all solver.
Definition action_constraint_solver.h:126
Operand represents either a source or a destination of an instruction.
Definition action_constraint_solver.h:152
Result contains either an error or all instructions for an action.
Definition action_constraint_solver.h:140