Skip to content

Commit 10c6681

Browse files
committed
Gate llvm.sideeffect under -Z insert-sideeffect
1 parent f71e0da commit 10c6681

File tree

16 files changed

+36
-45
lines changed

16 files changed

+36
-45
lines changed

src/librustc/session/config.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1472,6 +1472,9 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
14721472
"which mangling version to use for symbol names"),
14731473
binary_dep_depinfo: bool = (false, parse_bool, [TRACKED],
14741474
"include artifacts (sysroot, crate dependencies) used during compilation in dep-info"),
1475+
insert_sideeffect: bool = (false, parse_bool, [TRACKED],
1476+
"fix undefined behavior when a thread doesn't eventually make progress \
1477+
(such as entering an empty infinite loop) by inserting llvm.sideeffect"),
14751478
}
14761479

14771480
pub fn default_lib_output() -> CrateType {

src/librustc_codegen_llvm/intrinsic.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -726,8 +726,10 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
726726
}
727727

728728
fn sideeffect(&mut self) {
729-
let fnname = self.get_intrinsic(&("llvm.sideeffect"));
730-
self.call(fnname, &[], None);
729+
if self.tcx.sess.opts.debugging_opts.insert_sideeffect {
730+
let fnname = self.get_intrinsic(&("llvm.sideeffect"));
731+
self.call(fnname, &[], None);
732+
}
731733
}
732734

