Skip to content

Commit 2955c1f

Browse files
authored
Merge pull request #1654 from goblint/issue-1591
Use sets for widening thresholds instead of lists
2 parents db4413b + e7088e3 commit 2955c1f

File tree

7 files changed

+46
-40
lines changed

7 files changed

+46
-40
lines changed

src/autoTune.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ let apronOctagonOption factors file =
472472

473473

474474
let wideningOption factors file =
475-
let amountConsts = List.length @@ WideningThresholds.upper_thresholds () in
475+
let amountConsts = WideningThresholds.Thresholds.cardinal @@ ResettableLazy.force WideningThresholds.upper_thresholds in
476476
let cost = amountConsts * (factors.loops * 5 + factors.controlFlowStatements) in
477477
{
478478
value = amountConsts * (factors.loops * 5 + factors.controlFlowStatements);

src/cdomain/value/cdomains/intDomain0.ml

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ let should_wrap ik = not (Cil.isSigned ik) || get_string "sem.int.signed_overflo
6969
* Always false for unsigned types, true for signed types if 'sem.int.signed_overflow' is 'assume_none' *)
7070
let should_ignore_overflow ik = Cil.isSigned ik && get_string "sem.int.signed_overflow" = "assume_none"
7171

72-
let widening_thresholds = ResettableLazy.from_fun WideningThresholds.thresholds
73-
let widening_thresholds_desc = ResettableLazy.from_fun (List.rev % WideningThresholds.thresholds)
74-
7572
type overflow_info = { overflow: bool; underflow: bool;}
7673

7774
let set_overflow_flag ~cast ~underflow ~overflow ik =
@@ -93,8 +90,6 @@ let set_overflow_flag ~cast ~underflow ~overflow ik =
9390
| false, false -> assert false
9491

9592
let reset_lazy () =
96-
ResettableLazy.reset widening_thresholds;
97-
ResettableLazy.reset widening_thresholds_desc;
9893
ana_int_config.interval_threshold_widening <- None;
9994
ana_int_config.interval_narrow_by_meet <- None;
10095
ana_int_config.def_exc_widen_by_join <- None;
@@ -560,26 +555,32 @@ module IntervalArith (Ints_t : IntOps.IntOps) = struct
560555
let to_int (x1, x2) =
561556
if Ints_t.equal x1 x2 then Some x1 else None
562557

