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

Joey Gouly joey.gouly at arm.com
Thu May 30 07:21:51 PDT 2013


OpenCL requires string literals to be in the constant address space.

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

Files:
  lib/Sema/SemaExpr.cpp
  lib/Sema/SemaType.cpp
  test/SemaOpenCL/str_literals.cl

Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp
+++ lib/Sema/SemaExpr.cpp
@@ -1467,6 +1467,12 @@
   if (getLangOpts().CPlusPlus || getLangOpts().ConstStrings)
     StrTy.addConst();
 
+  // OpenCL v1.1 s6.5.3: a string literal is in the constant address space.
+  if (getLangOpts().OpenCL) {
+    StrTy.addConst();
+    StrTy = Context.getAddrSpaceQualType(StrTy, LangAS::opencl_constant);
+  }
+
   // Get an array type for the string, according to C99 6.4.5.  This includes
   // the nul terminator character as well as the string length for pascal
   // strings.
Index: lib/Sema/SemaType.cpp
===================================================================
--- lib/Sema/SemaType.cpp
+++ lib/Sema/SemaType.cpp
@@ -3829,6 +3829,10 @@
 
   unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
   Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
+
+  // Constant address space implies const qualifier
+  if(S.getLangOpts().OpenCL && ASIdx == LangAS::opencl_constant)
+    Type = S.Context.getConstType(Type);
 }
 
 /// Does this type have a "direct" ownership qualifier?  That is,
Index: test/SemaOpenCL/str_literals.cl
===================================================================
--- /dev/null
+++ test/SemaOpenCL/str_literals.cl
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 %s -verify
+// expected-no-diagnostics
+
+__constant char * __constant x = "hello world";
+
+void foo(__constant char * a) {
+
+}
+
+void bar() {
+  foo("hello world");
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D894.1.patch
Type: text/x-patch
Size: 1540 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130530/f51f2e1d/attachment.bin>


More information about the cfe-commits mailing list