-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make passthrough work with state edges for terminating loops
- Loading branch information
Showing
4 changed files
with
90 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
;; Analysis to get the number of iterations of a loop | ||
(ruleset loop-iter-analysis) | ||
|
||
;; inputs, outputs -> number of iterations | ||
;; The minimum possible guess is 1 because of do-while loops | ||
(function LoopNumItersGuess (Expr Expr) i64 :merge (max 1 (min old new))) | ||
|
||
;; Marks loops that we know will terminate | ||
(relation TerminatingLoop (Expr Expr)) | ||
|
||
;; by default, guess that all loops run 1000 times | ||
(rule ((DoWhile inputs outputs)) | ||
((set (LoopNumItersGuess inputs outputs) 1000)) | ||
:ruleset loop-iter-analysis) | ||
|
||
;; For a loop that is false, its num iters is 1 | ||
(rule | ||
((= loop (DoWhile inputs outputs)) | ||
(= (Const (Bool false) ty ctx) (Get outputs 0))) | ||
((set (LoopNumItersGuess inputs outputs) 1) | ||
(TerminatingLoop inputs outputs)) | ||
:ruleset loop-iter-analysis) | ||
|
||
;; Figure out number of iterations for a loop with constant bounds and initial value | ||
;; and i is updated before checking pred | ||
;; TODO: we could make it work for decrementing loops | ||
(rule | ||
((= lhs (DoWhile inputs outputs)) | ||
(= num-inputs (tuple-length inputs)) | ||
(= pred (Get outputs 0)) | ||
;; iteration counter starts at start_const | ||
(= (Const (Int start_const) _ty1 _ctx1) (Get inputs counter_i)) | ||
;; updated counter at counter_i | ||
(= next_counter (Get outputs (+ counter_i 1))) | ||
;; increments by some constant each loop | ||
(= next_counter (Bop (Add) (Get (Arg _ty _ctx) counter_i) | ||
(Const (Int increment) _ty2 _ctx2))) | ||
(> increment 0) | ||
;; while next_counter less than end_constant | ||
(= pred (Bop (LessThan) next_counter | ||
(Const (Int end_constant) _ty3 _ctx3))) | ||
;; end constant is at least start constant | ||
(>= end_constant start_const) | ||
) | ||
( | ||
(set (LoopNumItersGuess inputs outputs) (/ (- end_constant start_const) increment)) | ||
(TerminatingLoop inputs outputs) | ||
) | ||
:ruleset loop-iter-analysis) | ||
|
||
;; Figure out number of iterations for a loop with constant bounds and initial value | ||
;; and i is updated after checking pred | ||
(rule | ||
((= lhs (DoWhile inputs outputs)) | ||
(= num-inputs (tuple-length inputs)) | ||
(= pred (Get outputs 0)) | ||
;; iteration counter starts at start_const | ||
(= (Const (Int start_const) _ty1 _ctx1) (Get inputs counter_i)) | ||
;; updated counter at counter_i | ||
(= next_counter (Get outputs (+ counter_i 1))) | ||
;; increments by a constant each loop | ||
(= next_counter (Bop (Add) (Get (Arg _ty _ctx) counter_i) | ||
(Const (Int increment) _ty2 _ctx2))) | ||
(> increment 0) | ||
;; while this counter less than end_constant | ||
(= pred (Bop (LessThan) (Get (Arg _ty _ctx) counter_i) | ||
(Const (Int end_constant) _ty3 _ctx3))) | ||
;; end constant is at least start constant | ||
(>= end_constant start_const) | ||
) | ||
( | ||
(set (LoopNumItersGuess inputs outputs) (+ (/ (- end_constant start_const) increment) 1)) | ||
(TerminatingLoop inputs outputs) | ||
) | ||
:ruleset loop-iter-analysis) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters