Skip to content

Commit

Permalink
Rollup merge of rust-lang#76474 - bjorn3:driver_selected_codegen, r=o…
Browse files Browse the repository at this point in the history
…li-obk

Add option to pass a custom codegen backend from a driver

This allows the driver to pass information to the codegen backend. For example the headcrab debugger may in the future want to use cg_clif to JIT code to be injected in the debuggee. This would PR make it possible to tell cg_clif which symbol can be found at which address and to tell it to inject the JITed code into the right process.

This PR may also help with rust-lang/miri#1540 by allowing miri to provide a codegen backend that only emits metadata and doesn't perform any codegen.

cc @nbaksalyar (headcrab)
cc @RalfJung (miri)
  • Loading branch information
RalfJung authored Sep 28, 2020
2 parents f9dfbb4 + 71bc62b commit 94d13bb
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 5 deletions.
12 changes: 11 additions & 1 deletion compiler/rustc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ pub fn run_compiler(
callbacks: &mut (dyn Callbacks + Send),
file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
emitter: Option<Box<dyn Write + Send>>,
make_codegen_backend: Option<
Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>,
>,
) -> interface::Result<()> {
let mut args = Vec::new();
for arg in at_args {
Expand All @@ -162,6 +165,11 @@ pub fn run_compiler(
let sopts = config::build_session_options(&matches);
let cfg = interface::parse_cfgspecs(matches.opt_strs("cfg"));

// We wrap `make_codegen_backend` in another `Option` such that `dummy_config` can take
// ownership of it when necessary, while also allowing the non-dummy config to take ownership
// when `dummy_config` is not used.
let mut make_codegen_backend = Some(make_codegen_backend);

let mut dummy_config = |sopts, cfg, diagnostic_output| {
let mut config = interface::Config {
opts: sopts,
Expand All @@ -177,6 +185,7 @@ pub fn run_compiler(
lint_caps: Default::default(),
register_lints: None,
override_queries: None,
make_codegen_backend: make_codegen_backend.take().unwrap(),
registry: diagnostics_registry(),
};
callbacks.config(&mut config);
Expand Down Expand Up @@ -253,6 +262,7 @@ pub fn run_compiler(
lint_caps: Default::default(),
register_lints: None,
override_queries: None,
make_codegen_backend: make_codegen_backend.unwrap(),
registry: diagnostics_registry(),
};

Expand Down Expand Up @@ -1265,7 +1275,7 @@ pub fn main() -> ! {
})
})
.collect::<Vec<_>>();
run_compiler(&args, &mut callbacks, None, None)
run_compiler(&args, &mut callbacks, None, None, None)
});
// The extra `\t` is necessary to align this label with the others.
print_time_passes_entry(callbacks.time_passes, "\ttotal", start.elapsed());
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_interface/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ pub struct Config {
pub override_queries:
Option<fn(&Session, &mut ty::query::Providers, &mut ty::query::Providers)>,

/// This is a callback from the driver that is called to create a codegen backend.
pub make_codegen_backend:
Option<Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>>,

/// Registry of diagnostics codes.
pub registry: Registry,
}
Expand All @@ -167,6 +171,7 @@ pub fn create_compiler_and_run<R>(config: Config, f: impl FnOnce(&Compiler) -> R
config.file_loader,
config.input_path.clone(),
config.lint_caps,
config.make_codegen_backend,
registry.clone(),
);

Expand Down
10 changes: 9 additions & 1 deletion compiler/rustc_interface/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,17 @@ pub fn create_session(
file_loader: Option<Box<dyn FileLoader + Send + Sync + 'static>>,
input_path: Option<PathBuf>,
lint_caps: FxHashMap<lint::LintId, lint::Level>,
make_codegen_backend: Option<
Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>,
>,
descriptions: Registry,
) -> (Lrc<Session>, Lrc<Box<dyn CodegenBackend>>) {
let codegen_backend = get_codegen_backend(&sopts);
let codegen_backend = if let Some(make_codegen_backend) = make_codegen_backend {
make_codegen_backend(&sopts)
} else {
get_codegen_backend(&sopts)
};

// target_override is documented to be called before init(), so this is okay
let target_override = codegen_backend.target_override(&sopts);

Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ pub fn run_core(
(rustc_interface::DEFAULT_QUERY_PROVIDERS.typeck)(tcx, def_id)
};
}),
make_codegen_backend: None,
registry: rustc_driver::diagnostics_registry(),
};

Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/doctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pub fn run(options: Options) -> Result<(), ErrorReported> {
lint_caps,
register_lints: None,
override_queries: None,
make_codegen_backend: None,
registry: rustc_driver::diagnostics_registry(),
};

Expand Down
1 change: 1 addition & 0 deletions src/test/run-make-fulldeps/issue-19371/foo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
lint_caps: Default::default(),
register_lints: None,
override_queries: None,
make_codegen_backend: None,
registry: rustc_driver::diagnostics_registry(),
};

Expand Down
8 changes: 7 additions & 1 deletion src/test/ui-fulldeps/compiler-calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ fn main() {
let mut count = 1;
let args = vec!["compiler-calls".to_string(), "foo.rs".to_string()];
rustc_driver::catch_fatal_errors(|| {
rustc_driver::run_compiler(&args, &mut TestCalls { count: &mut count }, None, None).ok();
rustc_driver::run_compiler(
&args,
&mut TestCalls { count: &mut count },
None,
None,
None,
).ok();
}).ok();
assert_eq!(count, 2);
}
4 changes: 2 additions & 2 deletions src/tools/clippy/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ pub fn main() {
args.extend(vec!["--sysroot".into(), sys_root]);
};

return rustc_driver::run_compiler(&args, &mut DefaultCallbacks, None, None);
return rustc_driver::run_compiler(&args, &mut DefaultCallbacks, None, None, None);
}

if orig_args.iter().any(|a| a == "--version" || a == "-V") {
Expand Down Expand Up @@ -420,6 +420,6 @@ pub fn main() {
let mut default = DefaultCallbacks;
let callbacks: &mut (dyn rustc_driver::Callbacks + Send) =
if clippy_enabled { &mut clippy } else { &mut default };
rustc_driver::run_compiler(&args, callbacks, None, None)
rustc_driver::run_compiler(&args, callbacks, None, None, None)
}))
}

0 comments on commit 94d13bb

Please sign in to comment.