1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
|
; S-Expressions for 4-Valued Logic
; Copyright (C) 2010-2012 Centaur Technology
;
; Contact:
; Centaur Technology Formal Verification Group
; 7600-C N. Capital of Texas Highway, Suite 300, Austin, TX 78731, USA.
; http://www.centtech.com/
;
; License: (An MIT/X11-style license)
;
; Permission is hereby granted, free of charge, to any person obtaining a
; copy of this software and associated documentation files (the "Software"),
; to deal in the Software without restriction, including without limitation
; the rights to use, copy, modify, merge, publish, distribute, sublicense,
; and/or sell copies of the Software, and to permit persons to whom the
; Software is furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in
; all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
; DEALINGS IN THE SOFTWARE.
;
; sexpr-loop-debug.lisp
; Original author: Jared Davis <jared@centtech.com>
; Code for debugging apparent combinational loops for sexpr-fixpoint.
(in-package "ACL2")
(include-book "sexpr-fixpoint")
(include-book "centaur/esim/vltoe/emodwire" :dir :system)
(include-book "centaur/vl2014/util/cw-unformatted" :dir :system)
(local (include-book "centaur/vl2014/util/arithmetic" :dir :system))
(local (include-book "centaur/vl2014/util/osets" :dir :system))
; This file is a plug-in debugging mechanism for loops in the sexpr-fixpoint
; algorithm. We exploit sneaky-save and sneaky-load to make it possible to
; debug loops without complicating the logical story of sexpr-fixpoint.
;
; Roughly, when SEXPR-DFS optimizes the list of s-expressions and tries to
; topologically sort them, it saves the loops it has found in
;
; :SEXPR-LOOP-DEBUG-LOOPS
;
; But practically speaking these loops are hard to read because they are just
; lists of numbers. That is, SEXPR-DFS works on "translated" sexprs, where the
; variables have been replaced by numbers so that we can use faster variable
; gathering routines. So the loops are in terms of numbers that have nothing
; to do with the input variables.
;
; To make sense of these loops, we also save the INV-VARMAP (an alist mapping
; the numbers back to the original signal names) in
;
; :SEXPR-LOOP-DEBUG-INV-VARMAP
;
; This varmap lets us translate the loops we have found back in terms of the
; original sexpr vars.
(defun translate-loops (loops inv-varmap)
;; Use the INV-VARMAP to turn the loops from lists of numbers into lists
;; of proper sexpr variables.
(declare (xargs :guard t))
(if (atom loops)
nil
(cons (fal-extract-vals (car loops) inv-varmap)
(translate-loops (cdr loops) inv-varmap))))
(defun find-first-relevant-loop (sig-num loops)
;; Just find the first loop that mentions this signal.
;; - SIG-NUM is a number (i.e., not a proper sexpr var)
;; - LOOPS are the untranslated loops
(declare (xargs :guard t))
(cond ((atom loops)
nil)
((gentle-member-equal sig-num (car loops))
(car loops))
(t
(find-first-relevant-loop sig-num (cdr loops)))))
(defun find-all-relevant-loops (sig-num loops)
;; Find all the loops that mention this signal.
;; - SIG-NUM is a number (i.e., not a proper sexpr var)
;; - LOOPS are the untranslated loops
(declare (xargs :guard t))
(cond ((atom loops)
nil)
((gentle-member-equal sig-num (car loops))
(cons (car loops) (find-all-relevant-loops sig-num (cdr loops))))
(t
(find-all-relevant-loops sig-num (cdr loops)))))
(defsection len-sort
;; Sort lists so that the shortest ones come first.
(local (include-book "arithmetic-3/top" :dir :system))
(defun shorter-p (x y)
(declare (xargs :guard t))
(mbe :logic (< (len x) (len y))
:exec
(cond ((atom x)
(consp y))
((atom y)
nil)
(t
(shorter-p (cdr x) (cdr y))))))
(defthm shorter-p-transitive
(implies (and (shorter-p x y)
(shorter-p y z))
(shorter-p x z)))
(defsort :compare< shorter-p
:prefix len))
; Figuring out the Verilog signals that give rise to a loop.
;
; The following code basically assumes that the sexpr vars we're trying to
; reach a fixpoint for are VL emodwires. This is probably a reasonable
; assumption if we're using ESIM to simulate modules produced by VL.
;
; What are we trying to do here? Well, if ESIM is getting bogged down in
; apparent combinational loops, probably you need to install a custom function
; into ESIM-SIMPLIFY-UPDATE-FNS that will shannon-expand the clocks signals
; that are controlling certain latches so that the loop will go away. The
; biggest problem is figuring out why there seems to be a loop and what we need
; to expand to break it.
;
; The loops that we're going to find in the course of our topological sort are
; all going to be in terms of ESIM wires, but they can have just gobs of signals
; because typically you have a loop like:
;
; main inputs +---- feedback
; | | |
; \---------------/ |
; \ mux / |
; \-----------/ |
; | |
; +-------+ |
; clk --+---| LATCH | |
; | +-------+ |
; | | |
; | Do Stuff |
; | | |
; | +-------+ |
; +--o| LATCH | |
; +-------+ |
; | |
; Result ------
; |
;
; And if the busses involved are wide then you can end up with basically all of
; the bits of each wire involved taking part in the loop. This is too much
; stuff to look at.
;
; So what I want to do is basically collapse the loops down and just talk about
; Verilog wires instead of bits. Beyond that, I want to make the output to be
; easy to plug into the module browser's wireview tool so you can just go and
; visualize the loop without having to do anything but copy and paste it into
; wireview.
#!STR
(defsection strjoin
;; BOZO move to string library
(defun strjoin-aux (separator x acc)
(declare (xargs :guard (and (stringp separator)
(string-listp x))))
(cond ((atom x)
acc)
((atom (cdr x))
(str::revappend-chars (car x) acc))
(t
(let* ((acc (str::revappend-chars (car x) acc))
(acc (str::revappend-chars separator acc)))
(strjoin-aux separator (cdr x) acc)))))
(defthm character-listp-of-strjoin-aux
(implies (character-listp acc)
(character-listp (strjoin-aux separator x acc)))
:hints(("Goal" :in-theory (enable strjoin-aux))))
(defun strjoin (separator x)
(declare (xargs :guard (and (stringp separator)
(string-listp x))))
(reverse (coerce (strjoin-aux separator x nil) 'string))))
(defund verilog-loop-from-translated-loop (x)
(declare (xargs :guard t))
(if (vl2014::vl-emodwirelist-p x)
(set::mergesort (vl2014::vl-emodwirelist->basenames x))
(cw "Oops, loop contains mal-formed signals, not trying to convert
it into Verilog. ~x0.~%" x)))
(defthm string-listp-of-verilog-loop-from-translated-loop
(string-listp (verilog-loop-from-translated-loop x))
:hints(("Goal" :in-theory (enable verilog-loop-from-translated-loop))))
(defun verilog-loops-from-translated-loops-aux (x)
(declare (xargs :guard t))
(if (atom x)
nil
(cons (verilog-loop-from-translated-loop (car x))
(verilog-loops-from-translated-loops-aux (cdr x)))))
(defthm string-list-listp-of-verilog-loops-from-translated-loops-aux
(vl2014::string-list-listp (verilog-loops-from-translated-loops-aux x))
:hints(("Goal" :in-theory (enable verilog-loops-from-translated-loops-aux))))
(defun verilog-loops-from-translated-loops (x)
(declare (xargs :guard t))
(set::mergesort (remove-equal nil (verilog-loops-from-translated-loops-aux x))))
(defthm string-list-listp-of-verilog-loops-from-translated-loops
(vl2014::string-list-listp (verilog-loops-from-translated-loops x))
:hints(("Goal" :in-theory (enable verilog-loops-from-translated-loops))))
(defund comma-strings-from-verilog-loops (x)
(declare (xargs :guard (vl2014::string-list-listp x)))
(if (atom x)
nil
(cons (str::strjoin "," (car x))
(comma-strings-from-verilog-loops (cdr x)))))
(defthm string-listp-of-comma-strings-from-verilog-loops
(implies (vl2014::string-list-listp x)
(string-listp (comma-strings-from-verilog-loops x)))
:hints(("Goal" :in-theory (enable comma-strings-from-verilog-loops))))
(defun verilog-summarize-translated-loops-aux (n comma-strs)
(declare (xargs :guard (and (natp n)
(string-listp comma-strs))))
(if (atom comma-strs)
nil
(progn$
(cw "Loop ~x0: " n)
(vl2014::cw-unformatted (car comma-strs)) ;; so it all prints on one line
(cw "~%")
(verilog-summarize-translated-loops-aux (+ 1 n) (cdr comma-strs)))))
(defthm
vl2014::string-list-listp-of-len-sort
(iff (vl2014::string-list-listp (len-sort x))
(vl2014::string-list-listp x))
:hints
(("goal" :in-theory (disable vl2014::string-list-listp-when-subsetp-equal)
:use ((:instance vl2014::string-list-listp-when-subsetp-equal
(y (len-sort x)))
(:instance vl2014::string-list-listp-when-subsetp-equal
(x (len-sort x))
(y x))))))
(defund verilog-summarize-translated-loops (x)
(declare (xargs :guard t))
(b* ((loops (verilog-loops-from-translated-loops x))
(loops (len-sort loops))
((unless (vl2014::string-list-listp loops))
(er hard? 'verilog-summarize-translated-loops
"Jared thinks this is impossible."))
(strs (comma-strings-from-verilog-loops loops)))
(cw "Verilog-level Summary of Possible Loops:~%~%")
(verilog-summarize-translated-loops-aux 1 strs)
(cw "~%~%")))
(defconst *max-sneaky-debug-loops* 5)
(defun jared-sneaky-loop-debug-mutator (stored-vals ignored)
(declare (ignorable ignored)
(xargs :guard t))
; See sneaky-mutate. This is a mutator. We expect that stored-vals should
; be a list of the form (loops inv-varmap).
(b* (((unless (tuplep 2 stored-vals))
(er hard? 'sneaky-loop-debug-mutator
"Expected stored-vals to have the form (loops inv-varmap), found ~x0."
stored-vals))
((list loops inv-varmap) stored-vals)
((unless loops)
;; Common case: there are no loops in this module, so don't do anything.
nil)
(loops (ec-call (len-sort loops)))
;; The answer we return from the sneaky-mutator is an alist that says
;; how to update the sneaky store. We'll go ahead and change the stored
;; loops to their sorted equivalent, so that when we report particular
;; loops we'll say the shortest example.
(ret (list (cons :sexpr-loop-debug-loops loops)))
(loops (if (< *max-sneaky-debug-loops* (len loops))
(prog2$
(cw "; jared-loop-debug: printing ~x0 of ~x1 \"raw\" ~
loops. To increase the number of loops printed, ~
redefine ~s2."
*max-sneaky-debug-loops* (len loops)
'*max-sneaky-debug-loops*)
(take *max-sneaky-debug-loops* loops))
loops))
;; Loops are numeric, so change them to variables.
(loops (with-fast-alist inv-varmap (translate-loops loops inv-varmap)))
(- (cw "; loop-debug: ~x0~%~%" loops)))
;; The answer is an ALIST that says how to update the sneaky store.
;; Returning NIL as a sneaky mutator means, "don't change the sneaky store."
ret))
(defun jared-sneaky-loop-debugger ()
(declare (xargs :guard t))
(sneaky-mutate 'jared-sneaky-loop-debug-mutator
;; List of keys to get from the sneaky store
(list :sexpr-loop-debug-loops
:sexpr-loop-debug-inv-varmap)
;; User argument (none)
nil))
(defattach sneaky-loop-debugger jared-sneaky-loop-debugger)
(defun jared-sneaky-loop-say-how-bad-mutator (stored-vals user-arg)
(declare (xargs :guard t))
(b* (((unless (tuplep 2 stored-vals))
(er hard? 'sneaky-loop-say-how-bad-mutator
"Expected stored-vals to have the form (loops inv-varmap), found ~x0."
stored-vals))
((unless (consp user-arg))
(er hard? 'sneaky-loop-say-how-bad-mutator
"Expected user-arg to have the form (sig-number . ndeps), found ~x0."
user-arg))
((list loops inv-varmap) stored-vals)
((cons sig-number ndeps) user-arg)
(sig-name (cdr (hons-get sig-number inv-varmap)))
(- (cw "~x0 is a dependency of ~x1 previous signals.~%" sig-name ndeps))
(loops (find-all-relevant-loops sig-number loops))
((unless loops)
(cw "No loop info available for ~x0.~%" sig-name))
(loops (with-fast-alist inv-varmap
(translate-loops loops inv-varmap)))
(- (verilog-summarize-translated-loops loops)))
nil))
(defun jared-sneaky-loop-say-how-bad (sig-number ndeps)
(declare (xargs :guard t))
(sneaky-mutate 'jared-sneaky-loop-say-how-bad-mutator
(list :sexpr-loop-debug-loops
:sexpr-loop-debug-inv-varmap)
;; User argument
(cons sig-number ndeps)))
(defattach sneaky-loop-say-how-bad jared-sneaky-loop-say-how-bad)
|