lower.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. llvm::ArrayRef<Parse::GetTreeAndSubtreesFn> tree_and_subtrees_getters,
  16. const SemIR::File& sem_ir, const LowerToLLVMOptions& options)
  17. -> 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(),
  20. 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);
  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.dump_stream) {
  33. module->print(*options.dump_stream, /*AAW=*/nullptr,
  34. /*ShouldPreserveUseListOrder=*/true);
  35. }
  36. if (options.llvm_verifier_stream) {
  37. CARBON_CHECK(!llvm::verifyModule(*module, options.llvm_verifier_stream));
  38. }
  39. return module;
  40. }
  41. } // namespace Carbon::Lower