[LLVMdev] More detailed example...

Chris Lattner sabre at nondot.org
Fri Jan 5 17:35:58 PST 2007


On Fri, 5 Jan 2007, Nick Lewycky wrote:
> Sarah Thompson wrote:
>> Further to that, I thought an example might be useful. In the following
>> code, n should end up with a value that varies nondeterministically with
>> the scheduling decisions made by the underlying run time system -- my
>> model checker, for example, should in theory be able to enumerate all
>> possible values. Anyway, if you look at the compiler output (see below),
>> the volatile global variable, n, has vanished, with the printf output
>> only ever taking the (constant) result 0.
>
> How are you compiling this? I get the following sort of output:

How are you compiling?  If you are running gccld, then your global is 
getting marked 'internal' and then the globalopt pass is promoting it to a 
local scalar.  We could make globalopt avoid doing this for globals with 
volatile accesses.

If you *are* running gccld, try passing -disable-internalize to see if 
this goes away.

-Chris


> void %inc(int* %p) {
> entry:
>        %tmp = volatile load int* %p            ; <int> [#uses=1]
>        %tmp1 = add int %tmp, 1         ; <int> [#uses=1]
>        volatile store int %tmp1, int* %p
>        tail call void (...)* %mcp_yield( )
>        %tmp.1 = volatile load int* %p          ; <int> [#uses=1]
>        %tmp1.1 = add int %tmp.1, 1             ; <int> [#uses=1]
>        volatile store int %tmp1.1, int* %p
>        tail call void (...)* %mcp_yield( )
>        %tmp.2 = volatile load int* %p          ; <int> [#uses=1]
>        %tmp1.2 = add int %tmp.2, 1             ; <int> [#uses=1]
>        volatile store int %tmp1.2, int* %p
>        tail call void (...)* %mcp_yield( )
>        ret void
> }
>
> I ran this with "llvm-gcc -c mcp.c -o mcp.bc". In the example you gave,
> it is valid for LLVM to optimize away the volatile memory accesses if
> it were doing link-time optimizations; it could prove that "int n"'s
> value never changed the observable behaviour of the program and elided it.
>
> Nick Lewycky
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>

-Chris

-- 
http://nondot.org/sabre/
http://llvm.org/



More information about the llvm-dev mailing list