From b02e5cce169212bd4efe5857bc719d6ed47a53fc Mon Sep 17 00:00:00 2001 From: Denis Merigoux Date: Mon, 1 Oct 2018 10:32:09 +0200 Subject: [PATCH] Moved Backend interface into rustc_codegen_utils --- src/librustc_codegen_llvm/back/lto.rs | 3 +- src/librustc_codegen_llvm/back/write.rs | 4 +- src/librustc_codegen_llvm/base.rs | 26 +----- src/librustc_codegen_llvm/interfaces/asm.rs | 2 +- .../interfaces/builder.rs | 3 +- .../interfaces/debuginfo.rs | 2 +- .../interfaces/declare.rs | 2 +- .../interfaces/intrinsic.rs | 2 +- src/librustc_codegen_llvm/interfaces/misc.rs | 2 +- src/librustc_codegen_llvm/interfaces/mod.rs | 8 +- .../interfaces/statics.rs | 2 +- src/librustc_codegen_llvm/interfaces/type_.rs | 2 +- src/librustc_codegen_llvm/lib.rs | 54 +----------- src/librustc_codegen_utils/common.rs | 22 +++++ .../interfaces/backend.rs | 2 +- src/librustc_codegen_utils/interfaces/mod.rs | 18 ++++ src/librustc_codegen_utils/lib.rs | 88 +++++++++++++++---- 17 files changed, 129 insertions(+), 113 deletions(-) rename src/{librustc_codegen_llvm => librustc_codegen_utils}/interfaces/backend.rs (98%) create mode 100644 src/librustc_codegen_utils/interfaces/mod.rs diff --git a/src/librustc_codegen_llvm/back/lto.rs b/src/librustc_codegen_llvm/back/lto.rs index 5eb2e28a2a86..de69531fa8bf 100644 --- a/src/librustc_codegen_llvm/back/lto.rs +++ b/src/librustc_codegen_llvm/back/lto.rs @@ -24,7 +24,8 @@ use rustc::util::common::time_ext; use rustc_data_structures::fx::FxHashMap; use rustc_codegen_utils::symbol_export; use time_graph::Timeline; -use {ModuleCodegen, ModuleLlvm, ModuleKind}; +use ModuleLlvm; +use rustc_codegen_utils::{ModuleCodegen, ModuleKind}; use libc; diff --git a/src/librustc_codegen_llvm/back/write.rs b/src/librustc_codegen_llvm/back/write.rs index 72820f967afa..d96dd93e3e2c 100644 --- a/src/librustc_codegen_llvm/back/write.rs +++ b/src/librustc_codegen_llvm/back/write.rs @@ -26,8 +26,8 @@ use rustc::util::nodemap::FxHashMap; use time_graph::{self, TimeGraph, Timeline}; use llvm::{self, DiagnosticInfo, PassManager, SMDiagnostic}; use llvm_util; -use {CodegenResults, ModuleCodegen, CompiledModule, ModuleKind, ModuleLlvm, - CachedModuleCodegen}; +use {CodegenResults, ModuleLlvm}; +use rustc_codegen_utils::{ModuleCodegen, ModuleKind, CachedModuleCodegen, CompiledModule}; use CrateInfo; use rustc::hir::def_id::{CrateNum, LOCAL_CRATE}; use rustc::ty::TyCtxt; diff --git a/src/librustc_codegen_llvm/base.rs b/src/librustc_codegen_llvm/base.rs index 5a11bade2748..d8f5c25714f6 100644 --- a/src/librustc_codegen_llvm/base.rs +++ b/src/librustc_codegen_llvm/base.rs @@ -24,9 +24,7 @@ //! int) and rec(x=int, y=int, z=int) will have the same llvm::Type. use super::ModuleLlvm; -use super::ModuleCodegen; -use super::ModuleKind; -use super::CachedModuleCodegen; +use rustc_codegen_utils::{ModuleCodegen, ModuleKind, CachedModuleCodegen}; use super::LlvmCodegenBackend; use abi; @@ -1197,25 +1195,3 @@ pub fn visibility_to_llvm(linkage: Visibility) -> llvm::Visibility { Visibility::Protected => llvm::Visibility::Protected, } } - -// FIXME(mw): Anything that is produced via DepGraph::with_task() must implement -// the HashStable trait. Normally DepGraph::with_task() calls are -// hidden behind queries, but CGU creation is a special case in two -// ways: (1) it's not a query and (2) CGU are output nodes, so their -// Fingerprints are not actually needed. It remains to be clarified -// how exactly this case will be handled in the red/green system but -// for now we content ourselves with providing a no-op HashStable -// implementation for CGUs. -mod temp_stable_hash_impls { - use rustc_data_structures::stable_hasher::{StableHasherResult, StableHasher, - HashStable}; - use {ModuleCodegen, ModuleLlvm}; - - impl HashStable for ModuleCodegen { - fn hash_stable(&self, - _: &mut HCX, - _: &mut StableHasher) { - // do nothing - } - } -} diff --git a/src/librustc_codegen_llvm/interfaces/asm.rs b/src/librustc_codegen_llvm/interfaces/asm.rs index 33c751cc1d97..ffe9679fcd6c 100644 --- a/src/librustc_codegen_llvm/interfaces/asm.rs +++ b/src/librustc_codegen_llvm/interfaces/asm.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use super::backend::Backend; +use super::Backend; use super::HasCodegen; use mir::place::PlaceRef; use rustc::hir::{GlobalAsm, InlineAsm}; diff --git a/src/librustc_codegen_llvm/interfaces/builder.rs b/src/librustc_codegen_llvm/interfaces/builder.rs index 61e60d55e098..74a31975a390 100644 --- a/src/librustc_codegen_llvm/interfaces/builder.rs +++ b/src/librustc_codegen_llvm/interfaces/builder.rs @@ -213,7 +213,8 @@ pub trait BuilderMethods<'a, 'tcx: 'a>: fn set_cleanup(&self, landing_pad: Self::Value); fn resume(&self, exn: Self::Value) -> Self::Value; fn cleanup_pad(&self, parent: Option, args: &[Self::Value]) -> Self::Funclet; - fn cleanup_ret(&self, funclet: &Self::Funclet, unwind: Option) -> Self::Value; + fn cleanup_ret(&self, funclet: &Self::Funclet, unwind: Option) + -> Self::Value; fn catch_pad(&self, parent: Self::Value, args: &[Self::Value]) -> Self::Funclet; fn catch_ret(&self, funclet: &Self::Funclet, unwind: Self::BasicBlock) -> Self::Value; fn catch_switch( diff --git a/src/librustc_codegen_llvm/interfaces/debuginfo.rs b/src/librustc_codegen_llvm/interfaces/debuginfo.rs index 4e24e12bc686..24f6cb85c7bd 100644 --- a/src/librustc_codegen_llvm/interfaces/debuginfo.rs +++ b/src/librustc_codegen_llvm/interfaces/debuginfo.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use super::backend::Backend; +use super::Backend; use super::HasCodegen; use debuginfo::{FunctionDebugContext, MirDebugScope, VariableAccess, VariableKind}; use monomorphize::Instance; diff --git a/src/librustc_codegen_llvm/interfaces/declare.rs b/src/librustc_codegen_llvm/interfaces/declare.rs index 5c9aedd1a3be..373220fd345e 100644 --- a/src/librustc_codegen_llvm/interfaces/declare.rs +++ b/src/librustc_codegen_llvm/interfaces/declare.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use super::backend::Backend; +use super::Backend; use monomorphize::Instance; use rustc::hir::def_id::DefId; use rustc::mir::mono::{Linkage, Visibility}; diff --git a/src/librustc_codegen_llvm/interfaces/intrinsic.rs b/src/librustc_codegen_llvm/interfaces/intrinsic.rs index 5f2994c1a830..1ea377b5a1d0 100644 --- a/src/librustc_codegen_llvm/interfaces/intrinsic.rs +++ b/src/librustc_codegen_llvm/interfaces/intrinsic.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use super::backend::Backend; +use super::Backend; use super::HasCodegen; use abi::FnType; use mir::operand::OperandRef; diff --git a/src/librustc_codegen_llvm/interfaces/misc.rs b/src/librustc_codegen_llvm/interfaces/misc.rs index 2557b51b76de..6e6af597a763 100644 --- a/src/librustc_codegen_llvm/interfaces/misc.rs +++ b/src/librustc_codegen_llvm/interfaces/misc.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use super::backend::Backend; +use super::Backend; use libc::c_uint; use monomorphize::partitioning::CodegenUnit; use rustc::mir::mono::Stats; diff --git a/src/librustc_codegen_llvm/interfaces/mod.rs b/src/librustc_codegen_llvm/interfaces/mod.rs index f1040020aff2..61aa7e29326b 100644 --- a/src/librustc_codegen_llvm/interfaces/mod.rs +++ b/src/librustc_codegen_llvm/interfaces/mod.rs @@ -10,7 +10,6 @@ mod abi; mod asm; -mod backend; mod builder; mod consts; mod debuginfo; @@ -22,7 +21,6 @@ mod type_; pub use self::abi::{AbiBuilderMethods, AbiMethods}; pub use self::asm::{AsmBuilderMethods, AsmMethods}; -pub use self::backend::{Backend, BackendMethods, BackendTypes}; pub use self::builder::BuilderMethods; pub use self::consts::ConstMethods; pub use self::debuginfo::{DebugInfoBuilderMethods, DebugInfoMethods}; @@ -33,8 +31,7 @@ pub use self::statics::StaticMethods; pub use self::type_::{ ArgTypeMethods, BaseTypeMethods, DerivedTypeMethods, LayoutTypeMethods, TypeMethods, }; - -use std::fmt; +pub use rustc_codegen_utils::interfaces::{Backend, BackendMethods, BackendTypes, CodegenObject}; pub trait CodegenMethods<'tcx>: Backend<'tcx> @@ -76,6 +73,3 @@ pub trait HasCodegen<'tcx>: Backend<'tcx> { DIScope = Self::DIScope, >; } - -pub trait CodegenObject: Copy + PartialEq + fmt::Debug {} -impl CodegenObject for T {} diff --git a/src/librustc_codegen_llvm/interfaces/statics.rs b/src/librustc_codegen_llvm/interfaces/statics.rs index 0feb9d5255f5..27748a8dd427 100644 --- a/src/librustc_codegen_llvm/interfaces/statics.rs +++ b/src/librustc_codegen_llvm/interfaces/statics.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use super::backend::Backend; +use super::Backend; use rustc::hir::def_id::DefId; use rustc::ty::layout::Align; diff --git a/src/librustc_codegen_llvm/interfaces/type_.rs b/src/librustc_codegen_llvm/interfaces/type_.rs index a03bcac5ecd8..775ca2d2d026 100644 --- a/src/librustc_codegen_llvm/interfaces/type_.rs +++ b/src/librustc_codegen_llvm/interfaces/type_.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use super::backend::Backend; +use super::Backend; use super::HasCodegen; use mir::place::PlaceRef; use rustc::ty::layout::TyLayout; diff --git a/src/librustc_codegen_llvm/lib.rs b/src/librustc_codegen_llvm/lib.rs index 395fbae6aad8..e8f488ab04c1 100644 --- a/src/librustc_codegen_llvm/lib.rs +++ b/src/librustc_codegen_llvm/lib.rs @@ -37,7 +37,6 @@ #![feature(static_nobundle)] use back::write::create_target_machine; -use rustc::dep_graph::WorkProduct; use syntax_pos::symbol::Symbol; #[macro_use] extern crate bitflags; @@ -67,7 +66,6 @@ extern crate cc; // Used to locate MSVC extern crate tempfile; extern crate memmap; -use back::bytecode::RLIB_BYTECODE_EXTENSION; use interfaces::*; use time_graph::TimeGraph; use std::sync::mpsc::Receiver; @@ -93,7 +91,7 @@ use rustc::util::time_graph; use rustc::util::nodemap::{FxHashSet, FxHashMap}; use rustc::util::profiling::ProfileCategory; use rustc_mir::monomorphize; -use rustc_codegen_utils::{CompiledModule, ModuleKind}; +use rustc_codegen_utils::{ModuleCodegen, CompiledModule}; use rustc_codegen_utils::codegen_backend::CodegenBackend; use rustc_data_structures::svh::Svh; @@ -329,56 +327,6 @@ pub fn __rustc_codegen_backend() -> Box { LlvmCodegenBackend::new() } -pub struct ModuleCodegen { - /// The name of the module. When the crate may be saved between - /// compilations, incremental compilation requires that name be - /// unique amongst **all** crates. Therefore, it should contain - /// something unique to this crate (e.g., a module path) as well - /// as the crate name and disambiguator. - /// We currently generate these names via CodegenUnit::build_cgu_name(). - name: String, - module_llvm: M, - kind: ModuleKind, -} - -struct CachedModuleCodegen { - name: String, - source: WorkProduct, -} - -impl ModuleCodegen { - fn into_compiled_module(self, - emit_obj: bool, - emit_bc: bool, - emit_bc_compressed: bool, - outputs: &OutputFilenames) -> CompiledModule { - let object = if emit_obj { - Some(outputs.temp_path(OutputType::Object, Some(&self.name))) - } else { - None - }; - let bytecode = if emit_bc { - Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name))) - } else { - None - }; - let bytecode_compressed = if emit_bc_compressed { - Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name)) - .with_extension(RLIB_BYTECODE_EXTENSION)) - } else { - None - }; - - CompiledModule { - name: self.name.clone(), - kind: self.kind, - object, - bytecode, - bytecode_compressed, - } - } -} - pub struct ModuleLlvm { llcx: &'static mut llvm::Context, llmod_raw: *const llvm::Module, diff --git a/src/librustc_codegen_utils/common.rs b/src/librustc_codegen_utils/common.rs index c274fa4345ab..3f4389913ae4 100644 --- a/src/librustc_codegen_utils/common.rs +++ b/src/librustc_codegen_utils/common.rs @@ -113,3 +113,25 @@ pub enum TypeKind { X86_MMX, Token, } + +// FIXME(mw): Anything that is produced via DepGraph::with_task() must implement +// the HashStable trait. Normally DepGraph::with_task() calls are +// hidden behind queries, but CGU creation is a special case in two +// ways: (1) it's not a query and (2) CGU are output nodes, so their +// Fingerprints are not actually needed. It remains to be clarified +// how exactly this case will be handled in the red/green system but +// for now we content ourselves with providing a no-op HashStable +// implementation for CGUs. +mod temp_stable_hash_impls { + use rustc_data_structures::stable_hasher::{StableHasherResult, StableHasher, + HashStable}; + use ModuleCodegen; + + impl HashStable for ModuleCodegen { + fn hash_stable(&self, + _: &mut HCX, + _: &mut StableHasher) { + // do nothing + } + } +} diff --git a/src/librustc_codegen_llvm/interfaces/backend.rs b/src/librustc_codegen_utils/interfaces/backend.rs similarity index 98% rename from src/librustc_codegen_llvm/interfaces/backend.rs rename to src/librustc_codegen_utils/interfaces/backend.rs index d21e39769af5..3cdb1c6a0dde 100644 --- a/src/librustc_codegen_llvm/interfaces/backend.rs +++ b/src/librustc_codegen_utils/interfaces/backend.rs @@ -17,10 +17,10 @@ use rustc::middle::cstore::EncodedMetadata; use rustc::mir::mono::Stats; use rustc::session::Session; use rustc::ty::TyCtxt; +use rustc::util::time_graph::TimeGraph; use std::any::Any; use std::sync::mpsc::Receiver; use syntax_pos::symbol::InternedString; -use time_graph::TimeGraph; use ModuleCodegen; pub trait BackendTypes { diff --git a/src/librustc_codegen_utils/interfaces/mod.rs b/src/librustc_codegen_utils/interfaces/mod.rs new file mode 100644 index 000000000000..f958dbabe68a --- /dev/null +++ b/src/librustc_codegen_utils/interfaces/mod.rs @@ -0,0 +1,18 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +mod backend; + +pub use self::backend::{Backend, BackendMethods, BackendTypes}; + +use std::fmt; + +pub trait CodegenObject: Copy + PartialEq + fmt::Debug {} +impl CodegenObject for T {} diff --git a/src/librustc_codegen_utils/lib.rs b/src/librustc_codegen_utils/lib.rs index 2141a763d165..4fb182e4f054 100644 --- a/src/librustc_codegen_utils/lib.rs +++ b/src/librustc_codegen_utils/lib.rs @@ -21,6 +21,7 @@ #![feature(custom_attribute)] #![feature(nll)] #![allow(unused_attributes)] +#![allow(dead_code)] #![feature(quote)] #![feature(rustc_diagnostic_macros)] @@ -46,8 +47,11 @@ use std::path::PathBuf; use rustc::session::Session; use rustc::ty::TyCtxt; +use rustc::dep_graph::WorkProduct; +use rustc::session::config::{OutputFilenames, OutputType}; pub mod command; +pub mod interfaces; pub mod link; pub mod linker; pub mod codegen_backend; @@ -56,6 +60,74 @@ pub mod symbol_names; pub mod symbol_names_test; pub mod common; +pub struct ModuleCodegen { + /// The name of the module. When the crate may be saved between + /// compilations, incremental compilation requires that name be + /// unique amongst **all** crates. Therefore, it should contain + /// something unique to this crate (e.g., a module path) as well + /// as the crate name and disambiguator. + /// We currently generate these names via CodegenUnit::build_cgu_name(). + pub name: String, + pub module_llvm: M, + pub kind: ModuleKind, +} + +pub const RLIB_BYTECODE_EXTENSION: &str = "bc.z"; + +impl ModuleCodegen { + pub fn into_compiled_module(self, + emit_obj: bool, + emit_bc: bool, + emit_bc_compressed: bool, + outputs: &OutputFilenames) -> CompiledModule { + let object = if emit_obj { + Some(outputs.temp_path(OutputType::Object, Some(&self.name))) + } else { + None + }; + let bytecode = if emit_bc { + Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name))) + } else { + None + }; + let bytecode_compressed = if emit_bc_compressed { + Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name)) + .with_extension(RLIB_BYTECODE_EXTENSION)) + } else { + None + }; + + CompiledModule { + name: self.name.clone(), + kind: self.kind, + object, + bytecode, + bytecode_compressed, + } + } +} + +#[derive(Debug)] +pub struct CompiledModule { + pub name: String, + pub kind: ModuleKind, + pub object: Option, + pub bytecode: Option, + pub bytecode_compressed: Option, +} + +pub struct CachedModuleCodegen { + pub name: String, + pub source: WorkProduct, +} + +#[derive(Copy, Clone, Debug, PartialEq)] +pub enum ModuleKind { + Regular, + Metadata, + Allocator, +} + /// check for the #[rustc_error] annotation, which forces an /// error in codegen. This is used to write compile-fail tests /// that actually test that compilation succeeds without @@ -70,22 +142,6 @@ pub fn check_for_rustc_errors_attr(tcx: TyCtxt) { } } -#[derive(Copy, Clone, Debug, PartialEq)] -pub enum ModuleKind { - Regular, - Metadata, - Allocator, -} - -#[derive(Debug)] -pub struct CompiledModule { - pub name: String, - pub kind: ModuleKind, - pub object: Option, - pub bytecode: Option, - pub bytecode_compressed: Option, -} - pub fn find_library(name: &str, search_paths: &[PathBuf], sess: &Session) -> PathBuf { // On Windows, static libraries sometimes show up as libfoo.a and other