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

Paweł Batko via llvm-dev llvm-dev at lists.llvm.org
Mon Nov 20 13:17:53 PST 2017


Hi Tim,

On 20 November 2017 at 16:41, Tim Northover <t.p.northover at gmail.com> wrote:
> There are only a couple of valid uses for volatile these days

Do you mean volatile used alone or also the combination 'atomic volatile'?

It think that 'atomic volatile' is very useful. Consider following
pseudo-code examples, where
all loads and stores are atomic (with some memory ordering
constraints) but not volatile.

Example 1.

// shared variable
int i = 0;

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

I claim that the loop can be optimized to an infinite loop by a
compiler, because apparently j == i at all times in a single threaded
program.
If loads and stores (particularly the read in loop predicated) were
also marked as volatile, it wouldn't have been possible.
Is this correct?


Example 2.

// shared variable
int i = 0;

void signalHandler(){
    i = 1;
}

void main(){
    while(i == 0){
        printf("In the loop\n");
    }
}

Here I also claim that the loop can be optimized into an infinite loop
if volatile is not used.
Is this correct?

-- 
Paweł Batko


More information about the llvm-dev mailing list