Skip to content

Commit b7b6d0f

Browse files
committedApr 12, 2016
[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
1 parent 691ef09 commit b7b6d0f

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed
 

‎clang/lib/CodeGen/CGExprScalar.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,10 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
14111411
}
14121412
case CK_AddressSpaceConversion: {
14131413
Value *Src = Visit(const_cast<Expr*>(E));
1414-
return Builder.CreateAddrSpaceCast(Src, ConvertType(DestTy));
1414+
// Since target may map different address spaces in AST to the same address
1415+
// space, an address space conversion may end up as a bitcast.
1416+
return Builder.CreatePointerBitCastOrAddrSpaceCast(Src,
1417+
ConvertType(DestTy));
14151418
}
14161419
case CK_AtomicToNonAtomic:
14171420
case CK_NonAtomicToAtomic:

‎clang/test/CodeGenOpenCL/address-spaces-conversions.cl

+18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// RUN: %clang_cc1 %s -triple x86_64-unknown-linux-gnu -O0 -ffake-address-space-map -cl-std=CL2.0 -emit-llvm -o - | FileCheck %s
2+
// RUN: %clang_cc1 %s -triple x86_64-unknown-linux-gnu -O0 -cl-std=CL2.0 -emit-llvm -o - | FileCheck --check-prefix=CHECK-NOFAKE %s
3+
// When -ffake-address-space-map is not used, all addr space mapped to 0 for x86_64.
24

35
// test that we generate address space casts everywhere we need conversions of
46
// pointers to different address spaces
@@ -7,17 +9,33 @@ void test(global int *arg_glob, generic int *arg_gen) {
79
int var_priv;
810
arg_gen = arg_glob; // implicit cast global -> generic
911
// CHECK: %{{[0-9]+}} = addrspacecast i32 addrspace(1)* %{{[0-9]+}} to i32 addrspace(4)*
12+
// CHECK-NOFAKE-NOT: addrspacecast
1013

1114
arg_gen = &var_priv; // implicit cast with obtaining adr, private -> generic
1215
// CHECK: %{{[0-9]+}} = addrspacecast i32* %var_priv to i32 addrspace(4)*
16+
// CHECK-NOFAKE-NOT: addrspacecast
17+
1318
arg_glob = (global int *)arg_gen; // explicit cast
1419
// CHECK: %{{[0-9]+}} = addrspacecast i32 addrspace(4)* %{{[0-9]+}} to i32 addrspace(1)*
20+
// CHECK-NOFAKE-NOT: addrspacecast
21+
1522
global int *var_glob =
1623
(global int *)arg_glob; // explicit cast in the same address space
1724
// CHECK-NOT: %{{[0-9]+}} = addrspacecast i32 addrspace(1)* %{{[0-9]+}} to i32 addrspace(1)*
25+
// CHECK-NOFAKE-NOT: addrspacecast
26+
1827
var_priv = arg_gen - arg_glob; // arithmetic operation
1928
// CHECK: %{{.*}} = ptrtoint i32 addrspace(4)* %{{.*}} to i64
2029
// CHECK: %{{.*}} = ptrtoint i32 addrspace(1)* %{{.*}} to i64
30+
// CHECK-NOFAKE: %{{.*}} = ptrtoint i32* %{{.*}} to i64
31+
// CHECK-NOFAKE: %{{.*}} = ptrtoint i32* %{{.*}} to i64
32+
2133
var_priv = arg_gen > arg_glob; // comparison
2234
// CHECK: %{{[0-9]+}} = addrspacecast i32 addrspace(1)* %{{[0-9]+}} to i32 addrspace(4)*
35+
36+
generic void *var_gen_v = arg_glob;
37+
// CHECK: addrspacecast
38+
// CHECK-NOT: bitcast
39+
// CHECK-NOFAKE: bitcast
40+
// CHECK-NOFAKE-NOT: addrspacecast
2341
}

0 commit comments

Comments
 (0)
Please sign in to comment.