2018-10-04 15:23:10 +02:00
|
|
|
//! Interface of a Rust codegen backend
|
|
|
|
//!
|
|
|
|
//! This crate defines all the traits that have to be implemented by a codegen backend in order to
|
|
|
|
//! use the backend-agnostic codegen code in `rustc_codegen_ssa`.
|
|
|
|
//!
|
|
|
|
//! The interface is designed around two backend-specific data structures, the codegen context and
|
|
|
|
//! the builder. The codegen context is supposed to be read-only after its creation and during the
|
|
|
|
//! actual codegen, while the builder stores the information about the function during codegen and
|
|
|
|
//! is used to produce the instructions of the backend IR.
|
|
|
|
//!
|
2020-03-06 12:13:55 +01:00
|
|
|
//! Finally, a third `Backend` structure has to implement methods related to how codegen information
|
2018-10-04 15:23:10 +02:00
|
|
|
//! is passed to the backend, especially for asynchronous compilation.
|
|
|
|
//!
|
|
|
|
//! The traits contain associated types that are backend-specific, such as the backend's value or
|
|
|
|
//! basic blocks.
|
|
|
|
|
2018-10-03 13:49:57 +02:00
|
|
|
mod abi;
|
|
|
|
mod asm;
|
2018-10-01 10:32:09 +02:00
|
|
|
mod backend;
|
2018-10-03 13:49:57 +02:00
|
|
|
mod builder;
|
|
|
|
mod consts;
|
2020-06-21 23:29:08 -07:00
|
|
|
mod coverageinfo;
|
2018-10-03 13:49:57 +02:00
|
|
|
mod debuginfo;
|
2018-10-02 10:49:54 +02:00
|
|
|
mod declare;
|
2018-10-03 13:49:57 +02:00
|
|
|
mod intrinsic;
|
2018-10-02 10:49:54 +02:00
|
|
|
mod misc;
|
|
|
|
mod statics;
|
2018-10-03 13:49:57 +02:00
|
|
|
mod type_;
|
2018-10-23 17:01:35 +02:00
|
|
|
mod write;
|
2018-10-01 10:32:09 +02:00
|
|
|
|
2019-05-14 21:23:01 +05:30
|
|
|
pub use self::abi::AbiBuilderMethods;
|
2021-04-11 20:51:28 +01:00
|
|
|
pub use self::asm::{AsmBuilderMethods, AsmMethods, GlobalAsmOperandRef, InlineAsmOperandRef};
|
2020-03-12 18:07:58 -05:00
|
|
|
pub use self::backend::{Backend, BackendTypes, CodegenBackend, ExtraBackendMethods};
|
2018-11-24 16:44:17 +01:00
|
|
|
pub use self::builder::{BuilderMethods, OverflowOp};
|
2018-10-03 13:49:57 +02:00
|
|
|
pub use self::consts::ConstMethods;
|
2020-06-21 23:29:08 -07:00
|
|
|
pub use self::coverageinfo::{CoverageInfoBuilderMethods, CoverageInfoMethods};
|
2018-10-03 13:49:57 +02:00
|
|
|
pub use self::debuginfo::{DebugInfoBuilderMethods, DebugInfoMethods};
|
2020-09-18 13:06:53 +02:00
|
|
|
pub use self::declare::PreDefineMethods;
|
2018-11-24 16:44:17 +01:00
|
|
|
pub use self::intrinsic::IntrinsicCallMethods;
|
2018-10-02 10:49:54 +02:00
|
|
|
pub use self::misc::MiscMethods;
|
2018-11-26 18:36:58 +01:00
|
|
|
pub use self::statics::{StaticBuilderMethods, StaticMethods};
|
2018-10-03 13:49:57 +02:00
|
|
|
pub use self::type_::{
|
2019-10-29 19:29:42 +02:00
|
|
|
ArgAbiMethods, BaseTypeMethods, DerivedTypeMethods, LayoutTypeMethods, TypeMethods,
|
2018-10-03 13:49:57 +02:00
|
|
|
};
|
2018-10-23 17:01:35 +02:00
|
|
|
pub use self::write::{ModuleBufferMethods, ThinBufferMethods, WriteBackendMethods};
|
2019-05-04 15:02:22 +05:30
|
|
|
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::layout::{HasParamEnv, HasTyCtxt};
|
2019-05-17 02:20:14 +01:00
|
|
|
use rustc_target::spec::HasTargetSpec;
|
2018-10-01 10:32:09 +02:00
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
pub trait CodegenObject: Copy + PartialEq + fmt::Debug {}
|
|
|
|
impl<T: Copy + PartialEq + fmt::Debug> CodegenObject for T {}
|
2018-10-03 13:49:57 +02:00
|
|
|
|
|
|
|
pub trait CodegenMethods<'tcx>:
|
|
|
|
Backend<'tcx>
|
|
|
|
+ TypeMethods<'tcx>
|
|
|
|
+ MiscMethods<'tcx>
|
|
|
|
+ ConstMethods<'tcx>
|
2018-11-24 14:12:15 +01:00
|
|
|
+ StaticMethods
|
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
|
|
|
+ CoverageInfoMethods<'tcx>
|
2018-10-03 13:49:57 +02:00
|
|
|
+ DebugInfoMethods<'tcx>
|
2019-06-11 12:49:10 +03:00
|
|
|
+ AsmMethods
|
2018-10-03 13:49:57 +02:00
|
|
|
+ PreDefineMethods<'tcx>
|
2019-05-04 15:02:22 +05:30
|
|
|
+ HasParamEnv<'tcx>
|
2019-05-14 21:23:01 +05:30
|
|
|
+ HasTyCtxt<'tcx>
|
|
|
|
+ HasTargetSpec
|
2018-10-03 13:49:57 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx, T> CodegenMethods<'tcx> for T where
|
|
|
|
Self: Backend<'tcx>
|
|
|
|
+ TypeMethods<'tcx>
|
|
|
|
+ MiscMethods<'tcx>
|
|
|
|
+ ConstMethods<'tcx>
|
2018-11-24 14:12:15 +01:00
|
|
|
+ StaticMethods
|
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
|
|
|
+ CoverageInfoMethods<'tcx>
|
2018-10-03 13:49:57 +02:00
|
|
|
+ DebugInfoMethods<'tcx>
|
2019-06-11 12:49:10 +03:00
|
|
|
+ AsmMethods
|
2018-10-03 13:49:57 +02:00
|
|
|
+ PreDefineMethods<'tcx>
|
2019-05-04 15:02:22 +05:30
|
|
|
+ HasParamEnv<'tcx>
|
2019-05-14 21:23:01 +05:30
|
|
|
+ HasTyCtxt<'tcx>
|
|
|
|
+ HasTargetSpec
|
2018-11-24 16:15:26 +01:00
|
|
|
{
|
|
|
|
}
|
2018-10-03 13:49:57 +02:00
|
|
|
|
2018-11-27 18:35:35 +01:00
|
|
|
pub trait HasCodegen<'tcx>:
|
2020-10-13 10:17:05 +02:00
|
|
|
Backend<'tcx> + std::ops::Deref<Target = <Self as HasCodegen<'tcx>>::CodegenCx>
|
2018-11-27 18:35:35 +01:00
|
|
|
{
|
2018-10-03 13:49:57 +02:00
|
|
|
type CodegenCx: CodegenMethods<'tcx>
|
|
|
|
+ BackendTypes<
|
|
|
|
Value = Self::Value,
|
2019-10-13 11:28:19 +02:00
|
|
|
Function = Self::Function,
|
2018-10-03 13:49:57 +02:00
|
|
|
BasicBlock = Self::BasicBlock,
|
|
|
|
Type = Self::Type,
|
|
|
|
Funclet = Self::Funclet,
|
|
|
|
DIScope = Self::DIScope,
|
2020-02-10 22:52:30 +02:00
|
|
|
DILocation = Self::DILocation,
|
2020-01-26 18:50:13 +02:00
|
|
|
DIVariable = Self::DIVariable,
|
2019-05-05 23:39:04 +05:30
|
|
|
>;
|
2018-10-03 13:49:57 +02:00
|
|
|
}
|