Skip to content

Commit 24cc38e

Browse files
committedOct 11, 2017
rustc: Add LLVM nounwind with -C panic=abort
This informs LLVM that functions can't unwind, which while it should typically have already been inferred when necessary or otherwise not impact codegen is apparently needed on targets like ARM to avoid references to unnecessary symbols. Closes #44992
1 parent 264aafe commit 24cc38e

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed
 

‎src/librustc_trans/callee.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc::hir::def_id::DefId;
2424
use rustc::ty::{self, TypeFoldable};
2525
use rustc::traits;
2626
use rustc::ty::subst::Substs;
27+
use rustc_back::PanicStrategy;
2728
use type_of;
2829

2930
/// Translates a reference to a fn/method item, monomorphizing and
@@ -105,8 +106,10 @@ pub fn get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
105106
// *in Rust code* may unwind. Foreign items like `extern "C" {
106107
// fn foo(); }` are assumed not to unwind **unless** they have
107108
// a `#[unwind]` attribute.
108-
if !tcx.is_foreign_item(instance_def_id) {
109-
attributes::unwind(llfn, true);
109+
if tcx.sess.panic_strategy() == PanicStrategy::Unwind {
110+
if !tcx.is_foreign_item(instance_def_id) {
111+
attributes::unwind(llfn, true);
112+
}
110113
}
111114

112115
// Apply an appropriate linkage/visibility value to our item that we

‎src/librustc_trans/declare.rs

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use llvm::{self, ValueRef};
2424
use llvm::AttributePlace::Function;
2525
use rustc::ty::Ty;
2626
use rustc::session::config::Sanitizer;
27+
use rustc_back::PanicStrategy;
2728
use abi::{Abi, FnType};
2829
use attributes;
2930
use context::CrateContext;
@@ -98,6 +99,10 @@ fn declare_raw_fn(ccx: &CrateContext, name: &str, callconv: llvm::CallConv, ty:
9899
_ => {},
99100
}
100101

102+
if ccx.tcx().sess.panic_strategy() != PanicStrategy::Unwind {
103+
attributes::unwind(llfn, false);
104+
}
105+
101106
llfn
102107
}
103108

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[no_mangle]
12+
pub fn bar() {
13+
}

‎src/test/codegen/nounwind.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:nounwind.rs
12+
// compile-flags: -C no-prepopulate-passes -C panic=abort -C metadata=a
13+
// ignore-windows
14+
15+
#![crate_type = "lib"]
16+
17+
extern crate nounwind;
18+
19+
#[no_mangle]
20+
pub fn foo() {
21+
nounwind::bar();
22+
// CHECK: @foo() unnamed_addr #0
23+
// CHECK: @bar() unnamed_addr #0
24+
// CHECK: attributes #0 = { {{.*}}nounwind{{.*}} }
25+
}
26+

‎src/test/codegen/panic-abort-windows.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
#![crate_type = "lib"]
3030

31-
// CHECK: Function Attrs: uwtable
31+
// CHECK: Function Attrs: nounwind uwtable
3232
// CHECK-NEXT: define void @normal_uwtable()
3333
#[no_mangle]
3434
pub fn normal_uwtable() {

0 commit comments

Comments
 (0)
Please sign in to comment.