file_context.cpp 55 KB

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