WIP: optimizations

This commit is contained in:
Denis Merigoux 2021-04-03 13:44:11 +02:00
parent dbed2e72ed
commit 3712125504
9 changed files with 1128 additions and 1346 deletions

View File

@ -172,6 +172,7 @@ $(FRENCH_LAW_LIB_DIR)/law_source/unit_tests/tests_allocations_familiales.ml:
generate_french_law_library:\
$(FRENCH_LAW_LIB_DIR)/law_source/allocations_familiales.ml \
$(FRENCH_LAW_LIB_DIR)/law_source/unit_tests/tests_allocations_familiales.ml
$(MAKE) format
#> build_french_law_library : Builds the OCaml French law library
build_french_law_library: generate_french_law_library format

View File

@ -12,7 +12,7 @@
or implied. See the License for the specific language governing permissions and limitations under
the License. *)
[@@@ocaml.warning "-7"]
[@@@ocaml.warning "-7-34"]
open Utils
@ -33,19 +33,10 @@ module EnumConstructor : Uid.Id with type info = Uid.MarkedString.info =
module EnumMap : Map.S with type key = EnumName.t = Map.Make (EnumName)
type typ_lit = TBool | TUnit | TInt | TRat | TMoney | TDate | TDuration
[@@deriving
visitors { variety = "map"; name = "typ_lit_map"; nude = true },
visitors { variety = "iter"; name = "typ_lit_iter"; nude = true }]
type struct_name = (StructName.t[@opaque])
[@@deriving
visitors { variety = "map"; name = "struct_name_map"; nude = true },
visitors { variety = "iter"; name = "struct_name_iter"; nude = true }]
type struct_name = StructName.t
type enum_name = (EnumName.t[@opaque])
[@@deriving
visitors { variety = "map"; name = "enum_name_map"; nude = true },
visitors { variety = "iter"; name = "enum_name_iter"; nude = true }]
type enum_name = EnumName.t
type typ =
| TLit of typ_lit
@ -54,44 +45,16 @@ type typ =
| TArrow of typ Pos.marked * typ Pos.marked
| TArray of typ Pos.marked
| TAny
[@@deriving
visitors
{
variety = "map";
ancestors = [ "Pos.marked_map"; "typ_lit_map"; "struct_name_map"; "enum_name_map" ];
name = "typ_map";
},
visitors
{
variety = "iter";
ancestors = [ "Pos.marked_iter"; "typ_lit_iter"; "struct_name_iter"; "enum_name_iter" ];
name = "typ_iter";
}]
type date = (Runtime.date[@opaque])
[@@deriving
visitors { variety = "map"; name = "date_map"; nude = true },
visitors { variety = "iter"; name = "date_iter"; nude = true }]
type date = Runtime.date
type duration = (Runtime.duration[@opaque])
[@@deriving
visitors { variety = "map"; name = "duration_map"; nude = true },
visitors { variety = "iter"; name = "duration_iter"; nude = true }]
type duration = Runtime.duration
type integer = (Runtime.integer[@opaque])
[@@deriving
visitors { variety = "map"; name = "integer_map"; nude = true },
visitors { variety = "iter"; name = "integer_iter"; nude = true }]
type integer = Runtime.integer
type decimal = (Runtime.decimal[@opaque])
[@@deriving
visitors { variety = "map"; name = "decimal_map"; nude = true },
visitors { variety = "iter"; name = "decimal_iter"; nude = true }]
type decimal = Runtime.decimal
type money = (Runtime.money[@opaque])
[@@deriving
visitors { variety = "map"; name = "money_map"; nude = true },
visitors { variety = "iter"; name = "money_iter"; nude = true }]
type money = Runtime.money
type lit =
| LBool of bool
@ -102,29 +65,10 @@ type lit =
| LUnit
| LDate of date
| LDuration of duration
[@@deriving
visitors
{
variety = "map";
ancestors = [ "decimal_map"; "date_map"; "duration_map"; "integer_map"; "money_map" ];
name = "lit_map";
},
visitors
{
variety = "iter";
ancestors = [ "decimal_iter"; "date_iter"; "duration_iter"; "integer_iter"; "money_iter" ];
name = "lit_iter";
}]
type op_kind = KInt | KRat | KMoney | KDate | KDuration
[@@deriving
visitors { variety = "map"; name = "op_kind_map"; nude = true },
visitors { variety = "iter"; name = "op_kind_iter"; nude = true }]
type ternop = Fold
[@@deriving
visitors { variety = "map"; name = "ternop_map"; nude = true },
visitors { variety = "iter"; name = "ternop_iter"; nude = true }]
type binop =
| And
@ -142,14 +86,8 @@ type binop =
| Neq
| Map
| Filter
[@@deriving
visitors { variety = "map"; ancestors = [ "op_kind_map" ]; name = "binop_map"; nude = true },
visitors { variety = "iter"; ancestors = [ "op_kind_iter" ]; name = "binop_iter"; nude = true }]
type log_entry = VarDef | BeginCall | EndCall | PosRecordIfTrueBool
[@@deriving
visitors { variety = "map"; name = "log_entry_map"; nude = true },
visitors { variety = "iter"; name = "log_entry_iter"; nude = true }]
type unop =
| Not
@ -161,25 +99,8 @@ type unop =
| GetDay
| GetMonth
| GetYear
[@@deriving
visitors { variety = "map"; ancestors = [ "op_kind_map"; "log_entry_map" ]; name = "unop_map" },
visitors
{ variety = "iter"; ancestors = [ "op_kind_iter"; "log_entry_iter" ]; name = "unop_iter" }]
type operator = Ternop of ternop | Binop of binop | Unop of unop
[@@deriving
visitors
{
variety = "map";
ancestors = [ "ternop_map"; "binop_map"; "unop_map" ];
name = "operator_map";
},
visitors
{
variety = "iter";
ancestors = [ "ternop_iter"; "binop_iter"; "unop_iter" ];
name = "operator_iter";
}]
type expr =
| EVar of (expr Bindlib.var[@opaque]) Pos.marked
@ -189,36 +110,12 @@ type expr =
| EMatch of expr Pos.marked * expr Pos.marked list * enum_name
| EArray of expr Pos.marked list
| ELit of lit
| EAbs of ((expr, expr Pos.marked) Bindlib.mbinder[@opaque]) Pos.marked * typ Pos.marked list
| EAbs of (expr, expr Pos.marked) Bindlib.mbinder Pos.marked * typ Pos.marked list
| EApp of expr Pos.marked * expr Pos.marked list
| EAssert of expr Pos.marked
| EOp of operator
| EDefault of expr Pos.marked list * expr Pos.marked * expr Pos.marked
| EIfThenElse of expr Pos.marked * expr Pos.marked * expr Pos.marked
[@@deriving
visitors
{
variety = "map";
ancestors =
[
"Pos.marked_map"; "operator_map"; "struct_name_map"; "enum_name_map"; "typ_map"; "lit_map";
];
name = "expr_map";
},
visitors
{
variety = "iter";
ancestors =
[
"Pos.marked_iter";
"operator_iter";
"struct_name_iter";
"enum_name_iter";
"typ_iter";
"lit_iter";
];
name = "expr_iter";
}]
type struct_ctx = (StructFieldName.t * typ Pos.marked) list StructMap.t

