Skip to content

Commit fda530d

Browse files
committed
Streamline hidden visibility setting.
In `get_fn` there is a complicated set of if/elses to determine if `hidden` visibility should be applied. There are five calls to `LLVMRustSetVisibility` and some repetition in the comments. This commit streamlines it a bit: - Computes `hidden` and then uses it to determine if a single call to `LLVMRustSetVisibility` occurs. - Converts some of the if/elses into boolean expressions. - Removes the repetitive comments. Overall this makes it quite a bit shorter, and I find it easier to read.
1 parent eb57550 commit fda530d

File tree

1 file changed

+29
-49
lines changed

1 file changed

+29
-49
lines changed

compiler/rustc_codegen_llvm/src/callee.rs

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -101,62 +101,42 @@ pub(crate) fn get_fn<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'t
101101
let is_generic =
102102
instance.args.non_erasable_generics(tcx, instance.def_id()).next().is_some();
103103

104-
if is_generic {
105-
// This is a monomorphization. Its expected visibility depends
106-
// on whether we are in share-generics mode.
107-
108-
if cx.tcx.sess.opts.share_generics() {
109-
// We are in share_generics mode.
110-
104+
let is_hidden = if is_generic {
105+
// This is a monomorphization of a generic function.
106+
if !cx.tcx.sess.opts.share_generics() {
107+
// When not sharing generics, all instances are in the same
108+
// crate and have hidden visibility.
109+
true
110+
} else {
111111
if let Some(instance_def_id) = instance_def_id.as_local() {
112-
// This is a definition from the current crate. If the
113-
// definition is unreachable for downstream crates or
114-
// the current crate does not re-export generics, the
115-
// definition of the instance will have been declared
116-
// as `hidden`.
117-
if cx.tcx.is_unreachable_local_definition(instance_def_id)
112+
// This is a monomorphization of a generic function
113+
// defined in the current crate. It is hidden if:
114+
// - the definition is unreachable for downstream
115+
// crates, or
116+
// - the current crate does not re-export generics
117+
// (because the crate is a C library or executable)
118+
cx.tcx.is_unreachable_local_definition(instance_def_id)
118119
|| !cx.tcx.local_crate_exports_generics()
119-
{
120-
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
121-
}
122120
} else {
123121
// This is a monomorphization of a generic function
124-
// defined in an upstream crate.
125-
if instance.upstream_monomorphization(tcx).is_some() {
126-
// This is instantiated in another crate. It cannot
127-
// be `hidden`.
128-
} else {
129-
// This is a local instantiation of an upstream definition.
130-
// If the current crate does not re-export it
131-
// (because it is a C library or an executable), it
132-
// will have been declared `hidden`.
133-
if !cx.tcx.local_crate_exports_generics() {
134-
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
135-
}
136-
}
122+
// defined in an upstream crate. It is hidden if:
123+
// - it is instantiated in this crate, and
124+
// - the current crate does not re-export generics
125+
instance.upstream_monomorphization(tcx).is_none()
126+
&& !cx.tcx.local_crate_exports_generics()
137127
}
138-
} else {
139-
// When not sharing generics, all instances are in the same
140-
// crate and have hidden visibility
141-
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
142128
}
143129
} else {
144-
// This is a non-generic function
145-
if cx.tcx.is_codegened_item(instance_def_id) {
146-
// This is a function that is instantiated in the local crate
147-
148-
if instance_def_id.is_local() {
149-
// This is function that is defined in the local crate.
150-
// If it is not reachable, it is hidden.
151-
if !cx.tcx.is_reachable_non_generic(instance_def_id) {
152-
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
153-
}
154-
} else {
155-
// This is a function from an upstream crate that has
156-
// been instantiated here. These are always hidden.
157-
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
158-
}
159-
}
130+
// This is a non-generic function. It is hidden if:
131+
// - it is instantiated in the local crate, and
132+
// - it is defined an upstream crate (non-local), or
133+
// - it is not reachable
134+
cx.tcx.is_codegened_item(instance_def_id)
135+
&& (!instance_def_id.is_local()
136+
|| !cx.tcx.is_reachable_non_generic(instance_def_id))
137+
};
138+
if is_hidden {
139+
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
160140
}
161141

162142
// MinGW: For backward compatibility we rely on the linker to decide whether it

0 commit comments

Comments
 (0)