StructuredProgramContext.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using Ryujinx.Graphics.Shader.Translation;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using static Ryujinx.Graphics.Shader.StructuredIr.AstHelper;
  6. namespace Ryujinx.Graphics.Shader.StructuredIr
  7. {
  8. class StructuredProgramContext
  9. {
  10. private HashSet<BasicBlock> _loopTails;
  11. private Stack<(AstBlock Block, int CurrEndIndex, int LoopEndIndex)> _blockStack;
  12. private Dictionary<Operand, AstOperand> _localsMap;
  13. private Dictionary<int, AstAssignment> _gotoTempAsgs;
  14. private List<GotoStatement> _gotos;
  15. private AstBlock _currBlock;
  16. private int _currEndIndex;
  17. private int _loopEndIndex;
  18. public StructuredFunction CurrentFunction { get; private set; }
  19. public StructuredProgramInfo Info { get; }
  20. public ShaderConfig Config { get; }
  21. public StructuredProgramContext(ShaderConfig config)
  22. {
  23. Info = new StructuredProgramInfo();
  24. Config = config;
  25. }
  26. public void EnterFunction(
  27. int blocksCount,
  28. string name,
  29. VariableType returnType,
  30. VariableType[] inArguments,
  31. VariableType[] outArguments)
  32. {
  33. _loopTails = new HashSet<BasicBlock>();
  34. _blockStack = new Stack<(AstBlock, int, int)>();
  35. _localsMap = new Dictionary<Operand, AstOperand>();
  36. _gotoTempAsgs = new Dictionary<int, AstAssignment>();
  37. _gotos = new List<GotoStatement>();
  38. _currBlock = new AstBlock(AstBlockType.Main);
  39. _currEndIndex = blocksCount;
  40. _loopEndIndex = blocksCount;
  41. CurrentFunction = new StructuredFunction(_currBlock, name, returnType, inArguments, outArguments);
  42. }
  43. public void LeaveFunction()
  44. {
  45. Info.Functions.Add(CurrentFunction);
  46. }
  47. public void EnterBlock(BasicBlock block)
  48. {
  49. while (_currEndIndex == block.Index)
  50. {
  51. (_currBlock, _currEndIndex, _loopEndIndex) = _blockStack.Pop();
  52. }
  53. if (_gotoTempAsgs.TryGetValue(block.Index, out AstAssignment gotoTempAsg))
  54. {
  55. AddGotoTempReset(block, gotoTempAsg);
  56. }
  57. LookForDoWhileStatements(block);
  58. }
  59. public void LeaveBlock(BasicBlock block, Operation branchOp)
  60. {
  61. LookForIfStatements(block, branchOp);
  62. }
  63. private void LookForDoWhileStatements(BasicBlock block)
  64. {
  65. // Check if we have any predecessor whose index is greater than the
  66. // current block, this indicates a loop.
  67. bool done = false;
  68. foreach (BasicBlock predecessor in block.Predecessors.OrderByDescending(x => x.Index))
  69. {
  70. // If not a loop, break.
  71. if (predecessor.Index < block.Index)
  72. {
  73. break;
  74. }
  75. // Check if we can create a do-while loop here (only possible if the loop end
  76. // falls inside the current scope), if not add a goto instead.
  77. if (predecessor.Index < _currEndIndex && !done)
  78. {
  79. // Create do-while loop block. We must avoid inserting a goto at the end
  80. // of the loop later, when the tail block is processed. So we add the predecessor
  81. // to a list of loop tails to prevent it from being processed later.
  82. Operation branchOp = (Operation)predecessor.GetLastOp();
  83. NewBlock(AstBlockType.DoWhile, branchOp, predecessor.Index + 1);
  84. _loopTails.Add(predecessor);
  85. done = true;
  86. }
  87. else
  88. {
  89. // Failed to create loop. Since this block is the loop head, we reset the
  90. // goto condition variable here. The variable is always reset on the jump
  91. // target, and this block is the jump target for some loop.
  92. AddGotoTempReset(block, GetGotoTempAsg(block.Index));
  93. break;
  94. }
  95. }
  96. }
  97. private void LookForIfStatements(BasicBlock block, Operation branchOp)
  98. {
  99. if (block.Branch == null)
  100. {
  101. return;
  102. }
  103. // We can only enclose the "if" when the branch lands before
  104. // the end of the current block. If the current enclosing block
  105. // is not a loop, then we can also do so if the branch lands
  106. // right at the end of the current block. When it is a loop,
  107. // this is not valid as the loop condition would be evaluated,
  108. // and it could erroneously jump back to the start of the loop.
  109. bool inRange =
  110. block.Branch.Index < _currEndIndex ||
  111. (block.Branch.Index == _currEndIndex && block.Branch.Index < _loopEndIndex);
  112. bool isLoop = block.Branch.Index <= block.Index;
  113. if (inRange && !isLoop)
  114. {
  115. NewBlock(AstBlockType.If, branchOp, block.Branch.Index);
  116. }
  117. else if (!_loopTails.Contains(block))
  118. {
  119. AstAssignment gotoTempAsg = GetGotoTempAsg(block.Branch.Index);
  120. // We use DoWhile type here, as the condition should be true for
  121. // unconditional branches, or it should jump if the condition is true otherwise.
  122. IAstNode cond = GetBranchCond(AstBlockType.DoWhile, branchOp);
  123. AddNode(Assign(gotoTempAsg.Destination, cond));
  124. AstOperation branch = new AstOperation(branchOp.Inst);
  125. AddNode(branch);
  126. GotoStatement gotoStmt = new GotoStatement(branch, gotoTempAsg, isLoop);
  127. _gotos.Add(gotoStmt);
  128. }
  129. }
  130. private AstAssignment GetGotoTempAsg(int index)
  131. {
  132. if (_gotoTempAsgs.TryGetValue(index, out AstAssignment gotoTempAsg))
  133. {
  134. return gotoTempAsg;
  135. }
  136. AstOperand gotoTemp = NewTemp(VariableType.Bool);
  137. gotoTempAsg = Assign(gotoTemp, Const(IrConsts.False));
  138. _gotoTempAsgs.Add(index, gotoTempAsg);
  139. return gotoTempAsg;
  140. }
  141. private void AddGotoTempReset(BasicBlock block, AstAssignment gotoTempAsg)
  142. {
  143. // If it was already added, we don't need to add it again.
  144. if (gotoTempAsg.Parent != null)
  145. {
  146. return;
  147. }
  148. AddNode(gotoTempAsg);
  149. // For block 0, we don't need to add the extra "reset" at the beginning,
  150. // because it is already the first node to be executed on the shader,
  151. // so it is reset to false by the "local" assignment anyway.
  152. if (block.Index != 0)
  153. {
  154. CurrentFunction.MainBlock.AddFirst(Assign(gotoTempAsg.Destination, Const(IrConsts.False)));
  155. }
  156. }
  157. private void NewBlock(AstBlockType type, Operation branchOp, int endIndex)
  158. {
  159. NewBlock(type, GetBranchCond(type, branchOp), endIndex);
  160. }
  161. private void NewBlock(AstBlockType type, IAstNode cond, int endIndex)
  162. {
  163. AstBlock childBlock = new AstBlock(type, cond);
  164. AddNode(childBlock);
  165. _blockStack.Push((_currBlock, _currEndIndex, _loopEndIndex));
  166. _currBlock = childBlock;
  167. _currEndIndex = endIndex;
  168. if (type == AstBlockType.DoWhile)
  169. {
  170. _loopEndIndex = endIndex;
  171. }
  172. }
  173. private IAstNode GetBranchCond(AstBlockType type, Operation branchOp)
  174. {
  175. IAstNode cond;
  176. if (branchOp.Inst == Instruction.Branch)
  177. {
  178. // If the branch is not conditional, the condition is a constant.
  179. // For if it's false (always jump over, if block never executed).
  180. // For loops it's always true (always loop).
  181. cond = Const(type == AstBlockType.If ? IrConsts.False : IrConsts.True);
  182. }
  183. else
  184. {
  185. cond = GetOperandUse(branchOp.GetSource(0));
  186. Instruction invInst = type == AstBlockType.If
  187. ? Instruction.BranchIfTrue
  188. : Instruction.BranchIfFalse;
  189. if (branchOp.Inst == invInst)
  190. {
  191. cond = new AstOperation(Instruction.LogicalNot, cond);
  192. }
  193. }
  194. return cond;
  195. }
  196. public void AddNode(IAstNode node)
  197. {
  198. _currBlock.Add(node);
  199. }
  200. public GotoStatement[] GetGotos()
  201. {
  202. return _gotos.ToArray();
  203. }
  204. private AstOperand NewTemp(VariableType type)
  205. {
  206. AstOperand newTemp = Local(type);
  207. CurrentFunction.Locals.Add(newTemp);
  208. return newTemp;
  209. }
  210. public AstOperand GetOperandDef(Operand operand)
  211. {
  212. return GetOperand(operand);
  213. }
  214. public AstOperand GetOperandUse(Operand operand)
  215. {
  216. // If this flag is set, we're reading from an output attribute instead.
  217. if (operand.Type.IsAttribute() && (operand.Value & AttributeConsts.LoadOutputMask) != 0)
  218. {
  219. return GetOperandDef(operand);
  220. }
  221. return GetOperand(operand);
  222. }
  223. private AstOperand GetOperand(Operand operand)
  224. {
  225. if (operand == null)
  226. {
  227. return null;
  228. }
  229. if (operand.Type != OperandType.LocalVariable)
  230. {
  231. return new AstOperand(operand);
  232. }
  233. if (!_localsMap.TryGetValue(operand, out AstOperand astOperand))
  234. {
  235. astOperand = new AstOperand(operand);
  236. _localsMap.Add(operand, astOperand);
  237. CurrentFunction.Locals.Add(astOperand);
  238. }
  239. return astOperand;
  240. }
  241. }
  242. }