text
stringlengths
0
601k
let make_const_char n = let name = Internal_variable_names . const_char in name_expr_fst ( make_const_char_named n ) ~ name
let make_const_ptr_named n : Flambda . named * t = Const ( Const_pointer n ) , value_constptr n
let make_const_ptr ( n : int ) = let name = match n with | 0 -> Internal_variable_names . const_ptr_zero | 1 -> Internal_variable_names . const_ptr_one | _ -> Internal_variable_names . const_ptr in name_expr_fst ( make_const_ptr_named n ) ~ name
let make_const_bool_named b : Flambda . named * t = make_const_ptr_named ( if b then 1 else 0 )
let make_const_bool b = name_expr_fst ( make_const_bool_named b ) ~ name : Internal_variable_names . const_bool
let make_const_float_named f : Flambda . named * t = Allocated_const ( Float f ) , value_float f
let make_const_float f = name_expr_fst ( make_const_float_named f ) ~ name : Internal_variable_names . const_float
let make_const_boxed_int_named ( type bi ) ( t : bi boxed_int ) ( i : bi ) : Flambda . named * t = let c : Allocated_const . t = match t with | Int32 -> Int32 i | Int64 -> Int64 i | Nativeint -> Nativeint i in Allocated_const c , value_boxed_int t i
let make_const_boxed_int t i = name_expr_fst ( make_const_boxed_int_named t i ) ~ name : Internal_variable_names . const_boxed_int
type simplification_summary = | Nothing_done | Replaced_term
type simplification_result = Flambda . t * simplification_summary * t
type simplification_result_named = Flambda . named * simplification_summary * t
let simplify t ( lam : Flambda . t ) : simplification_result = if Effect_analysis . no_effects lam then match t . descr with | Value_int n -> let const , approx = make_const_int n in const , Replaced_term , approx | Value_char n -> let const , approx = make_const_char n in const , Replaced_term , approx | Value_constpt...
let simplify_named t ( named : Flambda . named ) : simplification_result_named = if Effect_analysis . no_effects_named named then match t . descr with | Value_int n -> let const , approx = make_const_int_named n in const , Replaced_term , approx | Value_char n -> let const , approx = make_const_char_named n in const , ...
let simplify_var t : ( Flambda . named * t ) option = match t . descr with | Value_int n -> Some ( make_const_int_named n ) | Value_char n -> Some ( make_const_char_named n ) | Value_constptr n -> Some ( make_const_ptr_named n ) | Value_float ( Some f ) -> Some ( make_const_float_named f ) | Value_boxed_int ( t , i ) -...
let join_summaries summary ~ replaced_by_var_or_symbol = match replaced_by_var_or_symbol , summary with | true , Nothing_done | true , Replaced_term | false , Replaced_term -> Replaced_term | false , Nothing_done -> Nothing_done
let simplify_using_env t ~ is_present_in_env flam = let replaced_by_var_or_symbol , flam = match t . var with | Some var when is_present_in_env var -> true , Flambda . Var var | _ -> match t . symbol with | Some ( sym , None ) -> let name = Internal_variable_names . symbol in ( true , U . name_expr ( Symbol sym ) ~ nam...
let simplify_named_using_env t ~ is_present_in_env named = let replaced_by_var_or_symbol , named = match t . var with | Some var when is_present_in_env var -> true , Flambda . Expr ( Var var ) | _ -> match t . symbol with | Some ( sym , None ) -> true , ( Flambda . Symbol sym : Flambda . named ) | Some ( sym , Some fie...
let simplify_var_to_var_using_env t ~ is_present_in_env = match t . var with | Some var when is_present_in_env var -> Some var | _ -> None
let known t = match t . descr with | Value_unresolved _ | Value_unknown _ -> false | Value_string _ | Value_float_array _ | Value_bottom | Value_block _ | Value_int _ | Value_char _ | Value_constptr _ | Value_set_of_closures _ | Value_closure _ | Value_extern _ | Value_float _ | Value_boxed_int _ | Value_symbol _ -> tr...
let useful t = match t . descr with | Value_unresolved _ | Value_unknown _ | Value_bottom -> false | Value_string _ | Value_float_array _ | Value_block _ | Value_int _ | Value_char _ | Value_constptr _ | Value_set_of_closures _ | Value_float _ | Value_boxed_int _ | Value_closure _ | Value_extern _ | Value_symbol _ -> t...
let all_not_useful ts = List . for_all ( fun t -> not ( useful t ) ) ts
let warn_on_mutation t = match t . descr with | Value_block ( _ , fields ) -> Array . length fields > 0 | Value_string { contents = Some _ } | Value_int _ | Value_char _ | Value_constptr _ | Value_set_of_closures _ | Value_float _ | Value_boxed_int _ | Value_closure _ -> true | Value_string { contents = None } | Value_...
type get_field_result = | Ok of t | Unreachable
let get_field t ~ field_index : i : get_field_result = match t . descr with | Value_block ( _tag , fields ) -> if i >= 0 && i < Array . length fields then begin Ok fields . ( i ) end else begin Unreachable end | Value_bottom | Value_int _ | Value_char _ | Value_constptr _ -> Ok value_bottom | Value_float_array _ -> Ok ...
type checked_approx_for_block = | Wrong | Ok of Tag . t * t array
let check_approx_for_block t = match t . descr with | Value_block ( tag , fields ) -> Ok ( tag , fields ) | Value_bottom | Value_int _ | Value_char _ | Value_constptr _ | Value_float_array _ | Value_string _ | Value_float _ | Value_boxed_int _ | Value_set_of_closures _ | Value_closure _ | Value_symbol _ | Value_extern ...
let descrs approxs = List . map ( fun v -> v . descr ) approxs
let equal_boxed_int ( type t1 ) ( type t2 ) ( bi1 : t1 boxed_int ) ( i1 : t1 ) ( bi2 : t2 boxed_int ) ( i2 : t2 ) = match bi1 , bi2 with | Int32 , Int32 -> Int32 . equal i1 i2 | Int64 , Int64 -> Int64 . equal i1 i2 | Nativeint , Nativeint -> Nativeint . equal i1 i2 | _ -> false
let equal_floats f1 f2 = match f1 , f2 with | None , None -> true | None , Some _ | Some _ , None -> false | Some f1 , Some f2 -> Allocated_const . compare_floats f1 f2 = 0
let rec meet_descr ~ really_import_approx d1 d2 = match d1 , d2 with | Value_int i , Value_int j when i = j -> d1 | Value_constptr i , Value_constptr j when i = j -> d1 | Value_symbol s1 , Value_symbol s2 when Symbol . equal s1 s2 -> d1 | Value_extern e1 , Value_extern e2 when Export_id . equal e1 e2 -> d1 | Value_floa...
let freshen_and_check_closure_id ( value_set_of_closures : value_set_of_closures ) closure_id = let closure_id = Freshening . Project_var . apply_closure_id value_set_of_closures . freshening closure_id in try ignore ( Variable . Map . find ( Closure_id . unwrap closure_id ) value_set_of_closures . function_decls . fun...
type checked_approx_for_set_of_closures = | Wrong | Unresolved of unresolved_value | Unknown | Unknown_because_of_unresolved_value of unresolved_value | Ok of Variable . t option * value_set_of_closures
let check_approx_for_set_of_closures t : checked_approx_for_set_of_closures = match t . descr with | Value_unresolved value -> Unresolved value | Value_unknown ( Unresolved_value value ) -> Unknown_because_of_unresolved_value value | Value_set_of_closures value_set_of_closures -> Ok ( t . var , value_set_of_closures ) ...
type strict_checked_approx_for_set_of_closures = | Wrong | Ok of Variable . t option * value_set_of_closures
let strict_check_approx_for_set_of_closures t : strict_checked_approx_for_set_of_closures = match check_approx_for_set_of_closures t with | Ok ( var , value_set_of_closures ) -> Ok ( var , value_set_of_closures ) | Wrong | Unresolved _ | Unknown | Unknown_because_of_unresolved_value _ -> Wrong
type checked_approx_for_closure_allowing_unresolved = | Wrong | Unresolved of unresolved_value | Unknown | Unknown_because_of_unresolved_value of unresolved_value | Ok of value_closure * Variable . t option * Symbol . t option * value_set_of_closures
let check_approx_for_closure_allowing_unresolved t : checked_approx_for_closure_allowing_unresolved = match t . descr with | Value_closure value_closure -> begin match value_closure . set_of_closures . descr with | Value_set_of_closures value_set_of_closures -> let symbol = match value_closure . set_of_closures . symbo...
type checked_approx_for_closure = | Wrong | Ok of value_closure * Variable . t option * Symbol . t option * value_set_of_closures
let check_approx_for_closure t : checked_approx_for_closure = match check_approx_for_closure_allowing_unresolved t with | Ok ( value_closure , set_of_closures_var , set_of_closures_symbol , value_set_of_closures ) -> Ok ( value_closure , set_of_closures_var , set_of_closures_symbol , value_set_of_closures ) | Wrong | U...
let approx_for_bound_var value_set_of_closures var = try Var_within_closure . Map . find var value_set_of_closures . bound_vars with | Not_found -> Misc . fatal_errorf " The set - of - closures approximation % a @ does not \ bind the variable % a . @% s . " @ print_value_set_of_closures value_set_of_closures Var_within...
let check_approx_for_float t : float option = match t . descr with | Value_float f -> f | Value_unresolved _ | Value_unknown _ | Value_string _ | Value_float_array _ | Value_bottom | Value_block _ | Value_int _ | Value_char _ | Value_constptr _ | Value_set_of_closures _ | Value_closure _ | Value_extern _ | Value_boxed_...
let float_array_as_constant ( t : value_float_array ) : float list option = match t . contents with | Unknown_or_mutable -> None | Contents contents -> Array . fold_right ( fun elt acc -> match acc , elt . descr with | Some acc , Value_float ( Some f ) -> Some ( f :: acc ) | None , _ | Some _ , ( Value_float None | Val...
let check_approx_for_string t : string option = match t . descr with | Value_string { contents } -> contents | Value_float _ | Value_unresolved _ | Value_unknown _ | Value_float_array _ | Value_bottom | Value_block _ | Value_int _ | Value_char _ | Value_constptr _ | Value_set_of_closures _ | Value_closure _ | Value_ext...
type switch_branch_selection = | Cannot_be_taken | Can_be_taken | Must_be_taken
let potentially_taken_const_switch_branch t branch = match t . descr with | Value_unresolved _ | Value_unknown _ | Value_extern _ | Value_symbol _ -> Can_be_taken | Value_constptr i | Value_int i when i = branch -> Must_be_taken | Value_char c when Char . code c = branch -> Must_be_taken | Value_constptr _ | Value_int ...
let potentially_taken_block_switch_branch t tag = match t . descr with | ( Value_unresolved _ | Value_unknown _ | Value_extern _ | Value_symbol _ ) -> Can_be_taken | ( Value_constptr _ | Value_int _ | Value_char _ ) -> Cannot_be_taken | Value_block ( block_tag , _ ) when Tag . to_int block_tag = tag -> Must_be_taken | ...
let function_arity ( fun_decl : function_declaration ) = List . length fun_decl . params
let function_declaration_approx ~ keep_body fun_var ( fun_decl : Flambda . function_declaration ) = let function_body = if not ( keep_body fun_var fun_decl ) then None else begin Some { body = fun_decl . body ; stub = fun_decl . stub ; inline = fun_decl . inline ; dbg = fun_decl . dbg ; specialise = fun_decl . speciali...
let function_declarations_approx ~ keep_body ( fun_decls : Flambda . function_declarations ) = let funs = Variable . Map . mapi ( function_declaration_approx ~ keep_body ) fun_decls . funs in { funs ; is_classic_mode = fun_decls . is_classic_mode ; set_of_closures_id = fun_decls . set_of_closures_id ; set_of_closures_o...
let import_function_declarations_for_pack function_decls import_set_of_closures_id import_set_of_closures_origin = { set_of_closures_id = import_set_of_closures_id function_decls . set_of_closures_id ; set_of_closures_origin = import_set_of_closures_origin function_decls . set_of_closures_origin ; funs = function_decls...
let update_function_declarations function_decls ~ funs = let compilation_unit = Compilation_unit . get_current_exn ( ) in let is_classic_mode = function_decls . is_classic_mode in let set_of_closures_id = Set_of_closures_id . create compilation_unit in let set_of_closures_origin = function_decls . set_of_closures_origi...
let clear_function_bodies ( function_decls : function_declarations ) = let funs = Variable . Map . map ( fun ( fun_decl : function_declaration ) -> match fun_decl . function_body with | None | Some { stub = true ; _ } -> fun_decl | Some _ -> { fun_decl with function_body = None } ) function_decls . funs in { function_d...
let update_function_declaration_body ( function_decl : function_declaration ) ( f : Flambda . t -> Flambda . t ) = match function_decl . function_body with | None -> function_decl | Some function_body -> let new_function_body = let body = f function_body . body in let free_variables = Flambda . free_variables body in l...
let make_closure_map input = let map = ref Closure_id . Map . empty in let add_set_of_closures _ ( function_decls : function_declarations ) = Variable . Map . iter ( fun var _ -> let closure_id = Closure_id . wrap var in map := Closure_id . Map . add closure_id function_decls ! map ) function_decls . funs in Set_of_clo...
module Examples = struct let two = Sum ( Unit , Unit ) ; ; let not = Comp ( ( Pair ( Iden , Unit ) ) , ( Case ( Injr Unit , Injl Unit ) ) ) ; ; let halfadder = Case ( Drop ( Pair ( Injl ( Unit ) , Iden ) ) , Drop ( Pair ( Iden , not ) ) ) ; ; let fulladder1 = Comp ( Pair ( Take halfadder , Drop Iden ) , Comp ( Pair ( T...
type unit0 = | Tt ; ;
type ( ' a , ' b ) sum = ; ;
type ( ' a , ' b ) prod = | Pair of ' a * ' b ; ;
let fst = function | Pair ( x , _ ) -> x ; ;
let snd = function | Pair ( _ , y ) -> y ; ;
let rec eval x a = match x with | Iden -> a | Comp ( s , t ) -> eval t ( eval s a ) | Unit -> Obj . magic Tt | Injl ( t ) -> Obj . magic ( Inl ( eval t a ) ) | Injr ( t ) -> Obj . magic ( Inr ( eval t a ) ) | Case ( s , t ) -> let Pair ( ab , c0 ) = Obj . magic a in ( match ab with | Inl a1 -> eval s ( Obj . magic ( Pa...
module Examples = struct let two = Sum ( Unit , Unit ) ; ; let not = Comp ( ( Pair0 ( Iden , Unit ) ) , ( Case ( Injr Unit , Injl Unit ) ) ) ; ; let halfadder = Case ( Drop ( Pair0 ( Injl ( Unit ) , Iden ) ) , Drop ( Pair0 ( Iden , not ) ) ) ; ; let fulladder1 = Comp ( Pair0 ( Take halfadder , Drop Iden ) , Comp ( Pair...
let rec eliminate_ref id = function Lvar v as lam -> if Ident . same v id then raise Real_reference else lam | Lconst _ as lam -> lam | Lapply ap -> Lapply { ap with ap_func = eliminate_ref id ap . ap_func ; ap_args = List . map ( eliminate_ref id ) ap . ap_args } | Lfunction _ as lam -> if Ident . Set . mem id ( free_...
type exit = { mutable count : int ; mutable max_depth : int ; }
let simplify_exits lam = let exits = Hashtbl . create 17 in let try_depth = ref 0 in let get_exit i = try Hashtbl . find exits i with Not_found -> { count = 0 ; max_depth = 0 } and incr_exit i nb d = match Hashtbl . find_opt exits i with | Some r -> r . count <- r . count + nb ; r . max_depth <- max r . max_depth d | N...
let exact_application { kind ; params ; _ } args = match kind with | Curried -> if List . length params <> List . length args then None else Some args | Tupled -> begin match args with | [ Lprim ( Pmakeblock _ , tupled_args , _ ) ] -> if List . length params <> List . length tupled_args then None else Some tupled_args ...
let beta_reduce params body args = List . fold_left2 ( fun l ( param , kind ) arg -> Llet ( Strict , kind , param , arg , l ) ) body params args
let simplify_lets lam = let optimize = ! Clflags . native_code || not ! Clflags . debug in let occ = ( Hashtbl . create 83 : ( Ident . t , int ref ) Hashtbl . t ) in let count_var v = try ( ! Hashtbl . find occ v ) with Not_found -> 0 and bind_var bv v = let r = ref 0 in Hashtbl . add occ v r ; Ident . Map . add v r bv...
let is_tail_native_heuristic : ( int -> bool ) ref = ref ( fun _ -> true )
let rec emit_tail_infos is_tail lambda = let call_kind args = if is_tail && ( ( not ! Clflags . native_code ) || ( ! is_tail_native_heuristic ( List . length args ) ) ) then Annot . Tail else Annot . Stack in match lambda with | Lvar _ -> ( ) | Lconst _ -> ( ) | Lapply ap -> if ap . ap_should_be_tailcall && not is_tail...
let split_default_wrapper ~ id : fun_id ~ kind ~ params ~ return ~ body ~ attr ~ loc = let rec aux map = function | Llet ( Strict , k , id , ( Lifthenelse ( Lvar optparam , _ , _ ) as def ) , rest ) when Ident . name optparam = " * opt " * && List . mem_assoc optparam params && not ( List . mem_assoc optparam map ) -> ...
type slot = { func : lfunction ; mutable scope : lambda option ; }
module LamTbl = Hashtbl . Make ( struct type t = lambda let equal = ( ) == let hash = Hashtbl . hash end )
let simplify_local_functions lam = let slots = Hashtbl . create 16 in let static_id = Hashtbl . create 16 in let static = LamTbl . create 16 in let current_scope = ref lam in let check_static lf = if lf . attr . local = Always_local then Location . prerr_warning lf . loc ( Warnings . Inlining_impossible " This function...
let simplify_lambda lam = let lam = lam |> ( if ! Clflags . native_code || not ! Clflags . debug then simplify_local_functions else Fun . id ) |> simplify_exits |> simplify_lets in if ! Clflags . annotations || Warnings . is_active Warnings . Expect_tailcall then emit_tail_infos true lam ; lam
module Field_type = struct type t = | Time_per_run | Minor_words_per_run | Major_words_per_run | Promoted_words_per_run [ @@ deriving compare ] let equal = [ % compare . equal : t ] let to_string = function | Time_per_run -> " time_per_run_nanos " | Minor_words_per_run -> " minor_words_per_run " | Major_words_per_run -...
module Result = struct type t = { benchmark_name : string ; benchmark_name_with_index : string ; full_benchmark_name : string ; dup_id : int option ; file_name : string ; module_name : string ; library_name : string ; version : string ; hg_revision : string option ; hg_active_bookmark : string option ; x_library_inlini...
module Results = struct type t = Result . t list [ @@ deriving sexp ] end
let extract ( ? hostname = " < unknown host " ) > ( ? libname = " " ) ( results : Analysis_result . t list ) = let get_bench_name_with_index res = let name = Analysis_result . name res in match String . lsplit2 ~ on ' ] ' : name with | Some ( _ , subname ) when String . is_prefix subname ~ prefix " : " -> String . drop...
let to_sexp ? hostname ? libname results = extract ? hostname ? libname results |> Results . sexp_of_t ; ;
module Free_variables = struct include Univ_map . Make ( Univ_map . Type_id_key ) ( struct type ' a t = unit [ @@ deriving sexp_of ] end ) let merge a b = List . fold ( to_alist b ) ~ init : a ~ f ( : fun acc ( T ( id , ( ) ) ) -> set acc ~ key : id ~ data ( ) ) : ; ; end
module Env = Univ_map . Make ( Univ_map . Type_id_key ) ( struct type ' a t = ' a [ @@ deriving sexp_of ] end )
module Option_or_miss = struct type ' a t = | None | Some of ' a | Miss of { free : Free_variables . t ; gen : Env . t -> ' a } let squash = function | None -> None | Some a -> Some a | Miss { free ; gen } when Free_variables . is_empty free -> Some ( gen Env . empty ) | other -> other ; ; let map a ~ f = match a with ...
let rec value_to_function : type key data result . result Value . t -> key Type_equal . Id . t -> data Type_equal . Id . t -> ( key -> data -> result ) Option_or_miss . t = fun value key_id data_id -> let open Option_or_miss in match value . value with | Constant ( r , _ ) -> Some ( fun _key _data -> r ) | Incr _ -> No...
let rec computation_to_function : type key data model dynamic_action static_action result . ( model , dynamic_action , static_action , result ) Computation . t -> key_id : key Type_equal . Id . t -> data_id : data Type_equal . Id . t -> ( Path . t -> key -> data -> result ) Option_or_miss . t = fun computation ~ key_id...
let computation_to_function t ~ key_compare ~ key_id ~ data_id = let make_path_element = Path . Elem . keyed ~ compare : key_compare key_id |> unstage in match computation_to_function t ~ key_id ~ data_id |> Option_or_miss . squash with | Some f -> Option . some ( fun path key data -> let path = Path . append path ( As...
module Simplify_boxed_integer_operator ( I : sig type t val kind : Lambda . boxed_integer val zero : t val add : t -> t -> t val sub : t -> t -> t val mul : t -> t -> t val div : t -> t -> t val rem : t -> t -> t val logand : t -> t -> t val logor : t -> t -> t val logxor : t -> t -> t val shift_left : t -> int -> t va...
module Simplify_boxed_nativeint = Simplify_boxed_integer_operator ( struct include Nativeint let to_int64 = Int64 . of_nativeint let swap = S . swapnative let kind = Lambda . Pnativeint end )
module Simplify_boxed_int32 = Simplify_boxed_integer_operator ( struct include Int32 let to_int32 i = i let to_int64 = Int64 . of_int32 let swap = S . swap32 let kind = Lambda . Pint32 end )
module Simplify_boxed_int64 = Simplify_boxed_integer_operator ( struct include Int64 let to_int64 i = i let swap = S . swap64 let kind = Lambda . Pint64 end )
let const_int_expr expr n = if Effect_analysis . no_effects_named expr then let ( new_expr , approx ) = A . make_const_int_named n in new_expr , approx , C . Benefit . remove_code_named expr C . Benefit . zero else expr , A . value_int n , C . Benefit . zero
let const_char_expr expr c = if Effect_analysis . no_effects_named expr then let ( new_expr , approx ) = A . make_const_char_named c in new_expr , approx , C . Benefit . remove_code_named expr C . Benefit . zero else expr , A . value_char c , C . Benefit . zero
let const_ptr_expr expr n = if Effect_analysis . no_effects_named expr then let ( new_expr , approx ) = A . make_const_ptr_named n in new_expr , approx , C . Benefit . remove_code_named expr C . Benefit . zero else expr , A . value_constptr n , C . Benefit . zero
let const_bool_expr expr b = const_int_expr expr ( if b then 1 else 0 )
let const_float_expr expr f = if Effect_analysis . no_effects_named expr then let ( new_expr , approx ) = A . make_const_float_named f in new_expr , approx , C . Benefit . remove_code_named expr C . Benefit . zero else expr , A . value_float f , C . Benefit . zero
let const_boxed_int_expr expr t i = if Effect_analysis . no_effects_named expr then let ( new_expr , approx ) = A . make_const_boxed_int_named t i in new_expr , approx , C . Benefit . remove_code_named expr C . Benefit . zero else expr , A . value_boxed_int t i , C . Benefit . zero
let const_integer_comparison_expr expr ( cmp : Lambda . integer_comparison ) x y = let open ! Stdlib in const_bool_expr expr ( match cmp with | Ceq -> x = y | Cne -> x <> y | Clt -> x < y | Cgt -> x > y | Cle -> x <= y | Cge -> x >= y )
let const_float_comparison_expr expr ( cmp : Lambda . float_comparison ) x y = let open ! Stdlib in const_bool_expr expr ( match cmp with | CFeq -> x = y | CFneq -> not ( x = y ) | CFlt -> x < y | CFnlt -> not ( x < y ) | CFgt -> x > y | CFngt -> not ( x > y ) | CFle -> x <= y | CFnle -> not ( x <= y ) | CFge -> x >= y...
let rec remove_leading_underscores s = if s = " " then " x " else if s . [ 0 ] <> ' _ ' then s else String . sub s 1 ( String . length s - 1 ) |> remove_leading_underscores