[llvm-dev] setjmp in llvm

Wael Yehia via llvm-dev llvm-dev at lists.llvm.org
Fri Jun 2 14:50:15 PDT 2017


Hi,I'm trying to prevent llvm instruction motion around an intrinsic function call. Throughout my experimenting, I was told that setjmp could create fake entry points into a region of code and that might prevent code motion.What I found is something surprising, and probably is a misuse of setjmp but I couldn't find an explanation for it.Consider this:#include <csetjmp>
std::jmp_buf jb;int main() {  int s = 1;  setjmp(jb);   if (s) {    s = 0;    std::longjmp(jb, 1);    return 2;  }  return 1;}
One would expect that the load of s in the if condition is not optimized away (by being replaced with if(1)), but clang at -O3 (on linux) generates this:define signext i32 @main() local_unnamed_addr #0 {entry:  %call = call signext i32 @_setjmp(%struct.__jmp_buf_tag* getelementptr inbounds ([1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* @jb, i64 0, i64 0)) #3  call void @longjmp(%struct.__jmp_buf_tag* getelementptr inbounds ([1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* @jb, i64 0, i64 0), i32 signext 1) #4  unreachable}
which leads to an infinite loop execution at runtime.Aren't we breaking the as-if rule because the semantics of the program imply that the value of s is unknown after the setjmp (because you can enter main from the location of setjmp in the program).
Thanks.
Wael
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170602/5d9da202/attachment.html>


More information about the llvm-dev mailing list