InstEmitFlowControl.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using Ryujinx.Graphics.Shader.Decoders;
  2. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  3. using Ryujinx.Graphics.Shader.Translation;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using static Ryujinx.Graphics.Shader.Instructions.InstEmitHelper;
  7. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  8. namespace Ryujinx.Graphics.Shader.Instructions
  9. {
  10. static partial class InstEmit
  11. {
  12. public static void Bra(EmitterContext context)
  13. {
  14. InstBra op = context.GetOp<InstBra>();
  15. EmitBranch(context, context.CurrBlock.Successors[^1].Address);
  16. }
  17. public static void Brk(EmitterContext context)
  18. {
  19. InstBrk op = context.GetOp<InstBrk>();
  20. EmitBrkContSync(context);
  21. }
  22. public static void Brx(EmitterContext context)
  23. {
  24. InstBrx op = context.GetOp<InstBrx>();
  25. InstOp currOp = context.CurrOp;
  26. int startIndex = context.CurrBlock.HasNext() ? 1 : 0;
  27. if (context.CurrBlock.Successors.Count <= startIndex)
  28. {
  29. context.Config.GpuAccessor.Log($"Failed to find targets for BRX instruction at 0x{currOp.Address:X}.");
  30. return;
  31. }
  32. int offset = (int)currOp.GetAbsoluteAddress();
  33. Operand address = context.IAdd(Register(op.SrcA, RegisterType.Gpr), Const(offset));
  34. // Sorting the target addresses in descending order improves the code,
  35. // since it will always check the most distant targets first, then the
  36. // near ones. This can be easily transformed into if/else statements.
  37. var sortedTargets = context.CurrBlock.Successors.Skip(startIndex).OrderByDescending(x => x.Address);
  38. Block lastTarget = sortedTargets.LastOrDefault();
  39. foreach (Block possibleTarget in sortedTargets)
  40. {
  41. Operand label = context.GetLabel(possibleTarget.Address);
  42. if (possibleTarget != lastTarget)
  43. {
  44. context.BranchIfTrue(label, context.ICompareEqual(address, Const((int)possibleTarget.Address)));
  45. }
  46. else
  47. {
  48. context.Branch(label);
  49. }
  50. }
  51. }
  52. public static void Cal(EmitterContext context)
  53. {
  54. InstCal op = context.GetOp<InstCal>();
  55. DecodedFunction function = context.Program.GetFunctionByAddress(context.CurrOp.GetAbsoluteAddress());
  56. if (function.IsCompilerGenerated)
  57. {
  58. switch (function.Type)
  59. {
  60. case FunctionType.BuiltInFSIBegin:
  61. context.FSIBegin();
  62. break;
  63. case FunctionType.BuiltInFSIEnd:
  64. context.FSIEnd();
  65. break;
  66. }
  67. }
  68. else
  69. {
  70. context.Call(function.Id, false);
  71. }
  72. }
  73. public static void Cont(EmitterContext context)
  74. {
  75. InstCont op = context.GetOp<InstCont>();
  76. EmitBrkContSync(context);
  77. }
  78. public static void Exit(EmitterContext context)
  79. {
  80. InstExit op = context.GetOp<InstExit>();
  81. if (context.IsNonMain)
  82. {
  83. context.Config.GpuAccessor.Log("Invalid exit on non-main function.");
  84. return;
  85. }
  86. // TODO: Figure out how this is supposed to work in the
  87. // presence of other condition codes.
  88. if (op.Ccc == Ccc.T)
  89. {
  90. context.Return();
  91. }
  92. }
  93. public static void Kil(EmitterContext context)
  94. {
  95. InstKil op = context.GetOp<InstKil>();
  96. context.Discard();
  97. }
  98. public static void Pbk(EmitterContext context)
  99. {
  100. InstPbk op = context.GetOp<InstPbk>();
  101. EmitPbkPcntSsy(context);
  102. }
  103. public static void Pcnt(EmitterContext context)
  104. {
  105. InstPcnt op = context.GetOp<InstPcnt>();
  106. EmitPbkPcntSsy(context);
  107. }
  108. public static void Ret(EmitterContext context)
  109. {
  110. InstRet op = context.GetOp<InstRet>();
  111. if (context.IsNonMain)
  112. {
  113. context.Return();
  114. }
  115. else
  116. {
  117. context.Config.GpuAccessor.Log("Invalid return on main function.");
  118. }
  119. }
  120. public static void Ssy(EmitterContext context)
  121. {
  122. InstSsy op = context.GetOp<InstSsy>();
  123. EmitPbkPcntSsy(context);
  124. }
  125. public static void Sync(EmitterContext context)
  126. {
  127. InstSync op = context.GetOp<InstSync>();
  128. EmitBrkContSync(context);
  129. }
  130. private static void EmitPbkPcntSsy(EmitterContext context)
  131. {
  132. var consumers = context.CurrBlock.PushOpCodes.First(x => x.Op.Address == context.CurrOp.Address).Consumers;
  133. foreach (KeyValuePair<Block, Operand> kv in consumers)
  134. {
  135. Block consumerBlock = kv.Key;
  136. Operand local = kv.Value;
  137. int id = consumerBlock.SyncTargets[context.CurrOp.Address].PushOpId;
  138. context.Copy(local, Const(id));
  139. }
  140. }
  141. private static void EmitBrkContSync(EmitterContext context)
  142. {
  143. var targets = context.CurrBlock.SyncTargets;
  144. if (targets.Count == 1)
  145. {
  146. // If we have only one target, then the SSY/PBK is basically
  147. // a branch, we can produce better codegen for this case.
  148. EmitBranch(context, targets.Values.First().PushOpInfo.Op.GetAbsoluteAddress());
  149. }
  150. else
  151. {
  152. // TODO: Support CC here aswell (condition).
  153. foreach (SyncTarget target in targets.Values)
  154. {
  155. PushOpInfo pushOpInfo = target.PushOpInfo;
  156. Operand label = context.GetLabel(pushOpInfo.Op.GetAbsoluteAddress());
  157. Operand local = pushOpInfo.Consumers[context.CurrBlock];
  158. context.BranchIfTrue(label, context.ICompareEqual(local, Const(target.PushOpId)));
  159. }
  160. }
  161. }
  162. private static void EmitBranch(EmitterContext context, ulong address)
  163. {
  164. InstOp op = context.CurrOp;
  165. InstConditional opCond = new InstConditional(op.RawOpCode);
  166. // If we're branching to the next instruction, then the branch
  167. // is useless and we can ignore it.
  168. if (address == op.Address + 8)
  169. {
  170. return;
  171. }
  172. Operand label = context.GetLabel(address);
  173. Operand pred = Register(opCond.Pred, RegisterType.Predicate);
  174. if (opCond.Ccc != Ccc.T)
  175. {
  176. Operand cond = GetCondition(context, opCond.Ccc);
  177. if (opCond.Pred == RegisterConsts.PredicateTrueIndex)
  178. {
  179. pred = cond;
  180. }
  181. else if (opCond.PredInv)
  182. {
  183. pred = context.BitwiseAnd(context.BitwiseNot(pred), cond);
  184. }
  185. else
  186. {
  187. pred = context.BitwiseAnd(pred, cond);
  188. }
  189. context.BranchIfTrue(label, pred);
  190. }
  191. else if (opCond.Pred == RegisterConsts.PredicateTrueIndex)
  192. {
  193. context.Branch(label);
  194. }
  195. else if (opCond.PredInv)
  196. {
  197. context.BranchIfFalse(label, pred);
  198. }
  199. else
  200. {
  201. context.BranchIfTrue(label, pred);
  202. }
  203. }
  204. private static Operand GetCondition(EmitterContext context, Ccc cond)
  205. {
  206. // TODO: More condition codes, figure out how they work.
  207. switch (cond)
  208. {
  209. case Ccc.Eq:
  210. case Ccc.Equ:
  211. return GetZF();
  212. case Ccc.Ne:
  213. case Ccc.Neu:
  214. return context.BitwiseNot(GetZF());
  215. }
  216. return Const(IrConsts.True);
  217. }
  218. }
  219. }