1use std::borrow::Borrow;
15
16use itertools::Itertools;
17use rustc_abi::AddressSpace;
18use rustc_codegen_ssa::traits::{MiscCodegenMethods, TypeMembershipCodegenMethods};
19use rustc_data_structures::fx::FxIndexSet;
20use rustc_middle::ty::{Instance, Ty};
21use rustc_sanitizers::{cfi, kcfi};
22use rustc_target::callconv::FnAbi;
23use smallvec::SmallVec;
24use tracing::debug;
25
26use crate::abi::FnAbiLlvmExt;
27use crate::attributes;
28use crate::common::AsCCharPtr;
29use crate::context::{CodegenCx, GenericCx, SCx, SimpleCx};
30use crate::llvm::AttributePlace::Function;
31use crate::llvm::{self, FromGeneric, Type, Value, Visibility};
32
33pub(crate) fn declare_simple_fn<'ll>(
38 cx: &SimpleCx<'ll>,
39 name: &str,
40 callconv: llvm::CallConv,
41 unnamed: llvm::UnnamedAddr,
42 visibility: llvm::Visibility,
43 ty: &'ll Type,
44) -> &'ll Value {
45 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/declare.rs:45",
"rustc_codegen_llvm::declare", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/declare.rs"),
::tracing_core::__macro_support::Option::Some(45u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::declare"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("declare_simple_fn(name={0:?}, ty={1:?})",
name, ty) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("declare_simple_fn(name={:?}, ty={:?})", name, ty);
46 let llfn = unsafe {
47 llvm::LLVMRustGetOrInsertFunction(cx.llmod, name.as_c_char_ptr(), name.len(), ty)
48 };
49
50 llvm::SetFunctionCallConv(llfn, callconv);
51 llvm::set_unnamed_address(llfn, unnamed);
52 llvm::set_visibility(llfn, visibility);
53
54 llfn
55}
56
57pub(crate) fn declare_raw_fn<'ll, 'tcx>(
62 cx: &CodegenCx<'ll, 'tcx>,
63 name: &str,
64 callconv: llvm::CallConv,
65 unnamed: llvm::UnnamedAddr,
66 visibility: llvm::Visibility,
67 ty: &'ll Type,
68) -> &'ll Value {
69 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/declare.rs:69",
"rustc_codegen_llvm::declare", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/declare.rs"),
::tracing_core::__macro_support::Option::Some(69u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::declare"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("declare_raw_fn(name={0:?}, ty={1:?})",
name, ty) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("declare_raw_fn(name={:?}, ty={:?})", name, ty);
70 let llfn = declare_simple_fn(cx, name, callconv, unnamed, visibility, ty);
71
72 let mut attrs = SmallVec::<[_; 4]>::new();
73
74 if cx.sess().target.is_like_gpu {
75 attrs.push(llvm::AttributeKind::Convergent.create_attr(cx.llcx));
79 }
80
81 if cx.tcx.sess.opts.cg.no_redzone.unwrap_or(cx.tcx.sess.target.disable_redzone) {
82 attrs.push(llvm::AttributeKind::NoRedZone.create_attr(cx.llcx));
83 }
84
85 attrs.extend(attributes::non_lazy_bind_attr(cx, cx.tcx.sess));
86
87 attributes::apply_to_llfn(llfn, Function, &attrs);
88
89 llfn
90}
91
92impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
93 pub(crate) fn declare_global(&self, name: &str, ty: &'ll Type) -> &'ll Value {
98 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/declare.rs:98",
"rustc_codegen_llvm::declare", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/declare.rs"),
::tracing_core::__macro_support::Option::Some(98u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::declare"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("declare_global(name={0:?})",
name) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("declare_global(name={:?})", name);
99 unsafe {
100 llvm::LLVMRustGetOrInsertGlobal(
101 (**self).borrow().llmod,
102 name.as_c_char_ptr(),
103 name.len(),
104 ty,
105 )
106 }
107 }
108
109 pub(crate) fn declare_global_in_addrspace(
114 &self,
115 name: &str,
116 ty: &'ll Type,
117 addr_space: AddressSpace,
118 ) -> &'ll Value {
119 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/declare.rs:119",
"rustc_codegen_llvm::declare", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/declare.rs"),
::tracing_core::__macro_support::Option::Some(119u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::declare"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("declare_global(name={0:?}, addrspace={1:?})",
name, addr_space) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("declare_global(name={name:?}, addrspace={addr_space:?})");
120 unsafe {
121 llvm::LLVMRustGetOrInsertGlobalInAddrspace(
122 (**self).borrow().llmod,
123 name.as_c_char_ptr(),
124 name.len(),
125 ty,
126 addr_space.0,
127 )
128 }
129 }
130}
131
132impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
133 pub(crate) fn declare_cfn(
141 &self,
142 name: &str,
143 unnamed: llvm::UnnamedAddr,
144 fn_type: &'ll Type,
145 ) -> &'ll Value {
146 declare_raw_fn(self, name, llvm::CCallConv, unnamed, Visibility::Default, fn_type)
149 }
150
151 pub(crate) fn declare_entry_fn(
159 &self,
160 name: &str,
161 callconv: llvm::CallConv,
162 unnamed: llvm::UnnamedAddr,
163 fn_type: &'ll Type,
164 ) -> &'ll Value {
165 let visibility = Visibility::from_generic(self.tcx.sess.default_visibility());
166 declare_raw_fn(self, name, callconv, unnamed, visibility, fn_type)
167 }
168
169 pub(crate) fn declare_fn(
174 &self,
175 name: &str,
176 fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
177 instance: Option<Instance<'tcx>>,
178 ) -> &'ll Value {
179 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/declare.rs:179",
"rustc_codegen_llvm::declare", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/declare.rs"),
::tracing_core::__macro_support::Option::Some(179u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::declare"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("declare_rust_fn(name={0:?}, fn_abi={1:?})",
name, fn_abi) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("declare_rust_fn(name={:?}, fn_abi={:?})", name, fn_abi);
180
181 let llfn = declare_raw_fn(
184 self,
185 name,
186 fn_abi.llvm_cconv(self),
187 llvm::UnnamedAddr::Global,
188 llvm::Visibility::Default,
189 fn_abi.llvm_type(self),
190 );
191 fn_abi.apply_attrs_llfn(self, llfn, instance);
192
193 if self.tcx.sess.is_sanitizer_cfi_enabled() {
194 if let Some(instance) = instance {
195 let mut typeids = FxIndexSet::default();
196 for options in [
197 cfi::TypeIdOptions::GENERALIZE_POINTERS,
198 cfi::TypeIdOptions::NORMALIZE_INTEGERS,
199 cfi::TypeIdOptions::USE_CONCRETE_SELF,
200 ]
201 .into_iter()
202 .powerset()
203 .map(cfi::TypeIdOptions::from_iter)
204 {
205 let typeid = cfi::typeid_for_instance(self.tcx, instance, options);
206 if typeids.insert(typeid.clone()) {
207 self.add_type_metadata(llfn, typeid.as_bytes());
208 }
209 }
210 } else {
211 for options in [
212 cfi::TypeIdOptions::GENERALIZE_POINTERS,
213 cfi::TypeIdOptions::NORMALIZE_INTEGERS,
214 ]
215 .into_iter()
216 .powerset()
217 .map(cfi::TypeIdOptions::from_iter)
218 {
219 let typeid = cfi::typeid_for_fnabi(self.tcx, fn_abi, options);
220 self.add_type_metadata(llfn, typeid.as_bytes());
221 }
222 }
223 }
224
225 if self.tcx.sess.is_sanitizer_kcfi_enabled() {
226 let mut options = kcfi::TypeIdOptions::empty();
228 if self.tcx.sess.is_sanitizer_cfi_generalize_pointers_enabled() {
229 options.insert(kcfi::TypeIdOptions::GENERALIZE_POINTERS);
230 }
231 if self.tcx.sess.is_sanitizer_cfi_normalize_integers_enabled() {
232 options.insert(kcfi::TypeIdOptions::NORMALIZE_INTEGERS);
233 }
234
235 if let Some(instance) = instance {
236 let kcfi_typeid = kcfi::typeid_for_instance(self.tcx, instance, options);
237 self.set_kcfi_type_metadata(llfn, kcfi_typeid);
238 } else {
239 let kcfi_typeid = kcfi::typeid_for_fnabi(self.tcx, fn_abi, options);
240 self.set_kcfi_type_metadata(llfn, kcfi_typeid);
241 }
242 }
243
244 llfn
245 }
246}
247
248impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
249 pub(crate) fn define_global(&self, name: &str, ty: &'ll Type) -> Option<&'ll Value> {
256 if self.get_defined_value(name).is_some() {
257 None
258 } else {
259 Some(self.declare_global(name, ty))
260 }
261 }
262
263 pub(crate) fn get_declared_value(&self, name: &str) -> Option<&'ll Value> {
265 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/declare.rs:265",
"rustc_codegen_llvm::declare", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/declare.rs"),
::tracing_core::__macro_support::Option::Some(265u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::declare"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("get_declared_value(name={0:?})",
name) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("get_declared_value(name={:?})", name);
266 unsafe { llvm::LLVMRustGetNamedValue(self.llmod(), name.as_c_char_ptr(), name.len()) }
267 }
268
269 pub(crate) fn get_defined_value(&self, name: &str) -> Option<&'ll Value> {
272 self.get_declared_value(name).and_then(|val| {
273 let declaration = llvm::is_declaration(val);
274 if !declaration { Some(val) } else { None }
275 })
276 }
277}