Decoder.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. using Ryujinx.Graphics.Shader.Translation;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  7. namespace Ryujinx.Graphics.Shader.Decoders
  8. {
  9. static class Decoder
  10. {
  11. public static DecodedProgram Decode(ShaderConfig config, ulong startAddress)
  12. {
  13. Queue<DecodedFunction> functionsQueue = new Queue<DecodedFunction>();
  14. Dictionary<ulong, DecodedFunction> functionsVisited = new Dictionary<ulong, DecodedFunction>();
  15. DecodedFunction EnqueueFunction(ulong address)
  16. {
  17. if (!functionsVisited.TryGetValue(address, out DecodedFunction function))
  18. {
  19. functionsVisited.Add(address, function = new DecodedFunction(address));
  20. functionsQueue.Enqueue(function);
  21. }
  22. return function;
  23. }
  24. DecodedFunction mainFunction = EnqueueFunction(0);
  25. while (functionsQueue.TryDequeue(out DecodedFunction currentFunction))
  26. {
  27. List<Block> blocks = new List<Block>();
  28. Queue<Block> workQueue = new Queue<Block>();
  29. Dictionary<ulong, Block> visited = new Dictionary<ulong, Block>();
  30. Block GetBlock(ulong blkAddress)
  31. {
  32. if (!visited.TryGetValue(blkAddress, out Block block))
  33. {
  34. block = new Block(blkAddress);
  35. workQueue.Enqueue(block);
  36. visited.Add(blkAddress, block);
  37. }
  38. return block;
  39. }
  40. GetBlock(currentFunction.Address);
  41. bool hasNewTarget;
  42. do
  43. {
  44. while (workQueue.TryDequeue(out Block currBlock))
  45. {
  46. // Check if the current block is inside another block.
  47. if (BinarySearch(blocks, currBlock.Address, out int nBlkIndex))
  48. {
  49. Block nBlock = blocks[nBlkIndex];
  50. if (nBlock.Address == currBlock.Address)
  51. {
  52. throw new InvalidOperationException("Found duplicate block address on the list.");
  53. }
  54. nBlock.Split(currBlock);
  55. blocks.Insert(nBlkIndex + 1, currBlock);
  56. continue;
  57. }
  58. // If we have a block after the current one, set the limit address.
  59. ulong limitAddress = ulong.MaxValue;
  60. if (nBlkIndex != blocks.Count)
  61. {
  62. Block nBlock = blocks[nBlkIndex];
  63. int nextIndex = nBlkIndex + 1;
  64. if (nBlock.Address < currBlock.Address && nextIndex < blocks.Count)
  65. {
  66. limitAddress = blocks[nextIndex].Address;
  67. }
  68. else if (nBlock.Address > currBlock.Address)
  69. {
  70. limitAddress = blocks[nBlkIndex].Address;
  71. }
  72. }
  73. FillBlock(config, currBlock, limitAddress, startAddress);
  74. if (currBlock.OpCodes.Count != 0)
  75. {
  76. // We should have blocks for all possible branch targets,
  77. // including those from PBK/PCNT/SSY instructions.
  78. foreach (PushOpInfo pushOp in currBlock.PushOpCodes)
  79. {
  80. GetBlock(pushOp.Op.GetAbsoluteAddress());
  81. }
  82. // Set child blocks. "Branch" is the block the branch instruction
  83. // points to (when taken), "Next" is the block at the next address,
  84. // executed when the branch is not taken. For Unconditional Branches
  85. // or end of program, Next is null.
  86. InstOp lastOp = currBlock.GetLastOp();
  87. if (lastOp.Name == InstName.Cal)
  88. {
  89. EnqueueFunction(lastOp.GetAbsoluteAddress()).AddCaller(currentFunction);
  90. }
  91. else if (lastOp.Name == InstName.Bra)
  92. {
  93. Block succBlock = GetBlock(lastOp.GetAbsoluteAddress());
  94. currBlock.Successors.Add(succBlock);
  95. succBlock.Predecessors.Add(currBlock);
  96. }
  97. if (!IsUnconditionalBranch(ref lastOp))
  98. {
  99. Block succBlock = GetBlock(currBlock.EndAddress);
  100. currBlock.Successors.Insert(0, succBlock);
  101. succBlock.Predecessors.Add(currBlock);
  102. }
  103. }
  104. // Insert the new block on the list (sorted by address).
  105. if (blocks.Count != 0)
  106. {
  107. Block nBlock = blocks[nBlkIndex];
  108. blocks.Insert(nBlkIndex + (nBlock.Address < currBlock.Address ? 1 : 0), currBlock);
  109. }
  110. else
  111. {
  112. blocks.Add(currBlock);
  113. }
  114. }
  115. // Propagate SSY/PBK addresses into their uses (SYNC/BRK).
  116. foreach (Block block in blocks.Where(x => x.PushOpCodes.Count != 0))
  117. {
  118. for (int pushOpIndex = 0; pushOpIndex < block.PushOpCodes.Count; pushOpIndex++)
  119. {
  120. PropagatePushOp(visited, block, pushOpIndex);
  121. }
  122. }
  123. // Try to find targets for BRX (indirect branch) instructions.
  124. hasNewTarget = FindBrxTargets(config, blocks, GetBlock);
  125. // If we discovered new branch targets from the BRX instruction,
  126. // we need another round of decoding to decode the new blocks.
  127. // Additionally, we may have more SSY/PBK targets to propagate,
  128. // and new BRX instructions.
  129. }
  130. while (hasNewTarget);
  131. currentFunction.SetBlocks(blocks.ToArray());
  132. }
  133. return new DecodedProgram(mainFunction, functionsVisited);
  134. }
  135. private static bool BinarySearch(List<Block> blocks, ulong address, out int index)
  136. {
  137. index = 0;
  138. int left = 0;
  139. int right = blocks.Count - 1;
  140. while (left <= right)
  141. {
  142. int size = right - left;
  143. int middle = left + (size >> 1);
  144. Block block = blocks[middle];
  145. index = middle;
  146. if (address >= block.Address && address < block.EndAddress)
  147. {
  148. return true;
  149. }
  150. if (address < block.Address)
  151. {
  152. right = middle - 1;
  153. }
  154. else
  155. {
  156. left = middle + 1;
  157. }
  158. }
  159. return false;
  160. }
  161. private static void FillBlock(ShaderConfig config, Block block, ulong limitAddress, ulong startAddress)
  162. {
  163. IGpuAccessor gpuAccessor = config.GpuAccessor;
  164. ulong address = block.Address;
  165. int bufferOffset = 0;
  166. ReadOnlySpan<ulong> buffer = ReadOnlySpan<ulong>.Empty;
  167. InstOp op = default;
  168. do
  169. {
  170. if (address + 7 >= limitAddress)
  171. {
  172. break;
  173. }
  174. // Ignore scheduling instructions, which are written every 32 bytes.
  175. if ((address & 0x1f) == 0)
  176. {
  177. address += 8;
  178. bufferOffset++;
  179. continue;
  180. }
  181. if (bufferOffset >= buffer.Length)
  182. {
  183. buffer = gpuAccessor.GetCode(startAddress + address, 8);
  184. bufferOffset = 0;
  185. }
  186. ulong opCode = buffer[bufferOffset++];
  187. op = InstTable.GetOp(address, opCode);
  188. if (op.Props.HasFlag(InstProps.TexB))
  189. {
  190. config.SetUsedFeature(FeatureFlags.Bindless);
  191. }
  192. if (op.Name == InstName.Ald || op.Name == InstName.Ast || op.Name == InstName.Ipa)
  193. {
  194. SetUserAttributeUses(config, op.Name, opCode);
  195. }
  196. else if (op.Name == InstName.Pbk || op.Name == InstName.Pcnt || op.Name == InstName.Ssy)
  197. {
  198. block.AddPushOp(op);
  199. }
  200. else if (op.Name == InstName.Ldl || op.Name == InstName.Stl)
  201. {
  202. config.SetUsedFeature(FeatureFlags.LocalMemory);
  203. }
  204. else if (op.Name == InstName.Atoms ||
  205. op.Name == InstName.AtomsCas ||
  206. op.Name == InstName.Lds ||
  207. op.Name == InstName.Sts)
  208. {
  209. config.SetUsedFeature(FeatureFlags.SharedMemory);
  210. }
  211. block.OpCodes.Add(op);
  212. address += 8;
  213. }
  214. while (!op.Props.HasFlag(InstProps.Bra));
  215. block.EndAddress = address;
  216. }
  217. private static void SetUserAttributeUses(ShaderConfig config, InstName name, ulong opCode)
  218. {
  219. int offset;
  220. int count = 1;
  221. bool isStore = false;
  222. bool indexed = false;
  223. bool perPatch = false;
  224. if (name == InstName.Ast)
  225. {
  226. InstAst opAst = new InstAst(opCode);
  227. count = (int)opAst.AlSize + 1;
  228. offset = opAst.Imm11;
  229. indexed = opAst.Phys;
  230. perPatch = opAst.P;
  231. isStore = true;
  232. }
  233. else if (name == InstName.Ald)
  234. {
  235. InstAld opAld = new InstAld(opCode);
  236. count = (int)opAld.AlSize + 1;
  237. offset = opAld.Imm11;
  238. indexed = opAld.Phys;
  239. perPatch = opAld.P;
  240. isStore = opAld.O;
  241. }
  242. else /* if (name == InstName.Ipa) */
  243. {
  244. InstIpa opIpa = new InstIpa(opCode);
  245. offset = opIpa.Imm10;
  246. indexed = opIpa.Idx;
  247. }
  248. if (indexed)
  249. {
  250. if (isStore)
  251. {
  252. config.SetAllOutputUserAttributes();
  253. config.SetUsedFeature(FeatureFlags.OaIndexing);
  254. }
  255. else
  256. {
  257. config.SetAllInputUserAttributes();
  258. config.SetUsedFeature(FeatureFlags.IaIndexing);
  259. }
  260. }
  261. else
  262. {
  263. for (int elemIndex = 0; elemIndex < count; elemIndex++)
  264. {
  265. int attr = offset + elemIndex * 4;
  266. if (perPatch)
  267. {
  268. if (attr >= AttributeConsts.UserAttributePerPatchBase && attr < AttributeConsts.UserAttributePerPatchEnd)
  269. {
  270. int userAttr = attr - AttributeConsts.UserAttributePerPatchBase;
  271. int index = userAttr / 16;
  272. if (isStore)
  273. {
  274. config.SetOutputUserAttributePerPatch(index);
  275. }
  276. else
  277. {
  278. config.SetInputUserAttributePerPatch(index);
  279. }
  280. }
  281. }
  282. else if (attr >= AttributeConsts.UserAttributeBase && attr < AttributeConsts.UserAttributeEnd)
  283. {
  284. int userAttr = attr - AttributeConsts.UserAttributeBase;
  285. int index = userAttr / 16;
  286. if (isStore)
  287. {
  288. config.SetOutputUserAttribute(index);
  289. }
  290. else
  291. {
  292. config.SetInputUserAttribute(index, (userAttr >> 2) & 3);
  293. }
  294. }
  295. if (!isStore &&
  296. (attr == AttributeConsts.FogCoord ||
  297. (attr >= AttributeConsts.FrontColorDiffuseR && attr < AttributeConsts.ClipDistance0) ||
  298. (attr >= AttributeConsts.TexCoordBase && attr < AttributeConsts.TexCoordEnd)))
  299. {
  300. config.SetUsedFeature(FeatureFlags.FixedFuncAttr);
  301. }
  302. }
  303. }
  304. }
  305. public static bool IsUnconditionalBranch(ref InstOp op)
  306. {
  307. return IsUnconditional(ref op) && op.Props.HasFlag(InstProps.Bra);
  308. }
  309. private static bool IsUnconditional(ref InstOp op)
  310. {
  311. InstConditional condOp = new InstConditional(op.RawOpCode);
  312. if ((op.Name == InstName.Bra || op.Name == InstName.Exit) && condOp.Ccc != Ccc.T)
  313. {
  314. return false;
  315. }
  316. return condOp.Pred == RegisterConsts.PredicateTrueIndex && !condOp.PredInv;
  317. }
  318. private static bool FindBrxTargets(ShaderConfig config, IEnumerable<Block> blocks, Func<ulong, Block> getBlock)
  319. {
  320. bool hasNewTarget = false;
  321. foreach (Block block in blocks)
  322. {
  323. InstOp lastOp = block.GetLastOp();
  324. bool hasNext = block.HasNext();
  325. if (lastOp.Name == InstName.Brx && block.Successors.Count == (hasNext ? 1 : 0))
  326. {
  327. HashSet<ulong> visited = new HashSet<ulong>();
  328. InstBrx opBrx = new InstBrx(lastOp.RawOpCode);
  329. ulong baseOffset = lastOp.GetAbsoluteAddress();
  330. // An indirect branch could go anywhere,
  331. // try to get the possible target offsets from the constant buffer.
  332. (int cbBaseOffset, int cbOffsetsCount) = FindBrxTargetRange(block, opBrx.SrcA);
  333. if (cbOffsetsCount != 0)
  334. {
  335. hasNewTarget = true;
  336. }
  337. for (int i = 0; i < cbOffsetsCount; i++)
  338. {
  339. uint targetOffset = config.ConstantBuffer1Read(cbBaseOffset + i * 4);
  340. ulong targetAddress = baseOffset + targetOffset;
  341. if (visited.Add(targetAddress))
  342. {
  343. Block target = getBlock(targetAddress);
  344. target.Predecessors.Add(block);
  345. block.Successors.Add(target);
  346. }
  347. }
  348. }
  349. }
  350. return hasNewTarget;
  351. }
  352. private static (int, int) FindBrxTargetRange(Block block, int brxReg)
  353. {
  354. // Try to match the following pattern:
  355. //
  356. // IMNMX.U32 Rx, Rx, UpperBound, PT
  357. // SHL Rx, Rx, 0x2
  358. // LDC Rx, c[0x1][Rx+BaseOffset]
  359. //
  360. // Here, Rx is an arbitrary register, "UpperBound" and "BaseOffset" are constants.
  361. // The above pattern is assumed to be generated by the compiler before BRX,
  362. // as the instruction is usually used to implement jump tables for switch statement optimizations.
  363. // On a successful match, "BaseOffset" is the offset in bytes where the jump offsets are
  364. // located on the constant buffer, and "UpperBound" is the total number of offsets for the BRX, minus 1.
  365. HashSet<Block> visited = new HashSet<Block>();
  366. var ldcLocation = FindFirstRegWrite(visited, new BlockLocation(block, block.OpCodes.Count - 1), brxReg);
  367. if (ldcLocation.Block == null || ldcLocation.Block.OpCodes[ldcLocation.Index].Name != InstName.Ldc)
  368. {
  369. return (0, 0);
  370. }
  371. GetOp<InstLdc>(ldcLocation, out var opLdc);
  372. if (opLdc.CbufSlot != 1 || opLdc.AddressMode != 0)
  373. {
  374. return (0, 0);
  375. }
  376. var shlLocation = FindFirstRegWrite(visited, ldcLocation, opLdc.SrcA);
  377. if (shlLocation.Block == null || !shlLocation.IsImmInst(InstName.Shl))
  378. {
  379. return (0, 0);
  380. }
  381. GetOp<InstShlI>(shlLocation, out var opShl);
  382. if (opShl.Imm20 != 2)
  383. {
  384. return (0, 0);
  385. }
  386. var imnmxLocation = FindFirstRegWrite(visited, shlLocation, opShl.SrcA);
  387. if (imnmxLocation.Block == null || !imnmxLocation.IsImmInst(InstName.Imnmx))
  388. {
  389. return (0, 0);
  390. }
  391. GetOp<InstImnmxI>(imnmxLocation, out var opImnmx);
  392. if (opImnmx.Signed || opImnmx.SrcPred != RegisterConsts.PredicateTrueIndex || opImnmx.SrcPredInv)
  393. {
  394. return (0, 0);
  395. }
  396. return (opLdc.CbufOffset, opImnmx.Imm20 + 1);
  397. }
  398. private static void GetOp<T>(BlockLocation location, out T op) where T : unmanaged
  399. {
  400. ulong rawOp = location.Block.OpCodes[location.Index].RawOpCode;
  401. op = Unsafe.As<ulong, T>(ref rawOp);
  402. }
  403. private readonly struct BlockLocation
  404. {
  405. public Block Block { get; }
  406. public int Index { get; }
  407. public BlockLocation(Block block, int index)
  408. {
  409. Block = block;
  410. Index = index;
  411. }
  412. public bool IsImmInst(InstName name)
  413. {
  414. InstOp op = Block.OpCodes[Index];
  415. return op.Name == name && op.Props.HasFlag(InstProps.Ib);
  416. }
  417. }
  418. private static BlockLocation FindFirstRegWrite(HashSet<Block> visited, BlockLocation location, int regIndex)
  419. {
  420. Queue<BlockLocation> toVisit = new Queue<BlockLocation>();
  421. toVisit.Enqueue(location);
  422. visited.Add(location.Block);
  423. while (toVisit.TryDequeue(out var currentLocation))
  424. {
  425. Block block = currentLocation.Block;
  426. for (int i = currentLocation.Index - 1; i >= 0; i--)
  427. {
  428. if (WritesToRegister(block.OpCodes[i], regIndex))
  429. {
  430. return new BlockLocation(block, i);
  431. }
  432. }
  433. foreach (Block predecessor in block.Predecessors)
  434. {
  435. if (visited.Add(predecessor))
  436. {
  437. toVisit.Enqueue(new BlockLocation(predecessor, predecessor.OpCodes.Count));
  438. }
  439. }
  440. }
  441. return new BlockLocation(null, 0);
  442. }
  443. private static bool WritesToRegister(InstOp op, int regIndex)
  444. {
  445. // Predicate instruction only ever writes to predicate, so we shouldn't check those.
  446. if ((op.Props & (InstProps.Rd | InstProps.Rd2)) == 0)
  447. {
  448. return false;
  449. }
  450. if (op.Props.HasFlag(InstProps.Rd2) && (byte)(op.RawOpCode >> 28) == regIndex)
  451. {
  452. return true;
  453. }
  454. return (byte)op.RawOpCode == regIndex;
  455. }
  456. private enum MergeType
  457. {
  458. Brk,
  459. Cont,
  460. Sync
  461. }
  462. private struct PathBlockState
  463. {
  464. public Block Block { get; }
  465. private enum RestoreType
  466. {
  467. None,
  468. PopPushOp,
  469. PushBranchOp
  470. }
  471. private RestoreType _restoreType;
  472. private ulong _restoreValue;
  473. private MergeType _restoreMergeType;
  474. public bool ReturningFromVisit => _restoreType != RestoreType.None;
  475. public PathBlockState(Block block)
  476. {
  477. Block = block;
  478. _restoreType = RestoreType.None;
  479. _restoreValue = 0;
  480. _restoreMergeType = default;
  481. }
  482. public PathBlockState(int oldStackSize)
  483. {
  484. Block = null;
  485. _restoreType = RestoreType.PopPushOp;
  486. _restoreValue = (ulong)oldStackSize;
  487. _restoreMergeType = default;
  488. }
  489. public PathBlockState(ulong syncAddress, MergeType mergeType)
  490. {
  491. Block = null;
  492. _restoreType = RestoreType.PushBranchOp;
  493. _restoreValue = syncAddress;
  494. _restoreMergeType = mergeType;
  495. }
  496. public void RestoreStackState(Stack<(ulong, MergeType)> branchStack)
  497. {
  498. if (_restoreType == RestoreType.PushBranchOp)
  499. {
  500. branchStack.Push((_restoreValue, _restoreMergeType));
  501. }
  502. else if (_restoreType == RestoreType.PopPushOp)
  503. {
  504. while (branchStack.Count > (uint)_restoreValue)
  505. {
  506. branchStack.Pop();
  507. }
  508. }
  509. }
  510. }
  511. private static void PropagatePushOp(Dictionary<ulong, Block> blocks, Block currBlock, int pushOpIndex)
  512. {
  513. PushOpInfo pushOpInfo = currBlock.PushOpCodes[pushOpIndex];
  514. InstOp pushOp = pushOpInfo.Op;
  515. Block target = blocks[pushOp.GetAbsoluteAddress()];
  516. Stack<PathBlockState> workQueue = new Stack<PathBlockState>();
  517. HashSet<Block> visited = new HashSet<Block>();
  518. Stack<(ulong, MergeType)> branchStack = new Stack<(ulong, MergeType)>();
  519. void Push(PathBlockState pbs)
  520. {
  521. // When block is null, this means we are pushing a restore operation.
  522. // Restore operations are used to undo the work done inside a block
  523. // when we return from it, for example it pops addresses pushed by
  524. // SSY/PBK instructions inside the block, and pushes addresses poped
  525. // by SYNC/BRK.
  526. // For blocks, if it's already visited, we just ignore to avoid going
  527. // around in circles and getting stuck here.
  528. if (pbs.Block == null || !visited.Contains(pbs.Block))
  529. {
  530. workQueue.Push(pbs);
  531. }
  532. }
  533. Push(new PathBlockState(currBlock));
  534. while (workQueue.TryPop(out PathBlockState pbs))
  535. {
  536. if (pbs.ReturningFromVisit)
  537. {
  538. pbs.RestoreStackState(branchStack);
  539. continue;
  540. }
  541. Block current = pbs.Block;
  542. // If the block was already processed, we just ignore it, otherwise
  543. // we would push the same child blocks of an already processed block,
  544. // and go around in circles until memory is exhausted.
  545. if (!visited.Add(current))
  546. {
  547. continue;
  548. }
  549. int pushOpsCount = current.PushOpCodes.Count;
  550. if (pushOpsCount != 0)
  551. {
  552. Push(new PathBlockState(branchStack.Count));
  553. for (int index = pushOpIndex; index < pushOpsCount; index++)
  554. {
  555. InstOp currentPushOp = current.PushOpCodes[index].Op;
  556. MergeType pushMergeType = GetMergeTypeFromPush(currentPushOp.Name);
  557. branchStack.Push((currentPushOp.GetAbsoluteAddress(), pushMergeType));
  558. }
  559. }
  560. pushOpIndex = 0;
  561. bool hasNext = current.HasNext();
  562. if (hasNext)
  563. {
  564. Push(new PathBlockState(current.Successors[0]));
  565. }
  566. InstOp lastOp = current.GetLastOp();
  567. if (IsPopBranch(lastOp.Name))
  568. {
  569. MergeType popMergeType = GetMergeTypeFromPop(lastOp.Name);
  570. bool found = true;
  571. ulong targetAddress = 0UL;
  572. MergeType mergeType;
  573. do
  574. {
  575. if (branchStack.Count == 0)
  576. {
  577. found = false;
  578. break;
  579. }
  580. (targetAddress, mergeType) = branchStack.Pop();
  581. // Push the target address (this will be used to push the address
  582. // back into the PBK/PCNT/SSY stack when we return from that block),
  583. Push(new PathBlockState(targetAddress, mergeType));
  584. }
  585. while (mergeType != popMergeType);
  586. // Make sure we found the correct address,
  587. // the push and pop instruction types must match, so:
  588. // - BRK can only consume addresses pushed by PBK.
  589. // - CONT can only consume addresses pushed by PCNT.
  590. // - SYNC can only consume addresses pushed by SSY.
  591. if (found)
  592. {
  593. if (branchStack.Count == 0)
  594. {
  595. // If the entire stack was consumed, then the current pop instruction
  596. // just consumed the address from our push instruction.
  597. if (current.SyncTargets.TryAdd(pushOp.Address, new SyncTarget(pushOpInfo, current.SyncTargets.Count)))
  598. {
  599. pushOpInfo.Consumers.Add(current, Local());
  600. target.Predecessors.Add(current);
  601. current.Successors.Add(target);
  602. }
  603. }
  604. else
  605. {
  606. // Push the block itself into the work queue for processing.
  607. Push(new PathBlockState(blocks[targetAddress]));
  608. }
  609. }
  610. }
  611. else
  612. {
  613. // By adding them in descending order (sorted by address), we process the blocks
  614. // in order (of ascending address), since we work with a LIFO.
  615. foreach (Block possibleTarget in current.Successors.OrderByDescending(x => x.Address))
  616. {
  617. if (!hasNext || possibleTarget != current.Successors[0])
  618. {
  619. Push(new PathBlockState(possibleTarget));
  620. }
  621. }
  622. }
  623. }
  624. }
  625. public static bool IsPopBranch(InstName name)
  626. {
  627. return name == InstName.Brk || name == InstName.Cont || name == InstName.Sync;
  628. }
  629. private static MergeType GetMergeTypeFromPush(InstName name)
  630. {
  631. return name switch
  632. {
  633. InstName.Pbk => MergeType.Brk,
  634. InstName.Pcnt => MergeType.Cont,
  635. _ => MergeType.Sync
  636. };
  637. }
  638. private static MergeType GetMergeTypeFromPop(InstName name)
  639. {
  640. return name switch
  641. {
  642. InstName.Brk => MergeType.Brk,
  643. InstName.Cont => MergeType.Cont,
  644. _ => MergeType.Sync
  645. };
  646. }
  647. }
  648. }