handle_aggregates.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 "llvm/ADT/STLExtras.h"
  5. #include "llvm/ADT/StringRef.h"
  6. #include "llvm/ADT/Twine.h"
  7. #include "llvm/IR/Constants.h"
  8. #include "llvm/IR/Value.h"
  9. #include "toolchain/lower/function_context.h"
  10. #include "toolchain/sem_ir/inst.h"
  11. #include "toolchain/sem_ir/typed_insts.h"
  12. namespace Carbon::Lower {
  13. auto HandleClassDecl(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  14. SemIR::ClassDecl /*inst*/) -> void {
  15. // No action to perform.
  16. }
  17. // Extracts an element of an aggregate, such as a struct, tuple, or class, by
  18. // index. Depending on the expression category and value representation of the
  19. // aggregate input, this will either produce a value or a reference.
  20. static auto GetAggregateElement(FunctionContext& context,
  21. SemIR::InstId aggr_inst_id,
  22. SemIR::ElementIndex idx,
  23. SemIR::TypeId result_type_id, llvm::Twine name)
  24. -> llvm::Value* {
  25. auto aggr_inst = context.sem_ir().insts().Get(aggr_inst_id);
  26. auto* aggr_value = context.GetValue(aggr_inst_id);
  27. switch (SemIR::GetExprCategory(context.sem_ir(), aggr_inst_id)) {
  28. case SemIR::ExprCategory::Error:
  29. case SemIR::ExprCategory::NotExpr:
  30. case SemIR::ExprCategory::Initializing:
  31. case SemIR::ExprCategory::Mixed:
  32. CARBON_FATAL() << "Unexpected expression category for aggregate access";
  33. case SemIR::ExprCategory::Value: {
  34. auto value_rep =
  35. SemIR::GetValueRepr(context.sem_ir(), aggr_inst.type_id());
  36. CARBON_CHECK(value_rep.aggregate_kind != SemIR::ValueRepr::NotAggregate)
  37. << "aggregate type should have aggregate value representation";
  38. switch (value_rep.kind) {
  39. case SemIR::ValueRepr::Unknown:
  40. CARBON_FATAL() << "Lowering access to incomplete aggregate type";
  41. case SemIR::ValueRepr::None:
  42. return aggr_value;
  43. case SemIR::ValueRepr::Copy:
  44. // We are holding the values of the aggregate directly, elementwise.
  45. return context.builder().CreateExtractValue(aggr_value, idx.index,
  46. name);
  47. case SemIR::ValueRepr::Pointer: {
  48. // The value representation is a pointer to an aggregate that we want
  49. // to index into.
  50. auto pointee_type_id =
  51. context.sem_ir().GetPointeeType(value_rep.type_id);
  52. auto* value_type = context.GetType(pointee_type_id);
  53. auto* elem_ptr = context.builder().CreateStructGEP(
  54. value_type, aggr_value, idx.index, name);
  55. if (!value_rep.elements_are_values()) {
  56. // `elem_ptr` points to an object representation, which is our
  57. // result.
  58. return elem_ptr;
  59. }
  60. // `elem_ptr` points to a value representation. Load it.
  61. auto result_value_type_id =
  62. SemIR::GetValueRepr(context.sem_ir(), result_type_id).type_id;
  63. return context.builder().CreateLoad(
  64. context.GetType(result_value_type_id), elem_ptr, name + ".load");
  65. }
  66. case SemIR::ValueRepr::Custom:
  67. CARBON_FATAL()
  68. << "Aggregate should never have custom value representation";
  69. }
  70. }
  71. case SemIR::ExprCategory::DurableRef:
  72. case SemIR::ExprCategory::EphemeralRef: {
  73. // Just locate the aggregate element.
  74. auto* aggr_type = context.GetType(aggr_inst.type_id());
  75. return context.builder().CreateStructGEP(aggr_type, aggr_value, idx.index,
  76. name);
  77. }
  78. }
  79. }
  80. static auto GetStructFieldName(FunctionContext& context,
  81. SemIR::TypeId struct_type_id,
  82. SemIR::ElementIndex index) -> llvm::StringRef {
  83. auto fields = context.sem_ir().inst_blocks().Get(
  84. context.sem_ir()
  85. .types()
  86. .GetAs<SemIR::StructType>(struct_type_id)
  87. .fields_id);
  88. auto field = context.sem_ir().insts().GetAs<SemIR::StructTypeField>(
  89. fields[index.index]);
  90. return context.sem_ir().names().GetIRBaseName(field.name_id);
  91. }
  92. auto HandleClassElementAccess(FunctionContext& context, SemIR::InstId inst_id,
  93. SemIR::ClassElementAccess inst) -> void {
  94. // Find the class that we're performing access into.
  95. auto class_type_id = context.sem_ir().insts().Get(inst.base_id).type_id();
  96. auto class_id =
  97. context.sem_ir().types().GetAs<SemIR::ClassType>(class_type_id).class_id;
  98. const auto& class_info = context.sem_ir().classes().Get(class_id);
  99. // Translate the class field access into a struct access on the object
  100. // representation.
  101. context.SetLocal(
  102. inst_id,
  103. GetAggregateElement(
  104. context, inst.base_id, inst.index, inst.type_id,
  105. GetStructFieldName(context, class_info.object_repr_id, inst.index)));
  106. }
  107. static auto EmitAggregateInitializer(FunctionContext& context,
  108. SemIR::TypeId type_id,
  109. SemIR::InstBlockId refs_id,
  110. llvm::Twine name) -> llvm::Value* {
  111. auto* llvm_type = context.GetType(type_id);
  112. switch (SemIR::GetInitRepr(context.sem_ir(), type_id).kind) {
  113. case SemIR::InitRepr::None:
  114. case SemIR::InitRepr::InPlace:
  115. // TODO: Add a helper to poison a value slot.
  116. return llvm::PoisonValue::get(llvm_type);
  117. case SemIR::InitRepr::ByCopy: {
  118. auto refs = context.sem_ir().inst_blocks().Get(refs_id);
  119. CARBON_CHECK(refs.size() == 1)
  120. << "Unexpected size for aggregate with by-copy value representation";
  121. // TODO: Remove the LLVM StructType wrapper in this case, so we don't
  122. // need this `insert_value` wrapping.
  123. return context.builder().CreateInsertValue(
  124. llvm::PoisonValue::get(llvm_type), context.GetValue(refs[0]), {0},
  125. name);
  126. }
  127. }
  128. }
  129. auto HandleClassInit(FunctionContext& context, SemIR::InstId inst_id,
  130. SemIR::ClassInit inst) -> void {
  131. context.SetLocal(
  132. inst_id, EmitAggregateInitializer(context, inst.type_id, inst.elements_id,
  133. "class.init"));
  134. }
  135. auto HandleStructAccess(FunctionContext& context, SemIR::InstId inst_id,
  136. SemIR::StructAccess inst) -> void {
  137. auto struct_type_id = context.sem_ir().insts().Get(inst.struct_id).type_id();
  138. context.SetLocal(
  139. inst_id, GetAggregateElement(
  140. context, inst.struct_id, inst.index, inst.type_id,
  141. GetStructFieldName(context, struct_type_id, inst.index)));
  142. }
  143. auto HandleStructLiteral(FunctionContext& /*context*/,
  144. SemIR::InstId /*inst_id*/,
  145. SemIR::StructLiteral /*inst*/) -> void {
  146. // A StructLiteral should always be converted to a StructInit or StructValue
  147. // if its value is needed.
  148. }
  149. // Emits the value representation for a struct or tuple whose elements are the
  150. // contents of `refs_id`.
  151. static auto EmitAggregateValueRepr(FunctionContext& context,
  152. SemIR::TypeId type_id,
  153. SemIR::InstBlockId refs_id) -> llvm::Value* {
  154. auto value_rep = SemIR::GetValueRepr(context.sem_ir(), type_id);
  155. switch (value_rep.kind) {
  156. case SemIR::ValueRepr::Unknown:
  157. CARBON_FATAL() << "Incomplete aggregate type in lowering";
  158. case SemIR::ValueRepr::None:
  159. // TODO: Add a helper to get a "no value representation" value.
  160. return llvm::PoisonValue::get(context.GetType(value_rep.type_id));
  161. case SemIR::ValueRepr::Copy: {
  162. auto refs = context.sem_ir().inst_blocks().Get(refs_id);
  163. CARBON_CHECK(refs.size() == 1)
  164. << "Unexpected size for aggregate with by-copy value representation";
  165. // TODO: Remove the LLVM StructType wrapper in this case, so we don't
  166. // need this `insert_value` wrapping.
  167. return context.builder().CreateInsertValue(
  168. llvm::PoisonValue::get(context.GetType(value_rep.type_id)),
  169. context.GetValue(refs[0]), {0});
  170. }
  171. case SemIR::ValueRepr::Pointer: {
  172. auto pointee_type_id = context.sem_ir().GetPointeeType(value_rep.type_id);
  173. auto* llvm_value_rep_type = context.GetType(pointee_type_id);
  174. // Write the value representation to a local alloca so we can produce a
  175. // pointer to it as the value representation of the struct or tuple.
  176. auto* alloca = context.builder().CreateAlloca(llvm_value_rep_type);
  177. for (auto [i, ref] :
  178. llvm::enumerate(context.sem_ir().inst_blocks().Get(refs_id))) {
  179. context.builder().CreateStore(
  180. context.GetValue(ref),
  181. context.builder().CreateStructGEP(llvm_value_rep_type, alloca, i));
  182. }
  183. return alloca;
  184. }
  185. case SemIR::ValueRepr::Custom:
  186. CARBON_FATAL()
  187. << "Aggregate should never have custom value representation";
  188. }
  189. }
  190. auto HandleStructInit(FunctionContext& context, SemIR::InstId inst_id,
  191. SemIR::StructInit inst) -> void {
  192. context.SetLocal(
  193. inst_id, EmitAggregateInitializer(context, inst.type_id, inst.elements_id,
  194. "struct.init"));
  195. }
  196. auto HandleStructValue(FunctionContext& context, SemIR::InstId inst_id,
  197. SemIR::StructValue inst) -> void {
  198. if (auto fn_type = context.sem_ir().types().TryGetAs<SemIR::FunctionType>(
  199. inst.type_id)) {
  200. context.SetLocal(inst_id, context.GetFunction(fn_type->function_id));
  201. return;
  202. }
  203. context.SetLocal(
  204. inst_id, EmitAggregateValueRepr(context, inst.type_id, inst.elements_id));
  205. }
  206. auto HandleStructTypeField(FunctionContext& /*context*/,
  207. SemIR::InstId /*inst_id*/,
  208. SemIR::StructTypeField /*inst*/) -> void {
  209. // No action to take.
  210. }
  211. auto HandleTupleAccess(FunctionContext& context, SemIR::InstId inst_id,
  212. SemIR::TupleAccess inst) -> void {
  213. context.SetLocal(inst_id,
  214. GetAggregateElement(context, inst.tuple_id, inst.index,
  215. inst.type_id, "tuple.elem"));
  216. }
  217. auto HandleTupleIndex(FunctionContext& context, SemIR::InstId inst_id,
  218. SemIR::TupleIndex inst) -> void {
  219. auto index_inst =
  220. context.sem_ir().insts().GetAs<SemIR::IntLiteral>(inst.index_id);
  221. auto index = context.sem_ir().ints().Get(index_inst.int_id).getZExtValue();
  222. context.SetLocal(inst_id, GetAggregateElement(context, inst.tuple_id,
  223. SemIR::ElementIndex(index),
  224. inst.type_id, "tuple.index"));
  225. }
  226. auto HandleTupleLiteral(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  227. SemIR::TupleLiteral /*inst*/) -> void {
  228. // A TupleLiteral should always be converted to a TupleInit or TupleValue if
  229. // its value is needed.
  230. }
  231. auto HandleTupleInit(FunctionContext& context, SemIR::InstId inst_id,
  232. SemIR::TupleInit inst) -> void {
  233. context.SetLocal(
  234. inst_id, EmitAggregateInitializer(context, inst.type_id, inst.elements_id,
  235. "tuple.init"));
  236. }
  237. auto HandleTupleValue(FunctionContext& context, SemIR::InstId inst_id,
  238. SemIR::TupleValue inst) -> void {
  239. context.SetLocal(
  240. inst_id, EmitAggregateValueRepr(context, inst.type_id, inst.elements_id));
  241. }
  242. } // namespace Carbon::Lower