]> granicus.if.org Git - clang/commitdiff
[OpenCL] Make sure we put string literals in the constant address space.
authorJoey Gouly <joey.gouly@arm.com>
Thu, 14 Nov 2013 18:26:10 +0000 (18:26 +0000)
committerJoey Gouly <joey.gouly@arm.com>
Thu, 14 Nov 2013 18:26:10 +0000 (18:26 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@194717 91177308-0d34-0410-b5e6-96231b3b80d8

lib/AST/ExprClassification.cpp
lib/CodeGen/CodeGenModule.cpp
lib/Sema/SemaExpr.cpp
test/CodeGenOpenCL/str_literals.cl [new file with mode: 0644]
test/SemaOpenCL/str_literals.cl [new file with mode: 0644]

index 4640d7d37d61b32f53a5acd9c3f4a377ce3f020b..54f77efef0f9ef68e4957108f89bc7794ed0a0c7 100644 (file)
@@ -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())
index 1f2f4bbde3ce9b9dcdfb6eb67396273cf791c70d..da54de0bed8868cddc657ccb7f9ca44edc0eb413 100644 (file)
@@ -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;
index 7cd567050934645a065a43a81d04ef2f8971106f..8ec58d4283de823f1759ef6ac6c56a114efddf30 100644 (file)
@@ -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 (file)
index 0000000..78a9305
--- /dev/null
@@ -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 (file)
index 0000000..da665c3
--- /dev/null
@@ -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);
+}