File 0299-Eliminate-internal-error-in-beam_ssa_opt-ssa_opt_tai.patch of Package erlang

From 3f15b66b094ea3b96081f22d8ee4a5a96b6bd139 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= <bjorn@erlang.org>
Date: Sat, 23 Mar 2024 07:06:08 +0100
Subject: [PATCH] Eliminate internal error in
 `beam_ssa_opt:ssa_opt_tail_phis/1`

This bug was introduced in 9e391fdbb021db44ce6e75f31553e9e07be04017.

Closes #8296
---
 lib/compiler/src/beam_ssa_bool.erl   |  3 ++-
 lib/compiler/src/beam_ssa_opt.erl    |  6 ++++--
 lib/compiler/test/beam_ssa_SUITE.erl | 16 ++++++++++++++++
 3 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/lib/compiler/src/beam_ssa_bool.erl b/lib/compiler/src/beam_ssa_bool.erl
index ec14674b34..3cd7a4983b 100644
--- a/lib/compiler/src/beam_ssa_bool.erl
+++ b/lib/compiler/src/beam_ssa_bool.erl
@@ -169,7 +169,8 @@ opt_function(#b_function{bs=Blocks0,cnt=Count0}=F) ->
         true ->
             %% There are no boolean operators that can be optimized in
             %% this function.
-            F#b_function{bs=Blocks1,cnt=Count1}
+            Blocks2 = beam_ssa:trim_unreachable(Blocks1), %Fix up phi nodes.
+            F#b_function{bs=Blocks2,cnt=Count1}
     end.
 
 %%%
diff --git a/lib/compiler/src/beam_ssa_opt.erl b/lib/compiler/src/beam_ssa_opt.erl
index fce0636436..fca1e08877 100644
--- a/lib/compiler/src/beam_ssa_opt.erl
+++ b/lib/compiler/src/beam_ssa_opt.erl
@@ -1038,9 +1038,11 @@ update_tuple_merge(Src, SetOps, Updates0, Seen0) ->
 %%% subexpressions across instructions that clobber the X registers.
 %%%
 
-ssa_opt_cse({#opt_st{ssa=Linear}=St, FuncDb}) ->
+ssa_opt_cse({#opt_st{ssa=Linear0}=St, FuncDb}) ->
     M = #{0 => #{}, ?EXCEPTION_BLOCK => #{}},
-    {St#opt_st{ssa=cse(Linear, #{}, M)}, FuncDb}.
+    Linear1 = cse(Linear0, #{}, M),
+    Linear = beam_ssa:trim_unreachable(Linear1), %Fix up phi nodes.
+    {St#opt_st{ssa=Linear}, FuncDb}.
 
 cse([{L,#b_blk{is=Is0,last=Last0}=Blk0}|Bs], Sub0, M0) ->
     case M0 of
diff --git a/lib/compiler/test/beam_ssa_SUITE.erl b/lib/compiler/test/beam_ssa_SUITE.erl
index 064f6b5bec..49a15b4be7 100644
--- a/lib/compiler/test/beam_ssa_SUITE.erl
+++ b/lib/compiler/test/beam_ssa_SUITE.erl
@@ -939,6 +939,9 @@ grab_bag(_Config) ->
 
     false = grab_bag_22(),
 
+    {reply,{ok,foo_bar},#{page_title := foo_bar}} =
+        grab_bag_23(id(#{page_title => unset})),
+
     ok.
 
 grab_bag_1() ->
@@ -1227,6 +1230,19 @@ grab_bag_22() ->
         bar ?= id(42)
     end.
 
+%% GH-8296: When the cse pass in beam_ssa_opt was changed to
+%% eliminate unreachable phi nodes, it failed to fix up phi nodes
+%% that reference eliminated blocks.
+grab_bag_23(#{page_title := unset} = State1) ->
+    State2 = State1#{page_title := foo_bar},
+    {reply,
+     {ok,
+      case State2 of
+          #{page_title := Val} -> Val;
+          _ -> lists:flatten([State2])
+      end},
+     State2}.
+
 redundant_br(_Config) ->
     {false,{x,y,z}} = redundant_br_1(id({x,y,z})),
     {true,[[a,b,c]]} = redundant_br_1(id([[[a,b,c]]])),
-- 
2.35.3

openSUSE Build Service is sponsored by