StructuredProgram.cs 14 KB

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