Decoder.cs 32 KB

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