View File

@ -0,0 +1,72 @@
(* This file is part of the Catala compiler, a specification language for tax and social benefits
computation rules. Copyright (C) 2020 Inria, contributor: Denis Merigoux
<denis.merigoux@inria.fr>
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing permissions and limitations under
the License. *)
open Utils
open Ast
let rec peephole_expr (e : expr Pos.marked) : expr Pos.marked Bindlib.box =
match Pos.unmark e with
| EVar (v, pos) -> Bindlib.box_apply (fun v -> (v, pos)) (Bindlib.box_var v)
| ETuple (args, n) ->
Bindlib.box_apply
(fun args -> (ETuple (args, n), Pos.get_position e))
(Bindlib.box_list (List.map peephole_expr args))
| ETupleAccess (e1, i, n, ts) ->
Bindlib.box_apply
(fun e1 -> (ETupleAccess (e1, i, n, ts), Pos.get_position e))
(peephole_expr e1)
| EInj (e1, i, n, ts) ->
Bindlib.box_apply (fun e1 -> (EInj (e1, i, n, ts), Pos.get_position e)) (peephole_expr e1)
| EMatch (arg, cases, n) ->
Bindlib.box_apply2
(fun arg cases -> (EMatch (arg, cases, n), Pos.get_position e))
(peephole_expr arg)
(Bindlib.box_list (List.map peephole_expr cases))
| EArray args ->
Bindlib.box_apply
(fun args -> (EArray args, Pos.get_position e))
(Bindlib.box_list (List.map peephole_expr args))
| EAbs ((binder, pos_binder), ts) ->
let vars, body = Bindlib.unmbind binder in
let body = peephole_expr body in
Bindlib.box_apply
(fun binder -> (EAbs ((binder, pos_binder), ts), Pos.get_position e))
(Bindlib.bind_mvar vars body)
| EApp (e1, args) ->
Bindlib.box_apply2
(fun e1 args -> (EApp (e1, args), Pos.get_position e))
(peephole_expr e1)
(Bindlib.box_list (List.map peephole_expr args))
| EAssert e1 -> Bindlib.box_apply (fun e1 -> (EAssert e1, Pos.get_position e)) (peephole_expr e1)
| EIfThenElse (e1, e2, e3) ->
Bindlib.box_apply3
(fun e1 e2 e3 -> (EIfThenElse (e1, e2, e3), Pos.get_position e))
(peephole_expr e1) (peephole_expr e2) (peephole_expr e3)
| EDefault (exceptions, just, cons) ->
Bindlib.box_apply3
(fun exceptions just cons ->
( (match exceptions with
| [] -> EIfThenElse (just, cons, (ELit LEmptyError, Pos.get_position e))
| _ -> EDefault (exceptions, just, cons)),
Pos.get_position e ))
(Bindlib.box_list (List.map peephole_expr exceptions))
(peephole_expr just) (peephole_expr cons)
| ELit _ | EOp _ -> Bindlib.box e
let peephole_optimizations (p : program) : program =
{
p with
scopes = List.map (fun (name, var, e) -> (name, var, Bindlib.unbox (peephole_expr e))) p.scopes;
}
let optimize_program (p : program) : program = peephole_optimizations p

