subst.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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/base/kind_switch.h"
  6. #include "toolchain/check/eval.h"
  7. #include "toolchain/check/generic.h"
  8. #include "toolchain/check/inst.h"
  9. #include "toolchain/sem_ir/copy_on_write_block.h"
  10. #include "toolchain/sem_ir/ids.h"
  11. #include "toolchain/sem_ir/inst.h"
  12. namespace Carbon::Check {
  13. namespace {
  14. // Information about an instruction that we are substituting into.
  15. struct WorklistItem {
  16. // The instruction that we are substituting into.
  17. SemIR::InstId inst_id;
  18. // Whether the operands of this instruction have been added to the worklist.
  19. bool is_expanded : 1;
  20. // The index of the worklist item to process after we finish updating this
  21. // one. For the final child of an instruction, this is the parent. For any
  22. // other child, this is the index of the next child of the parent. For the
  23. // root, this is -1.
  24. int next_index : 31;
  25. };
  26. // A list of instructions that we're currently in the process of substituting
  27. // into. For details of the algorithm used here, see `SubstConstant`.
  28. class Worklist {
  29. public:
  30. explicit Worklist(SemIR::InstId root_id) {
  31. worklist_.push_back(
  32. {.inst_id = root_id, .is_expanded = false, .next_index = -1});
  33. }
  34. auto operator[](int index) -> WorklistItem& { return worklist_[index]; }
  35. auto size() -> int { return worklist_.size(); }
  36. auto back() -> WorklistItem& { return worklist_.back(); }
  37. auto Push(SemIR::InstId inst_id) -> void {
  38. CARBON_CHECK(inst_id.has_value());
  39. worklist_.push_back({.inst_id = inst_id,
  40. .is_expanded = false,
  41. .next_index = static_cast<int>(worklist_.size() + 1)});
  42. CARBON_CHECK(worklist_.back().next_index > 0, "Constant too large.");
  43. }
  44. auto Pop() -> SemIR::InstId { return worklist_.pop_back_val().inst_id; }
  45. private:
  46. // Constants can get pretty large, so use a large worklist. This should be
  47. // about 4KiB, which should be small enough to comfortably fit on the stack,
  48. // but large enough that it's unlikely that we'll need a heap allocation.
  49. llvm::SmallVector<WorklistItem, 512> worklist_;
  50. };
  51. } // namespace
  52. // Pushes the specified operand onto the worklist.
  53. static auto PushOperand(Context& context, Worklist& worklist,
  54. SemIR::Inst::ArgAndKind arg) -> void {
  55. auto push_block = [&](SemIR::InstBlockId block_id) {
  56. for (auto inst_id :
  57. context.inst_blocks().Get(SemIR::InstBlockId(block_id))) {
  58. worklist.Push(inst_id);
  59. }
  60. };
  61. auto push_specific = [&](SemIR::SpecificId specific_id) {
  62. if (specific_id.has_value()) {
  63. push_block(context.specifics().Get(specific_id).args_id);
  64. }
  65. };
  66. CARBON_KIND_SWITCH(arg) {
  67. case CARBON_KIND(SemIR::InstId inst_id): {
  68. if (inst_id.has_value()) {
  69. worklist.Push(inst_id);
  70. }
  71. break;
  72. }
  73. case CARBON_KIND(SemIR::MetaInstId inst_id): {
  74. if (inst_id.has_value()) {
  75. worklist.Push(inst_id);
  76. }
  77. break;
  78. }
  79. case CARBON_KIND(SemIR::TypeInstId inst_id): {
  80. if (inst_id.has_value()) {
  81. worklist.Push(inst_id);
  82. }
  83. break;
  84. }
  85. case CARBON_KIND(SemIR::InstBlockId inst_block_id): {
  86. push_block(inst_block_id);
  87. break;
  88. }
  89. case CARBON_KIND(SemIR::StructTypeFieldsId fields_id): {
  90. for (auto field : context.struct_type_fields().Get(fields_id)) {
  91. worklist.Push(field.type_inst_id);
  92. }
  93. break;
  94. }
  95. case CARBON_KIND(SemIR::SpecificId specific_id): {
  96. push_specific(specific_id);
  97. break;
  98. }
  99. case CARBON_KIND(SemIR::SpecificInterfaceId interface_id): {
  100. auto interface = context.specific_interfaces().Get(interface_id);
  101. push_specific(interface.specific_id);
  102. break;
  103. }
  104. case CARBON_KIND(SemIR::FacetTypeId facet_type_id): {
  105. const auto& facet_type_info = context.facet_types().Get(facet_type_id);
  106. for (auto interface : facet_type_info.extend_constraints) {
  107. push_specific(interface.specific_id);
  108. }
  109. for (auto interface : facet_type_info.self_impls_constraints) {
  110. push_specific(interface.specific_id);
  111. }
  112. for (auto rewrite : facet_type_info.rewrite_constraints) {
  113. worklist.Push(rewrite.lhs_id);
  114. worklist.Push(rewrite.rhs_id);
  115. }
  116. // TODO: Process other requirements as well.
  117. break;
  118. }
  119. default:
  120. break;
  121. }
  122. }
  123. // Converts the operands of this instruction into `InstId`s and pushes them onto
  124. // the worklist.
  125. static auto ExpandOperands(Context& context, Worklist& worklist,
  126. SemIR::InstId inst_id) -> void {
  127. auto inst = context.insts().Get(inst_id);
  128. if (inst.type_id().has_value()) {
  129. worklist.Push(context.types().GetInstId(inst.type_id()));
  130. }
  131. PushOperand(context, worklist, inst.arg0_and_kind());
  132. PushOperand(context, worklist, inst.arg1_and_kind());
  133. }
  134. // Pops the specified operand from the worklist and returns it.
  135. static auto PopOperand(Context& context, Worklist& worklist,
  136. SemIR::Inst::ArgAndKind arg) -> int32_t {
  137. auto pop_block_id = [&](SemIR::InstBlockId old_inst_block_id) {
  138. auto size = context.inst_blocks().Get(old_inst_block_id).size();
  139. SemIR::CopyOnWriteInstBlock new_inst_block(&context.sem_ir(),
  140. old_inst_block_id);
  141. for (auto i : llvm::reverse(llvm::seq(size))) {
  142. new_inst_block.Set(i, worklist.Pop());
  143. }
  144. return new_inst_block.GetCanonical();
  145. };
  146. auto pop_specific = [&](SemIR::SpecificId specific_id) {
  147. if (!specific_id.has_value()) {
  148. return specific_id;
  149. }
  150. auto& specific = context.specifics().Get(specific_id);
  151. auto args_id = pop_block_id(specific.args_id);
  152. return context.specifics().GetOrAdd(specific.generic_id, args_id);
  153. };
  154. CARBON_KIND_SWITCH(arg) {
  155. case CARBON_KIND(SemIR::InstId inst_id): {
  156. if (!inst_id.has_value()) {
  157. return arg.value();
  158. }
  159. return worklist.Pop().index;
  160. }
  161. case CARBON_KIND(SemIR::MetaInstId inst_id): {
  162. if (!inst_id.has_value()) {
  163. return arg.value();
  164. }
  165. return worklist.Pop().index;
  166. }
  167. case CARBON_KIND(SemIR::TypeInstId inst_id): {
  168. if (!inst_id.has_value()) {
  169. return arg.value();
  170. }
  171. return worklist.Pop().index;
  172. }
  173. case CARBON_KIND(SemIR::InstBlockId inst_block_id): {
  174. return pop_block_id(inst_block_id).index;
  175. }
  176. case CARBON_KIND(SemIR::StructTypeFieldsId old_fields_id): {
  177. auto old_fields = context.struct_type_fields().Get(old_fields_id);
  178. SemIR::CopyOnWriteStructTypeFieldsBlock new_fields(&context.sem_ir(),
  179. old_fields_id);
  180. for (auto i : llvm::reverse(llvm::seq(old_fields.size()))) {
  181. new_fields.Set(
  182. i,
  183. {.name_id = old_fields[i].name_id,
  184. .type_inst_id = context.types().GetAsTypeInstId(worklist.Pop())});
  185. }
  186. return new_fields.GetCanonical().index;
  187. }
  188. case CARBON_KIND(SemIR::SpecificId specific_id): {
  189. return pop_specific(specific_id).index;
  190. }
  191. case CARBON_KIND(SemIR::SpecificInterfaceId interface_id): {
  192. auto interface = context.specific_interfaces().Get(interface_id);
  193. auto specific_id = pop_specific(interface.specific_id);
  194. return context.specific_interfaces()
  195. .Add({
  196. .interface_id = interface.interface_id,
  197. .specific_id = specific_id,
  198. })
  199. .index;
  200. }
  201. case CARBON_KIND(SemIR::FacetTypeId facet_type_id): {
  202. const auto& old_facet_type_info =
  203. context.facet_types().Get(facet_type_id);
  204. SemIR::FacetTypeInfo new_facet_type_info;
  205. // Since these were added to a stack, we get them back in reverse order.
  206. new_facet_type_info.rewrite_constraints.resize(
  207. old_facet_type_info.rewrite_constraints.size(),
  208. SemIR::FacetTypeInfo::RewriteConstraint::None);
  209. for (auto& new_constraint :
  210. llvm::reverse(new_facet_type_info.rewrite_constraints)) {
  211. auto rhs_id = worklist.Pop();
  212. auto lhs_id = worklist.Pop();
  213. new_constraint = {.lhs_id = lhs_id, .rhs_id = rhs_id};
  214. }
  215. new_facet_type_info.self_impls_constraints.resize(
  216. old_facet_type_info.self_impls_constraints.size(),
  217. SemIR::SpecificInterface::None);
  218. for (auto [old_constraint, new_constraint] : llvm::reverse(
  219. llvm::zip(old_facet_type_info.self_impls_constraints,
  220. new_facet_type_info.self_impls_constraints))) {
  221. new_constraint = {
  222. .interface_id = old_constraint.interface_id,
  223. .specific_id = pop_specific(old_constraint.specific_id)};
  224. }
  225. new_facet_type_info.extend_constraints.resize(
  226. old_facet_type_info.extend_constraints.size(),
  227. SemIR::SpecificInterface::None);
  228. for (auto [old_constraint, new_constraint] :
  229. llvm::reverse(llvm::zip(old_facet_type_info.extend_constraints,
  230. new_facet_type_info.extend_constraints))) {
  231. new_constraint = {
  232. .interface_id = old_constraint.interface_id,
  233. .specific_id = pop_specific(old_constraint.specific_id)};
  234. }
  235. new_facet_type_info.other_requirements =
  236. old_facet_type_info.other_requirements;
  237. new_facet_type_info.Canonicalize();
  238. return context.facet_types().Add(new_facet_type_info).index;
  239. }
  240. default:
  241. return arg.value();
  242. }
  243. }
  244. // Pops the operands of the specified instruction off the worklist and rebuilds
  245. // the instruction with the updated operands if it has changed.
  246. static auto Rebuild(Context& context, Worklist& worklist, SemIR::InstId inst_id,
  247. const SubstInstCallbacks& callbacks) -> SemIR::InstId {
  248. auto inst = context.insts().Get(inst_id);
  249. // Note that we pop in reverse order because we pushed them in forwards order.
  250. int32_t arg1 = PopOperand(context, worklist, inst.arg1_and_kind());
  251. int32_t arg0 = PopOperand(context, worklist, inst.arg0_and_kind());
  252. auto type_id = inst.type_id().has_value()
  253. ? context.types().GetTypeIdForTypeInstId(worklist.Pop())
  254. : SemIR::TypeId::None;
  255. if (type_id == inst.type_id() && arg0 == inst.arg0() && arg1 == inst.arg1()) {
  256. return callbacks.ReuseUnchanged(inst_id);
  257. }
  258. // TODO: Do we need to require this type to be complete?
  259. inst.SetType(type_id);
  260. inst.SetArgs(arg0, arg1);
  261. return callbacks.Rebuild(inst_id, inst);
  262. }
  263. auto SubstInst(Context& context, SemIR::InstId inst_id,
  264. const SubstInstCallbacks& callbacks) -> SemIR::InstId {
  265. Worklist worklist(inst_id);
  266. // For each instruction that forms part of the constant, we will visit it
  267. // twice:
  268. //
  269. // - First, we visit it with `is_expanded == false`, we add all of its
  270. // operands onto the worklist, and process them by following this same
  271. // process.
  272. // - Then, once all operands are processed, we visit the instruction with
  273. // `is_expanded == true`, pop the operands back off the worklist, and if any
  274. // of them changed, rebuild this instruction.
  275. //
  276. // The second step is skipped if we can detect in the first step that the
  277. // instruction will not need to be rebuilt.
  278. int index = 0;
  279. while (index != -1) {
  280. auto& item = worklist[index];
  281. if (item.is_expanded) {
  282. // Rebuild this item if necessary. Note that this might pop items from the
  283. // worklist but does not reallocate, so does not invalidate `item`.
  284. item.inst_id = Rebuild(context, worklist, item.inst_id, callbacks);
  285. index = item.next_index;
  286. continue;
  287. }
  288. if (callbacks.Subst(item.inst_id)) {
  289. index = item.next_index;
  290. continue;
  291. }
  292. // Extract the operands of this item into the worklist. Note that this
  293. // modifies the worklist, so it's not safe to use `item` after
  294. // `ExpandOperands` returns.
  295. item.is_expanded = true;
  296. int first_operand = worklist.size();
  297. int next_index = item.next_index;
  298. ExpandOperands(context, worklist, item.inst_id);
  299. // If there are any operands, go and update them before rebuilding this
  300. // item.
  301. if (worklist.size() > first_operand) {
  302. worklist.back().next_index = index;
  303. index = first_operand;
  304. } else {
  305. // No need to rebuild this instruction: its operands can't be changed by
  306. // substitution because it has none.
  307. index = next_index;
  308. }
  309. }
  310. CARBON_CHECK(worklist.size() == 1,
  311. "Unexpected data left behind in work list");
  312. return worklist.back().inst_id;
  313. }
  314. auto SubstInst(Context& context, SemIR::TypeInstId inst_id,
  315. const SubstInstCallbacks& callbacks) -> SemIR::TypeInstId {
  316. return context.types().GetAsTypeInstId(
  317. SubstInst(context, static_cast<SemIR::InstId>(inst_id), callbacks));
  318. }
  319. namespace {
  320. // Callbacks for performing substitution of a set of Substitutions into a
  321. // symbolic constant.
  322. class SubstConstantCallbacks final : public SubstInstCallbacks {
  323. public:
  324. // `context` must not be null.
  325. SubstConstantCallbacks(Context* context, Substitutions substitutions)
  326. : context_(context), substitutions_(substitutions) {}
  327. // Applies the given Substitutions to an instruction, in order to replace
  328. // BindSymbolicName instructions with the value of the binding.
  329. auto Subst(SemIR::InstId& inst_id) const -> bool override {
  330. if (context_->constant_values().Get(inst_id).is_concrete()) {
  331. // This instruction is a concrete constant, so can't contain any
  332. // bindings that need to be substituted.
  333. return true;
  334. }
  335. auto entity_name_id = SemIR::EntityNameId::None;
  336. if (auto bind =
  337. context_->insts().TryGetAs<SemIR::BindSymbolicName>(inst_id)) {
  338. entity_name_id = bind->entity_name_id;
  339. } else if (auto bind =
  340. context_->insts().TryGetAs<SemIR::SymbolicBindingPattern>(
  341. inst_id)) {
  342. entity_name_id = bind->entity_name_id;
  343. } else {
  344. return false;
  345. }
  346. // This is a symbolic binding. Check if we're substituting it.
  347. // TODO: Consider building a hash map for substitutions. We might have a
  348. // lot of them.
  349. for (auto [bind_index, replacement_id] : substitutions_) {
  350. if (context_->entity_names().Get(entity_name_id).bind_index() ==
  351. bind_index) {
  352. // This is the binding we're replacing. Perform substitution.
  353. inst_id = context_->constant_values().GetInstId(replacement_id);
  354. return true;
  355. }
  356. }
  357. // If it's not being substituted, don't look through it. Its constant
  358. // value doesn't depend on its operand.
  359. return true;
  360. }
  361. // Rebuilds an instruction by building a new constant.
  362. auto Rebuild(SemIR::InstId old_inst_id, SemIR::Inst new_inst) const
  363. -> SemIR::InstId override {
  364. auto const_id = EvalOrAddInst(
  365. *context_,
  366. SemIR::LocIdAndInst::UncheckedLoc(
  367. context_->insts().GetLocId(old_inst_id).ToImplicit(), new_inst));
  368. CARBON_CHECK(const_id.has_value(),
  369. "Substitution into constant produced non-constant");
  370. return context_->constant_values().GetInstId(const_id);
  371. }
  372. private:
  373. Context* context_;
  374. Substitutions substitutions_;
  375. };
  376. } // namespace
  377. auto SubstConstant(Context& context, SemIR::ConstantId const_id,
  378. Substitutions substitutions) -> SemIR::ConstantId {
  379. CARBON_CHECK(const_id.is_constant(), "Substituting into non-constant");
  380. if (substitutions.empty()) {
  381. // Nothing to substitute.
  382. return const_id;
  383. }
  384. if (!const_id.is_symbolic()) {
  385. // A concrete constant can't contain a reference to a symbolic binding.
  386. return const_id;
  387. }
  388. auto subst_inst_id =
  389. SubstInst(context, context.constant_values().GetInstId(const_id),
  390. SubstConstantCallbacks(&context, substitutions));
  391. return context.constant_values().Get(subst_inst_id);
  392. }
  393. } // namespace Carbon::Check