file_context.cpp 52 KB

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