OSDN Git Service

Fix invalid addrspacecast due to combining alloca with global var
authorYaxun Liu <Yaxun.Liu@amd.com>
Fri, 10 Feb 2017 21:46:07 +0000 (21:46 +0000)
committerYaxun Liu <Yaxun.Liu@amd.com>
Fri, 10 Feb 2017 21:46:07 +0000 (21:46 +0000)
commitbdc2c07348384d05211dc09d24064164d541e4b0
treebb729316f94b9bb4de64999843963707d82c9775
parenta613077da74e9dd650ecfc54f86456442af6e772
Fix invalid addrspacecast due to combining alloca with global var

For function-scope variables with large initialisation list, FE usually
generates a global variable to hold the initializer, then generates
memcpy intrinsic to initialize the alloca. InstCombiner::visitAllocaInst
identifies such allocas which are accessed only by reading and replaces
them with the global variable. This is done by casting the global variable
to the type of the alloca and replacing all references.

However, when the global variable is in a different address space which
is disjoint with addr space 0 (e.g. for IR generated from OpenCL,
global variable cannot be in private addr space i.e. addr space 0), casting
the global variable to addr space 0 results in invalid IR for certain
targets (e.g. amdgpu).

To fix this issue, when the global variable is not in addr space 0,
instead of casting it to addr space 0, this patch chases down the uses
of alloca until reaching the load instructions, then replaces load from
alloca with load from the global variable. If during the chasing
bitcast and GEP are encountered, new bitcast and GEP based on the global
variable are generated and used in the load instructions.

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@294786 91177308-0d34-0410-b5e6-96231b3b80d8
lib/Transforms/InstCombine/InstCombineInternal.h
lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
test/Transforms/InstCombine/memcpy-addrspace.ll [new file with mode: 0644]