Skip to content

Commit ce7f58b

Browse files
committed
Merge two operations that were always performed together
1 parent ea71808 commit ce7f58b

File tree

5 files changed

+70
-76
lines changed

5 files changed

+70
-76
lines changed

Diff for: compiler/rustc_codegen_llvm/src/back/lto.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,8 @@ fn fat_lto(
362362
ptr as *const *const libc::c_char,
363363
symbols_below_threshold.len() as libc::size_t,
364364
);
365-
save_temp_bitcode(cgcx, &module, "lto.after-restriction");
366365
}
366+
save_temp_bitcode(cgcx, &module, "lto.after-restriction");
367367
}
368368

369369
Ok(LtoModuleCodegen::Fat(module))

Diff for: compiler/rustc_codegen_llvm/src/callee.rs

+49-53
Original file line numberDiff line numberDiff line change
@@ -97,65 +97,61 @@ pub(crate) fn get_fn<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'t
9797
// has been applied to the definition (wherever that definition may be).
9898

9999
llvm::set_linkage(llfn, llvm::Linkage::ExternalLinkage);
100-
unsafe {
101-
let is_generic = instance.args.non_erasable_generics().next().is_some();
102-
103-
let is_hidden = if is_generic {
104-
// This is a monomorphization of a generic function.
105-
if !(cx.tcx.sess.opts.share_generics()
106-
|| tcx.codegen_fn_attrs(instance_def_id).inline
107-
== rustc_attr_parsing::InlineAttr::Never)
108-
{
109-
// When not sharing generics, all instances are in the same
110-
// crate and have hidden visibility.
111-
true
112-
} else {
113-
if let Some(instance_def_id) = instance_def_id.as_local() {
114-
// This is a monomorphization of a generic function
115-
// defined in the current crate. It is hidden if:
116-
// - the definition is unreachable for downstream
117-
// crates, or
118-
// - the current crate does not re-export generics
119-
// (because the crate is a C library or executable)
120-
cx.tcx.is_unreachable_local_definition(instance_def_id)
121-
|| !cx.tcx.local_crate_exports_generics()
122-
} else {
123-
// This is a monomorphization of a generic function
124-
// defined in an upstream crate. It is hidden if:
125-
// - it is instantiated in this crate, and
126-
// - the current crate does not re-export generics
127-
instance.upstream_monomorphization(tcx).is_none()
128-
&& !cx.tcx.local_crate_exports_generics()
129-
}
130-
}
131-
} else {
132-
// This is a non-generic function. It is hidden if:
133-
// - it is instantiated in the local crate, and
134-
// - it is defined an upstream crate (non-local), or
135-
// - it is not reachable
136-
cx.tcx.is_codegened_item(instance_def_id)
137-
&& (!instance_def_id.is_local()
138-
|| !cx.tcx.is_reachable_non_generic(instance_def_id))
139-
};
140-
if is_hidden {
141-
llvm::set_visibility(llfn, llvm::Visibility::Hidden);
142-
}
100+
let is_generic = instance.args.non_erasable_generics().next().is_some();
143101

144-
// MinGW: For backward compatibility we rely on the linker to decide whether it
145-
// should use dllimport for functions.
146-
if cx.use_dll_storage_attrs
147-
&& let Some(library) = tcx.native_library(instance_def_id)
148-
&& library.kind.is_dllimport()
149-
&& !matches!(tcx.sess.target.env.as_ref(), "gnu" | "uclibc")
102+
let is_hidden = if is_generic {
103+
// This is a monomorphization of a generic function.
104+
if !(cx.tcx.sess.opts.share_generics()
105+
|| tcx.codegen_fn_attrs(instance_def_id).inline
106+
== rustc_attr_parsing::InlineAttr::Never)
150107
{
151-
llvm::set_dllimport_storage_class(llfn);
108+
// When not sharing generics, all instances are in the same
109+
// crate and have hidden visibility.
110+
true
111+
} else {
112+
if let Some(instance_def_id) = instance_def_id.as_local() {
113+
// This is a monomorphization of a generic function
114+
// defined in the current crate. It is hidden if:
115+
// - the definition is unreachable for downstream
116+
// crates, or
117+
// - the current crate does not re-export generics
118+
// (because the crate is a C library or executable)
119+
cx.tcx.is_unreachable_local_definition(instance_def_id)
120+
|| !cx.tcx.local_crate_exports_generics()
121+
} else {
122+
// This is a monomorphization of a generic function
123+
// defined in an upstream crate. It is hidden if:
124+
// - it is instantiated in this crate, and
125+
// - the current crate does not re-export generics
126+
instance.upstream_monomorphization(tcx).is_none()
127+
&& !cx.tcx.local_crate_exports_generics()
128+
}
152129
}
130+
} else {
131+
// This is a non-generic function. It is hidden if:
132+
// - it is instantiated in the local crate, and
133+
// - it is defined an upstream crate (non-local), or
134+
// - it is not reachable
135+
cx.tcx.is_codegened_item(instance_def_id)
136+
&& (!instance_def_id.is_local()
137+
|| !cx.tcx.is_reachable_non_generic(instance_def_id))
138+
};
139+
if is_hidden {
140+
llvm::set_visibility(llfn, llvm::Visibility::Hidden);
141+
}
153142

154-
if cx.should_assume_dso_local(llfn, true) {
155-
llvm::LLVMRustSetDSOLocal(llfn, true);
156-
}
143+
// MinGW: For backward compatibility we rely on the linker to decide whether it
144+
// should use dllimport for functions.
145+
if cx.use_dll_storage_attrs
146+
&& let Some(library) = tcx.native_library(instance_def_id)
147+
&& library.kind.is_dllimport()
148+
&& !matches!(tcx.sess.target.env.as_ref(), "gnu" | "uclibc")
149+
{
150+
llvm::set_dllimport_storage_class(llfn);
157151
}
158152

153+
cx.assume_dso_local(llfn, true);
154+
159155
llfn
160156
};
161157

