StructuredProgram.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. 4, // TODO: Non-hardcoded array size.
  50. componentMask,
  51. sources);
  52. }
  53. if (operation.Dest != null)
  54. {
  55. AstOperand dest = context.GetOperandDef(operation.Dest);
  56. if (inst == Instruction.LoadConstant)
  57. {
  58. Operand slot = operation.GetSource(0);
  59. if (slot.Type != OperandType.Constant)
  60. {
  61. throw new InvalidOperationException("Found load with non-constant constant buffer slot.");
  62. }
  63. context.Info.CBuffers.Add(slot.Value);
  64. }
  65. else if (inst == Instruction.LoadStorage)
  66. {
  67. Operand slot = operation.GetSource(0);
  68. if (slot.Type != OperandType.Constant)
  69. {
  70. throw new InvalidOperationException("Found load or store with non-constant storage buffer slot.");
  71. }
  72. context.Info.SBuffers.Add(slot.Value);
  73. }
  74. AstAssignment assignment;
  75. // If all the sources are bool, it's better to use short-circuiting
  76. // logical operations, rather than forcing a cast to int and doing
  77. // a bitwise operation with the value, as it is likely to be used as
  78. // a bool in the end.
  79. if (IsBitwiseInst(inst) && AreAllSourceTypesEqual(sources, VariableType.Bool))
  80. {
  81. inst = GetLogicalFromBitwiseInst(inst);
  82. }
  83. bool isCondSel = inst == Instruction.ConditionalSelect;
  84. bool isCopy = inst == Instruction.Copy;
  85. if (isCondSel || isCopy)
  86. {
  87. VariableType type = GetVarTypeFromUses(operation.Dest);
  88. if (isCondSel && type == VariableType.F32)
  89. {
  90. inst |= Instruction.FP;
  91. }
  92. dest.VarType = type;
  93. }
  94. else
  95. {
  96. dest.VarType = InstructionInfo.GetDestVarType(inst);
  97. }
  98. IAstNode source;
  99. if (operation is TextureOperation texOp)
  100. {
  101. AstTextureOperation astTexOp = GetAstTextureOperation(texOp);
  102. if (texOp.Inst == Instruction.ImageLoad)
  103. {
  104. context.Info.Images.Add(astTexOp);
  105. }
  106. else
  107. {
  108. context.Info.Samplers.Add(astTexOp);
  109. }
  110. source = astTexOp;
  111. }
  112. else if (!isCopy)
  113. {
  114. source = new AstOperation(inst, componentMask, sources);
  115. }
  116. else
  117. {
  118. source = sources[0];
  119. }
  120. assignment = new AstAssignment(dest, source);
  121. context.AddNode(assignment);
  122. }
  123. else if (operation.Inst == Instruction.Comment)
  124. {
  125. context.AddNode(new AstComment(((CommentNode)operation).Comment));
  126. }
  127. else if (operation is TextureOperation texOp)
  128. {
  129. AstTextureOperation astTexOp = GetAstTextureOperation(texOp);
  130. context.Info.Images.Add(astTexOp);
  131. context.AddNode(astTexOp);
  132. }
  133. else
  134. {
  135. if (inst == Instruction.StoreStorage)
  136. {
  137. Operand slot = operation.GetSource(0);
  138. if (slot.Type != OperandType.Constant)
  139. {
  140. throw new InvalidOperationException("Found load or store with non-constant storage buffer slot.");
  141. }
  142. context.Info.SBuffers.Add(slot.Value);
  143. }
  144. context.AddNode(new AstOperation(inst, sources));
  145. }
  146. // Those instructions needs to be emulated by using helper functions,
  147. // because they are NVIDIA specific. Those flags helps the backend to
  148. // decide which helper functions are needed on the final generated code.
  149. switch (operation.Inst)
  150. {
  151. case Instruction.Shuffle:
  152. context.Info.HelperFunctionsMask |= HelperFunctionsMask.Shuffle;
  153. break;
  154. case Instruction.ShuffleDown:
  155. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleDown;
  156. break;
  157. case Instruction.ShuffleUp:
  158. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleUp;
  159. break;
  160. case Instruction.ShuffleXor:
  161. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleXor;
  162. break;
  163. case Instruction.SwizzleAdd:
  164. context.Info.HelperFunctionsMask |= HelperFunctionsMask.SwizzleAdd;
  165. break;
  166. }
  167. }
  168. private static VariableType GetVarTypeFromUses(Operand dest)
  169. {
  170. HashSet<Operand> visited = new HashSet<Operand>();
  171. Queue<Operand> pending = new Queue<Operand>();
  172. bool Enqueue(Operand operand)
  173. {
  174. if (visited.Add(operand))
  175. {
  176. pending.Enqueue(operand);
  177. return true;
  178. }
  179. return false;
  180. }
  181. Enqueue(dest);
  182. while (pending.TryDequeue(out Operand operand))
  183. {
  184. foreach (INode useNode in operand.UseOps)
  185. {
  186. if (!(useNode is Operation operation))
  187. {
  188. continue;
  189. }
  190. if (operation.Inst == Instruction.Copy)
  191. {
  192. if (operation.Dest.Type == OperandType.LocalVariable)
  193. {
  194. if (Enqueue(operation.Dest))
  195. {
  196. break;
  197. }
  198. }
  199. else
  200. {
  201. return OperandInfo.GetVarType(operation.Dest.Type);
  202. }
  203. }
  204. else
  205. {
  206. for (int index = 0; index < operation.SourcesCount; index++)
  207. {
  208. if (operation.GetSource(index) == operand)
  209. {
  210. return InstructionInfo.GetSrcVarType(operation.Inst, index);
  211. }
  212. }
  213. }
  214. }
  215. }
  216. return VariableType.S32;
  217. }
  218. private static bool AreAllSourceTypesEqual(IAstNode[] sources, VariableType type)
  219. {
  220. foreach (IAstNode node in sources)
  221. {
  222. if (!(node is AstOperand operand))
  223. {
  224. return false;
  225. }
  226. if (operand.VarType != type)
  227. {
  228. return false;
  229. }
  230. }
  231. return true;
  232. }
  233. private static bool IsBranchInst(Instruction inst)
  234. {
  235. switch (inst)
  236. {
  237. case Instruction.Branch:
  238. case Instruction.BranchIfFalse:
  239. case Instruction.BranchIfTrue:
  240. return true;
  241. }
  242. return false;
  243. }
  244. private static bool IsBitwiseInst(Instruction inst)
  245. {
  246. switch (inst)
  247. {
  248. case Instruction.BitwiseAnd:
  249. case Instruction.BitwiseExclusiveOr:
  250. case Instruction.BitwiseNot:
  251. case Instruction.BitwiseOr:
  252. return true;
  253. }
  254. return false;
  255. }
  256. private static Instruction GetLogicalFromBitwiseInst(Instruction inst)
  257. {
  258. switch (inst)
  259. {
  260. case Instruction.BitwiseAnd: return Instruction.LogicalAnd;
  261. case Instruction.BitwiseExclusiveOr: return Instruction.LogicalExclusiveOr;
  262. case Instruction.BitwiseNot: return Instruction.LogicalNot;
  263. case Instruction.BitwiseOr: return Instruction.LogicalOr;
  264. }
  265. throw new ArgumentException($"Unexpected instruction \"{inst}\".");
  266. }
  267. }
  268. }