inst.cpp 9.3 KB

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