Diff for: compiler/rustc_codegen_llvm/src/consts.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,7 @@ impl<'ll> CodegenCx<'ll, '_> {
336336
llvm::set_thread_local_mode(g, self.tls_model);
337337
}
338338

339-
let dso_local = self.should_assume_dso_local(g, true);
340-
if dso_local {
341-
unsafe {
342-
llvm::LLVMRustSetDSOLocal(g, true);
343-
}
344-
}
339+
let dso_local = self.assume_dso_local(g, true);
345340

346341
if !def_id.is_local() {
347342
let needs_dll_storage_attr = self.use_dll_storage_attrs
@@ -456,9 +451,7 @@ impl<'ll> CodegenCx<'ll, '_> {
456451
set_global_alignment(self, g, alloc.align);
457452
llvm::set_initializer(g, v);
458453

459-
if self.should_assume_dso_local(g, true) {
460-
llvm::LLVMRustSetDSOLocal(g, true);
461-
}
454+
self.assume_dso_local(g, true);
462455

463456
// Forward the allocation's mutability (picked by the const interner) to LLVM.
464457
if alloc.mutability.is_not() {

Diff for: compiler/rustc_codegen_llvm/src/llvm/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,9 @@ pub(crate) fn set_dllimport_storage_class<'ll>(v: &'ll Value) {
409409
LLVMSetDLLStorageClass(v, DLLStorageClass::DllImport);
410410
}
411411
}
412+
413+
pub(crate) fn set_dso_local<'ll>(v: &'ll Value) {
414+
unsafe {
415+
LLVMRustSetDSOLocal(v, true);
416+
}
417+
}

Diff for: compiler/rustc_codegen_llvm/src/mono_item.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,7 @@ impl<'tcx> PreDefineCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
3838

3939
llvm::set_linkage(g, base::linkage_to_llvm(linkage));
4040
llvm::set_visibility(g, base::visibility_to_llvm(visibility));
41-
unsafe {
42-
if self.should_assume_dso_local(g, false) {
43-
llvm::LLVMRustSetDSOLocal(g, true);
44-
}
45-
}
41+
self.assume_dso_local(g, false);
4642

4743
self.instances.borrow_mut().insert(instance, g);
4844
}
@@ -79,9 +75,7 @@ impl<'tcx> PreDefineCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
7975

8076
debug!("predefine_fn: instance = {:?}", instance);
8177

82-
if self.should_assume_dso_local(lldecl, false) {
83-
unsafe { llvm::LLVMRustSetDSOLocal(lldecl, true) };
84-
}
78+
self.assume_dso_local(lldecl, false);
8579

8680
self.instances.borrow_mut().insert(instance, lldecl);
8781
}
@@ -90,11 +84,16 @@ impl<'tcx> PreDefineCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
9084
impl CodegenCx<'_, '_> {
9185
/// Whether a definition or declaration can be assumed to be local to a group of
9286
/// libraries that form a single DSO or executable.
93-
pub(crate) fn should_assume_dso_local(
94-
&self,
95-
llval: &llvm::Value,
96-
is_declaration: bool,
97-
) -> bool {
87+
/// Marks the local as DSO if so.
88+
pub(crate) fn assume_dso_local(&self, llval: &llvm::Value, is_declaration: bool) -> bool {
89+
let assume = self.should_assume_dso_local(llval, is_declaration);
90+
if assume {
91+
llvm::set_dso_local(llval);
92+
}
93+
assume
94+
}
95+
96+
fn should_assume_dso_local(&self, llval: &llvm::Value, is_declaration: bool) -> bool {
9897
let linkage = llvm::get_linkage(llval);
9998
let visibility = llvm::get_visibility(llval);
10099

0 commit comments

Comments
 (0)