From b7b6d0fc664f974512279a55a07bebbeb0c3b129 Mon Sep 17 00:00:00 2001 From: Yaxun Liu Date: Tue, 12 Apr 2016 19:03:49 +0000 Subject: [PATCH] [OpenCL] Handle AddressSpaceConversion when target address space does not change. In codegen different address spaces may be mapped to the same address space for a target, e.g. in x86/x86-64 all address spaces are mapped to 0. Therefore AddressSpaceConversion should be translated by CreatePointerBitCastOrAddrSpaceCast instead of CreateAddrSpaceCast. Differential Revision: http://reviews.llvm.org/D18713 llvm-svn: 266107 --- clang/lib/CodeGen/CGExprScalar.cpp | 5 ++++- clang/test/CodeGenOpenCL/address-spaces-conversions.cl | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp index 76bfc57..1fd79d0 100644 --- a/clang/lib/CodeGen/CGExprScalar.cpp +++ b/clang/lib/CodeGen/CGExprScalar.cpp @@ -1411,7 +1411,10 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) { } case CK_AddressSpaceConversion: { Value *Src = Visit(const_cast(E)); - return Builder.CreateAddrSpaceCast(Src, ConvertType(DestTy)); + // Since target may map different address spaces in AST to the same address + // space, an address space conversion may end up as a bitcast. + return Builder.CreatePointerBitCastOrAddrSpaceCast(Src, + ConvertType(DestTy)); } case CK_AtomicToNonAtomic: case CK_NonAtomicToAtomic: diff --git a/clang/test/CodeGenOpenCL/address-spaces-conversions.cl b/clang/test/CodeGenOpenCL/address-spaces-conversions.cl index cdd6b58..a306e00 100644 --- a/clang/test/CodeGenOpenCL/address-spaces-conversions.cl +++ b/clang/test/CodeGenOpenCL/address-spaces-conversions.cl @@ -1,4 +1,6 @@ // RUN: %clang_cc1 %s -triple x86_64-unknown-linux-gnu -O0 -ffake-address-space-map -cl-std=CL2.0 -emit-llvm -o - | FileCheck %s +// RUN: %clang_cc1 %s -triple x86_64-unknown-linux-gnu -O0 -cl-std=CL2.0 -emit-llvm -o - | FileCheck --check-prefix=CHECK-NOFAKE %s +// When -ffake-address-space-map is not used, all addr space mapped to 0 for x86_64. // test that we generate address space casts everywhere we need conversions of // pointers to different address spaces @@ -7,17 +9,33 @@ void test(global int *arg_glob, generic int *arg_gen) { int var_priv; arg_gen = arg_glob; // implicit cast global -> generic // CHECK: %{{[0-9]+}} = addrspacecast i32 addrspace(1)* %{{[0-9]+}} to i32 addrspace(4)* + // CHECK-NOFAKE-NOT: addrspacecast arg_gen = &var_priv; // implicit cast with obtaining adr, private -> generic // CHECK: %{{[0-9]+}} = addrspacecast i32* %var_priv to i32 addrspace(4)* + // CHECK-NOFAKE-NOT: addrspacecast + arg_glob = (global int *)arg_gen; // explicit cast // CHECK: %{{[0-9]+}} = addrspacecast i32 addrspace(4)* %{{[0-9]+}} to i32 addrspace(1)* + // CHECK-NOFAKE-NOT: addrspacecast + global int *var_glob = (global int *)arg_glob; // explicit cast in the same address space // CHECK-NOT: %{{[0-9]+}} = addrspacecast i32 addrspace(1)* %{{[0-9]+}} to i32 addrspace(1)* + // CHECK-NOFAKE-NOT: addrspacecast + var_priv = arg_gen - arg_glob; // arithmetic operation // CHECK: %{{.*}} = ptrtoint i32 addrspace(4)* %{{.*}} to i64 // CHECK: %{{.*}} = ptrtoint i32 addrspace(1)* %{{.*}} to i64 + // CHECK-NOFAKE: %{{.*}} = ptrtoint i32* %{{.*}} to i64 + // CHECK-NOFAKE: %{{.*}} = ptrtoint i32* %{{.*}} to i64 + var_priv = arg_gen > arg_glob; // comparison // CHECK: %{{[0-9]+}} = addrspacecast i32 addrspace(1)* %{{[0-9]+}} to i32 addrspace(4)* + + generic void *var_gen_v = arg_glob; + // CHECK: addrspacecast + // CHECK-NOT: bitcast + // CHECK-NOFAKE: bitcast + // CHECK-NOFAKE-NOT: addrspacecast } -- 2.7.4