Author: yaxunl Date: Tue Jul 18 07:46:03 2017 New Revision: 308313 URL: http://llvm.org/viewvc/llvm-project?rev=308313&view=rev Log: CodeGen: Insert addr space cast for automatic/temp var at right position
The uses of alloca may be in different blocks other than the block containing the alloca. Therefore if the alloca addr space is non-zero and it needs to be casted to default address space, the cast needs to be inserted in the same BB as the alloca insted of the current builder insert point since the current insert point may be in a different BB. Differential Revision: https://reviews.llvm.org/D35438 Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp cfe/trunk/test/CodeGenCXX/amdgcn-automatic-variable.cpp Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=308313&r1=308312&r2=308313&view=diff ============================================================================== --- cfe/trunk/lib/CodeGen/CGExpr.cpp (original) +++ cfe/trunk/lib/CodeGen/CGExpr.cpp Tue Jul 18 07:46:03 2017 @@ -73,9 +73,12 @@ Address CodeGenFunction::CreateTempAlloc // cast alloca to the default address space when necessary. if (CastToDefaultAddrSpace && getASTAllocaAddressSpace() != LangAS::Default) { auto DestAddrSpace = getContext().getTargetAddressSpace(LangAS::Default); + auto CurIP = Builder.saveIP(); + Builder.SetInsertPoint(AllocaInsertPt); V = getTargetHooks().performAddrSpaceCast( *this, V, getASTAllocaAddressSpace(), LangAS::Default, Ty->getPointerTo(DestAddrSpace), /*non-null*/ true); + Builder.restoreIP(CurIP); } return Address(V, Align); Modified: cfe/trunk/test/CodeGenCXX/amdgcn-automatic-variable.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/amdgcn-automatic-variable.cpp?rev=308313&r1=308312&r2=308313&view=diff ============================================================================== --- cfe/trunk/test/CodeGenCXX/amdgcn-automatic-variable.cpp (original) +++ cfe/trunk/test/CodeGenCXX/amdgcn-automatic-variable.cpp Tue Jul 18 07:46:03 2017 @@ -13,31 +13,32 @@ void func1(int *x) { // CHECK-LABEL: define void @_Z5func2v() void func2(void) { // CHECK: %lv1 = alloca i32, align 4, addrspace(5) + // CHECK: %[[r0:.*]] = addrspacecast i32 addrspace(5)* %lv1 to i32* // CHECK: %lv2 = alloca i32, align 4, addrspace(5) + // CHECK: %[[r1:.*]] = addrspacecast i32 addrspace(5)* %lv2 to i32* // CHECK: %la = alloca [100 x i32], align 4, addrspace(5) + // CHECK: %[[r2:.*]] = addrspacecast [100 x i32] addrspace(5)* %la to [100 x i32]* // CHECK: %lp1 = alloca i32*, align 8, addrspace(5) + // CHECK: %[[r3:.*]] = addrspacecast i32* addrspace(5)* %lp1 to i32** // CHECK: %lp2 = alloca i32*, align 8, addrspace(5) + // CHECK: %[[r4:.*]] = addrspacecast i32* addrspace(5)* %lp2 to i32** // CHECK: %lvc = alloca i32, align 4, addrspace(5) + // CHECK: %[[r5:.*]] = addrspacecast i32 addrspace(5)* %lvc to i32* - // CHECK: %[[r0:.*]] = addrspacecast i32 addrspace(5)* %lv1 to i32* // CHECK: store i32 1, i32* %[[r0]] int lv1; lv1 = 1; - // CHECK: %[[r1:.*]] = addrspacecast i32 addrspace(5)* %lv2 to i32* // CHECK: store i32 2, i32* %[[r1]] int lv2 = 2; - // CHECK: %[[r2:.*]] = addrspacecast [100 x i32] addrspace(5)* %la to [100 x i32]* // CHECK: %[[arrayidx:.*]] = getelementptr inbounds [100 x i32], [100 x i32]* %[[r2]], i64 0, i64 0 // CHECK: store i32 3, i32* %[[arrayidx]], align 4 int la[100]; la[0] = 3; - // CHECK: %[[r3:.*]] = addrspacecast i32* addrspace(5)* %lp1 to i32** // CHECK: store i32* %[[r0]], i32** %[[r3]], align 8 int *lp1 = &lv1; - // CHECK: %[[r4:.*]] = addrspacecast i32* addrspace(5)* %lp2 to i32** // CHECK: %[[arraydecay:.*]] = getelementptr inbounds [100 x i32], [100 x i32]* %[[r2]], i32 0, i32 0 // CHECK: store i32* %[[arraydecay]], i32** %[[r4]], align 8 int *lp2 = la; @@ -45,7 +46,6 @@ void func2(void) { // CHECK: call void @_Z5func1Pi(i32* %[[r0]]) func1(&lv1); - // CHECK: %[[r5:.*]] = addrspacecast i32 addrspace(5)* %lvc to i32* // CHECK: store i32 4, i32* %[[r5]] // CHECK: store i32 4, i32* %[[r0]] const int lvc = 4; @@ -81,4 +81,25 @@ void func4(int x) { func1(&x); } +// CHECK-LABEL: define void @_Z5func5v +void func5() { + return; + int x = 0; +} + +// CHECK-LABEL: define void @_Z5func6v +void func6() { + return; + int x; +} + +// CHECK-LABEL: define void @_Z5func7v +extern void use(int *); +void func7() { + goto later; + int x; +later: + use(&x); +} + // CHECK-NOT: !opencl.ocl.version _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits