Skip to content

Commit cf98161

Browse files
committed
Remove the need for #[cfg] in #[use_c_shim_if]
This commit tweaks the implementation of the synthetic `#[use_c_shim_if]` attribute, renaming it to `#[maybe_use_optimized_c_shim]` in the process. This no longer requires specifying a `#[cfg]` clause indicating when the optimized intrinsic should be used, but rather this is inferred and printed from the build script. The build script will now print out appropriate `#[cfg]` directives for rustc to indicate what intrinsics it's compiling. This should remove the need for us to keep the build script and the source in sync, but rather the build script can simply take care of everything.
1 parent 6178e2c commit cf98161

File tree

9 files changed

+198
-222
lines changed

9 files changed

+198
-222
lines changed

build.rs

+167-169
Large diffs are not rendered by default.

ci/run.sh

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
set -ex
22

3+
export CARGO_INCREMENTAL=0
34
cargo=cargo
45

56
# Test our implementation

src/float/conv.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,7 @@ intrinsics! {
8787
int_to_float!(i, i32, f64)
8888
}
8989

90-
#[use_c_shim_if(any(
91-
all(target_arch = "x86", not(target_env = "msvc")),
92-
all(target_arch = "x86_64", not(windows)),
93-
all(target_arch = "x86_64", target_env = "msvc"),
94-
))]
90+
#[maybe_use_optimized_c_shim]
9591
#[arm_aeabi_alias = __aeabi_l2f]
9692
pub extern "C" fn __floatdisf(i: i64) -> f32 {
9793
// On x86_64 LLVM will use native instructions for this conversion, we
@@ -103,7 +99,7 @@ intrinsics! {
10399
}
104100
}
105101

106-
#[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))]
102+
#[maybe_use_optimized_c_shim]
107103
#[arm_aeabi_alias = __aeabi_l2d]
108104
pub extern "C" fn __floatdidf(i: i64) -> f64 {
109105
// On x86_64 LLVM will use native instructions for this conversion, we
@@ -135,19 +131,13 @@ intrinsics! {
135131
int_to_float!(i, u32, f64)
136132
}
137133

138-
#[use_c_shim_if(any(
139-
all(target_arch = "x86", not(target_env = "msvc")),
140-
all(target_arch = "x86_64", not(windows)),
141-
))]
134+
#[maybe_use_optimized_c_shim]
142135
#[arm_aeabi_alias = __aeabi_ul2f]
143136
pub extern "C" fn __floatundisf(i: u64) -> f32 {
144137
int_to_float!(i, u64, f32)
145138
}
146139

