file_context.cpp 36 KB

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