inst.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 inst_id = AddInstInNoBlock(context, loc_id_and_inst);
  83. context.pattern_block_stack().AddInstId(inst_id);
  84. return inst_id;
  85. }
  86. auto GetOrAddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  87. -> SemIR::InstId {
  88. CARBON_CHECK(!loc_id_and_inst.inst.kind().has_cleanup());
  89. auto handle_constant_id = [&](SemIR::ConstantId const_id) -> SemIR::InstId {
  90. CARBON_CHECK(const_id.has_value());
  91. // If we didn't produce a constant value for the instruction, we have to add
  92. // the instruction.
  93. if (!const_id.is_constant()) {
  94. return SemIR::InstId::None;
  95. }
  96. CARBON_VLOG_TO(context.vlog_stream(), "GetOrAddInst: constant: {0}\n",
  97. loc_id_and_inst.inst);
  98. return context.constant_values().GetInstId(const_id);
  99. };
  100. // If the instruction is implicit, produce its constant value instead if
  101. // possible.
  102. if (loc_id_and_inst.loc_id.is_implicit()) {
  103. switch (loc_id_and_inst.inst.kind().constant_needs_inst_id()) {
  104. case SemIR::InstConstantNeedsInstIdKind::No: {
  105. // Evaluation doesn't need an InstId. Just do it.
  106. auto const_id = TryEvalInstUnsafe(context, SemIR::InstId::None,
  107. loc_id_and_inst.inst);
  108. if (auto result_inst_id = handle_constant_id(const_id);
  109. result_inst_id.has_value()) {
  110. return result_inst_id;
  111. }
  112. break;
  113. }
  114. case SemIR::InstConstantNeedsInstIdKind::DuringEvaluation: {
  115. // Evaluation temporarily needs an InstId. Add one for now.
  116. auto inst_id = AddInstInNoBlock(context, loc_id_and_inst);
  117. auto const_id = context.constant_values().Get(inst_id);
  118. if (auto result_inst_id = handle_constant_id(const_id);
  119. result_inst_id.has_value()) {
  120. // TODO: We didn't end up needing the `inst_id` instruction. Consider
  121. // removing it from `insts` if it's still the most recently added
  122. // instruction.
  123. CARBON_CHECK(result_inst_id != inst_id);
  124. return result_inst_id;
  125. }
  126. context.inst_block_stack().AddInstId(inst_id);
  127. return inst_id;
  128. }
  129. case SemIR::InstConstantNeedsInstIdKind::Permanent: {
  130. // Evaluation needs a permanent InstId. Add the instruction.
  131. break;
  132. }
  133. }
  134. }
  135. // TODO: For an implicit instruction, this reattempts evaluation.
  136. return AddInst(context, loc_id_and_inst);
  137. }
  138. auto EvalOrAddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  139. -> SemIR::ConstantId {
  140. CARBON_CHECK(!loc_id_and_inst.inst.kind().has_cleanup());
  141. switch (loc_id_and_inst.inst.kind().constant_needs_inst_id()) {
  142. case SemIR::InstConstantNeedsInstIdKind::No: {
  143. // Evaluation doesn't need an InstId. Just do it.
  144. return TryEvalInstUnsafe(context, SemIR::InstId::None,
  145. loc_id_and_inst.inst);
  146. }
  147. case SemIR::InstConstantNeedsInstIdKind::DuringEvaluation: {
  148. // Evaluation temporarily needs an InstId. Add one for now.
  149. auto inst_id = AddInstInNoBlock(context, loc_id_and_inst);
  150. // TODO: Consider removing `inst_id` from `insts` if it's still the most
  151. // recently added instruction.
  152. return context.constant_values().Get(inst_id);
  153. }
  154. case SemIR::InstConstantNeedsInstIdKind::Permanent: {
  155. // Evaluation needs a permanent InstId. Add the instruction.
  156. auto inst_id = AddInst(context, loc_id_and_inst);
  157. return context.constant_values().Get(inst_id);
  158. }
  159. }
  160. }
  161. auto AddPlaceholderInstInNoBlock(Context& context,
  162. SemIR::LocIdAndInst loc_id_and_inst)
  163. -> SemIR::InstId {
  164. auto inst_id = context.sem_ir().insts().AddInNoBlock(loc_id_and_inst);
  165. CARBON_VLOG_TO(context.vlog_stream(), "AddPlaceholderInst: {0}\n",
  166. loc_id_and_inst.inst);
  167. context.constant_values().Set(inst_id, SemIR::ConstantId::None);
  168. return inst_id;
  169. }
  170. auto AddPlaceholderInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  171. -> SemIR::InstId {
  172. auto inst_id = AddPlaceholderInstInNoBlock(context, loc_id_and_inst);
  173. context.inst_block_stack().AddInstId(inst_id);
  174. return inst_id;
  175. }
  176. auto ReplaceLocIdAndInstBeforeConstantUse(Context& context,
  177. SemIR::InstId inst_id,
  178. SemIR::LocIdAndInst loc_id_and_inst)
  179. -> void {
  180. context.sem_ir().insts().SetLocIdAndInst(inst_id, loc_id_and_inst);
  181. CARBON_VLOG_TO(context.vlog_stream(), "ReplaceInst: {0} -> {1}\n", inst_id,
  182. loc_id_and_inst.inst);
  183. FinishInst(context, inst_id, loc_id_and_inst.inst);
  184. }
  185. auto ReplaceInstBeforeConstantUse(Context& context, SemIR::InstId inst_id,
  186. SemIR::Inst inst) -> void {
  187. context.sem_ir().insts().Set(inst_id, inst);
  188. CARBON_VLOG_TO(context.vlog_stream(), "ReplaceInst: {0} -> {1}\n", inst_id,
  189. inst);
  190. FinishInst(context, inst_id, inst);
  191. }
  192. auto ReplaceInstPreservingConstantValue(Context& context, SemIR::InstId inst_id,
  193. SemIR::Inst inst) -> void {
  194. auto old_const_id = context.constant_values().Get(inst_id);
  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. auto new_const_id = TryEvalInstUnsafe(context, inst_id, inst);
  199. CARBON_CHECK(old_const_id == new_const_id);
  200. }
  201. auto SetNamespaceNodeId(Context& context, SemIR::InstId inst_id,
  202. Parse::NodeId node_id) -> void {
  203. context.sem_ir().insts().SetLocId(inst_id, SemIR::LocId(node_id));
  204. }
  205. } // namespace Carbon::Check