file_context.cpp 11 KB

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