Optimizer.cs 12 KB

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