Skip to content

Commit 1e2a738

Browse files
committed
Auto merge of #66952 - 0dvictor:print, r=rkruppe
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.
2 parents d825e35 + 85df207 commit 1e2a738

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
@@ -588,14 +588,11 @@ pub(crate) unsafe fn codegen(cgcx: &CodegenContext<LlvmCodegenBackend>,
588588
cursor.position() as size_t
589589
}
590590

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

src/librustc_codegen_llvm/llvm/ffi.rs

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

src/rustllvm/PassWrapper.cpp

+3-40
Original file line numberDiff line numberDiff line change
@@ -658,46 +658,11 @@ class RustAssemblyAnnotationWriter : public AssemblyAnnotationWriter {
658658
}
659659
};
660660

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

687-
namespace llvm {
688-
void initializeRustPrintModulePassPass(PassRegistry&);
689-
}
690-
691-
char RustPrintModulePass::ID = 0;
692-
INITIALIZE_PASS(RustPrintModulePass, "print-rust-module",
693-
"Print rust module to stderr", false, false)
694-
695663
extern "C" LLVMRustResult
696-
LLVMRustPrintModule(LLVMPassManagerRef PMR, LLVMModuleRef M,
697-
const char *Path, DemangleFn Demangle) {
698-
llvm::legacy::PassManager *PM = unwrap<llvm::legacy::PassManager>(PMR);
664+
LLVMRustPrintModule(LLVMModuleRef M, const char *Path, DemangleFn Demangle) {
699665
std::string ErrorInfo;
700-
701666
std::error_code EC;
702667
raw_fd_ostream OS(Path, EC, sys::fs::F_None);
703668
if (EC)
@@ -707,11 +672,9 @@ LLVMRustPrintModule(LLVMPassManagerRef PMR, LLVMModuleRef M,
707672
return LLVMRustResult::Failure;
708673
}
709674

675+
RustAssemblyAnnotationWriter AAW(Demangle);
710676
formatted_raw_ostream FOS(OS);
711-
712-
PM->add(new RustPrintModulePass(FOS, Demangle));
713-
714-
PM->run(*unwrap(M));
677+
unwrap(M)->print(FOS, &AAW);
715678

716679
return LLVMRustResult::Success;
717680
}

0 commit comments

Comments
 (0)