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
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) = object
inherit nopCilVisitor
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;
SkipChildren
end
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 thisVisitor = new functionVisitor(calling,calledBy, argLists) in
visitCilFileSameGlobals thisVisitor (!Cilfacade.current_file);
!calling, !calledBy, !argLists)
let calledFunctions fd = ResettableLazy.force functionCallMaps |> fun (x,_,_) -> x |> FunctionCallMap.find_opt fd |> Option.value ~default:FunctionSet.empty
let callingFunctions fd = ResettableLazy.force functionCallMaps |> fun (_,x,_) -> x |> FunctionCallMap.find_opt fd |> Option.value ~default:(FunctionSet.empty, 0) |> fst
let timesCalled fd = ResettableLazy.force functionCallMaps |> fun (_,x,_) -> x |> FunctionCallMap.find_opt fd |> Option.value ~default:(FunctionSet.empty, 0) |> snd
let functionArgs fd = ResettableLazy.force functionCallMaps |> fun (_,_,x) -> x |> FunctionCallMap.find_opt fd
let findMallocWrappers () =
let isMalloc f =
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
|> (fun (x,_,_) -> x)
|> 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 -> print_endline ("malloc wrapper: " ^ 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 ->
print_endline (" " ^ vinfo.vname);
setCongruenceRecursive (Cilfacade.find_varinfo_fundec vinfo) (depth -1) neigbourFunction
)
(FunctionSet.filter
(fun x -> not (isExtern x.vstorage || BatString.starts_with x.vname "__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 ->
print_endline ("function " ^ (CilType.Fundec.show fd) ^" uses mod, enable congruence domain recursively for:");
print_endline (" \"down\":");
setCongruenceRecursive fd 6 calledFunctions;
print_endline (" \"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 |> fun (x,_,_) -> x |> FunctionCallMap.iter (fun f set ->
if FunctionSet.mem f set || (not @@ FunctionSet.disjoint (calledFunctions f) (callingFunctions f)) then (
print_endline ("function " ^ (f.vname) ^" is recursive, disable interval context");
f.vattr <- addAttributes (f.vattr) [Attr ("goblint_context",[AStr "base.no-interval"; AStr "apron.no-context"])];
)
)
let notNeccessaryThreadAnalyses = ["race"; "deadlock"; "maylocks"; "symb_locks"; "thread"; "threadid"; "threadJoins"; "threadreturn"]
let reduceThreadAnalyses () =
let hasThreadCreate () =
ResettableLazy.force functionCallMaps
|> (fun (_,x,_) -> x)
|> FunctionCallMap.exists (fun var (callers,_) ->
if LibraryFunctions.is_special var then (
let desc = LibraryFunctions.find var in
match functionArgs var with
| None -> false;
| Some args ->
match desc.special args with
| ThreadCreate _ ->
print_endline @@ "thread created by " ^ var.vname ^ ", called by:";
FunctionSet.iter ( fun c -> print_endline @@ " " ^ c.vname) callers;
true
| _ -> false
)
else
false
)
in
if not @@ hasThreadCreate () then (
print_endline @@ "no thread creation -> disabeling thread analyses \"" ^ (String.concat ", " notNeccessaryThreadAnalyses) ^ "\"";
let disableAnalysis = GobConfig.set_auto "ana.activated[-]" in
List.iter disableAnalysis notNeccessaryThreadAnalyses;
)
let focusOnSpecification () =
match Svcomp.Specification.of_option () with
| UnreachCall s -> ()
| NoDataRace ->
print_endline @@ "Specification: NoDataRace -> enabeling thread analyses \"" ^ (String.concat ", " notNeccessaryThreadAnalyses) ^ "\"";
let enableAnalysis = GobConfig.set_auto "ana.activated[+]" in
List.iter enableAnalysis notNeccessaryThreadAnalyses;
| NoOverflow ->
set_bool "ana.int.def_exc" true;
set_bool "ana.int.interval" true
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 rec = function
| UnOp (Neg, e, _) -> extractVar e
| Lval ((Var info),_) -> Some info
| _ -> None
let = function
| BinOp (PlusA, e1,e2, (TInt _))
| BinOp (MinusA, e1,e2, (TInt _)) -> (
match extractVar e1, extractVar e2 with
| Some a, Some b -> Some (`Left (a,b))
| Some a, None
| None, Some a -> if isConstant e1 then Some (`Right a) else None
| _,_ -> None
)
| _ -> None
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
let handle varMap v globals = function
| Some (`Left (a,b)) ->
addOrCreateVarMapping varMap a v globals;
addOrCreateVarMapping varMap b v globals;
| Some (`Right a) -> addOrCreateVarMapping varMap a v globals;
| None -> ()
class octagonVariableVisitor(varMap, globals) = object
inherit nopCilVisitor
method! vexpr = function
| BinOp (op, e1,e2, (TInt _)) when isComparison op -> (
handle varMap 5 globals (extractOctagonVars e1) ;
handle varMap 5 globals (extractOctagonVars e2) ;
DoChildren
)
| Lval ((Var info),_) -> handle varMap 1 globals (Some (`Right info)) ; SkipChildren
| 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 () =
print_endline @@ "Congruence: " ^ string_of_int cost;
set_bool "ana.int.congruence" true;
print_endline "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
match Svcomp.Specification.of_option () with
| NoOverflow -> 12
| _ -> 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 () =
print_endline @@ "Octagon: " ^ string_of_int cost;
set_bool "annotation.goblint_apron_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";
print_endline "Enabled octagon domain for:";
print_endline @@ String.concat ", " @@ List.map (fun info -> info.vname) allVars;
List.iter (fun info -> info.vattr <- addAttribute (Attr("goblint_apron_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 () ->
print_endline @@ "Widening: " ^ string_of_int cost;
set_bool "ana.int.interval_threshold_widening" true;
set_string "ana.int.interval_threshold_widening_constants" "comparisons";
print_endline "Enabled widening thresholds";
}
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 (print_endline @@ "Total: " ^ string_of_int (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
| [] -> print_endline @@ "Total: " ^ string_of_int (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 chooseConfig file =
let factors = collectFactors visitCilFileSameGlobals file in
let fileCompplexity = estimateComplexity factors file in
print_endline "Collected factors:";
printFactors factors;
print_endline "";
print_endline "Complexity estimates:";
print_endline @@ "File: " ^ string_of_int fileCompplexity;
if fileCompplexity < totalTarget && isActivated "congruence" then
addModAttributes file;
if isActivated "noRecursiveIntervals" then
disableIntervalContextsInRecursiveFunctions ();
if isActivated "mallocWrappers" then
findMallocWrappers ();
if isActivated "specification" && get_string "ana.specification" <> "" then
focusOnSpecification ();
if isActivated "enums" && hasEnums file then
set_bool "ana.int.enums" true;
if isActivated "singleThreaded" then
reduceThreadAnalyses ();
if isActivated "arrayDomain" then
selectArrayDomains file;
let options = [] in
let options = if isActivated "congruence" then (congruenceOption factors file)::options else options in
let options = if isActivated "octagon" 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