Optimizer.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 (node.Value is PhiNode phi && !isUnused)
  39. {
  40. isUnused = PropagatePhi(phi);
  41. }
  42. if (isUnused)
  43. {
  44. RemoveNode(block, node);
  45. modified = true;
  46. }
  47. node = nextNode;
  48. continue;
  49. }
  50. ConstantFolding.RunPass(operation);
  51. Simplification.RunPass(operation);
  52. if (DestIsLocalVar(operation))
  53. {
  54. if (operation.Inst == Instruction.Copy)
  55. {
  56. PropagateCopy(operation);
  57. RemoveNode(block, node);
  58. modified = true;
  59. }
  60. else if ((operation.Inst == Instruction.PackHalf2x16 && PropagatePack(operation)) ||
  61. (operation.Inst == Instruction.ShuffleXor && MatchDdxOrDdy(operation)))
  62. {
  63. if (DestHasNoUses(operation))
  64. {
  65. RemoveNode(block, node);
  66. }
  67. modified = true;
  68. }
  69. }
  70. node = nextNode;
  71. }
  72. if (BranchElimination.RunPass(block))
  73. {
  74. RemoveNode(block, block.Operations.Last);
  75. modified = true;
  76. }
  77. }
  78. }
  79. while (modified);
  80. }
  81. private static void PropagateCopy(Operation copyOp)
  82. {
  83. // Propagate copy source operand to all uses of
  84. // the destination operand.
  85. Operand dest = copyOp.Dest;
  86. Operand src = copyOp.GetSource(0);
  87. INode[] uses = dest.UseOps.ToArray();
  88. foreach (INode useNode in uses)
  89. {
  90. for (int index = 0; index < useNode.SourcesCount; index++)
  91. {
  92. if (useNode.GetSource(index) == dest)
  93. {
  94. useNode.SetSource(index, src);
  95. }
  96. }
  97. }
  98. }
  99. private static bool PropagatePhi(PhiNode phi)
  100. {
  101. // If all phi sources are the same, we can propagate it and remove the phi.
  102. Operand firstSrc = phi.GetSource(0);
  103. for (int index = 1; index < phi.SourcesCount; index++)
  104. {
  105. if (!IsSameOperand(firstSrc, phi.GetSource(index)))
  106. {
  107. return false;
  108. }
  109. }
  110. // All sources are equal, we can propagate the value.
  111. Operand dest = phi.Dest;
  112. INode[] uses = dest.UseOps.ToArray();
  113. foreach (INode useNode in uses)
  114. {
  115. for (int index = 0; index < useNode.SourcesCount; index++)
  116. {
  117. if (useNode.GetSource(index) == dest)
  118. {
  119. useNode.SetSource(index, firstSrc);
  120. }
  121. }
  122. }
  123. return true;
  124. }
  125. private static bool IsSameOperand(Operand x, Operand y)
  126. {
  127. if (x.Type != y.Type || x.Value != y.Value)
  128. {
  129. return false;
  130. }
  131. return x.Type == OperandType.Attribute ||
  132. x.Type == OperandType.AttributePerPatch ||
  133. x.Type == OperandType.Constant ||
  134. x.Type == OperandType.ConstantBuffer;
  135. }
  136. private static bool PropagatePack(Operation packOp)
  137. {
  138. // Propagate pack source operands to uses by unpack
  139. // instruction. The source depends on the unpack instruction.
  140. bool modified = false;
  141. Operand dest = packOp.Dest;
  142. Operand src0 = packOp.GetSource(0);
  143. Operand src1 = packOp.GetSource(1);
  144. INode[] uses = dest.UseOps.ToArray();
  145. foreach (INode useNode in uses)
  146. {
  147. if (!(useNode is Operation operation) || operation.Inst != Instruction.UnpackHalf2x16)
  148. {
  149. continue;
  150. }
  151. if (operation.GetSource(0) == dest)
  152. {
  153. operation.TurnIntoCopy(operation.Index == 1 ? src1 : src0);
  154. modified = true;
  155. }
  156. }
  157. return modified;
  158. }
  159. public static bool MatchDdxOrDdy(Operation operation)
  160. {
  161. // It's assumed that "operation.Inst" is ShuffleXor,
  162. // that should be checked before calling this method.
  163. Debug.Assert(operation.Inst == Instruction.ShuffleXor);
  164. bool modified = false;
  165. Operand src2 = operation.GetSource(1);
  166. Operand src3 = operation.GetSource(2);
  167. if (src2.Type != OperandType.Constant || (src2.Value != 1 && src2.Value != 2))
  168. {
  169. return false;
  170. }
  171. if (src3.Type != OperandType.Constant || src3.Value != 0x1c03)
  172. {
  173. return false;
  174. }
  175. bool isDdy = src2.Value == 2;
  176. bool isDdx = !isDdy;
  177. // We can replace any use by a FSWZADD with DDX/DDY, when
  178. // the following conditions are true:
  179. // - The mask should be 0b10100101 for DDY, or 0b10011001 for DDX.
  180. // - The first source operand must be the shuffle output.
  181. // - The second source operand must be the shuffle first source operand.
  182. INode[] uses = operation.Dest.UseOps.ToArray();
  183. foreach (INode use in uses)
  184. {
  185. if (!(use is Operation test))
  186. {
  187. continue;
  188. }
  189. if (!(use is Operation useOp) || useOp.Inst != Instruction.SwizzleAdd)
  190. {
  191. continue;
  192. }
  193. Operand fswzaddSrc1 = useOp.GetSource(0);
  194. Operand fswzaddSrc2 = useOp.GetSource(1);
  195. Operand fswzaddSrc3 = useOp.GetSource(2);
  196. if (fswzaddSrc1 != operation.Dest)
  197. {
  198. continue;
  199. }
  200. if (fswzaddSrc2 != operation.GetSource(0))
  201. {
  202. continue;
  203. }
  204. if (fswzaddSrc3.Type != OperandType.Constant)
  205. {
  206. continue;
  207. }
  208. int mask = fswzaddSrc3.Value;
  209. if ((isDdx && mask != 0b10011001) ||
  210. (isDdy && mask != 0b10100101))
  211. {
  212. continue;
  213. }
  214. useOp.TurnInto(isDdx ? Instruction.Ddx : Instruction.Ddy, fswzaddSrc2);
  215. modified = true;
  216. }
  217. return modified;
  218. }
  219. private static void RemoveNode(BasicBlock block, LinkedListNode<INode> llNode)
  220. {
  221. // Remove a node from the nodes list, and also remove itself
  222. // from all the use lists on the operands that this node uses.
  223. block.Operations.Remove(llNode);
  224. Queue<INode> nodes = new Queue<INode>();
  225. nodes.Enqueue(llNode.Value);
  226. while (nodes.TryDequeue(out INode node))
  227. {
  228. for (int index = 0; index < node.SourcesCount; index++)
  229. {
  230. Operand src = node.GetSource(index);
  231. if (src.Type != OperandType.LocalVariable)
  232. {
  233. continue;
  234. }
  235. if (src.UseOps.Remove(node) && src.UseOps.Count == 0)
  236. {
  237. Debug.Assert(src.AsgOp != null);
  238. nodes.Enqueue(src.AsgOp);
  239. }
  240. }
  241. }
  242. }
  243. private static bool IsUnused(INode node)
  244. {
  245. return !HasSideEffects(node) && DestIsLocalVar(node) && DestHasNoUses(node);
  246. }
  247. private static bool HasSideEffects(INode node)
  248. {
  249. if (node is Operation operation)
  250. {
  251. switch (operation.Inst & Instruction.Mask)
  252. {
  253. case Instruction.AtomicAdd:
  254. case Instruction.AtomicAnd:
  255. case Instruction.AtomicCompareAndSwap:
  256. case Instruction.AtomicMaxS32:
  257. case Instruction.AtomicMaxU32:
  258. case Instruction.AtomicMinS32:
  259. case Instruction.AtomicMinU32:
  260. case Instruction.AtomicOr:
  261. case Instruction.AtomicSwap:
  262. case Instruction.AtomicXor:
  263. case Instruction.Call:
  264. case Instruction.ImageAtomic:
  265. return true;
  266. }
  267. }
  268. return false;
  269. }
  270. private static bool DestIsLocalVar(INode node)
  271. {
  272. if (node.DestsCount == 0)
  273. {
  274. return false;
  275. }
  276. for (int index = 0; index < node.DestsCount; index++)
  277. {
  278. Operand dest = node.GetDest(index);
  279. if (dest != null && dest.Type != OperandType.LocalVariable)
  280. {
  281. return false;
  282. }
  283. }
  284. return true;
  285. }
  286. private static bool DestHasNoUses(INode node)
  287. {
  288. for (int index = 0; index < node.DestsCount; index++)
  289. {
  290. Operand dest = node.GetDest(index);
  291. if (dest != null && dest.UseOps.Count != 0)
  292. {
  293. return false;
  294. }
  295. }
  296. return true;
  297. }
  298. }
  299. }