2020-12-23 03:37:23 +00:00
|
|
|
|
//! Codegen the MIR to the LLVM IR.
|
2015-02-28 21:53:12 +00:00
|
|
|
|
//!
|
2018-05-08 13:10:16 +00:00
|
|
|
|
//! Hopefully useful general knowledge about codegen:
|
2015-02-28 21:53:12 +00:00
|
|
|
|
//!
|
2020-12-23 03:37:23 +00:00
|
|
|
|
//! * There's no way to find out the [`Ty`] type of a [`Value`]. Doing so
|
2019-02-08 13:53:55 +00:00
|
|
|
|
//! would be "trying to get the eggs out of an omelette" (credit:
|
2020-12-23 03:37:23 +00:00
|
|
|
|
//! pcwalton). You can, instead, find out its [`llvm::Type`] by calling [`val_ty`],
|
|
|
|
|
//! but one [`llvm::Type`] corresponds to many [`Ty`]s; for instance, `tup(int, int,
|
|
|
|
|
//! int)` and `rec(x=int, y=int, z=int)` will have the same [`llvm::Type`].
|
|
|
|
|
//!
|
|
|
|
|
//! [`Ty`]: rustc_middle::ty::Ty
|
|
|
|
|
//! [`val_ty`]: common::val_ty
|
2011-12-14 00:25:51 +00:00
|
|
|
|
|
2020-01-05 01:10:23 +00:00
|
|
|
|
use super::ModuleLlvm;
|
2014-11-27 12:21:26 +00:00
|
|
|
|
|
2020-01-16 00:00:00 +00:00
|
|
|
|
use crate::attributes;
|
2019-02-17 18:58:58 +00:00
|
|
|
|
use crate::builder::Builder;
|
|
|
|
|
use crate::common;
|
|
|
|
|
use crate::context::CodegenCx;
|
2019-12-22 22:42:04 +00:00
|
|
|
|
use crate::llvm;
|
2019-12-24 04:30:02 +00:00
|
|
|
|
use crate::value::Value;
|
|
|
|
|
|
2020-03-29 15:19:48 +00:00
|
|
|
|
use rustc_codegen_ssa::base::maybe_create_entry_wrapper;
|
|
|
|
|
use rustc_codegen_ssa::mono_item::MonoItemExt;
|
|
|
|
|
use rustc_codegen_ssa::traits::*;
|
|
|
|
|
use rustc_codegen_ssa::{ModuleCodegen, ModuleKind};
|
|
|
|
|
use rustc_data_structures::small_c_str::SmallCStr;
|
2021-09-24 16:15:36 +00:00
|
|
|
|
use rustc_metadata::EncodedMetadata;
|
2020-03-29 14:41:09 +00:00
|
|
|
|
use rustc_middle::dep_graph;
|
2020-06-14 00:00:00 +00:00
|
|
|
|
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
|
2020-03-29 14:41:09 +00:00
|
|
|
|
use rustc_middle::middle::exported_symbols;
|
|
|
|
|
use rustc_middle::mir::mono::{Linkage, Visibility};
|
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
2021-02-07 21:47:03 +00:00
|
|
|
|
use rustc_session::config::DebugInfo;
|
2019-12-31 17:15:40 +00:00
|
|
|
|
use rustc_span::symbol::Symbol;
|
2021-02-07 21:47:03 +00:00
|
|
|
|
use rustc_target::spec::SanitizerSet;
|
2019-12-24 04:30:02 +00:00
|
|
|
|
|
2017-10-17 20:08:13 +00:00
|
|
|
|
use std::ffi::CString;
|
2018-10-03 14:56:24 +00:00
|
|
|
|
use std::time::Instant;
|
2012-03-04 01:49:23 +00:00
|
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
|
pub fn write_compressed_metadata<'tcx>(
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-04-26 07:22:36 +00:00
|
|
|
|
metadata: &EncodedMetadata,
|
2019-06-11 21:11:55 +00:00
|
|
|
|
llvm_module: &mut ModuleLlvm,
|
2019-04-26 07:22:36 +00:00
|
|
|
|
) {
|
2020-08-20 23:16:30 +00:00
|
|
|
|
use snap::write::FrameEncoder;
|
2019-12-22 22:42:04 +00:00
|
|
|
|
use std::io::Write;
|
Store metadata separately in rlib files
Right now whenever an rlib file is linked against, all of the metadata from the
rlib is pulled in to the final staticlib or binary. The reason for this is that
the metadata is currently stored in a section of the object file. Note that this
is intentional for dynamic libraries in order to distribute metadata bundled
with static libraries.
This commit alters the situation for rlib libraries to instead store the
metadata in a separate file in the archive. In doing so, when the archive is
passed to the linker, none of the metadata will get pulled into the result
executable. Furthermore, the metadata file is skipped when assembling rlibs into
an archive.
The snag in this implementation comes with multiple output formats. When
generating a dylib, the metadata needs to be in the object file, but when
generating an rlib this needs to be separate. In order to accomplish this, the
metadata variable is inserted into an entirely separate LLVM Module which is
then codegen'd into a different location (foo.metadata.o). This is then linked
into dynamic libraries and silently ignored for rlib files.
While changing how metadata is inserted into archives, I have also stopped
compressing metadata when inserted into rlib files. We have wanted to stop
compressing metadata, but the sections it creates in object file sections are
apparently too large. Thankfully if it's just an arbitrary file it doesn't
matter how large it is.
I have seen massive reductions in executable sizes, as well as staticlib output
sizes (to confirm that this is all working).
2013-12-04 01:41:01 +00:00
|
|
|
|
|
2021-03-29 11:02:59 +00:00
|
|
|
|
// Historical note:
|
|
|
|
|
//
|
|
|
|
|
// When using link.exe it was seen that the section name `.note.rustc`
|
|
|
|
|
// was getting shortened to `.note.ru`, and according to the PE and COFF
|
|
|
|
|
// specification:
|
|
|
|
|
//
|
|
|
|
|
// > Executable images do not use a string table and do not support
|
|
|
|
|
// > section names longer than 8 characters
|
|
|
|
|
//
|
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/debug/pe-format
|
|
|
|
|
//
|
|
|
|
|
// As a result, we choose a slightly shorter name! As to why
|
2021-05-10 08:49:45 +00:00
|
|
|
|
// `.note.rustc` works on MinGW, see
|
|
|
|
|
// https://github.com/llvm/llvm-project/blob/llvmorg-12.0.0/lld/COFF/Writer.cpp#L1190-L1197
|
2021-03-29 11:02:59 +00:00
|
|
|
|
let section_name = if tcx.sess.target.is_like_osx { "__DATA,.rustc" } else { ".rustc" };
|
|
|
|
|
|
2018-06-27 14:57:25 +00:00
|
|
|
|
let (metadata_llcx, metadata_llmod) = (&*llvm_module.llcx, llvm_module.llmod());
|
2021-05-29 20:49:59 +00:00
|
|
|
|
let mut compressed = rustc_metadata::METADATA_HEADER.to_vec();
|
2021-09-24 16:15:36 +00:00
|
|
|
|
FrameEncoder::new(&mut compressed).write_all(metadata.raw_data()).unwrap();
|
2015-11-20 23:08:09 +00:00
|
|
|
|
|
2018-09-06 21:44:51 +00:00
|
|
|
|
let llmeta = common::bytes_in_context(metadata_llcx, &compressed);
|
|
|
|
|
let llconst = common::struct_in_context(metadata_llcx, &[llmeta], false);
|
2018-03-05 16:41:11 +00:00
|
|
|
|
let name = exported_symbols::metadata_symbol_name(tcx);
|
2015-02-18 06:47:40 +00:00
|
|
|
|
let buf = CString::new(name).unwrap();
|
2019-12-22 22:42:04 +00:00
|
|
|
|
let llglobal =
|
|
|
|
|
unsafe { llvm::LLVMAddGlobal(metadata_llmod, common::val_ty(llconst), buf.as_ptr()) };
|
2013-01-11 05:23:07 +00:00
|
|
|
|
unsafe {
|
|
|
|
|
llvm::LLVMSetInitializer(llglobal, llconst);
|
2018-08-07 14:04:34 +00:00
|
|
|
|
let name = SmallCStr::new(section_name);
|
2016-08-14 08:16:28 +00:00
|
|
|
|
llvm::LLVMSetSection(llglobal, name.as_ptr());
|
|
|
|
|
|
|
|
|
|
// Also generate a .section directive to force no
|
|
|
|
|
// flags, at least for ELF outputs, so that the
|
|
|
|
|
// metadata doesn't get loaded into memory.
|
|
|
|
|
let directive = format!(".section {}", section_name);
|
2020-03-11 00:00:00 +00:00
|
|
|
|
llvm::LLVMSetModuleInlineAsm2(metadata_llmod, directive.as_ptr().cast(), directive.len())
|
2013-01-11 05:23:07 +00:00
|
|
|
|
}
|
2011-06-27 23:09:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-10 10:28:39 +00:00
|
|
|
|
pub struct ValueIter<'ll> {
|
|
|
|
|
cur: Option<&'ll Value>,
|
|
|
|
|
step: unsafe extern "C" fn(&'ll Value) -> Option<&'ll Value>,
|
2015-08-21 07:41:07 +00:00
|
|
|
|
}
|
2014-08-01 17:29:44 +00:00
|
|
|
|
|
2018-07-10 10:28:39 +00:00
|
|
|
|
impl Iterator for ValueIter<'ll> {
|
|
|
|
|
type Item = &'ll Value;
|
2014-08-01 17:29:44 +00:00
|
|
|
|
|
2018-07-10 10:28:39 +00:00
|
|
|
|
fn next(&mut self) -> Option<&'ll Value> {
|
2015-08-21 07:41:07 +00:00
|
|
|
|
let old = self.cur;
|
2018-07-10 10:28:39 +00:00
|
|
|
|
if let Some(old) = old {
|
2015-10-18 00:15:26 +00:00
|
|
|
|
self.cur = unsafe { (self.step)(old) };
|
2015-08-21 07:41:07 +00:00
|
|
|
|
}
|
2018-07-10 10:28:39 +00:00
|
|
|
|
old
|
2014-08-01 17:29:44 +00:00
|
|
|
|
}
|
2015-08-21 07:41:07 +00:00
|
|
|
|
}
|
2014-08-01 17:29:44 +00:00
|
|
|
|
|
2018-07-10 10:28:39 +00:00
|
|
|
|
pub fn iter_globals(llmod: &'ll llvm::Module) -> ValueIter<'ll> {
|
2019-12-22 22:42:04 +00:00
|
|
|
|
unsafe { ValueIter { cur: llvm::LLVMGetFirstGlobal(llmod), step: llvm::LLVMGetNextGlobal } }
|
2015-08-21 07:41:07 +00:00
|
|
|
|
}
|
2015-01-02 04:54:03 +00:00
|
|
|
|
|
2019-09-25 17:14:43 +00:00
|
|
|
|
pub fn compile_codegen_unit(
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-10-21 06:14:03 +00:00
|
|
|
|
cgu_name: Symbol,
|
2020-01-05 01:10:23 +00:00
|
|
|
|
) -> (ModuleCodegen<ModuleLlvm>, u64) {
|
2017-09-14 03:26:39 +00:00
|
|
|
|
let start_time = Instant::now();
|
2018-08-20 11:42:07 +00:00
|
|
|
|
|
|
|
|
|
let dep_node = tcx.codegen_unit(cgu_name).codegen_dep_node(tcx);
|
2019-12-22 22:42:04 +00:00
|
|
|
|
let (module, _) =
|
|
|
|
|
tcx.dep_graph.with_task(dep_node, tcx, cgu_name, module_codegen, dep_graph::hash_result);
|
2018-05-08 13:10:16 +00:00
|
|
|
|
let time_to_codegen = start_time.elapsed();
|
2017-09-14 03:26:39 +00:00
|
|
|
|
|
|
|
|
|
// We assume that the cost to run LLVM on a CGU is proportional to
|
2018-05-08 13:10:16 +00:00
|
|
|
|
// the time we needed for codegenning it.
|
2020-09-20 08:12:57 +00:00
|
|
|
|
let cost = time_to_codegen.as_nanos() as u64;
|
2017-09-14 03:26:39 +00:00
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
|
fn module_codegen(tcx: TyCtxt<'_>, cgu_name: Symbol) -> ModuleCodegen<ModuleLlvm> {
|
2018-08-20 11:42:07 +00:00
|
|
|
|
let cgu = tcx.codegen_unit(cgu_name);
|
2020-11-03 12:26:00 +00:00
|
|
|
|
let _prof_timer = tcx.prof.generic_activity_with_args(
|
|
|
|
|
"codegen_module",
|
|
|
|
|
&[cgu_name.to_string(), cgu.size_estimate().to_string()],
|
|
|
|
|
);
|
2018-05-08 13:10:16 +00:00
|
|
|
|
// Instantiate monomorphizations without filling out definitions yet...
|
2019-02-20 20:27:00 +00:00
|
|
|
|
let llvm_module = ModuleLlvm::new(tcx, &cgu_name.as_str());
|
2019-05-28 22:21:27 +00:00
|
|
|
|
{
|
2018-09-27 13:31:20 +00:00
|
|
|
|
let cx = CodegenCx::new(tcx, cgu, &llvm_module);
|
2019-12-22 22:42:04 +00:00
|
|
|
|
let mono_items = cx.codegen_unit.items_in_deterministic_order(cx.tcx);
|
2018-05-08 13:10:16 +00:00
|
|
|
|
for &(mono_item, (linkage, visibility)) in &mono_items {
|
2019-02-25 07:40:18 +00:00
|
|
|
|
mono_item.predefine::<Builder<'_, '_, '_>>(&cx, linkage, visibility);
|
2017-09-14 03:26:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ... and now that we have everything pre-defined, fill out those definitions.
|
2018-05-08 13:10:16 +00:00
|
|
|
|
for &(mono_item, _) in &mono_items {
|
2019-02-25 07:40:18 +00:00
|
|
|
|
mono_item.define::<Builder<'_, '_, '_>>(&cx);
|
2017-09-14 03:26:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If this codegen unit contains the main function, also create the
|
|
|
|
|
// wrapper here
|
2020-01-16 00:00:00 +00:00
|
|
|
|
if let Some(entry) = maybe_create_entry_wrapper::<Builder<'_, '_, '_>>(&cx) {
|
2020-06-14 00:00:00 +00:00
|
|
|
|
attributes::sanitize(&cx, SanitizerSet::empty(), entry);
|
2020-01-16 00:00:00 +00:00
|
|
|
|
}
|
2017-09-14 03:26:39 +00:00
|
|
|
|
|
|
|
|
|
// Run replace-all-uses-with for statics that need it
|
2018-09-26 15:00:01 +00:00
|
|
|
|
for &(old_g, new_g) in cx.statics_to_rauw().borrow().iter() {
|
2018-11-16 11:33:28 +00:00
|
|
|
|
unsafe {
|
2018-11-24 13:18:13 +00:00
|
|
|
|
let bitcast = llvm::LLVMConstPointerCast(new_g, cx.val_ty(old_g));
|
|
|
|
|
llvm::LLVMReplaceAllUsesWith(old_g, bitcast);
|
|
|
|
|
llvm::LLVMDeleteGlobal(old_g);
|
2018-11-16 11:33:28 +00:00
|
|
|
|
}
|
2017-09-14 03:26:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-02 18:27:15 +00:00
|
|
|
|
// Finalize code coverage by injecting the coverage map. Note, the coverage map will
|
2021-08-07 15:04:32 +00:00
|
|
|
|
// also be added to the `llvm.compiler.used` variable, created next.
|
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 23:32:45 +00:00
|
|
|
|
if cx.sess().instrument_coverage() {
|
2020-07-02 18:27:15 +00:00
|
|
|
|
cx.coverageinfo_finalize();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-20 19:13:18 +00:00
|
|
|
|
// Create the llvm.used and llvm.compiler.used variables.
|
2018-09-26 15:00:01 +00:00
|
|
|
|
if !cx.used_statics().borrow().is_empty() {
|
|
|
|
|
cx.create_used_variable()
|
2017-09-14 03:26:39 +00:00
|
|
|
|
}
|
2021-08-20 19:13:18 +00:00
|
|
|
|
if !cx.compiler_used_statics().borrow().is_empty() {
|
|
|
|
|
cx.create_compiler_used_variable()
|
|
|
|
|
}
|
2017-09-14 03:26:39 +00:00
|
|
|
|
|
|
|
|
|
// Finalize debuginfo
|
2018-07-26 17:41:10 +00:00
|
|
|
|
if cx.sess().opts.debuginfo != DebugInfo::None {
|
2018-09-26 15:00:01 +00:00
|
|
|
|
cx.debuginfo_finalize();
|
2017-09-14 03:26:39 +00:00
|
|
|
|
}
|
2019-05-28 22:21:27 +00:00
|
|
|
|
}
|
2017-09-14 03:26:39 +00:00
|
|
|
|
|
2019-05-28 22:21:27 +00:00
|
|
|
|
ModuleCodegen {
|
2018-08-20 11:42:07 +00:00
|
|
|
|
name: cgu_name.to_string(),
|
2018-08-20 15:13:01 +00:00
|
|
|
|
module_llvm: llvm_module,
|
2018-06-27 14:57:25 +00:00
|
|
|
|
kind: ModuleKind::Regular,
|
2019-05-28 22:21:27 +00:00
|
|
|
|
}
|
2017-09-14 03:26:39 +00:00
|
|
|
|
}
|
2020-01-05 01:10:23 +00:00
|
|
|
|
|
|
|
|
|
(module, cost)
|
2017-09-14 03:26:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-03 11:49:57 +00:00
|
|
|
|
pub fn set_link_section(llval: &Value, attrs: &CodegenFnAttrs) {
|
|
|
|
|
let sect = match attrs.link_section {
|
|
|
|
|
Some(name) => name,
|
|
|
|
|
None => return,
|
2018-02-10 22:28:17 +00:00
|
|
|
|
};
|
2018-10-03 11:49:57 +00:00
|
|
|
|
unsafe {
|
|
|
|
|
let buf = SmallCStr::new(§.as_str());
|
|
|
|
|
llvm::LLVMSetSection(llval, buf.as_ptr());
|
|
|
|
|
}
|
2017-09-13 22:24:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-12 18:04:46 +00:00
|
|
|
|
pub fn linkage_to_llvm(linkage: Linkage) -> llvm::Linkage {
|
|
|
|
|
match linkage {
|
|
|
|
|
Linkage::External => llvm::Linkage::ExternalLinkage,
|
|
|
|
|
Linkage::AvailableExternally => llvm::Linkage::AvailableExternallyLinkage,
|
|
|
|
|
Linkage::LinkOnceAny => llvm::Linkage::LinkOnceAnyLinkage,
|
|
|
|
|
Linkage::LinkOnceODR => llvm::Linkage::LinkOnceODRLinkage,
|
|
|
|
|
Linkage::WeakAny => llvm::Linkage::WeakAnyLinkage,
|
|
|
|
|
Linkage::WeakODR => llvm::Linkage::WeakODRLinkage,
|
|
|
|
|
Linkage::Appending => llvm::Linkage::AppendingLinkage,
|
|
|
|
|
Linkage::Internal => llvm::Linkage::InternalLinkage,
|
|
|
|
|
Linkage::Private => llvm::Linkage::PrivateLinkage,
|
|
|
|
|
Linkage::ExternalWeak => llvm::Linkage::ExternalWeakLinkage,
|
|
|
|
|
Linkage::Common => llvm::Linkage::CommonLinkage,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn visibility_to_llvm(linkage: Visibility) -> llvm::Visibility {
|
|
|
|
|
match linkage {
|
|
|
|
|
Visibility::Default => llvm::Visibility::Default,
|
|
|
|
|
Visibility::Hidden => llvm::Visibility::Hidden,
|
|
|
|
|
Visibility::Protected => llvm::Visibility::Protected,
|
2017-09-18 10:14:52 +00:00
|
|
|
|
}
|
2017-09-07 14:11:58 +00:00
|
|
|
|
}
|