Skip to content

TLS support #784

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

Merged
merged 1 commit into from
Feb 26, 2020
Merged
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
317 changes: 153 additions & 164 deletions Cargo.lock

Large diffs are not rendered by default.

34 changes: 0 additions & 34 deletions crate_patches/regex.patch

This file was deleted.

11 changes: 10 additions & 1 deletion example/mini_core.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![feature(
no_core, lang_items, intrinsics, unboxed_closures, type_ascription, extern_types,
untagged_unions, decl_macro, rustc_attrs, transparent_unions, optin_builtin_traits
untagged_unions, decl_macro, rustc_attrs, transparent_unions, optin_builtin_traits,
thread_local,
)]
#![no_core]
#![allow(dead_code)]
Expand Down Expand Up @@ -551,3 +552,11 @@ struct PanicLocation {
line: u32,
column: u32,
}

#[no_mangle]
pub fn get_tls() -> u8 {
#[thread_local]
static A: u8 = 42;

A
}
79 changes: 77 additions & 2 deletions example/mini_core_hello_world.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Adapted from https://github.com/sunfishcode/mir2cranelift/blob/master/rust-examples/nocore-hello-world.rs

#![feature(no_core, unboxed_closures, start, lang_items, box_syntax, slice_patterns, never_type, linkage, extern_types)]
#![feature(
no_core, unboxed_closures, start, lang_items, box_syntax, slice_patterns, never_type, linkage,
extern_types, thread_local
)]
#![no_core]
#![allow(dead_code)]
#![allow(dead_code, non_camel_case_types)]

extern crate mini_core;

Expand Down Expand Up @@ -276,6 +279,78 @@ fn main() {
extern_nullptr as *const ();
let slice_ptr = &[] as *const [u8];
slice_ptr as *const u8;

#[cfg(not(jit))]
test_tls();
}

#[repr(C)]
enum c_void {
_1,
_2,
}

type c_int = i32;
type c_ulong = u64;

type pthread_t = c_ulong;

#[repr(C)]
struct pthread_attr_t {
__size: [u64; 7],
}

#[link(name = "pthread")]
extern "C" {
fn pthread_attr_init(attr: *mut pthread_attr_t) -> c_int;

fn pthread_create(
native: *mut pthread_t,
attr: *const pthread_attr_t,
f: extern "C" fn(_: *mut c_void) -> *mut c_void,
value: *mut c_void
) -> c_int;

fn pthread_join(
native: pthread_t,
value: *mut *mut c_void
) -> c_int;
}

#[thread_local]
#[cfg(not(jit))]
static mut TLS: u8 = 42;

#[cfg(not(jit))]
extern "C" fn mutate_tls(_: *mut c_void) -> *mut c_void {
unsafe { TLS = 0; }
0 as *mut c_void
}

#[cfg(not(jit))]
fn test_tls() {
unsafe {
let mut attr: pthread_attr_t = intrinsics::init();
let mut thread: pthread_t = 0;

assert_eq!(TLS, 42);

if pthread_attr_init(&mut attr) != 0 {
assert!(false);
}

if pthread_create(&mut thread, &attr, mutate_tls, 0 as *mut c_void) != 0 {
assert!(false);
}

let mut res = 0 as *mut c_void;
pthread_join(thread, &mut res);

// TLS of main thread must not have been changed by the other thread.
assert_eq!(TLS, 42);

puts("TLS works!\n\0" as *const str as *const u8);
}
}

