rust/compiler/rustc_codegen_gcc/src/base.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

155 lines
6.2 KiB
Rust
Raw Normal View History

2020-05-10 09:54:30 -05:00
use std::env;
use std::time::Instant;
use gccjit::{
Context,
FunctionType,
GlobalKind,
};
use rustc_middle::dep_graph;
use rustc_middle::ty::TyCtxt;
use rustc_middle::mir::mono::Linkage;
use rustc_codegen_ssa::{ModuleCodegen, ModuleKind};
use rustc_codegen_ssa::base::maybe_create_entry_wrapper;
use rustc_codegen_ssa::mono_item::MonoItemExt;
use rustc_codegen_ssa::traits::DebugInfoMethods;
use rustc_session::config::DebugInfo;
use rustc_span::Symbol;
use crate::GccContext;
2020-05-10 09:54:30 -05:00
use crate::builder::Builder;
use crate::context::CodegenCx;
pub fn global_linkage_to_gcc(linkage: Linkage) -> GlobalKind {
match linkage {
Linkage::External => GlobalKind::Imported,
Linkage::AvailableExternally => GlobalKind::Imported,
Linkage::LinkOnceAny => unimplemented!(),
Linkage::LinkOnceODR => unimplemented!(),
Linkage::WeakAny => unimplemented!(),
Linkage::WeakODR => unimplemented!(),
Linkage::Appending => unimplemented!(),
Linkage::Internal => GlobalKind::Internal,
Linkage::Private => GlobalKind::Internal,
2021-08-15 07:28:46 -05:00
Linkage::ExternalWeak => GlobalKind::Imported, // TODO(antoyo): should be weak linkage.
2020-05-10 09:54:30 -05:00
Linkage::Common => unimplemented!(),
}
}
pub fn linkage_to_gcc(linkage: Linkage) -> FunctionType {
match linkage {
Linkage::External => FunctionType::Exported,
Linkage::AvailableExternally => FunctionType::Extern,
Linkage::LinkOnceAny => unimplemented!(),
Linkage::LinkOnceODR => unimplemented!(),
2021-08-15 07:28:46 -05:00
Linkage::WeakAny => FunctionType::Exported, // FIXME(antoyo): should be similar to linkonce.
2020-05-10 09:54:30 -05:00
Linkage::WeakODR => unimplemented!(),
Linkage::Appending => unimplemented!(),
Linkage::Internal => FunctionType::Internal,
Linkage::Private => FunctionType::Internal,
Linkage::ExternalWeak => unimplemented!(),
Linkage::Common => unimplemented!(),
}
}
pub fn compile_codegen_unit<'tcx>(tcx: TyCtxt<'tcx>, cgu_name: Symbol, supports_128bit_integers: bool) -> (ModuleCodegen<GccContext>, u64) {
2020-05-10 09:54:30 -05:00
let prof_timer = tcx.prof.generic_activity("codegen_module");
let start_time = Instant::now();
let dep_node = tcx.codegen_unit(cgu_name).codegen_dep_node(tcx);
Sync from rust (#107) * Rebase fallout. * Move rustc_middle::middle::cstore to rustc_session. * Create more accurate debuginfo for vtables. Before this commit all vtables would have the same name "vtable" in debuginfo. Now they get a name that identifies the implementing type and the trait that is being implemented. * Remove alloc::prelude As per the libs team decision in #58935. Closes #58935 * Make hash_result an Option. * Properly check `target_features` not to trigger an assertion * Add LLVM CFI support to the Rust compiler This commit adds LLVM Control Flow Integrity (CFI) support to the Rust compiler. It initially provides forward-edge control flow protection for Rust-compiled code only by aggregating function pointers in groups identified by their number of arguments. Forward-edge control flow protection for C or C++ and Rust -compiled code "mixed binaries" (i.e., for when C or C++ and Rust -compiled code share the same virtual address space) will be provided in later work as part of this project by defining and using compatible type identifiers (see Type metadata in the design document in the tracking issue #89653). LLVM CFI can be enabled with -Zsanitizer=cfi and requires LTO (i.e., -Clto). * Update to nightly-2021-10-30 * Add deduplication of constant values as rustc relies on LLVM doing that Co-authored-by: Camille GILLOT <gillot.camille@gmail.com> Co-authored-by: Michael Woerister <michaelwoerister@posteo> Co-authored-by: Amanieu d'Antras <amanieu@gmail.com> Co-authored-by: Yuki Okushi <yuki.okushi@huawei.com> Co-authored-by: Ramon de C Valle <rcvalle@users.noreply.github.com>
2021-10-30 17:21:33 -05:00
let (module, _) = tcx.dep_graph.with_task(
dep_node,
tcx,
(cgu_name, supports_128bit_integers),
Sync from rust (#107) * Rebase fallout. * Move rustc_middle::middle::cstore to rustc_session. * Create more accurate debuginfo for vtables. Before this commit all vtables would have the same name "vtable" in debuginfo. Now they get a name that identifies the implementing type and the trait that is being implemented. * Remove alloc::prelude As per the libs team decision in #58935. Closes #58935 * Make hash_result an Option. * Properly check `target_features` not to trigger an assertion * Add LLVM CFI support to the Rust compiler This commit adds LLVM Control Flow Integrity (CFI) support to the Rust compiler. It initially provides forward-edge control flow protection for Rust-compiled code only by aggregating function pointers in groups identified by their number of arguments. Forward-edge control flow protection for C or C++ and Rust -compiled code "mixed binaries" (i.e., for when C or C++ and Rust -compiled code share the same virtual address space) will be provided in later work as part of this project by defining and using compatible type identifiers (see Type metadata in the design document in the tracking issue #89653). LLVM CFI can be enabled with -Zsanitizer=cfi and requires LTO (i.e., -Clto). * Update to nightly-2021-10-30 * Add deduplication of constant values as rustc relies on LLVM doing that Co-authored-by: Camille GILLOT <gillot.camille@gmail.com> Co-authored-by: Michael Woerister <michaelwoerister@posteo> Co-authored-by: Amanieu d'Antras <amanieu@gmail.com> Co-authored-by: Yuki Okushi <yuki.okushi@huawei.com> Co-authored-by: Ramon de C Valle <rcvalle@users.noreply.github.com>
2021-10-30 17:21:33 -05:00
module_codegen,
Some(dep_graph::hash_result),
);
2020-05-10 09:54:30 -05:00
let time_to_codegen = start_time.elapsed();
drop(prof_timer);
// We assume that the cost to run GCC on a CGU is proportional to
// the time we needed for codegenning it.
let cost = time_to_codegen.as_secs() * 1_000_000_000 + time_to_codegen.subsec_nanos() as u64;
fn module_codegen(tcx: TyCtxt<'_>, (cgu_name, supports_128bit_integers): (Symbol, bool)) -> ModuleCodegen<GccContext> {
2020-05-10 09:54:30 -05:00
let cgu = tcx.codegen_unit(cgu_name);
// Instantiate monomorphizations without filling out definitions yet...
//let llvm_module = ModuleLlvm::new(tcx, &cgu_name.as_str());
let context = Context::default();
2021-08-15 07:28:46 -05:00
// TODO(antoyo): only set on x86 platforms.
2020-05-10 09:54:30 -05:00
context.add_command_line_option("-masm=intel");
2022-02-06 16:04:24 -06:00
// TODO(antoyo): only add the following cli argument if the feature is supported.
2022-03-27 11:05:23 -05:00
context.add_command_line_option("-msse2");
2022-02-06 16:04:24 -06:00
context.add_command_line_option("-mavx2");
2022-03-27 11:05:23 -05:00
context.add_command_line_option("-msha");
context.add_command_line_option("-mpclmul");
2022-02-06 16:04:24 -06:00
// FIXME(antoyo): the following causes an illegal instruction on vmovdqu64 in std_example on my CPU.
// Only add if the CPU supports it.
//context.add_command_line_option("-mavx512f");
2020-05-10 09:54:30 -05:00
for arg in &tcx.sess.opts.cg.llvm_args {
context.add_command_line_option(arg);
}
// NOTE: This is needed to compile the file src/intrinsic/archs.rs during a bootstrap of rustc.
context.add_command_line_option("-fno-var-tracking-assignments");
2021-10-25 16:48:15 -05:00
// NOTE: an optimization (https://github.com/rust-lang/rustc_codegen_gcc/issues/53).
2020-05-10 09:54:30 -05:00
context.add_command_line_option("-fno-semantic-interposition");
2021-10-25 16:48:15 -05:00
// NOTE: Rust relies on LLVM not doing TBAA (https://github.com/rust-lang/unsafe-code-guidelines/issues/292).
context.add_command_line_option("-fno-strict-aliasing");
if tcx.sess.opts.unstable_opts.function_sections.unwrap_or(tcx.sess.target.function_sections) {
context.add_command_line_option("-ffunction-sections");
context.add_command_line_option("-fdata-sections");
}
2021-08-15 07:28:46 -05:00
if env::var("CG_GCCJIT_DUMP_CODE").as_deref() == Ok("1") {
context.set_dump_code_on_compile(true);
}
2020-05-10 09:54:30 -05:00
if env::var("CG_GCCJIT_DUMP_GIMPLE").as_deref() == Ok("1") {
context.set_dump_initial_gimple(true);
}
context.set_debug_info(true);
2021-08-15 07:28:46 -05:00
if env::var("CG_GCCJIT_DUMP_EVERYTHING").as_deref() == Ok("1") {
context.set_dump_everything(true);
}
if env::var("CG_GCCJIT_KEEP_INTERMEDIATES").as_deref() == Ok("1") {
context.set_keep_intermediates(true);
}
2020-05-10 09:54:30 -05:00
// TODO(bjorn3): Remove once unwinding is properly implemented
context.set_allow_unreachable_blocks(true);
2020-05-10 09:54:30 -05:00
{
let cx = CodegenCx::new(&context, cgu, tcx, supports_128bit_integers);
2020-05-10 09:54:30 -05:00
let mono_items = cgu.items_in_deterministic_order(tcx);
for &(mono_item, (linkage, visibility)) in &mono_items {
mono_item.predefine::<Builder<'_, '_, '_>>(&cx, linkage, visibility);
}
// ... and now that we have everything pre-defined, fill out those definitions.
for &(mono_item, _) in &mono_items {
mono_item.define::<Builder<'_, '_, '_>>(&cx);
}
// If this codegen unit contains the main function, also create the
// wrapper here
maybe_create_entry_wrapper::<Builder<'_, '_, '_>>(&cx);
// Finalize debuginfo
if cx.sess().opts.debuginfo != DebugInfo::None {
cx.debuginfo_finalize();
}
}
ModuleCodegen {
name: cgu_name.to_string(),
module_llvm: GccContext {
context
},
kind: ModuleKind::Regular,
}
}
(module, cost)
}