function_context.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. #ifndef CARBON_TOOLCHAIN_LOWER_FUNCTION_CONTEXT_H_
  5. #define CARBON_TOOLCHAIN_LOWER_FUNCTION_CONTEXT_H_
  6. #include <type_traits>
  7. #include "common/map.h"
  8. #include "common/raw_string_ostream.h"
  9. #include "llvm/IR/IRBuilder.h"
  10. #include "llvm/IR/LLVMContext.h"
  11. #include "llvm/IR/Module.h"
  12. #include "toolchain/lower/file_context.h"
  13. #include "toolchain/lower/specific_coalescer.h"
  14. #include "toolchain/sem_ir/file.h"
  15. #include "toolchain/sem_ir/ids.h"
  16. namespace Carbon::Lower {
  17. // Context and shared functionality for lowering handlers that produce an
  18. // `llvm::Function` definition.
  19. class FunctionContext {
  20. public:
  21. // `function` must not be null. `function_fingerprint` and `di_subprogram` may
  22. // be null (see members).
  23. explicit FunctionContext(
  24. FileContext& file_context, llvm::Function* function,
  25. FileContext& specific_file_context, SemIR::SpecificId specific_id,
  26. SpecificCoalescer::SpecificFunctionFingerprint* function_fingerprint,
  27. llvm::DISubprogram* di_subprogram, llvm::raw_ostream* vlog_stream);
  28. // Describes a function's body fingerprint while creating the function body.
  29. // The final fingerprint is stored in the `FileContext` as a
  30. // `SpecificFunctionFingerprint`.
  31. //
  32. // Create two function fingerprints, where both fingerprints include data
  33. // that's evaluated (and hence lowered) differently based on the
  34. // `SpecificId`. `common_fingerprint` includes global values, types
  35. // and `FunctionId` for functions called inside the function body.
  36. // `specific_fingerprint` includes `SpecificId`s for functions called.
  37. //
  38. // For two specifics of the same generic:
  39. // - If `common_fingerprint` is different, the specifics cannot be coalesced.
  40. // - If `common_fingerprint` and `specific_fingerprint` are the
  41. // same, the specifics can be coalesced without additional checks.
  42. // - If `common_fingerprint` is the same but `specific_fingerprint` is
  43. // different, additional checks are needed, i.e. inspecting the non-hashed
  44. // `SpecificId`s.
  45. //
  46. // TODO: Consider optimizations for repeated entries in both fingerprints.
  47. struct LoweringFunctionFingerprint {
  48. llvm::BLAKE3 common_fingerprint;
  49. llvm::BLAKE3 specific_fingerprint;
  50. };
  51. // A type in a particular file. This is used when lowering a specific and we
  52. // find a type that might be from the specific rather than the generic. By
  53. // convention, this represents a type that has not yet been added to the
  54. // specific fingerprint.
  55. struct TypeInFile {
  56. const SemIR::File* file;
  57. SemIR::TypeId type_id;
  58. auto GetPointeeType() -> TypeInFile {
  59. return {.file = file, .type_id = file->GetPointeeType(type_id)};
  60. }
  61. };
  62. // A value representation in a particular file. By convention, this represents
  63. // a value representation whose kind has been added to the fingerprint but
  64. // whose type has not.
  65. struct ValueReprInFile {
  66. const SemIR::File* file;
  67. SemIR::ValueRepr repr;
  68. auto type() -> TypeInFile {
  69. return {.file = file, .type_id = repr.type_id};
  70. }
  71. };
  72. // An inst in a particular file.
  73. struct InstInFile {
  74. const SemIR::File* file;
  75. SemIR::InstId inst_id;
  76. };
  77. // Information about a function's return type in a particular file. By
  78. // convention, this represents a value whose initializing representation has
  79. // been added to the fingerprint but whose type has not.
  80. struct ReturnTypeInfoInFile {
  81. const SemIR::File* file;
  82. SemIR::ReturnTypeInfo info;
  83. auto type() -> TypeInFile {
  84. return {.file = file, .type_id = info.type_id};
  85. }
  86. };
  87. // Returns a basic block corresponding to the start of the given semantics
  88. // block, and enqueues it for emission.
  89. auto GetBlock(SemIR::InstBlockId block_id) -> llvm::BasicBlock*;
  90. // If we have not yet allocated a `BasicBlock` for this `block_id`, set it to
  91. // `block`, and enqueue `block_id` for emission. Returns whether we set the
  92. // block.
  93. auto TryToReuseBlock(SemIR::InstBlockId block_id, llvm::BasicBlock* block)
  94. -> bool;
  95. // Builds LLVM IR for the sequence of instructions in `block_id`.
  96. auto LowerBlockContents(SemIR::InstBlockId block_id) -> void;
  97. // Builds LLVM IR for the specified instruction.
  98. auto LowerInst(SemIR::InstId inst_id) -> void;
  99. // Returns a phi node corresponding to the block argument of the given basic
  100. // block.
  101. auto GetBlockArg(SemIR::InstBlockId block_id, TypeInFile type)
  102. -> llvm::PHINode*;
  103. // Returns a value for the given instruction.
  104. auto GetValue(SemIR::InstId inst_id) -> llvm::Value*;
  105. // Sets the value for the given instruction.
  106. auto SetLocal(SemIR::InstId inst_id, llvm::Value* value) -> void {
  107. bool added = locals_.Insert(inst_id, value).is_inserted();
  108. CARBON_CHECK(added, "Duplicate local insert: {0} {1}", inst_id,
  109. sem_ir().insts().Get(inst_id));
  110. }
  111. // Gets a callable's function.
  112. auto GetFunction(SemIR::FunctionId function_id) -> llvm::Function* {
  113. return file_context_->GetFunction(function_id);
  114. }
  115. // Gets or creates a callable's function.
  116. auto GetOrCreateFunction(SemIR::FunctionId function_id,
  117. SemIR::SpecificId specific_id) -> llvm::Function* {
  118. return file_context_->GetOrCreateFunction(function_id, specific_id);
  119. }
  120. // Builds LLVM function type information for the specified function.
  121. auto BuildFunctionTypeInfo(const SemIR::Function& function,
  122. SemIR::SpecificId specific_id)
  123. -> FileContext::FunctionTypeInfo {
  124. return file_context_->BuildFunctionTypeInfo(function, specific_id);
  125. }
  126. // Returns a lowered type for the given type_id in the given file. This adds
  127. // the specified type to the fingerprint.
  128. auto GetType(TypeInFile type) -> llvm::Type* {
  129. auto* llvm_type = GetFileContext(type.file).GetType(type.type_id);
  130. AddTypeToCurrentFingerprint(llvm_type);
  131. return llvm_type;
  132. }
  133. // Returns the type of the given instruction in the current specific.
  134. auto GetTypeOfInst(SemIR::InstId inst_id) -> llvm::Type* {
  135. return GetType(GetTypeIdOfInst(inst_id));
  136. }
  137. // Returns the type of the given instruction in the current specific.
  138. auto GetTypeIdOfInst(SemIR::InstId inst_id) -> TypeInFile;
  139. // Returns the value representation of the given type. This adds the kind of
  140. // value representation, but not the underlying type, to the fingerprint.
  141. auto GetValueRepr(TypeInFile type) -> ValueReprInFile;
  142. // Returns the initializing representation of the given type. This adds the
  143. // kind of initializing representation to the fingerprint.
  144. auto GetInitRepr(TypeInFile type) -> SemIR::InitRepr;
  145. // Returns the return type information for the given callee inst. This adds
  146. // the kind of initializing representation to the fingerprint.
  147. auto GetReturnTypeInfo(InstInFile callee) -> ReturnTypeInfoInFile;
  148. // Returns a lowered value to use for a value of type `type`.
  149. auto GetTypeAsValue() -> llvm::Value* {
  150. return file_context_->GetTypeAsValue();
  151. }
  152. // Returns a lowered value to use for a value of literal type.
  153. auto GetLiteralAsValue() -> llvm::Constant* {
  154. return file_context_->GetLiteralAsValue();
  155. }
  156. // Create a synthetic block that corresponds to no SemIR::InstBlockId. Such
  157. // a block should only ever have a single predecessor, and is used when we
  158. // need multiple `llvm::BasicBlock`s to model the linear control flow in a
  159. // single SemIR::File block.
  160. auto MakeSyntheticBlock() -> llvm::BasicBlock*;
  161. // Determine whether block is the most recently created synthetic block.
  162. auto IsCurrentSyntheticBlock(llvm::BasicBlock* block) -> bool {
  163. return synthetic_block_ == block;
  164. }
  165. // Creates an alloca instruction of the given type, adds it to the entry
  166. // block, and starts the lifetime of the corresponding storage.
  167. auto CreateAlloca(llvm::Type* type, const llvm::Twine& name = llvm::Twine())
  168. -> llvm::AllocaInst*;
  169. // Returns the debug location to associate with the specified instruction.
  170. auto GetDebugLoc(SemIR::InstId inst_id) -> llvm::DebugLoc;
  171. // Emits a load of an object representation of type `type`.
  172. auto LoadObject(TypeInFile type, llvm::Value* addr, llvm::Twine name = "")
  173. -> llvm::Value*;
  174. // Emits a store of an object representation of type `type`.
  175. auto StoreObject(TypeInFile type, llvm::Value* value, llvm::Value* addr)
  176. -> void;
  177. // After emitting an initializer `init_id`, finishes performing the
  178. // initialization of `dest_id` from that initializer. This is a no-op if the
  179. // initialization was performed in-place, and otherwise performs a store or a
  180. // copy.
  181. auto FinishInit(TypeInFile type, SemIR::InstId dest_id,
  182. SemIR::InstId source_id) -> void;
  183. // When fingerprinting for a specific, adds the call, found in the function
  184. // body, to <function_id, specific_id>. `function_id` and `specific_id` are
  185. // IDs within the file identified by `function_file_id`.
  186. auto AddCallToCurrentFingerprint(SemIR::CheckIRId file_id,
  187. SemIR::FunctionId function_id,
  188. SemIR::SpecificId specific_id) -> void;
  189. // When fingerprinting for a specific, adds an integer.
  190. auto AddIntToCurrentFingerprint(uint64_t value) -> void;
  191. // When fingerprinting for a specific, adds an enumerator value.
  192. template <typename T>
  193. requires(std::is_enum_v<T>)
  194. auto AddEnumToCurrentFingerprint(T value) -> void {
  195. AddIntToCurrentFingerprint(static_cast<uint64_t>(value));
  196. }
  197. // When fingerprinting for a specific, adds the type.
  198. auto AddTypeToCurrentFingerprint(llvm::Type* type) -> void;
  199. // Emits the final function fingerprints. Only called when function lowering
  200. // is complete.
  201. auto EmitFinalFingerprint() -> void;
  202. // Returns the FileContext to use for lowering in the given file.
  203. auto GetFileContext(const SemIR::File* file) -> FileContext& {
  204. // Avoid hash table lookup for the expected files.
  205. if (file == &sem_ir()) {
  206. return *file_context_;
  207. }
  208. if (file == &specific_sem_ir()) {
  209. return *specific_file_context_;
  210. }
  211. return file_context_->context().GetFileContext(file);
  212. }
  213. auto llvm_context() -> llvm::LLVMContext& {
  214. return file_context_->llvm_context();
  215. }
  216. auto llvm_module() -> llvm::Module& { return file_context_->llvm_module(); }
  217. auto llvm_function() -> llvm::Function& { return *function_; }
  218. auto builder() -> llvm::IRBuilderBase& { return builder_; }
  219. auto sem_ir() -> const SemIR::File& { return file_context_->sem_ir(); }
  220. // The file context for the file that `specific_id()` is within.
  221. auto specific_file_context() -> FileContext& {
  222. return *specific_file_context_;
  223. }
  224. // The file that `specific_id()` is within.
  225. auto specific_sem_ir() -> const SemIR::File& {
  226. return specific_file_context_->sem_ir();
  227. }
  228. // The specific ID for the function that is being lowered. Note that this is
  229. // an ID from `specific_sem_ir()`, not from `sem_ir()`.
  230. auto specific_id() -> SemIR::SpecificId { return specific_id_; }
  231. // TODO: could template on BuiltinFunctionKind if more format
  232. // globals are eventually needed.
  233. auto printf_int_format_string() -> llvm::Value* {
  234. auto* format_string = file_context_->printf_int_format_string();
  235. if (!format_string) {
  236. format_string = builder().CreateGlobalString("%d\n", "printf.int.format");
  237. file_context_->SetPrintfIntFormatString(format_string);
  238. }
  239. return format_string;
  240. }
  241. auto GetVtable(SemIR::VtableId vtable_id, SemIR::SpecificId specific_id) const
  242. -> llvm::GlobalVariable* {
  243. return file_context_->GetVtable(vtable_id, specific_id);
  244. }
  245. private:
  246. // Custom instruction inserter for our IR builder. Automatically names
  247. // instructions.
  248. class Inserter : public llvm::IRBuilderDefaultInserter {
  249. public:
  250. explicit Inserter(const SemIR::InstNamer* inst_namer)
  251. : inst_namer_(inst_namer) {}
  252. // Sets the instruction we are currently emitting.
  253. auto SetCurrentInstId(SemIR::InstId inst_id) -> void { inst_id_ = inst_id; }
  254. private:
  255. auto InsertHelper(llvm::Instruction* inst, const llvm::Twine& name,
  256. llvm::BasicBlock::iterator insert_pt) const
  257. -> void override;
  258. // The instruction namer.
  259. const SemIR::InstNamer* inst_namer_;
  260. // The current instruction ID.
  261. SemIR::InstId inst_id_ = SemIR::InstId::None;
  262. };
  263. // Emits a value copy for type `type` from `source_id` to `dest_id`.
  264. // `source_id` must produce a value representation for `type`, and
  265. // `dest_id` must be a pointer to a `type` object.
  266. auto CopyValue(TypeInFile type, SemIR::InstId source_id,
  267. SemIR::InstId dest_id) -> void;
  268. // Emits an object representation copy for type `type` from `source_id` to
  269. // `dest_id`. `source_id` and `dest_id` must produce pointers to `type`
  270. // objects.
  271. auto CopyObject(TypeInFile type, SemIR::InstId source_id,
  272. SemIR::InstId dest_id) -> void;
  273. // When fingerprinting for a specific, adds the global.
  274. auto AddGlobalToCurrentFingerprint(llvm::Value* global) -> void;
  275. // Context for lowering in the file that contains this function's
  276. // instructions.
  277. FileContext* file_context_;
  278. // The IR function we're generating.
  279. llvm::Function* function_;
  280. // Context for lowering in the file that contains our `specific_id_`. Note
  281. // that this is a different file than the one referred to by `file_context_`
  282. // if we are lowering a specific that was generated for a generic function
  283. // defined in a different file.
  284. FileContext* specific_file_context_;
  285. // The specific id, if the function is a specific.
  286. SemIR::SpecificId specific_id_;
  287. // Builder for creating code in this function. The insertion point is held at
  288. // the location of the current SemIR instruction.
  289. llvm::IRBuilder<llvm::ConstantFolder, Inserter> builder_;
  290. // The instruction after all allocas. This is used as the insert point for new
  291. // allocas.
  292. llvm::Instruction* after_allocas_ = nullptr;
  293. llvm::DISubprogram* di_subprogram_;
  294. // The optional vlog stream.
  295. llvm::raw_ostream* vlog_stream_;
  296. // This is initialized and populated while lowering a specific function.
  297. // When complete, this is used to complete the function_fingerprint_.
  298. LoweringFunctionFingerprint current_fingerprint_;
  299. // The accumulated fingerprint is owned by the FileContext and passed into
  300. // the FunctionContext. The function fingerprint is currently only built for
  301. // specific functions, otherwise, this will be nullptr.
  302. SpecificCoalescer::SpecificFunctionFingerprint* function_fingerprint_;
  303. // Maps a function's SemIR::File blocks to lowered blocks.
  304. Map<SemIR::InstBlockId, llvm::BasicBlock*> blocks_;
  305. // The synthetic block we most recently created. May be null if there is no
  306. // such block.
  307. llvm::BasicBlock* synthetic_block_ = nullptr;
  308. // Maps a function's SemIR::File instructions to lowered values.
  309. Map<SemIR::InstId, llvm::Value*> locals_;
  310. };
  311. // Provides handlers for instructions that occur in a FunctionContext. Although
  312. // this is declared for all instructions, it should only be defined for
  313. // instructions which are non-constant and not always typed. See
  314. // `FunctionContext::LowerInst` for how this is used.
  315. #define CARBON_SEM_IR_INST_KIND(Name) \
  316. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id, \
  317. SemIR::Name inst) -> void;
  318. #include "toolchain/sem_ir/inst_kind.def"
  319. } // namespace Carbon::Lower
  320. #endif // CARBON_TOOLCHAIN_LOWER_FUNCTION_CONTEXT_H_