Optimizer.cs 9.5 KB

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