147-
#[use_c_shim_if(any(
148-
all(target_arch = "x86", not(target_env = "msvc")),
149-
all(target_arch = "x86_64", not(windows)),
150-
))]
140+
#[maybe_use_optimized_c_shim]
151141
#[arm_aeabi_alias = __aeabi_ul2d]
152142
pub extern "C" fn __floatundidf(i: u64) -> f64 {
153143
int_to_float!(i, u64, f64)

src/int/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ impl_wide_int!(u32, u64, 32);
302302
impl_wide_int!(u64, u128, 64);
303303

304304
intrinsics! {
305-
#[use_c_shim_if(/* always if C compilation is enabled */)]
305+
#[maybe_use_optimized_c_shim]
306306
#[cfg(any(
307307
target_pointer_width = "16",
308308
target_pointer_width = "32",

src/int/mul.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ trait UMulo: Int {
8484
impl UMulo for u128 {}
8585

8686
intrinsics! {
87-
#[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))]
87+
#[maybe_use_optimized_c_shim]
8888
#[arm_aeabi_alias = __aeabi_lmul]
8989
pub extern "C" fn __muldi3(a: u64, b: u64) -> u64 {
9090
a.mul(b)

src/int/sdiv.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ impl Divmod for i32 {}
5858
impl Divmod for i64 {}
5959

6060
intrinsics! {
61-
#[use_c_shim_if(all(target_arch = "arm", not(target_os = "ios"), not(thumb_1)))]
61+
#[maybe_use_optimized_c_shim]
6262
#[arm_aeabi_alias = __aeabi_idiv]
6363
pub extern "C" fn __divsi3(a: i32, b: i32) -> i32 {
6464
a.div(b)
6565
}
6666

67-
#[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))]
67+
#[maybe_use_optimized_c_shim]
6868
pub extern "C" fn __divdi3(a: i64, b: i64) -> i64 {
6969
a.div(b)
7070
}
@@ -74,15 +74,12 @@ intrinsics! {
7474
a.div(b)
7575
}
7676

77-
#[use_c_shim_if(all(target_arch = "arm",
78-
not(target_os = "ios"),
79-
not(target_env = "msvc"),
80-
not(thumb_1)))]
77+
#[maybe_use_optimized_c_shim]
8178
pub extern "C" fn __modsi3(a: i32, b: i32) -> i32 {
8279
a.mod_(b)
8380
}
8481

85-
#[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))]
82+
#[maybe_use_optimized_c_shim]
8683
pub extern "C" fn __moddi3(a: i64, b: i64) -> i64 {
8784
a.mod_(b)
8885
}
@@ -92,8 +89,7 @@ intrinsics! {
9289
a.mod_(b)
9390
}
9491

95-
#[use_c_shim_if(all(target_arch = "arm", not(target_env = "msvc"),
96-
not(target_os = "ios"), not(thumb_1)))]
92+
#[maybe_use_optimized_c_shim]
9793
pub extern "C" fn __divmodsi4(a: i32, b: i32, rem: &mut i32) -> i32 {
9894
a.divmod(b, rem, |a, b| __divsi3(a, b))
9995
}

src/int/shift.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl Lshr for u64 {}
7474
impl Lshr for u128 {}
7575

7676
intrinsics! {
77-
#[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))]
77+
#[maybe_use_optimized_c_shim]
7878
#[arm_aeabi_alias = __aeabi_llsl]
7979
pub extern "C" fn __ashldi3(a: u64, b: u32) -> u64 {
8080
a.ashl(b)
@@ -84,7 +84,7 @@ intrinsics! {
8484
a.ashl(b)
8585
}
8686

87-
#[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))]
87+
#[maybe_use_optimized_c_shim]
8888
#[arm_aeabi_alias = __aeabi_lasr]
8989
pub extern "C" fn __ashrdi3(a: i64, b: u32) -> i64 {
9090
a.ashr(b)
@@ -94,7 +94,7 @@ intrinsics! {
9494
a.ashr(b)
9595
}
9696

97-
#[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))]
97+
#[maybe_use_optimized_c_shim]
9898
#[arm_aeabi_alias = __aeabi_llsr]
9999
pub extern "C" fn __lshrdi3(a: u64, b: u32) -> u64 {
100100
a.lshr(b)

src/int/udiv.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,7 @@ macro_rules! udivmod_inner {
152152
}
153153

154154
intrinsics! {
155-
#[use_c_shim_if(all(target_arch = "arm",
156-
not(target_os = "ios"),
157-
not(thumb_1)))]
155+
#[maybe_use_optimized_c_shim]
158156
#[arm_aeabi_alias = __aeabi_uidiv]
159157
/// Returns `n / d`
160158
pub extern "C" fn __udivsi3(n: u32, d: u32) -> u32 {
@@ -212,20 +210,14 @@ intrinsics! {
212210
(q << 1) | carry
213211
}
214212

215-
#[use_c_shim_if(all(target_arch = "arm",
216-
not(target_os = "ios"),
217-
not(target_env = "msvc"),
218-
not(thumb_1)))]
213+
#[maybe_use_optimized_c_shim]
219214
/// Returns `n % d`
220215
pub extern "C" fn __umodsi3(n: u32, d: u32) -> u32 {
221216
let q = __udivsi3(n, d);
222217
n - q * d
223218
}
224219

