StructuredProgram.cs 10 KB

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