Skip to content

Commit 7dab895

Browse files
authored
build: Don't pass inherited PGO flags to GNU compilers (#1363)
Clang and GNU use different PGO libraries, so PGO flags inherited from rustc should not automatically be passed to GNU compilers. Fixes #1354
1 parent 559609a commit 7dab895

File tree

1 file changed

+101
-89
lines changed

1 file changed

+101
-89
lines changed

src/flags.rs

+101-89
Original file line numberDiff line numberDiff line change
@@ -174,26 +174,109 @@ impl<'this> RustcCodegenFlags<'this> {
174174
}
175175
};
176176

177-
match family {
178-
ToolFamily::Clang { .. } | ToolFamily::Gnu => {
179-
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mbranch-protection
180-
if let Some(value) = self.branch_protection {
181-
push_if_supported(
182-
format!("-mbranch-protection={}", value.replace(",", "+")).into(),
183-
);
184-
}
185-
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mcmodel
186-
if let Some(value) = self.code_model {
187-
push_if_supported(format!("-mcmodel={value}").into());
177+
let clang_or_gnu =
178+
matches!(family, ToolFamily::Clang { .. }) || matches!(family, ToolFamily::Gnu { .. });
179+
180+
// Flags shared between clang and gnu
181+
if clang_or_gnu {
182+
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mbranch-protection
183+
if let Some(value) = self.branch_protection {
184+
push_if_supported(
185+
format!("-mbranch-protection={}", value.replace(",", "+")).into(),
186+
);
187+
}
188+
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mcmodel
189+
if let Some(value) = self.code_model {
190+
push_if_supported(format!("-mcmodel={value}").into());
191+
}
192+
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fno-vectorize
193+
if self.no_vectorize_loops {
194+
push_if_supported("-fno-vectorize".into());
195+
}
196+
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fno-slp-vectorize
197+
if self.no_vectorize_slp {
198+
push_if_supported("-fno-slp-vectorize".into());
199+
}
200+
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mguard
201+
if let Some(value) = self.control_flow_guard {
202+
let cc_val = match value {
203+
"y" | "yes" | "on" | "true" | "checks" => Some("cf"),
204+
"nochecks" => Some("cf-nochecks"),
205+
"n" | "no" | "off" | "false" => Some("none"),
206+
_ => None,
207+
};
208+
if let Some(cc_val) = cc_val {
209+
push_if_supported(format!("-mguard={cc_val}").into());
188210
}
189-
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fno-vectorize
190-
if self.no_vectorize_loops {
191-
push_if_supported("-fno-vectorize".into());
211+
}
212+
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-flto
213+
if let Some(value) = self.lto {
214+
let cc_val = match value {
215+
"y" | "yes" | "on" | "true" | "fat" => Some("full"),
216+
"thin" => Some("thin"),
217+
_ => None,
218+
};
219+
if let Some(cc_val) = cc_val {
220+
push_if_supported(format!("-flto={cc_val}").into());
192221
}
193-
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fno-slp-vectorize
194-
if self.no_vectorize_slp {
195-
push_if_supported("-fno-slp-vectorize".into());
222+
}
223+
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fPIC
224+
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fPIE
225+
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mdynamic-no-pic
226+
if let Some(value) = self.relocation_model {
227+
let cc_flag = match value {
228+
"pic" => Some("-fPIC"),
229+
"pie" => Some("-fPIE"),
230+
"dynamic-no-pic" => Some("-mdynamic-no-pic"),
231+
_ => None,
232+
};
233+
if let Some(cc_flag) = cc_flag {
234+
push_if_supported(cc_flag.into());
196235
}
236+
}
237+
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fembed-bitcode
238+
if let Some(value) = self.embed_bitcode {
239+
let cc_val = if value { "all" } else { "off" };
240+
push_if_supported(format!("-fembed-bitcode={cc_val}").into());
241+
}
242+
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fno-omit-frame-pointer
243+
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fomit-frame-pointer
244+
if let Some(value) = self.force_frame_pointers {
245+
let cc_flag = if value {
246+
"-fno-omit-frame-pointer"
247+
} else {
248+
"-fomit-frame-pointer"
249+
};
250+
push_if_supported(cc_flag.into());
251+
}
252+
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-dead_strip
253+
if let Some(false) = self.link_dead_code {
254+
push_if_supported("-dead_strip".into());
255+
}
256+
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mno-red-zone
257+
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mred-zone
258+
if let Some(value) = self.no_redzone {
259+
let cc_flag = if value { "-mno-red-zone" } else { "-mred-zone" };
260+
push_if_supported(cc_flag.into());
261+
}
262+
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-msoft-float
263+
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mno-soft-float
264+
if let Some(value) = self.soft_float {
265+
let cc_flag = if value {
266+
"-msoft-float"
267+
} else {
268+
"-mno-soft-float"
269+
};
270+
push_if_supported(cc_flag.into());
271+
}
272+
}
273+
274+
// Compiler-exclusive flags
275+
match family {
276+
ToolFamily::Clang { .. } => {
277+
// GNU and Clang compilers both support the same PGO flags, but they use different libraries and
278+
// different formats for the profile files which are not compatible.
279+
// clang and rustc both internally use llvm, so we want to inherit the PGO flags only for clang.
197280
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fprofile-generate
198281
if let Some(value) = self.profile_generate {
199282
push_if_supported(format!("-fprofile-generate={value}").into());
@@ -202,79 +285,8 @@ impl<'this> RustcCodegenFlags<'this> {
202285
if let Some(value) = self.profile_use {
203286
push_if_supported(format!("-fprofile-use={value}").into());
204287
}
205-
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mguard
206-
if let Some(value) = self.control_flow_guard {
207-
let cc_val = match value {
208-
"y" | "yes" | "on" | "true" | "checks" => Some("cf"),
209-
"nochecks" => Some("cf-nochecks"),
210-
"n" | "no" | "off" | "false" => Some("none"),
211-
_ => None,
212-
};
213-
if let Some(cc_val) = cc_val {
214-
push_if_supported(format!("-mguard={cc_val}").into());
215-
}
216-
}
217-
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-flto
218-
if let Some(value) = self.lto {
219-
let cc_val = match value {
220-
"y" | "yes" | "on" | "true" | "fat" => Some("full"),
221-
"thin" => Some("thin"),
222-
_ => None,
223-
};
224-
if let Some(cc_val) = cc_val {
225-
push_if_supported(format!("-flto={cc_val}").into());
226-
}
227-
}
228-
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fPIC
229-
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fPIE
230-
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mdynamic-no-pic
231-
if let Some(value) = self.relocation_model {
232-
let cc_flag = match value {
233-
"pic" => Some("-fPIC"),
234-
"pie" => Some("-fPIE"),
235-
"dynamic-no-pic" => Some("-mdynamic-no-pic"),
236-
_ => None,
237-
};
238-
if let Some(cc_flag) = cc_flag {
239-
push_if_supported(cc_flag.into());
240-
}
241-
}
242-
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fembed-bitcode
243-
if let Some(value) = self.embed_bitcode {
244-
let cc_val = if value { "all" } else { "off" };
245-
push_if_supported(format!("-fembed-bitcode={cc_val}").into());
246-
}
247-
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fno-omit-frame-pointer
248-
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fomit-frame-pointer
249-
if let Some(value) = self.force_frame_pointers {
250-
let cc_flag = if value {
251-
"-fno-omit-frame-pointer"
252-
} else {
253-
"-fomit-frame-pointer"
254-
};
255-
push_if_supported(cc_flag.into());
256-
}
257-
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-dead_strip
258-
if let Some(false) = self.link_dead_code {
259-
push_if_supported("-dead_strip".into());
260-
}
261-
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mno-red-zone
262-
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mred-zone
263-
if let Some(value) = self.no_redzone {
264-
let cc_flag = if value { "-mno-red-zone" } else { "-mred-zone" };
265-
push_if_supported(cc_flag.into());
266-
}
267-
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-msoft-float
268-
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mno-soft-float
269-
if let Some(value) = self.soft_float {
270-
let cc_flag = if value {
271-
"-msoft-float"
272-
} else {
273-
"-mno-soft-float"
274-
};
275-
push_if_supported(cc_flag.into());
276-
}
277288
}
289+
ToolFamily::Gnu { .. } => {}
278290
ToolFamily::Msvc { .. } => {
279291
// https://learn.microsoft.com/en-us/cpp/build/reference/guard-enable-control-flow-guard
280292
if let Some(value) = self.control_flow_guard {

0 commit comments

Comments
 (0)