StructuredProgram.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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)
  9. {
  10. PhiFunctions.Remove(blocks);
  11. StructuredProgramContext context = new StructuredProgramContext(blocks.Length);
  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.Info);
  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. if (operation.Dest != null)
  42. {
  43. AstOperand dest = context.GetOperandDef(operation.Dest);
  44. if (inst == Instruction.LoadConstant)
  45. {
  46. Operand ldcSource = operation.GetSource(0);
  47. if (ldcSource.Type != OperandType.Constant)
  48. {
  49. throw new InvalidOperationException("Found LDC with non-constant constant buffer slot.");
  50. }
  51. context.Info.CBuffers.Add(ldcSource.Value);
  52. }
  53. AstAssignment assignment;
  54. // If all the sources are bool, it's better to use short-circuiting
  55. // logical operations, rather than forcing a cast to int and doing
  56. // a bitwise operation with the value, as it is likely to be used as
  57. // a bool in the end.
  58. if (IsBitwiseInst(inst) && AreAllSourceTypesEqual(sources, VariableType.Bool))
  59. {
  60. inst = GetLogicalFromBitwiseInst(inst);
  61. }
  62. bool isCondSel = inst == Instruction.ConditionalSelect;
  63. bool isCopy = inst == Instruction.Copy;
  64. if (isCondSel || isCopy)
  65. {
  66. VariableType type = GetVarTypeFromUses(operation.Dest);
  67. if (isCondSel && type == VariableType.F32)
  68. {
  69. inst |= Instruction.FP;
  70. }
  71. dest.VarType = type;
  72. }
  73. else
  74. {
  75. dest.VarType = InstructionInfo.GetDestVarType(inst);
  76. }
  77. int componentMask = 1 << operation.ComponentIndex;
  78. IAstNode source;
  79. if (operation is TextureOperation texOp)
  80. {
  81. AstTextureOperation astTexOp = new AstTextureOperation(
  82. inst,
  83. texOp.Type,
  84. texOp.Flags,
  85. texOp.Handle,
  86. componentMask,
  87. sources);
  88. context.Info.Samplers.Add(astTexOp);
  89. source = astTexOp;
  90. }
  91. else if (!isCopy)
  92. {
  93. source = new AstOperation(inst, componentMask, sources);
  94. }
  95. else
  96. {
  97. source = sources[0];
  98. }
  99. assignment = new AstAssignment(dest, source);
  100. context.AddNode(assignment);
  101. }
  102. else
  103. {
  104. context.AddNode(new AstOperation(inst, sources));
  105. }
  106. }
  107. private static VariableType GetVarTypeFromUses(Operand dest)
  108. {
  109. HashSet<Operand> visited = new HashSet<Operand>();
  110. Queue<Operand> pending = new Queue<Operand>();
  111. bool Enqueue(Operand operand)
  112. {
  113. if (visited.Add(operand))
  114. {
  115. pending.Enqueue(operand);
  116. return true;
  117. }
  118. return false;
  119. }
  120. Enqueue(dest);
  121. while (pending.TryDequeue(out Operand operand))
  122. {
  123. foreach (INode useNode in operand.UseOps)
  124. {
  125. if (!(useNode is Operation operation))
  126. {
  127. continue;
  128. }
  129. if (operation.Inst == Instruction.Copy)
  130. {
  131. if (operation.Dest.Type == OperandType.LocalVariable)
  132. {
  133. if (Enqueue(operation.Dest))
  134. {
  135. break;
  136. }
  137. }
  138. else
  139. {
  140. return OperandInfo.GetVarType(operation.Dest.Type);
  141. }
  142. }
  143. else
  144. {
  145. for (int index = 0; index < operation.SourcesCount; index++)
  146. {
  147. if (operation.GetSource(index) == operand)
  148. {
  149. return InstructionInfo.GetSrcVarType(operation.Inst, index);
  150. }
  151. }
  152. }
  153. }
  154. }
  155. return VariableType.S32;
  156. }
  157. private static bool AreAllSourceTypesEqual(IAstNode[] sources, VariableType type)
  158. {
  159. foreach (IAstNode node in sources)
  160. {
  161. if (!(node is AstOperand operand))
  162. {
  163. return false;
  164. }
  165. if (operand.VarType != type)
  166. {
  167. return false;
  168. }
  169. }
  170. return true;
  171. }
  172. private static bool IsBranchInst(Instruction inst)
  173. {
  174. switch (inst)
  175. {
  176. case Instruction.Branch:
  177. case Instruction.BranchIfFalse:
  178. case Instruction.BranchIfTrue:
  179. return true;
  180. }
  181. return false;
  182. }
  183. private static bool IsBitwiseInst(Instruction inst)
  184. {
  185. switch (inst)
  186. {
  187. case Instruction.BitwiseAnd:
  188. case Instruction.BitwiseExclusiveOr:
  189. case Instruction.BitwiseNot:
  190. case Instruction.BitwiseOr:
  191. return true;
  192. }
  193. return false;
  194. }
  195. private static Instruction GetLogicalFromBitwiseInst(Instruction inst)
  196. {
  197. switch (inst)
  198. {
  199. case Instruction.BitwiseAnd: return Instruction.LogicalAnd;
  200. case Instruction.BitwiseExclusiveOr: return Instruction.LogicalExclusiveOr;
  201. case Instruction.BitwiseNot: return Instruction.LogicalNot;
  202. case Instruction.BitwiseOr: return Instruction.LogicalOr;
  203. }
  204. throw new ArgumentException($"Unexpected instruction \"{inst}\".");
  205. }
  206. }
  207. }