Skip to content

Commit 6e3b1d6

Browse files
authored
Rollup merge of rust-lang#67393 - michaelwoerister:llvm-args-override, r=varkor
Enable opting out of specific default LLVM arguments. `rustc` by default adds a few arguments to LLVM (like `-mergefunc-use-aliases` for example). With this PR `rustc` will only emit these arguments if the same argument has not already been specified by the user via `-Cllvm-args`. This enables opting out of these defaults. The PR also removes a PGO specific `-Z` flag the effect of which can also be easily achieved by `-Cllvm-args`. Fixes rust-lang#64310.
2 parents c0bf3af + 1ca145c commit 6e3b1d6

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

src/librustc_codegen_llvm/llvm_util.rs

+33-15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::llvm;
33
use syntax_pos::symbol::Symbol;
44
use rustc::session::Session;
55
use rustc::session::config::PrintRequest;
6+
use rustc_data_structures::fx::FxHashSet;
67
use rustc_target::spec::{MergeFunctions, PanicStrategy};
78
use libc::c_int;
89
use std::ffi::CString;
@@ -51,43 +52,60 @@ unsafe fn configure_llvm(sess: &Session) {
5152

5253
llvm::LLVMRustInstallFatalErrorHandler();
5354

55+
fn llvm_arg_to_arg_name(full_arg: &str) -> &str {
56+
full_arg.trim().split(|c: char| {
57+
c == '=' || c.is_whitespace()
58+
}).next().unwrap_or("")
59+
}
60+
61+
let user_specified_args: FxHashSet<_> = sess
62+
.opts
63+
.cg
64+
.llvm_args
65+
.iter()
66+
.map(|s| llvm_arg_to_arg_name(s))
67+
.filter(|s| s.len() > 0)
68+
.collect();
69+
5470
{
55-
let mut add = |arg: &str| {
56-
let s = CString::new(arg).unwrap();
57-
llvm_args.push(s.as_ptr());
58-
llvm_c_strs.push(s);
71+
// This adds the given argument to LLVM. Unless `force` is true
72+
// user specified arguments are *not* overridden.
73+
let mut add = |arg: &str, force: bool| {
74+
if force || !user_specified_args.contains(llvm_arg_to_arg_name(arg)) {
75+
let s = CString::new(arg).unwrap();
76+
llvm_args.push(s.as_ptr());
77+
llvm_c_strs.push(s);
78+
}
5979
};
60-
add("rustc"); // fake program name
61-
if sess.time_llvm_passes() { add("-time-passes"); }
62-
if sess.print_llvm_passes() { add("-debug-pass=Structure"); }
63-
if sess.opts.debugging_opts.disable_instrumentation_preinliner {
64-
add("-disable-preinline");
65-
}
80+
add("rustc", true); // fake program name
81+
if sess.time_llvm_passes() { add("-time-passes", false); }
82+
if sess.print_llvm_passes() { add("-debug-pass=Structure", false); }
83+
6684
if sess.opts.debugging_opts.generate_arange_section {
67-
add("-generate-arange-section");
85+
add("-generate-arange-section", false);
6886
}
6987
if get_major_version() >= 8 {
7088
match sess.opts.debugging_opts.merge_functions
7189
.unwrap_or(sess.target.target.options.merge_functions) {
7290
MergeFunctions::Disabled |
7391
MergeFunctions::Trampolines => {}
7492
MergeFunctions::Aliases => {
75-
add("-mergefunc-use-aliases");
93+
add("-mergefunc-use-aliases", false);
7694
}
7795
}
7896
}
7997

8098
if sess.target.target.target_os == "emscripten" &&
8199
sess.panic_strategy() == PanicStrategy::Unwind {
82-
add("-enable-emscripten-cxx-exceptions");
100+
add("-enable-emscripten-cxx-exceptions", false);
83101
}
84102

85103
// HACK(eddyb) LLVM inserts `llvm.assume` calls to preserve align attributes
86104
// during inlining. Unfortunately these may block other optimizations.
87-
add("-preserve-alignment-assumptions-during-inlining=false");
105+
add("-preserve-alignment-assumptions-during-inlining=false", false);
88106

89107
for arg in &sess.opts.cg.llvm_args {
90-
add(&(*arg));
108+
add(&(*arg), true);
91109
}
92110
}
93111

src/librustc_session/options.rs

-2
Original file line numberDiff line numberDiff line change
@@ -866,8 +866,6 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
866866
"extra arguments to prepend to the linker invocation (space separated)"),
867867
profile: bool = (false, parse_bool, [TRACKED],
868868
"insert profiling code"),
869-
disable_instrumentation_preinliner: bool = (false, parse_bool, [TRACKED],
870-
"Disable the instrumentation pre-inliner, useful for profiling / PGO."),
871869
relro_level: Option<RelroLevel> = (None, parse_relro_level, [TRACKED],
872870
"choose which RELRO level to use"),
873871
nll_facts: bool = (false, parse_bool, [UNTRACKED],

0 commit comments

Comments
 (0)