Skip to content

Commit f2f5452

Browse files
committed
Use real atomic instructions instead of a global lock
1 parent 92f765f commit f2f5452

File tree

8 files changed

+129
-411
lines changed

8 files changed

+129
-411
lines changed

scripts/config.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export RUSTC=$dir"/bin/cg_clif"
2626
export RUSTDOCFLAGS=$linker' -Cpanic=abort -Zpanic-abort-tests '\
2727
'-Zcodegen-backend='$dir'/lib/librustc_codegen_cranelift.'$dylib_ext' --sysroot '$dir
2828

29-
# FIXME remove once the atomic shim is gone
29+
# FIXME fix `#[linkage = "extern_weak"]` without this
3030
if [[ "$unamestr" == 'Darwin' ]]; then
3131
export RUSTFLAGS="$RUSTFLAGS -Clink-arg=-undefined -Clink-arg=dynamic_lookup"
3232
fi

src/atomic_shim.rs

Lines changed: 0 additions & 185 deletions
This file was deleted.

src/backend.rs

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_session::Session;
88
use cranelift_module::FuncId;
99

1010
use object::write::*;
11-
use object::{RelocationEncoding, RelocationKind, SectionKind, SymbolFlags};
11+
use object::{RelocationEncoding, SectionKind, SymbolFlags};
1212

1313
use cranelift_object::{ObjectBuilder, ObjectModule, ObjectProduct};
1414

@@ -118,49 +118,6 @@ impl WriteDebugInfo for ObjectProduct {
118118
}
119119
}
120120

