Skip to content

Fix emitting asm and object file output at the same time #29385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/librustc_llvm/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2066,6 +2066,7 @@ extern {
PM: PassManagerRef,
M: ModuleRef,
Output: *const c_char,
skip_codegen: bool,
FileType: FileType) -> bool;
pub fn LLVMRustPrintModule(PM: PassManagerRef,
M: ModuleRef,
Expand Down
11 changes: 8 additions & 3 deletions src/librustc_trans/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ pub fn write_output_file(
pm: llvm::PassManagerRef,
m: ModuleRef,
output: &Path,
skip_codegen: bool,
file_type: llvm::FileType) {
unsafe {
let output_c = path2cstr(output);
let result = llvm::LLVMRustWriteOutputFile(
target, pm, m, output_c.as_ptr(), file_type);
target, pm, m, output_c.as_ptr(), skip_codegen, file_type);
if !result {
llvm_err(handler, format!("could not write output to {}", output.display()));
}
Expand Down Expand Up @@ -543,15 +544,19 @@ unsafe fn optimize_and_codegen(cgcx: &CodegenContext,
if config.emit_asm {
let path = output_names.with_extension(&format!("{}.s", name_extra));
with_codegen(tm, llmod, config.no_builtins, |cpm| {
write_output_file(cgcx.handler, tm, cpm, llmod, &path,
write_output_file(cgcx.handler, tm, cpm, llmod, &path, false,
llvm::AssemblyFileType);
});
}

if config.emit_obj {
// If we already emitted asm code, the codegen has already been done for this module,
// so don't do it again. See LLVMRustWriteOutputFile for details.
let skip_codegen = config.emit_asm;
let path = output_names.with_extension(&format!("{}.o", name_extra));
with_codegen(tm, llmod, config.no_builtins, |cpm| {
write_output_file(cgcx.handler, tm, cpm, llmod, &path, llvm::ObjectFileType);
write_output_file(cgcx.handler, tm, cpm, llmod, &path, skip_codegen,
llvm::ObjectFileType);
});
}
});
Expand Down
15 changes: 13 additions & 2 deletions src/rustllvm/PassWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ LLVMRustWriteOutputFile(LLVMTargetMachineRef Target,
LLVMPassManagerRef PMR,
LLVMModuleRef M,
const char *path,
bool skipCodegen,
TargetMachine::CodeGenFileType FileType) {
PassManager *PM = unwrap<PassManager>(PMR);

Expand All @@ -244,11 +245,21 @@ LLVMRustWriteOutputFile(LLVMTargetMachineRef Target,
return false;
}

// HACK: addPassesToEmitFile() also adds some codegen passes which are
// MachinePasses that may modify IR in a way that it becomes invalid (we've
// seen this with stack coloring). So the IR verifier would abort. Therefore,
// when we want to emit more than one filetype in a single run, we want to
// run the codegen passes and the verifier only for the first filetype.
// Telling LLVM to only start adding passes after it has seen a pass that
// doesn't exist allows us to achieve that.
char notAPass;
AnalysisID startBefore = skipCodegen ? (AnalysisID)&notAPass : nullptr;

#if LLVM_VERSION_MINOR >= 7
unwrap(Target)->addPassesToEmitFile(*PM, OS, FileType, false);
unwrap(Target)->addPassesToEmitFile(*PM, OS, FileType, false, startBefore);
#else
formatted_raw_ostream FOS(OS);
unwrap(Target)->addPassesToEmitFile(*PM, FOS, FileType, false);
unwrap(Target)->addPassesToEmitFile(*PM, FOS, FileType, false, startBefore);
#endif
PM->run(*unwrap(M));

Expand Down
7 changes: 7 additions & 0 deletions src/test/run-make/emit/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-include ../tools.mk

all:
$(RUSTC) -Copt-level=0 --emit=llvm-bc,llvm-ir,asm,obj,link test.rs
$(RUSTC) -Copt-level=1 --emit=llvm-bc,llvm-ir,asm,obj,link test.rs
$(RUSTC) -Copt-level=2 --emit=llvm-bc,llvm-ir,asm,obj,link test.rs
$(RUSTC) -Copt-level=3 --emit=llvm-bc,llvm-ir,asm,obj,link test.rs
19 changes: 19 additions & 0 deletions src/test/run-make/emit/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Checks for issue #24876

fn main() {
let mut v = 0;
for i in 0..0 {
v += i;
}
println!("{}", v)
}