file_context.cpp 49 KB

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