2019-02-18 03:58:58 +09:00
|
|
|
use crate::attributes;
|
2021-03-12 08:35:13 +09:00
|
|
|
use crate::back::write::to_llvm_code_model;
|
2020-03-11 12:49:08 +01:00
|
|
|
use crate::callee::get_fn;
|
2020-06-21 23:29:08 -07:00
|
|
|
use crate::coverageinfo;
|
2019-02-18 03:58:58 +09:00
|
|
|
use crate::debuginfo;
|
|
|
|
use crate::llvm;
|
2019-07-07 18:41:28 +02:00
|
|
|
use crate::llvm_util;
|
2019-02-18 03:58:58 +09:00
|
|
|
use crate::type_::Type;
|
2020-03-11 12:49:08 +01:00
|
|
|
use crate::value::Value;
|
2017-09-20 23:07:47 +03:00
|
|
|
|
2021-02-13 22:17:15 +11:00
|
|
|
use cstr::cstr;
|
2018-10-03 16:56:24 +02:00
|
|
|
use rustc_codegen_ssa::base::wants_msvc_seh;
|
2020-03-11 12:49:08 +01:00
|
|
|
use rustc_codegen_ssa::traits::*;
|
2019-02-18 03:58:58 +09:00
|
|
|
use rustc_data_structures::base_n;
|
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;
|
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::{
|
|
|
|
FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasParamEnv, LayoutError, LayoutOfHelpers,
|
|
|
|
TyAndLayout,
|
|
|
|
};
|
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};
|
2022-01-28 09:48:59 -08:00
|
|
|
use rustc_session::config::{BranchProtection, CFGuard, CFProtection};
|
|
|
|
use rustc_session::config::{CrateType, DebugInfo, PAuthKey, PacRet};
|
2020-03-11 12:49:08 +01:00
|
|
|
use rustc_session::Session;
|
2021-08-30 18:01:58 +03:00
|
|
|
use rustc_span::source_map::Span;
|
2021-09-02 00:09:34 +03:00
|
|
|
use rustc_target::abi::{
|
|
|
|
call::FnAbi, HasDataLayout, PointeeInfo, Size, TargetDataLayout, VariantIdx,
|
|
|
|
};
|
2020-04-25 21:45:21 +03:00
|
|
|
use rustc_target::spec::{HasTargetSpec, RelocModel, Target, TlsModel};
|
2021-08-09 12:25:33 +00:00
|
|
|
use smallvec::SmallVec;
|
2020-03-11 12:49:08 +01:00
|
|
|
|
2013-12-18 18:35:33 -08:00
|
|
|
use std::cell::{Cell, RefCell};
|
2016-04-18 15:38:45 +03:00
|
|
|
use std::ffi::CStr;
|
|
|
|
use std::str;
|
2013-06-13 14:49:01 +12:00
|
|
|
|
2018-01-05 07:01:54 +02:00
|
|
|
/// There is one `CodegenCx` per compilation unit. Each one has its own LLVM
|
2018-06-27 17:57:25 +03:00
|
|
|
/// `llvm::Context` so that several compilation units may be optimized in parallel.
|
|
|
|
/// All other LLVM data structures in the `CodegenCx` are tied to that `llvm::Context`.
|
2019-06-14 19:39:39 +03:00
|
|
|
pub struct CodegenCx<'ll, 'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
pub tcx: TyCtxt<'tcx>,
|
2018-01-05 06:58:34 +02:00
|
|
|
pub check_overflow: bool,
|
|
|
|
pub use_dll_storage_attrs: bool,
|
|
|
|
pub tls_model: llvm::ThreadLocalMode,
|
2014-07-16 11:27:57 -07:00
|
|
|
|
2018-09-26 16:01:43 +02:00
|
|
|
pub llmod: &'ll llvm::Module,
|
|
|
|
pub llcx: &'ll llvm::Context,
|
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
|
2019-06-12 00:11:55 +03:00
|
|
|
pub vtables:
|
|
|
|
RefCell<FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'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
|
|
|
|
2014-04-01 07:11:23 -04:00
|
|
|
/// Reverse-direction for const ptrs cast from globals.
|
2020-05-01 22:28:15 +02:00
|
|
|
///
|
|
|
|
/// Key is a Value holding a `*T`,
|
|
|
|
/// Val is a Value holding a `*[T]`.
|
2014-04-01 07:11:23 -04:00
|
|
|
///
|
|
|
|
/// Needed because LLVM loses pointer->pointee association
|
2018-05-08 16:10:16 +03:00
|
|
|
/// when we ptrcast, and we have to ptrcast during codegen
|
2020-05-01 22:28:15 +02:00
|
|
|
/// of a `[T]` const because we form a slice, a `(*T,usize)` pair, not
|
2015-01-29 14:03:34 +02:00
|
|
|
/// a pointer to an LLVM array type. Similar for trait objects.
|
2018-11-16 13:48:26 +02:00
|
|
|
pub const_unsized: RefCell<FxHashMap<&'ll Value, &'ll Value>>,
|
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
|
|
|
|
pub used_statics: RefCell<Vec<&'ll Value>>,
|
|
|
|
|
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
|
2021-08-20 21:13:18 +02:00
|
|
|
pub compiler_used_statics: RefCell<Vec<&'ll Value>>,
|
2017-02-20 14:42:47 -05:00
|
|
|
|
2021-08-05 19:14:55 +02:00
|
|
|
/// Mapping of non-scalar types to llvm types and field remapping if needed.
|
|
|
|
pub type_lowering: RefCell<FxHashMap<(Ty<'tcx>, Option<VariantIdx>), TypeLowering<'ll>>>,
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
2018-01-05 06:58:34 +02:00
|
|
|
pub pointee_infos: RefCell<FxHashMap<(Ty<'tcx>, Size), Option<PointeeInfo>>>,
|
2018-09-26 16:01:43 +02:00
|
|
|
pub isize_ty: &'ll Type,
|
2014-05-28 22:26:56 -07:00
|
|
|
|
coverage bug fixes and optimization support
Adjusted LLVM codegen for code compiled with `-Zinstrument-coverage` to
address multiple, somewhat related issues.
Fixed a significant flaw in prior coverage solution: Every counter
generated a new counter variable, but there should have only been one
counter variable per function. This appears to have bloated .profraw
files significantly. (For a small program, it increased the size by
about 40%. I have not tested large programs, but there is anecdotal
evidence that profraw files were way too large. This is a good fix,
regardless, but hopefully it also addresses related issues.
Fixes: #82144
Invalid LLVM coverage data produced when compiled with -C opt-level=1
Existing tests now work up to at least `opt-level=3`. This required a
detailed analysis of the LLVM IR, comparisons with Clang C++ LLVM IR
when compiled with coverage, and a lot of trial and error with codegen
adjustments.
The biggest hurdle was figuring out how to continue to support coverage
results for unused functions and generics. Rust's coverage results have
three advantages over Clang's coverage results:
1. Rust's coverage map does not include any overlapping code regions,
making coverage counting unambiguous.
2. Rust generates coverage results (showing zero counts) for all unused
functions, including generics. (Clang does not generate coverage for
uninstantiated template functions.)
3. Rust's unused functions produce minimal stubbed functions in LLVM IR,
sufficient for including in the coverage results; while Clang must
generate the complete LLVM IR for each unused function, even though
it will never be called.
This PR removes the previous hack of attempting to inject coverage into
some other existing function instance, and generates dedicated instances
for each unused function. This change, and a few other adjustments
(similar to what is required for `-C link-dead-code`, but with lower
impact), makes it possible to support LLVM optimizations.
Fixes: #79651
Coverage report: "Unexecuted instantiation:..." for a generic function
from multiple crates
Fixed by removing the aforementioned hack. Some "Unexecuted
instantiation" notices are unavoidable, as explained in the
`used_crate.rs` test, but `-Zinstrument-coverage` has new options to
back off support for either unused generics, or all unused functions,
which avoids the notice, at the cost of less coverage of unused
functions.
Fixes: #82875
Invalid LLVM coverage data produced with crate brotli_decompressor
Fixed by disabling the LLVM function attribute that forces inlining, if
`-Z instrument-coverage` is enabled. This attribute is applied to
Rust functions with `#[inline(always)], and in some cases, the forced
inlining breaks coverage instrumentation and reports.
2021-03-15 16:32:45 -07:00
|
|
|
pub coverage_cx: Option<coverageinfo::CrateCoverageContext<'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
|
|
|
|
2021-08-03 15:09:57 -07:00
|
|
|
intrinsics: RefCell<FxHashMap<&'static str, (&'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
|
|
|
}
|
|
|
|
|
2021-08-05 19:14:55 +02:00
|
|
|
pub struct TypeLowering<'ll> {
|
|
|
|
/// Associated LLVM type
|
|
|
|
pub lltype: &'ll Type,
|
|
|
|
|
|
|
|
/// If padding is used the slice maps fields from source order
|
|
|
|
/// to llvm order.
|
2021-08-09 15:42:37 +00:00
|
|
|
pub field_remapping: Option<SmallVec<[u32; 4]>>,
|
2021-08-05 19:14:55 +02: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,
|
2017-10-31 18:24:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 13:49:49 -05:00
|
|
|
pub 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);
|
2014-11-25 13:28:35 -08:00
|
|
|
let llmod = llvm::LLVMModuleCreateWithNameInContext(mod_name.as_ptr(), llcx);
|
|
|
|
|
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();
|
|
|
|
if llvm_version < (13, 0, 0) {
|
2021-07-28 22:48:45 +02:00
|
|
|
if sess.target.arch == "powerpc64" {
|
|
|
|
target_data_layout = target_data_layout.replace("-S128", "");
|
|
|
|
}
|
2021-07-28 22:26:14 +02:00
|
|
|
if sess.target.arch == "wasm32" {
|
|
|
|
target_data_layout = "e-m:e-p:32:32-i64:64-n32:64-S128".to_string();
|
|
|
|
}
|
|
|
|
if sess.target.arch == "wasm64" {
|
|
|
|
target_data_layout = "e-m:e-p:64:64-i64:64-n32:64-S128".to_string();
|
|
|
|
}
|
|
|
|
}
|
2022-02-02 10:01:02 +01:00
|
|
|
if llvm_version < (14, 0, 0) {
|
|
|
|
if sess.target.llvm_target == "i686-pc-windows-msvc"
|
|
|
|
|| sess.target.llvm_target == "i586-pc-windows-msvc"
|
|
|
|
{
|
|
|
|
target_data_layout =
|
|
|
|
"e-m:x-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:32-n8:16:32-a:0:32-S32"
|
|
|
|
.to_string();
|
|
|
|
}
|
2022-02-02 10:16:48 +01:00
|
|
|
if sess.target.arch == "wasm32" {
|
|
|
|
target_data_layout = target_data_layout.replace("-p10:8:8-p20:8:8", "");
|
|
|
|
}
|
2022-02-02 10:01:02 +01:00
|
|
|
}
|
2019-07-07 18:41:28 +02:00
|
|
|
|
2016-04-18 15:38:45 +03:00
|
|
|
// Ensure the data-layout values hardcoded remain the defaults.
|
2020-11-08 14:27:51 +03:00
|
|
|
if sess.target.is_builtin {
|
2020-04-23 21:10:01 +03:00
|
|
|
let tm = crate::back::write::create_informational_target_machine(tcx.sess);
|
2015-07-16 15:48:16 -07:00
|
|
|
llvm::LLVMRustSetDataLayoutFromTargetMachine(llmod, tm);
|
|
|
|
llvm::LLVMRustDisposeTargetMachine(tm);
|
2016-04-18 15:38:45 +03:00
|
|
|
|
2020-03-11 00:00:00 +00:00
|
|
|
let llvm_data_layout = llvm::LLVMGetDataLayoutStr(llmod);
|
2019-07-07 18:41:28 +02:00
|
|
|
let llvm_data_layout = str::from_utf8(CStr::from_ptr(llvm_data_layout).to_bytes())
|
2016-04-18 15:38:45 +03:00
|
|
|
.expect("got a non-UTF8 data-layout from LLVM");
|
|
|
|
|
2016-07-25 10:21:31 -07:00
|
|
|
// Unfortunately LLVM target specs change over time, and right now we
|
|
|
|
// don't have proper support to work with any more than one
|
|
|
|
// `data_layout` than the one that is in the rust-lang/rust repo. If
|
|
|
|
// this compiler is configured against a custom LLVM, we may have a
|
|
|
|
// differing data layout, even though we should update our own to use
|
|
|
|
// that one.
|
|
|
|
//
|
|
|
|
// As an interim hack, if CFG_LLVM_ROOT is not an empty string then we
|
|
|
|
// disable this check entirely as we may be configured with something
|
|
|
|
// that has a different target layout.
|
|
|
|
//
|
|
|
|
// Unsure if this will actually cause breakage when rustc is configured
|
|
|
|
// as such.
|
|
|
|
//
|
|
|
|
// FIXME(#34960)
|
|
|
|
let cfg_llvm_root = option_env!("CFG_LLVM_ROOT").unwrap_or("");
|
|
|
|
let custom_llvm_used = cfg_llvm_root.trim() != "";
|
|
|
|
|
2019-07-07 18:41:28 +02:00
|
|
|
if !custom_llvm_used && target_data_layout != llvm_data_layout {
|
2016-04-18 15:38:45 +03:00
|
|
|
bug!(
|
2021-06-06 15:41:58 +03:00
|
|
|
"data-layout for target `{rustc_target}`, `{rustc_layout}`, \
|
|
|
|
differs from LLVM target's `{llvm_target}` default layout, `{llvm_layout}`",
|
|
|
|
rustc_target = sess.opts.target_triple,
|
|
|
|
rustc_layout = target_data_layout,
|
|
|
|
llvm_target = sess.target.llvm_target,
|
|
|
|
llvm_layout = llvm_data_layout
|
2019-07-07 18:41:28 +02:00
|
|
|
);
|
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);
|
2016-04-18 15:38:45 +03:00
|
|
|
llvm::LLVMSetDataLayout(llmod, data_layout.as_ptr());
|
|
|
|
|
2020-10-15 11:44:00 +02:00
|
|
|
let llvm_target = SmallCStr::new(&sess.target.llvm_target);
|
2014-11-25 13:28:35 -08:00
|
|
|
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) {
|
2019-11-03 10:52:00 -06:00
|
|
|
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
|
|
|
|
|| sess.crate_types().iter().all(|ty| *ty == CrateType::Executable)
|
|
|
|
{
|
2020-05-21 20:53:41 +03:00
|
|
|
llvm::LLVMRustSetModulePIELevel(llmod);
|
|
|
|
}
|
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.
|
|
|
|
llvm::LLVMRustSetModuleCodeModel(llmod, to_llvm_code_model(sess.code_model()));
|
|
|
|
|
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() {
|
2019-10-05 03:48:14 -04:00
|
|
|
let avoid_plt = "RtLibUseGOT\0".as_ptr().cast();
|
2022-01-19 14:51:59 +00:00
|
|
|
llvm::LLVMRustAddModuleFlag(llmod, llvm::LLVMModFlagBehavior::Warning, avoid_plt, 1);
|
2018-09-26 19:19:55 +03:00
|
|
|
}
|
|
|
|
|
2021-10-07 15:33:13 -07:00
|
|
|
if sess.is_sanitizer_cfi_enabled() {
|
|
|
|
// FIXME(rcvalle): Add support for non canonical jump tables.
|
|
|
|
let canonical_jump_tables = "CFI Canonical Jump Tables\0".as_ptr().cast();
|
2022-01-19 14:51:59 +00:00
|
|
|
// FIXME(rcvalle): Add it with Override behavior flag.
|
|
|
|
llvm::LLVMRustAddModuleFlag(
|
|
|
|
llmod,
|
|
|
|
llvm::LLVMModFlagBehavior::Warning,
|
|
|
|
canonical_jump_tables,
|
|
|
|
1,
|
|
|
|
);
|
2021-10-07 15:33:13 -07:00
|
|
|
}
|
|
|
|
|
2020-07-06 16:10:42 +01:00
|
|
|
// Control Flow Guard is currently only supported by the MSVC linker on Windows.
|
2020-11-08 14:27:51 +03:00
|
|
|
if sess.target.is_like_msvc {
|
2020-07-14 15:27:42 +01:00
|
|
|
match sess.opts.cg.control_flow_guard {
|
2020-07-06 16:10:42 +01:00
|
|
|
CFGuard::Disabled => {}
|
|
|
|
CFGuard::NoChecks => {
|
|
|
|
// Set `cfguard=1` module flag to emit metadata only.
|
2022-01-19 14:51:59 +00:00
|
|
|
llvm::LLVMRustAddModuleFlag(
|
|
|
|
llmod,
|
|
|
|
llvm::LLVMModFlagBehavior::Warning,
|
|
|
|
"cfguard\0".as_ptr() as *const _,
|
|
|
|
1,
|
|
|
|
)
|
2020-07-06 16:10:42 +01:00
|
|
|
}
|
|
|
|
CFGuard::Checks => {
|
|
|
|
// Set `cfguard=2` module flag to emit metadata and checks.
|
2022-01-19 14:51:59 +00:00
|
|
|
llvm::LLVMRustAddModuleFlag(
|
|
|
|
llmod,
|
|
|
|
llvm::LLVMModFlagBehavior::Warning,
|
|
|
|
"cfguard\0".as_ptr() as *const _,
|
|
|
|
2,
|
|
|
|
)
|
2020-07-06 16:10:42 +01:00
|
|
|
}
|
2020-01-13 13:25:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 21:47:07 +02:00
|
|
|
if let Some(BranchProtection { bti, pac_ret }) = sess.opts.debugging_opts.branch_protection {
|
|
|
|
if sess.target.arch != "aarch64" {
|
|
|
|
sess.err("-Zbranch-protection is only supported on aarch64");
|
|
|
|
} else {
|
|
|
|
llvm::LLVMRustAddModuleFlag(
|
|
|
|
llmod,
|
|
|
|
llvm::LLVMModFlagBehavior::Error,
|
|
|
|
"branch-target-enforcement\0".as_ptr().cast(),
|
|
|
|
bti.into(),
|
|
|
|
);
|
|
|
|
llvm::LLVMRustAddModuleFlag(
|
|
|
|
llmod,
|
|
|
|
llvm::LLVMModFlagBehavior::Error,
|
|
|
|
"sign-return-address\0".as_ptr().cast(),
|
|
|
|
pac_ret.is_some().into(),
|
|
|
|
);
|
|
|
|
let pac_opts = pac_ret.unwrap_or(PacRet { leaf: false, key: PAuthKey::A });
|
|
|
|
llvm::LLVMRustAddModuleFlag(
|
|
|
|
llmod,
|
|
|
|
llvm::LLVMModFlagBehavior::Error,
|
|
|
|
"sign-return-address-all\0".as_ptr().cast(),
|
|
|
|
pac_opts.leaf.into(),
|
|
|
|
);
|
|
|
|
llvm::LLVMRustAddModuleFlag(
|
|
|
|
llmod,
|
|
|
|
llvm::LLVMModFlagBehavior::Error,
|
|
|
|
"sign-return-address-with-bkey\0".as_ptr().cast(),
|
|
|
|
u32::from(pac_opts.key == PAuthKey::B),
|
|
|
|
);
|
|
|
|
}
|
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).
|
|
|
|
if let CFProtection::Branch | CFProtection::Full = sess.opts.debugging_opts.cf_protection {
|
|
|
|
llvm::LLVMRustAddModuleFlag(
|
|
|
|
llmod,
|
|
|
|
llvm::LLVMModFlagBehavior::Override,
|
|
|
|
"cf-protection-branch\0".as_ptr().cast(),
|
|
|
|
1,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if let CFProtection::Return | CFProtection::Full = sess.opts.debugging_opts.cf_protection {
|
|
|
|
llvm::LLVMRustAddModuleFlag(
|
|
|
|
llmod,
|
|
|
|
llvm::LLVMModFlagBehavior::Override,
|
|
|
|
"cf-protection-return\0".as_ptr().cast(),
|
|
|
|
1,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-04-21 13:35:40 +01:00
|
|
|
if sess.opts.debugging_opts.virtual_function_elimination {
|
|
|
|
llvm::LLVMRustAddModuleFlag(
|
|
|
|
llmod,
|
|
|
|
llvm::LLVMModFlagBehavior::Error,
|
|
|
|
"Virtual Function Elim\0".as_ptr().cast(),
|
|
|
|
1,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-09-13 20:26:39 -07:00
|
|
|
let check_overflow = tcx.sess.overflow_checks();
|
|
|
|
|
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());
|
|
|
|
|
coverage bug fixes and optimization support
Adjusted LLVM codegen for code compiled with `-Zinstrument-coverage` to
address multiple, somewhat related issues.
Fixed a significant flaw in prior coverage solution: Every counter
generated a new counter variable, but there should have only been one
counter variable per function. This appears to have bloated .profraw
files significantly. (For a small program, it increased the size by
about 40%. I have not tested large programs, but there is anecdotal
evidence that profraw files were way too large. This is a good fix,
regardless, but hopefully it also addresses related issues.
Fixes: #82144
Invalid LLVM coverage data produced when compiled with -C opt-level=1
Existing tests now work up to at least `opt-level=3`. This required a
detailed analysis of the LLVM IR, comparisons with Clang C++ LLVM IR
when compiled with coverage, and a lot of trial and error with codegen
adjustments.
The biggest hurdle was figuring out how to continue to support coverage
results for unused functions and generics. Rust's coverage results have
three advantages over Clang's coverage results:
1. Rust's coverage map does not include any overlapping code regions,
making coverage counting unambiguous.
2. Rust generates coverage results (showing zero counts) for all unused
functions, including generics. (Clang does not generate coverage for
uninstantiated template functions.)
3. Rust's unused functions produce minimal stubbed functions in LLVM IR,
sufficient for including in the coverage results; while Clang must
generate the complete LLVM IR for each unused function, even though
it will never be called.
This PR removes the previous hack of attempting to inject coverage into
some other existing function instance, and generates dedicated instances
for each unused function. This change, and a few other adjustments
(similar to what is required for `-C link-dead-code`, but with lower
impact), makes it possible to support LLVM optimizations.
Fixes: #79651
Coverage report: "Unexecuted instantiation:..." for a generic function
from multiple crates
Fixed by removing the aforementioned hack. Some "Unexecuted
instantiation" notices are unavoidable, as explained in the
`used_crate.rs` test, but `-Zinstrument-coverage` has new options to
back off support for either unused generics, or all unused functions,
which avoids the notice, at the cost of less coverage of unused
functions.
Fixes: #82875
Invalid LLVM coverage data produced with crate brotli_decompressor
Fixed by disabling the LLVM function attribute that forces inlining, if
`-Z instrument-coverage` is enabled. This attribute is applied to
Rust functions with `#[inline(always)], and in some cases, the forced
inlining breaks coverage instrumentation and reports.
2021-03-15 16:32:45 -07:00
|
|
|
let coverage_cx = if tcx.sess.instrument_coverage() {
|
2020-06-21 23:29:08 -07:00
|
|
|
let covctx = coverageinfo::CrateCoverageContext::new();
|
|
|
|
Some(covctx)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2018-08-22 17:48:32 +02:00
|
|
|
let isize_ty = Type::ix_llcx(llcx, tcx.data_layout.pointer_size.bits());
|
2018-06-27 17:57:25 +03:00
|
|
|
|
|
|
|
CodegenCx {
|
|
|
|
tcx,
|
|
|
|
check_overflow,
|
|
|
|
use_dll_storage_attrs,
|
|
|
|
tls_model,
|
|
|
|
llmod,
|
|
|
|
llcx,
|
|
|
|
codegen_unit,
|
2018-10-16 16:57:53 +02:00
|
|
|
instances: Default::default(),
|
|
|
|
vtables: Default::default(),
|
2022-03-04 23:14:38 -05:00
|
|
|
const_str_cache: Default::default(),
|
2018-10-16 16:57:53 +02:00
|
|
|
const_unsized: Default::default(),
|
|
|
|
const_globals: Default::default(),
|
2018-06-27 17:57:25 +03:00
|
|
|
statics_to_rauw: RefCell::new(Vec::new()),
|
|
|
|
used_statics: RefCell::new(Vec::new()),
|
2021-08-20 21:13:18 +02:00
|
|
|
compiler_used_statics: RefCell::new(Vec::new()),
|
2021-08-05 19:14:55 +02:00
|
|
|
type_lowering: Default::default(),
|
2018-10-16 16:57:53 +02:00
|
|
|
scalar_lltypes: Default::default(),
|
|
|
|
pointee_infos: Default::default(),
|
2018-06-27 17:57:25 +03:00
|
|
|
isize_ty,
|
2020-06-21 23:29:08 -07:00
|
|
|
coverage_cx,
|
2018-06-27 17:57:25 +03:00
|
|
|
dbg_cx,
|
|
|
|
eh_personality: Cell::new(None),
|
2020-03-21 07:50:38 +00:00
|
|
|
eh_catch_typeinfo: Cell::new(None),
|
2018-06-27 17:57:25 +03:00
|
|
|
rust_try_fn: Cell::new(None),
|
2018-10-16 16:57:53 +02:00
|
|
|
intrinsics: Default::default(),
|
2018-06-27 17:57:25 +03:00
|
|
|
local_gen_sym_counter: Cell::new(0),
|
2022-03-01 00:53:25 +00:00
|
|
|
renamed_statics: Default::default(),
|
2014-07-16 11:27:57 -07:00
|
|
|
}
|
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
|
|
|
|
|
|
|
#[inline]
|
2021-12-14 13:49:49 -05:00
|
|
|
pub fn coverage_context(&self) -> Option<&coverageinfo::CrateCoverageContext<'ll, 'tcx>> {
|
2020-10-23 11:41:56 -07:00
|
|
|
self.coverage_cx.as_ref()
|
2020-06-21 23:29:08 -07:00
|
|
|
}
|
2021-08-20 21:13:18 +02:00
|
|
|
|
|
|
|
fn create_used_variable_impl(&self, name: &'static CStr, values: &[&'ll Value]) {
|
|
|
|
let section = cstr!("llvm.metadata");
|
2021-09-30 19:38:50 +02:00
|
|
|
let array = self.const_array(self.type_ptr_to(self.type_i8()), values);
|
2021-08-20 21:13:18 +02:00
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let g = llvm::LLVMAddGlobal(self.llmod, self.val_ty(array), name.as_ptr());
|
|
|
|
llvm::LLVMSetInitializer(g, array);
|
|
|
|
llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);
|
|
|
|
llvm::LLVMSetSection(g, section.as_ptr());
|
|
|
|
}
|
|
|
|
}
|
2014-07-16 11:27:57 -07:00
|
|
|
}
|
2014-03-15 22:29:34 +02:00
|
|
|
|
2021-12-14 13:49:49 -05:00
|
|
|
impl<'ll, 'tcx> MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
|
2018-09-13 14:58:19 +02:00
|
|
|
fn vtables(
|
|
|
|
&self,
|
2018-12-04 13:28:06 +02:00
|
|
|
) -> &RefCell<FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), &'ll Value>>
|
2018-09-13 14:58:19 +02:00
|
|
|
{
|
|
|
|
&self.vtables
|
|
|
|
}
|
2018-09-20 15:47:22 +02:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
let tcx = self.tcx;
|
|
|
|
let llfn = match tcx.lang_items().eh_personality() {
|
2018-10-03 16:56:24 +02:00
|
|
|
Some(def_id) if !wants_msvc_seh(self.sess()) => self.get_fn_addr(
|
2019-10-13 11:45:34 +02:00
|
|
|
ty::Instance::resolve(
|
|
|
|
tcx,
|
|
|
|
ty::ParamEnv::reveal_all(),
|
|
|
|
def_id,
|
|
|
|
tcx.intern_substs(&[]),
|
|
|
|
)
|
2020-04-10 05:13:29 +03:00
|
|
|
.unwrap()
|
2019-12-22 17:42:04 -05:00
|
|
|
.unwrap(),
|
|
|
|
),
|
2018-09-20 15:47:22 +02:00
|
|
|
_ => {
|
2018-10-03 16:56:24 +02:00
|
|
|
let name = if wants_msvc_seh(self.sess()) {
|
2018-09-20 15:47:22 +02:00
|
|
|
"__CxxFrameHandler3"
|
|
|
|
} else {
|
|
|
|
"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
|
|
|
}
|
|
|
|
|
|
|
|
fn check_overflow(&self) -> bool {
|
|
|
|
self.check_overflow
|
|
|
|
}
|
2018-09-26 17:00:01 +02:00
|
|
|
|
2020-03-14 12:41:32 +01:00
|
|
|
fn codegen_unit(&self) -> &'tcx CodegenUnit<'tcx> {
|
|
|
|
self.codegen_unit
|
2018-09-21 16:13:15 +02:00
|
|
|
}
|
2018-09-24 15:26:39 +02:00
|
|
|
|
2018-09-26 17:00:01 +02:00
|
|
|
fn used_statics(&self) -> &RefCell<Vec<&'ll Value>> {
|
|
|
|
&self.used_statics
|
|
|
|
}
|
|
|
|
|
2021-08-20 21:13:18 +02:00
|
|
|
fn compiler_used_statics(&self) -> &RefCell<Vec<&'ll Value>> {
|
|
|
|
&self.compiler_used_statics
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
fn create_used_variable(&self) {
|
2021-08-20 21:13:18 +02:00
|
|
|
self.create_used_variable_impl(cstr!("llvm.used"), &*self.used_statics.borrow());
|
|
|
|
}
|
2018-09-26 17:00:01 +02:00
|
|
|
|
2021-08-20 21:13:18 +02:00
|
|
|
fn create_compiler_used_variable(&self) {
|
|
|
|
self.create_used_variable_impl(
|
|
|
|
cstr!("llvm.compiler.used"),
|
|
|
|
&*self.compiler_used_statics.borrow(),
|
|
|
|
);
|
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> {
|
|
|
|
if self.get_declared_value("main").is_none() {
|
2021-01-23 17:19:49 -05:00
|
|
|
Some(self.declare_cfn("main", 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(..) {..}
|
|
|
|
// instead of #[start]
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2018-09-13 14:58:19 +02:00
|
|
|
}
|
|
|
|
|
2021-12-14 13:49:49 -05:00
|
|
|
impl<'ll> CodegenCx<'ll, '_> {
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn get_intrinsic(&self, key: &str) -> (&'ll Type, &'ll Value) {
|
2018-01-05 06:58:34 +02:00
|
|
|
if let Some(v) = self.intrinsics.borrow().get(key).cloned() {
|
2014-11-29 16:41:21 -05:00
|
|
|
return v;
|
2014-04-09 19:56:31 -04:00
|
|
|
}
|
2018-10-08 16:58:26 +02:00
|
|
|
|
2018-09-07 15:39:39 -07:00
|
|
|
self.declare_intrinsic(key).unwrap_or_else(|| bug!("unknown intrinsic '{}'", key))
|
|
|
|
}
|
|
|
|
|
2019-02-08 16:41:37 +01:00
|
|
|
fn insert_intrinsic(
|
|
|
|
&self,
|
|
|
|
name: &'static str,
|
2021-12-14 13:49:49 -05:00
|
|
|
args: Option<&[&'ll llvm::Type]>,
|
|
|
|
ret: &'ll llvm::Type,
|
|
|
|
) -> (&'ll llvm::Type, &'ll llvm::Value) {
|
2019-02-08 16:41:37 +01:00
|
|
|
let fn_ty = if let Some(args) = args {
|
|
|
|
self.type_func(args, ret)
|
|
|
|
} else {
|
|
|
|
self.type_variadic_func(&[], ret)
|
|
|
|
};
|
2021-01-23 17:19:49 -05:00
|
|
|
let f = self.declare_cfn(name, llvm::UnnamedAddr::No, fn_ty);
|
2021-08-03 15:09:57 -07:00
|
|
|
self.intrinsics.borrow_mut().insert(name, (fn_ty, f));
|
|
|
|
(fn_ty, f)
|
2019-02-08 16:41:37 +01:00
|
|
|
}
|
|
|
|
|
2021-12-14 13:49:49 -05:00
|
|
|
fn declare_intrinsic(&self, key: &str) -> Option<(&'ll Type, &'ll Value)> {
|
2018-09-07 15:39:39 -07:00
|
|
|
macro_rules! ifn {
|
|
|
|
($name:expr, fn() -> $ret:expr) => (
|
|
|
|
if key == $name {
|
2019-02-08 16:41:37 +01:00
|
|
|
return Some(self.insert_intrinsic($name, Some(&[]), $ret));
|
2018-09-07 15:39:39 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
($name:expr, fn(...) -> $ret:expr) => (
|
|
|
|
if key == $name {
|
2019-02-08 16:41:37 +01:00
|
|
|
return Some(self.insert_intrinsic($name, None, $ret));
|
2018-09-07 15:39:39 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
($name:expr, fn($($arg:expr),*) -> $ret:expr) => (
|
|
|
|
if key == $name {
|
2019-02-08 16:41:37 +01:00
|
|
|
return Some(self.insert_intrinsic($name, Some(&[$($arg),*]), $ret));
|
2018-09-07 15:39:39 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
macro_rules! mk_struct {
|
2018-11-07 12:08:41 +02:00
|
|
|
($($field_ty:expr),*) => (self.type_struct( &[$($field_ty),*], false))
|
2018-09-07 15:39:39 -07:00
|
|
|
}
|
|
|
|
|
2018-11-07 12:08:41 +02:00
|
|
|
let i8p = self.type_i8p();
|
|
|
|
let void = self.type_void();
|
|
|
|
let i1 = self.type_i1();
|
|
|
|
let t_i8 = self.type_i8();
|
|
|
|
let t_i16 = self.type_i16();
|
|
|
|
let t_i32 = self.type_i32();
|
|
|
|
let t_i64 = self.type_i64();
|
|
|
|
let t_i128 = self.type_i128();
|
2021-05-30 10:25:41 -07:00
|
|
|
let t_isize = self.type_isize();
|
2018-11-07 12:08:41 +02:00
|
|
|
let t_f32 = self.type_f32();
|
|
|
|
let t_f64 = self.type_f64();
|
2022-04-21 13:58:25 +01:00
|
|
|
let t_metadata = self.type_metadata();
|
2018-11-07 12:08:41 +02:00
|
|
|
|
2020-07-22 14:51:12 -07:00
|
|
|
ifn!("llvm.wasm.trunc.unsigned.i32.f32", fn(t_f32) -> t_i32);
|
|
|
|
ifn!("llvm.wasm.trunc.unsigned.i32.f64", fn(t_f64) -> t_i32);
|
|
|
|
ifn!("llvm.wasm.trunc.unsigned.i64.f32", fn(t_f32) -> t_i64);
|
|
|
|
ifn!("llvm.wasm.trunc.unsigned.i64.f64", fn(t_f64) -> t_i64);
|
|
|
|
ifn!("llvm.wasm.trunc.signed.i32.f32", fn(t_f32) -> t_i32);
|
|
|
|
ifn!("llvm.wasm.trunc.signed.i32.f64", fn(t_f64) -> t_i32);
|
|
|
|
ifn!("llvm.wasm.trunc.signed.i64.f32", fn(t_f32) -> t_i64);
|
|
|
|
ifn!("llvm.wasm.trunc.signed.i64.f64", fn(t_f64) -> t_i64);
|
2020-06-25 17:05:12 +02:00
|
|
|
|
2021-04-19 10:55:32 -07:00
|
|
|
ifn!("llvm.fptosi.sat.i8.f32", fn(t_f32) -> t_i8);
|
|
|
|
ifn!("llvm.fptosi.sat.i16.f32", fn(t_f32) -> t_i16);
|
|
|
|
ifn!("llvm.fptosi.sat.i32.f32", fn(t_f32) -> t_i32);
|
|
|
|
ifn!("llvm.fptosi.sat.i64.f32", fn(t_f32) -> t_i64);
|
|
|
|
ifn!("llvm.fptosi.sat.i128.f32", fn(t_f32) -> t_i128);
|
|
|
|
ifn!("llvm.fptosi.sat.i8.f64", fn(t_f64) -> t_i8);
|
|
|
|
ifn!("llvm.fptosi.sat.i16.f64", fn(t_f64) -> t_i16);
|
|
|
|
ifn!("llvm.fptosi.sat.i32.f64", fn(t_f64) -> t_i32);
|
|
|
|
ifn!("llvm.fptosi.sat.i64.f64", fn(t_f64) -> t_i64);
|
|
|
|
ifn!("llvm.fptosi.sat.i128.f64", fn(t_f64) -> t_i128);
|
|
|
|
|
|
|
|
ifn!("llvm.fptoui.sat.i8.f32", fn(t_f32) -> t_i8);
|
|
|
|
ifn!("llvm.fptoui.sat.i16.f32", fn(t_f32) -> t_i16);
|
|
|
|
ifn!("llvm.fptoui.sat.i32.f32", fn(t_f32) -> t_i32);
|
|
|
|
ifn!("llvm.fptoui.sat.i64.f32", fn(t_f32) -> t_i64);
|
|
|
|
ifn!("llvm.fptoui.sat.i128.f32", fn(t_f32) -> t_i128);
|
|
|
|
ifn!("llvm.fptoui.sat.i8.f64", fn(t_f64) -> t_i8);
|
|
|
|
ifn!("llvm.fptoui.sat.i16.f64", fn(t_f64) -> t_i16);
|
|
|
|
ifn!("llvm.fptoui.sat.i32.f64", fn(t_f64) -> t_i32);
|
|
|
|
ifn!("llvm.fptoui.sat.i64.f64", fn(t_f64) -> t_i64);
|
|
|
|
ifn!("llvm.fptoui.sat.i128.f64", fn(t_f64) -> t_i128);
|
|
|
|
|
2018-09-07 15:39:39 -07:00
|
|
|
ifn!("llvm.trap", fn() -> void);
|
|
|
|
ifn!("llvm.debugtrap", fn() -> void);
|
|
|
|
ifn!("llvm.frameaddress", fn(t_i32) -> i8p);
|
|
|
|
|
|
|
|
ifn!("llvm.powi.f32", fn(t_f32, t_i32) -> t_f32);
|
|
|
|
ifn!("llvm.powi.f64", fn(t_f64, t_i32) -> t_f64);
|
|
|
|
|
|
|
|
ifn!("llvm.pow.f32", fn(t_f32, t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.pow.f64", fn(t_f64, t_f64) -> t_f64);
|
|
|
|
|
|
|
|
ifn!("llvm.sqrt.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.sqrt.f64", fn(t_f64) -> t_f64);
|
|
|
|
|
|
|
|
ifn!("llvm.sin.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.sin.f64", fn(t_f64) -> t_f64);
|
|
|
|
|
|
|
|
ifn!("llvm.cos.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.cos.f64", fn(t_f64) -> t_f64);
|
|
|
|
|
|
|
|
ifn!("llvm.exp.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.exp.f64", fn(t_f64) -> t_f64);
|
|
|
|
|
|
|
|
ifn!("llvm.exp2.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.exp2.f64", fn(t_f64) -> t_f64);
|
|
|
|
|
|
|
|
ifn!("llvm.log.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.log.f64", fn(t_f64) -> t_f64);
|
|
|
|
|
|
|
|
ifn!("llvm.log10.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.log10.f64", fn(t_f64) -> t_f64);
|
|
|
|
|
|
|
|
ifn!("llvm.log2.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.log2.f64", fn(t_f64) -> t_f64);
|
|
|
|
|
|
|
|
ifn!("llvm.fma.f32", fn(t_f32, t_f32, t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.fma.f64", fn(t_f64, t_f64, t_f64) -> t_f64);
|
|
|
|
|
|
|
|
ifn!("llvm.fabs.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.fabs.f64", fn(t_f64) -> t_f64);
|
|
|
|
|
2019-06-06 21:27:23 +01:00
|
|
|
ifn!("llvm.minnum.f32", fn(t_f32, t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.minnum.f64", fn(t_f64, t_f64) -> t_f64);
|
|
|
|
ifn!("llvm.maxnum.f32", fn(t_f32, t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.maxnum.f64", fn(t_f64, t_f64) -> t_f64);
|
|
|
|
|
2018-09-07 15:39:39 -07:00
|
|
|
ifn!("llvm.floor.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.floor.f64", fn(t_f64) -> t_f64);
|
|
|
|
|
|
|
|
ifn!("llvm.ceil.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.ceil.f64", fn(t_f64) -> t_f64);
|
|
|
|
|
|
|
|
ifn!("llvm.trunc.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.trunc.f64", fn(t_f64) -> t_f64);
|
|
|
|
|
|
|
|
ifn!("llvm.copysign.f32", fn(t_f32, t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.copysign.f64", fn(t_f64, t_f64) -> t_f64);
|
|
|
|
ifn!("llvm.round.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.round.f64", fn(t_f64) -> t_f64);
|
|
|
|
|
|
|
|
ifn!("llvm.rint.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.rint.f64", fn(t_f64) -> t_f64);
|
|
|
|
ifn!("llvm.nearbyint.f32", fn(t_f32) -> t_f32);
|
|
|
|
ifn!("llvm.nearbyint.f64", fn(t_f64) -> t_f64);
|
|
|
|
|
|
|
|
ifn!("llvm.ctpop.i8", fn(t_i8) -> t_i8);
|
|
|
|
ifn!("llvm.ctpop.i16", fn(t_i16) -> t_i16);
|
|
|
|
ifn!("llvm.ctpop.i32", fn(t_i32) -> t_i32);
|
|
|
|
ifn!("llvm.ctpop.i64", fn(t_i64) -> t_i64);
|
|
|
|
ifn!("llvm.ctpop.i128", fn(t_i128) -> t_i128);
|
|
|
|
|
|
|
|
ifn!("llvm.ctlz.i8", fn(t_i8, i1) -> t_i8);
|
|
|
|
ifn!("llvm.ctlz.i16", fn(t_i16, i1) -> t_i16);
|
|
|
|
ifn!("llvm.ctlz.i32", fn(t_i32, i1) -> t_i32);
|
|
|
|
ifn!("llvm.ctlz.i64", fn(t_i64, i1) -> t_i64);
|
|
|
|
ifn!("llvm.ctlz.i128", fn(t_i128, i1) -> t_i128);
|
|
|
|
|
|
|
|
ifn!("llvm.cttz.i8", fn(t_i8, i1) -> t_i8);
|
|
|
|
ifn!("llvm.cttz.i16", fn(t_i16, i1) -> t_i16);
|
|
|
|
ifn!("llvm.cttz.i32", fn(t_i32, i1) -> t_i32);
|
|
|
|
ifn!("llvm.cttz.i64", fn(t_i64, i1) -> t_i64);
|
|
|
|
ifn!("llvm.cttz.i128", fn(t_i128, i1) -> t_i128);
|
|
|
|
|
|
|
|
ifn!("llvm.bswap.i16", fn(t_i16) -> t_i16);
|
|
|
|
ifn!("llvm.bswap.i32", fn(t_i32) -> t_i32);
|
|
|
|
ifn!("llvm.bswap.i64", fn(t_i64) -> t_i64);
|
|
|
|
ifn!("llvm.bswap.i128", fn(t_i128) -> t_i128);
|
|
|
|
|
|
|
|
ifn!("llvm.bitreverse.i8", fn(t_i8) -> t_i8);
|
|
|
|
ifn!("llvm.bitreverse.i16", fn(t_i16) -> t_i16);
|
|
|
|
ifn!("llvm.bitreverse.i32", fn(t_i32) -> t_i32);
|
|
|
|
ifn!("llvm.bitreverse.i64", fn(t_i64) -> t_i64);
|
|
|
|
ifn!("llvm.bitreverse.i128", fn(t_i128) -> t_i128);
|
|
|
|
|
2018-10-23 23:13:33 +00:00
|
|
|
ifn!("llvm.fshl.i8", fn(t_i8, t_i8, t_i8) -> t_i8);
|
|
|
|
ifn!("llvm.fshl.i16", fn(t_i16, t_i16, t_i16) -> t_i16);
|
|
|
|
ifn!("llvm.fshl.i32", fn(t_i32, t_i32, t_i32) -> t_i32);
|
|
|
|
ifn!("llvm.fshl.i64", fn(t_i64, t_i64, t_i64) -> t_i64);
|
|
|
|
ifn!("llvm.fshl.i128", fn(t_i128, t_i128, t_i128) -> t_i128);
|
|
|
|
|
|
|
|
ifn!("llvm.fshr.i8", fn(t_i8, t_i8, t_i8) -> t_i8);
|
|
|
|
ifn!("llvm.fshr.i16", fn(t_i16, t_i16, t_i16) -> t_i16);
|
|
|
|
ifn!("llvm.fshr.i32", fn(t_i32, t_i32, t_i32) -> t_i32);
|
|
|
|
ifn!("llvm.fshr.i64", fn(t_i64, t_i64, t_i64) -> t_i64);
|
|
|
|
ifn!("llvm.fshr.i128", fn(t_i128, t_i128, t_i128) -> t_i128);
|
2018-09-07 15:39:39 -07:00
|
|
|
|
|
|
|
ifn!("llvm.sadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
|
|
|
|
ifn!("llvm.sadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
|
|
|
|
ifn!("llvm.sadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
|
|
|
|
ifn!("llvm.sadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
|
|
|
|
ifn!("llvm.sadd.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
|
2019-12-22 17:42:04 -05:00
|
|
|
|
2018-09-07 15:39:39 -07:00
|
|
|
ifn!("llvm.uadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
|
|
|
|
ifn!("llvm.uadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
|
|
|
|
ifn!("llvm.uadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
|
|
|
|
ifn!("llvm.uadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
|
|
|
|
ifn!("llvm.uadd.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
|
2019-12-22 17:42:04 -05:00
|
|
|
|
2018-09-07 15:39:39 -07:00
|
|
|
ifn!("llvm.ssub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
|
|
|
|
ifn!("llvm.ssub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
|
|
|
|
ifn!("llvm.ssub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
|
|
|
|
ifn!("llvm.ssub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
|
|
|
|
ifn!("llvm.ssub.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
|
2019-12-22 17:42:04 -05:00
|
|
|
|
2018-09-07 15:39:39 -07:00
|
|
|
ifn!("llvm.usub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
|
|
|
|
ifn!("llvm.usub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
|
|
|
|
ifn!("llvm.usub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
|
|
|
|
ifn!("llvm.usub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
|
|
|
|
ifn!("llvm.usub.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
|
2019-12-22 17:42:04 -05:00
|
|
|
|
2018-09-07 15:39:39 -07:00
|
|
|
ifn!("llvm.smul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
|
|
|
|
ifn!("llvm.smul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
|
|
|
|
ifn!("llvm.smul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
|
|
|
|
ifn!("llvm.smul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
|
|
|
|
ifn!("llvm.smul.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
|
2019-12-22 17:42:04 -05:00
|
|
|
|
2018-09-07 15:39:39 -07:00
|
|
|
ifn!("llvm.umul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
|
|
|
|
ifn!("llvm.umul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
|
|
|
|
ifn!("llvm.umul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
|
|
|
|
ifn!("llvm.umul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
|
|
|
|
ifn!("llvm.umul.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
|
|
|
|
|
2019-01-29 22:16:43 +01:00
|
|
|
ifn!("llvm.sadd.sat.i8", fn(t_i8, t_i8) -> t_i8);
|
|
|
|
ifn!("llvm.sadd.sat.i16", fn(t_i16, t_i16) -> t_i16);
|
|
|
|
ifn!("llvm.sadd.sat.i32", fn(t_i32, t_i32) -> t_i32);
|
|
|
|
ifn!("llvm.sadd.sat.i64", fn(t_i64, t_i64) -> t_i64);
|
|
|
|
ifn!("llvm.sadd.sat.i128", fn(t_i128, t_i128) -> t_i128);
|
|
|
|
|
|
|
|
ifn!("llvm.uadd.sat.i8", fn(t_i8, t_i8) -> t_i8);
|
|
|
|
ifn!("llvm.uadd.sat.i16", fn(t_i16, t_i16) -> t_i16);
|
|
|
|
ifn!("llvm.uadd.sat.i32", fn(t_i32, t_i32) -> t_i32);
|
|
|
|
ifn!("llvm.uadd.sat.i64", fn(t_i64, t_i64) -> t_i64);
|
|
|
|
ifn!("llvm.uadd.sat.i128", fn(t_i128, t_i128) -> t_i128);
|
|
|
|
|
|
|
|
ifn!("llvm.ssub.sat.i8", fn(t_i8, t_i8) -> t_i8);
|
|
|
|
ifn!("llvm.ssub.sat.i16", fn(t_i16, t_i16) -> t_i16);
|
|
|
|
ifn!("llvm.ssub.sat.i32", fn(t_i32, t_i32) -> t_i32);
|
|
|
|
ifn!("llvm.ssub.sat.i64", fn(t_i64, t_i64) -> t_i64);
|
|
|
|
ifn!("llvm.ssub.sat.i128", fn(t_i128, t_i128) -> t_i128);
|
|
|
|
|
|
|
|
ifn!("llvm.usub.sat.i8", fn(t_i8, t_i8) -> t_i8);
|
|
|
|
ifn!("llvm.usub.sat.i16", fn(t_i16, t_i16) -> t_i16);
|
|
|
|
ifn!("llvm.usub.sat.i32", fn(t_i32, t_i32) -> t_i32);
|
|
|
|
ifn!("llvm.usub.sat.i64", fn(t_i64, t_i64) -> t_i64);
|
|
|
|
ifn!("llvm.usub.sat.i128", fn(t_i128, t_i128) -> t_i128);
|
|
|
|
|
2020-01-05 23:27:05 +01:00
|
|
|
ifn!("llvm.lifetime.start.p0i8", fn(t_i64, i8p) -> void);
|
|
|
|
ifn!("llvm.lifetime.end.p0i8", fn(t_i64, i8p) -> void);
|
2018-09-07 15:39:39 -07:00
|
|
|
|
|
|
|
ifn!("llvm.expect.i1", fn(i1, i1) -> i1);
|
|
|
|
ifn!("llvm.eh.typeid.for", fn(i8p) -> t_i32);
|
|
|
|
ifn!("llvm.localescape", fn(...) -> void);
|
|
|
|
ifn!("llvm.localrecover", fn(i8p, i8p, t_i32) -> i8p);
|
|
|
|
ifn!("llvm.x86.seh.recoverfp", fn(i8p, i8p) -> i8p);
|
|
|
|
|
|
|
|
ifn!("llvm.assume", fn(i1) -> void);
|
|
|
|
ifn!("llvm.prefetch", fn(i8p, t_i32, t_i32, t_i32) -> void);
|
|
|
|
|
2021-05-30 10:25:41 -07:00
|
|
|
// This isn't an "LLVM intrinsic", but LLVM's optimization passes
|
|
|
|
// recognize it like one and we assume it exists in `core::slice::cmp`
|
2022-03-22 11:43:05 +01:00
|
|
|
match self.sess().target.arch.as_ref() {
|
2021-11-10 20:14:23 -08:00
|
|
|
"avr" | "msp430" => ifn!("memcmp", fn(i8p, i8p, t_isize) -> t_i16),
|
|
|
|
_ => ifn!("memcmp", fn(i8p, i8p, t_isize) -> t_i32),
|
|
|
|
}
|
2021-05-30 10:25:41 -07:00
|
|
|
|
2018-10-23 23:13:33 +00:00
|
|
|
// variadic intrinsics
|
|
|
|
ifn!("llvm.va_start", fn(i8p) -> void);
|
|
|
|
ifn!("llvm.va_end", fn(i8p) -> void);
|
|
|
|
ifn!("llvm.va_copy", fn(i8p, i8p) -> void);
|
|
|
|
|
coverage bug fixes and optimization support
Adjusted LLVM codegen for code compiled with `-Zinstrument-coverage` to
address multiple, somewhat related issues.
Fixed a significant flaw in prior coverage solution: Every counter
generated a new counter variable, but there should have only been one
counter variable per function. This appears to have bloated .profraw
files significantly. (For a small program, it increased the size by
about 40%. I have not tested large programs, but there is anecdotal
evidence that profraw files were way too large. This is a good fix,
regardless, but hopefully it also addresses related issues.
Fixes: #82144
Invalid LLVM coverage data produced when compiled with -C opt-level=1
Existing tests now work up to at least `opt-level=3`. This required a
detailed analysis of the LLVM IR, comparisons with Clang C++ LLVM IR
when compiled with coverage, and a lot of trial and error with codegen
adjustments.
The biggest hurdle was figuring out how to continue to support coverage
results for unused functions and generics. Rust's coverage results have
three advantages over Clang's coverage results:
1. Rust's coverage map does not include any overlapping code regions,
making coverage counting unambiguous.
2. Rust generates coverage results (showing zero counts) for all unused
functions, including generics. (Clang does not generate coverage for
uninstantiated template functions.)
3. Rust's unused functions produce minimal stubbed functions in LLVM IR,
sufficient for including in the coverage results; while Clang must
generate the complete LLVM IR for each unused function, even though
it will never be called.
This PR removes the previous hack of attempting to inject coverage into
some other existing function instance, and generates dedicated instances
for each unused function. This change, and a few other adjustments
(similar to what is required for `-C link-dead-code`, but with lower
impact), makes it possible to support LLVM optimizations.
Fixes: #79651
Coverage report: "Unexecuted instantiation:..." for a generic function
from multiple crates
Fixed by removing the aforementioned hack. Some "Unexecuted
instantiation" notices are unavoidable, as explained in the
`used_crate.rs` test, but `-Zinstrument-coverage` has new options to
back off support for either unused generics, or all unused functions,
which avoids the notice, at the cost of less coverage of unused
functions.
Fixes: #82875
Invalid LLVM coverage data produced with crate brotli_decompressor
Fixed by disabling the LLVM function attribute that forces inlining, if
`-Z instrument-coverage` is enabled. This attribute is applied to
Rust functions with `#[inline(always)], and in some cases, the forced
inlining breaks coverage instrumentation and reports.
2021-03-15 16:32:45 -07:00
|
|
|
if self.sess().instrument_coverage() {
|
2020-06-21 23:29:08 -07:00
|
|
|
ifn!("llvm.instrprof.increment", fn(i8p, t_i64, t_i32, t_i32) -> void);
|
|
|
|
}
|
|
|
|
|
2022-04-21 13:58:25 +01:00
|
|
|
ifn!("llvm.type.test", fn(i8p, t_metadata) -> i1);
|
|
|
|
ifn!("llvm.type.checked.load", fn(i8p, t_i32, t_metadata) -> mk_struct! {i8p, i1});
|
2021-10-07 15:33:13 -07:00
|
|
|
|
2018-09-07 15:39:39 -07:00
|
|
|
if self.sess().opts.debuginfo != DebugInfo::None {
|
2022-04-21 13:58:25 +01:00
|
|
|
ifn!("llvm.dbg.declare", fn(t_metadata, t_metadata) -> void);
|
|
|
|
ifn!("llvm.dbg.value", fn(t_metadata, t_i64, t_metadata) -> void);
|
2018-09-07 15:39:39 -07:00
|
|
|
}
|
2020-03-20 15:03:11 +01:00
|
|
|
None
|
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),
|
|
|
|
_ => {
|
|
|
|
let ty = self
|
|
|
|
.type_struct(&[self.type_ptr_to(self.type_isize()), self.type_i8p()], false);
|
|
|
|
self.declare_global("rust_eh_catch_typeinfo", ty)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let eh_catch_typeinfo = self.const_bitcast(eh_catch_typeinfo, self.type_i8p());
|
|
|
|
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.
|
|
|
|
pub 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('.');
|
2018-01-08 12:30:52 +01:00
|
|
|
base_n::push_str(idx as u128, base_n::ALPHANUMERIC_ONLY, &mut name);
|
2016-11-04 17:37:42 -04:00
|
|
|
name
|
2016-10-21 12:41:20 -04: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
|
|
|
|
2021-09-01 14:19:49 +03:00
|
|
|
impl<'tcx, 'll> HasParamEnv<'tcx> for CodegenCx<'ll, 'tcx> {
|
|
|
|
fn param_env(&self) -> ty::ParamEnv<'tcx> {
|
|
|
|
ty::ParamEnv::reveal_all()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 13:49:49 -05:00
|
|
|
impl<'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
|
2021-08-30 17:38:27 +03:00
|
|
|
type LayoutOfResult = TyAndLayout<'tcx>;
|
2017-09-13 00:33:56 +03:00
|
|
|
|
2021-08-30 18:01:58 +03:00
|
|
|
#[inline]
|
|
|
|
fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
|
|
|
|
if let LayoutError::SizeOverflow(_) = err {
|
|
|
|
self.sess().span_fatal(span, &err.to_string())
|
|
|
|
} else {
|
|
|
|
span_bug!(span, "failed to get layout for `{}`: {}", ty, err)
|
|
|
|
}
|
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
|
|
|
type FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>;
|
|
|
|
|
|
|
|
#[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
|
|
|
) -> ! {
|
|
|
|
if let FnAbiError::Layout(LayoutError::SizeOverflow(_)) = err {
|
|
|
|
self.sess().span_fatal(span, &err.to_string())
|
|
|
|
} else {
|
|
|
|
match fn_abi_request {
|
|
|
|
FnAbiRequest::OfFnPtr { sig, extra_args } => {
|
|
|
|
span_bug!(
|
|
|
|
span,
|
|
|
|
"`fn_abi_of_fn_ptr({}, {:?})` failed: {}",
|
|
|
|
sig,
|
|
|
|
extra_args,
|
|
|
|
err
|
|
|
|
);
|
|
|
|
}
|
|
|
|
FnAbiRequest::OfInstance { instance, extra_args } => {
|
|
|
|
span_bug!(
|
|
|
|
span,
|
|
|
|
"`fn_abi_of_instance({}, {:?})` failed: {}",
|
|
|
|
instance,
|
|
|
|
extra_args,
|
|
|
|
err
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|