Source file codegen.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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
type codegen_error = Variable_not_found of Free_variable.t
exception Codegen_error of codegen_error
let pp_codegen_error fmtr = function
| Variable_not_found s ->
Format.fprintf fmtr "Codegen: Variable not found: %a" Free_variable.pp s
let () =
Printexc.register_printer (fun exn ->
match exn with
| Codegen_error err ->
let s = Format.asprintf "%a" pp_codegen_error err in
Some s
| _ -> None)
module Codegen_helpers = struct
open Ast_helper
let loc txt = {Asttypes.txt; loc = Location.none}
let loc_ident x = {Asttypes.txt = Longident.Lident x; loc = Location.none}
let loc_str (x : string) = {Asttypes.txt = x; loc = Location.none}
let ident x = Exp.ident (loc_ident x)
let pvar x = Pat.var (loc_str x)
let saturated name = ["S"; name]
let call f args =
let f = WithExceptions.Option.get ~loc:__LOC__ @@ Longident.unflatten f in
let args = List.map (fun x -> (Asttypes.Nolabel, x)) args in
Exp.(apply (ident (loc f)) args)
let string_of_fv fv = Format.asprintf "%a" Free_variable.pp fv
end
module Codegen : Costlang.S with type 'a repr = Parsetree.expression = struct
type 'a repr = Parsetree.expression
type size = int
let size_ty = Costlang.Ty.int
open Codegen_helpers
open Ast_helper
let true_ = Exp.construct (loc_ident "true") None
let false_ = Exp.construct (loc_ident "false") None
let int i = call (saturated "safe_int") [Exp.constant (Const.int i)]
let float f =
call
(saturated "safe_int")
[call ["int_of_float"] [Exp.constant @@ Const.float (string_of_float f)]]
let ( + ) x y = call ["+"] [x; y]
let sat_sub x y = call (saturated "sub") [x; y]
let ( * ) x y = call ["*"] [x; y]
let ( / ) x y = call ["/"] [x; y]
let max x y = call (saturated "max") [x; y]
let min x y = call (saturated "min") [x; y]
let log2 x = call ["log2"] [x]
let sqrt x = call ["sqrt"] [x]
let free ~name = Exp.ident (loc_ident (string_of_fv name))
let lt x y = call ["<"] [x; y]
let eq x y = call ["="] [x; y]
let shift_left i bits = call ["lsl"] [i; Exp.constant (Const.int bits)]
let shift_right i bits = call ["lsr"] [i; Exp.constant (Const.int bits)]
let lam' ~name _ty f =
let patt = pvar name in
let var = ident name in
Exp.fun_ Nolabel None patt (f var)
let lam ~name = lam' ~name size_ty
let app x y = Exp.apply x [(Nolabel, y)]
let let_ ~name m f =
let id = ident name in
let var = pvar name in
Exp.let_ Nonrecursive [Vb.mk var m] (f id)
let if_ cond ift iff = Exp.ifthenelse cond ift (Some iff)
end
let detach_funcs =
let open Parsetree in
let rec aux acc expr =
match expr with
| {
pexp_desc = Pexp_fun (_, _, {ppat_desc = Ppat_var {txt = arg; _}; _}, expr');
_;
} ->
aux (arg :: acc) expr'
| _ -> (acc, expr)
in
aux []
let rec restore_funcs ~used_vars (acc, expr) =
let open Ast_helper in
match acc with
| arg :: acc ->
let arg =
if List.mem ~equal:String.equal arg used_vars then arg else "_" ^ arg
in
let expr = Exp.fun_ Nolabel None (Codegen_helpers.pvar arg) expr in
restore_funcs ~used_vars (acc, expr)
| [] -> expr
let open_m =
let open Ast_helper in
let open Codegen_helpers in
Str.open_ (Opn.mk (Mod.ident (loc_ident "S.Syntax")))
let generate_let_binding =
let open Ast_helper in
let open Codegen_helpers in
fun ~takes_saturation_reprs name expr ->
let args, expr = detach_funcs expr in
let used_vars =
let vs = ref [] in
let super = Ast_iterator.default_iterator in
let f_expr (i : Ast_iterator.iterator) e =
match e.Parsetree.pexp_desc with
| Pexp_ident {txt = Longident.Lident v; _} -> vs := v :: !vs
| _ -> super.expr i e
in
let i = {super with expr = f_expr} in
i.expr i expr ;
!vs
in
let expr =
if takes_saturation_reprs then expr
else
List.fold_left
(fun e arg ->
if List.mem ~equal:String.equal arg used_vars then
let var = ident arg in
let patt = pvar arg in
Exp.let_
Nonrecursive
[Vb.mk patt (call (saturated "safe_int") [var])]
e
else e)
expr
args
in
let expr = restore_funcs ~used_vars (args, expr) in
Str.value Asttypes.Nonrecursive [Vb.mk (pvar name) expr]
type solution = {
map : float Free_variable.Map.t;
scores_list : ((string * Namespace.t) * Inference.scores) list;
}
let solution_encoding =
let open Data_encoding in
conv
(fun {map; scores_list} -> (map, scores_list))
(fun (map, scores_list) -> {map; scores_list})
@@ obj2
(req "map" (Free_variable.Map.encoding float))
(req
"scores_list"
(list
(tup2 (tup2 string Namespace.encoding) Inference.scores_encoding)))
let pp_solution ppf solution =
let open Format in
let alist =
List.sort (fun (fv1, _) (fv2, _) -> Free_variable.compare fv1 fv2)
@@ List.of_seq
@@ Free_variable.Map.to_seq solution.map
in
fprintf ppf "@[" ;
fprintf
ppf
"@[<2>free_variables:@ @[<v>%a@]@]@;"
(pp_print_list (fun ppf (fv, float) ->
fprintf ppf "%a = %.12g" Free_variable.pp fv float))
alist ;
fprintf
ppf
"@[<2>scores:@ @[<v>%a@]@]"
(pp_print_list (fun ppf ((_s, ns), scores) ->
fprintf ppf "%a : %a" Namespace.pp ns Inference.pp_scores scores))
(List.sort (fun (k1, _) (k2, _) -> compare k1 k2) solution.scores_list) ;
fprintf ppf "@]"
let load_solution_in_binary (fn : string) : solution =
In_channel.with_open_bin fn Marshal.from_channel
let save_solution_in_binary (s : solution) (fn : string) =
Out_channel.with_open_bin fn @@ fun outfile -> Marshal.to_channel outfile s []
let solution_to_csv {map; scores_list} =
let csv_mapping =
Inference.mapping_to_csv (map |> Free_variable.Map.to_seq |> List.of_seq)
in
let csv_scores_list =
List.fold_left
(fun csv (name, score) ->
Csv.concat csv (Inference.scores_to_csv_column name score))
[[]; []]
scores_list
in
Csv.concat csv_mapping csv_scores_list
let load_solution_in_json (fn : string) : solution =
In_channel.with_open_text fn @@ fun ic ->
let s = In_channel.input_all ic in
let open Data_encoding.Json in
Result.fold
~error:Stdlib.failwith
~ok:(destruct solution_encoding)
(from_string s)
let save_solution_in_json (s : solution) (fn : string) =
let open Data_encoding.Json in
let json = construct solution_encoding s in
Out_channel.with_open_text fn @@ fun oc ->
let json : Ezjsonm.t =
match json with `O kvs -> `O kvs | _ -> assert false
in
Ezjsonm.(to_channel ~minify:false oc json)
let load_solution (fn : string) : solution =
try load_solution_in_json fn with _ -> load_solution_in_binary fn
let save_solution s fn =
save_solution_in_binary s fn ;
save_solution_in_json s (fn ^ ".json")
type code =
| Item of {
comments : string list;
name : string option;
code : Parsetree.structure_item;
}
type module_ = code list
let get_name_of_code = function Item {name; _} -> name | _ -> None
let pp_code fmtr =
let open Format in
function
| Comment lines ->
fprintf
fmtr
"(* @[<v>%a@] *)"
(pp_print_list
~pp_sep:(fun fmtr () -> fprintf fmtr "@;")
pp_print_string)
lines
| Item {; name = _; code} ->
List.iter (fprintf fmtr "(* %s *)@;") comments ;
Pprintast.structure_item fmtr code
let pp_module fmtr items =
let open Format in
fprintf fmtr "@[<hv 0>" ;
pp_print_list ~pp_sep:(fun fmtr () -> fprintf fmtr "@;@;") pp_code fmtr items ;
fprintf fmtr "@]@;"
let pp_module fmtr items =
let s = Format.asprintf "%a" pp_module items in
let s =
match Ocamlformat.impl s with
| Ok s -> s
| Error e ->
Format.eprintf "ocamlformat failed: %s@." (Printexc.to_string e) ;
s
in
Format.pp_print_string fmtr s
let make_toplevel_module structure_items =
let open Ast_helper in
let open Codegen_helpers in
let this_file_was_autogenerated =
Comment
[
"Do not edit this file manually.";
"This file was automatically generated from benchmark models";
"If you wish to update a function in this file,";
"a. update the corresponding model, or";
"b. move the function to another module and edit it there.";
]
in
let suppress_unused_open_warning =
Item
{
comments = [];
name = None;
code =
Str.attribute
(Attr.mk
(loc_str "warning")
(PStr [Str.eval (Exp.constant (Const.string "-33"))]));
}
in
let rename_saturation_repr =
Item
{
comments = [];
name = None;
code =
Str.module_
(Mb.mk (loc (Some "S")) (Mod.ident (loc_ident "Saturation_repr")));
}
in
let open_syntax = Item {comments = []; name = None; code = open_m} in
[
this_file_was_autogenerated;
suppress_unused_open_warning;
rename_saturation_repr;
open_syntax;
]
@ structure_items
let ss = Comment ss
let function_name model_name =
"cost_"
^ String.map (function '.' -> '_' | c -> c) (Namespace.basename model_name)
let codegen (Model.Model model) (sol : solution)
(transform : Costlang.transform) model_name =
let subst fv =
match Free_variable.Map.find fv sol.map with
| None -> raise (Codegen_error (Variable_not_found fv))
| Some f -> f
in
let module M = (val model) in
let takes_saturation_reprs = M.takes_saturation_reprs in
let =
let open Costlang in
let ( ++ ) = compose in
let ((module Transform) : transform) =
(module Beta_normalize)
++ (module Ast.At_least_10)
++ (module Subst (struct
let subst = subst
end))
in
let module X = Transform (Comment) in
let module M = M.Def (X) in
let expr = X.prj M.model in
let expr = Format.asprintf "(* @[%a@]" Pprintast.expression expr in
let expr = Stdlib.Option.get @@ String.remove_prefix ~prefix:"(* " expr in
["model " ^ Namespace.to_string model_name; expr]
in
let fun_name = function_name model_name in
let code =
let open Costlang in
let ( ++ ) = compose in
let ((module Transform) : transform) =
(module Ast.Optimize)
++ (module Ast.At_least_10)
++ (module Let_lift)
++ transform
++ (module Subst (struct
let subst = subst
end))
in
let module X = Transform (Codegen) in
let module M = M.Def (X) in
let expr = X.prj M.model in
generate_let_binding ~takes_saturation_reprs fun_name expr
in
Item {comments; name = Some fun_name; code}
let get_codegen_destination
Registration.
{
model = Model.Model (module M);
from = local_models_info;
codegen_destination;
} =
match codegen_destination with
| Some s -> Some s
| None ->
if Namespace.equal M.name @@ Builtin_models.ns "timer_model" then None
else
List.find_map
(fun Registration.{bench_name; _} ->
let open Option_syntax in
let* (module B : Benchmark.S) =
Registration.find_benchmark bench_name
in
match B.purpose with Generate_code d -> Some d | _ -> None)
local_models_info
let codegen_models models sol transform =
List.map
(fun (model_name, info) ->
let benchmark_destination = get_codegen_destination info in
let code = codegen info.model sol transform model_name in
(benchmark_destination, code))
models
let%expect_test "basic_printing" =
let open Codegen in
let term =
lam ~name:"x" @@ fun x ->
lam ~name:"y" @@ fun y ->
let_ ~name:"tmp1" (int 42) @@ fun tmp1 ->
let_ ~name:"tmp2" (int 43) @@ fun tmp2 -> x + y + tmp1 + tmp2
in
let item = generate_let_binding ~takes_saturation_reprs:false "name" term in
Format.printf "%a" Pprintast.structure_item item ;
[%expect
{|
let name x y =
let x = S.safe_int x in
let y = S.safe_int y in
let tmp1 = S.safe_int 42 in
let tmp2 = S.safe_int 43 in ((x + y) + tmp1) + tmp2 |}]
let%expect_test "anonymous_int_literals" =
let open Codegen in
let term =
lam ~name:"x" @@ fun x ->
lam ~name:"y" @@ fun y -> x + y + int 42 + int 43
in
let item = generate_let_binding ~takes_saturation_reprs:false "name" term in
Format.printf "%a" Pprintast.structure_item item ;
[%expect
{|
let name x y =
let x = S.safe_int x in
let y = S.safe_int y in ((x + y) + (S.safe_int 42)) + (S.safe_int 43) |}]
let%expect_test "let_bound_lambda" =
let open Codegen in
let term =
lam ~name:"x" @@ fun x ->
lam ~name:"y" @@ fun y ->
let_ ~name:"incr" (lam ~name:"x" (fun x -> x + int 1)) @@ fun incr ->
app incr x + app incr y
in
let item = generate_let_binding ~takes_saturation_reprs:false "name" term in
Format.printf "%a" Pprintast.structure_item item ;
[%expect
{|
let name x y =
let x = S.safe_int x in
let y = S.safe_int y in
let incr x = x + (S.safe_int 1) in (incr x) + (incr y) |}]
let%expect_test "ill_typed_higher_order" =
let open Codegen in
let term =
lam ~name:"incr" @@ fun incr ->
lam ~name:"x" @@ fun x ->
lam ~name:"y" @@ fun y -> app incr x + app incr y
in
let item = generate_let_binding ~takes_saturation_reprs:false "name" term in
Format.printf "%a" Pprintast.structure_item item ;
[%expect
{|
let name incr x y =
let incr = S.safe_int incr in
let x = S.safe_int x in let y = S.safe_int y in (incr x) + (incr y) |}]
let%expect_test "if_conditional_operator" =
let open Codegen in
let term =
lam ~name:"x" @@ fun x ->
lam ~name:"y" @@ fun y -> if_ (lt x y) y x
in
let item = generate_let_binding ~takes_saturation_reprs:false "name" term in
Format.printf "%a" Pprintast.structure_item item ;
[%expect
{|
let name x y =
let x = S.safe_int x in let y = S.safe_int y in if x < y then y else x |}]
let%expect_test "module_generation" =
let open Codegen in
let term = lam ~name:"x" @@ fun x -> x in
let name = "func_name" in
let module_ =
make_toplevel_module
[
Item
{
comments = ["comment"];
name = Some name;
code =
generate_let_binding
~takes_saturation_reprs:false
"func_name"
term;
};
]
in
Format.printf "%a" pp_module module_ ;
[%expect
{|
(* Do not edit this file manually.
This file was automatically generated from benchmark models
If you wish to update a function in this file,
a. update the corresponding model, or
b. move the function to another module and edit it there. *)
[@@@warning "-33"]
module S = Saturation_repr
open S.Syntax
(* comment *)
let func_name x =
let x = S.safe_int x in
x |}]
let%expect_test "takes_saturation_reprs" =
let open Codegen in
let term =
lam' ~name:"x" Costlang.Ty.num @@ fun x ->
lam' ~name:"y" Costlang.Ty.num @@ fun y ->
let_ ~name:"tmp1" (int 42) @@ fun tmp1 ->
let_ ~name:"tmp2" (int 43) @@ fun tmp2 -> x + y + tmp1 + tmp2
in
let item = generate_let_binding ~takes_saturation_reprs:true "name" term in
Format.printf "%a" Pprintast.structure_item item ;
[%expect
{|
let name x y =
let tmp1 = S.safe_int 42 in
let tmp2 = S.safe_int 43 in ((x + y) + tmp1) + tmp2 |}]
module Parser = struct
let with_ic fn f =
let ic = open_in fn in
Fun.protect (fun () -> f ic) ~finally:(fun () -> close_in ic)
let parse ic =
let lexbuf = Lexing.from_channel ic in
Lexer.init () ;
Parser.implementation Lexer.token lexbuf
let get_pattern_vars pattern =
let open Parsetree in
let vars = ref [] in
let super = Ast_iterator.default_iterator in
let pat self p =
(match p.ppat_desc with
| Ppat_var {txt; _} | Ppat_alias (_, {txt; _}) -> vars := txt :: !vars
| _ -> ()) ;
super.pat self p
in
let self = {super with pat} in
self.pat self pattern ;
!vars
let scrape_defined_vars str =
let open Parsetree in
let defs = ref [] in
let super = Ast_iterator.default_iterator in
let value_binding _self vb =
defs := get_pattern_vars vb.Parsetree.pvb_pat @ !defs ;
()
in
let structure_item self si =
match si.pstr_desc with
| Pstr_value _ | Pstr_module _ -> super.structure_item self si
| _ -> ()
in
let self = {super with value_binding; structure_item} in
self.structure self str ;
!defs
let is_cost_function n =
let prefix = "cost_" in
TzString.has_prefix ~prefix n
let get_cost_functions fn =
try
with_ic fn @@ fun ic ->
Ok (List.filter is_cost_function @@ scrape_defined_vars @@ parse ic)
with exn -> Error exn
end