file_context.cpp 67 KB

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