subst.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/check/subst.h"
  5. #include "toolchain/check/eval.h"
  6. #include "toolchain/sem_ir/copy_on_write_block.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. namespace Carbon::Check {
  9. namespace {
  10. // Information about an instruction that we are substituting into.
  11. struct WorklistItem {
  12. // The instruction that we are substituting into.
  13. SemIR::InstId inst_id;
  14. // Whether the operands of this instruction have been added to the worklist.
  15. bool is_expanded : 1;
  16. // The index of the worklist item to process after we finish updating this
  17. // one. For the final child of an instruction, this is the parent. For any
  18. // other child, this is the index of the next child of the parent. For the
  19. // root, this is -1.
  20. int next_index : 31;
  21. };
  22. // A list of instructions that we're currently in the process of substituting
  23. // into. For details of the algorithm used here, see `SubstConstant`.
  24. class Worklist {
  25. public:
  26. explicit Worklist(SemIR::InstId root_id) {
  27. worklist_.push_back(
  28. {.inst_id = root_id, .is_expanded = false, .next_index = -1});
  29. }
  30. auto operator[](int index) -> WorklistItem& { return worklist_[index]; }
  31. auto size() -> int { return worklist_.size(); }
  32. auto back() -> WorklistItem& { return worklist_.back(); }
  33. auto Push(SemIR::InstId inst_id) -> void {
  34. worklist_.push_back({.inst_id = inst_id,
  35. .is_expanded = false,
  36. .next_index = static_cast<int>(worklist_.size() + 1)});
  37. CARBON_CHECK(worklist_.back().next_index > 0) << "Constant too large.";
  38. }
  39. auto Pop() -> SemIR::InstId { return worklist_.pop_back_val().inst_id; }
  40. private:
  41. // Constants can get pretty large, so use a large worklist. This should be
  42. // about 4KiB, which should be small enough to comfortably fit on the stack,
  43. // but large enough that it's unlikely that we'll need a heap allocation.
  44. llvm::SmallVector<WorklistItem, 512> worklist_;
  45. };
  46. } // namespace
  47. // Pushes the specified operand onto the worklist.
  48. static auto PushOperand(Context& context, Worklist& worklist,
  49. SemIR::IdKind kind, int32_t arg) -> void {
  50. switch (kind) {
  51. case SemIR::IdKind::For<SemIR::InstId>:
  52. worklist.Push(static_cast<SemIR::InstId>(arg));
  53. break;
  54. case SemIR::IdKind::For<SemIR::TypeId>:
  55. worklist.Push(context.types().GetInstId(static_cast<SemIR::TypeId>(arg)));
  56. break;
  57. case SemIR::IdKind::For<SemIR::InstBlockId>:
  58. for (auto inst_id :
  59. context.inst_blocks().Get(static_cast<SemIR::InstBlockId>(arg))) {
  60. worklist.Push(inst_id);
  61. }
  62. break;
  63. case SemIR::IdKind::For<SemIR::TypeBlockId>:
  64. for (auto type_id :
  65. context.type_blocks().Get(static_cast<SemIR::TypeBlockId>(arg))) {
  66. worklist.Push(context.types().GetInstId(type_id));
  67. }
  68. break;
  69. default:
  70. break;
  71. }
  72. }
  73. // Converts the operands of this instruction into `InstId`s and pushes them onto
  74. // the worklist.
  75. static auto ExpandOperands(Context& context, Worklist& worklist,
  76. SemIR::InstId inst_id) -> void {
  77. auto inst = context.insts().Get(inst_id);
  78. auto kinds = inst.ArgKinds();
  79. PushOperand(context, worklist, kinds.first, inst.arg0());
  80. PushOperand(context, worklist, kinds.second, inst.arg1());
  81. }
  82. // Pops the specified operand from the worklist and returns it.
  83. static auto PopOperand(Context& context, Worklist& worklist, SemIR::IdKind kind,
  84. int32_t arg) -> int32_t {
  85. switch (kind) {
  86. case SemIR::IdKind::For<SemIR::InstId>:
  87. return worklist.Pop().index;
  88. case SemIR::IdKind::For<SemIR::TypeId>:
  89. return context.GetTypeIdForTypeInst(worklist.Pop()).index;
  90. case SemIR::IdKind::For<SemIR::InstBlockId>: {
  91. auto old_inst_block_id = static_cast<SemIR::InstBlockId>(arg);
  92. auto size = context.inst_blocks().Get(old_inst_block_id).size();
  93. SemIR::CopyOnWriteInstBlock new_inst_block(context.sem_ir(),
  94. old_inst_block_id);
  95. for (auto i : llvm::reverse(llvm::seq(size))) {
  96. new_inst_block.Set(i, worklist.Pop());
  97. }
  98. return new_inst_block.id().index;
  99. }
  100. case SemIR::IdKind::For<SemIR::TypeBlockId>: {
  101. auto old_type_block_id = static_cast<SemIR::TypeBlockId>(arg);
  102. auto size = context.type_blocks().Get(old_type_block_id).size();
  103. SemIR::CopyOnWriteTypeBlock new_type_block(context.sem_ir(),
  104. old_type_block_id);
  105. for (auto i : llvm::index_range(0, size)) {
  106. new_type_block.Set(size - i - 1,
  107. context.GetTypeIdForTypeInst(worklist.Pop()));
  108. }
  109. return new_type_block.id().index;
  110. }
  111. default:
  112. return arg;
  113. }
  114. }
  115. // Pops the operands of the specified instruction off the worklist and rebuilds
  116. // the instruction with the updated operands.
  117. static auto Rebuild(Context& context, Worklist& worklist, SemIR::InstId inst_id)
  118. -> SemIR::InstId {
  119. auto inst = context.insts().Get(inst_id);
  120. auto kinds = inst.ArgKinds();
  121. // Note that we pop in reverse order because we pushed them in forwards order.
  122. int32_t arg1 = PopOperand(context, worklist, kinds.second, inst.arg1());
  123. int32_t arg0 = PopOperand(context, worklist, kinds.first, inst.arg0());
  124. if (arg0 == inst.arg0() && arg1 == inst.arg1()) {
  125. return inst_id;
  126. }
  127. // TODO: Updating the arguments might result in the instruction having a
  128. // different type. We should consider either recomputing the type or
  129. // substituting into it. In the latter case, consider caching, as we may
  130. // substitute into related types repeatedly.
  131. inst.SetArgs(arg0, arg1);
  132. auto result_id = TryEvalInst(context, SemIR::InstId::Invalid, inst);
  133. CARBON_CHECK(result_id.is_constant())
  134. << "Substitution into constant produced non-constant";
  135. return result_id.inst_id();
  136. }
  137. auto SubstConstant(Context& context, SemIR::ConstantId const_id,
  138. Substitutions substitutions) -> SemIR::ConstantId {
  139. CARBON_CHECK(const_id.is_constant()) << "Substituting into non-constant";
  140. if (substitutions.empty()) {
  141. // Nothing to substitute.
  142. return const_id;
  143. }
  144. if (!const_id.is_symbolic()) {
  145. // A template constant can't contain a reference to a symbolic binding.
  146. return const_id;
  147. }
  148. Worklist worklist(const_id.inst_id());
  149. // For each instruction that forms part of the constant, we will visit it
  150. // twice:
  151. //
  152. // - First, we visit it with `is_expanded == false`, we add all of its
  153. // operands onto the worklist, and process them by following this same
  154. // process.
  155. // - Then, once all operands are processed, we visit the instruction with
  156. // `is_expanded == true`, pop the operands back off the worklist, and if any
  157. // of them changed, rebuild this instruction.
  158. //
  159. // The second step is skipped if we can detect in the first step that the
  160. // instruction will not need to be rebuilt.
  161. int index = 0;
  162. while (index != -1) {
  163. auto& item = worklist[index];
  164. if (item.is_expanded) {
  165. // Rebuild this item if necessary. Note that this might pop items from the
  166. // worklist but does not reallocate, so does not invalidate `item`.
  167. item.inst_id = Rebuild(context, worklist, item.inst_id);
  168. index = item.next_index;
  169. continue;
  170. }
  171. if (context.constant_values().Get(item.inst_id).is_template()) {
  172. // This instruction is a template constant, so can't contain any
  173. // bindings that need to be substituted.
  174. index = item.next_index;
  175. continue;
  176. }
  177. if (context.insts().Is<SemIR::BindSymbolicName>(item.inst_id)) {
  178. // This is a symbolic binding. Check if we're substituting it.
  179. // TODO: Consider building a hash map for substitutions. We might have a
  180. // lot of them.
  181. for (auto [bind_id, replacement_id] : substitutions) {
  182. if (item.inst_id == bind_id) {
  183. // This is the binding we're replacing. Perform substitution.
  184. item.inst_id = replacement_id.inst_id();
  185. break;
  186. }
  187. }
  188. // If it's not being substituted, don't look through it. Its constant
  189. // value doesn't depend on its operand.
  190. index = item.next_index;
  191. continue;
  192. }
  193. // Extract the operands of this item into the worklist. Note that this
  194. // modifies the worklist, so it's not safe to use `item` after
  195. // `ExpandOperands` returns.
  196. item.is_expanded = true;
  197. int first_operand = worklist.size();
  198. int next_index = item.next_index;
  199. ExpandOperands(context, worklist, item.inst_id);
  200. // If there are any operands, go and update them before rebuilding this
  201. // item.
  202. if (worklist.size() > first_operand) {
  203. worklist.back().next_index = index;
  204. index = first_operand;
  205. } else {
  206. // No need to rebuild this instruction.
  207. index = next_index;
  208. }
  209. }
  210. CARBON_CHECK(worklist.size() == 1)
  211. << "Unexpected data left behind in work list";
  212. return context.constant_values().Get(worklist.back().inst_id);
  213. }
  214. auto SubstType(Context& context, SemIR::TypeId type_id,
  215. Substitutions substitutions) -> SemIR::TypeId {
  216. return context.GetTypeIdForTypeConstant(SubstConstant(
  217. context, context.types().GetConstantId(type_id), substitutions));
  218. }
  219. } // namespace Carbon::Check