Why compiler can not recognize what variables are volatile?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Why compiler can not recognize what variables are volatile?

David Blaikie via cfe-dev
Hi,

If I compile the following program without -O*, it will print this.

$ ./main.exe
2
20

If I compile it with -O1 or any other number > 1, it will print this.

$ ./main.exe
2
10

The optimization clearly changes the semantics of the program. Why the
compiler can not figure out local_var2 is volatile on its own to
reduce the burdens of the programmers in having to figure out what
variables should be specified as volatile?

Thanks.

#include <stdio.h>
#include <setjmp.h>

static jmp_buf buf;

int main() {
    volatile int local_var = 1;
    int local_var2 = 10;
    if(!setjmp(buf)) {
        local_var = 2;
        local_var2 = 20;
        longjmp(buf, 1);
    } else {
        printf("%d\n", local_var);
        printf("%d\n", local_var2);
    }

    return 0;
}

--
Regards,
Peng
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Why compiler can not recognize what variables are volatile?

David Blaikie via cfe-dev
The question of explicitly modeling setjmp control flow has come up before; see http://lists.llvm.org/pipermail/llvm-dev/2017-November/118820.html .  It's probably feasible, but I don't think anyone has actually attempted to implement it.

-Eli

> -----Original Message-----
> From: cfe-dev <[hidden email]> On Behalf Of Peng Yu via cfe-
> dev
> Sent: Wednesday, February 6, 2019 4:43 PM
> To: cfe-dev <[hidden email]>
> Subject: [EXT] [cfe-dev] Why compiler can not recognize what variables are
> volatile?
>
> Hi,
>
> If I compile the following program without -O*, it will print this.
>
> $ ./main.exe
> 2
> 20
>
> If I compile it with -O1 or any other number > 1, it will print this.
>
> $ ./main.exe
> 2
> 10
>
> The optimization clearly changes the semantics of the program. Why the
> compiler can not figure out local_var2 is volatile on its own to
> reduce the burdens of the programmers in having to figure out what
> variables should be specified as volatile?
>
> Thanks.
>
> #include <stdio.h>
> #include <setjmp.h>
>
> static jmp_buf buf;
>
> int main() {
>     volatile int local_var = 1;
>     int local_var2 = 10;
>     if(!setjmp(buf)) {
>         local_var = 2;
>         local_var2 = 20;
>         longjmp(buf, 1);
>     } else {
>         printf("%d\n", local_var);
>         printf("%d\n", local_var2);
>     }
>
>     return 0;
> }
>
> --
> Regards,
> Peng
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev