Skip to content

Emit low-level location information for FDO: fix backtraces (1/2) #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion backend/amd64/emit.mlp
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,31 @@ let function_name = ref ""
(* Entry point for tail recursive calls *)
let tailrec_entry_point = ref 0

(* Emit Debug info and Fdo info *)

let emit_debug_info_with_fdo dbg fdo =
(* CR-soon gyorsh: discriminators aren't supported yet in debug info
generated by ocaml compiler. It can be easily added, but for now we use
line numbers instead, to reduce the number of different compiler changes
required to make ocamlfdo work. *)
(* CR-soon gyorsh: next line is an expensive string operation, generating the
same string over and over again for each instruction in a given function.
We should pull it to top level, but it won't be needed at all
with discriminators, so leave it here locally. *)
let d = Fdo_info.get fdo in
let file = (emit_symbol !function_name) ^
Clflags.Compiler_ir.(extension Linear) in
let pos = { Lexing.dummy_pos with pos_fname = file; pos_lnum = d } in
let loc = { Location.loc_start = pos; loc_end = pos; loc_ghost = true } in
let scoped_loc = Debuginfo.Scoped_location.of_location ~scopes:[] loc in
let nd = Debuginfo.from_location scoped_loc in
emit_debug_info (dbg @ nd)

let emit_debug_info_linear i =
match Fdo_info.is_none i.fdo with
| true -> emit_debug_info i.dbg
| false -> emit_debug_info_with_fdo i.dbg i.fdo

(* Emit tracing probes *)

type probe =
Expand Down Expand Up @@ -649,7 +674,7 @@ let emit_call_probe_handler_wrapper i ~probe_label =

(* Emit an instruction *)
let emit_instr fallthrough i =
emit_debug_info i.dbg;
emit_debug_info_linear i;
match i.desc with
| Lend -> ()
| Lprologue ->
Expand Down
1 change: 1 addition & 0 deletions backend/debug/compute_ranges.ml
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ module Make (S : Compute_ranges_intf.S_functor) = struct
arg = [| |];
res = [| |];
dbg = insn.dbg;
fdo = insn.fdo;
live = insn.live;
}
in
Expand Down
17 changes: 17 additions & 0 deletions backend/fdo_info.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Greta Yorsh, Jane Street Europe *)
(* *)
(* Copyright 2019--2021 Jane Street Group LLC *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
type t = int option
include (Stdlib.Option : module type of Stdlib.Option
with type 'a t := 'a Stdlib.Option.t)
let create = some
22 changes: 22 additions & 0 deletions backend/fdo_info.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Greta Yorsh, Jane Street Europe *)
(* *)
(* Copyright 2019--2021 Jane Street Group LLC *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
(** Currently, [Fdo_info] holds low-level location information that
identifies instructions in the intermediate representation,
for the purpose of mapping execution profiles directly back
to them, instead of a source location. *)
type t = private int option
val none : t
val is_none : t -> bool
val create : int -> t
val get : t -> int
4 changes: 3 additions & 1 deletion backend/linear.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type instruction =
arg: Reg.t array;
res: Reg.t array;
dbg: Debuginfo.t;
fdo: Fdo_info.t;
live: Reg.Set.t }

and instruction_desc =
Expand Down Expand Up @@ -83,10 +84,11 @@ let rec end_instr =
arg = [||];
res = [||];
dbg = Debuginfo.none;
fdo = Fdo_info.none;
live = Reg.Set.empty }

(* Cons an instruction (live, debug empty) *)

let instr_cons d a r n =
{ desc = d; next = n; arg = a; res = r;
dbg = Debuginfo.none; live = Reg.Set.empty }
dbg = Debuginfo.none; fdo = Fdo_info.none; live = Reg.Set.empty }
1 change: 1 addition & 0 deletions backend/linear.mli
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type instruction =
arg: Reg.t array;
res: Reg.t array;
dbg: Debuginfo.t;
fdo: Fdo_info.t;
live: Reg.Set.t }

