]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_codegen_llvm/src/mono_item.rs
New upstream version 1.50.0+dfsg1
[rustc.git] / compiler / rustc_codegen_llvm / src / mono_item.rs
1 use crate::abi::FnAbi;
2 use crate::attributes;
3 use crate::base;
4 use crate::context::CodegenCx;
5 use crate::llvm;
6 use crate::type_of::LayoutLlvmExt;
7 use rustc_codegen_ssa::traits::*;
8 use rustc_hir::def_id::{DefId, LOCAL_CRATE};
9 pub use rustc_middle::mir::mono::MonoItem;
10 use rustc_middle::mir::mono::{Linkage, Visibility};
11 use rustc_middle::ty::layout::FnAbiExt;
12 use rustc_middle::ty::{self, Instance, TypeFoldable};
13 use rustc_target::abi::LayoutOf;
14 use tracing::debug;
15
16 impl PreDefineMethods<'tcx> for CodegenCx<'ll, 'tcx> {
17 fn predefine_static(
18 &self,
19 def_id: DefId,
20 linkage: Linkage,
21 visibility: Visibility,
22 symbol_name: &str,
23 ) {
24 let instance = Instance::mono(self.tcx, def_id);
25 let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
26 let llty = self.layout_of(ty).llvm_type(self);
27
28 let g = self.define_global(symbol_name, llty).unwrap_or_else(|| {
29 self.sess().span_fatal(
30 self.tcx.def_span(def_id),
31 &format!("symbol `{}` is already defined", symbol_name),
32 )
33 });
34
35 unsafe {
36 llvm::LLVMRustSetLinkage(g, base::linkage_to_llvm(linkage));
37 llvm::LLVMRustSetVisibility(g, base::visibility_to_llvm(visibility));
38 }
39
40 self.instances.borrow_mut().insert(instance, g);
41 }
42
43 fn predefine_fn(
44 &self,
45 instance: Instance<'tcx>,
46 linkage: Linkage,
47 visibility: Visibility,
48 symbol_name: &str,
49 ) {
50 assert!(!instance.substs.needs_infer());
51
52 let fn_abi = FnAbi::of_instance(self, instance, &[]);
53 let lldecl = self.declare_fn(symbol_name, &fn_abi);
54 unsafe { llvm::LLVMRustSetLinkage(lldecl, base::linkage_to_llvm(linkage)) };
55 let attrs = self.tcx.codegen_fn_attrs(instance.def_id());
56 base::set_link_section(lldecl, &attrs);
57 if linkage == Linkage::LinkOnceODR || linkage == Linkage::WeakODR {
58 llvm::SetUniqueComdat(self.llmod, lldecl);
59 }
60
61 // If we're compiling the compiler-builtins crate, e.g., the equivalent of
62 // compiler-rt, then we want to implicitly compile everything with hidden
63 // visibility as we're going to link this object all over the place but
64 // don't want the symbols to get exported.
65 if linkage != Linkage::Internal
66 && linkage != Linkage::Private
67 && self.tcx.is_compiler_builtins(LOCAL_CRATE)
68 {
69 unsafe {
70 llvm::LLVMRustSetVisibility(lldecl, llvm::Visibility::Hidden);
71 }
72 } else {
73 unsafe {
74 llvm::LLVMRustSetVisibility(lldecl, base::visibility_to_llvm(visibility));
75 }
76 }
77
78 debug!("predefine_fn: instance = {:?}", instance);
79
80 attributes::from_fn_attrs(self, lldecl, instance);
81
82 self.instances.borrow_mut().insert(instance, lldecl);
83 }
84 }