Skip to content

Enable optimizer #401

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

Merged
merged 2 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions crates/rustc_codegen_spirv/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustc_errors::FatalError;
use rustc_middle::bug;
use rustc_middle::dep_graph::WorkProduct;
use rustc_middle::middle::dependency_format::Linkage;
use rustc_session::config::{CrateType, Lto, OutputFilenames, OutputType};
use rustc_session::config::{CrateType, DebugInfo, Lto, OptLevel, OutputFilenames, OutputType};
use rustc_session::output::{check_file_is_writeable, invalid_output_for_target, out_filename};
use rustc_session::utils::NativeLibKind;
use rustc_session::Session;
Expand Down Expand Up @@ -124,7 +124,8 @@ fn link_exe(

let spv_binary = do_link(sess, &objects, &rlibs, legalize);

let spv_binary = if env::var("SPIRV_OPT").is_ok() {
let spv_binary = if sess.opts.optimize != OptLevel::No || sess.opts.debuginfo == DebugInfo::None
{
let _timer = sess.timer("link_spirv_opt");
do_spirv_opt(sess, spv_binary, out_filename)
} else {
Expand Down Expand Up @@ -157,10 +158,21 @@ fn do_spirv_opt(sess: &Session, spv_binary: Vec<u32>, filename: &Path) -> Vec<u3

let mut optimizer = opt::create(None);

optimizer
.register_size_passes()
.register_pass(opt::Passes::EliminateDeadConstant)
.register_pass(opt::Passes::StripDebugInfo);
match sess.opts.optimize {
OptLevel::No => {}
OptLevel::Less | OptLevel::Default | OptLevel::Aggressive => {
optimizer.register_performance_passes();
}
OptLevel::Size | OptLevel::SizeMin => {
optimizer.register_size_passes();
}
}

if sess.opts.debuginfo == DebugInfo::None {
optimizer
.register_pass(opt::Passes::EliminateDeadConstant)
.register_pass(opt::Passes::StripDebugInfo);
}

let result = optimizer.optimize(
&spv_binary,
Expand Down
12 changes: 11 additions & 1 deletion crates/spirv-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub enum MemoryModel {
pub struct SpirvBuilder {
path_to_crate: PathBuf,
print_metadata: bool,
release: bool,
spirv_version: Option<(u8, u8)>,
memory_model: Option<MemoryModel>,
}
Expand All @@ -44,6 +45,7 @@ impl SpirvBuilder {
Self {
path_to_crate: path_to_crate.as_ref().to_owned(),
print_metadata: true,
release: true,
spirv_version: None,
memory_model: None,
}
Expand All @@ -55,6 +57,12 @@ impl SpirvBuilder {
self
}

/// Build in release. Defaults to true.
pub fn release(mut self, v: bool) -> Self {
self.release = v;
self
}

/// Sets the SPIR-V binary version to use. Defaults to v1.3.
pub fn spirv_version(mut self, major: u8, minor: u8) -> Self {
self.spirv_version = Some((major, minor));
Expand Down Expand Up @@ -155,8 +163,10 @@ fn invoke_rustc(builder: &SpirvBuilder) -> Result<PathBuf, SpirvBuilderError> {
"build-std=core",
"--target",
"spirv-unknown-unknown",
"--release",
]);
if builder.release {
cargo.arg("--release");
}

// If we're nested in `cargo` invocation, use a different `--target-dir`,
// to avoid waiting on the same lock (which effectively dead-locks us).
Expand Down
3 changes: 3 additions & 0 deletions crates/spirv-builder/src/test/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@ pub fn main(i: Input<f32>, mut o: Output<f32>) {
}

#[test]
// Doesn't work, only worked before because I think it got optimized away before hitting the
// backend.
#[ignore]
fn allocate_const_scalar_pointer() {
val(r#"
use core::ptr::Unique;
Expand Down
5 changes: 5 additions & 0 deletions crates/spirv-builder/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ edition = "2018"
[lib]
crate-type = ["dylib"]

[profile.dev]
overflow-checks = false
debug-assertions = false

[dependencies]
spirv-std = { path = "../../crates/spirv-std" }

Expand Down Expand Up @@ -74,6 +78,7 @@ fn build(src: &str) -> PathBuf {
let project = setup(src).expect("Failed to set up project");
crate::SpirvBuilder::new(&project)
.print_metadata(false)
.release(false)
.build()
.expect("Failed to build test")
}
Expand Down