Skip to content

Commit fddc18e

Browse files
committed
auto merge of #12105 : huonw/rust/bench-black-box, r=alexcrichton
This allows a result to be marked as "used" by passing it to a function LLVM cannot see inside (unless LTO is enabled). Closes #8261.
2 parents a2290db + 3844734 commit fddc18e

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

src/libarena/lib.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -536,18 +536,18 @@ mod test {
536536
x: 1,
537537
y: 2,
538538
z: 3,
539-
});
539+
})
540540
})
541541
}
542542

543543
#[bench]
544544
pub fn bench_pod_nonarena(bh: &mut BenchHarness) {
545545
bh.iter(|| {
546-
let _ = ~Point {
546+
~Point {
547547
x: 1,
548548
y: 2,
549549
z: 3,
550-
};
550+
}
551551
})
552552
}
553553

@@ -561,7 +561,7 @@ mod test {
561561
y: 2,
562562
z: 3,
563563
}
564-
});
564+
})
565565
})
566566
}
567567

@@ -588,28 +588,28 @@ mod test {
588588
arena.alloc(Nonpod {
589589
string: ~"hello world",
590590
array: ~[ 1, 2, 3, 4, 5 ],
591-
});
591+
})
592592
})
593593
}
594594

595595
#[bench]
596596
pub fn bench_nonpod_nonarena(bh: &mut BenchHarness) {
597597
bh.iter(|| {
598-
let _ = ~Nonpod {
598+
~Nonpod {
599599
string: ~"hello world",
600600
array: ~[ 1, 2, 3, 4, 5 ],
601-
};
601+
}
602602
})
603603
}
604604

605605
#[bench]
606606
pub fn bench_nonpod_old_arena(bh: &mut BenchHarness) {
607607
let arena = Arena::new();
608608
bh.iter(|| {
609-
let _ = arena.alloc(|| Nonpod {
609+
arena.alloc(|| Nonpod {
610610
string: ~"hello world",
611611
array: ~[ 1, 2, 3, 4, 5 ],
612-
});
612+
})
613613
})
614614
}
615615
}

src/libextra/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Rust extras are part of the standard Rust distribution.
2929
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
3030
html_root_url = "http://static.rust-lang.org/doc/master")];
3131

32-
#[feature(macro_rules, globs, managed_boxes)];
32+
#[feature(macro_rules, globs, managed_boxes, asm)];
3333

3434
#[deny(non_camel_case_types)];
3535
#[deny(missing_doc)];

src/libextra/test.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -1091,13 +1091,25 @@ impl MetricMap {
10911091
10921092
// Benchmarking
10931093
1094+
/// A function that is opaque to the optimiser, to allow benchmarks to
1095+
/// pretend to use outputs to assist in avoiding dead-code
1096+
/// elimination.
1097+
///
1098+
/// This function is a no-op, and does not even read from `dummy`.
1099+
pub fn black_box<T>(dummy: T) {
1100+
// we need to "use" the argument in some way LLVM can't
1101+
// introspect.
1102+
unsafe {asm!("" : : "r"(&dummy))}
1103+
}
1104+
1105+
10941106
impl BenchHarness {
10951107
/// Callback for benchmark functions to run in their body.
1096-
pub fn iter(&mut self, inner: ||) {
1108+
pub fn iter<T>(&mut self, inner: || -> T) {
10971109
self.ns_start = precise_time_ns();
10981110
let k = self.iterations;
10991111
for _ in range(0u64, k) {
1100-
inner();
1112+
black_box(inner());
11011113
}
11021114
self.ns_end = precise_time_ns();
11031115
}

0 commit comments

Comments
 (0)