Skip to content

Commit

Permalink
add tests for separate compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
spernsteiner committed Sep 5, 2014
1 parent 73f8adc commit 4b70269
Show file tree
Hide file tree
Showing 18 changed files with 502 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/test/auxiliary/sepcomp-extern-lib.rs
@@ -0,0 +1,14 @@
// Copyright 2012 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.

#[no_mangle]
pub extern "C" fn foo() -> uint {
1234
}
17 changes: 17 additions & 0 deletions src/test/auxiliary/sepcomp_cci_lib.rs
@@ -0,0 +1,17 @@
// Copyright 2012 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.

#[inline]
pub fn cci_fn() -> uint {
1200
}

#[inline]
pub static CCI_STATIC: uint = 34;
31 changes: 31 additions & 0 deletions src/test/auxiliary/sepcomp_lib.rs
@@ -0,0 +1,31 @@
// Copyright 2012 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.

// compile-flags: -C codegen-units=3 --crate-type=rlib,dylib

pub mod a {
pub fn one() -> uint {
1
}
}

pub mod b {
pub fn two() -> uint {
2
}
}

pub mod c {
use a::one;
use b::two;
pub fn three() -> uint {
one() + two()
}
}
28 changes: 28 additions & 0 deletions src/test/compile-fail/sepcomp-lib-lto.rs
@@ -0,0 +1,28 @@
// Copyright 2012 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.

// Make sure we give a sane error message when the user requests LTO with a
// library built with -C codegen-units > 1.

// aux-build:sepcomp_lib.rs
// compile-flags: -Z lto
// error-pattern:missing compressed bytecode
// no-prefer-dynamic

extern crate sepcomp_lib;
use sepcomp_lib::a::one;
use sepcomp_lib::b::two;
use sepcomp_lib::c::three;

fn main() {
assert_eq!(one(), 1);
assert_eq!(two(), 2);
assert_eq!(three(), 3);
}
10 changes: 10 additions & 0 deletions src/test/run-make/sepcomp-cci-copies/Makefile
@@ -0,0 +1,10 @@
-include ../tools.mk

# Check that cross-crate inlined items are inlined in all compilation units
# that refer to them, and not in any other compilation units.

all:
$(RUSTC) cci_lib.rs
$(RUSTC) foo.rs --emit=ir -C codegen-units=3
[ "$$(cat "$(TMPDIR)"/foo.?.ll | grep -c define\ .*cci_fn)" -eq "2" ]
[ "$$(cat "$(TMPDIR)"/foo.?.ll | grep -c CCI_STATIC.*=.*constant)" -eq "2" ]
19 changes: 19 additions & 0 deletions src/test/run-make/sepcomp-cci-copies/cci_lib.rs
@@ -0,0 +1,19 @@
// Copyright 2012 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.

#![crate_type = "rlib"]

#[inline]
pub fn cci_fn() -> uint {
1234
}

#[inline]
pub static CCI_STATIC: uint = 2345;
36 changes: 36 additions & 0 deletions src/test/run-make/sepcomp-cci-copies/foo.rs
@@ -0,0 +1,36 @@
// Copyright 2014 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.

extern crate cci_lib;
use cci_lib::{cci_fn, CCI_STATIC};

fn call1() -> uint {
cci_fn() + CCI_STATIC
}

mod a {
use cci_lib::cci_fn;
pub fn call2() -> uint {
cci_fn()
}
}

mod b {
use cci_lib::CCI_STATIC;
pub fn call3() -> uint {
CCI_STATIC
}
}

fn main() {
call1();
a::call2();
b::call3();
}
13 changes: 13 additions & 0 deletions src/test/run-make/sepcomp-inlining/Makefile
@@ -0,0 +1,13 @@
-include ../tools.mk

# Test that #[inline(always)] functions still get inlined across compilation
# unit boundaries. Compilation should produce three IR files, with each one
# containing a definition of the inlined function. Also, the non-#[inline]
# function should be defined in only one compilation unit.

