file_context.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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/Basic/CodeGenOptions.h"
  7. #include "clang/CodeGen/ModuleBuilder.h"
  8. #include "clang/Lex/PreprocessorOptions.h"
  9. #include "toolchain/lower/context.h"
  10. #include "toolchain/lower/specific_coalescer.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. // Context and shared functionality for lowering within a SemIR file.
  17. class FileContext {
  18. public:
  19. using LoweredConstantStore =
  20. FixedSizeValueStore<SemIR::InstId, llvm::Constant*>;
  21. explicit FileContext(Context& context, const SemIR::File& sem_ir,
  22. const SemIR::InstNamer* inst_namer,
  23. llvm::raw_ostream* vlog_stream);
  24. // Creates the Clang `CodeGenerator` to generate LLVM module from imported C++
  25. // code. Returns null when not importing C++.
  26. auto CreateCppCodeGenerator() -> std::unique_ptr<clang::CodeGenerator>;
  27. // Prepares to lower code in this IR, by precomputing needed LLVM types,
  28. // constants, declarations, etc. Should only be called once, before we lower
  29. // anything in this file.
  30. auto PrepareToLower() -> void;
  31. // Lowers all the definitions provided by the SemIR::File to LLVM IR.
  32. auto LowerDefinitions() -> void;
  33. // Perform final cleanup tasks once all lowering has been completed.
  34. auto Finalize() -> void;
  35. // Gets a callable's function. Returns nullptr for a builtin or a function we
  36. // have not lowered.
  37. auto GetFunction(SemIR::FunctionId function_id,
  38. SemIR::SpecificId specific_id = SemIR::SpecificId::None)
  39. -> llvm::Function* {
  40. return *GetFunctionAddr(function_id, specific_id);
  41. }
  42. // Gets a or creates callable's function. Returns nullptr for a builtin.
  43. auto GetOrCreateFunction(SemIR::FunctionId function_id,
  44. SemIR::SpecificId specific_id) -> llvm::Function*;
  45. // Returns a lowered type for the given type_id.
  46. auto GetType(SemIR::TypeId type_id) -> llvm::Type* {
  47. return GetTypeAndDIType(type_id).llvm_ir_type;
  48. }
  49. struct LoweredTypes {
  50. llvm::Type* llvm_ir_type;
  51. llvm::DIType* llvm_di_type;
  52. };
  53. // Returns both the lowered llvm IR type and the lowered llvm IR debug info
  54. // type for the given type_id.
  55. auto GetTypeAndDIType(SemIR::TypeId type_id) const -> LoweredTypes {
  56. CARBON_CHECK(type_id.has_value(), "Should not be called with `None`");
  57. CARBON_CHECK(type_id.is_concrete(), "Lowering symbolic type {0}: {1}",
  58. type_id, sem_ir().types().GetAsInst(type_id));
  59. auto result = types_.Get(type_id);
  60. CARBON_CHECK(result.llvm_ir_type, "Missing type {0}: {1}", type_id,
  61. sem_ir().types().GetAsInst(type_id));
  62. return result;
  63. }
  64. // Returns location information for use with DebugInfo.
  65. auto GetLocForDI(SemIR::InstId inst_id) -> Context::LocForDI;
  66. // Returns a lowered value to use for a value of type `type`.
  67. auto GetTypeAsValue() -> llvm::Constant* {
  68. return context().GetTypeAsValue();
  69. }
  70. // Returns a lowered value to use for a value of literal type.
  71. auto GetLiteralAsValue() -> llvm::Constant* {
  72. return context().GetLiteralAsValue();
  73. }
  74. // Returns a value for the given constant. If specified, `use_inst_id` is the
  75. // instruction that is using this constant.
  76. auto GetConstant(SemIR::ConstantId const_id, SemIR::InstId use_inst_id)
  77. -> llvm::Value*;
  78. auto GetVtable(SemIR::VtableId vtable_id, SemIR::SpecificId specific_id)
  79. -> llvm::GlobalVariable* {
  80. if (!specific_id.has_value()) {
  81. return vtables_.Get(vtable_id);
  82. }
  83. auto*& specific_vtable = specific_vtables_.Get(specific_id);
  84. if (!specific_vtable) {
  85. specific_vtable =
  86. BuildVtable(sem_ir().vtables().Get(vtable_id), specific_id);
  87. }
  88. return specific_vtable;
  89. }
  90. // Returns the empty LLVM struct type used to represent the type `type`.
  91. auto GetTypeType() -> llvm::StructType* { return context().GetTypeType(); }
  92. auto context() -> Context& { return *context_; }
  93. auto llvm_context() -> llvm::LLVMContext& { return context().llvm_context(); }
  94. auto llvm_module() -> llvm::Module& { return context().llvm_module(); }
  95. auto cpp_code_generator() -> clang::CodeGenerator& {
  96. CARBON_CHECK(cpp_code_generator_);
  97. return *cpp_code_generator_;
  98. }
  99. auto sem_ir() const -> const SemIR::File& { return *sem_ir_; }
  100. auto cpp_file() -> const SemIR::CppFile* { return sem_ir().cpp_file(); }
  101. auto inst_namer() -> const SemIR::InstNamer* { return inst_namer_; }
  102. auto global_variables() -> const Map<SemIR::InstId, llvm::GlobalVariable*>& {
  103. return global_variables_;
  104. }
  105. auto printf_int_format_string() -> llvm::Value* {
  106. return context().printf_int_format_string();
  107. }
  108. auto SetPrintfIntFormatString(llvm::Value* printf_int_format_string) {
  109. context().SetPrintfIntFormatString(printf_int_format_string);
  110. }
  111. struct FunctionTypeInfo {
  112. llvm::FunctionType* type;
  113. llvm::SmallVector<SemIR::InstId> param_inst_ids;
  114. llvm::Type* return_type = nullptr;
  115. SemIR::InstId return_param_id = SemIR::InstId::None;
  116. };
  117. // Retrieve various features of the function's type useful for constructing
  118. // the `llvm::Type` for the `llvm::Function`. If any part of the type can't be
  119. // manifest (eg: incomplete return or parameter types), then the result is as
  120. // if the type was `void()`.
  121. auto BuildFunctionTypeInfo(const SemIR::Function& function,
  122. SemIR::SpecificId specific_id) -> FunctionTypeInfo;
  123. // Builds the global for the given instruction, which should then be cached by
  124. // the caller.
  125. auto BuildGlobalVariableDecl(SemIR::VarStorage var_storage)
  126. -> llvm::GlobalVariable*;
  127. // Builds the definition for the given function. If the function is only a
  128. // declaration with no definition, does nothing. If this is a generic it'll
  129. // only be lowered if the specific_id is specified. During this lowering of
  130. // a generic, more generic functions may be added for lowering.
  131. auto BuildFunctionDefinition(
  132. SemIR::FunctionId function_id,
  133. SemIR::SpecificId specific_id = SemIR::SpecificId::None) -> void;
  134. private:
  135. // Gets the location in which a callable's function is stored.
  136. auto GetFunctionAddr(SemIR::FunctionId function_id,
  137. SemIR::SpecificId specific_id) -> llvm::Function** {
  138. return specific_id.has_value() ? &specific_functions_.Get(specific_id)
  139. : &functions_.Get(function_id);
  140. }
  141. // Notes that a C++ function has been referenced for the first time, so we
  142. // should ask Clang to generate a definition for it if possible.
  143. auto HandleReferencedCppFunction(clang::FunctionDecl* cpp_decl) -> void;
  144. // Notes that a specific function has been referenced for the first time.
  145. // Updates the fingerprint to include the function's type, and adds the
  146. // function to the list of specific functions whose definitions should be
  147. // lowered.
  148. auto HandleReferencedSpecificFunction(SemIR::FunctionId function_id,
  149. SemIR::SpecificId specific_id,
  150. llvm::Type* llvm_type) -> void;
  151. // Builds the declaration for the given function, which should then be cached
  152. // by the caller.
  153. auto BuildFunctionDecl(SemIR::FunctionId function_id,
  154. SemIR::SpecificId specific_id =
  155. SemIR::SpecificId::None) -> llvm::Function*;
  156. // Builds a function's body. Common functionality for all functions.
  157. //
  158. // The `function_id` and `specific_id` identify the function within this
  159. // context's file. If the function was defined in a different file,
  160. // `definition_context` is a `FileContext` for that other file.
  161. // `definition_function` is the `Function` object within the file that owns
  162. // the definition.
  163. auto BuildFunctionBody(SemIR::FunctionId function_id,
  164. SemIR::SpecificId specific_id,
  165. const SemIR::Function& declaration_function,
  166. FileContext& definition_context,
  167. const SemIR::Function& definition_function) -> void;
  168. // Build the DISubprogram metadata for the given function.
  169. auto BuildDISubprogram(const SemIR::Function& function,
  170. SemIR::SpecificId specific_id,
  171. const llvm::Function* llvm_function)
  172. -> llvm::DISubprogram*;
  173. // Build a `DISubroutineType` for the given function, including the return and
  174. // parameter types.
  175. auto BuildDISubroutineType(const SemIR::Function&,
  176. SemIR::SpecificId specific_id)
  177. -> llvm::DISubroutineType*;
  178. // Builds the `llvm::Type` and `llvm::DIType` for the given instruction, which
  179. // should then be cached by the caller.
  180. auto BuildType(SemIR::InstId inst_id) -> LoweredTypes;
  181. auto BuildVtable(const SemIR::Vtable& vtable, SemIR::SpecificId specific_id)
  182. -> llvm::GlobalVariable*;
  183. // Records a specific that was lowered for a generic. These are added one
  184. // by one while lowering their definitions.
  185. auto AddLoweredSpecificForGeneric(SemIR::GenericId generic_id,
  186. SemIR::SpecificId specific_id) {
  187. lowered_specifics_.Get(generic_id).push_back(specific_id);
  188. }
  189. // The overall lowering context.
  190. Context* context_;
  191. // The input SemIR.
  192. const SemIR::File* const sem_ir_;
  193. // The options used to create the Clang Code Generator.
  194. clang::HeaderSearchOptions cpp_header_search_options_;
  195. clang::PreprocessorOptions cpp_preprocessor_options_;
  196. clang::CodeGenOptions cpp_code_gen_options_;
  197. // The Clang `CodeGenerator` to generate LLVM module from imported C++
  198. // code. Should be initialized using `CreateCppCodeGenerator()`. Can be null
  199. // if no C++ code is imported.
  200. std::unique_ptr<clang::CodeGenerator> cpp_code_generator_;
  201. // The instruction namer, if given.
  202. const SemIR::InstNamer* const inst_namer_;
  203. // The optional vlog stream.
  204. llvm::raw_ostream* vlog_stream_;
  205. // Maps callables to lowered functions. SemIR treats callables as the
  206. // canonical form of a function, so lowering needs to do the same.
  207. using LoweredFunctionStore =
  208. FixedSizeValueStore<SemIR::FunctionId, llvm::Function*>;
  209. LoweredFunctionStore functions_;
  210. // Maps specific callables to lowered functions.
  211. FixedSizeValueStore<SemIR::SpecificId, llvm::Function*> specific_functions_;
  212. // Provides lowered versions of types. Entries are non-symbolic types.
  213. using LoweredTypeStore = FixedSizeValueStore<SemIR::TypeId, LoweredTypes>;
  214. LoweredTypeStore types_;
  215. // Maps constants to their lowered values. Indexes are the `InstId` for
  216. // constant instructions.
  217. LoweredConstantStore constants_;
  218. // Maps global variables to their lowered variant.
  219. Map<SemIR::InstId, llvm::GlobalVariable*> global_variables_;
  220. // For a generic function, keep track of the specifics for which LLVM
  221. // function declarations were created. Those can be retrieved then from
  222. // `specific_functions_`.
  223. FixedSizeValueStore<SemIR::GenericId, llvm::SmallVector<SemIR::SpecificId>>
  224. lowered_specifics_;
  225. SpecificCoalescer coalescer_;
  226. FixedSizeValueStore<SemIR::VtableId, llvm::GlobalVariable*> vtables_;
  227. FixedSizeValueStore<SemIR::SpecificId, llvm::GlobalVariable*>
  228. specific_vtables_;
  229. };
  230. } // namespace Carbon::Lower
  231. #endif // CARBON_TOOLCHAIN_LOWER_FILE_CONTEXT_H_