and instruction_desc =
Expand Down
6 changes: 4 additions & 2 deletions backend/linearize.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ open Linear

let cons_instr d n =
{ desc = d; next = n; arg = [||]; res = [||];
dbg = Debuginfo.none; live = Reg.Set.empty }
dbg = Debuginfo.none; fdo = Fdo_info.none; live = Reg.Set.empty }

(* Build an instruction with arg, res, dbg, live taken from
the given Mach.instruction *)

let copy_instr d i n =
{ desc = d; next = n;
arg = i.Mach.arg; res = i.Mach.res;
dbg = i.Mach.dbg; live = i.Mach.live }
dbg = i.Mach.dbg; fdo = Fdo_info.none; live = i.Mach.live }

(*
Label the beginning of the given instruction sequence.
Expand Down Expand Up @@ -278,6 +278,7 @@ let add_prologue first_insn prologue_required =
arg = [| |];
res = [| |];
dbg = insn.dbg;
fdo = insn.fdo;
live = insn.live;
}
in
Expand Down Expand Up @@ -311,6 +312,7 @@ let add_prologue first_insn prologue_required =
arg = [| |];
res = [| |];
dbg = tailrec_entry_point.dbg;
fdo = tailrec_entry_point.fdo;
live = Reg.Set.empty; (* will not be used *)
}
in
Expand Down
8 changes: 7 additions & 1 deletion dune
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
CSE CSEgen deadcode emit emitaux interf interval linear linearize linscan
liveness mach printcmm printlinear printmach proc reg reload reloadgen
schedgen scheduling selectgen selection spacetime_profiling spill split
strmatch x86_ast x86_dsl x86_gas x86_masm x86_proc
strmatch x86_ast x86_dsl x86_gas x86_masm x86_proc fdo_info

;; file_formats/
linear_format
Expand Down Expand Up @@ -286,6 +286,7 @@
(compute_ranges.mli as compiler-libs/compute_ranges.mli)
(opterrors.mli as compiler-libs/opterrors.mli)
(optcompile.mli as compiler-libs/optcompile.mli)
(fdo_info.mli as compiler-libs/fdo_info.mli)

(.ocamloptcomp.objs/byte/linear_format.cmi as compiler-libs/linear_format.cmi)
(.ocamloptcomp.objs/byte/linear_format.cmo as compiler-libs/linear_format.cmo)
Expand Down Expand Up @@ -499,6 +500,10 @@
(.ocamloptcomp.objs/byte/x86_proc.cmo as compiler-libs/x86_proc.cmo)
(.ocamloptcomp.objs/byte/x86_proc.cmt as compiler-libs/x86_proc.cmt)
(.ocamloptcomp.objs/byte/x86_proc.cmti as compiler-libs/x86_proc.cmti)
(.ocamloptcomp.objs/byte/fdo_info.cmi as compiler-libs/fdo_info.cmi)
(.ocamloptcomp.objs/byte/fdo_info.cmo as compiler-libs/fdo_info.cmo)
(.ocamloptcomp.objs/byte/fdo_info.cmt as compiler-libs/fdo_info.cmt)
(.ocamloptcomp.objs/byte/fdo_info.cmti as compiler-libs/fdo_info.cmti)
(.ocamloptcomp.objs/native/afl_instrument.cmx as compiler-libs/afl_instrument.cmx)
(.ocamloptcomp.objs/native/arch.cmx as compiler-libs/arch.cmx)
(.ocamloptcomp.objs/native/asmgen.cmx as compiler-libs/asmgen.cmx)
Expand Down Expand Up @@ -551,6 +556,7 @@
(.ocamloptcomp.objs/native/x86_gas.cmx as compiler-libs/x86_gas.cmx)
(.ocamloptcomp.objs/native/x86_masm.cmx as compiler-libs/x86_masm.cmx)
(.ocamloptcomp.objs/native/x86_proc.cmx as compiler-libs/x86_proc.cmx)
(.ocamloptcomp.objs/native/fdo_info.cmx as compiler-libs/fdo_info.cmx)
))

(install
Expand Down