InstEmitFlowControl.cs 8.7 KB

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