file_context.cpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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_id.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/typed_insts.h"
  39. namespace Carbon::Lower {
  40. FileContext::FileContext(Context& context, const SemIR::File& sem_ir,
  41. const SemIR::InstNamer* inst_namer,
  42. llvm::raw_ostream* vlog_stream)
  43. : context_(&context),
  44. sem_ir_(&sem_ir),
  45. inst_namer_(inst_namer),
  46. vlog_stream_(vlog_stream),
  47. functions_(LoweredFunctionStore::MakeForOverwrite(sem_ir.functions())),
  48. specific_functions_(sem_ir.specifics(), std::nullopt),
  49. types_(LoweredTypeStore::MakeWithExplicitSize(
  50. sem_ir.constant_values().ConcreteStoreSize(),
  51. sem_ir.constant_values().GetTypeIdTag(), {nullptr, nullptr})),
  52. constants_(LoweredConstantStore::MakeWithExplicitSize(
  53. sem_ir.insts().size(), sem_ir.insts().GetIdTag(), nullptr)),
  54. lowered_specifics_(sem_ir.generics(),
  55. llvm::SmallVector<SemIR::SpecificId>()),
  56. coalescer_(vlog_stream_, sem_ir.specifics()),
  57. vtables_(decltype(vtables_)::MakeForOverwrite(sem_ir.vtables())),
  58. specific_vtables_(sem_ir.specifics(), nullptr) {
  59. // Initialization that relies on invariants of the class.
  60. cpp_code_generator_ = cpp_file() ? cpp_file()->GetCodeGenerator() : nullptr;
  61. CARBON_CHECK(
  62. !cpp_code_generator_ ||
  63. (&cpp_code_generator_->GetModule()->getContext() == &llvm_context()));
  64. CARBON_CHECK(!sem_ir.has_errors(),
  65. "Generating LLVM IR from invalid SemIR::File is unsupported.");
  66. }
  67. // TODO: Move this to lower.cpp.
  68. auto FileContext::PrepareToLower() -> void {
  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().GetTypeInstId(type_id)));
  73. }
  74. }
  75. // Lower function declarations.
  76. for (auto [id, function] : sem_ir_->functions().enumerate()) {
  77. if (id == sem_ir().global_ctor_id()) {
  78. // The global constructor is only lowered when we generate its definition.
  79. // LLVM doesn't allow an internal linkage function to be undefined.
  80. continue;
  81. }
  82. if (function.evaluation_mode == SemIR::Function::EvaluationMode::MustEval) {
  83. // musteval functions are never lowered.
  84. continue;
  85. }
  86. functions_.Set(id, BuildFunctionDecl(id));
  87. }
  88. // TODO: Split vtable declaration creation from definition creation to avoid
  89. // redundant vtable definitions for imported vtables.
  90. for (const auto& [id, vtable] : sem_ir_->vtables().enumerate()) {
  91. const auto& class_info = sem_ir().classes().Get(vtable.class_id);
  92. // Vtables can't be generated for generics, only for their specifics - and
  93. // must be done lazily based on the use of those specifics.
  94. if (!class_info.generic_id.has_value()) {
  95. vtables_.Set(id, BuildVtable(vtable, SemIR::SpecificId::None));
  96. }
  97. }
  98. // Lower constants.
  99. LowerConstants(*this, constants_);
  100. }
  101. // TODO: Move this to lower.cpp.
  102. auto FileContext::LowerDefinitions() -> void {
  103. // Lower global variable definitions.
  104. // TODO: Storing both a `constants_` array and a separate `global_variables_`
  105. // map is redundant.
  106. for (auto inst_id :
  107. sem_ir().inst_blocks().Get(sem_ir().top_inst_block_id())) {
  108. // Only `VarStorage` indicates a global variable declaration in the
  109. // top instruction block.
  110. if (auto var = sem_ir().insts().TryGetAs<SemIR::VarStorage>(inst_id)) {
  111. // Get the global variable declaration. We created this when lowering the
  112. // constant unless the variable is unnamed, in which case we need to
  113. // create it now.
  114. llvm::GlobalVariable* llvm_var = nullptr;
  115. if (auto const_id = sem_ir().constant_values().Get(inst_id);
  116. const_id.is_constant()) {
  117. llvm_var = cast<llvm::GlobalVariable>(GetConstant(const_id, inst_id));
  118. } else {
  119. // We should never be emitting a definition for a C++ global variable.
  120. llvm_var = BuildNonCppGlobalVariableDecl(*var);
  121. }
  122. // Convert the declaration of this variable into a definition by adding an
  123. // initializer.
  124. global_variables_.Insert(inst_id, llvm_var);
  125. llvm_var->setInitializer(
  126. llvm::Constant::getNullValue(llvm_var->getValueType()));
  127. }
  128. }
  129. // Lower function definitions.
  130. for (auto [id, fn_info] : sem_ir_->functions().enumerate()) {
  131. // If we created a declaration and the function definition is not imported,
  132. // build a definition.
  133. if (functions_.Get(id) && fn_info.definition_id.has_value() &&
  134. !sem_ir().insts().GetImportSource(fn_info.definition_id).has_value()) {
  135. BuildFunctionDefinition(id);
  136. }
  137. }
  138. // Append `__global_init` to `llvm::global_ctors` to initialize global
  139. // variables.
  140. if (auto global_ctor_id = sem_ir().global_ctor_id();
  141. global_ctor_id.has_value()) {
  142. auto llvm_function = BuildFunctionDecl(global_ctor_id);
  143. functions_.Set(global_ctor_id, llvm_function);
  144. const auto& global_ctor = sem_ir().functions().Get(global_ctor_id);
  145. BuildFunctionBody(global_ctor_id, SemIR::SpecificId::None, global_ctor,
  146. *this, global_ctor);
  147. llvm::appendToGlobalCtors(llvm_module(), llvm_function->llvm_function,
  148. /*Priority=*/0);
  149. }
  150. }
  151. auto FileContext::Finalize() -> void {
  152. if (cpp_code_generator_) {
  153. // Clang code generation should not actually modify the AST, but isn't
  154. // const-correct.
  155. cpp_code_generator_->HandleTranslationUnit(
  156. const_cast<clang::ASTContext&>(cpp_file()->ast_context()));
  157. }
  158. // Find equivalent specifics (from the same generic), replace all uses and
  159. // remove duplicately lowered function definitions.
  160. coalescer_.CoalesceEquivalentSpecifics(lowered_specifics_,
  161. specific_functions_);
  162. }
  163. auto FileContext::GetConstant(SemIR::ConstantId const_id,
  164. SemIR::InstId use_inst_id) -> llvm::Value* {
  165. auto const_inst_id = sem_ir().constant_values().GetInstId(const_id);
  166. auto* const_value = constants_.Get(const_inst_id);
  167. // For value expressions and initializing expressions, the value produced by
  168. // a constant instruction is a value representation of the constant. For
  169. // initializing expressions, `FinishInit` will perform a copy if needed.
  170. switch (auto cat = SemIR::GetExprCategory(sem_ir(), const_inst_id)) {
  171. case SemIR::ExprCategory::Value:
  172. case SemIR::ExprCategory::ReprInitializing:
  173. case SemIR::ExprCategory::InPlaceInitializing:
  174. break;
  175. case SemIR::ExprCategory::DurableRef:
  176. case SemIR::ExprCategory::EphemeralRef:
  177. // Constant reference expressions lower to an address.
  178. return const_value;
  179. case SemIR::ExprCategory::NotExpr:
  180. case SemIR::ExprCategory::Error:
  181. case SemIR::ExprCategory::Pattern:
  182. case SemIR::ExprCategory::Mixed:
  183. case SemIR::ExprCategory::RefTagged:
  184. case SemIR::ExprCategory::Dependent:
  185. CARBON_FATAL("Unexpected category {0} for lowered constant {1}", cat,
  186. sem_ir().insts().Get(const_inst_id));
  187. };
  188. auto value_rep = SemIR::ValueRepr::ForType(
  189. sem_ir(), sem_ir().insts().Get(const_inst_id).type_id());
  190. if (value_rep.kind != SemIR::ValueRepr::Pointer) {
  191. return const_value;
  192. }
  193. // The value representation is a pointer. Generate a variable to hold the
  194. // value, or find and reuse an existing one.
  195. if (auto result = global_variables().Lookup(const_inst_id)) {
  196. return result.value();
  197. }
  198. // Include both the name of the constant, if any, and the point of use in
  199. // the name of the variable.
  200. llvm::StringRef const_name;
  201. llvm::StringRef use_name;
  202. if (inst_namer_) {
  203. const_name = inst_namer_->GetUnscopedNameFor(const_inst_id);
  204. if (use_inst_id.has_value()) {
  205. use_name = inst_namer_->GetUnscopedNameFor(use_inst_id);
  206. }
  207. }
  208. // We always need to give the global a name even if the instruction namer
  209. // doesn't have one to use.
  210. if (const_name.empty()) {
  211. const_name = "const";
  212. }
  213. if (use_name.empty()) {
  214. use_name = "anon";
  215. }
  216. llvm::StringRef sep = (use_name[0] == '.') ? "" : ".";
  217. auto* global_variable = new llvm::GlobalVariable(
  218. llvm_module(), GetType(sem_ir().GetPointeeType(value_rep.type_id)),
  219. /*isConstant=*/true, llvm::GlobalVariable::InternalLinkage, const_value,
  220. const_name + sep + use_name);
  221. global_variables_.Insert(const_inst_id, global_variable);
  222. return global_variable;
  223. }
  224. auto FileContext::GetOrCreateFunctionInfo(
  225. SemIR::FunctionId function_id, SemIR::SpecificId specific_id,
  226. FileContext* fallback_file, SemIR::FunctionId fallback_function_id,
  227. SemIR::SpecificId fallback_specific_id) -> std::optional<FunctionInfo>& {
  228. // If we have already lowered a declaration of this function, just return it.
  229. // TODO: If the existing declaration is inexact, and we now have a fallback,
  230. // we should try again.
  231. auto& result = GetFunctionInfo(function_id, specific_id);
  232. if (!result) {
  233. result = BuildFunctionDecl(function_id, specific_id, fallback_file,
  234. fallback_function_id, fallback_specific_id);
  235. }
  236. return result;
  237. }
  238. // State machine for building a FunctionTypeInfo from SemIR.
  239. //
  240. // The main difficulty this class encapsulates is that each abstraction level
  241. // has different expectations about how the return is reflected in the parameter
  242. // list.
  243. // - In SemIR, if the function has an initializing return form, it has a
  244. // corresponding output parameter at the end of the parameter list.
  245. // - In LLVM IR, if the SemIR has an output parameter _and_ that parameter's
  246. // type has an in-place initializing representation, we emit a corresponding
  247. // `sret` output parameter (and the function's return type is void). By
  248. // convention the output parameter goes at the start of the parameter list.
  249. // - In LLVM debug info, the list of parameter types always starts with the
  250. // return type (which doubles as the type of the return parameter, if there
  251. // is one).
  252. //
  253. // Furthermore, SemIR is designed to eventually support compound return forms,
  254. // in which case there can be multiple output parameters for different pieces of
  255. // the return form, but it's not yet clear how we will lower such functions.
  256. //
  257. // We also deal with the case where the function signature involves incomplete
  258. // types. This can happen if the function is declared but never defined nor
  259. // called in this file. Declarations of such functions can still need to be
  260. // emitted; currently this happens if they are part of a class's vtable. Such
  261. // uses do not need an exact signature, so we emit them with the LLVM type
  262. // `void()` and set `inexact` on the result to indicate the type is not known.
  263. // LLVM can handle merging inexact and exact signatures, and this matches how
  264. // Clang handles the corresponding situation in C++.
  265. //
  266. // One additional complexity is that we may need to fetch information about the
  267. // same function from multiple different files. For a call to a generic
  268. // function, there may be no single file in which all the relevant types are
  269. // complete, so we will look at both the specific function definition that is
  270. // the resolved callee, as well as the partially-specific function from the call
  271. // site.
  272. //
  273. // In general, we support being given a list of variants of the function, in
  274. // which the first function in the list is the primary declaration and should be
  275. // the most specific function, and the others are used as fallbacks if an
  276. // incomplete type is encountered.
  277. class FileContext::FunctionTypeInfoBuilder {
  278. public:
  279. struct FunctionInContext {
  280. FileContext* context;
  281. SemIR::FunctionId function_id;
  282. SemIR::SpecificId specific_id;
  283. };
  284. // Creates a FunctionTypeInfoBuilder that uses the given functions.
  285. explicit FunctionTypeInfoBuilder(llvm::ArrayRef<FunctionInContext> functions)
  286. : context_(&functions.front().context->context()), functions_(functions) {
  287. CARBON_CHECK(!functions_.empty());
  288. }
  289. // Retrieves various features of the function's type useful for constructing
  290. // the `llvm::Type` and `llvm::DISubroutineType` for the `llvm::Function`. If
  291. // any part of the type can't be manifest (eg: incomplete return or parameter
  292. // types), then the result is as if the type was `void()`. Should only be
  293. // called once on a given builder.
  294. auto Build() && -> FunctionTypeInfo;
  295. private:
  296. // By convention, state transition methods return false (without changing the
  297. // accumulated information about the function) to indicate that we could not
  298. // manifest the complete function type successfully in this context.
  299. // Information about how a function is called in SemIR.
  300. struct SemIRIndexInfo {
  301. // The number of parameters in the SemIR call signature.
  302. int num_params;
  303. // The index of the first return parameter in the SemIR call signature.
  304. int return_param_index;
  305. friend auto operator==(const SemIRIndexInfo& lhs, const SemIRIndexInfo& rhs)
  306. -> bool = default;
  307. };
  308. // Get information about the SemIR function signature.
  309. auto GetSemIRIndexInfo(const FunctionInContext& fn_in_context)
  310. -> SemIRIndexInfo {
  311. const auto& sem_ir = fn_in_context.context->sem_ir();
  312. const auto& function = sem_ir.functions().Get(fn_in_context.function_id);
  313. int num_params =
  314. sem_ir.inst_blocks().Get(function.call_param_patterns_id).size();
  315. int return_param_index = -1;
  316. if (function.call_param_ranges.return_size() > 0) {
  317. CARBON_CHECK(function.call_param_ranges.return_size() == 1,
  318. "TODO: support multiple return forms");
  319. return_param_index = function.call_param_ranges.return_begin().index;
  320. }
  321. return {.num_params = num_params, .return_param_index = return_param_index};
  322. }
  323. // Handles the function's return form.
  324. //
  325. // This should be called before `HandleParameter`. It handles the return form
  326. // by trying each `FunctionInContext` until one succeeds, and returns false if
  327. // all attempts failed.
  328. auto HandleReturnForm() -> bool;
  329. // Tries to handle the return form using the given context. Delegates to
  330. // exactly one of `SetReturnByCopy`, `SetReturnByReference`, or
  331. // `SetReturnInPlace`, or returns false if the return type is incomplete.
  332. auto TryHandleReturnForm(const FunctionInContext& func_ctx) -> bool;
  333. // Records that the LLVM function returns by copy, with type `return_type_id`.
  334. // `return_type_id` can be `None`, which is treated as equivalent to the
  335. // default return type `()`.
  336. auto SetReturnByCopy(const FunctionInContext& func_ctx,
  337. SemIR::TypeId return_type_id) -> bool {
  338. CARBON_CHECK(return_type_ == nullptr);
  339. CARBON_CHECK(param_di_types_.empty());
  340. auto lowered_return_types = GetLoweredTypes(func_ctx, return_type_id);
  341. return_type_ = lowered_return_types.llvm_ir_type;
  342. param_di_types_.push_back(lowered_return_types.llvm_di_type);
  343. return true;
  344. }
  345. // Records that the LLVM function returns by reference, with type
  346. // `return_type_id`.
  347. auto SetReturnByReference(const FunctionInContext& func_ctx,
  348. SemIR::TypeId /*return_type_id*/) -> bool {
  349. return_type_ = llvm::PointerType::get(func_ctx.context->llvm_context(),
  350. /*AddressSpace=*/0);
  351. // TODO: replace this with a reference type.
  352. param_di_types_.push_back(GetPointerDIType(nullptr));
  353. return true;
  354. }
  355. // Records that the LLVM function returns in place, with type
  356. // `return_type_id`.
  357. auto SetReturnInPlace(const FunctionInContext& func_ctx,
  358. SemIR::TypeId return_type_id) -> bool {
  359. return_type_ = llvm::Type::getVoidTy(func_ctx.context->llvm_context());
  360. sret_type_ = func_ctx.context->GetType(return_type_id);
  361. // We don't add to param_di_types_ because that will be handled by the
  362. // loop over the SemIR parameters.
  363. return true;
  364. }
  365. // Handles `Call` parameter pattern at the given index. This should be called
  366. // on parameter patterns in the order that they should appear in the LLVM IR
  367. // parameter list, so in particular it should be called on the
  368. // `OutParamPattern` (if any) first. It should be called on all `Call`
  369. // parameters; it will determine which parameters belong in the LLVM IR
  370. // parameter list.
  371. //
  372. // This tries each `FunctionInContext` until one succeeds, and returns false
  373. // if all attempts failed.
  374. auto HandleParameter(SemIR::CallParamIndex index) -> bool;
  375. // Tries to handle the parameter pattern at the given index using the given
  376. // context. Delegates to either `AddLoweredParam` or `IgnoreParam`, or returns
  377. // false if the parameter type is incomplete.
  378. auto TryHandleParameter(const FunctionInContext& func_ctx,
  379. SemIR::CallParamIndex index) -> bool;
  380. // Records that the parameter pattern at the given index has the given ID, and
  381. // lowers to the given IR and DI types.
  382. auto AddLoweredParam(const FunctionInContext& func_ctx,
  383. SemIR::CallParamIndex index,
  384. SemIR::InstId param_pattern_id, LoweredTypes param_types)
  385. -> bool {
  386. lowered_param_indices_.push_back(index);
  387. param_name_ids_.push_back(SemIR::GetPrettyNameFromPatternId(
  388. func_ctx.context->sem_ir(), param_pattern_id));
  389. param_types_.push_back(param_types.llvm_ir_type);
  390. param_di_types_.push_back(param_types.llvm_di_type);
  391. return true;
  392. }
  393. // Records that the `Call` parameter pattern at the given index is not lowered
  394. // to an LLVM parameter.
  395. auto IgnoreParam(SemIR::CallParamIndex index) -> bool {
  396. unused_param_indices_.push_back(index);
  397. return true;
  398. }
  399. // Builds and returns a FunctionTypeInfo from the accumulated information.
  400. auto Finalize() -> FunctionTypeInfo;
  401. // Clears out accumulated state and returns a FunctionTypeInfo with the
  402. // fallback state `void()`.
  403. auto Abort() -> FunctionTypeInfo;
  404. // Returns LLVM IR and DI types for the given SemIR type. This is not a state
  405. // transition. It mostly delegates to context_.GetTypeAndDIType, but treats
  406. // TypeId::None as equivalent to the unit type, and uses an untyped pointer as
  407. // a placeholder DI type if context_ doesn't provide one.
  408. auto GetLoweredTypes(const FunctionInContext& func_ctx, SemIR::TypeId type_id)
  409. -> LoweredTypes;
  410. // Returns a DI type for a pointer to the given pointee. The pointee type may
  411. // be null.
  412. auto GetPointerDIType(llvm::DIType* pointee_type, unsigned address_space = 0)
  413. -> llvm::DIDerivedType* {
  414. const auto& data_layout = context_->llvm_module().getDataLayout();
  415. return context_->di_builder().createPointerType(
  416. pointee_type, data_layout.getPointerSizeInBits(address_space));
  417. }
  418. Context* context_;
  419. llvm::ArrayRef<FunctionInContext> functions_;
  420. // The number of input `Call` parameter patterns.
  421. int num_params_ = 0;
  422. // The types of the parameters in the LLVM IR function. Each one corresponds
  423. // to a SemIR `Call` parameter, but some `Call` parameters may be omitted
  424. // (e.g. if they are stateless) or reordered (e.g. the return parameter, if
  425. // any, always goes first).
  426. llvm::SmallVector<llvm::Type*> param_types_;
  427. // The LLLVM DI representation of the parameter list. As required by LLVM DI
  428. // convention, this starts with the function's return type, and ends with the
  429. // DI representations of param_types_ (in the same order). Note that those
  430. // two ranges may overlap: if the first element of param_types_ represents
  431. // a return parameter, the first element of param_di_types_ corresponds to it
  432. // while also representing the return type.
  433. llvm::SmallVector<llvm::Metadata*> param_di_types_;
  434. // The indices of the `Call` parameters that correspond to `param_types_`, in
  435. // the same order.
  436. llvm::SmallVector<SemIR::CallParamIndex> lowered_param_indices_;
  437. // The names of the `Call` parameters that correspond to `param_types_`, in
  438. // the same order.
  439. llvm::SmallVector<SemIR::NameId> param_name_ids_;
  440. // The indices of any `Call` param patterns that aren't present in
  441. // lowered_param_indices_.
  442. llvm::SmallVector<SemIR::CallParamIndex> unused_param_indices_;
  443. // The LLVM function's return type.
  444. llvm::Type* return_type_ = nullptr;
  445. // If not null, the LLVM function's first parameter should have a `sret`
  446. // attribute with this type.
  447. llvm::Type* sret_type_ = nullptr;
  448. // Whether we failed to form an exact description of the function type. This
  449. // can happen if a parameter or return type is incomplete. In this case, we
  450. // can still sometimes need to emit a declaration of the function, for example
  451. // because it appears in a vtable, but we cannot emit a definition or a call.
  452. bool inexact_ = false;
  453. };
  454. auto FileContext::FunctionTypeInfoBuilder::Build() && -> FunctionTypeInfo {
  455. // TODO: For the `Run` entry point, remap return type to i32 if it doesn't
  456. // return a value.
  457. // Determine how the parameters are numbered in SemIR, and make sure it's the
  458. // same for all versions of the function.
  459. auto semir_info = GetSemIRIndexInfo(functions_.front());
  460. CARBON_CHECK(
  461. llvm::all_of(functions_.drop_front(), [&](const auto& fn_in_context) {
  462. return GetSemIRIndexInfo(fn_in_context) == semir_info;
  463. }));
  464. num_params_ = semir_info.num_params;
  465. lowered_param_indices_.reserve(num_params_);
  466. param_name_ids_.reserve(num_params_);
  467. param_types_.reserve(num_params_);
  468. param_di_types_.reserve(num_params_);
  469. if (!HandleReturnForm()) {
  470. return Abort();
  471. }
  472. int params_end = num_params_;
  473. if (semir_info.return_param_index >= 0) {
  474. CARBON_CHECK(semir_info.return_param_index == semir_info.num_params - 1,
  475. "Unexpected parameter order");
  476. params_end = semir_info.return_param_index;
  477. // Handle the return parameter first, because it goes first in the LLVM
  478. // convention.
  479. if (!HandleParameter(
  480. SemIR::CallParamIndex(semir_info.return_param_index))) {
  481. return Abort();
  482. }
  483. }
  484. for (int i : llvm::seq(params_end)) {
  485. if (!HandleParameter(SemIR::CallParamIndex(i))) {
  486. return Abort();
  487. }
  488. }
  489. return Finalize();
  490. }
  491. auto FileContext::FunctionTypeInfoBuilder::HandleReturnForm() -> bool {
  492. for (const auto& func_ctx : functions_) {
  493. if (TryHandleReturnForm(func_ctx)) {
  494. return true;
  495. }
  496. }
  497. return false;
  498. }
  499. auto FileContext::FunctionTypeInfoBuilder::TryHandleReturnForm(
  500. const FunctionInContext& func_ctx) -> bool {
  501. const auto& function =
  502. func_ctx.context->sem_ir().functions().Get(func_ctx.function_id);
  503. auto return_form_inst_id = function.return_form_inst_id;
  504. if (!return_form_inst_id.has_value()) {
  505. return SetReturnByCopy(func_ctx, SemIR::TypeId::None);
  506. }
  507. auto return_form_const_id = SemIR::GetConstantValueInSpecific(
  508. func_ctx.context->sem_ir(), func_ctx.specific_id, return_form_inst_id);
  509. auto return_form_inst = func_ctx.context->sem_ir().insts().Get(
  510. func_ctx.context->sem_ir().constant_values().GetInstId(
  511. return_form_const_id));
  512. CARBON_KIND_SWITCH(return_form_inst) {
  513. case CARBON_KIND(SemIR::InitForm init_form): {
  514. auto return_type_id =
  515. func_ctx.context->sem_ir().types().GetTypeIdForTypeConstantId(
  516. SemIR::GetConstantValueInSpecific(
  517. func_ctx.context->sem_ir(), func_ctx.specific_id,
  518. init_form.type_component_inst_id));
  519. switch (
  520. SemIR::InitRepr::ForType(func_ctx.context->sem_ir(), return_type_id)
  521. .kind) {
  522. case SemIR::InitRepr::InPlace: {
  523. return SetReturnInPlace(func_ctx, return_type_id);
  524. }
  525. case SemIR::InitRepr::ByCopy: {
  526. return SetReturnByCopy(func_ctx, return_type_id);
  527. }
  528. case SemIR::InitRepr::None:
  529. return SetReturnByCopy(func_ctx, SemIR::TypeId::None);
  530. case SemIR::InitRepr::Dependent:
  531. CARBON_FATAL("Lowering function return with dependent type: {0}",
  532. return_form_inst);
  533. case SemIR::InitRepr::Incomplete:
  534. case SemIR::InitRepr::Abstract:
  535. return false;
  536. }
  537. }
  538. case CARBON_KIND(SemIR::RefForm ref_form): {
  539. auto return_type_id =
  540. func_ctx.context->sem_ir().types().GetTypeIdForTypeConstantId(
  541. SemIR::GetConstantValueInSpecific(
  542. func_ctx.context->sem_ir(), func_ctx.specific_id,
  543. ref_form.type_component_inst_id));
  544. return SetReturnByReference(func_ctx, return_type_id);
  545. }
  546. case CARBON_KIND(SemIR::ValueForm val_form): {
  547. auto return_type_id =
  548. func_ctx.context->sem_ir().types().GetTypeIdForTypeConstantId(
  549. SemIR::GetConstantValueInSpecific(
  550. func_ctx.context->sem_ir(), func_ctx.specific_id,
  551. val_form.type_component_inst_id));
  552. switch (
  553. SemIR::ValueRepr::ForType(func_ctx.context->sem_ir(), return_type_id)
  554. .kind) {
  555. case SemIR::ValueRepr::Unknown:
  556. return false;
  557. case SemIR::ValueRepr::Dependent:
  558. CARBON_FATAL("Lowering function return with dependent type: {0}",
  559. return_form_inst);
  560. case SemIR::ValueRepr::None:
  561. return SetReturnByCopy(func_ctx, SemIR::TypeId::None);
  562. case SemIR::ValueRepr::Copy:
  563. return SetReturnByCopy(func_ctx, return_type_id);
  564. case SemIR::ValueRepr::Pointer:
  565. case SemIR::ValueRepr::Custom:
  566. return SetReturnByReference(func_ctx, return_type_id);
  567. }
  568. }
  569. default:
  570. CARBON_FATAL("Unexpected inst kind: {0}", return_form_inst);
  571. }
  572. }
  573. auto FileContext::FunctionTypeInfoBuilder::HandleParameter(
  574. SemIR::CallParamIndex index) -> bool {
  575. for (const auto& func_ctx : functions_) {
  576. if (TryHandleParameter(func_ctx, index)) {
  577. return true;
  578. }
  579. }
  580. return false;
  581. }
  582. auto FileContext::FunctionTypeInfoBuilder::TryHandleParameter(
  583. const FunctionInContext& func_ctx, SemIR::CallParamIndex index) -> bool {
  584. const auto& sem_ir = func_ctx.context->sem_ir();
  585. auto param_pattern_id =
  586. sem_ir.inst_blocks().Get(sem_ir.functions()
  587. .Get(func_ctx.function_id)
  588. .call_param_patterns_id)[index.index];
  589. auto param_pattern = sem_ir.insts().Get(param_pattern_id);
  590. auto param_type_id = ExtractScrutineeType(
  591. sem_ir, SemIR::GetTypeOfInstInSpecific(sem_ir, func_ctx.specific_id,
  592. param_pattern_id));
  593. // Returns the appropriate LoweredTypes for reference-like parameters.
  594. auto ref_lowered_types = [&]() -> LoweredTypes {
  595. return {
  596. .llvm_ir_type = llvm::PointerType::get(func_ctx.context->llvm_context(),
  597. /*AddressSpace=*/0),
  598. // TODO: replace this with a reference type.
  599. .llvm_di_type = GetLoweredTypes(func_ctx, param_type_id).llvm_di_type};
  600. };
  601. CARBON_CHECK(
  602. !param_type_id.AsConstantId().is_symbolic(),
  603. "Found symbolic type id after resolution when lowering type {0}.",
  604. param_pattern.type_id());
  605. auto param_kind = param_pattern.kind();
  606. // Treat a form parameter pattern like the kind of param pattern that
  607. // corresponds to its form.
  608. if (auto form_param_pattern =
  609. param_pattern.TryAs<SemIR::FormParamPattern>()) {
  610. CARBON_CHECK(!form_param_pattern->form_id.is_symbolic(), "TODO");
  611. auto form_inst_id =
  612. sem_ir.constant_values().GetInstId(form_param_pattern->form_id);
  613. auto form_kind = sem_ir.insts().Get(form_inst_id).kind();
  614. switch (form_kind) {
  615. case SemIR::InitForm::Kind:
  616. param_kind = SemIR::VarParamPattern::Kind;
  617. break;
  618. case SemIR::RefForm::Kind:
  619. param_kind = SemIR::RefParamPattern::Kind;
  620. break;
  621. case SemIR::ValueForm::Kind:
  622. param_kind = SemIR::ValueParamPattern::Kind;
  623. break;
  624. default:
  625. CARBON_FATAL("Unexpected kind {0} for form inst", form_kind);
  626. }
  627. }
  628. switch (param_kind) {
  629. case SemIR::RefParamPattern::Kind:
  630. case SemIR::VarParamPattern::Kind: {
  631. return AddLoweredParam(func_ctx, index, param_pattern_id,
  632. ref_lowered_types());
  633. }
  634. case SemIR::OutParamPattern::Kind: {
  635. switch (SemIR::InitRepr::ForType(sem_ir, param_type_id).kind) {
  636. case SemIR::InitRepr::InPlace:
  637. return AddLoweredParam(func_ctx, index, param_pattern_id,
  638. ref_lowered_types());
  639. case SemIR::InitRepr::ByCopy:
  640. case SemIR::InitRepr::None:
  641. return IgnoreParam(index);
  642. case SemIR::InitRepr::Dependent:
  643. CARBON_FATAL("Lowering function parameter with dependent type: {0}",
  644. param_pattern);
  645. case SemIR::InitRepr::Incomplete:
  646. case SemIR::InitRepr::Abstract:
  647. return false;
  648. }
  649. }
  650. case SemIR::ValueParamPattern::Kind: {
  651. switch (auto value_rep = SemIR::ValueRepr::ForType(sem_ir, param_type_id);
  652. value_rep.kind) {
  653. case SemIR::ValueRepr::Unknown:
  654. return false;
  655. case SemIR::ValueRepr::Dependent:
  656. CARBON_FATAL("Lowering function parameter with dependent type: {0}",
  657. param_pattern);
  658. case SemIR::ValueRepr::None:
  659. return IgnoreParam(index);
  660. case SemIR::ValueRepr::Copy:
  661. case SemIR::ValueRepr::Custom:
  662. case SemIR::ValueRepr::Pointer: {
  663. if (value_rep.type_id.has_value()) {
  664. return AddLoweredParam(
  665. func_ctx, index, param_pattern_id,
  666. GetLoweredTypes(func_ctx, value_rep.type_id));
  667. } else {
  668. return IgnoreParam(index);
  669. }
  670. }
  671. }
  672. }
  673. default:
  674. CARBON_FATAL("Unexpected inst kind: {0}", param_pattern);
  675. }
  676. }
  677. auto FileContext::FunctionTypeInfoBuilder::Finalize() -> FunctionTypeInfo {
  678. CARBON_CHECK(lowered_param_indices_.size() + unused_param_indices_.size() ==
  679. static_cast<size_t>(num_params_));
  680. CARBON_CHECK(!param_di_types_.empty());
  681. auto& di_builder = context_->di_builder();
  682. return {.type = llvm::FunctionType::get(return_type_, param_types_,
  683. /*isVarArg=*/false),
  684. .di_type = di_builder.createSubroutineType(
  685. di_builder.getOrCreateTypeArray(param_di_types_),
  686. llvm::DINode::FlagZero),
  687. .lowered_param_indices = std::move(lowered_param_indices_),
  688. .unused_param_indices = std::move(unused_param_indices_),
  689. .param_name_ids = std::move(param_name_ids_),
  690. .sret_type = sret_type_,
  691. .inexact = inexact_};
  692. }
  693. auto FileContext::FunctionTypeInfoBuilder::Abort() -> FunctionTypeInfo {
  694. num_params_ = 0;
  695. lowered_param_indices_.clear();
  696. unused_param_indices_.clear();
  697. param_name_ids_.clear();
  698. param_types_.clear();
  699. param_di_types_.clear();
  700. return_type_ = llvm::Type::getVoidTy(context_->llvm_context());
  701. param_di_types_.push_back(nullptr);
  702. inexact_ = true;
  703. return Finalize();
  704. }
  705. auto FileContext::FunctionTypeInfoBuilder::GetLoweredTypes(
  706. const FunctionInContext& func_ctx, SemIR::TypeId type_id) -> LoweredTypes {
  707. if (!type_id.has_value()) {
  708. return {
  709. .llvm_ir_type = llvm::Type::getVoidTy(func_ctx.context->llvm_context()),
  710. .llvm_di_type = nullptr};
  711. }
  712. auto result = func_ctx.context->GetTypeAndDIType(type_id);
  713. if (result.llvm_di_type == nullptr) {
  714. // TODO: figure out what type should go here, or ensure this doesn't
  715. // happen.
  716. result.llvm_di_type = GetPointerDIType(nullptr);
  717. }
  718. return result;
  719. }
  720. auto FileContext::HandleReferencedCppFunction(clang::FunctionDecl* cpp_decl)
  721. -> llvm::Function* {
  722. // Create the LLVM function (`CodeGenModule::GetOrCreateLLVMFunction()`)
  723. // so that code generation (`CodeGenModule::EmitGlobal()`) would see this
  724. // function name (`CodeGenModule::getMangledName()`), and will generate
  725. // its definition.
  726. auto* function_address = dyn_cast<llvm::Function>(
  727. cpp_code_generator_->GetAddrOfGlobal(CreateGlobalDecl(cpp_decl),
  728. /*isForDefinition=*/false));
  729. CARBON_CHECK(function_address);
  730. return function_address;
  731. }
  732. auto FileContext::HandleReferencedSpecificFunction(
  733. SemIR::FunctionId function_id, SemIR::SpecificId specific_id,
  734. llvm::Type* llvm_type) -> void {
  735. CARBON_CHECK(specific_id.has_value());
  736. // Add this specific function to a list of specific functions whose
  737. // definitions we need to emit.
  738. // TODO: Don't do this if we know this function is emitted as a
  739. // non-discardable symbol in the IR for some other file.
  740. context().AddPendingSpecificFunctionDefinition({.context = this,
  741. .function_id = function_id,
  742. .specific_id = specific_id});
  743. // Create a unique fingerprint for the function type.
  744. // For now, we compute the function type fingerprint only for specifics,
  745. // though we might need it for all functions in order to create a canonical
  746. // fingerprint across translation units.
  747. coalescer_.CreateTypeFingerprint(specific_id, llvm_type);
  748. }
  749. auto FileContext::GetOrCreateLLVMFunction(
  750. const FunctionTypeInfo& function_type_info, SemIR::FunctionId function_id,
  751. SemIR::SpecificId specific_id) -> llvm::Function* {
  752. // If this is a C++ function, tell Clang that we referenced it.
  753. if (auto clang_decl_id = sem_ir().functions().Get(function_id).clang_decl_id;
  754. clang_decl_id.has_value()) {
  755. CARBON_CHECK(!specific_id.has_value(),
  756. "Specific functions cannot have C++ definitions");
  757. return HandleReferencedCppFunction(
  758. sem_ir().clang_decls().Get(clang_decl_id).key.decl->getAsFunction());
  759. }
  760. SemIR::Mangler m(sem_ir(), context().total_ir_count());
  761. std::string mangled_name = m.Mangle(function_id, specific_id);
  762. if (auto* existing = llvm_module().getFunction(mangled_name)) {
  763. // We might have already lowered this function while lowering a different
  764. // file. That's OK.
  765. // TODO: If the prior function was inexact and the new one is not, we should
  766. // lower this new one and replace the existing function with it.
  767. // TODO: Check-fail or maybe diagnose if the two LLVM functions are not
  768. // produced by declarations of the same Carbon function. Name collisions
  769. // between non-private members of the same library should have been
  770. // diagnosed by check if detected, but it's not clear that check will
  771. // always be able to see this problem. In theory, name collisions could
  772. // also occur due to fingerprint collision.
  773. return existing;
  774. }
  775. // If this is a specific function, we may need to do additional work to
  776. // emit its definition.
  777. if (specific_id.has_value()) {
  778. HandleReferencedSpecificFunction(function_id, specific_id,
  779. function_type_info.type);
  780. }
  781. // TODO: For an imported inline function, consider generating an
  782. // `available_externally` definition.
  783. auto linkage = llvm::Function::ExternalLinkage;
  784. if (function_id == sem_ir().global_ctor_id()) {
  785. // The global constructor name would collide with global constructors for
  786. // other files in the same package, so use an internal linkage symbol.
  787. linkage = llvm::Function::InternalLinkage;
  788. } else if (specific_id.has_value()) {
  789. // Specific functions are allowed to be duplicated across files.
  790. // TODO: CoreWitness should have the same behavior; see its use of
  791. // WeakODRLinkage in BuildFunctionDefinition.
  792. linkage = llvm::Function::LinkOnceODRLinkage;
  793. }
  794. auto* llvm_function = llvm::Function::Create(function_type_info.type, linkage,
  795. mangled_name, llvm_module());
  796. CARBON_CHECK(llvm_function->getName() == mangled_name,
  797. "Mangled name collision: {0}", mangled_name);
  798. // Set up parameters and the return slot.
  799. for (auto [name_id, arg] : llvm::zip_equal(function_type_info.param_name_ids,
  800. llvm_function->args())) {
  801. arg.setName(sem_ir().names().GetIRBaseName(name_id));
  802. }
  803. if (function_type_info.sret_type != nullptr) {
  804. auto& return_arg = *llvm_function->args().begin();
  805. return_arg.addAttr(llvm::Attribute::getWithStructRetType(
  806. llvm_context(), function_type_info.sret_type));
  807. }
  808. return llvm_function;
  809. }
  810. auto FileContext::BuildFunctionDecl(SemIR::FunctionId function_id,
  811. SemIR::SpecificId specific_id,
  812. FileContext* fallback_file,
  813. SemIR::FunctionId fallback_function_id,
  814. SemIR::SpecificId fallback_specific_id)
  815. -> std::optional<FunctionInfo> {
  816. const auto& function = sem_ir().functions().Get(function_id);
  817. // Don't lower generic functions. Note that associated functions in interfaces
  818. // have `Self` in scope, so are implicitly generic functions.
  819. if (function.generic_id.has_value() && !specific_id.has_value()) {
  820. return std::nullopt;
  821. }
  822. // Don't lower builtins.
  823. if (function.builtin_function_kind() != SemIR::BuiltinFunctionKind::None) {
  824. return std::nullopt;
  825. }
  826. // Don't lower C++ functions that use a thunk. We will never reference them
  827. // directly, and their signatures would not be expected to match the
  828. // corresponding C++ function anyway.
  829. if (function.special_function_kind ==
  830. SemIR::Function::SpecialFunctionKind::HasCppThunk) {
  831. return std::nullopt;
  832. }
  833. // TODO: Consider tracking whether the function has been used, and only
  834. // lowering it if it's needed.
  835. FunctionTypeInfoBuilder::FunctionInContext func_infos[] = {
  836. {this, function_id, specific_id},
  837. {fallback_file, fallback_function_id, fallback_specific_id}};
  838. auto function_type_info =
  839. FunctionTypeInfoBuilder(llvm::ArrayRef(func_infos, fallback_file ? 2 : 1))
  840. .Build();
  841. auto* llvm_function =
  842. GetOrCreateLLVMFunction(function_type_info, function_id, specific_id);
  843. return {{.type = function_type_info.type,
  844. .di_type = function_type_info.di_type,
  845. .lowered_param_indices =
  846. std::move(function_type_info.lowered_param_indices),
  847. .unused_param_indices =
  848. std::move(function_type_info.unused_param_indices),
  849. .llvm_function = llvm_function,
  850. .inexact = function_type_info.inexact}};
  851. }
  852. // Find the file and function ID describing the definition of a function.
  853. static auto GetFunctionDefinition(const SemIR::File* decl_ir,
  854. SemIR::FunctionId function_id)
  855. -> std::pair<const SemIR::File*, SemIR::FunctionId> {
  856. // Find the file containing the definition.
  857. auto decl_id = decl_ir->functions().Get(function_id).definition_id;
  858. if (!decl_id.has_value()) {
  859. // Function is not defined.
  860. return {nullptr, SemIR::FunctionId::None};
  861. }
  862. // Find the function declaration this function was originally imported from.
  863. while (true) {
  864. auto import_inst_id = decl_ir->insts().GetImportSource(decl_id);
  865. if (!import_inst_id.has_value()) {
  866. break;
  867. }
  868. auto import_inst = decl_ir->import_ir_insts().Get(import_inst_id);
  869. decl_ir = decl_ir->import_irs().Get(import_inst.ir_id()).sem_ir;
  870. decl_id = import_inst.inst_id();
  871. }
  872. auto decl_ir_function_id =
  873. decl_ir->insts().GetAs<SemIR::FunctionDecl>(decl_id).function_id;
  874. return {decl_ir, decl_ir_function_id};
  875. }
  876. auto FileContext::BuildFunctionDefinition(SemIR::FunctionId function_id,
  877. SemIR::SpecificId specific_id)
  878. -> void {
  879. auto [definition_ir, definition_ir_function_id] =
  880. GetFunctionDefinition(&sem_ir(), function_id);
  881. if (!definition_ir) {
  882. // Function is probably defined in another file; not an error.
  883. return;
  884. }
  885. const auto& definition_function =
  886. definition_ir->functions().Get(definition_ir_function_id);
  887. BuildFunctionBody(
  888. function_id, specific_id, sem_ir().functions().Get(function_id),
  889. context().GetFileContext(definition_ir), definition_function);
  890. }
  891. auto FileContext::BuildFunctionBody(SemIR::FunctionId function_id,
  892. SemIR::SpecificId specific_id,
  893. const SemIR::Function& declaration_function,
  894. FileContext& definition_context,
  895. const SemIR::Function& definition_function)
  896. -> void {
  897. // On crash, report the function we were lowering.
  898. PrettyStackTraceFunction stack_trace_entry([&](llvm::raw_ostream& output) {
  899. SemIR::DiagnosticLocConverter converter(
  900. &context().tree_and_subtrees_getters(), &sem_ir());
  901. auto converted =
  902. converter.Convert(SemIR::LocId(declaration_function.definition_id),
  903. /*token_only=*/false);
  904. converted.loc.FormatLocation(output);
  905. output << "Lowering function ";
  906. if (specific_id.has_value()) {
  907. output << SemIR::StringifySpecific(sem_ir(), specific_id);
  908. } else {
  909. output << SemIR::StringifyConstantInst(
  910. sem_ir(), declaration_function.definition_id);
  911. }
  912. output << "\n";
  913. // Crash output has a tab indent; try to indent slightly past that.
  914. converted.loc.FormatSnippet(output, /*indent=*/10);
  915. });
  916. // Note that `definition_function` is potentially from a different SemIR::File
  917. // than the one that this file context represents. Any lowering done for
  918. // values derived from `definition_function` should use `definition_context`
  919. // instead of our context.
  920. const auto& definition_ir = definition_context.sem_ir();
  921. auto function_info = GetFunctionInfo(function_id, specific_id);
  922. CARBON_CHECK(function_info && function_info->llvm_function,
  923. "Attempting to define function that was not declared");
  924. CARBON_CHECK(!function_info->inexact,
  925. "Attempting to emit definition of inexact function: {0}",
  926. *function_info->llvm_function);
  927. // TODO: Build CoreWitness functions when they're called instead of when
  928. // they're defined. That should allow LinkOnceODRLinkage.
  929. if (declaration_function.special_function_kind ==
  930. SemIR::Function::SpecialFunctionKind::CoreWitness) {
  931. function_info->llvm_function->setLinkage(llvm::Function::WeakODRLinkage);
  932. }
  933. const auto& body_block_ids = definition_function.body_block_ids;
  934. CARBON_DCHECK(!body_block_ids.empty(),
  935. "No function body blocks found during lowering.");
  936. // Store which specifics were already lowered (with definitions) for each
  937. // generic.
  938. if (declaration_function.generic_id.has_value() && specific_id.has_value()) {
  939. // TODO: We should track this in the definition context instead so that we
  940. // can deduplicate specifics from different files.
  941. AddLoweredSpecificForGeneric(declaration_function.generic_id, specific_id);
  942. }
  943. // Set attributes on the function definition.
  944. {
  945. llvm::AttrBuilder attr_builder(llvm_context());
  946. attr_builder.addAttribute(llvm::Attribute::NoUnwind);
  947. // TODO: We should take the opt level from the SemIR file; it might not be
  948. // the same for all files in a compilation.
  949. if (context().opt_level() == Lower::OptimizationLevel::None) {
  950. // --optimize=none disables all optimizations for this function.
  951. attr_builder.addAttribute(llvm::Attribute::OptimizeNone);
  952. attr_builder.addAttribute(llvm::Attribute::NoInline);
  953. } else {
  954. // Otherwise, always inline thunks.
  955. if (definition_function.special_function_kind ==
  956. SemIR::Function::SpecialFunctionKind::Thunk) {
  957. attr_builder.addAttribute(llvm::Attribute::AlwaysInline);
  958. }
  959. // Convert --optimize=size into optsize and minsize.
  960. if (context().opt_level() == Lower::OptimizationLevel::Size) {
  961. attr_builder.addAttribute(llvm::Attribute::OptimizeForSize);
  962. attr_builder.addAttribute(llvm::Attribute::MinSize);
  963. }
  964. // TODO: Should we generate an InlineHint for some functions? Perhaps for
  965. // those defined in the API file?
  966. }
  967. function_info->llvm_function->addFnAttrs(attr_builder);
  968. }
  969. auto* subprogram = BuildDISubprogram(declaration_function, *function_info);
  970. FunctionContext function_lowering(
  971. definition_context, function_info->llvm_function, *this, specific_id,
  972. coalescer_.InitializeFingerprintForSpecific(specific_id), subprogram,
  973. vlog_stream_);
  974. auto call_param_ids = definition_ir.inst_blocks().GetOrEmpty(
  975. definition_function.call_params_id);
  976. // Add local variables for the parameters.
  977. for (auto [llvm_index, index] :
  978. llvm::enumerate(function_info->lowered_param_indices)) {
  979. function_lowering.SetLocal(
  980. call_param_ids[index.index],
  981. function_info->llvm_function->getArg(llvm_index));
  982. }
  983. // Add local variables for the SemIR parameters that aren't LLVM parameters.
  984. // These shouldn't actually be used, so they're set to poison values.
  985. for (auto [llvm_index, index] :
  986. llvm::enumerate(function_info->unused_param_indices)) {
  987. auto param_id = call_param_ids[index.index];
  988. function_lowering.SetLocal(
  989. param_id,
  990. llvm::PoisonValue::get(function_lowering.GetTypeOfInst(param_id)));
  991. }
  992. auto decl_block_id = SemIR::InstBlockId::None;
  993. if (function_id == sem_ir().global_ctor_id()) {
  994. decl_block_id = SemIR::InstBlockId::Empty;
  995. } else {
  996. decl_block_id =
  997. definition_ir.insts()
  998. .GetAs<SemIR::FunctionDecl>(definition_function.latest_decl_id())
  999. .decl_block_id;
  1000. }
  1001. // Lowers the contents of decl_block_id into the corresponding LLVM block,
  1002. // creating it if it doesn't already exist.
  1003. auto lower_block = [&](SemIR::InstBlockId block_id) {
  1004. CARBON_VLOG("Lowering {0}\n", block_id);
  1005. auto* llvm_block = function_lowering.GetBlock(block_id);
  1006. // Keep the LLVM blocks in lexical order.
  1007. llvm_block->moveBefore(function_info->llvm_function->end());
  1008. function_lowering.builder().SetInsertPoint(llvm_block);
  1009. function_lowering.LowerBlockContents(block_id);
  1010. };
  1011. lower_block(decl_block_id);
  1012. // If the decl block is empty, reuse it as the first body block. We don't do
  1013. // this when the decl block is non-empty so that any branches back to the
  1014. // first body block don't also re-execute the decl.
  1015. llvm::BasicBlock* block = function_lowering.builder().GetInsertBlock();
  1016. if (block->empty() &&
  1017. function_lowering.TryToReuseBlock(body_block_ids.front(), block)) {
  1018. // Reuse this block as the first block of the function body.
  1019. } else {
  1020. function_lowering.builder().CreateBr(
  1021. function_lowering.GetBlock(body_block_ids.front()));
  1022. }
  1023. // Lower all blocks.
  1024. for (auto block_id : body_block_ids) {
  1025. lower_block(block_id);
  1026. }
  1027. // LLVM requires that the entry block has no predecessors.
  1028. auto* entry_block = &function_info->llvm_function->getEntryBlock();
  1029. if (entry_block->hasNPredecessorsOrMore(1)) {
  1030. auto* new_entry_block = llvm::BasicBlock::Create(
  1031. llvm_context(), "entry", function_info->llvm_function, entry_block);
  1032. llvm::UncondBrInst::Create(entry_block, new_entry_block);
  1033. }
  1034. // Emit fingerprint accumulated inside the function context.
  1035. function_lowering.EmitFinalFingerprint();
  1036. context().di_builder().finalizeSubprogram(subprogram);
  1037. }
  1038. auto FileContext::BuildDISubprogram(const SemIR::Function& function,
  1039. const FunctionInfo& function_info)
  1040. -> llvm::DISubprogram* {
  1041. if (!context().di_compile_unit()) {
  1042. return nullptr;
  1043. }
  1044. auto name = sem_ir().names().GetAsStringIfIdentifier(function.name_id);
  1045. CARBON_CHECK(name, "Unexpected special name for function: {0}",
  1046. function.name_id);
  1047. auto loc = GetLocForDI(function.definition_id);
  1048. llvm::DISubroutineType* subroutine_type = function_info.di_type;
  1049. auto* subprogram = context().di_builder().createFunction(
  1050. context().di_compile_unit(), *name,
  1051. function_info.llvm_function->getName(),
  1052. /*File=*/context().di_builder().createFile(loc.filename, ""),
  1053. /*LineNo=*/loc.line_number, subroutine_type,
  1054. /*ScopeLine=*/0, llvm::DINode::FlagZero,
  1055. llvm::DISubprogram::SPFlagDefinition);
  1056. // Add a variable for each parameter, as that is where DWARF debug information
  1057. // comes from.
  1058. // TODO: this doesn't declare a variable for the output parameter. Is that
  1059. // what we want?
  1060. for (auto [argument_number, type] :
  1061. llvm::enumerate(llvm::drop_begin(subroutine_type->getTypeArray()))) {
  1062. context().di_builder().createParameterVariable(
  1063. subprogram, "", argument_number + 1, nullptr, 0, type,
  1064. /*AlwaysPreserve=*/true);
  1065. }
  1066. return subprogram;
  1067. }
  1068. // BuildTypeForInst is used to construct types for FileContext::BuildType below.
  1069. // Implementations return the LLVM type for the instruction. This first overload
  1070. // is the fallback handler for non-type instructions.
  1071. template <typename InstT>
  1072. requires(InstT::Kind.is_type() == SemIR::InstIsType::Never)
  1073. static auto BuildTypeForInst(FileContext& /*context*/, InstT inst)
  1074. -> FileContext::LoweredTypes {
  1075. CARBON_FATAL("Cannot use inst as type: {0}", inst);
  1076. }
  1077. template <typename InstT>
  1078. requires(InstT::Kind.is_symbolic_when_type())
  1079. static auto BuildTypeForInst(FileContext& context, InstT /*inst*/)
  1080. -> FileContext::LoweredTypes {
  1081. // Treat non-monomorphized symbolic types as opaque.
  1082. return {llvm::StructType::get(context.llvm_context()), nullptr};
  1083. }
  1084. static auto BuildTypeForInst(FileContext& context, SemIR::ArrayType inst)
  1085. -> FileContext::LoweredTypes {
  1086. return {llvm::ArrayType::get(
  1087. context.GetType(context.sem_ir().types().GetTypeIdForTypeInstId(
  1088. inst.element_type_inst_id)),
  1089. *context.sem_ir().GetArrayBoundValue(inst.bound_id)),
  1090. nullptr};
  1091. }
  1092. static auto BuildTypeForInst(FileContext& context, SemIR::BoolType /*inst*/)
  1093. -> FileContext::LoweredTypes {
  1094. // TODO: We may want to have different representations for `bool` storage
  1095. // (`i8`) versus for `bool` values (`i1`).
  1096. return {llvm::Type::getInt1Ty(context.llvm_context()), nullptr};
  1097. }
  1098. static auto BuildTypeForInst(FileContext& context, SemIR::ClassType inst)
  1099. -> FileContext::LoweredTypes {
  1100. auto object_repr_id = context.sem_ir()
  1101. .classes()
  1102. .Get(inst.class_id)
  1103. .GetObjectRepr(context.sem_ir(), inst.specific_id);
  1104. return context.GetTypeAndDIType(object_repr_id);
  1105. }
  1106. template <typename InstT>
  1107. requires(SemIR::Internal::HasInstCategory<SemIR::AnyQualifiedType, InstT>)
  1108. static auto BuildTypeForInst(FileContext& context, InstT inst)
  1109. -> FileContext::LoweredTypes {
  1110. return {context.GetType(
  1111. context.sem_ir().types().GetTypeIdForTypeInstId(inst.inner_id)),
  1112. nullptr};
  1113. }
  1114. static auto BuildTypeForInst(FileContext& context, SemIR::CustomLayoutType inst)
  1115. -> FileContext::LoweredTypes {
  1116. auto layout = context.sem_ir().custom_layouts().Get(inst.layout_id);
  1117. return {llvm::ArrayType::get(llvm::Type::getInt8Ty(context.llvm_context()),
  1118. layout[SemIR::CustomLayoutId::SizeIndex]),
  1119. nullptr};
  1120. }
  1121. static auto BuildTypeForInst(FileContext& context,
  1122. SemIR::ImplWitnessAssociatedConstant inst)
  1123. -> FileContext::LoweredTypes {
  1124. return {context.GetType(inst.type_id), nullptr};
  1125. }
  1126. static auto BuildTypeForInst(FileContext& /*context*/,
  1127. SemIR::ErrorInst /*inst*/)
  1128. -> FileContext::LoweredTypes {
  1129. // This is a complete type but uses of it should never be lowered.
  1130. return {nullptr, nullptr};
  1131. }
  1132. static auto BuildTypeForInst(FileContext& context, SemIR::FloatType inst)
  1133. -> FileContext::LoweredTypes {
  1134. return {llvm::Type::getFloatingPointTy(context.llvm_context(),
  1135. inst.float_kind.Semantics()),
  1136. nullptr};
  1137. }
  1138. static auto BuildTypeForInst(FileContext& context, SemIR::IntType inst)
  1139. -> FileContext::LoweredTypes {
  1140. auto width_inst =
  1141. context.sem_ir().insts().TryGetAs<SemIR::IntValue>(inst.bit_width_id);
  1142. CARBON_CHECK(width_inst, "Can't lower int type with symbolic width");
  1143. auto width = context.sem_ir().ints().Get(width_inst->int_id).getZExtValue();
  1144. return {llvm::IntegerType::get(context.llvm_context(), width),
  1145. context.context().di_builder().createBasicType(
  1146. "int", width,
  1147. inst.int_kind.is_signed() ? llvm::dwarf::DW_ATE_signed
  1148. : llvm::dwarf::DW_ATE_unsigned)};
  1149. }
  1150. static auto BuildTypeForInst(FileContext& context, SemIR::PointerType /*inst*/)
  1151. -> FileContext::LoweredTypes {
  1152. return {llvm::PointerType::get(context.llvm_context(), /*AddressSpace=*/0),
  1153. nullptr};
  1154. }
  1155. static auto BuildTypeForInst(FileContext& /*context*/,
  1156. SemIR::PatternType /*inst*/)
  1157. -> FileContext::LoweredTypes {
  1158. CARBON_FATAL("Unexpected pattern type in lowering");
  1159. }
  1160. static auto BuildTypeForInst(FileContext& context, SemIR::StructType inst)
  1161. -> FileContext::LoweredTypes {
  1162. auto fields = context.sem_ir().struct_type_fields().Get(inst.fields_id);
  1163. llvm::SmallVector<llvm::Type*> subtypes;
  1164. subtypes.reserve(fields.size());
  1165. for (auto field : fields) {
  1166. subtypes.push_back(context.GetType(
  1167. context.sem_ir().types().GetTypeIdForTypeInstId(field.type_inst_id)));
  1168. }
  1169. return {llvm::StructType::get(context.llvm_context(), subtypes), nullptr};
  1170. }
  1171. static auto BuildTypeForInst(FileContext& context, SemIR::TupleType inst)
  1172. -> FileContext::LoweredTypes {
  1173. // TODO: Investigate special-casing handling of empty tuples so that they
  1174. // can be collectively replaced with LLVM's void, particularly around
  1175. // function returns. LLVM doesn't allow declaring variables with a void
  1176. // type, so that may require significant special casing.
  1177. auto elements = context.sem_ir().inst_blocks().Get(inst.type_elements_id);
  1178. llvm::SmallVector<llvm::Type*> subtypes;
  1179. subtypes.reserve(elements.size());
  1180. for (auto type_id : context.sem_ir().types().GetBlockAsTypeIds(elements)) {
  1181. subtypes.push_back(context.GetType(type_id));
  1182. }
  1183. return {llvm::StructType::get(context.llvm_context(), subtypes), nullptr};
  1184. }
  1185. static auto BuildTypeForInst(FileContext& context, SemIR::TypeType /*inst*/)
  1186. -> FileContext::LoweredTypes {
  1187. return {context.GetTypeType(), nullptr};
  1188. }
  1189. static auto BuildTypeForInst(FileContext& context, SemIR::FormType /*inst*/)
  1190. -> FileContext::LoweredTypes {
  1191. return {context.GetFormType(), nullptr};
  1192. }
  1193. static auto BuildTypeForInst(FileContext& context, SemIR::VtableType /*inst*/)
  1194. -> FileContext::LoweredTypes {
  1195. return {llvm::Type::getVoidTy(context.llvm_context()), nullptr};
  1196. }
  1197. static auto BuildTypeForInst(FileContext& context,
  1198. SemIR::SpecificFunctionType /*inst*/)
  1199. -> FileContext::LoweredTypes {
  1200. return {llvm::PointerType::get(context.llvm_context(), 0), nullptr};
  1201. }
  1202. template <typename InstT>
  1203. requires(InstT::Kind.template IsAnyOf<
  1204. SemIR::AssociatedEntityType, SemIR::AutoType, SemIR::BoundMethodType,
  1205. SemIR::CharLiteralType, SemIR::CppOverloadSetType,
  1206. SemIR::CppTemplateNameType, SemIR::FacetType,
  1207. SemIR::FloatLiteralType, SemIR::FunctionType,
  1208. SemIR::FunctionTypeWithSelfType, SemIR::GenericClassType,
  1209. SemIR::GenericInterfaceType, SemIR::GenericNamedConstraintType,
  1210. SemIR::InstType, SemIR::IntLiteralType, SemIR::NamespaceType,
  1211. SemIR::RequireSpecificDefinitionType, SemIR::UnboundElementType,
  1212. SemIR::WhereExpr, SemIR::WitnessType>())
  1213. static auto BuildTypeForInst(FileContext& context, InstT /*inst*/)
  1214. -> FileContext::LoweredTypes {
  1215. // Return an empty struct as a placeholder.
  1216. // TODO: Should we model an interface as a witness table, or an associated
  1217. // entity as an index?
  1218. return {llvm::StructType::get(context.llvm_context()), nullptr};
  1219. }
  1220. auto FileContext::BuildType(SemIR::InstId inst_id) -> LoweredTypes {
  1221. // Use overload resolution to select the implementation, producing compile
  1222. // errors when BuildTypeForInst isn't defined for a given instruction.
  1223. CARBON_KIND_SWITCH(sem_ir_->insts().Get(inst_id)) {
  1224. #define CARBON_SEM_IR_INST_KIND(Name) \
  1225. case CARBON_KIND(SemIR::Name inst): { \
  1226. return BuildTypeForInst(*this, inst); \
  1227. }
  1228. #include "toolchain/sem_ir/inst_kind.def"
  1229. }
  1230. }
  1231. auto FileContext::BuildGlobalVariableDecl(SemIR::VarStorage var_storage)
  1232. -> llvm::Constant* {
  1233. auto var_name_id =
  1234. SemIR::GetFirstBindingNameFromPatternId(sem_ir(), var_storage.pattern_id);
  1235. if (auto cpp_global_var_id =
  1236. sem_ir().cpp_global_vars().Lookup({.entity_name_id = var_name_id});
  1237. cpp_global_var_id.has_value()) {
  1238. SemIR::ClangDeclId clang_decl_id =
  1239. sem_ir().cpp_global_vars().Get(cpp_global_var_id).clang_decl_id;
  1240. CARBON_CHECK(clang_decl_id.has_value(),
  1241. "CppGlobalVar should have a clang_decl_id");
  1242. return cpp_code_generator_->GetAddrOfGlobal(
  1243. cast<clang::VarDecl>(
  1244. sem_ir().clang_decls().Get(clang_decl_id).key.decl),
  1245. /*isForDefinition=*/false);
  1246. }
  1247. return BuildNonCppGlobalVariableDecl(var_storage);
  1248. }
  1249. auto FileContext::BuildNonCppGlobalVariableDecl(SemIR::VarStorage var_storage)
  1250. -> llvm::GlobalVariable* {
  1251. SemIR::Mangler m(sem_ir(), context().total_ir_count());
  1252. auto mangled_name = m.MangleGlobalVariable(var_storage.pattern_id);
  1253. auto linkage = llvm::GlobalVariable::ExternalLinkage;
  1254. // If the variable doesn't have an externally-visible name, demote it to
  1255. // internal linkage and invent a plausible name that shouldn't collide with
  1256. // any of our real manglings.
  1257. if (mangled_name.empty()) {
  1258. linkage = llvm::GlobalVariable::InternalLinkage;
  1259. if (inst_namer_) {
  1260. mangled_name =
  1261. ("var.anon" + inst_namer_->GetUnscopedNameFor(var_storage.pattern_id))
  1262. .str();
  1263. }
  1264. }
  1265. auto* type = GetType(var_storage.type_id);
  1266. return new llvm::GlobalVariable(llvm_module(), type,
  1267. /*isConstant=*/false, linkage,
  1268. /*Initializer=*/nullptr, mangled_name);
  1269. }
  1270. auto FileContext::GetLocForDI(SemIR::InstId inst_id) -> Context::LocForDI {
  1271. auto abs_node_id = GetAbsoluteNodeId(sem_ir_, SemIR::LocId(inst_id)).back();
  1272. if (abs_node_id.check_ir_id() == SemIR::CheckIRId::Cpp) {
  1273. // TODO: Consider asking our cpp_code_generator to map the location to a
  1274. // debug location, in order to use Clang's rules for (eg) macro handling.
  1275. auto loc =
  1276. sem_ir().clang_source_locs().Get(abs_node_id.clang_source_loc_id());
  1277. auto presumed_loc =
  1278. sem_ir().cpp_file()->source_manager().getPresumedLoc(loc);
  1279. return {.filename = presumed_loc.getFilename(),
  1280. .line_number = static_cast<int32_t>(presumed_loc.getLine()),
  1281. .column_number = static_cast<int32_t>(presumed_loc.getColumn())};
  1282. }
  1283. return context().GetLocForDI(abs_node_id);
  1284. }
  1285. auto FileContext::BuildVtable(const SemIR::Vtable& vtable,
  1286. SemIR::SpecificId specific_id)
  1287. -> llvm::GlobalVariable* {
  1288. const auto& class_info = sem_ir().classes().Get(vtable.class_id);
  1289. SemIR::Mangler m(sem_ir(), context().total_ir_count());
  1290. std::string mangled_name = m.MangleVTable(class_info, specific_id);
  1291. if (sem_ir()
  1292. .insts()
  1293. .GetImportSource(class_info.first_owning_decl_id)
  1294. .has_value()) {
  1295. // Emit a declaration of an imported vtable using a(n opaque) pointer type.
  1296. // This doesn't have to match the definition that appears elsewhere, it'll
  1297. // still get merged correctly.
  1298. auto* gv = new llvm::GlobalVariable(
  1299. llvm_module(),
  1300. llvm::PointerType::get(llvm_context(), /*AddressSpace=*/0),
  1301. /*isConstant=*/true, llvm::GlobalValue::ExternalLinkage, nullptr,
  1302. mangled_name);
  1303. gv->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
  1304. return gv;
  1305. }
  1306. auto vtable_inst_block =
  1307. sem_ir().inst_blocks().Get(vtable.virtual_functions_id);
  1308. auto* entry_type = llvm::IntegerType::getInt32Ty(llvm_context());
  1309. auto* table_type = llvm::ArrayType::get(entry_type, vtable_inst_block.size());
  1310. auto* llvm_vtable = new llvm::GlobalVariable(
  1311. llvm_module(), table_type, /*isConstant=*/true,
  1312. llvm::GlobalValue::ExternalLinkage, nullptr, mangled_name);
  1313. auto* i32_type = llvm::IntegerType::getInt32Ty(llvm_context());
  1314. auto* i64_type = llvm::IntegerType::getInt64Ty(llvm_context());
  1315. auto* vtable_const_int =
  1316. llvm::ConstantExpr::getPtrToInt(llvm_vtable, i64_type);
  1317. llvm::SmallVector<llvm::Constant*> vfuncs;
  1318. vfuncs.reserve(vtable_inst_block.size());
  1319. for (auto fn_decl_id : vtable_inst_block) {
  1320. auto [_1, _2, fn_id, fn_specific_id] =
  1321. DecomposeVirtualFunction(sem_ir(), fn_decl_id, specific_id);
  1322. vfuncs.push_back(llvm::ConstantExpr::getTrunc(
  1323. llvm::ConstantExpr::getSub(
  1324. llvm::ConstantExpr::getPtrToInt(
  1325. GetOrCreateFunctionInfo(fn_id, fn_specific_id)->llvm_function,
  1326. i64_type),
  1327. vtable_const_int),
  1328. i32_type));
  1329. }
  1330. llvm_vtable->setInitializer(llvm::ConstantArray::get(table_type, vfuncs));
  1331. llvm_vtable->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
  1332. return llvm_vtable;
  1333. }
  1334. } // namespace Carbon::Lower