file_context.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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 "common/check.h"
  6. #include "common/vlog.h"
  7. #include "llvm/ADT/STLExtras.h"
  8. #include "llvm/ADT/Sequence.h"
  9. #include "llvm/Transforms/Utils/ModuleUtils.h"
  10. #include "toolchain/base/kind_switch.h"
  11. #include "toolchain/lower/constant.h"
  12. #include "toolchain/lower/function_context.h"
  13. #include "toolchain/lower/mangler.h"
  14. #include "toolchain/sem_ir/absolute_node_id.h"
  15. #include "toolchain/sem_ir/entry_point.h"
  16. #include "toolchain/sem_ir/file.h"
  17. #include "toolchain/sem_ir/function.h"
  18. #include "toolchain/sem_ir/generic.h"
  19. #include "toolchain/sem_ir/ids.h"
  20. #include "toolchain/sem_ir/inst.h"
  21. #include "toolchain/sem_ir/inst_kind.h"
  22. #include "toolchain/sem_ir/pattern.h"
  23. #include "toolchain/sem_ir/typed_insts.h"
  24. namespace Carbon::Lower {
  25. FileContext::FileContext(
  26. llvm::LLVMContext& llvm_context,
  27. std::optional<llvm::ArrayRef<Parse::GetTreeAndSubtreesFn>>
  28. tree_and_subtrees_getters_for_debug_info,
  29. llvm::StringRef module_name, const SemIR::File& sem_ir,
  30. clang::ASTUnit* cpp_ast, const SemIR::InstNamer* inst_namer,
  31. llvm::raw_ostream* vlog_stream)
  32. : llvm_context_(&llvm_context),
  33. llvm_module_(std::make_unique<llvm::Module>(module_name, llvm_context)),
  34. di_builder_(*llvm_module_),
  35. di_compile_unit_(
  36. tree_and_subtrees_getters_for_debug_info
  37. ? BuildDICompileUnit(module_name, *llvm_module_, di_builder_)
  38. : nullptr),
  39. tree_and_subtrees_getters_for_debug_info_(
  40. tree_and_subtrees_getters_for_debug_info),
  41. sem_ir_(&sem_ir),
  42. cpp_ast_(cpp_ast),
  43. inst_namer_(inst_namer),
  44. vlog_stream_(vlog_stream) {
  45. CARBON_CHECK(!sem_ir.has_errors(),
  46. "Generating LLVM IR from invalid SemIR::File is unsupported.");
  47. }
  48. // TODO: Move this to lower.cpp.
  49. auto FileContext::Run() -> std::unique_ptr<llvm::Module> {
  50. CARBON_CHECK(llvm_module_, "Run can only be called once.");
  51. // Lower all types that were required to be complete.
  52. types_.resize(sem_ir_->insts().size());
  53. for (auto type_id : sem_ir_->types().complete_types()) {
  54. if (type_id.index >= 0) {
  55. types_[type_id.index] = BuildType(sem_ir_->types().GetInstId(type_id));
  56. }
  57. }
  58. // Lower function declarations.
  59. functions_.resize_for_overwrite(sem_ir_->functions().size());
  60. for (auto [id, _] : sem_ir_->functions().enumerate()) {
  61. functions_[id.index] = BuildFunctionDecl(id);
  62. }
  63. // Specific functions are lowered when we emit a reference to them.
  64. specific_functions_.resize(sem_ir_->specifics().size());
  65. // Lower global variable declarations.
  66. for (auto inst_id :
  67. sem_ir().inst_blocks().Get(sem_ir().top_inst_block_id())) {
  68. // Only `VarStorage` indicates a global variable declaration in the
  69. // top instruction block.
  70. if (auto var = sem_ir().insts().TryGetAs<SemIR::VarStorage>(inst_id)) {
  71. global_variables_.Insert(inst_id, BuildGlobalVariableDecl(*var));
  72. }
  73. }
  74. // Lower constants.
  75. constants_.resize(sem_ir_->insts().size());
  76. LowerConstants(*this, constants_);
  77. // Lower function definitions.
  78. for (auto [id, _] : sem_ir_->functions().enumerate()) {
  79. BuildFunctionDefinition(id);
  80. }
  81. // Lower function definitions for generics.
  82. // This cannot be a range-based loop, as new definitions can be added
  83. // while building other definitions.
  84. // NOLINTNEXTLINE
  85. for (size_t i = 0; i != specific_function_definitions_.size(); ++i) {
  86. auto [function_id, specific_id] = specific_function_definitions_[i];
  87. BuildFunctionDefinition(function_id, specific_id);
  88. }
  89. // Append `__global_init` to `llvm::global_ctors` to initialize global
  90. // variables.
  91. if (sem_ir().global_ctor_id().has_value()) {
  92. llvm::appendToGlobalCtors(llvm_module(),
  93. GetFunction(sem_ir().global_ctor_id()),
  94. /*Priority=*/0);
  95. }
  96. return std::move(llvm_module_);
  97. }
  98. auto FileContext::BuildDICompileUnit(llvm::StringRef module_name,
  99. llvm::Module& llvm_module,
  100. llvm::DIBuilder& di_builder)
  101. -> llvm::DICompileUnit* {
  102. llvm_module.addModuleFlag(llvm::Module::Max, "Dwarf Version", 5);
  103. llvm_module.addModuleFlag(llvm::Module::Warning, "Debug Info Version",
  104. llvm::DEBUG_METADATA_VERSION);
  105. // TODO: Include directory path in the compile_unit_file.
  106. llvm::DIFile* compile_unit_file = di_builder.createFile(module_name, "");
  107. // TODO: Introduce a new language code for Carbon. C works well for now since
  108. // it's something debuggers will already know/have support for at least.
  109. // Probably have to bump to C++ at some point for virtual functions,
  110. // templates, etc.
  111. return di_builder.createCompileUnit(llvm::dwarf::DW_LANG_C, compile_unit_file,
  112. "carbon",
  113. /*isOptimized=*/false, /*Flags=*/"",
  114. /*RV=*/0);
  115. }
  116. auto FileContext::GetGlobal(SemIR::InstId inst_id,
  117. SemIR::SpecificId specific_id) -> llvm::Value* {
  118. auto const_id = GetConstantValueInSpecific(sem_ir(), specific_id, inst_id);
  119. if (const_id.is_concrete()) {
  120. auto const_inst_id = sem_ir().constant_values().GetInstId(const_id);
  121. // For value expressions and initializing expressions, the value produced by
  122. // a constant instruction is a value representation of the constant. For
  123. // initializing expressions, `FinishInit` will perform a copy if needed.
  124. // TODO: Handle reference expression constants.
  125. auto* const_value = constants_[const_inst_id.index];
  126. // If we want a pointer to the constant, materialize a global to hold it.
  127. // TODO: We could reuse the same global if the constant is used more than
  128. // once.
  129. auto value_rep = SemIR::ValueRepr::ForType(
  130. sem_ir(), sem_ir().insts().Get(const_inst_id).type_id());
  131. if (value_rep.kind == SemIR::ValueRepr::Pointer) {
  132. // Include both the name of the constant, if any, and the point of use in
  133. // the name of the variable.
  134. llvm::StringRef const_name;
  135. llvm::StringRef use_name;
  136. if (inst_namer_) {
  137. const_name = inst_namer_->GetUnscopedNameFor(const_inst_id);
  138. use_name = inst_namer_->GetUnscopedNameFor(inst_id);
  139. }
  140. // We always need to give the global a name even if the instruction namer
  141. // doesn't have one to use.
  142. if (const_name.empty()) {
  143. const_name = "const";
  144. }
  145. if (use_name.empty()) {
  146. use_name = "anon";
  147. }
  148. llvm::StringRef sep = (use_name[0] == '.') ? "" : ".";
  149. return new llvm::GlobalVariable(
  150. llvm_module(), GetType(sem_ir().GetPointeeType(value_rep.type_id)),
  151. /*isConstant=*/true, llvm::GlobalVariable::InternalLinkage,
  152. const_value, const_name + sep + use_name);
  153. }
  154. // Otherwise, we can use the constant value directly.
  155. return const_value;
  156. }
  157. CARBON_FATAL("Missing value: {0} {1} {2}", inst_id, specific_id,
  158. sem_ir().insts().Get(inst_id));
  159. }
  160. auto FileContext::GetOrCreateFunction(SemIR::FunctionId function_id,
  161. SemIR::SpecificId specific_id)
  162. -> llvm::Function* {
  163. // Non-generic functions are declared eagerly.
  164. if (!specific_id.has_value()) {
  165. return GetFunction(function_id);
  166. }
  167. if (auto* result = specific_functions_[specific_id.index]) {
  168. return result;
  169. }
  170. auto* result = BuildFunctionDecl(function_id, specific_id);
  171. // TODO: Add this function to a list of specific functions whose definitions
  172. // we need to emit.
  173. specific_functions_[specific_id.index] = result;
  174. // TODO: Use this to generate definitions for these functions.
  175. specific_function_definitions_.push_back({function_id, specific_id});
  176. return result;
  177. }
  178. auto FileContext::BuildFunctionTypeInfo(const SemIR::Function& function,
  179. SemIR::SpecificId specific_id)
  180. -> FunctionTypeInfo {
  181. const auto return_info =
  182. SemIR::ReturnTypeInfo::ForFunction(sem_ir(), function, specific_id);
  183. if (!return_info.is_valid()) {
  184. // The return type has not been completed, create a trivial type instead.
  185. return {.type =
  186. llvm::FunctionType::get(llvm::Type::getVoidTy(llvm_context()),
  187. /*isVarArg=*/false)};
  188. }
  189. // TODO nit: add is_symbolic() to type_id to forward to
  190. // type_id.AsConstantId().is_symbolic(). Update call below too.
  191. auto get_llvm_type = [&](SemIR::TypeId type_id) -> llvm::Type* {
  192. if (!type_id.has_value()) {
  193. return nullptr;
  194. }
  195. return GetType(SemIR::GetTypeInSpecific(sem_ir(), specific_id, type_id));
  196. };
  197. // TODO: expose the `Call` parameter patterns in `Function`, and use them here
  198. // instead of reconstructing them via the syntactic parameter lists.
  199. auto implicit_param_patterns =
  200. sem_ir().inst_blocks().GetOrEmpty(function.implicit_param_patterns_id);
  201. auto param_patterns =
  202. sem_ir().inst_blocks().GetOrEmpty(function.param_patterns_id);
  203. auto* return_type = get_llvm_type(return_info.type_id);
  204. llvm::SmallVector<llvm::Type*> param_types;
  205. // Compute the return type to use for the LLVM function. If the initializing
  206. // representation doesn't produce a value, set the return type to void.
  207. // TODO: For the `Run` entry point, remap return type to i32 if it doesn't
  208. // return a value.
  209. llvm::Type* function_return_type =
  210. (return_info.is_valid() &&
  211. return_info.init_repr.kind == SemIR::InitRepr::ByCopy)
  212. ? return_type
  213. : llvm::Type::getVoidTy(llvm_context());
  214. // TODO: Consider either storing `param_inst_ids` somewhere so that we can
  215. // reuse it from `BuildFunctionDefinition` and when building calls, or factor
  216. // out a mechanism to compute the mapping between parameters and arguments on
  217. // demand.
  218. llvm::SmallVector<SemIR::InstId> param_inst_ids;
  219. auto max_llvm_params = (return_info.has_return_slot() ? 1 : 0) +
  220. implicit_param_patterns.size() + param_patterns.size();
  221. param_types.reserve(max_llvm_params);
  222. param_inst_ids.reserve(max_llvm_params);
  223. auto return_param_id = SemIR::InstId::None;
  224. if (return_info.has_return_slot()) {
  225. param_types.push_back(
  226. llvm::PointerType::get(return_type, /*AddressSpace=*/0));
  227. return_param_id = function.return_slot_pattern_id;
  228. param_inst_ids.push_back(return_param_id);
  229. }
  230. for (auto param_pattern_id : llvm::concat<const SemIR::InstId>(
  231. implicit_param_patterns, param_patterns)) {
  232. auto param_pattern_info = SemIR::Function::GetParamPatternInfoFromPatternId(
  233. sem_ir(), param_pattern_id);
  234. if (!param_pattern_info) {
  235. continue;
  236. }
  237. auto param_type_id = SemIR::GetTypeInSpecific(
  238. sem_ir(), specific_id, param_pattern_info->inst.type_id);
  239. CARBON_CHECK(
  240. !param_type_id.AsConstantId().is_symbolic(),
  241. "Found symbolic type id after resolution when lowering type {0}.",
  242. param_pattern_info->inst.type_id);
  243. switch (auto value_rep = SemIR::ValueRepr::ForType(sem_ir(), param_type_id);
  244. value_rep.kind) {
  245. case SemIR::ValueRepr::Unknown:
  246. // This parameter type is incomplete. Fallback to describing the
  247. // function type as `void()`.
  248. return {.type = llvm::FunctionType::get(
  249. llvm::Type::getVoidTy(llvm_context()),
  250. /*isVarArg=*/false)};
  251. case SemIR::ValueRepr::None:
  252. break;
  253. case SemIR::ValueRepr::Copy:
  254. case SemIR::ValueRepr::Custom:
  255. case SemIR::ValueRepr::Pointer:
  256. auto* param_types_to_add = get_llvm_type(value_rep.type_id);
  257. param_types.push_back(param_types_to_add);
  258. param_inst_ids.push_back(param_pattern_id);
  259. break;
  260. }
  261. }
  262. return {.type = llvm::FunctionType::get(function_return_type, param_types,
  263. /*isVarArg=*/false),
  264. .param_inst_ids = std::move(param_inst_ids),
  265. .return_type = return_type,
  266. .return_param_id = return_param_id};
  267. }
  268. auto FileContext::BuildFunctionDecl(SemIR::FunctionId function_id,
  269. SemIR::SpecificId specific_id)
  270. -> llvm::Function* {
  271. const auto& function = sem_ir().functions().Get(function_id);
  272. // Don't lower generic functions. Note that associated functions in interfaces
  273. // have `Self` in scope, so are implicitly generic functions.
  274. if (function.generic_id.has_value() && !specific_id.has_value()) {
  275. return nullptr;
  276. }
  277. // Don't lower builtins.
  278. if (function.builtin_function_kind != SemIR::BuiltinFunctionKind::None) {
  279. return nullptr;
  280. }
  281. // TODO: Consider tracking whether the function has been used, and only
  282. // lowering it if it's needed.
  283. auto function_type_info = BuildFunctionTypeInfo(function, specific_id);
  284. Mangler m(*this);
  285. std::string mangled_name = m.Mangle(function_id, specific_id);
  286. auto* llvm_function = llvm::Function::Create(function_type_info.type,
  287. llvm::Function::ExternalLinkage,
  288. mangled_name, llvm_module());
  289. CARBON_CHECK(llvm_function->getName() == mangled_name,
  290. "Mangled name collision: {0}", mangled_name);
  291. // Set up parameters and the return slot.
  292. for (auto [inst_id, arg] : llvm::zip_equal(function_type_info.param_inst_ids,
  293. llvm_function->args())) {
  294. auto name_id = SemIR::NameId::None;
  295. if (inst_id == function_type_info.return_param_id) {
  296. name_id = SemIR::NameId::ReturnSlot;
  297. arg.addAttr(llvm::Attribute::getWithStructRetType(
  298. llvm_context(), function_type_info.return_type));
  299. } else {
  300. name_id = SemIR::GetPrettyNameFromPatternId(sem_ir(), inst_id);
  301. }
  302. arg.setName(sem_ir().names().GetIRBaseName(name_id));
  303. }
  304. return llvm_function;
  305. }
  306. auto FileContext::BuildFunctionDefinition(SemIR::FunctionId function_id,
  307. SemIR::SpecificId specific_id)
  308. -> void {
  309. const auto& function = sem_ir().functions().Get(function_id);
  310. const auto& body_block_ids = function.body_block_ids;
  311. if (body_block_ids.empty()) {
  312. // Function is probably defined in another file; not an error.
  313. return;
  314. }
  315. llvm::Function* llvm_function;
  316. if (specific_id.has_value()) {
  317. llvm_function = specific_functions_[specific_id.index];
  318. } else {
  319. llvm_function = GetFunction(function_id);
  320. if (!llvm_function) {
  321. // We chose not to lower this function at all, for example because it's a
  322. // generic function.
  323. return;
  324. }
  325. }
  326. // For non-generics we do not lower. For generics, the llvm function was
  327. // created via GetOrCreateFunction prior to this when building the
  328. // declaration.
  329. BuildFunctionBody(function_id, function, llvm_function, specific_id);
  330. }
  331. auto FileContext::BuildFunctionBody(SemIR::FunctionId function_id,
  332. const SemIR::Function& function,
  333. llvm::Function* llvm_function,
  334. SemIR::SpecificId specific_id) -> void {
  335. const auto& body_block_ids = function.body_block_ids;
  336. CARBON_DCHECK(llvm_function, "LLVM Function not found when lowering body.");
  337. CARBON_DCHECK(!body_block_ids.empty(),
  338. "No function body blocks found during lowering.");
  339. FunctionContext function_lowering(*this, llvm_function, specific_id,
  340. BuildDISubprogram(function, llvm_function),
  341. vlog_stream_);
  342. // Add parameters to locals.
  343. // TODO: This duplicates the mapping between sem_ir instructions and LLVM
  344. // function parameters that was already computed in BuildFunctionDecl.
  345. // We should only do that once.
  346. auto call_param_ids =
  347. sem_ir().inst_blocks().GetOrEmpty(function.call_params_id);
  348. int param_index = 0;
  349. // TODO: Find a way to ensure this code and the function-call lowering use
  350. // the same parameter ordering.
  351. // Lowers the given parameter. Must be called in LLVM calling convention
  352. // parameter order.
  353. auto lower_param = [&](SemIR::InstId param_id) {
  354. // Get the value of the parameter from the function argument.
  355. auto param_inst = sem_ir().insts().GetAs<SemIR::AnyParam>(param_id);
  356. llvm::Value* param_value;
  357. if (SemIR::ValueRepr::ForType(sem_ir(), param_inst.type_id).kind !=
  358. SemIR::ValueRepr::None) {
  359. param_value = llvm_function->getArg(param_index);
  360. ++param_index;
  361. } else {
  362. param_value = llvm::PoisonValue::get(GetType(
  363. SemIR::GetTypeInSpecific(sem_ir(), specific_id, param_inst.type_id)));
  364. }
  365. // The value of the parameter is the value of the argument.
  366. function_lowering.SetLocal(param_id, param_value);
  367. };
  368. // The subset of call_param_ids that is already in the order that the LLVM
  369. // calling convention expects.
  370. llvm::ArrayRef<SemIR::InstId> sequential_param_ids;
  371. if (function.return_slot_pattern_id.has_value()) {
  372. // The LLVM calling convention has the return slot first rather than last.
  373. // Note that this queries whether there is a return slot at the LLVM level,
  374. // whereas `function.return_slot_pattern_id.has_value()` queries whether
  375. // there is a return slot at the SemIR level.
  376. if (SemIR::ReturnTypeInfo::ForFunction(sem_ir(), function, specific_id)
  377. .has_return_slot()) {
  378. lower_param(call_param_ids.back());
  379. }
  380. sequential_param_ids = call_param_ids.drop_back();
  381. } else {
  382. sequential_param_ids = call_param_ids;
  383. }
  384. for (auto param_id : sequential_param_ids) {
  385. lower_param(param_id);
  386. }
  387. auto decl_block_id = SemIR::InstBlockId::None;
  388. if (function_id == sem_ir().global_ctor_id()) {
  389. decl_block_id = SemIR::InstBlockId::Empty;
  390. } else {
  391. decl_block_id = sem_ir()
  392. .insts()
  393. .GetAs<SemIR::FunctionDecl>(function.latest_decl_id())
  394. .decl_block_id;
  395. }
  396. // Lowers the contents of block_id into the corresponding LLVM block,
  397. // creating it if it doesn't already exist.
  398. auto lower_block = [&](SemIR::InstBlockId block_id) {
  399. CARBON_VLOG("Lowering {0}\n", block_id);
  400. auto* llvm_block = function_lowering.GetBlock(block_id);
  401. // Keep the LLVM blocks in lexical order.
  402. llvm_block->moveBefore(llvm_function->end());
  403. function_lowering.builder().SetInsertPoint(llvm_block);
  404. function_lowering.LowerBlockContents(block_id);
  405. };
  406. lower_block(decl_block_id);
  407. // If the decl block is empty, reuse it as the first body block. We don't do
  408. // this when the decl block is non-empty so that any branches back to the
  409. // first body block don't also re-execute the decl.
  410. llvm::BasicBlock* block = function_lowering.builder().GetInsertBlock();
  411. if (block->empty() &&
  412. function_lowering.TryToReuseBlock(body_block_ids.front(), block)) {
  413. // Reuse this block as the first block of the function body.
  414. } else {
  415. function_lowering.builder().CreateBr(
  416. function_lowering.GetBlock(body_block_ids.front()));
  417. }
  418. // Lower all blocks.
  419. for (auto block_id : body_block_ids) {
  420. lower_block(block_id);
  421. }
  422. // LLVM requires that the entry block has no predecessors.
  423. auto* entry_block = &llvm_function->getEntryBlock();
  424. if (entry_block->hasNPredecessorsOrMore(1)) {
  425. auto* new_entry_block = llvm::BasicBlock::Create(
  426. llvm_context(), "entry", llvm_function, entry_block);
  427. llvm::BranchInst::Create(entry_block, new_entry_block);
  428. }
  429. }
  430. auto FileContext::BuildDISubprogram(const SemIR::Function& function,
  431. const llvm::Function* llvm_function)
  432. -> llvm::DISubprogram* {
  433. if (!di_compile_unit_) {
  434. return nullptr;
  435. }
  436. auto name = sem_ir().names().GetAsStringIfIdentifier(function.name_id);
  437. CARBON_CHECK(name, "Unexpected special name for function: {0}",
  438. function.name_id);
  439. auto loc = GetLocForDI(function.definition_id);
  440. // TODO: Add more details here, including real subroutine type (once type
  441. // information is built), etc.
  442. return di_builder_.createFunction(
  443. di_compile_unit_, *name, llvm_function->getName(),
  444. /*File=*/di_builder_.createFile(loc.filename, ""),
  445. /*LineNo=*/loc.line_number,
  446. di_builder_.createSubroutineType(
  447. di_builder_.getOrCreateTypeArray(std::nullopt)),
  448. /*ScopeLine=*/0, llvm::DINode::FlagZero,
  449. llvm::DISubprogram::SPFlagDefinition);
  450. }
  451. // BuildTypeForInst is used to construct types for FileContext::BuildType below.
  452. // Implementations return the LLVM type for the instruction. This first overload
  453. // is the fallback handler for non-type instructions.
  454. template <typename InstT>
  455. requires(InstT::Kind.is_type() == SemIR::InstIsType::Never)
  456. static auto BuildTypeForInst(FileContext& /*context*/, InstT inst)
  457. -> llvm::Type* {
  458. CARBON_FATAL("Cannot use inst as type: {0}", inst);
  459. }
  460. template <typename InstT>
  461. requires(InstT::Kind.constant_kind() ==
  462. SemIR::InstConstantKind::SymbolicOnly &&
  463. InstT::Kind.is_type() != SemIR::InstIsType::Never)
  464. static auto BuildTypeForInst(FileContext& context, InstT /*inst*/)
  465. -> llvm::Type* {
  466. // Treat non-monomorphized symbolic types as opaque.
  467. return llvm::StructType::get(context.llvm_context());
  468. }
  469. static auto BuildTypeForInst(FileContext& context, SemIR::ArrayType inst)
  470. -> llvm::Type* {
  471. return llvm::ArrayType::get(
  472. context.GetType(inst.element_type_id),
  473. *context.sem_ir().GetArrayBoundValue(inst.bound_id));
  474. }
  475. static auto BuildTypeForInst(FileContext& /*context*/, SemIR::AutoType inst)
  476. -> llvm::Type* {
  477. CARBON_FATAL("Unexpected builtin type in lowering: {0}", inst);
  478. }
  479. static auto BuildTypeForInst(FileContext& context, SemIR::BoolType /*inst*/)
  480. -> llvm::Type* {
  481. // TODO: We may want to have different representations for `bool` storage
  482. // (`i8`) versus for `bool` values (`i1`).
  483. return llvm::Type::getInt1Ty(context.llvm_context());
  484. }
  485. static auto BuildTypeForInst(FileContext& context, SemIR::ClassType inst)
  486. -> llvm::Type* {
  487. auto object_repr_id = context.sem_ir()
  488. .classes()
  489. .Get(inst.class_id)
  490. .GetObjectRepr(context.sem_ir(), inst.specific_id);
  491. return context.GetType(object_repr_id);
  492. }
  493. static auto BuildTypeForInst(FileContext& context, SemIR::ConstType inst)
  494. -> llvm::Type* {
  495. return context.GetType(inst.inner_id);
  496. }
  497. static auto BuildTypeForInst(FileContext& /*context*/,
  498. SemIR::ErrorInst /*inst*/) -> llvm::Type* {
  499. // This is a complete type but uses of it should never be lowered.
  500. return nullptr;
  501. }
  502. static auto BuildTypeForInst(FileContext& context, SemIR::FloatType /*inst*/)
  503. -> llvm::Type* {
  504. // TODO: Handle different sizes.
  505. return llvm::Type::getDoubleTy(context.llvm_context());
  506. }
  507. static auto BuildTypeForInst(FileContext& context, SemIR::IntType inst)
  508. -> llvm::Type* {
  509. auto width =
  510. context.sem_ir().insts().TryGetAs<SemIR::IntValue>(inst.bit_width_id);
  511. CARBON_CHECK(width, "Can't lower int type with symbolic width");
  512. return llvm::IntegerType::get(
  513. context.llvm_context(),
  514. context.sem_ir().ints().Get(width->int_id).getZExtValue());
  515. }
  516. static auto BuildTypeForInst(FileContext& context,
  517. SemIR::LegacyFloatType /*inst*/) -> llvm::Type* {
  518. return llvm::Type::getDoubleTy(context.llvm_context());
  519. }
  520. static auto BuildTypeForInst(FileContext& context, SemIR::PointerType /*inst*/)
  521. -> llvm::Type* {
  522. return llvm::PointerType::get(context.llvm_context(), /*AddressSpace=*/0);
  523. }
  524. static auto BuildTypeForInst(FileContext& context, SemIR::StructType inst)
  525. -> llvm::Type* {
  526. auto fields = context.sem_ir().struct_type_fields().Get(inst.fields_id);
  527. llvm::SmallVector<llvm::Type*> subtypes;
  528. subtypes.reserve(fields.size());
  529. for (auto field : fields) {
  530. subtypes.push_back(context.GetType(field.type_id));
  531. }
  532. return llvm::StructType::get(context.llvm_context(), subtypes);
  533. }
  534. static auto BuildTypeForInst(FileContext& context, SemIR::TupleType inst)
  535. -> llvm::Type* {
  536. // TODO: Investigate special-casing handling of empty tuples so that they
  537. // can be collectively replaced with LLVM's void, particularly around
  538. // function returns. LLVM doesn't allow declaring variables with a void
  539. // type, so that may require significant special casing.
  540. auto elements = context.sem_ir().type_blocks().Get(inst.elements_id);
  541. llvm::SmallVector<llvm::Type*> subtypes;
  542. subtypes.reserve(elements.size());
  543. for (auto element_id : elements) {
  544. subtypes.push_back(context.GetType(element_id));
  545. }
  546. return llvm::StructType::get(context.llvm_context(), subtypes);
  547. }
  548. static auto BuildTypeForInst(FileContext& context, SemIR::TypeType /*inst*/)
  549. -> llvm::Type* {
  550. return context.GetTypeType();
  551. }
  552. static auto BuildTypeForInst(FileContext& context, SemIR::VtableType /*inst*/)
  553. -> llvm::Type* {
  554. return llvm::Type::getVoidTy(context.llvm_context());
  555. }
  556. template <typename InstT>
  557. requires(InstT::Kind.template IsAnyOf<SemIR::SpecificFunctionType,
  558. SemIR::StringType>())
  559. static auto BuildTypeForInst(FileContext& context, InstT /*inst*/)
  560. -> llvm::Type* {
  561. // TODO: Decide how we want to represent `StringType`.
  562. return llvm::PointerType::get(context.llvm_context(), 0);
  563. }
  564. template <typename InstT>
  565. requires(InstT::Kind
  566. .template IsAnyOf<SemIR::BoundMethodType, SemIR::IntLiteralType,
  567. SemIR::NamespaceType, SemIR::WitnessType>())
  568. static auto BuildTypeForInst(FileContext& context, InstT /*inst*/)
  569. -> llvm::Type* {
  570. // Return an empty struct as a placeholder.
  571. return llvm::StructType::get(context.llvm_context());
  572. }
  573. template <typename InstT>
  574. requires(InstT::Kind.template IsAnyOf<
  575. SemIR::AssociatedEntityType, SemIR::FacetType, SemIR::FunctionType,
  576. SemIR::FunctionTypeWithSelfType, SemIR::GenericClassType,
  577. SemIR::GenericInterfaceType, SemIR::UnboundElementType,
  578. SemIR::WhereExpr>())
  579. static auto BuildTypeForInst(FileContext& context, InstT /*inst*/)
  580. -> llvm::Type* {
  581. // Return an empty struct as a placeholder.
  582. // TODO: Should we model an interface as a witness table, or an associated
  583. // entity as an index?
  584. return llvm::StructType::get(context.llvm_context());
  585. }
  586. auto FileContext::BuildType(SemIR::InstId inst_id) -> llvm::Type* {
  587. // Use overload resolution to select the implementation, producing compile
  588. // errors when BuildTypeForInst isn't defined for a given instruction.
  589. CARBON_KIND_SWITCH(sem_ir_->insts().Get(inst_id)) {
  590. #define CARBON_SEM_IR_INST_KIND(Name) \
  591. case CARBON_KIND(SemIR::Name inst): { \
  592. return BuildTypeForInst(*this, inst); \
  593. }
  594. #include "toolchain/sem_ir/inst_kind.def"
  595. }
  596. }
  597. auto FileContext::BuildGlobalVariableDecl(SemIR::VarStorage var_storage)
  598. -> llvm::GlobalVariable* {
  599. // TODO: Mangle name.
  600. auto mangled_name =
  601. *sem_ir().names().GetAsStringIfIdentifier(var_storage.pretty_name_id);
  602. auto* type = GetType(var_storage.type_id);
  603. return new llvm::GlobalVariable(
  604. llvm_module(), type,
  605. /*isConstant=*/false, llvm::GlobalVariable::InternalLinkage,
  606. llvm::Constant::getNullValue(type), mangled_name);
  607. }
  608. auto FileContext::GetLocForDI(SemIR::InstId inst_id) -> LocForDI {
  609. SemIR::AbsoluteNodeId resolved = GetAbsoluteNodeId(sem_ir_, inst_id).back();
  610. const auto& tree_and_subtrees =
  611. (*tree_and_subtrees_getters_for_debug_info_)[resolved.check_ir_id
  612. .index]();
  613. const auto& tokens = tree_and_subtrees.tree().tokens();
  614. if (resolved.node_id.has_value()) {
  615. auto token = tree_and_subtrees.GetSubtreeTokenRange(resolved.node_id).begin;
  616. return {.filename = tokens.source().filename(),
  617. .line_number = tokens.GetLineNumber(token),
  618. .column_number = tokens.GetColumnNumber(token)};
  619. } else {
  620. return {.filename = tokens.source().filename(),
  621. .line_number = 0,
  622. .column_number = 0};
  623. }
  624. }
  625. } // namespace Carbon::Lower