file_context.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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/clang_global_decl.h"
  20. #include "toolchain/lower/constant.h"
  21. #include "toolchain/lower/function_context.h"
  22. #include "toolchain/lower/options.h"
  23. #include "toolchain/lower/specific_coalescer.h"
  24. #include "toolchain/sem_ir/absolute_node_ref.h"
  25. #include "toolchain/sem_ir/diagnostic_loc_converter.h"
  26. #include "toolchain/sem_ir/entry_point.h"
  27. #include "toolchain/sem_ir/expr_info.h"
  28. #include "toolchain/sem_ir/file.h"
  29. #include "toolchain/sem_ir/function.h"
  30. #include "toolchain/sem_ir/generic.h"
  31. #include "toolchain/sem_ir/ids.h"
  32. #include "toolchain/sem_ir/inst.h"
  33. #include "toolchain/sem_ir/inst_categories.h"
  34. #include "toolchain/sem_ir/inst_kind.h"
  35. #include "toolchain/sem_ir/mangler.h"
  36. #include "toolchain/sem_ir/pattern.h"
  37. #include "toolchain/sem_ir/stringify.h"
  38. #include "toolchain/sem_ir/type_info.h"
  39. #include "toolchain/sem_ir/typed_insts.h"
  40. namespace Carbon::Lower {
  41. FileContext::FileContext(Context& context, const SemIR::File& sem_ir,
  42. const SemIR::InstNamer* inst_namer,
  43. llvm::raw_ostream* vlog_stream)
  44. : context_(&context),
  45. sem_ir_(&sem_ir),
  46. inst_namer_(inst_namer),
  47. vlog_stream_(vlog_stream),
  48. functions_(LoweredFunctionStore::MakeForOverwrite(sem_ir.functions())),
  49. specific_functions_(sem_ir.specifics(), std::nullopt),
  50. types_(LoweredTypeStore::MakeWithExplicitSize(
  51. sem_ir.constant_values().ConcreteStoreSize(),
  52. sem_ir.constant_values().GetTypeIdTag(), {nullptr, nullptr})),
  53. constants_(LoweredConstantStore::MakeWithExplicitSize(
  54. sem_ir.insts().size(), sem_ir.insts().GetIdTag(), nullptr)),
  55. lowered_specifics_(sem_ir.generics(),
  56. llvm::SmallVector<SemIR::SpecificId>()),
  57. coalescer_(vlog_stream_, sem_ir.specifics()),
  58. vtables_(decltype(vtables_)::MakeForOverwrite(sem_ir.vtables())),
  59. specific_vtables_(sem_ir.specifics(), nullptr) {
  60. // Initialization that relies on invariants of the class.
  61. cpp_code_generator_ = cpp_file() ? cpp_file()->GetCodeGenerator() : nullptr;
  62. CARBON_CHECK(
  63. !cpp_code_generator_ ||
  64. (&cpp_code_generator_->GetModule()->getContext() == &llvm_context()));
  65. CARBON_CHECK(!sem_ir.has_errors(),
  66. "Generating LLVM IR from invalid SemIR::File is unsupported.");
  67. }
  68. // TODO: Move this to lower.cpp.
  69. auto FileContext::PrepareToLower() -> void {
  70. // Lower all types that were required to be complete.
  71. for (auto type_id : sem_ir_->types().complete_types()) {
  72. if (type_id.index >= 0) {
  73. types_.Set(type_id,
  74. BuildType(*this, sem_ir_->types().GetTypeInstId(type_id)));
  75. }
  76. }
  77. // Lower function declarations.
  78. for (auto [id, function] : sem_ir_->functions().enumerate()) {
  79. if (id == sem_ir().global_ctor_id()) {
  80. // The global constructor is only lowered when we generate its definition.
  81. // LLVM doesn't allow an internal linkage function to be undefined.
  82. continue;
  83. }
  84. if (function.evaluation_mode == SemIR::Function::EvaluationMode::MustEval) {
  85. // musteval functions are never lowered.
  86. continue;
  87. }
  88. functions_.Set(id, BuildFunctionDecl(id));
  89. }
  90. // TODO: Split vtable declaration creation from definition creation to avoid
  91. // redundant vtable definitions for imported vtables.
  92. for (const auto& [id, vtable] : sem_ir_->vtables().enumerate()) {
  93. const auto& class_info = sem_ir().classes().Get(vtable.class_id);
  94. // Vtables can't be generated for generics, only for their specifics - and
  95. // must be done lazily based on the use of those specifics.
  96. if (!class_info.generic_id.has_value()) {
  97. vtables_.Set(id, BuildVtable(vtable, SemIR::SpecificId::None));
  98. }
  99. }
  100. // Lower constants.
  101. LowerConstants(*this, constants_);
  102. }
  103. // TODO: Move this to lower.cpp.
  104. auto FileContext::LowerDefinitions() -> void {
  105. // Lower global variable definitions.
  106. // TODO: Storing both a `constants_` array and a separate `global_variables_`
  107. // map is redundant.
  108. for (auto inst_id :
  109. sem_ir().inst_blocks().Get(sem_ir().top_inst_block_id())) {
  110. // Only `VarStorage` indicates a global variable declaration in the
  111. // top instruction block.
  112. if (auto var = sem_ir().insts().TryGetAs<SemIR::VarStorage>(inst_id)) {
  113. // Get the global variable declaration. We created this when lowering the
  114. // constant unless the variable is unnamed, in which case we need to
  115. // create it now.
  116. llvm::GlobalVariable* llvm_var = nullptr;
  117. if (auto const_id = sem_ir().constant_values().Get(inst_id);
  118. const_id.is_constant()) {
  119. llvm_var = cast<llvm::GlobalVariable>(GetConstant(const_id, inst_id));
  120. } else {
  121. // We should never be emitting a definition for a C++ global variable.
  122. llvm_var = BuildNonCppGlobalVariableDecl(*var);
  123. }
  124. // Convert the declaration of this variable into a definition by adding an
  125. // initializer.
  126. global_variables_.Insert(inst_id, llvm_var);
  127. llvm_var->setInitializer(
  128. llvm::Constant::getNullValue(llvm_var->getValueType()));
  129. }
  130. }
  131. // Lower function definitions.
  132. for (auto [id, fn_info] : sem_ir_->functions().enumerate()) {
  133. // If we created a declaration and the function definition is not imported,
  134. // build a definition.
  135. if (functions_.Get(id) && fn_info.definition_id.has_value() &&
  136. !sem_ir().insts().GetImportSource(fn_info.definition_id).has_value()) {
  137. BuildFunctionDefinition(id);
  138. }
  139. }
  140. // Append `__global_init` to `llvm::global_ctors` to initialize global
  141. // variables.
  142. if (auto global_ctor_id = sem_ir().global_ctor_id();
  143. global_ctor_id.has_value()) {
  144. auto llvm_function = BuildFunctionDecl(global_ctor_id);
  145. functions_.Set(global_ctor_id, llvm_function);
  146. const auto& global_ctor = sem_ir().functions().Get(global_ctor_id);
  147. BuildFunctionBody(global_ctor_id, SemIR::SpecificId::None, global_ctor,
  148. *this, global_ctor);
  149. llvm::appendToGlobalCtors(llvm_module(), llvm_function->llvm_function,
  150. /*Priority=*/0);
  151. }
  152. }
  153. auto FileContext::Finalize() -> void {
  154. if (cpp_code_generator_) {
  155. // Clang code generation should not actually modify the AST, but isn't
  156. // const-correct.
  157. cpp_code_generator_->HandleTranslationUnit(
  158. const_cast<clang::ASTContext&>(cpp_file()->ast_context()));
  159. }
  160. // Find equivalent specifics (from the same generic), replace all uses and
  161. // remove duplicately lowered function definitions.
  162. coalescer_.CoalesceEquivalentSpecifics(lowered_specifics_,
  163. specific_functions_);
  164. }
  165. auto FileContext::GetConstant(SemIR::ConstantId const_id,
  166. SemIR::InstId use_inst_id) -> llvm::Value* {
  167. auto const_inst_id = sem_ir().constant_values().GetInstId(const_id);
  168. auto* const_value = constants_.Get(const_inst_id);
  169. // For value expressions and initializing expressions, the value produced by
  170. // a constant instruction is a value representation of the constant. For
  171. // initializing expressions, `FinishInit` will perform a copy if needed.
  172. switch (auto cat = SemIR::GetExprCategory(sem_ir(), const_inst_id)) {
  173. case SemIR::ExprCategory::Value:
  174. case SemIR::ExprCategory::ReprInitializing:
  175. case SemIR::ExprCategory::InPlaceInitializing:
  176. break;
  177. case SemIR::ExprCategory::DurableRef:
  178. case SemIR::ExprCategory::EphemeralRef:
  179. // Constant reference expressions lower to an address.
  180. return const_value;
  181. case SemIR::ExprCategory::NotExpr:
  182. case SemIR::ExprCategory::Error:
  183. case SemIR::ExprCategory::Pattern:
  184. case SemIR::ExprCategory::Mixed:
  185. case SemIR::ExprCategory::RefTagged:
  186. case SemIR::ExprCategory::Dependent:
  187. CARBON_FATAL("Unexpected category {0} for lowered constant {1}", cat,
  188. sem_ir().insts().Get(const_inst_id));
  189. };
  190. auto value_rep = SemIR::ValueRepr::ForType(
  191. sem_ir(), sem_ir().insts().Get(const_inst_id).type_id());
  192. if (value_rep.kind != SemIR::ValueRepr::Pointer) {
  193. return const_value;
  194. }
  195. // The value representation is a pointer. Generate a variable to hold the
  196. // value, or find and reuse an existing one.
  197. if (auto result = global_variables().Lookup(const_inst_id)) {
  198. return result.value();
  199. }
  200. // Include both the name of the constant, if any, and the point of use in
  201. // the name of the variable.
  202. llvm::StringRef const_name;
  203. llvm::StringRef use_name;
  204. if (inst_namer_) {
  205. const_name = inst_namer_->GetUnscopedNameFor(const_inst_id);
  206. if (use_inst_id.has_value()) {
  207. use_name = inst_namer_->GetUnscopedNameFor(use_inst_id);
  208. }
  209. }
  210. // We always need to give the global a name even if the instruction namer
  211. // doesn't have one to use.
  212. if (const_name.empty()) {
  213. const_name = "const";
  214. }
  215. if (use_name.empty()) {
  216. use_name = "anon";
  217. }
  218. llvm::StringRef sep = (use_name[0] == '.') ? "" : ".";
  219. auto* global_variable = new llvm::GlobalVariable(
  220. llvm_module(), GetType(sem_ir().GetPointeeType(value_rep.type_id)),
  221. /*isConstant=*/true, llvm::GlobalVariable::InternalLinkage, const_value,
  222. const_name + sep + use_name);
  223. global_variables_.Insert(const_inst_id, global_variable);
  224. return global_variable;
  225. }
  226. auto FileContext::GetOrCreateFunctionInfo(
  227. SemIR::FunctionId function_id, SemIR::SpecificId specific_id,
  228. FileContext* fallback_file, SemIR::FunctionId fallback_function_id,
  229. SemIR::SpecificId fallback_specific_id) -> std::optional<FunctionInfo>& {
  230. // If we have already lowered a declaration of this function, just return it.
  231. // TODO: If the existing declaration is inexact, and we now have a fallback,
  232. // we should try again.
  233. auto& result = GetFunctionInfo(function_id, specific_id);
  234. if (!result) {
  235. result = BuildFunctionDecl(function_id, specific_id, fallback_file,
  236. fallback_function_id, fallback_specific_id);
  237. }
  238. return result;
  239. }
  240. auto FileContext::HandleReferencedCppFunction(clang::FunctionDecl* cpp_decl)
  241. -> llvm::Function* {
  242. // Create the LLVM function (`CodeGenModule::GetOrCreateLLVMFunction()`)
  243. // so that code generation (`CodeGenModule::EmitGlobal()`) would see this
  244. // function name (`CodeGenModule::getMangledName()`), and will generate
  245. // its definition.
  246. auto* function_address = dyn_cast<llvm::Function>(
  247. cpp_code_generator_->GetAddrOfGlobal(CreateGlobalDecl(cpp_decl),
  248. /*isForDefinition=*/false));
  249. CARBON_CHECK(function_address);
  250. return function_address;
  251. }
  252. auto FileContext::HandleReferencedSpecificFunction(
  253. SemIR::FunctionId function_id, SemIR::SpecificId specific_id,
  254. llvm::Type* llvm_type) -> void {
  255. CARBON_CHECK(specific_id.has_value());
  256. // Add this specific function to a list of specific functions whose
  257. // definitions we need to emit.
  258. // TODO: Don't do this if we know this function is emitted as a
  259. // non-discardable symbol in the IR for some other file.
  260. context().AddPendingSpecificFunctionDefinition({.context = this,
  261. .function_id = function_id,
  262. .specific_id = specific_id});
  263. // Create a unique fingerprint for the function type.
  264. // For now, we compute the function type fingerprint only for specifics,
  265. // though we might need it for all functions in order to create a canonical
  266. // fingerprint across translation units.
  267. coalescer_.CreateTypeFingerprint(specific_id, llvm_type);
  268. }
  269. auto FileContext::GetOrCreateLLVMFunction(
  270. const FunctionTypeInfo& function_type_info, SemIR::FunctionId function_id,
  271. SemIR::SpecificId specific_id) -> llvm::Function* {
  272. // If this is a C++ function, tell Clang that we referenced it.
  273. if (auto clang_decl_id = sem_ir().functions().Get(function_id).clang_decl_id;
  274. clang_decl_id.has_value()) {
  275. CARBON_CHECK(!specific_id.has_value(),
  276. "Specific functions cannot have C++ definitions");
  277. return HandleReferencedCppFunction(
  278. sem_ir().clang_decls().Get(clang_decl_id).key.decl->getAsFunction());
  279. }
  280. SemIR::Mangler m(sem_ir(), context().total_ir_count());
  281. std::string mangled_name = m.Mangle(function_id, specific_id);
  282. if (auto* existing = llvm_module().getFunction(mangled_name)) {
  283. // We might have already lowered this function while lowering a different
  284. // file. That's OK.
  285. // TODO: If the prior function was inexact and the new one is not, we should
  286. // lower this new one and replace the existing function with it.
  287. // TODO: Check-fail or maybe diagnose if the two LLVM functions are not
  288. // produced by declarations of the same Carbon function. Name collisions
  289. // between non-private members of the same library should have been
  290. // diagnosed by check if detected, but it's not clear that check will
  291. // always be able to see this problem. In theory, name collisions could
  292. // also occur due to fingerprint collision.
  293. return existing;
  294. }
  295. // If this is a specific function, we may need to do additional work to
  296. // emit its definition.
  297. if (specific_id.has_value()) {
  298. HandleReferencedSpecificFunction(function_id, specific_id,
  299. function_type_info.type);
  300. }
  301. // TODO: For an imported inline function, consider generating an
  302. // `available_externally` definition.
  303. auto linkage = llvm::Function::ExternalLinkage;
  304. if (function_id == sem_ir().global_ctor_id()) {
  305. // The global constructor name would collide with global constructors for
  306. // other files in the same package, so use an internal linkage symbol.
  307. linkage = llvm::Function::InternalLinkage;
  308. } else if (specific_id.has_value()) {
  309. // Specific functions are allowed to be duplicated across files.
  310. // TODO: CoreWitness should have the same behavior; see its use of
  311. // WeakODRLinkage in BuildFunctionDefinition.
  312. linkage = llvm::Function::LinkOnceODRLinkage;
  313. }
  314. auto* llvm_function = llvm::Function::Create(function_type_info.type, linkage,
  315. mangled_name, llvm_module());
  316. CARBON_CHECK(llvm_function->getName() == mangled_name,
  317. "Mangled name collision: {0}", mangled_name);
  318. // Set up parameters and the return slot.
  319. for (auto [name_id, arg] : llvm::zip_equal(function_type_info.param_name_ids,
  320. llvm_function->args())) {
  321. arg.setName(sem_ir().names().GetIRBaseName(name_id));
  322. }
  323. if (function_type_info.sret_type != nullptr) {
  324. auto& return_arg = *llvm_function->args().begin();
  325. return_arg.addAttr(llvm::Attribute::getWithStructRetType(
  326. llvm_context(), function_type_info.sret_type));
  327. }
  328. return llvm_function;
  329. }
  330. auto FileContext::BuildFunctionDecl(SemIR::FunctionId function_id,
  331. SemIR::SpecificId specific_id,
  332. FileContext* fallback_file,
  333. SemIR::FunctionId fallback_function_id,
  334. SemIR::SpecificId fallback_specific_id)
  335. -> std::optional<FunctionInfo> {
  336. const auto& function = sem_ir().functions().Get(function_id);
  337. // Don't lower generic functions. Note that associated functions in interfaces
  338. // have `Self` in scope, so are implicitly generic functions.
  339. if (function.generic_id.has_value() && !specific_id.has_value()) {
  340. return std::nullopt;
  341. }
  342. // Don't lower builtins.
  343. if (function.builtin_function_kind() != SemIR::BuiltinFunctionKind::None) {
  344. return std::nullopt;
  345. }
  346. // Don't lower C++ functions that use a thunk. We will never reference them
  347. // directly, and their signatures would not be expected to match the
  348. // corresponding C++ function anyway.
  349. if (function.special_function_kind ==
  350. SemIR::Function::SpecialFunctionKind::HasCppThunk) {
  351. return std::nullopt;
  352. }
  353. // TODO: Consider tracking whether the function has been used, and only
  354. // lowering it if it's needed.
  355. FunctionInContext func_infos[] = {
  356. {this, function_id, specific_id},
  357. {fallback_file, fallback_function_id, fallback_specific_id}};
  358. auto function_type_info =
  359. BuildFunctionTypeInfo(llvm::ArrayRef(func_infos, fallback_file ? 2 : 1));
  360. auto* llvm_function =
  361. GetOrCreateLLVMFunction(function_type_info, function_id, specific_id);
  362. return {{.type = function_type_info.type,
  363. .di_type = function_type_info.di_type,
  364. .lowered_param_indices =
  365. std::move(function_type_info.lowered_param_indices),
  366. .unused_param_indices =
  367. std::move(function_type_info.unused_param_indices),
  368. .llvm_function = llvm_function,
  369. .inexact = function_type_info.inexact}};
  370. }
  371. // Find the file and function ID describing the definition of a function.
  372. static auto GetFunctionDefinition(const SemIR::File* decl_ir,
  373. SemIR::FunctionId function_id)
  374. -> std::pair<const SemIR::File*, SemIR::FunctionId> {
  375. // Find the file containing the definition.
  376. auto decl_id = decl_ir->functions().Get(function_id).definition_id;
  377. if (!decl_id.has_value()) {
  378. // Function is not defined.
  379. return {nullptr, SemIR::FunctionId::None};
  380. }
  381. // Find the function declaration this function was originally imported from.
  382. while (true) {
  383. auto import_inst_id = decl_ir->insts().GetImportSource(decl_id);
  384. if (!import_inst_id.has_value()) {
  385. break;
  386. }
  387. auto import_inst = decl_ir->import_ir_insts().Get(import_inst_id);
  388. decl_ir = decl_ir->import_irs().Get(import_inst.ir_id()).sem_ir;
  389. decl_id = import_inst.inst_id();
  390. }
  391. auto decl_ir_function_id =
  392. decl_ir->insts().GetAs<SemIR::FunctionDecl>(decl_id).function_id;
  393. return {decl_ir, decl_ir_function_id};
  394. }
  395. auto FileContext::BuildFunctionDefinition(SemIR::FunctionId function_id,
  396. SemIR::SpecificId specific_id)
  397. -> void {
  398. auto [definition_ir, definition_ir_function_id] =
  399. GetFunctionDefinition(&sem_ir(), function_id);
  400. if (!definition_ir) {
  401. // Function is probably defined in another file; not an error.
  402. return;
  403. }
  404. const auto& definition_function =
  405. definition_ir->functions().Get(definition_ir_function_id);
  406. BuildFunctionBody(
  407. function_id, specific_id, sem_ir().functions().Get(function_id),
  408. context().GetFileContext(definition_ir), definition_function);
  409. }
  410. auto FileContext::BuildFunctionBody(SemIR::FunctionId function_id,
  411. SemIR::SpecificId specific_id,
  412. const SemIR::Function& declaration_function,
  413. FileContext& definition_context,
  414. const SemIR::Function& definition_function)
  415. -> void {
  416. // On crash, report the function we were lowering.
  417. PrettyStackTraceFunction stack_trace_entry([&](llvm::raw_ostream& output) {
  418. SemIR::DiagnosticLocConverter converter(
  419. &context().tree_and_subtrees_getters(), &sem_ir());
  420. auto converted =
  421. converter.Convert(SemIR::LocId(declaration_function.definition_id),
  422. /*token_only=*/false);
  423. converted.loc.FormatLocation(output);
  424. output << "Lowering function ";
  425. if (specific_id.has_value()) {
  426. output << SemIR::StringifySpecific(sem_ir(), specific_id);
  427. } else {
  428. output << SemIR::StringifyConstantInst(
  429. sem_ir(), declaration_function.definition_id);
  430. }
  431. output << "\n";
  432. // Crash output has a tab indent; try to indent slightly past that.
  433. converted.loc.FormatSnippet(output, /*indent=*/10);
  434. });
  435. // Note that `definition_function` is potentially from a different SemIR::File
  436. // than the one that this file context represents. Any lowering done for
  437. // values derived from `definition_function` should use `definition_context`
  438. // instead of our context.
  439. const auto& definition_ir = definition_context.sem_ir();
  440. auto function_info = GetFunctionInfo(function_id, specific_id);
  441. CARBON_CHECK(function_info && function_info->llvm_function,
  442. "Attempting to define function that was not declared");
  443. CARBON_CHECK(!function_info->inexact,
  444. "Attempting to emit definition of inexact function: {0}",
  445. *function_info->llvm_function);
  446. // TODO: Build CoreWitness functions when they're called instead of when
  447. // they're defined. That should allow LinkOnceODRLinkage.
  448. if (declaration_function.special_function_kind ==
  449. SemIR::Function::SpecialFunctionKind::CoreWitness) {
  450. function_info->llvm_function->setLinkage(llvm::Function::WeakODRLinkage);
  451. }
  452. const auto& body_block_ids = definition_function.body_block_ids;
  453. CARBON_DCHECK(!body_block_ids.empty(),
  454. "No function body blocks found during lowering.");
  455. // Store which specifics were already lowered (with definitions) for each
  456. // generic.
  457. if (declaration_function.generic_id.has_value() && specific_id.has_value()) {
  458. // TODO: We should track this in the definition context instead so that we
  459. // can deduplicate specifics from different files.
  460. AddLoweredSpecificForGeneric(declaration_function.generic_id, specific_id);
  461. }
  462. // Set attributes on the function definition.
  463. {
  464. llvm::AttrBuilder attr_builder(llvm_context());
  465. attr_builder.addAttribute(llvm::Attribute::NoUnwind);
  466. // TODO: We should take the opt level from the SemIR file; it might not be
  467. // the same for all files in a compilation.
  468. if (context().opt_level() == Lower::OptimizationLevel::None) {
  469. // --optimize=none disables all optimizations for this function.
  470. attr_builder.addAttribute(llvm::Attribute::OptimizeNone);
  471. attr_builder.addAttribute(llvm::Attribute::NoInline);
  472. } else {
  473. // Otherwise, always inline thunks.
  474. if (definition_function.special_function_kind ==
  475. SemIR::Function::SpecialFunctionKind::Thunk) {
  476. attr_builder.addAttribute(llvm::Attribute::AlwaysInline);
  477. }
  478. // Convert --optimize=size into optsize and minsize.
  479. if (context().opt_level() == Lower::OptimizationLevel::Size) {
  480. attr_builder.addAttribute(llvm::Attribute::OptimizeForSize);
  481. attr_builder.addAttribute(llvm::Attribute::MinSize);
  482. }
  483. // TODO: Should we generate an InlineHint for some functions? Perhaps for
  484. // those defined in the API file?
  485. }
  486. function_info->llvm_function->addFnAttrs(attr_builder);
  487. }
  488. auto* subprogram = BuildDISubprogram(declaration_function, *function_info);
  489. FunctionContext function_lowering(
  490. definition_context, function_info->llvm_function, *this, specific_id,
  491. coalescer_.InitializeFingerprintForSpecific(specific_id), subprogram,
  492. vlog_stream_);
  493. auto call_param_ids = definition_ir.inst_blocks().GetOrEmpty(
  494. definition_function.call_params_id);
  495. // Add local variables for the parameters.
  496. for (auto [llvm_index, index] :
  497. llvm::enumerate(function_info->lowered_param_indices)) {
  498. function_lowering.SetLocal(
  499. call_param_ids[index.index],
  500. function_info->llvm_function->getArg(llvm_index));
  501. }
  502. // Add local variables for the SemIR parameters that aren't LLVM parameters.
  503. // These shouldn't actually be used, so they're set to poison values.
  504. for (auto [llvm_index, index] :
  505. llvm::enumerate(function_info->unused_param_indices)) {
  506. auto param_id = call_param_ids[index.index];
  507. function_lowering.SetLocal(
  508. param_id,
  509. llvm::PoisonValue::get(function_lowering.GetTypeOfInst(param_id)));
  510. }
  511. auto decl_block_id = SemIR::InstBlockId::None;
  512. if (function_id == sem_ir().global_ctor_id()) {
  513. decl_block_id = SemIR::InstBlockId::Empty;
  514. } else {
  515. decl_block_id =
  516. definition_ir.insts()
  517. .GetAs<SemIR::FunctionDecl>(definition_function.latest_decl_id())
  518. .decl_block_id;
  519. }
  520. // Lowers the contents of decl_block_id into the corresponding LLVM block,
  521. // creating it if it doesn't already exist.
  522. auto lower_block = [&](SemIR::InstBlockId block_id) {
  523. CARBON_VLOG("Lowering {0}\n", block_id);
  524. auto* llvm_block = function_lowering.GetBlock(block_id);
  525. // Keep the LLVM blocks in lexical order.
  526. llvm_block->moveBefore(function_info->llvm_function->end());
  527. function_lowering.builder().SetInsertPoint(llvm_block);
  528. function_lowering.LowerBlockContents(block_id);
  529. };
  530. lower_block(decl_block_id);
  531. // If the decl block is empty, reuse it as the first body block. We don't do
  532. // this when the decl block is non-empty so that any branches back to the
  533. // first body block don't also re-execute the decl.
  534. llvm::BasicBlock* block = function_lowering.builder().GetInsertBlock();
  535. if (block->empty() &&
  536. function_lowering.TryToReuseBlock(body_block_ids.front(), block)) {
  537. // Reuse this block as the first block of the function body.
  538. } else {
  539. function_lowering.builder().CreateBr(
  540. function_lowering.GetBlock(body_block_ids.front()));
  541. }
  542. // Lower all blocks.
  543. for (auto block_id : body_block_ids) {
  544. lower_block(block_id);
  545. }
  546. // LLVM requires that the entry block has no predecessors.
  547. auto* entry_block = &function_info->llvm_function->getEntryBlock();
  548. if (entry_block->hasNPredecessorsOrMore(1)) {
  549. auto* new_entry_block = llvm::BasicBlock::Create(
  550. llvm_context(), "entry", function_info->llvm_function, entry_block);
  551. llvm::UncondBrInst::Create(entry_block, new_entry_block);
  552. }
  553. // Emit fingerprint accumulated inside the function context.
  554. function_lowering.EmitFinalFingerprint();
  555. context().di_builder().finalizeSubprogram(subprogram);
  556. }
  557. auto FileContext::BuildDISubprogram(const SemIR::Function& function,
  558. const FunctionInfo& function_info)
  559. -> llvm::DISubprogram* {
  560. if (!context().di_compile_unit()) {
  561. return nullptr;
  562. }
  563. auto name = sem_ir().names().GetAsStringIfIdentifier(function.name_id);
  564. CARBON_CHECK(name, "Unexpected special name for function: {0}",
  565. function.name_id);
  566. auto loc = GetLocForDI(function.definition_id);
  567. llvm::DISubroutineType* subroutine_type = function_info.di_type;
  568. auto* subprogram = context().di_builder().createFunction(
  569. context().di_compile_unit(), *name,
  570. function_info.llvm_function->getName(),
  571. /*File=*/context().di_builder().createFile(loc.filename, ""),
  572. /*LineNo=*/loc.line_number, subroutine_type,
  573. /*ScopeLine=*/0, llvm::DINode::FlagZero,
  574. llvm::DISubprogram::SPFlagDefinition);
  575. // Add a variable for each parameter, as that is where DWARF debug information
  576. // comes from.
  577. // TODO: this doesn't declare a variable for the output parameter. Is that
  578. // what we want?
  579. for (auto [argument_number, type] :
  580. llvm::enumerate(llvm::drop_begin(subroutine_type->getTypeArray()))) {
  581. context().di_builder().createParameterVariable(
  582. subprogram, "", argument_number + 1, nullptr, 0, type,
  583. /*AlwaysPreserve=*/true);
  584. }
  585. return subprogram;
  586. }
  587. auto FileContext::BuildGlobalVariableDecl(SemIR::VarStorage var_storage)
  588. -> llvm::Constant* {
  589. auto var_name_id =
  590. SemIR::GetFirstBindingNameFromPatternId(sem_ir(), var_storage.pattern_id);
  591. if (auto cpp_global_var_id =
  592. sem_ir().cpp_global_vars().Lookup({.entity_name_id = var_name_id});
  593. cpp_global_var_id.has_value()) {
  594. SemIR::ClangDeclId clang_decl_id =
  595. sem_ir().cpp_global_vars().Get(cpp_global_var_id).clang_decl_id;
  596. CARBON_CHECK(clang_decl_id.has_value(),
  597. "CppGlobalVar should have a clang_decl_id");
  598. return cpp_code_generator_->GetAddrOfGlobal(
  599. cast<clang::VarDecl>(
  600. sem_ir().clang_decls().Get(clang_decl_id).key.decl),
  601. /*isForDefinition=*/false);
  602. }
  603. return BuildNonCppGlobalVariableDecl(var_storage);
  604. }
  605. auto FileContext::BuildNonCppGlobalVariableDecl(SemIR::VarStorage var_storage)
  606. -> llvm::GlobalVariable* {
  607. SemIR::Mangler m(sem_ir(), context().total_ir_count());
  608. auto mangled_name = m.MangleGlobalVariable(var_storage.pattern_id);
  609. auto linkage = llvm::GlobalVariable::ExternalLinkage;
  610. // If the variable doesn't have an externally-visible name, demote it to
  611. // internal linkage and invent a plausible name that shouldn't collide with
  612. // any of our real manglings.
  613. if (mangled_name.empty()) {
  614. linkage = llvm::GlobalVariable::InternalLinkage;
  615. if (inst_namer_) {
  616. mangled_name =
  617. ("var.anon" + inst_namer_->GetUnscopedNameFor(var_storage.pattern_id))
  618. .str();
  619. }
  620. }
  621. auto* type = GetType(var_storage.type_id);
  622. return new llvm::GlobalVariable(llvm_module(), type,
  623. /*isConstant=*/false, linkage,
  624. /*Initializer=*/nullptr, mangled_name);
  625. }
  626. auto FileContext::GetLocForDI(SemIR::InstId inst_id) -> Context::LocForDI {
  627. auto abs_node_ref = GetAbsoluteNodeRef(sem_ir_, SemIR::LocId(inst_id)).back();
  628. return context().GetLocForDI(abs_node_ref);
  629. }
  630. auto FileContext::BuildVtable(const SemIR::Vtable& vtable,
  631. SemIR::SpecificId specific_id)
  632. -> llvm::GlobalVariable* {
  633. const auto& class_info = sem_ir().classes().Get(vtable.class_id);
  634. SemIR::Mangler m(sem_ir(), context().total_ir_count());
  635. std::string mangled_name = m.MangleVTable(class_info, specific_id);
  636. if (sem_ir()
  637. .insts()
  638. .GetImportSource(class_info.first_owning_decl_id)
  639. .has_value()) {
  640. // Emit a declaration of an imported vtable using a(n opaque) pointer type.
  641. // This doesn't have to match the definition that appears elsewhere, it'll
  642. // still get merged correctly.
  643. auto* gv = new llvm::GlobalVariable(
  644. llvm_module(),
  645. llvm::PointerType::get(llvm_context(), /*AddressSpace=*/0),
  646. /*isConstant=*/true, llvm::GlobalValue::ExternalLinkage, nullptr,
  647. mangled_name);
  648. gv->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
  649. return gv;
  650. }
  651. auto vtable_inst_block =
  652. sem_ir().inst_blocks().Get(vtable.virtual_functions_id);
  653. auto* entry_type = llvm::IntegerType::getInt32Ty(llvm_context());
  654. auto* table_type = llvm::ArrayType::get(entry_type, vtable_inst_block.size());
  655. auto* llvm_vtable = new llvm::GlobalVariable(
  656. llvm_module(), table_type, /*isConstant=*/true,
  657. llvm::GlobalValue::ExternalLinkage, nullptr, mangled_name);
  658. auto* i32_type = llvm::IntegerType::getInt32Ty(llvm_context());
  659. auto* i64_type = llvm::IntegerType::getInt64Ty(llvm_context());
  660. auto* vtable_const_int =
  661. llvm::ConstantExpr::getPtrToInt(llvm_vtable, i64_type);
  662. llvm::SmallVector<llvm::Constant*> vfuncs;
  663. vfuncs.reserve(vtable_inst_block.size());
  664. for (auto fn_decl_id : vtable_inst_block) {
  665. auto [_1, _2, fn_id, fn_specific_id] =
  666. DecomposeVirtualFunction(sem_ir(), fn_decl_id, specific_id);
  667. vfuncs.push_back(llvm::ConstantExpr::getTrunc(
  668. llvm::ConstantExpr::getSub(
  669. llvm::ConstantExpr::getPtrToInt(
  670. GetOrCreateFunctionInfo(fn_id, fn_specific_id)->llvm_function,
  671. i64_type),
  672. vtable_const_int),
  673. i32_type));
  674. }
  675. llvm_vtable->setInitializer(llvm::ConstantArray::get(table_type, vfuncs));
  676. llvm_vtable->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
  677. return llvm_vtable;
  678. }
  679. } // namespace Carbon::Lower