From ffeef16aae92bf0fa1a731176ad257af13894ce1 Mon Sep 17 00:00:00 2001 From: Josh Berdine Date: Thu, 10 Oct 2019 06:17:02 -0700 Subject: [PATCH] [sledge] Add a flag to disable internalization Summary: By default all functions except those specified as entry points in the config file are "internalized". Internal functions are removed if they are not called. It is sometimes necessary to disable internalization, e.g. to analyze the llvm tests. Reviewed By: bennostein Differential Revision: D17725614 fbshipit-source-id: 4b13501f5 --- sledge/sledge-help.txt | 6 ++++++ sledge/src/llair/frontend.ml | 11 ++++++----- sledge/src/llair/frontend.mli | 3 ++- sledge/src/sledge.ml | 8 +++++++- sledge/test/Makefile | 2 +- 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/sledge/sledge-help.txt b/sledge/sledge-help.txt index 7b46c94c7..9001591a0 100644 --- a/sledge/sledge-help.txt +++ b/sledge/sledge-help.txt @@ -56,6 +56,8 @@ Analyze code in a buck target. This is a convenience wrapper for the sequence `s [-margin ] wrap debug tracing at columns [-modules ] write list of bitcode files to , or to standard output if is `-` + [-no-internalize] do not internalize all functions except the entry + points specified in the config file [-no-models] do not add models for C/C++ runtime and standard libraries [-preanalyze-globals] pre-analyze global variables used by each function @@ -139,6 +141,8 @@ Analyze code in one or more LLVM bitcode files. This is a convenience wrapper fo [-fuzzer] add a harness for libFuzzer targets [-llair-output ] write generated LLAIR to [-margin ] wrap debug tracing at columns + [-no-internalize] do not internalize all functions except the entry + points specified in the config file [-no-models] do not add models for C/C++ runtime and standard libraries [-preanalyze-globals] pre-analyze global variables used by each function @@ -162,6 +166,8 @@ Translate one or more LLVM bitcode files to LLAIR. Each filename may be [-fuzzer] add a harness for libFuzzer targets [-llair-output ] write generated LLAIR to [-margin ] wrap debug tracing at columns + [-no-internalize] do not internalize all functions except the entry + points specified in the config file [-no-models] do not add models for C/C++ runtime and standard libraries [-trace ] enable debug tracing diff --git a/sledge/src/llair/frontend.ml b/sledge/src/llair/frontend.ml index 1fda87aeb..6849caa88 100644 --- a/sledge/src/llair/frontend.ml +++ b/sledge/src/llair/frontend.ml @@ -1309,12 +1309,13 @@ let xlate_function : x -> Llvm.llvalue -> Llair.func = |> [%Trace.retn fun {pf} -> pf "@\n%a" Llair.Func.pp] -let transform : Llvm.llmodule -> unit = +let transform ~internalize : Llvm.llmodule -> unit = fun llmodule -> let pm = Llvm.PassManager.create () in let entry_points = Config.find_list "entry-points" in - Llvm_ipo.add_internalize_predicate pm (fun fn -> - List.exists entry_points ~f:(String.equal fn) ) ; + if internalize then + Llvm_ipo.add_internalize_predicate pm (fun fn -> + List.exists entry_points ~f:(String.equal fn) ) ; Llvm_ipo.add_global_dce pm ; Llvm_ipo.add_global_optimizer pm ; Llvm_ipo.add_merge_functions pm ; @@ -1349,7 +1350,7 @@ let link_in : Llvm.llcontext -> Llvm.lllinker -> string -> unit = fun llcontext link_ctx bc_file -> Llvm_linker.link_in link_ctx (read_and_parse llcontext bc_file) -let translate ~models ~fuzzer : string list -> Llair.t = +let translate ~models ~fuzzer ~internalize : string list -> Llair.t = fun inputs -> [%Trace.call fun {pf} -> pf "%a" (List.pp "@ " Format.pp_print_string) inputs] @@ -1371,7 +1372,7 @@ let translate ~models ~fuzzer : string list -> Llair.t = assert ( Llvm_analysis.verify_module llmodule |> Option.for_all ~f:invalid_llvm ) ; - transform llmodule ; + transform ~internalize llmodule ; scan_names_and_locs llmodule ; let lldatalayout = Llvm_target.DataLayout.of_string (Llvm.data_layout llmodule) diff --git a/sledge/src/llair/frontend.mli b/sledge/src/llair/frontend.mli index 0d2f59159..d58b2bb97 100644 --- a/sledge/src/llair/frontend.mli +++ b/sledge/src/llair/frontend.mli @@ -9,6 +9,7 @@ exception Invalid_llvm of string -val translate : models:bool -> fuzzer:bool -> string list -> Llair.t +val translate : + models:bool -> fuzzer:bool -> internalize:bool -> string list -> Llair.t (** Translate the compilation units in the named (llvm or bitcode) files to LLAIR. Attempts to raise [Invalid_llvm] when the input is invalid LLVM. *) diff --git a/sledge/src/sledge.ml b/sledge/src/sledge.ml index a1afddf73..d764bf85f 100644 --- a/sledge/src/sledge.ml +++ b/sledge/src/sledge.ml @@ -135,10 +135,16 @@ let translate = ~doc:"do not add models for C/C++ runtime and standard libraries" and fuzzer = flag "fuzzer" no_arg ~doc:"add a harness for libFuzzer targets" + and no_internalize = + flag "no-internalize" no_arg + ~doc: + "do not internalize all functions except the entry points \ + specified in the config file" in fun bitcode_inputs () -> let program = - Frontend.translate ~models:(not no_models) ~fuzzer bitcode_inputs + Frontend.translate ~models:(not no_models) ~fuzzer + ~internalize:(not no_internalize) bitcode_inputs in Option.iter ~f:(marshal program) llair_output ; program diff --git a/sledge/test/Makefile b/sledge/test/Makefile index eb2b3c439..e57d3bfb4 100644 --- a/sledge/test/Makefile +++ b/sledge/test/Makefile @@ -28,7 +28,7 @@ default: test # all analyze tests translate: @find -L llvm -name '*.ll' -or -name '*.bc' \ - | parallel --bar $(SLEDGE) llvm translate -no-models $(SLEDGE_ARGS) + | parallel --bar $(SLEDGE) llvm translate -no-models -no-internalize $(SLEDGE_ARGS) _translate-report-raw: @find -L llvm -name '*.out' \