225-
#[use_c_shim_if(all(target_arch = "arm",
226-
not(target_os = "ios"),
227-
not(target_env = "msvc"),
228-
not(thumb_1)))]
220+
#[maybe_use_optimized_c_shim]
229221
/// Returns `n / d` and sets `*rem = n % d`
230222
pub extern "C" fn __udivmodsi4(n: u32, d: u32, rem: Option<&mut u32>) -> u32 {
231223
let q = __udivsi3(n, d);
@@ -235,13 +227,13 @@ intrinsics! {
235227
q
236228
}
237229

238-
#[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))]
230+
#[maybe_use_optimized_c_shim]
239231
/// Returns `n / d`
240232
pub extern "C" fn __udivdi3(n: u64, d: u64) -> u64 {
241233
__udivmoddi4(n, d, None)
242234
}
243235

244-
#[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))]
236+
#[maybe_use_optimized_c_shim]
245237
/// Returns `n % d`
246238
pub extern "C" fn __umoddi3(n: u64, d: u64) -> u64 {
247239
let mut rem = 0;

src/macros.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
///
3131
/// A quick overview of attributes supported right now are:
3232
///
33-
/// * `use_c_shim_if` - takes a #[cfg] directive and falls back to the
34-
/// C-compiled version if `use_c` is specified.
33+
/// * `maybe_use_optimized_c_shim` - indicates that the Rust implementation is
34+
/// ignored if an optimized C version was compiled.
3535
/// * `aapcs_on_arm` - forces the ABI of the function to be `"aapcs"` on ARM and
3636
/// the specified ABI everywhere else.
3737
/// * `unadjusted_on_win64` - like `aapcs_on_arm` this switches to the
@@ -51,15 +51,14 @@ macro_rules! intrinsics {
5151
// to the architecture-specific versions which should be more optimized. The
5252
// purpose of this macro is to easily allow specifying this.
5353
//
54-
// The argument to `use_c_shim_if` is a `#[cfg]` directive which, when true,
55-
// will cause this crate's exported version of `$name` to just redirect to
56-
// the C implementation. No symbol named `$name` will be in the object file
57-
// for this crate itself.
58-
//
59-
// When the `#[cfg]` directive is false, or when the `c` feature is
60-
// disabled, the provided implementation is used instead.
54+
// The `#[maybe_use_optimized_c_shim]` attribute indicates that this
55+
// intrinsic may have an optimized C version. In these situations the build
56+
// script, if the C code is enabled and compiled, will emit a cfg directive
57+
// to get passed to rustc for our compilation. If that cfg is set we skip
58+
// the Rust implementation, but if the attribute is not enabled then we
59+
// compile in the Rust implementation.
6160
(
62-
#[use_c_shim_if($($cfg_clause:tt)*)]
61+
#[maybe_use_optimized_c_shim]
6362
$(#[$($attr:tt)*])*
6463
pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) -> $ret:ty {
6564
$($body:tt)*
@@ -68,7 +67,7 @@ macro_rules! intrinsics {
6867
$($rest:tt)*
6968
) => (
7069

71-
#[cfg(all(use_c, $($cfg_clause)*))]
70+
#[cfg($name = "optimized-c")]
7271
pub extern $abi fn $name( $($argname: $ty),* ) -> $ret {
7372
extern $abi {
7473
fn $name($($argname: $ty),*) -> $ret;
@@ -78,7 +77,7 @@ macro_rules! intrinsics {
7877
}
7978
}
8079

81-
#[cfg(not(all(use_c, $($cfg_clause)*)))]
80+
#[cfg(not($name = "optimized-c"))]
8281
intrinsics! {
8382
$(#[$($attr)*])*
8483
pub extern $abi fn $name( $($argname: $ty),* ) -> $ret {

0 commit comments

Comments
 (0)