From 85df207ecc3a7b8b7150e2b65c67eec3a23b7c81 Mon Sep 17 00:00:00 2001 From: Victor Ding Date: Mon, 2 Dec 2019 20:53:01 +1100 Subject: [PATCH] Use Module::print() instead of a PrintModulePass llvm::Module has a print() method. It is unnecessary to create a pass just for the purpose of printing LLVM IR. --- src/librustc_codegen_llvm/back/write.rs | 13 +++----- src/librustc_codegen_llvm/llvm/ffi.rs | 3 +- src/rustllvm/PassWrapper.cpp | 43 ++----------------------- 3 files changed, 9 insertions(+), 50 deletions(-) diff --git a/src/librustc_codegen_llvm/back/write.rs b/src/librustc_codegen_llvm/back/write.rs index 07ac76cec99..187063b866a 100644 --- a/src/librustc_codegen_llvm/back/write.rs +++ b/src/librustc_codegen_llvm/back/write.rs @@ -587,14 +587,11 @@ pub(crate) unsafe fn codegen(cgcx: &CodegenContext, cursor.position() as size_t } - with_codegen(tm, llmod, config.no_builtins, |cpm| { - let result = - llvm::LLVMRustPrintModule(cpm, llmod, out_c.as_ptr(), demangle_callback); - llvm::LLVMDisposePassManager(cpm); - result.into_result().map_err(|()| { - let msg = format!("failed to write LLVM IR to {}", out.display()); - llvm_err(diag_handler, &msg) - }) + let result = + llvm::LLVMRustPrintModule(llmod, out_c.as_ptr(), demangle_callback); + result.into_result().map_err(|()| { + let msg = format!("failed to write LLVM IR to {}", out.display()); + llvm_err(diag_handler, &msg) })?; } diff --git a/src/librustc_codegen_llvm/llvm/ffi.rs b/src/librustc_codegen_llvm/llvm/ffi.rs index a49e863fa21..762c821de4d 100644 --- a/src/librustc_codegen_llvm/llvm/ffi.rs +++ b/src/librustc_codegen_llvm/llvm/ffi.rs @@ -1726,8 +1726,7 @@ extern "C" { Output: *const c_char, FileType: FileType) -> LLVMRustResult; - pub fn LLVMRustPrintModule(PM: &PassManager<'a>, - M: &'a Module, + pub fn LLVMRustPrintModule(M: &'a Module, Output: *const c_char, Demangle: extern fn(*const c_char, size_t, diff --git a/src/rustllvm/PassWrapper.cpp b/src/rustllvm/PassWrapper.cpp index a116ed282ac..40823422c27 100644 --- a/src/rustllvm/PassWrapper.cpp +++ b/src/rustllvm/PassWrapper.cpp @@ -660,46 +660,11 @@ public: } }; -class RustPrintModulePass : public ModulePass { - raw_ostream* OS; - DemangleFn Demangle; -public: - static char ID; - RustPrintModulePass() : ModulePass(ID), OS(nullptr), Demangle(nullptr) {} - RustPrintModulePass(raw_ostream &OS, DemangleFn Demangle) - : ModulePass(ID), OS(&OS), Demangle(Demangle) {} - - bool runOnModule(Module &M) override { - RustAssemblyAnnotationWriter AW(Demangle); - - M.print(*OS, &AW, false); - - return false; - } - - void getAnalysisUsage(AnalysisUsage &AU) const override { - AU.setPreservesAll(); - } - - static StringRef name() { return "RustPrintModulePass"; } -}; - } // namespace -namespace llvm { - void initializeRustPrintModulePassPass(PassRegistry&); -} - -char RustPrintModulePass::ID = 0; -INITIALIZE_PASS(RustPrintModulePass, "print-rust-module", - "Print rust module to stderr", false, false) - extern "C" LLVMRustResult -LLVMRustPrintModule(LLVMPassManagerRef PMR, LLVMModuleRef M, - const char *Path, DemangleFn Demangle) { - llvm::legacy::PassManager *PM = unwrap(PMR); +LLVMRustPrintModule(LLVMModuleRef M, const char *Path, DemangleFn Demangle) { std::string ErrorInfo; - std::error_code EC; raw_fd_ostream OS(Path, EC, sys::fs::F_None); if (EC) @@ -709,11 +674,9 @@ LLVMRustPrintModule(LLVMPassManagerRef PMR, LLVMModuleRef M, return LLVMRustResult::Failure; } + RustAssemblyAnnotationWriter AAW(Demangle); formatted_raw_ostream FOS(OS); - - PM->add(new RustPrintModulePass(FOS, Demangle)); - - PM->run(*unwrap(M)); + unwrap(M)->print(FOS, &AAW); return LLVMRustResult::Success; }