[PATCH] [OpenCL] Add string literals to the constant address space.

Joey Gouly joey.gouly at arm.com
Thu Nov 14 06:51:04 PST 2013


  I updated this patch to remove the 'const' part as I now believe it is wrong, and I changed IsModifiable as Tanya suggested.

  Ok to commit?

  Sorry about the long delay on this.

http://llvm-reviews.chandlerc.com/D894

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D894?vs=2209&id=5545#toc

Files:
  lib/AST/ExprClassification.cpp
  lib/CodeGen/CodeGenModule.cpp
  lib/Sema/SemaExpr.cpp
  test/CodeGenOpenCL/str_literals.cl
  test/SemaOpenCL/str_literals.cl

Index: lib/AST/ExprClassification.cpp
===================================================================
--- lib/AST/ExprClassification.cpp
+++ lib/AST/ExprClassification.cpp
@@ -592,6 +592,8 @@
   // 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: lib/CodeGen/CodeGenModule.cpp
===================================================================
--- lib/CodeGen/CodeGenModule.cpp
+++ lib/CodeGen/CodeGenModule.cpp
@@ -2590,11 +2590,16 @@
   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: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp
+++ lib/Sema/SemaExpr.cpp
@@ -1516,6 +1516,11 @@
                                  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,
Index: test/CodeGenOpenCL/str_literals.cl
===================================================================
--- /dev/null
+++ test/CodeGenOpenCL/str_literals.cl
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 %s -emit-llvm -o - -ffake-address-space-map | FileCheck %s
+
+__constant char * __constant x = "hello world";
+
+// CHECK: addrspace(3) unnamed_addr constant
+// CHECK: @x = addrspace(3) global i8 addrspace(3)*
Index: test/SemaOpenCL/str_literals.cl
===================================================================
--- /dev/null
+++ 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("heallo world");
+  foo(x);
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D894.4.patch
Type: text/x-patch
Size: 3079 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131114/0d5bb62e/attachment.bin>


More information about the cfe-commits mailing list