StructuredProgram.cs 14 KB

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