Skip to content

Commit d14988d

Browse files
committed
propagate uids from typed tree through lambda
1 parent de413fd commit d14988d

30 files changed

+836
-446
lines changed

bytecomp/bytegen.ml

+6-4
Original file line numberDiff line numberDiff line change
@@ -875,8 +875,9 @@ and comp_expr stack_info env exp sz cont =
875875
Stack.push to_compile functions_to_compile;
876876
comp_args stack_info env (List.map (fun n -> Lvar n) fv) sz
877877
(Kclosure(lbl, List.length fv) :: cont)
878-
| Llet(_, _k, id, arg, body)
879-
| Lmutlet(_k, id, arg, body) ->
878+
| Llet(_, _k, id, _duid, arg, body)
879+
| Lmutlet(_k, id, _duid, arg, body) ->
880+
(* We are intentionally dropping the [debug_uid] identifiers here. *)
880881
comp_expr stack_info env arg sz
881882
(Kpush :: comp_expr stack_info (add_var id (sz+1) env) body (sz+1)
882883
(add_pop 1 cont))
@@ -1158,7 +1159,7 @@ and comp_expr stack_info env exp sz cont =
11581159
comp_args stack_info env args sz
11591160
(comp_primitive stack_info p (sz + nargs - 1) args :: cont)
11601161
| Lstaticcatch (body, (i, vars) , handler, _, _) ->
1161-
let vars = List.map fst vars in
1162+
let vars = List.map fst3 vars in
11621163
let nvars = List.length vars in
11631164
let branch1, cont1 = make_branch cont in
11641165
let r =
@@ -1202,7 +1203,8 @@ and comp_expr stack_info env exp sz cont =
12021203
comp_expr stack_info env arg sz cont
12031204
| _ -> comp_exit_args stack_info env args sz size cont
12041205
end
1205-
| Ltrywith(body, id, handler, _kind) ->
1206+
| Ltrywith(body, id, _duid, handler, _kind) ->
1207+
(* We are intentionally dropping the [debug_uid] identifiers here. *)
12061208
let (branch1, cont1) = make_branch cont in
12071209
let lbl_handler = new_label() in
12081210
let body_cont =

lambda/lambda.ml

+72-58
Original file line numberDiff line numberDiff line change
@@ -802,8 +802,12 @@ type parameter_attribute = {
802802
unbox_param: bool;
803803
}
804804

