[llvm-dev] Meaning of loads/stores marked both atomic and volatile

Paweł Batko via llvm-dev llvm-dev at lists.llvm.org
Tue Nov 21 02:27:42 PST 2017


On 21 November 2017 at 08:53, Tim Northover <t.p.northover at gmail.com> wrote:
> The global variable i is shadowed by the local there and I can't be
> sure exactly what you intended so I won't comment on it directly.

My mistake. I intended it to be a write to the shared variable i. Let me fix it.

Example 1.

// shared variable
int i = 0;

// call this method from 10 threads
void foo(){
    i = rand() % 2; // was: int i = rand() % 2;
    int j = i;
    while(i == j){
        printf("In the loop\n");
    }
}

> But in general terms atomic LLVM operations with at least "monotonic"
> ordering forbid unrestricted store-forwarding within a thread

> "If an address is written
> monotonic-ally by one thread, and other threads monotonic-ally read
> that address repeatedly, the other threads must eventually see the
> write."

Ok, let's say in Example 1. monotonic atomic prevents loop
optimization because compiler assumes existence of other threads.
(Compiler (effectively) assumes existence of other threads the moment
one starts using at least monotonic atomic load/stores?)


>> Example 2.
>>
>> // shared variable
>> int i = 0;
>>
>> void signalHandler(){
>>     i = 1;
>> }
>>
>> void main(){
>>     while(i == 0){
>>         printf("In the loop\n");
>>     }
>> }
>
> This is an interesting one. Monotonic atomic is again sufficient to
> synchronize with another thread (or signal handler I'd argue).

In Example. 2 let's consider signal handler in a single thread situation.
If monotonic atomic prevents loop optimization as in Example 1., then
I say it does the same Example 2.
It's because compiler cannot know that 'signalHandler' function is a
signal handler function, so it must assume it might be executed in
another thread.



-- 
Paweł Batko


More information about the llvm-dev mailing list