lower.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. #include "toolchain/lower/lower.h"
  5. #include <memory>
  6. #include <optional>
  7. #include "common/vlog.h"
  8. #include "llvm/IR/Verifier.h"
  9. #include "toolchain/lower/context.h"
  10. #include "toolchain/lower/file_context.h"
  11. namespace Carbon::Lower {
  12. auto LowerToLLVM(
  13. llvm::LLVMContext& llvm_context,
  14. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
  15. const Parse::GetTreeAndSubtreesStore& tree_and_subtrees_getters,
  16. const SemIR::File& sem_ir, int total_ir_count,
  17. const LowerToLLVMOptions& options) -> std::unique_ptr<llvm::Module> {
  18. Context context(
  19. &llvm_context, std::move(fs), options.want_debug_info,
  20. &tree_and_subtrees_getters,
  21. sem_ir.cpp_file() ? sem_ir.cpp_file()->GetCodeGenerator() : nullptr,
  22. sem_ir.filename(), total_ir_count, options.opt_level,
  23. options.vlog_stream);
  24. // TODO: Consider disabling instruction naming by default if we're not
  25. // producing textual LLVM IR.
  26. SemIR::InstNamer inst_namer(&sem_ir, total_ir_count);
  27. context.GetFileContext(&sem_ir, &inst_namer).LowerDefinitions();
  28. std::unique_ptr<llvm::Module> module = std::move(context).Finalize();
  29. if (options.vlog_stream) {
  30. CARBON_VLOG_TO(options.vlog_stream, "*** llvm::Module ***\n");
  31. module->print(*options.vlog_stream, /*AAW=*/nullptr,
  32. /*ShouldPreserveUseListOrder=*/false,
  33. /*IsForDebug=*/true);
  34. }
  35. if (options.llvm_verifier_stream) {
  36. CARBON_CHECK(!llvm::verifyModule(*module, options.llvm_verifier_stream));
  37. }
  38. return module;
  39. }
  40. } // namespace Carbon::Lower