Optimizer.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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(HelperFunctionManager hfm, BasicBlock[] blocks, ShaderConfig config)
  10. {
  11. RunOptimizationPasses(blocks, config);
  12. GlobalToStorage.RunPass(hfm, blocks, config);
  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. BindlessToIndexed.RunPass(blocks[blkIndex], config);
  17. BindlessElimination.RunPass(blocks[blkIndex], config);
  18. // FragmentCoord only exists on fragment shaders, so we don't need to check other stages.
  19. if (config.Stage == ShaderStage.Fragment)
  20. {
  21. EliminateMultiplyByFragmentCoordW(blocks[blkIndex]);
  22. }
  23. }
  24. // Run optimizations one last time to remove any code that is now optimizable after above passes.
  25. RunOptimizationPasses(blocks, config);
  26. }
  27. private static void RunOptimizationPasses(BasicBlock[] blocks, ShaderConfig config)
  28. {
  29. bool modified;
  30. do
  31. {
  32. modified = false;
  33. for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
  34. {
  35. BasicBlock block = blocks[blkIndex];
  36. LinkedListNode<INode> node = block.Operations.First;
  37. while (node != null)
  38. {
  39. LinkedListNode<INode> nextNode = node.Next;
  40. bool isUnused = IsUnused(node.Value);
  41. if (!(node.Value is Operation operation) || isUnused)
  42. {
  43. if (node.Value is PhiNode phi && !isUnused)
  44. {
  45. isUnused = PropagatePhi(phi);
  46. }
  47. if (isUnused)
  48. {
  49. RemoveNode(block, node);
  50. modified = true;
  51. }
  52. node = nextNode;
  53. continue;
  54. }
  55. ConstantFolding.RunPass(config, operation);
  56. Simplification.RunPass(operation);
  57. if (DestIsLocalVar(operation))
  58. {
  59. if (operation.Inst == Instruction.Copy)
  60. {
  61. PropagateCopy(operation);
  62. RemoveNode(block, node);
  63. modified = true;
  64. }
  65. else if ((operation.Inst == Instruction.PackHalf2x16 && PropagatePack(operation)) ||
  66. (operation.Inst == Instruction.ShuffleXor && MatchDdxOrDdy(operation)))
  67. {
  68. if (DestHasNoUses(operation))
  69. {
  70. RemoveNode(block, node);
  71. }
  72. modified = true;
  73. }
  74. }
  75. node = nextNode;
  76. }
  77. if (BranchElimination.RunPass(block))
  78. {
  79. RemoveNode(block, block.Operations.Last);
  80. modified = true;
  81. }
  82. }
  83. }
  84. while (modified);
  85. }
  86. private static void PropagateCopy(Operation copyOp)
  87. {
  88. // Propagate copy source operand to all uses of
  89. // the destination operand.
  90. Operand dest = copyOp.Dest;
  91. Operand src = copyOp.GetSource(0);
  92. INode[] uses = dest.UseOps.ToArray();
  93. foreach (INode useNode in uses)
  94. {
  95. for (int index = 0; index < useNode.SourcesCount; index++)
  96. {
  97. if (useNode.GetSource(index) == dest)
  98. {
  99. useNode.SetSource(index, src);
  100. }
  101. }
  102. }
  103. }
  104. private static bool PropagatePhi(PhiNode phi)
  105. {
  106. // If all phi sources are the same, we can propagate it and remove the phi.
  107. Operand firstSrc = phi.GetSource(0);
  108. for (int index = 1; index < phi.SourcesCount; index++)
  109. {
  110. if (!IsSameOperand(firstSrc, phi.GetSource(index)))
  111. {
  112. return false;
  113. }
  114. }
  115. // All sources are equal, we can propagate the value.
  116. Operand dest = phi.Dest;
  117. INode[] uses = dest.UseOps.ToArray();
  118. foreach (INode useNode in uses)
  119. {
  120. for (int index = 0; index < useNode.SourcesCount; index++)
  121. {
  122. if (useNode.GetSource(index) == dest)
  123. {
  124. useNode.SetSource(index, firstSrc);
  125. }
  126. }
  127. }
  128. return true;
  129. }
  130. private static bool IsSameOperand(Operand x, Operand y)
  131. {
  132. if (x.Type != y.Type || x.Value != y.Value)
  133. {
  134. return false;
  135. }
  136. // TODO: Handle Load operations with the same storage and the same constant parameters.
  137. return x.Type == OperandType.Constant || 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 EliminateMultiplyByFragmentCoordW(BasicBlock block)
  223. {
  224. foreach (INode node in block.Operations)
  225. {
  226. if (node is Operation operation)
  227. {
  228. EliminateMultiplyByFragmentCoordW(operation);
  229. }
  230. }
  231. }
  232. private static void EliminateMultiplyByFragmentCoordW(Operation operation)
  233. {
  234. // We're looking for the pattern:
  235. // y = x * gl_FragCoord.w
  236. // v = y * (1.0 / gl_FragCoord.w)
  237. // Then we transform it into:
  238. // v = x
  239. // This pattern is common on fragment shaders due to the way how perspective correction is done.
  240. // We are expecting a multiplication by the reciprocal of gl_FragCoord.w.
  241. if (operation.Inst != (Instruction.FP32 | Instruction.Multiply))
  242. {
  243. return;
  244. }
  245. Operand lhs = operation.GetSource(0);
  246. Operand rhs = operation.GetSource(1);
  247. // Check LHS of the the main multiplication operation. We expect an input being multiplied by gl_FragCoord.w.
  248. if (!(lhs.AsgOp is Operation attrMulOp) || attrMulOp.Inst != (Instruction.FP32 | Instruction.Multiply))
  249. {
  250. return;
  251. }
  252. Operand attrMulLhs = attrMulOp.GetSource(0);
  253. Operand attrMulRhs = attrMulOp.GetSource(1);
  254. // LHS should be any input, RHS should be exactly gl_FragCoord.w.
  255. if (!Utils.IsInputLoad(attrMulLhs.AsgOp) || !Utils.IsInputLoad(attrMulRhs.AsgOp, IoVariable.FragmentCoord, 3))
  256. {
  257. return;
  258. }
  259. // RHS of the main multiplication should be a reciprocal operation (1.0 / x).
  260. if (!(rhs.AsgOp is Operation reciprocalOp) || reciprocalOp.Inst != (Instruction.FP32 | Instruction.Divide))
  261. {
  262. return;
  263. }
  264. Operand reciprocalLhs = reciprocalOp.GetSource(0);
  265. Operand reciprocalRhs = reciprocalOp.GetSource(1);
  266. // Check if the divisor is a constant equal to 1.0.
  267. if (reciprocalLhs.Type != OperandType.Constant || reciprocalLhs.AsFloat() != 1.0f)
  268. {
  269. return;
  270. }
  271. // Check if the dividend is gl_FragCoord.w.
  272. if (!Utils.IsInputLoad(reciprocalRhs.AsgOp, IoVariable.FragmentCoord, 3))
  273. {
  274. return;
  275. }
  276. // If everything matches, we can replace the operation with the input load result.
  277. operation.TurnIntoCopy(attrMulLhs);
  278. }
  279. private static void RemoveNode(BasicBlock block, LinkedListNode<INode> llNode)
  280. {
  281. // Remove a node from the nodes list, and also remove itself
  282. // from all the use lists on the operands that this node uses.
  283. block.Operations.Remove(llNode);
  284. Queue<INode> nodes = new Queue<INode>();
  285. nodes.Enqueue(llNode.Value);
  286. while (nodes.TryDequeue(out INode node))
  287. {
  288. for (int index = 0; index < node.SourcesCount; index++)
  289. {
  290. Operand src = node.GetSource(index);
  291. if (src.Type != OperandType.LocalVariable)
  292. {
  293. continue;
  294. }
  295. if (src.UseOps.Remove(node) && src.UseOps.Count == 0)
  296. {
  297. Debug.Assert(src.AsgOp != null);
  298. nodes.Enqueue(src.AsgOp);
  299. }
  300. }
  301. }
  302. }
  303. private static bool IsUnused(INode node)
  304. {
  305. return !HasSideEffects(node) && DestIsLocalVar(node) && DestHasNoUses(node);
  306. }
  307. private static bool HasSideEffects(INode node)
  308. {
  309. if (node is Operation operation)
  310. {
  311. switch (operation.Inst & Instruction.Mask)
  312. {
  313. case Instruction.AtomicAdd:
  314. case Instruction.AtomicAnd:
  315. case Instruction.AtomicCompareAndSwap:
  316. case Instruction.AtomicMaxS32:
  317. case Instruction.AtomicMaxU32:
  318. case Instruction.AtomicMinS32:
  319. case Instruction.AtomicMinU32:
  320. case Instruction.AtomicOr:
  321. case Instruction.AtomicSwap:
  322. case Instruction.AtomicXor:
  323. case Instruction.Call:
  324. case Instruction.ImageAtomic:
  325. return true;
  326. }
  327. }
  328. return false;
  329. }
  330. private static bool DestIsLocalVar(INode node)
  331. {
  332. if (node.DestsCount == 0)
  333. {
  334. return false;
  335. }
  336. for (int index = 0; index < node.DestsCount; index++)
  337. {
  338. Operand dest = node.GetDest(index);
  339. if (dest != null && dest.Type != OperandType.LocalVariable)
  340. {
  341. return false;
  342. }
  343. }
  344. return true;
  345. }
  346. private static bool DestHasNoUses(INode node)
  347. {
  348. for (int index = 0; index < node.DestsCount; index++)
  349. {
  350. Operand dest = node.GetDest(index);
  351. if (dest != null && dest.UseOps.Count != 0)
  352. {
  353. return false;
  354. }
  355. }
  356. return true;
  357. }
  358. }
  359. }