121-
// FIXME remove once atomic instructions are implemented in Cranelift.
122-
pub(crate) trait AddConstructor {
123-
fn add_constructor(&mut self, func_id: FuncId);
124-
}
125-
126-
impl AddConstructor for ObjectProduct {
127-
fn add_constructor(&mut self, func_id: FuncId) {
128-
let symbol = self.function_symbol(func_id);
129-
let segment = self
130-
.object
131-
.segment_name(object::write::StandardSegment::Data);
132-
let init_array_section =
133-
self.object
134-
.add_section(segment.to_vec(), b".init_array".to_vec(), SectionKind::Data);
135-
let address_size = self
136-
.object
137-
.architecture()
138-
.address_size()
139-
.expect("address_size must be known")
140-
.bytes();
141-
self.object.append_section_data(
142-
init_array_section,
143-
&std::iter::repeat(0)
144-
.take(address_size.into())
145-
.collect::<Vec<u8>>(),
146-
8,
147-
);
148-
self.object
149-
.add_relocation(
150-
init_array_section,
151-
object::write::Relocation {
152-
offset: 0,
153-
size: address_size * 8,
154-
kind: RelocationKind::Absolute,
155-
encoding: RelocationEncoding::Generic,
156-
symbol,
157-
addend: 0,
158-
},
159-
)
160-
.unwrap();
161-
}
162-
}
163-
164121
pub(crate) fn with_object(sess: &Session, name: &str, f: impl FnOnce(&mut Object)) -> Vec<u8> {
165122
let triple = crate::build_isa(sess).triple().clone();
166123

src/driver/aot.rs

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ use rustc_middle::mir::mono::{CodegenUnit, MonoItem};
1212
use rustc_session::cgu_reuse_tracker::CguReuse;
1313
use rustc_session::config::{DebugInfo, OutputType};
1414

15-
use cranelift_object::{ObjectModule, ObjectProduct};
15+
use cranelift_object::ObjectModule;
1616

1717
use crate::{prelude::*, BackendConfig};
1818

19-
use crate::backend::AddConstructor;
20-
2119
fn new_module(tcx: TyCtxt<'_>, name: String) -> ObjectModule {
2220
let module = crate::backend::make_module(tcx.sess, name);
2321
assert_eq!(pointer_ty(tcx), module.target_config().pointer_type());
@@ -39,7 +37,6 @@ fn emit_module(
3937
module: ObjectModule,
4038
debug: Option<DebugContext<'_>>,
4139
unwind_context: UnwindContext<'_>,
42-
map_product: impl FnOnce(ObjectProduct) -> ObjectProduct,
4340
) -> ModuleCodegenResult {
4441
let mut product = module.finish();
4542

@@ -49,8 +46,6 @@ fn emit_module(
4946

5047
unwind_context.emit(&mut product);
5148

52-
let product = map_product(product);
53-
5449
let tmp_file = tcx
5550
.output_filenames(LOCAL_CRATE)
5651
.temp_path(OutputType::Object, Some(&name));
@@ -124,30 +119,7 @@ fn module_codegen(
124119
let cgu = tcx.codegen_unit(cgu_name);
125120
let mono_items = cgu.items_in_deterministic_order(tcx);
126121

127-
let mut module = new_module(tcx, cgu_name.as_str().to_string());
128-
129-
// Initialize the global atomic mutex using a constructor for proc-macros.
130-
// FIXME implement atomic instructions in Cranelift.
131-
let mut init_atomics_mutex_from_constructor = None;
132-
if tcx
133-
.sess
134-
.crate_types()
135-
.contains(&rustc_session::config::CrateType::ProcMacro)
136-
{
137-
if mono_items.iter().any(|(mono_item, _)| match mono_item {
138-
rustc_middle::mir::mono::MonoItem::Static(def_id) => tcx
139-
.symbol_name(Instance::mono(tcx, *def_id))
140-
.name
141-
.contains("__rustc_proc_macro_decls_"),
142-
_ => false,
143-
}) {
144-
init_atomics_mutex_from_constructor =
145-
Some(crate::atomic_shim::init_global_lock_constructor(
146-
&mut module,
147-
&format!("{}_init_atomics_mutex", cgu_name.as_str()),
148-
));
149-
}
150-
}
122+
let module = new_module(tcx, cgu_name.as_str().to_string());
151123

152124
let mut cx = crate::CodegenCx::new(
153125
tcx,
@@ -180,7 +152,7 @@ fn module_codegen(
180152
}
181153
let (mut module, global_asm, debug, mut unwind_context) =
182154
tcx.sess.time("finalize CodegenCx", || cx.finalize());
183-
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut module, &mut unwind_context, false);
155+
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut module, &mut unwind_context);
184156

185157
let codegen_result = emit_module(
186158
tcx,
@@ -189,13 +161,6 @@ fn module_codegen(
189161
module,
190162
debug,
191163
unwind_context,
192-
|mut product| {
193-
if let Some(func_id) = init_atomics_mutex_from_constructor {
194-
product.add_constructor(func_id);
195-
}
196-
197-
product
198-
},
199164
);
200165

201166
codegen_global_asm(tcx, &cgu.name().as_str(), &global_asm);
@@ -275,7 +240,6 @@ pub(super) fn run_aot(
275240
allocator_module,
276241
None,
277242
allocator_unwind_context,
278-
|product| product,
279243
);
280244
if let Some((id, product)) = work_product {
281245
work_products.insert(id, product);

src/driver/jit.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,6 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
2323
tcx.sess.fatal("JIT mode doesn't work with `cargo check`.");
2424
}
2525

26-
#[cfg(unix)]
27-
unsafe {
28-
// When not using our custom driver rustc will open us without the RTLD_GLOBAL flag, so
29-
// __cg_clif_global_atomic_mutex will not be exported. We fix this by opening ourself again
30-
// as global.
31-
// FIXME remove once atomic_shim is gone
32-
33-
let mut dl_info: libc::Dl_info = std::mem::zeroed();
34-
assert_ne!(
35-
libc::dladdr(run_jit as *const libc::c_void, &mut dl_info),
36-
0
37-
);
38-
assert_ne!(
39-
libc::dlopen(dl_info.dli_fname, libc::RTLD_NOW | libc::RTLD_GLOBAL),
40-
std::ptr::null_mut(),
41-
);
42-
}
43-
4426
let imported_symbols = load_imported_symbols_for_jit(tcx);
4527

4628
let mut jit_builder = JITBuilder::with_isa(
@@ -111,7 +93,7 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
11193
tcx.sess.fatal("Inline asm is not supported in JIT mode");
11294
}
11395

114-
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut jit_module, &mut unwind_context, true);
96+
crate::main_shim::maybe_create_entry_wrapper(tcx, &mut jit_module, &mut unwind_context);
11597
crate::allocator::codegen(tcx, &mut jit_module, &mut unwind_context);
11698

11799
tcx.sess.abort_if_errors();

0 commit comments

Comments
 (0)