StructuredProgram.cs 15 KB

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