2025-06-15 03:57:44 +05:30
|
|
|
use std::borrow::{Borrow, Cow};
|
2020-03-11 12:49:08 +01:00
|
|
|
use std::cell::{Cell, RefCell};
|
2025-01-01 21:42:45 +01:00
|
|
|
use std::ffi::{CStr, c_char, c_uint};
|
2025-02-24 08:15:31 +00:00
|
|
|
use std::marker::PhantomData;
|
2024-12-13 09:52:38 +00:00
|
|
|
use std::ops::{Deref, DerefMut};
|
2019-02-18 03:58:58 +09:00
|
|
|
use std::str;
|
2017-09-20 23:07:47 +03:00
|
|
|
|
2025-02-24 11:31:43 +00:00
|
|
|
use rustc_abi::{HasDataLayout, Size, TargetDataLayout, VariantIdx};
|
2024-11-01 17:07:18 +01:00
|
|
|
use rustc_codegen_ssa::back::versioned_llvm_target;
|
2023-05-01 19:24:21 +00:00
|
|
|
use rustc_codegen_ssa::base::{wants_msvc_seh, wants_wasm_eh};
|
2023-08-28 12:40:39 -07:00
|
|
|
use rustc_codegen_ssa::errors as ssa_errors;
|
2020-03-11 12:49:08 +01:00
|
|
|
use rustc_codegen_ssa::traits::*;
|
2024-04-03 14:09:21 -04:00
|
|
|
use rustc_data_structures::base_n::{ALPHANUMERIC_ONLY, ToBaseN};
|
2019-12-24 05:02:53 +01:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2018-08-07 16:04:34 +02:00
|
|
|
use rustc_data_structures::small_c_str::SmallCStr;
|
2022-03-01 00:53:25 +00:00
|
|
|
use rustc_hir::def_id::DefId;
|
2024-08-16 20:33:58 +00:00
|
|
|
use rustc_middle::middle::codegen_fn_attrs::PatchableFunctionEntry;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::mono::CodegenUnit;
|
2021-09-02 00:09:34 +03:00
|
|
|
use rustc_middle::ty::layout::{
|
2024-11-15 13:53:31 +01:00
|
|
|
FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasTypingEnv, LayoutError, LayoutOfHelpers,
|
2021-09-02 00:09:34 +03:00
|
|
|
};
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
|
2021-08-30 18:01:58 +03:00
|
|
|
use rustc_middle::{bug, span_bug};
|
2020-03-11 12:49:08 +01:00
|
|
|
use rustc_session::Session;
|
2022-01-28 09:48:59 -08:00
|
|
|
use rustc_session::config::{
|
2024-09-25 10:23:30 +02:00
|
|
|
BranchProtection, CFGuard, CFProtection, CrateType, DebugInfo, FunctionReturn, PAuthKey, PacRet,
|
2022-01-28 09:48:59 -08:00
|
|
|
};
|
2022-11-04 20:08:01 +01:00
|
|
|
use rustc_span::source_map::Spanned;
|
2024-07-01 16:32:32 -04:00
|
|
|
use rustc_span::{DUMMY_SP, Span};
|
2023-12-02 14:17:33 +00:00
|
|
|
use rustc_symbol_mangling::mangle_internal_symbol;
|
2024-09-10 12:19:16 -07:00
|
|
|
use rustc_target::spec::{HasTargetSpec, RelocModel, SmallDataThresholdSupport, Target, TlsModel};
|
2021-08-09 12:25:33 +00:00
|
|
|
use smallvec::SmallVec;
|
2020-03-11 12:49:08 +01:00
|
|
|
|
2021-03-12 08:35:13 +09:00
|
|
|
use crate::back::write::to_llvm_code_model;
|
2022-05-29 01:10:44 +02:00
|
|
|
use crate::callee::get_fn;
|
|
|
|
|
use crate::debuginfo::metadata::apply_vcall_visibility_metadata;
|
2025-02-24 14:38:55 +00:00
|
|
|
use crate::llvm::Metadata;
|
2024-01-17 14:26:26 +00:00
|
|
|
use crate::type_::Type;
|
2013-12-18 18:35:33 -08:00
|
|
|
use crate::value::Value;
|
2025-04-04 14:24:23 -04:00
|
|
|
use crate::{attributes, common, coverageinfo, debuginfo, llvm, llvm_util};
|
2013-06-13 14:49:01 +12:00
|
|
|
|
2025-01-24 16:05:26 -05:00
|
|
|
/// `TyCtxt` (and related cache datastructures) can't be move between threads.
|
|
|
|
|
/// However, there are various cx related functions which we want to be available to the builder and
|
|
|
|
|
/// other compiler pieces. Here we define a small subset which has enough information and can be
|
|
|
|
|
/// moved around more freely.
|
2025-02-24 08:15:31 +00:00
|
|
|
pub(crate) struct SCx<'ll> {
|
2025-01-24 16:05:26 -05:00
|
|
|
pub llmod: &'ll llvm::Module,
|
|
|
|
|
pub llcx: &'ll llvm::Context,
|
2025-02-24 11:31:43 +00:00
|
|
|
pub isize_ty: &'ll Type,
|
2025-01-24 16:05:26 -05:00
|
|
|
}
|
|
|
|
|
|
2025-02-24 08:15:31 +00:00
|
|
|
impl<'ll> Borrow<SCx<'ll>> for FullCx<'ll, '_> {
|
|
|
|
|
fn borrow(&self) -> &SCx<'ll> {
|
2025-01-24 16:05:26 -05:00
|
|
|
&self.scx
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-24 08:15:31 +00:00
|
|
|
impl<'ll, 'tcx> Deref for FullCx<'ll, 'tcx> {
|
2025-01-24 16:05:26 -05:00
|
|
|
type Target = SimpleCx<'ll>;
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
|
&self.scx
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-24 08:15:31 +00:00
|
|
|
pub(crate) struct GenericCx<'ll, T: Borrow<SCx<'ll>>>(T, PhantomData<SCx<'ll>>);
|
|
|
|
|
|
|
|
|
|
impl<'ll, T: Borrow<SCx<'ll>>> Deref for GenericCx<'ll, T> {
|
|
|
|
|
type Target = T;
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
|
&self.0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-13 09:52:38 +00:00
|
|
|
impl<'ll, T: Borrow<SCx<'ll>>> DerefMut for GenericCx<'ll, T> {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
|
&mut self.0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-24 08:15:31 +00:00
|
|
|
pub(crate) type SimpleCx<'ll> = GenericCx<'ll, SCx<'ll>>;
|
|
|
|
|
|
2024-09-18 12:45:28 +10:00
|
|
|
/// There is one `CodegenCx` per codegen unit. Each one has its own LLVM
|
|
|
|
|
/// `llvm::Context` so that several codegen units may be processed in parallel.
|
2018-06-27 17:57:25 +03:00
|
|
|
/// All other LLVM data structures in the `CodegenCx` are tied to that `llvm::Context`.
|
2025-02-24 08:15:31 +00:00
|
|
|
pub(crate) type CodegenCx<'ll, 'tcx> = GenericCx<'ll, FullCx<'ll, 'tcx>>;
|
|
|
|
|
|
|
|
|
|
pub(crate) struct FullCx<'ll, 'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
pub tcx: TyCtxt<'tcx>,
|
2025-01-24 16:05:26 -05:00
|
|
|
pub scx: SimpleCx<'ll>,
|
2018-01-05 06:58:34 +02:00
|
|
|
pub use_dll_storage_attrs: bool,
|
|
|
|
|
pub tls_model: llvm::ThreadLocalMode,
|
2014-07-16 11:27:57 -07:00
|
|
|
|
2020-03-14 12:41:32 +01:00
|
|
|
pub codegen_unit: &'tcx CodegenUnit<'tcx>,
|
2017-07-12 17:37:58 +02:00
|
|
|
|
2016-02-23 22:04:27 +02:00
|
|
|
/// Cache instances of monomorphic and polymorphic items
|
2018-11-16 13:48:26 +02:00
|
|
|
pub instances: RefCell<FxHashMap<Instance<'tcx>, &'ll Value>>,
|
2014-04-01 07:11:23 -04:00
|
|
|
/// Cache generated vtables
|
2025-01-10 20:26:10 +00:00
|
|
|
pub vtables: RefCell<FxHashMap<(Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>), &'ll Value>>,
|
2014-04-01 07:11:23 -04:00
|
|
|
/// Cache of constant strings,
|
2022-06-28 17:34:24 +00:00
|
|
|
pub const_str_cache: RefCell<FxHashMap<String, &'ll Value>>,
|
2013-06-13 14:02:33 +12:00
|
|
|
|
2015-01-29 14:03:34 +02:00
|
|
|
/// Cache of emitted const globals (value -> global)
|
2018-11-16 13:48:26 +02:00
|
|
|
pub const_globals: RefCell<FxHashMap<&'ll Value, &'ll Value>>,
|
2013-06-13 14:02:33 +12:00
|
|
|
|
2015-06-28 10:36:46 -07:00
|
|
|
/// List of globals for static variables which need to be passed to the
|
2018-05-08 16:10:16 +03:00
|
|
|
/// LLVM function ReplaceAllUsesWith (RAUW) when codegen is complete.
|
2018-07-10 13:28:39 +03:00
|
|
|
/// (We have to make sure we don't invalidate any Values referring
|
2015-06-28 10:36:46 -07:00
|
|
|
/// to constants.)
|
2018-11-16 13:48:26 +02:00
|
|
|
pub statics_to_rauw: RefCell<Vec<(&'ll Value, &'ll Value)>>,
|
2015-06-28 10:36:46 -07:00
|
|
|
|
2021-08-20 21:13:18 +02:00
|
|
|
/// Statics that will be placed in the llvm.used variable
|
|
|
|
|
/// See <https://llvm.org/docs/LangRef.html#the-llvm-used-global-variable> for details
|
2024-12-13 09:52:38 +00:00
|
|
|
pub used_statics: Vec<&'ll Value>,
|
2021-08-20 21:13:18 +02:00
|
|
|
|
2021-08-07 17:04:32 +02:00
|
|
|
/// Statics that will be placed in the llvm.compiler.used variable
|
|
|
|
|
/// See <https://llvm.org/docs/LangRef.html#the-llvm-compiler-used-global-variable> for details
|
2024-12-13 09:52:38 +00:00
|
|
|
pub compiler_used_statics: Vec<&'ll Value>,
|
2017-02-20 14:42:47 -05:00
|
|
|
|
2024-03-08 01:40:24 +00:00
|
|
|
/// Mapping of non-scalar types to llvm types.
|
|
|
|
|
pub type_lowering: RefCell<FxHashMap<(Ty<'tcx>, Option<VariantIdx>), &'ll Type>>,
|
2021-08-05 19:14:55 +02:00
|
|
|
|
|
|
|
|
/// Mapping of scalar types to llvm types.
|
2018-09-26 16:01:43 +02:00
|
|
|
pub scalar_lltypes: RefCell<FxHashMap<Ty<'tcx>, &'ll Type>>,
|
2021-08-05 19:14:55 +02:00
|
|
|
|
2024-11-23 13:24:57 +11:00
|
|
|
/// Extra per-CGU codegen state needed when coverage instrumentation is enabled.
|
|
|
|
|
pub coverage_cx: Option<coverageinfo::CguCoverageContext<'ll, 'tcx>>,
|
2022-03-03 11:15:25 +01:00
|
|
|
pub dbg_cx: Option<debuginfo::CodegenUnitDebugContext<'ll, 'tcx>>,
|
2014-04-09 19:56:31 -04:00
|
|
|
|
2018-11-16 13:48:26 +02:00
|
|
|
eh_personality: Cell<Option<&'ll Value>>,
|
2020-03-21 07:50:38 +00:00
|
|
|
eh_catch_typeinfo: Cell<Option<&'ll Value>>,
|
2021-08-03 15:09:57 -07:00
|
|
|
pub rust_try_fn: Cell<Option<(&'ll Type, &'ll Value)>>,
|
2014-05-19 09:30:09 -07:00
|
|
|
|
2025-05-30 21:41:36 +05:30
|
|
|
intrinsics:
|
2025-06-15 03:57:44 +05:30
|
|
|
RefCell<FxHashMap<(Cow<'static, str>, SmallVec<[&'ll Type; 2]>), (&'ll Type, &'ll Value)>>,
|
2014-07-21 16:42:34 -07:00
|
|
|
|
2016-10-21 12:41:20 -04:00
|
|
|
/// A counter that is used for generating local symbol names
|
|
|
|
|
local_gen_sym_counter: Cell<usize>,
|
2022-03-01 00:53:25 +00:00
|
|
|
|
|
|
|
|
/// `codegen_static` will sometimes create a second global variable with a
|
|
|
|
|
/// different type and clear the symbol name of the original global.
|
|
|
|
|
/// `global_asm!` needs to be able to find this new global so that it can
|
|
|
|
|
/// compute the correct mangled symbol name to insert into the asm.
|
|
|
|
|
pub renamed_statics: RefCell<FxHashMap<DefId, &'ll Value>>,
|
run optimization and codegen on worker threads
Refactor the code in `llvm::back` that invokes LLVM optimization and codegen
passes so that it can be called from worker threads. (Previously, it used
`&Session` extensively, and `Session` is not `Share`.) The new code can handle
multiple compilation units, by compiling each unit to `crate.0.o`, `crate.1.o`,
etc., and linking together all the `crate.N.o` files into a single `crate.o`
using `ld -r`. The later linking steps can then be run unchanged.
The new code preserves the behavior of `--emit`/`-o` when building a single
compilation unit. With multiple compilation units, the `--emit=asm/ir/bc`
options produce multiple files, so combinations like `--emit=ir -o foo.ll` will
not actually produce `foo.ll` (they instead produce several `foo.N.ll` files).
The new code supports `-Z lto` only when using a single compilation unit.
Compiling with multiple compilation units and `-Z lto` will produce an error.
(I can't think of any good reason to do such a thing.) Linking with `-Z lto`
against a library that was built as multiple compilation units will also fail,
because the rlib does not contain a `crate.bytecode.deflate` file. This could
be supported in the future by linking together the `crate.N.bc` files produced
when compiling the library into a single `crate.bc`, or by making the LTO code
support multiple `crate.N.bytecode.deflate` files.
2014-07-17 10:52:52 -07:00
|
|
|
}
|
|
|
|
|
|
2020-04-25 21:45:21 +03:00
|
|
|
fn to_llvm_tls_model(tls_model: TlsModel) -> llvm::ThreadLocalMode {
|
|
|
|
|
match tls_model {
|
|
|
|
|
TlsModel::GeneralDynamic => llvm::ThreadLocalMode::GeneralDynamic,
|
|
|
|
|
TlsModel::LocalDynamic => llvm::ThreadLocalMode::LocalDynamic,
|
|
|
|
|
TlsModel::InitialExec => llvm::ThreadLocalMode::InitialExec,
|
|
|
|
|
TlsModel::LocalExec => llvm::ThreadLocalMode::LocalExec,
|
2023-11-13 20:48:23 +08:00
|
|
|
TlsModel::Emulated => llvm::ThreadLocalMode::GeneralDynamic,
|
2017-10-31 18:24:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-06 22:26:42 +10:00
|
|
|
pub(crate) unsafe fn create_module<'ll>(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'_>,
|
2018-07-17 18:26:58 +03:00
|
|
|
llcx: &'ll llvm::Context,
|
|
|
|
|
mod_name: &str,
|
|
|
|
|
) -> &'ll llvm::Module {
|
2018-10-27 15:29:06 +03:00
|
|
|
let sess = tcx.sess;
|
2018-08-07 16:04:34 +02:00
|
|
|
let mod_name = SmallCStr::new(mod_name);
|
2024-07-14 14:27:57 -04:00
|
|
|
let llmod = unsafe { llvm::LLVMModuleCreateWithNameInContext(mod_name.as_ptr(), llcx) };
|
2014-11-25 13:28:35 -08:00
|
|
|
|
2025-07-09 09:36:19 +00:00
|
|
|
let cx = SimpleCx::new(llmod, llcx, tcx.data_layout.pointer_size());
|
|
|
|
|
|
2022-03-22 11:43:05 +01:00
|
|
|
let mut target_data_layout = sess.target.data_layout.to_string();
|
2022-02-02 10:01:02 +01:00
|
|
|
let llvm_version = llvm_util::get_version();
|
2019-07-07 18:41:28 +02:00
|
|
|
|
2024-10-15 20:56:20 +00:00
|
|
|
if llvm_version < (20, 0, 0) {
|
|
|
|
|
if sess.target.arch == "aarch64" || sess.target.arch.starts_with("arm64") {
|
|
|
|
|
// LLVM 20 defines three additional address spaces for alternate
|
|
|
|
|
// pointer kinds used in Windows.
|
|
|
|
|
// See https://github.com/llvm/llvm-project/pull/111879
|
|
|
|
|
target_data_layout =
|
|
|
|
|
target_data_layout.replace("-p270:32:32-p271:32:32-p272:64:64", "");
|
|
|
|
|
}
|
2024-10-31 20:35:24 +00:00
|
|
|
if sess.target.arch.starts_with("sparc") {
|
|
|
|
|
// LLVM 20 updates the sparc layout to correctly align 128 bit integers to 128 bit.
|
|
|
|
|
// See https://github.com/llvm/llvm-project/pull/106951
|
|
|
|
|
target_data_layout = target_data_layout.replace("-i128:128", "");
|
|
|
|
|
}
|
2024-11-07 20:59:50 +01:00
|
|
|
if sess.target.arch.starts_with("mips64") {
|
|
|
|
|
// LLVM 20 updates the mips64 layout to correctly align 128 bit integers to 128 bit.
|
|
|
|
|
// See https://github.com/llvm/llvm-project/pull/112084
|
|
|
|
|
target_data_layout = target_data_layout.replace("-i128:128", "");
|
|
|
|
|
}
|
2024-12-10 04:54:12 -05:00
|
|
|
if sess.target.arch.starts_with("powerpc64") {
|
|
|
|
|
// LLVM 20 updates the powerpc64 layout to correctly align 128 bit integers to 128 bit.
|
|
|
|
|
// See https://github.com/llvm/llvm-project/pull/118004
|
|
|
|
|
target_data_layout = target_data_layout.replace("-i128:128", "");
|
|
|
|
|
}
|
2024-12-11 04:14:16 -05:00
|
|
|
if sess.target.arch.starts_with("wasm32") || sess.target.arch.starts_with("wasm64") {
|
|
|
|
|
// LLVM 20 updates the wasm(32|64) layout to correctly align 128 bit integers to 128 bit.
|
|
|
|
|
// See https://github.com/llvm/llvm-project/pull/119204
|
|
|
|
|
target_data_layout = target_data_layout.replace("-i128:128", "");
|
|
|
|
|
}
|
2024-10-15 20:56:20 +00:00
|
|
|
}
|
2025-02-04 10:35:05 -05:00
|
|
|
if llvm_version < (21, 0, 0) {
|
|
|
|
|
if sess.target.arch == "nvptx64" {
|
|
|
|
|
// LLVM 21 updated the default layout on nvptx: https://github.com/llvm/llvm-project/pull/124961
|
|
|
|
|
target_data_layout = target_data_layout.replace("e-p6:32:32-i64", "e-i64");
|
|
|
|
|
}
|
2025-07-09 14:18:37 +02:00
|
|
|
if sess.target.arch == "amdgpu" {
|
|
|
|
|
// LLVM 21 adds the address width for address space 8.
|
|
|
|
|
// See https://github.com/llvm/llvm-project/pull/139419
|
|
|
|
|
target_data_layout = target_data_layout.replace("p8:128:128:128:48", "p8:128:128")
|
|
|
|
|
}
|
2025-02-04 10:35:05 -05:00
|
|
|
}
|
2025-08-11 11:19:47 +02:00
|
|
|
if llvm_version < (22, 0, 0) {
|
|
|
|
|
if sess.target.arch == "avr" {
|
|
|
|
|
// LLVM 22.0 updated the default layout on avr: https://github.com/llvm/llvm-project/pull/153010
|
|
|
|
|
target_data_layout = target_data_layout.replace("n8:16", "n8")
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-15 20:56:20 +00:00
|
|
|
|
2016-04-18 15:38:45 +03:00
|
|
|
// Ensure the data-layout values hardcoded remain the defaults.
|
2024-01-17 14:26:26 +00:00
|
|
|
{
|
2025-07-02 16:35:57 -07:00
|
|
|
let tm = crate::back::write::create_informational_target_machine(sess, false);
|
2024-07-14 14:27:57 -04:00
|
|
|
unsafe {
|
2025-02-04 12:20:09 +00:00
|
|
|
llvm::LLVMRustSetDataLayoutFromTargetMachine(llmod, tm.raw());
|
2024-07-14 14:27:57 -04:00
|
|
|
}
|
2016-04-18 15:38:45 +03:00
|
|
|
|
2024-07-14 14:27:57 -04:00
|
|
|
let llvm_data_layout = unsafe { llvm::LLVMGetDataLayoutStr(llmod) };
|
|
|
|
|
let llvm_data_layout =
|
|
|
|
|
str::from_utf8(unsafe { CStr::from_ptr(llvm_data_layout) }.to_bytes())
|
|
|
|
|
.expect("got a non-UTF8 data-layout from LLVM");
|
2016-04-18 15:38:45 +03:00
|
|
|
|
2024-01-17 14:26:26 +00:00
|
|
|
if target_data_layout != llvm_data_layout {
|
|
|
|
|
tcx.dcx().emit_err(crate::errors::MismatchedDataLayout {
|
|
|
|
|
rustc_target: sess.opts.target_triple.to_string().as_str(),
|
|
|
|
|
rustc_layout: target_data_layout.as_str(),
|
|
|
|
|
llvm_target: sess.target.llvm_target.borrow(),
|
|
|
|
|
llvm_layout: llvm_data_layout,
|
|
|
|
|
});
|
2016-04-18 15:38:45 +03:00
|
|
|
}
|
2015-07-16 15:48:16 -07:00
|
|
|
}
|
2014-11-25 13:28:35 -08:00
|
|
|
|
2019-07-07 18:41:28 +02:00
|
|
|
let data_layout = SmallCStr::new(&target_data_layout);
|
2024-07-14 14:27:57 -04:00
|
|
|
unsafe {
|
|
|
|
|
llvm::LLVMSetDataLayout(llmod, data_layout.as_ptr());
|
|
|
|
|
}
|
2016-04-18 15:38:45 +03:00
|
|
|
|
2024-11-01 17:07:18 +01:00
|
|
|
let llvm_target = SmallCStr::new(&versioned_llvm_target(sess));
|
2024-07-14 14:27:57 -04:00
|
|
|
unsafe {
|
|
|
|
|
llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
|
|
|
|
|
}
|
2016-07-14 23:40:14 +02:00
|
|
|
|
2021-09-10 15:11:56 +02:00
|
|
|
let reloc_model = sess.relocation_model();
|
|
|
|
|
if matches!(reloc_model, RelocModel::Pic | RelocModel::Pie) {
|
2024-07-14 14:27:57 -04:00
|
|
|
unsafe {
|
|
|
|
|
llvm::LLVMRustSetModulePICLevel(llmod);
|
|
|
|
|
}
|
2020-05-21 20:53:41 +03:00
|
|
|
// PIE is potentially more effective than PIC, but can only be used in executables.
|
|
|
|
|
// If all our outputs are executables, then we can relax PIC to PIE.
|
2021-09-10 15:11:56 +02:00
|
|
|
if reloc_model == RelocModel::Pie
|
2023-08-08 18:28:20 +08:00
|
|
|
|| tcx.crate_types().iter().all(|ty| *ty == CrateType::Executable)
|
2021-09-10 15:11:56 +02:00
|
|
|
{
|
2024-07-14 14:27:57 -04:00
|
|
|
unsafe {
|
|
|
|
|
llvm::LLVMRustSetModulePIELevel(llmod);
|
|
|
|
|
}
|
2020-05-21 20:53:41 +03:00
|
|
|
}
|
2016-07-14 23:40:14 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-12 08:35:13 +09:00
|
|
|
// Linking object files with different code models is undefined behavior
|
|
|
|
|
// because the compiler would have to generate additional code (to span
|
|
|
|
|
// longer jumps) if a larger code model is used with a smaller one.
|
|
|
|
|
//
|
|
|
|
|
// See https://reviews.llvm.org/D52322 and https://reviews.llvm.org/D52323.
|
2024-07-14 14:27:57 -04:00
|
|
|
unsafe {
|
|
|
|
|
llvm::LLVMRustSetModuleCodeModel(llmod, to_llvm_code_model(sess.code_model()));
|
|
|
|
|
}
|
2021-03-12 08:35:13 +09:00
|
|
|
|
2018-09-26 19:19:55 +03:00
|
|
|
// If skipping the PLT is enabled, we need to add some module metadata
|
|
|
|
|
// to ensure intrinsic calls don't use it.
|
|
|
|
|
if !sess.needs_plt() {
|
2024-10-29 13:38:17 +11:00
|
|
|
llvm::add_module_flag_u32(llmod, llvm::ModuleFlagMergeBehavior::Warning, "RtLibUseGOT", 1);
|
2018-09-26 19:19:55 +03:00
|
|
|
}
|
|
|
|
|
|
2022-12-12 22:42:44 -08:00
|
|
|
// Enable canonical jump tables if CFI is enabled. (See https://reviews.llvm.org/D65629.)
|
|
|
|
|
if sess.is_sanitizer_cfi_canonical_jump_tables_enabled() && sess.is_sanitizer_cfi_enabled() {
|
2024-10-29 13:38:17 +11:00
|
|
|
llvm::add_module_flag_u32(
|
|
|
|
|
llmod,
|
|
|
|
|
llvm::ModuleFlagMergeBehavior::Override,
|
|
|
|
|
"CFI Canonical Jump Tables",
|
|
|
|
|
1,
|
|
|
|
|
);
|
2021-10-07 15:33:13 -07:00
|
|
|
}
|
|
|
|
|
|
2024-08-16 20:33:58 +00:00
|
|
|
// If we're normalizing integers with CFI, ensure LLVM generated functions do the same.
|
|
|
|
|
// See https://github.com/llvm/llvm-project/pull/104826
|
|
|
|
|
if sess.is_sanitizer_cfi_normalize_integers_enabled() {
|
2024-10-29 13:38:17 +11:00
|
|
|
llvm::add_module_flag_u32(
|
|
|
|
|
llmod,
|
|
|
|
|
llvm::ModuleFlagMergeBehavior::Override,
|
|
|
|
|
"cfi-normalize-integers",
|
|
|
|
|
1,
|
|
|
|
|
);
|
2024-08-16 20:33:58 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-18 13:34:49 +10:00
|
|
|
// Enable LTO unit splitting if specified or if CFI is enabled. (See
|
|
|
|
|
// https://reviews.llvm.org/D53891.)
|
2022-12-12 22:42:44 -08:00
|
|
|
if sess.is_split_lto_unit_enabled() || sess.is_sanitizer_cfi_enabled() {
|
2024-10-29 13:38:17 +11:00
|
|
|
llvm::add_module_flag_u32(
|
|
|
|
|
llmod,
|
|
|
|
|
llvm::ModuleFlagMergeBehavior::Override,
|
|
|
|
|
"EnableSplitLTOUnit",
|
|
|
|
|
1,
|
|
|
|
|
);
|
2022-12-12 22:42:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add "kcfi" module flag if KCFI is enabled. (See https://reviews.llvm.org/D119296.)
|
2022-11-21 21:29:00 -08:00
|
|
|
if sess.is_sanitizer_kcfi_enabled() {
|
2024-10-29 13:38:17 +11:00
|
|
|
llvm::add_module_flag_u32(llmod, llvm::ModuleFlagMergeBehavior::Override, "kcfi", 1);
|
2024-08-16 20:33:58 +00:00
|
|
|
|
|
|
|
|
// Add "kcfi-offset" module flag with -Z patchable-function-entry (See
|
|
|
|
|
// https://reviews.llvm.org/D141172).
|
|
|
|
|
let pfe =
|
|
|
|
|
PatchableFunctionEntry::from_config(sess.opts.unstable_opts.patchable_function_entry);
|
|
|
|
|
if pfe.prefix() > 0 {
|
2024-10-29 13:38:17 +11:00
|
|
|
llvm::add_module_flag_u32(
|
|
|
|
|
llmod,
|
|
|
|
|
llvm::ModuleFlagMergeBehavior::Override,
|
|
|
|
|
"kcfi-offset",
|
|
|
|
|
pfe.prefix().into(),
|
|
|
|
|
);
|
2024-08-16 20:33:58 +00:00
|
|
|
}
|
2025-03-11 19:04:35 +00:00
|
|
|
|
|
|
|
|
// Add "kcfi-arity" module flag if KCFI arity indicator is enabled. (See
|
|
|
|
|
// https://github.com/llvm/llvm-project/pull/117121.)
|
|
|
|
|
if sess.is_sanitizer_kcfi_arity_enabled() {
|
|
|
|
|
// KCFI arity indicator requires LLVM 21.0.0 or later.
|
|
|
|
|
if llvm_version < (21, 0, 0) {
|
|
|
|
|
tcx.dcx().emit_err(crate::errors::SanitizerKcfiArityRequiresLLVM2100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
llvm::add_module_flag_u32(
|
|
|
|
|
llmod,
|
|
|
|
|
llvm::ModuleFlagMergeBehavior::Override,
|
|
|
|
|
"kcfi-arity",
|
|
|
|
|
1,
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-11-21 21:29:00 -08:00
|
|
|
}
|
|
|
|
|
|
2024-10-27 19:34:00 +01:00
|
|
|
// Control Flow Guard is currently only supported by MSVC and LLVM on Windows.
|
|
|
|
|
if sess.target.is_like_msvc
|
|
|
|
|
|| (sess.target.options.os == "windows"
|
|
|
|
|
&& sess.target.options.env == "gnu"
|
|
|
|
|
&& sess.target.options.abi == "llvm")
|
|
|
|
|
{
|
2024-10-29 13:38:17 +11:00
|
|
|
match sess.opts.cg.control_flow_guard {
|
|
|
|
|
CFGuard::Disabled => {}
|
|
|
|
|
CFGuard::NoChecks => {
|
|
|
|
|
// Set `cfguard=1` module flag to emit metadata only.
|
|
|
|
|
llvm::add_module_flag_u32(
|
|
|
|
|
llmod,
|
|
|
|
|
llvm::ModuleFlagMergeBehavior::Warning,
|
|
|
|
|
"cfguard",
|
|
|
|
|
1,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
CFGuard::Checks => {
|
|
|
|
|
// Set `cfguard=2` module flag to emit metadata and checks.
|
|
|
|
|
llvm::add_module_flag_u32(
|
|
|
|
|
llmod,
|
|
|
|
|
llvm::ModuleFlagMergeBehavior::Warning,
|
|
|
|
|
"cfguard",
|
|
|
|
|
2,
|
|
|
|
|
);
|
2020-07-06 16:10:42 +01:00
|
|
|
}
|
2020-01-13 13:25:39 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-13 08:51:58 +02:00
|
|
|
if let Some(regparm_count) = sess.opts.unstable_opts.regparm {
|
|
|
|
|
llvm::add_module_flag_u32(
|
|
|
|
|
llmod,
|
|
|
|
|
llvm::ModuleFlagMergeBehavior::Error,
|
|
|
|
|
"NumRegisterParameters",
|
|
|
|
|
regparm_count,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-06 07:44:47 -05:00
|
|
|
if let Some(BranchProtection { bti, pac_ret }) = sess.opts.unstable_opts.branch_protection {
|
2022-12-13 17:04:02 +00:00
|
|
|
if sess.target.arch == "aarch64" {
|
2024-10-29 13:38:17 +11:00
|
|
|
llvm::add_module_flag_u32(
|
|
|
|
|
llmod,
|
|
|
|
|
llvm::ModuleFlagMergeBehavior::Min,
|
|
|
|
|
"branch-target-enforcement",
|
|
|
|
|
bti.into(),
|
|
|
|
|
);
|
|
|
|
|
llvm::add_module_flag_u32(
|
|
|
|
|
llmod,
|
|
|
|
|
llvm::ModuleFlagMergeBehavior::Min,
|
|
|
|
|
"sign-return-address",
|
|
|
|
|
pac_ret.is_some().into(),
|
|
|
|
|
);
|
2024-10-16 15:39:58 +01:00
|
|
|
let pac_opts = pac_ret.unwrap_or(PacRet { leaf: false, pc: false, key: PAuthKey::A });
|
|
|
|
|
llvm::add_module_flag_u32(
|
|
|
|
|
llmod,
|
|
|
|
|
llvm::ModuleFlagMergeBehavior::Min,
|
|
|
|
|
"branch-protection-pauth-lr",
|
|
|
|
|
pac_opts.pc.into(),
|
|
|
|
|
);
|
2024-10-29 13:38:17 +11:00
|
|
|
llvm::add_module_flag_u32(
|
|
|
|
|
llmod,
|
|
|
|
|
llvm::ModuleFlagMergeBehavior::Min,
|
|
|
|
|
"sign-return-address-all",
|
|
|
|
|
pac_opts.leaf.into(),
|
|
|
|
|
);
|
|
|
|
|
llvm::add_module_flag_u32(
|
|
|
|
|
llmod,
|
|
|
|
|
llvm::ModuleFlagMergeBehavior::Min,
|
|
|
|
|
"sign-return-address-with-bkey",
|
|
|
|
|
u32::from(pac_opts.key == PAuthKey::B),
|
|
|
|
|
);
|
2022-12-13 17:04:02 +00:00
|
|
|
} else {
|
|
|
|
|
bug!(
|
|
|
|
|
"branch-protection used on non-AArch64 target; \
|
|
|
|
|
this should be checked in rustc_session."
|
|
|
|
|
);
|
2022-01-31 21:47:07 +02:00
|
|
|
}
|
2021-07-13 12:14:26 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-28 09:48:59 -08:00
|
|
|
// Pass on the control-flow protection flags to LLVM (equivalent to `-fcf-protection` in Clang).
|
2022-07-06 07:44:47 -05:00
|
|
|
if let CFProtection::Branch | CFProtection::Full = sess.opts.unstable_opts.cf_protection {
|
2024-10-29 13:38:17 +11:00
|
|
|
llvm::add_module_flag_u32(
|
|
|
|
|
llmod,
|
|
|
|
|
llvm::ModuleFlagMergeBehavior::Override,
|
|
|
|
|
"cf-protection-branch",
|
|
|
|
|
1,
|
|
|
|
|
);
|
2022-01-28 09:48:59 -08:00
|
|
|
}
|
2022-07-06 07:44:47 -05:00
|
|
|
if let CFProtection::Return | CFProtection::Full = sess.opts.unstable_opts.cf_protection {
|
2024-10-29 13:38:17 +11:00
|
|
|
llvm::add_module_flag_u32(
|
|
|
|
|
llmod,
|
|
|
|
|
llvm::ModuleFlagMergeBehavior::Override,
|
|
|
|
|
"cf-protection-return",
|
|
|
|
|
1,
|
|
|
|
|
);
|
2022-01-28 09:48:59 -08:00
|
|
|
}
|
|
|
|
|
|
2022-07-06 07:44:47 -05:00
|
|
|
if sess.opts.unstable_opts.virtual_function_elimination {
|
2024-10-29 13:38:17 +11:00
|
|
|
llvm::add_module_flag_u32(
|
|
|
|
|
llmod,
|
|
|
|
|
llvm::ModuleFlagMergeBehavior::Error,
|
|
|
|
|
"Virtual Function Elim",
|
|
|
|
|
1,
|
|
|
|
|
);
|
2022-04-21 13:35:40 +01:00
|
|
|
}
|
2023-11-21 14:24:23 -08:00
|
|
|
|
2023-11-17 10:05:38 -08:00
|
|
|
// Set module flag to enable Windows EHCont Guard (/guard:ehcont).
|
2023-11-21 14:24:23 -08:00
|
|
|
if sess.opts.unstable_opts.ehcont_guard {
|
2024-10-29 13:38:17 +11:00
|
|
|
llvm::add_module_flag_u32(llmod, llvm::ModuleFlagMergeBehavior::Warning, "ehcontguard", 1);
|
2023-11-17 10:05:38 -08:00
|
|
|
}
|
2022-04-21 13:35:40 +01:00
|
|
|
|
2024-09-25 10:23:30 +02:00
|
|
|
match sess.opts.unstable_opts.function_return {
|
|
|
|
|
FunctionReturn::Keep => {}
|
2024-10-29 13:38:17 +11:00
|
|
|
FunctionReturn::ThunkExtern => {
|
|
|
|
|
llvm::add_module_flag_u32(
|
2024-09-25 10:23:30 +02:00
|
|
|
llmod,
|
2024-10-29 13:38:17 +11:00
|
|
|
llvm::ModuleFlagMergeBehavior::Override,
|
|
|
|
|
"function_return_thunk_extern",
|
2024-09-25 10:23:30 +02:00
|
|
|
1,
|
2024-10-29 13:38:17 +11:00
|
|
|
);
|
|
|
|
|
}
|
2024-09-25 10:23:30 +02:00
|
|
|
}
|
|
|
|
|
|
2025-05-07 07:57:01 +00:00
|
|
|
if sess.opts.unstable_opts.indirect_branch_cs_prefix {
|
|
|
|
|
llvm::add_module_flag_u32(
|
|
|
|
|
llmod,
|
|
|
|
|
llvm::ModuleFlagMergeBehavior::Override,
|
|
|
|
|
"indirect_branch_cs_prefix",
|
|
|
|
|
1,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-10 12:19:16 -07:00
|
|
|
match (sess.opts.unstable_opts.small_data_threshold, sess.target.small_data_threshold_support())
|
|
|
|
|
{
|
|
|
|
|
// Set up the small-data optimization limit for architectures that use
|
|
|
|
|
// an LLVM module flag to control this.
|
|
|
|
|
(Some(threshold), SmallDataThresholdSupport::LlvmModuleFlag(flag)) => {
|
2024-10-29 13:38:17 +11:00
|
|
|
llvm::add_module_flag_u32(
|
|
|
|
|
llmod,
|
|
|
|
|
llvm::ModuleFlagMergeBehavior::Error,
|
|
|
|
|
&flag,
|
|
|
|
|
threshold as u32,
|
|
|
|
|
);
|
2024-09-10 12:19:16 -07:00
|
|
|
}
|
|
|
|
|
_ => (),
|
|
|
|
|
};
|
|
|
|
|
|
2022-05-29 01:10:44 +02:00
|
|
|
// Insert `llvm.ident` metadata.
|
|
|
|
|
//
|
|
|
|
|
// On the wasm targets it will get hooked up to the "producer" sections
|
|
|
|
|
// `processed-by` information.
|
2024-02-25 01:11:09 +03:00
|
|
|
#[allow(clippy::option_env_unwrap)]
|
2022-05-29 01:10:44 +02:00
|
|
|
let rustc_producer =
|
|
|
|
|
format!("rustc version {}", option_env!("CFG_VERSION").expect("CFG_VERSION"));
|
2025-07-09 09:31:58 +00:00
|
|
|
|
2025-07-09 09:48:46 +00:00
|
|
|
let name_metadata = cx.create_metadata(rustc_producer.as_bytes());
|
2025-07-09 09:36:19 +00:00
|
|
|
|
2024-07-14 14:27:57 -04:00
|
|
|
unsafe {
|
|
|
|
|
llvm::LLVMAddNamedMetadataOperand(
|
|
|
|
|
llmod,
|
|
|
|
|
c"llvm.ident".as_ptr(),
|
2025-07-09 09:36:19 +00:00
|
|
|
&cx.get_metadata_value(llvm::LLVMMDNodeInContext2(llcx, &name_metadata, 1)),
|
2024-07-14 14:27:57 -04:00
|
|
|
);
|
|
|
|
|
}
|
2022-05-29 01:10:44 +02:00
|
|
|
|
2024-01-28 18:38:41 +08:00
|
|
|
// Emit RISC-V specific target-abi metadata
|
|
|
|
|
// to workaround lld as the LTO plugin not
|
|
|
|
|
// correctly setting target-abi for the LTO object
|
|
|
|
|
// FIXME: https://github.com/llvm/llvm-project/issues/50591
|
|
|
|
|
// If llvm_abiname is empty, emit nothing.
|
2024-04-09 14:47:06 +08:00
|
|
|
let llvm_abiname = &sess.target.options.llvm_abiname;
|
|
|
|
|
if matches!(sess.target.arch.as_ref(), "riscv32" | "riscv64") && !llvm_abiname.is_empty() {
|
2024-10-29 13:38:17 +11:00
|
|
|
llvm::add_module_flag_str(
|
|
|
|
|
llmod,
|
|
|
|
|
llvm::ModuleFlagMergeBehavior::Error,
|
|
|
|
|
"target-abi",
|
|
|
|
|
llvm_abiname,
|
|
|
|
|
);
|
2024-01-28 18:38:41 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-09 02:22:14 -07:00
|
|
|
// Add module flags specified via -Z llvm_module_flag
|
2024-10-29 13:38:17 +11:00
|
|
|
for (key, value, merge_behavior) in &sess.opts.unstable_opts.llvm_module_flag {
|
|
|
|
|
let merge_behavior = match merge_behavior.as_str() {
|
|
|
|
|
"error" => llvm::ModuleFlagMergeBehavior::Error,
|
|
|
|
|
"warning" => llvm::ModuleFlagMergeBehavior::Warning,
|
|
|
|
|
"require" => llvm::ModuleFlagMergeBehavior::Require,
|
|
|
|
|
"override" => llvm::ModuleFlagMergeBehavior::Override,
|
|
|
|
|
"append" => llvm::ModuleFlagMergeBehavior::Append,
|
|
|
|
|
"appendunique" => llvm::ModuleFlagMergeBehavior::AppendUnique,
|
|
|
|
|
"max" => llvm::ModuleFlagMergeBehavior::Max,
|
|
|
|
|
"min" => llvm::ModuleFlagMergeBehavior::Min,
|
2023-10-09 02:22:14 -07:00
|
|
|
// We already checked this during option parsing
|
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
};
|
2024-10-29 13:38:17 +11:00
|
|
|
llvm::add_module_flag_u32(llmod, merge_behavior, key, *value);
|
2023-10-09 02:22:14 -07:00
|
|
|
}
|
|
|
|
|
|
2018-06-27 17:57:25 +03:00
|
|
|
llmod
|
2014-07-16 11:27:57 -07:00
|
|
|
}
|
|
|
|
|
|
2018-09-26 16:01:43 +02:00
|
|
|
impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn new(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-03-14 12:41:32 +01:00
|
|
|
codegen_unit: &'tcx CodegenUnit<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
llvm_module: &'ll crate::ModuleLlvm,
|
|
|
|
|
) -> Self {
|
2015-05-12 14:46:19 -07:00
|
|
|
// An interesting part of Windows which MSVC forces our hand on (and
|
|
|
|
|
// apparently MinGW didn't) is the usage of `dllimport` and `dllexport`
|
|
|
|
|
// attributes in LLVM IR as well as native dependencies (in C these
|
|
|
|
|
// correspond to `__declspec(dllimport)`).
|
|
|
|
|
//
|
2020-05-07 11:52:21 +02:00
|
|
|
// LD (BFD) in MinGW mode can often correctly guess `dllexport` but
|
|
|
|
|
// relying on that can result in issues like #50176.
|
|
|
|
|
// LLD won't support that and expects symbols with proper attributes.
|
|
|
|
|
// Because of that we make MinGW target emit dllexport just like MSVC.
|
|
|
|
|
// When it comes to dllimport we use it for constants but for functions
|
|
|
|
|
// rely on the linker to do the right thing. Opposed to dllexport this
|
|
|
|
|
// task is easy for them (both LD and LLD) and allows us to easily use
|
|
|
|
|
// symbols from static libraries in shared libraries.
|
|
|
|
|
//
|
|
|
|
|
// Whenever a dynamic library is built on Windows it must have its public
|
2015-05-12 14:46:19 -07:00
|
|
|
// interface specified by functions tagged with `dllexport` or otherwise
|
|
|
|
|
// they're not available to be linked against. This poses a few problems
|
|
|
|
|
// for the compiler, some of which are somewhat fundamental, but we use
|
|
|
|
|
// the `use_dll_storage_attrs` variable below to attach the `dllexport`
|
2018-11-27 02:59:49 +00:00
|
|
|
// attribute to all LLVM functions that are exported e.g., they're
|
2015-05-12 14:46:19 -07:00
|
|
|
// already tagged with external linkage). This is suboptimal for a few
|
|
|
|
|
// reasons:
|
|
|
|
|
//
|
|
|
|
|
// * If an object file will never be included in a dynamic library,
|
|
|
|
|
// there's no need to attach the dllexport attribute. Most object
|
|
|
|
|
// files in Rust are not destined to become part of a dll as binaries
|
|
|
|
|
// are statically linked by default.
|
|
|
|
|
// * If the compiler is emitting both an rlib and a dylib, the same
|
|
|
|
|
// source object file is currently used but with MSVC this may be less
|
|
|
|
|
// feasible. The compiler may be able to get around this, but it may
|
|
|
|
|
// involve some invasive changes to deal with this.
|
|
|
|
|
//
|
2022-03-30 01:39:38 -04:00
|
|
|
// The flip side of this situation is that whenever you link to a dll and
|
2015-05-12 14:46:19 -07:00
|
|
|
// you import a function from it, the import should be tagged with
|
|
|
|
|
// `dllimport`. At this time, however, the compiler does not emit
|
|
|
|
|
// `dllimport` for any declarations other than constants (where it is
|
|
|
|
|
// required), which is again suboptimal for even more reasons!
|
|
|
|
|
//
|
|
|
|
|
// * Calling a function imported from another dll without using
|
|
|
|
|
// `dllimport` causes the linker/compiler to have extra overhead (one
|
|
|
|
|
// `jmp` instruction on x86) when calling the function.
|
|
|
|
|
// * The same object file may be used in different circumstances, so a
|
|
|
|
|
// function may be imported from a dll if the object is linked into a
|
|
|
|
|
// dll, but it may be just linked against if linked into an rlib.
|
|
|
|
|
// * The compiler has no knowledge about whether native functions should
|
|
|
|
|
// be tagged dllimport or not.
|
|
|
|
|
//
|
|
|
|
|
// For now the compiler takes the perf hit (I do not have any numbers to
|
|
|
|
|
// this effect) by marking very little as `dllimport` and praying the
|
|
|
|
|
// linker will take care of everything. Fixing this problem will likely
|
|
|
|
|
// require adding a few attributes to Rust itself (feature gated at the
|
2020-05-07 11:52:21 +02:00
|
|
|
// start) and then strongly recommending static linkage on Windows!
|
2020-11-08 14:27:51 +03:00
|
|
|
let use_dll_storage_attrs = tcx.sess.target.is_like_windows;
|
2015-05-12 14:46:19 -07:00
|
|
|
|
2020-04-25 21:45:21 +03:00
|
|
|
let tls_model = to_llvm_tls_model(tcx.sess.tls_model());
|
2017-11-03 00:23:56 +00:00
|
|
|
|
2018-06-27 17:57:25 +03:00
|
|
|
let (llcx, llmod) = (&*llvm_module.llcx, llvm_module.llmod());
|
|
|
|
|
|
2023-02-15 11:43:41 +00:00
|
|
|
let coverage_cx =
|
2024-11-23 13:24:57 +11:00
|
|
|
tcx.sess.instrument_coverage().then(coverageinfo::CguCoverageContext::new);
|
2020-06-21 23:29:08 -07:00
|
|
|
|
2018-07-26 11:41:10 -06:00
|
|
|
let dbg_cx = if tcx.sess.opts.debuginfo != DebugInfo::None {
|
2022-03-03 11:15:25 +01:00
|
|
|
let dctx = debuginfo::CodegenUnitDebugContext::new(llmod);
|
|
|
|
|
debuginfo::metadata::build_compile_unit_di_node(
|
|
|
|
|
tcx,
|
|
|
|
|
codegen_unit.name().as_str(),
|
|
|
|
|
&dctx,
|
|
|
|
|
);
|
2018-06-27 17:57:25 +03:00
|
|
|
Some(dctx)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
|
2025-02-24 08:15:31 +00:00
|
|
|
GenericCx(
|
|
|
|
|
FullCx {
|
|
|
|
|
tcx,
|
2025-06-29 12:11:51 +02:00
|
|
|
scx: SimpleCx::new(llmod, llcx, tcx.data_layout.pointer_size()),
|
2025-02-24 08:15:31 +00:00
|
|
|
use_dll_storage_attrs,
|
|
|
|
|
tls_model,
|
|
|
|
|
codegen_unit,
|
|
|
|
|
instances: Default::default(),
|
|
|
|
|
vtables: Default::default(),
|
|
|
|
|
const_str_cache: Default::default(),
|
|
|
|
|
const_globals: Default::default(),
|
|
|
|
|
statics_to_rauw: RefCell::new(Vec::new()),
|
2024-12-13 09:52:38 +00:00
|
|
|
used_statics: Vec::new(),
|
|
|
|
|
compiler_used_statics: Vec::new(),
|
2025-02-24 08:15:31 +00:00
|
|
|
type_lowering: Default::default(),
|
|
|
|
|
scalar_lltypes: Default::default(),
|
|
|
|
|
coverage_cx,
|
|
|
|
|
dbg_cx,
|
|
|
|
|
eh_personality: Cell::new(None),
|
|
|
|
|
eh_catch_typeinfo: Cell::new(None),
|
|
|
|
|
rust_try_fn: Cell::new(None),
|
|
|
|
|
intrinsics: Default::default(),
|
|
|
|
|
local_gen_sym_counter: Cell::new(0),
|
|
|
|
|
renamed_statics: Default::default(),
|
|
|
|
|
},
|
|
|
|
|
PhantomData,
|
|
|
|
|
)
|
2014-03-15 22:29:34 +02:00
|
|
|
}
|
2018-11-24 14:18:13 +01:00
|
|
|
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn statics_to_rauw(&self) -> &RefCell<Vec<(&'ll Value, &'ll Value)>> {
|
2018-11-24 14:18:13 +01:00
|
|
|
&self.statics_to_rauw
|
|
|
|
|
}
|
2020-06-21 23:29:08 -07:00
|
|
|
|
2024-10-24 22:23:13 +11:00
|
|
|
/// Extra state that is only available when coverage instrumentation is enabled.
|
2020-06-21 23:29:08 -07:00
|
|
|
#[inline]
|
2024-10-31 21:12:15 +11:00
|
|
|
#[track_caller]
|
2024-11-23 13:24:57 +11:00
|
|
|
pub(crate) fn coverage_cx(&self) -> &coverageinfo::CguCoverageContext<'ll, 'tcx> {
|
2024-10-24 22:23:13 +11:00
|
|
|
self.coverage_cx.as_ref().expect("only called when coverage instrumentation is enabled")
|
2020-06-21 23:29:08 -07:00
|
|
|
}
|
2021-08-20 21:13:18 +02:00
|
|
|
|
2022-10-01 16:45:07 +00:00
|
|
|
pub(crate) fn create_used_variable_impl(&self, name: &'static CStr, values: &[&'ll Value]) {
|
2022-12-06 00:07:28 -05:00
|
|
|
let array = self.const_array(self.type_ptr(), values);
|
2021-08-20 21:13:18 +02:00
|
|
|
|
2025-01-14 12:25:16 +00:00
|
|
|
let g = llvm::add_global(self.llmod, self.val_ty(array), name);
|
|
|
|
|
llvm::set_initializer(g, array);
|
|
|
|
|
llvm::set_linkage(g, llvm::Linkage::AppendingLinkage);
|
|
|
|
|
llvm::set_section(g, c"llvm.metadata");
|
2021-08-20 21:13:18 +02:00
|
|
|
}
|
2025-01-24 16:05:26 -05:00
|
|
|
}
|
2025-04-04 14:24:23 -04:00
|
|
|
impl<'ll> SimpleCx<'ll> {
|
|
|
|
|
pub(crate) fn get_type_of_global(&self, val: &'ll Value) -> &'ll Type {
|
|
|
|
|
unsafe { llvm::LLVMGlobalGetValueType(val) }
|
|
|
|
|
}
|
|
|
|
|
pub(crate) fn val_ty(&self, v: &'ll Value) -> &'ll Type {
|
|
|
|
|
common::val_ty(v)
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-24 16:05:26 -05:00
|
|
|
impl<'ll> SimpleCx<'ll> {
|
2025-02-24 11:31:43 +00:00
|
|
|
pub(crate) fn new(
|
|
|
|
|
llmod: &'ll llvm::Module,
|
|
|
|
|
llcx: &'ll llvm::Context,
|
|
|
|
|
pointer_size: Size,
|
|
|
|
|
) -> Self {
|
|
|
|
|
let isize_ty = llvm::Type::ix_llcx(llcx, pointer_size.bits());
|
|
|
|
|
Self(SCx { llmod, llcx, isize_ty }, PhantomData)
|
2025-01-24 16:05:26 -05:00
|
|
|
}
|
2025-02-24 14:33:55 +00:00
|
|
|
}
|
2025-01-01 21:42:45 +01:00
|
|
|
|
2025-02-24 14:33:55 +00:00
|
|
|
impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
|
2025-01-01 21:42:45 +01:00
|
|
|
pub(crate) fn get_metadata_value(&self, metadata: &'ll Metadata) -> &'ll Value {
|
2025-02-24 14:45:16 +00:00
|
|
|
llvm::LLVMMetadataAsValue(self.llcx(), metadata)
|
2025-01-01 21:42:45 +01:00
|
|
|
}
|
|
|
|
|
|
2025-06-16 14:23:06 -07:00
|
|
|
pub(crate) fn get_const_int(&self, ty: &'ll Type, val: u64) -> &'ll Value {
|
|
|
|
|
unsafe { llvm::LLVMConstInt(ty, val, llvm::False) }
|
2025-04-04 14:24:23 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-02 16:35:57 -07:00
|
|
|
pub(crate) fn get_const_i64(&self, n: u64) -> &'ll Value {
|
|
|
|
|
self.get_const_int(self.type_i64(), n)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn get_const_i32(&self, n: u64) -> &'ll Value {
|
|
|
|
|
self.get_const_int(self.type_i32(), n)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn get_const_i16(&self, n: u64) -> &'ll Value {
|
|
|
|
|
self.get_const_int(self.type_i16(), n)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn get_const_i8(&self, n: u64) -> &'ll Value {
|
|
|
|
|
self.get_const_int(self.type_i8(), n)
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-01 21:42:45 +01:00
|
|
|
pub(crate) fn get_function(&self, name: &str) -> Option<&'ll Value> {
|
|
|
|
|
let name = SmallCStr::new(name);
|
2025-02-24 14:33:55 +00:00
|
|
|
unsafe { llvm::LLVMGetNamedFunction((**self).borrow().llmod, name.as_ptr()) }
|
2025-01-01 21:42:45 +01:00
|
|
|
}
|
|
|
|
|
|
2025-02-24 14:38:55 +00:00
|
|
|
pub(crate) fn get_md_kind_id(&self, name: &str) -> llvm::MetadataKindId {
|
2025-01-01 21:42:45 +01:00
|
|
|
unsafe {
|
|
|
|
|
llvm::LLVMGetMDKindIDInContext(
|
2025-02-24 14:33:55 +00:00
|
|
|
self.llcx(),
|
2025-01-01 21:42:45 +01:00
|
|
|
name.as_ptr() as *const c_char,
|
|
|
|
|
name.len() as c_uint,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-09 09:48:46 +00:00
|
|
|
pub(crate) fn create_metadata(&self, name: &[u8]) -> &'ll Metadata {
|
2025-07-09 09:31:58 +00:00
|
|
|
unsafe {
|
2025-02-24 14:33:55 +00:00
|
|
|
llvm::LLVMMDStringInContext2(self.llcx(), name.as_ptr() as *const c_char, name.len())
|
2025-07-09 09:31:58 +00:00
|
|
|
}
|
2025-01-01 21:42:45 +01:00
|
|
|
}
|
2014-07-16 11:27:57 -07:00
|
|
|
}
|
2014-03-15 22:29:34 +02:00
|
|
|
|
2024-09-17 10:15:26 +10:00
|
|
|
impl<'ll, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
|
2018-09-13 14:58:19 +02:00
|
|
|
fn vtables(
|
|
|
|
|
&self,
|
2025-01-10 20:26:10 +00:00
|
|
|
) -> &RefCell<FxHashMap<(Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>), &'ll Value>> {
|
2018-09-13 14:58:19 +02:00
|
|
|
&self.vtables
|
|
|
|
|
}
|
2018-09-20 15:47:22 +02:00
|
|
|
|
2024-03-30 12:01:57 +00:00
|
|
|
fn apply_vcall_visibility_metadata(
|
|
|
|
|
&self,
|
|
|
|
|
ty: Ty<'tcx>,
|
2025-01-10 20:26:10 +00:00
|
|
|
poly_trait_ref: Option<ty::ExistentialTraitRef<'tcx>>,
|
2024-03-30 12:01:57 +00:00
|
|
|
vtable: &'ll Value,
|
|
|
|
|
) {
|
|
|
|
|
apply_vcall_visibility_metadata(self, ty, poly_trait_ref, vtable);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-13 12:05:40 +02:00
|
|
|
fn get_fn(&self, instance: Instance<'tcx>) -> &'ll Value {
|
|
|
|
|
get_fn(self, instance)
|
2018-09-20 15:47:22 +02:00
|
|
|
}
|
|
|
|
|
|
2019-10-13 12:05:40 +02:00
|
|
|
fn get_fn_addr(&self, instance: Instance<'tcx>) -> &'ll Value {
|
2018-11-24 17:03:53 +01:00
|
|
|
get_fn(self, instance)
|
2018-09-13 14:58:19 +02:00
|
|
|
}
|
2018-09-20 15:47:22 +02:00
|
|
|
|
|
|
|
|
fn eh_personality(&self) -> &'ll Value {
|
|
|
|
|
// The exception handling personality function.
|
|
|
|
|
//
|
|
|
|
|
// If our compilation unit has the `eh_personality` lang item somewhere
|
|
|
|
|
// within it, then we just need to codegen that. Otherwise, we're
|
|
|
|
|
// building an rlib which will depend on some upstream implementation of
|
|
|
|
|
// this function, so we just codegen a generic reference to it. We don't
|
|
|
|
|
// specify any of the types for the function, we just make it a symbol
|
|
|
|
|
// that LLVM can later use.
|
|
|
|
|
//
|
|
|
|
|
// Note that MSVC is a little special here in that we don't use the
|
|
|
|
|
// `eh_personality` lang item at all. Currently LLVM has support for
|
|
|
|
|
// both Dwarf and SEH unwind mechanisms for MSVC targets and uses the
|
|
|
|
|
// *name of the personality function* to decide what kind of unwind side
|
|
|
|
|
// tables/landing pads to emit. It looks like Dwarf is used by default,
|
|
|
|
|
// injecting a dependency on the `_Unwind_Resume` symbol for resuming
|
|
|
|
|
// an "exception", but for MSVC we want to force SEH. This means that we
|
|
|
|
|
// can't actually have the personality function be our standard
|
|
|
|
|
// `rust_eh_personality` function, but rather we wired it up to the
|
|
|
|
|
// CRT's custom personality function, which forces LLVM to consider
|
|
|
|
|
// landing pads as "landing pads for SEH".
|
|
|
|
|
if let Some(llpersonality) = self.eh_personality.get() {
|
|
|
|
|
return llpersonality;
|
|
|
|
|
}
|
2023-05-01 19:24:21 +00:00
|
|
|
|
|
|
|
|
let name = if wants_msvc_seh(self.sess()) {
|
|
|
|
|
Some("__CxxFrameHandler3")
|
|
|
|
|
} else if wants_wasm_eh(self.sess()) {
|
2023-05-22 00:28:44 +02:00
|
|
|
// LLVM specifically tests for the name of the personality function
|
|
|
|
|
// There is no need for this function to exist anywhere, it will
|
|
|
|
|
// not be called. However, its name has to be "__gxx_wasm_personality_v0"
|
|
|
|
|
// for native wasm exceptions.
|
2023-05-01 19:24:21 +00:00
|
|
|
Some("__gxx_wasm_personality_v0")
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
let tcx = self.tcx;
|
|
|
|
|
let llfn = match tcx.lang_items().eh_personality() {
|
2024-03-10 11:49:27 +01:00
|
|
|
Some(def_id) if name.is_none() => self.get_fn_addr(ty::Instance::expect_resolve(
|
|
|
|
|
tcx,
|
2024-11-15 13:53:31 +01:00
|
|
|
self.typing_env(),
|
2024-03-10 11:49:27 +01:00
|
|
|
def_id,
|
|
|
|
|
ty::List::empty(),
|
2024-07-01 16:32:32 -04:00
|
|
|
DUMMY_SP,
|
2024-03-10 11:49:27 +01:00
|
|
|
)),
|
2018-09-20 15:47:22 +02:00
|
|
|
_ => {
|
2023-05-01 19:24:21 +00:00
|
|
|
let name = name.unwrap_or("rust_eh_personality");
|
2021-07-07 00:00:00 +00:00
|
|
|
if let Some(llfn) = self.get_declared_value(name) {
|
|
|
|
|
llfn
|
|
|
|
|
} else {
|
|
|
|
|
let fty = self.type_variadic_func(&[], self.type_i32());
|
|
|
|
|
let llfn = self.declare_cfn(name, llvm::UnnamedAddr::Global, fty);
|
2022-02-21 11:19:16 -05:00
|
|
|
let target_cpu = attributes::target_cpu_attr(self);
|
|
|
|
|
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[target_cpu]);
|
2021-07-07 00:00:00 +00:00
|
|
|
llfn
|
|
|
|
|
}
|
2018-09-20 15:47:22 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
self.eh_personality.set(Some(llfn));
|
|
|
|
|
llfn
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn sess(&self) -> &Session {
|
2021-09-30 19:38:50 +02:00
|
|
|
self.tcx.sess
|
2018-09-20 15:47:22 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-26 23:53:35 +03:00
|
|
|
fn set_frame_pointer_type(&self, llfn: &'ll Value) {
|
2022-02-21 11:19:16 -05:00
|
|
|
if let Some(attr) = attributes::frame_pointer_type_attr(self) {
|
|
|
|
|
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[attr]);
|
|
|
|
|
}
|
2018-09-24 15:26:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn apply_target_cpu_attr(&self, llfn: &'ll Value) {
|
2022-02-21 11:19:16 -05:00
|
|
|
let mut attrs = SmallVec::<[_; 2]>::new();
|
|
|
|
|
attrs.push(attributes::target_cpu_attr(self));
|
|
|
|
|
attrs.extend(attributes::tune_cpu_attr(self));
|
|
|
|
|
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &attrs);
|
2018-09-24 15:26:39 +02:00
|
|
|
}
|
2018-09-26 17:00:01 +02:00
|
|
|
|
2020-09-18 13:06:53 +02:00
|
|
|
fn declare_c_main(&self, fn_type: Self::Type) -> Option<Self::Function> {
|
2022-11-05 14:36:38 +05:30
|
|
|
let entry_name = self.sess().target.entry_name.as_ref();
|
|
|
|
|
if self.get_declared_value(entry_name).is_none() {
|
|
|
|
|
Some(self.declare_entry_fn(
|
|
|
|
|
entry_name,
|
2025-01-02 22:42:10 +01:00
|
|
|
llvm::CallConv::from_conv(
|
|
|
|
|
self.sess().target.entry_abi,
|
|
|
|
|
self.sess().target.arch.borrow(),
|
|
|
|
|
),
|
2022-11-05 14:36:38 +05:30
|
|
|
llvm::UnnamedAddr::Global,
|
|
|
|
|
fn_type,
|
|
|
|
|
))
|
2020-09-18 13:06:53 +02:00
|
|
|
} else {
|
|
|
|
|
// If the symbol already exists, it is an error: for example, the user wrote
|
|
|
|
|
// #[no_mangle] extern "C" fn main(..) {..}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-13 14:58:19 +02:00
|
|
|
}
|
|
|
|
|
|
2021-12-14 13:49:49 -05:00
|
|
|
impl<'ll> CodegenCx<'ll, '_> {
|
2025-05-30 21:41:36 +05:30
|
|
|
pub(crate) fn get_intrinsic(
|
|
|
|
|
&self,
|
2025-06-15 03:57:44 +05:30
|
|
|
base_name: Cow<'static, str>,
|
2025-05-30 21:41:36 +05:30
|
|
|
type_params: &[&'ll Type],
|
|
|
|
|
) -> (&'ll Type, &'ll Value) {
|
2025-06-15 03:57:44 +05:30
|
|
|
*self
|
|
|
|
|
.intrinsics
|
2025-05-30 21:41:36 +05:30
|
|
|
.borrow_mut()
|
2025-06-15 03:57:44 +05:30
|
|
|
.entry((base_name, SmallVec::from_slice(type_params)))
|
|
|
|
|
.or_insert_with_key(|(base_name, type_params)| {
|
|
|
|
|
self.declare_intrinsic(base_name, type_params)
|
|
|
|
|
})
|
2019-02-08 16:41:37 +01:00
|
|
|
}
|
|
|
|
|
|
2025-05-30 21:41:36 +05:30
|
|
|
fn declare_intrinsic(
|
|
|
|
|
&self,
|
|
|
|
|
base_name: &str,
|
|
|
|
|
type_params: &[&'ll Type],
|
|
|
|
|
) -> (&'ll Type, &'ll Value) {
|
2021-05-30 10:25:41 -07:00
|
|
|
// This isn't an "LLVM intrinsic", but LLVM's optimization passes
|
2023-08-02 12:45:52 -07:00
|
|
|
// recognize it like one (including turning it into `bcmp` sometimes)
|
|
|
|
|
// and we use it to implement intrinsics like `raw_eq` and `compare_bytes`
|
2025-05-30 21:41:36 +05:30
|
|
|
if base_name == "memcmp" {
|
2025-06-15 03:57:44 +05:30
|
|
|
let fn_ty = self
|
|
|
|
|
.type_func(&[self.type_ptr(), self.type_ptr(), self.type_isize()], self.type_int());
|
2025-05-30 21:41:36 +05:30
|
|
|
let f = self.declare_cfn("memcmp", llvm::UnnamedAddr::No, fn_ty);
|
|
|
|
|
|
|
|
|
|
return (fn_ty, f);
|
2021-11-10 20:14:23 -08:00
|
|
|
}
|
2021-05-30 10:25:41 -07:00
|
|
|
|
2025-06-15 03:57:44 +05:30
|
|
|
let intrinsic = llvm::Intrinsic::lookup(base_name.as_bytes())
|
|
|
|
|
.unwrap_or_else(|| bug!("Unknown intrinsic: `{base_name}`"));
|
|
|
|
|
let f = intrinsic.get_declaration(self.llmod, &type_params);
|
2025-05-30 21:41:36 +05:30
|
|
|
|
2025-06-15 03:57:44 +05:30
|
|
|
(self.get_type_of_global(f), f)
|
2014-04-09 19:56:31 -04:00
|
|
|
}
|
2020-03-21 07:50:38 +00:00
|
|
|
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn eh_catch_typeinfo(&self) -> &'ll Value {
|
2020-03-21 07:50:38 +00:00
|
|
|
if let Some(eh_catch_typeinfo) = self.eh_catch_typeinfo.get() {
|
|
|
|
|
return eh_catch_typeinfo;
|
|
|
|
|
}
|
|
|
|
|
let tcx = self.tcx;
|
2022-06-18 01:01:19 +03:00
|
|
|
assert!(self.sess().target.os == "emscripten");
|
2020-03-21 07:50:38 +00:00
|
|
|
let eh_catch_typeinfo = match tcx.lang_items().eh_catch_typeinfo() {
|
|
|
|
|
Some(def_id) => self.get_static(def_id),
|
|
|
|
|
_ => {
|
2022-12-06 00:07:28 -05:00
|
|
|
let ty = self.type_struct(&[self.type_ptr(), self.type_ptr()], false);
|
2023-12-02 14:17:33 +00:00
|
|
|
self.declare_global(&mangle_internal_symbol(self.tcx, "rust_eh_catch_typeinfo"), ty)
|
2020-03-21 07:50:38 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
self.eh_catch_typeinfo.set(Some(eh_catch_typeinfo));
|
|
|
|
|
eh_catch_typeinfo
|
|
|
|
|
}
|
2018-08-07 17:14:40 +02:00
|
|
|
}
|
2014-05-12 20:03:34 +03:00
|
|
|
|
2021-12-14 13:49:49 -05:00
|
|
|
impl CodegenCx<'_, '_> {
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Generates a new symbol name with the given prefix. This symbol name must
|
2016-10-21 12:41:20 -04:00
|
|
|
/// only be used for definitions with `internal` or `private` linkage.
|
2024-07-06 22:26:42 +10:00
|
|
|
pub(crate) fn generate_local_symbol_name(&self, prefix: &str) -> String {
|
2018-01-05 06:14:44 +02:00
|
|
|
let idx = self.local_gen_sym_counter.get();
|
|
|
|
|
self.local_gen_sym_counter.set(idx + 1);
|
2016-10-21 12:41:20 -04:00
|
|
|
// Include a '.' character, so there can be no accidental conflicts with
|
|
|
|
|
// user defined names
|
2016-11-04 17:37:42 -04:00
|
|
|
let mut name = String::with_capacity(prefix.len() + 6);
|
|
|
|
|
name.push_str(prefix);
|
2020-10-26 21:02:48 -04:00
|
|
|
name.push('.');
|
2024-04-03 14:09:21 -04:00
|
|
|
name.push_str(&(idx as u64).to_base(ALPHANUMERIC_ONLY));
|
2016-11-04 17:37:42 -04:00
|
|
|
name
|
2016-10-21 12:41:20 -04:00
|
|
|
}
|
2025-01-24 16:05:26 -05:00
|
|
|
}
|
|
|
|
|
|
2025-02-24 08:15:31 +00:00
|
|
|
impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
|
2025-01-24 16:05:26 -05:00
|
|
|
/// A wrapper for [`llvm::LLVMSetMetadata`], but it takes `Metadata` as a parameter instead of `Value`.
|
2025-02-24 14:45:16 +00:00
|
|
|
pub(crate) fn set_metadata<'a>(
|
|
|
|
|
&self,
|
|
|
|
|
val: &'a Value,
|
2025-02-24 14:38:55 +00:00
|
|
|
kind_id: impl Into<llvm::MetadataKindId>,
|
2025-02-24 14:45:16 +00:00
|
|
|
md: &'ll Metadata,
|
|
|
|
|
) {
|
|
|
|
|
let node = self.get_metadata_value(md);
|
2025-02-24 14:38:55 +00:00
|
|
|
llvm::LLVMSetMetadata(val, kind_id.into(), node);
|
2024-09-19 18:52:09 +08:00
|
|
|
}
|
2017-09-13 00:33:56 +03:00
|
|
|
}
|
|
|
|
|
|
2021-12-14 13:49:49 -05:00
|
|
|
impl HasDataLayout for CodegenCx<'_, '_> {
|
2021-06-01 00:00:00 +00:00
|
|
|
#[inline]
|
2020-03-31 18:16:47 +02:00
|
|
|
fn data_layout(&self) -> &TargetDataLayout {
|
2018-01-05 06:14:44 +02:00
|
|
|
&self.tcx.data_layout
|
2017-03-10 06:25:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 13:49:49 -05:00
|
|
|
impl HasTargetSpec for CodegenCx<'_, '_> {
|
2021-06-01 00:00:00 +00:00
|
|
|
#[inline]
|
2018-04-18 16:01:26 +03:00
|
|
|
fn target_spec(&self) -> &Target {
|
2020-10-15 11:44:00 +02:00
|
|
|
&self.tcx.sess.target
|
2018-04-18 16:01:26 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 13:49:49 -05:00
|
|
|
impl<'tcx> ty::layout::HasTyCtxt<'tcx> for CodegenCx<'_, 'tcx> {
|
2021-06-01 00:00:00 +00:00
|
|
|
#[inline]
|
2019-06-14 00:48:52 +03:00
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
2018-01-05 06:14:44 +02:00
|
|
|
self.tcx
|
2017-05-19 17:27:25 -04:00
|
|
|
}
|
2017-09-13 00:33:56 +03:00
|
|
|
}
|
2017-04-20 03:14:39 +03:00
|
|
|
|
2024-11-15 13:53:31 +01:00
|
|
|
impl<'tcx, 'll> HasTypingEnv<'tcx> for CodegenCx<'ll, 'tcx> {
|
|
|
|
|
fn typing_env(&self) -> ty::TypingEnv<'tcx> {
|
|
|
|
|
ty::TypingEnv::fully_monomorphized()
|
2021-09-01 14:19:49 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 13:49:49 -05:00
|
|
|
impl<'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
|
2021-08-30 18:01:58 +03:00
|
|
|
#[inline]
|
|
|
|
|
fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
|
2023-08-04 13:28:04 +08:00
|
|
|
if let LayoutError::SizeOverflow(_) | LayoutError::ReferencesError(_) = err {
|
2023-12-18 22:21:37 +11:00
|
|
|
self.tcx.dcx().emit_fatal(Spanned { span, node: err.into_diagnostic() })
|
2021-08-30 18:01:58 +03:00
|
|
|
} else {
|
2023-12-18 22:21:37 +11:00
|
|
|
self.tcx.dcx().emit_fatal(ssa_errors::FailedToGetLayout { span, ty, err })
|
2021-08-30 18:01:58 +03:00
|
|
|
}
|
2017-03-10 06:25:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
2021-09-02 00:09:34 +03:00
|
|
|
|
2021-12-14 13:49:49 -05:00
|
|
|
impl<'tcx> FnAbiOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
|
2021-09-02 00:09:34 +03:00
|
|
|
#[inline]
|
|
|
|
|
fn handle_fn_abi_err(
|
|
|
|
|
&self,
|
|
|
|
|
err: FnAbiError<'tcx>,
|
|
|
|
|
span: Span,
|
2021-09-02 00:29:15 +03:00
|
|
|
fn_abi_request: FnAbiRequest<'tcx>,
|
2021-09-02 00:09:34 +03:00
|
|
|
) -> ! {
|
2024-10-08 16:46:00 -04:00
|
|
|
match err {
|
|
|
|
|
FnAbiError::Layout(LayoutError::SizeOverflow(_) | LayoutError::Cycle(_)) => {
|
|
|
|
|
self.tcx.dcx().emit_fatal(Spanned { span, node: err });
|
|
|
|
|
}
|
|
|
|
|
_ => match fn_abi_request {
|
2021-09-02 00:09:34 +03:00
|
|
|
FnAbiRequest::OfFnPtr { sig, extra_args } => {
|
2023-05-17 10:30:14 +00:00
|
|
|
span_bug!(span, "`fn_abi_of_fn_ptr({sig}, {extra_args:?})` failed: {err:?}",);
|
2021-09-02 00:09:34 +03:00
|
|
|
}
|
|
|
|
|
FnAbiRequest::OfInstance { instance, extra_args } => {
|
|
|
|
|
span_bug!(
|
|
|
|
|
span,
|
2023-05-17 10:30:14 +00:00
|
|
|
"`fn_abi_of_instance({instance}, {extra_args:?})` failed: {err:?}",
|
2021-09-02 00:09:34 +03:00
|
|
|
);
|
|
|
|
|
}
|
2024-10-08 16:46:00 -04:00
|
|
|
},
|
2021-09-02 00:09:34 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|