From 758c4d86bb9b2298374fce5b3ca4a35f953f2d2e Mon Sep 17 00:00:00 2001 From: Joey Gouly Date: Thu, 14 Nov 2013 18:26:10 +0000 Subject: [PATCH] [OpenCL] Make sure we put string literals in the constant address space. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@194717 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/AST/ExprClassification.cpp | 2 ++ lib/CodeGen/CodeGenModule.cpp | 13 +++++++++---- lib/Sema/SemaExpr.cpp | 5 +++++ test/CodeGenOpenCL/str_literals.cl | 9 +++++++++ test/SemaOpenCL/str_literals.cl | 13 +++++++++++++ 5 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 test/CodeGenOpenCL/str_literals.cl create mode 100644 test/SemaOpenCL/str_literals.cl diff --git a/lib/AST/ExprClassification.cpp b/lib/AST/ExprClassification.cpp index 4640d7d37d..54f77efef0 100644 --- a/lib/AST/ExprClassification.cpp +++ b/lib/AST/ExprClassification.cpp @@ -592,6 +592,8 @@ static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E, // Const stuff is obviously not modifiable. if (CT.isConstQualified()) return Cl::CM_ConstQualified; + if (CT.getQualifiers().getAddressSpace() == LangAS::opencl_constant) + return Cl::CM_ConstQualified; // Arrays are not modifiable, only their elements are. if (CT->isArrayType()) diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index 1f2f4bbde3..da54de0bed 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -2625,11 +2625,16 @@ static llvm::GlobalVariable *GenerateStringLiteral(StringRef str, llvm::Constant *C = llvm::ConstantDataArray::getString(CGM.getLLVMContext(), str, false); + // OpenCL v1.1 s6.5.3: a string literal is in the constant address space. + unsigned AddrSpace = 0; + if (CGM.getLangOpts().OpenCL) + AddrSpace = CGM.getContext().getTargetAddressSpace(LangAS::opencl_constant); + // Create a global variable for this string - llvm::GlobalVariable *GV = - new llvm::GlobalVariable(CGM.getModule(), C->getType(), constant, - llvm::GlobalValue::PrivateLinkage, - C, GlobalName); + llvm::GlobalVariable *GV = new llvm::GlobalVariable( + CGM.getModule(), C->getType(), constant, + llvm::GlobalValue::PrivateLinkage, C, GlobalName, 0, + llvm::GlobalVariable::NotThreadLocal, AddrSpace); GV->setAlignment(Alignment); GV->setUnnamedAddr(true); return GV; diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 7cd5670509..8ec58d4283 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -1517,6 +1517,11 @@ Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks, llvm::APInt(32, Literal.GetNumStringChars()+1), ArrayType::Normal, 0); + // OpenCL v1.1 s6.5.3: a string literal is in the constant address space. + if (getLangOpts().OpenCL) { + StrTy = Context.getAddrSpaceQualType(StrTy, LangAS::opencl_constant); + } + // Pass &StringTokLocs[0], StringTokLocs.size() to factory! StringLiteral *Lit = StringLiteral::Create(Context, Literal.GetString(), Kind, Literal.Pascal, StrTy, diff --git a/test/CodeGenOpenCL/str_literals.cl b/test/CodeGenOpenCL/str_literals.cl new file mode 100644 index 0000000000..78a93056b9 --- /dev/null +++ b/test/CodeGenOpenCL/str_literals.cl @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 %s -emit-llvm -o - -ffake-address-space-map | FileCheck %s + +__constant char * __constant x = "hello world"; +__constant char * __constant y = "hello world"; + +// CHECK: addrspace(3) unnamed_addr constant +// CHECK-NOT: addrspace(3) unnamed_addr constant +// CHECK: @x = addrspace(3) global i8 addrspace(3)* +// CHECK: @y = addrspace(3) global i8 addrspace(3)* diff --git a/test/SemaOpenCL/str_literals.cl b/test/SemaOpenCL/str_literals.cl new file mode 100644 index 0000000000..da665c3fd1 --- /dev/null +++ b/test/SemaOpenCL/str_literals.cl @@ -0,0 +1,13 @@ +// RUN: %clang_cc1 %s -verify +// expected-no-diagnostics + +constant char * __constant x = "hello world"; + +void foo(__constant char * a) { + +} + +void bar() { + foo("hello world"); + foo(x); +} -- 2.40.0