InstEmitFlowControl.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. EmitBrkOrSync(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 Exit(EmitterContext context)
  74. {
  75. InstExit op = context.GetOp<InstExit>();
  76. if (context.IsNonMain)
  77. {
  78. context.Config.GpuAccessor.Log("Invalid exit on non-main function.");
  79. return;
  80. }
  81. // TODO: Figure out how this is supposed to work in the
  82. // presence of other condition codes.
  83. if (op.Ccc == Ccc.T)
  84. {
  85. context.Return();
  86. }
  87. }
  88. public static void Kil(EmitterContext context)
  89. {
  90. InstKil op = context.GetOp<InstKil>();
  91. context.Discard();
  92. }
  93. public static void Pbk(EmitterContext context)
  94. {
  95. InstPbk op = context.GetOp<InstPbk>();
  96. EmitPbkOrSsy(context);
  97. }
  98. public static void Ret(EmitterContext context)
  99. {
  100. InstRet op = context.GetOp<InstRet>();
  101. if (context.IsNonMain)
  102. {
  103. context.Return();
  104. }
  105. else
  106. {
  107. context.Config.GpuAccessor.Log("Invalid return on main function.");
  108. }
  109. }
  110. public static void Ssy(EmitterContext context)
  111. {
  112. InstSsy op = context.GetOp<InstSsy>();
  113. EmitPbkOrSsy(context);
  114. }
  115. public static void Sync(EmitterContext context)
  116. {
  117. InstSync op = context.GetOp<InstSync>();
  118. EmitBrkOrSync(context);
  119. }
  120. private static void EmitPbkOrSsy(EmitterContext context)
  121. {
  122. var consumers = context.CurrBlock.PushOpCodes.First(x => x.Op.Address == context.CurrOp.Address).Consumers;
  123. foreach (KeyValuePair<Block, Operand> kv in consumers)
  124. {
  125. Block consumerBlock = kv.Key;
  126. Operand local = kv.Value;
  127. int id = consumerBlock.SyncTargets[context.CurrOp.Address].PushOpId;
  128. context.Copy(local, Const(id));
  129. }
  130. }
  131. private static void EmitBrkOrSync(EmitterContext context)
  132. {
  133. var targets = context.CurrBlock.SyncTargets;
  134. if (targets.Count == 1)
  135. {
  136. // If we have only one target, then the SSY/PBK is basically
  137. // a branch, we can produce better codegen for this case.
  138. EmitBranch(context, targets.Values.First().PushOpInfo.Op.GetAbsoluteAddress());
  139. }
  140. else
  141. {
  142. // TODO: Support CC here aswell (condition).
  143. foreach (SyncTarget target in targets.Values)
  144. {
  145. PushOpInfo pushOpInfo = target.PushOpInfo;
  146. Operand label = context.GetLabel(pushOpInfo.Op.GetAbsoluteAddress());
  147. Operand local = pushOpInfo.Consumers[context.CurrBlock];
  148. context.BranchIfTrue(label, context.ICompareEqual(local, Const(target.PushOpId)));
  149. }
  150. }
  151. }
  152. private static void EmitBranch(EmitterContext context, ulong address)
  153. {
  154. InstOp op = context.CurrOp;
  155. InstConditional opCond = new InstConditional(op.RawOpCode);
  156. // If we're branching to the next instruction, then the branch
  157. // is useless and we can ignore it.
  158. if (address == op.Address + 8)
  159. {
  160. return;
  161. }
  162. Operand label = context.GetLabel(address);
  163. Operand pred = Register(opCond.Pred, RegisterType.Predicate);
  164. if (opCond.Ccc != Ccc.T)
  165. {
  166. Operand cond = GetCondition(context, opCond.Ccc);
  167. if (opCond.Pred == RegisterConsts.PredicateTrueIndex)
  168. {
  169. pred = cond;
  170. }
  171. else if (opCond.PredInv)
  172. {
  173. pred = context.BitwiseAnd(context.BitwiseNot(pred), cond);
  174. }
  175. else
  176. {
  177. pred = context.BitwiseAnd(pred, cond);
  178. }
  179. context.BranchIfTrue(label, pred);
  180. }
  181. else if (opCond.Pred == RegisterConsts.PredicateTrueIndex)
  182. {
  183. context.Branch(label);
  184. }
  185. else if (opCond.PredInv)
  186. {
  187. context.BranchIfFalse(label, pred);
  188. }
  189. else
  190. {
  191. context.BranchIfTrue(label, pred);
  192. }
  193. }
  194. private static Operand GetCondition(EmitterContext context, Ccc cond)
  195. {
  196. // TODO: More condition codes, figure out how they work.
  197. switch (cond)
  198. {
  199. case Ccc.Eq:
  200. case Ccc.Equ:
  201. return GetZF();
  202. case Ccc.Ne:
  203. case Ccc.Neu:
  204. return context.BitwiseNot(GetZF());
  205. }
  206. return Const(IrConsts.True);
  207. }
  208. }
  209. }