InstEmitFlowControl.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. var targets = context.CurrBlock.Successors.Skip(startIndex);
  35. bool allTargetsSinglePred = true;
  36. int total = context.CurrBlock.Successors.Count - startIndex;
  37. int count = 0;
  38. foreach (var target in targets.OrderBy(x => x.Address))
  39. {
  40. if (++count < total && (target.Predecessors.Count > 1 || target.Address <= context.CurrBlock.Address))
  41. {
  42. allTargetsSinglePred = false;
  43. break;
  44. }
  45. }
  46. if (allTargetsSinglePred)
  47. {
  48. // Chain blocks, each target block will check if the BRX target address
  49. // matches its own address, if not, it jumps to the next target which will do the same check,
  50. // until it reaches the last possible target, which executed unconditionally.
  51. // We can only do this if the BRX block is the only predecessor of all target blocks.
  52. // Additionally, this is not supported for blocks located before the current block,
  53. // since it will be too late to insert a label, but this is something that can be improved
  54. // in the future if necessary.
  55. var sortedTargets = targets.OrderBy(x => x.Address);
  56. Block currentTarget = null;
  57. ulong firstTargetAddress = 0;
  58. foreach (Block nextTarget in sortedTargets)
  59. {
  60. if (currentTarget != null)
  61. {
  62. if (currentTarget.Address != nextTarget.Address)
  63. {
  64. context.SetBrxTarget(currentTarget.Address, address, (int)currentTarget.Address, nextTarget.Address);
  65. }
  66. }
  67. else
  68. {
  69. firstTargetAddress = nextTarget.Address;
  70. }
  71. currentTarget = nextTarget;
  72. }
  73. context.Branch(context.GetLabel(firstTargetAddress));
  74. }
  75. else
  76. {
  77. // Emit the branches sequentially.
  78. // This generates slightly worse code, but should work for all cases.
  79. var sortedTargets = targets.OrderByDescending(x => x.Address);
  80. ulong lastTargetAddress = ulong.MaxValue;
  81. count = 0;
  82. foreach (Block target in sortedTargets)
  83. {
  84. Operand label = context.GetLabel(target.Address);
  85. if (++count < total)
  86. {
  87. if (target.Address != lastTargetAddress)
  88. {
  89. context.BranchIfTrue(label, context.ICompareEqual(address, Const((int)target.Address)));
  90. }
  91. lastTargetAddress = target.Address;
  92. }
  93. else
  94. {
  95. context.Branch(label);
  96. }
  97. }
  98. }
  99. }
  100. public static void Cal(EmitterContext context)
  101. {
  102. InstCal op = context.GetOp<InstCal>();
  103. DecodedFunction function = context.Program.GetFunctionByAddress(context.CurrOp.GetAbsoluteAddress());
  104. if (function.IsCompilerGenerated)
  105. {
  106. switch (function.Type)
  107. {
  108. case FunctionType.BuiltInFSIBegin:
  109. context.FSIBegin();
  110. break;
  111. case FunctionType.BuiltInFSIEnd:
  112. context.FSIEnd();
  113. break;
  114. }
  115. }
  116. else
  117. {
  118. context.Call(function.Id, false);
  119. }
  120. }
  121. public static void Cont(EmitterContext context)
  122. {
  123. InstCont op = context.GetOp<InstCont>();
  124. EmitBrkContSync(context);
  125. }
  126. public static void Exit(EmitterContext context)
  127. {
  128. InstExit op = context.GetOp<InstExit>();
  129. if (context.IsNonMain)
  130. {
  131. context.Config.GpuAccessor.Log("Invalid exit on non-main function.");
  132. return;
  133. }
  134. if (op.Ccc == Ccc.T)
  135. {
  136. context.Return();
  137. }
  138. else
  139. {
  140. Operand cond = GetCondition(context, op.Ccc, IrConsts.False);
  141. // If the condition is always false, we don't need to do anything.
  142. if (cond.Type != OperandType.Constant || cond.Value != IrConsts.False)
  143. {
  144. Operand lblSkip = Label();
  145. context.BranchIfFalse(lblSkip, cond);
  146. context.Return();
  147. context.MarkLabel(lblSkip);
  148. }
  149. }
  150. }
  151. public static void Kil(EmitterContext context)
  152. {
  153. InstKil op = context.GetOp<InstKil>();
  154. context.Discard();
  155. }
  156. public static void Pbk(EmitterContext context)
  157. {
  158. InstPbk op = context.GetOp<InstPbk>();
  159. EmitPbkPcntSsy(context);
  160. }
  161. public static void Pcnt(EmitterContext context)
  162. {
  163. InstPcnt op = context.GetOp<InstPcnt>();
  164. EmitPbkPcntSsy(context);
  165. }
  166. public static void Ret(EmitterContext context)
  167. {
  168. InstRet op = context.GetOp<InstRet>();
  169. if (context.IsNonMain)
  170. {
  171. context.Return();
  172. }
  173. else
  174. {
  175. context.Config.GpuAccessor.Log("Invalid return on main function.");
  176. }
  177. }
  178. public static void Ssy(EmitterContext context)
  179. {
  180. InstSsy op = context.GetOp<InstSsy>();
  181. EmitPbkPcntSsy(context);
  182. }
  183. public static void Sync(EmitterContext context)
  184. {
  185. InstSync op = context.GetOp<InstSync>();
  186. EmitBrkContSync(context);
  187. }
  188. private static void EmitPbkPcntSsy(EmitterContext context)
  189. {
  190. var consumers = context.CurrBlock.PushOpCodes.First(x => x.Op.Address == context.CurrOp.Address).Consumers;
  191. foreach (KeyValuePair<Block, Operand> kv in consumers)
  192. {
  193. Block consumerBlock = kv.Key;
  194. Operand local = kv.Value;
  195. int id = consumerBlock.SyncTargets[context.CurrOp.Address].PushOpId;
  196. context.Copy(local, Const(id));
  197. }
  198. }
  199. private static void EmitBrkContSync(EmitterContext context)
  200. {
  201. var targets = context.CurrBlock.SyncTargets;
  202. if (targets.Count == 1)
  203. {
  204. // If we have only one target, then the SSY/PBK is basically
  205. // a branch, we can produce better codegen for this case.
  206. EmitBranch(context, targets.Values.First().PushOpInfo.Op.GetAbsoluteAddress());
  207. }
  208. else
  209. {
  210. // TODO: Support CC here as well (condition).
  211. foreach (SyncTarget target in targets.Values)
  212. {
  213. PushOpInfo pushOpInfo = target.PushOpInfo;
  214. Operand label = context.GetLabel(pushOpInfo.Op.GetAbsoluteAddress());
  215. Operand local = pushOpInfo.Consumers[context.CurrBlock];
  216. context.BranchIfTrue(label, context.ICompareEqual(local, Const(target.PushOpId)));
  217. }
  218. }
  219. }
  220. private static void EmitBranch(EmitterContext context, ulong address)
  221. {
  222. InstOp op = context.CurrOp;
  223. InstConditional opCond = new InstConditional(op.RawOpCode);
  224. // If we're branching to the next instruction, then the branch
  225. // is useless and we can ignore it.
  226. if (address == op.Address + 8)
  227. {
  228. return;
  229. }
  230. Operand label = context.GetLabel(address);
  231. Operand pred = Register(opCond.Pred, RegisterType.Predicate);
  232. if (opCond.Ccc != Ccc.T)
  233. {
  234. Operand cond = GetCondition(context, opCond.Ccc);
  235. if (opCond.Pred == RegisterConsts.PredicateTrueIndex)
  236. {
  237. pred = cond;
  238. }
  239. else if (opCond.PredInv)
  240. {
  241. pred = context.BitwiseAnd(context.BitwiseNot(pred), cond);
  242. }
  243. else
  244. {
  245. pred = context.BitwiseAnd(pred, cond);
  246. }
  247. context.BranchIfTrue(label, pred);
  248. }
  249. else if (opCond.Pred == RegisterConsts.PredicateTrueIndex)
  250. {
  251. context.Branch(label);
  252. }
  253. else if (opCond.PredInv)
  254. {
  255. context.BranchIfFalse(label, pred);
  256. }
  257. else
  258. {
  259. context.BranchIfTrue(label, pred);
  260. }
  261. }
  262. }
  263. }