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

Joey Gouly joey.gouly at arm.com
Fri May 31 03:10:08 PDT 2013


  I made LangOpts.OpenCL imply LangOpts.ConstStrings, and updated some of the tests I missed the first time round.

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

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

Files:
  lib/Frontend/CompilerInvocation.cpp
  lib/Sema/SemaExpr.cpp
  lib/Sema/SemaType.cpp
  test/CodeGenOpenCL/opencl_types.cl
  test/SemaOpenCL/address-spaces.cl
  test/SemaOpenCL/str_literals.cl

Index: lib/Frontend/CompilerInvocation.cpp
===================================================================
--- lib/Frontend/CompilerInvocation.cpp
+++ lib/Frontend/CompilerInvocation.cpp
@@ -1014,6 +1014,7 @@
     Opts.LaxVectorConversions = 0;
     Opts.DefaultFPContract = 1;
     Opts.NativeHalfType = 1;
+    Opts.ConstStrings = 1;
   }
 
   if (LangStd == LangStandard::lang_cuda)
Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp
+++ lib/Sema/SemaExpr.cpp
@@ -1467,6 +1467,11 @@
   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 = 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/CodeGenOpenCL/opencl_types.cl
===================================================================
--- test/CodeGenOpenCL/opencl_types.cl
+++ test/CodeGenOpenCL/opencl_types.cl
@@ -1,7 +1,7 @@
 // RUN: %clang_cc1 %s -emit-llvm -o - -O0 | FileCheck %s
 
 constant sampler_t glb_smp = 7;
-// CHECK: global i32 7
+// CHECK: constant i32 7
 
 void fnc1(image1d_t img) {}
 // CHECK: @fnc1(%opencl.image1d_t*
Index: test/SemaOpenCL/address-spaces.cl
===================================================================
--- test/SemaOpenCL/address-spaces.cl
+++ test/SemaOpenCL/address-spaces.cl
@@ -9,5 +9,5 @@
   int *ip;
   ip = gip; // expected-error {{assigning '__global int *' to 'int *' changes address space of pointer}}
   ip = &li; // expected-error {{assigning '__local int *' to 'int *' changes address space of pointer}}
-  ip = &ci; // expected-error {{assigning '__constant int *' to 'int *' changes address space of pointer}}
+  ip = &ci; // expected-error {{assigning 'const __constant int *' to 'int *' changes address space of pointer}}
 }
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.2.patch
Type: text/x-patch
Size: 2970 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130531/c9db9b1a/attachment.bin>


More information about the cfe-commits mailing list