StructuredProgram.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using Ryujinx.Graphics.Shader.Translation;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Ryujinx.Graphics.Shader.StructuredIr
  6. {
  7. static class StructuredProgram
  8. {
  9. public static StructuredProgramInfo MakeStructuredProgram(Function[] functions, ShaderConfig config)
  10. {
  11. StructuredProgramContext context = new StructuredProgramContext(config);
  12. for (int funcIndex = 0; funcIndex < functions.Length; funcIndex++)
  13. {
  14. Function function = functions[funcIndex];
  15. BasicBlock[] blocks = function.Blocks;
  16. VariableType returnType = function.ReturnsValue ? VariableType.S32 : VariableType.None;
  17. VariableType[] inArguments = new VariableType[function.InArgumentsCount];
  18. VariableType[] outArguments = new VariableType[function.OutArgumentsCount];
  19. for (int i = 0; i < inArguments.Length; i++)
  20. {
  21. inArguments[i] = VariableType.S32;
  22. }
  23. for (int i = 0; i < outArguments.Length; i++)
  24. {
  25. outArguments[i] = VariableType.S32;
  26. }
  27. context.EnterFunction(blocks.Length, function.Name, returnType, inArguments, outArguments);
  28. PhiFunctions.Remove(blocks);
  29. for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
  30. {
  31. BasicBlock block = blocks[blkIndex];
  32. context.EnterBlock(block);
  33. for (LinkedListNode<INode> opNode = block.Operations.First; opNode != null; opNode = opNode.Next)
  34. {
  35. Operation operation = (Operation)opNode.Value;
  36. if (IsBranchInst(operation.Inst))
  37. {
  38. context.LeaveBlock(block, operation);
  39. }
  40. else
  41. {
  42. AddOperation(context, operation);
  43. }
  44. }
  45. }
  46. GotoElimination.Eliminate(context.GetGotos());
  47. AstOptimizer.Optimize(context);
  48. context.LeaveFunction();
  49. }
  50. return context.Info;
  51. }
  52. private static void AddOperation(StructuredProgramContext context, Operation operation)
  53. {
  54. Instruction inst = operation.Inst;
  55. int sourcesCount = operation.SourcesCount;
  56. int outDestsCount = operation.DestsCount != 0 ? operation.DestsCount - 1 : 0;
  57. IAstNode[] sources = new IAstNode[sourcesCount + outDestsCount];
  58. for (int index = 0; index < operation.SourcesCount; index++)
  59. {
  60. sources[index] = context.GetOperandUse(operation.GetSource(index));
  61. }
  62. for (int index = 0; index < outDestsCount; index++)
  63. {
  64. AstOperand oper = context.GetOperandDef(operation.GetDest(1 + index));
  65. oper.VarType = InstructionInfo.GetSrcVarType(inst, sourcesCount + index);
  66. sources[sourcesCount + index] = oper;
  67. }
  68. AstTextureOperation GetAstTextureOperation(TextureOperation texOp)
  69. {
  70. return new AstTextureOperation(
  71. inst,
  72. texOp.Type,
  73. texOp.Format,
  74. texOp.Flags,
  75. texOp.CbufSlot,
  76. texOp.Handle,
  77. texOp.Index,
  78. sources);
  79. }
  80. if (operation.Dest != null)
  81. {
  82. AstOperand dest = context.GetOperandDef(operation.Dest);
  83. // If all the sources are bool, it's better to use short-circuiting
  84. // logical operations, rather than forcing a cast to int and doing
  85. // a bitwise operation with the value, as it is likely to be used as
  86. // a bool in the end.
  87. if (IsBitwiseInst(inst) && AreAllSourceTypesEqual(sources, VariableType.Bool))
  88. {
  89. inst = GetLogicalFromBitwiseInst(inst);
  90. }
  91. bool isCondSel = inst == Instruction.ConditionalSelect;
  92. bool isCopy = inst == Instruction.Copy;
  93. if (isCondSel || isCopy)
  94. {
  95. VariableType type = GetVarTypeFromUses(operation.Dest);
  96. if (isCondSel && type == VariableType.F32)
  97. {
  98. inst |= Instruction.FP32;
  99. }
  100. dest.VarType = type;
  101. }
  102. else
  103. {
  104. dest.VarType = InstructionInfo.GetDestVarType(inst);
  105. }
  106. IAstNode source;
  107. if (operation is TextureOperation texOp)
  108. {
  109. if (texOp.Inst == Instruction.ImageLoad)
  110. {
  111. dest.VarType = texOp.Format.GetComponentType();
  112. }
  113. source = GetAstTextureOperation(texOp);
  114. }
  115. else if (!isCopy)
  116. {
  117. source = new AstOperation(inst, operation.Index, sources, operation.SourcesCount);
  118. }
  119. else
  120. {
  121. source = sources[0];
  122. }
  123. context.AddNode(new AstAssignment(dest, source));
  124. }
  125. else if (operation.Inst == Instruction.Comment)
  126. {
  127. context.AddNode(new AstComment(((CommentNode)operation).Comment));
  128. }
  129. else if (operation is TextureOperation texOp)
  130. {
  131. AstTextureOperation astTexOp = GetAstTextureOperation(texOp);
  132. context.AddNode(astTexOp);
  133. }
  134. else
  135. {
  136. context.AddNode(new AstOperation(inst, operation.Index, sources, operation.SourcesCount));
  137. }
  138. // Those instructions needs to be emulated by using helper functions,
  139. // because they are NVIDIA specific. Those flags helps the backend to
  140. // decide which helper functions are needed on the final generated code.
  141. switch (operation.Inst)
  142. {
  143. case Instruction.AtomicMaxS32 | Instruction.MrShared:
  144. case Instruction.AtomicMinS32 | Instruction.MrShared:
  145. context.Info.HelperFunctionsMask |= HelperFunctionsMask.AtomicMinMaxS32Shared;
  146. break;
  147. case Instruction.AtomicMaxS32 | Instruction.MrStorage:
  148. case Instruction.AtomicMinS32 | Instruction.MrStorage:
  149. context.Info.HelperFunctionsMask |= HelperFunctionsMask.AtomicMinMaxS32Storage;
  150. break;
  151. case Instruction.MultiplyHighS32:
  152. context.Info.HelperFunctionsMask |= HelperFunctionsMask.MultiplyHighS32;
  153. break;
  154. case Instruction.MultiplyHighU32:
  155. context.Info.HelperFunctionsMask |= HelperFunctionsMask.MultiplyHighU32;
  156. break;
  157. case Instruction.Shuffle:
  158. context.Info.HelperFunctionsMask |= HelperFunctionsMask.Shuffle;
  159. break;
  160. case Instruction.ShuffleDown:
  161. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleDown;
  162. break;
  163. case Instruction.ShuffleUp:
  164. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleUp;
  165. break;
  166. case Instruction.ShuffleXor:
  167. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleXor;
  168. break;
  169. case Instruction.SwizzleAdd:
  170. context.Info.HelperFunctionsMask |= HelperFunctionsMask.SwizzleAdd;
  171. break;
  172. }
  173. }
  174. private static VariableType GetVarTypeFromUses(Operand dest)
  175. {
  176. HashSet<Operand> visited = new HashSet<Operand>();
  177. Queue<Operand> pending = new Queue<Operand>();
  178. bool Enqueue(Operand operand)
  179. {
  180. if (visited.Add(operand))
  181. {
  182. pending.Enqueue(operand);
  183. return true;
  184. }
  185. return false;
  186. }
  187. Enqueue(dest);
  188. while (pending.TryDequeue(out Operand operand))
  189. {
  190. foreach (INode useNode in operand.UseOps)
  191. {
  192. if (useNode is not Operation operation)
  193. {
  194. continue;
  195. }
  196. if (operation.Inst == Instruction.Copy)
  197. {
  198. if (operation.Dest.Type == OperandType.LocalVariable)
  199. {
  200. if (Enqueue(operation.Dest))
  201. {
  202. break;
  203. }
  204. }
  205. else
  206. {
  207. return OperandInfo.GetVarType(operation.Dest.Type);
  208. }
  209. }
  210. else
  211. {
  212. for (int index = 0; index < operation.SourcesCount; index++)
  213. {
  214. if (operation.GetSource(index) == operand)
  215. {
  216. return InstructionInfo.GetSrcVarType(operation.Inst, index);
  217. }
  218. }
  219. }
  220. }
  221. }
  222. return VariableType.S32;
  223. }
  224. private static bool AreAllSourceTypesEqual(IAstNode[] sources, VariableType type)
  225. {
  226. foreach (IAstNode node in sources)
  227. {
  228. if (node is not AstOperand operand)
  229. {
  230. return false;
  231. }
  232. if (operand.VarType != type)
  233. {
  234. return false;
  235. }
  236. }
  237. return true;
  238. }
  239. private static bool IsBranchInst(Instruction inst)
  240. {
  241. return inst switch
  242. {
  243. Instruction.Branch or
  244. Instruction.BranchIfFalse or
  245. Instruction.BranchIfTrue => true,
  246. _ => false
  247. };
  248. }
  249. private static bool IsBitwiseInst(Instruction inst)
  250. {
  251. return inst switch
  252. {
  253. Instruction.BitwiseAnd or
  254. Instruction.BitwiseExclusiveOr or
  255. Instruction.BitwiseNot or
  256. Instruction.BitwiseOr => true,
  257. _ => false
  258. };
  259. }
  260. private static Instruction GetLogicalFromBitwiseInst(Instruction inst)
  261. {
  262. return inst switch
  263. {
  264. Instruction.BitwiseAnd => Instruction.LogicalAnd,
  265. Instruction.BitwiseExclusiveOr => Instruction.LogicalExclusiveOr,
  266. Instruction.BitwiseNot => Instruction.LogicalNot,
  267. Instruction.BitwiseOr => Instruction.LogicalOr,
  268. _ => throw new ArgumentException($"Unexpected instruction \"{inst}\".")
  269. };
  270. }
  271. }
  272. }