From 24cc38e3b00ff399acc99a57f92bfdef4ab8c949 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 4 Oct 2017 14:54:28 -0700 Subject: [PATCH] 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 --- src/librustc_trans/callee.rs | 7 +++++-- src/librustc_trans/declare.rs | 5 +++++ src/test/codegen/auxiliary/nounwind.rs | 13 +++++++++++++ src/test/codegen/nounwind.rs | 26 +++++++++++++++++++++++++ src/test/codegen/panic-abort-windows.rs | 2 +- 5 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 src/test/codegen/auxiliary/nounwind.rs create mode 100644 src/test/codegen/nounwind.rs diff --git a/src/librustc_trans/callee.rs b/src/librustc_trans/callee.rs index 18b91b18d8e..b515c9420bf 100644 --- a/src/librustc_trans/callee.rs +++ b/src/librustc_trans/callee.rs @@ -24,6 +24,7 @@ use rustc::hir::def_id::DefId; use rustc::ty::{self, TypeFoldable}; use rustc::traits; use rustc::ty::subst::Substs; +use rustc_back::PanicStrategy; use type_of; /// Translates a reference to a fn/method item, monomorphizing and @@ -105,8 +106,10 @@ pub fn get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, // *in Rust code* may unwind. Foreign items like `extern "C" { // fn foo(); }` are assumed not to unwind **unless** they have // a `#[unwind]` attribute. - if !tcx.is_foreign_item(instance_def_id) { - attributes::unwind(llfn, true); + if tcx.sess.panic_strategy() == PanicStrategy::Unwind { + if !tcx.is_foreign_item(instance_def_id) { + attributes::unwind(llfn, true); + } } // Apply an appropriate linkage/visibility value to our item that we diff --git a/src/librustc_trans/declare.rs b/src/librustc_trans/declare.rs index 3c8ff454997..f894bdf16e4 100644 --- a/src/librustc_trans/declare.rs +++ b/src/librustc_trans/declare.rs @@ -24,6 +24,7 @@ use llvm::{self, ValueRef}; use llvm::AttributePlace::Function; use rustc::ty::Ty; use rustc::session::config::Sanitizer; +use rustc_back::PanicStrategy; use abi::{Abi, FnType}; use attributes; use context::CrateContext; @@ -98,6 +99,10 @@ fn declare_raw_fn(ccx: &CrateContext, name: &str, callconv: llvm::CallConv, ty: _ => {}, } + if ccx.tcx().sess.panic_strategy() != PanicStrategy::Unwind { + attributes::unwind(llfn, false); + } + llfn } diff --git a/src/test/codegen/auxiliary/nounwind.rs b/src/test/codegen/auxiliary/nounwind.rs new file mode 100644 index 00000000000..5e40e8ede15 --- /dev/null +++ b/src/test/codegen/auxiliary/nounwind.rs @@ -0,0 +1,13 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[no_mangle] +pub fn bar() { +} diff --git a/src/test/codegen/nounwind.rs b/src/test/codegen/nounwind.rs new file mode 100644 index 00000000000..9fea907d3c8 --- /dev/null +++ b/src/test/codegen/nounwind.rs @@ -0,0 +1,26 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// aux-build:nounwind.rs +// compile-flags: -C no-prepopulate-passes -C panic=abort -C metadata=a +// ignore-windows + +#![crate_type = "lib"] + +extern crate nounwind; + +#[no_mangle] +pub fn foo() { + nounwind::bar(); +// CHECK: @foo() unnamed_addr #0 +// CHECK: @bar() unnamed_addr #0 +// CHECK: attributes #0 = { {{.*}}nounwind{{.*}} } +} + diff --git a/src/test/codegen/panic-abort-windows.rs b/src/test/codegen/panic-abort-windows.rs index 2ab15277084..15688bdc2a1 100644 --- a/src/test/codegen/panic-abort-windows.rs +++ b/src/test/codegen/panic-abort-windows.rs @@ -28,7 +28,7 @@ #![crate_type = "lib"] -// CHECK: Function Attrs: uwtable +// CHECK: Function Attrs: nounwind uwtable // CHECK-NEXT: define void @normal_uwtable() #[no_mangle] pub fn normal_uwtable() {