|
| 1 | +/// \file |
| 2 | +/// Class UnrollerSmt, PathUnroller, and MonoUnroller - a collection of |
| 3 | +/// ILA unrollers with SmtShim support. |
| 4 | + |
| 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 | + |
| 12 | +#include <ilang/ila/instr_lvl_abs.h> |
| 13 | +#include <ilang/target-smt/smt_shim.h> |
| 14 | +#include <ilang/util/log.h> |
| 15 | + |
| 16 | +/// \namespace ilang |
| 17 | +namespace ilang { |
| 18 | + |
| 19 | +/// \brief Base class for unrolling ILA execution in different settings. |
| 20 | +template <class Generator> class UnrollerSmt { |
| 21 | +public: |
| 22 | + // ------------------------- CONSTRUCTOR/DESTRUCTOR ----------------------- // |
| 23 | + /// Constructor. |
| 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 | + // |
| 31 | + /// Add the predicate p to be asserted globally. |
| 32 | + inline void AssertGlobal(const ExprPtr& p) { global_pred_.insert(p); } |
| 33 | + /// Add the predicate p to be asserted at the first step (initial condition). |
| 34 | + inline void AssertInitial(const ExprPtr& p) { initial_pred_.insert(p); } |
| 35 | + /// Add the predicate p to be asserted at the k-th step. |
| 36 | + inline void AssertStep(const ExprPtr& p, const int& k) { |
| 37 | + step_pred_[k].insert(p); |
| 38 | + } |
| 39 | + /// Clear all the global assertions. |
| 40 | + inline void ClearGlobalAssertion() { global_pred_.clear(); } |
| 41 | + /// Clear all the initial assertions. |
| 42 | + inline void ClearInitialAssertion() { initial_pred_.clear(); } |
| 43 | + /// Clear all the step-specific assertions. |
| 44 | + inline void ClearStepAssertion() { step_pred_.clear(); } |
| 45 | + |
| 46 | + // |
| 47 | + // Access SMT formula in the unrolled execution. |
| 48 | + // |
| 49 | + /// Return the SMT formula of expr at the beginning k-th step. |
| 50 | + inline auto GetSmtCurrent(const ExprPtr& expr, const size_t& k) const { |
| 51 | + return smt_gen_.GetShimExpr(expr, SuffixCurrent(k)); |
| 52 | + } |
| 53 | + /// Return the SMT formula of expr at the end of k-th step. |
| 54 | + inline auto GetSmtNext(const ExprPtr& expr, const size_t& k) const { |
| 55 | + return smt_gen_.GetShimExpr(expr, SuffixNext(k)); |
| 56 | + } |
| 57 | + /// Return the SMT function declaration of uninterpreted function func. |
| 58 | + inline auto GetSmtFuncDecl(const FuncPtr& func) const { |
| 59 | + return smt_gen_.GetShimFunc(func); |
| 60 | + } |
| 61 | + |
| 62 | +private: |
| 63 | + // ------------------------- MEMBERS -------------------------------------- // |
| 64 | + /// The underlying (templated) SMT generator. |
| 65 | + SmtShim<Generator>& smt_gen_; |
| 66 | + /// Unroller specific suffix. |
| 67 | + const std::string unroller_suffix_; |
| 68 | + |
| 69 | + /// Predicates to be asserted globally. |
| 70 | + ExprSet global_pred_; |
| 71 | + /// Predicates to be asserted initially. |
| 72 | + ExprSet initial_pred_; |
| 73 | + /// Predicates to be asserted at each step. |
| 74 | + std::map<size_t, ExprSet> step_pred_; |
| 75 | + |
| 76 | + // ------------------------- HELPERS -------------------------------------- // |
| 77 | + /// Return suffix for current state. |
| 78 | + inline std::string SuffixCurrent(const size_t& t) const { |
| 79 | + return std::to_string(t) + "_" + unroller_suffix_; |
| 80 | + } |
| 81 | + /// Return suffix for next state. |
| 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 -------------------------------------- // |
| 94 | + /// Variables on which the instruction sequence depends |
| 95 | + IlaExprVec deciding_vars_; |
| 96 | + /// State update functions of the deciding variables |
| 97 | + IlaExprVec update_holder_; |
| 98 | + /// Non-execution semantics (state update) properties |
| 99 | + IlaExprVec assert_holder_; |
| 100 | + |
| 101 | + // ------------------------- METHODS -------------------------------------- // |
| 102 | + /// Setup the deciding variabels |
| 103 | + virtual void SetDecidingVars() = 0; |
| 104 | + |
| 105 | + /// Make one transition (step) and setup the update_holder_ and assert_holder_ |
| 106 | + /// accordingly |
| 107 | + virtual void MakeOneTransition(const size_t& idx) = 0; |
| 108 | + |
| 109 | + /// Unroll the whole sequence |
| 110 | + SmtExpr Unroll_(const size_t& len, const size_t& begin); |
| 111 | + |
| 112 | + /// Unroll the whole sequence without connecting the steps |
| 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 | + |
| 152 | +/// \brief Application class for unrolling a sequence of instructions. |
| 153 | +template <class Generator> class PathUnroller : public UnrollerSmt<Generator> { |
| 154 | +public: |
| 155 | + // ------------------------- CONSTRUCTOR/DESTRUCTOR ----------------------- // |
| 156 | + /// Constructor. |
| 157 | + PathUnroller(SmtShim<Generator>& smt_shim, const std::string& suffix = "") |
| 158 | + : UnrollerSmt<Generator>(smt_shim, suffix) {} |
| 159 | + |
| 160 | + // ------------------------- METHODS -------------------------------------- // |
| 161 | + /// Unroll the sequence |
| 162 | + auto Unroll(const InstrVec& seq, const size_t& begin = 0) { |
| 163 | + seq_ = seq; |
| 164 | + return this->Unroll_(seq.size(), begin); |
| 165 | + } |
| 166 | + |
| 167 | + /// Unroll the sequence without connecting the steps |
| 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 -------------------------------------- // |
| 175 | + /// Setup the deciding variables |
| 176 | + void SetDecidingVars(); |
| 177 | + |
| 178 | + /// Make one transition and setup holders accordingly |
| 179 | + void MakeOneTransition(const size_t& idx); |
| 180 | + |
| 181 | +private: |
| 182 | + // ------------------------- MEMBERS -------------------------------------- // |
| 183 | + /// The sequence of instructions. |
| 184 | + InstrVec seq_; |
| 185 | + |
| 186 | +}; // class PathUnroller |
| 187 | + |
| 188 | +} // namespace ilang |
| 189 | + |
| 190 | +#endif // ILANG_ILA_MNGR_U_UNROLLER_SMT_H__ |
0 commit comments