View File

@ -19,12 +19,13 @@ module Pos = Utils.Pos
(** Entry function for the executable. Returns a negative number in case of error. *)
let driver (source_file : Pos.input_file) (debug : bool) (dcalc : bool) (unstyled : bool)
(wrap_weaved_output : bool) (backend : string) (language : string option)
(max_prec_digits : int option) (trace : bool) (ex_scope : string option)
(max_prec_digits : int option) (trace : bool) (optimize : bool) (ex_scope : string option)
(output_file : string option) : int =
try
Cli.debug_flag := debug;
Cli.style_flag := not unstyled;
Cli.trace_flag := trace;
Cli.optimize_flag := optimize;
Cli.debug_print "Reading files...";
(match source_file with FileName _ -> () | Contents c -> Cli.contents := c);
(match max_prec_digits with None -> () | Some i -> Cli.max_prec_digits := i);
@ -146,6 +147,13 @@ let driver (source_file : Pos.input_file) (debug : bool) (dcalc : bool) (unstyle
let prgm, prgm_expr, type_ordering =
Scopelang.Scope_to_dcalc.translate_program prgm scope_uid
in
let prgm =
if optimize then begin
Cli.debug_print "Optimizing default calculus...";
Dcalc.Optimizations.optimize_program prgm
end
else prgm
in
if dcalc then begin
Format.printf "%a\n"
(Dcalc.Print.format_expr prgm.decl_ctx)
@ -185,8 +193,10 @@ let driver (source_file : Pos.input_file) (debug : bool) (dcalc : bool) (unstyle
results;
0
| Cli.OCaml ->
Cli.debug_print "Compiling program into OCaml...";
Cli.debug_print "Compiling program into lambda calculus...";
let prgm = Lcalc.Compile_with_exceptions.translate_program prgm in
(* let prgm = if optimize then begin Cli.debug_print "Optimizing lambda calculus...";
Lcalc.Optimizations.optimize_program prgm end else prgm in *)
let source_file =
match source_file with
| FileName f -> f
@ -201,6 +211,7 @@ let driver (source_file : Pos.input_file) (debug : bool) (dcalc : bool) (unstyle
Cli.debug_print (Printf.sprintf "Writing to %s..." output_file);
let oc = open_out output_file in
let fmt = Format.formatter_of_out_channel oc in
Cli.debug_print "Compiling program into OCaml...";
Lcalc.To_ocaml.format_program fmt prgm type_ordering;
close_out oc;
0

View File

@ -107,6 +107,18 @@ and translate_expr (ctx : ctx) (e : D.expr Pos.marked) : A.expr Pos.marked Bindl
Bindlib.box_apply
(fun new_binder -> Pos.same_pos_as (A.EAbs ((new_binder, pos_binder), ts)) e)
new_binder
| D.EDefault ([ exn ], just, cons) when !Cli.optimize_flag ->
Bindlib.box_apply3
(fun exn just cons ->
Pos.same_pos_as
(A.ECatch
( exn,
A.EmptyError,
Pos.same_pos_as
(A.EIfThenElse (just, cons, Pos.same_pos_as (A.ERaise A.EmptyError) e))
e ))
e)
(translate_expr ctx exn) (translate_expr ctx just) (translate_expr ctx cons)
| D.EDefault (exceptions, just, cons) ->
translate_default ctx exceptions just cons (Pos.get_position e)

View File

@ -0,0 +1,72 @@
(* This file is part of the Catala compiler, a specification language for tax and social benefits
computation rules. Copyright (C) 2020 Inria, contributor: Denis Merigoux
<denis.merigoux@inria.fr>
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing permissions and limitations under
the License. *)
open Utils
open Ast
let rec peephole_expr (e : expr Pos.marked) : expr Pos.marked Bindlib.box =
match Pos.unmark e with
| EVar (v, pos) -> Bindlib.box_apply (fun v -> (v, pos)) (Bindlib.box_var v)
| ETuple (args, n) ->
Bindlib.box_apply
(fun args -> (ETuple (args, n), Pos.get_position e))
(Bindlib.box_list (List.map peephole_expr args))
| ETupleAccess (e1, i, n, ts) ->
Bindlib.box_apply
(fun e1 -> (ETupleAccess (e1, i, n, ts), Pos.get_position e))
(peephole_expr e1)
| EInj (e1, i, n, ts) ->
Bindlib.box_apply (fun e1 -> (EInj (e1, i, n, ts), Pos.get_position e)) (peephole_expr e1)
| EMatch (arg, cases, n) ->
Bindlib.box_apply2
(fun arg cases -> (EMatch (arg, cases, n), Pos.get_position e))
(peephole_expr arg)
(Bindlib.box_list (List.map peephole_expr cases))
| EArray args ->
Bindlib.box_apply
(fun args -> (EArray args, Pos.get_position e))
(Bindlib.box_list (List.map peephole_expr args))
| EAbs ((binder, pos_binder), ts) ->
let vars, body = Bindlib.unmbind binder in
let body = peephole_expr body in
Bindlib.box_apply
(fun binder -> (EAbs ((binder, pos_binder), ts), Pos.get_position e))
(Bindlib.bind_mvar vars body)
| EApp (e1, args) ->
Bindlib.box_apply2
(fun e1 args -> (EApp (e1, args), Pos.get_position e))
(peephole_expr e1)
(Bindlib.box_list (List.map peephole_expr args))
| EAssert e1 -> Bindlib.box_apply (fun e1 -> (EAssert e1, Pos.get_position e)) (peephole_expr e1)
| EIfThenElse (e1, e2, e3) ->
Bindlib.box_apply3
(fun e1 e2 e3 ->
match Pos.unmark e1 with
| ELit (LBool true) -> e2
| ELit (LBool false) -> e3
| _ -> (EIfThenElse (e1, e2, e3), Pos.get_position e))
(peephole_expr e1) (peephole_expr e2) (peephole_expr e3)
| ECatch (e1, exn, e2) ->
Bindlib.box_apply2
(fun e1 e2 ->
( (match (Pos.unmark e1, exn) with
| ERaise exn2, exn1 when exn1 = exn2 -> Pos.unmark e2
| _ -> ECatch (e1, exn, e2)),
Pos.get_position e ))
(peephole_expr e1) (peephole_expr e2)
| ERaise _ | ELit _ | EOp _ -> Bindlib.box e
let peephole_optimizations (p : program) : program =
{ p with scopes = List.map (fun (var, e) -> (var, Bindlib.unbox (peephole_expr e))) p.scopes }
let optimize_program (p : program) : program = peephole_optimizations p

View File

@ -37,6 +37,8 @@ let max_prec_digits = ref 20
let trace_flag = ref false
let optimize_flag = ref false
open Cmdliner
let file =
@ -52,6 +54,8 @@ let debug_dcalc =
let unstyled = Arg.(value & flag & info [ "unstyled" ] ~doc:"Removes styling from terminal output")
let optimize = Arg.(value & flag & info [ "optimize"; "O" ] ~doc:"Run compiler optimizations")
let trace_opt =
Arg.(value & flag & info [ "trace"; "t" ] ~doc:"Displays a trace of the intepreter's computation")
@ -98,7 +102,7 @@ let output =
let catala_t f =
Term.(
const f $ file $ debug $ debug_dcalc $ unstyled $ wrap_weaved_output $ backend $ language
$ max_prec_digits_opt $ trace_opt $ ex_scope $ output)
$ max_prec_digits_opt $ trace_opt $ optimize $ ex_scope $ output)
let version = "0.2.0"

View File

@ -32,6 +32,8 @@ val debug_flag : bool ref
val style_flag : bool ref
(** Styles the terminal output *)
val optimize_flag : bool ref
val max_prec_digits : int ref
(** Max number of digits to show for decimal results *)
@ -71,12 +73,13 @@ val catala_t :
string option ->
int option ->
bool ->
bool ->
string option ->
string option ->
'a) ->
'a Cmdliner.Term.t
(** Main entry point:
[catala_t file debug unstyled wrap_weaved_output backend language max_prec_digits_opt trace_opt
[catala_t file debug unstyled wrap_weaved_output backend language max_prec_digits_opt trace_opt optimize
ex_scope output] *)
val version : string

File diff suppressed because it is too large Load Diff