Decoder.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. using Ryujinx.Graphics.Shader.Instructions;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  6. namespace Ryujinx.Graphics.Shader.Decoders
  7. {
  8. static class Decoder
  9. {
  10. public static Block[] Decode(IGpuAccessor gpuAccessor, ulong startAddress)
  11. {
  12. List<Block> blocks = new List<Block>();
  13. Queue<Block> workQueue = new Queue<Block>();
  14. Dictionary<ulong, Block> visited = new Dictionary<ulong, Block>();
  15. Block GetBlock(ulong blkAddress)
  16. {
  17. if (!visited.TryGetValue(blkAddress, out Block block))
  18. {
  19. block = new Block(blkAddress);
  20. workQueue.Enqueue(block);
  21. visited.Add(blkAddress, block);
  22. }
  23. return block;
  24. }
  25. GetBlock(0);
  26. while (workQueue.TryDequeue(out Block currBlock))
  27. {
  28. // Check if the current block is inside another block.
  29. if (BinarySearch(blocks, currBlock.Address, out int nBlkIndex))
  30. {
  31. Block nBlock = blocks[nBlkIndex];
  32. if (nBlock.Address == currBlock.Address)
  33. {
  34. throw new InvalidOperationException("Found duplicate block address on the list.");
  35. }
  36. nBlock.Split(currBlock);
  37. blocks.Insert(nBlkIndex + 1, currBlock);
  38. continue;
  39. }
  40. // If we have a block after the current one, set the limit address.
  41. ulong limitAddress = ulong.MaxValue;
  42. if (nBlkIndex != blocks.Count)
  43. {
  44. Block nBlock = blocks[nBlkIndex];
  45. int nextIndex = nBlkIndex + 1;
  46. if (nBlock.Address < currBlock.Address && nextIndex < blocks.Count)
  47. {
  48. limitAddress = blocks[nextIndex].Address;
  49. }
  50. else if (nBlock.Address > currBlock.Address)
  51. {
  52. limitAddress = blocks[nBlkIndex].Address;
  53. }
  54. }
  55. FillBlock(gpuAccessor, currBlock, limitAddress, startAddress);
  56. if (currBlock.OpCodes.Count != 0)
  57. {
  58. // We should have blocks for all possible branch targets,
  59. // including those from SSY/PBK instructions.
  60. foreach (OpCodePush pushOp in currBlock.PushOpCodes)
  61. {
  62. GetBlock(pushOp.GetAbsoluteAddress());
  63. }
  64. // Set child blocks. "Branch" is the block the branch instruction
  65. // points to (when taken), "Next" is the block at the next address,
  66. // executed when the branch is not taken. For Unconditional Branches
  67. // or end of program, Next is null.
  68. OpCode lastOp = currBlock.GetLastOp();
  69. if (lastOp is OpCodeBranch opBr)
  70. {
  71. currBlock.Branch = GetBlock(opBr.GetAbsoluteAddress());
  72. }
  73. else if (lastOp is OpCodeBranchIndir opBrIndir)
  74. {
  75. // An indirect branch could go anywhere, we don't know the target.
  76. // Those instructions are usually used on a switch to jump table
  77. // compiler optimization, and in those cases the possible targets
  78. // seems to be always right after the BRX itself. We can assume
  79. // that the possible targets are all the blocks in-between the
  80. // instruction right after the BRX, and the common target that
  81. // all the "cases" should eventually jump to, acting as the
  82. // switch break.
  83. Block firstTarget = GetBlock(currBlock.EndAddress);
  84. firstTarget.BrIndir = opBrIndir;
  85. opBrIndir.PossibleTargets.Add(firstTarget);
  86. }
  87. if (!IsUnconditionalBranch(lastOp))
  88. {
  89. currBlock.Next = GetBlock(currBlock.EndAddress);
  90. }
  91. }
  92. // Insert the new block on the list (sorted by address).
  93. if (blocks.Count != 0)
  94. {
  95. Block nBlock = blocks[nBlkIndex];
  96. blocks.Insert(nBlkIndex + (nBlock.Address < currBlock.Address ? 1 : 0), currBlock);
  97. }
  98. else
  99. {
  100. blocks.Add(currBlock);
  101. }
  102. // Do we have a block after the current one?
  103. if (!IsExit(currBlock.GetLastOp()) && currBlock.BrIndir != null)
  104. {
  105. bool targetVisited = visited.ContainsKey(currBlock.EndAddress);
  106. Block possibleTarget = GetBlock(currBlock.EndAddress);
  107. currBlock.BrIndir.PossibleTargets.Add(possibleTarget);
  108. if (!targetVisited)
  109. {
  110. possibleTarget.BrIndir = currBlock.BrIndir;
  111. }
  112. }
  113. }
  114. foreach (Block block in blocks.Where(x => x.PushOpCodes.Count != 0))
  115. {
  116. for (int pushOpIndex = 0; pushOpIndex < block.PushOpCodes.Count; pushOpIndex++)
  117. {
  118. PropagatePushOp(visited, block, pushOpIndex);
  119. }
  120. }
  121. return blocks.ToArray();
  122. }
  123. private static bool BinarySearch(List<Block> blocks, ulong address, out int index)
  124. {
  125. index = 0;
  126. int left = 0;
  127. int right = blocks.Count - 1;
  128. while (left <= right)
  129. {
  130. int size = right - left;
  131. int middle = left + (size >> 1);
  132. Block block = blocks[middle];
  133. index = middle;
  134. if (address >= block.Address && address < block.EndAddress)
  135. {
  136. return true;
  137. }
  138. if (address < block.Address)
  139. {
  140. right = middle - 1;
  141. }
  142. else
  143. {
  144. left = middle + 1;
  145. }
  146. }
  147. return false;
  148. }
  149. private static void FillBlock(
  150. IGpuAccessor gpuAccessor,
  151. Block block,
  152. ulong limitAddress,
  153. ulong startAddress)
  154. {
  155. ulong address = block.Address;
  156. do
  157. {
  158. if (address + 7 >= limitAddress)
  159. {
  160. break;
  161. }
  162. // Ignore scheduling instructions, which are written every 32 bytes.
  163. if ((address & 0x1f) == 0)
  164. {
  165. address += 8;
  166. continue;
  167. }
  168. ulong opAddress = address;
  169. address += 8;
  170. long opCode = gpuAccessor.MemoryRead<long>(startAddress + opAddress);
  171. (InstEmitter emitter, OpCodeTable.OpActivator opActivator) = OpCodeTable.GetEmitter(opCode);
  172. if (emitter == null)
  173. {
  174. // TODO: Warning, illegal encoding.
  175. block.OpCodes.Add(new OpCode(null, opAddress, opCode));
  176. continue;
  177. }
  178. if (opActivator == null)
  179. {
  180. throw new ArgumentNullException(nameof(opActivator));
  181. }
  182. OpCode op = (OpCode)opActivator(emitter, opAddress, opCode);
  183. block.OpCodes.Add(op);
  184. }
  185. while (!IsBranch(block.GetLastOp()));
  186. block.EndAddress = address;
  187. block.UpdatePushOps();
  188. }
  189. private static bool IsUnconditionalBranch(OpCode opCode)
  190. {
  191. return IsUnconditional(opCode) && IsBranch(opCode);
  192. }
  193. private static bool IsUnconditional(OpCode opCode)
  194. {
  195. if (opCode is OpCodeExit op && op.Condition != Condition.Always)
  196. {
  197. return false;
  198. }
  199. return opCode.Predicate.Index == RegisterConsts.PredicateTrueIndex && !opCode.InvertPredicate;
  200. }
  201. private static bool IsBranch(OpCode opCode)
  202. {
  203. return (opCode is OpCodeBranch opBranch && !opBranch.PushTarget) ||
  204. opCode is OpCodeBranchIndir ||
  205. opCode is OpCodeBranchPop ||
  206. opCode is OpCodeExit;
  207. }
  208. private static bool IsExit(OpCode opCode)
  209. {
  210. return opCode is OpCodeExit;
  211. }
  212. private struct PathBlockState
  213. {
  214. public Block Block { get; }
  215. private enum RestoreType
  216. {
  217. None,
  218. PopPushOp,
  219. PushBranchOp
  220. }
  221. private RestoreType _restoreType;
  222. private ulong _restoreValue;
  223. public bool ReturningFromVisit => _restoreType != RestoreType.None;
  224. public PathBlockState(Block block)
  225. {
  226. Block = block;
  227. _restoreType = RestoreType.None;
  228. _restoreValue = 0;
  229. }
  230. public PathBlockState(int oldStackSize)
  231. {
  232. Block = null;
  233. _restoreType = RestoreType.PopPushOp;
  234. _restoreValue = (ulong)oldStackSize;
  235. }
  236. public PathBlockState(ulong syncAddress)
  237. {
  238. Block = null;
  239. _restoreType = RestoreType.PushBranchOp;
  240. _restoreValue = syncAddress;
  241. }
  242. public void RestoreStackState(Stack<ulong> branchStack)
  243. {
  244. if (_restoreType == RestoreType.PushBranchOp)
  245. {
  246. branchStack.Push(_restoreValue);
  247. }
  248. else if (_restoreType == RestoreType.PopPushOp)
  249. {
  250. while (branchStack.Count > (uint)_restoreValue)
  251. {
  252. branchStack.Pop();
  253. }
  254. }
  255. }
  256. }
  257. private static void PropagatePushOp(Dictionary<ulong, Block> blocks, Block currBlock, int pushOpIndex)
  258. {
  259. OpCodePush pushOp = currBlock.PushOpCodes[pushOpIndex];
  260. Stack<PathBlockState> workQueue = new Stack<PathBlockState>();
  261. HashSet<Block> visited = new HashSet<Block>();
  262. Stack<ulong> branchStack = new Stack<ulong>();
  263. void Push(PathBlockState pbs)
  264. {
  265. // When block is null, this means we are pushing a restore operation.
  266. // Restore operations are used to undo the work done inside a block
  267. // when we return from it, for example it pops addresses pushed by
  268. // SSY/PBK instructions inside the block, and pushes addresses poped
  269. // by SYNC/BRK.
  270. // For blocks, if it's already visited, we just ignore to avoid going
  271. // around in circles and getting stuck here.
  272. if (pbs.Block == null || !visited.Contains(pbs.Block))
  273. {
  274. workQueue.Push(pbs);
  275. }
  276. }
  277. Push(new PathBlockState(currBlock));
  278. while (workQueue.TryPop(out PathBlockState pbs))
  279. {
  280. if (pbs.ReturningFromVisit)
  281. {
  282. pbs.RestoreStackState(branchStack);
  283. continue;
  284. }
  285. Block current = pbs.Block;
  286. // If the block was already processed, we just ignore it, otherwise
  287. // we would push the same child blocks of an already processed block,
  288. // and go around in circles until memory is exhausted.
  289. if (!visited.Add(current))
  290. {
  291. continue;
  292. }
  293. int pushOpsCount = current.PushOpCodes.Count;
  294. if (pushOpsCount != 0)
  295. {
  296. Push(new PathBlockState(branchStack.Count));
  297. for (int index = pushOpIndex; index < pushOpsCount; index++)
  298. {
  299. branchStack.Push(current.PushOpCodes[index].GetAbsoluteAddress());
  300. }
  301. }
  302. pushOpIndex = 0;
  303. if (current.Next != null)
  304. {
  305. Push(new PathBlockState(current.Next));
  306. }
  307. if (current.Branch != null)
  308. {
  309. Push(new PathBlockState(current.Branch));
  310. }
  311. else if (current.GetLastOp() is OpCodeBranchIndir brIndir)
  312. {
  313. // By adding them in descending order (sorted by address), we process the blocks
  314. // in order (of ascending address), since we work with a LIFO.
  315. foreach (Block possibleTarget in brIndir.PossibleTargets.OrderByDescending(x => x.Address))
  316. {
  317. Push(new PathBlockState(possibleTarget));
  318. }
  319. }
  320. else if (current.GetLastOp() is OpCodeBranchPop op)
  321. {
  322. ulong targetAddress = branchStack.Pop();
  323. if (branchStack.Count == 0)
  324. {
  325. branchStack.Push(targetAddress);
  326. op.Targets.Add(pushOp, op.Targets.Count);
  327. pushOp.PopOps.TryAdd(op, Local());
  328. }
  329. else
  330. {
  331. // First we push the target address (this will be used to push the
  332. // address back into the SSY/PBK stack when we return from that block),
  333. // then we push the block itself into the work "queue" (well, it's a stack)
  334. // for processing.
  335. Push(new PathBlockState(targetAddress));
  336. Push(new PathBlockState(blocks[targetAddress]));
  337. }
  338. }
  339. }
  340. }
  341. }
  342. }