file_context.cpp 47 KB

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