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

Optimize RustcCodegenFlags #1305

Merged
merged 6 commits into from
Dec 4, 2024
Merged
Changes from 3 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
74 changes: 36 additions & 38 deletions src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ use std::ffi::OsString;
use std::path::Path;

#[derive(Debug, PartialEq, Default)]
pub(crate) struct RustcCodegenFlags {
branch_protection: Option<String>,
code_model: Option<String>,
pub(crate) struct RustcCodegenFlags<'a> {
branch_protection: Option<&'a str>,
code_model: Option<&'a str>,
no_vectorize_loops: bool,
no_vectorize_slp: bool,
profile_generate: Option<String>,
profile_use: Option<String>,
control_flow_guard: Option<String>,
lto: Option<String>,
relocation_model: Option<String>,
profile_generate: Option<&'a str>,
profile_use: Option<&'a str>,
control_flow_guard: Option<&'a str>,
lto: Option<&'a str>,
relocation_model: Option<&'a str>,
embed_bitcode: Option<bool>,
force_frame_pointers: Option<bool>,
link_dead_code: Option<bool>,
no_redzone: Option<bool>,
soft_float: Option<bool>,
}

impl RustcCodegenFlags {
impl<'this> RustcCodegenFlags<'this> {
// Parse flags obtained from CARGO_ENCODED_RUSTFLAGS
pub(crate) fn parse(rustflags_env: &str) -> Result<RustcCodegenFlags, Error> {
pub(crate) fn parse(rustflags_env: &'this str) -> Result<Self, Error> {
fn is_flag_prefix(flag: &str) -> bool {
[
"-Z",
Expand Down Expand Up @@ -89,12 +89,12 @@ impl RustcCodegenFlags {
}

let (flag, value) = if let Some((flag, value)) = flag.split_once('=') {
(flag, Some(value.to_owned()))
(flag, Some(value))
} else {
(flag, None)
};

fn flag_ok_or(flag: Option<String>, msg: &'static str) -> Result<String, Error> {
fn flag_ok_or<'flag>(flag: Option<&'flag str>, msg: &'static str) -> Result<&'flag str, Error> {
flag.ok_or(Error::new(ErrorKind::InvalidFlag, msg))
}

Expand Down Expand Up @@ -176,13 +176,13 @@ impl RustcCodegenFlags {
match family {
ToolFamily::Clang { .. } | ToolFamily::Gnu => {
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mbranch-protection
if let Some(value) = &self.branch_protection {
if let Some(value) = self.branch_protection {
push_if_supported(
format!("-mbranch-protection={}", value.replace(",", "+")).into(),
);
}
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mcmodel
if let Some(value) = &self.code_model {
if let Some(value) = self.code_model {
push_if_supported(format!("-mcmodel={value}").into());
}
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fno-vectorize
Expand All @@ -194,16 +194,16 @@ impl RustcCodegenFlags {
push_if_supported("-fno-slp-vectorize".into());
}
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fprofile-generate
if let Some(value) = &self.profile_generate {
if let Some(value) = self.profile_generate {
push_if_supported(format!("-fprofile-generate={value}").into());
}
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fprofile-use
if let Some(value) = &self.profile_use {
if let Some(value) = self.profile_use {
push_if_supported(format!("-fprofile-use={value}").into());
}
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mguard
if let Some(value) = &self.control_flow_guard {
let cc_val = match value.as_str() {
if let Some(value) = self.control_flow_guard {
let cc_val = match value {
"y" | "yes" | "on" | "true" | "checks" => Some("cf"),
"nochecks" => Some("cf-nochecks"),
"n" | "no" | "off" | "false" => Some("none"),
Expand All @@ -214,8 +214,8 @@ impl RustcCodegenFlags {
}
}
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-flto
if let Some(value) = &self.lto {
let cc_val = match value.as_str() {
if let Some(value) = self.lto {
let cc_val = match value {
"y" | "yes" | "on" | "true" | "fat" => Some("full"),
"thin" => Some("thin"),
_ => None,
Expand All @@ -227,8 +227,8 @@ impl RustcCodegenFlags {
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fPIC
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fPIE
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mdynamic-no-pic
if let Some(value) = &self.relocation_model {
let cc_flag = match value.as_str() {
if let Some(value) = self.relocation_model {
let cc_flag = match value {
"pic" => Some("-fPIC"),
"pie" => Some("-fPIE"),
"dynamic-no-pic" => Some("-mdynamic-no-pic"),
Expand All @@ -239,30 +239,28 @@ impl RustcCodegenFlags {
}
}
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fembed-bitcode
if let Some(value) = &self.embed_bitcode {
let cc_val = if *value { "all" } else { "off" };
if let Some(value) = self.embed_bitcode {
let cc_val = if value { "all" } else { "off" };
push_if_supported(format!("-fembed-bitcode={cc_val}").into());
}
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fno-omit-frame-pointer
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fomit-frame-pointer
if let Some(value) = &self.force_frame_pointers {
let cc_flag = if *value {
if let Some(value) = self.force_frame_pointers {
let cc_flag = if value {
"-fno-omit-frame-pointer"
} else {
"-fomit-frame-pointer"
};
push_if_supported(cc_flag.into());
}
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-dead_strip
if let Some(value) = &self.link_dead_code {
if !value {
push_if_supported("-dead_strip".into());
}
if let Some(false) = self.link_dead_code {
push_if_supported("-dead_strip".into());
}
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mno-red-zone
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mred-zone
if let Some(value) = &self.no_redzone {
let cc_flag = if *value {
if let Some(value) = self.no_redzone {
let cc_flag = if value {
"-mno-red-zone"
} else {
"-mred-zone"
Expand All @@ -271,8 +269,8 @@ impl RustcCodegenFlags {
}
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-msoft-float
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mno-soft-float
if let Some(value) = &self.soft_float {
let cc_flag = if *value {
if let Some(value) = self.soft_float {
let cc_flag = if value {
"-msoft-float"
} else {
"-mno-soft-float"
Expand All @@ -282,8 +280,8 @@ impl RustcCodegenFlags {
}
ToolFamily::Msvc { .. } => {
// https://learn.microsoft.com/en-us/cpp/build/reference/guard-enable-control-flow-guard
if let Some(value) = &self.control_flow_guard {
let cc_val = match value.as_str() {
if let Some(value) = self.control_flow_guard {
let cc_val = match value {
"y" | "yes" | "on" | "true" | "checks" => Some("cf"),
"n" | "no" | "off" | "false" => Some("cf-"),
_ => None,
Expand All @@ -293,8 +291,8 @@ impl RustcCodegenFlags {
}
}
// https://learn.microsoft.com/en-us/cpp/build/reference/oy-frame-pointer-omission
if let Some(value) = &self.force_frame_pointers {
let cc_flag = if *value { "/Oy-" } else { "/Oy" };
if let Some(value) = self.force_frame_pointers {
let cc_flag = if value { "/Oy-" } else { "/Oy" };
push_if_supported(cc_flag.into());
}
}
Expand Down
Loading