file_context.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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_FILE_CONTEXT_H_
  5. #define CARBON_TOOLCHAIN_LOWER_FILE_CONTEXT_H_
  6. #include "clang/CodeGen/ModuleBuilder.h"
  7. #include "clang/Lex/PreprocessorOptions.h"
  8. #include "toolchain/lower/context.h"
  9. #include "toolchain/lower/specific_coalescer.h"
  10. #include "toolchain/lower/type.h"
  11. #include "toolchain/parse/tree_and_subtrees.h"
  12. #include "toolchain/sem_ir/file.h"
  13. #include "toolchain/sem_ir/ids.h"
  14. #include "toolchain/sem_ir/inst_namer.h"
  15. namespace Carbon::Lower {
  16. // Information about how a given function declaration is lowered.
  17. struct FunctionInfo {
  18. // The type of the lowered function.
  19. llvm::FunctionType* type;
  20. // The debug info type of the lowered function.
  21. llvm::DISubroutineType* di_type;
  22. // The indices of the `Call` parameter patterns that correspond to parameters
  23. // of the LLVM IR function, in the order of the LLVM IR parameter list. Some
  24. // `Call` parameters may be omitted (e.g. if they are stateless), and the
  25. // order may differ from the SemIR `Call` parameter list (e.g. the return
  26. // parameter, if any, always goes first).
  27. llvm::SmallVector<SemIR::CallParamIndex> lowered_param_indices;
  28. // The indices of any `Call` param patterns that aren't present in
  29. // lowered_param_pattern_ids.
  30. llvm::SmallVector<SemIR::CallParamIndex> unused_param_indices;
  31. // The lowered function declaration.
  32. llvm::Function* llvm_function;
  33. // Whether the function type information is inexact, because some component
  34. // type was incomplete. If this is set, the function should not be used to
  35. // emit a definition or a call.
  36. bool inexact;
  37. };
  38. // Context and shared functionality for lowering within a SemIR file.
  39. class FileContext {
  40. public:
  41. using LoweredConstantStore =
  42. FixedSizeValueStore<SemIR::InstId, llvm::Constant*,
  43. Tag<SemIR::CheckIRId>>;
  44. explicit FileContext(Context& context, const SemIR::File& sem_ir,
  45. const SemIR::InstNamer* inst_namer,
  46. llvm::raw_ostream* vlog_stream);
  47. // Prepares to lower code in this IR, by precomputing needed LLVM types,
  48. // constants, declarations, etc. Should only be called once, before we lower
  49. // anything in this file.
  50. auto PrepareToLower() -> void;
  51. // Lowers all the definitions provided by the SemIR::File to LLVM IR.
  52. auto LowerDefinitions() -> void;
  53. // Perform final cleanup tasks once all lowering has been completed.
  54. auto Finalize() -> void;
  55. // Gets a callable's function. Returns nullptr for a builtin or a function we
  56. // have not lowered.
  57. auto GetFunction(SemIR::FunctionId function_id,
  58. SemIR::SpecificId specific_id = SemIR::SpecificId::None)
  59. -> llvm::Function* {
  60. const auto& function_info = GetFunctionInfo(function_id, specific_id);
  61. return function_info ? function_info->llvm_function : nullptr;
  62. }
  63. // Returns the FunctionInfo for the given function in the given specific, if
  64. // it has already been computed.
  65. auto GetFunctionInfo(SemIR::FunctionId function_id,
  66. SemIR::SpecificId specific_id)
  67. -> std::optional<FunctionInfo>& {
  68. return specific_id.has_value() ? specific_functions_.Get(specific_id)
  69. : functions_.Get(function_id);
  70. }
  71. // Returns the FunctionInfo for the given function in the given specific. If
  72. // it's not already available, this function will compute it, including
  73. // creating the `llvm::Function` for it. Returns nullopt for a builtin.
  74. //
  75. // The fallback information is used if the specific function has incomplete
  76. // types.
  77. auto GetOrCreateFunctionInfo(
  78. SemIR::FunctionId function_id, SemIR::SpecificId specific_id,
  79. FileContext* fallback_file = nullptr,
  80. SemIR::FunctionId fallback_function_id = SemIR::FunctionId::None,
  81. SemIR::SpecificId fallback_specific_id = SemIR::SpecificId::None)
  82. -> std::optional<FunctionInfo>&;
  83. // Returns a lowered type for the given type_id.
  84. auto GetType(SemIR::TypeId type_id) -> llvm::Type* {
  85. return GetTypeAndDIType(type_id).llvm_ir_type;
  86. }
  87. // Returns both the lowered llvm IR type and the lowered llvm IR debug info
  88. // type for the given type_id.
  89. auto GetTypeAndDIType(SemIR::TypeId type_id) const -> LoweredTypes {
  90. CARBON_CHECK(type_id.has_value(), "Should not be called with `None`");
  91. CARBON_CHECK(type_id.is_concrete(), "Lowering symbolic type {0}: {1}",
  92. type_id, sem_ir().types().GetAsInst(type_id));
  93. auto result = types_.Get(type_id);
  94. if (!result.llvm_ir_type) {
  95. result.llvm_ir_type = context_->GetOpaqueType();
  96. }
  97. return result;
  98. }
  99. // Returns location information for use with DebugInfo.
  100. auto GetLocForDI(SemIR::InstId inst_id) -> Context::LocForDI;
  101. // Returns a lowered value to use for a value of type `type`.
  102. auto GetTypeAsValue() -> llvm::Constant* {
  103. return context().GetTypeAsValue();
  104. }
  105. // Returns a lowered value to use for a value of literal type.
  106. auto GetLiteralAsValue() -> llvm::Constant* {
  107. return context().GetLiteralAsValue();
  108. }
  109. // Returns a value for the given constant. If specified, `use_inst_id` is the
  110. // instruction that is using this constant.
  111. auto GetConstant(SemIR::ConstantId const_id, SemIR::InstId use_inst_id)
  112. -> llvm::Value*;
  113. auto GetVtable(SemIR::VtableId vtable_id, SemIR::SpecificId specific_id)
  114. -> llvm::GlobalVariable* {
  115. if (!specific_id.has_value()) {
  116. return vtables_.Get(vtable_id);
  117. }
  118. auto*& specific_vtable = specific_vtables_.Get(specific_id);
  119. if (!specific_vtable) {
  120. specific_vtable =
  121. BuildVtable(sem_ir().vtables().Get(vtable_id), specific_id);
  122. }
  123. return specific_vtable;
  124. }
  125. // Returns the empty LLVM struct type used to represent the type `type`.
  126. auto GetTypeType() -> llvm::StructType* { return context().GetTypeType(); }
  127. auto GetFormType() -> llvm::StructType* { return context().GetFormType(); }
  128. auto context() -> Context& { return *context_; }
  129. auto llvm_context() -> llvm::LLVMContext& { return context().llvm_context(); }
  130. auto llvm_module() -> llvm::Module& { return context().llvm_module(); }
  131. auto cpp_code_generator() -> clang::CodeGenerator& {
  132. CARBON_CHECK(cpp_code_generator_);
  133. return *cpp_code_generator_;
  134. }
  135. auto sem_ir() const -> const SemIR::File& { return *sem_ir_; }
  136. auto cpp_file() -> const SemIR::CppFile* { return sem_ir().cpp_file(); }
  137. auto inst_namer() -> const SemIR::InstNamer* { return inst_namer_; }
  138. auto global_variables() -> const Map<SemIR::InstId, llvm::GlobalVariable*>& {
  139. return global_variables_;
  140. }
  141. auto printf_int_format_string() -> llvm::Value* {
  142. return context().printf_int_format_string();
  143. }
  144. auto SetPrintfIntFormatString(llvm::Value* printf_int_format_string) {
  145. context().SetPrintfIntFormatString(printf_int_format_string);
  146. }
  147. // Builds the global for the given instruction, which should then be cached by
  148. // the caller.
  149. auto BuildGlobalVariableDecl(SemIR::VarStorage var_storage)
  150. -> llvm::Constant*;
  151. // Builds the global for the given instruction which is known to not be
  152. // imported from C++.
  153. auto BuildNonCppGlobalVariableDecl(SemIR::VarStorage var_storage)
  154. -> llvm::GlobalVariable*;
  155. // Builds the definition for the given function. If the function is only a
  156. // declaration with no definition, does nothing. If this is a generic it'll
  157. // only be lowered if the specific_id is specified. During this lowering of
  158. // a generic, more generic functions may be added for lowering.
  159. auto BuildFunctionDefinition(
  160. SemIR::FunctionId function_id,
  161. SemIR::SpecificId specific_id = SemIR::SpecificId::None) -> void;
  162. private:
  163. // Notes that a C++ function has been referenced for the first time, so we
  164. // should ask Clang to generate a definition for it if possible.
  165. auto HandleReferencedCppFunction(clang::FunctionDecl* cpp_decl)
  166. -> llvm::Function*;
  167. // Notes that a specific function has been referenced for the first time.
  168. // Updates the fingerprint to include the function's type, and adds the
  169. // function to the list of specific functions whose definitions should be
  170. // lowered.
  171. auto HandleReferencedSpecificFunction(SemIR::FunctionId function_id,
  172. SemIR::SpecificId specific_id,
  173. llvm::Type* llvm_type) -> void;
  174. // Builds an LLVM function declaration for the given function, or returns an
  175. // existing one if we've already lowered another declaration of the same
  176. // function.
  177. auto GetOrCreateLLVMFunction(const FunctionTypeInfo& function_type_info,
  178. SemIR::FunctionId function_id,
  179. SemIR::SpecificId specific_id)
  180. -> llvm::Function*;
  181. // Builds the declaration for the given function, which should then be cached
  182. // by the caller.
  183. auto BuildFunctionDecl(
  184. SemIR::FunctionId function_id,
  185. SemIR::SpecificId specific_id = SemIR::SpecificId::None,
  186. FileContext* fallback_file = nullptr,
  187. SemIR::FunctionId fallback_function_id = SemIR::FunctionId::None,
  188. SemIR::SpecificId fallback_specific_id = SemIR::SpecificId::None)
  189. -> std::optional<FunctionInfo>;
  190. // Builds a function's body. Common functionality for all functions.
  191. //
  192. // The `function_id` and `specific_id` identify the function within this
  193. // context's file. If the function was defined in a different file,
  194. // `definition_context` is a `FileContext` for that other file.
  195. // `definition_function` is the `Function` object within the file that owns
  196. // the definition.
  197. auto BuildFunctionBody(SemIR::FunctionId function_id,
  198. SemIR::SpecificId specific_id,
  199. const SemIR::Function& declaration_function,
  200. FileContext& definition_context,
  201. const SemIR::Function& definition_function) -> void;
  202. // Build the DISubprogram metadata for the given function.
  203. auto BuildDISubprogram(const SemIR::Function& function,
  204. const FunctionInfo& function_info)
  205. -> llvm::DISubprogram*;
  206. auto BuildVtable(const SemIR::Vtable& vtable, SemIR::SpecificId specific_id)
  207. -> llvm::GlobalVariable*;
  208. // Records a specific that was lowered for a generic. These are added one
  209. // by one while lowering their definitions.
  210. auto AddLoweredSpecificForGeneric(SemIR::GenericId generic_id,
  211. SemIR::SpecificId specific_id) {
  212. lowered_specifics_.Get(generic_id).push_back(specific_id);
  213. }
  214. // The overall lowering context.
  215. Context* context_;
  216. // The input SemIR.
  217. const SemIR::File* const sem_ir_;
  218. // The Clang `CodeGenerator` to generate LLVM module from imported C++
  219. // code. Can be null if no C++ code is imported.
  220. clang::CodeGenerator* cpp_code_generator_;
  221. // The instruction namer, if given.
  222. const SemIR::InstNamer* const inst_namer_;
  223. // The optional vlog stream.
  224. llvm::raw_ostream* vlog_stream_;
  225. // Maps callables to lowered functions. SemIR treats callables as the
  226. // canonical form of a function, so lowering needs to do the same.
  227. using LoweredFunctionStore =
  228. FixedSizeValueStore<SemIR::FunctionId, std::optional<FunctionInfo>,
  229. Tag<SemIR::CheckIRId>>;
  230. LoweredFunctionStore functions_;
  231. // Maps specific callables to lowered functions.
  232. FixedSizeValueStore<SemIR::SpecificId, std::optional<FunctionInfo>,
  233. Tag<SemIR::CheckIRId>>
  234. specific_functions_;
  235. // Provides lowered versions of types. Entries are non-symbolic types.
  236. //
  237. // TypeIds internally are concrete ConstantIds.
  238. using LoweredTypeStore =
  239. FixedSizeValueStore<SemIR::TypeId, LoweredTypes, Tag<SemIR::CheckIRId>>;
  240. LoweredTypeStore types_;
  241. // Maps constants to their lowered values. Indexes are the `InstId` for
  242. // constant instructions.
  243. LoweredConstantStore constants_;
  244. // Maps global variables to their lowered variant.
  245. Map<SemIR::InstId, llvm::GlobalVariable*> global_variables_;
  246. // For a generic function, keep track of the specifics for which LLVM
  247. // function declarations were created. Those can be retrieved then from
  248. // `specific_functions_`.
  249. FixedSizeValueStore<SemIR::GenericId, llvm::SmallVector<SemIR::SpecificId>,
  250. Tag<SemIR::CheckIRId>>
  251. lowered_specifics_;
  252. SpecificCoalescer coalescer_;
  253. FixedSizeValueStore<SemIR::VtableId, llvm::GlobalVariable*,
  254. Tag<SemIR::CheckIRId>>
  255. vtables_;
  256. FixedSizeValueStore<SemIR::SpecificId, llvm::GlobalVariable*,
  257. Tag<SemIR::CheckIRId>>
  258. specific_vtables_;
  259. };
  260. } // namespace Carbon::Lower
  261. #endif // CARBON_TOOLCHAIN_LOWER_FILE_CONTEXT_H_