file_context.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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/function.h"
  12. #include "toolchain/sem_ir/inst.h"
  13. #include "toolchain/sem_ir/typed_insts.h"
  14. namespace Carbon::Lower {
  15. FileContext::FileContext(llvm::LLVMContext& llvm_context,
  16. llvm::StringRef module_name, const SemIR::File& sem_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. sem_ir_(&sem_ir),
  21. vlog_stream_(vlog_stream) {
  22. CARBON_CHECK(!sem_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(sem_ir_->types().size());
  32. for (auto type_id : sem_ir_->complete_types()) {
  33. types_[type_id.index] = BuildType(sem_ir_->types().GetInstId(type_id));
  34. }
  35. // Lower function declarations.
  36. functions_.resize_for_overwrite(sem_ir_->functions().size());
  37. for (auto i : llvm::seq(sem_ir_->functions().size())) {
  38. functions_[i] = BuildFunctionDecl(SemIR::FunctionId(i));
  39. }
  40. // TODO: Lower global variable declarations.
  41. // Lower function definitions.
  42. for (auto i : llvm::seq(sem_ir_->functions().size())) {
  43. BuildFunctionDefinition(SemIR::FunctionId(i));
  44. }
  45. // TODO: Lower global variable initializers.
  46. return std::move(llvm_module_);
  47. }
  48. auto FileContext::GetGlobal(SemIR::InstId inst_id) -> llvm::Value* {
  49. // All builtins are types, with the same empty lowered value.
  50. if (inst_id.is_builtin()) {
  51. return GetTypeAsValue();
  52. }
  53. auto target = sem_ir().insts().Get(inst_id);
  54. while (auto alias = target.TryAs<SemIR::BindAlias>()) {
  55. inst_id = alias->value_id;
  56. target = sem_ir().insts().Get(inst_id);
  57. }
  58. if (auto function_decl = target.TryAs<SemIR::FunctionDecl>()) {
  59. return GetFunction(function_decl->function_id);
  60. }
  61. if (target.Is<SemIR::AssociatedEntity>() || target.Is<SemIR::FieldDecl>() ||
  62. target.Is<SemIR::BaseDecl>()) {
  63. return llvm::ConstantStruct::getAnon(llvm_context(), {});
  64. }
  65. if (target.type_id() == SemIR::TypeId::TypeType) {
  66. return GetTypeAsValue();
  67. }
  68. CARBON_FATAL() << "Missing value: " << inst_id << " " << target;
  69. }
  70. auto FileContext::BuildFunctionDecl(SemIR::FunctionId function_id)
  71. -> llvm::Function* {
  72. const auto& function = sem_ir().functions().Get(function_id);
  73. // Don't lower associated functions.
  74. // TODO: We shouldn't lower any function that has generic parameters.
  75. if (sem_ir().insts().Is<SemIR::InterfaceDecl>(
  76. sem_ir().name_scopes().Get(function.enclosing_scope_id).inst_id)) {
  77. return nullptr;
  78. }
  79. // Don't lower builtins.
  80. if (function.builtin_kind != SemIR::BuiltinFunctionKind::None) {
  81. return nullptr;
  82. }
  83. const bool has_return_slot = function.return_slot_id.is_valid();
  84. auto implicit_param_refs =
  85. sem_ir().inst_blocks().Get(function.implicit_param_refs_id);
  86. auto param_refs = sem_ir().inst_blocks().Get(function.param_refs_id);
  87. SemIR::InitRepr return_rep =
  88. function.return_type_id.is_valid()
  89. ? SemIR::GetInitRepr(sem_ir(), function.return_type_id)
  90. : SemIR::InitRepr{.kind = SemIR::InitRepr::None};
  91. CARBON_CHECK(return_rep.has_return_slot() == has_return_slot);
  92. llvm::SmallVector<llvm::Type*> param_types;
  93. // TODO: Consider either storing `param_inst_ids` somewhere so that we can
  94. // reuse it from `BuildFunctionDefinition` and when building calls, or factor
  95. // out a mechanism to compute the mapping between parameters and arguments on
  96. // demand.
  97. llvm::SmallVector<SemIR::InstId> param_inst_ids;
  98. auto max_llvm_params =
  99. has_return_slot + implicit_param_refs.size() + param_refs.size();
  100. param_types.reserve(max_llvm_params);
  101. param_inst_ids.reserve(max_llvm_params);
  102. if (has_return_slot) {
  103. param_types.push_back(GetType(function.return_type_id)->getPointerTo());
  104. param_inst_ids.push_back(function.return_slot_id);
  105. }
  106. for (auto param_ref_id :
  107. llvm::concat<const SemIR::InstId>(implicit_param_refs, param_refs)) {
  108. auto param_type_id =
  109. SemIR::Function::GetParamFromParamRefId(sem_ir(), param_ref_id)
  110. .second.type_id;
  111. switch (auto value_rep = SemIR::GetValueRepr(sem_ir(), param_type_id);
  112. value_rep.kind) {
  113. case SemIR::ValueRepr::Unknown:
  114. CARBON_FATAL()
  115. << "Incomplete parameter type lowering function declaration";
  116. case SemIR::ValueRepr::None:
  117. break;
  118. case SemIR::ValueRepr::Copy:
  119. case SemIR::ValueRepr::Custom:
  120. case SemIR::ValueRepr::Pointer:
  121. param_types.push_back(GetType(value_rep.type_id));
  122. param_inst_ids.push_back(param_ref_id);
  123. break;
  124. }
  125. }
  126. // If the initializing representation doesn't produce a value, set the return
  127. // type to void.
  128. llvm::Type* return_type = return_rep.kind == SemIR::InitRepr::ByCopy
  129. ? GetType(function.return_type_id)
  130. : llvm::Type::getVoidTy(llvm_context());
  131. std::string mangled_name;
  132. if (SemIR::IsEntryPoint(sem_ir(), function_id)) {
  133. // TODO: Add an implicit `return 0` if `Run` doesn't return `i32`.
  134. mangled_name = "main";
  135. } else if (auto name =
  136. sem_ir().names().GetAsStringIfIdentifier(function.name_id)) {
  137. // TODO: Decide on a name mangling scheme.
  138. mangled_name = *name;
  139. } else {
  140. CARBON_FATAL() << "Unexpected special name for function: "
  141. << function.name_id;
  142. }
  143. llvm::FunctionType* function_type =
  144. llvm::FunctionType::get(return_type, param_types, /*isVarArg=*/false);
  145. auto* llvm_function =
  146. llvm::Function::Create(function_type, llvm::Function::ExternalLinkage,
  147. mangled_name, llvm_module());
  148. // Set up parameters and the return slot.
  149. for (auto [inst_id, arg] :
  150. llvm::zip_equal(param_inst_ids, llvm_function->args())) {
  151. auto name_id = SemIR::NameId::Invalid;
  152. if (inst_id == function.return_slot_id) {
  153. name_id = SemIR::NameId::ReturnSlot;
  154. arg.addAttr(llvm::Attribute::getWithStructRetType(
  155. llvm_context(), GetType(function.return_type_id)));
  156. } else {
  157. name_id = SemIR::Function::GetParamFromParamRefId(sem_ir(), inst_id)
  158. .second.name_id;
  159. }
  160. arg.setName(sem_ir().names().GetIRBaseName(name_id));
  161. }
  162. return llvm_function;
  163. }
  164. auto FileContext::BuildFunctionDefinition(SemIR::FunctionId function_id)
  165. -> void {
  166. const auto& function = sem_ir().functions().Get(function_id);
  167. const auto& body_block_ids = function.body_block_ids;
  168. if (body_block_ids.empty()) {
  169. // Function is probably defined in another file; not an error.
  170. return;
  171. }
  172. llvm::Function* llvm_function = GetFunction(function_id);
  173. FunctionContext function_lowering(*this, llvm_function, vlog_stream_);
  174. const bool has_return_slot = function.return_slot_id.is_valid();
  175. // Add parameters to locals.
  176. // TODO: This duplicates the mapping between sem_ir instructions and LLVM
  177. // function parameters that was already computed in BuildFunctionDecl.
  178. // We should only do that once.
  179. auto implicit_param_refs =
  180. sem_ir().inst_blocks().Get(function.implicit_param_refs_id);
  181. auto param_refs = sem_ir().inst_blocks().Get(function.param_refs_id);
  182. int param_index = 0;
  183. if (has_return_slot) {
  184. function_lowering.SetLocal(function.return_slot_id,
  185. llvm_function->getArg(param_index));
  186. ++param_index;
  187. }
  188. for (auto param_ref_id :
  189. llvm::concat<const SemIR::InstId>(implicit_param_refs, param_refs)) {
  190. auto [param_id, param] =
  191. SemIR::Function::GetParamFromParamRefId(sem_ir(), param_ref_id);
  192. // Get the value of the parameter from the function argument.
  193. auto param_type_id = param.type_id;
  194. llvm::Value* param_value = llvm::PoisonValue::get(GetType(param_type_id));
  195. if (SemIR::GetValueRepr(sem_ir(), param_type_id).kind !=
  196. SemIR::ValueRepr::None) {
  197. param_value = llvm_function->getArg(param_index);
  198. ++param_index;
  199. }
  200. // The value of the parameter is the value of the argument.
  201. function_lowering.SetLocal(param_id, param_value);
  202. // Match the portion of the pattern corresponding to the parameter against
  203. // the parameter value. For now this is always a single name binding,
  204. // possibly wrapped in `addr`.
  205. //
  206. // TODO: Support general patterns here.
  207. auto bind_name_id = param_ref_id;
  208. if (auto addr =
  209. sem_ir().insts().TryGetAs<SemIR::AddrPattern>(param_ref_id)) {
  210. bind_name_id = addr->inner_id;
  211. }
  212. auto bind_name = sem_ir().insts().Get(bind_name_id);
  213. // TODO: Should we stop passing compile-time bindings at runtime?
  214. CARBON_CHECK(bind_name.Is<SemIR::AnyBindName>());
  215. function_lowering.SetLocal(bind_name_id, param_value);
  216. }
  217. // Lower all blocks.
  218. for (auto block_id : body_block_ids) {
  219. CARBON_VLOG() << "Lowering " << block_id << "\n";
  220. auto* llvm_block = function_lowering.GetBlock(block_id);
  221. // Keep the LLVM blocks in lexical order.
  222. llvm_block->moveBefore(llvm_function->end());
  223. function_lowering.builder().SetInsertPoint(llvm_block);
  224. function_lowering.LowerBlock(block_id);
  225. }
  226. // LLVM requires that the entry block has no predecessors.
  227. auto* entry_block = &llvm_function->getEntryBlock();
  228. if (entry_block->hasNPredecessorsOrMore(1)) {
  229. auto* new_entry_block = llvm::BasicBlock::Create(
  230. llvm_context(), "entry", llvm_function, entry_block);
  231. llvm::BranchInst::Create(entry_block, new_entry_block);
  232. }
  233. }
  234. auto FileContext::BuildType(SemIR::InstId inst_id) -> llvm::Type* {
  235. switch (inst_id.index) {
  236. case SemIR::BuiltinKind::FloatType.AsInt():
  237. // TODO: Handle different sizes.
  238. return llvm::Type::getDoubleTy(*llvm_context_);
  239. case SemIR::BuiltinKind::IntType.AsInt():
  240. // TODO: Handle different sizes.
  241. return llvm::Type::getInt32Ty(*llvm_context_);
  242. case SemIR::BuiltinKind::BoolType.AsInt():
  243. // TODO: We may want to have different representations for `bool` storage
  244. // (`i8`) versus for `bool` values (`i1`).
  245. return llvm::Type::getInt1Ty(*llvm_context_);
  246. case SemIR::BuiltinKind::FunctionType.AsInt():
  247. case SemIR::BuiltinKind::BoundMethodType.AsInt():
  248. case SemIR::BuiltinKind::NamespaceType.AsInt():
  249. case SemIR::BuiltinKind::WitnessType.AsInt():
  250. // Return an empty struct as a placeholder.
  251. return llvm::StructType::get(*llvm_context_);
  252. default:
  253. // Handled below.
  254. break;
  255. }
  256. auto inst = sem_ir_->insts().Get(inst_id);
  257. switch (inst.kind()) {
  258. case SemIR::ArrayType::Kind: {
  259. auto array_type = inst.As<SemIR::ArrayType>();
  260. return llvm::ArrayType::get(
  261. GetType(array_type.element_type_id),
  262. sem_ir_->GetArrayBoundValue(array_type.bound_id));
  263. }
  264. case SemIR::AssociatedEntityType::Kind:
  265. // No runtime operations are provided on an associated entity name, so use
  266. // an empty representation.
  267. return llvm::StructType::get(*llvm_context_);
  268. case SemIR::BindSymbolicName::Kind:
  269. // Treat non-monomorphized type bindings as opaque.
  270. return llvm::StructType::get(*llvm_context_);
  271. case SemIR::ClassType::Kind: {
  272. auto object_repr_id = sem_ir_->classes()
  273. .Get(inst.As<SemIR::ClassType>().class_id)
  274. .object_repr_id;
  275. return GetType(object_repr_id);
  276. }
  277. case SemIR::ConstType::Kind:
  278. return GetType(inst.As<SemIR::ConstType>().inner_id);
  279. case SemIR::InterfaceType::Kind:
  280. // Return an empty struct as a placeholder.
  281. // TODO: Should we model an interface as a witness table?
  282. return llvm::StructType::get(*llvm_context_);
  283. case SemIR::PointerType::Kind:
  284. return llvm::PointerType::get(*llvm_context_, /*AddressSpace=*/0);
  285. case SemIR::StructType::Kind: {
  286. auto fields =
  287. sem_ir_->inst_blocks().Get(inst.As<SemIR::StructType>().fields_id);
  288. llvm::SmallVector<llvm::Type*> subtypes;
  289. subtypes.reserve(fields.size());
  290. for (auto field_id : fields) {
  291. auto field = sem_ir_->insts().GetAs<SemIR::StructTypeField>(field_id);
  292. subtypes.push_back(GetType(field.field_type_id));
  293. }
  294. return llvm::StructType::get(*llvm_context_, subtypes);
  295. }
  296. case SemIR::TupleType::Kind: {
  297. // TODO: Investigate special-casing handling of empty tuples so that they
  298. // can be collectively replaced with LLVM's void, particularly around
  299. // function returns. LLVM doesn't allow declaring variables with a void
  300. // type, so that may require significant special casing.
  301. auto elements =
  302. sem_ir_->type_blocks().Get(inst.As<SemIR::TupleType>().elements_id);
  303. llvm::SmallVector<llvm::Type*> subtypes;
  304. subtypes.reserve(elements.size());
  305. for (auto element_id : elements) {
  306. subtypes.push_back(GetType(element_id));
  307. }
  308. return llvm::StructType::get(*llvm_context_, subtypes);
  309. }
  310. case SemIR::UnboundElementType::Kind: {
  311. // Return an empty struct as a placeholder.
  312. return llvm::StructType::get(*llvm_context_);
  313. }
  314. default: {
  315. CARBON_FATAL() << "Cannot use inst as type: " << inst_id << " " << inst;
  316. }
  317. }
  318. }
  319. } // namespace Carbon::Lower