Skip to content

Commit cc65b47

Browse files
committed
Put unwinding support behind a cargo feature
1 parent f2ce6bc commit cc65b47

File tree

14 files changed

+231
-150
lines changed

14 files changed

+231
-150
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ smallvec = "1.8.1"
4646
unstable-features = ["jit", "inline_asm_sym"]
4747
jit = ["cranelift-jit", "libloading"]
4848
inline_asm_sym = []
49+
unwinding = [] # Not yet included in unstable-features for performance reasons
4950

5051
[package.metadata.rust-analyzer]
5152
rustc_private = true

build_system/abi_cafe.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub(crate) fn run(
1919
cg_clif_dylib: &CodegenBackend,
2020
rustup_toolchain_name: Option<&str>,
2121
bootstrap_host_compiler: &Compiler,
22+
panic_unwind_support: bool,
2223
) {
2324
std::fs::create_dir_all(&dirs.download_dir).unwrap();
2425
ABI_CAFE_REPO.fetch(dirs);
@@ -32,6 +33,7 @@ pub(crate) fn run(
3233
bootstrap_host_compiler,
3334
rustup_toolchain_name,
3435
bootstrap_host_compiler.triple.clone(),
36+
panic_unwind_support,
3537
);
3638

3739
eprintln!("Running abi-cafe");

build_system/build_backend.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub(crate) fn build_backend(
1212
dirs: &Dirs,
1313
bootstrap_host_compiler: &Compiler,
1414
use_unstable_features: bool,
15+
panic_unwind_support: bool,
1516
) -> PathBuf {
1617
let _group = LogGroup::guard("Build backend");
1718

@@ -31,6 +32,10 @@ pub(crate) fn build_backend(
3132
cmd.arg("--features").arg("unstable-features");
3233
}
3334

35+
if panic_unwind_support {
36+
cmd.arg("--features").arg("unwinding");
37+
}
38+
3439
cmd.arg("--release");
3540

3641
eprintln!("[BUILD] rustc_codegen_cranelift");

build_system/build_sysroot.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub(crate) fn build_sysroot(
1717
bootstrap_host_compiler: &Compiler,
1818
rustup_toolchain_name: Option<&str>,
1919
target_triple: String,
20+
panic_unwind_support: bool,
2021
) -> Compiler {
2122
let _guard = LogGroup::guard("Build sysroot");
2223

@@ -52,6 +53,9 @@ pub(crate) fn build_sysroot(
5253
.arg("-o")
5354
.arg(&wrapper_path)
5455
.arg("-Cstrip=debuginfo");
56+
if panic_unwind_support {
57+
build_cargo_wrapper_cmd.arg("--cfg").arg("support_panic_unwind");
58+
}
5559
if let Some(rustup_toolchain_name) = &rustup_toolchain_name {
5660
build_cargo_wrapper_cmd
5761
.env("TOOLCHAIN_NAME", rustup_toolchain_name)
@@ -77,6 +81,7 @@ pub(crate) fn build_sysroot(
7781
bootstrap_host_compiler.clone(),
7882
&cg_clif_dylib_path,
7983
sysroot_kind,
84+
panic_unwind_support,
8085
);
8186
host.install_into_sysroot(dist_dir);
8287

@@ -91,6 +96,7 @@ pub(crate) fn build_sysroot(
9196
},
9297
&cg_clif_dylib_path,
9398
sysroot_kind,
99+
panic_unwind_support,
94100
)
95101
.install_into_sysroot(dist_dir);
96102
}
@@ -141,12 +147,15 @@ fn build_sysroot_for_triple(
141147
compiler: Compiler,
142148
cg_clif_dylib_path: &CodegenBackend,
143149
sysroot_kind: SysrootKind,
150+
panic_unwind_support: bool,
144151
) -> SysrootTarget {
145152
match sysroot_kind {
146153
SysrootKind::None => build_rtstartup(dirs, &compiler)
147154
.unwrap_or(SysrootTarget { triple: compiler.triple, libs: vec![] }),
148155
SysrootKind::Llvm => build_llvm_sysroot_for_triple(compiler),
149-
SysrootKind::Clif => build_clif_sysroot_for_triple(dirs, compiler, cg_clif_dylib_path),
156+
SysrootKind::Clif => {
157+
build_clif_sysroot_for_triple(dirs, compiler, cg_clif_dylib_path, panic_unwind_support)
158+
}
150159
}
151160
}
152161

@@ -188,6 +197,7 @@ fn build_clif_sysroot_for_triple(
188197
dirs: &Dirs,
189198
mut compiler: Compiler,
190199
cg_clif_dylib_path: &CodegenBackend,
200+
panic_unwind_support: bool,
191201
) -> SysrootTarget {
192202
let mut target_libs = SysrootTarget { triple: compiler.triple.clone(), libs: vec![] };
193203

@@ -206,7 +216,10 @@ fn build_clif_sysroot_for_triple(
206216
}
207217

208218
// Build sysroot
209-
let mut rustflags = vec!["-Zforce-unstable-if-unmarked".to_owned(), "-Cpanic=abort".to_owned()];
219+
let mut rustflags = vec!["-Zforce-unstable-if-unmarked".to_owned()];
220+
if !panic_unwind_support {
221+
rustflags.push("-Cpanic=abort".to_owned());
222+
}
210223
match cg_clif_dylib_path {
211224
CodegenBackend::Local(path) => {
212225
rustflags.push(format!("-Zcodegen-backend={}", path.to_str().unwrap()));

build_system/main.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ fn main() {
8383
let mut download_dir = None;
8484
let mut sysroot_kind = SysrootKind::Clif;
8585
let mut use_unstable_features = true;
86+
let mut panic_unwind_support = false;
8687
let mut frozen = false;
8788
let mut skip_tests = vec![];
8889
let mut use_backend = None;
@@ -108,6 +109,7 @@ fn main() {
108109
}
109110
}
110111
"--no-unstable-features" => use_unstable_features = false,
112+
"--panic-unwind-support" => panic_unwind_support = true,
111113
"--frozen" => frozen = true,
112114
"--skip-test" => {
113115
// FIXME check that all passed in tests actually exist
@@ -201,6 +203,7 @@ fn main() {
201203
&dirs,
202204
&bootstrap_host_compiler,
203205
use_unstable_features,
206+
panic_unwind_support,
204207
))
205208
};
206209
match command {
@@ -212,6 +215,7 @@ fn main() {
212215
&dirs,
213216
sysroot_kind,
214217
use_unstable_features,
218+
panic_unwind_support,
215219
&skip_tests.iter().map(|test| &**test).collect::<Vec<_>>(),
216220
&cg_clif_dylib,
217221
&bootstrap_host_compiler,
@@ -230,6 +234,7 @@ fn main() {
230234
&cg_clif_dylib,
231235
rustup_toolchain_name.as_deref(),
232236
&bootstrap_host_compiler,
237+
panic_unwind_support,
233238
);
234239
}
235240
Command::Build => {
@@ -240,6 +245,7 @@ fn main() {
240245
&bootstrap_host_compiler,
241246
rustup_toolchain_name.as_deref(),
242247
target_triple,
248+
panic_unwind_support,
243249
);
244250
}
245251
Command::Bench => {
@@ -250,6 +256,7 @@ fn main() {
250256
&bootstrap_host_compiler,
251257
rustup_toolchain_name.as_deref(),
252258
target_triple,
259+
panic_unwind_support,
253260
);
254261
bench::benchmark(&dirs, &compiler);
255262
}

build_system/tests.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ pub(crate) fn run_tests(
233233
dirs: &Dirs,
234234
sysroot_kind: SysrootKind,
235235
use_unstable_features: bool,
236+
panic_unwind_support: bool,
236237
skip_tests: &[&str],
237238
cg_clif_dylib: &CodegenBackend,
238239
bootstrap_host_compiler: &Compiler,
@@ -251,12 +252,14 @@ pub(crate) fn run_tests(
251252
bootstrap_host_compiler,
252253
rustup_toolchain_name,
253254
target_triple.clone(),
255+
panic_unwind_support,
254256
);
255257

256258
let runner = TestRunner::new(
257259
dirs.clone(),
258260
target_compiler,
259261
use_unstable_features,
262+
panic_unwind_support,
260263
skip_tests,
261264
bootstrap_host_compiler.triple == target_triple,
262265
stdlib_source.clone(),
@@ -283,12 +286,14 @@ pub(crate) fn run_tests(
283286
bootstrap_host_compiler,
284287
rustup_toolchain_name,
285288
target_triple.clone(),
289+
panic_unwind_support,
286290
);
287291

288292
let mut runner = TestRunner::new(
289293
dirs.clone(),
290294
target_compiler,
291295
use_unstable_features,
296+
panic_unwind_support,
292297
skip_tests,
293298
bootstrap_host_compiler.triple == target_triple,
294299
stdlib_source,
@@ -314,6 +319,7 @@ pub(crate) fn run_tests(
314319
struct TestRunner<'a> {
315320
is_native: bool,
316321
jit_supported: bool,
322+
panic_unwind_support: bool,
317323
skip_tests: &'a [&'a str],
318324
dirs: Dirs,
319325
target_compiler: Compiler,
@@ -325,6 +331,7 @@ impl<'a> TestRunner<'a> {
325331
dirs: Dirs,
326332
mut target_compiler: Compiler,
327333
use_unstable_features: bool,
334+
panic_unwind_support: bool,
328335
skip_tests: &'a [&'a str],
329336
is_native: bool,
330337
stdlib_source: PathBuf,
@@ -335,7 +342,15 @@ impl<'a> TestRunner<'a> {
335342
let jit_supported =
336343
use_unstable_features && is_native && !target_compiler.triple.contains("windows");
337344

338-
Self { is_native, jit_supported, skip_tests, dirs, target_compiler, stdlib_source }
345+
Self {
346+
is_native,
347+
jit_supported,
348+
panic_unwind_support,
349+
skip_tests,
350+
dirs,
351+
target_compiler,
352+
stdlib_source,
353+
}
339354
}
340355

341356
fn run_testsuite(&self, tests: &[TestCase]) {
@@ -404,7 +419,9 @@ impl<'a> TestRunner<'a> {
404419
cmd.arg("-Cdebuginfo=2");
405420
cmd.arg("--target");
406421
cmd.arg(&self.target_compiler.triple);
407-
cmd.arg("-Cpanic=abort");
422+
if !self.panic_unwind_support {
423+
cmd.arg("-Cpanic=abort");
424+
}
408425
cmd.arg("--check-cfg=cfg(jit)");
409426
cmd.args(args);
410427
cmd

build_system/usage.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ OPTIONS:
2525
Some features are not yet ready for production usage. This option will disable these
2626
features. This includes the JIT mode and inline assembly support.
2727

28+
--panic-unwind-support
29+
Enable support for unwinding when -Cpanic=unwind is used. This currently regresses build
30+
performance.
31+
2832
--frozen
2933
Require Cargo.lock and cache are up to date
3034

scripts/cargo-clif.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ fn main() {
1212
sysroot = sysroot.parent().unwrap();
1313
}
1414

15-
let mut rustflags = vec!["-Cpanic=abort".to_owned(), "-Zpanic-abort-tests".to_owned()];
15+
let mut rustflags = vec![];
16+
if !cfg!(support_panic_unwind) {
17+
rustflags.push("-Cpanic=abort".to_owned());
18+
rustflags.push("-Zpanic-abort-tests".to_owned());
19+
}
1620
if let Some(name) = option_env!("BUILTIN_BACKEND") {
1721
rustflags.push(format!("-Zcodegen-backend={name}"));
1822
} else {

scripts/rustc-clif.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ fn main() {
1717

1818
let passed_args = std::env::args_os().skip(1).collect::<Vec<_>>();
1919
let mut args = vec![];
20-
args.push(OsString::from("-Cpanic=abort"));
21-
args.push(OsString::from("-Zpanic-abort-tests"));
20+
if !cfg!(support_panic_unwind) {
21+
args.push(OsString::from("-Cpanic=abort"));
22+
args.push(OsString::from("-Zpanic-abort-tests"));
23+
}
2224
if let Some(name) = option_env!("BUILTIN_BACKEND") {
2325
args.push(OsString::from(format!("-Zcodegen-backend={name}")))
2426
} else {

scripts/rustdoc-clif.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ fn main() {
1717

1818
let passed_args = std::env::args_os().skip(1).collect::<Vec<_>>();
1919
let mut args = vec![];
20-
args.push(OsString::from("-Cpanic=abort"));
21-
args.push(OsString::from("-Zpanic-abort-tests"));
20+
if !cfg!(support_panic_unwind) {
21+
args.push(OsString::from("-Cpanic=abort"));
22+
args.push(OsString::from("-Zpanic-abort-tests"));
23+
}
2224
if let Some(name) = option_env!("BUILTIN_BACKEND") {
2325
args.push(OsString::from(format!("-Zcodegen-backend={name}")))
2426
} else {

src/abi/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ pub(crate) fn codegen_call_with_unwind_action(
844844
fx: &mut FunctionCx<'_, '_, '_>,
845845
span: Span,
846846
func_ref: CallTarget,
847-
unwind: UnwindAction,
847+
mut unwind: UnwindAction,
848848
call_args: &[Value],
849849
target_block: Option<Block>,
850850
) -> SmallVec<[Value; 2]> {
@@ -856,6 +856,11 @@ pub(crate) fn codegen_call_with_unwind_action(
856856
if target_block.is_some() {
857857
assert!(fx.bcx.func.dfg.signatures[sig_ref].returns.is_empty());
858858
}
859+
860+
if cfg!(not(feature = "unwinding")) {
861+
unwind = UnwindAction::Unreachable;
862+
}
863+
859864
match unwind {
860865
UnwindAction::Continue | UnwindAction::Unreachable => {
861866
let call_inst = match func_ref {

src/base.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
300300
}
301301

302302
if bb_data.is_cleanup {
303+
if cfg!(not(feature = "unwinding")) {
304+
continue;
305+
}
306+
303307
fx.bcx.set_cold_block(block);
304308
}
305309

@@ -534,13 +538,15 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
534538
codegen_unwind_terminate(fx, source_info.span, *reason);
535539
}
536540
TerminatorKind::UnwindResume => {
537-
let exception_ptr = fx.bcx.use_var(fx.exception_slot);
538-
fx.lib_call(
539-
"_Unwind_Resume",
540-
vec![AbiParam::new(fx.pointer_type)],
541-
vec![],
542-
&[exception_ptr],
543-
);
541+
if cfg!(feature = "unwinding") {
542+
let exception_ptr = fx.bcx.use_var(fx.exception_slot);
543+
fx.lib_call(
544+
"_Unwind_Resume",
545+
vec![AbiParam::new(fx.pointer_type)],
546+
vec![],
547+
&[exception_ptr],
548+
);
549+
}
544550
fx.bcx.ins().trap(TrapCode::user(1 /* unreachable */).unwrap());
545551
}
546552
TerminatorKind::Unreachable => {

0 commit comments

Comments
 (0)