function_context.cpp 18 KB

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