[PATCH] D15293: [OpenCL] Correct NULL constant handling after applying default AS

Anastasia Stulova via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 7 09:09:08 PST 2015


Anastasia created this revision.
Anastasia added a reviewer: pekka.jaaskelainen.
Anastasia added a subscriber: cfe-commits.

In OpenCL2.0 s6.5 all pointers are implicitly in generic address space unless address space is explicitly specified.

Correct the behavior of NULL constant detection - generic AS void pointer should be accepted as a valid NULL constant.

http://reviews.llvm.org/D15293

Files:
  lib/AST/Expr.cpp
  lib/Sema/SemaExpr.cpp
  test/SemaOpenCL/null_literal.cl

Index: test/SemaOpenCL/null_literal.cl
===================================================================
--- /dev/null
+++ test/SemaOpenCL/null_literal.cl
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -verify %s
+// RUN: %clang_cc1 -cl-std=CL2.0 -DCL20 -verify %s
+
+#define NULL ((void*)0)
+
+void foo(){
+
+global int* ptr1 = NULL;
+
+global int* ptr2 = (global void*)0;
+
+constant int* ptr3 = NULL;
+
+constant int* ptr4 = (global void*)0; // expected-error{{initializing '__constant int *' with an expression of type '__global void *' changes address space of pointer}}
+
+#ifdef CL20
+// accept explicitly pointer to generic address space in OpenCL v2.0
+global int* ptr5 = (generic void*)0;
+#endif
+
+global int* ptr6 = (local void*)0; // expected-error{{initializing '__global int *' with an expression of type '__local void *' changes address space of pointer}}
+
+bool cmp = ptr1 == NULL;
+
+cmp = ptr1 == (local void*)0; // expected-error{{comparison between  ('__global int *' and '__local void *') which are pointers to non-overlapping address spaces}}
+
+cmp = ptr3 == NULL;
+
+}
Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp
+++ lib/Sema/SemaExpr.cpp
@@ -8807,7 +8807,8 @@
       diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, /*isError*/false);
     }
     if (LCanPointeeTy != RCanPointeeTy) {
-      if (getLangOpts().OpenCL) {
+      // Treat NULL constant as a special case in OpenCL
+      if (getLangOpts().OpenCL && !LHSIsNull && !RHSIsNull) {
         const PointerType *LHSPtr = LHSType->getAs<PointerType>();
         if (!LHSPtr->isAddressSpaceOverlapping(*RHSType->getAs<PointerType>())) {
           Diag(Loc,
Index: lib/AST/Expr.cpp
===================================================================
--- lib/AST/Expr.cpp
+++ lib/AST/Expr.cpp
@@ -3261,9 +3261,20 @@
       // Check that it is a cast to void*.
       if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
         QualType Pointee = PT->getPointeeType();
-        if (!Pointee.hasQualifiers() &&
-            Pointee->isVoidType() &&                              // to void*
-            CE->getSubExpr()->getType()->isIntegerType())         // from int.
+        Qualifiers Q = Pointee.getQualifiers();
+        // in OpenCL v2.0 generic address space acts as a placeholder
+        // and should be ignored.
+        bool IsASValid = true;
+        if (Ctx.getLangOpts().OpenCLVersion >= 200) {
+          if (Pointee.getAddressSpace() == LangAS::opencl_generic)
+            Q.removeAddressSpace();
+          else
+            IsASValid = false;
+        }
+
+        if (IsASValid && !Q.hasQualifiers() &&
+            Pointee->isVoidType() &&                      // to void*
+            CE->getSubExpr()->getType()->isIntegerType()) // from int.
           return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
       }
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D15293.42076.patch
Type: text/x-patch
Size: 2928 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20151207/47af518d/attachment.bin>


More information about the cfe-commits mailing list