Source file autoTune.ml
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
(** Autotuning of the configuration based on syntactic heuristics. *)
open GobConfig
open GoblintCil
open AutoTune0
module FunctionSet = Set.Make(CilType.Varinfo)
module FunctionCallMap = Map.Make(CilType.Varinfo)
let addOrCreateMap fd = function
| Some (set, i) -> Some (FunctionSet.add fd set, i+1)
| None -> Some (FunctionSet.singleton fd, 1)
class collectFunctionCallsVisitor(callSet, calledBy, argLists, fd) = object
inherit nopCilVisitor
method! vinst = function
| Call (_,Lval ((Var info), NoOffset),args,_,_)->
callSet := FunctionSet.add info !callSet;
calledBy := FunctionCallMap.update info (addOrCreateMap fd) !calledBy;
argLists := FunctionCallMap.add info args !argLists;
DoChildren
| _ -> DoChildren
end
class functionVisitor(calling, calledBy, argLists, dynamicallyCalled) = object
inherit nopCilVisitor
method! vglob = function
| GVarDecl (vinfo,_) ->
if vinfo.vaddrof && isFunctionType vinfo.vtype then dynamicallyCalled := FunctionSet.add vinfo !dynamicallyCalled;
DoChildren
| _ -> DoChildren
method! vfunc fd =
let callSet = ref FunctionSet.empty in
let callVisitor = new collectFunctionCallsVisitor (callSet, calledBy, argLists, fd.svar) in
ignore @@ Cil.visitCilFunction callVisitor fd;
calling := FunctionCallMap.add fd.svar !callSet !calling;
DoChildren
end
exception Found
class findAllocsInLoops = object
inherit nopCilVisitor
val mutable inloop = false
method! vstmt stmt =
let outOfLoop stmt =
match stmt.skind with
| Loop _ -> inloop <- false; stmt
| _ -> stmt
in
match stmt.skind with
| Loop _ -> inloop <- true; ChangeDoChildrenPost(stmt, outOfLoop)
| _ -> DoChildren
method! vinst = function
| Call (_, Lval (Var f, NoOffset), args,_,_) when LibraryFunctions.is_special f ->
Goblint_backtrace.protect ~mark:(fun () -> Cilfacade.FunVarinfo f) ~finally:Fun.id @@ fun () ->
let desc = LibraryFunctions.find f in
begin match desc.special args with
| Malloc _
| Alloca _ when inloop -> raise Found
| _ -> DoChildren
end
| _ -> DoChildren
end
type functionCallMaps = {
calling: FunctionSet.t FunctionCallMap.t;
calledBy: (FunctionSet.t * int) FunctionCallMap.t;
argLists: Cil.exp list FunctionCallMap.t;
dynamicallyCalled: FunctionSet.t;
}
let functionCallMaps = ResettableLazy.from_fun (fun () ->
let calling = ref FunctionCallMap.empty in
let calledBy = ref FunctionCallMap.empty in
let argLists = ref FunctionCallMap.empty in
let dynamicallyCalled = ref FunctionSet.empty in
let thisVisitor = new functionVisitor(calling,calledBy, argLists, dynamicallyCalled) in
visitCilFileSameGlobals thisVisitor (!Cilfacade.current_file);
{calling = !calling; calledBy = !calledBy; argLists = !argLists; dynamicallyCalled= !dynamicallyCalled})
let calledFunctions fd = (ResettableLazy.force functionCallMaps).calling |> FunctionCallMap.find_opt fd |> Option.value ~default:FunctionSet.empty
let callingFunctions fd = (ResettableLazy.force functionCallMaps).calledBy |> FunctionCallMap.find_opt fd |> Option.value ~default:(FunctionSet.empty, 0) |> fst
let timesCalled fd = (ResettableLazy.force functionCallMaps).calledBy |> FunctionCallMap.find_opt fd |> Option.value ~default:(FunctionSet.empty, 0) |> snd
let functionArgs fd = (ResettableLazy.force functionCallMaps).argLists |> FunctionCallMap.find_opt fd
let findMallocWrappers () =
let isMalloc f =
Goblint_backtrace.wrap_val ~mark:(Cilfacade.FunVarinfo f) @@ fun () ->
if LibraryFunctions.is_special f then (
let desc = LibraryFunctions.find f in
match functionArgs f with
| None -> false
| Some args ->
match desc.special args with
| Malloc _ -> true
| _ -> false
)
else
false
in
(ResettableLazy.force functionCallMaps).calling
|> FunctionCallMap.filter (fun _ allCalled -> FunctionSet.exists isMalloc allCalled)
|> FunctionCallMap.filter (fun f _ -> timesCalled f > 10)
|> FunctionCallMap.bindings
|> List.map (fun (v,_) -> v.vname)
|> List.iter (fun n -> Logs.info "malloc wrapper: %s" n; GobConfig.set_auto "ana.malloc.wrappers[+]" n)
let isExtern = function
| Extern -> true
| _ -> false
let rec setCongruenceRecursive fd depth neigbourFunction =
if depth >= 0 then (
fd.svar.vattr <- addAttributes (fd.svar.vattr) [Attr ("goblint_precision",[AStr "congruence"])];
FunctionSet.iter
(fun vinfo ->
Logs.info " %s" vinfo.vname;
match Cilfacade.find_varinfo_fundec vinfo with
| fd -> setCongruenceRecursive fd (depth -1) neigbourFunction
| exception Not_found -> ()
)
(FunctionSet.filter
(fun x -> not (isExtern x.vstorage || String.starts_with x.vname ~prefix:"__builtin"))
(neigbourFunction fd.svar)
)
;
)
exception ModFound
class modVisitor = object
inherit nopCilVisitor
method! vexpr = function
| BinOp (Mod,_,_,_) ->
raise ModFound;
| _ -> DoChildren
end
class modFunctionAnnotatorVisitor = object
inherit nopCilVisitor
method! vfunc fd =
let thisVisitor = new modVisitor in
try ignore (visitCilFunction thisVisitor fd) with
| ModFound ->
Logs.info "function %a uses mod, enable congruence domain recursively for:" CilType.Fundec.pretty fd;
Logs.info " \"down\":";
setCongruenceRecursive fd 6 calledFunctions;
Logs.info " \"up\":";
setCongruenceRecursive fd 3 callingFunctions;
;
SkipChildren
end
let addModAttributes file =
set_bool "annotation.int.enabled" true;
let thisVisitor = new modFunctionAnnotatorVisitor in
ignore (visitCilFileSameGlobals thisVisitor file)
let disableIntervalContextsInRecursiveFunctions () =
(ResettableLazy.force functionCallMaps).calling |> FunctionCallMap.iter (fun f set ->
if FunctionSet.mem f set || (not @@ FunctionSet.disjoint (calledFunctions f) (callingFunctions f)) then (
Logs.info "function %s is recursive, disable interval and interval_set contexts" f.vname;
f.vattr <- addAttributes (f.vattr) [Attr ("goblint_context",[AStr "base.no-interval"; AStr "base.no-interval_set"; AStr "relation.no-context"])];
)
)
let hasFunction pred =
let relevant_static var =
Goblint_backtrace.wrap_val ~mark:(Cilfacade.FunVarinfo var) @@ fun () ->
if LibraryFunctions.is_special var then
let desc = LibraryFunctions.find var in
GobOption.exists (fun args -> pred desc args) (functionArgs var)
else
false
in
let relevant_dynamic var =
Goblint_backtrace.wrap_val ~mark:(Cilfacade.FunVarinfo var) @@ fun () ->
if LibraryFunctions.is_special var then
let desc = LibraryFunctions.find var in
match unrollType var.vtype with
| TFun (_, args, _, _) ->
let args = BatOption.map_default (List.map (fun (x,_,_) -> MyCFG.unknown_exp)) [] args in
pred desc args
| _ -> false
else
false
in
let calls = ResettableLazy.force functionCallMaps in
calls.calledBy |> FunctionCallMap.exists (fun var _ -> relevant_static var) ||
calls.dynamicallyCalled |> FunctionSet.exists relevant_dynamic
let disableAnalyses anas =
List.iter (GobConfig.set_auto "ana.activated[-]") anas
let enableAnalyses anas =
List.iter (GobConfig.set_auto "ana.activated[+]") anas
let notNeccessaryThreadAnalyses = ["race"; "deadlock"; "maylocks"; "symb_locks"; "thread"; "threadid"; "threadJoins"; "threadreturn"; "mhp"; "region"; "pthreadMutexType"]
let reduceThreadAnalyses () =
let isThreadCreate (desc: LibraryDesc.t) args =
match desc.special args with
| LibraryDesc.ThreadCreate _ -> true
| _ -> LibraryDesc.Accesses.find_kind desc.accs Spawn args <> []
in
let hasThreadCreate = hasFunction isThreadCreate in
if not @@ hasThreadCreate then (
Logs.info "no thread creation -> disabling thread analyses \"%s\"" (String.concat ", " notNeccessaryThreadAnalyses);
disableAnalyses notNeccessaryThreadAnalyses;
)
let focusOnMemSafetySpecification (spec: Svcomp.Specification.t) =
match spec with
| ValidMemtrack
| ValidMemcleanup ->
if (get_int "ana.malloc.unique_address_count") < 1 then (
Logs.info "Setting \"ana.malloc.unique_address_count\" to 5";
set_int "ana.malloc.unique_address_count" 5;
);
| _ -> ()
let focusOnMemSafetySpecification () =
List.iter focusOnMemSafetySpecification (Svcomp.Specification.of_option ())
let focusOnTermination (spec: Svcomp.Specification.t) =
match spec with
| Termination ->
let terminationAnas = ["threadflag"; "apron"] in
Logs.info "Specification: Termination -> enabling termination analyses \"%s\"" (String.concat ", " terminationAnas);
enableAnalyses terminationAnas;
set_string "sem.int.signed_overflow" "assume_none";
set_bool "ana.int.interval" true;
set_string "ana.apron.domain" "polyhedra";
()
| _ -> ()
let focusOnTermination () =
List.iter focusOnTermination (Svcomp.Specification.of_option ())
let concurrencySafety (spec: Svcomp.Specification.t) =
match spec with
| NoDataRace ->
Logs.info "Specification: NoDataRace -> enabling thread analyses \"%s\"" (String.concat ", " notNeccessaryThreadAnalyses);
enableAnalyses notNeccessaryThreadAnalyses;
| _ -> ()
let noOverflows (spec: Svcomp.Specification.t) =
match spec with
| NoOverflow ->
set_bool "ana.int.def_exc" true;
begin
try
ignore @@ visitCilFileSameGlobals (new findAllocsInLoops) (!Cilfacade.current_file);
set_int "ana.malloc.unique_address_count" 1
with Found -> set_int "ana.malloc.unique_address_count" 0;
end
| _ -> ()
let focusOn (f : SvcompSpec.t -> unit) =
List.iter f (Svcomp.Specification.of_option ())
exception EnumFound
class enumVisitor = object
inherit nopCilVisitor
method! vglob = function
| GEnumTag _
| GEnumTagDecl _ ->
raise EnumFound;
| _ -> SkipChildren;
end
let hasEnums file =
let thisVisitor = new enumVisitor in
try
ignore (visitCilFileSameGlobals thisVisitor file);
false;
with EnumFound -> true
class addTypeAttributeVisitor = object
inherit nopCilVisitor
method! vvdec info =
(if is_large_array info.vtype && not @@ hasAttribute "goblint_array_domain" (typeAttrs info.vtype) then
info.vattr <- addAttribute (Attr ("goblint_array_domain", [AStr "partitioned"])) info.vattr);
DoChildren
method! vtype typ =
let is_important_type (t: typ): bool = match t with
| TNamed (info, attr) -> List.mem info.tname ["pthread_mutex_t"; "spinlock_t"; "pthread_t"]
| TInt (IInt, attr) -> hasAttribute "mutex" attr
| _ -> false
in
if is_important_type typ && not @@ hasAttribute "goblint_array_domain" (typeAttrs typ) then
ChangeTo (typeAddAttributes [Attr ("goblint_array_domain", [AStr "unroll"])] typ)
else SkipChildren
end
let selectArrayDomains file =
set_bool "annotation.goblint_array_domain" true;
let thisVisitor = new addTypeAttributeVisitor in
ignore (visitCilFileSameGlobals thisVisitor file)
type option = {
value:int;
cost:int;
activate: unit -> unit
}
module VariableMap = Map.Make(CilType.Varinfo)
module VariableSet = Set.Make(CilType.Varinfo)
let isComparison = function
| Lt | Gt | Le | Ge | Ne | Eq -> true
| _ -> false
let isGoblintStub v = List.exists (fun (Attr(s,_)) -> s = "goblint_stub") v.vattr
let rec = function
| UnOp (Neg, e, _)
| CastE (_, e) -> extractVar e
| Lval ((Var info),_) when not (isGoblintStub info) -> Some info
| _ -> None
let e1 e2 =
match extractVar e1, extractVar e2 with
| Some a, Some b -> [a; b]
| Some a, None when isConstant e2 -> [a]
| None, Some b when isConstant e1 -> [b]
| _, _ -> []
let = function
| BinOp (PlusA, e1,e2, (TInt _))
| BinOp (MinusA, e1,e2, (TInt _)) -> extractBinOpVars e1 e2
| e -> Option.to_list (extractVar e)
let addOrCreateVarMapping varMap key v globals = if key.vglob = globals then varMap :=
if VariableMap.mem key !varMap then
let old = VariableMap.find key !varMap in
VariableMap.add key (old + v) !varMap
else
VariableMap.add key v !varMap
class octagonVariableVisitor(varMap, globals) = object
inherit nopCilVisitor
method! vexpr = function
| BinOp (op, e1,e2, (TInt _)) when isComparison op -> (
List.iter (fun var -> addOrCreateVarMapping varMap var 5 globals) (extractOctagonVars e1);
List.iter (fun var -> addOrCreateVarMapping varMap var 5 globals) (extractOctagonVars e2);
DoChildren
)
| Lval ((Var info),_) when not (isGoblintStub info) -> addOrCreateVarMapping varMap info 1 globals; SkipChildren
| UnOp (LNot, _,_)
| UnOp (Neg, _,_)
| BinOp (PlusA,_,_,_)
| BinOp (MinusA,_,_,_)
| BinOp (Mult,_,_,_)
| BinOp (LAnd,_,_,_)
| BinOp (LOr,_,_,_) -> DoChildren
| _ -> SkipChildren
end
let topVars n varMap=
let compareValueDesc = (fun (_,v1) (_,v2) -> - (compare v1 v2)) in
varMap
|> VariableMap.bindings
|> List.sort compareValueDesc
|> BatList.take n
|> List.map fst
class octagonFunctionVisitor(list, amount) = object
inherit nopCilVisitor
method! vfunc f =
let varMap = ref VariableMap.empty in
let visitor = new octagonVariableVisitor(varMap, false) in
ignore (visitCilFunction visitor f);
list := topVars amount !varMap ::!list;
SkipChildren
end
let congruenceOption factors file =
let locals, globals = factors.integralVars in
let cost = (locals + globals) * (factors.instructions / 12) + 5 * factors.functionCalls in
let value = 5 * locals + globals in
let activate () =
Logs.debug "Congruence: %d" cost;
set_bool "ana.int.congruence" true;
Logs.info "Enabled congruence domain.";
in
{
value;
cost;
activate;
}
let apronOctagonOption factors file =
let locals =
if List.mem "specification" (get_string_list "ana.autotune.activated" ) && get_string "ana.specification" <> "" then
if List.mem Svcomp.Specification.NoOverflow (Svcomp.Specification.of_option ()) then
12
else
8
else 8
in let globals = 2 in
let selectedLocals =
let list = ref [] in
let visitor = new octagonFunctionVisitor(list, locals) in
visitCilFileSameGlobals visitor file;
List.concat !list
in
let selectedGlobals =
let varMap = ref VariableMap.empty in
let visitor = new octagonVariableVisitor(varMap, true) in
visitCilFileSameGlobals visitor file;
topVars globals !varMap
in
let allVars = (selectedGlobals @ selectedLocals) in
let cost = (Batteries.Int.pow (locals + globals) 3) * (factors.instructions / 70) in
let activateVars () =
Logs.debug "Octagon: %d" cost;
set_bool "annotation.goblint_relation_track" true;
set_string "ana.apron.domain" "octagon";
set_auto "ana.activated[+]" "apron";
set_bool "ana.apron.threshold_widening" true;
set_string "ana.apron.threshold_widening_constants" "comparisons";
Logs.info "Enabled octagon domain ONLY for:";
Logs.info "%s" @@ String.concat ", " @@ List.map (fun info -> info.vname) allVars;
List.iter (fun info -> info.vattr <- addAttribute (Attr("goblint_relation_track",[])) info.vattr) allVars
in
{
value = 50 * (List.length allVars) ;
cost = cost;
activate = activateVars;
}
let wideningOption factors file =
let amountConsts = List.length @@ WideningThresholds.upper_thresholds () in
let cost = amountConsts * (factors.loops * 5 + factors.controlFlowStatements) in
{
value = amountConsts * (factors.loops * 5 + factors.controlFlowStatements);
cost = cost;
activate = fun () ->
Logs.debug "Widening: %d" cost;
set_bool "ana.int.interval_threshold_widening" true;
set_string "ana.int.interval_threshold_widening_constants" "comparisons";
Logs.info "Enabled widening thresholds";
}
let activateTmpSpecialAnalysis () =
let isMathFun (desc: LibraryDesc.t) args =
match desc.special args with
| LibraryDesc.Math _ -> true
| _ -> false
in
let hasMathFunctions = hasFunction isMathFun in
if hasMathFunctions then (
Logs.info "math function -> enabling tmpSpecial analysis and floating-point domain";
enableAnalyses ["tmpSpecial"];
set_bool "ana.float.interval" true;
)
let estimateComplexity factors file =
let pathsEstimate = factors.loops + factors.controlFlowStatements / 90 in
let operationEstimate = factors.instructions + (factors.expressions / 60) in
let callsEstimate = factors.functionCalls * factors.loops / factors.functions / 10 in
let globalVars = fst factors.pointerVars * 2 + fst factors.arrayVars * 4 + fst factors.integralVars in
let localVars = snd factors.pointerVars * 2 + snd factors.arrayVars * 4 + snd factors.integralVars in
let varEstimates = globalVars + localVars / factors.functions in
pathsEstimate * operationEstimate * callsEstimate + varEstimates / 10
let totalTarget = 30000
let chooseFromOptions costTarget options =
let ratio o = Float.of_int o.value /. Float.of_int o.cost in
let compareRatio o1 o2 = Float.compare (ratio o1) (ratio o2) in
let rec takeFitting remainingTarget options =
if remainingTarget < 0 then (Logs.debug "Total: %d" (totalTarget - remainingTarget); [] ) else match options with
| o::os ->
if o.cost < remainingTarget + costTarget / 20 then
o::takeFitting (remainingTarget - o.cost) os
else
takeFitting (remainingTarget - o.cost) os
| [] -> Logs.debug "Total: %d" (totalTarget - remainingTarget); []
in
takeFitting costTarget @@ List.sort compareRatio options
let isActivated a = get_bool "ana.autotune.enabled" && List.mem a @@ get_string_list "ana.autotune.activated"
let isTerminationTask () = List.mem Svcomp.Specification.Termination (Svcomp.Specification.of_option ())
let chooseConfig file =
let factors = collectFactors visitCilFileSameGlobals file in
let fileCompplexity = estimateComplexity factors file in
Logs.debug "Collected factors:";
printFactors factors;
Logs.debug "";
Logs.debug "Complexity estimates:";
Logs.debug "File: %d" fileCompplexity;
if fileCompplexity < totalTarget && isActivated "congruence" then
addModAttributes file;
if isActivated "noRecursiveIntervals" then
disableIntervalContextsInRecursiveFunctions ();
if isActivated "mallocWrappers" then
findMallocWrappers ();
if isActivated "concurrencySafetySpecification" then focusOn concurrencySafety;
if isActivated "noOverflows" then focusOn noOverflows;
if isActivated "enums" && hasEnums file then
set_bool "ana.int.enums" true;
if isActivated "singleThreaded" then
reduceThreadAnalyses ();
if isActivated "arrayDomain" then
selectArrayDomains file;
if isActivated "tmpSpecialAnalysis" then
activateTmpSpecialAnalysis ();
let options = [] in
let options = if isActivated "congruence" then (congruenceOption factors file)::options else options in
let options = if isActivated "octagon" && not (isTerminationTask ()) then (apronOctagonOption factors file)::options else options in
let options = if isActivated "wideningThresholds" then (wideningOption factors file)::options else options in
List.iter (fun o -> o.activate ()) @@ chooseFromOptions (totalTarget - fileCompplexity) options
let reset_lazy () = ResettableLazy.reset functionCallMaps