Skip to content

Commit 96ddd4e

Browse files
author
Jorge Aparicio
committed
use test::black_box instead of ptr::*_volatile
both prevent LLVM from optimizing away the intrinsics but the former doesn't produce an `intrinsics` binary that segfaults
1 parent c824035 commit 96ddd4e

File tree

2 files changed

+53
-62
lines changed

2 files changed

+53
-62
lines changed

ci/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ esac
3232
# TODO(#79) fix the undefined references problem for debug-assertions+lto
3333
case $1 in
3434
thumb*)
35-
RUSTFLAGS="-C debug-assertions=no" xargo rustc --features c --target $1 --bin intrinsics -- -C lto
35+
RUSTFLAGS="-C debug-assertions=no -C link-arg=-nostartfiles" xargo rustc --features c --target $1 --bin intrinsics -- -C lto
3636
xargo rustc --features c --target $1 --bin intrinsics --release -- -C lto
3737
;;
3838
*)

src/bin/intrinsics.rs

Lines changed: 52 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
// to link due to the missing intrinsic (symbol).
55

66
#![allow(unused_features)]
7+
#![cfg_attr(thumb, no_main)]
78
#![deny(dead_code)]
9+
#![feature(asm)]
810
#![feature(core_float)]
911
#![feature(lang_items)]
1012
#![feature(libc)]
1113
#![feature(start)]
1214
#![no_std]
13-
#![cfg_attr(thumb, no_main)]
1415

