StructuredProgram.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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.AtomicMaxS32 | Instruction.MrShared:
  198. case Instruction.AtomicMinS32 | Instruction.MrShared:
  199. context.Info.HelperFunctionsMask |= HelperFunctionsMask.AtomicMinMaxS32Shared;
  200. break;
  201. case Instruction.AtomicMaxS32 | Instruction.MrStorage:
  202. case Instruction.AtomicMinS32 | Instruction.MrStorage:
  203. context.Info.HelperFunctionsMask |= HelperFunctionsMask.AtomicMinMaxS32Storage;
  204. break;
  205. case Instruction.MultiplyHighS32:
  206. context.Info.HelperFunctionsMask |= HelperFunctionsMask.MultiplyHighS32;
  207. break;
  208. case Instruction.MultiplyHighU32:
  209. context.Info.HelperFunctionsMask |= HelperFunctionsMask.MultiplyHighU32;
  210. break;
  211. case Instruction.Shuffle:
  212. context.Info.HelperFunctionsMask |= HelperFunctionsMask.Shuffle;
  213. break;
  214. case Instruction.ShuffleDown:
  215. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleDown;
  216. break;
  217. case Instruction.ShuffleUp:
  218. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleUp;
  219. break;
  220. case Instruction.ShuffleXor:
  221. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleXor;
  222. break;
  223. case Instruction.SwizzleAdd:
  224. context.Info.HelperFunctionsMask |= HelperFunctionsMask.SwizzleAdd;
  225. break;
  226. }
  227. }
  228. private static void AddSBufferUse(HashSet<int> sBuffers, Operation operation)
  229. {
  230. Operand slot = operation.GetSource(0);
  231. if (slot.Type == OperandType.Constant)
  232. {
  233. sBuffers.Add(slot.Value);
  234. }
  235. else
  236. {
  237. // If the value is not constant, then we don't know
  238. // how many storage buffers are used, so we assume
  239. // all of them are used.
  240. for (int index = 0; index < GlobalMemory.StorageMaxCount; index++)
  241. {
  242. sBuffers.Add(index);
  243. }
  244. }
  245. }
  246. private static VariableType GetVarTypeFromUses(Operand dest)
  247. {
  248. HashSet<Operand> visited = new HashSet<Operand>();
  249. Queue<Operand> pending = new Queue<Operand>();
  250. bool Enqueue(Operand operand)
  251. {
  252. if (visited.Add(operand))
  253. {
  254. pending.Enqueue(operand);
  255. return true;
  256. }
  257. return false;
  258. }
  259. Enqueue(dest);
  260. while (pending.TryDequeue(out Operand operand))
  261. {
  262. foreach (INode useNode in operand.UseOps)
  263. {
  264. if (!(useNode is Operation operation))
  265. {
  266. continue;
  267. }
  268. if (operation.Inst == Instruction.Copy)
  269. {
  270. if (operation.Dest.Type == OperandType.LocalVariable)
  271. {
  272. if (Enqueue(operation.Dest))
  273. {
  274. break;
  275. }
  276. }
  277. else
  278. {
  279. return OperandInfo.GetVarType(operation.Dest.Type);
  280. }
  281. }
  282. else
  283. {
  284. for (int index = 0; index < operation.SourcesCount; index++)
  285. {
  286. if (operation.GetSource(index) == operand)
  287. {
  288. return InstructionInfo.GetSrcVarType(operation.Inst, index);
  289. }
  290. }
  291. }
  292. }
  293. }
  294. return VariableType.S32;
  295. }
  296. private static bool AreAllSourceTypesEqual(IAstNode[] sources, VariableType type)
  297. {
  298. foreach (IAstNode node in sources)
  299. {
  300. if (!(node is AstOperand operand))
  301. {
  302. return false;
  303. }
  304. if (operand.VarType != type)
  305. {
  306. return false;
  307. }
  308. }
  309. return true;
  310. }
  311. private static bool IsBranchInst(Instruction inst)
  312. {
  313. switch (inst)
  314. {
  315. case Instruction.Branch:
  316. case Instruction.BranchIfFalse:
  317. case Instruction.BranchIfTrue:
  318. return true;
  319. }
  320. return false;
  321. }
  322. private static bool IsBitwiseInst(Instruction inst)
  323. {
  324. switch (inst)
  325. {
  326. case Instruction.BitwiseAnd:
  327. case Instruction.BitwiseExclusiveOr:
  328. case Instruction.BitwiseNot:
  329. case Instruction.BitwiseOr:
  330. return true;
  331. }
  332. return false;
  333. }
  334. private static Instruction GetLogicalFromBitwiseInst(Instruction inst)
  335. {
  336. switch (inst)
  337. {
  338. case Instruction.BitwiseAnd: return Instruction.LogicalAnd;
  339. case Instruction.BitwiseExclusiveOr: return Instruction.LogicalExclusiveOr;
  340. case Instruction.BitwiseNot: return Instruction.LogicalNot;
  341. case Instruction.BitwiseOr: return Instruction.LogicalOr;
  342. }
  343. throw new ArgumentException($"Unexpected instruction \"{inst}\".");
  344. }
  345. private static bool UsesStorage(Instruction inst)
  346. {
  347. if (inst == Instruction.LoadStorage || inst == Instruction.StoreStorage)
  348. {
  349. return true;
  350. }
  351. return inst.IsAtomic() && (inst & Instruction.MrMask) == Instruction.MrStorage;
  352. }
  353. }
  354. }