StructuredProgram.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. AggregateType returnType = function.ReturnsValue ? AggregateType.S32 : AggregateType.Void;
  18. AggregateType[] inArguments = new AggregateType[function.InArgumentsCount];
  19. AggregateType[] outArguments = new AggregateType[function.OutArgumentsCount];
  20. for (int i = 0; i < inArguments.Length; i++)
  21. {
  22. inArguments[i] = AggregateType.S32;
  23. }
  24. for (int i = 0; i < outArguments.Length; i++)
  25. {
  26. outArguments[i] = AggregateType.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. StorageKind storageKind = operation.StorageKind;
  57. if ((inst == Instruction.Load || inst == Instruction.Store) && storageKind.IsInputOrOutput())
  58. {
  59. IoVariable ioVariable = (IoVariable)operation.GetSource(0).Value;
  60. bool isOutput = storageKind.IsOutput();
  61. bool perPatch = storageKind.IsPerPatch();
  62. int location = 0;
  63. int component = 0;
  64. if (context.Config.HasPerLocationInputOrOutput(ioVariable, isOutput))
  65. {
  66. location = operation.GetSource(1).Value;
  67. if (operation.SourcesCount > 2 &&
  68. operation.GetSource(2).Type == OperandType.Constant &&
  69. context.Config.HasPerLocationInputOrOutputComponent(ioVariable, location, operation.GetSource(2).Value, isOutput))
  70. {
  71. component = operation.GetSource(2).Value;
  72. }
  73. }
  74. context.Info.IoDefinitions.Add(new IoDefinition(storageKind, ioVariable, location, component));
  75. }
  76. bool vectorDest = IsVectorDestInst(inst);
  77. int sourcesCount = operation.SourcesCount;
  78. int outDestsCount = operation.DestsCount != 0 && !vectorDest ? operation.DestsCount - 1 : 0;
  79. IAstNode[] sources = new IAstNode[sourcesCount + outDestsCount];
  80. for (int index = 0; index < operation.SourcesCount; index++)
  81. {
  82. sources[index] = context.GetOperand(operation.GetSource(index));
  83. }
  84. for (int index = 0; index < outDestsCount; index++)
  85. {
  86. AstOperand oper = context.GetOperand(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. texOp.Index,
  100. sources);
  101. }
  102. int componentsCount = BitOperations.PopCount((uint)operation.Index);
  103. if (vectorDest && componentsCount > 1)
  104. {
  105. AggregateType destType = InstructionInfo.GetDestVarType(inst);
  106. IAstNode source;
  107. if (operation is TextureOperation texOp)
  108. {
  109. if (texOp.Inst == Instruction.ImageLoad)
  110. {
  111. destType = texOp.Format.GetComponentType();
  112. }
  113. source = GetAstTextureOperation(texOp);
  114. }
  115. else
  116. {
  117. source = new AstOperation(inst, operation.StorageKind, operation.Index, sources, operation.SourcesCount);
  118. }
  119. AggregateType destElemType = destType;
  120. switch (componentsCount)
  121. {
  122. case 2: destType |= AggregateType.Vector2; break;
  123. case 3: destType |= AggregateType.Vector3; break;
  124. case 4: destType |= AggregateType.Vector4; break;
  125. }
  126. AstOperand destVec = context.NewTemp(destType);
  127. context.AddNode(new AstAssignment(destVec, source));
  128. for (int i = 0; i < operation.DestsCount; i++)
  129. {
  130. AstOperand dest = context.GetOperand(operation.GetDest(i));
  131. AstOperand index = new AstOperand(OperandType.Constant, i);
  132. dest.VarType = destElemType;
  133. context.AddNode(new AstAssignment(dest, new AstOperation(Instruction.VectorExtract, StorageKind.None, new[] { destVec, index }, 2)));
  134. }
  135. }
  136. else if (operation.Dest != null)
  137. {
  138. AstOperand dest = context.GetOperand(operation.Dest);
  139. // If all the sources are bool, it's better to use short-circuiting
  140. // logical operations, rather than forcing a cast to int and doing
  141. // a bitwise operation with the value, as it is likely to be used as
  142. // a bool in the end.
  143. if (IsBitwiseInst(inst) && AreAllSourceTypesEqual(sources, AggregateType.Bool))
  144. {
  145. inst = GetLogicalFromBitwiseInst(inst);
  146. }
  147. bool isCondSel = inst == Instruction.ConditionalSelect;
  148. bool isCopy = inst == Instruction.Copy;
  149. if (isCondSel || isCopy)
  150. {
  151. AggregateType type = GetVarTypeFromUses(operation.Dest);
  152. if (isCondSel && type == AggregateType.FP32)
  153. {
  154. inst |= Instruction.FP32;
  155. }
  156. dest.VarType = type;
  157. }
  158. else
  159. {
  160. dest.VarType = InstructionInfo.GetDestVarType(inst);
  161. }
  162. IAstNode source;
  163. if (operation is TextureOperation texOp)
  164. {
  165. if (texOp.Inst == Instruction.ImageLoad)
  166. {
  167. dest.VarType = texOp.Format.GetComponentType();
  168. }
  169. source = GetAstTextureOperation(texOp);
  170. }
  171. else if (!isCopy)
  172. {
  173. source = new AstOperation(inst, operation.StorageKind, 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.AddNode(astTexOp);
  189. }
  190. else
  191. {
  192. context.AddNode(new AstOperation(inst, operation.StorageKind, operation.Index, sources, operation.SourcesCount));
  193. }
  194. // Those instructions needs to be emulated by using helper functions,
  195. // because they are NVIDIA specific. Those flags helps the backend to
  196. // decide which helper functions are needed on the final generated code.
  197. switch (operation.Inst)
  198. {
  199. case Instruction.AtomicMaxS32:
  200. case Instruction.AtomicMinS32:
  201. if (operation.StorageKind == StorageKind.SharedMemory)
  202. {
  203. context.Info.HelperFunctionsMask |= HelperFunctionsMask.AtomicMinMaxS32Shared;
  204. }
  205. else if (operation.StorageKind == StorageKind.StorageBuffer)
  206. {
  207. context.Info.HelperFunctionsMask |= HelperFunctionsMask.AtomicMinMaxS32Storage;
  208. }
  209. break;
  210. case Instruction.MultiplyHighS32:
  211. context.Info.HelperFunctionsMask |= HelperFunctionsMask.MultiplyHighS32;
  212. break;
  213. case Instruction.MultiplyHighU32:
  214. context.Info.HelperFunctionsMask |= HelperFunctionsMask.MultiplyHighU32;
  215. break;
  216. case Instruction.Shuffle:
  217. context.Info.HelperFunctionsMask |= HelperFunctionsMask.Shuffle;
  218. break;
  219. case Instruction.ShuffleDown:
  220. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleDown;
  221. break;
  222. case Instruction.ShuffleUp:
  223. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleUp;
  224. break;
  225. case Instruction.ShuffleXor:
  226. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleXor;
  227. break;
  228. case Instruction.StoreShared16:
  229. case Instruction.StoreShared8:
  230. context.Info.HelperFunctionsMask |= HelperFunctionsMask.StoreSharedSmallInt;
  231. break;
  232. case Instruction.StoreStorage16:
  233. case Instruction.StoreStorage8:
  234. context.Info.HelperFunctionsMask |= HelperFunctionsMask.StoreStorageSmallInt;
  235. break;
  236. case Instruction.SwizzleAdd:
  237. context.Info.HelperFunctionsMask |= HelperFunctionsMask.SwizzleAdd;
  238. break;
  239. case Instruction.FSIBegin:
  240. case Instruction.FSIEnd:
  241. context.Info.HelperFunctionsMask |= HelperFunctionsMask.FSI;
  242. break;
  243. }
  244. }
  245. private static AggregateType GetVarTypeFromUses(Operand dest)
  246. {
  247. HashSet<Operand> visited = new HashSet<Operand>();
  248. Queue<Operand> pending = new Queue<Operand>();
  249. bool Enqueue(Operand operand)
  250. {
  251. if (visited.Add(operand))
  252. {
  253. pending.Enqueue(operand);
  254. return true;
  255. }
  256. return false;
  257. }
  258. Enqueue(dest);
  259. while (pending.TryDequeue(out Operand operand))
  260. {
  261. foreach (INode useNode in operand.UseOps)
  262. {
  263. if (useNode is not Operation operation)
  264. {
  265. continue;
  266. }
  267. if (operation.Inst == Instruction.Copy)
  268. {
  269. if (operation.Dest.Type == OperandType.LocalVariable)
  270. {
  271. if (Enqueue(operation.Dest))
  272. {
  273. break;
  274. }
  275. }
  276. else
  277. {
  278. return OperandInfo.GetVarType(operation.Dest.Type);
  279. }
  280. }
  281. else
  282. {
  283. for (int index = 0; index < operation.SourcesCount; index++)
  284. {
  285. if (operation.GetSource(index) == operand)
  286. {
  287. return InstructionInfo.GetSrcVarType(operation.Inst, index);
  288. }
  289. }
  290. }
  291. }
  292. }
  293. return AggregateType.S32;
  294. }
  295. private static bool AreAllSourceTypesEqual(IAstNode[] sources, AggregateType type)
  296. {
  297. foreach (IAstNode node in sources)
  298. {
  299. if (node is not AstOperand operand)
  300. {
  301. return false;
  302. }
  303. if (operand.VarType != type)
  304. {
  305. return false;
  306. }
  307. }
  308. return true;
  309. }
  310. private static bool IsVectorDestInst(Instruction inst)
  311. {
  312. return inst switch
  313. {
  314. Instruction.ImageLoad or
  315. Instruction.TextureSample => true,
  316. _ => false
  317. };
  318. }
  319. private static bool IsBranchInst(Instruction inst)
  320. {
  321. return inst switch
  322. {
  323. Instruction.Branch or
  324. Instruction.BranchIfFalse or
  325. Instruction.BranchIfTrue => true,
  326. _ => false
  327. };
  328. }
  329. private static bool IsBitwiseInst(Instruction inst)
  330. {
  331. return inst switch
  332. {
  333. Instruction.BitwiseAnd or
  334. Instruction.BitwiseExclusiveOr or
  335. Instruction.BitwiseNot or
  336. Instruction.BitwiseOr => true,
  337. _ => false
  338. };
  339. }
  340. private static Instruction GetLogicalFromBitwiseInst(Instruction inst)
  341. {
  342. return inst switch
  343. {
  344. Instruction.BitwiseAnd => Instruction.LogicalAnd,
  345. Instruction.BitwiseExclusiveOr => Instruction.LogicalExclusiveOr,
  346. Instruction.BitwiseNot => Instruction.LogicalNot,
  347. Instruction.BitwiseOr => Instruction.LogicalOr,
  348. _ => throw new ArgumentException($"Unexpected instruction \"{inst}\".")
  349. };
  350. }
  351. }
  352. }