context.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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_CONTEXT_H_
  5. #define CARBON_TOOLCHAIN_LOWER_CONTEXT_H_
  6. #include <memory>
  7. #include <optional>
  8. #include <utility>
  9. #include "llvm/IR/Constants.h"
  10. #include "llvm/IR/DIBuilder.h"
  11. #include "llvm/IR/LLVMContext.h"
  12. #include "llvm/IR/Module.h"
  13. #include "toolchain/base/fixed_size_value_store.h"
  14. #include "toolchain/lower/options.h"
  15. #include "toolchain/parse/tree_and_subtrees.h"
  16. #include "toolchain/sem_ir/absolute_node_ref.h"
  17. #include "toolchain/sem_ir/ids.h"
  18. #include "toolchain/sem_ir/inst_namer.h"
  19. namespace Carbon::Lower {
  20. class FileContext;
  21. // Context for lowering to an LLVM module.
  22. class Context {
  23. public:
  24. // Location information for use with DebugInfo. The line_number and
  25. // column_number are >= 0, with 0 as unknown, so that they can be passed
  26. // directly to DebugInfo.
  27. struct LocForDI {
  28. llvm::StringRef filename;
  29. int32_t line_number;
  30. int32_t column_number;
  31. };
  32. // A specific function whose definition needs to be lowered.
  33. struct PendingSpecificFunctionDefinition {
  34. FileContext* context;
  35. SemIR::FunctionId function_id;
  36. SemIR::SpecificId specific_id;
  37. };
  38. // `llvm_context` and `tree_and_subtrees_getters` must be non-null.
  39. // `vlog_stream` is optional.
  40. explicit Context(
  41. llvm::LLVMContext* llvm_context,
  42. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs, bool want_debug_info,
  43. const Parse::GetTreeAndSubtreesStore* tree_and_subtrees_getters,
  44. clang::CodeGenerator* code_generator, llvm::StringRef module_name,
  45. int total_ir_count, Lower::OptimizationLevel opt_level,
  46. llvm::raw_ostream* vlog_stream);
  47. // Gets or creates the `FileContext` for a given SemIR file. If an
  48. // `inst_namer` is specified the first time this is called for a file, it will
  49. // be used for that file. Otherwise, no instruction namer will be used.
  50. // TODO: Consider building an InstNamer if we're not given one.
  51. auto GetFileContext(const SemIR::File* file,
  52. const SemIR::InstNamer* inst_namer = nullptr)
  53. -> FileContext&;
  54. // Registers a specific function definition to be lowered later.
  55. auto AddPendingSpecificFunctionDefinition(
  56. PendingSpecificFunctionDefinition pending) -> void {
  57. specific_function_definitions_.push_back(pending);
  58. }
  59. // Finishes lowering and takes ownership of the LLVM module. The context
  60. // cannot be used further after calling this.
  61. auto Finalize() && -> std::unique_ptr<llvm::Module>;
  62. // Returns location information for use with DebugInfo.
  63. auto GetLocForDI(SemIR::AbsoluteNodeRef abs_node_id) -> LocForDI;
  64. // Returns a lowered value to use for a value of type `type`.
  65. auto GetTypeAsValue() -> llvm::Constant* {
  66. return llvm::ConstantStruct::get(GetTypeType());
  67. }
  68. // Returns a lowered value to use for a value of literal type.
  69. auto GetLiteralAsValue() -> llvm::Constant* {
  70. // TODO: Consider adding a named struct type for literals.
  71. return llvm::ConstantStruct::get(llvm::StructType::get(llvm_context()));
  72. }
  73. // Returns the empty LLVM struct type used to represent the type `type`.
  74. auto GetTypeType() -> llvm::StructType* {
  75. if (!type_type_) {
  76. // `type` is lowered to an empty LLVM StructType.
  77. type_type_ = llvm::StructType::create(*llvm_context_, {}, "type");
  78. }
  79. return type_type_;
  80. }
  81. // Returns the empty LLVM struct type used to represent the type `type`.
  82. auto GetFormType() -> llvm::StructType* {
  83. if (!form_type_) {
  84. // `Core.Form` is lowered to an empty LLVM StructType.
  85. form_type_ = llvm::StructType::create(*llvm_context_, {}, "Core.Form");
  86. }
  87. return form_type_;
  88. }
  89. // Returns the opaque LLVM struct type used to represent an incomplete type.
  90. auto GetOpaqueType() -> llvm::StructType* {
  91. if (!opaque_type_) {
  92. // `type` is lowered to an empty LLVM StructType.
  93. opaque_type_ = llvm::StructType::create(*llvm_context_, "opaque");
  94. }
  95. return opaque_type_;
  96. }
  97. auto llvm_context() -> llvm::LLVMContext& { return *llvm_context_; }
  98. auto llvm_module() -> llvm::Module& { return *llvm_module_; }
  99. auto file_system() -> llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>& {
  100. return file_system_;
  101. }
  102. auto opt_level() -> Lower::OptimizationLevel { return opt_level_; }
  103. auto di_builder() -> llvm::DIBuilder& { return di_builder_; }
  104. auto di_compile_unit() -> llvm::DICompileUnit* { return di_compile_unit_; }
  105. auto tree_and_subtrees_getters() -> const Parse::GetTreeAndSubtreesStore& {
  106. return *tree_and_subtrees_getters_;
  107. }
  108. auto total_ir_count() -> int { return total_ir_count_; }
  109. auto printf_int_format_string() -> llvm::Value* {
  110. return printf_int_format_string_;
  111. }
  112. auto SetPrintfIntFormatString(llvm::Value* printf_int_format_string) {
  113. CARBON_CHECK(!printf_int_format_string_,
  114. "PrintInt formatting string already generated");
  115. printf_int_format_string_ = printf_int_format_string;
  116. }
  117. private:
  118. // Create the DICompileUnit metadata for this compilation.
  119. auto BuildDICompileUnit(llvm::StringRef module_name,
  120. llvm::Module& llvm_module,
  121. llvm::DIBuilder& di_builder) -> llvm::DICompileUnit*;
  122. // Lower any definitions that have been registered for later lowering.
  123. // Currently, this lowers specifics for generic functions.
  124. auto LowerPendingDefinitions() -> void;
  125. // State for building the LLVM IR.
  126. llvm::LLVMContext* llvm_context_;
  127. clang::CodeGenerator* clang_code_generator_;
  128. std::unique_ptr<llvm::Module> llvm_module_owner_;
  129. llvm::Module* llvm_module_;
  130. // The filesystem for source code.
  131. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> file_system_;
  132. // The optimization level to specify for lowered function definitions.
  133. Lower::OptimizationLevel opt_level_;
  134. // State for building the LLVM IR debug info metadata.
  135. llvm::DIBuilder di_builder_;
  136. // The DICompileUnit, if any - null implies debug info is not being emitted.
  137. llvm::DICompileUnit* di_compile_unit_;
  138. // Parse trees. Used for debug information and crash diagnostics.
  139. const Parse::GetTreeAndSubtreesStore* tree_and_subtrees_getters_;
  140. // The optional vlog stream.
  141. llvm::raw_ostream* vlog_stream_;
  142. // The total number of files.
  143. int total_ir_count_;
  144. // The `FileContext`s for each IR that is involved in this lowering action.
  145. using FileContextStore =
  146. FixedSizeValueStore<SemIR::CheckIRId, std::unique_ptr<FileContext>>;
  147. FileContextStore file_contexts_;
  148. // Lowered version of the builtin type `type`.
  149. llvm::StructType* type_type_ = nullptr;
  150. // Lowered version of the builtin type `Core.Form`.
  151. llvm::StructType* form_type_ = nullptr;
  152. // An opaque type, used for external globals with incomplete types.
  153. llvm::StructType* opaque_type_ = nullptr;
  154. // Global format string for `printf.int.format` used by the PrintInt builtin.
  155. llvm::Value* printf_int_format_string_ = nullptr;
  156. // Tracks which specific functions need to have their definitions lowered.
  157. // This list may grow while lowering generic definitions from this list.
  158. llvm::SmallVector<PendingSpecificFunctionDefinition>
  159. specific_function_definitions_;
  160. };
  161. } // namespace Carbon::Lower
  162. #endif // CARBON_TOOLCHAIN_LOWER_CONTEXT_H_