From 5a900f38c52269ee1282695e5e62a05269d0a478 Mon Sep 17 00:00:00 2001
From: gdkchan <gab.dark.100@gmail.com>
Date: Sun, 10 Mar 2024 21:16:40 -0300
Subject: [PATCH] Fix lost copy and swap problem on shader SSA deconstruction
 (#6455)

* Fix lost copy on shader SSA deconstruction

* Shader cache version bump
---
 .../Shader/DiskCache/DiskCacheHostStorage.cs              | 2 +-
 src/Ryujinx.Graphics.Shader/StructuredIr/PhiFunctions.cs  | 8 ++++++--
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/src/Ryujinx.Graphics.Gpu/Shader/DiskCache/DiskCacheHostStorage.cs b/src/Ryujinx.Graphics.Gpu/Shader/DiskCache/DiskCacheHostStorage.cs
index c5763b0258..4a00d4d8eb 100644
--- a/src/Ryujinx.Graphics.Gpu/Shader/DiskCache/DiskCacheHostStorage.cs
+++ b/src/Ryujinx.Graphics.Gpu/Shader/DiskCache/DiskCacheHostStorage.cs
@@ -22,7 +22,7 @@ namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
         private const ushort FileFormatVersionMajor = 1;
         private const ushort FileFormatVersionMinor = 2;
         private const uint FileFormatVersionPacked = ((uint)FileFormatVersionMajor << 16) | FileFormatVersionMinor;
-        private const uint CodeGenVersion = 6253;
+        private const uint CodeGenVersion = 6455;
 
         private const string SharedTocFileName = "shared.toc";
         private const string SharedDataFileName = "shared.data";
diff --git a/src/Ryujinx.Graphics.Shader/StructuredIr/PhiFunctions.cs b/src/Ryujinx.Graphics.Shader/StructuredIr/PhiFunctions.cs
index 8b1cb9c566..90f1f2f6d8 100644
--- a/src/Ryujinx.Graphics.Shader/StructuredIr/PhiFunctions.cs
+++ b/src/Ryujinx.Graphics.Shader/StructuredIr/PhiFunctions.cs
@@ -24,17 +24,21 @@ namespace Ryujinx.Graphics.Shader.StructuredIr
                         continue;
                     }
 
+                    Operand temp = OperandHelper.Local();
+
                     for (int index = 0; index < phi.SourcesCount; index++)
                     {
                         Operand src = phi.GetSource(index);
-
                         BasicBlock srcBlock = phi.GetBlock(index);
 
-                        Operation copyOp = new(Instruction.Copy, phi.Dest, src);
+                        Operation copyOp = new(Instruction.Copy, temp, src);
 
                         srcBlock.Append(copyOp);
                     }
 
+                    Operation copyOp2 = new(Instruction.Copy, phi.Dest, temp);
+
+                    nextNode = block.Operations.AddAfter(node, copyOp2).Next;
                     block.Operations.Remove(node);
 
                     node = nextNode;