type.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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/type.h"
  5. #include "common/check.h"
  6. #include "llvm/ADT/ArrayRef.h"
  7. #include "llvm/ADT/STLExtras.h"
  8. #include "llvm/ADT/Sequence.h"
  9. #include "llvm/ADT/SmallVector.h"
  10. #include "llvm/IR/DebugInfoMetadata.h"
  11. #include "llvm/IR/DerivedTypes.h"
  12. #include "toolchain/base/kind_switch.h"
  13. #include "toolchain/lower/file_context.h"
  14. #include "toolchain/sem_ir/file.h"
  15. #include "toolchain/sem_ir/ids.h"
  16. #include "toolchain/sem_ir/inst.h"
  17. #include "toolchain/sem_ir/inst_kind.h"
  18. #include "toolchain/sem_ir/pattern.h"
  19. #include "toolchain/sem_ir/typed_insts.h"
  20. namespace Carbon::Lower {
  21. namespace {
  22. // State machine for building a FunctionTypeInfo from SemIR.
  23. //
  24. // The main difficulty this class encapsulates is that each abstraction level
  25. // has different expectations about how the return is reflected in the parameter
  26. // list.
  27. // - In SemIR, if the function has an initializing return form, it has a
  28. // corresponding output parameter at the end of the parameter list.
  29. // - In LLVM IR, if the SemIR has an output parameter _and_ that parameter's
  30. // type has an in-place initializing representation, we emit a corresponding
  31. // `sret` output parameter (and the function's return type is void). By
  32. // convention the output parameter goes at the start of the parameter list.
  33. // - In LLVM debug info, the list of parameter types always starts with the
  34. // return type (which doubles as the type of the return parameter, if there
  35. // is one).
  36. //
  37. // Furthermore, SemIR is designed to eventually support compound return forms,
  38. // in which case there can be multiple output parameters for different pieces of
  39. // the return form, but it's not yet clear how we will lower such functions.
  40. //
  41. // We also deal with the case where the function signature involves incomplete
  42. // types. This can happen if the function is declared but never defined nor
  43. // called in this file. Declarations of such functions can still need to be
  44. // emitted; currently this happens if they are part of a class's vtable. Such
  45. // uses do not need an exact signature, so we emit them with the LLVM type
  46. // `void()` and set `inexact` on the result to indicate the type is not known.
  47. // LLVM can handle merging inexact and exact signatures, and this matches how
  48. // Clang handles the corresponding situation in C++.
  49. //
  50. // One additional complexity is that we may need to fetch information about the
  51. // same function from multiple different files. For a call to a generic
  52. // function, there may be no single file in which all the relevant types are
  53. // complete, so we will look at both the specific function definition that is
  54. // the resolved callee, as well as the partially-specific function from the call
  55. // site.
  56. //
  57. // In general, we support being given a list of variants of the function, in
  58. // which the first function in the list is the primary declaration and should be
  59. // the most specific function, and the others are used as fallbacks if an
  60. // incomplete type is encountered.
  61. class FunctionTypeInfoBuilder {
  62. public:
  63. // Creates a FunctionTypeInfoBuilder that uses the given functions.
  64. explicit FunctionTypeInfoBuilder(llvm::ArrayRef<FunctionInContext> functions)
  65. : context_(&functions.front().context->context()), functions_(functions) {
  66. CARBON_CHECK(!functions_.empty());
  67. }
  68. // Retrieves various features of the function's type useful for constructing
  69. // the `llvm::Type` and `llvm::DISubroutineType` for the `llvm::Function`. If
  70. // any part of the type can't be manifest (eg: incomplete return or parameter
  71. // types), then the result is as if the type was `void()`. Should only be
  72. // called once on a given builder.
  73. auto Build() && -> FunctionTypeInfo;
  74. private:
  75. // By convention, state transition methods return false (without changing the
  76. // accumulated information about the function) to indicate that we could not
  77. // manifest the complete function type successfully in this context.
  78. // Information about how a function is called in SemIR.
  79. struct SemIRIndexInfo {
  80. // The number of parameters in the SemIR call signature.
  81. int num_params;
  82. // The index of the first return parameter in the SemIR call signature.
  83. int return_param_index;
  84. friend auto operator==(const SemIRIndexInfo& lhs, const SemIRIndexInfo& rhs)
  85. -> bool = default;
  86. };
  87. // Get information about the SemIR function signature.
  88. auto GetSemIRIndexInfo(const FunctionInContext& fn_in_context)
  89. -> SemIRIndexInfo {
  90. const auto& sem_ir = fn_in_context.context->sem_ir();
  91. const auto& function = sem_ir.functions().Get(fn_in_context.function_id);
  92. int num_params =
  93. sem_ir.inst_blocks().Get(function.call_param_patterns_id).size();
  94. int return_param_index = -1;
  95. if (function.call_param_ranges.return_size() > 0) {
  96. CARBON_CHECK(function.call_param_ranges.return_size() == 1,
  97. "TODO: support multiple return forms");
  98. return_param_index = function.call_param_ranges.return_begin().index;
  99. }
  100. return {.num_params = num_params, .return_param_index = return_param_index};
  101. }
  102. // Handles the function's return form.
  103. //
  104. // This should be called before `HandleParameter`. It handles the return form
  105. // by trying each `FunctionInContext` until one succeeds, and returns false if
  106. // all attempts failed.
  107. auto HandleReturnForm() -> bool;
  108. // Tries to handle the return form using the given context. Delegates to
  109. // exactly one of `SetReturnByCopy`, `SetReturnByReference`, or
  110. // `SetReturnInPlace`, or returns false if the return type is incomplete.
  111. auto TryHandleReturnForm(const FunctionInContext& func_ctx) -> bool;
  112. // Records that the LLVM function returns by copy, with type `return_type_id`.
  113. // `return_type_id` can be `None`, which is treated as equivalent to the
  114. // default return type `()`.
  115. auto SetReturnByCopy(const FunctionInContext& func_ctx,
  116. SemIR::TypeId return_type_id) -> bool {
  117. CARBON_CHECK(return_type_ == nullptr);
  118. CARBON_CHECK(param_di_types_.empty());
  119. auto lowered_return_types = GetLoweredTypes(func_ctx, return_type_id);
  120. return_type_ = lowered_return_types.llvm_ir_type;
  121. param_di_types_.push_back(lowered_return_types.llvm_di_type);
  122. return true;
  123. }
  124. // Records that the LLVM function returns by reference, with type
  125. // `return_type_id`.
  126. auto SetReturnByReference(const FunctionInContext& func_ctx,
  127. SemIR::TypeId /*return_type_id*/) -> bool {
  128. return_type_ = llvm::PointerType::get(func_ctx.context->llvm_context(),
  129. /*AddressSpace=*/0);
  130. // TODO: replace this with a reference type.
  131. param_di_types_.push_back(GetPointerDIType(nullptr));
  132. return true;
  133. }
  134. // Records that the LLVM function returns in place, with type
  135. // `return_type_id`.
  136. auto SetReturnInPlace(const FunctionInContext& func_ctx,
  137. SemIR::TypeId return_type_id) -> bool {
  138. return_type_ = llvm::Type::getVoidTy(func_ctx.context->llvm_context());
  139. sret_type_ = func_ctx.context->GetType(return_type_id);
  140. // We don't add to param_di_types_ because that will be handled by the
  141. // loop over the SemIR parameters.
  142. return true;
  143. }
  144. // Handles `Call` parameter pattern at the given index. This should be called
  145. // on parameter patterns in the order that they should appear in the LLVM IR
  146. // parameter list, so in particular it should be called on the
  147. // `OutParamPattern` (if any) first. It should be called on all `Call`
  148. // parameters; it will determine which parameters belong in the LLVM IR
  149. // parameter list.
  150. //
  151. // This tries each `FunctionInContext` until one succeeds, and returns false
  152. // if all attempts failed.
  153. auto HandleParameter(SemIR::CallParamIndex index) -> bool;
  154. // Tries to handle the parameter pattern at the given index using the given
  155. // context. Delegates to either `AddLoweredParam` or `IgnoreParam`, or returns
  156. // false if the parameter type is incomplete.
  157. auto TryHandleParameter(const FunctionInContext& func_ctx,
  158. SemIR::CallParamIndex index) -> bool;
  159. // Records that the parameter pattern at the given index has the given ID, and
  160. // lowers to the given IR and DI types.
  161. auto AddLoweredParam(const FunctionInContext& func_ctx,
  162. SemIR::CallParamIndex index,
  163. SemIR::InstId param_pattern_id, LoweredTypes param_types)
  164. -> bool {
  165. lowered_param_indices_.push_back(index);
  166. param_name_ids_.push_back(SemIR::GetPrettyNameFromPatternId(
  167. func_ctx.context->sem_ir(), param_pattern_id));
  168. param_types_.push_back(param_types.llvm_ir_type);
  169. param_di_types_.push_back(param_types.llvm_di_type);
  170. return true;
  171. }
  172. // Records that the `Call` parameter pattern at the given index is not lowered
  173. // to an LLVM parameter.
  174. auto IgnoreParam(SemIR::CallParamIndex index) -> bool {
  175. unused_param_indices_.push_back(index);
  176. return true;
  177. }
  178. // Builds and returns a FunctionTypeInfo from the accumulated information.
  179. auto Finalize() -> FunctionTypeInfo;
  180. // Clears out accumulated state and returns a FunctionTypeInfo with the
  181. // fallback state `void()`.
  182. auto Abort() -> FunctionTypeInfo;
  183. // Returns LLVM IR and DI types for the given SemIR type. This is not a state
  184. // transition. It mostly delegates to context_.GetTypeAndDIType, but treats
  185. // TypeId::None as equivalent to the unit type, and uses an untyped pointer as
  186. // a placeholder DI type if context_ doesn't provide one.
  187. auto GetLoweredTypes(const FunctionInContext& func_ctx, SemIR::TypeId type_id)
  188. -> LoweredTypes;
  189. // Returns a DI type for a pointer to the given pointee. The pointee type may
  190. // be null.
  191. auto GetPointerDIType(llvm::DIType* pointee_type, unsigned address_space = 0)
  192. -> llvm::DIDerivedType* {
  193. const auto& data_layout = context_->llvm_module().getDataLayout();
  194. return context_->di_builder().createPointerType(
  195. pointee_type, data_layout.getPointerSizeInBits(address_space));
  196. }
  197. Context* context_;
  198. llvm::ArrayRef<FunctionInContext> functions_;
  199. // The number of input `Call` parameter patterns.
  200. int num_params_ = 0;
  201. // The types of the parameters in the LLVM IR function. Each one corresponds
  202. // to a SemIR `Call` parameter, but some `Call` parameters may be omitted
  203. // (e.g. if they are stateless) or reordered (e.g. the return parameter, if
  204. // any, always goes first).
  205. llvm::SmallVector<llvm::Type*> param_types_;
  206. // The LLLVM DI representation of the parameter list. As required by LLVM DI
  207. // convention, this starts with the function's return type, and ends with the
  208. // DI representations of param_types_ (in the same order). Note that those
  209. // two ranges may overlap: if the first element of param_types_ represents
  210. // a return parameter, the first element of param_di_types_ corresponds to it
  211. // while also representing the return type.
  212. llvm::SmallVector<llvm::Metadata*> param_di_types_;
  213. // The indices of the `Call` parameters that correspond to `param_types_`, in
  214. // the same order.
  215. llvm::SmallVector<SemIR::CallParamIndex> lowered_param_indices_;
  216. // The names of the `Call` parameters that correspond to `param_types_`, in
  217. // the same order.
  218. llvm::SmallVector<SemIR::NameId> param_name_ids_;
  219. // The indices of any `Call` param patterns that aren't present in
  220. // lowered_param_indices_.
  221. llvm::SmallVector<SemIR::CallParamIndex> unused_param_indices_;
  222. // The LLVM function's return type.
  223. llvm::Type* return_type_ = nullptr;
  224. // If not null, the LLVM function's first parameter should have a `sret`
  225. // attribute with this type.
  226. llvm::Type* sret_type_ = nullptr;
  227. // Whether we failed to form an exact description of the function type. This
  228. // can happen if a parameter or return type is incomplete. In this case, we
  229. // can still sometimes need to emit a declaration of the function, for example
  230. // because it appears in a vtable, but we cannot emit a definition or a call.
  231. bool inexact_ = false;
  232. };
  233. auto FunctionTypeInfoBuilder::Build() && -> FunctionTypeInfo {
  234. // TODO: For the `Run` entry point, remap return type to i32 if it doesn't
  235. // return a value.
  236. // Determine how the parameters are numbered in SemIR, and make sure it's the
  237. // same for all versions of the function.
  238. auto semir_info = GetSemIRIndexInfo(functions_.front());
  239. CARBON_CHECK(llvm::all_of(
  240. functions_.drop_front(), [&](const FunctionInContext& fn_in_context) {
  241. return GetSemIRIndexInfo(fn_in_context) == semir_info;
  242. }));
  243. num_params_ = semir_info.num_params;
  244. lowered_param_indices_.reserve(num_params_);
  245. param_name_ids_.reserve(num_params_);
  246. param_types_.reserve(num_params_);
  247. param_di_types_.reserve(num_params_);
  248. if (!HandleReturnForm()) {
  249. return Abort();
  250. }
  251. int params_end = num_params_;
  252. if (semir_info.return_param_index >= 0) {
  253. CARBON_CHECK(semir_info.return_param_index == semir_info.num_params - 1,
  254. "Unexpected parameter order");
  255. params_end = semir_info.return_param_index;
  256. // Handle the return parameter first, because it goes first in the LLVM
  257. // convention.
  258. if (!HandleParameter(
  259. SemIR::CallParamIndex(semir_info.return_param_index))) {
  260. return Abort();
  261. }
  262. }
  263. for (int i : llvm::seq(params_end)) {
  264. if (!HandleParameter(SemIR::CallParamIndex(i))) {
  265. return Abort();
  266. }
  267. }
  268. return Finalize();
  269. }
  270. auto FunctionTypeInfoBuilder::HandleReturnForm() -> bool {
  271. for (const auto& func_ctx : functions_) {
  272. if (TryHandleReturnForm(func_ctx)) {
  273. return true;
  274. }
  275. }
  276. return false;
  277. }
  278. auto FunctionTypeInfoBuilder::TryHandleReturnForm(
  279. const FunctionInContext& func_ctx) -> bool {
  280. const auto& function =
  281. func_ctx.context->sem_ir().functions().Get(func_ctx.function_id);
  282. auto return_form_inst_id = function.return_form_inst_id;
  283. if (!return_form_inst_id.has_value()) {
  284. return SetReturnByCopy(func_ctx, SemIR::TypeId::None);
  285. }
  286. auto return_form_const_id = SemIR::GetConstantValueInSpecific(
  287. func_ctx.context->sem_ir(), func_ctx.specific_id, return_form_inst_id);
  288. auto return_form_inst = func_ctx.context->sem_ir().insts().Get(
  289. func_ctx.context->sem_ir().constant_values().GetInstId(
  290. return_form_const_id));
  291. CARBON_KIND_SWITCH(return_form_inst) {
  292. case CARBON_KIND(SemIR::InitForm init_form): {
  293. auto return_type_id =
  294. func_ctx.context->sem_ir().types().GetTypeIdForTypeConstantId(
  295. SemIR::GetConstantValueInSpecific(
  296. func_ctx.context->sem_ir(), func_ctx.specific_id,
  297. init_form.type_component_inst_id));
  298. switch (
  299. SemIR::InitRepr::ForType(func_ctx.context->sem_ir(), return_type_id)
  300. .kind) {
  301. case SemIR::InitRepr::InPlace:
  302. return SetReturnInPlace(func_ctx, return_type_id);
  303. case SemIR::InitRepr::ByCopy:
  304. return SetReturnByCopy(func_ctx, return_type_id);
  305. case SemIR::InitRepr::None:
  306. return SetReturnByCopy(func_ctx, SemIR::TypeId::None);
  307. case SemIR::InitRepr::Dependent:
  308. CARBON_FATAL("Lowering function return with dependent type: {0}",
  309. return_form_inst);
  310. case SemIR::InitRepr::Incomplete:
  311. case SemIR::InitRepr::Abstract:
  312. return false;
  313. }
  314. }
  315. case CARBON_KIND(SemIR::RefForm ref_form): {
  316. auto return_type_id =
  317. func_ctx.context->sem_ir().types().GetTypeIdForTypeConstantId(
  318. SemIR::GetConstantValueInSpecific(
  319. func_ctx.context->sem_ir(), func_ctx.specific_id,
  320. ref_form.type_component_inst_id));
  321. return SetReturnByReference(func_ctx, return_type_id);
  322. }
  323. case CARBON_KIND(SemIR::ValueForm val_form): {
  324. auto return_type_id =
  325. func_ctx.context->sem_ir().types().GetTypeIdForTypeConstantId(
  326. SemIR::GetConstantValueInSpecific(
  327. func_ctx.context->sem_ir(), func_ctx.specific_id,
  328. val_form.type_component_inst_id));
  329. switch (
  330. SemIR::ValueRepr::ForType(func_ctx.context->sem_ir(), return_type_id)
  331. .kind) {
  332. case SemIR::ValueRepr::Unknown:
  333. return false;
  334. case SemIR::ValueRepr::Dependent:
  335. CARBON_FATAL("Lowering function return with dependent type: {0}",
  336. return_form_inst);
  337. case SemIR::ValueRepr::None:
  338. return SetReturnByCopy(func_ctx, SemIR::TypeId::None);
  339. case SemIR::ValueRepr::Copy:
  340. return SetReturnByCopy(func_ctx, return_type_id);
  341. case SemIR::ValueRepr::Pointer:
  342. case SemIR::ValueRepr::Custom:
  343. return SetReturnByReference(func_ctx, return_type_id);
  344. }
  345. }
  346. default:
  347. CARBON_FATAL("Unexpected inst kind: {0}", return_form_inst);
  348. }
  349. }
  350. auto FunctionTypeInfoBuilder::HandleParameter(SemIR::CallParamIndex index)
  351. -> bool {
  352. for (const auto& func_ctx : functions_) {
  353. if (TryHandleParameter(func_ctx, index)) {
  354. return true;
  355. }
  356. }
  357. return false;
  358. }
  359. auto FunctionTypeInfoBuilder::TryHandleParameter(
  360. const FunctionInContext& func_ctx, SemIR::CallParamIndex index) -> bool {
  361. const auto& sem_ir = func_ctx.context->sem_ir();
  362. auto param_pattern_id =
  363. sem_ir.inst_blocks().Get(sem_ir.functions()
  364. .Get(func_ctx.function_id)
  365. .call_param_patterns_id)[index.index];
  366. auto param_pattern = sem_ir.insts().Get(param_pattern_id);
  367. auto param_type_id = ExtractScrutineeType(
  368. sem_ir, SemIR::GetTypeOfInstInSpecific(sem_ir, func_ctx.specific_id,
  369. param_pattern_id));
  370. // Returns the appropriate LoweredTypes for reference-like parameters.
  371. auto ref_lowered_types = [&]() -> LoweredTypes {
  372. return {
  373. .llvm_ir_type = llvm::PointerType::get(func_ctx.context->llvm_context(),
  374. /*AddressSpace=*/0),
  375. // TODO: replace this with a reference type.
  376. .llvm_di_type = GetLoweredTypes(func_ctx, param_type_id).llvm_di_type};
  377. };
  378. CARBON_CHECK(
  379. !param_type_id.AsConstantId().is_symbolic(),
  380. "Found symbolic type id after resolution when lowering type {0}.",
  381. param_pattern.type_id());
  382. auto param_kind = param_pattern.kind();
  383. // Treat a form parameter pattern like the kind of param pattern that
  384. // corresponds to its form.
  385. if (auto form_param_pattern =
  386. param_pattern.TryAs<SemIR::FormParamPattern>()) {
  387. auto form_kind = sem_ir.insts().Get(form_param_pattern->form_id).kind();
  388. switch (form_kind) {
  389. case SemIR::InitForm::Kind:
  390. param_kind = SemIR::VarParamPattern::Kind;
  391. break;
  392. case SemIR::RefForm::Kind:
  393. param_kind = SemIR::RefParamPattern::Kind;
  394. break;
  395. case SemIR::ValueForm::Kind:
  396. param_kind = SemIR::ValueParamPattern::Kind;
  397. break;
  398. default:
  399. CARBON_FATAL("Unexpected kind {0} for form inst", form_kind);
  400. }
  401. }
  402. switch (param_kind) {
  403. case SemIR::RefParamPattern::Kind:
  404. case SemIR::VarParamPattern::Kind: {
  405. return AddLoweredParam(func_ctx, index, param_pattern_id,
  406. ref_lowered_types());
  407. }
  408. case SemIR::OutParamPattern::Kind: {
  409. switch (SemIR::InitRepr::ForType(sem_ir, param_type_id).kind) {
  410. case SemIR::InitRepr::InPlace:
  411. return AddLoweredParam(func_ctx, index, param_pattern_id,
  412. ref_lowered_types());
  413. case SemIR::InitRepr::ByCopy:
  414. case SemIR::InitRepr::None:
  415. return IgnoreParam(index);
  416. case SemIR::InitRepr::Dependent:
  417. CARBON_FATAL("Lowering function parameter with dependent type: {0}",
  418. param_pattern);
  419. case SemIR::InitRepr::Incomplete:
  420. case SemIR::InitRepr::Abstract:
  421. return false;
  422. }
  423. }
  424. case SemIR::ValueParamPattern::Kind: {
  425. switch (auto value_rep = SemIR::ValueRepr::ForType(sem_ir, param_type_id);
  426. value_rep.kind) {
  427. case SemIR::ValueRepr::Unknown:
  428. return false;
  429. case SemIR::ValueRepr::Dependent:
  430. CARBON_FATAL("Lowering function parameter with dependent type: {0}",
  431. param_pattern);
  432. case SemIR::ValueRepr::None:
  433. return IgnoreParam(index);
  434. case SemIR::ValueRepr::Copy:
  435. case SemIR::ValueRepr::Custom:
  436. case SemIR::ValueRepr::Pointer: {
  437. if (value_rep.type_id.has_value()) {
  438. return AddLoweredParam(
  439. func_ctx, index, param_pattern_id,
  440. GetLoweredTypes(func_ctx, value_rep.type_id));
  441. } else {
  442. return IgnoreParam(index);
  443. }
  444. }
  445. }
  446. }
  447. default:
  448. CARBON_FATAL("Unexpected inst kind: {0}", param_pattern);
  449. }
  450. }
  451. auto FunctionTypeInfoBuilder::Finalize() -> FunctionTypeInfo {
  452. CARBON_CHECK(lowered_param_indices_.size() + unused_param_indices_.size() ==
  453. static_cast<size_t>(num_params_));
  454. CARBON_CHECK(!param_di_types_.empty());
  455. auto& di_builder = context_->di_builder();
  456. return {.type = llvm::FunctionType::get(return_type_, param_types_,
  457. /*isVarArg=*/false),
  458. .di_type = di_builder.createSubroutineType(
  459. di_builder.getOrCreateTypeArray(param_di_types_),
  460. llvm::DINode::FlagZero),
  461. .lowered_param_indices = std::move(lowered_param_indices_),
  462. .unused_param_indices = std::move(unused_param_indices_),
  463. .param_name_ids = std::move(param_name_ids_),
  464. .sret_type = sret_type_,
  465. .inexact = inexact_};
  466. }
  467. auto FunctionTypeInfoBuilder::Abort() -> FunctionTypeInfo {
  468. num_params_ = 0;
  469. lowered_param_indices_.clear();
  470. unused_param_indices_.clear();
  471. param_name_ids_.clear();
  472. param_types_.clear();
  473. param_di_types_.clear();
  474. return_type_ = llvm::Type::getVoidTy(context_->llvm_context());
  475. param_di_types_.push_back(nullptr);
  476. inexact_ = true;
  477. return Finalize();
  478. }
  479. auto FunctionTypeInfoBuilder::GetLoweredTypes(const FunctionInContext& func_ctx,
  480. SemIR::TypeId type_id)
  481. -> LoweredTypes {
  482. if (!type_id.has_value()) {
  483. return {
  484. .llvm_ir_type = llvm::Type::getVoidTy(func_ctx.context->llvm_context()),
  485. .llvm_di_type = nullptr};
  486. }
  487. auto result = func_ctx.context->GetTypeAndDIType(type_id);
  488. if (result.llvm_di_type == nullptr) {
  489. // TODO: figure out what type should go here, or ensure this doesn't
  490. // happen.
  491. result.llvm_di_type = GetPointerDIType(nullptr);
  492. }
  493. return result;
  494. }
  495. } // namespace
  496. auto BuildFunctionTypeInfo(llvm::ArrayRef<FunctionInContext> functions)
  497. -> FunctionTypeInfo {
  498. return FunctionTypeInfoBuilder(functions).Build();
  499. }
  500. // Given an LLVM type, build a corresponding type with `padding_bytes` bytes of
  501. // explicit tail padding.
  502. static auto BuildTailPaddedType(llvm::Type* subtype, int64_t padding_bytes)
  503. -> llvm::Type* {
  504. if (padding_bytes == 0) {
  505. return subtype;
  506. }
  507. // Build the type `<{subtype, [i8 x padding_bytes]}>`.
  508. llvm::Type* type_with_padding[2] = {
  509. subtype,
  510. llvm::ArrayType::get(llvm::Type::getInt8Ty(subtype->getContext()),
  511. padding_bytes)};
  512. return llvm::StructType::get(subtype->getContext(), type_with_padding,
  513. /*isPacked=*/true);
  514. }
  515. // BuildTypeForInst is used to construct types for FileContext::BuildType below.
  516. // Implementations return the LLVM type for the instruction. This first overload
  517. // is the fallback handler for non-type instructions.
  518. template <typename InstT>
  519. requires(InstT::Kind.is_type() == SemIR::InstIsType::Never)
  520. static auto BuildTypeForInst(FileContext& /*context*/, InstT inst)
  521. -> LoweredTypes {
  522. CARBON_FATAL("Cannot use inst as type: {0}", inst);
  523. }
  524. template <typename InstT>
  525. requires(InstT::Kind.is_symbolic_when_type())
  526. static auto BuildTypeForInst(FileContext& context, InstT /*inst*/)
  527. -> LoweredTypes {
  528. // Treat non-monomorphized symbolic types as opaque.
  529. return {llvm::StructType::get(context.llvm_context()), nullptr};
  530. }
  531. static auto BuildTypeForInst(FileContext& context, SemIR::ArrayType inst)
  532. -> LoweredTypes {
  533. auto elem_type_id = context.sem_ir().types().GetTypeIdForTypeInstId(
  534. inst.element_type_inst_id);
  535. auto stride = context.sem_ir()
  536. .types()
  537. .GetCompleteTypeInfo(elem_type_id)
  538. .object_layout.ArrayStride();
  539. auto* elem_type = context.GetType(elem_type_id);
  540. auto elem_size = SemIR::ObjectSize::Bytes(
  541. context.llvm_module().getDataLayout().getTypeAllocSize(elem_type));
  542. if (elem_size != stride) {
  543. CARBON_CHECK(elem_size < stride, "Array element type too large");
  544. elem_type = BuildTailPaddedType(context.GetType(elem_type_id),
  545. stride.bytes() - elem_size.bytes());
  546. }
  547. return {llvm::ArrayType::get(
  548. elem_type, *context.sem_ir().GetZExtIntValue(inst.bound_id)),
  549. nullptr};
  550. }
  551. static auto BuildTypeForInst(FileContext& context, SemIR::BoolType /*inst*/)
  552. -> LoweredTypes {
  553. // TODO: We may want to have different representations for `bool` storage
  554. // (`i8`) versus for `bool` values (`i1`).
  555. return {llvm::Type::getInt1Ty(context.llvm_context()), nullptr};
  556. }
  557. static auto BuildTypeForInst(FileContext& context, SemIR::ClassType inst)
  558. -> LoweredTypes {
  559. auto object_repr_id = context.sem_ir()
  560. .classes()
  561. .Get(inst.class_id)
  562. .GetObjectRepr(context.sem_ir(), inst.specific_id);
  563. return context.GetTypeAndDIType(object_repr_id);
  564. }
  565. template <typename InstT>
  566. requires(SemIR::Internal::HasInstCategory<SemIR::AnyQualifiedType, InstT>)
  567. static auto BuildTypeForInst(FileContext& context, InstT inst) -> LoweredTypes {
  568. return {context.GetType(
  569. context.sem_ir().types().GetTypeIdForTypeInstId(inst.inner_id)),
  570. nullptr};
  571. }
  572. static auto BuildTypeForInst(FileContext& context, SemIR::CustomLayoutType inst)
  573. -> LoweredTypes {
  574. auto layout = context.sem_ir().custom_layouts().Get(inst.layout_id);
  575. return {
  576. llvm::ArrayType::get(llvm::Type::getInt8Ty(context.llvm_context()),
  577. layout[SemIR::CustomLayoutId::SizeIndex].bytes()),
  578. nullptr};
  579. }
  580. static auto BuildTypeForInst(FileContext& context,
  581. SemIR::ImplWitnessAssociatedConstant inst)
  582. -> LoweredTypes {
  583. return {context.GetType(inst.type_id), nullptr};
  584. }
  585. static auto BuildTypeForInst(FileContext& /*context*/,
  586. SemIR::ErrorInst /*inst*/) -> LoweredTypes {
  587. // This is a complete type but uses of it should never be lowered.
  588. return {nullptr, nullptr};
  589. }
  590. static auto BuildTypeForInst(FileContext& context, SemIR::FloatType inst)
  591. -> LoweredTypes {
  592. return {llvm::Type::getFloatingPointTy(context.llvm_context(),
  593. inst.float_kind.Semantics()),
  594. nullptr};
  595. }
  596. static auto BuildTypeForInst(FileContext& /*context*/,
  597. SemIR::ImplWitnessAccess /*inst*/)
  598. -> LoweredTypes {
  599. CARBON_FATAL("Unexpected ImplWitnessAccess in lowering");
  600. }
  601. static auto BuildTypeForInst(FileContext& context, SemIR::IntType inst)
  602. -> LoweredTypes {
  603. auto width_inst =
  604. context.sem_ir().insts().TryGetAs<SemIR::IntValue>(inst.bit_width_id);
  605. CARBON_CHECK(width_inst, "Can't lower int type with symbolic width");
  606. auto width = context.sem_ir().ints().Get(width_inst->int_id).getZExtValue();
  607. return {llvm::IntegerType::get(context.llvm_context(), width),
  608. context.context().di_builder().createBasicType(
  609. "int", width,
  610. inst.int_kind.is_signed() ? llvm::dwarf::DW_ATE_signed
  611. : llvm::dwarf::DW_ATE_unsigned)};
  612. }
  613. static auto BuildTypeForInst(FileContext& context, SemIR::PointerType /*inst*/)
  614. -> LoweredTypes {
  615. return {llvm::PointerType::get(context.llvm_context(), /*AddressSpace=*/0),
  616. nullptr};
  617. }
  618. static auto BuildTypeForInst(FileContext& /*context*/,
  619. SemIR::PatternType /*inst*/) -> LoweredTypes {
  620. CARBON_FATAL("Unexpected pattern type in lowering");
  621. }
  622. // Builds an LLVM packed struct type whose layout matches the Carbon layout for
  623. // an aggregate with the given field types and field layouts.
  624. static auto BuildPackedStructType(FileContext& context,
  625. llvm::MutableArrayRef<llvm::Type*> subtypes,
  626. llvm::ArrayRef<SemIR::ObjectLayout> layouts)
  627. -> llvm::StructType* {
  628. const auto& data_layout = context.llvm_module().getDataLayout();
  629. auto struct_layout = SemIR::ObjectLayout::Empty();
  630. auto size_so_far = SemIR::ObjectSize::Zero();
  631. llvm::Type** previous_type = nullptr;
  632. for (auto [type, layout] : llvm::zip_equal(subtypes, layouts)) {
  633. auto offset = struct_layout.FieldOffset(layout);
  634. // If this field has padding before it, represent that padding explicitly as
  635. // part of the previous field. This allows us to always use GEP indexes that
  636. // match the field indexes.
  637. if (offset != size_so_far) {
  638. CARBON_CHECK(previous_type, "Padding before first field?");
  639. CARBON_CHECK(offset > size_so_far, "Extraneous padding after field {0}",
  640. **previous_type);
  641. int64_t padding_bytes = offset.bytes() - struct_layout.size.bytes();
  642. *previous_type = BuildTailPaddedType(*previous_type, padding_bytes);
  643. size_so_far += SemIR::ObjectSize::Bytes(padding_bytes);
  644. CARBON_CHECK(offset == size_so_far, "Field at non-byte offset");
  645. }
  646. size_so_far += SemIR::ObjectSize::Bytes(data_layout.getTypeAllocSize(type));
  647. struct_layout.AppendField(layout);
  648. previous_type = &type;
  649. }
  650. return llvm::StructType::get(context.llvm_context(), subtypes,
  651. /*isPacked=*/true);
  652. }
  653. // Returns whether the given LLVM layout matches the expected Carbon layout for
  654. // an aggregate with the given field layouts.
  655. static auto StructLayoutMatches(llvm::ArrayRef<SemIR::ObjectLayout> layouts,
  656. const llvm::StructLayout& llvm_layout) -> bool {
  657. auto struct_layout = SemIR::ObjectLayout::Empty();
  658. // Check each field is at the right offset.
  659. for (auto [i, layout] : llvm::enumerate(layouts)) {
  660. if (static_cast<int64_t>(llvm_layout.getElementOffsetInBits(i)) !=
  661. struct_layout.FieldOffset(layout).bits()) {
  662. return false;
  663. }
  664. struct_layout.AppendField(layout);
  665. }
  666. // Treat the LLVM layout as being acceptable if it's the right byte size and
  667. // does not require more alignment than the Carbon type. We could ignore the
  668. // alignment, but an overaligned LLVM type will prevent the type from being
  669. // used in non-packed structs in more situations.
  670. return static_cast<int64_t>(llvm_layout.getSizeInBytes()) ==
  671. struct_layout.size.bytes() &&
  672. llvm_layout.getAlignment() <=
  673. llvm::Align(struct_layout.alignment.bytes());
  674. }
  675. // Builds an LLVM struct type whose layout matches the Carbon layout for an
  676. // aggregate with the given field types and field layouts.
  677. static auto BuildStructType(FileContext& context,
  678. llvm::MutableArrayRef<llvm::Type*> subtypes,
  679. llvm::ArrayRef<SemIR::ObjectLayout> layouts)
  680. -> LoweredTypes {
  681. // Opportunistically try building an llvm StructType from the subtypes. If it
  682. // has the right layout, we're done. We prefer to use a non-packed struct type
  683. // where possible to produce a smaller LLVM IR representation for the type and
  684. // for constant values of the type, and to improve the readability of the IR.
  685. auto* struct_type = llvm::StructType::get(context.llvm_context(), subtypes);
  686. if (!StructLayoutMatches(
  687. layouts, *context.llvm_module().getDataLayout().getStructLayout(
  688. struct_type))) {
  689. struct_type = BuildPackedStructType(context, subtypes, layouts);
  690. }
  691. return {struct_type, nullptr};
  692. }
  693. static auto BuildTypeForInst(FileContext& context, SemIR::StructType inst)
  694. -> LoweredTypes {
  695. auto fields = context.sem_ir().struct_type_fields().Get(inst.fields_id);
  696. llvm::SmallVector<llvm::Type*> subtypes;
  697. llvm::SmallVector<SemIR::ObjectLayout> layouts;
  698. subtypes.reserve(fields.size());
  699. layouts.reserve(fields.size());
  700. for (auto field : fields) {
  701. auto type_id =
  702. context.sem_ir().types().GetTypeIdForTypeInstId(field.type_inst_id);
  703. subtypes.push_back(context.GetType(type_id));
  704. layouts.push_back(
  705. context.sem_ir().types().GetCompleteTypeInfo(type_id).object_layout);
  706. }
  707. return BuildStructType(context, subtypes, layouts);
  708. }
  709. static auto BuildTypeForInst(FileContext& context, SemIR::TupleType inst)
  710. -> LoweredTypes {
  711. // TODO: Investigate special-casing handling of empty tuples so that they
  712. // can be collectively replaced with LLVM's void, particularly around
  713. // function returns. LLVM doesn't allow declaring variables with a void
  714. // type, so that may require significant special casing.
  715. auto elements = context.sem_ir().inst_blocks().Get(inst.type_elements_id);
  716. llvm::SmallVector<llvm::Type*> subtypes;
  717. llvm::SmallVector<SemIR::ObjectLayout> layouts;
  718. subtypes.reserve(elements.size());
  719. layouts.reserve(elements.size());
  720. for (auto type_id : context.sem_ir().types().GetBlockAsTypeIds(elements)) {
  721. subtypes.push_back(context.GetType(type_id));
  722. layouts.push_back(
  723. context.sem_ir().types().GetCompleteTypeInfo(type_id).object_layout);
  724. }
  725. return BuildStructType(context, subtypes, layouts);
  726. }
  727. static auto BuildTypeForInst(FileContext& context, SemIR::TypeType /*inst*/)
  728. -> LoweredTypes {
  729. return {context.GetTypeType(), nullptr};
  730. }
  731. static auto BuildTypeForInst(FileContext& context, SemIR::FormType /*inst*/)
  732. -> LoweredTypes {
  733. return {context.GetFormType(), nullptr};
  734. }
  735. static auto BuildTypeForInst(FileContext& context, SemIR::VtableType /*inst*/)
  736. -> LoweredTypes {
  737. return {llvm::Type::getVoidTy(context.llvm_context()), nullptr};
  738. }
  739. template <typename InstT>
  740. requires(InstT::Kind.template IsAnyOf<
  741. SemIR::AssociatedEntityType, SemIR::AutoType, SemIR::BoundMethodType,
  742. SemIR::CharLiteralType, SemIR::CppOverloadSetType,
  743. SemIR::CppTemplateNameType, SemIR::FacetType,
  744. SemIR::FloatLiteralType, SemIR::FunctionType,
  745. SemIR::FunctionTypeWithSelfType, SemIR::GenericClassType,
  746. SemIR::GenericInterfaceType, SemIR::GenericNamedConstraintType,
  747. SemIR::InstType, SemIR::IntLiteralType, SemIR::NamespaceType,
  748. SemIR::RequireSpecificDefinitionType, SemIR::SpecificFunctionType,
  749. SemIR::UnboundElementType, SemIR::WhereExpr, SemIR::WitnessType>())
  750. static auto BuildTypeForInst(FileContext& context, InstT /*inst*/)
  751. -> LoweredTypes {
  752. // Return an empty struct as a placeholder.
  753. // TODO: Should we model an interface as a witness table, or an associated
  754. // entity as an index?
  755. return {llvm::StructType::get(context.llvm_context()), nullptr};
  756. }
  757. auto BuildType(FileContext& context, SemIR::InstId inst_id) -> LoweredTypes {
  758. // Use overload resolution to select the implementation, producing compile
  759. // errors when BuildTypeForInst isn't defined for a given instruction.
  760. LoweredTypes result;
  761. CARBON_KIND_SWITCH(context.sem_ir().insts().Get(inst_id)) {
  762. #define CARBON_SEM_IR_INST_KIND(Name) \
  763. case CARBON_KIND(SemIR::Name inst): { \
  764. result = BuildTypeForInst(context, inst); \
  765. break; \
  766. }
  767. #include "toolchain/sem_ir/inst_kind.def"
  768. }
  769. // In debug builds, check that the type we built has the expected size.
  770. CARBON_DCHECK([&] {
  771. if (!result.llvm_ir_type) {
  772. return true;
  773. }
  774. const auto& layout = context.llvm_module().getDataLayout();
  775. auto expected_layout =
  776. context.sem_ir()
  777. .types()
  778. .GetCompleteTypeInfo(
  779. context.sem_ir().types().GetTypeIdForTypeInstId(inst_id))
  780. .object_layout;
  781. CARBON_CHECK(expected_layout.has_value());
  782. auto size =
  783. SemIR::ObjectSize::Bits(layout.getTypeSizeInBits(result.llvm_ir_type));
  784. // Round up to byte granularity for this check, since LLVM doesn't support
  785. // non-byte-sized packed structs.
  786. CARBON_CHECK(
  787. size.bytes() == expected_layout.size.bytes(),
  788. "Lowered type {0} for {1} has unexpected size {2}, expected {3}",
  789. *result.llvm_ir_type, context.sem_ir().insts().Get(inst_id), size,
  790. expected_layout.size);
  791. return true;
  792. }());
  793. return result;
  794. }
  795. } // namespace Carbon::Lower