Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite loop enabling optimization and debugging #13238

Closed
erickt opened this issue Apr 1, 2014 · 4 comments
Closed

Infinite loop enabling optimization and debugging #13238

erickt opened this issue Apr 1, 2014 · 4 comments

Comments

@erickt
Copy link
Contributor

erickt commented Apr 1, 2014

Compiling this code with both -O and -g gets into an infinite loop:

#![feature(asm)]

pub fn black_box<T>(dummy: T) { unsafe { asm!("" : : "r"(&dummy)) } }

fn silent_recurse() {
    let buf = [0, ..1000];
    black_box(buf);
    silent_recurse();
}

fn main() {
    silent_recurse();
}

The problem is that enabling debugging seems to optimize silent_recurse into a tail call. Here's the optimized-but-not-debugged function:

...
; Function Attrs: nounwind uwtable
define internal fastcc void @_ZN14silent_recurse20h29298914b0c999efraa4v0.0E() unnamed_addr #1 {
entry-block:
  %arg = alloca [1000 x i64], align 8
  %arg4 = bitcast [1000 x i64]* %arg to i8*
  call void @llvm.memset.p0i8.i64(i8* %arg4, i8 0, i64 8000, i32 8, i1 false)
  call void asm "", "r,~{dirflag},~{fpsr},~{flags}"([1000 x i64]* %arg) #0
  call fastcc void @_ZN14silent_recurse20h29298914b0c999efraa4v0.0E()
  ret void
}
...

Here's the optimized-and-debugged function:

...
; Function Attrs: nounwind uwtable
define internal fastcc void @_ZN14silent_recurse20h29298914b0c999efraa4v0.0E() unnamed_addr #2 {
entry-block:
  %arg = alloca [1000 x i64], align 8
  %arg4 = bitcast [1000 x i64]* %arg to i8*
  call void @llvm.memset.p0i8.i64(i8* %arg4, i8 0, i64 8000, i32 8, i1 false)
  call fastcc void @_ZN9black_box20h802b764baabf3bd5eaa4v0.0E([1000 x i64]* nocapture %arg), !dbg !27
  tail call fastcc void @_ZN14silent_recurse20h29298914b0c999efraa4v0.0E(), !dbg !28
  ret void, !dbg !28
}

; Function Attrs: nounwind uwtable
define internal fastcc void @_ZN9black_box20h802b764baabf3bd5eaa4v0.0E([1000 x i64]* noalias nocapture readonly) unnamed_addr #2 {
entry-block:
  %dummy = alloca [1000 x i64], align 8
  %1 = bitcast [1000 x i64]* %0 to i8*
  %2 = bitcast [1000 x i64]* %dummy to i8*
  call void @llvm.memcpy.p0i8.p0i8.i64(i8* %2, i8* %1, i64 8000, i32 8, i1 false)
  call void @llvm.dbg.declare(metadata !{[1000 x i64]* %dummy}, metadata !23), !dbg !29
  call void asm "", "r,~{dirflag},~{fpsr},~{flags}"([1000 x i64]* %dummy) #0, !dbg !30
  ret void, !dbg !30
}
...
@Aatch
Copy link
Contributor

Aatch commented Apr 1, 2014

So I'm really confused here. What's the issue? You have unconditional recursion, so an infinite loop existing isn't a problem. And I assume that compilation finished, so it's not compilation that's going in an infinite loop.

I'm not sure where the bug is here, and if there is one, it seems like it'd be in LLVM, not Rust...

@dmski
Copy link
Contributor

dmski commented Apr 1, 2014

@Aatch the code is taken from the test (run-pass/out-of-stack.rs) and if you happen to have RUSTFLAGS='-g' when running tests, they'll never finish because of this. I think that this issue might have something to do with the way debug info is generated. Or it might not - in this case the test will probably have to be modified to ensure that it doesn't compile to tail call even with '-g -O'

@alexcrichton
Copy link
Member

I agree with @Aatch, I don't think this is a bug. I think that test just needs to get rewritten in a way that LLVM won't optimize to an infinite loop.

@Aatch
Copy link
Contributor

Aatch commented Apr 1, 2014

Discussed this with @erickt on IRC. Based on my understanding on LLVM's optimizations, it's likely that debugging is preventing the inlining of black_box, which means that the asm isn't inlined, which means the tail-call optimisation isn't prevented. Whether or not it should do the call -> tail call transformation with debugging on is questionable, but Rust is not the place to have that discussion.

Marking black_box with #[inline(always)] should fix this.

Unless somebody has any objections, I'm going to close this issue.

@Aatch Aatch closed this as completed Apr 1, 2014
erickt added a commit to erickt/rust that referenced this issue Apr 4, 2014
This fixes rust-lang#13238. It avoids an infinite loop when compiling
the tests with `-g`. Without this change, the debuginfo on
`black_box` prevents the method from being inlined, which
allows llvm to convert `silent_recurse` into a tail-call. This
then loops forever instead of consuming all the stack like it
is supposed to. This patch forces inlining `black_box`, which
triggers the right error.
ckendell pushed a commit to ckendell/rust that referenced this issue Apr 4, 2014
This fixes rust-lang#13238. It avoids an infinite loop when compiling
the tests with `-g`. Without this change, the debuginfo on
`black_box` prevents the method from being inlined, which
allows llvm to convert `silent_recurse` into a tail-call. This
then loops forever instead of consuming all the stack like it
is supposed to. This patch forces inlining `black_box`, which
triggers the right error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants