Skip to content

Commit c8de552

Browse files
committed
Tls support
1 parent 0e0afb4 commit c8de552

13 files changed

+270
-207
lines changed

Cargo.lock

+153-164
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crate_patches/regex.patch

-34
This file was deleted.

example/mini_core.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![feature(
22
no_core, lang_items, intrinsics, unboxed_closures, type_ascription, extern_types,
3-
untagged_unions, decl_macro, rustc_attrs, transparent_unions, optin_builtin_traits
3+
untagged_unions, decl_macro, rustc_attrs, transparent_unions, optin_builtin_traits,
4+
thread_local,
45
)]
56
#![no_core]
67
#![allow(dead_code)]
@@ -551,3 +552,11 @@ struct PanicLocation {
551552
line: u32,
552553
column: u32,
553554
}
555+
556+
#[no_mangle]
557+
pub fn get_tls() -> u8 {
558+
#[thread_local]
559+
static A: u8 = 42;
560+
561+
A
562+
}

example/mini_core_hello_world.rs

+77-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
// Adapted from https://github.com/sunfishcode/mir2cranelift/blob/master/rust-examples/nocore-hello-world.rs
22

3-
#![feature(no_core, unboxed_closures, start, lang_items, box_syntax, slice_patterns, never_type, linkage, extern_types)]
3+
#![feature(
4+
no_core, unboxed_closures, start, lang_items, box_syntax, slice_patterns, never_type, linkage,
5+
extern_types, thread_local
6+
)]
47
#![no_core]
5-
#![allow(dead_code)]
8+
#![allow(dead_code, non_camel_case_types)]
69

710
extern crate mini_core;
811

@@ -276,6 +279,78 @@ fn main() {
276279
extern_nullptr as *const ();
277280
let slice_ptr = &[] as *const [u8];
278281
slice_ptr as *const u8;
282+
283+
#[cfg(not(jit))]
284+
test_tls();
285+
}
286+
287+
#[repr(C)]
288+
enum c_void {
289+
_1,
290+
_2,
291+
}
292+
293+
type c_int = i32;
294+
type c_ulong = u64;
295+
296+
type pthread_t = c_ulong;
297+
298+
#[repr(C)]
299+
struct pthread_attr_t {
300+
__size: [u64; 7],
301+
}
302+
303+
#[link(name = "pthread")]
304+
extern "C" {
305+
fn pthread_attr_init(attr: *mut pthread_attr_t) -> c_int;
306+
307+
fn pthread_create(
308+
native: *mut pthread_t,
309+
attr: *const pthread_attr_t,
310+
f: extern "C" fn(_: *mut c_void) -> *mut c_void,
311+
value: *mut c_void
312+
) -> c_int;
313+
314+
fn pthread_join(
315+
native: pthread_t,
316+
value: *mut *mut c_void
317+
) -> c_int;
318+
}
319+
320+
#[thread_local]
321+
#[cfg(not(jit))]
322+
static mut TLS: u8 = 42;
323+
324+
#[cfg(not(jit))]
325+
extern "C" fn mutate_tls(_: *mut c_void) -> *mut c_void {
326+
unsafe { TLS = 0; }
327+
0 as *mut c_void
328+
}
329+
330+
#[cfg(not(jit))]
331+
fn test_tls() {
332+
unsafe {
333+
let mut attr: pthread_attr_t = intrinsics::init();
334+
let mut thread: pthread_t = 0;
335+
336+
assert_eq!(TLS, 42);
337+
338+
if pthread_attr_init(&mut attr) != 0 {
339+
assert!(false);
340+
}
341+
342+
if pthread_create(&mut thread, &attr, mutate_tls, 0 as *mut c_void) != 0 {
343+
assert!(false);
344+
}
345+
346+
let mut res = 0 as *mut c_void;
347+
pthread_join(thread, &mut res);
348+
349+
// TLS of main thread must not have been changed by the other thread.
350+
assert_eq!(TLS, 42);
351+
352+
puts("TLS works!\n\0" as *const str as *const u8);
353+
}
279354
}
280355

281356
// Copied ui/issues/issue-61696.rs

example/std_example.rs

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ fn main() {
1212
let stderr = ::std::io::stderr();
1313
let mut stderr = stderr.lock();
1414

15+
std::thread::spawn(move || {
16+
println!("Hello from another thread!");
17+
});
18+
1519
writeln!(stderr, "some {} text", "<unknown>").unwrap();
1620

1721
let _ = std::process::Command::new("true").env("c", "d").spawn();

patches/0018-Add-FnBox-back.patch

+3-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ index 143cf2f..a6e8faf 100644
9595
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
9696
- pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
9797
+ pub unsafe fn new(stack: usize, p: Box<dyn FnBox()>) -> io::Result<Thread> {
98-
panic!("Warning: Threads are not yet fully supported, because cranelift doesn't support atomics.");
99-
98+
- panic!("Warning: Threads are not yet fully supported, because cranelift doesn't support atomics.");
99+
+ println!("Spawned thread");
100+
100101
let p = box p;
101102
let mut native: libc::pthread_t = mem::zeroed();
102103
diff --git a/src/libstd/sys_common/at_exit_imp.rs b/src/libstd/sys_common/at_exit_imp.rs

prepare.sh

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ git clone https://github.com/rust-lang/regex.git || echo "rust-lang/regex has al
99
pushd regex
1010
git checkout -- .
1111
git checkout 341f207c1071f7290e3f228c710817c280c8dca1
12-
git apply ../crate_patches/regex.patch
1312
popd
1413

1514
git clone https://github.com/ebobby/simple-raytracer || echo "ebobby/simple-raytracer has already been cloned"

src/atomic_shim.rs

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub fn init_global_lock(module: &mut Module<impl Backend>, bcx: &mut FunctionBui
2222
"__cg_clif_global_atomic_mutex",
2323
Linkage::Export,
2424
true,
25+
false,
2526
Some(16),
2627
).unwrap();
2728
module.define_data(atomic_mutex, &data_ctx).unwrap();
@@ -50,6 +51,7 @@ pub fn lock_global_lock(fx: &mut FunctionCx<'_, '_, impl Backend>) {
5051
"__cg_clif_global_atomic_mutex",
5152
Linkage::Import,
5253
true,
54+
false,
5355
None,
5456
).unwrap();
5557

@@ -74,6 +76,7 @@ pub fn unlock_global_lock(fx: &mut FunctionCx<'_, '_, impl Backend>) {
7476
"__cg_clif_global_atomic_mutex",
7577
Linkage::Import,
7678
true,
79+
false,
7780
None,
7881
).unwrap();
7982

src/constant.rs

+4
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ fn data_id_for_alloc_id<B: Backend>(
212212
&format!("__alloc_{}", alloc_id.0),
213213
Linkage::Local,
214214
false,
215+
false,
215216
Some(align.bytes() as u8),
216217
)
217218
.unwrap()
@@ -238,11 +239,14 @@ fn data_id_for_static(
238239
.pref
239240
.bytes();
240241

242+
let attrs = tcx.codegen_fn_attrs(def_id);
243+
241244
let data_id = module
242245
.declare_data(
243246
&*symbol_name,
244247
linkage,
245248
is_mutable,
249+
attrs.flags.contains(rustc::middle::codegen_fn_attrs::CodegenFnAttrFlags::THREAD_LOCAL),
246250
Some(align.try_into().unwrap()),
247251
)
248252
.unwrap();

src/lib.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ fn target_triple(sess: &Session) -> target_lexicon::Triple {
256256
}
257257

258258
fn build_isa(sess: &Session, enable_pic: bool) -> Box<dyn isa::TargetIsa + 'static> {
259+
use target_lexicon::BinaryFormat;
260+
261+
let target_triple = crate::target_triple(sess);
262+
259263
let mut flags_builder = settings::builder();
260264
if enable_pic {
261265
flags_builder.enable("is_pic").unwrap();
@@ -274,6 +278,14 @@ fn build_isa(sess: &Session, enable_pic: bool) -> Box<dyn isa::TargetIsa + 'stat
274278
)
275279
.unwrap();
276280

281+
let tls_model = match target_triple.binary_format {
282+
BinaryFormat::Elf => "elf_gd",
283+
BinaryFormat::Macho => "macho",
284+
BinaryFormat::Coff => "coff",
285+
_ => "none",
286+
};
287+
flags_builder.set("tls_model", tls_model).unwrap();
288+
277289
// FIXME(CraneStation/cranelift#732) fix LICM in presence of jump tables
278290
/*
279291
use rustc::session::config::OptLevel;
@@ -290,7 +302,6 @@ fn build_isa(sess: &Session, enable_pic: bool) -> Box<dyn isa::TargetIsa + 'stat
290302
}
291303
}*/
292304

293-
let target_triple = crate::target_triple(sess);
294305
let flags = settings::Flags::new(flags_builder);
295306
cranelift_codegen::isa::lookup(target_triple)
296307
.unwrap()

src/trap.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ fn codegen_print(fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>, ms
2929
&(symbol_name.name.as_str().to_string() + msg),
3030
Linkage::Local,
3131
false,
32+
false,
3233
None,
3334
)
3435
.unwrap();

src/vtable.rs

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ fn build_vtable<'tcx>(
129129
&format!("vtable.{:?}.for.{:?}", trait_ref, layout.ty),
130130
Linkage::Local,
131131
false,
132+
false,
132133
Some(
133134
fx.tcx
134135
.data_layout

test.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ echo "[BUILD] example"
2222
$RUSTC example/example.rs --crate-type lib
2323

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

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

9494
echo "[TEST] rust-lang/regex tests"

0 commit comments

Comments
 (0)