733735
fn va_start(&mut self, va_list: &'ll Value) -> &'ll Value {

src/librustc_codegen_ssa/mir/block.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,15 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'a, 'tcx> {
157157
bx: &mut Bx,
158158
targets: &[mir::BasicBlock],
159159
) {
160-
if targets.iter().any(|target| {
161-
*target <= *self.bb
162-
&& target
163-
.start_location()
164-
.is_predecessor_of(self.bb.start_location(), mir)
165-
}) {
166-
bx.sideeffect();
160+
if bx.tcx().sess.opts.debugging_opts.insert_sideeffect {
161+
if targets.iter().any(|target| {
162+
*target <= *self.bb
163+
&& target
164+
.start_location()
165+
.is_predecessor_of(self.bb.start_location(), mir)
166+
}) {
167+
bx.sideeffect();
168+
}
167169
}
168170
}
169171
}

src/test/codegen/alloc-optimisation.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
pub fn alloc_test(data: u32) {
88
// CHECK-LABEL: @alloc_test
99
// CHECK-NEXT: start:
10-
// CHECK-NOT: alloc
11-
// CHECK: ret void
10+
// CHECK-NEXT: ret void
1211
let x = Box::new(data);
1312
drop(x);
1413
}

src/test/codegen/dealloc-no-unwind.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl Drop for A {
1717
pub fn a(a: Box<i32>) {
1818
// CHECK-LABEL: define void @a
1919
// CHECK: call void @__rust_dealloc
20-
// CHECK: call void @foo
20+
// CHECK-NEXT: call void @foo
2121
let _a = A;
2222
drop(a);
2323
}

src/test/codegen/issue-34947-pow-i32.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66
#[no_mangle]
77
pub fn issue_34947(x: i32) -> i32 {
88
// CHECK: mul
9-
// CHECK-NOT: br label
10-
// CHECK: mul
11-
// CHECK-NOT: br label
12-
// CHECK: mul
13-
// CHECK-NOT: br label
14-
// CHECK: ret
9+
// CHECK-NEXT: mul
10+
// CHECK-NEXT: mul
11+
// CHECK-NEXT: ret
1512
x.pow(5)
1613
}

src/test/codegen/issue-45222.rs

-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
// ignore-test
2-
3-
// FIXME:
4-
// LLVM can't optimize some loops with a large number of iterations because of
5-
// @llvm.sideeffect() (see also #59546)
6-
71
// compile-flags: -O
82
// ignore-debug: the debug assertions get in the way
93

src/test/codegen/naked-functions.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ignore-tidy-linelength
2+
13
// compile-flags: -C no-prepopulate-passes
24

35
#![crate_type = "lib"]
@@ -51,27 +53,27 @@ pub fn naked_with_args_and_return(a: isize) -> isize {
5153
#[naked]
5254
pub fn naked_recursive() {
5355
// CHECK-NEXT: {{.+}}:
54-
// CHECK: call void @naked_empty()
56+
// CHECK-NEXT: call void @naked_empty()
5557

5658
// FIXME(#39685) Avoid one block per call.
5759
// CHECK-NEXT: br label %bb1
5860
// CHECK: bb1:
5961

6062
naked_empty();
6163

62-
// CHECK: %{{[0-9]+}} = call i{{[0-9]+}} @naked_with_return()
64+
// CHECK-NEXT: %{{[0-9]+}} = call i{{[0-9]+}} @naked_with_return()
6365

6466
// FIXME(#39685) Avoid one block per call.
6567
// CHECK-NEXT: br label %bb2
6668
// CHECK: bb2:
6769

68-
// CHECK: %{{[0-9]+}} = call i{{[0-9]+}} @naked_with_args_and_return(i{{[0-9]+}} %{{[0-9]+}})
70+
// CHECK-NEXT: %{{[0-9]+}} = call i{{[0-9]+}} @naked_with_args_and_return(i{{[0-9]+}} %{{[0-9]+}})
6971

7072
// FIXME(#39685) Avoid one block per call.
7173
// CHECK-NEXT: br label %bb3
7274
// CHECK: bb3:
7375

74-
// CHECK: call void @naked_with_args(i{{[0-9]+}} %{{[0-9]+}})
76+
// CHECK-NEXT: call void @naked_with_args(i{{[0-9]+}} %{{[0-9]+}})
7577

7678
// FIXME(#39685) Avoid one block per call.
7779
// CHECK-NEXT: br label %bb4

src/test/codegen/non-terminate/infinite-loop-1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -C opt-level=3
1+
// compile-flags: -C opt-level=3 -Z insert-sideeffect
22

33
#![crate_type = "lib"]
44

src/test/codegen/non-terminate/infinite-loop-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -C opt-level=3
1+
// compile-flags: -C opt-level=3 -Z insert-sideeffect
22

33
#![crate_type = "lib"]
44

src/test/codegen/non-terminate/infinite-recursion.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -C opt-level=3
1+
// compile-flags: -C opt-level=3 -Z insert-sideeffect
22

33
#![crate_type = "lib"]
44

src/test/codegen/repeat-trusted-len.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ pub fn helper(_: usize) {
1414
// CHECK-LABEL: @repeat_take_collect
1515
#[no_mangle]
1616
pub fn repeat_take_collect() -> Vec<u8> {
17-
// FIXME: At the time of writing LLVM transforms this loop into a single
18-
// `store` and then a `memset` with size = 99999. The correct check should be:
19-
// call void @llvm.memset.p0i8.[[USIZE]](i8* {{(nonnull )?}}align 1 %{{[a-z0-9.]+}}, i8 42, [[USIZE]] 100000, i1 false)
20-
21-
// CHECK: call void @llvm.memset.p0i8.[[USIZE]](i8* {{(nonnull )?}}align 1 %{{[a-z0-9.]+}}, i8 42, [[USIZE]] 99999, i1 false)
17+
// CHECK: call void @llvm.memset.p0i8.[[USIZE]](i8* {{(nonnull )?}}align 1 %{{[0-9]+}}, i8 42, [[USIZE]] 100000, i1 false)
2218
iter::repeat(42).take(100000).collect()
2319
}

src/test/codegen/vec-clear.rs

-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
// ignore-test
2-
3-
// FIXME:
4-
// LLVM can't optimize some loops with unknown number of iterations because of
5-
// @llvm.sideeffect() (see also #59546)
6-
71
// ignore-debug: the debug assertions get in the way
82
// compile-flags: -O
93

src/test/codegen/vec-iter-collect-len.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#[no_mangle]
77
pub fn get_len() -> usize {
8-
// CHECK-COUNT-1: {{^define}}
8+
// CHECK-LABEL: @get_len
9+
// CHECK-NOT: call
10+
// CHECK-NOT: invoke
911
[1, 2, 3].iter().collect::<Vec<_>>().len()
1012
}

src/test/codegen/vec-optimizes-away.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
pub fn sum_me() -> i32 {
99
// CHECK-LABEL: @sum_me
1010
// CHECK-NEXT: {{^.*:$}}
11-
// CHECK: ret i32 6
11+
// CHECK-NEXT: ret i32 6
1212
vec![1, 2, 3].iter().sum::<i32>()
1313
}

src/test/run-make-fulldeps/inline-always-many-cgu/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
all:
44
$(RUSTC) foo.rs --emit llvm-ir -C codegen-units=2
5-
if cat $(TMPDIR)/*.ll | grep -v 'call void @llvm.sideeffect()' | $(CGREP) -e '\bcall\b'; then \
5+
if cat $(TMPDIR)/*.ll | $(CGREP) -e '\bcall\b'; then \
66
echo "found call instruction when one wasn't expected"; \
77
exit 1; \
88
fi

0 commit comments

Comments
 (0)