file_context.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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 <memory>
  6. #include <optional>
  7. #include <string>
  8. #include <utility>
  9. #include "clang/CodeGen/ModuleBuilder.h"
  10. #include "common/check.h"
  11. #include "common/pretty_stack_trace_function.h"
  12. #include "common/vlog.h"
  13. #include "llvm/ADT/STLExtras.h"
  14. #include "llvm/ADT/Sequence.h"
  15. #include "llvm/Linker/Linker.h"
  16. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  17. #include "llvm/Transforms/Utils/ModuleUtils.h"
  18. #include "toolchain/base/kind_switch.h"
  19. #include "toolchain/lower/constant.h"
  20. #include "toolchain/lower/function_context.h"
  21. #include "toolchain/lower/mangler.h"
  22. #include "toolchain/lower/specific_coalescer.h"
  23. #include "toolchain/sem_ir/absolute_node_id.h"
  24. #include "toolchain/sem_ir/diagnostic_loc_converter.h"
  25. #include "toolchain/sem_ir/entry_point.h"
  26. #include "toolchain/sem_ir/expr_info.h"
  27. #include "toolchain/sem_ir/file.h"
  28. #include "toolchain/sem_ir/function.h"
  29. #include "toolchain/sem_ir/generic.h"
  30. #include "toolchain/sem_ir/ids.h"
  31. #include "toolchain/sem_ir/inst.h"
  32. #include "toolchain/sem_ir/inst_categories.h"
  33. #include "toolchain/sem_ir/inst_kind.h"
  34. #include "toolchain/sem_ir/pattern.h"
  35. #include "toolchain/sem_ir/stringify.h"
  36. #include "toolchain/sem_ir/typed_insts.h"
  37. namespace Carbon::Lower {
  38. FileContext::FileContext(Context& context, const SemIR::File& sem_ir,
  39. const SemIR::InstNamer* inst_namer,
  40. llvm::raw_ostream* vlog_stream)
  41. : context_(&context),
  42. sem_ir_(&sem_ir),
  43. inst_namer_(inst_namer),
  44. vlog_stream_(vlog_stream),
  45. functions_(LoweredFunctionStore::MakeForOverwrite(sem_ir.functions())),
  46. specific_functions_(sem_ir.specifics(), nullptr),
  47. types_(LoweredTypeStore::MakeWithExplicitSize(sem_ir.insts().size(),
  48. nullptr)),
  49. constants_(LoweredConstantStore::MakeWithExplicitSize(
  50. sem_ir.insts().size(), nullptr)),
  51. lowered_specifics_(sem_ir.generics(),
  52. llvm::SmallVector<SemIR::SpecificId>()),
  53. coalescer_(vlog_stream_, sem_ir.specifics()),
  54. vtables_(decltype(vtables_)::MakeForOverwrite(sem_ir.vtables())),
  55. specific_vtables_(sem_ir.specifics(), nullptr) {
  56. // Initialization that relies on invariants of the class.
  57. cpp_code_generator_ = CreateCppCodeGenerator();
  58. CARBON_CHECK(!sem_ir.has_errors(),
  59. "Generating LLVM IR from invalid SemIR::File is unsupported.");
  60. }
  61. // TODO: Move this to lower.cpp.
  62. auto FileContext::PrepareToLower() -> void {
  63. if (cpp_code_generator_) {
  64. // Clang code generation should not actually modify the AST, but isn't
  65. // const-correct.
  66. cpp_code_generator_->Initialize(
  67. const_cast<clang::ASTContext&>(cpp_ast()->getASTContext()));
  68. }
  69. // Lower all types that were required to be complete.
  70. for (auto type_id : sem_ir_->types().complete_types()) {
  71. if (type_id.index >= 0) {
  72. types_.Set(type_id, BuildType(sem_ir_->types().GetInstId(type_id)));
  73. }
  74. }
  75. // Lower function declarations.
  76. for (auto [id, _] : sem_ir_->functions().enumerate()) {
  77. functions_.Set(id, BuildFunctionDecl(id));
  78. }
  79. // TODO: Split vtable declaration creation from definition creation to avoid
  80. // redundant vtable definitions for imported vtables.
  81. for (const auto& [id, vtable] : sem_ir_->vtables().enumerate()) {
  82. const auto& class_info = sem_ir().classes().Get(vtable.class_id);
  83. // Vtables can't be generated for generics, only for their specifics - and
  84. // must be done lazily based on the use of those specifics.
  85. if (!class_info.generic_id.has_value()) {
  86. vtables_.Set(id, BuildVtable(vtable, SemIR::SpecificId::None));
  87. }
  88. }
  89. // Lower constants.
  90. LowerConstants(*this, constants_);
  91. }
  92. // TODO: Move this to lower.cpp.
  93. auto FileContext::LowerDefinitions() -> void {
  94. // Lower global variable definitions.
  95. // TODO: Storing both a `constants_` array and a separate `global_variables_`
  96. // map is redundant.
  97. for (auto inst_id :
  98. sem_ir().inst_blocks().Get(sem_ir().top_inst_block_id())) {
  99. // Only `VarStorage` indicates a global variable declaration in the
  100. // top instruction block.
  101. if (auto var = sem_ir().insts().TryGetAs<SemIR::VarStorage>(inst_id)) {
  102. // Get the global variable declaration. We created this when lowering the
  103. // constant unless the variable is unnamed, in which case we need to
  104. // create it now.
  105. llvm::GlobalVariable* llvm_var = nullptr;
  106. if (auto const_id = sem_ir().constant_values().Get(inst_id);
  107. const_id.is_constant()) {
  108. llvm_var = cast<llvm::GlobalVariable>(GetConstant(const_id, inst_id));
  109. } else {
  110. llvm_var = BuildGlobalVariableDecl(*var);
  111. }
  112. // Convert the declaration of this variable into a definition by adding an
  113. // initializer.
  114. global_variables_.Insert(inst_id, llvm_var);
  115. llvm_var->setInitializer(
  116. llvm::Constant::getNullValue(llvm_var->getValueType()));
  117. }
  118. }
  119. // Lower function definitions.
  120. for (auto [id, fn_info] : sem_ir_->functions().enumerate()) {
  121. // If we created a declaration and the function definition is not imported,
  122. // build a definition.
  123. if (functions_.Get(id) && fn_info.definition_id.has_value() &&
  124. !sem_ir().insts().GetImportSource(fn_info.definition_id).has_value()) {
  125. BuildFunctionDefinition(id);
  126. }
  127. }
  128. // Append `__global_init` to `llvm::global_ctors` to initialize global
  129. // variables.
  130. if (auto global_ctor_id = sem_ir().global_ctor_id();
  131. global_ctor_id.has_value()) {
  132. const auto& global_ctor = sem_ir().functions().Get(global_ctor_id);
  133. BuildFunctionBody(global_ctor_id, SemIR::SpecificId::None, global_ctor,
  134. *this, global_ctor);
  135. llvm::appendToGlobalCtors(llvm_module(),
  136. GetFunction(sem_ir().global_ctor_id()),
  137. /*Priority=*/0);
  138. }
  139. }
  140. auto FileContext::Finalize() -> void {
  141. if (cpp_code_generator_) {
  142. // Clang code generation should not actually modify the AST, but isn't
  143. // const-correct.
  144. cpp_code_generator_->HandleTranslationUnit(
  145. const_cast<clang::ASTContext&>(cpp_ast()->getASTContext()));
  146. bool link_error = llvm::Linker::linkModules(
  147. /*Dest=*/llvm_module(),
  148. /*Src=*/std::unique_ptr<llvm::Module>(
  149. cpp_code_generator_->ReleaseModule()));
  150. CARBON_CHECK(!link_error);
  151. }
  152. // Find equivalent specifics (from the same generic), replace all uses and
  153. // remove duplicately lowered function definitions.
  154. coalescer_.CoalesceEquivalentSpecifics(lowered_specifics_,
  155. specific_functions_);
  156. }
  157. auto FileContext::CreateCppCodeGenerator()
  158. -> std::unique_ptr<clang::CodeGenerator> {
  159. if (!cpp_ast()) {
  160. return nullptr;
  161. }
  162. RawStringOstream clang_module_name_stream;
  163. clang_module_name_stream << llvm_module().getName() << ".clang";
  164. // Do not emit Clang's name and version as the creator of the output file.
  165. cpp_code_gen_options_.EmitVersionIdentMetadata = false;
  166. return std::unique_ptr<clang::CodeGenerator>(clang::CreateLLVMCodeGen(
  167. cpp_ast()->getASTContext().getDiagnostics(),
  168. clang_module_name_stream.TakeStr(), context().file_system(),
  169. cpp_header_search_options_, cpp_preprocessor_options_,
  170. cpp_code_gen_options_, llvm_context()));
  171. }
  172. auto FileContext::GetConstant(SemIR::ConstantId const_id,
  173. SemIR::InstId use_inst_id) -> llvm::Value* {
  174. auto const_inst_id = sem_ir().constant_values().GetInstId(const_id);
  175. auto* const_value = constants_.Get(const_inst_id);
  176. // For value expressions and initializing expressions, the value produced by
  177. // a constant instruction is a value representation of the constant. For
  178. // initializing expressions, `FinishInit` will perform a copy if needed.
  179. switch (auto cat = SemIR::GetExprCategory(sem_ir(), const_inst_id)) {
  180. case SemIR::ExprCategory::Value:
  181. case SemIR::ExprCategory::Initializing:
  182. break;
  183. case SemIR::ExprCategory::DurableRef:
  184. case SemIR::ExprCategory::EphemeralRef:
  185. // Constant reference expressions lower to an address.
  186. return const_value;
  187. case SemIR::ExprCategory::NotExpr:
  188. case SemIR::ExprCategory::Error:
  189. case SemIR::ExprCategory::Mixed:
  190. CARBON_FATAL("Unexpected category {0} for lowered constant {1}", cat,
  191. sem_ir().insts().Get(const_inst_id));
  192. };
  193. auto value_rep = SemIR::ValueRepr::ForType(
  194. sem_ir(), sem_ir().insts().Get(const_inst_id).type_id());
  195. if (value_rep.kind != SemIR::ValueRepr::Pointer) {
  196. return const_value;
  197. }
  198. // The value representation is a pointer. Generate a variable to hold the
  199. // value, or find and reuse an existing one.
  200. if (auto result = global_variables().Lookup(const_inst_id)) {
  201. return result.value();
  202. }
  203. // Include both the name of the constant, if any, and the point of use in
  204. // the name of the variable.
  205. llvm::StringRef const_name;
  206. llvm::StringRef use_name;
  207. if (inst_namer_) {
  208. const_name = inst_namer_->GetUnscopedNameFor(const_inst_id);
  209. if (use_inst_id.has_value()) {
  210. use_name = inst_namer_->GetUnscopedNameFor(use_inst_id);
  211. }
  212. }
  213. // We always need to give the global a name even if the instruction namer
  214. // doesn't have one to use.
  215. if (const_name.empty()) {
  216. const_name = "const";
  217. }
  218. if (use_name.empty()) {
  219. use_name = "anon";
  220. }
  221. llvm::StringRef sep = (use_name[0] == '.') ? "" : ".";
  222. auto* global_variable = new llvm::GlobalVariable(
  223. llvm_module(), GetType(sem_ir().GetPointeeType(value_rep.type_id)),
  224. /*isConstant=*/true, llvm::GlobalVariable::InternalLinkage, const_value,
  225. const_name + sep + use_name);
  226. global_variables_.Insert(const_inst_id, global_variable);
  227. return global_variable;
  228. }
  229. auto FileContext::GetOrCreateFunction(SemIR::FunctionId function_id,
  230. SemIR::SpecificId specific_id)
  231. -> llvm::Function* {
  232. // If we have already lowered a declaration of this function, just return it.
  233. auto** result = GetFunctionAddr(function_id, specific_id);
  234. if (!*result) {
  235. *result = BuildFunctionDecl(function_id, specific_id);
  236. }
  237. return *result;
  238. }
  239. auto FileContext::BuildFunctionTypeInfo(const SemIR::Function& function,
  240. SemIR::SpecificId specific_id)
  241. -> FunctionTypeInfo {
  242. const auto return_info =
  243. SemIR::ReturnTypeInfo::ForFunction(sem_ir(), function, specific_id);
  244. if (!return_info.is_valid()) {
  245. // The return type has not been completed, create a trivial type instead.
  246. return {.type =
  247. llvm::FunctionType::get(llvm::Type::getVoidTy(llvm_context()),
  248. /*isVarArg=*/false)};
  249. }
  250. auto get_llvm_type = [&](SemIR::TypeId type_id) -> llvm::Type* {
  251. if (!type_id.has_value()) {
  252. return nullptr;
  253. }
  254. return GetType(type_id);
  255. };
  256. // TODO: expose the `Call` parameter patterns in `Function`, and use them here
  257. // instead of reconstructing them via the syntactic parameter lists.
  258. auto implicit_param_patterns =
  259. sem_ir().inst_blocks().GetOrEmpty(function.implicit_param_patterns_id);
  260. auto param_patterns =
  261. sem_ir().inst_blocks().GetOrEmpty(function.param_patterns_id);
  262. auto* return_type = get_llvm_type(return_info.type_id);
  263. llvm::SmallVector<llvm::Type*> param_types;
  264. // Compute the return type to use for the LLVM function. If the initializing
  265. // representation doesn't produce a value, set the return type to void.
  266. // TODO: For the `Run` entry point, remap return type to i32 if it doesn't
  267. // return a value.
  268. llvm::Type* function_return_type =
  269. (return_info.is_valid() &&
  270. return_info.init_repr.kind == SemIR::InitRepr::ByCopy)
  271. ? return_type
  272. : llvm::Type::getVoidTy(llvm_context());
  273. // TODO: Consider either storing `param_inst_ids` somewhere so that we can
  274. // reuse it from `BuildFunctionDefinition` and when building calls, or factor
  275. // out a mechanism to compute the mapping between parameters and arguments on
  276. // demand.
  277. llvm::SmallVector<SemIR::InstId> param_inst_ids;
  278. auto max_llvm_params = (return_info.has_return_slot() ? 1 : 0) +
  279. implicit_param_patterns.size() + param_patterns.size();
  280. param_types.reserve(max_llvm_params);
  281. param_inst_ids.reserve(max_llvm_params);
  282. auto return_param_id = SemIR::InstId::None;
  283. if (return_info.has_return_slot()) {
  284. param_types.push_back(
  285. llvm::PointerType::get(llvm_context(), /*AddressSpace=*/0));
  286. return_param_id = function.return_slot_pattern_id;
  287. param_inst_ids.push_back(return_param_id);
  288. }
  289. for (auto param_pattern_id : llvm::concat<const SemIR::InstId>(
  290. implicit_param_patterns, param_patterns)) {
  291. auto param_pattern_info = SemIR::Function::GetParamPatternInfoFromPatternId(
  292. sem_ir(), param_pattern_id);
  293. if (!param_pattern_info) {
  294. continue;
  295. }
  296. auto param_type_id = ExtractScrutineeType(
  297. sem_ir(), SemIR::GetTypeOfInstInSpecific(sem_ir(), specific_id,
  298. param_pattern_info->inst_id));
  299. CARBON_CHECK(
  300. !param_type_id.AsConstantId().is_symbolic(),
  301. "Found symbolic type id after resolution when lowering type {0}.",
  302. param_pattern_info->inst.type_id);
  303. switch (auto value_rep = SemIR::ValueRepr::ForType(sem_ir(), param_type_id);
  304. value_rep.kind) {
  305. case SemIR::ValueRepr::Unknown:
  306. // This parameter type is incomplete. Fallback to describing the
  307. // function type as `void()`.
  308. return {.type = llvm::FunctionType::get(
  309. llvm::Type::getVoidTy(llvm_context()),
  310. /*isVarArg=*/false)};
  311. case SemIR::ValueRepr::None:
  312. break;
  313. case SemIR::ValueRepr::Copy:
  314. case SemIR::ValueRepr::Custom:
  315. case SemIR::ValueRepr::Pointer:
  316. auto* param_types_to_add = get_llvm_type(value_rep.type_id);
  317. param_types.push_back(param_types_to_add);
  318. param_inst_ids.push_back(param_pattern_id);
  319. break;
  320. }
  321. }
  322. return {.type = llvm::FunctionType::get(function_return_type, param_types,
  323. /*isVarArg=*/false),
  324. .param_inst_ids = std::move(param_inst_ids),
  325. .return_type = return_type,
  326. .return_param_id = return_param_id};
  327. }
  328. auto FileContext::HandleReferencedCppFunction(clang::FunctionDecl* cpp_decl)
  329. -> void {
  330. // TODO: To support recursive inline functions, collect all calls to
  331. // `HandleTopLevelDecl()` in a custom `ASTConsumer` configured in the
  332. // `ASTUnit`, and replay them in lowering in the `CodeGenerator`. See
  333. // https://discord.com/channels/655572317891461132/768530752592805919/1370509111585935443
  334. clang::FunctionDecl* cpp_def = cpp_decl->getDefinition();
  335. if (!cpp_def) {
  336. return;
  337. }
  338. // Create the LLVM function (`CodeGenModule::GetOrCreateLLVMFunction()`)
  339. // so that code generation (`CodeGenModule::EmitGlobal()`) would see this
  340. // function name (`CodeGenModule::getMangledName()`), and will generate
  341. // its definition.
  342. llvm::Constant* function_address =
  343. cpp_code_generator_->GetAddrOfGlobal(clang::GlobalDecl(cpp_def),
  344. /*isForDefinition=*/false);
  345. CARBON_CHECK(function_address);
  346. // Emit the function code.
  347. cpp_code_generator_->HandleTopLevelDecl(clang::DeclGroupRef(cpp_def));
  348. }
  349. auto FileContext::HandleReferencedSpecificFunction(
  350. SemIR::FunctionId function_id, SemIR::SpecificId specific_id,
  351. llvm::Type* llvm_type) -> void {
  352. CARBON_CHECK(specific_id.has_value());
  353. // Add this specific function to a list of specific functions whose
  354. // definitions we need to emit.
  355. // TODO: Don't do this if we know this function is emitted as a
  356. // non-discardable symbol in the IR for some other file.
  357. context().AddPendingSpecificFunctionDefinition({.context = this,
  358. .function_id = function_id,
  359. .specific_id = specific_id});
  360. // Create a unique fingerprint for the function type.
  361. // For now, we compute the function type fingerprint only for specifics,
  362. // though we might need it for all functions in order to create a canonical
  363. // fingerprint across translation units.
  364. coalescer_.CreateTypeFingerprint(specific_id, llvm_type);
  365. }
  366. auto FileContext::BuildFunctionDecl(SemIR::FunctionId function_id,
  367. SemIR::SpecificId specific_id)
  368. -> llvm::Function* {
  369. const auto& function = sem_ir().functions().Get(function_id);
  370. // Don't lower generic functions. Note that associated functions in interfaces
  371. // have `Self` in scope, so are implicitly generic functions.
  372. if (function.generic_id.has_value() && !specific_id.has_value()) {
  373. return nullptr;
  374. }
  375. // Don't lower builtins.
  376. if (function.builtin_function_kind() != SemIR::BuiltinFunctionKind::None) {
  377. return nullptr;
  378. }
  379. // TODO: Consider tracking whether the function has been used, and only
  380. // lowering it if it's needed.
  381. auto function_type_info = BuildFunctionTypeInfo(function, specific_id);
  382. // TODO: For an imported inline function, consider generating an
  383. // `available_externally` definition.
  384. auto linkage = specific_id.has_value() ? llvm::Function::LinkOnceODRLinkage
  385. : llvm::Function::ExternalLinkage;
  386. Mangler m(*this);
  387. std::string mangled_name = m.Mangle(function_id, specific_id);
  388. if (auto* existing = llvm_module().getFunction(mangled_name)) {
  389. // We might have already lowered this function while lowering a different
  390. // file. That's OK.
  391. // TODO: Check-fail or maybe diagnose if the two LLVM functions are not
  392. // produced by declarations of the same Carbon function. Name collisions
  393. // between non-private members of the same library should have been
  394. // diagnosed by check if detected, but it's not clear that check will always
  395. // be able to see this problem. In theory, name collisions could also occur
  396. // due to fingerprint collision.
  397. return existing;
  398. }
  399. // If this is a C++ function, tell Clang that we referenced it.
  400. if (auto clang_decl_id = sem_ir().functions().Get(function_id).clang_decl_id;
  401. clang_decl_id.has_value()) {
  402. CARBON_CHECK(!specific_id.has_value(),
  403. "Specific functions cannot have C++ definitions");
  404. HandleReferencedCppFunction(
  405. sem_ir().clang_decls().Get(clang_decl_id).decl->getAsFunction());
  406. // TODO: Check that the signature and mangling generated by Clang and the
  407. // one we generated are the same.
  408. }
  409. // If this is a specific function, we may need to do additional work to emit
  410. // its definition.
  411. if (specific_id.has_value()) {
  412. HandleReferencedSpecificFunction(function_id, specific_id,
  413. function_type_info.type);
  414. }
  415. auto* llvm_function = llvm::Function::Create(function_type_info.type, linkage,
  416. mangled_name, llvm_module());
  417. CARBON_CHECK(llvm_function->getName() == mangled_name,
  418. "Mangled name collision: {0}", mangled_name);
  419. // Set up parameters and the return slot.
  420. for (auto [inst_id, arg] : llvm::zip_equal(function_type_info.param_inst_ids,
  421. llvm_function->args())) {
  422. auto name_id = SemIR::NameId::None;
  423. if (inst_id == function_type_info.return_param_id) {
  424. name_id = SemIR::NameId::ReturnSlot;
  425. arg.addAttr(llvm::Attribute::getWithStructRetType(
  426. llvm_context(), function_type_info.return_type));
  427. } else {
  428. name_id = SemIR::GetPrettyNameFromPatternId(sem_ir(), inst_id);
  429. }
  430. arg.setName(sem_ir().names().GetIRBaseName(name_id));
  431. }
  432. return llvm_function;
  433. }
  434. // Find the file and function ID describing the definition of a function.
  435. static auto GetFunctionDefinition(const SemIR::File* decl_ir,
  436. SemIR::FunctionId function_id)
  437. -> std::pair<const SemIR::File*, SemIR::FunctionId> {
  438. // Find the file containing the definition.
  439. auto decl_id = decl_ir->functions().Get(function_id).definition_id;
  440. if (!decl_id.has_value()) {
  441. // Function is not defined.
  442. return {nullptr, SemIR::FunctionId::None};
  443. }
  444. // Find the function declaration this function was originally imported from.
  445. while (true) {
  446. auto import_inst_id = decl_ir->insts().GetImportSource(decl_id);
  447. if (!import_inst_id.has_value()) {
  448. break;
  449. }
  450. auto import_inst = decl_ir->import_ir_insts().Get(import_inst_id);
  451. decl_ir = decl_ir->import_irs().Get(import_inst.ir_id()).sem_ir;
  452. decl_id = import_inst.inst_id();
  453. }
  454. auto decl_ir_function_id =
  455. decl_ir->insts().GetAs<SemIR::FunctionDecl>(decl_id).function_id;
  456. return {decl_ir, decl_ir_function_id};
  457. }
  458. auto FileContext::BuildFunctionDefinition(SemIR::FunctionId function_id,
  459. SemIR::SpecificId specific_id)
  460. -> void {
  461. auto [definition_ir, definition_ir_function_id] =
  462. GetFunctionDefinition(&sem_ir(), function_id);
  463. if (!definition_ir) {
  464. // Function is probably defined in another file; not an error.
  465. return;
  466. }
  467. const auto& definition_function =
  468. definition_ir->functions().Get(definition_ir_function_id);
  469. BuildFunctionBody(
  470. function_id, specific_id, sem_ir().functions().Get(function_id),
  471. context().GetFileContext(definition_ir), definition_function);
  472. }
  473. auto FileContext::BuildFunctionBody(SemIR::FunctionId function_id,
  474. SemIR::SpecificId specific_id,
  475. const SemIR::Function& declaration_function,
  476. FileContext& definition_context,
  477. const SemIR::Function& definition_function)
  478. -> void {
  479. // On crash, report the function we were lowering.
  480. PrettyStackTraceFunction stack_trace_entry([&](llvm::raw_ostream& output) {
  481. SemIR::DiagnosticLocConverter converter(
  482. &context().tree_and_subtrees_getters(), &sem_ir());
  483. auto converted =
  484. converter.Convert(SemIR::LocId(declaration_function.definition_id),
  485. /*token_only=*/false);
  486. converted.loc.FormatLocation(output);
  487. output << "Lowering function ";
  488. if (specific_id.has_value()) {
  489. output << SemIR::StringifySpecific(sem_ir(), specific_id);
  490. } else {
  491. output << SemIR::StringifyConstantInst(
  492. sem_ir(), declaration_function.definition_id);
  493. }
  494. output << "\n";
  495. // Crash output has a tab indent; try to indent slightly past that.
  496. converted.loc.FormatSnippet(output, /*indent=*/10);
  497. });
  498. // Note that `definition_function` is potentially from a different SemIR::File
  499. // than the one that this file context represents. Any lowering done for
  500. // values derived from `definition_function` should use `definition_context`
  501. // instead of our context.
  502. const auto& definition_ir = definition_context.sem_ir();
  503. auto* llvm_function = GetFunction(function_id, specific_id);
  504. CARBON_CHECK(llvm_function,
  505. "Attempting to define function that was not declared");
  506. const auto& body_block_ids = definition_function.body_block_ids;
  507. CARBON_DCHECK(!body_block_ids.empty(),
  508. "No function body blocks found during lowering.");
  509. // Store which specifics were already lowered (with definitions) for each
  510. // generic.
  511. if (declaration_function.generic_id.has_value() && specific_id.has_value()) {
  512. // TODO: We should track this in the definition context instead so that we
  513. // can deduplicate specifics from different files.
  514. AddLoweredSpecificForGeneric(declaration_function.generic_id, specific_id);
  515. }
  516. FunctionContext function_lowering(
  517. definition_context, llvm_function, *this, specific_id,
  518. coalescer_.InitializeFingerprintForSpecific(specific_id),
  519. definition_context.BuildDISubprogram(definition_function, llvm_function),
  520. vlog_stream_);
  521. // Add parameters to locals.
  522. // TODO: This duplicates the mapping between sem_ir instructions and LLVM
  523. // function parameters that was already computed in BuildFunctionDecl.
  524. // We should only do that once.
  525. auto call_param_ids = definition_ir.inst_blocks().GetOrEmpty(
  526. definition_function.call_params_id);
  527. int param_index = 0;
  528. // TODO: Find a way to ensure this code and the function-call lowering use
  529. // the same parameter ordering.
  530. // Lowers the given parameter. Must be called in LLVM calling convention
  531. // parameter order.
  532. auto lower_param = [&](SemIR::InstId param_id) {
  533. // Get the value of the parameter from the function argument.
  534. llvm::Value* param_value;
  535. // The `type_id` of a parameter tracks the parameter's type.
  536. CARBON_CHECK(definition_ir.insts().Is<SemIR::AnyParam>(param_id));
  537. auto param_type = function_lowering.GetTypeIdOfInst(param_id);
  538. if (function_lowering.GetValueRepr(param_type).repr.kind !=
  539. SemIR::ValueRepr::None) {
  540. param_value = llvm_function->getArg(param_index);
  541. ++param_index;
  542. } else {
  543. param_value =
  544. llvm::PoisonValue::get(function_lowering.GetType(param_type));
  545. }
  546. // The value of the parameter is the value of the argument.
  547. function_lowering.SetLocal(param_id, param_value);
  548. };
  549. // The subset of call_param_ids that is already in the order that the LLVM
  550. // calling convention expects.
  551. llvm::ArrayRef<SemIR::InstId> sequential_param_ids;
  552. if (declaration_function.return_slot_pattern_id.has_value()) {
  553. // The LLVM calling convention has the return slot first rather than last.
  554. // Note that this queries whether there is a return slot at the LLVM level,
  555. // whereas `function.return_slot_pattern_id.has_value()` queries whether
  556. // there is a return slot at the SemIR level.
  557. if (SemIR::ReturnTypeInfo::ForFunction(sem_ir(), declaration_function,
  558. specific_id)
  559. .has_return_slot()) {
  560. lower_param(call_param_ids.back());
  561. }
  562. sequential_param_ids = call_param_ids.drop_back();
  563. } else {
  564. sequential_param_ids = call_param_ids;
  565. }
  566. for (auto param_id : sequential_param_ids) {
  567. lower_param(param_id);
  568. }
  569. auto decl_block_id = SemIR::InstBlockId::None;
  570. if (function_id == sem_ir().global_ctor_id()) {
  571. decl_block_id = SemIR::InstBlockId::Empty;
  572. } else {
  573. decl_block_id =
  574. definition_ir.insts()
  575. .GetAs<SemIR::FunctionDecl>(definition_function.latest_decl_id())
  576. .decl_block_id;
  577. }
  578. // Lowers the contents of decl_block_id into the corresponding LLVM block,
  579. // creating it if it doesn't already exist.
  580. auto lower_block = [&](SemIR::InstBlockId block_id) {
  581. CARBON_VLOG("Lowering {0}\n", block_id);
  582. auto* llvm_block = function_lowering.GetBlock(block_id);
  583. // Keep the LLVM blocks in lexical order.
  584. llvm_block->moveBefore(llvm_function->end());
  585. function_lowering.builder().SetInsertPoint(llvm_block);
  586. function_lowering.LowerBlockContents(block_id);
  587. };
  588. lower_block(decl_block_id);
  589. // If the decl block is empty, reuse it as the first body block. We don't do
  590. // this when the decl block is non-empty so that any branches back to the
  591. // first body block don't also re-execute the decl.
  592. llvm::BasicBlock* block = function_lowering.builder().GetInsertBlock();
  593. if (block->empty() &&
  594. function_lowering.TryToReuseBlock(body_block_ids.front(), block)) {
  595. // Reuse this block as the first block of the function body.
  596. } else {
  597. function_lowering.builder().CreateBr(
  598. function_lowering.GetBlock(body_block_ids.front()));
  599. }
  600. // Lower all blocks.
  601. for (auto block_id : body_block_ids) {
  602. lower_block(block_id);
  603. }
  604. // LLVM requires that the entry block has no predecessors.
  605. auto* entry_block = &llvm_function->getEntryBlock();
  606. if (entry_block->hasNPredecessorsOrMore(1)) {
  607. auto* new_entry_block = llvm::BasicBlock::Create(
  608. llvm_context(), "entry", llvm_function, entry_block);
  609. llvm::BranchInst::Create(entry_block, new_entry_block);
  610. }
  611. // Emit fingerprint accumulated inside the function context.
  612. function_lowering.EmitFinalFingerprint();
  613. }
  614. auto FileContext::BuildDISubprogram(const SemIR::Function& function,
  615. const llvm::Function* llvm_function)
  616. -> llvm::DISubprogram* {
  617. if (!context().di_compile_unit()) {
  618. return nullptr;
  619. }
  620. auto name = sem_ir().names().GetAsStringIfIdentifier(function.name_id);
  621. CARBON_CHECK(name, "Unexpected special name for function: {0}",
  622. function.name_id);
  623. auto loc = GetLocForDI(function.definition_id);
  624. // TODO: Add more details here, including real subroutine type (once type
  625. // information is built), etc.
  626. return context().di_builder().createFunction(
  627. context().di_compile_unit(), *name, llvm_function->getName(),
  628. /*File=*/context().di_builder().createFile(loc.filename, ""),
  629. /*LineNo=*/loc.line_number,
  630. context().di_builder().createSubroutineType(
  631. context().di_builder().getOrCreateTypeArray(std::nullopt)),
  632. /*ScopeLine=*/0, llvm::DINode::FlagZero,
  633. llvm::DISubprogram::SPFlagDefinition);
  634. }
  635. // BuildTypeForInst is used to construct types for FileContext::BuildType below.
  636. // Implementations return the LLVM type for the instruction. This first overload
  637. // is the fallback handler for non-type instructions.
  638. template <typename InstT>
  639. requires(InstT::Kind.is_type() == SemIR::InstIsType::Never)
  640. static auto BuildTypeForInst(FileContext& /*context*/, InstT inst)
  641. -> llvm::Type* {
  642. CARBON_FATAL("Cannot use inst as type: {0}", inst);
  643. }
  644. template <typename InstT>
  645. requires(InstT::Kind.is_symbolic_when_type())
  646. static auto BuildTypeForInst(FileContext& context, InstT /*inst*/)
  647. -> llvm::Type* {
  648. // Treat non-monomorphized symbolic types as opaque.
  649. return llvm::StructType::get(context.llvm_context());
  650. }
  651. static auto BuildTypeForInst(FileContext& context, SemIR::ArrayType inst)
  652. -> llvm::Type* {
  653. return llvm::ArrayType::get(
  654. context.GetType(context.sem_ir().types().GetTypeIdForTypeInstId(
  655. inst.element_type_inst_id)),
  656. *context.sem_ir().GetArrayBoundValue(inst.bound_id));
  657. }
  658. static auto BuildTypeForInst(FileContext& /*context*/, SemIR::AutoType inst)
  659. -> llvm::Type* {
  660. CARBON_FATAL("Unexpected builtin type in lowering: {0}", inst);
  661. }
  662. static auto BuildTypeForInst(FileContext& context, SemIR::BoolType /*inst*/)
  663. -> llvm::Type* {
  664. // TODO: We may want to have different representations for `bool` storage
  665. // (`i8`) versus for `bool` values (`i1`).
  666. return llvm::Type::getInt1Ty(context.llvm_context());
  667. }
  668. static auto BuildTypeForInst(FileContext& context, SemIR::ClassType inst)
  669. -> llvm::Type* {
  670. auto object_repr_id = context.sem_ir()
  671. .classes()
  672. .Get(inst.class_id)
  673. .GetObjectRepr(context.sem_ir(), inst.specific_id);
  674. return context.GetType(object_repr_id);
  675. }
  676. static auto BuildTypeForInst(FileContext& context, SemIR::ConstType inst)
  677. -> llvm::Type* {
  678. return context.GetType(
  679. context.sem_ir().types().GetTypeIdForTypeInstId(inst.inner_id));
  680. }
  681. static auto BuildTypeForInst(FileContext& context, SemIR::CustomLayoutType inst)
  682. -> llvm::Type* {
  683. auto layout = context.sem_ir().custom_layouts().Get(inst.layout_id);
  684. return llvm::ArrayType::get(llvm::Type::getInt8Ty(context.llvm_context()),
  685. layout[SemIR::CustomLayoutId::SizeIndex]);
  686. }
  687. static auto BuildTypeForInst(FileContext& context, SemIR::PartialType inst)
  688. -> llvm::Type* {
  689. return context.GetType(
  690. context.sem_ir().types().GetTypeIdForTypeInstId(inst.inner_id));
  691. }
  692. static auto BuildTypeForInst(FileContext& context,
  693. SemIR::ImplWitnessAssociatedConstant inst)
  694. -> llvm::Type* {
  695. return context.GetType(inst.type_id);
  696. }
  697. static auto BuildTypeForInst(FileContext& /*context*/,
  698. SemIR::ErrorInst /*inst*/) -> llvm::Type* {
  699. // This is a complete type but uses of it should never be lowered.
  700. return nullptr;
  701. }
  702. static auto BuildTypeForInst(FileContext& context, SemIR::FloatType /*inst*/)
  703. -> llvm::Type* {
  704. // TODO: Handle different sizes.
  705. return llvm::Type::getDoubleTy(context.llvm_context());
  706. }
  707. static auto BuildTypeForInst(FileContext& context, SemIR::IntType inst)
  708. -> llvm::Type* {
  709. auto width =
  710. context.sem_ir().insts().TryGetAs<SemIR::IntValue>(inst.bit_width_id);
  711. CARBON_CHECK(width, "Can't lower int type with symbolic width");
  712. return llvm::IntegerType::get(
  713. context.llvm_context(),
  714. context.sem_ir().ints().Get(width->int_id).getZExtValue());
  715. }
  716. static auto BuildTypeForInst(FileContext& context,
  717. SemIR::LegacyFloatType /*inst*/) -> llvm::Type* {
  718. return llvm::Type::getDoubleTy(context.llvm_context());
  719. }
  720. static auto BuildTypeForInst(FileContext& context, SemIR::PointerType /*inst*/)
  721. -> llvm::Type* {
  722. return llvm::PointerType::get(context.llvm_context(), /*AddressSpace=*/0);
  723. }
  724. static auto BuildTypeForInst(FileContext& /*context*/,
  725. SemIR::PatternType /*inst*/) -> llvm::Type* {
  726. CARBON_FATAL("Unexpected pattern type in lowering");
  727. }
  728. static auto BuildTypeForInst(FileContext& context, SemIR::StructType inst)
  729. -> llvm::Type* {
  730. auto fields = context.sem_ir().struct_type_fields().Get(inst.fields_id);
  731. llvm::SmallVector<llvm::Type*> subtypes;
  732. subtypes.reserve(fields.size());
  733. for (auto field : fields) {
  734. subtypes.push_back(context.GetType(
  735. context.sem_ir().types().GetTypeIdForTypeInstId(field.type_inst_id)));
  736. }
  737. return llvm::StructType::get(context.llvm_context(), subtypes);
  738. }
  739. static auto BuildTypeForInst(FileContext& context, SemIR::TupleType inst)
  740. -> llvm::Type* {
  741. // TODO: Investigate special-casing handling of empty tuples so that they
  742. // can be collectively replaced with LLVM's void, particularly around
  743. // function returns. LLVM doesn't allow declaring variables with a void
  744. // type, so that may require significant special casing.
  745. auto elements = context.sem_ir().inst_blocks().Get(inst.type_elements_id);
  746. llvm::SmallVector<llvm::Type*> subtypes;
  747. subtypes.reserve(elements.size());
  748. for (auto type_id : context.sem_ir().types().GetBlockAsTypeIds(elements)) {
  749. subtypes.push_back(context.GetType(type_id));
  750. }
  751. return llvm::StructType::get(context.llvm_context(), subtypes);
  752. }
  753. static auto BuildTypeForInst(FileContext& context, SemIR::TypeType /*inst*/)
  754. -> llvm::Type* {
  755. return context.GetTypeType();
  756. }
  757. static auto BuildTypeForInst(FileContext& context, SemIR::VtableType /*inst*/)
  758. -> llvm::Type* {
  759. return llvm::Type::getVoidTy(context.llvm_context());
  760. }
  761. template <typename InstT>
  762. requires(InstT::Kind.template IsAnyOf<SemIR::SpecificFunctionType,
  763. SemIR::StringType>())
  764. static auto BuildTypeForInst(FileContext& context, InstT /*inst*/)
  765. -> llvm::Type* {
  766. // TODO: Decide how we want to represent `StringType`.
  767. return llvm::PointerType::get(context.llvm_context(), 0);
  768. }
  769. template <typename InstT>
  770. requires(InstT::Kind
  771. .template IsAnyOf<SemIR::BoundMethodType, SemIR::IntLiteralType,
  772. SemIR::NamespaceType, SemIR::WitnessType>())
  773. static auto BuildTypeForInst(FileContext& context, InstT /*inst*/)
  774. -> llvm::Type* {
  775. // Return an empty struct as a placeholder.
  776. return llvm::StructType::get(context.llvm_context());
  777. }
  778. template <typename InstT>
  779. requires(InstT::Kind.template IsAnyOf<
  780. SemIR::AssociatedEntityType, SemIR::FacetType, SemIR::FunctionType,
  781. SemIR::FunctionTypeWithSelfType, SemIR::GenericClassType,
  782. SemIR::GenericInterfaceType, SemIR::InstType,
  783. SemIR::UnboundElementType, SemIR::WhereExpr>())
  784. static auto BuildTypeForInst(FileContext& context, InstT /*inst*/)
  785. -> llvm::Type* {
  786. // Return an empty struct as a placeholder.
  787. // TODO: Should we model an interface as a witness table, or an associated
  788. // entity as an index?
  789. return llvm::StructType::get(context.llvm_context());
  790. }
  791. auto FileContext::BuildType(SemIR::InstId inst_id) -> llvm::Type* {
  792. // Use overload resolution to select the implementation, producing compile
  793. // errors when BuildTypeForInst isn't defined for a given instruction.
  794. CARBON_KIND_SWITCH(sem_ir_->insts().Get(inst_id)) {
  795. #define CARBON_SEM_IR_INST_KIND(Name) \
  796. case CARBON_KIND(SemIR::Name inst): { \
  797. return BuildTypeForInst(*this, inst); \
  798. }
  799. #include "toolchain/sem_ir/inst_kind.def"
  800. }
  801. }
  802. auto FileContext::BuildGlobalVariableDecl(SemIR::VarStorage var_storage)
  803. -> llvm::GlobalVariable* {
  804. Mangler m(*this);
  805. auto mangled_name = m.MangleGlobalVariable(var_storage.pattern_id);
  806. auto linkage = llvm::GlobalVariable::ExternalLinkage;
  807. // If the variable doesn't have an externally-visible name, demote it to
  808. // internal linkage and invent a plausible name that shouldn't collide with
  809. // any of our real manglings.
  810. if (mangled_name.empty()) {
  811. linkage = llvm::GlobalVariable::InternalLinkage;
  812. if (inst_namer_) {
  813. mangled_name =
  814. ("var.anon" + inst_namer_->GetUnscopedNameFor(var_storage.pattern_id))
  815. .str();
  816. }
  817. }
  818. auto* type = GetType(var_storage.type_id);
  819. return new llvm::GlobalVariable(llvm_module(), type,
  820. /*isConstant=*/false, linkage,
  821. /*Initializer=*/nullptr, mangled_name);
  822. }
  823. auto FileContext::GetLocForDI(SemIR::InstId inst_id) -> Context::LocForDI {
  824. return context().GetLocForDI(
  825. GetAbsoluteNodeId(sem_ir_, SemIR::LocId(inst_id)).back());
  826. }
  827. auto FileContext::BuildVtable(const SemIR::Vtable& vtable,
  828. SemIR::SpecificId specific_id)
  829. -> llvm::GlobalVariable* {
  830. const auto& class_info = sem_ir().classes().Get(vtable.class_id);
  831. Mangler m(*this);
  832. std::string mangled_name = m.MangleVTable(class_info, specific_id);
  833. if (sem_ir()
  834. .insts()
  835. .GetImportSource(class_info.first_owning_decl_id)
  836. .has_value()) {
  837. // Emit a declaration of an imported vtable using a(n opaque) pointer type.
  838. // This doesn't have to match the definition that appears elsewhere, it'll
  839. // still get merged correctly.
  840. auto* gv = new llvm::GlobalVariable(
  841. llvm_module(),
  842. llvm::PointerType::get(llvm_context(), /*AddressSpace=*/0),
  843. /*isConstant=*/true, llvm::GlobalValue::ExternalLinkage, nullptr,
  844. mangled_name);
  845. gv->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
  846. return gv;
  847. }
  848. auto vtable_inst_block =
  849. sem_ir().inst_blocks().Get(vtable.virtual_functions_id);
  850. auto* entry_type = llvm::IntegerType::getInt32Ty(llvm_context());
  851. auto* table_type = llvm::ArrayType::get(entry_type, vtable_inst_block.size());
  852. auto* llvm_vtable = new llvm::GlobalVariable(
  853. llvm_module(), table_type, /*isConstant=*/true,
  854. llvm::GlobalValue::ExternalLinkage, nullptr, mangled_name);
  855. auto* i32_type = llvm::IntegerType::getInt32Ty(llvm_context());
  856. auto* i64_type = llvm::IntegerType::getInt64Ty(llvm_context());
  857. auto* vtable_const_int =
  858. llvm::ConstantExpr::getPtrToInt(llvm_vtable, i64_type);
  859. llvm::SmallVector<llvm::Constant*> vfuncs;
  860. vfuncs.reserve(vtable_inst_block.size());
  861. for (auto fn_decl_id : vtable_inst_block) {
  862. auto [_1, _2, fn_id, fn_specific_id] =
  863. DecomposeVirtualFunction(sem_ir(), fn_decl_id, specific_id);
  864. vfuncs.push_back(llvm::ConstantExpr::getTrunc(
  865. llvm::ConstantExpr::getSub(
  866. llvm::ConstantExpr::getPtrToInt(
  867. GetOrCreateFunction(fn_id, fn_specific_id), i64_type),
  868. vtable_const_int),
  869. i32_type));
  870. }
  871. llvm_vtable->setInitializer(llvm::ConstantArray::get(table_type, vfuncs));
  872. llvm_vtable->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
  873. return llvm_vtable;
  874. }
  875. } // namespace Carbon::Lower