StructuredProgram.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace Ryujinx.Graphics.Shader.StructuredIr
  5. {
  6. static class StructuredProgram
  7. {
  8. public static StructuredProgramInfo MakeStructuredProgram(BasicBlock[] blocks, ShaderConfig config)
  9. {
  10. PhiFunctions.Remove(blocks);
  11. StructuredProgramContext context = new StructuredProgramContext(blocks.Length, config);
  12. for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
  13. {
  14. BasicBlock block = blocks[blkIndex];
  15. context.EnterBlock(block);
  16. foreach (INode node in block.Operations)
  17. {
  18. Operation operation = (Operation)node;
  19. if (IsBranchInst(operation.Inst))
  20. {
  21. context.LeaveBlock(block, operation);
  22. }
  23. else
  24. {
  25. AddOperation(context, operation);
  26. }
  27. }
  28. }
  29. GotoElimination.Eliminate(context.GetGotos());
  30. AstOptimizer.Optimize(context);
  31. return context.Info;
  32. }
  33. private static void AddOperation(StructuredProgramContext context, Operation operation)
  34. {
  35. Instruction inst = operation.Inst;
  36. IAstNode[] sources = new IAstNode[operation.SourcesCount];
  37. for (int index = 0; index < sources.Length; index++)
  38. {
  39. sources[index] = context.GetOperandUse(operation.GetSource(index));
  40. }
  41. int componentMask = 1 << operation.ComponentIndex;
  42. AstTextureOperation GetAstTextureOperation(TextureOperation texOp)
  43. {
  44. return new AstTextureOperation(
  45. inst,
  46. texOp.Type,
  47. texOp.Flags,
  48. texOp.Handle,
  49. componentMask,
  50. sources);
  51. }
  52. if (operation.Dest != null)
  53. {
  54. AstOperand dest = context.GetOperandDef(operation.Dest);
  55. if (inst == Instruction.LoadConstant)
  56. {
  57. Operand slot = operation.GetSource(0);
  58. if (slot.Type != OperandType.Constant)
  59. {
  60. throw new InvalidOperationException("Found load with non-constant constant buffer slot.");
  61. }
  62. context.Info.CBuffers.Add(slot.Value);
  63. }
  64. else if (inst == Instruction.LoadStorage)
  65. {
  66. Operand slot = operation.GetSource(0);
  67. if (slot.Type != OperandType.Constant)
  68. {
  69. throw new InvalidOperationException("Found load or store with non-constant storage buffer slot.");
  70. }
  71. context.Info.SBuffers.Add(slot.Value);
  72. }
  73. AstAssignment assignment;
  74. // If all the sources are bool, it's better to use short-circuiting
  75. // logical operations, rather than forcing a cast to int and doing
  76. // a bitwise operation with the value, as it is likely to be used as
  77. // a bool in the end.
  78. if (IsBitwiseInst(inst) && AreAllSourceTypesEqual(sources, VariableType.Bool))
  79. {
  80. inst = GetLogicalFromBitwiseInst(inst);
  81. }
  82. bool isCondSel = inst == Instruction.ConditionalSelect;
  83. bool isCopy = inst == Instruction.Copy;
  84. if (isCondSel || isCopy)
  85. {
  86. VariableType type = GetVarTypeFromUses(operation.Dest);
  87. if (isCondSel && type == VariableType.F32)
  88. {
  89. inst |= Instruction.FP;
  90. }
  91. dest.VarType = type;
  92. }
  93. else
  94. {
  95. dest.VarType = InstructionInfo.GetDestVarType(inst);
  96. }
  97. IAstNode source;
  98. if (operation is TextureOperation texOp)
  99. {
  100. AstTextureOperation astTexOp = GetAstTextureOperation(texOp);
  101. if (texOp.Inst == Instruction.ImageLoad)
  102. {
  103. context.Info.Images.Add(astTexOp);
  104. }
  105. else
  106. {
  107. context.Info.Samplers.Add(astTexOp);
  108. }
  109. source = astTexOp;
  110. }
  111. else if (!isCopy)
  112. {
  113. source = new AstOperation(inst, componentMask, sources);
  114. }
  115. else
  116. {
  117. source = sources[0];
  118. }
  119. assignment = new AstAssignment(dest, source);
  120. context.AddNode(assignment);
  121. }
  122. else if (operation.Inst == Instruction.Comment)
  123. {
  124. context.AddNode(new AstComment(((CommentNode)operation).Comment));
  125. }
  126. else if (operation is TextureOperation texOp)
  127. {
  128. AstTextureOperation astTexOp = GetAstTextureOperation(texOp);
  129. context.Info.Images.Add(astTexOp);
  130. context.AddNode(astTexOp);
  131. }
  132. else
  133. {
  134. if (inst == Instruction.StoreStorage)
  135. {
  136. Operand slot = operation.GetSource(0);
  137. if (slot.Type != OperandType.Constant)
  138. {
  139. throw new InvalidOperationException("Found load or store with non-constant storage buffer slot.");
  140. }
  141. context.Info.SBuffers.Add(slot.Value);
  142. }
  143. context.AddNode(new AstOperation(inst, sources));
  144. }
  145. }
  146. private static VariableType GetVarTypeFromUses(Operand dest)
  147. {
  148. HashSet<Operand> visited = new HashSet<Operand>();
  149. Queue<Operand> pending = new Queue<Operand>();
  150. bool Enqueue(Operand operand)
  151. {
  152. if (visited.Add(operand))
  153. {
  154. pending.Enqueue(operand);
  155. return true;
  156. }
  157. return false;
  158. }
  159. Enqueue(dest);
  160. while (pending.TryDequeue(out Operand operand))
  161. {
  162. foreach (INode useNode in operand.UseOps)
  163. {
  164. if (!(useNode is Operation operation))
  165. {
  166. continue;
  167. }
  168. if (operation.Inst == Instruction.Copy)
  169. {
  170. if (operation.Dest.Type == OperandType.LocalVariable)
  171. {
  172. if (Enqueue(operation.Dest))
  173. {
  174. break;
  175. }
  176. }
  177. else
  178. {
  179. return OperandInfo.GetVarType(operation.Dest.Type);
  180. }
  181. }
  182. else
  183. {
  184. for (int index = 0; index < operation.SourcesCount; index++)
  185. {
  186. if (operation.GetSource(index) == operand)
  187. {
  188. return InstructionInfo.GetSrcVarType(operation.Inst, index);
  189. }
  190. }
  191. }
  192. }
  193. }
  194. return VariableType.S32;
  195. }
  196. private static bool AreAllSourceTypesEqual(IAstNode[] sources, VariableType type)
  197. {
  198. foreach (IAstNode node in sources)
  199. {
  200. if (!(node is AstOperand operand))
  201. {
  202. return false;
  203. }
  204. if (operand.VarType != type)
  205. {
  206. return false;
  207. }
  208. }
  209. return true;
  210. }
  211. private static bool IsBranchInst(Instruction inst)
  212. {
  213. switch (inst)
  214. {
  215. case Instruction.Branch:
  216. case Instruction.BranchIfFalse:
  217. case Instruction.BranchIfTrue:
  218. return true;
  219. }
  220. return false;
  221. }
  222. private static bool IsBitwiseInst(Instruction inst)
  223. {
  224. switch (inst)
  225. {
  226. case Instruction.BitwiseAnd:
  227. case Instruction.BitwiseExclusiveOr:
  228. case Instruction.BitwiseNot:
  229. case Instruction.BitwiseOr:
  230. return true;
  231. }
  232. return false;
  233. }
  234. private static Instruction GetLogicalFromBitwiseInst(Instruction inst)
  235. {
  236. switch (inst)
  237. {
  238. case Instruction.BitwiseAnd: return Instruction.LogicalAnd;
  239. case Instruction.BitwiseExclusiveOr: return Instruction.LogicalExclusiveOr;
  240. case Instruction.BitwiseNot: return Instruction.LogicalNot;
  241. case Instruction.BitwiseOr: return Instruction.LogicalOr;
  242. }
  243. throw new ArgumentException($"Unexpected instruction \"{inst}\".");
  244. }
  245. }
  246. }