Skip to content

Commit ac1ef36

Browse files
committed
DWARF inlined frames: Debuginfo.t and Flambda 2
1 parent de74a55 commit ac1ef36

17 files changed

+331
-68
lines changed

middle_end/flambda2/from_lambda/closure_conversion.ml

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@ module Inlining = struct
322322
~are_rebuilding_terms decision;
323323
res
324324

325-
let make_inlined_body acc ~callee ~region_inlined_into ~params ~args
326-
~my_closure ~my_region ~my_depth ~body ~free_names_of_body
325+
let make_inlined_body acc ~callee ~called_code_id ~region_inlined_into ~params
326+
~args ~my_closure ~my_region ~my_depth ~body ~free_names_of_body
327327
~exn_continuation ~return_continuation ~apply_exn_continuation
328328
~apply_return_continuation ~apply_depth ~apply_dbg =
329329
let rec_info =
@@ -355,16 +355,20 @@ module Inlining = struct
355355
acc, Expr.apply_renaming body renaming
356356
in
357357
let acc, body =
358-
Inlining_helpers.make_inlined_body ~callee ~region_inlined_into ~params
359-
~args ~my_closure ~my_region ~my_depth ~rec_info ~body:(acc, body)
360-
~exn_continuation ~return_continuation ~apply_exn_continuation
361-
~apply_return_continuation ~bind_params ~bind_depth ~apply_renaming
358+
Inlining_helpers.make_inlined_body ~callee ~called_code_id
359+
~region_inlined_into ~params ~args ~my_closure ~my_region ~my_depth
360+
~rec_info ~body:(acc, body) ~exn_continuation ~return_continuation
361+
~apply_exn_continuation ~apply_return_continuation ~bind_params
362+
~bind_depth ~apply_renaming
363+
in
364+
let inlined_debuginfo =
365+
Inlined_debuginfo.create ~called_code_id ~apply_dbg
362366
in
363367
Let_with_acc.create acc
364368
(Bound_pattern.singleton
365369
(VB.create (Variable.create "inlined_dbg") Name_mode.normal))
366370
(Named.create_prim
367-
(Nullary (Enter_inlined_apply { dbg = apply_dbg }))
371+
(Nullary (Enter_inlined_apply { dbg = inlined_debuginfo }))
368372
Debuginfo.none)
369373
~body
370374

@@ -421,7 +425,8 @@ module Inlining = struct
421425
| Known free_names -> free_names
422426
in
423427
let make_inlined_body =
424-
make_inlined_body ~callee ~region_inlined_into
428+
make_inlined_body ~callee ~called_code_id:(Code.code_id code)
429+
~region_inlined_into
425430
~params:(Bound_parameters.vars params)
426431
~args ~my_closure ~my_region ~my_depth ~body ~free_names_of_body
427432
~exn_continuation ~return_continuation ~apply_depth ~apply_dbg

middle_end/flambda2/simplify/env/downwards_env.ml

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type get_imported_code = unit -> Exported_code.t
3131
type t =
3232
{ round : int;
3333
typing_env : TE.t;
34-
inlined_debuginfo : Debuginfo.t;
34+
inlined_debuginfo : Inlined_debuginfo.t;
3535
can_inline : bool;
3636
inlining_state : Inlining_state.t;
3737
propagating_float_consts : bool;
@@ -49,11 +49,6 @@ type t =
4949
loopify_state : Loopify_state.t
5050
}
5151

