inst.cpp 9.5 KB

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