inst.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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/inst.h"
  5. #include "common/vlog.h"
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/check/eval.h"
  8. #include "toolchain/check/generic.h"
  9. #include "toolchain/sem_ir/constant.h"
  10. #include "toolchain/sem_ir/ids.h"
  11. #include "toolchain/sem_ir/inst_kind.h"
  12. namespace Carbon::Check {
  13. // Finish producing an instruction. Set its constant value, and register it in
  14. // any applicable instruction lists.
  15. static auto FinishInst(Context& context, SemIR::InstId inst_id,
  16. SemIR::Inst inst) -> void {
  17. DependentInstKind dep_kind = DependentInstKind::None;
  18. // If the instruction has a symbolic constant type, track that we need to
  19. // substitute into it.
  20. if (context.constant_values().DependsOnGenericParameter(
  21. context.types().GetConstantId(inst.type_id()))) {
  22. dep_kind.Add(DependentInstKind::SymbolicType);
  23. }
  24. // If the instruction has a constant value, compute it.
  25. auto const_id = TryEvalInstUnsafe(context, inst_id, inst);
  26. context.constant_values().Set(inst_id, const_id);
  27. if (const_id.is_constant()) {
  28. CARBON_VLOG_TO(context.vlog_stream(), "Constant: {0} -> {1}\n", inst,
  29. context.constant_values().GetInstId(const_id));
  30. // If the constant value is symbolic, track that we need to substitute into
  31. // it.
  32. if (context.constant_values().DependsOnGenericParameter(const_id)) {
  33. dep_kind.Add(DependentInstKind::SymbolicConstant);
  34. }
  35. }
  36. // Template-dependent instructions are handled separately by
  37. // `AddDependentActionInst`.
  38. CARBON_CHECK(
  39. inst.kind().constant_kind() != SemIR::InstConstantKind::InstAction,
  40. "Use AddDependentActionInst to add an action instruction");
  41. // Keep track of dependent instructions.
  42. if (!dep_kind.empty()) {
  43. AttachDependentInstToCurrentGeneric(context,
  44. {.inst_id = inst_id, .kind = dep_kind});
  45. }
  46. }
  47. auto AddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  48. -> SemIR::InstId {
  49. auto inst_id = AddInstInNoBlock(context, loc_id_and_inst);
  50. context.inst_block_stack().AddInstId(inst_id);
  51. return inst_id;
  52. }
  53. auto AddInstInNoBlock(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  54. -> SemIR::InstId {
  55. auto inst_id = context.sem_ir().insts().AddInNoBlock(loc_id_and_inst);
  56. CARBON_VLOG_TO(context.vlog_stream(), "AddInst: {0}\n", loc_id_and_inst.inst);
  57. FinishInst(context, inst_id, loc_id_and_inst.inst);
  58. return inst_id;
  59. }
  60. auto AddDependentActionInst(Context& context,
  61. SemIR::LocIdAndInst loc_id_and_inst)
  62. -> SemIR::InstId {
  63. auto inst_id = context.sem_ir().insts().AddInNoBlock(loc_id_and_inst);
  64. CARBON_VLOG_TO(context.vlog_stream(), "AddDependentActionInst: {0}\n",
  65. loc_id_and_inst.inst);
  66. // Set the constant value of this instruction to point back to itself.
  67. auto const_id = context.constant_values().AddSymbolicConstant(
  68. {.inst_id = inst_id,
  69. .generic_id = SemIR::GenericId::None,
  70. .index = SemIR::GenericInstIndex::None,
  71. .dependence = SemIR::ConstantDependence::Template});
  72. context.constant_values().Set(inst_id, const_id);
  73. // Register the instruction to be added to the eval block.
  74. AttachDependentInstToCurrentGeneric(
  75. context, {.inst_id = inst_id, .kind = DependentInstKind::Template});
  76. return inst_id;
  77. }
  78. auto AddPatternInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  79. -> SemIR::InstId {
  80. auto type_id = loc_id_and_inst.inst.type_id();
  81. CARBON_CHECK(type_id == SemIR::ErrorInst::TypeId ||
  82. context.types().Is<SemIR::PatternType>(type_id));
  83. auto inst_id = AddInstInNoBlock(context, loc_id_and_inst);
  84. context.pattern_block_stack().AddInstId(inst_id);
  85. return inst_id;
  86. }
  87. auto GetOrAddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  88. -> SemIR::InstId {
  89. CARBON_CHECK(!loc_id_and_inst.inst.kind().has_cleanup());
  90. auto handle_constant_id = [&](SemIR::ConstantId const_id) -> SemIR::InstId {
  91. CARBON_CHECK(const_id.has_value());
  92. // If we didn't produce a constant value for the instruction, we have to add
  93. // the instruction.
  94. if (!const_id.is_constant()) {
  95. return SemIR::InstId::None;
  96. }
  97. if (const_id.is_symbolic()) {
  98. // TODO: Only add this instruction to the eval block, and don't
  99. // re-evaluate it.
  100. return AddInst(context, loc_id_and_inst);
  101. }
  102. CARBON_VLOG_TO(context.vlog_stream(), "GetOrAddInst: constant: {0}\n",
  103. loc_id_and_inst.inst);
  104. return context.constant_values().GetInstId(const_id);
  105. };
  106. // If the instruction is from desugaring, produce its constant value instead
  107. // if possible.
  108. if (loc_id_and_inst.loc_id.is_desugared()) {
  109. switch (loc_id_and_inst.inst.kind().constant_needs_inst_id()) {
  110. case SemIR::InstConstantNeedsInstIdKind::No: {
  111. // Evaluation doesn't need an InstId. Just do it.
  112. auto const_id = TryEvalInstUnsafe(context, SemIR::InstId::None,
  113. loc_id_and_inst.inst);
  114. if (auto result_inst_id = handle_constant_id(const_id);
  115. result_inst_id.has_value()) {
  116. return result_inst_id;
  117. }
  118. break;
  119. }
  120. case SemIR::InstConstantNeedsInstIdKind::DuringEvaluation: {
  121. // Evaluation temporarily needs an InstId. Add one for now.
  122. auto inst_id = AddInstInNoBlock(context, loc_id_and_inst);
  123. auto const_id = context.constant_values().Get(inst_id);
  124. if (auto result_inst_id = handle_constant_id(const_id);
  125. result_inst_id.has_value()) {
  126. // TODO: We didn't end up needing the `inst_id` instruction. Consider
  127. // removing it from `insts` if it's still the most recently added
  128. // instruction.
  129. CARBON_CHECK(result_inst_id != inst_id);
  130. return result_inst_id;
  131. }
  132. context.inst_block_stack().AddInstId(inst_id);
  133. return inst_id;
  134. }
  135. case SemIR::InstConstantNeedsInstIdKind::Permanent: {
  136. // Evaluation needs a permanent InstId. Add the instruction.
  137. break;
  138. }
  139. }
  140. }
  141. // TODO: For an implicit instruction, this reattempts evaluation.
  142. return AddInst(context, loc_id_and_inst);
  143. }
  144. auto EvalOrAddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  145. -> SemIR::ConstantId {
  146. CARBON_CHECK(!loc_id_and_inst.inst.kind().has_cleanup());
  147. switch (loc_id_and_inst.inst.kind().constant_needs_inst_id()) {
  148. case SemIR::InstConstantNeedsInstIdKind::No: {
  149. // Evaluation doesn't need an InstId. Just do it.
  150. return TryEvalInstUnsafe(context, SemIR::InstId::None,
  151. loc_id_and_inst.inst);
  152. }
  153. case SemIR::InstConstantNeedsInstIdKind::DuringEvaluation: {
  154. // Evaluation temporarily needs an InstId. Add one for now.
  155. auto inst_id = AddInstInNoBlock(context, loc_id_and_inst);
  156. // TODO: Consider removing `inst_id` from `insts` if it's still the most
  157. // recently added instruction.
  158. return context.constant_values().Get(inst_id);
  159. }
  160. case SemIR::InstConstantNeedsInstIdKind::Permanent: {
  161. // Evaluation needs a permanent InstId. Add the instruction.
  162. auto inst_id = AddInst(context, loc_id_and_inst);
  163. return context.constant_values().Get(inst_id);
  164. }
  165. }
  166. }
  167. auto AddPlaceholderInstInNoBlock(Context& context,
  168. SemIR::LocIdAndInst loc_id_and_inst)
  169. -> SemIR::InstId {
  170. auto inst_id = context.sem_ir().insts().AddInNoBlock(loc_id_and_inst);
  171. CARBON_VLOG_TO(context.vlog_stream(), "AddPlaceholderInst: {0}\n",
  172. loc_id_and_inst.inst);
  173. context.constant_values().Set(inst_id, SemIR::ConstantId::None);
  174. return inst_id;
  175. }
  176. auto AddPlaceholderImportedInstInNoBlock(Context& context,
  177. SemIR::LocIdAndInst loc_id_and_inst)
  178. -> SemIR::InstId {
  179. auto inst_id = AddPlaceholderInstInNoBlock(context, loc_id_and_inst);
  180. context.imports().push_back(inst_id);
  181. return inst_id;
  182. }
  183. auto AddPlaceholderInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  184. -> SemIR::InstId {
  185. auto inst_id = AddPlaceholderInstInNoBlock(context, loc_id_and_inst);
  186. context.inst_block_stack().AddInstId(inst_id);
  187. return inst_id;
  188. }
  189. auto ReplaceLocIdAndInstBeforeConstantUse(Context& context,
  190. SemIR::InstId inst_id,
  191. SemIR::LocIdAndInst loc_id_and_inst)
  192. -> void {
  193. context.sem_ir().insts().SetLocIdAndInst(inst_id, loc_id_and_inst);
  194. CARBON_VLOG_TO(context.vlog_stream(), "ReplaceInst: {0} -> {1}\n", inst_id,
  195. loc_id_and_inst.inst);
  196. FinishInst(context, inst_id, loc_id_and_inst.inst);
  197. }
  198. auto ReplaceInstBeforeConstantUse(Context& context, SemIR::InstId inst_id,
  199. SemIR::Inst inst) -> void {
  200. context.sem_ir().insts().Set(inst_id, inst);
  201. CARBON_VLOG_TO(context.vlog_stream(), "ReplaceInst: {0} -> {1}\n", inst_id,
  202. inst);
  203. FinishInst(context, inst_id, inst);
  204. }
  205. auto ReplaceInstPreservingConstantValue(Context& context, SemIR::InstId inst_id,
  206. SemIR::Inst inst) -> void {
  207. // Check that the type didn't change: a change of type will change the
  208. // constant value. Replace the type with the attached type.
  209. auto old_type_id = context.insts().GetAttachedType(inst_id);
  210. CARBON_CHECK(context.types().GetUnattachedType(old_type_id) == inst.type_id(),
  211. "Given wrong type for replacement instruction");
  212. inst.SetType(old_type_id);
  213. // Update the instruction.
  214. context.sem_ir().insts().Set(inst_id, inst);
  215. CARBON_VLOG_TO(context.vlog_stream(), "ReplaceInst: {0} -> {1}\n", inst_id,
  216. inst);
  217. // Check the constant value didn't change.
  218. auto old_const_id = context.constant_values().Get(inst_id);
  219. auto new_const_id = TryEvalInstUnsafe(context, inst_id, inst);
  220. CARBON_CHECK(old_const_id == new_const_id);
  221. }
  222. auto SetNamespaceNodeId(Context& context, SemIR::InstId inst_id,
  223. Parse::NodeId node_id) -> void {
  224. context.sem_ir().insts().SetLocId(inst_id, SemIR::LocId(node_id));
  225. }
  226. } // namespace Carbon::Check