InstEmitFlowControl.cs 7.4 KB

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