52-
let print_debuginfo ppf dbg =
53-
if Debuginfo.is_none dbg
54-
then Format.pp_print_string ppf "None"
55-
else Debuginfo.print_compact ppf dbg
56-
5752
let [@ocamlformat "disable"] print ppf { round; typing_env;
5853
inlined_debuginfo; can_inline;
5954
inlining_state; propagating_float_consts;
@@ -84,7 +79,7 @@ let [@ocamlformat "disable"] print ppf { round; typing_env;
8479
)@]"
8580
round
8681
TE.print typing_env
87-
print_debuginfo inlined_debuginfo
82+
Inlined_debuginfo.print inlined_debuginfo
8883
can_inline
8984
Inlining_state.print inlining_state
9085
propagating_float_consts
@@ -112,7 +107,7 @@ let create ~round ~(resolver : resolver)
112107
in
113108
{ round;
114109
typing_env;
115-
inlined_debuginfo = Debuginfo.none;
110+
inlined_debuginfo = Inlined_debuginfo.none;
116111
can_inline = true;
117112
inlining_state = Inlining_state.default ~round;
118113
propagating_float_consts;
@@ -193,7 +188,7 @@ let enter_set_of_closures
193188
} =
194189
{ round;
195190
typing_env = TE.closure_env typing_env;
196-
inlined_debuginfo = Debuginfo.none;
191+
inlined_debuginfo = Inlined_debuginfo.none;
197192
can_inline;
198193
inlining_state;
199194
propagating_float_consts;
@@ -461,12 +456,6 @@ let define_code t ~code_id ~code =
461456
let all_code = Code_id.Map.add code_id code t.all_code in
462457
{ t with typing_env; all_code }
463458

464-
let set_inlined_debuginfo t dbg = { t with inlined_debuginfo = dbg }
465-
466-
let get_inlined_debuginfo t = t.inlined_debuginfo
467-
468-
let add_inlined_debuginfo t dbg = Debuginfo.inline t.inlined_debuginfo dbg
469-
470459
let cse t = t.cse
471460

472461
let comparison_results t = t.comparison_results
@@ -519,6 +508,14 @@ let set_inlining_arguments arguments t =
519508
inlining_state = Inlining_state.with_arguments arguments t.inlining_state
520509
}
521510

