Skip to content

Commit fb2093e

Browse files
cknittclaude
andauthored
Store an application's location directly in Lambda.lambda_apply (#8736)
Removing the @inlined attribute left ap_info as a record with a single field, ap_loc. Store the location in lambda_apply instead, and pass it to Lambda.apply as a plain Location.t, like prim takes its location. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 97067ca commit fb2093e

11 files changed

Lines changed: 34 additions & 43 deletions

‎compiler/core/lam_beta_reduce_util.ml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ let simple_beta_reduce params body args =
9494
(Lvar _ | Lprim {primitive = Pfield _; args = [Lglobal_module _]}) as
9595
f;
9696
ap_args;
97-
ap_info;
97+
ap_loc;
9898
} -> (
9999
let () =
100100
List.iter2
@@ -114,7 +114,7 @@ let simple_beta_reduce params body args =
114114
| _ -> f
115115
in
116116
let result =
117-
Hash_ident.fold param_hash (Lambda.apply f new_args ap_info)
117+
Hash_ident.fold param_hash (Lambda.apply f new_args ap_loc)
118118
(fun _param stat acc ->
119119
let {lambda; used} = stat in
120120
if not used then Lambda.seq lambda acc else acc)

‎compiler/core/lam_bounded_vars.ml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ let rewrite (map : _ Hash_ident.t) (lam : Lambda.t) : Lambda.t =
118118
(* here it makes sure that global vars are not rebound *)
119119
Lambda.prim ~primitive ~args:(Ext_list.map args aux) loc
120120
| Lglobal_module _ -> lam
121-
| Lapply {ap_func; ap_args; ap_info; ap_transformed_jsx} ->
121+
| Lapply {ap_func; ap_args; ap_loc; ap_transformed_jsx} ->
122122
let fn = aux ap_func in
123123
let args = Ext_list.map ap_args aux in
124-
Lambda.apply ~ap_transformed_jsx fn args ap_info
124+
Lambda.apply ~ap_transformed_jsx fn args ap_loc
125125
| Lswitch
126126
( l,
127127
{

‎compiler/core/lam_compile.ml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ let with_source_loc loc (exp : J.expression) =
3434

3535
let rec source_loc_of_lam (lam : Lambda.t) =
3636
match lam with
37-
| Lapply {ap_info = {ap_loc}} -> Some ap_loc
37+
| Lapply {ap_loc} -> Some ap_loc
3838
| Lprim {loc} | Lfunction {loc} -> Some loc
3939
| Llet (_, _, arg, body) -> (
4040
match source_loc_of_lam arg with
@@ -356,7 +356,7 @@ let compile output_prefix =
356356
appinfo)
357357
fn args
358358
in
359-
let expression = with_source_loc appinfo.ap_info.ap_loc expression in
359+
let expression = with_source_loc appinfo.ap_loc expression in
360360
Js_output.output_of_block_and_expression lambda_cxt.continuation args_code
361361
expression
362362
(*
@@ -1606,7 +1606,7 @@ let compile output_prefix =
16061606
| _ ->
16071607
Js_output.output_of_block_and_expression lambda_cxt.continuation
16081608
args_code
1609-
(with_source_loc appinfo.ap_info.ap_loc
1609+
(with_source_loc appinfo.ap_loc
16101610
(E.call
16111611
~info:
16121612
(call_info_of_apply lambda_cxt.meta appinfo.ap_transformed_jsx

‎compiler/core/lam_pass_remove_alias.ml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t =
130130
_;
131131
} as l1;
132132
ap_args = args;
133-
ap_info;
133+
ap_loc;
134134
} -> (
135135
match Lam_compile_env.query_external_id_info ident fld_name with
136136
| {
@@ -151,22 +151,22 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t =
151151
let fn = simpl l1 in
152152
let args' = Ext_list.map_sharing args simpl in
153153
if fn == l1 && args' == args then lam
154-
else Lambda.apply fn args' ap_info ?ap_transformed_jsx:None)
154+
else Lambda.apply fn args' ap_loc ?ap_transformed_jsx:None)
155155
(* Function inlining interact with other optimizations...
156156
157157
- parameter attributes
158158
- scope issues
159159
- code bloat
160160
*)
161161
| Lapply
162-
{ap_func = Lvar v as fn; ap_args = args; ap_info; ap_transformed_jsx}
162+
{ap_func = Lvar v as fn; ap_args = args; ap_loc; ap_transformed_jsx}
163163
-> (
164164
(* Check info for always inlining *)
165165
let ap_args = Ext_list.map_sharing args simpl in
166166
let[@local] normal () =
167167
let fn' = simpl fn in
168168
if fn' == fn && ap_args == args then lam
169-
else Lambda.apply fn' ap_args ap_info ~ap_transformed_jsx
169+
else Lambda.apply fn' ap_args ap_loc ~ap_transformed_jsx
170170
in
171171
match Hash_ident.find_opt meta.ident_tbl v with
172172
| Some

‎compiler/core/lam_util.ml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ let refine_let ?original ~kind param (arg : Lambda.t) (l : Lambda.t) : Lambda.t
105105
with `value`. This only happens for primitives that are pure and do not
106106
allocate new blocks, so evaluation order and side effects stay the same. *)
107107
Lambda.prim ~primitive ~args:[arg] loc
108-
| _, _, Lapply {ap_func = fn; ap_args = [Lvar w]; ap_info; ap_transformed_jsx}
108+
| _, _, Lapply {ap_func = fn; ap_args = [Lvar w]; ap_loc; ap_transformed_jsx}
109109
when Ident.same w param && not (Lam_hit.hit_variable param fn) ->
110110
(* For a function call such as `{ let x = value; someFn(x) }`, we can
111111
rewrite to `someFn(value)` as long as the callee does not capture `x`.
112112
This removes the temporary binding while preserving the call semantics. *)
113-
Lambda.apply fn [arg] ap_info ~ap_transformed_jsx
113+
Lambda.apply fn [arg] ap_loc ~ap_transformed_jsx
114114
| (Strict | StrictOpt), arg, _ when is_safe_to_alias arg ->
115115
(* `Strict` and `StrictOpt` bindings both evaluate the RHS immediately
116116
(with `StrictOpt` allowing later elimination if unused). When that RHS

‎compiler/ml/lambda.ml‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,10 @@ and lfunction = {
362362

363363
and prim_info = {primitive: primitive; args: t list; loc: Location.t}
364364

365-
and ap_info = {ap_loc: Location.t}
366-
367365
and lambda_apply = {
368366
ap_func: t;
369367
ap_args: t list;
370-
ap_info: ap_info;
368+
ap_loc: Location.t;
371369
ap_transformed_jsx: bool;
372370
}
373371

@@ -1074,7 +1072,7 @@ let prim ~primitive:(prim : primitive) ~args loc : t =
10741072
*)
10751073
| _ -> default ())
10761074

1077-
let rec apply ?(ap_transformed_jsx = false) fn args (ap_info : ap_info) : t =
1075+
let rec apply ?(ap_transformed_jsx = false) fn args (ap_loc : Location.t) : t =
10781076
match fn with
10791077
| Lfunction
10801078
{
@@ -1091,21 +1089,20 @@ let rec apply ?(ap_transformed_jsx = false) fn args (ap_info : ap_info) : t =
10911089
} -> (
10921090
match is_eta_conversion_exn params inner_args args with
10931091
| args ->
1094-
let loc = ap_info.ap_loc in
10951092
prim ~primitive:wrap
1096-
~args:[prim ~primitive:primitive_call.primitive ~args loc]
1097-
loc
1093+
~args:[prim ~primitive:primitive_call.primitive ~args ap_loc]
1094+
ap_loc
10981095
| exception Not_simple_form ->
1099-
Lapply {ap_func = fn; ap_args = args; ap_info; ap_transformed_jsx})
1096+
Lapply {ap_func = fn; ap_args = args; ap_loc; ap_transformed_jsx})
11001097
| Lfunction
11011098
{
11021099
params;
11031100
body = Lprim ({primitive = _; args = inner_args} as primitive_call);
11041101
} -> (
11051102
match is_eta_conversion_exn params inner_args args with
1106-
| args -> prim ~primitive:primitive_call.primitive ~args ap_info.ap_loc
1103+
| args -> prim ~primitive:primitive_call.primitive ~args ap_loc
11071104
| exception _ ->
1108-
Lapply {ap_func = fn; ap_args = args; ap_info; ap_transformed_jsx})
1105+
Lapply {ap_func = fn; ap_args = args; ap_loc; ap_transformed_jsx})
11091106
| Lfunction
11101107
{
11111108
params;
@@ -1115,20 +1112,19 @@ let rec apply ?(ap_transformed_jsx = false) fn args (ap_info : ap_info) : t =
11151112
(Lconst _ as const) );
11161113
} -> (
11171114
match is_eta_conversion_exn params inner_args args with
1118-
| args ->
1119-
seq (prim ~primitive:primitive_call.primitive ~args ap_info.ap_loc) const
1115+
| args -> seq (prim ~primitive:primitive_call.primitive ~args ap_loc) const
11201116
| exception _ ->
1121-
Lapply {ap_func = fn; ap_args = args; ap_info; ap_transformed_jsx}
1117+
Lapply {ap_func = fn; ap_args = args; ap_loc; ap_transformed_jsx}
11221118
(* | Lfunction {params;body} when Ext_list.same_length params args ->
11231119
Ext_list.fold_right2 (fun p arg acc ->
11241120
Llet(Strict,p,arg,acc)
11251121
) params args body *)
11261122
(* TODO: more rigirous analysis on [let_kind] *))
11271123
| Llet (kind, id, e, (Lfunction _ as fn)) ->
1128-
let_ kind id e (apply fn args ap_info ~ap_transformed_jsx)
1124+
let_ kind id e (apply fn args ap_loc ~ap_transformed_jsx)
11291125
(* | Llet (kind0, id0, e0, Llet (kind,id, e, (Lfunction _ as fn))) ->
11301126
Llet(kind0,id0,e0,Llet (kind, id, e, apply fn args loc status)) *)
1131-
| _ -> Lapply {ap_func = fn; ap_args = args; ap_info; ap_transformed_jsx}
1127+
| _ -> Lapply {ap_func = fn; ap_args = args; ap_loc; ap_transformed_jsx}
11321128

11331129
let not_ loc x : t =
11341130
match x with

‎compiler/ml/lambda.mli‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,12 +375,10 @@ and lfunction = {
375375

376376
and prim_info = private {primitive: primitive; args: t list; loc: Location.t}
377377

378-
and ap_info = {ap_loc: Location.t}
379-
380378
and lambda_apply = private {
381379
ap_func: t;
382380
ap_args: t list;
383-
ap_info: ap_info;
381+
ap_loc: Location.t;
384382
ap_transformed_jsx: bool;
385383
}
386384

@@ -456,7 +454,7 @@ val global_module : Ident.t -> t
456454

457455
val const : structured_constant -> t
458456

459-
val apply : ?ap_transformed_jsx:bool -> t -> t list -> ap_info -> t
457+
val apply : ?ap_transformed_jsx:bool -> t -> t list -> Location.t -> t
460458

461459
val function_ :
462460
loc:Location.t ->

‎compiler/ml/lambda_traverse.ml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ let shallow_map_sharing (f : t -> t) (lam : t) : t =
4040
let fn = f ap.ap_func in
4141
let args = Ext_list.map_sharing ap.ap_args f in
4242
if fn == ap.ap_func && args == ap.ap_args then lam
43-
else apply fn args ap.ap_info ~ap_transformed_jsx:ap.ap_transformed_jsx
43+
else apply fn args ap.ap_loc ~ap_transformed_jsx:ap.ap_transformed_jsx
4444
| Lfunction {params; body; attr; loc} ->
4545
let body' = f body in
4646
if body' == body then lam else function_ ~loc ~attr ~params ~body:body'
@@ -124,7 +124,7 @@ let make_key e =
124124
| Lglobal_module _ | Lconst _ -> e
125125
| Lapply ap ->
126126
apply ~ap_transformed_jsx:ap.ap_transformed_jsx (tr_rec env ap.ap_func)
127-
(tr_recs env ap.ap_args) {ap_loc = Location.none}
127+
(tr_recs env ap.ap_args) Location.none
128128
| Llet (Alias, x, ex, e) ->
129129
(* Ignore aliases -> substitute *)
130130
let ex = tr_rec env ex in

‎compiler/ml/translcore.ml‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ let wrap_exn loc arg =
909909
~args:
910910
[global_module (Ident.create_persistent Primitive_modules.exceptions)]
911911
loc)
912-
[arg] {ap_loc = loc}
912+
[arg] loc
913913
let exception_id_destructed (l : Lambda.t) (fv : Ident.t) : bool =
914914
let rec hit_opt = function
915915
| None -> false
@@ -1314,7 +1314,7 @@ and transl_cases cases = List.map transl_case cases
13141314
and transl_apply ?(uncurried_partial_application = None)
13151315
?(transformed_jsx = false) lam sargs loc =
13161316
let lapply ap_func ap_args =
1317-
apply ~ap_transformed_jsx:transformed_jsx ap_func ap_args {ap_loc = loc}
1317+
apply ~ap_transformed_jsx:transformed_jsx ap_func ap_args loc
13181318
in
13191319
let rec build_apply lam args = function
13201320
| (None, optional) :: l ->
@@ -1364,9 +1364,7 @@ and transl_apply ?(uncurried_partial_application = None)
13641364
in
13651365
let extra_args = Ext_list.map extra_ids (fun id -> var id) in
13661366
let ap_args = args @ extra_args in
1367-
let l0 =
1368-
apply ~ap_transformed_jsx:transformed_jsx lam ap_args {ap_loc = loc}
1369-
in
1367+
let l0 = apply ~ap_transformed_jsx:transformed_jsx lam ap_args loc in
13701368
function_ ~loc ~attr:default_function_attribute
13711369
~params:(List.rev_append !none_ids extra_ids)
13721370
~body:l0

‎compiler/ml/translmod.ml‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ and apply_coercion_result loc strict funct param arg cc_res =
125125
~params:[param]
126126
~body:
127127
(apply_coercion loc Strict cc_res
128-
(Lambda.apply ~ap_transformed_jsx:false (Lambda.var id) [arg]
129-
{ap_loc = loc})))
128+
(Lambda.apply ~ap_transformed_jsx:false (Lambda.var id) [arg] loc)))
130129

131130
and wrap_id_pos_list loc id_pos_list get_field lam =
132131
let fv = Lambda_traverse.free_variables lam in
@@ -289,7 +288,7 @@ and transl_module0 cc rootpath mexp =
289288
(Lambda.apply ~ap_transformed_jsx:false
290289
(transl_module Tcoerce_none None funct)
291290
[transl_module ccarg None arg]
292-
{ap_loc = loc})
291+
loc)
293292
| Tmod_constraint (arg, _, _, ccarg) ->
294293
transl_module (compose_coercions cc ccarg) rootpath arg
295294
| Tmod_unpack (arg, _) ->

0 commit comments

Comments
 (0)