StructuredProgram.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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(Function[] functions, ShaderConfig config)
  11. {
  12. StructuredProgramContext context = new StructuredProgramContext(config);
  13. for (int funcIndex = 0; funcIndex < functions.Length; funcIndex++)
  14. {
  15. Function function = functions[funcIndex];
  16. BasicBlock[] blocks = function.Blocks;
  17. VariableType returnType = function.ReturnsValue ? VariableType.S32 : VariableType.None;
  18. VariableType[] inArguments = new VariableType[function.InArgumentsCount];
  19. VariableType[] outArguments = new VariableType[function.OutArgumentsCount];
  20. for (int i = 0; i < inArguments.Length; i++)
  21. {
  22. inArguments[i] = VariableType.S32;
  23. }
  24. for (int i = 0; i < outArguments.Length; i++)
  25. {
  26. outArguments[i] = VariableType.S32;
  27. }
  28. context.EnterFunction(blocks.Length, function.Name, returnType, inArguments, outArguments);
  29. PhiFunctions.Remove(blocks);
  30. for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
  31. {
  32. BasicBlock block = blocks[blkIndex];
  33. context.EnterBlock(block);
  34. for (LinkedListNode<INode> opNode = block.Operations.First; opNode != null; opNode = opNode.Next)
  35. {
  36. Operation operation = (Operation)opNode.Value;
  37. if (IsBranchInst(operation.Inst))
  38. {
  39. context.LeaveBlock(block, operation);
  40. }
  41. else
  42. {
  43. AddOperation(context, operation);
  44. }
  45. }
  46. }
  47. GotoElimination.Eliminate(context.GetGotos());
  48. AstOptimizer.Optimize(context);
  49. context.LeaveFunction();
  50. }
  51. return context.Info;
  52. }
  53. private static void AddOperation(StructuredProgramContext context, Operation operation)
  54. {
  55. Instruction inst = operation.Inst;
  56. int sourcesCount = operation.SourcesCount;
  57. int outDestsCount = operation.DestsCount != 0 ? operation.DestsCount - 1 : 0;
  58. IAstNode[] sources = new IAstNode[sourcesCount + outDestsCount];
  59. for (int index = 0; index < operation.SourcesCount; index++)
  60. {
  61. sources[index] = context.GetOperandUse(operation.GetSource(index));
  62. }
  63. for (int index = 0; index < outDestsCount; index++)
  64. {
  65. AstOperand oper = context.GetOperandDef(operation.GetDest(1 + index));
  66. oper.VarType = InstructionInfo.GetSrcVarType(inst, sourcesCount + index);
  67. sources[sourcesCount + index] = oper;
  68. }
  69. AstTextureOperation GetAstTextureOperation(TextureOperation texOp)
  70. {
  71. return new AstTextureOperation(
  72. inst,
  73. texOp.Type,
  74. texOp.Format,
  75. texOp.Flags,
  76. texOp.CbufSlot,
  77. texOp.Handle,
  78. 4, // TODO: Non-hardcoded array size.
  79. texOp.Index,
  80. sources);
  81. }
  82. if (operation.Dest != null)
  83. {
  84. AstOperand dest = context.GetOperandDef(operation.Dest);
  85. if (inst == Instruction.LoadConstant)
  86. {
  87. Operand slot = operation.GetSource(0);
  88. if (slot.Type == OperandType.Constant)
  89. {
  90. context.Info.CBuffers.Add(slot.Value);
  91. }
  92. else
  93. {
  94. // If the value is not constant, then we don't know
  95. // how many constant buffers are used, so we assume
  96. // all of them are used.
  97. int cbCount = 32 - BitOperations.LeadingZeroCount(context.Config.GpuAccessor.QueryConstantBufferUse());
  98. for (int index = 0; index < cbCount; index++)
  99. {
  100. context.Info.CBuffers.Add(index);
  101. }
  102. context.Info.UsesCbIndexing = true;
  103. }
  104. }
  105. else if (UsesStorage(inst))
  106. {
  107. AddSBufferUse(context.Info.SBuffers, operation);
  108. }
  109. // If all the sources are bool, it's better to use short-circuiting
  110. // logical operations, rather than forcing a cast to int and doing
  111. // a bitwise operation with the value, as it is likely to be used as
  112. // a bool in the end.
  113. if (IsBitwiseInst(inst) && AreAllSourceTypesEqual(sources, VariableType.Bool))
  114. {
  115. inst = GetLogicalFromBitwiseInst(inst);
  116. }
  117. bool isCondSel = inst == Instruction.ConditionalSelect;
  118. bool isCopy = inst == Instruction.Copy;
  119. if (isCondSel || isCopy)
  120. {
  121. VariableType type = GetVarTypeFromUses(operation.Dest);
  122. if (isCondSel && type == VariableType.F32)
  123. {
  124. inst |= Instruction.FP32;
  125. }
  126. dest.VarType = type;
  127. }
  128. else
  129. {
  130. dest.VarType = InstructionInfo.GetDestVarType(inst);
  131. }
  132. IAstNode source;
  133. if (operation is TextureOperation texOp)
  134. {
  135. if (texOp.Inst == Instruction.ImageLoad || texOp.Inst == Instruction.ImageStore)
  136. {
  137. dest.VarType = texOp.Format.GetComponentType();
  138. }
  139. AstTextureOperation astTexOp = GetAstTextureOperation(texOp);
  140. if (texOp.Inst == Instruction.ImageLoad)
  141. {
  142. context.Info.Images.Add(astTexOp);
  143. }
  144. else
  145. {
  146. context.Info.Samplers.Add(astTexOp);
  147. }
  148. source = astTexOp;
  149. }
  150. else if (!isCopy)
  151. {
  152. source = new AstOperation(inst, operation.Index, sources, operation.SourcesCount);
  153. }
  154. else
  155. {
  156. source = sources[0];
  157. }
  158. context.AddNode(new AstAssignment(dest, source));
  159. }
  160. else if (operation.Inst == Instruction.Comment)
  161. {
  162. context.AddNode(new AstComment(((CommentNode)operation).Comment));
  163. }
  164. else if (operation is TextureOperation texOp)
  165. {
  166. AstTextureOperation astTexOp = GetAstTextureOperation(texOp);
  167. context.Info.Images.Add(astTexOp);
  168. context.AddNode(astTexOp);
  169. }
  170. else
  171. {
  172. if (UsesStorage(inst))
  173. {
  174. AddSBufferUse(context.Info.SBuffers, operation);
  175. }
  176. context.AddNode(new AstOperation(inst, operation.Index, sources, operation.SourcesCount));
  177. }
  178. // Those instructions needs to be emulated by using helper functions,
  179. // because they are NVIDIA specific. Those flags helps the backend to
  180. // decide which helper functions are needed on the final generated code.
  181. switch (operation.Inst)
  182. {
  183. case Instruction.AtomicMaxS32 | Instruction.MrShared:
  184. case Instruction.AtomicMinS32 | Instruction.MrShared:
  185. context.Info.HelperFunctionsMask |= HelperFunctionsMask.AtomicMinMaxS32Shared;
  186. break;
  187. case Instruction.AtomicMaxS32 | Instruction.MrStorage:
  188. case Instruction.AtomicMinS32 | Instruction.MrStorage:
  189. context.Info.HelperFunctionsMask |= HelperFunctionsMask.AtomicMinMaxS32Storage;
  190. break;
  191. case Instruction.MultiplyHighS32:
  192. context.Info.HelperFunctionsMask |= HelperFunctionsMask.MultiplyHighS32;
  193. break;
  194. case Instruction.MultiplyHighU32:
  195. context.Info.HelperFunctionsMask |= HelperFunctionsMask.MultiplyHighU32;
  196. break;
  197. case Instruction.Shuffle:
  198. context.Info.HelperFunctionsMask |= HelperFunctionsMask.Shuffle;
  199. break;
  200. case Instruction.ShuffleDown:
  201. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleDown;
  202. break;
  203. case Instruction.ShuffleUp:
  204. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleUp;
  205. break;
  206. case Instruction.ShuffleXor:
  207. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleXor;
  208. break;
  209. case Instruction.SwizzleAdd:
  210. context.Info.HelperFunctionsMask |= HelperFunctionsMask.SwizzleAdd;
  211. break;
  212. }
  213. }
  214. private static void AddSBufferUse(HashSet<int> sBuffers, Operation operation)
  215. {
  216. Operand slot = operation.GetSource(0);
  217. if (slot.Type == OperandType.Constant)
  218. {
  219. sBuffers.Add(slot.Value);
  220. }
  221. else
  222. {
  223. // If the value is not constant, then we don't know
  224. // how many storage buffers are used, so we assume
  225. // all of them are used.
  226. for (int index = 0; index < GlobalMemory.StorageMaxCount; index++)
  227. {
  228. sBuffers.Add(index);
  229. }
  230. }
  231. }
  232. private static VariableType GetVarTypeFromUses(Operand dest)
  233. {
  234. HashSet<Operand> visited = new HashSet<Operand>();
  235. Queue<Operand> pending = new Queue<Operand>();
  236. bool Enqueue(Operand operand)
  237. {
  238. if (visited.Add(operand))
  239. {
  240. pending.Enqueue(operand);
  241. return true;
  242. }
  243. return false;
  244. }
  245. Enqueue(dest);
  246. while (pending.TryDequeue(out Operand operand))
  247. {
  248. foreach (INode useNode in operand.UseOps)
  249. {
  250. if (!(useNode is Operation operation))
  251. {
  252. continue;
  253. }
  254. if (operation.Inst == Instruction.Copy)
  255. {
  256. if (operation.Dest.Type == OperandType.LocalVariable)
  257. {
  258. if (Enqueue(operation.Dest))
  259. {
  260. break;
  261. }
  262. }
  263. else
  264. {
  265. return OperandInfo.GetVarType(operation.Dest.Type);
  266. }
  267. }
  268. else
  269. {
  270. for (int index = 0; index < operation.SourcesCount; index++)
  271. {
  272. if (operation.GetSource(index) == operand)
  273. {
  274. return InstructionInfo.GetSrcVarType(operation.Inst, index);
  275. }
  276. }
  277. }
  278. }
  279. }
  280. return VariableType.S32;
  281. }
  282. private static bool AreAllSourceTypesEqual(IAstNode[] sources, VariableType type)
  283. {
  284. foreach (IAstNode node in sources)
  285. {
  286. if (!(node is AstOperand operand))
  287. {
  288. return false;
  289. }
  290. if (operand.VarType != type)
  291. {
  292. return false;
  293. }
  294. }
  295. return true;
  296. }
  297. private static bool IsBranchInst(Instruction inst)
  298. {
  299. switch (inst)
  300. {
  301. case Instruction.Branch:
  302. case Instruction.BranchIfFalse:
  303. case Instruction.BranchIfTrue:
  304. return true;
  305. }
  306. return false;
  307. }
  308. private static bool IsBitwiseInst(Instruction inst)
  309. {
  310. switch (inst)
  311. {
  312. case Instruction.BitwiseAnd:
  313. case Instruction.BitwiseExclusiveOr:
  314. case Instruction.BitwiseNot:
  315. case Instruction.BitwiseOr:
  316. return true;
  317. }
  318. return false;
  319. }
  320. private static Instruction GetLogicalFromBitwiseInst(Instruction inst)
  321. {
  322. switch (inst)
  323. {
  324. case Instruction.BitwiseAnd: return Instruction.LogicalAnd;
  325. case Instruction.BitwiseExclusiveOr: return Instruction.LogicalExclusiveOr;
  326. case Instruction.BitwiseNot: return Instruction.LogicalNot;
  327. case Instruction.BitwiseOr: return Instruction.LogicalOr;
  328. }
  329. throw new ArgumentException($"Unexpected instruction \"{inst}\".");
  330. }
  331. private static bool UsesStorage(Instruction inst)
  332. {
  333. if (inst == Instruction.LoadStorage || inst == Instruction.StoreStorage)
  334. {
  335. return true;
  336. }
  337. return inst.IsAtomic() && (inst & Instruction.MrMask) == Instruction.MrStorage;
  338. }
  339. }
  340. }