context.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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_id.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. llvm::StringRef module_name, int total_ir_count,
  45. Lower::OptimizationLevel opt_level, llvm::raw_ostream* vlog_stream);
  46. // Gets or creates the `FileContext` for a given SemIR file. If an
  47. // `inst_namer` is specified the first time this is called for a file, it will
  48. // be used for that file. Otherwise, no instruction namer will be used.
  49. // TODO: Consider building an InstNamer if we're not given one.
  50. auto GetFileContext(const SemIR::File* file,
  51. const SemIR::InstNamer* inst_namer = nullptr)
  52. -> FileContext&;
  53. // Registers a specific function definition to be lowered later.
  54. auto AddPendingSpecificFunctionDefinition(
  55. PendingSpecificFunctionDefinition pending) -> void {
  56. specific_function_definitions_.push_back(pending);
  57. }
  58. // Finishes lowering and takes ownership of the LLVM module. The context
  59. // cannot be used further after calling this.
  60. auto Finalize() && -> std::unique_ptr<llvm::Module>;
  61. // Returns location information for use with DebugInfo.
  62. auto GetLocForDI(SemIR::AbsoluteNodeId abs_node_id) -> LocForDI;
  63. // Returns a lowered value to use for a value of type `type`.
  64. auto GetTypeAsValue() -> llvm::Constant* {
  65. return llvm::ConstantStruct::get(GetTypeType());
  66. }
  67. // Returns a lowered value to use for a value of literal type.
  68. auto GetLiteralAsValue() -> llvm::Constant* {
  69. // TODO: Consider adding a named struct type for literals.
  70. return llvm::ConstantStruct::get(llvm::StructType::get(llvm_context()));
  71. }
  72. // Returns the empty LLVM struct type used to represent the type `type`.
  73. auto GetTypeType() -> llvm::StructType* {
  74. if (!type_type_) {
  75. // `type` is lowered to an empty LLVM StructType.
  76. type_type_ = llvm::StructType::create(*llvm_context_, {}, "type");
  77. }
  78. return type_type_;
  79. }
  80. // Returns the empty LLVM struct type used to represent the type `type`.
  81. auto GetFormType() -> llvm::StructType* {
  82. if (!form_type_) {
  83. // `Core.Form` is lowered to an empty LLVM StructType.
  84. form_type_ = llvm::StructType::create(*llvm_context_, {}, "Core.Form");
  85. }
  86. return form_type_;
  87. }
  88. // Returns the opaque LLVM struct type used to represent an incomplete type.
  89. auto GetOpaqueType() -> llvm::StructType* {
  90. if (!opaque_type_) {
  91. // `type` is lowered to an empty LLVM StructType.
  92. opaque_type_ = llvm::StructType::create(*llvm_context_, "opaque");
  93. }
  94. return opaque_type_;
  95. }
  96. auto llvm_context() -> llvm::LLVMContext& { return *llvm_context_; }
  97. auto llvm_module() -> llvm::Module& { return *llvm_module_; }
  98. auto file_system() -> llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>& {
  99. return file_system_;
  100. }
  101. auto opt_level() -> Lower::OptimizationLevel { return opt_level_; }
  102. auto di_builder() -> llvm::DIBuilder& { return di_builder_; }
  103. auto di_compile_unit() -> llvm::DICompileUnit* { return di_compile_unit_; }
  104. auto tree_and_subtrees_getters() -> const Parse::GetTreeAndSubtreesStore& {
  105. return *tree_and_subtrees_getters_;
  106. }
  107. auto total_ir_count() -> int { return total_ir_count_; }
  108. auto printf_int_format_string() -> llvm::Value* {
  109. return printf_int_format_string_;
  110. }
  111. auto SetPrintfIntFormatString(llvm::Value* printf_int_format_string) {
  112. CARBON_CHECK(!printf_int_format_string_,
  113. "PrintInt formatting string already generated");
  114. printf_int_format_string_ = printf_int_format_string;
  115. }
  116. private:
  117. // Create the DICompileUnit metadata for this compilation.
  118. auto BuildDICompileUnit(llvm::StringRef module_name,
  119. llvm::Module& llvm_module,
  120. llvm::DIBuilder& di_builder) -> llvm::DICompileUnit*;
  121. // Lower any definitions that have been registered for later lowering.
  122. // Currently, this lowers specifics for generic functions.
  123. auto LowerPendingDefinitions() -> void;
  124. // State for building the LLVM IR.
  125. llvm::LLVMContext* llvm_context_;
  126. std::unique_ptr<llvm::Module> llvm_module_;
  127. // The filesystem for source code.
  128. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> file_system_;
  129. // The optimization level to specify for lowered function definitions.
  130. Lower::OptimizationLevel opt_level_;
  131. // State for building the LLVM IR debug info metadata.
  132. llvm::DIBuilder di_builder_;
  133. // The DICompileUnit, if any - null implies debug info is not being emitted.
  134. llvm::DICompileUnit* di_compile_unit_;
  135. // Parse trees. Used for debug information and crash diagnostics.
  136. const Parse::GetTreeAndSubtreesStore* tree_and_subtrees_getters_;
  137. // The optional vlog stream.
  138. llvm::raw_ostream* vlog_stream_;
  139. // The total number of files.
  140. int total_ir_count_;
  141. // The `FileContext`s for each IR that is involved in this lowering action.
  142. using FileContextStore =
  143. FixedSizeValueStore<SemIR::CheckIRId, std::unique_ptr<FileContext>>;
  144. FileContextStore file_contexts_;
  145. // Lowered version of the builtin type `type`.
  146. llvm::StructType* type_type_ = nullptr;
  147. // Lowered version of the builtin type `Core.Form`.
  148. llvm::StructType* form_type_ = nullptr;
  149. // An opaque type, used for external globals with incomplete types.
  150. llvm::StructType* opaque_type_ = nullptr;
  151. // Global format string for `printf.int.format` used by the PrintInt builtin.
  152. llvm::Value* printf_int_format_string_ = nullptr;
  153. // Tracks which specific functions need to have their definitions lowered.
  154. // This list may grow while lowering generic definitions from this list.
  155. llvm::SmallVector<PendingSpecificFunctionDefinition>
  156. specific_function_definitions_;
  157. };
  158. } // namespace Carbon::Lower
  159. #endif // CARBON_TOOLCHAIN_LOWER_CONTEXT_H_