From 351edb49bbf9b9de09cbd264049a28a3819a9100 Mon Sep 17 00:00:00 2001 From: Greta Yorsh Date: Thu, 27 Jun 2019 17:07:25 +0100 Subject: [PATCH] Add compile-time option -function-sections --- Makefile | 7 ++++- asmcomp/amd64/emit.mlp | 2 +- asmcomp/arm/emit.mlp | 2 +- asmcomp/arm64/emit.mlp | 2 +- asmcomp/asmlink.ml | 6 ++--- asmcomp/i386/emit.mlp | 2 +- configure | 41 ++++++++++++++--------------- configure.ac | 22 ++++++++-------- driver/main_args.ml | 14 ++++++++++ driver/main_args.mli | 1 + driver/optmain.ml | 4 +++ otherlibs/Makefile.otherlibs.common | 3 +++ stdlib/Makefile | 3 +++ utils/clflags.ml | 2 ++ utils/clflags.mli | 1 + utils/config.mli | 2 +- 16 files changed, 73 insertions(+), 41 deletions(-) diff --git a/Makefile b/Makefile index f935387d4..15640c0a4 100644 --- a/Makefile +++ b/Makefile @@ -55,6 +55,11 @@ INCLUDES=-I utils -I parsing -I typing -I bytecomp -I file_formats \ COMPFLAGS=-strict-sequence -principal -absname -w +a-4-9-40-41-42-44-45-48-66 \ -warn-error A \ -bin-annot -safe-string -strict-formats $(INCLUDES) +ifeq "$(FUNCTION_SECTIONS)" "true" +OPTCOMPFLAGS= -function-sections +else +OPTCOMPFLAGS= +endif LINKFLAGS= ifeq "$(strip $(NATDYNLINKOPTS))" "" @@ -1285,7 +1290,7 @@ endif $(CAMLC) $(COMPFLAGS) -c $< .ml.cmx: - $(CAMLOPT) $(COMPFLAGS) -c $< + $(CAMLOPT) $(COMPFLAGS) $(OPTCOMPFLAGS) -c $< partialclean:: for d in utils parsing typing bytecomp asmcomp middle_end file_formats \ diff --git a/asmcomp/amd64/emit.mlp b/asmcomp/amd64/emit.mlp index 83e976f89..e798e5e3e 100644 --- a/asmcomp/amd64/emit.mlp +++ b/asmcomp/amd64/emit.mlp @@ -482,7 +482,7 @@ let emit_global_label s = supported on the target system. *) let emit_named_text_section func_name = - if Config.function_sections then + if !Clflags.function_sections then begin match system with | S_macosx (* Names of section segments in macosx are restricted to 16 characters, diff --git a/asmcomp/arm/emit.mlp b/asmcomp/arm/emit.mlp index 4efb090b3..eea446868 100644 --- a/asmcomp/arm/emit.mlp +++ b/asmcomp/arm/emit.mlp @@ -439,7 +439,7 @@ let emit_load_handler_address handler = (* Output .text section directive, or named .text. if enabled. *) let emit_named_text_section func_name = - if Config.function_sections then begin + if !Clflags.function_sections then begin ` .text.{emit_symbol func_name},{emit_string_literal "ax"},{emit_string_literal "%progbits"}\n` end else diff --git a/asmcomp/arm64/emit.mlp b/asmcomp/arm64/emit.mlp index 56bb4f7e0..71abef916 100644 --- a/asmcomp/arm64/emit.mlp +++ b/asmcomp/arm64/emit.mlp @@ -566,7 +566,7 @@ let assembly_code_for_allocation ?label_after_call_gc i ~n ~far = (* Output .text section directive, or named .text. if enabled. *) let emit_named_text_section func_name = - if Config.function_sections then begin + if !Clflags.function_sections then begin ` .text.{emit_symbol func_name},{emit_string_literal "ax"},{emit_string_literal "%progbits"}\n` end else diff --git a/asmcomp/asmlink.ml b/asmcomp/asmlink.ml index 969298fea..d637e6d2a 100644 --- a/asmcomp/asmlink.ml +++ b/asmcomp/asmlink.ml @@ -240,10 +240,10 @@ let make_startup_file ~ppf_dump units_list ~crc_interfaces = let globals_map = make_globals_map units_list ~crc_interfaces in compile_phrase (Cmmgen.globals_map globals_map); compile_phrase(Cmmgen.data_segment_table ("_startup" :: name_list)); - if (Config.function_sections) then - compile_phrase(Cmmgen.code_segment_table ("_hot" :: "_startup" :: name_list)) + if !Clflags.function_sections then + compile_phrase(Cmmgen.code_segment_table("_hot" :: "_startup" :: name_list)) else - compile_phrase(Cmmgen.code_segment_table ("_startup" :: name_list)); + compile_phrase(Cmmgen.code_segment_table("_startup" :: name_list)); let all_names = "_startup" :: "_system" :: name_list in compile_phrase (Cmmgen.frame_table all_names); if Config.spacetime then begin diff --git a/asmcomp/i386/emit.mlp b/asmcomp/i386/emit.mlp index 3e26e2f07..3dcf44531 100644 --- a/asmcomp/i386/emit.mlp +++ b/asmcomp/i386/emit.mlp @@ -464,7 +464,7 @@ let emit_global_label s = (* Output .text section directive, or named .text. if enabled. *) let emit_named_text_section func_name = - if Config.function_sections then + if !Clflags.function_sections then begin match system with | S_macosx | S_mingw | S_cygwin | S_win32 -> D.text () | _ -> D.section [ ".text."^(emit_symbol func_name) ] (Some "ax") ["@progbits"] diff --git a/configure b/configure index d943365b0..93347c940 100755 --- a/configure +++ b/configure @@ -1521,9 +1521,8 @@ Optional Features: force strings to be safe --disable-flat-float-array do not use flat float arrays - --enable-function-sections - generate each function in a separate section if - target supports it + --disable-function-sections + do not emit each function in a separate section --enable-shared[=PKGS] build shared libraries [default=yes] --enable-static[=PKGS] build static libraries [default=yes] --enable-fast-install[=PKGS] @@ -16529,39 +16528,41 @@ else flat_float_array=true fi -if test x"$enable_function_sections" = "xyes"; then : +if test x"$enable_function_sections" = "xno"; then : + function_sections=false +else case $arch in #( amd64|i386|arm|arm64) : case $target in #( - *-*-cygwin*|*-*-mingw*|*-pc-windows) : + *-cygwin*|*-mingw*|*-windows|*-apple-darwin*) : function_sections=false; - { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: Disabling function sections. No target support." >&5 -$as_echo "$as_me: WARNING: Disabling function sections. No target support." >&2;} ;; #( + { $as_echo "$as_me:${as_lineno-$LINENO}: Disabling function sections. No target support." >&5 +$as_echo "$as_me: Disabling function sections. No target support." >&6;} ;; #( *) : case $ocaml_cv_cc_vendor in #( gcc-0123-*|gcc-4-01234567) : function_sections=false; - { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: Disabling function sections. + { $as_echo "$as_me:${as_lineno-$LINENO}: Disabling function sections. Not supported in GCC prior to version 4.8." >&5 -$as_echo "$as_me: WARNING: Disabling function sections. - Not supported in GCC prior to version 4.8." >&2;} ;; #( +$as_echo "$as_me: Disabling function sections. + Not supported in GCC prior to version 4.8." >&6;} ;; #( clang-012-*|clang-3-01234) : function_sections=false; - { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: Disabling function sections. + { $as_echo "$as_me:${as_lineno-$LINENO}: Disabling function sections. Not supported in Clang prior to version 3.5." >&5 -$as_echo "$as_me: WARNING: Disabling function sections. - Not supported in Clang prior to version 3.5." >&2;} ;; #( +$as_echo "$as_me: Disabling function sections. + Not supported in Clang prior to version 3.5." >&6;} ;; #( gcc-*|clang-*) : function_sections=true; - common_cflags="$common_cflags -ffunction-sections"; + internal_cflags="$internal_cflags -ffunction-sections"; $as_echo "#define FUNCTION_SECTIONS 1" >>confdefs.h ;; #( *) : function_sections=false; - { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: Disabling function sections. + { $as_echo "$as_me:${as_lineno-$LINENO}: Disabling function sections. Compiler not supported." >&5 -$as_echo "$as_me: WARNING: Disabling function sections. - Compiler not supported." >&2;} ;; #( +$as_echo "$as_me: Disabling function sections. + Compiler not supported." >&6;} ;; #( *) : ;; esac ;; #( @@ -16570,11 +16571,9 @@ esac ;; #( esac ;; #( *) : function_sections=false - { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: Disabling function sections on this target." >&5 -$as_echo "$as_me: WARNING: Disabling function sections on this target." >&2;} ;; + { $as_echo "$as_me:${as_lineno-$LINENO}: Disabling function sections on this target." >&5 +$as_echo "$as_me: Disabling function sections on this target." >&6;} ;; esac -else - function_sections=false fi if test x"$with_afl" = "xyes"; then : diff --git a/configure.ac b/configure.ac index 2fb469491..d238523f4 100644 --- a/configure.ac +++ b/configure.ac @@ -354,8 +354,8 @@ AC_ARG_ENABLE([flat-float-array], [do not use flat float arrays])]) AC_ARG_ENABLE([function-sections], - [AS_HELP_STRING([--enable-function-sections], - [generate each function in a separate section if target supports it])]) + [AS_HELP_STRING([--disable-function-sections], + [do not emit each function in a separate section])]) AC_ARG_WITH([afl], [AS_HELP_STRING([--with-afl], @@ -1618,34 +1618,34 @@ AS_IF([test x"$enable_flat_float_array" = "xno"], [AC_DEFINE([FLAT_FLOAT_ARRAY]) flat_float_array=true]) -AS_IF([test x"$enable_function_sections" = "xyes"], +AS_IF([test x"$enable_function_sections" = "xno"], + [function_sections=false], [AS_CASE([$arch], [amd64|i386|arm|arm64], [AS_CASE([$target], - [*-*-cygwin*|*-*-mingw*|*-pc-windows], + [*-cygwin*|*-mingw*|*-windows|*-apple-darwin*], [function_sections=false; - AC_MSG_WARN([Disabling function sections. No target support.])], + AC_MSG_NOTICE([Disabling function sections. No target support.])], [*], [AS_CASE([$ocaml_cv_cc_vendor], [gcc-[0123]-*|gcc-4-[01234567]], [function_sections=false; - AC_MSG_WARN([Disabling function sections. + AC_MSG_NOTICE([Disabling function sections. Not supported in GCC prior to version 4.8.])], [clang-[012]-*|clang-3-[01234]], [function_sections=false; - AC_MSG_WARN([Disabling function sections. + AC_MSG_NOTICE([Disabling function sections. Not supported in Clang prior to version 3.5.])], [gcc-*|clang-*], [function_sections=true; - common_cflags="$common_cflags -ffunction-sections"; + internal_cflags="$internal_cflags -ffunction-sections"; AC_DEFINE([FUNCTION_SECTIONS])], [*], [function_sections=false; - AC_MSG_WARN([Disabling function sections. + AC_MSG_NOTICE([Disabling function sections. Compiler not supported.])])])], [function_sections=false - AC_MSG_WARN([Disabling function sections on this target.])])], - [function_sections=false]) + AC_MSG_NOTICE([Disabling function sections on this target.])])]) AS_IF([test x"$with_afl" = "xyes"], [afl=true], diff --git a/driver/main_args.ml b/driver/main_args.ml index b7e3c0821..b6e8ab928 100644 --- a/driver/main_args.ml +++ b/driver/main_args.ml @@ -94,6 +94,18 @@ let mk_dllpath f = " Add to the run-time search path for shared libraries" ;; +let mk_function_sections f = + if Config.function_sections then + "-function-sections", Arg.Unit f, + " Generate each function in a separate section if target supports it" + else + let err () = + raise (Arg.Bad "OCaml has been configured without support for \ + -function-sections") + in + "-function-sections", Arg.Unit err, " (option not available)" +;; + let mk_stop_after f = "-stop-after", Arg.Symbol (Clflags.Compiler_pass.pass_names, f), " Stop after the given compilation pass." @@ -1068,6 +1080,7 @@ module type Optcomp_options = sig val _afl_instrument : unit -> unit val _afl_inst_ratio : int -> unit val _dinterval : unit -> unit + val _function_sections : unit -> unit end;; module type Opttop_options = sig @@ -1289,6 +1302,7 @@ struct mk_dtypes F._annot; mk_for_pack_opt F._for_pack; mk_g_opt F._g; + mk_function_sections F._function_sections; mk_stop_after F._stop_after; mk_i F._i; mk_I F._I; diff --git a/driver/main_args.mli b/driver/main_args.mli index 64067b2c2..7bc082f88 100644 --- a/driver/main_args.mli +++ b/driver/main_args.mli @@ -224,6 +224,7 @@ module type Optcomp_options = sig val _afl_instrument : unit -> unit val _afl_inst_ratio : int -> unit val _dinterval : unit -> unit + val _function_sections : unit -> unit end;; module type Opttop_options = sig diff --git a/driver/optmain.ml b/driver/optmain.ml index 59e531e42..604496f86 100644 --- a/driver/optmain.ml +++ b/driver/optmain.ml @@ -56,6 +56,10 @@ module Options = Main_args.Make_optcomp_options (struct let _config = Misc.show_config_and_exit let _config_var = Misc.show_config_variable_and_exit let _for_pack s = for_package := Some s + let _function_sections () = + assert (Config.function_sections); + first_ccopts := "-ffunction-sections" :: !first_ccopts; + function_sections := true let _g = set debug let _i () = print_types := true; diff --git a/otherlibs/Makefile.otherlibs.common b/otherlibs/Makefile.otherlibs.common index 9348d3b7b..45e810f97 100644 --- a/otherlibs/Makefile.otherlibs.common +++ b/otherlibs/Makefile.otherlibs.common @@ -35,6 +35,9 @@ OPTCOMPFLAGS=-O3 else OPTCOMPFLAGS= endif +ifeq "$(FUNCTION_SECTIONS)" "true" +OPTCOMPFLAGS += -function-sections +endif MKLIB=$(CAMLRUN) $(ROOTDIR)/tools/ocamlmklib # Variables that must be defined by individual libraries: diff --git a/stdlib/Makefile b/stdlib/Makefile index 12bc717a7..88d745326 100644 --- a/stdlib/Makefile +++ b/stdlib/Makefile @@ -30,6 +30,9 @@ OPTCOMPFLAGS=-O3 else OPTCOMPFLAGS= endif +ifeq "$(FUNCTION_SECTIONS)" "true" +OPTCOMPFLAGS += -function-sections +endif OPTCOMPILER=$(ROOTDIR)/ocamlopt CAMLOPT=$(CAMLRUN) $(OPTCOMPILER) CAMLDEP=$(BOOT_OCAMLC) -depend diff --git a/utils/clflags.ml b/utils/clflags.ml index 5d85b6ca6..67055caa7 100644 --- a/utils/clflags.ml +++ b/utils/clflags.ml @@ -178,6 +178,8 @@ let inlining_report = ref false (* -inlining-report *) let afl_instrument = ref Config.afl_instrument (* -afl-instrument *) let afl_inst_ratio = ref 100 (* -afl-inst-ratio *) +let function_sections = ref false (* -function-sections *) + let simplify_rounds = ref None (* -rounds *) let default_simplify_rounds = ref 1 (* -rounds *) let rounds () = diff --git a/utils/clflags.mli b/utils/clflags.mli index 1aaff70cc..1f0b64bbb 100644 --- a/utils/clflags.mli +++ b/utils/clflags.mli @@ -206,6 +206,7 @@ val dump_flambda_verbose : bool ref val classic_inlining : bool ref val afl_instrument : bool ref val afl_inst_ratio : int ref +val function_sections : bool ref val all_passes : string list ref val dumped_pass : string -> bool diff --git a/utils/config.mli b/utils/config.mli index 2bddcffe6..560283f22 100644 --- a/utils/config.mli +++ b/utils/config.mli @@ -225,7 +225,7 @@ val flat_float_array : bool val function_sections : bool (** Whether the compiler was configured to generate - functions in separate section *) + each function in a separate section *) val windows_unicode: bool (** Whether Windows Unicode runtime is enabled *)