function_context.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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/function_context.h"
  5. #include "common/pretty_stack_trace_function.h"
  6. #include "common/vlog.h"
  7. #include "toolchain/base/kind_switch.h"
  8. #include "toolchain/sem_ir/diagnostic_loc_converter.h"
  9. #include "toolchain/sem_ir/expr_info.h"
  10. #include "toolchain/sem_ir/file.h"
  11. #include "toolchain/sem_ir/generic.h"
  12. namespace Carbon::Lower {
  13. FunctionContext::FunctionContext(
  14. FileContext& file_context, llvm::Function* function,
  15. FileContext& specific_file_context, SemIR::SpecificId specific_id,
  16. SpecificCoalescer::SpecificFunctionFingerprint* function_fingerprint,
  17. llvm::DISubprogram* di_subprogram, llvm::raw_ostream* vlog_stream)
  18. : file_context_(&file_context),
  19. function_(function),
  20. specific_file_context_(&specific_file_context),
  21. specific_id_(specific_id),
  22. builder_(file_context.llvm_context(), llvm::ConstantFolder(),
  23. Inserter(file_context.inst_namer())),
  24. di_subprogram_(di_subprogram),
  25. vlog_stream_(vlog_stream),
  26. function_fingerprint_(function_fingerprint) {
  27. function_->setSubprogram(di_subprogram_);
  28. }
  29. auto FunctionContext::GetBlock(SemIR::InstBlockId block_id)
  30. -> llvm::BasicBlock* {
  31. auto result = blocks_.Insert(block_id, [&] {
  32. llvm::StringRef label_name;
  33. if (const auto* inst_namer = file_context_->inst_namer()) {
  34. label_name = inst_namer->GetUnscopedLabelFor(block_id);
  35. }
  36. return llvm::BasicBlock::Create(llvm_context(), label_name, function_);
  37. });
  38. return result.value();
  39. }
  40. auto FunctionContext::TryToReuseBlock(SemIR::InstBlockId block_id,
  41. llvm::BasicBlock* block) -> bool {
  42. if (!blocks_.Insert(block_id, block).is_inserted()) {
  43. return false;
  44. }
  45. if (block == synthetic_block_) {
  46. synthetic_block_ = nullptr;
  47. }
  48. if (const auto* inst_namer = file_context_->inst_namer()) {
  49. block->setName(inst_namer->GetUnscopedLabelFor(block_id));
  50. }
  51. return true;
  52. }
  53. auto FunctionContext::LowerBlockContents(SemIR::InstBlockId block_id) -> void {
  54. auto inst_id_for_stack_trace = SemIR::InstId::None;
  55. // On crash, report the instruction we were lowering.
  56. PrettyStackTraceFunction stack_trace_entry([&](llvm::raw_ostream& output) {
  57. SemIR::DiagnosticLocConverter converter(
  58. &file_context_->context().tree_and_subtrees_getters(), &sem_ir());
  59. auto converted = converter.Convert(SemIR::LocId(inst_id_for_stack_trace),
  60. /*token_only=*/false);
  61. converted.loc.FormatLocation(output);
  62. // TODO: Format SemIR for the instruction we were lowering?
  63. output << "Lowering "
  64. << sem_ir().insts().Get(inst_id_for_stack_trace).kind().ir_name()
  65. << "\n";
  66. // Crash output has a tab indent; try to indent slightly past that.
  67. converted.loc.FormatSnippet(output, /*indent=*/10);
  68. });
  69. for (auto inst_id : sem_ir().inst_blocks().Get(block_id)) {
  70. inst_id_for_stack_trace = inst_id;
  71. LowerInst(inst_id);
  72. }
  73. }
  74. // Handles typed instructions for LowerInst. Many instructions lower using
  75. // HandleInst, but others are unsupported or have trivial lowering.
  76. //
  77. // This only calls HandleInst for versions that should have implementations. A
  78. // different approach would be to have the logic below implemented as HandleInst
  79. // overloads. However, forward declarations of HandleInst exist for all `InstT`
  80. // types, which would make getting the right overload resolution complex.
  81. template <typename InstT>
  82. static auto LowerInstHelper(FunctionContext& context, SemIR::InstId inst_id,
  83. InstT inst) -> void {
  84. if constexpr (!InstT::Kind.is_lowered()) {
  85. CARBON_FATAL(
  86. "Encountered an instruction that isn't expected to lower. It's "
  87. "possible that logic needs to be changed in order to stop showing this "
  88. "instruction in lowered contexts. Instruction: {0}",
  89. inst);
  90. } else if constexpr (InstT::Kind.constant_kind() ==
  91. SemIR::InstConstantKind::Always ||
  92. InstT::Kind.constant_kind() ==
  93. SemIR::InstConstantKind::AlwaysUnique) {
  94. CARBON_FATAL("Missing constant value for constant instruction {0}", inst);
  95. } else if constexpr (InstT::Kind.is_type() == SemIR::InstIsType::Always) {
  96. // For instructions that are always of type `type`, produce the trivial
  97. // runtime representation of type `type`.
  98. context.SetLocal(inst_id, context.GetTypeAsValue());
  99. } else {
  100. HandleInst(context, inst_id, inst);
  101. }
  102. }
  103. // TODO: Consider renaming Handle##Name, instead relying on typed_inst overload
  104. // resolution. That would allow putting the nonexistent handler implementations
  105. // in `requires`-style overloads.
  106. auto FunctionContext::LowerInst(SemIR::InstId inst_id) -> void {
  107. // Skip over constants. `FileContext::GetConstant` lowers them as needed.
  108. if (sem_ir().constant_values().Get(inst_id).is_constant()) {
  109. return;
  110. }
  111. auto inst = sem_ir().insts().Get(inst_id);
  112. CARBON_VLOG("Lowering {0}: {1}\n", inst_id, inst);
  113. builder_.getInserter().SetCurrentInstId(inst_id);
  114. auto debug_loc = GetDebugLoc(inst_id);
  115. if (debug_loc) {
  116. builder_.SetCurrentDebugLocation(debug_loc);
  117. }
  118. CARBON_KIND_SWITCH(inst) {
  119. #define CARBON_SEM_IR_INST_KIND(Name) \
  120. case CARBON_KIND(SemIR::Name typed_inst): { \
  121. LowerInstHelper(*this, inst_id, typed_inst); \
  122. break; \
  123. }
  124. #include "toolchain/sem_ir/inst_kind.def"
  125. }
  126. if (debug_loc) {
  127. builder_.SetCurrentDebugLocation(llvm::DebugLoc());
  128. }
  129. builder_.getInserter().SetCurrentInstId(SemIR::InstId::None);
  130. }
  131. auto FunctionContext::GetBlockArg(SemIR::InstBlockId block_id, TypeInFile type)
  132. -> llvm::PHINode* {
  133. llvm::BasicBlock* block = GetBlock(block_id);
  134. // Find the existing phi, if any.
  135. auto phis = block->phis();
  136. if (!phis.empty()) {
  137. CARBON_CHECK(std::next(phis.begin()) == phis.end(),
  138. "Expected at most one phi, found {0}",
  139. std::distance(phis.begin(), phis.end()));
  140. return &*phis.begin();
  141. }
  142. // The number of predecessor slots to reserve.
  143. static constexpr unsigned NumReservedPredecessors = 2;
  144. auto* phi = llvm::PHINode::Create(GetType(type), NumReservedPredecessors);
  145. phi->insertInto(block, block->begin());
  146. return phi;
  147. }
  148. auto FunctionContext::GetValue(SemIR::InstId inst_id) -> llvm::Value* {
  149. // All builtins are types, with the same empty lowered value.
  150. if (SemIR::IsSingletonInstId(inst_id)) {
  151. return GetTypeAsValue();
  152. }
  153. if (auto result = locals_.Lookup(inst_id)) {
  154. return result.value();
  155. }
  156. if (auto result = file_context_->global_variables().Lookup(inst_id)) {
  157. return result.value();
  158. }
  159. auto [const_ir, const_id] = GetConstantValueInSpecific(
  160. specific_sem_ir(), specific_id_, sem_ir(), inst_id);
  161. CARBON_CHECK(const_ir == &sem_ir() || const_ir == &specific_sem_ir());
  162. CARBON_CHECK(const_id.is_concrete(),
  163. "Missing value: {0} {1} in {2} has non-concrete value {3}",
  164. inst_id, sem_ir().insts().Get(inst_id), specific_id_, const_id);
  165. // We can only pass on the InstId if it refers to the file in which the
  166. // constant value was provided.
  167. auto* global = GetFileContext(const_ir).GetConstant(
  168. const_id, const_ir == &sem_ir() ? inst_id : SemIR::InstId::None);
  169. AddGlobalToCurrentFingerprint(global);
  170. return global;
  171. }
  172. auto FunctionContext::MakeSyntheticBlock() -> llvm::BasicBlock* {
  173. synthetic_block_ = llvm::BasicBlock::Create(llvm_context(), "", function_);
  174. return synthetic_block_;
  175. }
  176. auto FunctionContext::CreateAlloca(llvm::Type* type, const llvm::Twine& name)
  177. -> llvm::AllocaInst* {
  178. // Position the first alloca right before the start of the executable code in
  179. // the function.
  180. llvm::AllocaInst* alloca;
  181. {
  182. llvm::IRBuilderBase::InsertPointGuard guard(builder());
  183. auto debug_loc = builder().getCurrentDebugLocation();
  184. if (after_allocas_) {
  185. builder().SetInsertPoint(after_allocas_);
  186. } else {
  187. builder().SetInsertPointPastAllocas(&llvm_function());
  188. }
  189. // IRBuilder tramples over our debug location when setting the insert point,
  190. // so undo that.
  191. builder().SetCurrentDebugLocation(debug_loc);
  192. // Create an alloca for this variable in the entry block.
  193. // TODO: Compute alignment of the type, which may be greater than the
  194. // alignment computed by LLVM.
  195. alloca = builder().CreateAlloca(type, /*ArraySize=*/nullptr, name);
  196. }
  197. // Create a lifetime start intrinsic here to indicate where its scope really
  198. // begins.
  199. builder().CreateLifetimeStart(alloca);
  200. // If we just created the first alloca, there is now definitely at least one
  201. // instruction after it -- there is a lifetime start instruction if nothing
  202. // else. Use that instruction as our insert point for all future allocas.
  203. if (!after_allocas_) {
  204. auto loc = alloca->getIterator();
  205. ++loc;
  206. after_allocas_ = &*loc;
  207. }
  208. // TODO: Create a matching `@llvm.lifetime.end` intrinsic call when the
  209. // variable goes out of scope.
  210. return alloca;
  211. }
  212. auto FunctionContext::GetDebugLoc(SemIR::InstId inst_id) -> llvm::DebugLoc {
  213. if (!di_subprogram_) {
  214. return llvm::DebugLoc();
  215. }
  216. auto loc = file_context_->GetLocForDI(inst_id);
  217. if (loc.filename != di_subprogram_->getFile()->getFilename()) {
  218. // Location is from a different file. We can't represent that directly
  219. // within the scope of this function's subprogram, and we don't want to
  220. // generate a new subprogram, so just discard the location information. This
  221. // happens for thunks when emitting the portion of the thunk that is
  222. // duplicated from the original signature.
  223. //
  224. // TODO: Handle this case better.
  225. if (sem_ir().insts().Is<SemIR::Call>(inst_id)) {
  226. // Return a stub location for calls, because they may be inlineable (an
  227. // LLVM verifier issue).
  228. return llvm::DILocation::get(builder_.getContext(), -1, -1,
  229. di_subprogram_);
  230. }
  231. return llvm::DebugLoc();
  232. }
  233. return llvm::DILocation::get(builder_.getContext(), loc.line_number,
  234. loc.column_number, di_subprogram_);
  235. }
  236. auto FunctionContext::InitializeStorage(TypeInFile type, SemIR::InstId dest_id,
  237. SemIR::InstId source_id) -> void {
  238. switch (GetInitRepr(type).kind) {
  239. case SemIR::InitRepr::None:
  240. break;
  241. case SemIR::InitRepr::InPlace:
  242. if (sem_ir().constant_values().Get(source_id).is_constant()) {
  243. // When initializing from a constant, emission of the source doesn't
  244. // initialize the destination. Copy the constant value instead.
  245. // TODO: If the type is small, emit a store rather than a memcpy.
  246. CopyValue(type, source_id, dest_id);
  247. }
  248. break;
  249. case SemIR::InitRepr::ByCopy:
  250. CopyValue(type, source_id, dest_id);
  251. break;
  252. case SemIR::InitRepr::Abstract:
  253. CARBON_FATAL("Lowering aggregate initialization of abstract type {0}",
  254. type.file->types().GetAsInst(type.type_id));
  255. case SemIR::InitRepr::Incomplete:
  256. CARBON_FATAL("Lowering aggregate initialization of incomplete type {0}",
  257. type.file->types().GetAsInst(type.type_id));
  258. case SemIR::InitRepr::Dependent:
  259. CARBON_FATAL("Lowering aggregate initialization of dependent type {0}",
  260. type.file->types().GetAsInst(type.type_id));
  261. }
  262. }
  263. auto FunctionContext::InitializeStorage(SemIR::InstId init_id) -> void {
  264. InitializeStorage(GetTypeIdOfInst(init_id),
  265. SemIR::FindStorageArgForInitializer(sem_ir(), init_id),
  266. init_id);
  267. }
  268. auto FunctionContext::GetTypeIdOfInst(SemIR::InstId inst_id) -> TypeInFile {
  269. auto [file, type_id] = SemIR::GetTypeOfInstInSpecific(
  270. specific_sem_ir(), specific_id(), sem_ir(), inst_id);
  271. return {.file = file, .type_id = type_id};
  272. }
  273. auto FunctionContext::GetValueRepr(TypeInFile type) -> ValueReprInFile {
  274. ValueReprInFile result = {
  275. .file = type.file,
  276. .repr = SemIR::ValueRepr::ForType(*type.file, type.type_id)};
  277. AddEnumToCurrentFingerprint(result.repr.kind);
  278. AddEnumToCurrentFingerprint(result.repr.aggregate_kind);
  279. return result;
  280. }
  281. auto FunctionContext::GetInitRepr(TypeInFile type) -> SemIR::InitRepr {
  282. auto result = SemIR::InitRepr::ForType(*type.file, type.type_id);
  283. AddEnumToCurrentFingerprint(result.kind);
  284. return result;
  285. }
  286. // Given a type used for an LLVM value, return the type that we use to store
  287. // that value in memory. This is the same type unless the type is a
  288. // non-multiple-of-8 integer type, which we explicitly widen to a multiple of 8
  289. // for Clang compatibility and to make our generated IR easier for LLVM to
  290. // handle.
  291. static auto GetWidenedMemoryType(llvm::Type* type) -> llvm::Type* {
  292. if (auto* int_type = dyn_cast<llvm::IntegerType>(type)) {
  293. auto width = llvm::alignToPowerOf2(int_type->getBitWidth(), 8);
  294. if (width != int_type->getBitWidth()) {
  295. return llvm::IntegerType::get(type->getContext(), width);
  296. }
  297. }
  298. return type;
  299. }
  300. auto FunctionContext::LoadObject(TypeInFile type, llvm::Value* addr,
  301. llvm::Twine name) -> llvm::Value* {
  302. auto* llvm_type = GetType(type);
  303. auto* load_type = GetWidenedMemoryType(llvm_type);
  304. // TODO: Include alias and alignment information.
  305. llvm::Value* value = builder().CreateLoad(load_type, addr, name);
  306. if (load_type != llvm_type) {
  307. value = builder().CreateTrunc(value, llvm_type);
  308. }
  309. return value;
  310. }
  311. auto FunctionContext::StoreObject(TypeInFile type, llvm::Value* value,
  312. llvm::Value* addr) -> void {
  313. // TODO: Include alias and alignment information.
  314. auto* llvm_type = GetType(type);
  315. CARBON_CHECK(value->getType() == llvm_type);
  316. // Don't emit a store of `iN` if N is not a multiple of 8. See `LoadObject`.
  317. auto* store_type = GetWidenedMemoryType(llvm_type);
  318. if (store_type != llvm_type) {
  319. // TODO: Should we consider creating a sext if the value is signed?
  320. value = builder().CreateZExt(value, store_type);
  321. }
  322. builder().CreateStore(value, addr);
  323. }
  324. auto FunctionContext::CopyValue(TypeInFile type, SemIR::InstId source_id,
  325. SemIR::InstId dest_id) -> void {
  326. switch (GetValueRepr(type).repr.kind) {
  327. case SemIR::ValueRepr::Unknown:
  328. CARBON_FATAL("Attempt to copy incomplete type");
  329. case SemIR::ValueRepr::Dependent:
  330. CARBON_FATAL("Attempt to copy dependent type");
  331. case SemIR::ValueRepr::None:
  332. break;
  333. case SemIR::ValueRepr::Copy:
  334. StoreObject(type, GetValue(source_id), GetValue(dest_id));
  335. break;
  336. case SemIR::ValueRepr::Pointer:
  337. CopyObject(type, source_id, dest_id);
  338. break;
  339. case SemIR::ValueRepr::Custom:
  340. CARBON_FATAL("TODO: Add support for CopyValue with custom value rep");
  341. }
  342. }
  343. auto FunctionContext::CopyObject(TypeInFile type, SemIR::InstId source_id,
  344. SemIR::InstId dest_id) -> void {
  345. const auto& layout = llvm_module().getDataLayout();
  346. auto* llvm_type = GetType(type);
  347. // TODO: Compute known alignment of the source and destination, which may
  348. // be greater than the alignment computed by LLVM.
  349. auto align = layout.getABITypeAlign(llvm_type);
  350. // TODO: Attach !tbaa.struct metadata indicating which portions of the
  351. // type we actually need to copy and which are padding.
  352. builder().CreateMemCpy(GetValue(dest_id), align, GetValue(source_id), align,
  353. layout.getTypeAllocSize(llvm_type));
  354. }
  355. auto FunctionContext::Inserter::InsertHelper(
  356. llvm::Instruction* inst, const llvm::Twine& name,
  357. llvm::BasicBlock::iterator insert_pt) const -> void {
  358. llvm::StringRef base_name;
  359. llvm::StringRef separator;
  360. if (inst_namer_ && !inst->getType()->isVoidTy()) {
  361. base_name = inst_namer_->GetUnscopedNameFor(inst_id_);
  362. }
  363. if (!base_name.empty() && !name.isTriviallyEmpty()) {
  364. separator = ".";
  365. }
  366. IRBuilderDefaultInserter::InsertHelper(inst, base_name + separator + name,
  367. insert_pt);
  368. }
  369. auto FunctionContext::AddCallToCurrentFingerprint(SemIR::CheckIRId file_id,
  370. SemIR::FunctionId function_id,
  371. SemIR::SpecificId specific_id)
  372. -> void {
  373. if (!function_fingerprint_) {
  374. return;
  375. }
  376. RawStringOstream os;
  377. // TODO: Replace indexes with info that is translation unit independent.
  378. // Using a string that includes the `FunctionId` string and the index to
  379. // avoid possible collisions. This needs revisiting.
  380. os << "file_id" << file_id.index << "\n";
  381. os << "function_id" << function_id.index << "\n";
  382. current_fingerprint_.common_fingerprint.update(os.TakeStr());
  383. // TODO: Replace index with info that is translation unit independent.
  384. if (specific_id.has_value()) {
  385. current_fingerprint_.specific_fingerprint.update(specific_id.index);
  386. // TODO: Uses -1 as delimiter. This needs revisiting.
  387. current_fingerprint_.specific_fingerprint.update(-1);
  388. function_fingerprint_->calls.push_back(specific_id);
  389. }
  390. }
  391. auto FunctionContext::AddIntToCurrentFingerprint(uint64_t value) -> void {
  392. if (!function_fingerprint_) {
  393. return;
  394. }
  395. // TODO: Instead just include the raw bytes of the integer?
  396. RawStringOstream os;
  397. os << value << "\n";
  398. current_fingerprint_.common_fingerprint.update(os.TakeStr());
  399. }
  400. auto FunctionContext::AddTypeToCurrentFingerprint(llvm::Type* type) -> void {
  401. if (!function_fingerprint_ || !type) {
  402. return;
  403. }
  404. RawStringOstream os;
  405. type->print(os);
  406. os << "\n";
  407. current_fingerprint_.common_fingerprint.update(os.TakeStr());
  408. }
  409. auto FunctionContext::AddGlobalToCurrentFingerprint(llvm::Value* global)
  410. -> void {
  411. if (!function_fingerprint_ || !global) {
  412. return;
  413. }
  414. RawStringOstream os;
  415. global->print(os);
  416. os << "\n";
  417. current_fingerprint_.common_fingerprint.update(os.TakeStr());
  418. }
  419. auto FunctionContext::EmitFinalFingerprint() -> void {
  420. if (!function_fingerprint_) {
  421. return;
  422. }
  423. current_fingerprint_.common_fingerprint.final(
  424. function_fingerprint_->common_fingerprint);
  425. current_fingerprint_.specific_fingerprint.final(
  426. function_fingerprint_->specific_fingerprint);
  427. }
  428. } // namespace Carbon::Lower