805+
type debug_uid = Shape.Uid.t
806+
let debug_uid_none = Shape.Uid.internal_not_actually_unique
807+
805808
type lparam = {
806809
name : Ident.t;
810+
debug_uid : debug_uid;
807811
layout : layout;
808812
attributes : parameter_attribute;
809813
mode : locality_mode
@@ -819,18 +823,18 @@ type lambda =
819823
| Lconst of structured_constant
820824
| Lapply of lambda_apply
821825
| Lfunction of lfunction
822-
| Llet of let_kind * layout * Ident.t * lambda * lambda
823-
| Lmutlet of layout * Ident.t * lambda * lambda
826+
| Llet of let_kind * layout * Ident.t * debug_uid * lambda * lambda
827+
| Lmutlet of layout * Ident.t * debug_uid * lambda * lambda
824828
| Lletrec of rec_binding list * lambda
825829
| Lprim of primitive * lambda list * scoped_location
826830
| Lswitch of lambda * lambda_switch * scoped_location * layout
827831
| Lstringswitch of
828832
lambda * (string * lambda) list * lambda option * scoped_location * layout
829833
| Lstaticraise of static_label * lambda list
830834
| Lstaticcatch of
831-
lambda * (static_label * (Ident.t * layout) list) * lambda
835+
lambda * (static_label * (Ident.t * debug_uid * layout) list) * lambda
832836
* pop_region * layout
833-
| Ltrywith of lambda * Ident.t * lambda * layout
837+
| Ltrywith of lambda * Ident.t * debug_uid * lambda * layout
834838
| Lifthenelse of lambda * lambda * lambda * layout
835839
| Lsequence of lambda * lambda
836840
| Lwhile of lambda_while
@@ -846,6 +850,7 @@ type lambda =
846850

847851
and rec_binding = {
848852
id : Ident.t;
853+
debug_uid : debug_uid;
849854
def : lfunction;
850855
}
851856

@@ -867,6 +872,7 @@ and lambda_while =
867872

868873
and lambda_for =
869874
{ for_id : Ident.t;
875+
for_debug_uid : debug_uid;
870876
for_loc : scoped_location;
871877
for_from : lambda;
872878
for_to : lambda;
@@ -1092,20 +1098,20 @@ let make_key e =
10921098
Lapply {ap with ap_func = tr_rec env ap.ap_func;
10931099
ap_args = tr_recs env ap.ap_args;
10941100
ap_loc = Loc_unknown}
1095-
| Llet (Alias,_k,x,ex,e) -> (* Ignore aliases -> substitute *)
1101+
| Llet (Alias,_k,x,_x_duid,ex,e) -> (* Ignore aliases -> substitute *)
10961102
let ex = tr_rec env ex in
10971103
tr_rec (Ident.add x ex env) e
1098-
| Llet ((Strict | StrictOpt),_k,x,ex,Lvar v) when Ident.same v x ->
1104+
| Llet ((Strict | StrictOpt),_k,x,_x_duid,ex,Lvar v) when Ident.same v x ->
10991105
tr_rec env ex
1100-
| Llet (str,k,x,ex,e) ->
1106+
| Llet (str,k,x,x_duid,ex,e) ->
11011107
(* Because of side effects, keep other lets with normalized names *)
11021108
let ex = tr_rec env ex in
11031109
let y = make_key x in
1104-
Llet (str,k,y,ex,tr_rec (Ident.add x (Lvar y) env) e)
1105-
| Lmutlet (k,x,ex,e) ->
1110+
Llet (str,k,y,x_duid,ex,tr_rec (Ident.add x (Lvar y) env) e)
1111+
| Lmutlet (k,x,x_duid,ex,e) ->
11061112
let ex = tr_rec env ex in
11071113
let y = make_key x in
1108-
Lmutlet (k,y,ex,tr_rec (Ident.add x (Lmutvar y) env) e)
1114+
Lmutlet (k,y,x_duid,ex,tr_rec (Ident.add x (Lmutvar y) env) e)
11091115
| Lprim (p,es,_) ->
11101116
Lprim (p,tr_recs env es, Loc_unknown)
11111117
| Lswitch (e,sw,loc,kind) ->
@@ -1120,8 +1126,8 @@ let make_key e =
11201126
Lstaticraise (i,tr_recs env es)
11211127
| Lstaticcatch (e1,xs,e2, r, kind) ->
11221128
Lstaticcatch (tr_rec env e1,xs,tr_rec env e2, r, kind)
1123-
| Ltrywith (e1,x,e2,kind) ->
1124-
Ltrywith (tr_rec env e1,x,tr_rec env e2,kind)
1129+
| Ltrywith (e1,x,x_duid,e2,kind) ->
1130+
Ltrywith (tr_rec env e1,x,x_duid,tr_rec env e2,kind)
11251131
| Lifthenelse (cond,ifso,ifnot,kind) ->
11261132
Lifthenelse (tr_rec env cond,tr_rec env ifso,tr_rec env ifnot,kind)
11271133
| Lsequence (e1,e2) ->
@@ -1163,7 +1169,8 @@ let name_lambda strict arg layout fn =
11631169
Lvar id -> fn id
11641170
| _ ->
11651171
let id = Ident.create_local "let" in
1166-
Llet(strict, layout, id, arg, fn id)
1172+
let id_debug_uid = debug_uid_none in
1173+
Llet(strict, layout, id, id_debug_uid, arg, fn id)
11671174

11681175
let name_lambda_list args fn =
11691176
let rec name_list names = function
@@ -1172,7 +1179,8 @@ let name_lambda_list args fn =
11721179
name_list (arg :: names) rem
11731180
| (arg, layout) :: rem ->
11741181
let id = Ident.create_local "let" in
1175-
Llet(Strict, layout, id, arg, name_list (Lvar id :: names) rem) in
1182+
let id_debug_uid = debug_uid_none in
1183+
Llet(Strict, layout, id, id_debug_uid, arg, name_list (Lvar id :: names) rem) in
11761184
name_list [] args
11771185

11781186

@@ -1188,8 +1196,8 @@ let shallow_iter ~tail ~non_tail:f = function
11881196
f fn; List.iter f args
11891197
| Lfunction{body} ->
11901198
f body
1191-
| Llet(_, _k, _id, arg, body)
1192-
| Lmutlet(_k, _id, arg, body) ->
1199+
| Llet(_, _k, _id, _duid, arg, body)
1200+
| Lmutlet(_k, _id, _duid, arg, body) ->
11931201
f arg; tail body
11941202
| Lletrec(decl, body) ->
11951203
tail body;
@@ -1213,7 +1221,7 @@ let shallow_iter ~tail ~non_tail:f = function
12131221
List.iter f args
12141222
| Lstaticcatch(e1, _, e2, _, _kind) ->
12151223
tail e1; tail e2
1216-
| Ltrywith(e1, _, e2,_) ->
1224+
| Ltrywith(e1, _, _, e2,_) ->
12171225
f e1; tail e2
12181226
| Lifthenelse(e1, e2, e3,_) ->
12191227
f e1; tail e2; tail e3
@@ -1248,8 +1256,8 @@ let rec free_variables = function
12481256
| Lfunction{body; params} ->
12491257
Ident.Set.diff (free_variables body)
12501258
(Ident.Set.of_list (List.map (fun p -> p.name) params))
1251-
| Llet(_, _k, id, arg, body)
1252-
| Lmutlet(_k, id, arg, body) ->
1259+
| Llet(_, _k, id, _duid, arg, body)
1260+
| Lmutlet(_k, id, _duid, arg, body) ->
12531261
Ident.Set.union
12541262
(free_variables arg)
12551263
(Ident.Set.remove id (free_variables body))
@@ -1288,9 +1296,9 @@ let rec free_variables = function
12881296
Ident.Set.union
12891297
(Ident.Set.diff
12901298
(free_variables handler)
1291-
(Ident.Set.of_list (List.map fst params)))
1299+
(Ident.Set.of_list (List.map fst3 params)))
12921300
(free_variables body)
1293-
| Ltrywith(body, param, handler, _) ->
1301+
| Ltrywith(body, param, _duid, handler, _) ->
12941302
Ident.Set.union
12951303
(Ident.Set.remove
12961304
param
@@ -1340,15 +1348,15 @@ let staticfail = Lstaticraise (0,[])
13401348

13411349
let rec is_guarded = function
13421350
| Lifthenelse(_cond, _body, Lstaticraise (0,[]),_) -> true
1343-
| Llet(_str, _k, _id, _lam, body) -> is_guarded body
1351+
| Llet(_str, _k, _id, _duid, _lam, body) -> is_guarded body
13441352
| Levent(lam, _ev) -> is_guarded lam
13451353
| _ -> false
13461354

13471355
let rec patch_guarded patch = function
13481356
| Lifthenelse (cond, body, Lstaticraise (0,[]), kind) ->
13491357
Lifthenelse (cond, body, patch, kind)
1350-
| Llet(str, k, id, lam, body) ->
1351-
Llet (str, k, id, lam, patch_guarded patch body)
1358+
| Llet(str, k, id, duid, lam, body) ->
1359+
Llet (str, k, id, duid, lam, patch_guarded patch body)
13521360
| Levent(lam, ev) ->
13531361
Levent (patch_guarded patch lam, ev)
13541362
| _ -> fatal_error "Lambda.patch_guarded"
@@ -1445,26 +1453,29 @@ let build_substs update_env ?(freshen_bound_variables = false) s =
14451453
[l] with all the bound variables of the input term in the current
14461454
scope, mapped to either themselves or freshened versions of
14471455
themselves when [freshen_bound_variables] is set. *)
1448-
let bind id l =
1456+
let bind id duid l =
14491457
let id' = if not freshen_bound_variables then id else Ident.rename id in
1450-
id', Ident.Map.add id id' l
1458+
(* CR sspies: If [freshen_bound_variables] is set, this code duplicates
1459+
the debug uids. [freshen_bound_variables] is currently only set by
1460+
[duplicate] below, which is called from [tmc.ml]. *)
1461+
id', duid, Ident.Map.add id id' l
14511462
in
14521463
let bind_many ids l =
1453-
List.fold_right (fun (id, rhs) (ids', l) ->
1454-
let id', l = bind id l in
1455-
((id', rhs) :: ids' , l)
1464+
List.fold_right (fun (id, duid, rhs) (ids', l) ->
1465+
let id', duid', l = bind id duid l in
1466+
((id', duid', rhs) :: ids' , l)
14561467
) ids ([], l)
14571468
in
14581469
let bind_params params l =
1459-
List.fold_right (fun p (params', l) ->
1460-
let name', l = bind p.name l in
1461-
({ p with name = name' } :: params' , l)
1470+
List.fold_right (fun (p: lparam) (params', l) ->
1471+
let name', duid', l = bind p.name p.debug_uid l in
1472+
({ p with name = name'; debug_uid = duid' } :: params' , l)
14621473
) params ([], l)
14631474
in
14641475
let bind_rec ids l =
1465-
List.fold_right (fun rb (ids', l) ->
1466-
let id', l = bind rb.id l in
1467-
({ rb with id = id' } :: ids' , l)
1476+
List.fold_right (fun (rb: rec_binding) (ids', l) ->
1477+
let id', duid', l = bind rb.id rb.debug_uid l in
1478+
({ rb with id = id'; debug_uid = duid' } :: ids' , l)
14681479
) ids ([], l)
14691480
in
14701481
let rec subst s l lam =
@@ -1492,12 +1503,12 @@ let build_substs update_env ?(freshen_bound_variables = false) s =
14921503
ap_args = subst_list s l ap.ap_args}
14931504
| Lfunction lf ->
14941505
Lfunction (subst_lfun s l lf)
1495-
| Llet(str, k, id, arg, body) ->
1496-
let id, l' = bind id l in
1497-
Llet(str, k, id, subst s l arg, subst s l' body)
1498-
| Lmutlet(k, id, arg, body) ->
1499-
let id, l' = bind id l in
1500-
Lmutlet(k, id, subst s l arg, subst s l' body)
1506+
| Llet(str, k, id, duid, arg, body) ->
1507+
let id, duid, l' = bind id duid l in
1508+
Llet(str, k, id, duid, subst s l arg, subst s l' body)
1509+
| Lmutlet(k, id, duid, arg, body) ->
1510+
let id, duid, l' = bind id duid l in
1511+
Lmutlet(k, id, duid, subst s l arg, subst s l' body)
15011512
| Lletrec(decl, body) ->
15021513
let decl, l' = bind_rec decl l in
15031514
Lletrec(List.map (subst_decl s l') decl, subst s l' body)
@@ -1519,17 +1530,18 @@ let build_substs update_env ?(freshen_bound_variables = false) s =
15191530
let params, l' = bind_many params l in
15201531
Lstaticcatch(subst s l body, (id, params),
15211532
subst s l' handler, r, kind)
1522-
| Ltrywith(body, exn, handler,kind) ->
1523-
let exn, l' = bind exn l in
1524-
Ltrywith(subst s l body, exn, subst s l' handler,kind)
1533+
| Ltrywith(body, exn, duid, handler,kind) ->
1534+
let exn, duid, l' = bind exn duid l in
1535+
Ltrywith(subst s l body, exn, duid, subst s l' handler,kind)
15251536
| Lifthenelse(e1, e2, e3,kind) ->
15261537
Lifthenelse(subst s l e1, subst s l e2, subst s l e3,kind)
15271538
| Lsequence(e1, e2) -> Lsequence(subst s l e1, subst s l e2)
15281539
| Lwhile lw -> Lwhile { wh_cond = subst s l lw.wh_cond;
15291540
wh_body = subst s l lw.wh_body}
15301541
| Lfor lf ->
1531-
let for_id, l' = bind lf.for_id l in
1542+
let for_id, for_duid, l' = bind lf.for_id lf.for_debug_uid l in
15321543
Lfor {lf with for_id;
1544+
for_debug_uid = for_duid;
15331545
for_from = subst s l lf.for_from;
15341546
for_to = subst s l lf.for_to;
15351547
for_body = subst s l' lf.for_body}
@@ -1640,10 +1652,10 @@ let shallow_map ~tail ~non_tail:f = function
16401652
}
16411653
| Lfunction lfun ->
16421654
Lfunction (map_lfunction f lfun)
1643-
| Llet (str, layout, v, e1, e2) ->
1644-
Llet (str, layout, v, f e1, tail e2)
1645-
| Lmutlet (layout, v, e1, e2) ->
1646-
Lmutlet (layout, v, f e1, tail e2)
1655+
| Llet (str, layout, v, v_duid, e1, e2) ->
1656+
Llet (str, layout, v, v_duid, f e1, tail e2)
1657+
| Lmutlet (layout, v, v_duid, e1, e2) ->
1658+
Lmutlet (layout, v, v_duid, f e1, tail e2)
16471659
| Lletrec (idel, e2) ->
16481660
Lletrec
16491661
(List.map (fun rb ->
@@ -1674,8 +1686,8 @@ let shallow_map ~tail ~non_tail:f = function
16741686
Lstaticraise (i, List.map f args)
16751687
| Lstaticcatch (body, id, handler, r, layout) ->
16761688
Lstaticcatch (tail body, id, tail handler, r, layout)
1677-
| Ltrywith (e1, v, e2, layout) ->
1678-
Ltrywith (f e1, v, tail e2, layout)
1689+
| Ltrywith (e1, v, duid, e2, layout) ->
1690+
Ltrywith (f e1, v, duid, tail e2, layout)
16791691
| Lifthenelse (e1, e2, e3, layout) ->
16801692
Lifthenelse (f e1, tail e2, tail e3, layout)
16811693
| Lsequence (e1, e2) ->
@@ -1706,10 +1718,12 @@ let map f =
17061718

17071719
(* To let-bind expressions to variables *)
17081720

1709-
let bind_with_layout str (var, layout) exp body =
1721+
let bind_with_layout str (var, duid, layout) exp body =
17101722
match exp with
17111723
Lvar var' when Ident.same var var' -> body
1712-
| _ -> Llet(str, layout, var, exp, body)
1724+
(* CR sspies: This implicitly assumes that they have the same debug uid,
1725+
which is probably correct.*)
1726+
| _ -> Llet(str, layout, var, duid, exp, body)
17131727

17141728
let negate_integer_comparison = function
17151729
| Ceq -> Cne
@@ -2421,7 +2435,7 @@ let compute_expr_layout free_vars_kind lam =
24212435
| Lfunction _ -> layout_function
24222436
| Lapply { ap_result_layout; _ } -> ap_result_layout
24232437
| Lsend (_, _, _, _, _, _, _, layout) -> layout
2424-
| Llet(_, kind, id, _, body) | Lmutlet(kind, id, _, body) ->
2438+
| Llet(_, kind, id, _duid, _, body) | Lmutlet(kind, id, _duid, _, body) ->
24252439
compute_expr_layout (Ident.Map.add id kind kinds) body
24262440
| Lletrec(defs, body) ->
24272441
let kinds =
@@ -2432,7 +2446,7 @@ let compute_expr_layout free_vars_kind lam =
24322446
| Lprim(p, _, _) ->
24332447
primitive_result_layout p
24342448
| Lswitch(_, _, _, kind) | Lstringswitch(_, _, _, _, kind)
2435-
| Lstaticcatch(_, _, _, _, kind) | Ltrywith(_, _, _, kind)
2449+
| Lstaticcatch(_, _, _, _, kind) | Ltrywith(_, _, _, _, kind)
24362450
| Lifthenelse(_, _, _, kind) | Lregion (_, kind) ->
24372451
kind
24382452
| Lstaticraise (_, _) ->
@@ -2555,8 +2569,8 @@ let rec try_to_find_location lam =
25552569
| Lsend (_, _, _, _, _, _, loc, _)
25562570
| Levent (_, { lev_loc = loc; _ }) ->
25572571
loc
2558-
| Llet (_, _, _, lam, _)
2559-
| Lmutlet (_, _, lam, _)
2572+
| Llet (_, _, _, _, lam, _)
2573+
| Lmutlet (_, _, _, lam, _)
25602574
| Lifthenelse (lam, _, _, _)
25612575
| Lstaticcatch (lam, _, _, _, _)
25622576
| Lstaticraise (_, lam :: _)
@@ -2566,7 +2580,7 @@ let rec try_to_find_location lam =
25662580
| Lifused (_, lam)
25672581
| Lregion (lam, _)
25682582
| Lexclave lam
2569-
| Ltrywith (lam, _, _, _) ->
2583+
| Ltrywith (lam, _, _, _, _) ->
25702584
try_to_find_location lam
25712585
| Lvar _ | Lmutvar _ | Lconst _ | Lletrec _ | Lstaticraise (_, []) ->
25722586
Debuginfo.Scoped_location.Loc_unknown

0 commit comments

Comments
 (0)