Skip to content

Commit 02774e1

Browse files
author
Andrew Savonichev
committed
[SYCL] Put string literals to private address space
Although OpenCL specification explicitly states that a string literal should reside in constant address space, it does not work for SYCL with "generic by default" address space rules. For example: const char *getLiteral() { return "A"; } void func(bool AorB) { char B[] = {'B', '\0'}; const char* C = AorB ? A : B; } If `A' reside in constant address space, it cannot be returned from a function `getLiteral', because it returns a generic const char*. Signed-off-by: Andrew Savonichev <andrew.savonichev@intel.com>
1 parent 6ea08ec commit 02774e1

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3663,6 +3663,14 @@ LangAS CodeGenModule::getStringLiteralAddressSpace() const {
36633663
// OpenCL v1.2 s6.5.3: a string literal is in the constant address space.
36643664
if (LangOpts.OpenCL)
36653665
return LangAS::opencl_constant;
3666+
if (LangOpts.SYCLIsDevice && !getenv("DISABLE_INFER_AS"))
3667+
// If we keep a literal string in constant address space, the following code
3668+
// becomes illegal:
3669+
//
3670+
// const char *getLiteral() n{
3671+
// return "AB";
3672+
// }
3673+
return LangAS::opencl_private;
36663674
if (auto AS = getTarget().getConstantAddressSpace())
36673675
return AS.getValue();
36683676
return LangAS::Default;

clang/test/CodeGenSYCL/address-space-new.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1-
// RUN: DISABLE_INFER_AS=1 %clang_cc1 -triple spir64-unknown-linux-sycldevice -std=c++11 -fsycl-is-device -disable-llvm-passes -emit-llvm -x c++ %s -o - | FileCheck %s --check-prefix=CHECK-LEGACY
2-
// RUN: %clang_cc1 -triple spir64-unknown-linux-sycldevice -std=c++11 -fsycl-is-device -disable-llvm-passes -emit-llvm -x c++ %s -o - | FileCheck %s --check-prefix=CHECK-NEW
3-
1+
// RUN: DISABLE_INFER_AS=1 %clang_cc1 -triple spir64-unknown-linux-sycldevice -std=c++11 -fsycl-is-device -disable-llvm-passes -emit-llvm -x c++ %s -o - | FileCheck %s --check-prefixes=CHECK,CHECK-LEGACY
2+
// RUN: %clang_cc1 -triple spir64-unknown-linux-sycldevice -std=c++11 -fsycl-is-device -disable-llvm-passes -emit-llvm -x c++ %s -o - | FileCheck %s --check-prefixes=CHECK,CHECK-NEW
43

54
void test() {
65
static const int foo = 0x42;
76
// CHECK-LEGACY: @_ZZ4testvE3foo = internal constant i32 66, align 4
87
// CHECK-NEW: @_ZZ4testvE3foo = internal addrspace(1) constant i32 66, align 4
98

9+
// CHECK: @[[STR:[.a-zA-Z0-9_]+]] = private unnamed_addr constant [14 x i8] c"Hello, world!\00", align 1
1010
int i = 0;
1111
int *pptr = &i;
1212
// CHECK-LEGACY: store i32* %i, i32** %pptr
1313
// CHECK-NEW: %[[GEN:[0-9]+]] = addrspacecast i32* %i to i32 addrspace(4)*
1414
// CHECK-NEW: store i32 addrspace(4)* %[[GEN]], i32 addrspace(4)** %pptr
15-
1615
*pptr = foo;
16+
17+
const char *str = "Hello, world!";
18+
// CHECK-LEGACY: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @[[STR]], i64 0, i64 0), i8** %{{.*}}, align 8
19+
// CHECK-NEW: store i8 addrspace(4)* addrspacecast (i8* getelementptr inbounds ([14 x i8], [14 x i8]* @[[STR]], i64 0, i64 0) to i8 addrspace(4)*), i8 addrspace(4)** %{{.*}}, align 8
20+
21+
i = str[0];
1722
}
1823

1924

0 commit comments

Comments
 (0)