diff options
| author | Xiang Fan <sfanxiang@gmail.com> | 2019-06-06 08:39:20 +0800 |
|---|---|---|
| committer | Xiang Fan <sfanxiang@gmail.com> | 2019-09-28 04:45:08 +0800 |
| commit | f71e0daa29b232d8f689f77fecb84dcb87fce6da (patch) | |
| tree | 7e450a5f114b0973aad2b5b7799bc7f54c6776c7 /src/test/codegen/non-terminate | |
| parent | a37fe2de697bb1a9d304e4e811836e125f944cd5 (diff) | |
| download | rust-f71e0daa29b232d8f689f77fecb84dcb87fce6da.tar.gz rust-f71e0daa29b232d8f689f77fecb84dcb87fce6da.zip | |
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
Diffstat (limited to 'src/test/codegen/non-terminate')
| -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..fa9c66b47c0 --- /dev/null +++ b/src/test/codegen/non-terminate/infinite-loop-1.rs @@ -0,0 +1,17 @@ +// compile-flags: -C opt-level=3 + +#![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..81d62ab33d7 --- /dev/null +++ b/src/test/codegen/non-terminate/infinite-loop-2.rs @@ -0,0 +1,19 @@ +// compile-flags: -C opt-level=3 + +#![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..6d1f2d4bf8f --- /dev/null +++ b/src/test/codegen/non-terminate/infinite-recursion.rs @@ -0,0 +1,14 @@ +// compile-flags: -C opt-level=3 + +#![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() +} |
