StructuredProgram.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using Ryujinx.Graphics.Shader.Translation;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Numerics;
  6. namespace Ryujinx.Graphics.Shader.StructuredIr
  7. {
  8. static class StructuredProgram
  9. {
  10. public static StructuredProgramInfo MakeStructuredProgram(BasicBlock[] blocks, ShaderConfig config)
  11. {
  12. PhiFunctions.Remove(blocks);
  13. StructuredProgramContext context = new StructuredProgramContext(blocks.Length, config);
  14. for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
  15. {
  16. BasicBlock block = blocks[blkIndex];
  17. context.EnterBlock(block);
  18. foreach (INode node in block.Operations)
  19. {
  20. Operation operation = (Operation)node;
  21. if (IsBranchInst(operation.Inst))
  22. {
  23. context.LeaveBlock(block, operation);
  24. }
  25. else
  26. {
  27. AddOperation(context, operation);
  28. }
  29. }
  30. }
  31. GotoElimination.Eliminate(context.GetGotos());
  32. AstOptimizer.Optimize(context);
  33. return context.Info;
  34. }
  35. private static void AddOperation(StructuredProgramContext context, Operation operation)
  36. {
  37. Instruction inst = operation.Inst;
  38. IAstNode[] sources = new IAstNode[operation.SourcesCount];
  39. for (int index = 0; index < sources.Length; index++)
  40. {
  41. sources[index] = context.GetOperandUse(operation.GetSource(index));
  42. }
  43. AstTextureOperation GetAstTextureOperation(TextureOperation texOp)
  44. {
  45. return new AstTextureOperation(
  46. inst,
  47. texOp.Type,
  48. texOp.Format,
  49. texOp.Flags,
  50. texOp.Handle,
  51. 4, // TODO: Non-hardcoded array size.
  52. texOp.Index,
  53. sources);
  54. }
  55. if (operation.Dest != null)
  56. {
  57. AstOperand dest = context.GetOperandDef(operation.Dest);
  58. if (inst == Instruction.LoadConstant)
  59. {
  60. Operand slot = operation.GetSource(0);
  61. if (slot.Type == OperandType.Constant)
  62. {
  63. context.Info.CBuffers.Add(slot.Value);
  64. }
  65. else
  66. {
  67. // If the value is not constant, then we don't know
  68. // how many constant buffers are used, so we assume
  69. // all of them are used.
  70. int cbCount = 32 - BitOperations.LeadingZeroCount(context.Config.GpuAccessor.QueryConstantBufferUse());
  71. for (int index = 0; index < cbCount; index++)
  72. {
  73. context.Info.CBuffers.Add(index);
  74. }
  75. context.Info.UsesCbIndexing = true;
  76. }
  77. }
  78. else if (UsesStorage(inst))
  79. {
  80. AddSBufferUse(context.Info.SBuffers, operation);
  81. }
  82. AstAssignment assignment;
  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 || texOp.Inst == Instruction.ImageStore)
  110. {
  111. dest.VarType = texOp.Format.GetComponentType();
  112. }
  113. AstTextureOperation astTexOp = GetAstTextureOperation(texOp);
  114. if (texOp.Inst == Instruction.ImageLoad)
  115. {
  116. context.Info.Images.Add(astTexOp);
  117. }
  118. else
  119. {
  120. context.Info.Samplers.Add(astTexOp);
  121. }
  122. source = astTexOp;
  123. }
  124. else if (!isCopy)
  125. {
  126. source = new AstOperation(inst, operation.Index, sources);
  127. }
  128. else
  129. {
  130. source = sources[0];
  131. }
  132. assignment = new AstAssignment(dest, source);
  133. context.AddNode(assignment);
  134. }
  135. else if (operation.Inst == Instruction.Comment)
  136. {
  137. context.AddNode(new AstComment(((CommentNode)operation).Comment));
  138. }
  139. else if (operation is TextureOperation texOp)
  140. {
  141. AstTextureOperation astTexOp = GetAstTextureOperation(texOp);
  142. context.Info.Images.Add(astTexOp);
  143. context.AddNode(astTexOp);
  144. }
  145. else
  146. {
  147. if (UsesStorage(inst))
  148. {
  149. AddSBufferUse(context.Info.SBuffers, operation);
  150. }
  151. context.AddNode(new AstOperation(inst, operation.Index, sources));
  152. }
  153. // Those instructions needs to be emulated by using helper functions,
  154. // because they are NVIDIA specific. Those flags helps the backend to
  155. // decide which helper functions are needed on the final generated code.
  156. switch (operation.Inst)
  157. {
  158. case Instruction.MultiplyHighS32:
  159. context.Info.HelperFunctionsMask |= HelperFunctionsMask.MultiplyHighS32;
  160. break;
  161. case Instruction.MultiplyHighU32:
  162. context.Info.HelperFunctionsMask |= HelperFunctionsMask.MultiplyHighU32;
  163. break;
  164. case Instruction.Shuffle:
  165. context.Info.HelperFunctionsMask |= HelperFunctionsMask.Shuffle;
  166. break;
  167. case Instruction.ShuffleDown:
  168. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleDown;
  169. break;
  170. case Instruction.ShuffleUp:
  171. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleUp;
  172. break;
  173. case Instruction.ShuffleXor:
  174. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleXor;
  175. break;
  176. case Instruction.SwizzleAdd:
  177. context.Info.HelperFunctionsMask |= HelperFunctionsMask.SwizzleAdd;
  178. break;
  179. }
  180. }
  181. private static void AddSBufferUse(HashSet<int> sBuffers, Operation operation)
  182. {
  183. Operand slot = operation.GetSource(0);
  184. if (slot.Type == OperandType.Constant)
  185. {
  186. sBuffers.Add(slot.Value);
  187. }
  188. else
  189. {
  190. // If the value is not constant, then we don't know
  191. // how many storage buffers are used, so we assume
  192. // all of them are used.
  193. for (int index = 0; index < GlobalMemory.StorageMaxCount; index++)
  194. {
  195. sBuffers.Add(index);
  196. }
  197. }
  198. }
  199. private static VariableType GetVarTypeFromUses(Operand dest)
  200. {
  201. HashSet<Operand> visited = new HashSet<Operand>();
  202. Queue<Operand> pending = new Queue<Operand>();
  203. bool Enqueue(Operand operand)
  204. {
  205. if (visited.Add(operand))
  206. {
  207. pending.Enqueue(operand);
  208. return true;
  209. }
  210. return false;
  211. }
  212. Enqueue(dest);
  213. while (pending.TryDequeue(out Operand operand))
  214. {
  215. foreach (INode useNode in operand.UseOps)
  216. {
  217. if (!(useNode is Operation operation))
  218. {
  219. continue;
  220. }
  221. if (operation.Inst == Instruction.Copy)
  222. {
  223. if (operation.Dest.Type == OperandType.LocalVariable)
  224. {
  225. if (Enqueue(operation.Dest))
  226. {
  227. break;
  228. }
  229. }
  230. else
  231. {
  232. return OperandInfo.GetVarType(operation.Dest.Type);
  233. }
  234. }
  235. else
  236. {
  237. for (int index = 0; index < operation.SourcesCount; index++)
  238. {
  239. if (operation.GetSource(index) == operand)
  240. {
  241. return InstructionInfo.GetSrcVarType(operation.Inst, index);
  242. }
  243. }
  244. }
  245. }
  246. }
  247. return VariableType.S32;
  248. }
  249. private static bool AreAllSourceTypesEqual(IAstNode[] sources, VariableType type)
  250. {
  251. foreach (IAstNode node in sources)
  252. {
  253. if (!(node is AstOperand operand))
  254. {
  255. return false;
  256. }
  257. if (operand.VarType != type)
  258. {
  259. return false;
  260. }
  261. }
  262. return true;
  263. }
  264. private static bool IsBranchInst(Instruction inst)
  265. {
  266. switch (inst)
  267. {
  268. case Instruction.Branch:
  269. case Instruction.BranchIfFalse:
  270. case Instruction.BranchIfTrue:
  271. return true;
  272. }
  273. return false;
  274. }
  275. private static bool IsBitwiseInst(Instruction inst)
  276. {
  277. switch (inst)
  278. {
  279. case Instruction.BitwiseAnd:
  280. case Instruction.BitwiseExclusiveOr:
  281. case Instruction.BitwiseNot:
  282. case Instruction.BitwiseOr:
  283. return true;
  284. }
  285. return false;
  286. }
  287. private static Instruction GetLogicalFromBitwiseInst(Instruction inst)
  288. {
  289. switch (inst)
  290. {
  291. case Instruction.BitwiseAnd: return Instruction.LogicalAnd;
  292. case Instruction.BitwiseExclusiveOr: return Instruction.LogicalExclusiveOr;
  293. case Instruction.BitwiseNot: return Instruction.LogicalNot;
  294. case Instruction.BitwiseOr: return Instruction.LogicalOr;
  295. }
  296. throw new ArgumentException($"Unexpected instruction \"{inst}\".");
  297. }
  298. private static bool UsesStorage(Instruction inst)
  299. {
  300. if (inst == Instruction.LoadStorage || inst == Instruction.StoreStorage)
  301. {
  302. return true;
  303. }
  304. return inst.IsAtomic() && (inst & Instruction.MrMask) == Instruction.MrStorage;
  305. }
  306. }
  307. }