Age | Commit message (Collapse) | Author |
|
* maint:
Eliminate crash in the beam_ssa_dead compiler pass
|
|
bjorng/bjorn/compiler/fix-beam_ssa_dead-crash/ERL-956/OTP-15848
Eliminate crash in the beam_ssa_dead compiler pass
|
|
The compiler could crash in the beam_ssa_dead pass while compiling
complex nested `case` expressions. See the added test case for an
example and explanation.
https://bugs.erlang.org/browse/ERL-956
|
|
* maint:
Fix loading of Core Erlang code for extracting a map element
Fix unsafe optimizations where guard tests could be removed
|
|
A repeated test could be optimized away. Example:
bar(A) ->
if is_bitstring(A) ->
if is_binary(A) ->
binary;
true ->
bitstring
end;
true ->
other
end.
In the example, the `is_binary/1` test would be optimized away,
basically turning the example into:
bar(A) ->
if is_bitstring(A) ->
bitstring;
true ->
other
end.
Thanks user Marcus Kruse in the Elixir forum for noticing this bug.
|
|
The `beam_ssa_dead` pass is supposed to eliminate tests that are
determined to be redundant based on the outcome of a previous test.
For example, in the following example that repeats a guard test,
the second clause can never be executed:
foo(A) when A >= 42 -> one;
foo(A) when A >= 42 -> two;
foo(_) -> three.
`beam_ssa_dead` should have eliminated the second clause, but
didn't:
{test,is_ge,{f,5},[{x,0},{integer,42}]}.
{move,{atom,one},{x,0}}.
return.
{label,5}.
{test,is_ge,{f,6},[{x,0},{integer,42}]}.
{move,{atom,two},{x,0}}.
return.
{label,6}.
{move,{atom,three},{x,0}}.
return.
Correct the optimization of four different combinations of relational
operations that were too conservate. To ensure the correctness of the
optimization, also add an exahaustive test of all combinations of
relational operations with one variable and one literal. (Also remove
the weak and now redundant coverage tests in `beam_ssa_SUITE`.) With
this correction, the following code will be generated for the example:
{test,is_ge,{f,5},[{x,0},{integer,42}]}.
{move,{atom,one},{x,0}}.
return.
{label,5}.
{move,{atom,three},{x,0}}.
return.
Thanks to Dániel Szoboszlay (@dszoboszlay), whose talk at Code BEAM
STO 2019 made me aware of this missed opportunity for optimization.
|
|
|
|
This is cleaner and slightly faster.
|
|
The general complexity of the shortcut sub pass of `beam_ssa_dead` is
quadratic, but those optimizations will reduce the constant factor
somewhat.
|
|
Refactor the code to avoid putting any variable from a skippable block
into the set of unset variables. Keeping the set of unset variables as
small as possible will make beam_ssa_dead almost twice as fast when
compiling lib/unicode/tokenizer.ex in elixir.
|
|
Two helper functions in beam_ssa_opt and beam_ssa_dead
are body-recursive for no good reason.
While at it, add some clarifying comments to the functions.
|
|
Avoiding calls usually reduces the size of the stack frame and reduces
register shuffling.
|
|
beam_ssa_dead could be very slow if there were many blocks
connected with unconditional branches (for example, if a block
had contained many `call` instructions and been split by
ssa_opt_split_blocks).
It turns out that `comb_get_sw/3` does an unnecessary (and perhaps
incorrect) recursive call to itself when the terminator for the
block is an unconditional branch. Removing the recursive call does
not disable any optimizations, but will be much faster if there
are many blocks connected with unconditional branches.
Reported-by: Michał Muskała
|
|
Compilation of code similar the following would be very slow:
uts46_map(CP) when 0 =< CP, CP =< 44 -> '3';
uts46_map(CP) when 45 =< CP, CP =< 46 -> 'V';
uts46_map(CP) when 48 =< CP, CP =< 57 -> 'V';
%% More than 2500 similar lines follows.
.
.
.
The code is from from:
https://github.com/benoitc/erlang-idna/blob/3eb54ccbfa6fb917c0f4ca9197da337ad888ffe0/src/idna_mapping.erl#L6780
By using information about skippable blocks, the beam_ssa_dead
pass can be sped up to compile idna_mapping.erl about 10 times faster.
|
|
`beam_ssa_dead` can waste a lot of time trying to optimize
an unoptimizable `switch` instruction.
By being a little bit smarter when optimizing `switch` instructions,
the runtime for the beam_ssa_dead pass was reduced approximately by
half on my computer for this module:
https://github.com/aggelgian/cuter/blob/master/src/cuter_binlib.erl
Noticed-by: Kostis Sagonas
|
|
We chose to refer to variables through their var_name() because we
anticipated the need to annotate them, but it turned out we didn't
really need that, and many things become a lot cleaner if the
entire #b_var{} is used to represent variables.
|
|
Add beam_ssa_dead to perform the main optimizations done
by beam_dead:
* Shortcut branches that jump to another block with a branch.
If it can be seen that the second branch will always branch
to a specific block, replace the target of the first branch.
* Combined nested sequences of '=:=' tests and switch
instructions operating on the same variable to a single switch.
Diffing the compiler output, it seems that beam_ssa_dead finds
many more opportunities for optimizations than beam_dead,
although it does not find all opportunities that beam_dead does.
In total, beam_ssa_dead is such improvement over beam_dead that
there is no reason to keep beam_dead as well as beam_ssa_dead.
Note that beam_ssa_dead does not attempt to optimize away
redundant bs_context_binary instructions, because that instruction
will be superseded by new instructions in the near future.
|