ConstantFolding.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. using Ryujinx.Graphics.Shader.Decoders;
  2. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  3. using System;
  4. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  5. namespace Ryujinx.Graphics.Shader.Translation.Optimizations
  6. {
  7. static class ConstantFolding
  8. {
  9. public static void RunPass(Operation operation)
  10. {
  11. if (!AreAllSourcesConstant(operation))
  12. {
  13. return;
  14. }
  15. switch (operation.Inst)
  16. {
  17. case Instruction.Add:
  18. EvaluateBinary(operation, (x, y) => x + y);
  19. break;
  20. case Instruction.BitCount:
  21. EvaluateUnary(operation, (x) => BitCount(x));
  22. break;
  23. case Instruction.BitwiseAnd:
  24. EvaluateBinary(operation, (x, y) => x & y);
  25. break;
  26. case Instruction.BitwiseExclusiveOr:
  27. EvaluateBinary(operation, (x, y) => x ^ y);
  28. break;
  29. case Instruction.BitwiseNot:
  30. EvaluateUnary(operation, (x) => ~x);
  31. break;
  32. case Instruction.BitwiseOr:
  33. EvaluateBinary(operation, (x, y) => x | y);
  34. break;
  35. case Instruction.BitfieldExtractS32:
  36. BitfieldExtractS32(operation);
  37. break;
  38. case Instruction.BitfieldExtractU32:
  39. BitfieldExtractU32(operation);
  40. break;
  41. case Instruction.Clamp:
  42. EvaluateTernary(operation, (x, y, z) => Math.Clamp(x, y, z));
  43. break;
  44. case Instruction.ClampU32:
  45. EvaluateTernary(operation, (x, y, z) => (int)Math.Clamp((uint)x, (uint)y, (uint)z));
  46. break;
  47. case Instruction.CompareEqual:
  48. EvaluateBinary(operation, (x, y) => x == y);
  49. break;
  50. case Instruction.CompareGreater:
  51. EvaluateBinary(operation, (x, y) => x > y);
  52. break;
  53. case Instruction.CompareGreaterOrEqual:
  54. EvaluateBinary(operation, (x, y) => x >= y);
  55. break;
  56. case Instruction.CompareGreaterOrEqualU32:
  57. EvaluateBinary(operation, (x, y) => (uint)x >= (uint)y);
  58. break;
  59. case Instruction.CompareGreaterU32:
  60. EvaluateBinary(operation, (x, y) => (uint)x > (uint)y);
  61. break;
  62. case Instruction.CompareLess:
  63. EvaluateBinary(operation, (x, y) => x < y);
  64. break;
  65. case Instruction.CompareLessOrEqual:
  66. EvaluateBinary(operation, (x, y) => x <= y);
  67. break;
  68. case Instruction.CompareLessOrEqualU32:
  69. EvaluateBinary(operation, (x, y) => (uint)x <= (uint)y);
  70. break;
  71. case Instruction.CompareLessU32:
  72. EvaluateBinary(operation, (x, y) => (uint)x < (uint)y);
  73. break;
  74. case Instruction.CompareNotEqual:
  75. EvaluateBinary(operation, (x, y) => x != y);
  76. break;
  77. case Instruction.Divide:
  78. EvaluateBinary(operation, (x, y) => y != 0 ? x / y : 0);
  79. break;
  80. case Instruction.FP32 | Instruction.Add:
  81. EvaluateFPBinary(operation, (x, y) => x + y);
  82. break;
  83. case Instruction.FP32 | Instruction.Clamp:
  84. EvaluateFPTernary(operation, (x, y, z) => Math.Clamp(x, y, z));
  85. break;
  86. case Instruction.FP32 | Instruction.CompareEqual:
  87. EvaluateFPBinary(operation, (x, y) => x == y);
  88. break;
  89. case Instruction.FP32 | Instruction.CompareGreater:
  90. EvaluateFPBinary(operation, (x, y) => x > y);
  91. break;
  92. case Instruction.FP32 | Instruction.CompareGreaterOrEqual:
  93. EvaluateFPBinary(operation, (x, y) => x >= y);
  94. break;
  95. case Instruction.FP32 | Instruction.CompareLess:
  96. EvaluateFPBinary(operation, (x, y) => x < y);
  97. break;
  98. case Instruction.FP32 | Instruction.CompareLessOrEqual:
  99. EvaluateFPBinary(operation, (x, y) => x <= y);
  100. break;
  101. case Instruction.FP32 | Instruction.CompareNotEqual:
  102. EvaluateFPBinary(operation, (x, y) => x != y);
  103. break;
  104. case Instruction.FP32 | Instruction.Divide:
  105. EvaluateFPBinary(operation, (x, y) => x / y);
  106. break;
  107. case Instruction.FP32 | Instruction.Multiply:
  108. EvaluateFPBinary(operation, (x, y) => x * y);
  109. break;
  110. case Instruction.FP32 | Instruction.Negate:
  111. EvaluateFPUnary(operation, (x) => -x);
  112. break;
  113. case Instruction.FP32 | Instruction.Subtract:
  114. EvaluateFPBinary(operation, (x, y) => x - y);
  115. break;
  116. case Instruction.IsNan:
  117. EvaluateFPUnary(operation, (x) => float.IsNaN(x));
  118. break;
  119. case Instruction.LoadConstant:
  120. operation.TurnIntoCopy(Cbuf(operation.GetSource(0).Value, operation.GetSource(1).Value));
  121. break;
  122. case Instruction.Maximum:
  123. EvaluateBinary(operation, (x, y) => Math.Max(x, y));
  124. break;
  125. case Instruction.MaximumU32:
  126. EvaluateBinary(operation, (x, y) => (int)Math.Max((uint)x, (uint)y));
  127. break;
  128. case Instruction.Minimum:
  129. EvaluateBinary(operation, (x, y) => Math.Min(x, y));
  130. break;
  131. case Instruction.MinimumU32:
  132. EvaluateBinary(operation, (x, y) => (int)Math.Min((uint)x, (uint)y));
  133. break;
  134. case Instruction.Multiply:
  135. EvaluateBinary(operation, (x, y) => x * y);
  136. break;
  137. case Instruction.Negate:
  138. EvaluateUnary(operation, (x) => -x);
  139. break;
  140. case Instruction.ShiftLeft:
  141. EvaluateBinary(operation, (x, y) => x << y);
  142. break;
  143. case Instruction.ShiftRightS32:
  144. EvaluateBinary(operation, (x, y) => x >> y);
  145. break;
  146. case Instruction.ShiftRightU32:
  147. EvaluateBinary(operation, (x, y) => (int)((uint)x >> y));
  148. break;
  149. case Instruction.Subtract:
  150. EvaluateBinary(operation, (x, y) => x - y);
  151. break;
  152. case Instruction.UnpackHalf2x16:
  153. UnpackHalf2x16(operation);
  154. break;
  155. }
  156. }
  157. private static bool AreAllSourcesConstant(Operation operation)
  158. {
  159. for (int index = 0; index < operation.SourcesCount; index++)
  160. {
  161. if (operation.GetSource(index).Type != OperandType.Constant)
  162. {
  163. return false;
  164. }
  165. }
  166. return true;
  167. }
  168. private static int BitCount(int value)
  169. {
  170. int count = 0;
  171. for (int bit = 0; bit < 32; bit++)
  172. {
  173. if (value.Extract(bit))
  174. {
  175. count++;
  176. }
  177. }
  178. return count;
  179. }
  180. private static void BitfieldExtractS32(Operation operation)
  181. {
  182. int value = GetBitfieldExtractValue(operation);
  183. int shift = 32 - operation.GetSource(2).Value;
  184. value = (value << shift) >> shift;
  185. operation.TurnIntoCopy(Const(value));
  186. }
  187. private static void BitfieldExtractU32(Operation operation)
  188. {
  189. operation.TurnIntoCopy(Const(GetBitfieldExtractValue(operation)));
  190. }
  191. private static int GetBitfieldExtractValue(Operation operation)
  192. {
  193. int value = operation.GetSource(0).Value;
  194. int lsb = operation.GetSource(1).Value;
  195. int length = operation.GetSource(2).Value;
  196. return value.Extract(lsb, length);
  197. }
  198. private static void UnpackHalf2x16(Operation operation)
  199. {
  200. int value = operation.GetSource(0).Value;
  201. value = (value >> operation.Index * 16) & 0xffff;
  202. operation.TurnIntoCopy(ConstF((float)BitConverter.UInt16BitsToHalf((ushort)value)));
  203. }
  204. private static void FPNegate(Operation operation)
  205. {
  206. float value = operation.GetSource(0).AsFloat();
  207. operation.TurnIntoCopy(ConstF(-value));
  208. }
  209. private static void EvaluateUnary(Operation operation, Func<int, int> op)
  210. {
  211. int x = operation.GetSource(0).Value;
  212. operation.TurnIntoCopy(Const(op(x)));
  213. }
  214. private static void EvaluateFPUnary(Operation operation, Func<float, float> op)
  215. {
  216. float x = operation.GetSource(0).AsFloat();
  217. operation.TurnIntoCopy(ConstF(op(x)));
  218. }
  219. private static void EvaluateFPUnary(Operation operation, Func<float, bool> op)
  220. {
  221. float x = operation.GetSource(0).AsFloat();
  222. operation.TurnIntoCopy(Const(op(x) ? IrConsts.True : IrConsts.False));
  223. }
  224. private static void EvaluateBinary(Operation operation, Func<int, int, int> op)
  225. {
  226. int x = operation.GetSource(0).Value;
  227. int y = operation.GetSource(1).Value;
  228. operation.TurnIntoCopy(Const(op(x, y)));
  229. }
  230. private static void EvaluateBinary(Operation operation, Func<int, int, bool> op)
  231. {
  232. int x = operation.GetSource(0).Value;
  233. int y = operation.GetSource(1).Value;
  234. operation.TurnIntoCopy(Const(op(x, y) ? IrConsts.True : IrConsts.False));
  235. }
  236. private static void EvaluateFPBinary(Operation operation, Func<float, float, float> op)
  237. {
  238. float x = operation.GetSource(0).AsFloat();
  239. float y = operation.GetSource(1).AsFloat();
  240. operation.TurnIntoCopy(ConstF(op(x, y)));
  241. }
  242. private static void EvaluateFPBinary(Operation operation, Func<float, float, bool> op)
  243. {
  244. float x = operation.GetSource(0).AsFloat();
  245. float y = operation.GetSource(1).AsFloat();
  246. operation.TurnIntoCopy(Const(op(x, y) ? IrConsts.True : IrConsts.False));
  247. }
  248. private static void EvaluateTernary(Operation operation, Func<int, int, int, int> op)
  249. {
  250. int x = operation.GetSource(0).Value;
  251. int y = operation.GetSource(1).Value;
  252. int z = operation.GetSource(2).Value;
  253. operation.TurnIntoCopy(Const(op(x, y, z)));
  254. }
  255. private static void EvaluateFPTernary(Operation operation, Func<float, float, float, float> op)
  256. {
  257. float x = operation.GetSource(0).AsFloat();
  258. float y = operation.GetSource(1).AsFloat();
  259. float z = operation.GetSource(2).AsFloat();
  260. operation.TurnIntoCopy(ConstF(op(x, y, z)));
  261. }
  262. }
  263. }