Skip to content

Commit

Permalink
Rollup merge of rust-lang#66952 - 0dvictor:print, r=rkruppe
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JohnTitor committed Dec 5, 2019
2 parents 0559618 + 85df207 commit efcca89
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 50 deletions.
13 changes: 5 additions & 8 deletions src/librustc_codegen_llvm/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,14 +588,11 @@ pub(crate) unsafe fn codegen(cgcx: &CodegenContext<LlvmCodegenBackend>,
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)
})?;
}

Expand Down
3 changes: 1 addition & 2 deletions src/librustc_codegen_llvm/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1727,8 +1727,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,
Expand Down
43 changes: 3 additions & 40 deletions src/rustllvm/PassWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -658,46 +658,11 @@ class RustAssemblyAnnotationWriter : public AssemblyAnnotationWriter {
}
};

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<llvm::legacy::PassManager>(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)
Expand All @@ -707,11 +672,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;
}
Expand Down

0 comments on commit efcca89

Please sign in to comment.