Optimizer.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. // TODO: Handle Load operations with the same storage and the same constant parameters.
  135. return x.Type == OperandType.Constant || x.Type == OperandType.ConstantBuffer;
  136. }
  137. private static bool PropagatePack(Operation packOp)
  138. {
  139. // Propagate pack source operands to uses by unpack
  140. // instruction. The source depends on the unpack instruction.
  141. bool modified = false;
  142. Operand dest = packOp.Dest;
  143. Operand src0 = packOp.GetSource(0);
  144. Operand src1 = packOp.GetSource(1);
  145. INode[] uses = dest.UseOps.ToArray();
  146. foreach (INode useNode in uses)
  147. {
  148. if (!(useNode is Operation operation) || operation.Inst != Instruction.UnpackHalf2x16)
  149. {
  150. continue;
  151. }
  152. if (operation.GetSource(0) == dest)
  153. {
  154. operation.TurnIntoCopy(operation.Index == 1 ? src1 : src0);
  155. modified = true;
  156. }
  157. }
  158. return modified;
  159. }
  160. public static bool MatchDdxOrDdy(Operation operation)
  161. {
  162. // It's assumed that "operation.Inst" is ShuffleXor,
  163. // that should be checked before calling this method.
  164. Debug.Assert(operation.Inst == Instruction.ShuffleXor);
  165. bool modified = false;
  166. Operand src2 = operation.GetSource(1);
  167. Operand src3 = operation.GetSource(2);
  168. if (src2.Type != OperandType.Constant || (src2.Value != 1 && src2.Value != 2))
  169. {
  170. return false;
  171. }
  172. if (src3.Type != OperandType.Constant || src3.Value != 0x1c03)
  173. {
  174. return false;
  175. }
  176. bool isDdy = src2.Value == 2;
  177. bool isDdx = !isDdy;
  178. // We can replace any use by a FSWZADD with DDX/DDY, when
  179. // the following conditions are true:
  180. // - The mask should be 0b10100101 for DDY, or 0b10011001 for DDX.
  181. // - The first source operand must be the shuffle output.
  182. // - The second source operand must be the shuffle first source operand.
  183. INode[] uses = operation.Dest.UseOps.ToArray();
  184. foreach (INode use in uses)
  185. {
  186. if (!(use is Operation test))
  187. {
  188. continue;
  189. }
  190. if (!(use is Operation useOp) || useOp.Inst != Instruction.SwizzleAdd)
  191. {
  192. continue;
  193. }
  194. Operand fswzaddSrc1 = useOp.GetSource(0);
  195. Operand fswzaddSrc2 = useOp.GetSource(1);
  196. Operand fswzaddSrc3 = useOp.GetSource(2);
  197. if (fswzaddSrc1 != operation.Dest)
  198. {
  199. continue;
  200. }
  201. if (fswzaddSrc2 != operation.GetSource(0))
  202. {
  203. continue;
  204. }
  205. if (fswzaddSrc3.Type != OperandType.Constant)
  206. {
  207. continue;
  208. }
  209. int mask = fswzaddSrc3.Value;
  210. if ((isDdx && mask != 0b10011001) ||
  211. (isDdy && mask != 0b10100101))
  212. {
  213. continue;
  214. }
  215. useOp.TurnInto(isDdx ? Instruction.Ddx : Instruction.Ddy, fswzaddSrc2);
  216. modified = true;
  217. }
  218. return modified;
  219. }
  220. private static void RemoveNode(BasicBlock block, LinkedListNode<INode> llNode)
  221. {
  222. // Remove a node from the nodes list, and also remove itself
  223. // from all the use lists on the operands that this node uses.
  224. block.Operations.Remove(llNode);
  225. Queue<INode> nodes = new Queue<INode>();
  226. nodes.Enqueue(llNode.Value);
  227. while (nodes.TryDequeue(out INode node))
  228. {
  229. for (int index = 0; index < node.SourcesCount; index++)
  230. {
  231. Operand src = node.GetSource(index);
  232. if (src.Type != OperandType.LocalVariable)
  233. {
  234. continue;
  235. }
  236. if (src.UseOps.Remove(node) && src.UseOps.Count == 0)
  237. {
  238. Debug.Assert(src.AsgOp != null);
  239. nodes.Enqueue(src.AsgOp);
  240. }
  241. }
  242. }
  243. }
  244. private static bool IsUnused(INode node)
  245. {
  246. return !HasSideEffects(node) && DestIsLocalVar(node) && DestHasNoUses(node);
  247. }
  248. private static bool HasSideEffects(INode node)
  249. {
  250. if (node is Operation operation)
  251. {
  252. switch (operation.Inst & Instruction.Mask)
  253. {
  254. case Instruction.AtomicAdd:
  255. case Instruction.AtomicAnd:
  256. case Instruction.AtomicCompareAndSwap:
  257. case Instruction.AtomicMaxS32:
  258. case Instruction.AtomicMaxU32:
  259. case Instruction.AtomicMinS32:
  260. case Instruction.AtomicMinU32:
  261. case Instruction.AtomicOr:
  262. case Instruction.AtomicSwap:
  263. case Instruction.AtomicXor:
  264. case Instruction.Call:
  265. case Instruction.ImageAtomic:
  266. return true;
  267. }
  268. }
  269. return false;
  270. }
  271. private static bool DestIsLocalVar(INode node)
  272. {
  273. if (node.DestsCount == 0)
  274. {
  275. return false;
  276. }
  277. for (int index = 0; index < node.DestsCount; index++)
  278. {
  279. Operand dest = node.GetDest(index);
  280. if (dest != null && dest.Type != OperandType.LocalVariable)
  281. {
  282. return false;
  283. }
  284. }
  285. return true;
  286. }
  287. private static bool DestHasNoUses(INode node)
  288. {
  289. for (int index = 0; index < node.DestsCount; index++)
  290. {
  291. Operand dest = node.GetDest(index);
  292. if (dest != null && dest.UseOps.Count != 0)
  293. {
  294. return false;
  295. }
  296. }
  297. return true;
  298. }
  299. }
  300. }