558+
let find_thresholds lower_or_upper =
559+
let ts = if get_interval_threshold_widening_constants () = "comparisons" then lower_or_upper else WideningThresholds.thresholds in
560+
ResettableLazy.force ts
561+
563562
let upper_threshold u max_ik =
564-
let ts = if get_interval_threshold_widening_constants () = "comparisons" then WideningThresholds.upper_thresholds () else ResettableLazy.force widening_thresholds in
565563
let u = Ints_t.to_bigint u in
566564
let max_ik' = Ints_t.to_bigint max_ik in
567-
let t = List.find_opt (fun x -> Z.compare u x <= 0 && Z.compare x max_ik' <= 0) ts in
568-
BatOption.map_default Ints_t.of_bigint max_ik t
565+
find_thresholds WideningThresholds.upper_thresholds
566+
|> WideningThresholds.Thresholds.find_first_opt (fun x -> Z.compare u x <= 0)
567+
|> BatOption.filter (fun x -> Z.compare x max_ik' <= 0)
568+
|> BatOption.map_default Ints_t.of_bigint max_ik
569569
let lower_threshold l min_ik =
570-
let ts = if get_interval_threshold_widening_constants () = "comparisons" then WideningThresholds.lower_thresholds () else ResettableLazy.force widening_thresholds_desc in
571570
let l = Ints_t.to_bigint l in
572571
let min_ik' = Ints_t.to_bigint min_ik in
573-
let t = List.find_opt (fun x -> Z.compare l x >= 0 && Z.compare x min_ik' >= 0) ts in
574-
BatOption.map_default Ints_t.of_bigint min_ik t
575-
let is_upper_threshold u =
576-
let ts = if get_interval_threshold_widening_constants () = "comparisons" then WideningThresholds.upper_thresholds () else ResettableLazy.force widening_thresholds in
577-
let u = Ints_t.to_bigint u in
578-
List.exists (Z.equal u) ts
579-
let is_lower_threshold l =
580-
let ts = if get_interval_threshold_widening_constants () = "comparisons" then WideningThresholds.lower_thresholds () else ResettableLazy.force widening_thresholds_desc in
581-
let l = Ints_t.to_bigint l in
582-
List.exists (Z.equal l) ts
572+
find_thresholds WideningThresholds.lower_thresholds
573+
|> WideningThresholds.Thresholds.find_last_opt (fun x -> Z.compare l x >= 0)
574+
|> BatOption.filter (fun x -> Z.compare x min_ik' >= 0)
575+
|> BatOption.map_default Ints_t.of_bigint min_ik
576+
577+
let is_threshold t ts =
578+
let ts = find_thresholds ts in
579+
let t = Ints_t.to_bigint t in
580+
WideningThresholds.Thresholds.mem t ts
581+
582+
let is_upper_threshold u = is_threshold u WideningThresholds.upper_thresholds
583+
let is_lower_threshold l = is_threshold l WideningThresholds.lower_thresholds
583584
end
584585

585586
module IntInvariant =

src/cdomain/value/util/wideningThresholds.ml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,13 @@ let conditional_widening_thresholds = ResettableLazy.from_fun (fun () ->
7272
let octagon = ref default_thresholds in
7373
let thisVisitor = new extractThresholdsFromConditionsVisitor(upper,lower,octagon) in
7474
visitCilFileSameGlobals thisVisitor (!Cilfacade.current_file);
75-
Thresholds.elements !upper, List.rev (Thresholds.elements !lower), Thresholds.elements !octagon )
75+
!upper, !lower, !octagon)
7676

77-
let upper_thresholds () =
78-
let (u,_,_) = ResettableLazy.force conditional_widening_thresholds in u
77+
let upper_thresholds = ResettableLazy.map Tuple3.first conditional_widening_thresholds
7978

80-
let lower_thresholds () =
81-
let (_,l,_) = ResettableLazy.force conditional_widening_thresholds in l
79+
let lower_thresholds = ResettableLazy.map Tuple3.second conditional_widening_thresholds
8280

83-
let octagon_thresholds () =
84-
let (_,_,o) = ResettableLazy.force conditional_widening_thresholds in o
81+
let octagon_thresholds = ResettableLazy.map Tuple3.third conditional_widening_thresholds
8582

8683

8784
class extractConstantsVisitor(widening_thresholds,widening_thresholds_incl_mul2) = object
@@ -105,13 +102,11 @@ let widening_thresholds = ResettableLazy.from_fun (fun () ->
105102
let set_incl_mul2 = ref Thresholds.empty in
106103
let thisVisitor = new extractConstantsVisitor(set,set_incl_mul2) in
107104
visitCilFileSameGlobals thisVisitor (!Cilfacade.current_file);
108-
Thresholds.elements !set, Thresholds.elements !set_incl_mul2)
105+
!set, !set_incl_mul2)
109106

110-
let thresholds () =
111-
fst @@ ResettableLazy.force widening_thresholds
107+
let thresholds = ResettableLazy.map fst widening_thresholds
112108

113-
let thresholds_incl_mul2 () =
114-
snd @@ ResettableLazy.force widening_thresholds
109+
let thresholds_incl_mul2 = ResettableLazy.map snd widening_thresholds
115110

116111
module EH = BatHashtbl.Make (CilType.Exp)
117112

@@ -153,4 +148,9 @@ let exps = ResettableLazy.from_fun (fun () ->
153148
let reset_lazy () =
154149
ResettableLazy.reset widening_thresholds;
155150
ResettableLazy.reset conditional_widening_thresholds;
156-
ResettableLazy.reset exps
151+
ResettableLazy.reset exps;
152+
ResettableLazy.reset thresholds;
153+
ResettableLazy.reset thresholds_incl_mul2;
154+
ResettableLazy.reset upper_thresholds;
155+
ResettableLazy.reset lower_thresholds;
156+
ResettableLazy.reset octagon_thresholds;
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
(** Widening threshold utilities. *)
22

3-
val thresholds : unit -> Z.t list
4-
val thresholds_incl_mul2 : unit -> Z.t list
3+
module Thresholds : Set.S with type elt = Z.t
4+
5+
val thresholds : Thresholds.t ResettableLazy.t
6+
val thresholds_incl_mul2 : Thresholds.t ResettableLazy.t
57
val exps: GoblintCil.exp list ResettableLazy.t
68

79
val reset_lazy : unit -> unit
8-
val upper_thresholds : unit -> Z.t list
9-
val lower_thresholds : unit -> Z.t list
10-
val octagon_thresholds : unit -> Z.t list
10+
val upper_thresholds : Thresholds.t ResettableLazy.t
11+
val lower_thresholds : Thresholds.t ResettableLazy.t
12+
val octagon_thresholds : Thresholds.t ResettableLazy.t

src/cdomains/apron/apronDomain.apron.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ module M = Messages
1818
- heterogeneous environments: https://link.springer.com/chapter/10.1007%2F978-3-030-17184-1_26 (Section 4.1) *)
1919

2020
let widening_thresholds_apron = ResettableLazy.from_fun (fun () ->
21-
let t = if GobConfig.get_string "ana.apron.threshold_widening_constants" = "comparisons" then WideningThresholds.octagon_thresholds () else WideningThresholds.thresholds_incl_mul2 () in
22-
let r = List.map Scalar.of_z t in
21+
let t = if GobConfig.get_string "ana.apron.threshold_widening_constants" = "comparisons" then WideningThresholds.octagon_thresholds else WideningThresholds.thresholds_incl_mul2 in
22+
let r = List.map Scalar.of_z (WideningThresholds.Thresholds.elements (ResettableLazy.force t)) in
2323
Array.of_list r
2424
)
2525

src/common/util/resettableLazy.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ let from_fun f = make_map ~gen:f
77
let force cache = cache.get ()
88

99
let reset cache = cache.del ()
10+
11+
let map f cache = from_fun (fun () -> f (force cache))

src/common/util/resettableLazy.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ type 'a t
55
val from_fun: (unit -> 'a) -> 'a t
66
val force: 'a t -> 'a
77
val reset: 'a t -> unit
8+
val map: ('a -> 'b) -> 'a t -> 'b t

0 commit comments

Comments
 (0)