handle_aggregates.cpp 12 KB

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