Author: Ole Strohm
Date: 2021-09-13T12:56:04+01:00
New Revision: 8008009fd25bf51c2c85c612bfefec64e975bbe4

URL: 
https://github.com/llvm/llvm-project/commit/8008009fd25bf51c2c85c612bfefec64e975bbe4
DIFF: 
https://github.com/llvm/llvm-project/commit/8008009fd25bf51c2c85c612bfefec64e975bbe4.diff

LOG: [OpenCL] Initialize temporaries in the private address space

This patch fixes initializing temporaries, which are currently initialized
without an address space, meaning that no constructor can ever be applicable.
Now they will be constructed in the private addrspace.

Fixes the second issue in PR43296.

Reviewed By: Anastasia

Differential Revision: https://reviews.llvm.org/D107553

Added: 
    clang/test/SemaOpenCLCXX/temporaries.clcpp

Modified: 
    clang/include/clang/Sema/Initialization.h
    clang/lib/Sema/SemaExprCXX.cpp
    clang/test/SemaOpenCLCXX/addrspace-constructors.clcpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Sema/Initialization.h 
b/clang/include/clang/Sema/Initialization.h
index 8feb66995f95c..4664861c4e32a 100644
--- a/clang/include/clang/Sema/Initialization.h
+++ b/clang/include/clang/Sema/Initialization.h
@@ -335,8 +335,15 @@ class alignas(8) InitializedEntity {
   }
 
   /// Create the initialization entity for a temporary.
-  static InitializedEntity InitializeTemporary(TypeSourceInfo *TypeInfo) {
-    return InitializeTemporary(TypeInfo, TypeInfo->getType());
+  static InitializedEntity InitializeTemporary(ASTContext &Context,
+                                               TypeSourceInfo *TypeInfo) {
+    QualType Type = TypeInfo->getType();
+    if (Context.getLangOpts().OpenCLCPlusPlus) {
+      assert(!Type.hasAddressSpace() && "Temporary already has address 
space!");
+      Type = Context.getAddrSpaceQualType(Type, LangAS::opencl_private);
+    }
+
+    return InitializeTemporary(TypeInfo, Type);
   }
 
   /// Create the initialization entity for a temporary.

diff  --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 7627ed4f37d52..e76c4509d7888 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -1453,7 +1453,8 @@ Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo,
          "List initialization must have initializer list as expression.");
   SourceRange FullRange = SourceRange(TyBeginLoc, RParenOrBraceLoc);
 
-  InitializedEntity Entity = InitializedEntity::InitializeTemporary(TInfo);
+  InitializedEntity Entity =
+      InitializedEntity::InitializeTemporary(Context, TInfo);
   InitializationKind Kind =
       Exprs.size()
           ? ListInitialization
@@ -5290,7 +5291,8 @@ static bool evaluateTypeTrait(Sema &S, TypeTrait Kind, 
SourceLocation KWLoc,
         S, Sema::ExpressionEvaluationContext::Unevaluated);
     Sema::SFINAETrap SFINAE(S, /*AccessCheckingSFINAE=*/true);
     Sema::ContextRAII TUContext(S, S.Context.getTranslationUnitDecl());
-    InitializedEntity To(InitializedEntity::InitializeTemporary(Args[0]));
+    InitializedEntity To(
+        InitializedEntity::InitializeTemporary(S.Context, Args[0]));
     InitializationKind InitKind(InitializationKind::CreateDirect(KWLoc, KWLoc,
                                                                  RParenLoc));
     InitializationSequence Init(S, To, InitKind, ArgExprs);

diff  --git a/clang/test/SemaOpenCLCXX/addrspace-constructors.clcpp 
b/clang/test/SemaOpenCLCXX/addrspace-constructors.clcpp
index 0605a48d0fe91..1b97484767b1a 100644
--- a/clang/test/SemaOpenCLCXX/addrspace-constructors.clcpp
+++ b/clang/test/SemaOpenCLCXX/addrspace-constructors.clcpp
@@ -32,6 +32,8 @@ kernel void k() {
   __local X lx;
   __private X x;
 
+  __private X tx = X();
+
   __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'}}

diff  --git a/clang/test/SemaOpenCLCXX/temporaries.clcpp 
b/clang/test/SemaOpenCLCXX/temporaries.clcpp
new file mode 100644
index 0000000000000..37d4f17183210
--- /dev/null
+++ b/clang/test/SemaOpenCLCXX/temporaries.clcpp
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 %s -pedantic -ast-dump | FileCheck %s
+
+struct X {
+  X() __private = default;
+};
+
+// CHECK: VarDecl {{.*}} gx
+// CHECK: CXXTemporaryObjectExpr {{.*}} '__private X'
+__global X gx = X();
+
+void k() {
+  // CHECK: VarDecl {{.*}} x1
+  // CHECK: CXXTemporaryObjectExpr {{.*}} '__private X'
+  X x1 = X();
+
+  // CHECK: VarDecl {{.*}} x2
+  // CHECK: CXXConstructExpr {{.*}} 'const __private X'
+  const X x2;
+}


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to