diff options
| author | bors <bors@rust-lang.org> | 2019-10-10 15:40:39 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-10-10 15:40:39 +0000 |
| commit | 58b54911fabcd1b328fab78014b6833a4e8cfaa3 (patch) | |
| tree | 6f621f3f837f3fb915cccf5d47d6c768b07472d2 /src/test/codegen | |
| parent | 9c588c1e2391c20c156814f7ae118f62e96a3976 (diff) | |
| parent | e9acfa306f47a40be27e8cf72c55dbec35d94017 (diff) | |
| download | rust-58b54911fabcd1b328fab78014b6833a4e8cfaa3.tar.gz rust-58b54911fabcd1b328fab78014b6833a4e8cfaa3.zip | |
Auto merge of #59546 - sfanxiang:interminable-ub, r=nagisa
Add llvm.sideeffect to potential infinite loops and recursions
LLVM assumes that a thread will eventually cause side effect. This is
not true in Rust if a loop or recursion does nothing in its body,
causing undefined behavior even in common cases like `loop {}`.
Inserting llvm.sideeffect fixes the undefined behavior.
As a micro-optimization, only insert llvm.sideeffect when jumping back
in blocks or calling a function.
A patch for LLVM is expected to allow empty non-terminate code by
default and fix this issue from LLVM side.
https://github.com/rust-lang/rust/issues/28728
**UPDATE:** [Mentoring instructions here](https://github.com/rust-lang/rust/pull/59546#issuecomment-515072429) to unstall this PR
Diffstat (limited to 'src/test/codegen')
| -rw-r--r-- | src/test/codegen/non-terminate/infinite-loop-1.rs | 17 | ||||
| -rw-r--r-- | src/test/codegen/non-terminate/infinite-loop-2.rs | 19 | ||||
| -rw-r--r-- | src/test/codegen/non-terminate/infinite-recursion.rs | 14 |
3 files changed, 50 insertions, 0 deletions
diff --git a/src/test/codegen/non-terminate/infinite-loop-1.rs b/src/test/codegen/non-terminate/infinite-loop-1.rs new file mode 100644 index 00000000000..56b360e0a7f --- /dev/null +++ b/src/test/codegen/non-terminate/infinite-loop-1.rs @@ -0,0 +1,17 @@ +// compile-flags: -C opt-level=3 -Z insert-sideeffect + +#![crate_type = "lib"] + +fn infinite_loop() -> u8 { + loop {} +} + +// CHECK-LABEL: @test +#[no_mangle] +fn test() -> u8 { + // CHECK-NOT: unreachable + // CHECK: br label %{{.+}} + // CHECK-NOT: unreachable + let x = infinite_loop(); + x +} diff --git a/src/test/codegen/non-terminate/infinite-loop-2.rs b/src/test/codegen/non-terminate/infinite-loop-2.rs new file mode 100644 index 00000000000..2921ab6dc04 --- /dev/null +++ b/src/test/codegen/non-terminate/infinite-loop-2.rs @@ -0,0 +1,19 @@ +// compile-flags: -C opt-level=3 -Z insert-sideeffect + +#![crate_type = "lib"] + +fn infinite_loop() -> u8 { + let i = 2; + while i > 1 {} + 1 +} + +// CHECK-LABEL: @test +#[no_mangle] +fn test() -> u8 { + // CHECK-NOT: unreachable + // CHECK: br label %{{.+}} + // CHECK-NOT: unreachable + let x = infinite_loop(); + x +} diff --git a/src/test/codegen/non-terminate/infinite-recursion.rs b/src/test/codegen/non-terminate/infinite-recursion.rs new file mode 100644 index 00000000000..1f292ce379f --- /dev/null +++ b/src/test/codegen/non-terminate/infinite-recursion.rs @@ -0,0 +1,14 @@ +// compile-flags: -C opt-level=3 -Z insert-sideeffect + +#![crate_type = "lib"] + +#![allow(unconditional_recursion)] + +// CHECK-LABEL: @infinite_recursion +#[no_mangle] +fn infinite_recursion() -> u8 { + // CHECK-NOT: ret i8 undef + // CHECK: br label %{{.+}} + // CHECK-NOT: ret i8 undef + infinite_recursion() +} |
