Optimizer.cs 12 KB

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