all:
$(RUSTC) foo.rs --emit=ir -C codegen-units=3
[ "$$(cat "$(TMPDIR)"/foo.?.ll | grep -c define\ i32\ .*inlined)" -eq "1" ]
[ "$$(cat "$(TMPDIR)"/foo.?.ll | grep -c define\ available_externally\ i32\ .*inlined)" -eq "2" ]
[ "$$(cat "$(TMPDIR)"/foo.?.ll | grep -c define\ i32\ .*normal)" -eq "1" ]
[ "$$(cat "$(TMPDIR)"/foo.?.ll | grep -c declare\ i32\ .*normal)" -eq "2" ]
35 changes: 35 additions & 0 deletions src/test/run-make/sepcomp-inlining/foo.rs
@@ -0,0 +1,35 @@
// Copyright 2014 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.

#[inline]
fn inlined() -> u32 {
1234
}

fn normal() -> u32 {
2345
}

mod a {
pub fn f() -> u32 {
::inlined() + ::normal()
}
}

mod b {
pub fn f() -> u32 {
::inlined() + ::normal()
}
}

fn main() {
a::f();
b::f();
}
9 changes: 9 additions & 0 deletions src/test/run-make/sepcomp-separate/Makefile
@@ -0,0 +1,9 @@
-include ../tools.mk

# Test that separate compilation actually puts code into separate compilation
# units. `foo.rs` defines `magic_fn` in three different modules, which should
# wind up in three different compilation units.

all:
$(RUSTC) foo.rs --emit=ir -C codegen-units=3
[ "$$(cat "$(TMPDIR)"/foo.?.ll | grep -c define\ .*magic_fn)" -eq "3" ]
27 changes: 27 additions & 0 deletions src/test/run-make/sepcomp-separate/foo.rs
@@ -0,0 +1,27 @@
// Copyright 2014 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.

fn magic_fn() -> uint {
1234
}

mod a {
pub fn magic_fn() -> uint {
2345
}
}

mod b {
pub fn magic_fn() -> uint {
3456
}
}

fn main() { }
41 changes: 41 additions & 0 deletions src/test/run-pass/sepcomp-cci.rs
@@ -0,0 +1,41 @@
// Copyright 2012 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.

// compile-flags: -C codegen-units=3
// aux-build:sepcomp_cci_lib.rs

// Test accessing cross-crate inlined items from multiple compilation units.

extern crate sepcomp_cci_lib;
use sepcomp_cci_lib::{cci_fn, CCI_STATIC};

fn call1() -> uint {
cci_fn() + CCI_STATIC
}

mod a {
use sepcomp_cci_lib::{cci_fn, CCI_STATIC};
pub fn call2() -> uint {
cci_fn() + CCI_STATIC
}
}

mod b {
use sepcomp_cci_lib::{cci_fn, CCI_STATIC};
pub fn call3() -> uint {
cci_fn() + CCI_STATIC
}
}

fn main() {
assert_eq!(call1(), 1234);
assert_eq!(a::call2(), 1234);
assert_eq!(b::call3(), 1234);
}
42 changes: 42 additions & 0 deletions src/test/run-pass/sepcomp-extern.rs
@@ -0,0 +1,42 @@
// Copyright 2012 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.

// compile-flags: -C codegen-units=3
// aux-build:sepcomp-extern-lib.rs

// Test accessing external items from multiple compilation units.

#[link(name = "sepcomp-extern-lib")]
extern {
#[allow(ctypes)]
fn foo() -> uint;
}

fn call1() -> uint {
unsafe { foo() }
}

mod a {
pub fn call2() -> uint {
unsafe { ::foo() }
}
}

mod b {
pub fn call3() -> uint {
unsafe { ::foo() }
}
}

fn main() {
assert_eq!(call1(), 1234);
assert_eq!(a::call2(), 1234);
assert_eq!(b::call3(), 1234);
}
41 changes: 41 additions & 0 deletions src/test/run-pass/sepcomp-fns-backwards.rs
@@ -0,0 +1,41 @@
// Copyright 2012 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.

// compile-flags: -C codegen-units=3

// Test references to items that haven't been translated yet.

// Generate some code in the first compilation unit before declaring any
// modules. This ensures that the first module doesn't go into the same
// compilation unit as the top-level module.
fn pad() -> uint { 0 }

mod b {
pub fn three() -> uint {
::one() + ::a::two()
}
}

mod a {
pub fn two() -> uint {
::one() + ::one()
}
}

fn one() -> uint {
1
}

fn main() {
assert_eq!(one(), 1);
assert_eq!(a::two(), 2);
assert_eq!(b::three(), 3);
}

0 comments on commit 4b70269

Please sign in to comment.