[m-rev.] for review: work around slow hipe compilation

Peter Wang wangp at students.csse.unimelb.edu.au
Mon Jul 2 12:45:01 AEST 2007


On 2007-06-29, Peter Wang <wangp at students.csse.unimelb.edu.au> wrote:
> Estimated hours taken: 5
> Branches: main
> 
> compiler/erl_code_gen.m:
> 	The HiPE compiler is extremely slow compiling functions containing long
> 	case statements involving strings.  Workaround: for a string switch
> 	with many cases, convert the string to an atom and switch on atoms
> 	instead.

I forgot that Erlang atoms are limited in length, so this workaround
should be explicitly enabled by the user.  Interdiff follows.


Estimated hours taken: 6
Branches: main

The HiPE compiler is extremely slow compiling functions containing long case
statements involving strings.  Workaround: for a string switch with many cases,
convert the string to an atom and switch on atoms instead, as long as none of
the cases are longer than 255 characters long (the maximum length of an atom in
the Erlang implementation).

Since this workaround can break legitimate code (if a string longer than 255
characters is passed, the conversion to a atom will throw an exception at
runtime) only use it if the user enables the option
--erlang-switch-on-strings-as-atoms.

compiler/erl_code_gen.m:
	Implement the workaround above.

compiler/options.m:
doc/user_guide.texi:
	Add the new option --erlang-switch-on-strings-as-atoms.


diff -u compiler/erl_code_gen.m compiler/erl_code_gen.m
--- compiler/erl_code_gen.m	29 Jun 2007 06:36:08 -0000
+++ compiler/erl_code_gen.m	2 Jul 2007 02:40:14 -0000
@@ -62,6 +62,7 @@
 :- import_module hlds.pred_table.
 :- import_module libs.compiler_util.
 :- import_module libs.globals.
+:- import_module libs.options.
 :- import_module parse_tree.prog_data.
 :- import_module parse_tree.prog_foreign.
 :- import_module parse_tree.prog_type.
@@ -72,6 +73,7 @@
 :- import_module map.
 :- import_module maybe.
 :- import_module set.
+:- import_module string.
 :- import_module varset.
 
 %-----------------------------------------------------------------------------%
@@ -641,10 +643,27 @@
 
     (if
         % The HiPE compiler is extremely slow compiling functions containing
-        % long case statements involving strings.  Workaround: for long string
-        % switches, convert the string to an atom and switch on atoms instead.
+        % long case statements involving strings.  Workaround: for a string
+        % switch with many cases, convert the string to an atom and switch on
+        % atoms instead.
         TypeCategory = type_cat_string,
-        list.length(CasesList) > switch_strings_as_atoms_limit
+        list.length(CasesList) > switch_strings_as_atoms_limit,
+
+        % list_to_atom could throw an exception for long strings, so we don't
+        % enable the workaround unless the user specifically passes
+        % --erlang-switch-on-strings-as-atoms.
+        module_info_get_globals(ModuleInfo, Globals),
+        globals.lookup_bool_option(Globals, erlang_switch_on_strings_as_atoms,
+            yes),
+
+        % The Erlang implementation limits atoms to be 255 characters long or
+        % less, so we don't use the workaround if any cases are longer than
+        % that.
+        all [String] (
+            list.member(case(string_const(String), _), CasesList)
+        =>
+            string.length(String) =< 255
+        )
     then
         erl_gen_info_new_named_var("Atom", AtomVar, !Info),
         StringToAtom = elds_eq(expr_from_var(AtomVar),
only in patch2:
unchanged:
--- compiler/options.m	25 Jun 2007 00:58:12 -0000	1.569
+++ compiler/options.m	2 Jul 2007 02:40:14 -0000
@@ -645,6 +645,9 @@
     %   - IL
     %   (none yet)
 
+    %   - Erlang
+    ;       erlang_switch_on_strings_as_atoms
+
     % Target code compilation options
     ;       target_debug
 
@@ -1394,7 +1397,10 @@
     emit_c_loops                        -   bool(no),
     procs_per_c_function                -   int(1),
     everything_in_one_c_function        -   special,
-    local_thread_engine_base            -   bool(yes)
+    local_thread_engine_base            -   bool(yes),
+
+    % Erlang
+    erlang_switch_on_strings_as_atoms   -   bool(no)
 ]).
 option_defaults_2(target_code_compilation_option, [
     % Target code compilation options
@@ -2216,6 +2222,10 @@
 long_option("inline-alloc",         inline_alloc).
 long_option("local-thread-engine-base", local_thread_engine_base).
 
+% Erlang
+long_option("erlang-switch-on-strings-as-atoms",
+                erlang_switch_on_strings_as_atoms).
+
 % Target code compilation options
 long_option("target-debug",         target_debug).
 
@@ -2919,6 +2929,7 @@
     options_help_hlds_llds_optimization,
     options_help_llds_llds_optimization,
     options_help_mlds_mlds_optimization,
+    options_help_hlds_elds_optimization,
     options_help_output_optimization,
     options_help_target_code_compilation,
     options_help_link,
@@ -4507,6 +4518,18 @@
         "\tin the standard library."
 ]).
 
+:- pred options_help_hlds_elds_optimization(io::di, io::uo) is det.
+
+options_help_hlds_elds_optimization -->
+    io.write_string("\n    HLDS -> ELDS optimizations:\n"),
+    write_tabbed_lines([
+        "--erlang-switch-on-strings-as-atoms",
+        "\tEnable a workaround for slow HiPE compilation of large string",
+        "\tswitches by converting the string to an atom at runtime and",
+        "\tswitching on atoms. Do not enable if the string switched on",
+        "\tcould be longer than 255 characters at runtime."
+    ]).
+
 :- pred options_help_output_optimization(io::di, io::uo) is det.
 
 options_help_output_optimization -->
only in patch2:
unchanged:
--- doc/user_guide.texi	22 Jun 2007 08:31:12 -0000	1.530
+++ doc/user_guide.texi	2 Jul 2007 02:40:14 -0000
@@ -7803,6 +7803,7 @@
 * High-level (HLDS -> HLDS) optimization options::
 * MLDS backend (MLDS -> MLDS) optimization options::
 * Medium-level (HLDS -> LLDS) optimization options::
+* Erlang (HLDS -> ELDS) optimization options::
 * Low-level (LLDS -> LLDS) optimization options::
 * Output-level (LLDS -> C) optimization options::
 @end menu
@@ -8328,6 +8329,21 @@
 in the standard library.
 @end table
 
+ at node Erlang (HLDS -> ELDS) optimization options
+ at subsection Erlang (HLDS -> ELDS) optimization options
+ at cindex HLDS
+ at cindex ELDS
+
+ at table @code
+ at item --erlang-switch-on-strings-as-atoms
+ at findex --no-erlang-switch-on-strings-as-atoms
+ at findex --erlang-switch-on-strings-as-atoms
+Enable a workaround for slow HiPE compilation of large string
+switches by converting the string to an atom at runtime and
+switching on atoms. Do not enable if the string switched on
+could be longer than 255 characters at runtime.
+ at end table
+
 @node Medium-level (HLDS -> LLDS) optimization options
 @subsection Medium-level (HLDS -> LLDS) optimization options
 @cindex HLDS
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list