Skip to content

Commit 0b99489

Browse files
authored
Rollup merge of #69965 - mark-i-m:codegen-utils, r=eddyb
Refactorings to get rid of rustc_codegen_utils r? @eddyb cc #45276 After this, the only modules left in `rustc_codegen_utils` are - `link`: a bunch of linking-related functions (many dealing with file names). These are mostly consumed by save analysis, rustc_driver, rustc_interface, and of course codegen. I assume they live here because we don't want a dependency of save analysis on codegen... Perhaps they can be moved to librustc? - ~`symbol_names` and `symbol_names_test`: honestly it seems odd that `symbol_names_test` is not a submodule of `symbol_names`. It seems like these could honestly live in their own crate or move to librustc. Already name mangling is exported as the `symbol_name` query.~ (move it to its own crate) I don't mind doing either of the above as part of this PR or a followup if you want.
2 parents 426a4cc + 2d75a33 commit 0b99489

File tree

32 files changed

+264
-289
lines changed

32 files changed

+264
-289
lines changed

Cargo.lock

+20-22
Original file line numberDiff line numberDiff line change
@@ -3567,7 +3567,6 @@ dependencies = [
35673567
"rustc_ast",
35683568
"rustc_attr",
35693569
"rustc_codegen_ssa",
3570-
"rustc_codegen_utils",
35713570
"rustc_data_structures",
35723571
"rustc_errors",
35733572
"rustc_feature",
@@ -3598,37 +3597,21 @@ dependencies = [
35983597
"rustc_apfloat",
35993598
"rustc_ast",
36003599
"rustc_attr",
3601-
"rustc_codegen_utils",
36023600
"rustc_data_structures",
36033601
"rustc_errors",
36043602
"rustc_fs_util",
36053603
"rustc_hir",
36063604
"rustc_incremental",
36073605
"rustc_index",
3606+
"rustc_metadata",
36083607
"rustc_session",
36093608
"rustc_span",
3609+
"rustc_symbol_mangling",
36103610
"rustc_target",
36113611
"serialize",
36123612
"tempfile",
36133613
]
36143614

3615-
[[package]]
3616-
name = "rustc_codegen_utils"
3617-
version = "0.0.0"
3618-
dependencies = [
3619-
"log",
3620-
"punycode",
3621-
"rustc",
3622-
"rustc-demangle",
3623-
"rustc_ast",
3624-
"rustc_data_structures",
3625-
"rustc_hir",
3626-
"rustc_metadata",
3627-
"rustc_session",
3628-
"rustc_span",
3629-
"rustc_target",
3630-
]
3631-
36323615
[[package]]
36333616
name = "rustc_data_structures"
36343617
version = "0.0.0"
@@ -3665,7 +3648,6 @@ dependencies = [
36653648
"rustc_ast",
36663649
"rustc_ast_pretty",
36673650
"rustc_codegen_ssa",
3668-
"rustc_codegen_utils",
36693651
"rustc_data_structures",
36703652
"rustc_error_codes",
36713653
"rustc_errors",
@@ -3814,7 +3796,6 @@ dependencies = [
38143796
"rustc_builtin_macros",
38153797
"rustc_codegen_llvm",
38163798
"rustc_codegen_ssa",
3817-
"rustc_codegen_utils",
38183799
"rustc_data_structures",
38193800
"rustc_errors",
38203801
"rustc_expand",
@@ -3832,6 +3813,7 @@ dependencies = [
38323813
"rustc_resolve",
38333814
"rustc_session",
38343815
"rustc_span",
3816+
"rustc_symbol_mangling",
38353817
"rustc_target",
38363818
"rustc_trait_selection",
38373819
"rustc_traits",
@@ -4071,7 +4053,6 @@ dependencies = [
40714053
"rustc",
40724054
"rustc_ast",
40734055
"rustc_ast_pretty",
4074-
"rustc_codegen_utils",
40754056
"rustc_data_structures",
40764057
"rustc_hir",
40774058
"rustc_parse",
@@ -4112,6 +4093,23 @@ dependencies = [
41124093
"unicode-width",
41134094
]
41144095

4096+
[[package]]
4097+
name = "rustc_symbol_mangling"
4098+
version = "0.0.0"
4099+
dependencies = [
4100+
"log",
4101+
"punycode",
4102+
"rustc",
4103+
"rustc-demangle",
4104+
"rustc_ast",
4105+
"rustc_data_structures",
4106+
"rustc_hir",
4107+
"rustc_metadata",
4108+
"rustc_session",
4109+
"rustc_span",
4110+
"rustc_target",
4111+
]
4112+
41154113
[[package]]
41164114
name = "rustc_target"
41174115
version = "0.0.0"

src/librustc/ty/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3148,6 +3148,7 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
31483148
context::provide(providers);
31493149
erase_regions::provide(providers);
31503150
layout::provide(providers);
3151+
super::util::bug::provide(providers);
31513152
*providers = ty::query::Providers {
31523153
trait_impls_of: trait_def::trait_impls_of_provider,
31533154
all_local_trait_impls: trait_def::all_local_trait_impls,

src/librustc/util/bug.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// These functions are used by macro expansion for bug! and span_bug!
22

3-
use crate::ty::tls;
3+
use crate::ty::{tls, TyCtxt};
44
use rustc_span::{MultiSpan, Span};
55
use std::fmt;
66

@@ -39,3 +39,17 @@ fn opt_span_bug_fmt<S: Into<MultiSpan>>(
3939
});
4040
unreachable!();
4141
}
42+
43+
/// A query to trigger a `delay_span_bug`. Clearly, if one has a `tcx` one can already trigger a
44+
/// `delay_span_bug`, so what is the point of this? It exists to help us test `delay_span_bug`'s
45+
/// interactions with the query system and incremental.
46+
pub fn trigger_delay_span_bug(tcx: TyCtxt<'_>, key: rustc_hir::def_id::DefId) {
47+
tcx.sess.delay_span_bug(
48+
tcx.def_span(key),
49+
"delayed span bug triggered by #[rustc_error(delay_span_bug_from_inside_query)]",
50+
);
51+
}
52+
53+
pub fn provide(providers: &mut crate::ty::query::Providers<'_>) {
54+
*providers = crate::ty::query::Providers { trigger_delay_span_bug, ..*providers };
55+
}

src/librustc_codegen_llvm/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ rustc = { path = "../librustc" }
2020
rustc-demangle = "0.1"
2121
rustc_attr = { path = "../librustc_attr" }
2222
rustc_codegen_ssa = { path = "../librustc_codegen_ssa" }
23-
rustc_codegen_utils = { path = "../librustc_codegen_utils" }
2423
rustc_data_structures = { path = "../librustc_data_structures" }
2524
rustc_errors = { path = "../librustc_errors" }
2625
rustc_feature = { path = "../librustc_feature" }

src/librustc_codegen_llvm/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use rustc_codegen_ssa::back::write::{CodegenContext, FatLTOInput, ModuleConfig};
2727
use rustc_codegen_ssa::traits::*;
2828
use rustc_codegen_ssa::ModuleCodegen;
2929
use rustc_codegen_ssa::{CodegenResults, CompiledModule};
30-
use rustc_codegen_utils::codegen_backend::CodegenBackend;
3130
use rustc_errors::{FatalError, Handler};
3231
use rustc_serialize::json;
3332
use rustc_session::config::{self, OptLevel, OutputFilenames, PrintRequest};

src/librustc_codegen_ssa/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ rustc_span = { path = "../librustc_span" }
2525
rustc = { path = "../librustc" }
2626
rustc_apfloat = { path = "../librustc_apfloat" }
2727
rustc_attr = { path = "../librustc_attr" }
28-
rustc_codegen_utils = { path = "../librustc_codegen_utils" }
28+
rustc_symbol_mangling = { path = "../librustc_symbol_mangling" }
2929
rustc_data_structures = { path = "../librustc_data_structures"}
3030
rustc_errors = { path = "../librustc_errors" }
3131
rustc_fs_util = { path = "../librustc_fs_util" }
@@ -34,3 +34,4 @@ rustc_incremental = { path = "../librustc_incremental" }
3434
rustc_index = { path = "../librustc_index" }
3535
rustc_target = { path = "../librustc_target" }
3636
rustc_session = { path = "../librustc_session" }
37+
rustc_metadata = { path = "../librustc_metadata" }

src/librustc_codegen_ssa/back/link.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_hir::def_id::CrateNum;
66
use rustc_session::config::{
77
self, CFGuard, DebugInfo, OutputFilenames, OutputType, PrintRequest, Sanitizer,
88
};
9+
use rustc_session::output::{check_file_is_writeable, invalid_output_for_target, out_filename};
910
use rustc_session::search_paths::PathKind;
1011
/// For all the linkers we support, and information they might
1112
/// need out of the shared crate context before we get rid of it.
@@ -36,8 +37,6 @@ use std::path::{Path, PathBuf};
3637
use std::process::{ExitStatus, Output, Stdio};
3738
use std::str;
3839

39-
pub use rustc_codegen_utils::link::*;
40-
4140
pub fn remove(sess: &Session, path: &Path) {
4241
if let Err(e) = fs::remove_file(path) {
4342
sess.err(&format!("failed to remove {}: {}", path.display(), e));

src/librustc_codegen_ssa/back/symbol_export.rs

+15-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc::ty::subst::{GenericArgKind, SubstsRef};
88
use rustc::ty::Instance;
99
use rustc::ty::{SymbolName, TyCtxt};
1010
use rustc_ast::expand::allocator::ALLOCATOR_METHODS;
11-
use rustc_codegen_utils::symbol_names;
1211
use rustc_data_structures::fingerprint::Fingerprint;
1312
use rustc_data_structures::fx::FxHashMap;
1413
use rustc_hir as hir;
@@ -423,17 +422,21 @@ pub fn symbol_name_for_instance_in_crate<'tcx>(
423422
// This is something instantiated in an upstream crate, so we have to use
424423
// the slower (because uncached) version of computing the symbol name.
425424
match symbol {
426-
ExportedSymbol::NonGeneric(def_id) => symbol_names::symbol_name_for_instance_in_crate(
427-
tcx,
428-
Instance::mono(tcx, def_id),
429-
instantiating_crate,
430-
),
431-
ExportedSymbol::Generic(def_id, substs) => symbol_names::symbol_name_for_instance_in_crate(
432-
tcx,
433-
Instance::new(def_id, substs),
434-
instantiating_crate,
435-
),
436-
ExportedSymbol::DropGlue(ty) => symbol_names::symbol_name_for_instance_in_crate(
425+
ExportedSymbol::NonGeneric(def_id) => {
426+
rustc_symbol_mangling::symbol_name_for_instance_in_crate(
427+
tcx,
428+
Instance::mono(tcx, def_id),
429+
instantiating_crate,
430+
)
431+
}
432+
ExportedSymbol::Generic(def_id, substs) => {
433+
rustc_symbol_mangling::symbol_name_for_instance_in_crate(
434+
tcx,
435+
Instance::new(def_id, substs),
436+
instantiating_crate,
437+
)
438+
}
439+
ExportedSymbol::DropGlue(ty) => rustc_symbol_mangling::symbol_name_for_instance_in_crate(
437440
tcx,
438441
Instance::resolve_drop_in_place(tcx, ty),
439442
instantiating_crate,

src/librustc_codegen_ssa/base.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ use rustc::ty::layout::{FAT_PTR_ADDR, FAT_PTR_EXTRA};
3636
use rustc::ty::query::Providers;
3737
use rustc::ty::{self, Instance, Ty, TyCtxt};
3838
use rustc_attr as attr;
39-
use rustc_codegen_utils::{check_for_rustc_errors_attr, symbol_names_test};
4039
use rustc_data_structures::fx::FxHashMap;
4140
use rustc_data_structures::profiling::print_time_passes_entry;
4241
use rustc_data_structures::sync::{par_iter, Lock, ParallelIterator};
@@ -47,6 +46,7 @@ use rustc_session::cgu_reuse_tracker::CguReuse;
4746
use rustc_session::config::{self, EntryFnType, Lto};
4847
use rustc_session::Session;
4948
use rustc_span::Span;
49+
use rustc_symbol_mangling::test as symbol_names_test;
5050

5151
use std::cmp;
5252
use std::ops::{Deref, DerefMut};
@@ -514,8 +514,6 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
514514
metadata: EncodedMetadata,
515515
need_metadata_module: bool,
516516
) -> OngoingCodegen<B> {
517-
check_for_rustc_errors_attr(tcx);
518-
519517
// Skip crate items and just output metadata in -Z no-codegen mode.
520518
if tcx.sess.opts.debugging_opts.no_codegen || !tcx.sess.opts.output_types.should_codegen() {
521519
let ongoing_codegen = start_async_codegen(backend, tcx, metadata, 1);

src/librustc_codegen_ssa/traits/backend.rs

+56-5
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@ use super::write::WriteBackendMethods;
22
use super::CodegenObject;
33
use crate::ModuleCodegen;
44

5-
use rustc::middle::cstore::EncodedMetadata;
5+
use rustc::dep_graph::DepGraph;
6+
use rustc::middle::cstore::{EncodedMetadata, MetadataLoaderDyn};
67
use rustc::ty::layout::{HasTyCtxt, LayoutOf, TyLayout};
7-
use rustc::ty::Ty;
8-
use rustc::ty::TyCtxt;
8+
use rustc::ty::query::Providers;
9+
use rustc::ty::{Ty, TyCtxt};
10+
use rustc::util::common::ErrorReported;
911
use rustc_ast::expand::allocator::AllocatorKind;
10-
use rustc_codegen_utils::codegen_backend::CodegenBackend;
11-
use rustc_session::{config, Session};
12+
use rustc_session::{
13+
config::{self, OutputFilenames, PrintRequest},
14+
Session,
15+
};
1216
use rustc_span::symbol::Symbol;
1317

18+
pub use rustc_data_structures::sync::MetadataRef;
19+
20+
use std::any::Any;
1421
use std::sync::Arc;
1522

1623
pub trait BackendTypes {
@@ -37,6 +44,50 @@ impl<'tcx, T> Backend<'tcx> for T where
3744
{
3845
}
3946

47+
pub trait CodegenBackend {
48+
fn init(&self, _sess: &Session) {}
49+
fn print(&self, _req: PrintRequest, _sess: &Session) {}
50+
fn target_features(&self, _sess: &Session) -> Vec<Symbol> {
51+
vec![]
52+
}
53+
fn print_passes(&self) {}
54+
fn print_version(&self) {}
55+
56+
fn metadata_loader(&self) -> Box<MetadataLoaderDyn>;
57+
fn provide(&self, _providers: &mut Providers<'_>);
58+
fn provide_extern(&self, _providers: &mut Providers<'_>);
59+
fn codegen_crate<'tcx>(
60+
&self,
61+
tcx: TyCtxt<'tcx>,
62+
metadata: EncodedMetadata,
63+
need_metadata_module: bool,
64+
) -> Box<dyn Any>;
65+
66+
/// This is called on the returned `Box<dyn Any>` from `codegen_backend`
67+
///
68+
/// # Panics
69+
///
70+
/// Panics when the passed `Box<dyn Any>` was not returned by `codegen_backend`.
71+
fn join_codegen(
72+
&self,
73+
ongoing_codegen: Box<dyn Any>,
74+
sess: &Session,
75+
dep_graph: &DepGraph,
76+
) -> Result<Box<dyn Any>, ErrorReported>;
77+
78+
/// This is called on the returned `Box<dyn Any>` from `join_codegen`
79+
///
80+
/// # Panics
81+
///
82+
/// Panics when the passed `Box<dyn Any>` was not returned by `join_codegen`.
83+
fn link(
84+
&self,
85+
sess: &Session,
86+
codegen_results: Box<dyn Any>,
87+
outputs: &OutputFilenames,
88+
) -> Result<(), ErrorReported>;
89+
}
90+
4091
pub trait ExtraBackendMethods: CodegenBackend + WriteBackendMethods + Sized + Send + Sync {
4192
fn new_metadata(&self, sess: TyCtxt<'_>, mod_name: &str) -> Self::Module;
4293
fn write_compressed_metadata<'tcx>(

src/librustc_codegen_ssa/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ mod write;
2929

3030
pub use self::abi::AbiBuilderMethods;
3131
pub use self::asm::{AsmBuilderMethods, AsmMethods};
32-
pub use self::backend::{Backend, BackendTypes, ExtraBackendMethods};
32+
pub use self::backend::{Backend, BackendTypes, CodegenBackend, ExtraBackendMethods};
3333
pub use self::builder::{BuilderMethods, OverflowOp};
3434
pub use self::consts::ConstMethods;
3535
pub use self::debuginfo::{DebugInfoBuilderMethods, DebugInfoMethods};

0 commit comments

Comments
 (0)