lowering_context.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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/lowering/lowering_context.h"
  5. #include "common/vlog.h"
  6. #include "llvm/ADT/STLExtras.h"
  7. #include "llvm/ADT/Sequence.h"
  8. #include "toolchain/lowering/lowering_function_context.h"
  9. #include "toolchain/semantics/semantics_ir.h"
  10. #include "toolchain/semantics/semantics_node.h"
  11. #include "toolchain/semantics/semantics_node_kind.h"
  12. namespace Carbon {
  13. LoweringContext::LoweringContext(llvm::LLVMContext& llvm_context,
  14. llvm::StringRef module_name,
  15. const SemIR::File& semantics_ir,
  16. llvm::raw_ostream* vlog_stream)
  17. : llvm_context_(&llvm_context),
  18. llvm_module_(std::make_unique<llvm::Module>(module_name, llvm_context)),
  19. semantics_ir_(&semantics_ir),
  20. vlog_stream_(vlog_stream) {
  21. CARBON_CHECK(!semantics_ir.has_errors())
  22. << "Generating LLVM IR from invalid SemIR::File is unsupported.";
  23. }
  24. // TODO: Move this to lower_to_llvm.cpp.
  25. auto LoweringContext::Run() -> std::unique_ptr<llvm::Module> {
  26. CARBON_CHECK(llvm_module_) << "Run can only be called once.";
  27. // Lower types.
  28. auto types = semantics_ir_->types();
  29. types_.resize_for_overwrite(types.size());
  30. for (auto [i, type] : llvm::enumerate(types)) {
  31. types_[i] = BuildType(type);
  32. }
  33. // Lower function declarations.
  34. functions_.resize_for_overwrite(semantics_ir_->functions_size());
  35. for (auto i : llvm::seq(semantics_ir_->functions_size())) {
  36. functions_[i] = BuildFunctionDeclaration(SemIR::FunctionId(i));
  37. }
  38. // TODO: Lower global variable declarations.
  39. // Lower function definitions.
  40. for (auto i : llvm::seq(semantics_ir_->functions_size())) {
  41. BuildFunctionDefinition(SemIR::FunctionId(i));
  42. }
  43. // TODO: Lower global variable initializers.
  44. return std::move(llvm_module_);
  45. }
  46. auto LoweringContext::BuildFunctionDeclaration(SemIR::FunctionId function_id)
  47. -> llvm::Function* {
  48. const auto& function = semantics_ir().GetFunction(function_id);
  49. const bool has_return_slot = function.return_slot_id.is_valid();
  50. auto& param_refs = semantics_ir().GetNodeBlock(function.param_refs_id);
  51. SemIR::InitializingRepresentation return_rep =
  52. function.return_type_id.is_valid()
  53. ? SemIR::GetInitializingRepresentation(semantics_ir(),
  54. function.return_type_id)
  55. : SemIR::InitializingRepresentation{
  56. .kind = SemIR::InitializingRepresentation::None};
  57. CARBON_CHECK(return_rep.has_return_slot() == has_return_slot);
  58. llvm::SmallVector<llvm::Type*> param_types;
  59. // TODO: Consider either storing `param_node_ids` somewhere so that we can
  60. // reuse it from `BuildFunctionDefinition` and when building calls, or factor
  61. // out a mechanism to compute the mapping between parameters and arguments on
  62. // demand.
  63. llvm::SmallVector<SemIR::NodeId> param_node_ids;
  64. param_types.reserve(has_return_slot + param_refs.size());
  65. param_node_ids.reserve(has_return_slot + param_refs.size());
  66. if (has_return_slot) {
  67. param_types.push_back(GetType(function.return_type_id)->getPointerTo());
  68. param_node_ids.push_back(function.return_slot_id);
  69. }
  70. for (auto param_ref_id : param_refs) {
  71. auto param_type_id = semantics_ir().GetNode(param_ref_id).type_id();
  72. switch (auto value_rep =
  73. SemIR::GetValueRepresentation(semantics_ir(), param_type_id);
  74. value_rep.kind) {
  75. case SemIR::ValueRepresentation::None:
  76. break;
  77. case SemIR::ValueRepresentation::Copy:
  78. case SemIR::ValueRepresentation::Custom:
  79. param_types.push_back(GetType(value_rep.type));
  80. param_node_ids.push_back(param_ref_id);
  81. break;
  82. case SemIR::ValueRepresentation::Pointer:
  83. param_types.push_back(GetType(value_rep.type)->getPointerTo());
  84. param_node_ids.push_back(param_ref_id);
  85. break;
  86. }
  87. }
  88. // If the initializing representation doesn't produce a value, set the return
  89. // type to void.
  90. llvm::Type* return_type =
  91. return_rep.kind == SemIR::InitializingRepresentation::ByCopy
  92. ? GetType(function.return_type_id)
  93. : llvm::Type::getVoidTy(llvm_context());
  94. llvm::FunctionType* function_type =
  95. llvm::FunctionType::get(return_type, param_types, /*isVarArg=*/false);
  96. auto* llvm_function = llvm::Function::Create(
  97. function_type, llvm::Function::ExternalLinkage,
  98. semantics_ir().GetString(function.name_id), llvm_module());
  99. // Set up parameters and the return slot.
  100. for (auto [node_id, arg] :
  101. llvm::zip_equal(param_node_ids, llvm_function->args())) {
  102. if (node_id == function.return_slot_id) {
  103. arg.setName("return");
  104. arg.addAttr(llvm::Attribute::getWithStructRetType(
  105. llvm_context(), GetType(function.return_type_id)));
  106. } else {
  107. arg.setName(semantics_ir().GetString(
  108. semantics_ir().GetNode(node_id).GetAsParameter()));
  109. }
  110. }
  111. return llvm_function;
  112. }
  113. auto LoweringContext::BuildFunctionDefinition(SemIR::FunctionId function_id)
  114. -> void {
  115. const auto& function = semantics_ir().GetFunction(function_id);
  116. const auto& body_block_ids = function.body_block_ids;
  117. if (body_block_ids.empty()) {
  118. // Function is probably defined in another file; not an error.
  119. return;
  120. }
  121. llvm::Function* llvm_function = GetFunction(function_id);
  122. LoweringFunctionContext function_lowering(*this, llvm_function);
  123. const bool has_return_slot = function.return_slot_id.is_valid();
  124. // Add parameters to locals.
  125. // TODO: This duplicates the mapping between semantics nodes and LLVM
  126. // function parameters that was already computed in BuildFunctionDeclaration.
  127. // We should only do that once.
  128. auto& param_refs = semantics_ir().GetNodeBlock(function.param_refs_id);
  129. int param_index = 0;
  130. if (has_return_slot) {
  131. function_lowering.SetLocal(function.return_slot_id,
  132. llvm_function->getArg(param_index));
  133. ++param_index;
  134. }
  135. for (auto param_ref_id : param_refs) {
  136. auto param_type_id = semantics_ir().GetNode(param_ref_id).type_id();
  137. if (SemIR::GetValueRepresentation(semantics_ir(), param_type_id).kind ==
  138. SemIR::ValueRepresentation::None) {
  139. function_lowering.SetLocal(
  140. param_ref_id, llvm::PoisonValue::get(GetType(param_type_id)));
  141. } else {
  142. function_lowering.SetLocal(param_ref_id,
  143. llvm_function->getArg(param_index));
  144. ++param_index;
  145. }
  146. }
  147. // Lower all blocks.
  148. for (auto block_id : body_block_ids) {
  149. CARBON_VLOG() << "Lowering " << block_id << "\n";
  150. auto* llvm_block = function_lowering.GetBlock(block_id);
  151. // Keep the LLVM blocks in lexical order.
  152. llvm_block->moveBefore(llvm_function->end());
  153. function_lowering.builder().SetInsertPoint(llvm_block);
  154. for (const auto& node_id : semantics_ir().GetNodeBlock(block_id)) {
  155. auto node = semantics_ir().GetNode(node_id);
  156. CARBON_VLOG() << "Lowering " << node_id << ": " << node << "\n";
  157. // clang warns on unhandled enum values; clang-tidy is incorrect here.
  158. // NOLINTNEXTLINE(bugprone-switch-missing-default-case)
  159. switch (node.kind()) {
  160. #define CARBON_SEMANTICS_NODE_KIND(Name) \
  161. case SemIR::NodeKind::Name: \
  162. LoweringHandle##Name(function_lowering, node_id, node); \
  163. break;
  164. #include "toolchain/semantics/semantics_node_kind.def"
  165. }
  166. }
  167. }
  168. }
  169. auto LoweringContext::BuildType(SemIR::NodeId node_id) -> llvm::Type* {
  170. switch (node_id.index) {
  171. case SemIR::BuiltinKind::FloatingPointType.AsInt():
  172. // TODO: Handle different sizes.
  173. return llvm::Type::getDoubleTy(*llvm_context_);
  174. case SemIR::BuiltinKind::IntegerType.AsInt():
  175. // TODO: Handle different sizes.
  176. return llvm::Type::getInt32Ty(*llvm_context_);
  177. case SemIR::BuiltinKind::BoolType.AsInt():
  178. // TODO: We may want to have different representations for `bool` storage
  179. // (`i8`) versus for `bool` values (`i1`).
  180. return llvm::Type::getInt1Ty(*llvm_context_);
  181. default:
  182. // Handled below.
  183. break;
  184. }
  185. auto node = semantics_ir_->GetNode(node_id);
  186. switch (node.kind()) {
  187. case SemIR::NodeKind::ArrayType: {
  188. auto [bound_node_id, type_id] = node.GetAsArrayType();
  189. return llvm::ArrayType::get(
  190. GetType(type_id), semantics_ir_->GetArrayBoundValue(bound_node_id));
  191. }
  192. case SemIR::NodeKind::ConstType:
  193. return GetType(node.GetAsConstType());
  194. case SemIR::NodeKind::PointerType:
  195. return llvm::PointerType::get(*llvm_context_, /*AddressSpace=*/0);
  196. case SemIR::NodeKind::StructType: {
  197. auto& refs = semantics_ir_->GetNodeBlock(node.GetAsStructType());
  198. llvm::SmallVector<llvm::Type*> subtypes;
  199. subtypes.reserve(refs.size());
  200. for (auto ref_id : refs) {
  201. auto [field_name_id, field_type_id] =
  202. semantics_ir_->GetNode(ref_id).GetAsStructTypeField();
  203. // TODO: Handle recursive types. The restriction for builtins prevents
  204. // recursion while still letting them cache.
  205. CARBON_CHECK(field_type_id.index < SemIR::BuiltinKind::ValidCount)
  206. << field_type_id;
  207. subtypes.push_back(GetType(field_type_id));
  208. }
  209. return llvm::StructType::get(*llvm_context_, subtypes);
  210. }
  211. case SemIR::NodeKind::TupleType: {
  212. // TODO: Investigate special-casing handling of empty tuples so that they
  213. // can be collectively replaced with LLVM's void, particularly around
  214. // function returns. LLVM doesn't allow declaring variables with a void
  215. // type, so that may require significant special casing.
  216. auto& refs = semantics_ir_->GetTypeBlock(node.GetAsTupleType());
  217. llvm::SmallVector<llvm::Type*> subtypes;
  218. subtypes.reserve(refs.size());
  219. for (auto ref_id : refs) {
  220. subtypes.push_back(GetType(ref_id));
  221. }
  222. return llvm::StructType::get(*llvm_context_, subtypes);
  223. }
  224. default: {
  225. CARBON_FATAL() << "Cannot use node as type: " << node_id;
  226. }
  227. }
  228. }
  229. } // namespace Carbon