// Copied ui/issues/issue-61696.rs
Expand Down
4 changes: 4 additions & 0 deletions example/std_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ fn main() {
let stderr = ::std::io::stderr();
let mut stderr = stderr.lock();

std::thread::spawn(move || {
println!("Hello from another thread!");
});

writeln!(stderr, "some {} text", "<unknown>").unwrap();

let _ = std::process::Command::new("true").env("c", "d").spawn();
Expand Down
5 changes: 3 additions & 2 deletions patches/0018-Add-FnBox-back.patch
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ index 143cf2f..a6e8faf 100644
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
- pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
+ pub unsafe fn new(stack: usize, p: Box<dyn FnBox()>) -> io::Result<Thread> {
panic!("Warning: Threads are not yet fully supported, because cranelift doesn't support atomics.");

- panic!("Warning: Threads are not yet fully supported, because cranelift doesn't support atomics.");
+ println!("Spawned thread");

let p = box p;
let mut native: libc::pthread_t = mem::zeroed();
diff --git a/src/libstd/sys_common/at_exit_imp.rs b/src/libstd/sys_common/at_exit_imp.rs
Expand Down
1 change: 0 additions & 1 deletion prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ git clone https://github.com/rust-lang/regex.git || echo "rust-lang/regex has al
pushd regex
git checkout -- .
git checkout 341f207c1071f7290e3f228c710817c280c8dca1
git apply ../crate_patches/regex.patch
popd

git clone https://github.com/ebobby/simple-raytracer || echo "ebobby/simple-raytracer has already been cloned"
Expand Down
3 changes: 3 additions & 0 deletions src/atomic_shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub fn init_global_lock(module: &mut Module<impl Backend>, bcx: &mut FunctionBui
"__cg_clif_global_atomic_mutex",
Linkage::Export,
true,
false,
Some(16),
).unwrap();
module.define_data(atomic_mutex, &data_ctx).unwrap();
Expand Down Expand Up @@ -50,6 +51,7 @@ pub fn lock_global_lock(fx: &mut FunctionCx<'_, '_, impl Backend>) {
"__cg_clif_global_atomic_mutex",
Linkage::Import,
true,
false,
None,
).unwrap();

Expand All @@ -74,6 +76,7 @@ pub fn unlock_global_lock(fx: &mut FunctionCx<'_, '_, impl Backend>) {
"__cg_clif_global_atomic_mutex",
Linkage::Import,
true,
false,
None,
).unwrap();

Expand Down
4 changes: 4 additions & 0 deletions src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ fn data_id_for_alloc_id<B: Backend>(
&format!("__alloc_{}", alloc_id.0),
Linkage::Local,
false,
false,
Some(align.bytes() as u8),
)
.unwrap()
Expand All @@ -238,11 +239,14 @@ fn data_id_for_static(
.pref
.bytes();

let attrs = tcx.codegen_fn_attrs(def_id);

let data_id = module
.declare_data(
&*symbol_name,
linkage,
is_mutable,
attrs.flags.contains(rustc::middle::codegen_fn_attrs::CodegenFnAttrFlags::THREAD_LOCAL),
Some(align.try_into().unwrap()),
)
.unwrap();
Expand Down
13 changes: 12 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ fn target_triple(sess: &Session) -> target_lexicon::Triple {
}

fn build_isa(sess: &Session, enable_pic: bool) -> Box<dyn isa::TargetIsa + 'static> {
use target_lexicon::BinaryFormat;

let target_triple = crate::target_triple(sess);

let mut flags_builder = settings::builder();
if enable_pic {
flags_builder.enable("is_pic").unwrap();
Expand All @@ -274,6 +278,14 @@ fn build_isa(sess: &Session, enable_pic: bool) -> Box<dyn isa::TargetIsa + 'stat
)
.unwrap();

let tls_model = match target_triple.binary_format {
BinaryFormat::Elf => "elf_gd",
BinaryFormat::Macho => "macho",
BinaryFormat::Coff => "coff",
_ => "none",
};
flags_builder.set("tls_model", tls_model).unwrap();

// FIXME(CraneStation/cranelift#732) fix LICM in presence of jump tables
/*
use rustc::session::config::OptLevel;
Expand All @@ -290,7 +302,6 @@ fn build_isa(sess: &Session, enable_pic: bool) -> Box<dyn isa::TargetIsa + 'stat
}
}*/

let target_triple = crate::target_triple(sess);
let flags = settings::Flags::new(flags_builder);
cranelift_codegen::isa::lookup(target_triple)
.unwrap()
Expand Down
1 change: 1 addition & 0 deletions src/trap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fn codegen_print(fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>, ms
&(symbol_name.name.as_str().to_string() + msg),
Linkage::Local,
false,
false,
None,
)
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions src/vtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ fn build_vtable<'tcx>(
&format!("vtable.{:?}.for.{:?}", trait_ref, layout.ty),
Linkage::Local,
false,
false,
Some(
fx.tcx
.data_layout
Expand Down
4 changes: 2 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ echo "[BUILD] example"
$RUSTC example/example.rs --crate-type lib

echo "[JIT] mini_core_hello_world"
JIT_ARGS="abc bcd" SHOULD_RUN=1 $RUSTC --crate-type bin -Cprefer-dynamic example/mini_core_hello_world.rs
JIT_ARGS="abc bcd" SHOULD_RUN=1 $RUSTC --crate-type bin -Cprefer-dynamic example/mini_core_hello_world.rs --cfg jit

echo "[AOT] mini_core_hello_world"
$RUSTC example/mini_core_hello_world.rs --crate-name mini_core_hello_world --crate-type bin -g
Expand Down Expand Up @@ -88,7 +88,7 @@ echo "[TEST] rust-lang/regex example shootout-regex-dna"
../cargo.sh clean
# Make sure `[codegen mono items] start` doesn't poison the diff
../cargo.sh build --example shootout-regex-dna
cat examples/regexdna-input.txt | ../cargo.sh run --example shootout-regex-dna > res.txt
cat examples/regexdna-input.txt | ../cargo.sh run --example shootout-regex-dna | grep -v "Spawned thread" > res.txt
diff -u res.txt examples/regexdna-output.txt

echo "[TEST] rust-lang/regex tests"
Expand Down