file_context.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/lower/file_context.h"
  5. #include <memory>
  6. #include <optional>
  7. #include <string>
  8. #include <utility>
  9. #include "clang/CodeGen/ModuleBuilder.h"
  10. #include "common/check.h"
  11. #include "common/vlog.h"
  12. #include "llvm/ADT/STLExtras.h"
  13. #include "llvm/ADT/Sequence.h"
  14. #include "llvm/Linker/Linker.h"
  15. #include "llvm/Support/BLAKE3.h"
  16. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  17. #include "llvm/Transforms/Utils/ModuleUtils.h"
  18. #include "toolchain/base/kind_switch.h"
  19. #include "toolchain/lower/constant.h"
  20. #include "toolchain/lower/function_context.h"
  21. #include "toolchain/lower/mangler.h"
  22. #include "toolchain/sem_ir/absolute_node_id.h"
  23. #include "toolchain/sem_ir/entry_point.h"
  24. #include "toolchain/sem_ir/expr_info.h"
  25. #include "toolchain/sem_ir/file.h"
  26. #include "toolchain/sem_ir/function.h"
  27. #include "toolchain/sem_ir/generic.h"
  28. #include "toolchain/sem_ir/ids.h"
  29. #include "toolchain/sem_ir/inst.h"
  30. #include "toolchain/sem_ir/inst_kind.h"
  31. #include "toolchain/sem_ir/pattern.h"
  32. #include "toolchain/sem_ir/typed_insts.h"
  33. namespace Carbon::Lower {
  34. FileContext::FileContext(
  35. llvm::LLVMContext& llvm_context,
  36. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
  37. std::optional<llvm::ArrayRef<Parse::GetTreeAndSubtreesFn>>
  38. tree_and_subtrees_getters_for_debug_info,
  39. llvm::StringRef module_name, const SemIR::File& sem_ir,
  40. clang::ASTUnit* cpp_ast, const SemIR::InstNamer* inst_namer,
  41. llvm::raw_ostream* vlog_stream)
  42. : llvm_context_(&llvm_context),
  43. llvm_module_(std::make_unique<llvm::Module>(module_name, llvm_context)),
  44. fs_(std::move(fs)),
  45. di_builder_(*llvm_module_),
  46. di_compile_unit_(
  47. tree_and_subtrees_getters_for_debug_info
  48. ? BuildDICompileUnit(module_name, *llvm_module_, di_builder_)
  49. : nullptr),
  50. tree_and_subtrees_getters_for_debug_info_(
  51. tree_and_subtrees_getters_for_debug_info),
  52. sem_ir_(&sem_ir),
  53. cpp_ast_(cpp_ast),
  54. inst_namer_(inst_namer),
  55. vlog_stream_(vlog_stream) {
  56. // Initialization that relies on invariants of the class.
  57. cpp_code_generator_ = CreateCppCodeGenerator();
  58. CARBON_CHECK(!sem_ir.has_errors(),
  59. "Generating LLVM IR from invalid SemIR::File is unsupported.");
  60. }
  61. // TODO: Move this to lower.cpp.
  62. auto FileContext::Run() -> std::unique_ptr<llvm::Module> {
  63. CARBON_CHECK(llvm_module_, "Run can only be called once.");
  64. if (cpp_code_generator_) {
  65. cpp_code_generator_->Initialize(cpp_ast()->getASTContext());
  66. }
  67. // Lower all types that were required to be complete.
  68. types_.resize(sem_ir_->insts().size());
  69. for (auto type_id : sem_ir_->types().complete_types()) {
  70. if (type_id.index >= 0) {
  71. types_[type_id.index] = BuildType(sem_ir_->types().GetInstId(type_id));
  72. }
  73. }
  74. // Lower function declarations.
  75. functions_.resize_for_overwrite(sem_ir_->functions().size());
  76. for (auto [id, _] : sem_ir_->functions().enumerate()) {
  77. functions_[id.index] = BuildFunctionDecl(id);
  78. }
  79. for (const auto& class_info : sem_ir_->classes().array_ref()) {
  80. if (auto* llvm_vtable = BuildVtable(class_info)) {
  81. global_variables_.Insert(class_info.vtable_id, llvm_vtable);
  82. }
  83. }
  84. // Specific functions are lowered when we emit a reference to them.
  85. specific_functions_.resize(sem_ir_->specifics().size());
  86. // Additional data stored for specifics, for when attempting to coalesce.
  87. // Indexed by `GenericId`.
  88. lowered_specifics_.resize(sem_ir_->generics().size());
  89. // Indexed by `SpecificId`.
  90. lowered_specifics_type_fingerprint_.resize(sem_ir_->specifics().size());
  91. lowered_specific_fingerprint_.resize(sem_ir_->specifics().size());
  92. equivalent_specifics_.resize(sem_ir_->specifics().size(),
  93. SemIR::SpecificId::None);
  94. // Lower constants.
  95. constants_.resize(sem_ir_->insts().size());
  96. LowerConstants(*this, constants_);
  97. // Lower global variable definitions.
  98. // TODO: Storing both a `constants_` array and a separate `global_variables_`
  99. // map is redundant.
  100. for (auto inst_id :
  101. sem_ir().inst_blocks().Get(sem_ir().top_inst_block_id())) {
  102. // Only `VarStorage` indicates a global variable declaration in the
  103. // top instruction block.
  104. if (auto var = sem_ir().insts().TryGetAs<SemIR::VarStorage>(inst_id)) {
  105. // Get the global variable declaration. We created this when lowering the
  106. // constant unless the variable is unnamed, in which case we need to
  107. // create it now.
  108. llvm::GlobalVariable* llvm_var = nullptr;
  109. if (sem_ir().constant_values().Get(inst_id).is_constant()) {
  110. llvm_var = cast<llvm::GlobalVariable>(
  111. GetGlobal(inst_id, SemIR::SpecificId::None));
  112. } else {
  113. llvm_var = BuildGlobalVariableDecl(*var);
  114. }
  115. // Convert the declaration of this variable into a definition by adding an
  116. // initializer.
  117. global_variables_.Insert(inst_id, llvm_var);
  118. llvm_var->setInitializer(
  119. llvm::Constant::getNullValue(llvm_var->getValueType()));
  120. }
  121. }
  122. // Lower function definitions.
  123. for (auto [id, _] : sem_ir_->functions().enumerate()) {
  124. BuildFunctionDefinition(id);
  125. }
  126. // Lower function definitions for generics.
  127. // This cannot be a range-based loop, as new definitions can be added
  128. // while building other definitions.
  129. // NOLINTNEXTLINE
  130. for (size_t i = 0; i != specific_function_definitions_.size(); ++i) {
  131. auto [function_id, specific_id] = specific_function_definitions_[i];
  132. BuildFunctionDefinition(function_id, specific_id);
  133. }
  134. // Find equivalent specifics (from the same generic), replace all uses and
  135. // remove duplicately lowered function definitions.
  136. CoalesceEquivalentSpecifics();
  137. // Append `__global_init` to `llvm::global_ctors` to initialize global
  138. // variables.
  139. if (sem_ir().global_ctor_id().has_value()) {
  140. llvm::appendToGlobalCtors(llvm_module(),
  141. GetFunction(sem_ir().global_ctor_id()),
  142. /*Priority=*/0);
  143. }
  144. if (cpp_code_generator_) {
  145. cpp_code_generator_->HandleTranslationUnit(cpp_ast()->getASTContext());
  146. bool link_error = llvm::Linker::linkModules(
  147. /*Dest=*/*llvm_module_,
  148. /*Src=*/std::unique_ptr<llvm::Module>(
  149. cpp_code_generator_->ReleaseModule()));
  150. CARBON_CHECK(!link_error);
  151. }
  152. return std::move(llvm_module_);
  153. }
  154. auto FileContext::InsertPair(
  155. SemIR::SpecificId specific_id1, SemIR::SpecificId specific_id2,
  156. Set<std::pair<SemIR::SpecificId, SemIR::SpecificId>>& set_of_pairs)
  157. -> bool {
  158. if (specific_id1.index > specific_id2.index) {
  159. std::swap(specific_id1.index, specific_id2.index);
  160. }
  161. auto insert_result =
  162. set_of_pairs.Insert(std::make_pair(specific_id1, specific_id2));
  163. return insert_result.is_inserted();
  164. }
  165. auto FileContext::ContainsPair(
  166. SemIR::SpecificId specific_id1, SemIR::SpecificId specific_id2,
  167. const Set<std::pair<SemIR::SpecificId, SemIR::SpecificId>>& set_of_pairs)
  168. -> bool {
  169. if (specific_id1.index > specific_id2.index) {
  170. std::swap(specific_id1.index, specific_id2.index);
  171. }
  172. return set_of_pairs.Contains(std::make_pair(specific_id1, specific_id2));
  173. }
  174. auto FileContext::CoalesceEquivalentSpecifics() -> void {
  175. for (auto& specifics : lowered_specifics_) {
  176. // i cannot be unsigned due to the comparison with a negative number when
  177. // the specifics vector is empty.
  178. for (int i = 0; i < static_cast<int>(specifics.size()) - 1; ++i) {
  179. // This specific was already replaced, skip it.
  180. if (equivalent_specifics_[specifics[i].index].has_value() &&
  181. equivalent_specifics_[specifics[i].index] != specifics[i]) {
  182. specifics[i] = specifics[specifics.size() - 1];
  183. specifics.pop_back();
  184. --i;
  185. continue;
  186. }
  187. // TODO: Improve quadratic behavior by using a single hash based on
  188. // `lowered_specifics_type_fingerprint_` and `common_fingerprint`.
  189. for (int j = i + 1; j < static_cast<int>(specifics.size()); ++j) {
  190. // When the specific was already replaced, skip it.
  191. if (equivalent_specifics_[specifics[j].index].has_value() &&
  192. equivalent_specifics_[specifics[j].index] != specifics[j]) {
  193. specifics[j] = specifics[specifics.size() - 1];
  194. specifics.pop_back();
  195. --j;
  196. continue;
  197. }
  198. // When the two specifics are not equivalent due to the function type
  199. // info stored in lowered_specifics_types, mark non-equivalance. This
  200. // can be reused to short-cut another path and continue the search for
  201. // other equivalences.
  202. if (!AreFunctionTypesEquivalent(specifics[i], specifics[j])) {
  203. InsertPair(specifics[i], specifics[j], non_equivalent_specifics_);
  204. continue;
  205. }
  206. Set<std::pair<SemIR::SpecificId, SemIR::SpecificId>>
  207. visited_equivalent_specifics;
  208. InsertPair(specifics[i], specifics[j], visited_equivalent_specifics);
  209. // Function type information matches; check usages inside the function
  210. // body that are dependent on the specific. This information has been
  211. // stored in lowered_states while lowering each function body.
  212. if (AreFunctionBodiesEquivalent(specifics[i], specifics[j],
  213. visited_equivalent_specifics)) {
  214. // When processing equivalences, we may change the canonical specific
  215. // multiple times, so we don't delete replaced specifics until the
  216. // end.
  217. llvm::SmallVector<SemIR::SpecificId> specifics_to_delete;
  218. visited_equivalent_specifics.ForEach(
  219. [&](std::pair<SemIR::SpecificId, SemIR::SpecificId>
  220. equivalent_entry) {
  221. CARBON_VLOG("Found equivalent specifics: {0}, {1}",
  222. equivalent_entry.first, equivalent_entry.second);
  223. ProcessSpecificEquivalence(equivalent_entry,
  224. specifics_to_delete);
  225. });
  226. // Delete function bodies for already replaced functions.
  227. for (auto specific_id : specifics_to_delete) {
  228. specific_functions_[specific_id.index]->eraseFromParent();
  229. specific_functions_[specific_id.index] =
  230. specific_functions_[equivalent_specifics_[specific_id.index]
  231. .index];
  232. }
  233. // Removed the replaced specific from the list of emitted specifics.
  234. // Only the top level, since the others are somewhere else in the
  235. // vector, they will be found and removed during processing.
  236. specifics[j] = specifics[specifics.size() - 1];
  237. specifics.pop_back();
  238. --j;
  239. } else {
  240. // Only mark non-equivalence based on state for starting specifics.
  241. InsertPair(specifics[i], specifics[j], non_equivalent_specifics_);
  242. }
  243. }
  244. }
  245. }
  246. }
  247. auto FileContext::ProcessSpecificEquivalence(
  248. std::pair<SemIR::SpecificId, SemIR::SpecificId> pair,
  249. llvm::SmallVector<SemIR::SpecificId>& specifics_to_delete) -> void {
  250. auto [specific_id1, specific_id2] = pair;
  251. CARBON_CHECK(specific_id1.has_value() && specific_id2.has_value(),
  252. "Expected values in equivalence check");
  253. auto get_canon = [&](SemIR::SpecificId specific_id) {
  254. return equivalent_specifics_[specific_id.index].has_value()
  255. ? std::make_pair(
  256. equivalent_specifics_[specific_id.index],
  257. (equivalent_specifics_[specific_id.index] != specific_id))
  258. : std::make_pair(specific_id, false);
  259. };
  260. auto [canon_id1, replaced_before1] = get_canon(specific_id1);
  261. auto [canon_id2, replaced_before2] = get_canon(specific_id2);
  262. if (canon_id1 == canon_id2) {
  263. // Already equivalent, there was a previous replacement.
  264. return;
  265. }
  266. if (canon_id1.index >= canon_id2.index) {
  267. // Prefer the earlier index for canonical values.
  268. std::swap(canon_id1, canon_id2);
  269. std::swap(replaced_before1, replaced_before2);
  270. }
  271. // Update equivalent_specifics_ for all. This is used as an indicator that
  272. // this specific_id may be the canonical one when reducing the equivalence
  273. // chains in `IsKnownEquivalence`.
  274. equivalent_specifics_[specific_id1.index] = canon_id1;
  275. equivalent_specifics_[specific_id2.index] = canon_id1;
  276. specific_functions_[canon_id2.index]->replaceAllUsesWith(
  277. specific_functions_[canon_id1.index]);
  278. if (!replaced_before2) {
  279. specifics_to_delete.push_back(canon_id2);
  280. }
  281. }
  282. auto FileContext::IsKnownEquivalence(SemIR::SpecificId specific_id1,
  283. SemIR::SpecificId specific_id2) -> bool {
  284. if (!equivalent_specifics_[specific_id1.index].has_value() ||
  285. !equivalent_specifics_[specific_id2.index].has_value()) {
  286. return false;
  287. }
  288. auto update_equivalent_specific = [&](SemIR::SpecificId specific_id) {
  289. llvm::SmallVector<SemIR::SpecificId> stack;
  290. SemIR::SpecificId specific_to_update = specific_id;
  291. while (equivalent_specifics_[equivalent_specifics_[specific_to_update.index]
  292. .index] !=
  293. equivalent_specifics_[specific_to_update.index]) {
  294. stack.push_back(specific_to_update);
  295. specific_to_update = equivalent_specifics_[specific_to_update.index];
  296. }
  297. for (auto specific : llvm::reverse(stack)) {
  298. equivalent_specifics_[specific.index] =
  299. equivalent_specifics_[equivalent_specifics_[specific.index].index];
  300. }
  301. };
  302. update_equivalent_specific(specific_id1);
  303. update_equivalent_specific(specific_id2);
  304. return equivalent_specifics_[specific_id1.index] ==
  305. equivalent_specifics_[specific_id2.index];
  306. }
  307. auto FileContext::AreFunctionTypesEquivalent(SemIR::SpecificId specific_id1,
  308. SemIR::SpecificId specific_id2)
  309. -> bool {
  310. CARBON_CHECK(specific_id1.has_value() && specific_id2.has_value());
  311. return lowered_specifics_type_fingerprint_[specific_id1.index] ==
  312. lowered_specifics_type_fingerprint_[specific_id2.index];
  313. }
  314. auto FileContext::AreFunctionBodiesEquivalent(
  315. SemIR::SpecificId specific_id1, SemIR::SpecificId specific_id2,
  316. Set<std::pair<SemIR::SpecificId, SemIR::SpecificId>>&
  317. visited_equivalent_specifics) -> bool {
  318. llvm::SmallVector<std::pair<SemIR::SpecificId, SemIR::SpecificId>> worklist;
  319. worklist.push_back({specific_id1, specific_id2});
  320. while (!worklist.empty()) {
  321. auto outer_pair = worklist.pop_back_val();
  322. auto [specific_id1, specific_id2] = outer_pair;
  323. auto state1 = lowered_specific_fingerprint_[specific_id1.index];
  324. auto state2 = lowered_specific_fingerprint_[specific_id2.index];
  325. if (state1.common_fingerprint != state2.common_fingerprint) {
  326. InsertPair(specific_id1, specific_id2, non_equivalent_specifics_);
  327. return false;
  328. }
  329. if (state1.specific_fingerprint == state2.specific_fingerprint) {
  330. continue;
  331. }
  332. // A size difference should have been detected by the common fingerprint.
  333. CARBON_CHECK(state1.calls.size() == state2.calls.size(),
  334. "Number of specific calls expected to be the same.");
  335. for (auto [state1_call, state2_call] :
  336. llvm::zip(state1.calls, state2.calls)) {
  337. if (state1_call != state2_call) {
  338. if (ContainsPair(state1_call, state2_call, non_equivalent_specifics_)) {
  339. return false;
  340. }
  341. if (IsKnownEquivalence(state1_call, state2_call)) {
  342. continue;
  343. }
  344. if (!InsertPair(state1_call, state2_call,
  345. visited_equivalent_specifics)) {
  346. continue;
  347. }
  348. // Leave the added equivalence pair in place and continue.
  349. worklist.push_back({state1_call, state2_call});
  350. }
  351. }
  352. }
  353. return true;
  354. }
  355. auto FileContext::BuildDICompileUnit(llvm::StringRef module_name,
  356. llvm::Module& llvm_module,
  357. llvm::DIBuilder& di_builder)
  358. -> llvm::DICompileUnit* {
  359. llvm_module.addModuleFlag(llvm::Module::Max, "Dwarf Version", 5);
  360. llvm_module.addModuleFlag(llvm::Module::Warning, "Debug Info Version",
  361. llvm::DEBUG_METADATA_VERSION);
  362. // TODO: Include directory path in the compile_unit_file.
  363. llvm::DIFile* compile_unit_file = di_builder.createFile(module_name, "");
  364. // TODO: Introduce a new language code for Carbon. C works well for now since
  365. // it's something debuggers will already know/have support for at least.
  366. // Probably have to bump to C++ at some point for virtual functions,
  367. // templates, etc.
  368. return di_builder.createCompileUnit(llvm::dwarf::DW_LANG_C, compile_unit_file,
  369. "carbon",
  370. /*isOptimized=*/false, /*Flags=*/"",
  371. /*RV=*/0);
  372. }
  373. auto FileContext::CreateCppCodeGenerator()
  374. -> std::unique_ptr<clang::CodeGenerator> {
  375. if (!cpp_ast()) {
  376. return nullptr;
  377. }
  378. RawStringOstream clang_module_name_stream;
  379. clang_module_name_stream << llvm_module_->getName() << ".clang";
  380. // Do not emit Clang's name and version as the creator of the output file.
  381. cpp_code_gen_options_.EmitVersionIdentMetadata = false;
  382. return std::unique_ptr<clang::CodeGenerator>(clang::CreateLLVMCodeGen(
  383. cpp_ast()->getASTContext().getDiagnostics(),
  384. clang_module_name_stream.TakeStr(), fs_, cpp_header_search_options_,
  385. cpp_preprocessor_options_, cpp_code_gen_options_, *llvm_context_));
  386. }
  387. auto FileContext::GetGlobal(SemIR::InstId inst_id,
  388. SemIR::SpecificId specific_id) -> llvm::Value* {
  389. auto const_id = GetConstantValueInSpecific(sem_ir(), specific_id, inst_id);
  390. CARBON_CHECK(const_id.is_concrete(), "Missing value: {0} {1} {2}", inst_id,
  391. specific_id, sem_ir().insts().Get(inst_id));
  392. auto const_inst_id = sem_ir().constant_values().GetInstId(const_id);
  393. auto* const_value = constants_[const_inst_id.index];
  394. // For value expressions and initializing expressions, the value produced by
  395. // a constant instruction is a value representation of the constant. For
  396. // initializing expressions, `FinishInit` will perform a copy if needed.
  397. switch (auto cat = SemIR::GetExprCategory(sem_ir(), const_inst_id)) {
  398. case SemIR::ExprCategory::Value:
  399. case SemIR::ExprCategory::Initializing:
  400. break;
  401. case SemIR::ExprCategory::DurableRef:
  402. case SemIR::ExprCategory::EphemeralRef:
  403. // Constant reference expressions lower to an address.
  404. return const_value;
  405. case SemIR::ExprCategory::NotExpr:
  406. case SemIR::ExprCategory::Error:
  407. case SemIR::ExprCategory::Mixed:
  408. CARBON_FATAL("Unexpected category {0} for lowered constant {1}", cat,
  409. sem_ir().insts().Get(const_inst_id));
  410. };
  411. auto value_rep = SemIR::ValueRepr::ForType(
  412. sem_ir(), sem_ir().insts().Get(const_inst_id).type_id());
  413. if (value_rep.kind != SemIR::ValueRepr::Pointer) {
  414. return const_value;
  415. }
  416. // The value representation is a pointer. Generate a variable to hold the
  417. // value, or find and reuse an existing one.
  418. if (auto result = global_variables().Lookup(const_inst_id)) {
  419. return result.value();
  420. }
  421. // Include both the name of the constant, if any, and the point of use in
  422. // the name of the variable.
  423. llvm::StringRef const_name;
  424. llvm::StringRef use_name;
  425. if (inst_namer_) {
  426. const_name = inst_namer_->GetUnscopedNameFor(const_inst_id);
  427. use_name = inst_namer_->GetUnscopedNameFor(inst_id);
  428. }
  429. // We always need to give the global a name even if the instruction namer
  430. // doesn't have one to use.
  431. if (const_name.empty()) {
  432. const_name = "const";
  433. }
  434. if (use_name.empty()) {
  435. use_name = "anon";
  436. }
  437. llvm::StringRef sep = (use_name[0] == '.') ? "" : ".";
  438. auto* global_variable = new llvm::GlobalVariable(
  439. llvm_module(), GetType(sem_ir().GetPointeeType(value_rep.type_id)),
  440. /*isConstant=*/true, llvm::GlobalVariable::InternalLinkage, const_value,
  441. const_name + sep + use_name);
  442. global_variables_.Insert(const_inst_id, global_variable);
  443. return global_variable;
  444. }
  445. auto FileContext::GetOrCreateFunction(SemIR::FunctionId function_id,
  446. SemIR::SpecificId specific_id)
  447. -> llvm::Function* {
  448. // Non-generic functions are declared eagerly.
  449. if (!specific_id.has_value()) {
  450. return GetFunction(function_id);
  451. }
  452. if (auto* result = specific_functions_[specific_id.index]) {
  453. return result;
  454. }
  455. auto* result = BuildFunctionDecl(function_id, specific_id);
  456. // TODO: Add this function to a list of specific functions whose definitions
  457. // we need to emit.
  458. specific_functions_[specific_id.index] = result;
  459. // TODO: Use this to generate definitions for these functions.
  460. specific_function_definitions_.push_back({function_id, specific_id});
  461. return result;
  462. }
  463. auto FileContext::BuildFunctionTypeInfo(const SemIR::Function& function,
  464. SemIR::SpecificId specific_id)
  465. -> FunctionTypeInfo {
  466. const auto return_info =
  467. SemIR::ReturnTypeInfo::ForFunction(sem_ir(), function, specific_id);
  468. if (!return_info.is_valid()) {
  469. // The return type has not been completed, create a trivial type instead.
  470. return {.type =
  471. llvm::FunctionType::get(llvm::Type::getVoidTy(llvm_context()),
  472. /*isVarArg=*/false)};
  473. }
  474. auto get_llvm_type = [&](SemIR::TypeId type_id) -> llvm::Type* {
  475. if (!type_id.has_value()) {
  476. return nullptr;
  477. }
  478. return GetType(type_id);
  479. };
  480. // TODO: expose the `Call` parameter patterns in `Function`, and use them here
  481. // instead of reconstructing them via the syntactic parameter lists.
  482. auto implicit_param_patterns =
  483. sem_ir().inst_blocks().GetOrEmpty(function.implicit_param_patterns_id);
  484. auto param_patterns =
  485. sem_ir().inst_blocks().GetOrEmpty(function.param_patterns_id);
  486. auto* return_type = get_llvm_type(return_info.type_id);
  487. llvm::SmallVector<llvm::Type*> param_types;
  488. // Compute the return type to use for the LLVM function. If the initializing
  489. // representation doesn't produce a value, set the return type to void.
  490. // TODO: For the `Run` entry point, remap return type to i32 if it doesn't
  491. // return a value.
  492. llvm::Type* function_return_type =
  493. (return_info.is_valid() &&
  494. return_info.init_repr.kind == SemIR::InitRepr::ByCopy)
  495. ? return_type
  496. : llvm::Type::getVoidTy(llvm_context());
  497. // TODO: Consider either storing `param_inst_ids` somewhere so that we can
  498. // reuse it from `BuildFunctionDefinition` and when building calls, or factor
  499. // out a mechanism to compute the mapping between parameters and arguments on
  500. // demand.
  501. llvm::SmallVector<SemIR::InstId> param_inst_ids;
  502. auto max_llvm_params = (return_info.has_return_slot() ? 1 : 0) +
  503. implicit_param_patterns.size() + param_patterns.size();
  504. param_types.reserve(max_llvm_params);
  505. param_inst_ids.reserve(max_llvm_params);
  506. auto return_param_id = SemIR::InstId::None;
  507. if (return_info.has_return_slot()) {
  508. param_types.push_back(
  509. llvm::PointerType::get(llvm_context(), /*AddressSpace=*/0));
  510. return_param_id = function.return_slot_pattern_id;
  511. param_inst_ids.push_back(return_param_id);
  512. }
  513. for (auto param_pattern_id : llvm::concat<const SemIR::InstId>(
  514. implicit_param_patterns, param_patterns)) {
  515. auto param_pattern_info = SemIR::Function::GetParamPatternInfoFromPatternId(
  516. sem_ir(), param_pattern_id);
  517. if (!param_pattern_info) {
  518. continue;
  519. }
  520. auto param_type_id = ExtractScrutineeType(
  521. sem_ir(), SemIR::GetTypeOfInstInSpecific(sem_ir(), specific_id,
  522. param_pattern_info->inst_id));
  523. CARBON_CHECK(
  524. !param_type_id.AsConstantId().is_symbolic(),
  525. "Found symbolic type id after resolution when lowering type {0}.",
  526. param_pattern_info->inst.type_id);
  527. switch (auto value_rep = SemIR::ValueRepr::ForType(sem_ir(), param_type_id);
  528. value_rep.kind) {
  529. case SemIR::ValueRepr::Unknown:
  530. // This parameter type is incomplete. Fallback to describing the
  531. // function type as `void()`.
  532. return {.type = llvm::FunctionType::get(
  533. llvm::Type::getVoidTy(llvm_context()),
  534. /*isVarArg=*/false)};
  535. case SemIR::ValueRepr::None:
  536. break;
  537. case SemIR::ValueRepr::Copy:
  538. case SemIR::ValueRepr::Custom:
  539. case SemIR::ValueRepr::Pointer:
  540. auto* param_types_to_add = get_llvm_type(value_rep.type_id);
  541. param_types.push_back(param_types_to_add);
  542. param_inst_ids.push_back(param_pattern_id);
  543. break;
  544. }
  545. }
  546. return {.type = llvm::FunctionType::get(function_return_type, param_types,
  547. /*isVarArg=*/false),
  548. .param_inst_ids = std::move(param_inst_ids),
  549. .return_type = return_type,
  550. .return_param_id = return_param_id};
  551. }
  552. auto FileContext::BuildFunctionDecl(SemIR::FunctionId function_id,
  553. SemIR::SpecificId specific_id)
  554. -> llvm::Function* {
  555. const auto& function = sem_ir().functions().Get(function_id);
  556. // Don't lower generic functions. Note that associated functions in interfaces
  557. // have `Self` in scope, so are implicitly generic functions.
  558. if (function.generic_id.has_value() && !specific_id.has_value()) {
  559. return nullptr;
  560. }
  561. // Don't lower builtins.
  562. if (function.builtin_function_kind != SemIR::BuiltinFunctionKind::None) {
  563. return nullptr;
  564. }
  565. // TODO: Consider tracking whether the function has been used, and only
  566. // lowering it if it's needed.
  567. auto function_type_info = BuildFunctionTypeInfo(function, specific_id);
  568. auto linkage = specific_id.has_value() ? llvm::Function::LinkOnceODRLinkage
  569. : llvm::Function::ExternalLinkage;
  570. Mangler m(*this);
  571. std::string mangled_name = m.Mangle(function_id, specific_id);
  572. // Create a unique fingerprint for the function type.
  573. // For now, compute the function type fingerprint only for specifics, though
  574. // we might need it for all functions in order to create a canonical
  575. // fingerprint across translation units.
  576. if (specific_id.has_value()) {
  577. llvm::BLAKE3 function_type_fingerprint;
  578. RawStringOstream os;
  579. function_type_info.type->print(os);
  580. function_type_fingerprint.update(os.TakeStr());
  581. function_type_fingerprint.final(
  582. lowered_specifics_type_fingerprint_[specific_id.index]);
  583. }
  584. auto* llvm_function = llvm::Function::Create(function_type_info.type, linkage,
  585. mangled_name, llvm_module());
  586. CARBON_CHECK(llvm_function->getName() == mangled_name,
  587. "Mangled name collision: {0}", mangled_name);
  588. // Set up parameters and the return slot.
  589. for (auto [inst_id, arg] : llvm::zip_equal(function_type_info.param_inst_ids,
  590. llvm_function->args())) {
  591. auto name_id = SemIR::NameId::None;
  592. if (inst_id == function_type_info.return_param_id) {
  593. name_id = SemIR::NameId::ReturnSlot;
  594. arg.addAttr(llvm::Attribute::getWithStructRetType(
  595. llvm_context(), function_type_info.return_type));
  596. } else {
  597. name_id = SemIR::GetPrettyNameFromPatternId(sem_ir(), inst_id);
  598. }
  599. arg.setName(sem_ir().names().GetIRBaseName(name_id));
  600. }
  601. return llvm_function;
  602. }
  603. auto FileContext::BuildFunctionDefinition(SemIR::FunctionId function_id,
  604. SemIR::SpecificId specific_id)
  605. -> void {
  606. const auto& function = sem_ir().functions().Get(function_id);
  607. const auto& body_block_ids = function.body_block_ids;
  608. if (body_block_ids.empty() &&
  609. (!function.cpp_decl || !function.cpp_decl->isDefined())) {
  610. // Function is probably defined in another file; not an error.
  611. return;
  612. }
  613. llvm::Function* llvm_function;
  614. if (specific_id.has_value()) {
  615. llvm_function = specific_functions_[specific_id.index];
  616. } else {
  617. llvm_function = GetFunction(function_id);
  618. if (!llvm_function) {
  619. // We chose not to lower this function at all, for example because it's a
  620. // generic function.
  621. return;
  622. }
  623. }
  624. // For non-generics we do not lower. For generics, the llvm function was
  625. // created via GetOrCreateFunction prior to this when building the
  626. // declaration.
  627. BuildFunctionBody(function_id, function, llvm_function, specific_id);
  628. }
  629. auto FileContext::BuildFunctionBody(SemIR::FunctionId function_id,
  630. const SemIR::Function& function,
  631. llvm::Function* llvm_function,
  632. SemIR::SpecificId specific_id) -> void {
  633. const auto& body_block_ids = function.body_block_ids;
  634. CARBON_DCHECK(llvm_function, "LLVM Function not found when lowering body.");
  635. if (function.cpp_decl) {
  636. // TODO: To support recursive inline functions, collect all calls to
  637. // `HandleTopLevelDecl()` in a custom `ASTConsumer` configured in the
  638. // `ASTUnit`, and replay them in lowering in the `CodeGenerator`. See
  639. // https://discord.com/channels/655572317891461132/768530752592805919/1370509111585935443
  640. clang::FunctionDecl* cpp_def = function.cpp_decl->getDefinition();
  641. CARBON_DCHECK(cpp_def, "No Clang function body found during lowering");
  642. // Create the LLVM function (`CodeGenModule::GetOrCreateLLVMFunction()`) so
  643. // that code generation (`CodeGenModule::EmitGlobal()`) would see this
  644. // function name (`CodeGenModule::getMangledName()`), and will generate its
  645. // definition.
  646. llvm::Constant* function_address =
  647. cpp_code_generator_->GetAddrOfGlobal(clang::GlobalDecl(cpp_def),
  648. /*isForDefinition=*/false);
  649. CARBON_DCHECK(function_address);
  650. // Emit the function code.
  651. cpp_code_generator_->HandleTopLevelDecl(clang::DeclGroupRef(cpp_def));
  652. return;
  653. }
  654. CARBON_DCHECK(!body_block_ids.empty(),
  655. "No function body blocks found during lowering.");
  656. // Store which specifics were already lowered (with definitions) for each
  657. // generic.
  658. if (function.generic_id.has_value() && specific_id.has_value()) {
  659. AddLoweredSpecificForGeneric(function.generic_id, specific_id);
  660. }
  661. FunctionContext function_lowering(
  662. *this, llvm_function, specific_id,
  663. InitializeFingerprintForSpecific(specific_id),
  664. BuildDISubprogram(function, llvm_function), vlog_stream_);
  665. // Add parameters to locals.
  666. // TODO: This duplicates the mapping between sem_ir instructions and LLVM
  667. // function parameters that was already computed in BuildFunctionDecl.
  668. // We should only do that once.
  669. auto call_param_ids =
  670. sem_ir().inst_blocks().GetOrEmpty(function.call_params_id);
  671. int param_index = 0;
  672. // TODO: Find a way to ensure this code and the function-call lowering use
  673. // the same parameter ordering.
  674. // Lowers the given parameter. Must be called in LLVM calling convention
  675. // parameter order.
  676. auto lower_param = [&](SemIR::InstId param_id) {
  677. // Get the value of the parameter from the function argument.
  678. auto param_inst = sem_ir().insts().GetAs<SemIR::AnyParam>(param_id);
  679. llvm::Value* param_value;
  680. if (SemIR::ValueRepr::ForType(sem_ir(), param_inst.type_id).kind !=
  681. SemIR::ValueRepr::None) {
  682. param_value = llvm_function->getArg(param_index);
  683. ++param_index;
  684. } else {
  685. param_value = llvm::PoisonValue::get(GetType(
  686. SemIR::GetTypeOfInstInSpecific(sem_ir(), specific_id, param_id)));
  687. }
  688. // The value of the parameter is the value of the argument.
  689. function_lowering.SetLocal(param_id, param_value);
  690. };
  691. // The subset of call_param_ids that is already in the order that the LLVM
  692. // calling convention expects.
  693. llvm::ArrayRef<SemIR::InstId> sequential_param_ids;
  694. if (function.return_slot_pattern_id.has_value()) {
  695. // The LLVM calling convention has the return slot first rather than last.
  696. // Note that this queries whether there is a return slot at the LLVM level,
  697. // whereas `function.return_slot_pattern_id.has_value()` queries whether
  698. // there is a return slot at the SemIR level.
  699. if (SemIR::ReturnTypeInfo::ForFunction(sem_ir(), function, specific_id)
  700. .has_return_slot()) {
  701. lower_param(call_param_ids.back());
  702. }
  703. sequential_param_ids = call_param_ids.drop_back();
  704. } else {
  705. sequential_param_ids = call_param_ids;
  706. }
  707. for (auto param_id : sequential_param_ids) {
  708. lower_param(param_id);
  709. }
  710. auto decl_block_id = SemIR::InstBlockId::None;
  711. if (function_id == sem_ir().global_ctor_id()) {
  712. decl_block_id = SemIR::InstBlockId::Empty;
  713. } else {
  714. decl_block_id = sem_ir()
  715. .insts()
  716. .GetAs<SemIR::FunctionDecl>(function.latest_decl_id())
  717. .decl_block_id;
  718. }
  719. // Lowers the contents of block_id into the corresponding LLVM block,
  720. // creating it if it doesn't already exist.
  721. auto lower_block = [&](SemIR::InstBlockId block_id) {
  722. CARBON_VLOG("Lowering {0}\n", block_id);
  723. auto* llvm_block = function_lowering.GetBlock(block_id);
  724. // Keep the LLVM blocks in lexical order.
  725. llvm_block->moveBefore(llvm_function->end());
  726. function_lowering.builder().SetInsertPoint(llvm_block);
  727. function_lowering.LowerBlockContents(block_id);
  728. };
  729. lower_block(decl_block_id);
  730. // If the decl block is empty, reuse it as the first body block. We don't do
  731. // this when the decl block is non-empty so that any branches back to the
  732. // first body block don't also re-execute the decl.
  733. llvm::BasicBlock* block = function_lowering.builder().GetInsertBlock();
  734. if (block->empty() &&
  735. function_lowering.TryToReuseBlock(body_block_ids.front(), block)) {
  736. // Reuse this block as the first block of the function body.
  737. } else {
  738. function_lowering.builder().CreateBr(
  739. function_lowering.GetBlock(body_block_ids.front()));
  740. }
  741. // Lower all blocks.
  742. for (auto block_id : body_block_ids) {
  743. lower_block(block_id);
  744. }
  745. // LLVM requires that the entry block has no predecessors.
  746. auto* entry_block = &llvm_function->getEntryBlock();
  747. if (entry_block->hasNPredecessorsOrMore(1)) {
  748. auto* new_entry_block = llvm::BasicBlock::Create(
  749. llvm_context(), "entry", llvm_function, entry_block);
  750. llvm::BranchInst::Create(entry_block, new_entry_block);
  751. }
  752. // Emit fingerprint accumulated inside the function context.
  753. function_lowering.EmitFinalFingerprint();
  754. }
  755. auto FileContext::BuildDISubprogram(const SemIR::Function& function,
  756. const llvm::Function* llvm_function)
  757. -> llvm::DISubprogram* {
  758. if (!di_compile_unit_) {
  759. return nullptr;
  760. }
  761. auto name = sem_ir().names().GetAsStringIfIdentifier(function.name_id);
  762. CARBON_CHECK(name, "Unexpected special name for function: {0}",
  763. function.name_id);
  764. auto loc = GetLocForDI(function.definition_id);
  765. // TODO: Add more details here, including real subroutine type (once type
  766. // information is built), etc.
  767. return di_builder_.createFunction(
  768. di_compile_unit_, *name, llvm_function->getName(),
  769. /*File=*/di_builder_.createFile(loc.filename, ""),
  770. /*LineNo=*/loc.line_number,
  771. di_builder_.createSubroutineType(
  772. di_builder_.getOrCreateTypeArray(std::nullopt)),
  773. /*ScopeLine=*/0, llvm::DINode::FlagZero,
  774. llvm::DISubprogram::SPFlagDefinition);
  775. }
  776. // BuildTypeForInst is used to construct types for FileContext::BuildType below.
  777. // Implementations return the LLVM type for the instruction. This first overload
  778. // is the fallback handler for non-type instructions.
  779. template <typename InstT>
  780. requires(InstT::Kind.is_type() == SemIR::InstIsType::Never)
  781. static auto BuildTypeForInst(FileContext& /*context*/, InstT inst)
  782. -> llvm::Type* {
  783. CARBON_FATAL("Cannot use inst as type: {0}", inst);
  784. }
  785. template <typename InstT>
  786. requires(InstT::Kind.is_symbolic_when_type())
  787. static auto BuildTypeForInst(FileContext& context, InstT /*inst*/)
  788. -> llvm::Type* {
  789. // Treat non-monomorphized symbolic types as opaque.
  790. return llvm::StructType::get(context.llvm_context());
  791. }
  792. static auto BuildTypeForInst(FileContext& context, SemIR::ArrayType inst)
  793. -> llvm::Type* {
  794. return llvm::ArrayType::get(
  795. context.GetType(context.sem_ir().types().GetTypeIdForTypeInstId(
  796. inst.element_type_inst_id)),
  797. *context.sem_ir().GetArrayBoundValue(inst.bound_id));
  798. }
  799. static auto BuildTypeForInst(FileContext& /*context*/, SemIR::AutoType inst)
  800. -> llvm::Type* {
  801. CARBON_FATAL("Unexpected builtin type in lowering: {0}", inst);
  802. }
  803. static auto BuildTypeForInst(FileContext& context, SemIR::BoolType /*inst*/)
  804. -> llvm::Type* {
  805. // TODO: We may want to have different representations for `bool` storage
  806. // (`i8`) versus for `bool` values (`i1`).
  807. return llvm::Type::getInt1Ty(context.llvm_context());
  808. }
  809. static auto BuildTypeForInst(FileContext& context, SemIR::ClassType inst)
  810. -> llvm::Type* {
  811. auto object_repr_id = context.sem_ir()
  812. .classes()
  813. .Get(inst.class_id)
  814. .GetObjectRepr(context.sem_ir(), inst.specific_id);
  815. return context.GetType(object_repr_id);
  816. }
  817. static auto BuildTypeForInst(FileContext& context, SemIR::ConstType inst)
  818. -> llvm::Type* {
  819. return context.GetType(
  820. context.sem_ir().types().GetTypeIdForTypeInstId(inst.inner_id));
  821. }
  822. static auto BuildTypeForInst(FileContext& context,
  823. SemIR::ImplWitnessAssociatedConstant inst)
  824. -> llvm::Type* {
  825. return context.GetType(inst.type_id);
  826. }
  827. static auto BuildTypeForInst(FileContext& /*context*/,
  828. SemIR::ErrorInst /*inst*/) -> llvm::Type* {
  829. // This is a complete type but uses of it should never be lowered.
  830. return nullptr;
  831. }
  832. static auto BuildTypeForInst(FileContext& context, SemIR::FloatType /*inst*/)
  833. -> llvm::Type* {
  834. // TODO: Handle different sizes.
  835. return llvm::Type::getDoubleTy(context.llvm_context());
  836. }
  837. static auto BuildTypeForInst(FileContext& context, SemIR::IntType inst)
  838. -> llvm::Type* {
  839. auto width =
  840. context.sem_ir().insts().TryGetAs<SemIR::IntValue>(inst.bit_width_id);
  841. CARBON_CHECK(width, "Can't lower int type with symbolic width");
  842. return llvm::IntegerType::get(
  843. context.llvm_context(),
  844. context.sem_ir().ints().Get(width->int_id).getZExtValue());
  845. }
  846. static auto BuildTypeForInst(FileContext& context,
  847. SemIR::LegacyFloatType /*inst*/) -> llvm::Type* {
  848. return llvm::Type::getDoubleTy(context.llvm_context());
  849. }
  850. static auto BuildTypeForInst(FileContext& context, SemIR::PointerType /*inst*/)
  851. -> llvm::Type* {
  852. return llvm::PointerType::get(context.llvm_context(), /*AddressSpace=*/0);
  853. }
  854. static auto BuildTypeForInst(FileContext& /*context*/,
  855. SemIR::PatternType /*inst*/) -> llvm::Type* {
  856. CARBON_FATAL("Unexpected pattern type in lowering");
  857. }
  858. static auto BuildTypeForInst(FileContext& context, SemIR::StructType inst)
  859. -> llvm::Type* {
  860. auto fields = context.sem_ir().struct_type_fields().Get(inst.fields_id);
  861. llvm::SmallVector<llvm::Type*> subtypes;
  862. subtypes.reserve(fields.size());
  863. for (auto field : fields) {
  864. subtypes.push_back(context.GetType(
  865. context.sem_ir().types().GetTypeIdForTypeInstId(field.type_inst_id)));
  866. }
  867. return llvm::StructType::get(context.llvm_context(), subtypes);
  868. }
  869. static auto BuildTypeForInst(FileContext& context, SemIR::TupleType inst)
  870. -> llvm::Type* {
  871. // TODO: Investigate special-casing handling of empty tuples so that they
  872. // can be collectively replaced with LLVM's void, particularly around
  873. // function returns. LLVM doesn't allow declaring variables with a void
  874. // type, so that may require significant special casing.
  875. auto elements = context.sem_ir().inst_blocks().Get(inst.type_elements_id);
  876. llvm::SmallVector<llvm::Type*> subtypes;
  877. subtypes.reserve(elements.size());
  878. for (auto type_id : context.sem_ir().types().GetBlockAsTypeIds(elements)) {
  879. subtypes.push_back(context.GetType(type_id));
  880. }
  881. return llvm::StructType::get(context.llvm_context(), subtypes);
  882. }
  883. static auto BuildTypeForInst(FileContext& context, SemIR::TypeType /*inst*/)
  884. -> llvm::Type* {
  885. return context.GetTypeType();
  886. }
  887. static auto BuildTypeForInst(FileContext& context, SemIR::VtableType /*inst*/)
  888. -> llvm::Type* {
  889. return llvm::Type::getVoidTy(context.llvm_context());
  890. }
  891. template <typename InstT>
  892. requires(InstT::Kind.template IsAnyOf<SemIR::SpecificFunctionType,
  893. SemIR::StringType>())
  894. static auto BuildTypeForInst(FileContext& context, InstT /*inst*/)
  895. -> llvm::Type* {
  896. // TODO: Decide how we want to represent `StringType`.
  897. return llvm::PointerType::get(context.llvm_context(), 0);
  898. }
  899. template <typename InstT>
  900. requires(InstT::Kind
  901. .template IsAnyOf<SemIR::BoundMethodType, SemIR::IntLiteralType,
  902. SemIR::NamespaceType, SemIR::WitnessType>())
  903. static auto BuildTypeForInst(FileContext& context, InstT /*inst*/)
  904. -> llvm::Type* {
  905. // Return an empty struct as a placeholder.
  906. return llvm::StructType::get(context.llvm_context());
  907. }
  908. template <typename InstT>
  909. requires(InstT::Kind.template IsAnyOf<
  910. SemIR::AssociatedEntityType, SemIR::FacetType, SemIR::FunctionType,
  911. SemIR::FunctionTypeWithSelfType, SemIR::GenericClassType,
  912. SemIR::GenericInterfaceType, SemIR::InstType,
  913. SemIR::UnboundElementType, SemIR::WhereExpr>())
  914. static auto BuildTypeForInst(FileContext& context, InstT /*inst*/)
  915. -> llvm::Type* {
  916. // Return an empty struct as a placeholder.
  917. // TODO: Should we model an interface as a witness table, or an associated
  918. // entity as an index?
  919. return llvm::StructType::get(context.llvm_context());
  920. }
  921. auto FileContext::BuildType(SemIR::InstId inst_id) -> llvm::Type* {
  922. // Use overload resolution to select the implementation, producing compile
  923. // errors when BuildTypeForInst isn't defined for a given instruction.
  924. CARBON_KIND_SWITCH(sem_ir_->insts().Get(inst_id)) {
  925. #define CARBON_SEM_IR_INST_KIND(Name) \
  926. case CARBON_KIND(SemIR::Name inst): { \
  927. return BuildTypeForInst(*this, inst); \
  928. }
  929. #include "toolchain/sem_ir/inst_kind.def"
  930. }
  931. }
  932. auto FileContext::BuildGlobalVariableDecl(SemIR::VarStorage var_storage)
  933. -> llvm::GlobalVariable* {
  934. Mangler m(*this);
  935. auto mangled_name = m.MangleGlobalVariable(var_storage.pattern_id);
  936. auto linkage = llvm::GlobalVariable::ExternalLinkage;
  937. // If the variable doesn't have an externally-visible name, demote it to
  938. // internal linkage and invent a plausible name that shouldn't collide with
  939. // any of our real manglings.
  940. if (mangled_name.empty()) {
  941. linkage = llvm::GlobalVariable::InternalLinkage;
  942. if (inst_namer_) {
  943. mangled_name =
  944. ("var.anon" + inst_namer_->GetUnscopedNameFor(var_storage.pattern_id))
  945. .str();
  946. }
  947. }
  948. auto* type = GetType(var_storage.type_id);
  949. return new llvm::GlobalVariable(llvm_module(), type,
  950. /*isConstant=*/false, linkage,
  951. /*Initializer=*/nullptr, mangled_name);
  952. }
  953. auto FileContext::GetLocForDI(SemIR::InstId inst_id) -> LocForDI {
  954. SemIR::AbsoluteNodeId resolved =
  955. GetAbsoluteNodeId(sem_ir_, SemIR::LocId(inst_id)).back();
  956. const auto& tree_and_subtrees =
  957. (*tree_and_subtrees_getters_for_debug_info_)[resolved.check_ir_id()
  958. .index]();
  959. const auto& tokens = tree_and_subtrees.tree().tokens();
  960. if (resolved.node_id().has_value()) {
  961. auto token =
  962. tree_and_subtrees.GetSubtreeTokenRange(resolved.node_id()).begin;
  963. return {.filename = tokens.source().filename(),
  964. .line_number = tokens.GetLineNumber(token),
  965. .column_number = tokens.GetColumnNumber(token)};
  966. } else {
  967. return {.filename = tokens.source().filename(),
  968. .line_number = 0,
  969. .column_number = 0};
  970. }
  971. }
  972. auto FileContext::BuildVtable(const SemIR::Class& class_info)
  973. -> llvm::GlobalVariable* {
  974. // Bail out if this class is not dynamic (this will account for classes that
  975. // are declared-and-not-defined (including extern declarations) as well).
  976. if (!class_info.is_dynamic) {
  977. return nullptr;
  978. }
  979. // Vtables can't be generated for generics, only for their specifics - and
  980. // must be done lazily based on the use of those specifics.
  981. if (class_info.generic_id != SemIR::GenericId::None) {
  982. return nullptr;
  983. }
  984. Mangler m(*this);
  985. std::string mangled_name = m.MangleVTable(class_info);
  986. auto first_owning_decl_loc =
  987. sem_ir().insts().GetCanonicalLocId(class_info.first_owning_decl_id);
  988. if (first_owning_decl_loc.kind() == SemIR::LocId::Kind::ImportIRInstId) {
  989. // Emit a declaration of an imported vtable using a(n opaque) pointer type.
  990. // This doesn't have to match the definition that appears elsewhere, it'll
  991. // still get merged correctly.
  992. auto* gv = new llvm::GlobalVariable(
  993. llvm_module(),
  994. llvm::PointerType::get(llvm_context(), /*AddressSpace=*/0),
  995. /*isConstant=*/true, llvm::GlobalValue::ExternalLinkage, nullptr,
  996. mangled_name);
  997. gv->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
  998. return gv;
  999. }
  1000. auto canonical_vtable_id =
  1001. sem_ir().constant_values().GetConstantInstId(class_info.vtable_id);
  1002. auto vtable_inst_block =
  1003. sem_ir().inst_blocks().Get(sem_ir()
  1004. .insts()
  1005. .GetAs<SemIR::Vtable>(canonical_vtable_id)
  1006. .virtual_functions_id);
  1007. auto* entry_type = llvm::IntegerType::getInt32Ty(llvm_context());
  1008. auto* table_type = llvm::ArrayType::get(entry_type, vtable_inst_block.size());
  1009. auto* llvm_vtable = new llvm::GlobalVariable(
  1010. llvm_module(), table_type, /*isConstant=*/true,
  1011. llvm::GlobalValue::ExternalLinkage, nullptr, mangled_name);
  1012. auto* i32_type = llvm::IntegerType::getInt32Ty(llvm_context());
  1013. auto* i64_type = llvm::IntegerType::getInt64Ty(llvm_context());
  1014. auto* vtable_const_int =
  1015. llvm::ConstantExpr::getPtrToInt(llvm_vtable, i64_type);
  1016. llvm::SmallVector<llvm::Constant*> vfuncs;
  1017. vfuncs.reserve(vtable_inst_block.size());
  1018. for (auto fn_decl_id : vtable_inst_block) {
  1019. auto fn_decl = GetCalleeFunction(sem_ir(), fn_decl_id);
  1020. vfuncs.push_back(llvm::ConstantExpr::getTrunc(
  1021. llvm::ConstantExpr::getSub(
  1022. llvm::ConstantExpr::getPtrToInt(
  1023. GetOrCreateFunction(fn_decl.function_id,
  1024. SemIR::SpecificId::None),
  1025. i64_type),
  1026. vtable_const_int),
  1027. i32_type));
  1028. }
  1029. llvm_vtable->setInitializer(llvm::ConstantArray::get(table_type, vfuncs));
  1030. llvm_vtable->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
  1031. return llvm_vtable;
  1032. }
  1033. } // namespace Carbon::Lower