olestrohm updated this revision to Diff 345436.
olestrohm added a comment.

Added a codegen test and clarified what some parts are testing.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102248/new/

https://reviews.llvm.org/D102248

Files:
  clang/lib/Sema/SemaDecl.cpp
  clang/test/CodeGenOpenCLCXX/addrspace-constructors.clcpp
  clang/test/SemaOpenCLCXX/addrspace-constructors.clcpp

Index: clang/test/SemaOpenCLCXX/addrspace-constructors.clcpp
===================================================================
--- /dev/null
+++ clang/test/SemaOpenCLCXX/addrspace-constructors.clcpp
@@ -0,0 +1,45 @@
+// RUN: %clang_cc1 %s -pedantic -verify -fsyntax-only
+
+__constant int g1; // expected-error {{variable in constant address space must be initialized}}
+__constant int g2 = 0;
+
+struct X {
+  int x;
+  constexpr X() __constant : x(0) {}
+  constexpr X(int x) __constant : x(x) {}
+};
+
+//expected-note@+2{{candidate constructor (the implicit copy constructor) not viable: no known conversion from 'int' to 'const __generic Y' for 1st argument}}
+//expected-note@+1{{candidate constructor (the implicit move constructor) not viable: no known conversion from 'int' to '__generic Y' for 1st argument}}
+struct Y {
+  int y;
+  Y() __generic = default; // expected-note{{candidate constructor not viable: requires 0 arguments, but 1 was provided}}
+};
+
+kernel void k() {
+  __constant X cx1;
+  __constant X cx2(1);
+  __local X lx;
+
+  __private Y py;
+  __constant Y cy1; // expected-error{{variable in constant address space must be initialized}}
+  __constant Y cy2(1); // expected-error{{no matching constructor for initialization of '__constant Y'}}
+}
+
+struct Z {
+  int z;
+  // The address space is deduced to be __generic if omitted
+  Z() = default; // expected-note{{previous definition is here}}
+  Z() __generic = default; // expected-error {{constructor cannot be redeclared}}
+
+  Z() __private = default;
+  Z() __local = default;
+  Z() __global = default;
+  // Can't default constexpr constructors
+  constexpr Z() __constant : z(0) {}
+};
+
+struct W {
+  int w;
+  constexpr W() __constant = default; // expected-error {{defaulted definition of default constructor is not constexpr}}
+};
Index: clang/test/CodeGenOpenCLCXX/addrspace-constructors.clcpp
===================================================================
--- /dev/null
+++ clang/test/CodeGenOpenCLCXX/addrspace-constructors.clcpp
@@ -0,0 +1,40 @@
+// RUN: %clang_cc1 %s -emit-llvm -o - -O0 -triple spir-unknown-unknown | FileCheck %s
+
+// CHECK: %struct.X = type { i32 }
+
+// CHECK: @ci = dso_local addrspace(2) constant i32 0, align 4
+// CHECK: @gi = dso_local addrspace(1) global i32 0, align 4
+__constant int ci = 0;
+__global int gi = 0;
+
+struct X {
+  int x;
+
+  // Local variables are handled in local_addrspace_init.clcpp
+  // FIXME: __private and __generic constructors clash for __private variable
+  // X() /*__generic*/ = default;
+  X() __private : x(0) {}
+  X() __global : x(0) {}
+  constexpr X() __constant : x(0) {}
+  constexpr X(int x) __constant : x(x) {}
+};
+
+// CHECK: @cx1 = dso_local addrspace(2) constant %struct.X zeroinitializer, align 4
+// CHECK: @cx2 = dso_local addrspace(2) constant %struct.X { i32 1 }, align 4
+// CHECK: @gx = dso_local addrspace(1) global %struct.X zeroinitializer, align 4
+__constant X cx1;
+__constant X cx2(1);
+__global X gx;
+
+// CHECK: @_ZZ1kE3cx1 = internal addrspace(2) constant %struct.X zeroinitializer, align 4
+// CHECK: @_ZZ1kE3cx2 = internal addrspace(2) constant %struct.X { i32 1 }, align 4
+kernel void k() {
+  // Check that the constructor for px is executed
+  __private X px;
+  // CHECK: [[PX:%[.a-z0-9]+]] = alloca %struct.X, align 4
+  // CHECK-NEXT: call spir_func void @_ZN1XC1Ev(%struct.X* nonnull dereferenceable(4) [[PX]]) #3
+
+  __constant X cx1;
+  __constant X cx2(1);
+  // CHECK-NEXT: ret void
+}
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -12638,9 +12638,21 @@
     if (!Var->isInvalidDecl() &&
         Var->getType().getAddressSpace() == LangAS::opencl_constant &&
         Var->getStorageClass() != SC_Extern && !Var->getInit()) {
-      Diag(Var->getLocation(), diag::err_opencl_constant_no_init);
-      Var->setInvalidDecl();
-      return;
+      bool HasConstExprDefaultConstructor = false;
+      if (CXXRecordDecl *RD = Var->getType()->getAsCXXRecordDecl()) {
+        for (auto *Ctor : RD->ctors()) {
+          if (Ctor->isConstexpr() && Ctor->getNumParams() == 0 &&
+              Ctor->getMethodQualifiers().getAddressSpace() ==
+                  LangAS::opencl_constant) {
+            HasConstExprDefaultConstructor = true;
+          }
+        }
+      }
+      if (!HasConstExprDefaultConstructor) {
+        Diag(Var->getLocation(), diag::err_opencl_constant_no_init);
+        Var->setInvalidDecl();
+        return;
+      }
     }
 
     if (!Var->isInvalidDecl() && RealDecl->hasAttr<LoaderUninitializedAttr>()) {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to