Balaram Makam
2014-Apr-28 19:01 UTC
[LLVMdev] correctness in localized global variable between setjmp and longjmp
I noticed that c code below failed with -O3 : #include <setjmp.h> #include <stdio.h> static int cnt; int main(){ int var; jmp_buf buffer; cnt = 0; if ((var = setjmp(buffer)) == 0) { printf(" if true, var: %d, .count:%d , increase count !\n",var, cnt); cnt++; longjmp(buffer, 2); } else { if (cnt == 1) printf(" Pass var: %d, .count:%d \n",var, cnt); else printf(" Fail !!! var: %d, .count:%d \n",var, cnt); } return 0; } The documentation of setjmp/longjmp says that the values of objects of automatic storage duration which are local to the function containing the invocation of the corresponding setjmp() which do not have volatile-qualified type and which are changed between the setjmp() invocation and longjmp() call are indeterminate. Even though "cnt" is a global variable, above c code failed because the globalopt pass localizes the global value (cnt) in main(), causing that the value of the global variable becomes indeterminate between setjmp() and longjmp. I believe the globalopt pass need to check this case before localizing global variables. Is there any better solution about it? Thanks, Balaram -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140428/1be2d8e6/attachment.html>
Rafael Espíndola
2014-Apr-28 21:37 UTC
[LLVMdev] correctness in localized global variable between setjmp and longjmp
On 28 April 2014 15:01, Balaram Makam <bmakam at codeaurora.org> wrote:> I noticed that c code below failed with -O3 : > > > > #include <setjmp.h> > > #include <stdio.h> > > > > static int cnt; > > > > int main(){ > > int var; > > jmp_buf buffer; > > cnt = 0; > > > > if ((var = setjmp(buffer)) == 0) { > > printf(" if true, var: %d, .count:%d , increase count !\n",var, cnt); > > cnt++; > > longjmp(buffer, 2); > > } else { > > if (cnt == 1) > > printf(" Pass var: %d, .count:%d \n",var, cnt); > > else > > printf(" Fail !!! var: %d, .count:%d \n",var, cnt); > > } > > return 0; > > } > > > > The documentation of setjmp/longjmp says that the values of objects of > automatic storage duration which are local to the function containing the > invocation of the corresponding setjmp() which do not have > volatile-qualified type and which are changed between the setjmp() > invocation and longjmp() call are indeterminate. > > > > Even though “cnt” is a global variable, above c code failed because the > globalopt pass localizes the global value (cnt) in main(), causing that the > value of the global variable becomes indeterminate between setjmp() and > longjmp. I believe the globalopt pass need to check this case before > localizing global variables. Is there any better solution about it?No, looks like it has to check it. This is a very specific optimization, but it looks like it is still important in old benchmarks. Cheers, Rafael
Chad Rosier
2014-Apr-29 17:27 UTC
[LLVMdev] correctness in localized global variable between setjmp and longjmp
Balaram, Perhaps you should file a bug report here: http://llvm.org/bugs/ Chad> I noticed that c code below failed with -O3 : > > > > #include <setjmp.h> > > #include <stdio.h> > > > > static int cnt; > > > > int main(){ > > int var; > > jmp_buf buffer; > > cnt = 0; > > > > if ((var = setjmp(buffer)) == 0) { > > printf(" if true, var: %d, .count:%d , increase count !\n",var, cnt); > > cnt++; > > longjmp(buffer, 2); > > } else { > > if (cnt == 1) > > printf(" Pass var: %d, .count:%d \n",var, cnt); > > else > > printf(" Fail !!! var: %d, .count:%d \n",var, cnt); > > } > > return 0; > > } > > > > The documentation of setjmp/longjmp says that the values of objects of > automatic storage duration which are local to the function containing the > invocation of the corresponding setjmp() which do not have > volatile-qualified type and which are changed between the setjmp() > invocation and longjmp() call are indeterminate. > > > > Even though "cnt" is a global variable, above c code failed because the > globalopt pass localizes the global value (cnt) in main(), causing that > the > value of the global variable becomes indeterminate between setjmp() and > longjmp. I believe the globalopt pass need to check this case before > localizing global variables. Is there any better solution about it? > > > > Thanks, > > Balaram > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >-- Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation