[OpenCL][CodeGen] Fix replacing memcpy with addrspacecast
authorAndrew Savonichev <andrew.savonichev@intel.com>
Mon, 10 Dec 2018 12:03:00 +0000 (12:03 +0000)
committerAndrew Savonichev <andrew.savonichev@intel.com>
Mon, 10 Dec 2018 12:03:00 +0000 (12:03 +0000)
commit1bf1a156d673d5d48ef2ca41cba642c5ed11d683
tree2fff37735798eee282d6a2deef6246140cec73ae
parent045c67769d7fe577fc38cccb6fb40fd814437447
[OpenCL][CodeGen] Fix replacing memcpy with addrspacecast

Summary:
If a function argument is byval and RV is located in default or alloca address space
an optimization of creating addrspacecast instead of memcpy is performed. That is
not correct for OpenCL, where that can lead to a situation of address space casting
from __private * to __global *. See an example below:

```
typedef struct {
  int x;
} MyStruct;

void foo(MyStruct val) {}

kernel void KernelOneMember(__global MyStruct* x) {
  foo (*x);
}
```

for this code clang generated following IR:
...
%0 = load %struct.MyStruct addrspace(1)*, %struct.MyStruct addrspace(1)**
%x.addr, align 4
%1 = addrspacecast %struct.MyStruct addrspace(1)* %0 to %struct.MyStruct*
...

So the optimization was disallowed for OpenCL if RV is located in an address space
different than that of the argument (0).

Reviewers: yaxunl, Anastasia

Reviewed By: Anastasia

Subscribers: cfe-commits, asavonic

Differential Revision: https://reviews.llvm.org/D54947

llvm-svn: 348752
clang/lib/CodeGen/CGCall.cpp
clang/test/CodeGenOpenCL/addr-space-struct-arg.cl