type.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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(
  367. sem_ir.constant_values().GetConstantInstId(param_pattern_id));
  368. auto param_type_id = ExtractScrutineeType(
  369. sem_ir, SemIR::GetTypeOfInstInSpecific(sem_ir, func_ctx.specific_id,
  370. param_pattern_id));
  371. // Returns the appropriate LoweredTypes for reference-like parameters.
  372. auto ref_lowered_types = [&]() -> LoweredTypes {
  373. return {
  374. .llvm_ir_type = llvm::PointerType::get(func_ctx.context->llvm_context(),
  375. /*AddressSpace=*/0),
  376. // TODO: replace this with a reference type.
  377. .llvm_di_type = GetLoweredTypes(func_ctx, param_type_id).llvm_di_type};
  378. };
  379. CARBON_CHECK(
  380. !param_type_id.AsConstantId().is_symbolic(),
  381. "Found symbolic type id after resolution when lowering type {0}.",
  382. param_pattern.type_id());
  383. auto param_kind = param_pattern.kind();
  384. switch (param_kind) {
  385. case SemIR::RefParamPattern::Kind:
  386. case SemIR::VarParamPattern::Kind: {
  387. return AddLoweredParam(func_ctx, index, param_pattern_id,
  388. ref_lowered_types());
  389. }
  390. case SemIR::OutParamPattern::Kind: {
  391. switch (SemIR::InitRepr::ForType(sem_ir, param_type_id).kind) {
  392. case SemIR::InitRepr::InPlace:
  393. return AddLoweredParam(func_ctx, index, param_pattern_id,
  394. ref_lowered_types());
  395. case SemIR::InitRepr::ByCopy:
  396. case SemIR::InitRepr::None:
  397. return IgnoreParam(index);
  398. case SemIR::InitRepr::Dependent:
  399. CARBON_FATAL("Lowering function parameter with dependent type: {0}",
  400. param_pattern);
  401. case SemIR::InitRepr::Incomplete:
  402. case SemIR::InitRepr::Abstract:
  403. return false;
  404. }
  405. }
  406. case SemIR::ValueParamPattern::Kind: {
  407. switch (auto value_rep = SemIR::ValueRepr::ForType(sem_ir, param_type_id);
  408. value_rep.kind) {
  409. case SemIR::ValueRepr::Unknown:
  410. return false;
  411. case SemIR::ValueRepr::Dependent:
  412. CARBON_FATAL("Lowering function parameter with dependent type: {0}",
  413. param_pattern);
  414. case SemIR::ValueRepr::None:
  415. return IgnoreParam(index);
  416. case SemIR::ValueRepr::Copy:
  417. case SemIR::ValueRepr::Custom:
  418. case SemIR::ValueRepr::Pointer: {
  419. if (value_rep.type_id.has_value()) {
  420. return AddLoweredParam(
  421. func_ctx, index, param_pattern_id,
  422. GetLoweredTypes(func_ctx, value_rep.type_id));
  423. } else {
  424. return IgnoreParam(index);
  425. }
  426. }
  427. }
  428. }
  429. default:
  430. CARBON_FATAL("Unexpected inst kind: {0}", param_pattern);
  431. }
  432. }
  433. auto FunctionTypeInfoBuilder::Finalize() -> FunctionTypeInfo {
  434. CARBON_CHECK(lowered_param_indices_.size() + unused_param_indices_.size() ==
  435. static_cast<size_t>(num_params_));
  436. CARBON_CHECK(!param_di_types_.empty());
  437. auto& di_builder = context_->di_builder();
  438. return {.type = llvm::FunctionType::get(return_type_, param_types_,
  439. /*isVarArg=*/false),
  440. .di_type = di_builder.createSubroutineType(
  441. di_builder.getOrCreateTypeArray(param_di_types_),
  442. llvm::DINode::FlagZero),
  443. .lowered_param_indices = std::move(lowered_param_indices_),
  444. .unused_param_indices = std::move(unused_param_indices_),
  445. .param_name_ids = std::move(param_name_ids_),
  446. .sret_type = sret_type_,
  447. .inexact = inexact_};
  448. }
  449. auto FunctionTypeInfoBuilder::Abort() -> FunctionTypeInfo {
  450. num_params_ = 0;
  451. lowered_param_indices_.clear();
  452. unused_param_indices_.clear();
  453. param_name_ids_.clear();
  454. param_types_.clear();
  455. param_di_types_.clear();
  456. return_type_ = llvm::Type::getVoidTy(context_->llvm_context());
  457. param_di_types_.push_back(nullptr);
  458. inexact_ = true;
  459. return Finalize();
  460. }
  461. auto FunctionTypeInfoBuilder::GetLoweredTypes(const FunctionInContext& func_ctx,
  462. SemIR::TypeId type_id)
  463. -> LoweredTypes {
  464. if (!type_id.has_value()) {
  465. return {
  466. .llvm_ir_type = llvm::Type::getVoidTy(func_ctx.context->llvm_context()),
  467. .llvm_di_type = nullptr};
  468. }
  469. auto result = func_ctx.context->GetTypeAndDIType(type_id);
  470. if (result.llvm_di_type == nullptr) {
  471. // TODO: figure out what type should go here, or ensure this doesn't
  472. // happen.
  473. result.llvm_di_type = GetPointerDIType(nullptr);
  474. }
  475. return result;
  476. }
  477. } // namespace
  478. auto BuildFunctionTypeInfo(llvm::ArrayRef<FunctionInContext> functions)
  479. -> FunctionTypeInfo {
  480. return FunctionTypeInfoBuilder(functions).Build();
  481. }
  482. // Given an LLVM type, build a corresponding type with `padding_bytes` bytes of
  483. // explicit tail padding.
  484. static auto BuildTailPaddedType(llvm::Type* subtype, int64_t padding_bytes)
  485. -> llvm::Type* {
  486. if (padding_bytes == 0) {
  487. return subtype;
  488. }
  489. // Build the type `<{subtype, [i8 x padding_bytes]}>`.
  490. llvm::Type* type_with_padding[2] = {
  491. subtype,
  492. llvm::ArrayType::get(llvm::Type::getInt8Ty(subtype->getContext()),
  493. padding_bytes)};
  494. return llvm::StructType::get(subtype->getContext(), type_with_padding,
  495. /*isPacked=*/true);
  496. }
  497. // BuildTypeForInst is used to construct types for FileContext::BuildType below.
  498. // Implementations return the LLVM type for the instruction. This first overload
  499. // is the fallback handler for non-type instructions.
  500. template <typename InstT>
  501. requires(InstT::Kind.is_type() == SemIR::InstIsType::Never)
  502. static auto BuildTypeForInst(FileContext& /*context*/, InstT inst)
  503. -> LoweredTypes {
  504. CARBON_FATAL("Cannot use inst as type: {0}", inst);
  505. }
  506. template <typename InstT>
  507. requires(InstT::Kind.is_symbolic_when_type())
  508. static auto BuildTypeForInst(FileContext& context, InstT /*inst*/)
  509. -> LoweredTypes {
  510. // Treat non-monomorphized symbolic types as opaque.
  511. return {llvm::StructType::get(context.llvm_context()), nullptr};
  512. }
  513. static auto BuildTypeForInst(FileContext& context, SemIR::ArrayType inst)
  514. -> LoweredTypes {
  515. auto elem_type_id = context.sem_ir().types().GetTypeIdForTypeInstId(
  516. inst.element_type_inst_id);
  517. auto stride = context.sem_ir()
  518. .types()
  519. .GetCompleteTypeInfo(elem_type_id)
  520. .object_layout.ArrayStride();
  521. auto* elem_type = context.GetType(elem_type_id);
  522. auto elem_size = SemIR::ObjectSize::Bytes(
  523. context.llvm_module().getDataLayout().getTypeAllocSize(elem_type));
  524. if (elem_size != stride) {
  525. CARBON_CHECK(elem_size < stride, "Array element type too large");
  526. elem_type = BuildTailPaddedType(context.GetType(elem_type_id),
  527. stride.bytes() - elem_size.bytes());
  528. }
  529. return {llvm::ArrayType::get(
  530. elem_type, *context.sem_ir().GetZExtIntValue(inst.bound_id)),
  531. nullptr};
  532. }
  533. static auto BuildTypeForInst(FileContext& context, SemIR::BoolType /*inst*/)
  534. -> LoweredTypes {
  535. // TODO: We may want to have different representations for `bool` storage
  536. // (`i8`) versus for `bool` values (`i1`).
  537. return {llvm::Type::getInt1Ty(context.llvm_context()), nullptr};
  538. }
  539. static auto BuildTypeForInst(FileContext& context, SemIR::ClassType inst)
  540. -> LoweredTypes {
  541. auto object_repr_id = context.sem_ir()
  542. .classes()
  543. .Get(inst.class_id)
  544. .GetObjectRepr(context.sem_ir(), inst.specific_id);
  545. return context.GetTypeAndDIType(object_repr_id);
  546. }
  547. template <typename InstT>
  548. requires(SemIR::Internal::HasInstCategory<SemIR::AnyQualifiedType, InstT>)
  549. static auto BuildTypeForInst(FileContext& context, InstT inst) -> LoweredTypes {
  550. return {context.GetType(
  551. context.sem_ir().types().GetTypeIdForTypeInstId(inst.inner_id)),
  552. nullptr};
  553. }
  554. static auto BuildTypeForInst(FileContext& context, SemIR::CustomLayoutType inst)
  555. -> LoweredTypes {
  556. auto layout = context.sem_ir().custom_layouts().Get(inst.layout_id);
  557. return {
  558. llvm::ArrayType::get(llvm::Type::getInt8Ty(context.llvm_context()),
  559. layout[SemIR::CustomLayoutId::SizeIndex].bytes()),
  560. nullptr};
  561. }
  562. static auto BuildTypeForInst(FileContext& context,
  563. SemIR::ImplWitnessAssociatedConstant inst)
  564. -> LoweredTypes {
  565. return {context.GetType(inst.type_id), nullptr};
  566. }
  567. static auto BuildTypeForInst(FileContext& /*context*/,
  568. SemIR::ErrorInst /*inst*/) -> LoweredTypes {
  569. // This is a complete type but uses of it should never be lowered.
  570. return {nullptr, nullptr};
  571. }
  572. static auto BuildTypeForInst(FileContext& context, SemIR::FloatType inst)
  573. -> LoweredTypes {
  574. return {llvm::Type::getFloatingPointTy(context.llvm_context(),
  575. inst.float_kind.Semantics()),
  576. nullptr};
  577. }
  578. static auto BuildTypeForInst(FileContext& /*context*/,
  579. SemIR::ImplWitnessAccess /*inst*/)
  580. -> LoweredTypes {
  581. CARBON_FATAL("Unexpected ImplWitnessAccess in lowering");
  582. }
  583. static auto BuildTypeForInst(FileContext& context, SemIR::IntType inst)
  584. -> LoweredTypes {
  585. auto width_inst =
  586. context.sem_ir().insts().TryGetAs<SemIR::IntValue>(inst.bit_width_id);
  587. CARBON_CHECK(width_inst, "Can't lower int type with symbolic width");
  588. auto width = context.sem_ir().ints().Get(width_inst->int_id).getZExtValue();
  589. return {llvm::IntegerType::get(context.llvm_context(), width),
  590. context.context().di_builder().createBasicType(
  591. "int", width,
  592. inst.int_kind.is_signed() ? llvm::dwarf::DW_ATE_signed
  593. : llvm::dwarf::DW_ATE_unsigned)};
  594. }
  595. static auto BuildTypeForInst(FileContext& context, SemIR::PointerType /*inst*/)
  596. -> LoweredTypes {
  597. return {llvm::PointerType::get(context.llvm_context(), /*AddressSpace=*/0),
  598. nullptr};
  599. }
  600. static auto BuildTypeForInst(FileContext& /*context*/,
  601. SemIR::PatternType /*inst*/) -> LoweredTypes {
  602. CARBON_FATAL("Unexpected pattern type in lowering");
  603. }
  604. // Builds an LLVM packed struct type whose layout matches the Carbon layout for
  605. // an aggregate with the given field types and field layouts.
  606. static auto BuildPackedStructType(FileContext& context,
  607. llvm::MutableArrayRef<llvm::Type*> subtypes,
  608. llvm::ArrayRef<SemIR::ObjectLayout> layouts)
  609. -> llvm::StructType* {
  610. const auto& data_layout = context.llvm_module().getDataLayout();
  611. auto struct_layout = SemIR::ObjectLayout::Empty();
  612. auto size_so_far = SemIR::ObjectSize::Zero();
  613. llvm::Type** previous_type = nullptr;
  614. for (auto [type, layout] : llvm::zip_equal(subtypes, layouts)) {
  615. auto offset = struct_layout.FieldOffset(layout);
  616. // If this field has padding before it, represent that padding explicitly as
  617. // part of the previous field. This allows us to always use GEP indexes that
  618. // match the field indexes.
  619. if (offset != size_so_far) {
  620. CARBON_CHECK(previous_type, "Padding before first field?");
  621. CARBON_CHECK(offset > size_so_far, "Extraneous padding after field {0}",
  622. **previous_type);
  623. int64_t padding_bytes = offset.bytes() - struct_layout.size.bytes();
  624. *previous_type = BuildTailPaddedType(*previous_type, padding_bytes);
  625. size_so_far += SemIR::ObjectSize::Bytes(padding_bytes);
  626. CARBON_CHECK(offset == size_so_far, "Field at non-byte offset");
  627. }
  628. size_so_far += SemIR::ObjectSize::Bytes(data_layout.getTypeAllocSize(type));
  629. struct_layout.AppendField(layout);
  630. previous_type = &type;
  631. }
  632. return llvm::StructType::get(context.llvm_context(), subtypes,
  633. /*isPacked=*/true);
  634. }
  635. // Returns whether the given LLVM layout matches the expected Carbon layout for
  636. // an aggregate with the given field layouts.
  637. static auto StructLayoutMatches(llvm::ArrayRef<SemIR::ObjectLayout> layouts,
  638. const llvm::StructLayout& llvm_layout) -> bool {
  639. auto struct_layout = SemIR::ObjectLayout::Empty();
  640. // Check each field is at the right offset.
  641. for (auto [i, layout] : llvm::enumerate(layouts)) {
  642. if (static_cast<int64_t>(llvm_layout.getElementOffsetInBits(i)) !=
  643. struct_layout.FieldOffset(layout).bits()) {
  644. return false;
  645. }
  646. struct_layout.AppendField(layout);
  647. }
  648. // Treat the LLVM layout as being acceptable if it's the right byte size and
  649. // does not require more alignment than the Carbon type. We could ignore the
  650. // alignment, but an overaligned LLVM type will prevent the type from being
  651. // used in non-packed structs in more situations.
  652. return static_cast<int64_t>(llvm_layout.getSizeInBytes()) ==
  653. struct_layout.size.bytes() &&
  654. llvm_layout.getAlignment() <=
  655. llvm::Align(struct_layout.alignment.bytes());
  656. }
  657. // Builds an LLVM struct type whose layout matches the Carbon layout for an
  658. // aggregate with the given field types and field layouts.
  659. static auto BuildStructType(FileContext& context,
  660. llvm::MutableArrayRef<llvm::Type*> subtypes,
  661. llvm::ArrayRef<SemIR::ObjectLayout> layouts)
  662. -> LoweredTypes {
  663. // Opportunistically try building an llvm StructType from the subtypes. If it
  664. // has the right layout, we're done. We prefer to use a non-packed struct type
  665. // where possible to produce a smaller LLVM IR representation for the type and
  666. // for constant values of the type, and to improve the readability of the IR.
  667. auto* struct_type = llvm::StructType::get(context.llvm_context(), subtypes);
  668. if (!StructLayoutMatches(
  669. layouts, *context.llvm_module().getDataLayout().getStructLayout(
  670. struct_type))) {
  671. struct_type = BuildPackedStructType(context, subtypes, layouts);
  672. }
  673. return {struct_type, nullptr};
  674. }
  675. static auto BuildTypeForInst(FileContext& context, SemIR::StructType inst)
  676. -> LoweredTypes {
  677. auto fields = context.sem_ir().struct_type_fields().Get(inst.fields_id);
  678. llvm::SmallVector<llvm::Type*> subtypes;
  679. llvm::SmallVector<SemIR::ObjectLayout> layouts;
  680. subtypes.reserve(fields.size());
  681. layouts.reserve(fields.size());
  682. for (auto field : fields) {
  683. auto type_id =
  684. context.sem_ir().types().GetTypeIdForTypeInstId(field.type_inst_id);
  685. subtypes.push_back(context.GetType(type_id));
  686. layouts.push_back(
  687. context.sem_ir().types().GetCompleteTypeInfo(type_id).object_layout);
  688. }
  689. return BuildStructType(context, subtypes, layouts);
  690. }
  691. static auto BuildTypeForInst(FileContext& context, SemIR::TupleType inst)
  692. -> LoweredTypes {
  693. // TODO: Investigate special-casing handling of empty tuples so that they
  694. // can be collectively replaced with LLVM's void, particularly around
  695. // function returns. LLVM doesn't allow declaring variables with a void
  696. // type, so that may require significant special casing.
  697. auto elements = context.sem_ir().inst_blocks().Get(inst.type_elements_id);
  698. llvm::SmallVector<llvm::Type*> subtypes;
  699. llvm::SmallVector<SemIR::ObjectLayout> layouts;
  700. subtypes.reserve(elements.size());
  701. layouts.reserve(elements.size());
  702. for (auto type_id : context.sem_ir().types().GetBlockAsTypeIds(elements)) {
  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::TypeType /*inst*/)
  710. -> LoweredTypes {
  711. return {context.GetTypeType(), nullptr};
  712. }
  713. static auto BuildTypeForInst(FileContext& context, SemIR::FormType /*inst*/)
  714. -> LoweredTypes {
  715. return {context.GetFormType(), nullptr};
  716. }
  717. static auto BuildTypeForInst(FileContext& context, SemIR::VtableType /*inst*/)
  718. -> LoweredTypes {
  719. return {llvm::Type::getVoidTy(context.llvm_context()), nullptr};
  720. }
  721. template <typename InstT>
  722. requires(InstT::Kind.template IsAnyOf<
  723. SemIR::AssociatedEntityType, SemIR::AutoType, SemIR::BoundMethodType,
  724. SemIR::CharLiteralType, SemIR::CppOverloadSetType,
  725. SemIR::CppTemplateNameType, SemIR::FacetType,
  726. SemIR::FloatLiteralType, SemIR::FunctionType,
  727. SemIR::FunctionTypeWithSelfType, SemIR::GenericClassType,
  728. SemIR::GenericInterfaceType, SemIR::GenericNamedConstraintType,
  729. SemIR::InstType, SemIR::IntLiteralType, SemIR::NamespaceType,
  730. SemIR::RequireSpecificDefinitionType, SemIR::SpecificFunctionType,
  731. SemIR::UnboundElementType, SemIR::WhereExpr, SemIR::WitnessType>())
  732. static auto BuildTypeForInst(FileContext& context, InstT /*inst*/)
  733. -> LoweredTypes {
  734. // Return an empty struct as a placeholder.
  735. // TODO: Should we model an interface as a witness table, or an associated
  736. // entity as an index?
  737. return {llvm::StructType::get(context.llvm_context()), nullptr};
  738. }
  739. auto BuildType(FileContext& context, SemIR::InstId inst_id) -> LoweredTypes {
  740. // Use overload resolution to select the implementation, producing compile
  741. // errors when BuildTypeForInst isn't defined for a given instruction.
  742. LoweredTypes result;
  743. CARBON_KIND_SWITCH(context.sem_ir().insts().Get(inst_id)) {
  744. #define CARBON_SEM_IR_INST_KIND(Name) \
  745. case CARBON_KIND(SemIR::Name inst): { \
  746. result = BuildTypeForInst(context, inst); \
  747. break; \
  748. }
  749. #include "toolchain/sem_ir/inst_kind.def"
  750. }
  751. // In debug builds, check that the type we built has the expected size.
  752. CARBON_DCHECK([&] {
  753. if (!result.llvm_ir_type) {
  754. return true;
  755. }
  756. const auto& layout = context.llvm_module().getDataLayout();
  757. auto expected_layout =
  758. context.sem_ir()
  759. .types()
  760. .GetCompleteTypeInfo(
  761. context.sem_ir().types().GetTypeIdForTypeInstId(inst_id))
  762. .object_layout;
  763. CARBON_CHECK(expected_layout.has_value());
  764. auto size =
  765. SemIR::ObjectSize::Bits(layout.getTypeSizeInBits(result.llvm_ir_type));
  766. // Round up to byte granularity for this check, since LLVM doesn't support
  767. // non-byte-sized packed structs.
  768. CARBON_CHECK(
  769. size.bytes() == expected_layout.size.bytes(),
  770. "Lowered type {0} for {1} has unexpected size {2}, expected {3}",
  771. *result.llvm_ir_type, context.sem_ir().insts().Get(inst_id), size,
  772. expected_layout.size);
  773. return true;
  774. }());
  775. return result;
  776. }
  777. } // namespace Carbon::Lower