context.h 6.0 KB

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