@@ -87,6 +87,7 @@ let tupled_function_call_stub original_params unboxed_version ~closure_bound_var
8787 redundancy here (func is also unboxed_version) *)
8888 kind = Direct (Closure_id. wrap unboxed_version);
8989 dbg = Debuginfo. none;
90+ position = Apply_nontail ;
9091 inline = Default_inline ;
9192 specialise = Default_specialise ;
9293 })
@@ -99,8 +100,10 @@ let tupled_function_call_stub original_params unboxed_version ~closure_bound_var
99100 pos + 1 , Flambda. create_let param lam body)
100101 (0 , call) params
101102 in
102- let tuple_param = Parameter. wrap tuple_param_var in
103- Flambda. create_function_declaration ~params: [tuple_param]
103+ (* Tupled functions are always Alloc_heap. See translcore.ml *)
104+ let alloc_mode = Lambda. Alloc_heap in
105+ let tuple_param = Parameter. wrap tuple_param_var alloc_mode in
106+ Flambda. create_function_declaration ~params: [tuple_param] ~alloc_mode
104107 ~body ~stub: true ~dbg: Debuginfo. none ~inline: Default_inline
105108 ~specialise: Default_specialise ~is_a_functor: false
106109 ~closure_origin: (Closure_origin. create (Closure_id. wrap closure_bound_var))
@@ -207,15 +210,15 @@ let rec close t env (lam : Lambda.lambda) : Flambda.t =
207210 initial_value = var;
208211 body;
209212 contents_kind = block_kind })
210- | Lfunction { kind; params; body; attr; loc; (* FIXME mode *) } ->
213+ | Lfunction { kind; params; body; attr; loc; mode } ->
211214 let name = Names. anon_fn_with_loc loc in
212215 let closure_bound_var = Variable. create name in
213216 (* CR-soon mshinwell: some of this is now very similar to the let rec case
214217 below *)
215218 let set_of_closures_var = Variable. create Names. set_of_closures in
216219 let set_of_closures =
217220 let decl =
218- Function_decl. create ~let_rec_ident: None ~closure_bound_var ~kind
221+ Function_decl. create ~let_rec_ident: None ~closure_bound_var ~kind ~mode
219222 ~params: (List. map fst params) ~body ~attr ~loc
220223 in
221224 close_functions t env (Function_decls. create [decl])
@@ -228,7 +231,7 @@ let rec close t env (lam : Lambda.lambda) : Flambda.t =
228231 Flambda. create_let set_of_closures_var set_of_closures
229232 (name_expr (Project_closure (project_closure)) ~name )
230233 | Lapply { ap_func; ap_args; ap_loc;
231- ap_tailcall = _; ap_inlined; ap_specialised; } ->
234+ ap_tailcall = _; ap_inlined; ap_specialised; ap_position } ->
232235 Lift_code. lifting_helper (close_list t env ap_args)
233236 ~evaluation_order: `Right_to_left
234237 ~name: Names. apply_arg
@@ -241,6 +244,7 @@ let rec close t env (lam : Lambda.lambda) : Flambda.t =
241244 args;
242245 kind = Indirect ;
243246 dbg = Debuginfo. from_location ap_loc;
247+ position = ap_position;
244248 inline = ap_inlined;
245249 specialise = ap_specialised;
246250 })))
@@ -255,13 +259,13 @@ let rec close t env (lam : Lambda.lambda) : Flambda.t =
255259 will be named after the corresponding identifier in the [let rec]. *)
256260 List. map (function
257261 | (let_rec_ident,
258- Lambda. Lfunction { kind; params; body; attr; loc (* FIXME mode *) }) ->
262+ Lambda. Lfunction { kind; params; body; attr; loc; mode }) ->
259263 let closure_bound_var =
260264 Variable. create_with_same_name_as_ident let_rec_ident
261265 in
262266 let function_declaration =
263267 Function_decl. create ~let_rec_ident: (Some let_rec_ident)
264- ~closure_bound_var ~kind ~params: (List. map fst params) ~body
268+ ~closure_bound_var ~kind ~mode ~ params: (List. map fst params) ~body
265269 ~attr ~loc
266270 in
267271 Some function_declaration
@@ -312,7 +316,7 @@ let rec close t env (lam : Lambda.lambda) : Flambda.t =
312316 in
313317 Let_rec (defs, close t env body)
314318 end
315- | Lsend (kind , meth , obj , args , _FIXME , loc ) ->
319+ | Lsend (kind , meth , obj , args , position , loc ) ->
316320 let meth_var = Variable. create Names. meth in
317321 let obj_var = Variable. create Names. obj in
318322 let dbg = Debuginfo. from_location loc in
@@ -322,7 +326,7 @@ let rec close t env (lam : Lambda.lambda) : Flambda.t =
322326 ~evaluation_order: `Right_to_left
323327 ~name: Names. send_arg
324328 ~create_body: (fun args ->
325- Send { kind; meth = meth_var; obj = obj_var; args; dbg; })))
329+ Send { kind; meth = meth_var; obj = obj_var; args; dbg; position })))
326330 | Lprim ((Pdivint Safe | Pmodint Safe
327331 | Pdivbint { is_safe = Safe } | Pmodbint { is_safe = Safe }) as prim,
328332 [arg1; arg2], loc)
@@ -568,8 +572,8 @@ let rec close t env (lam : Lambda.lambda) : Flambda.t =
568572 or by completely removing it (replacing by unit). *)
569573 Misc. fatal_error " [Lifused] should have been removed by \
570574 [Simplif.simplify_lets]"
571- | Lregion _ ->
572- Misc. fatal_error " FIXME: Lregion unimplemented in Flambda "
575+ | Lregion body ->
576+ Region (close t env body)
573577
574578(* * Perform closure conversion on a set of function declarations, returning a
575579 set of closures. (The set will often only contain a single function;
@@ -600,22 +604,34 @@ and close_functions t external_env function_declarations : Flambda.named =
600604 not marked as stub but certainly should *)
601605 let stub = Function_decl. stub decl in
602606 let param_vars = List. map (Env. find_var closure_env) params in
603- let params = List. map Parameter. wrap param_vars in
607+ let nheap =
608+ match Function_decl. mode decl, Function_decl. kind decl with
609+ | _ , Curried {nlocal} -> List. length params - nlocal
610+ | Alloc_heap , Tupled -> List. length params
611+ | Alloc_local , Tupled -> 0
612+ in
613+ let params = List. mapi (fun i v ->
614+ let alloc_mode : Lambda.alloc_mode =
615+ if i < nheap then Alloc_heap else Alloc_local in
616+ Parameter. wrap v alloc_mode) param_vars
617+ in
604618 let closure_bound_var = Function_decl. closure_bound_var decl in
605619 let unboxed_version = Variable. rename closure_bound_var in
606620 let body = close t closure_env body in
607621 let closure_origin =
608622 Closure_origin. create (Closure_id. wrap unboxed_version)
609623 in
610624 let fun_decl =
611- Flambda. create_function_declaration ~params ~body ~stub ~dbg
625+ Flambda. create_function_declaration
626+ ~params ~alloc_mode: (Function_decl. mode decl)
627+ ~body ~stub ~dbg
612628 ~inline: (Function_decl. inline decl)
613629 ~specialise: (Function_decl. specialise decl)
614630 ~is_a_functor: (Function_decl. is_a_functor decl)
615631 ~closure_origin
616632 in
617633 match Function_decl. kind decl with
618- | Curried _ (* FIXME nlocal *) ->
634+ | Curried _ ->
619635 Variable.Map. add closure_bound_var fun_decl map
620636 | Tupled ->
621637 let unboxed_version = Variable. rename closure_bound_var in
@@ -661,12 +677,12 @@ and close_list t sb l = List.map (close t sb) l
661677and close_let_bound_expression t ?let_rec_ident let_bound_var env
662678 (lam : Lambda.lambda ) : Flambda.named =
663679 match lam with
664- | Lfunction { kind; params; body; attr; loc; } ->
680+ | Lfunction { kind; params; body; attr; loc; mode } ->
665681 (* Ensure that [let] and [let rec]-bound functions have appropriate
666682 names. *)
667683 let closure_bound_var = Variable. rename let_bound_var in
668684 let decl =
669- Function_decl. create ~let_rec_ident ~closure_bound_var ~kind
685+ Function_decl. create ~let_rec_ident ~closure_bound_var ~kind ~mode
670686 ~params: (List. map fst params) ~body ~attr ~loc
671687 in
672688 let set_of_closures_var = Variable. rename let_bound_var in
0 commit comments