-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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. #28728 **UPDATE:** [Mentoring instructions here](#59546 (comment)) to unstall this PR
- Loading branch information
Showing
9 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |