Skip to content

Commit a8e4246

Browse files
committed
Rollup merge of rust-lang#30452 - dotdash:24876_take_2, r=alexcrichton
LLVM doesn't really support reusing the same module to emit more than one file. One bug this causes is that the IR is invalidated by the stack coloring pass when emitting the first file, and then the IR verifier complains by the time we try to emit the second file. Also, we get different binaries with --emit=asm,link than with just --emit=link. In some cases leading to segfaults. Unfortunately, it seems that at this point in time, the most sensible option to circumvent this problem is to just clone the whole llvm module for the asm output if we need both, asm and obj file output. Fixes rust-lang#24876 Fixes rust-lang#26235
2 parents 4f8b32c + 88ffb26 commit a8e4246

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed

src/librustc_llvm/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ extern {
616616
C: ContextRef)
617617
-> ModuleRef;
618618
pub fn LLVMGetModuleContext(M: ModuleRef) -> ContextRef;
619+
pub fn LLVMCloneModule(M: ModuleRef) -> ModuleRef;
619620
pub fn LLVMDisposeModule(M: ModuleRef);
620621

621622
/// Data layout. See Module::getDataLayout.

src/librustc_trans/back/write.rs

+12
Original file line numberDiff line numberDiff line change
@@ -544,10 +544,22 @@ unsafe fn optimize_and_codegen(cgcx: &CodegenContext,
544544

545545
if config.emit_asm {
546546
let path = output_names.with_extension(&format!("{}.s", name_extra));
547+
548+
// We can't use the same module for asm and binary output, because that triggers
549+
// various errors like invalid IR or broken binaries, so we might have to clone the
550+
// module to produce the asm output
551+
let llmod = if config.emit_obj {
552+
llvm::LLVMCloneModule(llmod)
553+
} else {
554+
llmod
555+
};
547556
with_codegen(tm, llmod, config.no_builtins, |cpm| {
548557
write_output_file(cgcx.handler, tm, cpm, llmod, &path,
549558
llvm::AssemblyFileType);
550559
});
560+
if config.emit_obj {
561+
llvm::LLVMDisposeModule(llmod);
562+
}
551563
}
552564

553565
if config.emit_obj {

src/test/run-make/emit/Makefile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-include ../tools.mk
2+
3+
all:
4+
$(RUSTC) -Copt-level=0 --emit=llvm-bc,llvm-ir,asm,obj,link test-24876.rs
5+
$(RUSTC) -Copt-level=1 --emit=llvm-bc,llvm-ir,asm,obj,link test-24876.rs
6+
$(RUSTC) -Copt-level=2 --emit=llvm-bc,llvm-ir,asm,obj,link test-24876.rs
7+
$(RUSTC) -Copt-level=3 --emit=llvm-bc,llvm-ir,asm,obj,link test-24876.rs
8+
$(RUSTC) -Copt-level=0 --emit=llvm-bc,llvm-ir,asm,obj,link test-26235.rs
9+
$(call RUN,test-26235) || exit 1
10+
$(RUSTC) -Copt-level=1 --emit=llvm-bc,llvm-ir,asm,obj,link test-26235.rs
11+
$(call RUN,test-26235) || exit 1
12+
$(RUSTC) -Copt-level=2 --emit=llvm-bc,llvm-ir,asm,obj,link test-26235.rs
13+
$(call RUN,test-26235) || exit 1
14+
$(RUSTC) -Copt-level=3 --emit=llvm-bc,llvm-ir,asm,obj,link test-26235.rs
15+
$(call RUN,test-26235) || exit 1

src/test/run-make/emit/test-24876.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Checks for issue #24876
12+
13+
fn main() {
14+
let mut v = 0;
15+
for i in 0..0 {
16+
v += i;
17+
}
18+
println!("{}", v)
19+
}

src/test/run-make/emit/test-26235.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Checks for issue #26235
12+
13+
fn main() {
14+
use std::thread;
15+
16+
type Key = u32;
17+
const NUM_THREADS: usize = 2;
18+
19+
#[derive(Clone,Copy)]
20+
struct Stats<S> {
21+
upsert: S,
22+
delete: S,
23+
insert: S,
24+
update: S
25+
};
26+
27+
impl<S> Stats<S> where S: Copy {
28+
fn dot<B, F, T>(self, s: Stats<T>, f: F) -> Stats<B> where F: Fn(S, T) -> B {
29+
let Stats { upsert: u1, delete: d1, insert: i1, update: p1 } = self;
30+
let Stats { upsert: u2, delete: d2, insert: i2, update: p2 } = s;
31+
Stats { upsert: f(u1, u2), delete: f(d1, d2), insert: f(i1, i2), update: f(p1, p2) }
32+
}
33+
34+
fn new(init: S) -> Self {
35+
Stats { upsert: init, delete: init, insert: init, update: init }
36+
}
37+
}
38+
39+
fn make_threads() -> Vec<thread::JoinHandle<()>> {
40+
let mut t = Vec::with_capacity(NUM_THREADS);
41+
for _ in 0..NUM_THREADS {
42+
t.push(thread::spawn(move || {}));
43+
}
44+
t
45+
}
46+
47+
let stats = [Stats::new(0); NUM_THREADS];
48+
make_threads();
49+
50+
{
51+
let Stats { ref upsert, ref delete, ref insert, ref update } = stats.iter().fold(
52+
Stats::new(0), |res, &s| res.dot(s, |x: Key, y: Key| x.wrapping_add(y)));
53+
println!("upserts: {}, deletes: {}, inserts: {}, updates: {}",
54+
upsert, delete, insert, update);
55+
}
56+
}

0 commit comments

Comments
 (0)