Optimizer.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. namespace Ryujinx.Graphics.Shader.Translation.Optimizations
  6. {
  7. static class Optimizer
  8. {
  9. public static void RunPass(BasicBlock[] blocks, ShaderConfig config)
  10. {
  11. RunOptimizationPasses(blocks);
  12. // Those passes are looking for specific patterns and only needs to run once.
  13. for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
  14. {
  15. GlobalToStorage.RunPass(blocks[blkIndex], config);
  16. BindlessToIndexed.RunPass(blocks[blkIndex], config);
  17. BindlessElimination.RunPass(blocks[blkIndex], config);
  18. }
  19. // Run optimizations one last time to remove any code that is now optimizable after above passes.
  20. RunOptimizationPasses(blocks);
  21. }
  22. private static void RunOptimizationPasses(BasicBlock[] blocks)
  23. {
  24. bool modified;
  25. do
  26. {
  27. modified = false;
  28. for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
  29. {
  30. BasicBlock block = blocks[blkIndex];
  31. LinkedListNode<INode> node = block.Operations.First;
  32. while (node != null)
  33. {
  34. LinkedListNode<INode> nextNode = node.Next;
  35. bool isUnused = IsUnused(node.Value);
  36. if (!(node.Value is Operation operation) || isUnused)
  37. {
  38. if (isUnused)
  39. {
  40. RemoveNode(block, node);
  41. modified = true;
  42. }
  43. node = nextNode;
  44. continue;
  45. }
  46. ConstantFolding.RunPass(operation);
  47. Simplification.RunPass(operation);
  48. if (DestIsLocalVar(operation))
  49. {
  50. if (operation.Inst == Instruction.Copy)
  51. {
  52. PropagateCopy(operation);
  53. RemoveNode(block, node);
  54. modified = true;
  55. }
  56. else if ((operation.Inst == Instruction.PackHalf2x16 && PropagatePack(operation)) ||
  57. (operation.Inst == Instruction.ShuffleXor && MatchDdxOrDdy(operation)))
  58. {
  59. if (DestHasNoUses(operation))
  60. {
  61. RemoveNode(block, node);
  62. }
  63. modified = true;
  64. }
  65. }
  66. node = nextNode;
  67. }
  68. if (BranchElimination.RunPass(block))
  69. {
  70. RemoveNode(block, block.Operations.Last);
  71. modified = true;
  72. }
  73. }
  74. }
  75. while (modified);
  76. }
  77. private static void PropagateCopy(Operation copyOp)
  78. {
  79. // Propagate copy source operand to all uses of
  80. // the destination operand.
  81. Operand dest = copyOp.Dest;
  82. Operand src = copyOp.GetSource(0);
  83. INode[] uses = dest.UseOps.ToArray();
  84. foreach (INode useNode in uses)
  85. {
  86. for (int index = 0; index < useNode.SourcesCount; index++)
  87. {
  88. if (useNode.GetSource(index) == dest)
  89. {
  90. useNode.SetSource(index, src);
  91. }
  92. }
  93. }
  94. }
  95. private static bool PropagatePack(Operation packOp)
  96. {
  97. // Propagate pack source operands to uses by unpack
  98. // instruction. The source depends on the unpack instruction.
  99. bool modified = false;
  100. Operand dest = packOp.Dest;
  101. Operand src0 = packOp.GetSource(0);
  102. Operand src1 = packOp.GetSource(1);
  103. INode[] uses = dest.UseOps.ToArray();
  104. foreach (INode useNode in uses)
  105. {
  106. if (!(useNode is Operation operation) || operation.Inst != Instruction.UnpackHalf2x16)
  107. {
  108. continue;
  109. }
  110. if (operation.GetSource(0) == dest)
  111. {
  112. operation.TurnIntoCopy(operation.Index == 1 ? src1 : src0);
  113. modified = true;
  114. }
  115. }
  116. return modified;
  117. }
  118. public static bool MatchDdxOrDdy(Operation operation)
  119. {
  120. // It's assumed that "operation.Inst" is ShuffleXor,
  121. // that should be checked before calling this method.
  122. Debug.Assert(operation.Inst == Instruction.ShuffleXor);
  123. bool modified = false;
  124. Operand src2 = operation.GetSource(1);
  125. Operand src3 = operation.GetSource(2);
  126. if (src2.Type != OperandType.Constant || (src2.Value != 1 && src2.Value != 2))
  127. {
  128. return false;
  129. }
  130. if (src3.Type != OperandType.Constant || src3.Value != 0x1c03)
  131. {
  132. return false;
  133. }
  134. bool isDdy = src2.Value == 2;
  135. bool isDdx = !isDdy;
  136. // We can replace any use by a FSWZADD with DDX/DDY, when
  137. // the following conditions are true:
  138. // - The mask should be 0b10100101 for DDY, or 0b10011001 for DDX.
  139. // - The first source operand must be the shuffle output.
  140. // - The second source operand must be the shuffle first source operand.
  141. INode[] uses = operation.Dest.UseOps.ToArray();
  142. foreach (INode use in uses)
  143. {
  144. if (!(use is Operation test))
  145. {
  146. continue;
  147. }
  148. if (!(use is Operation useOp) || useOp.Inst != Instruction.SwizzleAdd)
  149. {
  150. continue;
  151. }
  152. Operand fswzaddSrc1 = useOp.GetSource(0);
  153. Operand fswzaddSrc2 = useOp.GetSource(1);
  154. Operand fswzaddSrc3 = useOp.GetSource(2);
  155. if (fswzaddSrc1 != operation.Dest)
  156. {
  157. continue;
  158. }
  159. if (fswzaddSrc2 != operation.GetSource(0))
  160. {
  161. continue;
  162. }
  163. if (fswzaddSrc3.Type != OperandType.Constant)
  164. {
  165. continue;
  166. }
  167. int mask = fswzaddSrc3.Value;
  168. if ((isDdx && mask != 0b10011001) ||
  169. (isDdy && mask != 0b10100101))
  170. {
  171. continue;
  172. }
  173. useOp.TurnInto(isDdx ? Instruction.Ddx : Instruction.Ddy, fswzaddSrc2);
  174. modified = true;
  175. }
  176. return modified;
  177. }
  178. private static void RemoveNode(BasicBlock block, LinkedListNode<INode> llNode)
  179. {
  180. // Remove a node from the nodes list, and also remove itself
  181. // from all the use lists on the operands that this node uses.
  182. block.Operations.Remove(llNode);
  183. Queue<INode> nodes = new Queue<INode>();
  184. nodes.Enqueue(llNode.Value);
  185. while (nodes.TryDequeue(out INode node))
  186. {
  187. for (int index = 0; index < node.SourcesCount; index++)
  188. {
  189. Operand src = node.GetSource(index);
  190. if (src.Type != OperandType.LocalVariable)
  191. {
  192. continue;
  193. }
  194. if (src.UseOps.Remove(node) && src.UseOps.Count == 0)
  195. {
  196. Debug.Assert(src.AsgOp != null);
  197. nodes.Enqueue(src.AsgOp);
  198. }
  199. }
  200. }
  201. }
  202. private static bool IsUnused(INode node)
  203. {
  204. return !HasSideEffects(node) && DestIsLocalVar(node) && DestHasNoUses(node);
  205. }
  206. private static bool HasSideEffects(INode node)
  207. {
  208. if (node is Operation operation)
  209. {
  210. switch (operation.Inst & Instruction.Mask)
  211. {
  212. case Instruction.AtomicAdd:
  213. case Instruction.AtomicAnd:
  214. case Instruction.AtomicCompareAndSwap:
  215. case Instruction.AtomicMaxS32:
  216. case Instruction.AtomicMaxU32:
  217. case Instruction.AtomicMinS32:
  218. case Instruction.AtomicMinU32:
  219. case Instruction.AtomicOr:
  220. case Instruction.AtomicSwap:
  221. case Instruction.AtomicXor:
  222. case Instruction.Call:
  223. case Instruction.ImageAtomic:
  224. return true;
  225. }
  226. }
  227. return false;
  228. }
  229. private static bool DestIsLocalVar(INode node)
  230. {
  231. if (node.DestsCount == 0)
  232. {
  233. return false;
  234. }
  235. for (int index = 0; index < node.DestsCount; index++)
  236. {
  237. Operand dest = node.GetDest(index);
  238. if (dest != null && dest.Type != OperandType.LocalVariable)
  239. {
  240. return false;
  241. }
  242. }
  243. return true;
  244. }
  245. private static bool DestHasNoUses(INode node)
  246. {
  247. for (int index = 0; index < node.DestsCount; index++)
  248. {
  249. Operand dest = node.GetDest(index);
  250. if (dest != null && dest.UseOps.Count != 0)
  251. {
  252. return false;
  253. }
  254. }
  255. return true;
  256. }
  257. }
  258. }