[LLVMdev] LLVM introduces racy read - Unsafe transformation?

sohachak at mpi-sws.org sohachak at mpi-sws.org
Mon Jan 26 07:22:18 PST 2015


Hi,

I am looking for thoughts on the following LLVM transformation.

Consider the following transformation which replaces conditional load(a)
with load(a);select instructions.

Source
--------
int a; bool flag;

int readA() {
 int r=0;
 if(flag) {
   r = a;
 }
return r;
}

Command
--------
clang++ -std=c++11 -pthread -emit-llvm <filename>.cpp -S;opt -O3
<filename>.ll -o <filename>.opt.bc -S

Target
-------

define i32 @_Z5readAv() #3 {
entry:
  %0 = load i8* @flag, align 1
  %1 = and i8 %0, 1
  %tobool = icmp eq i8 %1, 0
  %2 = load i32* @a, align 4
  %. = select i1 %tobool, i32 0, i32 %2
  ret i32 %.
}

Consider the following function writeA() runs in parallel with readA().

void writeA(){
  a = 42;
}

The source program has no data race if flag=false. But the target program
is racy due to the introduced load(a) operation.

This is a benign race since the load(a) is used only when flag=true.

However, according to the C11/C++11 consistency model the semantics of a
racy program is undefined and may have arbitrary behavior.

Thus the transformation is unsafe.

Note: The full example files are attached.

Regards,
soham
-------------- next part --------------
A non-text attachment was scrubbed...
Name: loadrace.zip
Type: application/x-zip-compressed
Size: 28366 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150126/31013770/attachment.bin>


More information about the llvm-dev mailing list