511+
let set_inlined_debuginfo t ~from =
512+
{ t with inlined_debuginfo = from.inlined_debuginfo }
513+
514+
let set_inlined_debuginfo' t inlined_debuginfo = { t with inlined_debuginfo }
515+
516+
let add_inlined_debuginfo t dbg =
517+
Inlined_debuginfo.rewrite t.inlined_debuginfo dbg
518+
522519
let enter_inlined_apply ~called_code ~apply ~was_inline_always t =
523520
let arguments =
524521
Inlining_state.arguments t.inlining_state
@@ -543,8 +540,12 @@ let enter_inlined_apply ~called_code ~apply ~was_inline_always t =
543540
in
544541
Inlining_state.increment_depth t.inlining_state ~by)
545542
in
543+
let inlined_debuginfo =
544+
Inlined_debuginfo.create ~called_code_id:(Code.code_id called_code)
545+
~apply_dbg:(Apply.dbg apply)
546+
in
546547
{ t with
547-
inlined_debuginfo = Apply.dbg apply;
548+
inlined_debuginfo;
548549
inlining_state;
549550
inlining_history_tracker =
550551
Inlining_history.Tracker.enter_inlined_apply

middle_end/flambda2/simplify/env/downwards_env.mli

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,11 @@ val mem_code : t -> Code_id.t -> bool
147147
(** This function raises if the code ID is unbound. *)
148148
val find_code_exn : t -> Code_id.t -> Code_or_metadata.t
149149

150-
val set_inlined_debuginfo : t -> Debuginfo.t -> t
150+
val set_inlined_debuginfo : t -> from:t -> t
151151

152-
val add_inlined_debuginfo : t -> Debuginfo.t -> Debuginfo.t
152+
val set_inlined_debuginfo' : t -> Inlined_debuginfo.t -> t
153153

154-
val get_inlined_debuginfo : t -> Debuginfo.t
154+
val add_inlined_debuginfo : t -> Debuginfo.t -> Debuginfo.t
155155

156156
val round : t -> int
157157

middle_end/flambda2/simplify/inlining/inlining_transforms.ml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ module DE = Downwards_env
2020
module FT = Flambda2_types.Function_type
2121
module VB = Bound_var
2222

23-
let make_inlined_body ~callee ~unroll_to ~params ~args ~my_closure ~my_region
24-
~my_depth ~rec_info ~body ~exn_continuation ~return_continuation
25-
~apply_exn_continuation ~apply_return_continuation =
23+
let make_inlined_body ~callee ~called_code_id ~unroll_to ~params ~args
24+
~my_closure ~my_region ~my_depth ~rec_info ~body ~exn_continuation
25+
~return_continuation ~apply_exn_continuation ~apply_return_continuation =
2626
let callee, rec_info =
2727
match callee with
2828
| None ->
@@ -68,10 +68,10 @@ let make_inlined_body ~callee ~unroll_to ~params ~args ~my_closure ~my_region
6868
~body ~free_names_of_body:Unknown
6969
|> Expr.create_let
7070
in
71-
Inlining_helpers.make_inlined_body ~callee ~params ~args ~my_closure
72-
~my_region ~my_depth ~rec_info ~body ~exn_continuation ~return_continuation
73-
~apply_exn_continuation ~apply_return_continuation ~bind_params ~bind_depth
74-
~apply_renaming:Expr.apply_renaming
71+
Inlining_helpers.make_inlined_body ~callee ~called_code_id ~params ~args
72+
~my_closure ~my_region ~my_depth ~rec_info ~body ~exn_continuation
73+
~return_continuation ~apply_exn_continuation ~apply_return_continuation
74+
~bind_params ~bind_depth ~apply_renaming:Expr.apply_renaming
7575

7676
let wrap_inlined_body_for_exn_extra_args ~extra_args ~apply_exn_continuation
7777
~apply_return_continuation ~result_arity ~make_inlined_body =
@@ -143,7 +143,8 @@ let inline dacc ~apply ~unroll_to ~was_inline_always function_decl =
143143
~free_names_of_body:_
144144
->
145145
let make_inlined_body () =
146-
make_inlined_body ~callee ~region_inlined_into ~unroll_to
146+
make_inlined_body ~callee ~called_code_id:(Code.code_id code)
147+
~region_inlined_into ~unroll_to
147148
~params:(Bound_parameters.to_list params)
148149
~args ~my_closure ~my_region ~my_depth ~rec_info ~body
149150
~exn_continuation ~return_continuation

middle_end/flambda2/simplify/join_points.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,7 @@ let compute_handler_env ?cut_after uses ~is_recursive ~env_at_fork
223223
(DE.inlining_history_tracker env_at_fork)
224224
handler_env
225225
in
226-
let handler_env =
227-
DE.set_inlined_debuginfo handler_env
228-
(DE.get_inlined_debuginfo env_at_fork)
229-
in
226+
let handler_env = DE.set_inlined_debuginfo handler_env ~from:env_at_fork in
230227
let handler_env =
231228
DE.set_at_unit_toplevel_state handler_env
232229
(DE.at_unit_toplevel env_at_fork)

middle_end/flambda2/simplify/simplify_nullary_primitive.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ let simplify_nullary_primitive dacc original_prim (prim : P.nullary_primitive)
4242
Simplify_primitive_result.create named ~try_reify:false dacc
4343
| Enter_inlined_apply { dbg } ->
4444
let dacc =
45-
DA.map_denv dacc ~f:(fun denv ->
46-
DE.set_inlined_debuginfo denv (DE.add_inlined_debuginfo denv dbg))
45+
DA.map_denv dacc ~f:(fun denv -> DE.set_inlined_debuginfo' denv dbg)
4746
in
4847
let named = Named.create_simple Simple.const_unit in
4948
let ty = T.this_tagged_immediate Targetint_31_63.zero in

middle_end/flambda2/simplify_shared/inlining_helpers.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
open! Flambda.Import
1818
module RC = Apply.Result_continuation
1919

20-
let make_inlined_body ~callee ~region_inlined_into ~params ~args ~my_closure
21-
~my_region ~my_depth ~rec_info ~body ~exn_continuation ~return_continuation
22-
~apply_exn_continuation ~apply_return_continuation ~bind_params ~bind_depth
23-
~apply_renaming =
20+
let make_inlined_body ~callee ~called_code_id:_ ~region_inlined_into ~params
21+
~args ~my_closure ~my_region ~my_depth ~rec_info ~body ~exn_continuation
22+
~return_continuation ~apply_exn_continuation ~apply_return_continuation
23+
~bind_params ~bind_depth ~apply_renaming =
2424
let renaming = Renaming.empty in
2525
let renaming =
2626
match (apply_return_continuation : RC.t) with

middle_end/flambda2/simplify_shared/inlining_helpers.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
val make_inlined_body :
1818
callee:Simple.t option ->
19+
called_code_id:Code_id.t ->
1920
region_inlined_into:Alloc_mode.For_allocations.t ->
2021
params:'param list ->
2122
args:Simple.List.t ->
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
(******************************************************************************
2+
* flambda-backend *
3+
* Mark Shinwell, Jane Street *
4+
* -------------------------------------------------------------------------- *
5+
* MIT License *
6+
* *
7+
* Copyright (c) 2024 Jane Street Group LLC *
8+
9+
* *
10+
* Permission is hereby granted, free of charge, to any person obtaining a *
11+
* copy of this software and associated documentation files (the "Software"), *
12+
* to deal in the Software without restriction, including without limitation *
13+
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
14+
* and/or sell copies of the Software, and to permit persons to whom the *
15+
* Software is furnished to do so, subject to the following conditions: *
16+
* *
17+
* The above copyright notice and this permission notice shall be included *
18+
* in all copies or substantial portions of the Software. *
19+
* *
20+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
21+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
22+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
23+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
25+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
26+
* DEALINGS IN THE SOFTWARE. *
27+
******************************************************************************)
28+
29+
type t =
30+
{ dbg : Debuginfo.t;
31+
function_symbol : Linkage_name.t;
32+
uid : string
33+
}
34+
35+
(* CR mshinwell/poechsel: if this becomes the flambda2 replacement for
36+
[Debuginfo.t] we could maybe avoid making the uids in the same way, and add a
37+
"below" constructor or something to identify different instances of
38+
inlining. *)
39+
40+
(* CR mshinwell/poechsel: maybe this could be integrated with the inlining
41+
histories *)
42+
43+
let print_debuginfo ppf dbg =
44+
if Debuginfo.is_none dbg
45+
then Format.pp_print_string ppf "None"
46+
else Debuginfo.print_compact ppf dbg
47+
48+
let print ppf { dbg; function_symbol; uid } =
49+
Format.fprintf ppf
50+
"@[<hov 1>(@[<hov 1>(dbg@ %a)@]@ @[<hov 1>(function_symbol@ %a)@]@ @[<hov \
51+
1>(uid@ %a)@])@]"
52+
print_debuginfo dbg Linkage_name.print function_symbol
53+
Format.pp_print_string uid
54+
55+
let none =
56+
{ dbg = Debuginfo.none;
57+
function_symbol = Linkage_name.of_string "";
58+
uid = ""
59+
}
60+
61+
let is_none t = Debuginfo.compare t.dbg Debuginfo.none = 0
62+
63+
let inlining_counter = ref 0
64+
65+
let create ~called_code_id ~apply_dbg =
66+
let function_symbol = Code_id.linkage_name called_code_id in
67+
let uid =
68+
incr inlining_counter;
69+
(* CR mshinwell: consider improving this *)
70+
Hashtbl.hash (apply_dbg, function_symbol, !inlining_counter)
71+
|> string_of_int
72+
in
73+
{ dbg = apply_dbg; function_symbol; uid }
74+
75+
let rewrite t dbg_from_inlined_body =
76+
if is_none t
77+
then dbg_from_inlined_body
78+
else
79+
let dbg_from_inlined_body =
80+
(* uids of _all_ [Debuginfo.t] values in the body of the function being
81+
inlined get freshened (consistently, for any given inlining).
82+
83+
See the doc comment in the .mli for further details. *)
84+
Debuginfo.mapi_items dbg_from_inlined_body
85+
~f:(fun n ({ dinfo_function_symbol; _ } as item : Debuginfo.item) ->
86+
let dinfo_function_symbol =
87+
if n = 0
88+
then Some (Linkage_name.to_string t.function_symbol)
89+
else dinfo_function_symbol
90+
in
91+
Debuginfo.item_with_uid_and_function_symbol item
92+
~dinfo_uid:(Some t.uid) ~dinfo_function_symbol)
93+
in
94+
(* Note that [t.dbg] might not be a singleton -- however everything should
95+
be ok in terms of each frame (except the first) being annotated with a
96+
uid and function symbol in [t.dbg]. *)
97+
Debuginfo.inline t.dbg dbg_from_inlined_body
98+
99+
let compare t1 t2 =
100+
let c = Debuginfo.compare t1.dbg t2.dbg in
101+
if c <> 0
102+
then c
103+
else
104+
let c = Linkage_name.compare t1.function_symbol t2.function_symbol in
105+
if c <> 0 then c else String.compare t1.uid t2.uid

0 commit comments

Comments
 (0)