text
stringlengths
12
786k
type exp = | Var of string | Let of string * exp * exp | Fun of string list * exp | App of exp * exp list
let reserved = [ " let " ; " in " ; " fun " ]
let initial = letter <|> exactly ' _ '
let subseqt = alpha_num <|> exactly ' _ '
let ident = ( spaces >> initial <~> many subseqt ) => implode >>= function | s when List . mem s reserved -> mzero | s -> return s
let parens = between ( token " ( " ) ( token " ) " )
let bracks = between ( token " [ " ) ( token " ] " )
let comma_list x = sep_by x ( token " , " )
let comma_list1 x = sep_by1 x ( token " , " )
let rec expr input = ( let_expr <|> fun_expr <|> simple_expr ) input ( token " let " >> ident >>= fun name -> token " " = >> expr >>= fun value -> token " in " >> expr >>= fun body -> return ( Let ( name , value , body ) ) ) input ( token " fun " >> many ident >>= fu...
let parse_expr = parse expr
type ty = | TConst of string | TApp of ty * ty list | TFun of ty list * ty | TVar of tvar ref | Poly of int | Bound of ty | Unbound of int * level
type env = ty Env . t
let id_counter = ref ( - 1 )
let gen_id = fun ( ) -> incr id_counter ; ! id_counter
let reset_id = fun ( ) -> id_counter := ( - 1 )
let fresh_var level = TVar ( ref @@ Unbound ( gen_id ( ) , level ) )
let fresh_poly_var ( ) = TVar ( ref @@ Poly ( gen_id ( ) ) )
let rec string_of_ty ( t : ty ) : string = let id_name_map = Hashtbl . create 26 in let gensym = let counter = ref ( - 1 ) in fun ( ) -> incr counter ; char_of_int ( 97 + ! counter ) |> String . make 1 in let rec walk = function | TConst k -> k | TApp ( t1 , args ) -> let...
let rec generalize ( level : level ) ( t : ty ) : ty = match t with | TVar { contents = Unbound ( id , lv ) } when lv > level -> TVar ( ref @@ Poly id ) | TVar { contents = Bound t ' } -> generalize level t ' | TApp ( t1 , args ) -> TApp ( generalize level t1 , List...
let rec unify ( t1 : ty ) ( t2 : ty ) : unit = match t1 , t2 with | _ when t1 = t2 -> ( ) | TApp ( x , args ) , TApp ( x ' , args ' ) -> ( unify x x ' ; List . iter2 unify args args ' ) | TFun ( params , t ) , TFun ( params ' , t ' ) -> ( List ...
let rec w ( env : env ) ( level : level ) ( exp : exp ) : ty = match exp with | Var v -> begin try instantiate level ( Env . find v env ) with Not_found -> raise @@ Runtime_error ( " unbound type variable " ^ v ) end | Fun ( params , body ) -> let t_params = List . map ( ...
let replace_consts vars t = let env = List . fold_left ( fun env v -> Env . add v ( fresh_poly_var ( ) ) env ) Env . empty vars in let rec walk = function | TConst k when Env . mem k env -> Env . find k env | TApp ( fn , args ) -> TApp ( walk fn , List . map walk args ) | TF...
let ty_ident = ( spaces >> initial <~> many subseqt ) => implode >>= function | " forall " -> mzero | s -> return s
let rec ty input = ( fun_or_simple <|> paren_fun_ty ) input let maybe_fun t = ( token " " -> >> ty >>= fun body -> return ( TFun ( [ t ] , body ) ) ) <|> return t in ( simple_ty >>= maybe_fun ) input let rec foldl t = ( bracks ( comma_list1 ty ) >>= fun a -> foldl ( TAp...
let ty_forall = token " forall " >> token " [ " >> many ty_ident >>= fun vars -> token " ] " >> ty >>= fun body -> return ( replace_consts vars body )
let parse_ty = parse ( ty_forall <|> ty )
let make_env ( ) = let assume name ty_str env = match parse_ty ( LazyStream . of_string ty_str ) with | None -> raise Syntax_error | Some t -> Env . add name t env in Env . empty |> assume " head " " forall [ a ] list [ a ] -> a " |> assume " tail " " forall [ a ] list [...
let ( ) = match parse_expr ( LazyStream . of_channel stdin ) with | None -> raise Syntax_error | Some exp -> let env = make_env ( ) in reset_id ( ) ; exp |> w env 0 |> generalize ( - 1 ) |> string_of_ty |> print_endline
let rec ask v x = printf " % s \ n > " %! x ; let r = try read_line ( ) with End_of_file -> raise AbortedByUser in match v r with | None -> r | Some vp -> printf " error : % s \ n " vp ; ask v x
let rec ask_many v x = let r = ask v x in if r = " " then [ ] else r :: ask_many v x
let run ( ) = ( try let _ = Project . findPath ( ) in raise ProjectAlreadyExists with Project . NoConfFile -> ( ) ) ; if not ( Unix . isatty Unix . stdout ) then ( raise CannotRunNotInteractive ) ; printf " % swelcome to the obuild wizard % s \ n " ( color_green ( ) ...
let constant_field ( expr : Flambda . t ) : Flambda . constant_defining_value_block_field option = match expr with | Let { var ; defining_expr = Const c ; body = Var var ' ; _ } -> assert ( Variable . equal var var ' ) ; Some ( Flambda . Const c ) | Let { var ; defining_...
let rec loop ( program : Flambda . program_body ) : Flambda . program_body = match program with | Initialize_symbol ( symbol , tag , fields , program ) -> let constant_fields = List . map constant_field fields in begin match Misc . Stdlib . List . some_if_all_elements_are_some constant_f...
let run ( program : Flambda . program ) = { program with program_body = loop program . program_body ; }
type t = | Always_inlined | Hint_inlined | Never_inlined | Unroll of int | Default_inlined
let [ @ ocamlformat " disable " ] print ppf t = let fprintf = Format . fprintf in match t with | Always_inlined -> fprintf ppf " Always_inlined " | Hint_inlined -> fprintf ppf " Hint_inlined " | Never_inlined -> fprintf ppf " Never_inlined " | Unroll n -> fprintf ppf " [ ( @ Unro...
let equal t1 t2 = match t1 , t2 with | Always_inlined , Always_inlined | Hint_inlined , Hint_inlined | Never_inlined , Never_inlined | Default_inlined , Default_inlined -> true | Unroll n1 , Unroll n2 -> n1 = n2 | ( ( Always_inlined | Hint_inlined | Never_inlined | Unroll _ | Default_inli...
let is_default t = match t with | Default_inlined -> true | Always_inlined | Hint_inlined | Never_inlined | Unroll _ -> false
let from_lambda ( attr : Lambda . inlined_attribute ) = match attr with | Always_inlined -> Always_inlined | Never_inlined -> Never_inlined | Hint_inlined -> Hint_inlined | Unroll i -> Unroll i | Default_inlined -> Default_inlined
type simplify_variable_result = | No_binding of Variable . t | Binding of Variable . t * ( Flambda . named Flambda . With_free_variables . t )
let simplify_free_variable_internal env original_var = let var = Freshening . apply_variable ( E . freshening env ) original_var in let original_var = var in let var = let approx = E . find_exn env var in match approx . var with | Some var when E . mem env var -> var | Some _ | None -> var in m...
let simplify_free_variable env var ~ f : Flambda . t * R . t = match simplify_free_variable_internal env var with | No_binding var , approx -> f env var approx | Binding ( var , named ) , approx -> let module W = Flambda . With_free_variables in let var = Variable . rename var in let env = ...
let simplify_free_variables env vars ~ f : Flambda . t * R . t = let rec collect_bindings vars env bound_vars approxs : Flambda . t * R . t = match vars with | [ ] -> f env ( List . rev bound_vars ) ( List . rev approxs ) | var :: vars -> match simplify_free_variable_internal env var...
let simplify_free_variables_named env vars ~ f : Flambda . named * R . t = let rec collect_bindings vars env bound_vars approxs : Flambda . maybe_named * R . t = match vars with | [ ] -> let named , r = f env ( List . rev bound_vars ) ( List . rev approxs ) in Is_named named , r |...
let simplify_free_variable_named env var ~ f : Flambda . named * R . t = simplify_free_variables_named env [ var ] ~ f ( : fun env vars vars_approxs -> match vars , vars_approxs with | [ var ] , [ approx ] -> f env var approx | _ -> assert false )
let simplify_named_using_approx r lam approx = let lam , _summary , approx = A . simplify_named approx lam in lam , R . set_approx r approx
let simplify_using_approx_and_env env r original_lam approx = let lam , summary , approx = A . simplify_using_env approx ~ is_present_in_env ( : E . mem env ) original_lam in let r = let r = ret r approx in match summary with | Replaced_term -> R . map_benefit r ( B . remove_code original_lam ...
let simplify_named_using_approx_and_env env r original_named approx = let named , summary , approx = A . simplify_named_using_env approx ~ is_present_in_env ( : E . mem env ) original_named in let r = let r = ret r approx in match summary with | Replaced_term -> R . map_benefit r ( B . remove_...
let simplify_const ( const : Flambda . const ) = match const with | Int i -> A . value_int i | Char c -> A . value_char c
let approx_for_allocated_const ( const : Allocated_const . t ) = match const with | String s -> A . value_string ( String . length s ) None | Immutable_string s -> A . value_string ( String . length s ) ( Some s ) | Int32 i -> A . value_boxed_int Int32 i | Int64 i -> A . value_bo...
type filtered_switch_branches = | Must_be_taken of Flambda . t | Can_be_taken of ( int * Flambda . t ) list
let reference_recursive_function_directly env closure_id = let closure_id = Closure_id . unwrap closure_id in match E . find_opt env closure_id with | None -> None | Some approx -> Some ( Flambda . Expr ( Var closure_id ) , approx )
let simplify_project_closure env r ( ~ project_closure : Flambda . project_closure ) : Flambda . named * R . t = simplify_free_variable_named env project_closure . set_of_closures ~ f ( : fun _env set_of_closures set_of_closures_approx -> match A . check_approx_for_set_of_closures set_of_closur...
let simplify_move_within_set_of_closures env r ( ~ move_within_set_of_closures : Flambda . move_within_set_of_closures ) : Flambda . named * R . t = simplify_free_variable_named env move_within_set_of_closures . closure ~ f ( : fun _env closure closure_approx -> match A . check_approx_for_closu...
let rec simplify_project_var env r ( ~ project_var : Flambda . project_var ) : Flambda . named * R . t = simplify_free_variable_named env project_var . closure ~ f ( : fun _env closure approx -> match A . check_approx_for_closure_allowing_unresolved approx with | Ok ( value_closure , _set_...
let constant_defining_value_approx env ( constant_defining_value : Flambda . constant_defining_value ) = match constant_defining_value with | Allocated_const const -> approx_for_allocated_const const | Block ( tag , fields ) -> let fields = List . map ( function | Flambda . Symbol sym -> begi...
let define_let_rec_symbol_approx orig_env defs = let init_env = List . fold_left ( fun building_env ( symbol , _ ) -> E . add_symbol building_env symbol ( A . value_unresolved ( Symbol symbol ) ) ) orig_env defs in let rec loop times lookup_env = if times <= 0 then lookup_env else let env...
let simplify_constant_defining_value env r symbol ( constant_defining_value : Flambda . constant_defining_value ) = let r , constant_defining_value , approx = match constant_defining_value with | Allocated_const const -> r , constant_defining_value , approx_for_allocated_const const | Block ( tag ...
let rec simplify_program_body env r ( program : Flambda . program_body ) : Flambda . program_body * R . t = match program with | Let_rec_symbol ( defs , program ) -> let set_of_closures_defs , other_defs = List . partition ( function | ( _ , Flambda . Set_of_closures _ ) -> true...
let simplify_program env r ( program : Flambda . program ) = let env , r = Symbol . Set . fold ( fun symbol ( env , r ) -> let env , approx = match E . find_symbol_exn env symbol with | exception Not_found -> let module Backend = ( val ( E . backend env ) : Backend_intf . S ) ...
let add_predef_exns_to_environment ~ env ~ backend = let module Backend = ( val backend : Backend_intf . S ) in List . fold_left ( fun env predef_exn -> assert ( Ident . is_predef predef_exn ) ; let symbol = Backend . symbol_for_global ' predef_exn in let name = Ident . name predef_exn i...
let run ~ never_inline ~ backend ~ prefixname ~ round ~ ppf_dump program = let r = R . create ( ) in let report = ! Clflags . inlining_report in if never_inline then Clflags . inlining_report := false ; let initial_env = add_predef_exns_to_environment ~ env ( : E . create ~ never_inline ~...
module Env = struct type scope = Current | Outer type t = { backend : ( module Backend_intf . S ) ; round : int ; ppf_dump : Format . formatter ; approx : ( scope * Simple_value_approx . t ) Variable . Map . t ; approx_mutable : Simple_value_approx . t Mutable_variable . Map . ...
let initial_inlining_threshold ~ round : Inlining_cost . Threshold . t = let unscaled = Clflags . Float_arg_helper . get ~ key : round ! Clflags . inline_threshold in Can_inline_if_no_larger_than ( int_of_float ( unscaled . * float_of_int Inlining_cost . scale_inline_threshold_by ) )
let initial_inlining_toplevel_threshold ~ round : Inlining_cost . Threshold . t = let ordinary_threshold = Clflags . Float_arg_helper . get ~ key : round ! Clflags . inline_threshold in let toplevel_threshold = Clflags . Int_arg_helper . get ~ key : round ! Clflags . inline_toplevel_threshold...
module Result = struct type t = { approx : Simple_value_approx . t ; used_static_exceptions : Static_exception . Set . t ; inlining_threshold : Inlining_cost . Threshold . t option ; benefit : Inlining_cost . Benefit . t ; num_direct_applications : int ; may_use_region : bool ; } l...
let keep_body_check ~ is_classic_mode ~ recursive = if not is_classic_mode then begin fun _ _ -> true end else begin let can_inline_non_rec_function ( fun_decl : Flambda . function_declaration ) = let inlining_threshold = initial_inlining_threshold ~ round : 0 in let bonus = Flambda_utils . functio...
let prepare_to_simplify_set_of_closures ~ env ( ~ set_of_closures : Flambda . set_of_closures ) ~ function_decls ~ freshen ( ~ only_for_function_decl : Flambda . function_declaration option ) = let free_vars = Variable . Map . map ( fun ( external_var : Flambda . specialised_to ) -> l...
let populate_closure_approximations ( ~ function_decl : Flambda . function_declaration ) ( ~ free_vars : ( _ * A . t ) Variable . Map . t ) ( ~ parameter_approximations : A . t Variable . Map . t ) ~ set_of_closures_env = let env = Variable . Map . fold ( fun id ( _ , ...
let prepare_to_simplify_closure ( ~ function_decl : Flambda . function_declaration ) ~ free_vars ~ specialised_args ~ parameter_approximations ~ set_of_closures_env = let closure_env = populate_closure_approximations ~ function_decl ~ free_vars ~ parameter_approximations ~ set_of_closures_env in let a...
type t = | Always_inline | Available_inline | Never_inline | Unroll of int | Default_inline
let [ @ ocamlformat " disable " ] print ppf t = let fprintf = Format . fprintf in match t with | Always_inline -> fprintf ppf " Always_inline " | Available_inline -> fprintf ppf " Available_inline " | Never_inline -> fprintf ppf " Never_inline " | Unroll n -> fprintf ppf " [ ( @ ...
let equal t1 t2 = match t1 , t2 with | Always_inline , Always_inline | Available_inline , Available_inline | Never_inline , Never_inline | Default_inline , Default_inline -> true | Unroll n1 , Unroll n2 -> n1 = n2 | ( ( Always_inline | Available_inline | Never_inline | Unroll _ | Default_...
let is_default t = match t with | Default_inline -> true | Always_inline | Available_inline | Never_inline | Unroll _ -> false
let number_of_unrolls = function | Unroll n -> n | Always_inline | Available_inline | Never_inline | Default_inline -> 0
let from_lambda ( attr : Lambda . inline_attribute ) = match attr with | Always_inline -> Always_inline | Never_inline -> Never_inline | Available_inline -> Available_inline | Unroll i -> Unroll i | Default_inline -> Default_inline
let f x = raise ( Failure " test " ) + 1
let g x = f x + 1
let h x = print_int ( g x ) ; print_endline " h "
let i x = if h x = ( ) then ( )
let ( ) = let open Printexc in try i ( ) with _ -> let trace = get_raw_backtrace ( ) in let print_slot slot = let x = convert_raw_backtrace_slot slot in let is_raise = Slot . is_raise x in let is_inline = Slot . is_inline x in let location = match Slot . location x with | None -> " < unk...
module Args = struct type t = { max_inlining_depth : int ; max_rec_depth : int ; call_cost : float ; alloc_cost : float ; prim_cost : float ; branch_cost : float ; indirect_call_cost : float ; poly_compare_cost : float ; small_function_size : int ; large_function_size : int ; threshold...
let [ @ ocamlformat " disable " ] print ppf = Args . print ppf
let max_inlining_depth t = t . Args . max_inlining_depth
let call_cost t = t . Args . call_cost
let alloc_cost t = t . Args . alloc_cost
let prim_cost t = t . Args . prim_cost
let branch_cost t = t . Args . branch_cost
let indirect_call_cost t = t . Args . indirect_call_cost
let poly_compare_cost t = t . Args . poly_compare_cost
let small_function_size t = t . Args . small_function_size
let large_function_size t = t . Args . large_function_size
let threshold t = t . Args . threshold
let meet t1 t2 = if Args . ( <= ) t1 t2 then t1 else if Args . ( <= ) t2 t1 then t2 else Args . meet t1 t2
let create ~ round = Args . create ~ round
let equal t1 t2 = Args . equal t1 t2
let prim_size ( prim : Clambda_primitives . primitive ) args = match prim with | Pmakeblock _ -> 5 + List . length args | Pfield _ -> 1 | Psetfield ( _ , isptr , init ) -> begin match init with | Root_initialization -> 1 | Assignment _ | Heap_initialization -> match isptr with | Po...
let lambda_smaller ' lam ~ than : threshold = let size = ref 0 in let rec lambda_size ( lam : Flambda . t ) = if ! size > threshold then raise Exit ; match lam with | Var _ -> ( ) | Apply ( { func = _ ; args = _ ; probe = None ; kind = direct } ) -> let call_cost = match...
let lambda_size lam = match lambda_smaller ' lam ~ than : max_int with | Some size -> size | None -> assert false