Skip to content

Commit

Permalink
Merge pull request #6663 from bjorng/bjorn/compiler/fix-try_opt/GH-6660
Browse files Browse the repository at this point in the history
Fix unsafe optimization of try...after
  • Loading branch information
bjorng authored Jan 16, 2023
2 parents ff23617 + 34ee7d1 commit fddc163
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/compiler/src/beam_ssa_opt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1654,13 +1654,19 @@ sink_try_is(Is) ->
sink_try_is_1([#b_set{op=kill_try_tag}=Kill | Is], Acc) ->
[Kill | reverse(Acc, Is)];
sink_try_is_1([I | Is], Acc) ->
case beam_ssa:no_side_effect(I) of
case is_safe_sink_try(I) of
true -> sink_try_is_1(Is, [I | Acc]);
false -> reverse(Acc, [I | Is])
end;
sink_try_is_1([], Acc) ->
reverse(Acc).

is_safe_sink_try(#b_set{op=Op}=I) ->
case Op of
bs_extract -> false;
_ -> beam_ssa:no_side_effect(I)
end.

%% Does a strength reduction of try/catch and catch.
%%
%% In try/catch constructs where the expression is restricted
Expand Down
15 changes: 15 additions & 0 deletions lib/compiler/test/bs_match_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2685,6 +2685,9 @@ bs_match(_Config) ->
ok = do_bs_match_gh_6613(<<0>>),
<<0,0>> = do_bs_match_gh_6613(<<0,0>>),

<<"abc">> = do_bs_match_gh_6660(id(<<"abc">>)),
{'EXIT', {{try_clause,abc},_}} = catch do_bs_match_gh_6660(id(abc)),

ok.

do_bs_match_1(_, X) ->
Expand Down Expand Up @@ -2732,6 +2735,18 @@ do_bs_match_gh_6613(X) ->
end,
X.

do_bs_match_gh_6660(X) ->
try X of
<<Y/bytes>> ->
%% The `bs_extract` instruction was sunk to after the
%% `kill_try_tag` instruction, preventing
%% beam_ssa_pre_codegen from combining it with the
%% preceding bs_match instruction.
Y
after
ok
end.

%% GH-6348/OTP-18297: Allow aliases for binaries.
-record(ba_foo, {a,b,c}).

Expand Down

0 comments on commit fddc163

Please sign in to comment.