Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support compiling rustc without LLVM #42752

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/bootstrap/config.rs
Expand Up @@ -53,6 +53,7 @@ pub struct Config {
pub profiler: bool,

// llvm codegen options
pub llvm_enabled: bool,
pub llvm_assertions: bool,
pub llvm_optimize: bool,
pub llvm_release_debuginfo: bool,
Expand Down Expand Up @@ -182,6 +183,7 @@ struct Install {
/// TOML representation of how the LLVM build is configured.
#[derive(RustcDecodable, Default)]
struct Llvm {
enabled: Option<bool>,
ccache: Option<StringOrBool>,
ninja: Option<bool>,
assertions: Option<bool>,
Expand Down Expand Up @@ -252,6 +254,7 @@ struct TomlTarget {
impl Config {
pub fn parse(build: &str, file: Option<PathBuf>) -> Config {
let mut config = Config::default();
config.llvm_enabled = true;
config.llvm_optimize = true;
config.use_jemalloc = true;
config.backtrace = true;
Expand Down Expand Up @@ -345,6 +348,7 @@ impl Config {
Some(StringOrBool::Bool(false)) | None => {}
}
set(&mut config.ninja, llvm.ninja);
set(&mut config.llvm_enabled, llvm.enabled);
set(&mut config.llvm_assertions, llvm.assertions);
set(&mut config.llvm_optimize, llvm.optimize);
set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
Expand Down
3 changes: 3 additions & 0 deletions src/bootstrap/config.toml.example
Expand Up @@ -14,6 +14,9 @@
# =============================================================================
[llvm]

# Indicates whether rustc will support compilation with LLVM (currently broken)
#enabled = true

# Indicates whether the LLVM build is a Release or Debug build
#optimize = true

Expand Down
3 changes: 3 additions & 0 deletions src/bootstrap/lib.rs
Expand Up @@ -583,6 +583,9 @@ impl Build {
if self.config.use_jemalloc {
features.push_str(" jemalloc");
}
if self.config.llvm_enabled {
features.push_str(" llvm");
}
return features
}

Expand Down
5 changes: 5 additions & 0 deletions src/bootstrap/native.rs
Expand Up @@ -35,6 +35,11 @@ use build_helper::up_to_date;

/// Compile LLVM for `target`.
pub fn llvm(build: &Build, target: &str) {
// If we're not compiling for LLVM bail out here.
if !build.config.llvm_enabled {
return;
}

// If we're using a custom LLVM bail out here, but we can only use a
// custom LLVM for the build triple.
if let Some(config) = build.config.target_config.get(target) {
Expand Down
5 changes: 4 additions & 1 deletion src/librustc_driver/Cargo.toml
Expand Up @@ -29,9 +29,12 @@ rustc_plugin = { path = "../librustc_plugin" }
rustc_privacy = { path = "../librustc_privacy" }
rustc_resolve = { path = "../librustc_resolve" }
rustc_save_analysis = { path = "../librustc_save_analysis" }
rustc_trans = { path = "../librustc_trans" }
rustc_trans = { path = "../librustc_trans", optional=true }
rustc_typeck = { path = "../librustc_typeck" }
serialize = { path = "../libserialize" }
syntax = { path = "../libsyntax" }
syntax_ext = { path = "../libsyntax_ext" }
syntax_pos = { path = "../libsyntax_pos" }

[features]
llvm = ["rustc_trans"]
90 changes: 59 additions & 31 deletions src/librustc_driver/driver.rs
Expand Up @@ -31,7 +31,11 @@ use rustc_incremental::{self, IncrementalHashesMap};
use rustc_resolve::{MakeGlobMap, Resolver};
use rustc_metadata::creader::CrateLoader;
use rustc_metadata::cstore::{self, CStore};
#[cfg(feature="llvm")]
use rustc_trans::back::{link, write};
#[cfg(not(feature="llvm"))]
use ::link;
#[cfg(feature="llvm")]
use rustc_trans as trans;
use rustc_typeck as typeck;
use rustc_privacy;
Expand Down Expand Up @@ -111,7 +115,7 @@ pub fn compile_input(sess: &Session,
};

let outputs = build_output_filenames(input, outdir, output, &krate.attrs, sess);
let crate_name = link::find_crate_name(Some(sess), &krate.attrs, input);
let crate_name: String = link::find_crate_name(Some(sess), &krate.attrs, input);
let ExpansionResult { expanded_crate, defs, analysis, resolutions, mut hir_forest } = {
phase_2_configure_and_expand(
sess, &cstore, krate, registry, &crate_name, addl_plugins, control.make_glob_map,
Expand Down Expand Up @@ -204,55 +208,71 @@ pub fn compile_input(sess: &Session,
println!("Pre-trans");
tcx.print_debug_stats();
}
let trans = phase_4_translate_to_llvm(tcx, analysis, &incremental_hashes_map,
&outputs);

if log_enabled!(::log::LogLevel::Info) {
println!("Post-trans");
tcx.print_debug_stats();
}
#[cfg(feature="llvm")]
{
let trans = phase_4_translate_to_llvm(tcx, analysis, &incremental_hashes_map,
&outputs);

if log_enabled!(::log::LogLevel::Info) {
println!("Post-trans");
tcx.print_debug_stats();
}

if tcx.sess.opts.output_types.contains_key(&OutputType::Mir) {
if let Err(e) = mir::transform::dump_mir::emit_mir(tcx, &outputs) {
sess.err(&format!("could not emit MIR: {}", e));
sess.abort_if_errors();
if tcx.sess.opts.output_types.contains_key(&OutputType::Mir) {
if let Err(e) = mir::transform::dump_mir::emit_mir(tcx, &outputs) {
sess.err(&format!("could not emit MIR: {}", e));
sess.abort_if_errors();
}
}

return Ok((outputs, trans))
}

Ok((outputs, trans))
#[cfg(not(feature="llvm"))]
panic!("Unreachable")
})??
};

if sess.opts.debugging_opts.print_type_sizes {
sess.code_stats.borrow().print_type_sizes();
}

let phase5_result = phase_5_run_llvm_passes(sess, &trans, &outputs);
#[cfg(feature="llvm")]
{
let phase5_result = phase_5_run_llvm_passes(sess, &trans, &outputs);

controller_entry_point!(after_llvm,
sess,
CompileState::state_after_llvm(input, sess, outdir, output, &trans),
phase5_result);
phase5_result?;
controller_entry_point!(after_llvm,
sess,
CompileState::state_after_llvm(input, sess, outdir, output, &trans),
phase5_result);
phase5_result?;

write::cleanup_llvm(&trans);
write::cleanup_llvm(&trans);

phase_6_link_output(sess, &trans, &outputs);
phase_6_link_output(sess, &trans, &outputs);

// Now that we won't touch anything in the incremental compilation directory
// any more, we can finalize it (which involves renaming it)
rustc_incremental::finalize_session_directory(sess, trans.link.crate_hash);
// Now that we won't touch anything in the incremental compilation directory
// any more, we can finalize it (which involves renaming it)
rustc_incremental::finalize_session_directory(sess, trans.link.crate_hash);

if sess.opts.debugging_opts.perf_stats {
sess.print_perf_stats();
}
if sess.opts.debugging_opts.perf_stats {
sess.print_perf_stats();
}

controller_entry_point!(compilation_done,
sess,
CompileState::state_when_compilation_done(input,
sess,
outdir,
output),
Ok(()));

controller_entry_point!(compilation_done,
sess,
CompileState::state_when_compilation_done(input, sess, outdir, output),
Ok(()));
return Ok(())
}

Ok(())
#[cfg(not(feature="llvm"))]
panic!("Unreachable")
}

fn keep_hygiene_data(sess: &Session) -> bool {
Expand Down Expand Up @@ -355,6 +375,7 @@ pub struct CompileState<'a, 'tcx: 'a> {
pub resolutions: Option<&'a Resolutions>,
pub analysis: Option<&'a ty::CrateAnalysis>,
pub tcx: Option<TyCtxt<'a, 'tcx, 'tcx>>,
#[cfg(feature="llvm")]
pub trans: Option<&'a trans::CrateTranslation>,
}

Expand All @@ -381,6 +402,7 @@ impl<'a, 'tcx> CompileState<'a, 'tcx> {
resolutions: None,
analysis: None,
tcx: None,
#[cfg(feature="llvm")]
trans: None,
}
}
Expand Down Expand Up @@ -470,6 +492,7 @@ impl<'a, 'tcx> CompileState<'a, 'tcx> {
}


#[cfg(feature="llvm")]
fn state_after_llvm(input: &'a Input,
session: &'tcx Session,
out_dir: &'a Option<PathBuf>,
Expand Down Expand Up @@ -893,6 +916,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
mir::provide(&mut local_providers);
reachable::provide(&mut local_providers);
rustc_privacy::provide(&mut local_providers);
#[cfg(feature="llvm")]
trans::provide(&mut local_providers);
typeck::provide(&mut local_providers);
ty::provide(&mut local_providers);
Expand All @@ -904,6 +928,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,

let mut extern_providers = ty::maps::Providers::default();
cstore::provide(&mut extern_providers);
#[cfg(feature="llvm")]
trans::provide(&mut extern_providers);
ty::provide_extern(&mut extern_providers);
traits::provide_extern(&mut extern_providers);
Expand Down Expand Up @@ -1045,6 +1070,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,

/// Run the translation phase to LLVM, after which the AST and analysis can
/// be discarded.
#[cfg(feature="llvm")]
pub fn phase_4_translate_to_llvm<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
analysis: ty::CrateAnalysis,
incremental_hashes_map: &IncrementalHashesMap,
Expand Down Expand Up @@ -1076,6 +1102,7 @@ pub fn phase_4_translate_to_llvm<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,

/// Run LLVM itself, producing a bitcode file, assembly file or object file
/// as a side effect.
#[cfg(feature="llvm")]
pub fn phase_5_run_llvm_passes(sess: &Session,
trans: &trans::CrateTranslation,
outputs: &OutputFilenames) -> CompileResult {
Expand Down Expand Up @@ -1124,6 +1151,7 @@ pub fn phase_5_run_llvm_passes(sess: &Session,

/// Run the linker on any artifacts that resulted from the LLVM run.
/// This should produce either a finished executable or library.
#[cfg(feature="llvm")]
pub fn phase_6_link_output(sess: &Session,
trans: &trans::CrateTranslation,
outputs: &OutputFilenames) {
Expand Down