Is optimization to move block inside both the block of a if-else branch correct?

46 Views Asked by At

Warning(Pseudocode)

Suppose we have a kernel :

def kernel(array):
   a = get_global_id(0)
   if a > 1:
      array[0] = 10
   barrier(LOCAL_MEM_FENCE)

Is it wrong for the code to get optimized like:

def kernel(array):
   a = get_global_id(0)
   if a > 1:
      array[0] = 10
      barrier(LOCAL_MEM_FENCE)
   else:
      barrier(LOCAL_MEM_FENCE)

Although this optimization doesn't look wrong, it yields incorrect results and data-races. I have looked some of the documentation provided by clang , but still can't figure out why this is wrong? This is on intel x86 architecture and consider OpenCl implementation for barriers. Is there an issue if we call barrier from different locations in different threads?

1

There are 1 best solutions below

0
On

No, you can't have OpenCL barriers in if-else branches, even if both branches eventually call the barrier. All barriers must be outside of branching statements.