function_context.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 "common/map.h"
  7. #include "common/raw_string_ostream.h"
  8. #include "llvm/IR/IRBuilder.h"
  9. #include "llvm/IR/LLVMContext.h"
  10. #include "llvm/IR/Module.h"
  11. #include "toolchain/lower/file_context.h"
  12. #include "toolchain/sem_ir/file.h"
  13. namespace Carbon::Lower {
  14. // Context and shared functionality for lowering handlers that produce an
  15. // `llvm::Function` definition.
  16. class FunctionContext {
  17. public:
  18. // `function` must not be null. `function_fingerprint` and `di_subprogram` may
  19. // be null (see members).
  20. explicit FunctionContext(
  21. FileContext& file_context, llvm::Function* function,
  22. SemIR::SpecificId specific_id,
  23. FileContext::SpecificFunctionFingerprint* function_fingerprint,
  24. llvm::DISubprogram* di_subprogram, llvm::raw_ostream* vlog_stream);
  25. // Describes a function's body fingerprint while creating the function body.
  26. // The final fingerprint is stored in the `FileContext` as a
  27. // `SpecificFunctionFingerprint`.
  28. //
  29. // Create two function fingerprints, where both fingerprints include data
  30. // that's evaluated (and hence lowered) differently based on the
  31. // `SpecificId`. `common_fingerprint` includes global values, types
  32. // and `FunctionId` for functions called inside the function body.
  33. // `specific_fingerprint` includes `SpecificId`s for functions called.
  34. //
  35. // For two specifics of the same generic:
  36. // - If `common_fingerprint` is different, the specifics cannot be coalesced.
  37. // - If `common_fingerprint` and `specific_fingerprint` are the
  38. // same, the specifics can be coalesced without additional checks.
  39. // - If `common_fingerprint` is the same but `specific_fingerprint` is
  40. // different, additional checks are needed, i.e. inspecting the non-hashed
  41. // `SpecificId`s.
  42. //
  43. // TODO: Consider optimizations for repeated entries in both fingerprints.
  44. struct LoweringFunctionFingerprint {
  45. llvm::BLAKE3 common_fingerprint;
  46. llvm::BLAKE3 specific_fingerprint;
  47. };
  48. // Returns a basic block corresponding to the start of the given semantics
  49. // block, and enqueues it for emission.
  50. auto GetBlock(SemIR::InstBlockId block_id) -> llvm::BasicBlock*;
  51. // If we have not yet allocated a `BasicBlock` for this `block_id`, set it to
  52. // `block`, and enqueue `block_id` for emission. Returns whether we set the
  53. // block.
  54. auto TryToReuseBlock(SemIR::InstBlockId block_id, llvm::BasicBlock* block)
  55. -> bool;
  56. // Builds LLVM IR for the sequence of instructions in `block_id`.
  57. auto LowerBlockContents(SemIR::InstBlockId block_id) -> void;
  58. // Builds LLVM IR for the specified instruction.
  59. auto LowerInst(SemIR::InstId inst_id) -> void;
  60. // Returns a phi node corresponding to the block argument of the given basic
  61. // block.
  62. auto GetBlockArg(SemIR::InstBlockId block_id, SemIR::TypeId type_id)
  63. -> llvm::PHINode*;
  64. // Returns a value for the given instruction.
  65. auto GetValue(SemIR::InstId inst_id) -> llvm::Value*;
  66. // Sets the value for the given instruction.
  67. auto SetLocal(SemIR::InstId inst_id, llvm::Value* value) -> void {
  68. bool added = locals_.Insert(inst_id, value).is_inserted();
  69. CARBON_CHECK(added, "Duplicate local insert: {0} {1}", inst_id,
  70. sem_ir().insts().Get(inst_id));
  71. }
  72. // Gets a callable's function.
  73. auto GetFunction(SemIR::FunctionId function_id) -> llvm::Function* {
  74. return file_context_->GetFunction(function_id);
  75. }
  76. // Gets or creates a callable's function.
  77. auto GetOrCreateFunction(SemIR::FunctionId function_id,
  78. SemIR::SpecificId specific_id) -> llvm::Function* {
  79. return file_context_->GetOrCreateFunction(function_id, specific_id);
  80. }
  81. // Builds LLVM function type information for the specified function.
  82. auto BuildFunctionTypeInfo(const SemIR::Function& function,
  83. SemIR::SpecificId specific_id)
  84. -> FileContext::FunctionTypeInfo {
  85. return file_context_->BuildFunctionTypeInfo(function, specific_id);
  86. }
  87. // Returns a lowered type for the given type_id.
  88. auto GetType(SemIR::TypeId type_id) -> llvm::Type* {
  89. return file_context_->GetType(type_id);
  90. }
  91. // Returns the type of the given instruction in the current specific.
  92. auto GetTypeOfInst(SemIR::InstId inst_id) -> SemIR::TypeId;
  93. // Returns a lowered value to use for a value of type `type`.
  94. auto GetTypeAsValue() -> llvm::Value* {
  95. return file_context_->GetTypeAsValue();
  96. }
  97. // Returns a lowered value to use for a value of int literal type.
  98. auto GetIntLiteralAsValue() -> llvm::Constant* {
  99. return file_context_->GetIntLiteralAsValue();
  100. }
  101. // Returns the instruction immediately after all the existing static allocas.
  102. // This is the insert point for future static allocas.
  103. auto GetInstructionAfterAllocas() const -> llvm::Instruction* {
  104. return after_allocas_;
  105. }
  106. // Sets the instruction after static allocas. This should be called once,
  107. // after the first alloca is created.
  108. auto SetInstructionAfterAllocas(llvm::Instruction* after_allocas) -> void {
  109. CARBON_CHECK(!after_allocas_);
  110. after_allocas_ = after_allocas;
  111. }
  112. // Create a synthetic block that corresponds to no SemIR::InstBlockId. Such
  113. // a block should only ever have a single predecessor, and is used when we
  114. // need multiple `llvm::BasicBlock`s to model the linear control flow in a
  115. // single SemIR::File block.
  116. auto MakeSyntheticBlock() -> llvm::BasicBlock*;
  117. // Determine whether block is the most recently created synthetic block.
  118. auto IsCurrentSyntheticBlock(llvm::BasicBlock* block) -> bool {
  119. return synthetic_block_ == block;
  120. }
  121. // Returns the debug location to associate with the specified instruction.
  122. auto GetDebugLoc(SemIR::InstId inst_id) -> llvm::DebugLoc;
  123. // After emitting an initializer `init_id`, finishes performing the
  124. // initialization of `dest_id` from that initializer. This is a no-op if the
  125. // initialization was performed in-place, and otherwise performs a store or a
  126. // copy.
  127. auto FinishInit(SemIR::TypeId type_id, SemIR::InstId dest_id,
  128. SemIR::InstId source_id) -> void;
  129. // When fingerprinting for a specific, adds the call, found in the function
  130. // body, to <function_id, specific_id>.
  131. auto AddCallToCurrentFingerprint(SemIR::FunctionId function_id,
  132. SemIR::SpecificId specific_id) -> void;
  133. // When fingerprinting for a specific, adds the type.
  134. auto AddTypeToCurrentFingerprint(llvm::Type* type) -> void;
  135. // Emits the final function fingerprints. Only called when function lowering
  136. // is complete.
  137. auto EmitFinalFingerprint() -> void;
  138. auto llvm_context() -> llvm::LLVMContext& {
  139. return file_context_->llvm_context();
  140. }
  141. auto llvm_module() -> llvm::Module& { return file_context_->llvm_module(); }
  142. auto llvm_function() -> llvm::Function& { return *function_; }
  143. auto specific_id() -> SemIR::SpecificId { return specific_id_; }
  144. auto builder() -> llvm::IRBuilderBase& { return builder_; }
  145. auto sem_ir() -> const SemIR::File& { return file_context_->sem_ir(); }
  146. // TODO: could template on BuiltinFunctionKind if more format
  147. // globals are eventually needed.
  148. auto printf_int_format_string() -> llvm::Value* {
  149. auto* format_string = file_context_->printf_int_format_string();
  150. if (!format_string) {
  151. format_string = builder().CreateGlobalString("%d\n", "printf.int.format");
  152. file_context_->SetPrintfIntFormatString(format_string);
  153. }
  154. return format_string;
  155. }
  156. private:
  157. // Custom instruction inserter for our IR builder. Automatically names
  158. // instructions.
  159. class Inserter : public llvm::IRBuilderDefaultInserter {
  160. public:
  161. explicit Inserter(const SemIR::InstNamer* inst_namer)
  162. : inst_namer_(inst_namer) {}
  163. // Sets the instruction we are currently emitting.
  164. auto SetCurrentInstId(SemIR::InstId inst_id) -> void { inst_id_ = inst_id; }
  165. private:
  166. auto InsertHelper(llvm::Instruction* inst, const llvm::Twine& name,
  167. llvm::BasicBlock::iterator insert_pt) const
  168. -> void override;
  169. // The instruction namer.
  170. const SemIR::InstNamer* inst_namer_;
  171. // The current instruction ID.
  172. SemIR::InstId inst_id_ = SemIR::InstId::None;
  173. };
  174. // Emits a value copy for type `type_id` from `source_id` to `dest_id`.
  175. // `source_id` must produce a value representation for `type_id`, and
  176. // `dest_id` must be a pointer to a `type_id` object.
  177. auto CopyValue(SemIR::TypeId type_id, SemIR::InstId source_id,
  178. SemIR::InstId dest_id) -> void;
  179. // Emits an object representation copy for type `type_id` from `source_id` to
  180. // `dest_id`. `source_id` and `dest_id` must produce pointers to `type_id`
  181. // objects.
  182. auto CopyObject(SemIR::TypeId type_id, SemIR::InstId source_id,
  183. SemIR::InstId dest_id) -> void;
  184. // When fingerprinting for a specific, adds the global.
  185. auto AddGlobalToCurrentFingerprint(llvm::Value* global) -> void;
  186. // Context for the overall lowering process.
  187. FileContext* file_context_;
  188. // The IR function we're generating.
  189. llvm::Function* function_;
  190. // The specific id, if the function is a specific.
  191. SemIR::SpecificId specific_id_;
  192. // Builder for creating code in this function. The insertion point is held at
  193. // the location of the current SemIR instruction.
  194. llvm::IRBuilder<llvm::ConstantFolder, Inserter> builder_;
  195. // The instruction after all allocas. This is used as the insert point for new
  196. // allocas.
  197. llvm::Instruction* after_allocas_ = nullptr;
  198. llvm::DISubprogram* di_subprogram_;
  199. // The optional vlog stream.
  200. llvm::raw_ostream* vlog_stream_;
  201. // This is initialized and populated while lowering a specific function.
  202. // When complete, this is used to complete the function_fingerprint_.
  203. LoweringFunctionFingerprint current_fingerprint_;
  204. // The accumulated fingerprint is owned by the FileContext and passed into
  205. // the FunctionContext. The function fingerprint is currently only built for
  206. // specific functions, otherwise, this will be nullptr.
  207. FileContext::SpecificFunctionFingerprint* function_fingerprint_;
  208. // Maps a function's SemIR::File blocks to lowered blocks.
  209. Map<SemIR::InstBlockId, llvm::BasicBlock*> blocks_;
  210. // The synthetic block we most recently created. May be null if there is no
  211. // such block.
  212. llvm::BasicBlock* synthetic_block_ = nullptr;
  213. // Maps a function's SemIR::File instructions to lowered values.
  214. Map<SemIR::InstId, llvm::Value*> locals_;
  215. };
  216. // Provides handlers for instructions that occur in a FunctionContext. Although
  217. // this is declared for all instructions, it should only be defined for
  218. // instructions which are non-constant and not always typed. See
  219. // `FunctionContext::LowerInst` for how this is used.
  220. #define CARBON_SEM_IR_INST_KIND(Name) \
  221. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id, \
  222. SemIR::Name inst) -> void;
  223. #include "toolchain/sem_ir/inst_kind.def"
  224. } // namespace Carbon::Lower
  225. #endif // CARBON_TOOLCHAIN_LOWER_FUNCTION_CONTEXT_H_