lower.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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(&llvm_context, std::move(fs), options.want_debug_info,
  19. &tree_and_subtrees_getters, sem_ir.filename(), total_ir_count,
  20. options.opt_level, options.vlog_stream);
  21. // TODO: Consider disabling instruction naming by default if we're not
  22. // producing textual LLVM IR.
  23. SemIR::InstNamer inst_namer(&sem_ir, total_ir_count);
  24. context.GetFileContext(&sem_ir, &inst_namer).LowerDefinitions();
  25. std::unique_ptr<llvm::Module> module = std::move(context).Finalize();
  26. if (options.vlog_stream) {
  27. CARBON_VLOG_TO(options.vlog_stream, "*** llvm::Module ***\n");
  28. module->print(*options.vlog_stream, /*AAW=*/nullptr,
  29. /*ShouldPreserveUseListOrder=*/false,
  30. /*IsForDebug=*/true);
  31. }
  32. if (options.llvm_verifier_stream) {
  33. CARBON_CHECK(!llvm::verifyModule(*module, options.llvm_verifier_stream));
  34. }
  35. return module;
  36. }
  37. } // namespace Carbon::Lower