Files
mercury/tests/hard_coded/test_split_switch_arms.exp
Zoltan Somogyi 88d4bccdba Add new optimization option --split-switch-arms.
Given a switch arm that matches several cons_ids, and which contains
one or more switches on the *same* variable, such as the arm for
f1/f2/f3/f4 below,

    (
        (X = f1 ; X = f2 ; X = f3 ; X = f4),
        ...
        (
            X = f1,
            ...
        ;
            (X = f2 ; X = f3),
            ...
        ;
            X = f4,
            ...
        ),
        ...
    ;
        ...
    )

this new optimization

- partitions the set of cons_id in that arm (in this case, {f1,f2,f3,f4})
  as finely as needed by any other the switches on X in that arm
  (in this case, that is three partitions containing {f1}, {f2,f3} and {f4}),

- splits that original switch arm into N arms, one arm for each partition,
  making a copy of the switch arm's goal for each partition,

- restricts any switches on the original switch variable (in this case, X)
  inside the copy of the arm goal inside each new case to only the cons_ids
  in that case's partition, and then replacing any resulting one-arm switches
  with the just goal inside that one arm.

The code resulting from these three steps will include some code duplication
(some of the pieces of code denoted by ... in the example above would be
duplicated), but it will need to execute fewer transfers of control.
This is worthwhile because (a) the branch instructions used to implement
switches are hard to predict unless most paths through the nested switches
are rarely if ever taken, and (b) the pipeline breaks caused by branches
that are not correctly predicted are one of the two major contributors
to the runtime of Mercury programs. (The other major contributors are
data cache misses.)

The implementation of this option has two major parts.

- Part 1 consists of discovering whether a procedure body contains
  any code in which a switch on a variable is nested inside an arm
  of another switch on that same variable. For any instance of such
  a pair of switches, it records the identity of the variable and
  the set of cons_ids of the outermost arm.

- Part 2 consists of actually transforming the procedure body
  by splitting each outermost switch arm thus recorded. (This part
  contains all three of the steps above.)

We integrate part 1 with the usual procedure body traversal of the
simplification pass, which makes it quite cheap. In most procedure bodies,
it won't find any nested switches meeting its criteria. We execute part 2,
which is relatively expensive, only if it does.

compiler/simplify_info.m:
    Add a new type, switch_arm, which represents one arm of a switch.

    Add a new field to the simplify_nested_context type. Its type
    is list(switch_arm), and it represents the stack of switch arms (if any)
    that the goal currently being simplified is inside. simplify_goal_switch.m
    uses this field to detect switches that occur inside an arm of an
    ancestor goal that is also a switch on the same variable.

    Add a new field to the simplify_info, a set of switch_arms,
    that denotes the set of switch arms from which that detection
    has actually happened, and which should therefore be
    partitioned and split.

compiler/simplify_goal_switch.m:
    Add the code for doing the detection and recording mentioned just above.
    This is the Part 1 mentioned above.

compiler/split_switch_arms.m:
    This new module implements the splitting up process.
    This is the Part 2 mentioned above.

compiler/simplify.m:
    Include the new module in the simplify subpackage of the check_hlds
    package.

compiler/notes/compiler_design.html:
    Document the new module.

compiler/simplify_proc.m:
    Invoke split_switch_arms.m (part 2) if the part of simplify_goal_switch.m
    implementing part 1 has found any work for it to do.

compiler/simplify_tasks.m:
    Add split_switch_arms as one of the tasks that simplification
    may be asked to do. Set its default value from the value of
    a new optimization option that, when specified, calls for it to be done.

compiler/options.m:
    Add this option, --split-switch-arms.

    Change the internal name of an existing option,
    everything_in_one_c_function, to the one expected by
    tools/make_optimization_options_middle.

    Replace the part of this file that is constructed by
    tools/make_optimization_options_middle.

doc/user_guide.texi:
    Document the new option.

tools/make_optimization_options_db:
    Add --split-switch-arms to the optimization tuple.

    Fix software rot by

    - renaming optimize_tailcalls to optimize_mlds_tailcalls inside the
      optimization tuple, as it has been in options.m, and

    - deleting erlang_switch_on_strings_as_atoms from the opt_tuple,
      as it has been from options.m.

tools/make_optimization_options_end:
    Enable the new option by default at optimization level 2.

tools/make_optimization_options_middle:
    Fix bugs in this script, which generates compiler/optimization_options.m.

    One bug was that it referred to the opt_level and opt_space options
    by the wrong name (optopt_level and optopt_space respectively).
    The other bug was that it expected opt_level to be a string_special option,
    when it is an int_special option.

    Make the handler_file this script generates easier to put into options.m
    by not generating a line that (a) already exists in options.m, and
    (b) would need to have a comma put after it, if one wanted this line
    to replace the copy already in options.m.

compiler/optimization_options.m:
    Rebuild this file after the changes above.

compiler/simplify_goal_unify.m:
    Conform to the changes above.

compiler/inst_merge.m:
    Mark two predicates, inst_merge_[34], to be inlined. The intention
    is that inst_merge_4 should be inlined into inst_merge_3, and then
    inst_merge_3 should be inlined inside inst_merge_2; that would then
    generate six levels of switches, three on one of the insts to be
    merged and three on the other, which the new optimization could
    then flatten. Unfortunately, inlining does not do this yet.

tests/hard_coded/test_split_switch_arms.{m,exp}:
    A new test case for the the new transformation.

tests/hard_coded/Mmakefile:
tests/hard_coded/Mercury.options:
    Enable the new test case, and specify the new option for it.
2023-07-24 19:14:17 +02:00

21 lines
682 B
Plaintext

test(f0, f5(5000), "a a <f0>")
test(f0, f6(6000), "a a <f0>")
test(f1, f5(5000), "a a <f1>")
test(f1, f6(6000), "a a <f1>")
test(f2, f5(5000), "a a [f2]")
test(f2, f6(6000), "a a [f2]")
test(f3, f5(5000), "a c {f3}")
test(f3, f6(6000), "a c {f3}")
test(f4, f5(5000), "a c {f4}")
test(f4, f6(6000), "a c {f4}")
test(f5(50), f5(5000), "a c (f5(50)) 50")
test(f5(50), f6(6000), "a c (f5(50)) 50")
test(f6(60), f5(5000), "d e /60/")
test(f6(60), f6(6000), "d f *6060*")
test(f7(70), f5(5000), "d e /70/")
test(f7(70), f6(6000), "d f *6070*")
test(f8(80), f5(5000), "d e !80!")
test(f8(80), f6(6000), "d f -6080-")
test(f9(90), f5(5000), "d e !90!")
test(f9(90), f6(6000), "d f -6090-")