Skip to content

Commit 85df207

Browse files
author
Victor Ding
committed
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.
1 parent f5c81e0 commit 85df207

File tree

3 files changed

+9
-50
lines changed

3 files changed

+9
-50
lines changed

src/librustc_codegen_llvm/back/write.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -587,14 +587,11 @@ pub(crate) unsafe fn codegen(cgcx: &CodegenContext<LlvmCodegenBackend>,
587587
cursor.position() as size_t
588588
}
589589

590-
with_codegen(tm, llmod, config.no_builtins, |cpm| {
591-
let result =
592-
llvm::LLVMRustPrintModule(cpm, llmod, out_c.as_ptr(), demangle_callback);
593-
llvm::LLVMDisposePassManager(cpm);
594-
result.into_result().map_err(|()| {
595-
let msg = format!("failed to write LLVM IR to {}", out.display());
596-
llvm_err(diag_handler, &msg)
597-
})
590+
let result =
591+
llvm::LLVMRustPrintModule(llmod, out_c.as_ptr(), demangle_callback);
592+
result.into_result().map_err(|()| {
593+
let msg = format!("failed to write LLVM IR to {}", out.display());
594+
llvm_err(diag_handler, &msg)
598595
})?;
599596
}
600597

src/librustc_codegen_llvm/llvm/ffi.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1726,8 +1726,7 @@ extern "C" {
17261726
Output: *const c_char,
17271727
FileType: FileType)
17281728
-> LLVMRustResult;
1729-
pub fn LLVMRustPrintModule(PM: &PassManager<'a>,
1730-
M: &'a Module,
1729+
pub fn LLVMRustPrintModule(M: &'a Module,
17311730
Output: *const c_char,
17321731
Demangle: extern fn(*const c_char,
17331732
size_t,

src/rustllvm/PassWrapper.cpp

+3-40
Original file line numberDiff line numberDiff line change
@@ -660,46 +660,11 @@ class RustAssemblyAnnotationWriter : public AssemblyAnnotationWriter {
660660
}
661661
};
662662

663-
class RustPrintModulePass : public ModulePass {
664-
raw_ostream* OS;
665-
DemangleFn Demangle;
666-
public:
667-
static char ID;
668-
RustPrintModulePass() : ModulePass(ID), OS(nullptr), Demangle(nullptr) {}
669-
RustPrintModulePass(raw_ostream &OS, DemangleFn Demangle)
670-
: ModulePass(ID), OS(&OS), Demangle(Demangle) {}
671-
672-
bool runOnModule(Module &M) override {
673-
RustAssemblyAnnotationWriter AW(Demangle);
674-
675-
M.print(*OS, &AW, false);
676-
677-
return false;
678-
}
679-
680-
void getAnalysisUsage(AnalysisUsage &AU) const override {
681-
AU.setPreservesAll();
682-
}
683-
684-
static StringRef name() { return "RustPrintModulePass"; }
685-
};
686-
687663
} // namespace
688664

689-
namespace llvm {
690-
void initializeRustPrintModulePassPass(PassRegistry&);
691-
}
692-
693-
char RustPrintModulePass::ID = 0;
694-
INITIALIZE_PASS(RustPrintModulePass, "print-rust-module",
695-
"Print rust module to stderr", false, false)
696-
697665
extern "C" LLVMRustResult
698-
LLVMRustPrintModule(LLVMPassManagerRef PMR, LLVMModuleRef M,
699-
const char *Path, DemangleFn Demangle) {
700-
llvm::legacy::PassManager *PM = unwrap<llvm::legacy::PassManager>(PMR);
666+
LLVMRustPrintModule(LLVMModuleRef M, const char *Path, DemangleFn Demangle) {
701667
std::string ErrorInfo;
702-
703668
std::error_code EC;
704669
raw_fd_ostream OS(Path, EC, sys::fs::F_None);
705670
if (EC)
@@ -709,11 +674,9 @@ LLVMRustPrintModule(LLVMPassManagerRef PMR, LLVMModuleRef M,
709674
return LLVMRustResult::Failure;
710675
}
711676

677+
RustAssemblyAnnotationWriter AAW(Demangle);
712678
formatted_raw_ostream FOS(OS);
713-
714-
PM->add(new RustPrintModulePass(FOS, Demangle));
715-
716-
PM->run(*unwrap(M));
679+
unwrap(M)->print(FOS, &AAW);
717680

718681
return LLVMRustResult::Success;
719682
}

0 commit comments

Comments
 (0)