ilang  1.1.4
ILAng: A Modeling and Verification Platform for SoCs
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros
u_unroller_smt.h
Go to the documentation of this file.
1 
5 #ifndef ILANG_ILA_MNGR_U_UNROLLER_SMT_H__
6 #define ILANG_ILA_MNGR_U_UNROLLER_SMT_H__
7 
8 #include <map>
9 #include <string>
10 #include <vector>
11 
14 #include <ilang/util/log.h>
15 
17 namespace ilang {
18 
20 template <class Generator> class UnrollerSmt {
21 public:
22  // ------------------------- CONSTRUCTOR/DESTRUCTOR ----------------------- //
24  UnrollerSmt(SmtShim<Generator>& smt_shim, const std::string& suffix)
25  : smt_gen_(smt_shim), unroller_suffix_(suffix) {}
26 
27  // ------------------------- METHODS -------------------------------------- //
28  //
29  // Control property/predicate beyond the transition.
30  //
32  inline void AssertGlobal(const ExprPtr& p) { global_pred_.insert(p); }
34  inline void AssertInitial(const ExprPtr& p) { initial_pred_.insert(p); }
36  inline void AssertStep(const ExprPtr& p, const int& k) {
37  step_pred_[k].insert(p);
38  }
40  inline void ClearGlobalAssertion() { global_pred_.clear(); }
42  inline void ClearInitialAssertion() { initial_pred_.clear(); }
44  inline void ClearStepAssertion() { step_pred_.clear(); }
45 
46  //
47  // Access SMT formula in the unrolled execution.
48  //
50  inline auto GetSmtCurrent(const ExprPtr& expr, const size_t& k) const {
51  return smt_gen_.GetShimExpr(expr, SuffixCurrent(k));
52  }
54  inline auto GetSmtNext(const ExprPtr& expr, const size_t& k) const {
55  return smt_gen_.GetShimExpr(expr, SuffixNext(k));
56  }
58  inline auto GetSmtFuncDecl(const FuncPtr& func) const {
59  return smt_gen_.GetShimFunc(func);
60  }
61 
62 private:
63  // ------------------------- MEMBERS -------------------------------------- //
65  SmtShim<Generator>& smt_gen_;
67  const std::string unroller_suffix_;
68 
70  ExprSet global_pred_;
72  ExprSet initial_pred_;
74  std::map<size_t, ExprSet> step_pred_;
75 
76  // ------------------------- HELPERS -------------------------------------- //
78  inline std::string SuffixCurrent(const size_t& t) const {
79  return std::to_string(t) + "_" + unroller_suffix_;
80  }
82  inline std::string SuffixNext(const size_t& t) const {
83  return std::to_string(t) + "_" + unroller_suffix_ + ".nxt";
84  }
85 
86 protected:
87  // ------------------------- TYPES ---------------------------------------- //
88  typedef decltype(smt_gen_.GetShimExpr(nullptr, "")) SmtExpr;
89  typedef decltype(smt_gen_.GetShimFunc(nullptr)) SmtFunc;
90  typedef std::vector<ExprPtr> IlaExprVec;
91  typedef std::vector<SmtExpr> SmtExprVec;
92 
93  // ------------------------- MEMBERS -------------------------------------- //
95  IlaExprVec deciding_vars_;
97  IlaExprVec update_holder_;
99  IlaExprVec assert_holder_;
100 
101  // ------------------------- METHODS -------------------------------------- //
103  virtual void SetDecidingVars() = 0;
104 
107  virtual void MakeOneTransition(const size_t& idx) = 0;
108 
110  SmtExpr Unroll_(const size_t& len, const size_t& begin);
111 
113  SmtExpr UnrollWithStepsUnconnected_(const size_t& len, const size_t& begin);
114 
115 private:
116  // ------------------------- HELPERS -------------------------------------- //
117  inline void InterpIlaExprAndAppend(const IlaExprVec& ila_expr_src,
118  const std::string& suffix,
119  SmtExprVec& smt_expr_dst) {
120  for (const auto& e : ila_expr_src) {
121  smt_expr_dst.push_back(smt_gen_.GetShimExpr(e, suffix));
122  }
123  }
124 
125  inline void InterpIlaExprAndAppend(const ExprSet& ila_expr_src,
126  const std::string& suffix,
127  SmtExprVec& smt_expr_dst) {
128  for (const auto& e : ila_expr_src) {
129  smt_expr_dst.push_back(smt_gen_.GetShimExpr(e, suffix));
130  }
131  }
132 
133  inline void ElementWiseEqualAndAppend(const SmtExprVec& a,
134  const SmtExprVec& b,
135  SmtExprVec& smt_expr_dst) {
136  ILA_ASSERT(a.size() == b.size());
137  for (size_t i = 0; i < a.size(); i++) {
138  smt_expr_dst.push_back(smt_gen_.Equal(a.at(i), b.at(i)));
139  }
140  }
141 
142  inline SmtExpr ConjunctAll(const SmtExprVec& vec) const {
143  auto conjunct_holder = smt_gen_.GetShimExpr(asthub::BoolConst(true));
144  for (const auto& e : vec) {
145  conjunct_holder = smt_gen_.BoolAnd(conjunct_holder, e);
146  }
147  return conjunct_holder;
148  }
149 
150 }; // class UnrollerSmt
151 
153 template <class Generator> class PathUnroller : public UnrollerSmt<Generator> {
154 public:
155  // ------------------------- CONSTRUCTOR/DESTRUCTOR ----------------------- //
157  PathUnroller(SmtShim<Generator>& smt_shim, const std::string& suffix = "")
158  : UnrollerSmt<Generator>(smt_shim, suffix) {}
159 
160  // ------------------------- METHODS -------------------------------------- //
162  auto Unroll(const InstrVec& seq, const size_t& begin = 0) {
163  seq_ = seq;
164  return this->Unroll_(seq.size(), begin);
165  }
166 
168  auto UnrollWithStepsUnconnected(const InstrVec& seq, const size_t& begin) {
169  seq_ = seq;
170  return this->UnrollWithStepsUnconnected_(seq.size(), begin);
171  }
172 
173 protected:
174  // ------------------------- METHODS -------------------------------------- //
176  void SetDecidingVars();
177 
179  void MakeOneTransition(const size_t& idx);
180 
181 private:
182  // ------------------------- MEMBERS -------------------------------------- //
184  InstrVec seq_;
185 
186 }; // class PathUnroller
187 
188 } // namespace ilang
189 
190 #endif // ILANG_ILA_MNGR_U_UNROLLER_SMT_H__
auto GetSmtCurrent(const ExprPtr &expr, const size_t &k) const
Return the SMT formula of expr at the beginning k-th step.
Definition: u_unroller_smt.h:50
#define ILA_ASSERT(b)
Assertion with message logged to FATAL (lvl 3). (Debug)
Definition: log.h:33
auto Unroll(const InstrVec &seq, const size_t &begin=0)
Unroll the sequence.
Definition: u_unroller_smt.h:162
void AssertInitial(const ExprPtr &p)
Add the predicate p to be asserted at the first step (initial condition).
Definition: u_unroller_smt.h:34
Base class for unrolling ILA execution in different settings.
Definition: u_unroller_smt.h:20
Expr::ExprPtr ExprPtr
Pointer type for normal use of Expr.
Definition: expr.h:138
std::vector< InstrPtr > InstrVec
Type for storing a set of Instr.
Definition: instr.h:136
void AssertGlobal(const ExprPtr &p)
Add the predicate p to be asserted globally.
Definition: u_unroller_smt.h:32
SmtExpr Unroll_(const size_t &len, const size_t &begin)
Unroll the whole sequence.
void SetDecidingVars()
Setup the deciding variables.
virtual void MakeOneTransition(const size_t &idx)=0
void AssertStep(const ExprPtr &p, const int &k)
Add the predicate p to be asserted at the k-th step.
Definition: u_unroller_smt.h:36
IlaExprVec update_holder_
State update functions of the deciding variables.
Definition: u_unroller_smt.h:97
void ClearStepAssertion()
Clear all the step-specific assertions.
Definition: u_unroller_smt.h:44
PathUnroller(SmtShim< Generator > &smt_shim, const std::string &suffix="")
Constructor.
Definition: u_unroller_smt.h:157
std::unordered_set< ExprPtr, ExprHash > ExprSet
Type for storing a set of Expr.
Definition: expr.h:153
void ClearInitialAssertion()
Clear all the initial assertions.
Definition: u_unroller_smt.h:42
IlaExprVec assert_holder_
Non-execution semantics (state update) properties.
Definition: u_unroller_smt.h:99
Func::FuncPtr FuncPtr
Pointer type for normal use of Func.
Definition: func.h:83
IlaExprVec deciding_vars_
Variables on which the instruction sequence depends.
Definition: u_unroller_smt.h:95
auto UnrollWithStepsUnconnected(const InstrVec &seq, const size_t &begin)
Unroll the sequence without connecting the steps.
Definition: u_unroller_smt.h:168
SmtExpr UnrollWithStepsUnconnected_(const size_t &len, const size_t &begin)
Unroll the whole sequence without connecting the steps.
Application class for unrolling a sequence of instructions.
Definition: u_unroller_smt.h:153
void MakeOneTransition(const size_t &idx)
Make one transition and setup holders accordingly.
UnrollerSmt(SmtShim< Generator > &smt_shim, const std::string &suffix)
Constructor.
Definition: u_unroller_smt.h:24
auto GetSmtFuncDecl(const FuncPtr &func) const
Return the SMT function declaration of uninterpreted function func.
Definition: u_unroller_smt.h:58
A templated class for wrapping z3 and smt-switch to provide a unified interface for different applica...
Definition: smt_shim.h:15
void ClearGlobalAssertion()
Clear all the global assertions.
Definition: u_unroller_smt.h:40
auto GetSmtNext(const ExprPtr &expr, const size_t &k) const
Return the SMT formula of expr at the end of k-th step.
Definition: u_unroller_smt.h:54
virtual void SetDecidingVars()=0
Setup the deciding variabels.