1516
#[cfg(not(thumb))]
1617
extern crate libc;
@@ -302,68 +303,58 @@ mod intrinsics {
302303

303304
#[cfg(feature = "c")]
304305
fn run() {
305-
use core::ptr;
306306
use intrinsics::*;
307307

308-
// We use volatile load/stores to prevent LLVM from optimizing away the intrinsics during LTO
309-
macro_rules! arg {
310-
() => {
311-
ptr::read_volatile(0x0 as *const _)
312-
}
313-
}
314-
315-
macro_rules! ret {
316-
($e:expr) => {
317-
unsafe {
318-
ptr::write_volatile(0x0 as *mut _, $e)
319-
}
320-
}
321-
}
322-
323-
ret!(aeabi_d2f(arg!()));
324-
ret!(aeabi_d2i(arg!()));
325-
ret!(aeabi_d2l(arg!()));
326-
ret!(aeabi_d2uiz(arg!()));
327-
ret!(aeabi_d2ulz(arg!()));
328-
ret!(aeabi_dadd(arg!(), arg!()));
329-
ret!(aeabi_dcmpeq(arg!(), arg!()));
330-
ret!(aeabi_dcmpgt(arg!(), arg!()));
331-
ret!(aeabi_dcmplt(arg!(), arg!()));
332-
ret!(aeabi_ddiv(arg!(), arg!()));
333-
ret!(aeabi_dmul(arg!(), arg!()));
334-
ret!(aeabi_dsub(arg!(), arg!()));
335-
ret!(aeabi_f2d(arg!()));
336-
ret!(aeabi_f2iz(arg!()));
337-
ret!(aeabi_f2lz(arg!()));
338-
ret!(aeabi_f2uiz(arg!()));
339-
ret!(aeabi_f2ulz(arg!()));
340-
ret!(aeabi_fadd(arg!(), arg!()));
341-
ret!(aeabi_fcmpeq(arg!(), arg!()));
342-
ret!(aeabi_fcmpgt(arg!(), arg!()));
343-
ret!(aeabi_fcmplt(arg!(), arg!()));
344-
ret!(aeabi_fdiv(arg!(), arg!()));
345-
ret!(aeabi_fmul(arg!(), arg!()));
346-
ret!(aeabi_fsub(arg!(), arg!()));
347-
ret!(aeabi_i2d(arg!()));
348-
ret!(aeabi_i2f(arg!()));
349-
ret!(aeabi_idiv(arg!(), arg!()));
350-
ret!(aeabi_idivmod(arg!(), arg!()));
351-
ret!(aeabi_l2d(arg!()));
352-
ret!(aeabi_l2f(arg!()));
353-
ret!(aeabi_ldivmod(arg!(), arg!()));
354-
ret!(aeabi_lmul(arg!(), arg!()));
355-
ret!(aeabi_ui2d(arg!()));
356-
ret!(aeabi_ui2f(arg!()));
357-
ret!(aeabi_uidiv(arg!(), arg!()));
358-
ret!(aeabi_uidivmod(arg!(), arg!()));
359-
ret!(aeabi_ul2d(arg!()));
360-
ret!(aeabi_ul2f(arg!()));
361-
ret!(aeabi_uldivmod(arg!(), arg!()));
362-
ret!(moddi3(arg!(), arg!()));
363-
ret!(mulodi4(arg!(), arg!()));
364-
ret!(powidf2(arg!(), arg!()));
365-
ret!(powisf2(arg!(), arg!()));
366-
ret!(umoddi3(arg!(), arg!()));
308+
// A copy of "test::black_box". Used to prevent LLVM from optimizing away the intrinsics during LTO
309+
fn bb<T>(dummy: T) -> T {
310+
unsafe { asm!("" : : "r"(&dummy)) }
311+
dummy
312+
}
313+
314+
bb(aeabi_d2f(bb(2.)));
315+
bb(aeabi_d2i(bb(2.)));
316+
bb(aeabi_d2l(bb(2.)));
317+
bb(aeabi_d2uiz(bb(2.)));
318+
bb(aeabi_d2ulz(bb(2.)));
319+
bb(aeabi_dadd(bb(2.), bb(3.)));
320+
bb(aeabi_dcmpeq(bb(2.), bb(3.)));
321+
bb(aeabi_dcmpgt(bb(2.), bb(3.)));
322+
bb(aeabi_dcmplt(bb(2.), bb(3.)));
323+
bb(aeabi_ddiv(bb(2.), bb(3.)));
324+
bb(aeabi_dmul(bb(2.), bb(3.)));
325+
bb(aeabi_dsub(bb(2.), bb(3.)));
326+
bb(aeabi_f2d(bb(2.)));
327+
bb(aeabi_f2iz(bb(2.)));
328+
bb(aeabi_f2lz(bb(2.)));
329+
bb(aeabi_f2uiz(bb(2.)));
330+
bb(aeabi_f2ulz(bb(2.)));
331+
bb(aeabi_fadd(bb(2.), bb(3.)));
332+
bb(aeabi_fcmpeq(bb(2.), bb(3.)));
333+
bb(aeabi_fcmpgt(bb(2.), bb(3.)));
334+
bb(aeabi_fcmplt(bb(2.), bb(3.)));
335+
bb(aeabi_fdiv(bb(2.), bb(3.)));
336+
bb(aeabi_fmul(bb(2.), bb(3.)));
337+
bb(aeabi_fsub(bb(2.), bb(3.)));
338+
bb(aeabi_i2d(bb(2)));
339+
bb(aeabi_i2f(bb(2)));
340+
bb(aeabi_idiv(bb(2), bb(3)));
341+
bb(aeabi_idivmod(bb(2), bb(3)));
342+
bb(aeabi_l2d(bb(2)));
343+
bb(aeabi_l2f(bb(2)));
344+
bb(aeabi_ldivmod(bb(2), bb(3)));
345+
bb(aeabi_lmul(bb(2), bb(3)));
346+
bb(aeabi_ui2d(bb(2)));
347+
bb(aeabi_ui2f(bb(2)));
348+
bb(aeabi_uidiv(bb(2), bb(3)));
349+
bb(aeabi_uidivmod(bb(2), bb(3)));
350+
bb(aeabi_ul2d(bb(2)));
351+
bb(aeabi_ul2f(bb(2)));
352+
bb(aeabi_uldivmod(bb(2), bb(3)));
353+
bb(moddi3(bb(2), bb(3)));
354+
bb(mulodi4(bb(2), bb(3)));
355+
bb(powidf2(bb(2.), bb(3)));
356+
bb(powisf2(bb(2.), bb(3)));
357+
bb(umoddi3(bb(2), bb(3)));
367358
}
368359

369360
#[cfg(all(feature = "c", not(thumb)))]

0 commit comments

Comments
 (0)