action.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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/action.h"
  5. #include "toolchain/base/kind_switch.h"
  6. #include "toolchain/check/generic_region_stack.h"
  7. #include "toolchain/check/inst.h"
  8. #include "toolchain/check/type.h"
  9. #include "toolchain/sem_ir/constant.h"
  10. #include "toolchain/sem_ir/copy_on_write_block.h"
  11. #include "toolchain/sem_ir/id_kind.h"
  12. #include "toolchain/sem_ir/inst.h"
  13. #include "toolchain/sem_ir/typed_insts.h"
  14. namespace Carbon::Check {
  15. auto PerformAction(Context& context, SemIR::LocId loc_id,
  16. SemIR::RefineTypeAction action) -> SemIR::InstId {
  17. return AddInst<SemIR::AsCompatible>(
  18. context, loc_id,
  19. {.type_id =
  20. context.types().GetTypeIdForTypeInstId(action.inst_type_inst_id),
  21. .source_id = action.inst_id});
  22. }
  23. auto PerformAction(Context& context, SemIR::LocId loc_id,
  24. SemIR::RefineFormAction action) -> SemIR::InstId {
  25. auto expr_const_id = context.constant_values().Get(action.form_id);
  26. if (!expr_const_id.is_constant()) {
  27. // This is an error which will be diagnosed elsewhere, so we should just
  28. // get out of its way.
  29. return action.form_id;
  30. }
  31. auto expr =
  32. context.insts().Get(context.constant_values().GetInstId(expr_const_id));
  33. CARBON_KIND_SWITCH(expr) {
  34. case SemIR::InitForm::Kind:
  35. case SemIR::RefForm::Kind:
  36. case SemIR::ValueForm::Kind:
  37. // Primitive forms have no form operands, so the action is a no-op.
  38. return action.form_id;
  39. case CARBON_KIND(SemIR::TupleValue tuple_value): {
  40. SemIR::CopyOnWriteInstBlock new_inst_block(&context.sem_ir(),
  41. tuple_value.elements_id);
  42. for (auto [i, element_id] : llvm::enumerate(
  43. context.inst_blocks().Get(tuple_value.elements_id))) {
  44. new_inst_block.Set(i, HandleAction<SemIR::RefineFormAction>(
  45. context, loc_id, SemIR::FormType::TypeInstId,
  46. {.type_id = SemIR::InstType::TypeId,
  47. .form_id = element_id}));
  48. }
  49. auto new_inst_block_id = new_inst_block.GetCanonical();
  50. if (new_inst_block_id == tuple_value.elements_id) {
  51. return action.form_id;
  52. }
  53. return AddInst<SemIR::TupleValue>(context, loc_id,
  54. {.type_id = SemIR::FormType::TypeId,
  55. .elements_id = new_inst_block_id});
  56. }
  57. default:
  58. CARBON_FATAL("Unexpected kind for non-dependent form constant {0}", expr);
  59. }
  60. }
  61. static auto OperandDependence(Context& context, SemIR::ConstantId const_id)
  62. -> SemIR::ConstantDependence {
  63. // A type operand makes the instruction dependent if it is a
  64. // template-dependent constant.
  65. if (!const_id.is_symbolic()) {
  66. return SemIR::ConstantDependence::None;
  67. }
  68. return context.constant_values().GetSymbolicConstant(const_id).dependence;
  69. }
  70. auto OperandDependence(Context& context, SemIR::TypeId type_id)
  71. -> SemIR::ConstantDependence {
  72. // A type operand makes the instruction dependent if it is a
  73. // template-dependent type.
  74. return OperandDependence(context, context.types().GetConstantId(type_id));
  75. }
  76. auto OperandDependence(Context& context, SemIR::InstId inst_id)
  77. -> SemIR::ConstantDependence {
  78. // An instruction operand makes the instruction dependent if its type or
  79. // constant value is dependent.
  80. return std::max(
  81. OperandDependence(context, context.insts().Get(inst_id).type_id()),
  82. OperandDependence(context, context.constant_values().Get(inst_id)));
  83. }
  84. auto OperandDependence(Context& context, SemIR::TypeInstId inst_id)
  85. -> SemIR::ConstantDependence {
  86. // An instruction operand makes the instruction dependent if its type or
  87. // constant value is dependent. TypeInstId has type `TypeType` which is
  88. // concrete, so we only need to look at the constant value.
  89. return OperandDependence(context, context.constant_values().Get(inst_id));
  90. }
  91. static auto OperandDependence(Context& context, SemIR::Inst::ArgAndKind arg)
  92. -> SemIR::ConstantDependence {
  93. CARBON_KIND_SWITCH(arg) {
  94. case CARBON_KIND(SemIR::InstId inst_id): {
  95. return OperandDependence(context, inst_id);
  96. }
  97. case CARBON_KIND(SemIR::MetaInstId inst_id): {
  98. return OperandDependence(context, inst_id);
  99. }
  100. case CARBON_KIND(SemIR::TypeInstId inst_id): {
  101. return OperandDependence(context, inst_id);
  102. }
  103. case SemIR::IdKind::None:
  104. case SemIR::IdKind::For<SemIR::AbsoluteInstId>:
  105. case SemIR::IdKind::For<SemIR::NameId>:
  106. return SemIR::ConstantDependence::None;
  107. default:
  108. // TODO: Properly handle different argument kinds.
  109. CARBON_FATAL("Unexpected argument kind for action");
  110. }
  111. }
  112. auto ActionIsPerformable(Context& context, SemIR::Inst action_inst) -> bool {
  113. if (auto refine_action = action_inst.TryAs<SemIR::RefineTypeAction>()) {
  114. // `RefineTypeAction` can be performed whenever the type is not template-
  115. // dependent, even if we don't know the instruction yet.
  116. return OperandDependence(context, refine_action->inst_type_inst_id) <
  117. SemIR::ConstantDependence::Template;
  118. }
  119. if (auto refine_action = action_inst.TryAs<SemIR::RefineFormAction>()) {
  120. auto form_const_id = context.constant_values().Get(refine_action->form_id);
  121. auto form_id = context.constant_values().GetInstIdIfValid(form_const_id);
  122. if (!form_id.has_value()) {
  123. // This is an error which will be diagnosed elsewhere, so we should just
  124. // get out of its way.
  125. return true;
  126. }
  127. // A RefineFormAction can be performed if we can identify all of the
  128. // subexpressions of `form_id` that are in form positions.
  129. CARBON_KIND_SWITCH(context.insts().Get(form_id)) {
  130. case SemIR::InitForm::Kind:
  131. case SemIR::RefForm::Kind:
  132. case SemIR::ValueForm::Kind:
  133. case SemIR::TupleValue::Kind:
  134. // These inst kinds are not rewritten by constant evaluation except to
  135. // substitute values for their operands (i.e. their constant kind is
  136. // WheneverPossible, Always, or AlwaysUnique), so we can identify all of
  137. // their form subexpressions.
  138. return true;
  139. default:
  140. // All other inst kinds either can't appear in a form position, or
  141. // may be rewritten by constant evaluation, so we can't identify
  142. // their form subexpressions unless they are already concrete.
  143. return OperandDependence(context, form_const_id) ==
  144. SemIR::ConstantDependence::None;
  145. }
  146. }
  147. // A form-parameterized action is performable if we can see at least the top
  148. // level of its form's structure (i.e. it is not an action or a splice).
  149. if (auto form_parameterized_action =
  150. action_inst.TryAs<SemIR::AnyFormParamAction>()) {
  151. auto form_const_id =
  152. context.constant_values().Get(form_parameterized_action->form_id);
  153. auto form_id = context.constant_values().GetInstIdIfValid(form_const_id);
  154. if (!form_id.has_value()) {
  155. // This is an error which will be diagnosed elsewhere, so we should just
  156. // get out of its way.
  157. return true;
  158. }
  159. return !context.insts().Is<SemIR::RefineFormAction>(form_id) &&
  160. !context.insts().Is<SemIR::SpliceInst>(form_id);
  161. }
  162. return OperandDependence(context, action_inst.type_id()) <
  163. SemIR::ConstantDependence::Template &&
  164. OperandDependence(context, action_inst.arg0_and_kind()) <
  165. SemIR::ConstantDependence::Template &&
  166. OperandDependence(context, action_inst.arg1_and_kind()) <
  167. SemIR::ConstantDependence::Template;
  168. }
  169. static auto AddDependentActionSpliceImpl(Context& context,
  170. SemIR::LocIdAndInst action,
  171. SemIR::TypeInstId result_type_inst_id)
  172. -> SemIR::InstId {
  173. auto inst_id = AddDependentActionInst(context, action);
  174. if (!result_type_inst_id.has_value()) {
  175. result_type_inst_id = AddDependentActionTypeInst(
  176. context, action.loc_id,
  177. SemIR::TypeOfInst{.type_id = SemIR::TypeType::TypeId,
  178. .inst_id = inst_id});
  179. }
  180. return AddInst(
  181. context, action.loc_id,
  182. SemIR::SpliceInst{.type_id = context.types().GetTypeIdForTypeInstId(
  183. result_type_inst_id),
  184. .inst_id = inst_id});
  185. }
  186. // Refine one operand of an action. Given an argument from a template, this
  187. // produces an argument that has the template-dependent parts replaced with
  188. // their concrete values, so that the action doesn't need to know which specific
  189. // it is operating on.
  190. static auto RefineOperand(Context& context, SemIR::LocId loc_id,
  191. SemIR::Inst::ArgAndKind arg) -> int32_t {
  192. if (auto inst_id = arg.TryAs<SemIR::MetaInstId>()) {
  193. auto inst = context.insts().Get(*inst_id);
  194. if (inst.Is<SemIR::SpliceInst>()) {
  195. // The argument will evaluate to the spliced instruction, which is already
  196. // refined.
  197. return arg.value();
  198. }
  199. // If the type of the action argument is dependent, refine to an instruction
  200. // with a concrete type.
  201. if (OperandDependence(context, inst.type_id()) ==
  202. SemIR::ConstantDependence::Template) {
  203. auto type_inst_id = context.types().GetTypeInstId(inst.type_id());
  204. inst_id = AddDependentActionSpliceImpl(
  205. context,
  206. SemIR::LocIdAndInst(
  207. loc_id,
  208. SemIR::RefineTypeAction{.type_id = GetSingletonType(
  209. context, SemIR::InstType::TypeInstId),
  210. .inst_id = *inst_id,
  211. .inst_type_inst_id = type_inst_id}),
  212. type_inst_id);
  213. }
  214. // TODO: Handle the case where the constant value of the instruction is
  215. // template-dependent.
  216. return inst_id->index;
  217. }
  218. return arg.value();
  219. }
  220. // Refine the operands of an action, ensuring that they will refer to concrete
  221. // instructions that don't have template-dependent types.
  222. static auto RefineOperands(Context& context, SemIR::LocId loc_id,
  223. SemIR::Inst action) -> SemIR::Inst {
  224. auto arg0 = RefineOperand(context, loc_id, action.arg0_and_kind());
  225. auto arg1 = RefineOperand(context, loc_id, action.arg1_and_kind());
  226. action.SetArgs(arg0, arg1);
  227. return action;
  228. }
  229. auto AddDependentActionSplice(Context& context, SemIR::LocIdAndInst action,
  230. SemIR::TypeInstId result_type_inst_id)
  231. -> SemIR::InstId {
  232. action.inst = RefineOperands(context, action.loc_id, action.inst);
  233. return AddDependentActionSpliceImpl(context, action, result_type_inst_id);
  234. }
  235. auto Internal::BeginPerformDelayedAction(Context& context) -> void {
  236. // Push an `InstBlock` to hold any instructions created by the action.
  237. // Note that we assume that actions don't need to create multiple blocks. If
  238. // this changes, we should push a region too.
  239. context.inst_block_stack().Push();
  240. context.pattern_block_stack().Push();
  241. }
  242. auto Internal::EndPerformDelayedAction(Context& context,
  243. SemIR::InstId result_id)
  244. -> SemIR::InstId {
  245. // If the only created instruction is the result, then we can use it directly.
  246. auto contents = context.inst_block_stack().PeekCurrentBlockContents();
  247. auto pattern_contents =
  248. context.pattern_block_stack().PeekCurrentBlockContents();
  249. if ((contents == llvm::ArrayRef(result_id) && pattern_contents.empty()) ||
  250. (pattern_contents == llvm::ArrayRef(result_id) && contents.empty())) {
  251. context.inst_block_stack().PopAndDiscard();
  252. context.pattern_block_stack().PopAndDiscard();
  253. return result_id;
  254. }
  255. // Otherwise, create a splice_block to represent the sequence of instructions
  256. // created by the action.
  257. auto block_id = SemIR::InstBlockId::None;
  258. if (pattern_contents.empty()) {
  259. block_id = context.inst_block_stack().Pop();
  260. context.pattern_block_stack().PopAndDiscard();
  261. } else {
  262. // TODO: pattern insts can depend on non-pattern insts, so we'll probably
  263. // eventually need to support actions that produce both.
  264. CARBON_CHECK(!contents.empty());
  265. block_id = context.pattern_block_stack().Pop();
  266. context.inst_block_stack().PopAndDiscard();
  267. }
  268. auto result = context.insts().GetWithLocId(result_id);
  269. return AddInstInNoBlock(context, result.loc_id,
  270. SemIR::SpliceBlock{.type_id = result.inst.type_id(),
  271. .block_id = block_id,
  272. .result_id = result_id});
  273. }
  274. } // namespace Carbon::Check