Optimizer.cs 10 KB

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